AnsweredAssumed Answered

USB CDC Bug in CubeMX firmware

Question asked by herzog.richard on Apr 11, 2014
Latest reply on Dec 27, 2017 by zhivko zivkovic
Hey everybody,

in order to initialize a working Virtual Com Port with CubeMX on the STM32F4 Discovery, I had to change

   pdev->pClassData = USBD_malloc(sizeof (USBD_CDC_HandleTypeDef));


   pdev->pClassData = (void *) USBD_malloc(sizeof (USBD_CDC_HandleTypeDef));

Without the type cast pClassData would always be null, which will make the parent function (USBD_CDC_Init in usbd_cdc.c) return before initializing the interface.
Btw I used firmware 1.1.0.

EDIT: Sorry, that was too fast. This line was actually not the root cause, it was the following line in USB_CDC.h

   #define CDC_DATA_HS_MAX_PACKET_SIZE        512

Changing that value to 256 made it work for me now. In case anyone is interested, here's a small code example for a vcp mirror. Add this code to CDC_Receive in usb_cdc_if.c

    static uint8_t buff_RX[256];
    static uint8_t buff_TX[256];
    int i = 0;
    for (i = 0; i < *Len; i++)
        buff_TX[i] = buff_RX[i];
    USBD_CDC_SetTxBuffer(&hUsbDeviceFS, &buff_TX[0], *Len);
    USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &buff_RX[0]);

Also you will have to add

    extern USBD_HandleTypeDef hUsbDeviceFS;

to this file.