cancel
Showing results for 
Search instead for 
Did you mean: 

How to implement USB Communication Device Class (CDC)

Lubos KOUDELKA
ST Employee

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.
Comments
Javier1
Principal

Finally! lets hope CDC_TransmitCplt_xS works, i was hardcoding a halDelay(); after each transmitt transaction.

https://community.st.com/s/question/0D53W00000hPoRCSA0/cdctransmitfs-is-giving-me-a-headache-usb-cdc-device-mode

ill give it a try and let you know

Brieuc
Associate III

Hi Javier,

Did you have some success using the TransmitCplt callback ? My project is stuck with the same problem as yours, stopping Transmission after a while, even with small data amounts (a few bytes every second....). The callback works for a few minutes at best, then stop being called. Nothing more until I reset the board. Is there a running condition leading to this bug, and a workaround ??

Version history
Last update:
‎2021-05-18 09:21 AM
Updated by: