KevinOConnor / can2040

Software CAN bus implementation for rp2040 micro-controllers
GNU General Public License v3.0
686 stars 68 forks source link

Add support for can2040_stop() to stop processing of CAN bus messages #42

Closed KevinOConnor closed 1 year ago

KevinOConnor commented 1 year ago

This adds an API function to stop CAN bus processing. The new can2040_stop() effectively does the inverse of can2040_start(). After stopping, one may call can2040_start() again, or fully clear the transmit queue and restart by redoing the init starting from can2040_setup(). See the updated API documentation on this branch for further details.

I don't have a good way to test this code right now. I'm posting it in case others are looking for this functionality.

-Kevin

linted commented 1 year ago

Since can2040_stop() is implied to be the inverse of the start function, would it make sense to clear the pio's instruction memory as well?

KevinOConnor commented 1 year ago

Thanks for reviewing. The current implementation of can2040_stop() on this PR just disables the PIO irq and makes sure the CAN Tx line is high (it doesn't even stop the PIO). The goal is to provide a mechanism to safely stop processing CAN messages (safe with respect to race conditions with irq handling) while also keeping the code simple.

It is possible to write code that would fully stop the PIO and clear the PIO state, but I'm not sure what value that would provide. If some other code wants to use the PIO it can perform a hardware reset on the PIO itself.

Cheers, -Kevin

linted commented 1 year ago

Hey! Thanks for the added context. I went ahead and tested the code and it appears to be correctly working as your described.

KevinOConnor commented 1 year ago

Thanks for testing. I committed this PR.

-Kevin