1""" 2UART test for the CC3200 based boards. 3UART0 and UART1 must be connected together for this test to pass. 4""" 5 6from machine import UART 7from machine import Pin 8import os 9import time 10 11mch = os.uname().machine 12if "LaunchPad" in mch: 13 uart_id_range = range(0, 2) 14 uart_pins = [ 15 [("GP12", "GP13"), ("GP12", "GP13", "GP7", "GP6")], 16 [("GP16", "GP17"), ("GP16", "GP17", "GP7", "GP6")], 17 ] 18elif "WiPy" in mch: 19 uart_id_range = range(0, 2) 20 uart_pins = [ 21 [("GP12", "GP13"), ("GP12", "GP13", "GP7", "GP6")], 22 [("GP16", "GP17"), ("GP16", "GP17", "GP7", "GP6")], 23 ] 24else: 25 raise Exception("Board not supported!") 26 27# just in case we have the repl duplicated on any of the uarts 28os.dupterm(None) 29 30for uart_id in uart_id_range: 31 uart = UART(uart_id, 38400) 32 print(uart) 33 uart.init(57600, 8, None, 1, pins=uart_pins[uart_id][0]) 34 uart.init(baudrate=9600, stop=2, parity=UART.EVEN, pins=uart_pins[uart_id][1]) 35 uart.init(baudrate=115200, parity=UART.ODD, stop=0, pins=uart_pins[uart_id][0]) 36 uart = UART(baudrate=1000000) 37 uart.sendbreak() 38 39uart = UART(baudrate=1000000) 40uart = UART() 41print(uart) 42uart = UART(baudrate=38400, pins=("GP12", "GP13")) 43print(uart) 44uart = UART(pins=("GP12", "GP13")) 45print(uart) 46uart = UART(pins=(None, "GP17")) 47print(uart) 48uart = UART(baudrate=57600, pins=("GP16", "GP17")) 49print(uart) 50 51# now it's time for some loopback tests between the uarts 52uart0 = UART(0, 1000000, pins=uart_pins[0][0]) 53print(uart0) 54uart1 = UART(1, 1000000, pins=uart_pins[1][0]) 55print(uart1) 56 57print(uart0.write(b"123456") == 6) 58print(uart1.read() == b"123456") 59 60print(uart1.write(b"123") == 3) 61print(uart0.read(1) == b"1") 62print(uart0.read(2) == b"23") 63print(uart0.read() == None) 64 65uart0.write(b"123") 66buf = bytearray(3) 67print(uart1.readinto(buf, 1) == 1) 68print(buf) 69print(uart1.readinto(buf) == 2) 70print(buf) 71 72# try initializing without the id 73uart0 = UART(baudrate=1000000, pins=uart_pins[0][0]) 74uart0.write(b"1234567890") 75time.sleep_ms(2) # because of the fifo interrupt levels 76print(uart1.any() == 10) 77print(uart1.readline() == b"1234567890") 78print(uart1.any() == 0) 79 80uart0.write(b"1234567890") 81print(uart1.read() == b"1234567890") 82 83# tx only mode 84uart0 = UART(0, 1000000, pins=("GP12", None)) 85print(uart0.write(b"123456") == 6) 86print(uart1.read() == b"123456") 87print(uart1.write(b"123") == 3) 88print(uart0.read() == None) 89 90# rx only mode 91uart0 = UART(0, 1000000, pins=(None, "GP13")) 92print(uart0.write(b"123456") == 6) 93print(uart1.read() == None) 94print(uart1.write(b"123") == 3) 95print(uart0.read() == b"123") 96 97# leave pins as they were (rx only mode) 98uart0 = UART(0, 1000000, pins=None) 99print(uart0.write(b"123456") == 6) 100print(uart1.read() == None) 101print(uart1.write(b"123") == 3) 102print(uart0.read() == b"123") 103 104# no pin assignment 105uart0 = UART(0, 1000000, pins=(None, None)) 106print(uart0.write(b"123456789") == 9) 107print(uart1.read() == None) 108print(uart1.write(b"123456789") == 9) 109print(uart0.read() == None) 110print(Pin.board.GP12) 111print(Pin.board.GP13) 112 113# check for memory leaks... 114for i in range(0, 1000): 115 uart0 = UART(0, 1000000) 116 uart1 = UART(1, 1000000) 117 118# next ones must raise 119try: 120 UART(0, 9600, parity=None, pins=("GP12", "GP13", "GP7")) 121except Exception: 122 print("Exception") 123 124try: 125 UART(0, 9600, parity=UART.ODD, pins=("GP12", "GP7")) 126except Exception: 127 print("Exception") 128 129uart0 = UART(0, 1000000) 130uart0.deinit() 131try: 132 uart0.any() 133except Exception: 134 print("Exception") 135 136try: 137 uart0.read() 138except Exception: 139 print("Exception") 140 141try: 142 uart0.write("abc") 143except Exception: 144 print("Exception") 145 146try: 147 uart0.sendbreak("abc") 148except Exception: 149 print("Exception") 150 151try: 152 UART(2, 9600) 153except Exception: 154 print("Exception") 155 156for uart_id in uart_id_range: 157 uart = UART(uart_id, 1000000) 158 uart.deinit() 159 # test printing an unitialized uart 160 print(uart) 161 # initialize it back and check that it works again 162 uart.init(115200) 163 print(uart) 164 uart.read() 165