How to implement USB Communication Device Class (CDC)
1. Communication of USB CDC in device mode
For handling communication of USB CDC in device mode using legacy STM32 USB libraries are important 3 functions located in file usbd_cdc_if.c:
- CDC_Receive_xS - interrupt callback signaling received packet from USB host. Received data can be processed here, but routines in this function should be fast and still consider fact, that this function is called from interrupt.
- CDC_Transmit_xS - interface function, which prepares data to be send to the host. Once CDC_Transmit_xS returns USBD_OK status, data are not yet transmitted, data are stored in USB peripheral buffer of FIFO, waiting for host request to be transmitted. In this function is important to monitor return status. When status other than USBD_OK is received, data passed to the function won't be transmitted. When USBD_BUSY is received, user can either pool till USBD_OK is returned, or wait till CDC_TransmitCplt_xS callback is called.
- CDC_TransmitCplt_xS - callback from lower layers of USB library. It is signaling, that data prepared by CDC_Transmit_xS were transited to USB host and CDC_Transmit_xS can be called again
2. CDC examples
Please, check examples in
STM32CubeMx repository.