AnsweredAssumed Answered

CubeMX STN32F7 CDC Example: Enumerates, but randomly fails to connect

Question asked by softport . on Jan 30, 2018

I have spent a few days puzzling over why the CDC example would consistently enumerate, that is it would show up in the task manager (Windows 8) as STMicroelectronics Virtual COM Port with no errors, but trying to connect with Putty or Realterm would randomly fail.

 

By 'randomly' I mean that any trivial change in the code, like deleting or adding comments, adding a line of code unrelated to CDC, would make the CDC sometimes work or break. It seems like I have found the problem, that is it now works consistently, however it does not explain the previous random behaviour.

 

In file usbd_cdc_if.c, there is this function:

static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length)

 

As I understand it, it processes usb control requests from the host, and consists of a 'switch' statement which handles each control request. Unfortunately, none of the switch 'case' statements have any code. I found that after plugging in the usb cable, the host was making a control request: CDC_GET_LINE_CODING. Since there was no code, the host would not get a response, so would not connect.

 

case CDC_GET_LINE_CODING:

   // This is the code I added:

   pbuf[0] = 0x20;  // bits/second 115200  (= 0x0001c220)
   pbuf[1] = 0xc2;
   pbuf[2] = 0x01;
   pbuf[3] = 0x00;
   pbuf[4] = 0x00; // 1 stop bit
   pbuf[5] = 0x00; // parity none
   pbuf[6] = 0x08; // 8 data bits

   // End added code

break;

 

It's now been working through several code changes, although I'm still nervously keeping my fingers crossed.

A personal note: Could ST please complete the example by filling in all the cases, instead of leaving them blank? This would be much appreciated.

 

One other change I made (before adding the above code) was to change the parser to "GNU Elf Parser":

Project->Properties->C/C++ Build->Settings->Binary Parsers

It was previously set to "Elf Parser".

 

STM32Cube V1.0, Version 4.24.0,

Peripherals: OSB_OTG_FS (device only), USART1 (Asyncronous), SYS (JTAG 5pin, System wake-up, SysTick),

   RCC(HSE & LSE crystal)

MiddleWares: USB_Device(FS IP Virtual com port)
Microcontroller package STM32CubeFW_F4V1.19.0

 

System Workbench for STM32 - OpenOCD (for Windows 32bits),
Version: 1.13.2.201703061529

Outcomes