Problem (solved?) with USB CDC
While working on a project based on STM32F202VG, I experienced intermittent and unexplainable problems with USB used with CDC class.
Sometimes everything was working perfectly, but after power-cycling the board or the PC, opening the COM port failed with a Windows 'Incorrect parameter' error.
After an in-depth analysis, I discovered a difference between correc' and incorrect initialization.
The two sequences are identical up to a certain point. Then, there is a difference in a setup packet from the board to the PC.
See the transcript from Device Monitoring Studio:
GOOD
Control Transfer (UP), 2017-05-14 18:15:21,8528439 +0,0001177. (1. Device: STMicroelectronics Virtual COM Port (COM13)) Status: 0x00000000
Pipe Handle: Control Pipe00 00 00 00 00 00 00
BAD
Control Transfer (UP), 2017-05-14 18:14:31,2491097 +0,0001700. (1. Device: STMicroelectronics Virtual COM Port (COM13)) Status: 0x00000000
Pipe Handle: Control Pipe80 64 06 2B 3E 50 1B
The only difference is that the unused part of the packet does contain zeroes, or does not.
As a blind test, I did this change to the CubeMX file usbd_cdc.c, row 516 :
...
pdev->pClassData = USBD_malloc(sizeof (USBD_CDC_HandleTypeDef));
memset(pdev->pClassData,0,sizeof(USBD_CDC_HandleTypeDef)); // THIS LINE WAS ADDED
if(pdev->pClassData == NULL)
...
After this change, CDC works everytime the board is turned on.
If the patch is removed, the intermittent problem returns.
Nonetheless, i cannot consider the bug as fixed. I don't understand if this patch solves something, and why...but after all I am not an USB expert !!
Can someone shed a light on this issue?
If needed, I have the full trasncripts of the 'good' and 'bad' USB initialization sequence.
I am using STM32Cube FW_F2 V1.6.0
#cube-mx #usb_cdc