kentindell / canhack

The Yes We CAN project of Canis Labs
MIT License
326 stars 63 forks source link

Create optional Python interrupt callback #17

Closed kentindell closed 1 year ago

kentindell commented 2 years ago

The CAN ISR could call a standard MicroPython interrupt function (which is not permitted to use the heap, directly or indirectly).

The function is called with attributes of the incoming CAN frame (or possibly can make a call to read the front of the FIFO and obtain the CAN ID and payload directly: cannot create a CANFrame because cannot trigger garbage collection).

See here for MicroPythpn interrupts: https://docs.micropython.org/en/latest/reference/isr_rules.html

kentindell commented 1 year ago

Upcoming release of firmware now has a receive callback function. Function is associated when the CAN controller class is created. It is passed a CANFrame instance, which it can act on.

Screenshot 2022-07-29 at 12 59 22

Caveats:

  1. MicroPython ISRs in Python are highly constrained (no heap, for example)
  2. The CANFrame instance has a lifetime for the duration of the callback only: it must extract the data it needs from the instance and must not pass out a reference to the instance.

Remember that the callback is running from inside the ISR and if it executes for too long then this will disrupt CAN frame reception.

beyonlo commented 1 year ago

@kentindell That's a great news!

This will be a amazing new feature for this project :)

Thank you!

kentindell commented 1 year ago

New firmware released (including new source).