3.7 USB CDC - How to use

The USB CDC class implementation handles basic CDC requests and mainly consists of a handler function and several helper functions to access status variables transmitted to and from the host.

The initialization of the CDC class is done by setting up a pointer to the USB descriptor and by registering the request handler in the USB core as a callback to be called when needed.

The USB_CDCRequestHandler function handles all requests not on the control endpoint, it starts by checking that the incoming request is meant for an interface. It then handles the request based on direction and type. If the request is not handled it will return “UNSUPPORTED”.

controlLineState is a 16-bit variable that contains two status bits:
  • Request To Send (RTS) - Data Circuit-terminating Equipment (DCE) code to indicate that the host request data out transmission
  • Data Terminal Ready (DTR) - Data Terminal Equipment (DTE) code to indicate terminal window opened by host
The host sets the DTR bit through the request handler and it can be accessed using USB_CDCDataTerminalReady.
lineCoding is a struct containing information on the formatting for each character transmission and has the following members:
  • Data Terminal Rate (DTERate) - Transmission rate in bits per second
  • Character Format - The number of stop bits {1, 1.5, 2}
  • Parity Type - Type of parity used {none, odd, even, mark, space}
  • Data Bits - Number of actual data bits in transmission {5, 6, 7, 8, 16}
These values can be set by both the host and device and communicating them is done through the request handler and can be accessed using the following helper functions:
  • USB_CDCSetBaud
  • USB_CDCGetBaud
  • USB_CDCSetStopBits
  • USB_CDCGetStopBits
  • USB_CDCSetParity
  • USB_CDCGetParity
  • USB_CDCSetDataBits
  • USB_CDCGetDataBits