Electronic – USB VCP-CDC and UART on STM32

stm32stm32cubemxstm32f10xuartusb

I generated USB VCP code for "STM32F103C8T6" with STM32CubeMX. I'm trying to send received data by STM from PC, to an Arduino board using UART. The code works good for data with little size, but when I want to send bigger data the received data in Arduino is corrupted. I'm using "Teraterm" and "Realterm" for sending data from PC to STM32.
I modified the CDC_Receive_FS for sending data via UART in DMA mode, here is the code:

static int8_t CDC_Receive_FS (uint8_t* Buf, uint32_t *Len)
{

    HAL_UART_Transmit_DMA(&huart3, Buf,(uint16_t)* Len);/* same problem with HAL_UART_Transmit_IT*/ 
    USBD_CDC_ReceivePacket(hUsbDevice_0);//Getting ready for receiving next data
    return (USBD_OK);

}

I sent this

{1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40}

and received

{1 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 20 21 22 23 24 2}

The USART baud rate is 19200, and in STM32, size of Received buffer, APP_RX_DATA_SIZE is defined 640.
What should I do?

Best Answer

when you use uart dma or interrupt version it is not blocking , the uart function return straight away before data fully send So it happen that you get ready to receive new data from usb before previous sent on uart . As you use same buffer for usb rx and uart tx usb will start refiling buffer as it still getting send by uart (why you miss things).

if to use dma or it then handle the completion callback to restart usb reception or implement a more elaborated fifo/buffer in between usb and uart to bufferiez incoming data as it get sent.