Merge pull request #1293 from jepler/jepler-feathercan
Add examples for upcoming Adafruit Feather M4 CAN guide
This commit is contained in:
commit
d18e18a91b
4 changed files with 182 additions and 0 deletions
47
FeatherCAN_CircuitPython/listener-ack.py
Normal file
47
FeatherCAN_CircuitPython/listener-ack.py
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
import struct
|
||||||
|
|
||||||
|
import board
|
||||||
|
import canio
|
||||||
|
import digitalio
|
||||||
|
|
||||||
|
# If the CAN transceiver has a standby pin, bring it out of standby mode
|
||||||
|
if hasattr(board, 'CAN_STANDBY'):
|
||||||
|
standby = digitalio.DigitalInOut(board.CAN_STANDBY)
|
||||||
|
standby.switch_to_output(False)
|
||||||
|
|
||||||
|
# If the CAN transceiver is powered by a boost converter, turn on its supply
|
||||||
|
if hasattr(board, 'BOOST_ENABLE'):
|
||||||
|
standby = digitalio.DigitalInOut(board.CAN_STANDBY)
|
||||||
|
standby.switch_to_output(True)
|
||||||
|
|
||||||
|
can = canio.CAN(rx=board.CAN_RX, tx=board.CAN_TX, baudrate=250_000, auto_restart=True)
|
||||||
|
listener = can.listen(matches=[canio.Match(0x408)], timeout=.9)
|
||||||
|
|
||||||
|
old_bus_state = None
|
||||||
|
old_count = -1
|
||||||
|
|
||||||
|
while True:
|
||||||
|
bus_state = can.state
|
||||||
|
if bus_state != old_bus_state:
|
||||||
|
print(f"Bus state changed to {bus_state}")
|
||||||
|
old_bus_state = bus_state
|
||||||
|
|
||||||
|
message = listener.receive()
|
||||||
|
if message is None:
|
||||||
|
print("No messsage received within timeout")
|
||||||
|
continue
|
||||||
|
|
||||||
|
data = message.data
|
||||||
|
if len(data) != 8:
|
||||||
|
print(f"Unusual message length {len(data)}")
|
||||||
|
continue
|
||||||
|
|
||||||
|
count, now_ms = struct.unpack("<II", data)
|
||||||
|
gap = count - old_count
|
||||||
|
old_count = count
|
||||||
|
print(f"received message: id={message.id:x} count={count} now_ms={now_ms}")
|
||||||
|
if gap != 1:
|
||||||
|
print(f"gap: {gap}")
|
||||||
|
|
||||||
|
print("Sending ACK")
|
||||||
|
can.send(canio.Message(id=0x409, data=struct.pack("<I", count)))
|
||||||
44
FeatherCAN_CircuitPython/listener.py
Normal file
44
FeatherCAN_CircuitPython/listener.py
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
import struct
|
||||||
|
|
||||||
|
import board
|
||||||
|
import canio
|
||||||
|
import digitalio
|
||||||
|
|
||||||
|
# If the CAN transceiver has a standby pin, bring it out of standby mode
|
||||||
|
if hasattr(board, 'CAN_STANDBY'):
|
||||||
|
standby = digitalio.DigitalInOut(board.CAN_STANDBY)
|
||||||
|
standby.switch_to_output(False)
|
||||||
|
|
||||||
|
# If the CAN transceiver is powered by a boost converter, turn on its supply
|
||||||
|
if hasattr(board, 'BOOST_ENABLE'):
|
||||||
|
standby = digitalio.DigitalInOut(board.CAN_STANDBY)
|
||||||
|
standby.switch_to_output(True)
|
||||||
|
|
||||||
|
can = canio.CAN(rx=board.CAN_RX, tx=board.CAN_TX, baudrate=250_000, auto_restart=True)
|
||||||
|
listener = can.listen(matches=[canio.Match(0x408)], timeout=.9)
|
||||||
|
|
||||||
|
old_bus_state = None
|
||||||
|
old_count = -1
|
||||||
|
|
||||||
|
while True:
|
||||||
|
bus_state = can.state
|
||||||
|
if bus_state != old_bus_state:
|
||||||
|
print(f"Bus state changed to {bus_state}")
|
||||||
|
old_bus_state = bus_state
|
||||||
|
|
||||||
|
message = listener.receive()
|
||||||
|
if message is None:
|
||||||
|
print("No messsage received within timeout")
|
||||||
|
continue
|
||||||
|
|
||||||
|
data = message.data
|
||||||
|
if len(data) != 8:
|
||||||
|
print(f"Unusual message length {len(data)}")
|
||||||
|
continue
|
||||||
|
|
||||||
|
count, now_ms = struct.unpack("<II", data)
|
||||||
|
gap = count - old_count
|
||||||
|
old_count = count
|
||||||
|
print(f"received message: count={count} now_ms={now_ms}")
|
||||||
|
if gap != 1:
|
||||||
|
print(f"gap: {gap}")
|
||||||
55
FeatherCAN_CircuitPython/sender-ack.py
Normal file
55
FeatherCAN_CircuitPython/sender-ack.py
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
import struct
|
||||||
|
import time
|
||||||
|
|
||||||
|
import board
|
||||||
|
import canio
|
||||||
|
import digitalio
|
||||||
|
|
||||||
|
# If the CAN transceiver has a standby pin, bring it out of standby mode
|
||||||
|
if hasattr(board, 'CAN_STANDBY'):
|
||||||
|
standby = digitalio.DigitalInOut(board.CAN_STANDBY)
|
||||||
|
standby.switch_to_output(False)
|
||||||
|
|
||||||
|
# If the CAN transceiver is powered by a boost converter, turn on its supply
|
||||||
|
if hasattr(board, 'BOOST_ENABLE'):
|
||||||
|
standby = digitalio.DigitalInOut(board.CAN_STANDBY)
|
||||||
|
standby.switch_to_output(True)
|
||||||
|
|
||||||
|
can = canio.CAN(rx=board.CAN_RX, tx=board.CAN_TX, baudrate=250_000, auto_restart=True)
|
||||||
|
listener = can.listen(matches=[canio.Match(0x409)], timeout=.1)
|
||||||
|
|
||||||
|
old_bus_state = None
|
||||||
|
count = 0
|
||||||
|
|
||||||
|
while True:
|
||||||
|
bus_state = can.state
|
||||||
|
if bus_state != old_bus_state:
|
||||||
|
print(f"Bus state changed to {bus_state}")
|
||||||
|
old_bus_state = bus_state
|
||||||
|
|
||||||
|
now_ms = (time.monotonic_ns() // 1_000_000) & 0xffffffff
|
||||||
|
print(f"Sending message: count={count} now_ms={now_ms}")
|
||||||
|
|
||||||
|
message = canio.Message(id=0x408, data=struct.pack("<II", count, now_ms))
|
||||||
|
while True:
|
||||||
|
can.send(message)
|
||||||
|
|
||||||
|
message_in = listener.receive()
|
||||||
|
if message_in is None:
|
||||||
|
print("No ACK received within timeout")
|
||||||
|
continue
|
||||||
|
|
||||||
|
data = message_in.data
|
||||||
|
if len(data) != 4:
|
||||||
|
print(f"Unusual message length {len(data)}")
|
||||||
|
continue
|
||||||
|
|
||||||
|
ack_count = struct.unpack("<I", data)[0]
|
||||||
|
if ack_count == count:
|
||||||
|
print(f"Received ACK")
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
print(f"Received incorrect ACK: {ack_count} should be {count}")
|
||||||
|
|
||||||
|
time.sleep(.5)
|
||||||
|
count += 1
|
||||||
36
FeatherCAN_CircuitPython/sender.py
Normal file
36
FeatherCAN_CircuitPython/sender.py
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
import struct
|
||||||
|
import time
|
||||||
|
|
||||||
|
import board
|
||||||
|
import canio
|
||||||
|
import digitalio
|
||||||
|
|
||||||
|
# If the CAN transceiver has a standby pin, bring it out of standby mode
|
||||||
|
if hasattr(board, 'CAN_STANDBY'):
|
||||||
|
standby = digitalio.DigitalInOut(board.CAN_STANDBY)
|
||||||
|
standby.switch_to_output(False)
|
||||||
|
|
||||||
|
# If the CAN transceiver is powered by a boost converter, turn on its supply
|
||||||
|
if hasattr(board, 'BOOST_ENABLE'):
|
||||||
|
standby = digitalio.DigitalInOut(board.CAN_STANDBY)
|
||||||
|
standby.switch_to_output(True)
|
||||||
|
|
||||||
|
can = canio.CAN(rx=board.CAN_RX, tx=board.CAN_TX, baudrate=250_000, auto_restart=True)
|
||||||
|
|
||||||
|
old_bus_state = None
|
||||||
|
count = 0
|
||||||
|
|
||||||
|
while True:
|
||||||
|
bus_state = can.state
|
||||||
|
if bus_state != old_bus_state:
|
||||||
|
print(f"Bus state changed to {bus_state}")
|
||||||
|
old_bus_state = bus_state
|
||||||
|
|
||||||
|
now_ms = (time.monotonic_ns() // 1_000_000) & 0xffffffff
|
||||||
|
print(f"Sending message: count={count} now_ms={now_ms}")
|
||||||
|
|
||||||
|
message = canio.Message(id=0x408, data=struct.pack("<II", count, now_ms))
|
||||||
|
can.send(message)
|
||||||
|
|
||||||
|
time.sleep(.5)
|
||||||
|
count += 1
|
||||||
Loading…
Reference in a new issue