Serial work only after disconnect/reconnect USB !??? (solved)

Post here first, or if you can't find a relevant section!
Post Reply
User avatar
ManX84
Posts: 20
Joined: Tue Oct 17, 2023 3:30 pm

Serial work only after disconnect/reconnect USB !??? (solved)

Post by ManX84 »

Hello all, we are here for a new STM32duino mysteries!

First my configuration : ST32 core version 2.6.0
uC: STM32F103C8 (generic)
USB Support : "None"
USART Support : "Enable generic serial"
Upload Methode : "STM32CubeProgrammer (Serial)"

IDE : Arduino 2 (for a simple situation every body could test)


Well I wrote this ultra simple code (could not be more simple, this is the first thing I learned to my son !) :

Code: Select all

void setup() {

  while(!Serial);
  Serial.setRx(PA10); // using pin name
  Serial.setTx(PA9);
  Serial.begin(115200);

}

void loop() {
  Serial.println("STM32Duino Forum !!!");
  delay(200);
}
First problem : at the end of upload program I open a serial monitor (any, Serial Monitor from IDE, or a Putty), then reset the STM32F103 ... nothing append ! It work ONLY if I disconnect/Connect the cable!

Second problem : By default Serial would use usart 1, not ?? so why if I remove pin assignment (SetRx and SetTx) it is not working (even after disconnect/connect)

All this serial stuff, who normally must work from scratch as simple as push a button look very "special" on STM32 !!!!!!
(I spent 4 hours before found I need to connect / unconnect, and I didnt told you all the story, it was a pity!)

So after all of that, Is ther somebody here who could clarify STM32duino serial ??? (Or am I the only one struggling with such a simple thing?)

Thanks Guys !
Last edited by ManX84 on Fri Oct 20, 2023 2:39 pm, edited 1 time in total.
Exposing your opinion is good, exposing your code is better! :mrgreen:
User avatar
ManX84
Posts: 20
Joined: Tue Oct 17, 2023 3:30 pm

Re: Serial work only after disconnect/reconnect USB !???

Post by ManX84 »

Same with Arduino IDE 1.8.19
Exposing your opinion is good, exposing your code is better! :mrgreen:
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Serial work only after disconnect/reconnect USB !???

Post by ag123 »

select:
"USB Support ( CDC generic 'Serial' supercede u(s)art ) " from the menu to get usb-serial, i.e. Serial.println("hello"); goes to your serial monitor.

https://github.com/stm32duino/Arduino_C ... wareserial
The STM32 MCU's have several U(S)ART peripherals. By convenience, the U(S)ARTx number is used to define the Serialxinstance:

Serial1 for USART1
Serial2 for USART2
Serial3 for USART3
Serial4 for UART4
so your uart would be Serial1, Serial2 etc.
otherwise do it as like the example given

Code: Select all

//                      RX    TX
HardwareSerial Serial1(PA10, PA9);

void setup() {
  Serial1.begin(115200); 
}

void loop() {
  Serial1.println("Hello World!");
  delay(1000);
}
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Serial work only after disconnect/reconnect USB !???

Post by fpiSTM »

Simply do while(!Serial); after the begin
User avatar
ManX84
Posts: 20
Joined: Tue Oct 17, 2023 3:30 pm

Re: Serial work only after disconnect/reconnect USB !???

Post by ManX84 »

@fpiSTM : no this not working
Exposing your opinion is good, exposing your code is better! :mrgreen:
User avatar
ManX84
Posts: 20
Joined: Tue Oct 17, 2023 3:30 pm

Re: Serial work only after disconnect/reconnect USB !???

Post by ManX84 »

@ag123 :

Code: Select all

HardwareSerial Serial1(PA10, PA9);

void setup() {
  
  // Serial.setRx(PA10); // using pin name
  // Serial.setTx(PA9);
  // Serial.begin(115200);
  Serial1.begin(115200);
  while(!Serial1);
}

void loop() {
  Serial1.println("STM32Duino Forum !!!");
  delay(200);
}
This code work, BUT only after connect/unconnect USB.
BUT this clarify the compilator setting to use multiple usuart. This is nice.

I think my problem come from STM32CubeProgrammer who not release the port ..
it is call by this script :
C:\...\AppData\Local\arduino15\packages\STMicroelectronics\tools\STM32Tools\2.2.1\stm32CubeProg.sh

is there an alternative cli tools that I could use ?
Exposing your opinion is good, exposing your code is better! :mrgreen:
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Serial work only after disconnect/reconnect USB !???

Post by fpiSTM »

Some doc about HardwareSerial:
https://github.com/stm32duino/Arduino_C ... wareserial

Default Serial instance of the generic:
https://github.com/stm32duino/Arduino_C ... #L140-L145

So it uses PA2/PA3 which uses USART2. This means default Serial instance is mapped to Serial2.
You can redefine those pins using the build_opt.h:
https://github.com/stm32duino/Arduino_C ... uild_opt.h

With:

Code: Select all

-DPIN_SERIAL_RX=PA10 -DPIN_SERIAL_TX=PA9 -DSERIAL_UART_INSTANCE=1
About CubeProgrammer, it should be released and the board reset. Maybe your board does not properly reset, hardware issue?
User avatar
ManX84
Posts: 20
Joined: Tue Oct 17, 2023 3:30 pm

Re: Serial work only after disconnect/reconnect USB !???

Post by ManX84 »

@fpiSTM:

Yes, this is an hard issue !
Why ? because when I started to work with this board I modified the stm32CubeProg.sh like it command RTS and DTR line (on my board RESET condition is achieved with RTS = 1 and DTR = 0, Boot mode is with RTS = 1 and DTR = 0 ). But I didn't release correctly these lines and after STMCubeProgrammer finish the job, RTS and DTR was not in the right levels !

All done, thanks guys for your hep and clarify some points !
Exposing your opinion is good, exposing your code is better! :mrgreen:
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Serial work only after disconnect/reconnect USB !??? (solved)

Post by ag123 »

why not use usb-serial?
select:
"USB Support ( CDC generic 'Serial' supercede u(s)art ) " from the menu to get usb-serial, i.e. Serial.println("hello"); goes to your serial monitor.

no conflicts, it gets its own virtual com: port

more commonly, I'd use usb-serial

Then use Serial1, ..., i.e. uart for other things e.g. to connect to other devices that use a uart for comms.
And you can easily connect Serial to Serial1 by passing data between them,

Code: Select all

HardwareSerial Serial1(PA10, PA9);

void setup() {
  Serial.begin(); // this is for usb (CDC) serial  
  Serial1.begin(115200);
  //while(!Serial1); //if you don't need to wait for connection this can be skipped
}

void loop() {
  if(Serial.available()) {
    Serial1.write(Serial.read());
  }
  if(Serial1.available()) {
    Serial.write(Serial1.read());
  }
}
to improve performance, you can use buffer etc and write a full block of data passing data between them.

usb (CDC) -serial is a *feature* of stm32 mcus that have usb, and it is faster than some usb-uart dongles which may limit speeds at 115200 and or that some usb-uart dongles works badly and gives lots of issues. in some tests i get about 1 Mbps with usb (CDC) serial, though your mileage may vary as usb bandwidth (12 Mbps) is multiplexed by the host, the more devices and hubs there are, the bandwidth becomes divided by N devices at least. And usb (CDC) serial doesn't care about 'baud' rates, it is 12 mbps full usb speeds.
Post Reply

Return to “General discussion”