tests/pyb: Update CAN tests to match revised CAN API.

Signed-off-by: Damien George <damien@micropython.org>
pull/6756/merge
Damien George 2022-04-02 22:14:33 +11:00
rodzic 5cdf964571
commit 71344c15f4
4 zmienionych plików z 76 dodań i 62 usunięć

Wyświetl plik

@ -17,14 +17,13 @@ for bus in (-1, 0, 1, 3):
print("ValueError", bus) print("ValueError", bus)
CAN(1).deinit() CAN(1).deinit()
CAN.initfilterbanks(14)
can = CAN(1) can = CAN(1)
print(can) print(can)
# Test state when de-init'd # Test state when de-init'd
print(can.state() == can.STOPPED) print(can.state() == can.STOPPED)
can.init(CAN.LOOPBACK) can.init(CAN.LOOPBACK, num_filter_banks=14)
print(can) print(can)
print(can.any(0)) print(can.any(0))
@ -61,7 +60,7 @@ else:
# Test that recv can work without allocating memory on the heap # Test that recv can work without allocating memory on the heap
buf = bytearray(10) buf = bytearray(10)
l = [0, 0, 0, memoryview(buf)] l = [0, 0, 0, 0, memoryview(buf)]
l2 = None l2 = None
micropython.heap_lock() micropython.heap_lock()
@ -69,30 +68,30 @@ micropython.heap_lock()
can.send("", 42) can.send("", 42)
l2 = can.recv(0, l) l2 = can.recv(0, l)
assert l is l2 assert l is l2
print(l, len(l[3]), buf) print(l, len(l[4]), buf)
can.send("1234", 42) can.send("1234", 42)
l2 = can.recv(0, l) l2 = can.recv(0, l)
assert l is l2 assert l is l2
print(l, len(l[3]), buf) print(l, len(l[4]), buf)
can.send("01234567", 42) can.send("01234567", 42)
l2 = can.recv(0, l) l2 = can.recv(0, l)
assert l is l2 assert l is l2
print(l, len(l[3]), buf) print(l, len(l[4]), buf)
can.send("abc", 42) can.send("abc", 42)
l2 = can.recv(0, l) l2 = can.recv(0, l)
assert l is l2 assert l is l2
print(l, len(l[3]), buf) print(l, len(l[4]), buf)
micropython.heap_unlock() micropython.heap_unlock()
# Test that recv can work with different arrays behind the memoryview # Test that recv can work with different arrays behind the memoryview
can.send("abc", 1) can.send("abc", 1)
print(bytes(can.recv(0, [0, 0, 0, memoryview(array("B", range(8)))])[3])) print(bytes(can.recv(0, [0, 0, 0, 0, memoryview(array("B", range(8)))])[4]))
can.send("def", 1) can.send("def", 1)
print(bytes(can.recv(0, [0, 0, 0, memoryview(array("b", range(8)))])[3])) print(bytes(can.recv(0, [0, 0, 0, 0, memoryview(array("b", range(8)))])[4]))
# Test for non-list passed as second arg to recv # Test for non-list passed as second arg to recv
can.send("abc", 1) can.send("abc", 1)
@ -111,7 +110,7 @@ except ValueError:
# Test for non-memoryview passed as 4th element to recv # Test for non-memoryview passed as 4th element to recv
can.send("abc", 1) can.send("abc", 1)
try: try:
can.recv(0, [0, 0, 0, 0]) can.recv(0, [0, 0, 0, 0, 0])
except TypeError: except TypeError:
print("TypeError") print("TypeError")
@ -132,19 +131,21 @@ except ValueError:
del can del can
# Testing extended IDs # Testing extended IDs
can = CAN(1, CAN.LOOPBACK, extframe=True) print("==== TEST extframe=True ====")
# Catch all filter
can.setfilter(0, CAN.MASK32, 0, (0, 0)) can = CAN(1, CAN.LOOPBACK)
# Catch all filter, but only for extframe's
can.setfilter(0, CAN.MASK32, 0, (0, 0), extframe=True)
print(can) print(can)
try: try:
can.send("abcde", 0x7FF + 1, timeout=5000) can.send("abcde", 0x7FF + 1, timeout=5000, extframe=True)
except ValueError: except ValueError:
print("failed") print("failed")
else: else:
r = can.recv(0) r = can.recv(0)
if r[0] == 0x7FF + 1 and r[3] == b"abcde": if r[0] == 0x7FF + 1 and r[4] == b"abcde":
print("passed") print("passed")
else: else:
print("failed, wrong data received") print("failed, wrong data received")
@ -156,22 +157,24 @@ for n in [0, 8, 16, 24]:
id_ok = 0b00001010 << n id_ok = 0b00001010 << n
id_fail = 0b00011010 << n id_fail = 0b00011010 << n
can.clearfilter(0) can.clearfilter(0, extframe=True)
can.setfilter(0, pyb.CAN.MASK32, 0, (filter_id, filter_mask)) can.setfilter(0, pyb.CAN.MASK32, 0, (filter_id, filter_mask), extframe=True)
can.send("ok", id_ok, timeout=3) can.send("ok", id_ok, timeout=3, extframe=True)
if can.any(0): if can.any(0):
msg = can.recv(0) msg = can.recv(0)
print((hex(filter_id), hex(filter_mask), hex(msg[0]), msg[3])) print((hex(filter_id), hex(filter_mask), hex(msg[0]), msg[1], msg[4]))
can.send("fail", id_fail, timeout=3) can.send("fail", id_fail, timeout=3, extframe=True)
if can.any(0): if can.any(0):
msg = can.recv(0) msg = can.recv(0)
print((hex(filter_id), hex(filter_mask), hex(msg[0]), msg[3])) print((hex(filter_id), hex(filter_mask), hex(msg[0]), msg[1], msg[4]))
del can del can
# Test RxCallbacks # Test RxCallbacks
print("==== TEST rx callbacks ====")
can = CAN(1, CAN.LOOPBACK) can = CAN(1, CAN.LOOPBACK)
can.setfilter(0, CAN.LIST16, 0, (1, 2, 3, 4)) can.setfilter(0, CAN.LIST16, 0, (1, 2, 3, 4))
can.setfilter(1, CAN.LIST16, 1, (5, 6, 7, 8)) can.setfilter(1, CAN.LIST16, 1, (5, 6, 7, 8))
@ -248,6 +251,8 @@ print(can.recv(1))
del can del can
# Testing asynchronous send # Testing asynchronous send
print("==== TEST async send ====")
can = CAN(1, CAN.LOOPBACK) can = CAN(1, CAN.LOOPBACK)
can.setfilter(0, CAN.MASK16, 0, (0, 0, 0, 0)) can.setfilter(0, CAN.MASK16, 0, (0, 0, 0, 0))
@ -277,6 +282,8 @@ while can.any(0):
print(can.recv(0)) print(can.recv(0))
# Testing rtr messages # Testing rtr messages
print("==== TEST rtr messages ====")
bus1 = CAN(1, CAN.LOOPBACK) bus1 = CAN(1, CAN.LOOPBACK)
while bus1.any(0): while bus1.any(0):
bus1.recv(0) bus1.recv(0)
@ -298,6 +305,8 @@ bus1.send("", 32, rtr=True)
print(bus1.recv(0)) print(bus1.recv(0))
# test HAL error, timeout # test HAL error, timeout
print("==== TEST errors ====")
can = pyb.CAN(1, pyb.CAN.NORMAL) can = pyb.CAN(1, pyb.CAN.NORMAL)
try: try:
can.send("1", 1, timeout=50) can.send("1", 1, timeout=50)

Wyświetl plik

@ -4,19 +4,19 @@ CAN 1
ValueError 3 ValueError 3
CAN(1) CAN(1)
True True
CAN(1, CAN.LOOPBACK, extframe=False, auto_restart=False) CAN(1, CAN.LOOPBACK, auto_restart=False)
False False
True True
[0, 0, 0, 0, 0, 0, 0, 0] [0, 0, 0, 0, 0, 0, 0, 0]
True [0, 0, 0, 0, 0, 0, 1, 0] True [0, 0, 0, 0, 0, 0, 1, 0]
(123, False, 0, b'abcd') (123, False, False, 0, b'abcd')
(2047, False, 0, b'abcd') (2047, False, False, 0, b'abcd')
(0, False, 0, b'abcd') (0, False, False, 0, b'abcd')
passed passed
[42, False, 0, <memoryview>] 0 bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') [42, False, False, 0, <memoryview>] 0 bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
[42, False, 0, <memoryview>] 4 bytearray(b'1234\x00\x00\x00\x00\x00\x00') [42, False, False, 0, <memoryview>] 4 bytearray(b'1234\x00\x00\x00\x00\x00\x00')
[42, False, 0, <memoryview>] 8 bytearray(b'01234567\x00\x00') [42, False, False, 0, <memoryview>] 8 bytearray(b'01234567\x00\x00')
[42, False, 0, <memoryview>] 3 bytearray(b'abc34567\x00\x00') [42, False, False, 0, <memoryview>] 3 bytearray(b'abc34567\x00\x00')
b'abc' b'abc'
b'def' b'def'
TypeError TypeError
@ -24,12 +24,14 @@ ValueError
TypeError TypeError
ValueError ValueError
ValueError ValueError
CAN(1, CAN.LOOPBACK, extframe=True, auto_restart=False) ==== TEST extframe=True ====
CAN(1, CAN.LOOPBACK, auto_restart=False)
passed passed
('0x8', '0x1c', '0xa', b'ok') ('0x8', '0x1c', '0xa', True, b'ok')
('0x800', '0x1c00', '0xa00', b'ok') ('0x800', '0x1c00', '0xa00', True, b'ok')
('0x80000', '0x1c0000', '0xa0000', b'ok') ('0x80000', '0x1c0000', '0xa0000', True, b'ok')
('0x8000000', '0x1c000000', '0xa000000', b'ok') ('0x8000000', '0x1c000000', '0xa000000', True, b'ok')
==== TEST rx callbacks ====
cb0 cb0
pending pending
cb0 cb0
@ -42,28 +44,31 @@ cb1
full full
cb1a cb1a
overflow overflow
(1, False, 0, b'11111111') (1, False, False, 0, b'11111111')
(2, False, 1, b'22222222') (2, False, False, 1, b'22222222')
(4, False, 3, b'44444444') (4, False, False, 3, b'44444444')
(5, False, 0, b'55555555') (5, False, False, 0, b'55555555')
(6, False, 1, b'66666666') (6, False, False, 1, b'66666666')
(8, False, 3, b'88888888') (8, False, False, 3, b'88888888')
cb0a cb0a
pending pending
cb1a cb1a
pending pending
(1, False, 0, b'11111111') (1, False, False, 0, b'11111111')
(5, False, 0, b'55555555') (5, False, False, 0, b'55555555')
==== TEST async send ====
False False
(1, False, 0, b'abcde') (1, False, False, 0, b'abcde')
passed passed
(2, False, 0, b'abcde') (2, False, False, 0, b'abcde')
(3, False, 0, b'abcde') (3, False, False, 0, b'abcde')
(4, False, 0, b'abcde') (4, False, False, 0, b'abcde')
==== TEST rtr messages ====
False False
(5, True, 4, b'') (5, False, True, 4, b'')
(6, True, 5, b'') (6, False, True, 5, b'')
(7, True, 6, b'') (7, False, True, 6, b'')
False False
(32, True, 9, b'') (32, False, True, 9, b'')
==== TEST errors ====
OSError(110,) OSError(110,)

Wyświetl plik

@ -7,19 +7,19 @@ except (ImportError, ValueError):
raise SystemExit raise SystemExit
# Testing rtr messages # Testing rtr messages
bus2 = CAN(2, CAN.LOOPBACK, extframe=True) bus2 = CAN(2, CAN.LOOPBACK)
while bus2.any(0): while bus2.any(0):
bus2.recv(0) bus2.recv(0)
bus2.setfilter(0, CAN.LIST32, 0, (1, 2), rtr=(True, True)) bus2.setfilter(0, CAN.LIST32, 0, (1, 2), rtr=(True, True), extframe=True)
bus2.setfilter(1, CAN.LIST32, 0, (3, 4), rtr=(True, False)) bus2.setfilter(1, CAN.LIST32, 0, (3, 4), rtr=(True, False), extframe=True)
bus2.setfilter(2, CAN.MASK32, 0, (16, 16), rtr=(False,)) bus2.setfilter(2, CAN.MASK32, 0, (16, 16), rtr=(False,), extframe=True)
bus2.setfilter(2, CAN.MASK32, 0, (32, 32), rtr=(True,)) bus2.setfilter(2, CAN.MASK32, 0, (32, 32), rtr=(True,), extframe=True)
bus2.send("", 1, rtr=True) bus2.send("", 1, rtr=True, extframe=True)
print(bus2.recv(0)) print(bus2.recv(0))
bus2.send("", 2, rtr=True) bus2.send("", 2, rtr=True, extframe=True)
print(bus2.recv(0)) print(bus2.recv(0))
bus2.send("", 3, rtr=True) bus2.send("", 3, rtr=True, extframe=True)
print(bus2.recv(0)) print(bus2.recv(0))
bus2.send("", 4, rtr=True) bus2.send("", 4, rtr=True, extframe=True)
print(bus2.any(0)) print(bus2.any(0))

Wyświetl plik

@ -1,4 +1,4 @@
(1, True, 0, b'') (1, True, True, 0, b'')
(2, True, 1, b'') (2, True, True, 1, b'')
(3, True, 2, b'') (3, True, True, 2, b'')
False False