2016-03-27 01:57 PM
please read my post under
[DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Java/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Java/STM32%20Virtual%20Com%20Port%20problem&FolderCTID=0x01200200770978C69A1141439FE559EB459D758000F9A0E3A95BA69146A17C2E80209ADC21&TopicsView=https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Java/AllItems.aspx?View%3D%257bA4EEE515%252d04F0%252d42D9%252dAC32%252dA6D8340F101B%257d%26FolderCTID%3D0x012001¤tviews=161]Virtual COM Port problem [solved] link current generated code for CDC_Receive_FS is wrong. Also see my current coding patch below. Regards, Adib. static int8_t CDC_Receive_FS (uint8_t* Buf, uint32_t *Len) { /* USER CODE BEGIN 6 */ // USBD_CDC_SetRxBuffer(hUsbDevice_0, &Buf[0]); // USBD_CDC_ReceivePacket(hUsbDevice_0); usb_cdc_rx_handler(Buf, *Len); return (USBD_OK); /* USER CODE END 6 */ } #stm32-cubemx-usb-vcp-stm32f42016-03-28 02:44 AM
Hi Adib,
usbd_cdc_if.c is a driver for user application, and the code implemented now is just for help so you can modify it or add your own code [in user code section].For more details, on how you use the STM32Cube USb device library, please refer touser manual.-Syrine-2016-03-28 08:23 AM
Hello Syrine,
the code in usbd_cdc_if.c is autogenerated by CubeMx. I do expect the code to be correct and useful. However some code/documentation is clearly not correct or useful. 1) CDC_Receive_FS ''This function will block any OUT packet reception on USB endpoint untill exiting this function.'' !!!WRONG!!! Dangerous. If the function does not exit, then the application is stalled and blocked. Correct wording: ''This handler will block any OUT packet reception on USB endpoint untill calling USBD_CDC_ReceivePacket.'' ''USBD_CDC_SetRxBuffer(hUsbDevice_0, &Buf[0]);'' !!!USELESS!!! this will assign the exact same buffer as in CDC_Init_FS Correct: no need to reassign rx buffer ''USBD_CDC_ReceivePacket(hUsbDevice_0);'' !!!WRONG!!! As documentation says, that application will not cope with the speed of the USB. There is no usecase to directly reactivate receiving within this function. Correct would be to inform application by means of event flag that rx buffer is ready for reading. 2) ''#define APP_RX_DATA_SIZE 4'' !!!WRONG!!! this will lead to buffer overflow In file usbd_cdc.c in function USBD_CDC_ReceivePacket() ''USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, hcdc->RxBuffer, CDC_DATA_FS_OUT_PACKET_SIZE);'' expects the buffer to be 64 bytes! (=CDC_DATA_FS_OUT_PACKET_SIZE) 3) CDC_Transmit_FS In the USB CDC modul there is no user callback once the TxBuffer has been sent out. The USART module has HAL_UART_TxCpltCallback. !!!FEATURE REQUEST!!! pls implement a callback for tx complete In order to do this in file usbd_cdc.c in function CDC_TransmitCplt() '' ... hcdc->TxState = 0; + CDC_TransmitCplt(hcdc); ... '' 4) ''#define APP_TX_DATA_SIZE 4'' !!!USELESS!!! in init function the driver sets Tx buffer size to 0, And this buffer will not be used. Then the CDC_Transmit_FS() function will set a different TxBuffer pointer. So the above statement is a waste of memory. Correct would be to not have TXbuffer allocated. Please fix broken and wrong behaviour of USB driver by code created from CubeMx. Regards, Adib.