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

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2022-04-02 22:14:33 +11:00
parent 5cdf964571
commit 71344c15f4
4 changed files with 76 additions and 62 deletions

View File

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

View File

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

View File

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

View File

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