2014-04-11 01:07 AM
Hey everybody,
in order to initialize a working Virtual Com Port with CubeMX on the STM32F4 Discovery, I had to changepdev->pClassData = USBD_malloc(sizeof (USBD_CDC_HandleTypeDef));
topdev->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 512Changing 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 #vcp2014-04-15 08:58 AM
Hello Richard,
please be aware that changingCDC_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 usesCDC_DATA_FS_MAX_PACKET_SIZE
.2014-04-16 04:44 AM
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.
2014-04-16 05:21 AM
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.2014-05-05 06:31 AM
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 ? regards2014-05-07 01:49 AM
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.2014-05-09 03:37 AM
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.2014-05-12 03:37 AM
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_Loopbecause 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.2014-05-12 04:09 AM
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,Ram2014-05-13 12:02 AM
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 */ }