CAN bus interrupt problem (MBED 5.15)

Sorry, I was focusing on RX. Yes, you are right. That example is wrong. The TX ISR calls the write method which is not allowed because the write method, like the read one, calls the Mutex lock method.
Another option, which I forgot to mention in my first post, is to use EventQueue rather than interrupt:

...
CAN             can(PA_11, PA_12);
CANMessage      rxMsg;
EventQueue      eventQueue;
Thread          t;
...
void onCanReceived(void)
{
    can.read(&rxMsg);
   ...
}
...
int main()
{
    t.start(callback(&eventQueue, &EventQueue::dispatch_forever));
    can.attach(eventQueue.event(onCanReceived));
    ...

    while(1) {
        ...
    }   
}

Or you can call EventQueue from the interrupt handler:

...
CAN             can(PA_11, PA_12);
CANMessage      rxMsg;
EventQueue      eventQueue;
Thread          t;
...
void onCanReceived(void)
{
     eventQueue.call(can.read(&rxMsg));  // No Mutex method is called. Execution of can.read is deferred to thread t.
     ...
}
...
int main()
{
    t.start(callback(&eventQueue, &EventQueue::dispatch_forever));
    can.attach(onCanReceived, CAN::RxIrq);  // attach interrupt handler
    ...

    while(1) {
        ...
    }   
}
1 Like