Skip to content

Commit 9dacdf9

Browse files
authored
feat(uart): sets correct ESP32/S2 clock source for the example
1 parent c110ca8 commit 9dacdf9

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

libraries/ESP32/examples/Serial/RxTimeout_Demo/RxTimeout_Demo.ino

+15
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@
2121
If UART receives less than 120 bytes, it will wait RX Timeout to understand that the bus is IDLE and
2222
then copy the data from the FIFO to the Arduino internal buffer, making it available to the Arduino API.
2323
24+
There is an important detail about how HardwareSerial works using ESP32 and ESP32-S2:
25+
If the baud rate is lower than 250,000, it will select REF_TICK as clock source in order to avoid that
26+
the baud rate may change when the CPU Frequency is changed. Default UART clock source is APB, which changes
27+
when CPU clock source is also changed. But when it selects REF_TICK as UART clock source, RX Timeout is limited to 1.
28+
Therefore, in order to change the ESP32/ESP32-S2 RX Timeout it is necessary to fix the UART Clock Source to APB.
29+
30+
In the case of the other SoC, such as ESP32-S3, C3, C6, H2 and P4, there is no such RX Timeout limitation.
31+
Those will set the UART Source Clock as XTAL, which allows the baud rate to be high and it is steady, not
32+
changing with the CPU Frequency.
2433
*/
2534

2635
#include <Arduino.h>
@@ -45,6 +54,12 @@ void setup() {
4554

4655
// UART1 will have its RX<->TX cross connected
4756
// GPIO4 <--> GPIO5 using external wire
57+
#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
58+
// UART_CLK_SRC_APB will allow higher values of RX Timeout
59+
// default for ESP32 and ESP32-S2 is REF_TICK which limits the RX Timeout to 1
60+
// setClockSource() must be called before begin()
61+
Serial1.setClockSource(UART_CLK_SRC_APB);
62+
#endif
4863
Serial1.begin(BAUD, SERIAL_8N1, RXPIN, TXPIN); // Rx = 4, Tx = 5 will work for ESP32, S2, S3 and C3
4964
#if USE_INTERNAL_PIN_LOOPBACK
5065
uart_internal_loopback(TEST_UART, RXPIN);

0 commit comments

Comments
 (0)