AnsweredAssumed Answered

Cube USB virtual com port bug.

Question asked by mikey on Sep 17, 2015
Latest reply on Mar 27, 2018 by Eugene Toichkin
Hello,

I have found a bug in USB device stack for virtual com port implementation generated by CUBE for stm32 chips. The symptoms are that sometimes it is possible to connect to the device from PC sometimes it is not (connection error 10). After debugging turned out that during host interrogation STM chip was sending random rubbish as a response to class requests which sometimes was an acceptable rubbish, sometimes not. In file usbd_cdc.c in function USBD_CDC_Init() line 43 is dynamic memory allocation. This uninitialised memory is later on being used as an answer during host interrogation. By adding memory initialization in line 51 the issue gets solved and there are no longer problems with USB connection.
01.static uint8_t  USBD_CDC_Init (USBD_HandleTypeDef *pdev,
02.                               uint8_t cfgidx)
03.{
04.  uint8_t ret = 0;
05.  USBD_CDC_HandleTypeDef   *hcdc;
06.   
07.  if(pdev->dev_speed == USBD_SPEED_HIGH  )
08.  
09.    /* Open EP IN */
10.    USBD_LL_OpenEP(pdev,
11.                   CDC_IN_EP,
12.                   USBD_EP_TYPE_BULK,
13.                   CDC_DATA_HS_IN_PACKET_SIZE);
14.     
15.    /* Open EP OUT */
16.    USBD_LL_OpenEP(pdev,
17.                   CDC_OUT_EP,
18.                   USBD_EP_TYPE_BULK,
19.                   CDC_DATA_HS_OUT_PACKET_SIZE);
20.     
21.  }
22.  else
23.  {
24.    /* Open EP IN */
25.    USBD_LL_OpenEP(pdev,
26.                   CDC_IN_EP,
27.                   USBD_EP_TYPE_BULK,
28.                   CDC_DATA_FS_IN_PACKET_SIZE);
29.     
30.    /* Open EP OUT */
31.    USBD_LL_OpenEP(pdev,
32.                   CDC_OUT_EP,
33.                   USBD_EP_TYPE_BULK,
34.                   CDC_DATA_FS_OUT_PACKET_SIZE);
35.  }
36.  /* Open Command IN EP */
37.  USBD_LL_OpenEP(pdev,
38.                 CDC_CMD_EP,
39.                 USBD_EP_TYPE_INTR,
40.                 CDC_CMD_PACKET_SIZE);
41.   
42.     
43.  pdev->pClassData = USBD_malloc(sizeof (USBD_CDC_HandleTypeDef));
44.   
45.  if(pdev->pClassData == NULL)
46.  {
47.    ret = 1;
48.  }
49.  else
50.  {
51.    memset(pdev->pClassData, 0, sizeof (USBD_CDC_HandleTypeDef));
52. 
53.    hcdc = (USBD_CDC_HandleTypeDef*) pdev->pClassData;
54.     
55.    /* Init  physical Interface components */
56.    ((USBD_CDC_ItfTypeDef *)pdev->pUserData)->Init();
57.     
58.    /* Init Xfer states */
59.    hcdc->TxState =0;
60.    hcdc->RxState =0;
61.        
62.    if(pdev->dev_speed == USBD_SPEED_HIGH  )
63.    {     
64.      /* Prepare Out endpoint to receive next packet */
65.      USBD_LL_PrepareReceive(pdev,
66.                             CDC_OUT_EP,
67.                             hcdc->RxBuffer,
68.                             CDC_DATA_HS_OUT_PACKET_SIZE);
69.    }
70.    else
71.    {
72.      /* Prepare Out endpoint to receive next packet */
73.      USBD_LL_PrepareReceive(pdev,
74.                             CDC_OUT_EP,
75.                             hcdc->RxBuffer,
76.                             CDC_DATA_FS_OUT_PACKET_SIZE);
77.    }
78.     
79.     
80.  }
81.  return ret;
82.}



Outcomes