cancel
Showing results for 
Search instead for 
Did you mean: 

CUBEMX example USB CDC to UART

Rudas.Robert
Associate II
Posted on March 18, 2016 at 14:42

Hello,

i'm trying to make a USB cdc virtual port to transfer data to uart and back. I have successfully made the virtual port using CubeMX and transfer data to uart. But i have trouble to read data from uart and transfer it to virtual port (some characters get lost). Could someone help me with example or point me to an example using CubeMX HAL libraries?

Thanks in advance for the answers.

#usb #cdc
2 REPLIES 2
Rudas.Robert
Associate II
Posted on March 19, 2016 at 22:34

tsuneo
Senior
Posted on March 22, 2016 at 05:37

> But i have trouble to read data from uart and transfer it to virtual port (some characters get lost).

It's because the difference of transfer interval timing between UART and USB bulk IN endpoint. While your CDC device connects to Windows, the bulk IN endpoint should wait for next 1ms SOF timing, before it could put another transfer to the bus (ie. USBD_CDC_TransmitPacket() call). On the other hand, UART RX interrupt could occur in shorter interval. And then, if your UART ISR would directly call USBD_CDC_TransmitPacket(), some characters would get lost.

To escape from this timing difference,

1) In your UART ISR, received data is stored on a buffer, once.

2) In a timer interrupt of a couple of ms interval (or USB SOF ISR), the buffer is examined. If any data on the buffer, the data is sent using USBD_CDC_TransmitPacket().

You'll find this implementation using a timer in CDC_Standalone examples in cube ''projects'', for example,

\Repository\STM32Cube_FW_F0_V1.5.0\Projects\STM32072B_EVAL\Applications\USB_Device\CDC_Standalone\Src\usbd_cdc_interface.c

Find an example for your MCU, in the cube firmware library, in an EVAL board folder.

Tsuneo