cancel
Showing results for 
Search instead for 
Did you mean: 

stm32 USB VCP Receive problem.

sycinfinity
Associate
Posted on May 18, 2016 at 07:26

stm32 USB VCP Receive problem. 

hello. I need help. 

I use STM32F4 eval board. and I want use ROTS and USB_HS.

my eval board has USB_OTG_FS and USB_OTG_HS. so I use USB_OTG_HS.

I select in cubeMX, USB_OTG_HS's Exteranl Phy -> Device_Only.

And select LWIP, FREERTOS, USB_DEVICE->Communication Device Class(Virtual Port Com).

Code generate, and success to connect USB_HS. but, when I transmit from board to my windows10 OS computer, It work well.

very good. but, Receive form computer to device, the data was not good. 

example , when I trans alphabet 'a', my board take it 'a\', when 'd', 'dAT', when 'c', 'c'..... 

I don't know why it works like this. 

This is my code. 

in main, init others pins, and devices. and at first RTOS defaultTask init USB HOST. 

----------------------------------------------------------------------

void StartDefaultTask(void const * argument)

{

  /* init code for LWIP */

  MX_LWIP_Init();

  /* init code for USB_DEVICE */

  MX_USB_DEVICE_Init();

  /* USER CODE BEGIN 5 */

  /* Infinite loop */

  

  for(;;)

  {

    osDelay(1);

  }

  /* USER CODE END 5 */ 

}

----------------------------------------------------------------------

and my other Task, I use USB code. like this. 

----------------------------------------------------------------------

void StartTask07(void const * argument)

{

  /* USER CODE BEGIN StartTask07 */

  /* Infinite loop */

  uint8_t TxUsb;

  USBD_CDC_SetRxBuffer(&hUsbDeviceHS, RxUsb);

  USBD_CDC_SetTxBuffer(&hUsbDeviceHS , &TxUsb, 1);*/

  osDelay(500);

  char MyRxBuff[16] = {'\0'};

  char testBuff[32];

  uint8_t size;

  uint32_t cnt = 0;

 for(;;)

  {

    if(hUsbDeviceHS.dev_state == USBD_STATE_CONFIGURED)

    {

      cnt++;

      sprintf(testBuff, '' myTest is %d \n'', cnt);

      Transmit(testBuff,strlen(testBuff));

      

      USBD_CDC_TransmitPacket(&hUsbDeviceHS);

   

     if(Receive(MyRxBuff) == USBD_OK)

     {

        size = strlen(MyRxBuff);

        Transmit(MyRxBuff,strlen(MyRxBuff));

     }

     else

     {

        USBD_CDC_SetTxBuffer(&hUsbDeviceHS , ''Hello\n'',7);

        USBD_CDC_TransmitPacket(&hUsbDeviceHS );

     } 

        

    }

   osDelay(500);

  }

}

----------------------------------------------------------------------

int8_t Transmit (char* Buf, uint16_t Len)

{

  /* USER CODE BEGIN 3 */

  uint8_t result = USBD_OK;

  

  USBD_CDC_SetTxBuffer(&hUsbDeviceHS, Buf, Len);

  result = USBD_CDC_TransmitPacket(&hUsbDeviceHS);

  return (0);

  /* USER CODE END 3 */

}

int8_t Receive (char* Buf)

{

  /* USER CODE BEGIN 3 */

  uint8_t result = USBD_OK;

  USBD_CDC_SetRxBuffer(&hUsbDeviceHS, Buf);

  result = USBD_CDC_ReceivePacket(&hUsbDeviceHS);

  return (0);

  /* USER CODE END 3 */

}

----------------------------------------------------------------------

this codes are in main.c.

and next code is generated by CubeMx USBD_LL_INIT.

--------------------------------------------------------------------------

USBD_StatusTypeDef  USBD_LL_Init (USBD_HandleTypeDef *pdev)

  /* Init USB_IP */

  if (pdev->id == DEVICE_HS) {

  /* Link The driver to the stack */

  hpcd_USB_OTG_HS.pData = pdev;

  pdev->pData = &hpcd_USB_OTG_HS;

  hpcd_USB_OTG_HS.Instance = USB_OTG_HS;

  hpcd_USB_OTG_HS.Init.dev_endpoints = 11;

  hpcd_USB_OTG_HS.Init.speed = PCD_SPEED_HIGH;

  hpcd_USB_OTG_HS.Init.dma_enable = DISABLE;

  hpcd_USB_OTG_HS.Init.ep0_mps = DEP0CTL_MPS_32;

  hpcd_USB_OTG_HS.Init.phy_itface = USB_OTG_ULPI_PHY;

  hpcd_USB_OTG_HS.Init.Sof_enable = DISABLE;

  hpcd_USB_OTG_HS.Init.low_power_enable = DISABLE;

  hpcd_USB_OTG_HS.Init.lpm_enable = ENABLE;

  hpcd_USB_OTG_HS.Init.vbus_sensing_enable = ENABLE;

  hpcd_USB_OTG_HS.Init.use_dedicated_ep1 = DISABLE;

  hpcd_USB_OTG_HS.Init.use_external_vbus = DISABLE;

  HAL_PCD_Init(&hpcd_USB_OTG_HS);

  HAL_PCDEx_SetRxFiFo(&hpcd_USB_OTG_HS, 0x200);

  HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_HS, 0, 0x80);

  HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_HS, 1, 0x174);

  }

  return USBD_OK;

}

--------------------------------------------------------------------------

and next is my TeraTerm result. 

aW myTest is 23809

aW myTest is 23810

aW myTest is 23811

aW myTest is 23812

c myTest is 23813

dAT myTest is 23814

dAT myTest is 23815

dAT myTest is 23816

l@ myTest is 23817

l@ myTest is 23818

i myTest is 23819

i myTest is 23820

i myTest is 23821

i myTest is 23822

i myTest is 23823

j&&sharp16; myTest is 23824

j&&sharp16; myTest is 23825

l@ myTest is 23826

hAQ myTest is 23827

hAQ myTest is 23828

hAQ myTest is 23829

o myTest is 23830

o myTest is 23831

o myTest is 23832

pA[ myTest is 23833

pA[ myTest is 23834

pA[ myTest is 23835

''myTest is %d'' are Trans from device. and front alphabet is wrong data. 

Thank you. 

#stm32-cubemx-usb-vcp-stm32f4
2 REPLIES 2
Walid FTITI_O
Senior II
Posted on September 01, 2016 at 19:28

Hi, 

It may be related to a bug in CubeMx which enable by default the Vbus sensing. It should be fixed in the last version.

As a work around :

Replace :

hpcd_USB_OTG_HS.Init.vbus_sensing_enable = ENABLE;

by 

hpcd_USB_OTG_HS.Init.vbus_sensing_enable = DISABLE;

-Hannibal-

christoph2399
Associate II
Posted on September 02, 2016 at 08:58

do you still have the problem with receiving?

maybe this is helping: 

/fe800d0