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.
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.
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) extern bool my_callback_cdc_change(USBH_device_t* dev, bool b_plug); #define UHI_CDC_RX_NOTIFY() my_callback_cdc_rx_notify() extern void my_callback_cdc_rx_notify(void); #include "uhi_cdc.h" // At the end of conf_usb_host.h file
Add to application C-file:
static bool my_flag_cdc_available = false; bool my_callback_cdc_change(USBH_device_t* dev, bool b_plug) { if (b_plug) { // USB Device CDC connected my_flag_cdc_available = true; // Open and configure USB CDC ports CDCLineCoding cfg = { .dwDTERate = CPU_TO_LE32(115200), .bCharFormat = CDC_STOP_BITS_1, .bParityType = CDC_PAR_NONE, .bDataBits = 8, }; uhi_cdc_open(0, &cfg); } else { my_flag_cdc_available = false; } } void my_callback_cdc_rx_notify(void) { // Wakeup my_task_rx() task } #define MESSAGE "Hello" void my_task(void) { static bool startup = true; if (!my_flag_cdc_available) { startup = true; return; } if (startup) { startup = false; // Send data on CDC communication port uhi_cdc_write_buf(0, MESSAGE, sizeof(MESSAGE)-1); uhi_cdc_putc(0,'\n'); return; } } void my_task_rx(void) { while (uhi_cdc_is_rx_ready(0)) { int value = uhi_cdc_getc(0); } }
#define USB_HOST_UHI UHI_CDC
#define UHI_CDC_CHANGE(dev, b_plug) my_callback_cdc_change(dev, b_plug) extern bool my_callback_cdc_change(USBH_device_t* dev, bool b_plug);
#define UHI_CDC_RX_NOTIFY() my_callback_cdc_rx_notify() extern void my_callback_cdc_rx_notify(void);
For more advanced use of the UHI CDC module, see the following use cases: