1
votes

I generated a code for "stm32f103c8t6" with CubeMX for USB VCP, when I add "CDC_Transmit_FS" command to send data, the port isn't recognized by windows10! what should I do? Here is the code which is compiled without error:

#include "stm32f1xx_hal.h"
#include "usb_device.h"
#include "usbd_cdc_if.h"

int main(void)
{
  uint8_t Text[] = "Hello\r\n";
  while (1)
  {
    CDC_Transmit_FS(Text,6); /*when commented the port is recognized*/
        HAL_Delay(1000);
  }
}
5
I am pretty sure there should be some initialization code around...Eugene Sh.
I watched 2 videos on youtube, both are same as I did and works well for them!Mehran
There is a bug in CDC_Transmit_FS function, and I guess, you have a problem that is described here: electronics.stackexchange.com/questions/161772/…SergeyLebedev

5 Answers

10
votes

There are three things you need to check in my experience:

  1. startup_stm32f405xx.s --> Increase the Heap size. I use heap size 800 and stack size 800 as well.
  2. usbd_cdc_if.c --> APP_RX_DATA_SIZE 64 and APP_TX_DATA_SIZE 64
  3. usbd_cdc_if.c --> add below code to the CDC_Control_FS() function

Code:

case CDC_SET_LINE_CODING:
  tempbuf[0]=pbuf[0];
  tempbuf[1]=pbuf[1];
  tempbuf[2]=pbuf[2];
  tempbuf[3]=pbuf[3];
  tempbuf[4]=pbuf[4];
  tempbuf[5]=pbuf[5];
  tempbuf[6]=pbuf[6];
  break;
case CDC_GET_LINE_CODING:
  pbuf[0]=tempbuf[0];
  pbuf[1]=tempbuf[1];
  pbuf[2]=tempbuf[2];
  pbuf[3]=tempbuf[3];
  pbuf[4]=tempbuf[4];
  pbuf[5]=tempbuf[5];
  pbuf[6]=tempbuf[6];
  break;

and define the uint8_t tempbuf[7]; in the user private_variables section.

Without the increased heap size, Windows does not react at all. Without the point 3, Windows will send the baud rate information and then read the baud rate, expecting to get back the same values. Since you do not return any values, the virtual com port remains as driver-not-loaded.

If you do all of that, the Windows 10 out-of-the-box VCP driver can be used. No need to install the very old ST VCP driver on your system.

PS: I read somewhere turning on VSense makes problems, too. Don't know, I have not configured it and all works like a charm.

6
votes

Put delay before CDC_Transmit_FS call - it will wait for the initiatialization. Your code should be like this

int main(void)
{
  uint8_t Text[] = "Hello\r\n";
  HAL_Delay(1000);
  while (1)
  {
    CDC_Transmit_FS(Text,6); /*when commented the port is recognized*/
        HAL_Delay(1000);
  }
}
1
votes

I had similar issue. I couldn't connect to a port and the port appears as just "virtual com port". I added while loop to wait for USBD_OK from CDC_Transmit_FS. Then it stars work even with out it or a delay after init function. I am not sure what the issue was.

while(CDC_Transmit_FS((uint8_t*)txBuf, strlen(txBuf))!=USBD_OK)
 {
 }
0
votes

you may have to install driver to get device recognized as com port you can get it from st site if not installed the device is listed with question or exclamation mark on device manager

note that you cannot send until device get connected to host! not sure that CubeMX CDC_Transmit_FS is checking for this also instead of delay to resend you shall check the CDC class data "TXSstate" is 0 mean tx is over.

0
votes

I know it's a bit late, but I stumbled upon this post and it was extremely helpful.

Here is what I needed to do:

  • do the Line-Coding (I think only necessary on Windows-Systems)
  • increase Heap (Stack was left at default 0x200)

Here is what wasn't necessary for me (on a STM32F405RGT6 Chip):

  • change APP_RX_DATA_SIZE / APP_TX_DATA_SIZE (left it at 2048)
  • add a delay befor running CDC_Tranmit_FS()

Also some things to consider that happened to me in the past:

  • be sure to use a USB-Cable with data lines (most charging-cables don't have them)
  • double check the traces/connections if you use a custom board