2020-03-23 02:26:08 +00:00
|
|
|
"""
|
2015-09-08 13:19:24 +00:00
|
|
|
UART test for the CC3200 based boards.
|
2015-09-07 07:23:46 +00:00
|
|
|
UART0 and UART1 must be connected together for this test to pass.
|
2020-03-23 02:26:08 +00:00
|
|
|
"""
|
2015-09-07 07:23:46 +00:00
|
|
|
|
2015-09-27 11:45:48 +00:00
|
|
|
from machine import UART
|
|
|
|
from machine import Pin
|
2015-09-07 07:23:46 +00:00
|
|
|
import os
|
2015-09-16 12:09:51 +00:00
|
|
|
import time
|
2015-09-07 07:23:46 +00:00
|
|
|
|
2015-09-27 11:45:48 +00:00
|
|
|
mch = os.uname().machine
|
2020-03-23 02:26:08 +00:00
|
|
|
if "LaunchPad" in mch:
|
2015-09-07 07:23:46 +00:00
|
|
|
uart_id_range = range(0, 2)
|
2020-03-23 02:26:08 +00:00
|
|
|
uart_pins = [
|
|
|
|
[("GP12", "GP13"), ("GP12", "GP13", "GP7", "GP6")],
|
|
|
|
[("GP16", "GP17"), ("GP16", "GP17", "GP7", "GP6")],
|
|
|
|
]
|
|
|
|
elif "WiPy" in mch:
|
2015-09-07 07:23:46 +00:00
|
|
|
uart_id_range = range(0, 2)
|
2020-03-23 02:26:08 +00:00
|
|
|
uart_pins = [
|
|
|
|
[("GP12", "GP13"), ("GP12", "GP13", "GP7", "GP6")],
|
|
|
|
[("GP16", "GP17"), ("GP16", "GP17", "GP7", "GP6")],
|
|
|
|
]
|
2015-09-07 07:23:46 +00:00
|
|
|
else:
|
2020-03-23 02:26:08 +00:00
|
|
|
raise Exception("Board not supported!")
|
2015-09-07 07:23:46 +00:00
|
|
|
|
2015-09-25 13:20:07 +00:00
|
|
|
# just in case we have the repl duplicated on any of the uarts
|
|
|
|
os.dupterm(None)
|
2015-09-09 20:41:32 +00:00
|
|
|
|
2015-09-07 07:23:46 +00:00
|
|
|
for uart_id in uart_id_range:
|
|
|
|
uart = UART(uart_id, 38400)
|
|
|
|
print(uart)
|
2015-09-11 07:33:19 +00:00
|
|
|
uart.init(57600, 8, None, 1, pins=uart_pins[uart_id][0])
|
2015-09-13 13:59:45 +00:00
|
|
|
uart.init(baudrate=9600, stop=2, parity=UART.EVEN, pins=uart_pins[uart_id][1])
|
|
|
|
uart.init(baudrate=115200, parity=UART.ODD, stop=0, pins=uart_pins[uart_id][0])
|
2015-09-11 07:33:19 +00:00
|
|
|
uart = UART(baudrate=1000000)
|
2015-09-07 07:23:46 +00:00
|
|
|
uart.sendbreak()
|
|
|
|
|
2015-09-11 09:05:58 +00:00
|
|
|
uart = UART(baudrate=1000000)
|
2015-09-11 07:33:19 +00:00
|
|
|
uart = UART()
|
|
|
|
print(uart)
|
2020-03-23 02:26:08 +00:00
|
|
|
uart = UART(baudrate=38400, pins=("GP12", "GP13"))
|
2015-09-11 07:33:19 +00:00
|
|
|
print(uart)
|
2020-03-23 02:26:08 +00:00
|
|
|
uart = UART(pins=("GP12", "GP13"))
|
2015-09-11 07:33:19 +00:00
|
|
|
print(uart)
|
2020-03-23 02:26:08 +00:00
|
|
|
uart = UART(pins=(None, "GP17"))
|
2015-09-11 07:33:19 +00:00
|
|
|
print(uart)
|
2020-03-23 02:26:08 +00:00
|
|
|
uart = UART(baudrate=57600, pins=("GP16", "GP17"))
|
2015-09-11 07:33:19 +00:00
|
|
|
print(uart)
|
|
|
|
|
2015-09-07 07:23:46 +00:00
|
|
|
# now it's time for some loopback tests between the uarts
|
|
|
|
uart0 = UART(0, 1000000, pins=uart_pins[0][0])
|
|
|
|
print(uart0)
|
|
|
|
uart1 = UART(1, 1000000, pins=uart_pins[1][0])
|
|
|
|
print(uart1)
|
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
print(uart0.write(b"123456") == 6)
|
|
|
|
print(uart1.read() == b"123456")
|
2015-09-07 07:23:46 +00:00
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
print(uart1.write(b"123") == 3)
|
|
|
|
print(uart0.read(1) == b"1")
|
|
|
|
print(uart0.read(2) == b"23")
|
2015-10-21 12:54:16 +00:00
|
|
|
print(uart0.read() == None)
|
2015-09-07 07:23:46 +00:00
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
uart0.write(b"123")
|
2015-09-07 07:23:46 +00:00
|
|
|
buf = bytearray(3)
|
2020-03-23 02:26:08 +00:00
|
|
|
print(uart1.readinto(buf, 1) == 1)
|
2015-09-07 07:23:46 +00:00
|
|
|
print(buf)
|
|
|
|
print(uart1.readinto(buf) == 2)
|
|
|
|
print(buf)
|
|
|
|
|
2015-09-11 07:33:19 +00:00
|
|
|
# try initializing without the id
|
|
|
|
uart0 = UART(baudrate=1000000, pins=uart_pins[0][0])
|
2020-03-23 02:26:08 +00:00
|
|
|
uart0.write(b"1234567890")
|
|
|
|
time.sleep_ms(2) # because of the fifo interrupt levels
|
2015-09-07 19:19:11 +00:00
|
|
|
print(uart1.any() == 10)
|
2020-03-23 02:26:08 +00:00
|
|
|
print(uart1.readline() == b"1234567890")
|
2015-09-07 07:23:46 +00:00
|
|
|
print(uart1.any() == 0)
|
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
uart0.write(b"1234567890")
|
|
|
|
print(uart1.read() == b"1234567890")
|
2015-09-07 07:23:46 +00:00
|
|
|
|
|
|
|
# tx only mode
|
2020-03-23 02:26:08 +00:00
|
|
|
uart0 = UART(0, 1000000, pins=("GP12", None))
|
|
|
|
print(uart0.write(b"123456") == 6)
|
|
|
|
print(uart1.read() == b"123456")
|
|
|
|
print(uart1.write(b"123") == 3)
|
2015-10-21 12:54:16 +00:00
|
|
|
print(uart0.read() == None)
|
2015-09-07 07:23:46 +00:00
|
|
|
|
|
|
|
# rx only mode
|
2020-03-23 02:26:08 +00:00
|
|
|
uart0 = UART(0, 1000000, pins=(None, "GP13"))
|
|
|
|
print(uart0.write(b"123456") == 6)
|
2015-10-21 12:54:16 +00:00
|
|
|
print(uart1.read() == None)
|
2020-03-23 02:26:08 +00:00
|
|
|
print(uart1.write(b"123") == 3)
|
|
|
|
print(uart0.read() == b"123")
|
2015-09-07 07:23:46 +00:00
|
|
|
|
2015-09-07 19:19:11 +00:00
|
|
|
# leave pins as they were (rx only mode)
|
2015-09-07 08:14:27 +00:00
|
|
|
uart0 = UART(0, 1000000, pins=None)
|
2020-03-23 02:26:08 +00:00
|
|
|
print(uart0.write(b"123456") == 6)
|
2015-10-21 12:54:16 +00:00
|
|
|
print(uart1.read() == None)
|
2020-03-23 02:26:08 +00:00
|
|
|
print(uart1.write(b"123") == 3)
|
|
|
|
print(uart0.read() == b"123")
|
2015-09-07 19:19:11 +00:00
|
|
|
|
2017-05-29 07:08:14 +00:00
|
|
|
# no pin assignment
|
2015-09-07 19:19:11 +00:00
|
|
|
uart0 = UART(0, 1000000, pins=(None, None))
|
2020-03-23 02:26:08 +00:00
|
|
|
print(uart0.write(b"123456789") == 9)
|
2015-10-21 12:54:16 +00:00
|
|
|
print(uart1.read() == None)
|
2020-03-23 02:26:08 +00:00
|
|
|
print(uart1.write(b"123456789") == 9)
|
2015-10-21 12:54:16 +00:00
|
|
|
print(uart0.read() == None)
|
2015-09-07 08:14:27 +00:00
|
|
|
print(Pin.board.GP12)
|
|
|
|
print(Pin.board.GP13)
|
|
|
|
|
2015-09-07 19:19:11 +00:00
|
|
|
# check for memory leaks...
|
2020-03-23 02:26:08 +00:00
|
|
|
for i in range(0, 1000):
|
2015-09-07 19:19:11 +00:00
|
|
|
uart0 = UART(0, 1000000)
|
|
|
|
uart1 = UART(1, 1000000)
|
|
|
|
|
2015-09-07 07:23:46 +00:00
|
|
|
# next ones must raise
|
|
|
|
try:
|
2020-03-23 02:26:08 +00:00
|
|
|
UART(0, 9600, parity=None, pins=("GP12", "GP13", "GP7"))
|
2015-09-07 07:23:46 +00:00
|
|
|
except Exception:
|
2020-03-23 02:26:08 +00:00
|
|
|
print("Exception")
|
2015-09-07 19:19:11 +00:00
|
|
|
|
2015-09-07 07:23:46 +00:00
|
|
|
try:
|
2020-03-23 02:26:08 +00:00
|
|
|
UART(0, 9600, parity=UART.ODD, pins=("GP12", "GP7"))
|
2015-09-07 07:23:46 +00:00
|
|
|
except Exception:
|
2020-03-23 02:26:08 +00:00
|
|
|
print("Exception")
|
2015-09-07 19:19:11 +00:00
|
|
|
|
|
|
|
uart0 = UART(0, 1000000)
|
|
|
|
uart0.deinit()
|
|
|
|
try:
|
|
|
|
uart0.any()
|
|
|
|
except Exception:
|
2020-03-23 02:26:08 +00:00
|
|
|
print("Exception")
|
2015-09-07 19:19:11 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
uart0.read()
|
|
|
|
except Exception:
|
2020-03-23 02:26:08 +00:00
|
|
|
print("Exception")
|
2015-09-07 19:19:11 +00:00
|
|
|
|
|
|
|
try:
|
2020-03-23 02:26:08 +00:00
|
|
|
uart0.write("abc")
|
2015-09-07 19:19:11 +00:00
|
|
|
except Exception:
|
2020-03-23 02:26:08 +00:00
|
|
|
print("Exception")
|
2015-09-07 19:19:11 +00:00
|
|
|
|
|
|
|
try:
|
2020-03-23 02:26:08 +00:00
|
|
|
uart0.sendbreak("abc")
|
2015-09-07 19:19:11 +00:00
|
|
|
except Exception:
|
2020-03-23 02:26:08 +00:00
|
|
|
print("Exception")
|
2015-09-08 13:19:24 +00:00
|
|
|
|
2015-09-11 07:33:19 +00:00
|
|
|
try:
|
|
|
|
UART(2, 9600)
|
|
|
|
except Exception:
|
2020-03-23 02:26:08 +00:00
|
|
|
print("Exception")
|
2015-09-11 07:33:19 +00:00
|
|
|
|
2015-09-08 13:19:24 +00:00
|
|
|
for uart_id in uart_id_range:
|
|
|
|
uart = UART(uart_id, 1000000)
|
|
|
|
uart.deinit()
|
|
|
|
# test printing an unitialized uart
|
|
|
|
print(uart)
|
|
|
|
# initialize it back and check that it works again
|
|
|
|
uart.init(115200)
|
|
|
|
print(uart)
|
|
|
|
uart.read()
|