Due to high demand expect some shipping delays at this time, orders may not ship for 1-2 business days.

M4 CAN Express example code problem
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

M4 CAN Express example code problem

by valtsu23 on Wed Apr 28, 2021 4:59 am


I have Adafruit Feather M4 CAN Express with ATSAME51 running CircuitPython 6.2.0.
I uploaded Can listener example code: https://learn.adafruit.com/using-canio- ... nd-receive

At first it seems to work fine. I get the message "No messsage received within timeout" when there is nothing connected to Can Bus.
But when I reset the board computer won't see the device any more. I have try'd with Windows and Ubuntu.

Only way I can fix it is to use the safe mode and modify the code.

This problem seems to go away if I import time library and add line "time.sleep(1)" after the line 34 "print("No messsage received within timeout")"

Modified code:
Code: Select all | TOGGLE FULL SIZE
import struct

import board
import canio
import digitalio
import time

# 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)

# If the CAN transceiver is powered by a boost converter, turn on its supply
if hasattr(board, 'BOOST_ENABLE'):
    boost_enable = digitalio.DigitalInOut(board.BOOST_ENABLE)

# Use this line if your board has dedicated CAN pins. (Feather M4 CAN and Feather STM32F405)
can = canio.CAN(rx=board.CAN_RX, tx=board.CAN_TX, baudrate=250_000, auto_restart=True)
# On ESP32S2 most pins can be used for CAN.  Uncomment the following line to use IO5 and IO6
#can = canio.CAN(rx=board.IO6, tx=board.IO5, 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")

    data = message.data
    if len(data) != 8:
        print(f"Unusual message length {len(data)}")

    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}")

Posts: 8
Joined: Tue Mar 12, 2013 2:09 pm

Please be positive and constructive with your questions and comments.