Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
7 years, 8 months ago.
USBSerial and Serial interrupt
Hello,
I have a problem with the following code. At first I used only serial interrupt and everything worked good. I put USBSerial interrupt to my code where the code waits to received character "1". The problem is that nothing works at the moment. Neither serial interrupt nor USB serial interrupt. I do not know where is a bug.
#include "mbed.h" #include "USBSerial.h" #include "MODSERIAL.h" #include "at25sf041.h" //#include "rtos.h" DigitalOut LedStatus(P0_13); DigitalOut LedBattery(P0_14); DigitalOut USB_connect(P0_6); AnalogIn ain(P0_11); USBSerial usb(0x1f00, 0x2012, 0x0001, false); DigitalIn Vbus(P0_3); AT25SF041 memory(P0_9, P0_8, P0_10, P0_15); MODSERIAL uart(P0_19, P0_18); InterruptIn GPS_fix(P0_17); volatile bool newline_detected = false; char tmp; void Rx_interrupt(MODSERIAL_IRQ_INFO *q) { MODSERIAL *serial = q->serial; if ( serial->rxGetLastChar() == '\n') { newline_detected = true; } LedBattery = !LedBattery; } void toogle() { LedStatus = !LedStatus; } void usb_rx() { LedStatus = !LedStatus; tmp = usb.getc(); LedStatus = !LedStatus; } int main(){ USB_connect = 0; GPS_fix.rise(&toogle); usb.attach(&usb_rx); uart.baud(9600); uart.attach(&Rx_interrupt, MODSERIAL::RxIrq); while (1) { // if (usb.readable()) //{ // LedStatus = !LedStatus; // tmp = usb.getc(); // } if (tmp == '1' && newline_detected) { newline_detected = false; char c; while((c = uart.getc()) != '\n') { usb.putc(c); } usb.printf("\r\n"); //tmp = ' '; } } }
1 Answer
7 years, 2 months ago.
Hello,
I have similar problem with one of my projects. It seems that that mbed-rtos causes USBSerial.getc() to block in interrupts. With normal serial ports this can be resolved by accessing the serial registers directly inside the interrupt. With UsbSerial I don't know a proper solution yet.
USBSerial.getc() seems to start jamming in the interrupt after mbed-rtos is added to the project (even when rtos.h is not included).
If you're using mbed-rtos, I would recommend using the latest release of mbed, mbed OS, which includes mbed-rtos in the library. See Serial documentation here - https://docs.mbed.com/docs/mbed-os-api-reference/en/latest/APIs/interfaces/digital/Serial/
posted by 01 Sep 2017