Solved: USB serial CDC stalls at initial connection

Post here all questions related to STM32 core if you can't find a relevant section!
Post Reply
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Solved: USB serial CDC stalls at initial connection

Post by ag123 »

hi.

This issue pertains to the STM official core for USB CDC Serial.
i'm not too sure if i'm the only one observing this or that it is rather common.

I've been seeing that usb (cdc) serial initially stalls could be into the 10s of seconds after a reset.
a test sketch is something like this

Code: Select all

#include <Arduino.h>
#include <USBSerial.h>

uint32_t begin;

void setup() {
	pinMode(LED_BUILTIN, OUTPUT);
	Serial.begin();
	Serial.println("press space to begin");
	while(1) {
		if(Serial.available()) {
			int16_t c = Serial.read();
			if(c == ' ')
				break;
		}
	}
	while(Serial.available()) Serial.read();
	begin = millis();
}

void loop() {
	//echo the characters back
	if(Serial.available()) {
		int16_t c = Serial.read();
		Serial.write(c);
		if(c == '\r')
			Serial.write('\n');
	}
	if(millis() - begin > 500) {
		digitalToggle(LED_BUILTIN);
		begin = millis();
	}

	asm("wfi");
}
in setup() i'm waiting for a key press, in this case i specifically wait for a ' ' space key.
a thing is serial (i.e. the virtual comm port) would stall/freeze, i'm using linux so that is /dev/ttyACM0

this did appear in dmesg

Code: Select all

[ 9685.396495] usb 3-4: new full-speed USB device number 14 using xhci_hcd
[ 9685.545724] usb 3-4: New USB device found, idVendor=0483, idProduct=5740
[ 9685.545726] usb 3-4: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 9685.545726] usb 3-4: Product: BLACKPILL_F401CC CDC in FS Mode
[ 9685.545727] usb 3-4: Manufacturer: STMicroelectronics
[ 9685.545727] usb 3-4: SerialNumber: 388236403135
[ 9685.546138] cdc_acm 3-4:1.0: ttyACM0: USB ACM device
but that there would be no response in the serial monitor (putty), in fact the serial monitor (e.g. putty) simply won't connect
and reports and error with the serial port.
https://www.putty.org/
this happens for some like at least 10 seconds or more after a reset.
in the above code, usb-serial is responsive after the connection is established. but the initial freeze tends to be irritating as it stalls for quite a while and connect fails.

the mcu board used is stm32f401ccu black pill. but that i observe similar on stm32f103 pill type boards
by mlundin » Fri Apr 09, 2021 9:59 am
A very common reason for connection problems under Linux is that the Modem Manager connects to the serial port automagically and blocks access from other applications. I dont have such a system set up for testing at the moment so cannot help much more, but I remember that I have previously uninstalled the Modem Manager from Linux systems.
Go to full post
Last edited by ag123 on Fri Apr 09, 2021 5:18 pm, edited 2 times in total.
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 27
Location: Prudnik, Poland

Re: USB serial CDC stalls at initial connection

Post by GonzoG »

I don't have any problems with this code (on F103, F401 and F411) on windows.
May there is some issue with linux connecting to STM.
Try adding

Code: Select all

while(!Serial){}
after Serial.begin();

This way, it will wait until usb connection is established .
mlundin
Posts: 94
Joined: Wed Nov 04, 2020 1:20 pm
Answers: 6
Location: Sweden

Re: USB serial CDC stalls at initial connection

Post by mlundin »

A very common reason for connection problems under Linux is that the Modem Manager connects to the serial port automagically and blocks access from other applications. I dont have such a system set up for testing at the moment so cannot help much more, but I remember that I have previously uninstalled the Modem Manager from Linux systems.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: USB serial CDC stalls at initial connection

Post by ag123 »

thanks, you may be right about it, i'd check that first. it could be that it is trying to 'reset' the port.

hi thanks all,

the problem is apparently resolved after i edited
/etc/udev/rules.d/45-maple.rules
and add

Code: Select all

ATTRS{idProduct}=="5740", ATTRS{idVendor}=="0483", MODE="664", GROUP="plugdev" SYMLINK+="maple" ENV{ID_MM_DEVICE_IGNORE}="1"
that ENV{ID_MM_DEVICE_IGNORE}="1" made the difference
Post Reply

Return to “General discussion”