2017-02-05 21:48:01 -05:00
|
|
|
try:
|
|
|
|
from pyb import CAN
|
|
|
|
except ImportError:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("SKIP")
|
2017-06-10 13:14:16 -04:00
|
|
|
raise SystemExit
|
2017-02-05 21:48:01 -05:00
|
|
|
|
2018-03-19 00:15:39 -04:00
|
|
|
from array import array
|
|
|
|
import micropython
|
2015-04-16 18:52:43 -04:00
|
|
|
import pyb
|
2014-10-02 12:30:28 -04:00
|
|
|
|
2019-12-13 00:43:17 -05:00
|
|
|
# test we can correctly create by id (2 handled in can2.py test)
|
|
|
|
for bus in (-1, 0, 1, 3):
|
2015-05-28 06:06:12 -04:00
|
|
|
try:
|
|
|
|
CAN(bus, CAN.LOOPBACK)
|
|
|
|
print("CAN", bus)
|
|
|
|
except ValueError:
|
|
|
|
print("ValueError", bus)
|
2016-05-06 13:11:49 -04:00
|
|
|
CAN(1).deinit()
|
2015-05-28 06:06:12 -04:00
|
|
|
|
2014-11-21 17:24:23 -05:00
|
|
|
CAN.initfilterbanks(14)
|
2014-10-30 19:13:53 -04:00
|
|
|
can = CAN(1)
|
|
|
|
print(can)
|
|
|
|
|
2018-03-19 00:15:39 -04:00
|
|
|
# Test state when de-init'd
|
|
|
|
print(can.state() == can.STOPPED)
|
|
|
|
|
2014-10-30 19:13:53 -04:00
|
|
|
can.init(CAN.LOOPBACK)
|
2014-10-02 12:30:28 -04:00
|
|
|
print(can)
|
|
|
|
print(can.any(0))
|
2014-10-30 19:13:53 -04:00
|
|
|
|
2018-03-19 00:15:39 -04:00
|
|
|
# Test state when freshly created
|
|
|
|
print(can.state() == can.ERROR_ACTIVE)
|
|
|
|
|
|
|
|
# Test that restart can be called
|
|
|
|
can.restart()
|
|
|
|
|
|
|
|
# Test info returns a sensible value
|
|
|
|
print(can.info())
|
|
|
|
|
2014-11-21 17:24:23 -05:00
|
|
|
# Catch all filter
|
|
|
|
can.setfilter(0, CAN.MASK16, 0, (0, 0, 0, 0))
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
can.send("abcd", 123, timeout=5000)
|
2018-03-19 00:15:39 -04:00
|
|
|
print(can.any(0), can.info())
|
2014-10-02 12:30:28 -04:00
|
|
|
print(can.recv(0))
|
2014-10-30 18:13:26 -04:00
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
can.send("abcd", -1, timeout=5000)
|
2014-10-30 18:13:26 -04:00
|
|
|
print(can.recv(0))
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
can.send("abcd", 0x7FF + 1, timeout=5000)
|
2014-10-30 18:13:26 -04:00
|
|
|
print(can.recv(0))
|
|
|
|
|
2014-11-21 17:24:23 -05:00
|
|
|
# Test too long message
|
2014-10-30 18:13:26 -04:00
|
|
|
try:
|
2020-03-22 22:26:08 -04:00
|
|
|
can.send("abcdefghi", 0x7FF, timeout=5000)
|
2014-10-30 18:13:26 -04:00
|
|
|
except ValueError:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("passed")
|
2014-10-30 18:13:26 -04:00
|
|
|
else:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("failed")
|
2014-10-30 18:13:26 -04:00
|
|
|
|
2018-03-19 00:15:39 -04:00
|
|
|
# Test that recv can work without allocating memory on the heap
|
|
|
|
|
|
|
|
buf = bytearray(10)
|
|
|
|
l = [0, 0, 0, memoryview(buf)]
|
|
|
|
l2 = None
|
|
|
|
|
|
|
|
micropython.heap_lock()
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
can.send("", 42)
|
2018-03-19 00:15:39 -04:00
|
|
|
l2 = can.recv(0, l)
|
|
|
|
assert l is l2
|
|
|
|
print(l, len(l[3]), buf)
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
can.send("1234", 42)
|
2018-03-19 00:15:39 -04:00
|
|
|
l2 = can.recv(0, l)
|
|
|
|
assert l is l2
|
|
|
|
print(l, len(l[3]), buf)
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
can.send("01234567", 42)
|
2018-03-19 00:15:39 -04:00
|
|
|
l2 = can.recv(0, l)
|
|
|
|
assert l is l2
|
|
|
|
print(l, len(l[3]), buf)
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
can.send("abc", 42)
|
2018-03-19 00:15:39 -04:00
|
|
|
l2 = can.recv(0, l)
|
|
|
|
assert l is l2
|
|
|
|
print(l, len(l[3]), buf)
|
|
|
|
|
|
|
|
micropython.heap_unlock()
|
|
|
|
|
|
|
|
# Test that recv can work with different arrays behind the memoryview
|
2020-03-22 22:26:08 -04:00
|
|
|
can.send("abc", 1)
|
|
|
|
print(bytes(can.recv(0, [0, 0, 0, memoryview(array("B", range(8)))])[3]))
|
|
|
|
can.send("def", 1)
|
|
|
|
print(bytes(can.recv(0, [0, 0, 0, memoryview(array("b", range(8)))])[3]))
|
2018-03-19 00:15:39 -04:00
|
|
|
|
|
|
|
# Test for non-list passed as second arg to recv
|
2020-03-22 22:26:08 -04:00
|
|
|
can.send("abc", 1)
|
2018-03-19 00:15:39 -04:00
|
|
|
try:
|
|
|
|
can.recv(0, 1)
|
|
|
|
except TypeError:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("TypeError")
|
2018-03-19 00:15:39 -04:00
|
|
|
|
|
|
|
# Test for too-short-list passed as second arg to recv
|
2020-03-22 22:26:08 -04:00
|
|
|
can.send("abc", 1)
|
2018-03-19 00:15:39 -04:00
|
|
|
try:
|
|
|
|
can.recv(0, [0, 0, 0])
|
|
|
|
except ValueError:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("ValueError")
|
2018-03-19 00:15:39 -04:00
|
|
|
|
|
|
|
# Test for non-memoryview passed as 4th element to recv
|
2020-03-22 22:26:08 -04:00
|
|
|
can.send("abc", 1)
|
2018-03-19 00:15:39 -04:00
|
|
|
try:
|
|
|
|
can.recv(0, [0, 0, 0, 0])
|
|
|
|
except TypeError:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("TypeError")
|
2018-03-19 00:15:39 -04:00
|
|
|
|
|
|
|
# Test for read-only-memoryview passed as 4th element to recv
|
2020-03-22 22:26:08 -04:00
|
|
|
can.send("abc", 1)
|
2018-03-19 00:15:39 -04:00
|
|
|
try:
|
|
|
|
can.recv(0, [0, 0, 0, memoryview(bytes(8))])
|
|
|
|
except ValueError:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("ValueError")
|
2018-03-19 00:15:39 -04:00
|
|
|
|
|
|
|
# Test for bad-typecode-memoryview passed as 4th element to recv
|
2020-03-22 22:26:08 -04:00
|
|
|
can.send("abc", 1)
|
2018-03-19 00:15:39 -04:00
|
|
|
try:
|
2020-03-22 22:26:08 -04:00
|
|
|
can.recv(0, [0, 0, 0, memoryview(array("i", range(8)))])
|
2018-03-19 00:15:39 -04:00
|
|
|
except ValueError:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("ValueError")
|
2018-03-19 00:15:39 -04:00
|
|
|
|
2014-10-30 18:13:26 -04:00
|
|
|
del can
|
|
|
|
|
2014-11-21 17:24:23 -05:00
|
|
|
# Testing extended IDs
|
2020-03-22 22:26:08 -04:00
|
|
|
can = CAN(1, CAN.LOOPBACK, extframe=True)
|
2014-11-21 17:24:23 -05:00
|
|
|
# Catch all filter
|
|
|
|
can.setfilter(0, CAN.MASK32, 0, (0, 0))
|
|
|
|
|
2014-10-30 18:13:26 -04:00
|
|
|
print(can)
|
|
|
|
|
|
|
|
try:
|
2020-03-22 22:26:08 -04:00
|
|
|
can.send("abcde", 0x7FF + 1, timeout=5000)
|
2014-10-30 18:13:26 -04:00
|
|
|
except ValueError:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("failed")
|
2014-10-30 18:13:26 -04:00
|
|
|
else:
|
|
|
|
r = can.recv(0)
|
2020-03-22 22:26:08 -04:00
|
|
|
if r[0] == 0x7FF + 1 and r[3] == b"abcde":
|
|
|
|
print("passed")
|
2014-10-30 18:13:26 -04:00
|
|
|
else:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("failed, wrong data received")
|
2015-01-15 17:16:57 -05:00
|
|
|
|
2015-11-01 17:30:46 -05:00
|
|
|
# Test filters
|
|
|
|
for n in [0, 8, 16, 24]:
|
|
|
|
filter_id = 0b00001000 << n
|
|
|
|
filter_mask = 0b00011100 << n
|
|
|
|
id_ok = 0b00001010 << n
|
|
|
|
id_fail = 0b00011010 << n
|
|
|
|
|
|
|
|
can.clearfilter(0)
|
|
|
|
can.setfilter(0, pyb.CAN.MASK32, 0, (filter_id, filter_mask))
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
can.send("ok", id_ok, timeout=3)
|
2015-11-01 17:30:46 -05:00
|
|
|
if can.any(0):
|
|
|
|
msg = can.recv(0)
|
|
|
|
print((hex(filter_id), hex(filter_mask), hex(msg[0]), msg[3]))
|
|
|
|
|
|
|
|
can.send("fail", id_fail, timeout=3)
|
|
|
|
if can.any(0):
|
|
|
|
msg = can.recv(0)
|
|
|
|
print((hex(filter_id), hex(filter_mask), hex(msg[0]), msg[3]))
|
|
|
|
|
2015-01-15 17:16:57 -05:00
|
|
|
del can
|
|
|
|
|
|
|
|
# Test RxCallbacks
|
|
|
|
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))
|
2020-03-22 22:26:08 -04:00
|
|
|
|
|
|
|
|
2015-01-15 17:16:57 -05:00
|
|
|
def cb0(bus, reason):
|
2020-03-22 22:26:08 -04:00
|
|
|
print("cb0")
|
2015-01-15 17:16:57 -05:00
|
|
|
if reason == 0:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("pending")
|
2015-01-15 17:16:57 -05:00
|
|
|
if reason == 1:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("full")
|
2015-01-15 17:16:57 -05:00
|
|
|
if reason == 2:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("overflow")
|
|
|
|
|
2015-01-15 17:16:57 -05:00
|
|
|
|
|
|
|
def cb1(bus, reason):
|
2020-03-22 22:26:08 -04:00
|
|
|
print("cb1")
|
2015-01-15 17:16:57 -05:00
|
|
|
if reason == 0:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("pending")
|
2015-01-15 17:16:57 -05:00
|
|
|
if reason == 1:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("full")
|
2015-01-15 17:16:57 -05:00
|
|
|
if reason == 2:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("overflow")
|
|
|
|
|
2015-01-15 17:16:57 -05:00
|
|
|
|
|
|
|
def cb0a(bus, reason):
|
2020-03-22 22:26:08 -04:00
|
|
|
print("cb0a")
|
2015-01-15 17:16:57 -05:00
|
|
|
if reason == 0:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("pending")
|
2015-01-15 17:16:57 -05:00
|
|
|
if reason == 1:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("full")
|
2015-01-15 17:16:57 -05:00
|
|
|
if reason == 2:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("overflow")
|
|
|
|
|
2015-01-15 17:16:57 -05:00
|
|
|
|
|
|
|
def cb1a(bus, reason):
|
2020-03-22 22:26:08 -04:00
|
|
|
print("cb1a")
|
2015-01-15 17:16:57 -05:00
|
|
|
if reason == 0:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("pending")
|
2015-01-15 17:16:57 -05:00
|
|
|
if reason == 1:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("full")
|
2015-01-15 17:16:57 -05:00
|
|
|
if reason == 2:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("overflow")
|
2015-01-15 17:16:57 -05:00
|
|
|
|
|
|
|
|
|
|
|
can.rxcallback(0, cb0)
|
|
|
|
can.rxcallback(1, cb1)
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
can.send("11111111", 1, timeout=5000)
|
|
|
|
can.send("22222222", 2, timeout=5000)
|
|
|
|
can.send("33333333", 3, timeout=5000)
|
2015-01-15 17:16:57 -05:00
|
|
|
can.rxcallback(0, cb0a)
|
2020-03-22 22:26:08 -04:00
|
|
|
can.send("44444444", 4, timeout=5000)
|
2015-01-15 17:16:57 -05:00
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
can.send("55555555", 5, timeout=5000)
|
|
|
|
can.send("66666666", 6, timeout=5000)
|
|
|
|
can.send("77777777", 7, timeout=5000)
|
2015-01-15 17:16:57 -05:00
|
|
|
can.rxcallback(1, cb1a)
|
2020-03-22 22:26:08 -04:00
|
|
|
can.send("88888888", 8, timeout=5000)
|
2015-01-15 17:16:57 -05:00
|
|
|
|
|
|
|
print(can.recv(0))
|
|
|
|
print(can.recv(0))
|
|
|
|
print(can.recv(0))
|
|
|
|
print(can.recv(1))
|
|
|
|
print(can.recv(1))
|
|
|
|
print(can.recv(1))
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
can.send("11111111", 1, timeout=5000)
|
|
|
|
can.send("55555555", 5, timeout=5000)
|
2015-01-15 17:16:57 -05:00
|
|
|
|
|
|
|
print(can.recv(0))
|
|
|
|
print(can.recv(1))
|
|
|
|
|
2015-04-16 18:52:43 -04:00
|
|
|
del can
|
|
|
|
|
2017-05-29 03:08:14 -04:00
|
|
|
# Testing asynchronous send
|
2015-04-16 18:52:43 -04:00
|
|
|
can = CAN(1, CAN.LOOPBACK)
|
|
|
|
can.setfilter(0, CAN.MASK16, 0, (0, 0, 0, 0))
|
|
|
|
|
|
|
|
while can.any(0):
|
|
|
|
can.recv(0)
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
can.send("abcde", 1, timeout=0)
|
2015-04-16 18:52:43 -04:00
|
|
|
print(can.any(0))
|
|
|
|
while not can.any(0):
|
|
|
|
pass
|
|
|
|
|
|
|
|
print(can.recv(0))
|
|
|
|
|
|
|
|
try:
|
2020-03-22 22:26:08 -04:00
|
|
|
can.send("abcde", 2, timeout=0)
|
|
|
|
can.send("abcde", 3, timeout=0)
|
|
|
|
can.send("abcde", 4, timeout=0)
|
|
|
|
can.send("abcde", 5, timeout=0)
|
2015-04-16 18:52:43 -04:00
|
|
|
except OSError as e:
|
2020-03-22 22:26:08 -04:00
|
|
|
if str(e) == "16":
|
|
|
|
print("passed")
|
2015-04-16 18:52:43 -04:00
|
|
|
else:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("failed")
|
2015-04-16 18:52:43 -04:00
|
|
|
|
|
|
|
pyb.delay(500)
|
|
|
|
while can.any(0):
|
|
|
|
print(can.recv(0))
|
2015-04-17 15:15:26 -04:00
|
|
|
|
|
|
|
# Testing rtr messages
|
|
|
|
bus1 = CAN(1, CAN.LOOPBACK)
|
|
|
|
while bus1.any(0):
|
|
|
|
bus1.recv(0)
|
|
|
|
bus1.setfilter(0, CAN.LIST16, 0, (1, 2, 3, 4))
|
|
|
|
bus1.setfilter(1, CAN.LIST16, 0, (5, 6, 7, 8), rtr=(True, True, True, True))
|
|
|
|
bus1.setfilter(2, CAN.MASK16, 0, (64, 64, 32, 32), rtr=(False, True))
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
bus1.send("", 1, rtr=True)
|
2015-04-17 15:15:26 -04:00
|
|
|
print(bus1.any(0))
|
2020-03-22 22:26:08 -04:00
|
|
|
bus1.send("", 5, rtr=True)
|
2015-04-17 15:15:26 -04:00
|
|
|
print(bus1.recv(0))
|
2020-03-22 22:26:08 -04:00
|
|
|
bus1.send("", 6, rtr=True)
|
2015-04-17 15:15:26 -04:00
|
|
|
print(bus1.recv(0))
|
2020-03-22 22:26:08 -04:00
|
|
|
bus1.send("", 7, rtr=True)
|
2015-04-17 15:15:26 -04:00
|
|
|
print(bus1.recv(0))
|
2020-03-22 22:26:08 -04:00
|
|
|
bus1.send("", 16, rtr=True)
|
2015-04-17 15:15:26 -04:00
|
|
|
print(bus1.any(0))
|
2020-03-22 22:26:08 -04:00
|
|
|
bus1.send("", 32, rtr=True)
|
2015-04-17 15:15:26 -04:00
|
|
|
print(bus1.recv(0))
|
|
|
|
|
2019-12-13 00:43:17 -05:00
|
|
|
# test HAL error, timeout
|
|
|
|
can = pyb.CAN(1, pyb.CAN.NORMAL)
|
|
|
|
try:
|
2020-03-22 22:26:08 -04:00
|
|
|
can.send("1", 1, timeout=50)
|
2019-12-13 00:43:17 -05:00
|
|
|
except OSError as e:
|
|
|
|
print(repr(e))
|