Examples

mxusb_demo.cpp: Simple example with only one endpoint, and associated libusb based PC source code.

mxusb_serial.cpp: virtual com port using the standard CDC ACM class.

You might also want to have a look at the testsuite for a more complex example involving multiple configurations.

Writing descriptors

When writing descriptors it is highly recomended to enable endpoint validation and trace mode in usb_config.h.

How to detect when the USB cable is disconnected

If the USB device is bus powered, there is no problem: If the cable is disconnected the device is no longer powered and therefore there is no need to detect cable disconnect. In this case USB suspend/resume events can be unambiguously interpreted, because if the device enters suspend and still has power to process the event, it is surely an USB suspend.

If the device is self powered and the 1k5 pullup to D+ is wired in a way that it pulls the D+ line to +3v3 also when the cable is disconnected, then when the cable is disconnected a suspend event will be generated, and when the cable is connected again a resume followed by a reset is generated. Therefore a suspend event can be both caused by a real suspend or a cable disconnect, and the same for resume. This is not too bad, since if the device is self powered, it does not need to enter low power mode. If it is really necessary to differentiate between suspend and cable disconnect, it is possible to wire a gpio to the +5v that come from the USB cable.

The STM3210E-Eval, however, uses two transistors to drive the pullup to D+ so that the pullup is only connected when the USB cable is connected. This makes it harder to detect USB cable disconnect: when the cable is disconnected, a suspend event immediately followed by a resume event is generated, and when the cable is connected again, a reset event is generated.