Hi
I’ve been attempting to use the CAN bus with interrupts (using .attach).
However whenever I attach my handler, it appears that I can not read the CAN bus.
And after a while, MBED crashes with a Mutex error.
Actually, you are allowed to attach a CAN RX interrupt handler (ISR) and for example set a flag indicating that data have been received. But, unfortunately, you must not call the read method in an ISR anymore because it is calling the Mutex lock method an according to this explanation Mbed OS will treat that as a fatal system error.
I think it’s simpler to call the read method in the main’s while loop and poll for new data, as in this example, rather than to set a flag in the CAN RX interrupt handler and then clear it in the main.
Best regards, Zoltan
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:
Nope. Same issue in MBED5.15. Crashes on interrupt. The bug I posted in the original link shows the issue is still open. Your syntax is mostly clean and it makes sense but I have tried a variety of implementations such as yours and still crashes. I think the bug is still there.
Did you happen to try your code on an STM32 board? I suspect you’ll see the same bug.
yes, that is here for a long time.
The Interrupt flag can be clean only by CAN::read() method but this method is covered by the Mutex so It can not be used in interupt context. When CAN::read() is not called in the interrupt handler then the program will stay in the interrupt for ever.
The EventQueue leads to the ISR queue overflow.
I tested both methods from my previous post today on a Bluepill board (STM32F103C8T6) and they both did work fine. No runtime error occurred and all messaged were received correctly when using CAN frequency 500000Hz.
I built my test programs offline with Mbed OS 6.4.0.
Ok, thanks. Sounds like 6.x might have the fix I need but it is not supported on my variant of ST board.
Can you revert your mbed-os to 5.15 to see if it fails? That would close this case in my mind.
Ok, after understanding the root cause (stuck interrupt) I took out a BFH and fixed it quickly with the following…
volatile uint32_t can_rx_msg = 0;
uint32_t can_ier;
I’m glad you solved it. In case you need to build it with Mbed OS 6.4.0 (maybe also with any other Mbed OS 6.X.X revision) add the following to the mbed_app.json configuration file: