9 years, 11 months ago.

Serial attach crash

Hi,

Using the serial attach function crashes my application. (complete freeze).

I tried some of the simplest examples i could find like:

example serial crash

#include "mbed.h"
 
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
 
Serial computer(USBTX, USBRX);
 
// This function is called when a character goes into the TX buffer.
void txCallback(MODSERIAL_IRQ_INFO *q) {
    led2 = !led2;
}
 
// This function is called when a character goes into the RX buffer.
void rxCallback() {
    led3 = !led3;
}
 
int main() {
    computer.baud(115200);
    computer.attach(&txCallback, Serial::TxIrq);
    
    computer.attach(&rxCallback, Serial::RxIrq);
 
    while (1) {
        led1 = !led1;
        wait(0.5);
        computer.putc('A');
        wait(0.5);
    }
}

and i also tried this on several platforms (KL25Z/ KL05Z/ LPC1768) without any results.

  • KL25Z, KL05Z: immediate crash
  • LPC1768: output of 'A' works, crash after sending a character back to the platform.

I also tried emptying the buffer with a getc() in the interrupt routine, same problem..

Switching to the MODSERIAL library instead of Serial has solved this issue for me.

Hope to save someone some time with this!

I'll gladly provide more details for anyone interested.

1 Answer

9 years, 11 months ago.

The RX callback needs to have getc. MODSERIAL doesn't need this since it does that itself already. But without getc that interrupt is called permanently.

Accepted Answer

Thanks for your answer!

I did some more experiments; in case of the LPC1768 platform you're right. Following code works as expected:

example II serial crash

#include "mbed.h"
 
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
 
Serial computer(USBTX, USBRX);
 
// This function is called when a character goes into the TX buffer.
void txCallback() {
    led2 = !led2;
}
 
// This function is called when a character goes into the RX buffer.
void rxCallback() {
    led3 = !led3;
    computer.getc();
}
 
int main() {
    computer.baud(115200);
    computer.attach(&txCallback, Serial::TxIrq);
    
    computer.attach(&rxCallback, Serial::RxIrq);
 
    while (1) {
        led1 = !led1;
        wait(0.5);
        computer.putc('A');
        wait(0.5);
    }
}

But *exactly* the same code build to a KL25Z target crashes immediately (not one char. transmitted to the console, no leds flashing)

posted by Jaap Prickartz 22 May 2014

The txcallback is the issue on the KL25Z. While generally RX callback is more important, this still shouldn't happen. I will check if I can find what is going wrong.

Edit: Going to throw it on: "Working as intended". Difference is that the LPC1768 throws an interrupt when it transmits a character, the KL25 doesn't have this option, and instead throws one when it can transmit a character (which essentially is the same, since when the LPC1768 transmit one there is also space again for the next one). Difference is that the KL25 keeps calling interrupts while it can transmit: If you use it for an interrupt driven serial driver for example this isn't an issue, and you only need to take into account you disable the tx interrupt when you are done sending. For just flashing an LED it doesn't work though.

posted by Erik - 22 May 2014

Thanks for looking into this! You're answer is clear!

posted by Jaap Prickartz 22 May 2014