RTOS serial interrupt

02 May 2012

Hi could someone explain to my what my code hangs when i try to read something from the serial port? My code seems to run fine until i get an interrupt. if i comment the line "pc.putc(pc.getc());", it runs fine.

here is my code

#include "mbed.h"
#include "rtos.h"

DigitalOut led1(LED1);
Serial          pc(USBTX,USBRX);



void pcrxcallback(void){
    pc.putc(pc.getc());

}


int main() {
pc.baud(115200);
pc.attach( &pcrxcallback);


    while (true) {
        led1 = !led1;
        Thread::wait(500);
    }
}
23 Jul 2012

For me the code fails no matter what I do with it. As is, it fails upon 1st character receipt. If I comment out the pc.putc line it fails upon boot. If I replace the pc.putc line with an i++; (with i declared inside function) it fails on 1st character receipt. I'm chasing serial hangs in my own programs. Going over forum posts (for serial and hang) it appears there is a history of serial hangs, libraries getting interrupt protection long after release (ModSerial) and very odd fixes like firing off null characters inside of ISR's to try to fix serial hangs. I have only one mbed at a remote location (Baghdad) and I don't have the luxury right now to get more hardware to try to factor out the possibility that I have bad hardware. If anyone knows of anything definitive and final about serial hangs being on this forum or elsewhere (for the mbed platform) please post here.

23 Jul 2012

Hi Doug, The ISR in your example is expected to remain stuck because putc will try to lock the stdio mutex: this is not allowed in an ISR. (See RTOS documentation).

HTH, Emilio

23 Jul 2012

Thanks Emilio (even though it wasn't my example). I only followed the example because I continue to have numerous hangs with serial routines even outside of RTOS and was looking for clues. I'll try to have my own code to post on the next round of my pursuit. regards, doug

27 Oct 2012

Dunno if this is still of interest, but I ran into a very similar problem. For me the fix was keeping well away from getch and putc and dealing with the uart direct. This assumes UART0 and the serial over usb link...

char theChar = LPC_UART0->RBR;  //read from UART
//see this for reg names http://mbed.org/users/4180_1/notebook/cc-io-register-names/
 do { //wait for uart ready
      } while ((LPC_UART0->LSR & 0x20)==0) ;
 LPC_UART0->THR = theChar ; //send the character back out