cancel
Showing results for 
Search instead for 
Did you mean: 

Correct method of using CDC (Virtual Com Port)

SWill.10
Associate II

Hi there,

I love using Cube and being able to get a project up and running super quickly, especially because I work in an agile development environment.

I am using CDC/USB/VCP for the first time on an STM32 system (STM32F070RB specifically, FYI) and was just after some help with the "proper" way to use it. We simply plan on using it to do simple interfacing to the Micro (send simple instructions like "LED ON", and receive simple data like raw ADC readings like "506").

From what I can tell, there are two main functions I should be interested in:

  • CDC_Receive_FS(uint8_t* Buf, uint32_t *Len)
  • CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)

0690X000006DoKzQAK.jpg

I can see how these two work fairly easily; when data is received, the Receive_FS function is called and stores the received characters in the hUsbDeviceFS RX buffer; when data is transmitted, the Transmit_FS needs to be called and the data parsed into it.

The transmit is pretty easy to understand. After #including the CDC_IF h file into my main, I can simply call the CDC_Transmit_FS function whenever I want to transmit a packet of data - very simple and easy.

The difficulty comes in receiving data from the host. From the code in usbd_cdc_if.c, I can see that the Receive_FS function is called; of interest, this calls the functions USBD_CDC_SetRxBuffer and USBD_CDC_ReceivePacket. This is where my understanding breaks down, and I am not sure how to access the RxBuffer from main.c and process incoming bytes.

I tried doing something like using the "hUsbDeviceFS" variable that main.c has access to, and accessing the RxBuffer through this (i.e. &((&hUsbDviceFS)->pClassData)->RxBuffer). However, this causes the PC to throw a "USB Device Not Recognized", leading me to believe that I am not supposed to do this as it interferes with the normal processes on-going at a low-level.

Can anyone please help me? Perhaps a simple example processing incoming data, but preferably doing it all from main.c. I know I could easily start adding my own buffers and parsing data around in the USBD files, but this is bad practice and I imagine (or at least hope) STM have a way that the user can access the buffer without modifying generated Cube peripheral files - especially when it comes to a propriety, high-speed protocol like USB.

Thank you! Your time is very much appreciated 🙂

0 REPLIES 0