cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 USB CDC Device: transmission stalls after reconnecting the board

j_filipe
Senior

Greetings! 
So I have a personal project, using a STM32U585 MCU, with a self-designed board, in which I have a bootloader and the main firmware. 
So basically I implemented a protocol for communicating with the board, I have a C program that sends a command and the board sends data (around 20Kb), using USB.
The scenario is the following: I flash the firmware with the bootloader, after flashing, the bootloader jumps into the main program and starts running, I send a command (via host) to get data and the board sends me data and everything is running smoothly. The problem starts when I unplug the board and plug it again, when I send the command to get data, the board starts to send it and then it completely freezes. 
Another curiosity, this situation only happens when I plug the board in a USB hub... if I plug the board directly to the laptop, I won't have any problem. 
Any clue of what can be the problem? Power related? Is there any issue with the jump to the main program? 

Best regards! 

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Super User

> The problem starts when I unplug the board and plug it again, when I send the command to get data, the board starts to send it and then it completely freezes.

Sounds like a program bug.

Debug your program to find out what is happening. You can connect to a chip without resetting it if you modify the debug configuration to not download or reset the chip.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

6 REPLIES 6
TDK
Super User

> The problem starts when I unplug the board and plug it again, when I send the command to get data, the board starts to send it and then it completely freezes.

Sounds like a program bug.

Debug your program to find out what is happening. You can connect to a chip without resetting it if you modify the debug configuration to not download or reset the chip.

If you feel a post has answered your question, please click "Accept as Solution".
Andrew Neil
Super User

@j_filipe wrote:

 I have a C program that sends a command 


Are you sure that your program copes correctly with its COM port disappearing and reappearing?

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

@Andrew Neil I forgot to mention, I also close the program from the host and connect again. The thing is, this only happens when I connect the board to an USB hub...

gbm
Principal

1. Make sure you are calling the Transmit from an ISR of the same priority as the USB interrupt, NOT from main() or any function called form main().

2. Make sure you don't attempt to transmit anything until the connection is established - this is not exactly an easy thing with ST USB stack, cause Disconnect callback is called after the device is connected for the second time, not when it's disconnected. Also, you should ensure that CDC connection is established by handling the class requests.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice

@j_filipe wrote:

 this only happens when I connect the board to an USB hub...


Instrument your code so that you can see what happens differently between the working & non-working cases.

 

As @TDK said, you can connect to a running target without having to download or reset - see here.

 

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

@TDK 
Yes, it was a program bug related to the jump from the bootloader to the main program. I was de-initialising some stuff that I shouldn't. Thank you anyway. 

Best regards!