Homepage GitHub

I want use Libcanard CAN RX interrupt, what should I care about?


(Cherish Gww) #1

Hi Libcanard
I am honored to use your code.
and I want use Libcanard CAN RX with interrupt, where should I care about? memory pool lock or others?


(Pavel Kirienko) #2

I wouldn’t recommend invoking any part of the library API from the interrupt context, that is prone to race conditions and you will have to plaster the code with critical sections in order to make it work, and that will probably also result in excessive locking with poor interrupt latencies elsewhere.

Rather, you should use the interrupt handler only to manage the RX queue. When the RX interrupt handler is invoked, use it to pull the received frame from the CAN controller and store it somewhere in a queue. Then pull the frame from the queue from a regular (i.e., non-interrupt) context, and feed it into the library. In this case you will only need to use critical sections to protect the access to the queue, but not in the library itself.

This approach is used in Libuavcan everywhere, for example. In fact, it is enforced by libuavcan (can’t do otherwise).