1""" 2UART IRQ test for the CC3200 based boards. 3""" 4 5from machine import UART 6import os 7import time 8 9mch = os.uname().machine 10if "LaunchPad" in mch: 11 uart_pins = [ 12 [("GP12", "GP13"), ("GP12", "GP13", "GP7", "GP6")], 13 [("GP16", "GP17"), ("GP16", "GP17", "GP7", "GP6")], 14 ] 15elif "WiPy" in mch: 16 uart_pins = [ 17 [("GP12", "GP13"), ("GP12", "GP13", "GP7", "GP6")], 18 [("GP16", "GP17"), ("GP16", "GP17", "GP7", "GP6")], 19 ] 20else: 21 raise Exception("Board not supported!") 22 23# just in case we have stdio duplicated on any of the uarts 24os.dupterm(None) 25 26uart0 = UART(0, 1000000, pins=uart_pins[0][0]) 27uart1 = UART(1, 1000000, pins=uart_pins[1][0]) 28 29uart0_int_count = 0 30uart1_int_count = 0 31 32 33def uart0_handler(uart_o): 34 global uart0_irq 35 global uart0_int_count 36 if uart0_irq.flags() & UART.RX_ANY: 37 uart0_int_count += 1 38 39 40def uart1_handler(uart_o): 41 global uart1_irq 42 global uart1_int_count 43 if uart1_irq.flags() & UART.RX_ANY: 44 uart1_int_count += 1 45 46 47uart0_irq = uart0.irq(trigger=UART.RX_ANY, handler=uart0_handler) 48uart1_irq = uart1.irq(trigger=UART.RX_ANY, handler=uart1_handler) 49 50uart0.write(b"123") 51# wait for the characters to be received 52while not uart1.any(): 53 pass 54 55time.sleep_us(100) 56print(uart1.any() == 3) 57print(uart1_int_count > 0) 58print(uart1_irq.flags() == 0) 59print(uart0_irq.flags() == 0) 60print(uart1.read() == b"123") 61 62uart1.write(b"12345") 63# wait for the characters to be received 64while not uart0.any(): 65 pass 66 67time.sleep_us(100) 68print(uart0.any() == 5) 69print(uart0_int_count > 0) 70print(uart0_irq.flags() == 0) 71print(uart1_irq.flags() == 0) 72print(uart0.read() == b"12345") 73 74# do it again 75uart1_int_count = 0 76uart0.write(b"123") 77# wait for the characters to be received 78while not uart1.any(): 79 pass 80 81time.sleep_us(100) 82print(uart1.any() == 3) 83print(uart1_int_count > 0) 84print(uart1_irq.flags() == 0) 85print(uart0_irq.flags() == 0) 86print(uart1.read() == b"123") 87 88# disable the interrupt 89uart1_irq.disable() 90# do it again 91uart1_int_count = 0 92uart0.write(b"123") 93# wait for the characters to be received 94while not uart1.any(): 95 pass 96 97time.sleep_us(100) 98print(uart1.any() == 3) 99print(uart1_int_count == 0) # no interrupt triggered this time 100print(uart1_irq.flags() == 0) 101print(uart0_irq.flags() == 0) 102print(uart1.read() == b"123") 103 104# enable the interrupt 105uart1_irq.enable() 106# do it again 107uart1_int_count = 0 108uart0.write(b"123") 109# wait for the characters to be received 110while not uart1.any(): 111 pass 112 113time.sleep_us(100) 114print(uart1.any() == 3) 115print(uart1_int_count > 0) 116print(uart1_irq.flags() == 0) 117print(uart0_irq.flags() == 0) 118print(uart1.read() == b"123") 119 120uart1_irq.init(trigger=UART.RX_ANY, handler=None) # No handler 121# do it again 122uart1_int_count = 0 123uart0.write(b"123") 124# wait for the characters to be received 125while not uart1.any(): 126 pass 127 128time.sleep_us(100) 129print(uart1.any() == 3) 130print(uart1_int_count == 0) # no interrupt handler called 131print(uart1_irq.flags() == 0) 132print(uart0_irq.flags() == 0) 133print(uart1.read() == b"123") 134 135# check for memory leaks 136for i in range(0, 1000): 137 uart0_irq = uart0.irq(trigger=UART.RX_ANY, handler=uart0_handler) 138 uart1_irq = uart1.irq(trigger=UART.RX_ANY, handler=uart1_handler) 139 140# next ones must raise 141try: 142 uart0_irq = uart0.irq(trigger=100, handler=uart0_handler) 143except: 144 print("Exception") 145 146try: 147 uart0_irq = uart0.irq(trigger=0) 148except: 149 print("Exception") 150 151try: 152 uart0_irq = uart0.irq(trigger=UART.RX_ANY, wake=Sleep.SUSPENDED) 153except: 154 print("Exception") 155 156uart0_irq.disable() 157uart1_irq.disable() 158