This is the quick start guide for the USB host Communication Device Class module (UHI CDC) with step-by-step instructions on how to configure and use the modules in a selection of use cases.
The use cases contain several code fragments. The code fragments in the steps for setup can be copied into a custom initialization function, while the steps for usage can be copied into, e.g., the main application function.
Basic use case
In this basic use case, the "USB Host CDC (Single Class support)" module is used.
The "USB Host CDC (Multiple Classes support)" module usage is described in Advanced use cases.
Setup steps
Prerequisites
This module is based on USB host stack full interrupt driven and supporting Sleep manager sleepmgr. For AVR and SAM3/4 devices the Clock Management clock services is supported. For SAMD21 devices the asfdoc_sam0_system_clock_group clock driver is supported.
The following procedure must be executed to setup the project correctly:
- Specify the clock configuration:
- UC3 and SAM3/4 devices without USB high speed support need 48MHz clock input.
You must use a PLL and an external OSC.
- UC3 and SAM3/4 devices with USB high speed support need 12MHz clock input.
You must use an external OSC.
- UC3 devices with USBC hardware need CPU frequency higher than 25MHz.
- SAMD21 devices without USB high speed support need 48MHz clock input.
You must use a DFLL and an external OSC.
- In conf_board.h, the define CONF_BOARD_USB_PORT must be added to enable USB lines. (Not mandatory for all boards)
- Enable interrupts
- Initialize the clock service
The usage of Sleep manager sleepmgr service is optional, but recommended to reduce power consumption:
- Initialize the sleep manager service
- Activate sleep mode when the application is in IDLE state
conf_clock.h examples with USB support.
For AVR and SAM3/4 devices, add to the initialization code:
For SAMD21 devices, add to the initialization code:
Add to the main IDLE loop:
Example code
Content of conf_usb_host.h:
#define USB_HOST_POWER_MAX 500
Add to application C-file:
Workflow
- Ensure that conf_usb_host.h is available and contains the following configuration which is the main USB device configuration:
#define USB_HOST_POWER_MAX 500 // (500mA)
- Call the USB host stack start function to enable USB Host stack:
Usage steps
Example code
Content of conf_usb_host.h:
#define USB_HOST_UHI UHI_CDC
#define UHI_CDC_CHANGE(dev, b_plug) my_callback_cdc_change(dev, b_plug)
#define UHI_CDC_RX_NOTIFY() my_callback_cdc_rx_notify()
extern void my_callback_cdc_rx_notify(void);
Add to application C-file:
static bool my_flag_cdc_available = false;
{
if (b_plug) {
my_flag_cdc_available = true;
.bDataBits = 8,
};
} else {
my_flag_cdc_available = false;
}
}
void my_callback_cdc_rx_notify(void)
{
}
#define MESSAGE "Hello"
void my_task(void)
{
static bool startup = true;
if (!my_flag_cdc_available) {
startup = true;
return;
}
if (startup) {
startup = false;
return;
}
}
void my_task_rx(void)
{
}
}
Workflow
- Ensure that conf_usb_host.h is available and contains the following configuration which is the USB host CDC configuration:
#define USB_HOST_UHI UHI_CDC
- Note
- It defines the list of UHI supported by USB host.
#define UHI_CDC_CHANGE(dev, b_plug) my_callback_cdc_change(dev, b_plug)
extern bool my_callback_cdc_change(uhc_device_t* dev, bool b_plug);
- Note
- This callback is called when a USB device CDC is plugged or unplugged. The communication port can be opened and configured here.
#define UHI_CDC_RX_NOTIFY() my_callback_cdc_rx_notify()
extern void my_callback_cdc_rx_notify(void);
- Note
- This callback is called when a new data are received. This can be used to manage data reception through interrupt and avoid pooling.
- The CDC data access functions are described in UHI for Communication Device Class.
Advanced use cases
For more advanced use of the UHI CDC module, see the following use cases: