The steps to create a microcontroller application that uses USB communication with an USB Device controller are:
For interfacing to an USB Host computer, standard USB Device Classes drivers can be used. This may require additional software development for the USB Host application. An exemplary application for interfacing to an USB HID Device is explained here.
Only a few steps are necessary to complete the RTE Component selection:
The USB Device Driver and the USB Controller of the microcontroller need to be correctly configured. In particular this means:
The configuration file USBD_Config_n.h contains a number of important settings for the specific USB Device.
Refer to USB Core Configuration for more configuration options of the USB Device.
The USB Device Class Parameters and Endpoint Settings are configured in separate files for each USB Device Class and separately for each instance. The configuration files contain Device Class specific Endpoint Settings.
Each USB Endpoint can only be used once on the same USB Device. It has to be made sure that the different USB Device Classes or multiple instances of the same USB Device Class use different Endpoints. The default configuration supports applications that use a single USB Device Class. The remaining parameters are specific settings that configure parameters for USB communication speed and the USB Device Class.
For proper operation, the USB Device Component requires some system configuration settings. The requirements are:
For more information, check the USB Device component's Resource Requirements section.
files provide function templates used to implement USB Device Class functionality. The available functions are explained in the Reference section of the USB Component. These routines can be adapted to the needs of the microcontroller application, in case that functionality different then default is needed.
The following templates are available for the USB Device component:
Template Name | Purpose |
---|---|
USBD_User_ADC_n.c | Functions for an ADC device. |
USBD_User_CDC_ACM_n.c | Functions for a CDC (ACM) device. |
USBD_User_CDC_ACM_RNDIS_VETH_n.c | Functions for a CDC (ACM) RNDIS virtual Ethernet device. |
USBD_User_CDC_ACM_RNDIS_ETH_n.c | Functions for a CDC (ACM) RNDIS and Ethernet bridge device (Ethernet-over-USB). |
USBD_User_CDC_ACM_UART_n.c | Functions for a CDC (ACM) device demonstrating a USB <-> UART bridge. |
USBD_User_CDC_NCM_n.c | Functions for a CDC (NCM) device. |
USBD_User_CDC_NCM_ETH_n.c | Functions for a CDC (NCM) device (Ethernet-over-USB). |
USBD_User_CustomClass_n.c | Functions for a device supporting a custom USB Device class. |
USBD_User_Device_n.c | Functions for specific Control Endpoint 0 requests handling. |
USBD_User_SerNum_n.c | Example for specific Control Endpoint 0 requests handling: Changing the serial number of the USB Device. |
USBD_User_HID_n.c | Functions for a HID device. |
USBD_User_HID_Mouse_n.c | Example for a HID device acting as a mouse input device. |
USBD_User_MSC_n.c | Functions for a mass storage device. |
USBD_MSC_n.c | Shows how to get access to storage media either from the application/File System or from the USB host. |
If there are different requirements regarding the USB Descriptors, the USB Component allows that. A user can change any or all of the default USB descriptors. Default descriptors are the ones that library creates based on Device and Classes configuration file settings and are located in the code memory.
The descriptors can be changed in two of the following ways:
Static change of descriptors can be done to replace default descriptors if they will not change at run-time. The descriptors can be easily overridden by user code by creating descriptors with the same name.
USB Device Descriptor | Purpose |
---|---|
const uint8_t usbdn_ep0_descriptor[] | Control Endpoint 0 descriptor. |
const uint8_t usbdn_device_descriptor[] | USB Device descriptor. |
const uint8_t usbdn_config_descriptor_fs[] | Configuration descriptor for low/full-speed. |
const uint8_t usbdn_config_descriptor_hs[] | Configuration descriptor for high-speed. |
const uint8_t usbdn_device_qualifier_fs[] | Device qualifier for low/full-speed. |
const uint8_t usbdn_device_qualifier_hs[] | Device qualifier for high-speed. |
const uint8_t usbdn_other_speed_config_descriptor_fs[] | Other speed configuration descriptor for low/full-speed. |
const uint8_t usbdn_other_speed_config_descriptor_hs[] | Other speed configuration descriptor for high-speed. |
n
in usbdn_
represents the USB Device instance. So for the USB Device 0 instance, you have to use usbd0_...
Code Example
Dynamic change of descriptors can be used to change descriptors at run-time. The struct usbd_desc_t
contains the required information. It is stored in RAM and contains pointers to the USB descriptors. If you change the pointers in the structure to point to the externally created ones, you effectively change the descriptors.
The structure usbdn_desc
(n indicating the USB Device instance number) contains the pointers to the descriptors. The following code example shows how to override the device descriptor for the USB Device 0 (usbd0_desc
):
Code Example
USB Device Component is distributed in a source form and it allows direct code debug. However, with breakpoint stopping the program execution the USB Device will usually malfunction as USB Host will not be able to properly enumerate the device or communicate with it thus causing communication timeouts and failures. For such usage case a non-intrusive debug via the debug events is also provided (via Event Recorder).
USB_Debug.h
configuration file is used to configure the level of debug events.
The USB Device:Debug Events describes the events implemented in the USB Device Component.
is a powerful tool that provides visibility to the dynamic execution of the program.
The USB Device Component generates a broad set of Debug Events for the Event Recorder and implements required infrastructure to interface with it.
To use the Event Recorder it is required to create an image with event generation support. The necessary steps are:
USB_Debug.h
enable USB Device event generation and configure event filters.Now, when the USB Device generates event information, it can be viewed in the .
This section describes the configuration settings for the Event Recorder.
USB Event Generation Configuration
Selecting the USB:CORE will add the file USB_Debug.h
to your project. Use this file to set the event generation configuration for USB core, drivers, and device classes separately. The file is common for USB Device and Host components.
The following settings are available for event generation configuration of each module:
The USB Device component uses the following event IDs:
Component | Event ID |
---|---|
USBD_Core | 0xA0 |
USBD_Driver | 0xA1 |
USBD_CC | 0xA2 |
USBD_ADC | 0xA3 |
USBD_CDC | 0xA4 |
USBD_HID | 0xA5 |
USBD_MSC | 0xA6 |