Microchip® Advanced Software Framework

Quick Start Guide for USB Device Communication Class Device Module (UDI CDC)

This is the quick start guide for the USB Device Interface CDC Module (UDI CDC) with step-by-step instructions on how to configure and use the modules in a selection of use cases.

The use cases contain or highlights 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 CDC (Single Interface Device)" module is used with only one communication port. The "USB CDC (Composite Device)" module usage is described in Advanced Use Cases.

Setup Steps

As a USB device, it follows common USB device setup steps. Refer to USB Device Basic Setup.

Usage Steps

Example Code

Content of conf_usb.h:

#define UDI_CDC_ENABLE_EXT(port) my_callback_cdc_enable()
extern bool my_callback_cdc_enable(void);
#define UDI_CDC_DISABLE_EXT(port) my_callback_cdc_disable()
extern void my_callback_cdc_disable(void);
#define UDI_CDC_LOW_RATE
#define UDI_CDC_DEFAULT_RATE 115200
#define UDI_CDC_DEFAULT_STOPBITS CDC_STOP_BITS_1
#define UDI_CDC_DEFAULT_PARITY CDC_PAR_NONE
#define UDI_CDC_DEFAULT_DATABITS 8
#include "udi_cdc_conf.h" // At the end of conf_usb.h file

Add to application C-file:

static bool my_flag_autorize_cdc_transfert = false;
bool my_callback_cdc_enable(void)
{
my_flag_autorize_cdc_transfert = true;
return true;
}
void my_callback_cdc_disable(void)
{
my_flag_autorize_cdc_transfert = false;
}
void task(void)
{
if (my_flag_autorize_cdc_transfert) {
}
}

Workflow

  1. Ensure that conf_usb.h is available and contains the following configuration, which is the USB device CDC configuration:
    #define USB_DEVICE_SERIAL_NAME "12...EF" // Disk SN for CDC
    Note
    The USB serial number is mandatory when a CDC interface is used.
    #define UDI_CDC_ENABLE_EXT(port) my_callback_cdc_enable()
    extern bool my_callback_cdc_enable(void);
    Note
    After the device enumeration (detecting and identifying USB devices), the USB host starts the device configuration. When the USB CDC interface from the device is accepted by the host, the USB host enables this interface and the UDI_CDC_ENABLE_EXT() callback function is called and return true. Thus, when this event is received, the data transfer on CDC interface are authorized.
    #define UDI_CDC_DISABLE_EXT(port) my_callback_cdc_disable()
    extern void my_callback_cdc_disable(void);
    Note
    When the USB device is unplugged or is reset by the USB host, the USB interface is disabled and the UDI_CDC_DISABLE_EXT() callback function is called. Thus, the data transfer must be stopped on CDC interface.
    #define UDI_CDC_LOW_RATE
    Note
    Define it when the transfer CDC Device to Host is a low rate (<512000 bauds) to reduce CDC buffers size.
    #define UDI_CDC_DEFAULT_RATE 115200
    #define UDI_CDC_DEFAULT_STOPBITS CDC_STOP_BITS_1
    #define UDI_CDC_DEFAULT_PARITY CDC_PAR_NONE
    #define UDI_CDC_DEFAULT_DATABITS 8
    Note
    Default configuration of communication port at startup.
  2. Send or wait data on CDC line:
    // Waits and gets a value on CDC line
    int udi_cdc_getc(void);
    // Reads a RAM buffer on CDC line
    // Puts a byte on CDC line
    int udi_cdc_putc(int value);
    // Writes a RAM buffer on CDC line

Advanced Use Cases

For more advanced use of the UDI CDC module, see the following use cases: