9 years, 8 months ago.

How to implement an optional USB device ?

I am trying to build an Mbed based device that can communicate with the rest of world either by UART or USB. USB is working fine when connected to a Host. Serial is now also working.

If connected to a USB Host both USB and serial communication work.

But in case no USB host is available, the program just freeze. It looks like the program just freeze because the USB communication doesn't start (no Host connected)

Which platform? What is your power source when not using USB? Can you post your code?

posted by Andy A 13 Aug 2014

Platform is Mbed LPC1768 with standard UART and USB Device in Use. Power scheme made like this: http://mbed.org/media/uploads/samux/img_0080.jpg To power the Mbed board when no Host is connected, I just use an alternate power supply connected to VIN instead. Is there any way to check if USB Host is present or not (or connection established) ?

posted by Laurent Pouillard 13 Aug 2014

By the way, how can one check that USB communication is established ? I would like to send data through USB to the Host only when the USB Host is connected and send the data with the UART serial port when no USB Host is connected ...

posted by Laurent Pouillard 13 Aug 2014

1 Answer

9 years, 8 months ago.

Although there have been improvements, I don't know how well it will behave with inserting/removing the USB, and then switching between the two options. You can of course also route the VCC pin to a digital pin, enable pull down, and you can check if the USB is powering it at least.

If it only need to work once to switch it to usb (per reset obviously), you can at the start of your program connect it non-blocking. To do that in the constructor you need to put blocking on false, which means you need also to enter a ton of other arguments, see; https://mbed.org/users/mjr/code/USBDevice/file/81f57ea86f8f/USBSerial/USBSerial.h

Then you can use the .configured() function to check if it is plugged in and decide if you should use USBSerial or regular Serial. Do note that I don't think it becomes false again if you unplug it.

thanks for the tip, I will check that ...

posted by Laurent Pouillard 14 Aug 2014