-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HardwareSerial::end(bool) review + Baud Rate detection review and exa…
…mple (#8762) * removes pin detaching from end(false) * adds UART0 baud rate detection example * fixes baud rate messages
- Loading branch information
Showing
3 changed files
with
52 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
libraries/ESP32/examples/Serial/BaudRateDetect_Demo/BaudRateDetect_Demo.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
This Sketch demonstrates how to detect and set the baud rate when the UART0 is connected to | ||
some port that is sending data. It can be used with the Arduino IDE Serial Monitor to send the data. | ||
Serial.begin(0) will start the baud rate detection. Valid range is 300 to 230400 baud. | ||
It will try to detect for 20 seconds, by default, while reading RX. | ||
This timeout of 20 seconds can be changed in the begin() function through <<timeout_ms>> parameter: | ||
void HardwareSerial::begin(baud, config, rxPin, txPin, invert, <<timeout_ms>>, rxfifo_full_thrhd) | ||
It is necessary that the other end sends some data within <<timeout_ms>>, otherwise the detection won't work. | ||
IMPORTANT NOTE: baud rate detection seem to only work with ESP32 and ESP32-S2. | ||
In other other SoCs, it doesn't work. | ||
*/ | ||
|
||
// Open the Serial Monitor with testing baud start typing and sending caracters | ||
void setup() { | ||
Serial.begin(0); // it will try to detect the baud rate for 20 seconds | ||
|
||
Serial.print("\n==>The baud rate is "); | ||
Serial.println(Serial.baudRate()); | ||
|
||
//after 20 seconds timeout, when not detected, it will return zero - in this case, we set it back to 115200. | ||
if (Serial.baudRate() == 0) { | ||
// Trying to set Serial to a safe state at 115200 | ||
Serial.end(); | ||
Serial.begin(115200); | ||
Serial.setDebugOutput(true); | ||
delay(1000); | ||
log_e("Baud rate detection failed."); | ||
} | ||
} | ||
|
||
void loop() { | ||
} |