cancel
Showing results for 
Search instead for 
Did you mean: 

USB CDC Bug in CubeMX firmware

richard239955
Associate II
Posted on April 11, 2014 at 10:07

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));

to

   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

   &sharpdefine 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_TransmitPacket(&hUsbDeviceFS);

    

    USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &buff_RX[0]);

    USBD_CDC_ReceivePacket(&hUsbDeviceFS);

Also you will have to add

    extern USBD_HandleTypeDef hUsbDeviceFS;

to this file.

 

#usb #cdc #cdc #cdc #cdc_receive_fs #full-echo #usb-cdc #vcp #vcp
49 REPLIES 49
schauer
Associate II
Posted on April 15, 2014 at 17:58

Hello Richard,

please be aware that changing

CDC_DATA_HS_MAX_PACKET_SIZE to something other than 512 breaks the USB specification, if you keep the bulk transfer used by the example (USB 2.0 Spec, Chapter 5.8.3) and transfer data in HighSpeed mode. I expected the latter as you described the _HS_ constant. If you do not have a HighSpeed connection,

CDC_DATA_HS_MAX_PACKET_SIZE

should not affect you at all, as the example in that case uses

CDC_DATA_FS_MAX_PACKET_SIZE

.

richard239955
Associate II
Posted on April 16, 2014 at 13:44

I have a FS connection since STM32F4 Discovery supports FS by default. And you're right, changing HS packet size should not affect FS connections. But when you take a look at the definition of USBD_CDC_HandleTypeDef in USB_CDC.h you will see that the data buffer uses the HS max packet size. This somehow interferes with FS connection setup. Any buffer size greater than 118 results in connection errors.

schauer
Associate II
Posted on April 16, 2014 at 14:21

Have you tried to statically assign the variable pdev->pClassData instead of using dynamically allocated memory through malloc? 

I rely on that code too, but I do not have that issue.

staylor
Associate
Posted on May 05, 2014 at 15:31

Hi Guys,

I think you will find the problem is the default sys start up generated (startup_stmfxxxxx.s)

The heap is set to 0x0200 by default - the malloc fails because there isn't enough space on the heap.

So increase the heap (at least 0x0400 - you will need more if you are using rtos as well etc.) and put back CDC_DATA_HS_MAX_PACKET_SIZE        512

in usbd_cdc_if.h - this will fix the malloc problem, and a bare bones cdc example will now work ( without the 'device could not start' driver error).

Anyone work how to do a composite device with the new libs - for instance cdc and hid

?

regards

derun_a
Associate II
Posted on May 07, 2014 at 10:49

Hello Richard,

I just started learning the STM32 and USB library.

I have tried your example for the echo function. It works.

Could you tell me please how to call the TX-function directly from the main.c file.

Thank your in advance. 

richard239955
Associate II
Posted on May 09, 2014 at 12:37

Thanks for the advice Simon. I will try this later.

Andrii: You will only have to add ''#include ''usbd_cdc.h'' to your private includes in main.c. Then you should be able to call the usb_cdc functions.

derun_a
Associate II
Posted on May 12, 2014 at 12:37

Richard: I added the include definition in the main.c and call     USBD_CDC_SetTxBuffer(&hUsbDeviceFS, &buff_TX[0], *Len);

USBD_CDC_TransmitPacket(&hUsbDeviceFS);

functions to transmit data. It works.

But I got another problem. Some times when I send data (just one byte enough )from the PC to MCU , the program enter in the Infinite_Loop:

  b  Infinite_Loop

because of the unexpected interrupt. 

I make power-off and power-on again. And the program works well, I can send infinite number of data. Next time I make power-ON  just one byte put the program in the  Infinite_Loop.

I thought that the problem in my addition code, I have commented  all my code. 

But problem still appears in your the echo-example too. 

May be someone have the same problem. I have no idea how to solve problem.

Thank you in advance for any help. 

ayyaramdas
Associate II
Posted on May 12, 2014 at 13:09

hi,

i amusing STM32CubeMX for the discovery board learning. i generated the project successfully, and i am able to control the LEDs correctly. but i have issue in CDC connectivity. i am quite new to both USB and STM32F4 please could guide me some tutorials or something. or please share the STM32CubeMX project with me, that would be of great help.

thanks,

Ram

ayyaramdas
Associate II
Posted on May 13, 2014 at 09:02

hi all,

i am very new to the STM32 area, i have successfully created a keil project using STM32CubeMX, now i compiled and flashed in the STM32F4 discovery board. i am able to control the LEDs. but i got stuck in USB in CDC mode. the board is working fine i already checked it with the COOCOX example project for CDC it works fine.  i am not sure what i miss in the Keil project , can any one please tell me what i need to do in the following function. how to connect, ( i have already installed the drivers that is required from the PC side.). 

static int8_t CDC_Init(void)

{

  /* USER CODE BEGIN 3 */ 

  return (0);

  /* USER CODE END 3 */ 

}