STM32CubeMX USB CDC VCP?

I found a large number of examples, but nothing about how to do this “correctly” from the STM32MXCube.

How to create skeletal code from STM32CubeMX for exchanging USB CDC virtual COM ports (if possible, STM32F4 detection)?

+7
usb stm32 stm32f4discovery
source share
3 answers

The STM32CubeMX project for Discovery F4 with CDC as a USB device should work out of the box. Assuming you are using the updated STM32CubeMX and library:

  • Launch STM32CubeMX
  • Choose a Discovery F4 board
  • Enable only UBS_OTG_FS peripheral (leave the disable checkbox checked)
  • Enable the middle program USB_Device Communication ... aka CDC

On the clock tab, verify that the clock source is HSE HCLK. It should give HLCK 168 MHz and 48 MHz at a frequency of 48 μs (USB). Check for red color anywhere.

Save project

Generate code (I used SW4STM32 program chains)

Build (you may have to switch to an internal CDT constructor against GNU make).

Now add the code to send data through the COM port, and it will work.

In fact, the hard part is not trying to access the "CDC" until the host USB host is connected (there is no CDC installation yet)

Here's how I did it to quickly emit a test:

In the usbd_cdc_if.c file

uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len) { uint8_t result = USBD_OK; /* USER CODE BEGIN 7 */ if (hUsbDevice_0 == NULL) return -1; USBD_CDC_SetTxBuffer(hUsbDevice_0, Buf, Len); result = USBD_CDC_TransmitPacket(hUsbDevice_0); /* USER CODE END 7 */ return result; } static int8_t CDC_DeInit_FS(void) { /* USER CODE BEGIN 4 */ hUsbDevice_0 = NULL; return (USBD_OK); /* USER CODE END 4 */ } 

In the main.c file

 /* USER CODE BEGIN Includes */ #include "usbd_cdc_if.h" /* USER CODE END Includes */ .... /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ uint8_t HiMsg[] = "hello\r\n"; CDC_Transmit_FS(HiMsg, strlen(HiMsg)); HAL_Delay(200); } 

As soon as you connect micro USB (CN5), CDC data will be displayed on the main terminal.

It works. I see hello on the terminal (you may need to install the driver, http://www.st.com/web/en/catalog/tools/PF257938 ).

To receive it, it must first be armed with, say, the first call to USBD_CDC_ReceivePacket () in a good place. There may be CDC_Init_FS for this.

You can then process the data as it arrives at CDC_Receive_FS and restart the reception from here again.

This works for me.

 static int8_t CDC_Receive_FS (uint8_t* Buf, uint32_t *Len) { /* USER CODE BEGIN 6 */ USBD_CDC_ReceivePacket(hUsbDevice_0); return (USBD_OK); /* USER CODE END 6 */ } static int8_t CDC_Init_FS(void) { hUsbDevice_0 = &hUsbDeviceFS; /* USER CODE BEGIN 3 */ /* Set Application Buffers */ USBD_CDC_SetTxBuffer(hUsbDevice_0, UserTxBufferFS, 0); USBD_CDC_SetRxBuffer(hUsbDevice_0, UserRxBufferFS); USBD_CDC_ReceivePacket(hUsbDevice_0); return (USBD_OK); /* USER CODE END 3 */ } 
+11
source share

There are several STM32F4 detection boards supported by the STM32Cube software, and you did not indicate what you are using, but Ive had exactly the same problem with the Discovery board with the MCU F401VCT.

After installing the STM virtual COM port driver, Windows Device Manager showed the STMicroelectronics virtual COM port, but with a yellow warning sign. The COM port was not accessible with the terminal application ( PuTTY ).

As a result, I found that there is a problem with the source code output from the STMCube program . But there is a simple fix:

  • Open a new STM32Cube project and enable USB_OTG_FS as the device Only and select CDC Virtual Port COM from MiddleWares USB_Device pop-up menu.
  • Generate source code without any changes necessary for any USB settings.
  • In the usbd_cdc_if.c file, change #define USB_HS_MAX_PACKET_SIZE from 512 to 256 .
  • In the usbd_cdc.c file, change #define CDC_DATA_HS_MAX_PACKET_SIZE from 512 to 256 .

After that, the yellow warning disappeared from the device manager, and I could receive data in the CDC_Receive_FS function (in the usbd_cdc_if.c file) when using PuTTY. Keep in mind that these definitions return to their incorrect values ​​each time the STM32Cube generates code, and I have not yet found a way around this.

Hope this helps.

+4
source share

iChal fix helped remove the yellow warning label.

I would like to mention that USB_HS_MAX_PACKET_SIZE now in usbd_def.h and CDC_DATA_HS_MAX_PACKET_SIZE is in usbd_cdc.h

I am using STM32CubeMX v4.11.0 STM32Cube v1.0 and STM32F401C-DISCO.

In future work, I should only set the heap size to a larger value. I set it to 0x600 since I also have FreeRTOS. I use IAR EWARM, so the change is made in the linker of the script stm32f401xc_flash.icf .

+3
source share

All Articles