Mbed asynch Serial example breaks when an unrelated instance of USBserial defined

I was planning to use the USB port as a virtual serial port for printf debugging while using async serial for interpreting a serial based protocol.

Since I want to read data in byte per byte, I figure the asynchronous serial is a good place to start (modified to work with my target):

#include "mbed.h"
#include "USBSerial.h"

static DigitalOut       led(LED1);
static UnbufferedSerial serial_port(PA_9, PA_10, 115200);
// USBSerial s1(true, 0x1f00, 0x2012, 0x0001);

void on_rx_interrupt()
{
    char c;

    // Toggle the LED.
    led = !led;

    // Read the data to clear the receive interrupt.
    if (serial_port.read(&c, 1)) {
        // Echo the input back to the terminal.
        serial_port.write(&c, 1);
    }
}

int main(void)
{
    // Set desired properties (9600-8-N-1).
    serial_port.baud(115200);
    serial_port.format(
        /* bits */     8,
        /* parity */   SerialBase::None,
        /* stop bit */ 1
    );
    // Register a callback to process a Rx (receive) interrupt.
    serial_port.attach(&on_rx_interrupt, SerialBase::RxIrq);
}

However, I find that as soon as I uncomment line 6 to enable the USBSerial s1, then the code breaks and from what I can tell, the ISR is never entered.

I suspected it could be because the call is blocking, I know from playing with it standalone, that port must be opened also for it to unblock and continue to main(), where in this instance serial_port.attach is executed. However even if I open both ports concurrently, I still can’t get the LED to toggle or the characters sent to be mirrored.

Does USBSerial disable ISR’s?
Also why is the unbuffered serial object declared as static in this instance? It’s been a while since I last wrote in C/C++ so I’m a bit rusty, I use static inside functions when I want the variable to remain in memory outside the scope in which it was defined and so the declaration inside a loop, it doesn’t get reinitialized over and over. Not sure what it does for global variables however.

It is looking like USBSerial also breaks BufferedSerial. and I suspect for the same reason. I find that when USBSerial is defined, characters are not received in the input buffer and writable function never returns true.

Hello Maxim,

What is your target board? On some targets (for example NUCLEO-F411RE) the PA_9 pin is connected also to the USB_VBUS and the PA_10 pin also to the USB ID.

OTG adds a fifth pin to the standard USB connector, called the ID-pin; the micro-A plug has the ID pin grounded, while the ID in the micro-B plug is floating. A device with a micro-A plug inserted becomes an OTG A-device, and a device with a micro-B plug inserted becomes a B-device. The type of plug inserted is detected by the state of the pin ID …

For more info visit

Try for example:

//static UnbufferedSerial serial_port(PA_9, PA_10, 115200);
static UnbufferedSerial serial_port(PB_6, PB_7, 115200);