10 years, 10 months ago.

USBSerial debugging, connection state test?

Great forum here, thanks for helping a beginner so much.

I am now up and running with USBSerial based debugging and my system is taking shape. Now the testing should have started, but disconnecting the usb cable the board blocks the constructor of the USBSerial.

I have found a few answers of this question in the forum, but it looks really complex, does anyone have some simple code testing if a cable is connected or not, so the system itself knows if it is in debugging mode or not.

Thanks again

1 Answer

10 years, 10 months ago.

If you need a serial connection for debug output, why can't you use the built-in serial device on the mbed? E.g., on the LPC1768 and LPC11U24 it is the same USB port as you would use for drag-and-drop programming of the firmware (e.g., on the FRDM KL25Z it is the CMSIS-DAP USB port). This leaves your application USB pins free for your application or other GPIO usage.

The mbed chip exposes a composite device on the built-in USB connector comprising of the USB MSD drag-and-drop programming device, but also a USB CDC serial port. If you just plug the mbed into your PC without installing the mbed drivers, it will appear as merely a USB Flash drive, if I am not mistaken. The driver is here: https://mbed.org/handbook/SerialPC

You can then just do this:

#include "mbed.h"

Serial pc(USBTX,USBRX);

...

Is this what you mean?

Jason

Nope, I must be unclear. Everything is working, but I would like to test if the serial port is available when my program is running on the device. When it is disconnected, the serial port is not available and the constructor of the serial blocks the device for further action.

Furthermore, I think my platform (Arch GPRS) cannot use the same port. So my case might be different from yours.

You above code will block the device, if it is disconnected from pc. Or am I wrong?

posted by Niels Buch 20 Jan 2014

Correct, because of the USBSerial constructor. Have you see this reply? [[http://mbed.org/handbook/USBSerial#c2555]

posted by Jason Tay 20 Jan 2014

Actually this is a valid question and the same issue is frustrating me too. The reason its valid is very simple, not all mbed hardware has a built in serial connection over USB. For example Teensy needs the USBSerial library. It does not have the mbed chip on the board. This works just fine, but, yes, when you unplug the usb port, any progress is blocked by the USBSerial constructor. It's a very simple solution. The instantiation allows a non-blocking mode, after product version in the calling arguments, you have the option to select blocking (default) or non-blocking. Then things are fine.

Instantiate non-blocking

USBSerial pc(0x16c0, 0x0471, 0x0001, false);

You also might consider setting the priority of the USB IRQ. By default its set highest (0).

Setting USB IRQ priority

NVIC_SetPriority(USB0_IRQn, 3);

If you are running some time critical code, you probably don't want the USB transfer preempting it.

posted by Aidan Walton 17 Jun 2017