1

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);
  }
}
Mehran
  • 307
  • 1
  • 3
  • 15
  • 2
    I am pretty sure there should be some initialization code around... – Eugene Sh. Nov 14 '16 at 21:41
  • I watched 2 videos on youtube, both are same as I did and works well for them! – Mehran Nov 14 '16 at 21:57
  • 1
    There is a bug in `CDC_Transmit_FS` function, and I guess, you have a problem that is described here: http://electronics.stackexchange.com/questions/161772/stm32-usb-vcp-bug – SergeyLebedev Nov 14 '16 at 22:59

5 Answers5

11

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.

Werner Daehn
  • 577
  • 6
  • 16
  • Thanks for this. I did not have to change data sizes (point 2) but I **did** have to add the CDC_XXX_LINE_CODING cases. That was critical. Although I did the stack and heap changes, I did not test the original condition. I also _felt_ that the ST Micro USB drivers on windows 10 were flaky, and I uninstalled them and used the Windows default serial drivers and all has worked flawlessly since. – Edward Dec 27 '17 at 18:48
  • 1
    Just commenting: this item #3 is still relevant in May 2020 – Edward May 22 '20 at 10:53
7

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);
  }
}
SergeyLebedev
  • 3,673
  • 15
  • 29
2

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

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.

Michel Sanches
  • 428
  • 3
  • 9
  • The driver was installed, the problem is solved as Sergey told the USB needs a delay for initialization. – Mehran Nov 19 '16 at 11:59
0

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
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
J.H
  • 21
  • 4