2020-03-23 02:26:08 +00:00
|
|
|
"""
|
2015-09-22 22:20:29 +01:00
|
|
|
UART IRQ test for the CC3200 based boards.
|
2020-03-23 02:26:08 +00:00
|
|
|
"""
|
2015-09-22 22:20:29 +01:00
|
|
|
|
2015-09-27 12:45:48 +01:00
|
|
|
from machine import UART
|
2015-09-22 22:20:29 +01:00
|
|
|
import os
|
|
|
|
import time
|
|
|
|
|
2015-09-27 12:45:48 +01:00
|
|
|
mch = os.uname().machine
|
2020-03-23 02:26:08 +00:00
|
|
|
if "LaunchPad" in mch:
|
|
|
|
uart_pins = [
|
|
|
|
[("GP12", "GP13"), ("GP12", "GP13", "GP7", "GP6")],
|
|
|
|
[("GP16", "GP17"), ("GP16", "GP17", "GP7", "GP6")],
|
|
|
|
]
|
|
|
|
elif "WiPy" in mch:
|
|
|
|
uart_pins = [
|
|
|
|
[("GP12", "GP13"), ("GP12", "GP13", "GP7", "GP6")],
|
|
|
|
[("GP16", "GP17"), ("GP16", "GP17", "GP7", "GP6")],
|
|
|
|
]
|
2015-09-22 22:20:29 +01:00
|
|
|
else:
|
2020-03-23 02:26:08 +00:00
|
|
|
raise Exception("Board not supported!")
|
2015-09-22 22:20:29 +01:00
|
|
|
|
|
|
|
# just in case we have stdio duplicated on any of the uarts
|
2015-09-25 14:20:07 +01:00
|
|
|
os.dupterm(None)
|
2015-09-22 22:20:29 +01:00
|
|
|
|
|
|
|
uart0 = UART(0, 1000000, pins=uart_pins[0][0])
|
|
|
|
uart1 = UART(1, 1000000, pins=uart_pins[1][0])
|
|
|
|
|
|
|
|
uart0_int_count = 0
|
|
|
|
uart1_int_count = 0
|
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
|
|
|
|
def uart0_handler(uart_o):
|
2015-09-22 22:20:29 +01:00
|
|
|
global uart0_irq
|
|
|
|
global uart0_int_count
|
2020-03-23 02:26:08 +00:00
|
|
|
if uart0_irq.flags() & UART.RX_ANY:
|
2015-09-22 22:20:29 +01:00
|
|
|
uart0_int_count += 1
|
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
|
|
|
|
def uart1_handler(uart_o):
|
2015-09-22 22:20:29 +01:00
|
|
|
global uart1_irq
|
|
|
|
global uart1_int_count
|
2020-03-23 02:26:08 +00:00
|
|
|
if uart1_irq.flags() & UART.RX_ANY:
|
2015-09-22 22:20:29 +01:00
|
|
|
uart1_int_count += 1
|
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2015-09-22 22:20:29 +01:00
|
|
|
uart0_irq = uart0.irq(trigger=UART.RX_ANY, handler=uart0_handler)
|
|
|
|
uart1_irq = uart1.irq(trigger=UART.RX_ANY, handler=uart1_handler)
|
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
uart0.write(b"123")
|
2015-09-22 22:20:29 +01:00
|
|
|
# wait for the characters to be received
|
|
|
|
while not uart1.any():
|
|
|
|
pass
|
|
|
|
|
|
|
|
time.sleep_us(100)
|
|
|
|
print(uart1.any() == 3)
|
|
|
|
print(uart1_int_count > 0)
|
|
|
|
print(uart1_irq.flags() == 0)
|
|
|
|
print(uart0_irq.flags() == 0)
|
2020-03-23 02:26:08 +00:00
|
|
|
print(uart1.read() == b"123")
|
2015-09-22 22:20:29 +01:00
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
uart1.write(b"12345")
|
2015-09-22 22:20:29 +01:00
|
|
|
# wait for the characters to be received
|
|
|
|
while not uart0.any():
|
|
|
|
pass
|
|
|
|
|
|
|
|
time.sleep_us(100)
|
|
|
|
print(uart0.any() == 5)
|
|
|
|
print(uart0_int_count > 0)
|
|
|
|
print(uart0_irq.flags() == 0)
|
|
|
|
print(uart1_irq.flags() == 0)
|
2020-03-23 02:26:08 +00:00
|
|
|
print(uart0.read() == b"12345")
|
2015-09-22 22:20:29 +01:00
|
|
|
|
|
|
|
# do it again
|
|
|
|
uart1_int_count = 0
|
2020-03-23 02:26:08 +00:00
|
|
|
uart0.write(b"123")
|
2015-09-22 22:20:29 +01:00
|
|
|
# wait for the characters to be received
|
|
|
|
while not uart1.any():
|
|
|
|
pass
|
|
|
|
|
|
|
|
time.sleep_us(100)
|
|
|
|
print(uart1.any() == 3)
|
|
|
|
print(uart1_int_count > 0)
|
|
|
|
print(uart1_irq.flags() == 0)
|
|
|
|
print(uart0_irq.flags() == 0)
|
2020-03-23 02:26:08 +00:00
|
|
|
print(uart1.read() == b"123")
|
2015-09-22 22:20:29 +01:00
|
|
|
|
|
|
|
# disable the interrupt
|
|
|
|
uart1_irq.disable()
|
|
|
|
# do it again
|
|
|
|
uart1_int_count = 0
|
2020-03-23 02:26:08 +00:00
|
|
|
uart0.write(b"123")
|
2015-09-22 22:20:29 +01:00
|
|
|
# wait for the characters to be received
|
|
|
|
while not uart1.any():
|
|
|
|
pass
|
|
|
|
|
|
|
|
time.sleep_us(100)
|
|
|
|
print(uart1.any() == 3)
|
2020-03-23 02:26:08 +00:00
|
|
|
print(uart1_int_count == 0) # no interrupt triggered this time
|
2015-09-22 22:20:29 +01:00
|
|
|
print(uart1_irq.flags() == 0)
|
|
|
|
print(uart0_irq.flags() == 0)
|
2020-03-23 02:26:08 +00:00
|
|
|
print(uart1.read() == b"123")
|
2015-09-22 22:20:29 +01:00
|
|
|
|
|
|
|
# enable the interrupt
|
|
|
|
uart1_irq.enable()
|
|
|
|
# do it again
|
|
|
|
uart1_int_count = 0
|
2020-03-23 02:26:08 +00:00
|
|
|
uart0.write(b"123")
|
2015-09-22 22:20:29 +01:00
|
|
|
# wait for the characters to be received
|
|
|
|
while not uart1.any():
|
|
|
|
pass
|
|
|
|
|
|
|
|
time.sleep_us(100)
|
|
|
|
print(uart1.any() == 3)
|
|
|
|
print(uart1_int_count > 0)
|
|
|
|
print(uart1_irq.flags() == 0)
|
|
|
|
print(uart0_irq.flags() == 0)
|
2020-03-23 02:26:08 +00:00
|
|
|
print(uart1.read() == b"123")
|
2015-09-22 22:20:29 +01:00
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
uart1_irq.init(trigger=UART.RX_ANY, handler=None) # No handler
|
2015-09-22 22:20:29 +01:00
|
|
|
# do it again
|
|
|
|
uart1_int_count = 0
|
2020-03-23 02:26:08 +00:00
|
|
|
uart0.write(b"123")
|
2015-09-22 22:20:29 +01:00
|
|
|
# wait for the characters to be received
|
|
|
|
while not uart1.any():
|
|
|
|
pass
|
|
|
|
|
|
|
|
time.sleep_us(100)
|
|
|
|
print(uart1.any() == 3)
|
2020-03-23 02:26:08 +00:00
|
|
|
print(uart1_int_count == 0) # no interrupt handler called
|
2015-09-22 22:20:29 +01:00
|
|
|
print(uart1_irq.flags() == 0)
|
|
|
|
print(uart0_irq.flags() == 0)
|
2020-03-23 02:26:08 +00:00
|
|
|
print(uart1.read() == b"123")
|
2015-09-22 22:20:29 +01:00
|
|
|
|
|
|
|
# check for memory leaks
|
|
|
|
for i in range(0, 1000):
|
|
|
|
uart0_irq = uart0.irq(trigger=UART.RX_ANY, handler=uart0_handler)
|
|
|
|
uart1_irq = uart1.irq(trigger=UART.RX_ANY, handler=uart1_handler)
|
|
|
|
|
|
|
|
# next ones must raise
|
|
|
|
try:
|
|
|
|
uart0_irq = uart0.irq(trigger=100, handler=uart0_handler)
|
|
|
|
except:
|
2020-03-23 02:26:08 +00:00
|
|
|
print("Exception")
|
2015-09-22 22:20:29 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
uart0_irq = uart0.irq(trigger=0)
|
|
|
|
except:
|
2020-03-23 02:26:08 +00:00
|
|
|
print("Exception")
|
2015-09-22 22:20:29 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
uart0_irq = uart0.irq(trigger=UART.RX_ANY, wake=Sleep.SUSPENDED)
|
|
|
|
except:
|
2020-03-23 02:26:08 +00:00
|
|
|
print("Exception")
|
2015-09-22 22:20:29 +01:00
|
|
|
|
|
|
|
uart0_irq.disable()
|
|
|
|
uart1_irq.disable()
|