summaryrefslogtreecommitdiffstatshomepage
path: root/tests/wipy/uart_irq.py
blob: e631e46a9c2fae15bd78137f9ded948d14a95ceb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"""
UART IRQ test for the CC3200 based boards.
"""

from machine import UART
import os
import time

mch = os.uname().machine
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")],
    ]
else:
    raise Exception("Board not supported!")

# just in case we have stdio duplicated on any of the uarts
os.dupterm(None)

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


def uart0_handler(uart_o):
    global uart0_irq
    global uart0_int_count
    if uart0_irq.flags() & UART.RX_ANY:
        uart0_int_count += 1


def uart1_handler(uart_o):
    global uart1_irq
    global uart1_int_count
    if uart1_irq.flags() & UART.RX_ANY:
        uart1_int_count += 1


uart0_irq = uart0.irq(trigger=UART.RX_ANY, handler=uart0_handler)
uart1_irq = uart1.irq(trigger=UART.RX_ANY, handler=uart1_handler)

uart0.write(b"123")
# 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)
print(uart1.read() == b"123")

uart1.write(b"12345")
# 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)
print(uart0.read() == b"12345")

# do it again
uart1_int_count = 0
uart0.write(b"123")
# 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)
print(uart1.read() == b"123")

# disable the interrupt
uart1_irq.disable()
# do it again
uart1_int_count = 0
uart0.write(b"123")
# 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)  # no interrupt triggered this time
print(uart1_irq.flags() == 0)
print(uart0_irq.flags() == 0)
print(uart1.read() == b"123")

# enable the interrupt
uart1_irq.enable()
# do it again
uart1_int_count = 0
uart0.write(b"123")
# 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)
print(uart1.read() == b"123")

uart1_irq.init(trigger=UART.RX_ANY, handler=None)  # No handler
# do it again
uart1_int_count = 0
uart0.write(b"123")
# 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)  # no interrupt handler called
print(uart1_irq.flags() == 0)
print(uart0_irq.flags() == 0)
print(uart1.read() == b"123")

# 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:
    print("Exception")

try:
    uart0_irq = uart0.irq(trigger=0)
except:
    print("Exception")

try:
    uart0_irq = uart0.irq(trigger=UART.RX_ANY, wake=Sleep.SUSPENDED)
except:
    print("Exception")

uart0_irq.disable()
uart1_irq.disable()