adafruit / Adafruit_CircuitPython_MCP2515

A CircuitPython library for working with the MCP2515 CAN bus controller
MIT License
23 stars 14 forks source link

RuntimeError: No Masks Available #23

Open nikstp opened 1 year ago

nikstp commented 1 year ago

Hi,

I'm trying to re-use the code I've had with the M4 CAN Feather boards and it's just not working.

It seems that me having code such as

can_message_one = mcp.listen(matches=[canio.Match(0x18FF0AFB, extended=True)], timeout = 10)
can_message_two = mcp.listen(matches=[canio.Match(0x18FF00FB, extended=True)], timeout = 10)

is causing this issue, but it wasn't an issue before. Doesn't MCP2515 have 2 masks available?

The init code which I'm using with the ESP32S3 Feather (which as I checked, lines up with CS = D5 when stacked):

from adafruit_mcp2515 import canio
from adafruit_mcp2515 import MCP2515 as CAN

...

cs = digitalio.DigitalInOut(board.D5)
cs.switch_to_output()
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
mcp  = CAN(spi, cs, debug=True)
nikstp commented 1 year ago

I was able to accommodate my code with the following changes to the listen function in init.py (since the firmware code isn't designed to support more than one listener):

def listen(self, matches=None, *, timeout: float = 10, set_unused_masks=True):

# if matches were made and there are unused masks
# set the unused masks to prevent them from leaking packets
if(set_unused_masks) is not False:
  if len(matches) > 0 and used_masks < len(MASKS):
      next_mask_index = used_masks
      for idx in range(next_mask_index, len(MASKS)):
          print("using unused mask index:", idx)
          self._create_mask(matches[-1])

Let me know if this could potentially be a PR.