Hello,
I’ve been struggling to get the CAN interrupt to work for some time now without succes. I’m using a Nucleo F446RE board on which I’d like to use an interrupt to receive CAN messages. My code is as follows:
#include "mbed.h"
// CAN-bus
CAN can(D15, D14);
int canFrequency = 100000;
// LED
DigitalOut led1(LED1);
CANMessage msg;
bool newMsg = false;
/**
* Called when a CAN message is received
*/
void interruptCanMessage() {
newMsg = true;
}
int main() {
can.frequency(canFrequency);
can.attach(&interruptCanMessage, CAN::RxIrq);
while (true) {
if (newMsg) {
can.read(msg);
led1 = !led1;
newMsg = false;
}
}
}
I use another Nucleo F446RE to send a CAN message every second. The first Nucleo is able to receive the messages when using no interrupt attached and this in my main:
while (true) {
if (can.read(msg)) {
led1 = !led1;
}
}
-I’ve found on various sites that you can’t call the can.read() function within the interrupt so that’s why I used the boolean, but maybe that is the correct way, I don’t know.
-Another problem might lie in the boolean that is used in multiple threads maybe?
-Also if I toggle the led in the ISR function (interruptCanMessage()) the LED is rapidly toggled because the can message is never read so the ISR function is continiously called.
Can someone explain what I’m doing wrong and point me in the right direction.
Thanks in advance.