adafruit / Adafruit_Blinka

Add CircuitPython hardware API and libraries to MicroPython & CPython devices
https://learn.adafruit.com/circuitpython-on-raspberrypi-linux
MIT License
457 stars 350 forks source link

Adafruit_CircuitPython_MCP2515 not working correctly #770

Open valtsu23 opened 10 months ago

valtsu23 commented 10 months ago

Board Name

Raspberry Pi 4 4Gb

Steps

Test code to receive Can Bus messages:

import board
import busio
from digitalio import DigitalInOut
import adafruit_mcp2515 as mcp2515

can_bus_states = {0 : "Active", 1 : "Warning", 2 : "Passive", 3 : "Off"}

cs = DigitalInOut(board.D5)
cs.switch_to_output()
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)

can_bus = mcp2515.MCP2515(spi, cs, baudrate=500000, crystal_freq=8000000)
# can_bus_filter = mcp2515.canio.Match(address=0x600, mask=0x5F8, extended=False)
listener = can_bus.listen(timeout=3.0) #matches=[can_bus_filter], timeout=1.0)

old_can_bus_state = None
for x in range(10):
    can_bus_state = can_bus.state
    if old_can_bus_state != can_bus_state:
        print("Can bus state: ", can_bus_states[can_bus_state])
        old_can_bus_state = can_bus_state
    message = listener.receive()
    if message is None:
        print("No message within timeout")
    else:
        print("Id: ", message.id)
        print("Data: ", message.data)

Output from Raspberry Pi:

Can bus state: Active
Id: 6
Data: bytearray(b'\x00')
Id: 2062344
Data: bytearray(b'\x00\x02')
Id: 6
Data: bytearray(b'\x00\x03\x03')
Id: 2062344
Data: bytearray(b'\x00\x04\x04\x00')
Id: 6
Data: bytearray(b'\x00\x05\x05\x00\x00')
...

Message id should be 1536 and data length should be 8 bytes on every message.

Description

It seems that, the first data byte is read as a data length. Message id is also changing between two id:s and both of them are wrong.

Same code works correctly with same MCP2515 board with Feather RP2040. Output using Feather RP2040:

Can bus state: Active
Id: 1536
Data: bytearray(b'\x01\x00\x01\x01\x00\x00\x00\x00')
Id: 1536
Data: bytearray(b'\x02\x00\x02\x02\x00\x00\x00\x00')
Id: 1536
Data: bytearray(b'\x03\x00\x03\x03\x00\x00\x00\x00')
Id: 1536
Data: bytearray(b'\x04\x00\x04\x04\x00\x00\x00\x00')
Id: 1536
Data: bytearray(b'\x05\x00\x05\x05\x00\x00\x00\x00')
...

Picture of connecitions: https://filedn.com/lKOo3aQn9ubHtKC7DXLEkHh/IMG_20231215_221650761.jpg

Additional information

https://forums.adafruit.com/viewtopic.php?t=206724

fly-robin-fly commented 3 months ago

I do have the same issue. Even the simple example of the library docs does not work. Any updates or solutions?