cancel
Showing results for 
Search instead for 
Did you mean: 

Cube USB virtual com port bug.

kalbar666
Associate II
Posted on September 17, 2015 at 13:11

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.


static uint8_t USBD_CDC_Init (USBD_HandleTypeDef *pdev, 

uint8_t cfgidx)

{

uint8_t ret = 0;

USBD_CDC_HandleTypeDef *hcdc;


if(pdev->dev_speed == USBD_SPEED_HIGH ) 

{ 

/* Open EP IN */

USBD_LL_OpenEP(pdev,

CDC_IN_EP,

USBD_EP_TYPE_BULK,

CDC_DATA_HS_IN_PACKET_SIZE);


/* Open EP OUT */

USBD_LL_OpenEP(pdev,

CDC_OUT_EP,

USBD_EP_TYPE_BULK,

CDC_DATA_HS_OUT_PACKET_SIZE);


}

else

{

/* Open EP IN */

USBD_LL_OpenEP(pdev,

CDC_IN_EP,

USBD_EP_TYPE_BULK,

CDC_DATA_FS_IN_PACKET_SIZE);


/* Open EP OUT */

USBD_LL_OpenEP(pdev,

CDC_OUT_EP,

USBD_EP_TYPE_BULK,

CDC_DATA_FS_OUT_PACKET_SIZE);

}

/* Open Command IN EP */

USBD_LL_OpenEP(pdev,

CDC_CMD_EP,

USBD_EP_TYPE_INTR,

CDC_CMD_PACKET_SIZE);



pdev->pClassData = USBD_malloc(sizeof (USBD_CDC_HandleTypeDef));


if(pdev->pClassData == NULL)

{

ret = 1; 

}

else

{

memset(pdev->pClassData, 0, sizeof (USBD_CDC_HandleTypeDef));


hcdc = (USBD_CDC_HandleTypeDef*) pdev->pClassData;


/* Init physical Interface components */

((USBD_CDC_ItfTypeDef *)pdev->pUserData)->Init();


/* Init Xfer states */

hcdc->TxState =0;

hcdc->RxState =0;


if(pdev->dev_speed == USBD_SPEED_HIGH ) 

{ 

/* Prepare Out endpoint to receive next packet */

USBD_LL_PrepareReceive(pdev,

CDC_OUT_EP,

hcdc->RxBuffer,

CDC_DATA_HS_OUT_PACKET_SIZE);

}

else

{

/* Prepare Out endpoint to receive next packet */

USBD_LL_PrepareReceive(pdev,

CDC_OUT_EP,

hcdc->RxBuffer,

CDC_DATA_FS_OUT_PACKET_SIZE);

}



}

return ret;

}

#cube-stm32f4-usb-virtual-com
1 REPLY 1
Eugene Toichkin
Associate
Posted on March 27, 2018 at 19:18

Almost three years past but that bug is ithe same.

I have stumbled on it in 2018, march.

'memset' on line 51 also solve issue with

hUSBDeviceFS.dev_state == USB_STATE_CONFIGURED (Device connected and configuired) while

((USBD_CDC_HandleTypeDef*)hUSBDeviceFS.pClassData)->RxLength (length of read data) goes absolutely random number just after device connected and NO data received from host.