Serial callback not working

I’ve been trying to transmit and receive information through the serial port in my NUCLEO-F072RB using mbed-os 5.12 with some particular problems.

I have tested serial.getc() in my main loop and it worked fine, but now I want to use a callback whenever I get new information to avoid the blocking call and it does not seem to work. When I send something through the serial port led1 starts blinking (4 times at normal pace, then 3 fast blinks) and it appears that the code gets stuck in the callback.

My code looks like this:

#include "mbed.h"
    
Serial pc(PC_4, PC_5, 9600);
volatile char c = 'n';
bool charReceived;

DigitalOut led1(LED1);

void onCharReceived(){
    c = pc.getc();
    charReceived = true;
}

int main() {
    DigitalOut led1(LED1);

    pc.printf("Start \n");
    pc.attach(&onCharReceived, Serial::RxIrq);

    while(1){
        if(charReceived == true){
            led1 = true;
            wait(1);
            led1 = false;
            pc.printf("%c \n", c);
            charReceived = false;
        }
    }
}

Any suggestions of what I may be missing?

Thanks in advance.

Hi José,
It’s an issue long time ago…, please refer here

We recommend to use RawSerial when you want to use RxIrq callback.

And I think you should use volatile also on charReceived.

Regards,
Desmond

Thanks! Using raw serial worked out.