Arm Mbed OS support forum

USB(Serial) examples or best practices?

Hello,
I’ve been searching for a while how to properly use USB connections in mbed. Unfortunately, I haven’t found anything much - and honestly - I feel too lazy to absorb the whole USB protocol RFC papers and bite though all the tiresome abstracts…

Could you please point me where I could get some more materials in more tangible form, or even prefferrably - if there are some best practices / examples directly for mbed framework?

More specifically, I ran into some issues with my prototype and I don’t know where to get answers for them. All of these are related to USBSerial only.

  • Connection reneval. If I restart the board, the usb connection is not reestablished (even when I call .disconnect(); .connect(); To fix this state, I have to plug the usb out and in again.
  • Waiting for connection. usb.wait_ready(); causes mbed hard fault crash regardless anything I try to write around it.
  • Reading data as FileHandle. Even when USBSerial (as well as BufferedSerial) inherits FileHandle, the behavior is obviously different. What is the actual difference? Namely, it seems that file.read(buf, size_t bytes); ignores the set_blocking(bool) settings and acts weird (I wasn’t able to map it thorougly since the debugger in mbed studio does not work for me after update to 1.4 …)

These three are just initial problems and I believe I could find my own solution/workaround but there’s so few materials about this classes I do not know where else to look.

Thanks for all advices.

Hello,

maybe would be good to publish an example where those problems are seen and share basic informations - you target, Mbed OS version.
For example, I had no problem with re-connection when I tried it last time (few weeks ago).

I do not know if that will help you but - Mbed usb tests

BR, Jan

Hi,
sorry for late reply.
The tests you linked were actually very helpful, thanks for that.

The hottest problem I had was about reconnecting USB after resetting the device (or powering off and on again). After some research I solved this a physical way - pulling the usb D+ up and down in particular moments.

However, It might be - perhaps - a good idea to place the link for the tests into the docs page, because these somehow does not contain a word about the connection handling.

The “Reading data as FileHandle” I solved with a workaround which does not use posix read() behavior.
I just reworked it to use ´FH->readable()´.

This probably concludes my questions, thanks for your help.