USB Component
Version 6.17.0
MDK Middleware for USB Device and Host Communication
|
This chapter describes the software structure of the USB Device Component and its use for creating applications. The USB Device Component simplifies the software development of microcontroller systems that interface to a USB Host.
Attributes of the USB Device Component:
For interfacing to an USB Host Computer, additional software may be required. Page USB Host Computer Applications shows an example for such a software running on Windows PCs.
The picture shows the relationship between RTE Components and the microcontroller's USB Device peripheral (USB Controller). RTE Components provide configuration files and user code templates. Configuration files configure the RTE Components, hardware interfaces, memory resources and USB Device driver parameters. They can have an impact on multiple RTE Components (for example RTE_Device.h configures the USB Controller 0 and Driver_USBD0). User code templates provide the skeleton for implementing the USB Device functionality.
The grey area around the RTE Components USB Device 1 and Driver_USBD1, as well as USB Controller 1 means that these components are optional and can only be used if a microcontroller device has multiple USB controllers present. If this is the case, an USB Device Class can be connected to any of the USB Device Instances.
USB Device peripherals can have one or more of the following USB Device Classes:
Generic information about USB Device Classes can be found on the USB-IF's Approved Class Specification Documents page.
Multiple RTE Component instances can interface with more than one USB Controller or can implement multiple USB Device Classes. RTE Component instances are numbered. The number is appended to the RTE Component name, related configuration files, and user code templates. Each RTE Component has a separate configuration file. For example, for HID 0 and HID 1 the configuration files have the name USB_Config_HID_0.h and USB_Config_HID_1.h.
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.c is listed in the Project Windows under the Component USB and 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 Numbers and are listed in the Project Window under the Component USB.
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:
Stack_Size
).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 different then default functionality is needed.
The following templates are available for the USB Device component:
Template Name | Purpose |
---|---|
USBD_User_ADC_n.c | Required functions to create an ADC device. |
USBD_User_CDC_ACM_n.c | Required functions to create a CDC (ACM) device. |
USBD_User_CDC_ACM_RNDIS_VETH_n.c | Required functions to create a CDC (ACM) RNDIS virtual Ethernet device. |
USBD_User_CDC_ACM_RNDIS_ETH_n.c | Required functions to create a CDC (ACM) RNDIS and Ethernet bridge device (Ethernet-over-USB). |
USBD_User_CDC_ACM_UART_n.c | Required functions to create a CDC (ACM) device demonstrating a USB <-> UART bridge. |
USBD_User_CDC_NCM_n.c | Required functions to create a CDC (NCM) device. |
USBD_User_CDC_NCM_ETH_n.c | Required functions to create a CDC (NCM) device (Ethernet-over-USB). |
USBD_User_CustomClass_n.c | Required functions to create a device supporting a custom USB Device class. |
USBD_User_Device_n.c | Required functions for run-time configuration of the USB Device. |
USBD_User_SerNum_n.c | Example for run-time configuration: Changing the serial number of the USB Device. |
USBD_User_HID_n.c | Required functions to create a HID device. |
USBD_User_HID_Mouse_n.c | Implements functions to create a USB HID device acting as a mouse pointer input device. |
USBD_User_MSC_n.c | Required functions to create a MSC 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 than the USB Component allows, 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 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 runtime. 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_string_descriptor [] | String descriptors |
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_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_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 done to change descriptors at runtime. 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 the point to the externally created ones, you can change the descriptors at runtime.
The actual variable names of the structures holding descriptor pointers are usbdn_desc
(n indicating the USB Device instance number). 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 library form and doesn't allow direct code debug. However it can be easily configured to generate debug events and provide dynamic visibility to the component operation.
Following variants can be selected for the USB:CORE component in the Manage Run-Time Environment window:
The figure below shows selection of the Debug variant.
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:
Now, when the USB Device generates event information, it can be viewed in the .
This section describes the configuration settings for the Event Recorder. The usage requires the debug variant of the USB:CORE software component; refer to Event Recorder Support for more information.
USB Event Generation Configuration
Selecting the USB:CORE debug variant will add the file USB_Debug.c
to your project. Use this file to set the event generation configuration for USB core, drivers, and device classes separately. The file is available for USB Device and Host components.
The following settings are available for event generation configuration of each module:
Event IDs
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 |