File tree Expand file tree Collapse file tree 4 files changed +122
-0
lines changed
ports/stm/boards/swan_r5/tests Expand file tree Collapse file tree 4 files changed +122
-0
lines changed Original file line number Diff line number Diff line change 1+ import board
2+ import busio
3+
4+ i2c = busio .I2C (board .SCL , board .SDA )
5+ count = 0
6+
7+ # Wait for I2C lock
8+ while not i2c .try_lock ():
9+ pass
10+
11+ # Scan for devices on the I2C bus
12+ print ("Scanning I2C bus" )
13+ for x in i2c .scan ():
14+ print (hex (x ))
15+ count += 1
16+
17+ print ("%d device(s) found on I2C bus" % count )
18+
19+ # Release the I2C bus
20+ i2c .unlock ()
Original file line number Diff line number Diff line change 1+
2+ import board
3+ import busio
4+ import digitalio
5+
6+ cs = digitalio .DigitalInOut (board .SS )
7+ cs .direction = digitalio .Direction .OUTPUT
8+ cs .value = True
9+
10+ BME680_SPI_REGISTER = 0x73
11+ BME680_CHIPID_REGISTER = 0xD0
12+ BME680_CHIPID = 0x61
13+ SPI_HERZ = 0x500000
14+
15+ spi = busio .SPI (board .SCK , MISO = board .MISO , MOSI = board .MOSI )
16+
17+ def readByte (addr ):
18+ value = - 1
19+ while not spi .try_lock ():
20+ pass
21+ try :
22+ spi .configure (baudrate = 500000 , phase = 0 , polarity = 0 )
23+
24+ cs .value = False
25+ result = bytearray (1 )
26+ result [0 ] = addr | 0x80
27+ spi .write (result )
28+ spi .readinto (result )
29+ value = result [0 ]
30+ return value
31+ finally :
32+ spi .unlock ()
33+ cs .value = True
34+
35+ def writeByte (addr , value ):
36+ while not spi .try_lock ():
37+ pass
38+ try :
39+ spi .configure (baudrate = 500000 , phase = 0 , polarity = 0 )
40+
41+ cs .value = False
42+ result = bytearray (2 )
43+ result [0 ] = addr & ~ 0x80
44+ result [1 ] = value
45+ spi .write (result )
46+ finally :
47+ spi .unlock ()
48+
49+ # put the device in the correct mode to read the ID
50+ reg = readByte (BME680_SPI_REGISTER )
51+ if (reg & 16 )!= 0 :
52+ writeByte (BME680_SPI_REGISTER , reg & ~ 16 )
53+
54+ id = readByte (BME680_CHIPID_REGISTER )
55+
56+ print (f"id is { id } , expected { BME680_CHIPID } " )
Original file line number Diff line number Diff line change 1+ import board
2+ import busio
3+ import digitalio
4+ import usb_cdc
5+ import time
6+
7+ while not usb_cdc .console .in_waiting :
8+ time .sleep (0.1 )
9+
10+ print ("USART test" )
11+
12+ # For most CircuitPython boards:
13+ led = digitalio .DigitalInOut (board .LED )
14+ # For QT Py M0:
15+ # led = digitalio.DigitalInOut(board.SCK)
16+ led .direction = digitalio .Direction .OUTPUT
17+
18+ uart = busio .UART (board .TX , board .RX , baudrate = 9600 )
19+
20+ while True :
21+ data = uart .read (32 ) # read up to 32 bytes
22+ # print(data) # this is a bytearray type
23+
24+ if data is not None :
25+ led .value = True
26+
27+ # convert bytearray to string
28+ data_string = '*' .join ([chr (b ) for b in data ])
29+ print (data_string , end = "" )
30+
31+ led .value = False
32+
33+ if usb_cdc .console .in_waiting :
34+ data = usb_cdc .console .read ()
35+ data_string = '*' .join ([chr (b ) for b in data ])
36+ print ("writing " + data_string )
37+ uart .write (data )
Original file line number Diff line number Diff line change 1+ import os
2+
3+ def main ():
4+ print ("Random number test" )
5+ r = os .urandom (32 )
6+ print (f"urandom TRNG string is { r } " )
7+
8+ main ()
9+
You can’t perform that action at this time.
0 commit comments