From 85d83f373b4da16f8a252a38578bf341944723f7 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Wed, 9 Oct 2024 14:44:40 -0300 Subject: [PATCH 01/30] fix(uart): uses IDF 5.x API to set loopback Uses void uart_internal_loopback(uint8_t uartNum, bool loop_back_en) IDF 5.x API. --- cores/esp32/esp32-hal-uart.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cores/esp32/esp32-hal-uart.c b/cores/esp32/esp32-hal-uart.c index 9eb117988f6..9a33fe50952 100644 --- a/cores/esp32/esp32-hal-uart.c +++ b/cores/esp32/esp32-hal-uart.c @@ -1114,11 +1114,11 @@ unsigned long uartDetectBaudrate(uart_t *uart) { This function internally binds defined UARTs TX signal with defined RX pin of any UART (same or different). This creates a loop that lets us receive anything we send on the UART without external wires. */ -void uart_internal_loopback(uint8_t uartNum, int8_t rxPin) { - if (uartNum > SOC_UART_NUM - 1 || !GPIO_IS_VALID_GPIO(rxPin)) { +void uart_internal_loopback(uint8_t uartNum, bool loop_back_en) { + if (uartNum > SOC_UART_NUM - 1) { return; } - esp_rom_gpio_connect_out_signal(rxPin, UART_TX_SIGNAL(uartNum), false, false); + uart_set_loop_back(uartNum, loop_back_en); } /* From 412b76ee0307d3212953e2d2c763226c0f2bb172 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Wed, 9 Oct 2024 14:47:05 -0300 Subject: [PATCH 02/30] fix(uart): ESP32-P4 loopback functions Uses void uart_internal_loopback(uint8_t uartNum, bool loop_back_en) IDF 5.x API. --- cores/esp32/esp32-hal-uart.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp32/esp32-hal-uart.h b/cores/esp32/esp32-hal-uart.h index 402b5785915..389193df9da 100644 --- a/cores/esp32/esp32-hal-uart.h +++ b/cores/esp32/esp32-hal-uart.h @@ -106,7 +106,7 @@ unsigned long uartDetectBaudrate(uart_t *uart); // Make sure UART's RX signal is connected to TX pin // This creates a loop that lets us receive anything we send on the UART -void uart_internal_loopback(uint8_t uartNum, int8_t rxPin); +void uart_internal_loopback(uint8_t uartNum, bool loop_back_en); // Routines that generate BREAK in the UART for testing purpose From d7bb46e1627803c962afa2676b5add0032bb1382 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Wed, 9 Oct 2024 14:50:50 -0300 Subject: [PATCH 03/30] fix(uart): fix example Update OnReceiveError_BREAK_Demo.ino to use new API. --- .../OnReceiveError_BREAK_Demo/OnReceiveError_BREAK_Demo.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/ESP32/examples/Serial/OnReceiveError_BREAK_Demo/OnReceiveError_BREAK_Demo.ino b/libraries/ESP32/examples/Serial/OnReceiveError_BREAK_Demo/OnReceiveError_BREAK_Demo.ino index fb7af04c5f5..727dc61e47d 100644 --- a/libraries/ESP32/examples/Serial/OnReceiveError_BREAK_Demo/OnReceiveError_BREAK_Demo.ino +++ b/libraries/ESP32/examples/Serial/OnReceiveError_BREAK_Demo/OnReceiveError_BREAK_Demo.ino @@ -93,7 +93,7 @@ void setup() { // GPIO4 <--> GPIO5 using external wire Serial1.begin(BAUD, SERIAL_8N1, RXPIN, TXPIN); // Rx = 4, Tx = 5 will work for ESP32, S2, S3 and C3 #if USE_INTERNAL_PIN_LOOPBACK - uart_internal_loopback(TEST_UART, RXPIN); + uart_internal_loopback(TEST_UART, true); #endif for (uint8_t i = 0; i < sizeof(fifoFullTestCases); i++) { From 55a2d1ed6b05efacb66f90eb73e12b01dc60944d Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Wed, 9 Oct 2024 14:52:07 -0300 Subject: [PATCH 04/30] fix(uart): new testing loopback API Update OnReceive_Demo.ino to se new API. --- .../ESP32/examples/Serial/OnReceive_Demo/OnReceive_Demo.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/ESP32/examples/Serial/OnReceive_Demo/OnReceive_Demo.ino b/libraries/ESP32/examples/Serial/OnReceive_Demo/OnReceive_Demo.ino index cac59a50cd6..0abaa4d653e 100644 --- a/libraries/ESP32/examples/Serial/OnReceive_Demo/OnReceive_Demo.ino +++ b/libraries/ESP32/examples/Serial/OnReceive_Demo/OnReceive_Demo.ino @@ -75,7 +75,7 @@ void setup() { // GPIO4 <--> GPIO5 using external wire Serial1.begin(BAUD, SERIAL_8N1, RXPIN, TXPIN); // Rx = 4, Tx = 5 will work for ESP32, S2, S3 and C3 #if USE_INTERNAL_PIN_LOOPBACK - uart_internal_loopback(TEST_UART, RXPIN); + uart_internal_loopback(TEST_UART, true); #endif for (uint8_t i = 0; i < sizeof(fifoFullTestCases); i++) { From 4ef3148dbb35ed818f8fc3d143048cc321fab046 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Wed, 9 Oct 2024 14:53:26 -0300 Subject: [PATCH 05/30] fix(uart): new testing loopback API Update RxFIFOFull_Demo.ino to use new testing API. --- .../ESP32/examples/Serial/RxFIFOFull_Demo/RxFIFOFull_Demo.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/ESP32/examples/Serial/RxFIFOFull_Demo/RxFIFOFull_Demo.ino b/libraries/ESP32/examples/Serial/RxFIFOFull_Demo/RxFIFOFull_Demo.ino index 7722ef6f47a..a0365163414 100644 --- a/libraries/ESP32/examples/Serial/RxFIFOFull_Demo/RxFIFOFull_Demo.ino +++ b/libraries/ESP32/examples/Serial/RxFIFOFull_Demo/RxFIFOFull_Demo.ino @@ -47,7 +47,7 @@ void setup() { // GPIO4 <--> GPIO5 using external wire Serial1.begin(BAUD, SERIAL_8N1, RXPIN, TXPIN); // Rx = 4, Tx = 5 will work for ESP32, S2, S3 and C3 #if USE_INTERNAL_PIN_LOOPBACK - uart_internal_loopback(TEST_UART, RXPIN); + uart_internal_loopback(TEST_UART, true); #endif for (uint8_t i = 0; i < sizeof(fifoFullTestCases); i++) { From 38b4474f9a49e596905cf15417ce4be91ac17abf Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Wed, 9 Oct 2024 14:54:42 -0300 Subject: [PATCH 06/30] fix(uart): new testing loopback API Update RxTimeout_Demo.ino to use new testing API. --- .../ESP32/examples/Serial/RxTimeout_Demo/RxTimeout_Demo.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/ESP32/examples/Serial/RxTimeout_Demo/RxTimeout_Demo.ino b/libraries/ESP32/examples/Serial/RxTimeout_Demo/RxTimeout_Demo.ino index 64d15d3d916..c231ed6f7b6 100644 --- a/libraries/ESP32/examples/Serial/RxTimeout_Demo/RxTimeout_Demo.ino +++ b/libraries/ESP32/examples/Serial/RxTimeout_Demo/RxTimeout_Demo.ino @@ -47,7 +47,7 @@ void setup() { // GPIO4 <--> GPIO5 using external wire Serial1.begin(BAUD, SERIAL_8N1, RXPIN, TXPIN); // Rx = 4, Tx = 5 will work for ESP32, S2, S3 and C3 #if USE_INTERNAL_PIN_LOOPBACK - uart_internal_loopback(TEST_UART, RXPIN); + uart_internal_loopback(TEST_UART, true); #endif for (uint8_t i = 0; i < sizeof(rxTimeoutTestCases); i++) { From 2b0672eee15f94c336e770e33e00a25e9ef1c529 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Wed, 9 Oct 2024 14:55:43 -0300 Subject: [PATCH 07/30] fix(uart): new testing loopback API Update Serial_STD_Func_OnReceive.ino to use new testing API --- .../Serial_STD_Func_OnReceive/Serial_STD_Func_OnReceive.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/ESP32/examples/Serial/Serial_STD_Func_OnReceive/Serial_STD_Func_OnReceive.ino b/libraries/ESP32/examples/Serial/Serial_STD_Func_OnReceive/Serial_STD_Func_OnReceive.ino index eff21a1939e..1f9a7fa9c60 100644 --- a/libraries/ESP32/examples/Serial/Serial_STD_Func_OnReceive/Serial_STD_Func_OnReceive.ino +++ b/libraries/ESP32/examples/Serial/Serial_STD_Func_OnReceive/Serial_STD_Func_OnReceive.ino @@ -89,7 +89,7 @@ void setup() { // creating a loopback that will allow to write to TEST_UART number // and send it to RX with no need to physically connect both pins #if TEST_UART > 0 - uart_internal_loopback(TEST_UART, RXPIN); + uart_internal_loopback(TEST_UART, true); #else // when UART0 is used for testing, it is necessary to send data using the Serial Monitor/Terminal // Data must be sent by the CP2102, manually using the Serial Monitor/Terminal From 71769708f796cc51619d58414c42797c7b8f11be Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Wed, 9 Oct 2024 14:58:54 -0300 Subject: [PATCH 08/30] fix(uart): new testing loopback API Update periman.ino to use new UART loopback test API --- tests/validation/periman/periman.ino | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/validation/periman/periman.ino b/tests/validation/periman/periman.ino index 8da59dd23b9..ad15214dc57 100644 --- a/tests/validation/periman/periman.ino +++ b/tests/validation/periman/periman.ino @@ -72,7 +72,7 @@ void setup_test(String test_name, int8_t rx_pin = UART1_RX_DEFAULT, int8_t tx_pi pinMode(uart1_rx_pin, INPUT_PULLUP); pinMode(uart1_tx_pin, OUTPUT); Serial1.setPins(uart1_rx_pin, uart1_tx_pin); - uart_internal_loopback(1, uart1_rx_pin); + uart_internal_loopback(1, true); delay(100); log_v("Running %s test", test_name.c_str()); } @@ -88,7 +88,7 @@ void teardown_test(void) { Serial1.flush(); Serial1.setPins(uart1_rx_pin, uart1_tx_pin); - uart_internal_loopback(1, uart1_rx_pin); + uart_internal_loopback(1, true); delay(100); } @@ -280,7 +280,7 @@ void setup() { delay(10); } Serial1.onReceive(onReceive_cb); - uart_internal_loopback(1, uart1_rx_pin); + uart_internal_loopback(1, true); gpio_test(); sigmadelta_test(); From f07866203d6daf89fd4b0a665a92731dab8df3e4 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Wed, 9 Oct 2024 15:05:38 -0300 Subject: [PATCH 09/30] fix(uart): new testing loopback API Update uart.ino to use newuart loopback API --- tests/validation/uart/uart.ino | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/tests/validation/uart/uart.ino b/tests/validation/uart/uart.ino index 527d28241d9..00e079e9f70 100644 --- a/tests/validation/uart/uart.ino +++ b/tests/validation/uart/uart.ino @@ -149,7 +149,7 @@ void setUp(void) { Serial1.onReceive([]() { onReceive_cb(Serial1); }); - uart_internal_loopback(1, RX1); + uart_internal_loopback(1, true); #elif SOC_UART_NUM == 3 log_d("Setup internal loop-back between Serial1 (UART1) <<--->> Serial2 (UART2)"); @@ -159,8 +159,8 @@ void setUp(void) { Serial2.onReceive([]() { onReceive_cb(Serial2); }); - uart_internal_loopback(1, RX2); - uart_internal_loopback(2, RX1); + uart_internal_loopback(1, true); // it was suppose to cross connect TX1 to RX2! + uart_internal_loopback(2, true); // it was suppose to cross connect TX2 to RX1! #endif } @@ -446,10 +446,10 @@ void change_pins_test(void) { log_d("Re-enabling UART loopback"); #if SOC_UART_NUM == 2 - uart_internal_loopback(1, NEW_RX1); + uart_internal_loopback(1, true); #elif SOC_UART_NUM == 3 - uart_internal_loopback(1, RX1); - uart_internal_loopback(2, RX2); + uart_internal_loopback(1, true); + uart_internal_loopback(2, true); #endif transmit_and_check_msg("using new pins"); @@ -469,7 +469,7 @@ void auto_baudrate_test(void) { #if SOC_UART_NUM == 2 selected_serial = &Serial1; - uart_internal_loopback(0, RX1); + uart_internal_loopback(0, RX1); // it was suppose to cross connect TX0 to RX1 #elif SOC_UART_NUM == 3 selected_serial = &Serial2; #endif @@ -520,10 +520,10 @@ void periman_test(void) { #if SOC_UART_NUM == 3 Serial2.setPins(RX2, TX2); - uart_internal_loopback(1, RX2); - uart_internal_loopback(2, RX1); + uart_internal_loopback(1, true); // it was suppose to cross connect TX2 to RX1 + uart_internal_loopback(2, true); // it was suppose to cross connect TX1 to RX2 #elif SOC_UART_NUM == 2 - uart_internal_loopback(1, RX1); + uart_internal_loopback(1, true); #endif log_d("Trying to send message using UART with I2C disabled"); @@ -576,7 +576,7 @@ void setup() { Serial1.onReceive([]() { onReceive_cb(Serial1); }); - uart_internal_loopback(1, RX1); + uart_internal_loopback(1, true); #elif SOC_UART_NUM == 3 log_d("Setup internal loop-back between Serial1 (UART1) <<--->> Serial2 (UART2)"); @@ -586,8 +586,8 @@ void setup() { Serial2.onReceive([]() { onReceive_cb(Serial2); }); - uart_internal_loopback(1, RX2); - uart_internal_loopback(2, RX1); + uart_internal_loopback(1, true); // it was suppose to cross connect TX2 to RX1 + uart_internal_loopback(2, true); // it was suppose to cross connect TX1 to RX2 #endif log_d("Setup done. Starting tests"); From 61e2263acdebe35d4b341c7aa99812848a052879 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Wed, 9 Oct 2024 15:11:17 -0300 Subject: [PATCH 10/30] fix(uart): new testing loopback API Update uart.ino - missing change in code --- tests/validation/uart/uart.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/validation/uart/uart.ino b/tests/validation/uart/uart.ino index 00e079e9f70..3751e08af05 100644 --- a/tests/validation/uart/uart.ino +++ b/tests/validation/uart/uart.ino @@ -469,7 +469,7 @@ void auto_baudrate_test(void) { #if SOC_UART_NUM == 2 selected_serial = &Serial1; - uart_internal_loopback(0, RX1); // it was suppose to cross connect TX0 to RX1 + uart_internal_loopback(0, true); // it was suppose to cross connect TX0 to RX1 #elif SOC_UART_NUM == 3 selected_serial = &Serial2; #endif From 5748548ea21b5cfa40aec6396ac1199893007ce3 Mon Sep 17 00:00:00 2001 From: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com> Date: Fri, 11 Oct 2024 00:58:14 -0300 Subject: [PATCH 11/30] fix(test): Fix UART validation test --- tests/validation/uart/uart.ino | 352 +++++++++++++++------------------ 1 file changed, 154 insertions(+), 198 deletions(-) diff --git a/tests/validation/uart/uart.ino b/tests/validation/uart/uart.ino index 3751e08af05..34bb456c961 100644 --- a/tests/validation/uart/uart.ino +++ b/tests/validation/uart/uart.ino @@ -11,16 +11,16 @@ #include "Wire.h" // Default pins: -// | Name | ESP32 | S2 | S3 | C3 | C6 | H2 | -// UART0 RX | SOC_RX0 | 3 | 44 | 44 | 20 | 17 | 23 | -// UART0 TX | SOC_TX0 | 1 | 43 | 43 | 21 | 16 | 24 | -// UART1 RX | RX1 | 26 | 4 | 15 | 18 | 4 | 0 | -// UART1 TX | TX1 | 27 | 5 | 16 | 19 | 5 | 1 | -// UART2 RX | RX2 | 4 | -- | 19 | -- | -- | -- | -// UART2 TX | TX2 | 25 | -- | 20 | -- | -- | -- | +// | Name | ESP32 | S2 | S3 | C3 | C6 | H2 | P4 | +// UART0 RX | SOC_RX0 | 3 | 44 | 44 | 20 | 17 | 23 | 38 | +// UART0 TX | SOC_TX0 | 1 | 43 | 43 | 21 | 16 | 24 | 37 | +// UART1 RX | RX1 | 26 | 4 | 15 | 18 | 4 | 0 | 11 | +// UART1 TX | TX1 | 27 | 5 | 16 | 19 | 5 | 1 | 10 | +// UART2 RX | RX2 | 4 | -- | 19 | -- | -- | -- | 15 | +// UART2 TX | TX2 | 25 | -- | 20 | -- | -- | -- | 14 | /* - * For 2 UARTS: + * For each UART: * * terminal * | ^ @@ -30,32 +30,55 @@ * report status * | * TX <---> RX - * UART1 - * - * For 3 UARTS: - * - * =====terminal====== - * ^ | ^ ^ - * | v UART0 | | - * | RX TX | - * | | - * ^ report status ^ - * | | - * | TX ---> RX | - * UART2 RX <--- TX UART1 - * + * UARTx */ -#if SOC_UART_NUM == 2 -// Used for the pin swap test -#define NEW_RX1 9 -#define NEW_TX1 10 +typedef struct test_uart_t +{ + uint8_t rx_pin; + uint8_t tx_pin; + HardwareSerial &serial; +} test_uart_t; + +const test_uart_t test_uarts[SOC_UART_NUM] = { + { + .rx_pin = SOC_RX0, + .tx_pin = SOC_TX0, + .serial = Serial, + }, + { + .rx_pin = RX1, + .tx_pin = TX1, + .serial = Serial1, + }, +#if SOC_UART_NUM >= 3 + { + .rx_pin = RX2, + .tx_pin = TX2, + .serial = Serial2, + }, +#endif +#if SOC_UART_NUM >= 4 + { + .rx_pin = RX3, + .tx_pin = TX3, + .serial = Serial3, + }, #endif +#if SOC_UART_NUM >= 5 + { + .rx_pin = RX4, + .tx_pin = TX4, + .serial = Serial4, + }, +#endif +}; /* Utility global variables */ +uint8_t i; static String recv_msg = ""; -static int peeked_char = -1; +static int peeked_char[SOC_UART_NUM]; /* Utility functions */ @@ -64,104 +87,68 @@ extern int8_t uart_get_TxPin(uint8_t uart_num); // This function starts all the available test UARTs void start_serial(unsigned long baudrate = 115200) { -#if SOC_UART_NUM >= 2 - Serial1.begin(baudrate); - while (!Serial1) { - delay(10); + for (i = 1; i < SOC_UART_NUM; i++) { + test_uarts[i].serial.begin(baudrate, SERIAL_8N1, test_uarts[i].rx_pin, test_uarts[i].tx_pin); + while (!test_uarts[i].serial) { + delay(10); + } } -#endif - -#if SOC_UART_NUM >= 3 - Serial2.begin(baudrate); - while (!Serial2) { - delay(10); - } -#endif } // This function stops all the available test UARTs -void stop_serial(bool hard_stop = false) { -#if SOC_UART_NUM >= 2 - Serial1.end(/*hard_stop*/); -#endif - -#if SOC_UART_NUM >= 3 - Serial2.end(/*hard_stop*/); -#endif +void stop_serial() { + for (i = 1; i < SOC_UART_NUM; i++) { + test_uarts[i].serial.end(); + } } // This function transmits a message and checks if it was received correctly void transmit_and_check_msg(const String msg_append, bool perform_assert = true) { delay(100); // Wait for some settings changes to take effect -#if SOC_UART_NUM == 2 - Serial1.print("Hello from Serial1 (UART1) >>> via loopback >>> Serial1 (UART1) " + msg_append); - Serial1.flush(); - delay(100); - if (perform_assert) { - TEST_ASSERT_EQUAL_STRING(("Hello from Serial1 (UART1) >>> via loopback >>> Serial1 (UART1) " + msg_append).c_str(), recv_msg.c_str()); + for (i = 1; i < SOC_UART_NUM; i++) { + test_uarts[i].serial.print("Hello from Serial" + String(i) + " (UART" + String(i) + ") >>> via loopback >>> Serial" + String(i) + " (UART" + String(i) + ") " + msg_append); + test_uarts[i].serial.flush(); + delay(100); + if (perform_assert) { + TEST_ASSERT_EQUAL_STRING(("Hello from Serial" + String(i) + " (UART" + String(i) + ") >>> via loopback >>> Serial" + String(i) + " (UART" + String(i) + ") " + msg_append).c_str(), recv_msg.c_str()); + } } -#elif SOC_UART_NUM == 3 - Serial1.print("Hello from Serial1 (UART1) >>> to >>> Serial2 (UART2) " + msg_append); - Serial1.flush(); - delay(100); - if (perform_assert) { - TEST_ASSERT_EQUAL_STRING(("Hello from Serial1 (UART1) >>> to >>> Serial2 (UART2) " + msg_append).c_str(), recv_msg.c_str()); - } - - Serial2.print("Hello from Serial2 (UART2) >>> to >>> Serial1 (UART1) " + msg_append); - Serial2.flush(); - delay(100); - if (perform_assert) { - TEST_ASSERT_EQUAL_STRING(("Hello from Serial2 (UART2) >>> to >>> Serial1 (UART1) " + msg_append).c_str(), recv_msg.c_str()); - } -#else - log_d("No UARTs available for transmission"); - TEST_FAIL(); -#endif } /* Tasks */ +/* // This task is used to send a message after a delay to test the auto baudrate detection void task_delayed_msg(void *pvParameters) { HardwareSerial *selected_serial; -#if SOC_UART_NUM == 2 - selected_serial = &Serial; -#elif SOC_UART_NUM == 3 +#if SOC_UART_NUM >= 3 selected_serial = &Serial1; +#else + selected_serial = &Serial; #endif delay(2000); - selected_serial->println("Hello from Serial1 to detect baudrate"); + selected_serial->println("Hello from Serial to detect baudrate"); selected_serial->flush(); vTaskDelete(NULL); } +*/ /* Unity functions */ // This function is automatically called by unity before each test is run void setUp(void) { start_serial(115200); -#if SOC_UART_NUM == 2 - log_d("Setup internal loop-back from and back to Serial1 (UART1) TX >> Serial1 (UART1) RX"); - - Serial1.onReceive([]() { - onReceive_cb(Serial1); - }); - uart_internal_loopback(1, true); -#elif SOC_UART_NUM == 3 - log_d("Setup internal loop-back between Serial1 (UART1) <<--->> Serial2 (UART2)"); - - Serial1.onReceive([]() { - onReceive_cb(Serial1); - }); - Serial2.onReceive([]() { - onReceive_cb(Serial2); - }); - uart_internal_loopback(1, true); // it was suppose to cross connect TX1 to RX2! - uart_internal_loopback(2, true); // it was suppose to cross connect TX2 to RX1! -#endif + + for (i = 1; i < SOC_UART_NUM; i++) { + log_d("Setup internal loop-back from and back to Serial%d (UART%d) TX >> Serial%d (UART%d) RX", i, i, i, i); + + test_uarts[i].serial.onReceive([]() { + onReceive_cb(i); + }); + uart_internal_loopback(i, true); + } } // This function is automatically called by unity after each test is run @@ -172,29 +159,15 @@ void tearDown(void) { /* Callback functions */ // This is a callback function that will be activated on UART RX events -void onReceive_cb(HardwareSerial &selected_serial) { - int uart_num = -1; +void onReceive_cb(int uart_num) { + HardwareSerial &selected_serial = test_uarts[uart_num].serial; char c; - (void)uart_num; // Avoid compiler warning when debug level is set to none - - if (&selected_serial == &Serial) { - uart_num = 0; -#if SOC_UART_NUM >= 2 - } else if (&selected_serial == &Serial1) { - uart_num = 1; -#endif -#if SOC_UART_NUM >= 3 - } else if (&selected_serial == &Serial2) { - uart_num = 2; -#endif - } - recv_msg = ""; size_t available = selected_serial.available(); if (available != 0) { - peeked_char = selected_serial.peek(); + peeked_char[uart_num] = selected_serial.peek(); } while (available--) { @@ -202,7 +175,7 @@ void onReceive_cb(HardwareSerial &selected_serial) { recv_msg += c; } - log_d("UART %d received message: %s\n", uart_num, recv_msg.c_str()); + log_d("UART%d received message: %s\n", uart_num, recv_msg.c_str()); } /* Test functions */ @@ -221,14 +194,11 @@ void change_baudrate_test(void) { //Test first using the updateBaudRate method and then using the begin method log_d("Changing baudrate to 9600"); - //Baudrate error should be within 2% of the target baudrate - Serial1.updateBaudRate(9600); - TEST_ASSERT_UINT_WITHIN(192, 9600, Serial1.baudRate()); - -#if SOC_UART_NUM == 3 - Serial2.updateBaudRate(9600); - TEST_ASSERT_UINT_WITHIN(192, 9600, Serial2.baudRate()); -#endif + for (i = 1; i < SOC_UART_NUM; i++) { + test_uarts[i].serial.updateBaudRate(9600); + //Baudrate error should be within 2% of the target baudrate + TEST_ASSERT_UINT_WITHIN(192, 9600, test_uarts[i].serial.baudRate()); + } log_d("Sending string using 9600 baudrate"); transmit_and_check_msg("using 9600 baudrate"); @@ -236,12 +206,10 @@ void change_baudrate_test(void) { log_d("Changing baudrate back to 115200"); start_serial(115200); - //Baudrate error should be within 2% of the target baudrate - TEST_ASSERT_UINT_WITHIN(2304, 115200, Serial1.baudRate()); - -#if SOC_UART_NUM == 3 - TEST_ASSERT_UINT_WITHIN(2304, 115200, Serial2.baudRate()); -#endif + for (i = 1; i < SOC_UART_NUM; i++) { + //Baudrate error should be within 2% of the target baudrate + TEST_ASSERT_UINT_WITHIN(2304, 115200, test_uarts[i].serial.baudRate()); + } log_d("Sending string using 115200 baudrate"); transmit_and_check_msg("using 115200 baudrate"); @@ -285,9 +253,9 @@ void begin_when_running_test(void) { void end_when_stopped_test(void) { log_d("Trying to end serial twice"); - // Calling end(true) twice should not crash - stop_serial(true); - stop_serial(true); + // Calling end() twice should not crash + stop_serial(); + stop_serial(); Serial.println("End when stopped test successful"); } @@ -311,7 +279,7 @@ void enabled_uart_calls_test(void) { TEST_ASSERT_EQUAL(true, boolean_ret); log_d("Checking if Serial 1 is peekable while running"); - TEST_ASSERT_GREATER_OR_EQUAL(0, peeked_char); + TEST_ASSERT_GREATER_OR_EQUAL(0, peeked_char[1]); log_d("Checking if Serial 1 can read bytes while running"); integer_ret = Serial1.readBytes(test_buf, 1); @@ -415,48 +383,47 @@ void disabled_uart_calls_test(void) { // This test checks if the pins can be changed and if the message can be transmitted and received correctly after the change void change_pins_test(void) { - //stop_serial(); - log_d("Disabling UART loopback"); + //stop_serial(); -#if SOC_UART_NUM == 2 - esp_rom_gpio_connect_out_signal(SOC_RX0, SIG_GPIO_OUT_IDX, false, false); -#elif SOC_UART_NUM == 3 - esp_rom_gpio_connect_out_signal(RX1, SIG_GPIO_OUT_IDX, false, false); - esp_rom_gpio_connect_out_signal(RX2, SIG_GPIO_OUT_IDX, false, false); -#endif + for (i = 1; i < SOC_UART_NUM; i++) { + uart_internal_loopback(i, false); + } log_d("Swapping UART pins"); #if SOC_UART_NUM == 2 - Serial1.setPins(NEW_RX1, NEW_TX1); - TEST_ASSERT_EQUAL(NEW_RX1, uart_get_RxPin(1)); - TEST_ASSERT_EQUAL(NEW_TX1, uart_get_TxPin(1)); -#elif SOC_UART_NUM == 3 - Serial1.setPins(RX2, TX2); - Serial2.setPins(RX1, TX1); - TEST_ASSERT_EQUAL(RX2, uart_get_RxPin(1)); - TEST_ASSERT_EQUAL(TX2, uart_get_TxPin(1)); - TEST_ASSERT_EQUAL(RX1, uart_get_RxPin(2)); - TEST_ASSERT_EQUAL(TX1, uart_get_TxPin(2)); + Serial1.setPins(TX1, RX1); // Invert TX and RX pins + TEST_ASSERT_EQUAL(TX1, uart_get_RxPin(1)); // TX1 is now RX pin + TEST_ASSERT_EQUAL(RX1, uart_get_TxPin(1)); // RX1 is now TX pin +#elif SOC_UART_NUM >= 3 + for (i = 1; i < SOC_UART_NUM; i++) { + // Swapping pins with the next available UART + int uart_replacement = (i + 1) % SOC_UART_NUM; + uart_replacement = uart_replacement == 0 ? 1 : uart_replacement; + test_uarts[i].serial.setPins(test_uarts[uart_replacement].rx_pin, test_uarts[uart_replacement].tx_pin); + TEST_ASSERT_EQUAL(test_uarts[uart_replacement].tx_pin, uart_get_TxPin(i)); + TEST_ASSERT_EQUAL(test_uarts[uart_replacement].rx_pin, uart_get_RxPin(i)); + } #endif start_serial(115200); log_d("Re-enabling UART loopback"); -#if SOC_UART_NUM == 2 - uart_internal_loopback(1, true); -#elif SOC_UART_NUM == 3 - uart_internal_loopback(1, true); - uart_internal_loopback(2, true); -#endif + for (i = 1; i < SOC_UART_NUM; i++) { + uart_internal_loopback(i, true); + } transmit_and_check_msg("using new pins"); Serial.println("Change pins test successful"); } +/* + +// The new loopback API does not allow cross connecting UARTs. This test is disabled for now. + // This test checks if the auto baudrate detection works on ESP32 and ESP32-S2 #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 void auto_baudrate_test(void) { @@ -470,7 +437,7 @@ void auto_baudrate_test(void) { #if SOC_UART_NUM == 2 selected_serial = &Serial1; uart_internal_loopback(0, true); // it was suppose to cross connect TX0 to RX1 -#elif SOC_UART_NUM == 3 +#elif SOC_UART_NUM >= 3 selected_serial = &Serial2; #endif @@ -495,39 +462,32 @@ void auto_baudrate_test(void) { Serial.println("Auto baudrate test successful"); } #endif +*/ // This test checks if the peripheral manager can properly manage UART pins void periman_test(void) { log_d("Checking if peripheral manager can properly manage UART pins"); - log_d("Setting up I2C on the same pins as UART"); - - Wire.begin(RX1, TX1); - -#if SOC_UART_NUM == 3 - Wire1.begin(RX2, TX2); -#endif - - recv_msg = ""; + for (i = 1; i < SOC_UART_NUM; i++) { + log_d("Setting up I2C on the same pins as UART%d", i); + Wire.begin(test_uarts[i].rx_pin, test_uarts[i].tx_pin); - log_d("Trying to send message using UART with I2C enabled"); - transmit_and_check_msg("while used by I2C", false); - TEST_ASSERT_EQUAL_STRING("", recv_msg.c_str()); + recv_msg = ""; - log_d("Disabling I2C and re-enabling UART"); + log_d("Trying to send message using UART%d with I2C enabled", i); + test_uarts[i].serial.print("Hello from Serial" + String(i) + " (UART" + String(i) + ") >>> via loopback >>> Serial" + String(i) + " (UART" + String(i) + ") while used by I2C"); + test_uarts[i].serial.flush(); + delay(100); + TEST_ASSERT_EQUAL_STRING("", recv_msg.c_str()); - Serial1.setPins(RX1, TX1); + log_d("Disabling I2C and re-enabling UART"); -#if SOC_UART_NUM == 3 - Serial2.setPins(RX2, TX2); - uart_internal_loopback(1, true); // it was suppose to cross connect TX2 to RX1 - uart_internal_loopback(2, true); // it was suppose to cross connect TX1 to RX2 -#elif SOC_UART_NUM == 2 - uart_internal_loopback(1, true); -#endif + Serial1.setPins(test_uarts[i].rx_pin, test_uarts[i].tx_pin); + uart_internal_loopback(i, true); - log_d("Trying to send message using UART with I2C disabled"); - transmit_and_check_msg("while I2C is disabled"); + log_d("Trying to send message using UART with I2C disabled"); + transmit_and_check_msg("while I2C is disabled"); + } Serial.println("Peripheral manager test successful"); } @@ -567,28 +527,20 @@ void setup() { } log_d("SOC_UART_NUM = %d", SOC_UART_NUM); + for (i = 0; i < SOC_UART_NUM; i++) { + peeked_char[i] = -1; + } + // Begin needs to be called before setting up the loopback because it creates the serial object start_serial(115200); -#if SOC_UART_NUM == 2 - log_d("Setup internal loop-back from and back to Serial1 (UART1) TX >> Serial1 (UART1) RX"); - - Serial1.onReceive([]() { - onReceive_cb(Serial1); - }); - uart_internal_loopback(1, true); -#elif SOC_UART_NUM == 3 - log_d("Setup internal loop-back between Serial1 (UART1) <<--->> Serial2 (UART2)"); - - Serial1.onReceive([]() { - onReceive_cb(Serial1); - }); - Serial2.onReceive([]() { - onReceive_cb(Serial2); - }); - uart_internal_loopback(1, true); // it was suppose to cross connect TX2 to RX1 - uart_internal_loopback(2, true); // it was suppose to cross connect TX1 to RX2 -#endif + for (i = 1; i < SOC_UART_NUM; i++) { + log_d("Setup internal loop-back from and back to Serial%d (UART%d) TX >> Serial%d (UART%d) RX", i, i, i, i); + test_uarts[i].serial.onReceive([]() { + onReceive_cb(i); + }); + uart_internal_loopback(i, true); + } log_d("Setup done. Starting tests"); @@ -600,13 +552,17 @@ void setup() { RUN_TEST(change_cpu_frequency_test); RUN_TEST(disabled_uart_calls_test); RUN_TEST(enabled_uart_calls_test); +/* #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 RUN_TEST(auto_baudrate_test); #endif +*/ RUN_TEST(periman_test); RUN_TEST(change_pins_test); RUN_TEST(end_when_stopped_test); UNITY_END(); } -void loop() {} +void loop() { + vTaskDelete(NULL); +} From ef501768ab76dc9783ec7ffe13f7e161e17ad95f Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Thu, 24 Oct 2024 11:37:26 -0300 Subject: [PATCH 12/30] fix(uart): fixes P4 pins for uart testing case --- tests/validation/uart/uart.ino | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/tests/validation/uart/uart.ino b/tests/validation/uart/uart.ino index d13d2fe58be..25947fe4c9b 100644 --- a/tests/validation/uart/uart.ino +++ b/tests/validation/uart/uart.ino @@ -16,8 +16,8 @@ // UART0 TX | SOC_TX0 | 1 | 43 | 43 | 21 | 16 | 24 | 37 | // UART1 RX | RX1 | 26 | 4 | 15 | 18 | 4 | 0 | 11 | // UART1 TX | TX1 | 27 | 5 | 16 | 19 | 5 | 1 | 10 | -// UART2 RX | RX2 | 4 | -- | 19 | -- | -- | -- | 15 | -// UART2 TX | TX2 | 25 | -- | 20 | -- | -- | -- | 14 | +// UART2 RX | RX2 | 4 | -- | 19 | -- | -- | -- | -- | +// UART2 TX | TX2 | 25 | -- | 20 | -- | -- | -- | -- | /* * For each UART: @@ -53,22 +53,37 @@ const test_uart_t test_uarts[SOC_UART_NUM] = { }, #if SOC_UART_NUM >= 3 { +#ifdef RX2 .rx_pin = RX2, .tx_pin = TX2, +#else + .rx_pin = RX1, + .tx_pin = TX1, +#endif .serial = Serial2, }, #endif #if SOC_UART_NUM >= 4 { +#ifdef RX3 .rx_pin = RX3, .tx_pin = TX3, +#else + .rx_pin = RX1, + .tx_pin = TX1, +#endif .serial = Serial3, }, #endif #if SOC_UART_NUM >= 5 { +#ifdef RX4 .rx_pin = RX4, .tx_pin = TX4, +#else + .rx_pin = RX1, + .tx_pin = TX1, +#endif .serial = Serial4, }, #endif From c35b76e9f6ddbc2ad200555b4b5a465a66fbf8b2 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Thu, 24 Oct 2024 11:39:41 -0300 Subject: [PATCH 13/30] fix(uart): fixes pins detaching - shall revert loopback --- cores/esp32/esp32-hal-uart.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cores/esp32/esp32-hal-uart.c b/cores/esp32/esp32-hal-uart.c index 4432287ac8c..7b2250c34fa 100644 --- a/cores/esp32/esp32-hal-uart.c +++ b/cores/esp32/esp32-hal-uart.c @@ -166,6 +166,7 @@ static bool _uartDetachBus_RX(void *busptr) { // sanity check - it should never happen assert(busptr && "_uartDetachBus_RX bus NULL pointer."); uart_t *bus = (uart_t *)busptr; + uart_set_loop_back(bus->num, false); // disable loopback return _uartDetachPins(bus->num, bus->_rxPin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); } @@ -173,6 +174,7 @@ static bool _uartDetachBus_TX(void *busptr) { // sanity check - it should never happen assert(busptr && "_uartDetachBus_TX bus NULL pointer."); uart_t *bus = (uart_t *)busptr; + uart_set_loop_back(bus->num, false); // disable loopback return _uartDetachPins(bus->num, UART_PIN_NO_CHANGE, bus->_txPin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); } From 8d73687e13443508156fdf68311b33d0cf234764 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Thu, 24 Oct 2024 11:42:16 -0300 Subject: [PATCH 14/30] fix(uart): removes esp32-p4 rx2 and tx2 pin definition --- cores/esp32/HardwareSerial.h | 4 ---- 1 file changed, 4 deletions(-) diff --git a/cores/esp32/HardwareSerial.h b/cores/esp32/HardwareSerial.h index 8eb7f2c91a6..a33d5def34d 100644 --- a/cores/esp32/HardwareSerial.h +++ b/cores/esp32/HardwareSerial.h @@ -200,8 +200,6 @@ typedef enum { #define RX2 (gpio_num_t)4 #elif CONFIG_IDF_TARGET_ESP32S3 #define RX2 (gpio_num_t)19 -#elif CONFIG_IDF_TARGET_ESP32P4 -#define RX2 (gpio_num_t)15 #endif #endif @@ -210,8 +208,6 @@ typedef enum { #define TX2 (gpio_num_t)25 #elif CONFIG_IDF_TARGET_ESP32S3 #define TX2 (gpio_num_t)20 -#elif CONFIG_IDF_TARGET_ESP32P4 -#define TX2 (gpio_num_t)14 #endif #endif #endif /* SOC_UART_HP_NUM > 2 */ From a97e7ab7964e48ee636bd36bde665442ca6df714 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Thu, 24 Oct 2024 11:45:21 -0300 Subject: [PATCH 15/30] fix(uart): no uart2,3,4 esp32-p4 pins check --- cores/esp32/HardwareSerial.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/cores/esp32/HardwareSerial.cpp b/cores/esp32/HardwareSerial.cpp index a6a7573f6e3..f758c63615f 100644 --- a/cores/esp32/HardwareSerial.cpp +++ b/cores/esp32/HardwareSerial.cpp @@ -341,14 +341,25 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in case UART_NUM_2: if (rxPin < 0 && txPin < 0) { // do not change RX2/TX2 if it has already been set before +#ifdef RX2 rxPin = _rxPin < 0 ? (int8_t)RX2 : _rxPin; +#endif +#ifdef TX2 txPin = _txPin < 0 ? (int8_t)TX2 : _txPin; +#endif } break; #endif } } + // if no RX/TX pins are defined, it will not start the UART driver + if (rxPin < 0 && txPin < 0) { + log_e("No RX/TX pins defined. Please set RX/TX pins."); + HSERIAL_MUTEX_UNLOCK(); + return; + } + // IDF UART driver keeps Pin setting on restarting. Negative Pin number will keep it unmodified. // it will detach previous UART attached pins From ec6436973dcef6e862b6a060df001ef4704cd545 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Thu, 24 Oct 2024 11:55:39 -0300 Subject: [PATCH 16/30] fix(uart): update uart.ino commentaries --- tests/validation/uart/uart.ino | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/validation/uart/uart.ino b/tests/validation/uart/uart.ino index 25947fe4c9b..5b29ac604d4 100644 --- a/tests/validation/uart/uart.ino +++ b/tests/validation/uart/uart.ino @@ -62,7 +62,7 @@ const test_uart_t test_uarts[SOC_UART_NUM] = { #endif .serial = Serial2, }, -#endif +#endif // SOC_UART_NUM >= 3 #if SOC_UART_NUM >= 4 { #ifdef RX3 @@ -74,7 +74,7 @@ const test_uart_t test_uarts[SOC_UART_NUM] = { #endif .serial = Serial3, }, -#endif +#endif // SOC_UART_NUM >= 4 #if SOC_UART_NUM >= 5 { #ifdef RX4 @@ -86,7 +86,7 @@ const test_uart_t test_uarts[SOC_UART_NUM] = { #endif .serial = Serial4, }, -#endif +#endif // SOC_UART_NUM >= 5 }; /* Utility global variables */ From 3c93de7c06e3fcaa807820da1f12c20599331be1 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Thu, 24 Oct 2024 12:00:33 -0300 Subject: [PATCH 17/30] feat(uart): allows the board pins_arduino.h to define other uart pins --- cores/esp32/HardwareSerial.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/cores/esp32/HardwareSerial.cpp b/cores/esp32/HardwareSerial.cpp index f758c63615f..0badf48ebdf 100644 --- a/cores/esp32/HardwareSerial.cpp +++ b/cores/esp32/HardwareSerial.cpp @@ -349,8 +349,34 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in #endif } break; +#endif +#if SOC_UART_HP_NUM > 3 // may save some flash bytes... + case UART_NUM_3: + if (rxPin < 0 && txPin < 0) { + // do not change RX2/TX2 if it has already been set before +#ifdef RX3 + rxPin = _rxPin < 0 ? (int8_t)RX3 : _rxPin; +#endif +#ifdef TX3 + txPin = _txPin < 0 ? (int8_t)TX3 : _txPin; +#endif + } + break; #endif } +#if SOC_UART_HP_NUM > 4 // may save some flash bytes... + case UART_NUM_4: + if (rxPin < 0 && txPin < 0) { + // do not change RX2/TX2 if it has already been set before +#ifdef RX4 + rxPin = _rxPin < 0 ? (int8_t)RX4 : _rxPin; +#endif +#ifdef TX4 + txPin = _txPin < 0 ? (int8_t)TX4 : _txPin; +#endif + } + break; +#endif } // if no RX/TX pins are defined, it will not start the UART driver From 06dffa141401023cdb961cece21e1fcc171ccbff Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Thu, 24 Oct 2024 12:05:44 -0300 Subject: [PATCH 18/30] fix(uart): typo - additional } in the code --- cores/esp32/HardwareSerial.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/cores/esp32/HardwareSerial.cpp b/cores/esp32/HardwareSerial.cpp index 0badf48ebdf..189b102380d 100644 --- a/cores/esp32/HardwareSerial.cpp +++ b/cores/esp32/HardwareSerial.cpp @@ -363,7 +363,6 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in } break; #endif - } #if SOC_UART_HP_NUM > 4 // may save some flash bytes... case UART_NUM_4: if (rxPin < 0 && txPin < 0) { From f9b291f96b9e55289f5b4da0f745687bbe5d533d Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Thu, 24 Oct 2024 12:11:02 -0300 Subject: [PATCH 19/30] fix(uart): wrong block coding - bad { } --- cores/esp32/HardwareSerial.cpp | 123 +++++++++++++++++---------------- 1 file changed, 62 insertions(+), 61 deletions(-) diff --git a/cores/esp32/HardwareSerial.cpp b/cores/esp32/HardwareSerial.cpp index 189b102380d..8131667c0f2 100644 --- a/cores/esp32/HardwareSerial.cpp +++ b/cores/esp32/HardwareSerial.cpp @@ -376,77 +376,78 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in } break; #endif - } + } - // if no RX/TX pins are defined, it will not start the UART driver - if (rxPin < 0 && txPin < 0) { - log_e("No RX/TX pins defined. Please set RX/TX pins."); - HSERIAL_MUTEX_UNLOCK(); - return; - } - - // IDF UART driver keeps Pin setting on restarting. Negative Pin number will keep it unmodified. - // it will detach previous UART attached pins + // if no RX/TX pins are defined, it will not start the UART driver + if (rxPin < 0 && txPin < 0) { + log_e("No RX/TX pins defined. Please set RX/TX pins."); + HSERIAL_MUTEX_UNLOCK(); + return; + } - // indicates that uartbegin() has to initialize a new IDF driver - if (_testUartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, _rxBufferSize, _txBufferSize, invert, rxfifo_full_thrhd)) { - _destroyEventTask(); // when IDF uart driver must be restarted, _eventTask must finish too - } + // IDF UART driver keeps Pin setting on restarting. Negative Pin number will keep it unmodified. + // it will detach previous UART attached pins - // IDF UART driver keeps Pin setting on restarting. Negative Pin number will keep it unmodified. - // it will detach previous UART attached pins - _uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, _rxBufferSize, _txBufferSize, invert, rxfifo_full_thrhd); - if (_uart == NULL) { - log_e("UART driver failed to start. Please check the logs."); - HSERIAL_MUTEX_UNLOCK(); - return; - } - if (!baud) { - // using baud rate as zero, forces it to try to detect the current baud rate in place - uartStartDetectBaudrate(_uart); - time_t startMillis = millis(); - unsigned long detectedBaudRate = 0; - while (millis() - startMillis < timeout_ms && !(detectedBaudRate = uartDetectBaudrate(_uart))) { - yield(); + // indicates that uartbegin() has to initialize a new IDF driver + if (_testUartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, _rxBufferSize, _txBufferSize, invert, rxfifo_full_thrhd)) { + _destroyEventTask(); // when IDF uart driver must be restarted, _eventTask must finish too + } + + // IDF UART driver keeps Pin setting on restarting. Negative Pin number will keep it unmodified. + // it will detach previous UART attached pins + _uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, _rxBufferSize, _txBufferSize, invert, rxfifo_full_thrhd); + if (_uart == NULL) { + log_e("UART driver failed to start. Please check the logs."); + HSERIAL_MUTEX_UNLOCK(); + return; } + if (!baud) { + // using baud rate as zero, forces it to try to detect the current baud rate in place + uartStartDetectBaudrate(_uart); + time_t startMillis = millis(); + unsigned long detectedBaudRate = 0; + while (millis() - startMillis < timeout_ms && !(detectedBaudRate = uartDetectBaudrate(_uart))) { + yield(); + } - if (detectedBaudRate) { - delay(100); // Give some time... - _uart = uartBegin(_uart_nr, detectedBaudRate, config, rxPin, txPin, _rxBufferSize, _txBufferSize, invert, rxfifo_full_thrhd); - if (_uart == NULL) { - log_e("UART driver failed to start. Please check the logs."); - HSERIAL_MUTEX_UNLOCK(); - return; + if (detectedBaudRate) { + delay(100); // Give some time... + _uart = uartBegin(_uart_nr, detectedBaudRate, config, rxPin, txPin, _rxBufferSize, _txBufferSize, invert, rxfifo_full_thrhd); + if (_uart == NULL) { + log_e("UART driver failed to start. Please check the logs."); + HSERIAL_MUTEX_UNLOCK(); + return; + } + } else { + log_e("Could not detect baudrate. Serial data at the port must be present within the timeout for detection to be possible"); + _uart = NULL; } - } else { - log_e("Could not detect baudrate. Serial data at the port must be present within the timeout for detection to be possible"); - _uart = NULL; } - } - // create a task to deal with Serial Events when, for example, calling begin() twice to change the baudrate, - // or when setting the callback before calling begin() - if (_uart != NULL && (_onReceiveCB != NULL || _onReceiveErrorCB != NULL) && _eventTask == NULL) { - _createEventTask(this); - } + // create a task to deal with Serial Events when, for example, calling begin() twice to change the baudrate, + // or when setting the callback before calling begin() + if (_uart != NULL && (_onReceiveCB != NULL || _onReceiveErrorCB != NULL) && _eventTask == NULL) { + _createEventTask(this); + } - // Set UART RX timeout - uartSetRxTimeout(_uart, _rxTimeout); - - // Set UART FIFO Full depending on the baud rate. - // Lower baud rates will force to emulate byte-by-byte reading - // Higher baud rates will keep IDF default of 120 bytes for FIFO FULL Interrupt - // It can also be changed by the application at any time - if (!_rxFIFOFull) { // it has not being changed before calling begin() - // set a default FIFO Full value for the IDF driver - uint8_t fifoFull = 1; - if (baud > 57600 || (_onReceiveCB != NULL && _onReceiveTimeout)) { - fifoFull = 120; + // Set UART RX timeout + uartSetRxTimeout(_uart, _rxTimeout); + + // Set UART FIFO Full depending on the baud rate. + // Lower baud rates will force to emulate byte-by-byte reading + // Higher baud rates will keep IDF default of 120 bytes for FIFO FULL Interrupt + // It can also be changed by the application at any time + if (!_rxFIFOFull) { // it has not being changed before calling begin() + // set a default FIFO Full value for the IDF driver + uint8_t fifoFull = 1; + if (baud > 57600 || (_onReceiveCB != NULL && _onReceiveTimeout)) { + fifoFull = 120; + } + uartSetRxFIFOFull(_uart, fifoFull); + _rxFIFOFull = fifoFull; } - uartSetRxFIFOFull(_uart, fifoFull); - _rxFIFOFull = fifoFull; - } - HSERIAL_MUTEX_UNLOCK(); + HSERIAL_MUTEX_UNLOCK(); + } } void HardwareSerial::updateBaudRate(unsigned long baud) { From 812e6c5532240778c6767caa518646fe49d70a2e Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Thu, 24 Oct 2024 12:14:20 -0300 Subject: [PATCH 20/30] fix(uart): code blocks errors --- cores/esp32/HardwareSerial.cpp | 122 ++++++++++++++++----------------- 1 file changed, 61 insertions(+), 61 deletions(-) diff --git a/cores/esp32/HardwareSerial.cpp b/cores/esp32/HardwareSerial.cpp index 8131667c0f2..87177b468b4 100644 --- a/cores/esp32/HardwareSerial.cpp +++ b/cores/esp32/HardwareSerial.cpp @@ -377,77 +377,77 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in break; #endif } + } + + // if no RX/TX pins are defined, it will not start the UART driver + if (rxPin < 0 && txPin < 0) { + log_e("No RX/TX pins defined. Please set RX/TX pins."); + HSERIAL_MUTEX_UNLOCK(); + return; + } - // if no RX/TX pins are defined, it will not start the UART driver - if (rxPin < 0 && txPin < 0) { - log_e("No RX/TX pins defined. Please set RX/TX pins."); - HSERIAL_MUTEX_UNLOCK(); - return; - } - - // IDF UART driver keeps Pin setting on restarting. Negative Pin number will keep it unmodified. - // it will detach previous UART attached pins + // IDF UART driver keeps Pin setting on restarting. Negative Pin number will keep it unmodified. + // it will detach previous UART attached pins - // indicates that uartbegin() has to initialize a new IDF driver - if (_testUartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, _rxBufferSize, _txBufferSize, invert, rxfifo_full_thrhd)) { - _destroyEventTask(); // when IDF uart driver must be restarted, _eventTask must finish too - } + // indicates that uartbegin() has to initialize a new IDF driver + if (_testUartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, _rxBufferSize, _txBufferSize, invert, rxfifo_full_thrhd)) { + _destroyEventTask(); // when IDF uart driver must be restarted, _eventTask must finish too + } - // IDF UART driver keeps Pin setting on restarting. Negative Pin number will keep it unmodified. - // it will detach previous UART attached pins - _uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, _rxBufferSize, _txBufferSize, invert, rxfifo_full_thrhd); - if (_uart == NULL) { - log_e("UART driver failed to start. Please check the logs."); - HSERIAL_MUTEX_UNLOCK(); - return; + // IDF UART driver keeps Pin setting on restarting. Negative Pin number will keep it unmodified. + // it will detach previous UART attached pins + _uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, _rxBufferSize, _txBufferSize, invert, rxfifo_full_thrhd); + if (_uart == NULL) { + log_e("UART driver failed to start. Please check the logs."); + HSERIAL_MUTEX_UNLOCK(); + return; + } + if (!baud) { + // using baud rate as zero, forces it to try to detect the current baud rate in place + uartStartDetectBaudrate(_uart); + time_t startMillis = millis(); + unsigned long detectedBaudRate = 0; + while (millis() - startMillis < timeout_ms && !(detectedBaudRate = uartDetectBaudrate(_uart))) { + yield(); } - if (!baud) { - // using baud rate as zero, forces it to try to detect the current baud rate in place - uartStartDetectBaudrate(_uart); - time_t startMillis = millis(); - unsigned long detectedBaudRate = 0; - while (millis() - startMillis < timeout_ms && !(detectedBaudRate = uartDetectBaudrate(_uart))) { - yield(); - } - if (detectedBaudRate) { - delay(100); // Give some time... - _uart = uartBegin(_uart_nr, detectedBaudRate, config, rxPin, txPin, _rxBufferSize, _txBufferSize, invert, rxfifo_full_thrhd); - if (_uart == NULL) { - log_e("UART driver failed to start. Please check the logs."); - HSERIAL_MUTEX_UNLOCK(); - return; - } - } else { - log_e("Could not detect baudrate. Serial data at the port must be present within the timeout for detection to be possible"); - _uart = NULL; + if (detectedBaudRate) { + delay(100); // Give some time... + _uart = uartBegin(_uart_nr, detectedBaudRate, config, rxPin, txPin, _rxBufferSize, _txBufferSize, invert, rxfifo_full_thrhd); + if (_uart == NULL) { + log_e("UART driver failed to start. Please check the logs."); + HSERIAL_MUTEX_UNLOCK(); + return; } + } else { + log_e("Could not detect baudrate. Serial data at the port must be present within the timeout for detection to be possible"); + _uart = NULL; } - // create a task to deal with Serial Events when, for example, calling begin() twice to change the baudrate, - // or when setting the callback before calling begin() - if (_uart != NULL && (_onReceiveCB != NULL || _onReceiveErrorCB != NULL) && _eventTask == NULL) { - _createEventTask(this); - } + } + // create a task to deal with Serial Events when, for example, calling begin() twice to change the baudrate, + // or when setting the callback before calling begin() + if (_uart != NULL && (_onReceiveCB != NULL || _onReceiveErrorCB != NULL) && _eventTask == NULL) { + _createEventTask(this); + } - // Set UART RX timeout - uartSetRxTimeout(_uart, _rxTimeout); - - // Set UART FIFO Full depending on the baud rate. - // Lower baud rates will force to emulate byte-by-byte reading - // Higher baud rates will keep IDF default of 120 bytes for FIFO FULL Interrupt - // It can also be changed by the application at any time - if (!_rxFIFOFull) { // it has not being changed before calling begin() - // set a default FIFO Full value for the IDF driver - uint8_t fifoFull = 1; - if (baud > 57600 || (_onReceiveCB != NULL && _onReceiveTimeout)) { - fifoFull = 120; - } - uartSetRxFIFOFull(_uart, fifoFull); - _rxFIFOFull = fifoFull; + // Set UART RX timeout + uartSetRxTimeout(_uart, _rxTimeout); + + // Set UART FIFO Full depending on the baud rate. + // Lower baud rates will force to emulate byte-by-byte reading + // Higher baud rates will keep IDF default of 120 bytes for FIFO FULL Interrupt + // It can also be changed by the application at any time + if (!_rxFIFOFull) { // it has not being changed before calling begin() + // set a default FIFO Full value for the IDF driver + uint8_t fifoFull = 1; + if (baud > 57600 || (_onReceiveCB != NULL && _onReceiveTimeout)) { + fifoFull = 120; } - - HSERIAL_MUTEX_UNLOCK(); + uartSetRxFIFOFull(_uart, fifoFull); + _rxFIFOFull = fifoFull; } + + HSERIAL_MUTEX_UNLOCK(); } void HardwareSerial::updateBaudRate(unsigned long baud) { From 4c62634ac77e7c36e0572ff39458c235a40a412a Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Thu, 24 Oct 2024 12:26:08 -0300 Subject: [PATCH 21/30] fix(uart): CI error with uninitialized struct member --- tests/validation/uart/uart.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/validation/uart/uart.ino b/tests/validation/uart/uart.ino index 5b29ac604d4..91eb3d74de5 100644 --- a/tests/validation/uart/uart.ino +++ b/tests/validation/uart/uart.ino @@ -85,7 +85,7 @@ const test_uart_t test_uarts[SOC_UART_NUM] = { .tx_pin = TX1, #endif .serial = Serial4, - }, + } #endif // SOC_UART_NUM >= 5 }; From df73cb41148435b36022576a502252116fbfa89b Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Thu, 24 Oct 2024 13:04:47 -0300 Subject: [PATCH 22/30] fix(uart): fixes uart.ino CI test code --- tests/validation/uart/uart.ino | 172 ++++++++++++++++----------------- 1 file changed, 86 insertions(+), 86 deletions(-) diff --git a/tests/validation/uart/uart.ino b/tests/validation/uart/uart.ino index 91eb3d74de5..9298dd801a2 100644 --- a/tests/validation/uart/uart.ino +++ b/tests/validation/uart/uart.ino @@ -1,9 +1,9 @@ /* UART test - * - * This test is using UART0 (Serial) only for reporting test status and helping with the auto - * baudrate detection test. - * UART1 (Serial1) and UART2 (Serial2), where available, are used for testing. - */ + + This test is using UART0 (Serial) only for reporting test status and helping with the auto + baudrate detection test. + UART1 (Serial1) and UART2 (Serial2), where available, are used for testing. +*/ #include #include "HardwareSerial.h" @@ -16,42 +16,42 @@ // UART0 TX | SOC_TX0 | 1 | 43 | 43 | 21 | 16 | 24 | 37 | // UART1 RX | RX1 | 26 | 4 | 15 | 18 | 4 | 0 | 11 | // UART1 TX | TX1 | 27 | 5 | 16 | 19 | 5 | 1 | 10 | -// UART2 RX | RX2 | 4 | -- | 19 | -- | -- | -- | -- | -// UART2 TX | TX2 | 25 | -- | 20 | -- | -- | -- | -- | +// UART2 RX | RX2 | 4 | -- | 19 | -- | -- | -- | UU | +// UART2 TX | TX2 | 25 | -- | 20 | -- | -- | -- | UU | /* - * For each UART: - * - * terminal - * | ^ - * v UART0 | - * RX ^ TX - * | - * report status - * | - * TX <---> RX - * UARTx - */ + For each UART: + + terminal + | ^ + v UART0 | + RX ^ TX + | + report status + | + TX <---> RX + UARTx +*/ typedef struct test_uart_t { uint8_t rx_pin; uint8_t tx_pin; - HardwareSerial &serial; + HardwareSerial *serial; } test_uart_t; -const test_uart_t test_uarts[SOC_UART_NUM] = { +const test_uart_t test_uarts[SOC_UART_HP_NUM] = { { .rx_pin = SOC_RX0, .tx_pin = SOC_TX0, - .serial = Serial, + .serial = &Serial, }, { .rx_pin = RX1, .tx_pin = TX1, - .serial = Serial1, + .serial = &Serial1, }, -#if SOC_UART_NUM >= 3 +#if SOC_UART_HP_NUM >= 3 { #ifdef RX2 .rx_pin = RX2, @@ -60,10 +60,10 @@ const test_uart_t test_uarts[SOC_UART_NUM] = { .rx_pin = RX1, .tx_pin = TX1, #endif - .serial = Serial2, + .serial = &Serial2, }, -#endif // SOC_UART_NUM >= 3 -#if SOC_UART_NUM >= 4 +#endif // SOC_UART_HP_NUM >= 3 +#if SOC_UART_HP_NUM >= 4 { #ifdef RX3 .rx_pin = RX3, @@ -72,10 +72,10 @@ const test_uart_t test_uarts[SOC_UART_NUM] = { .rx_pin = RX1, .tx_pin = TX1, #endif - .serial = Serial3, + .serial = &Serial3, }, -#endif // SOC_UART_NUM >= 4 -#if SOC_UART_NUM >= 5 +#endif // SOC_UART_HP_NUM >= 4 +#if SOC_UART_HP_NUM >= 5 { #ifdef RX4 .rx_pin = RX4, @@ -84,16 +84,16 @@ const test_uart_t test_uarts[SOC_UART_NUM] = { .rx_pin = RX1, .tx_pin = TX1, #endif - .serial = Serial4, + .serial = &Serial4, } -#endif // SOC_UART_NUM >= 5 +#endif // SOC_UART_HP_NUM >= 5 }; /* Utility global variables */ uint8_t i; static String recv_msg = ""; -static int peeked_char[SOC_UART_NUM]; +static int peeked_char[SOC_UART_HP_NUM]; /* Utility functions */ @@ -102,8 +102,8 @@ extern int8_t uart_get_TxPin(uint8_t uart_num); // This function starts all the available test UARTs void start_serial(unsigned long baudrate = 115200) { - for (i = 1; i < SOC_UART_NUM; i++) { - test_uarts[i].serial.begin(baudrate, SERIAL_8N1, test_uarts[i].rx_pin, test_uarts[i].tx_pin); + for (i = 1; i < SOC_UART_HP_NUM; i++) { + test_uarts[i].serial->begin(baudrate, SERIAL_8N1, test_uarts[i].rx_pin, test_uarts[i].tx_pin); while (!test_uarts[i].serial) { delay(10); } @@ -112,17 +112,17 @@ void start_serial(unsigned long baudrate = 115200) { // This function stops all the available test UARTs void stop_serial() { - for (i = 1; i < SOC_UART_NUM; i++) { - test_uarts[i].serial.end(); + for (i = 1; i < SOC_UART_HP_NUM; i++) { + test_uarts[i].serial->end(); } } // This function transmits a message and checks if it was received correctly void transmit_and_check_msg(const String msg_append, bool perform_assert = true) { delay(100); // Wait for some settings changes to take effect - for (i = 1; i < SOC_UART_NUM; i++) { - test_uarts[i].serial.print("Hello from Serial" + String(i) + " (UART" + String(i) + ") >>> via loopback >>> Serial" + String(i) + " (UART" + String(i) + ") " + msg_append); - test_uarts[i].serial.flush(); + for (i = 1; i < SOC_UART_HP_NUM; i++) { + test_uarts[i].serial->print("Hello from Serial" + String(i) + " (UART" + String(i) + ") >>> via loopback >>> Serial" + String(i) + " (UART" + String(i) + ") " + msg_append); + test_uarts[i].serial->flush(); delay(100); if (perform_assert) { TEST_ASSERT_EQUAL_STRING(("Hello from Serial" + String(i) + " (UART" + String(i) + ") >>> via loopback >>> Serial" + String(i) + " (UART" + String(i) + ") " + msg_append).c_str(), recv_msg.c_str()); @@ -133,21 +133,21 @@ void transmit_and_check_msg(const String msg_append, bool perform_assert = true) /* Tasks */ /* -// This task is used to send a message after a delay to test the auto baudrate detection -void task_delayed_msg(void *pvParameters) { + // This task is used to send a message after a delay to test the auto baudrate detection + void task_delayed_msg(void *pvParameters) { HardwareSerial *selected_serial; -#if SOC_UART_NUM >= 3 + #if SOC_UART_HP_NUM >= 3 selected_serial = &Serial1; -#else + #else selected_serial = &Serial; -#endif + #endif delay(2000); selected_serial->println("Hello from Serial to detect baudrate"); selected_serial->flush(); vTaskDelete(NULL); -} + } */ /* Unity functions */ @@ -156,10 +156,10 @@ void task_delayed_msg(void *pvParameters) { void setUp(void) { start_serial(115200); - for (i = 1; i < SOC_UART_NUM; i++) { + for (i = 1; i < SOC_UART_HP_NUM; i++) { log_d("Setup internal loop-back from and back to Serial%d (UART%d) TX >> Serial%d (UART%d) RX", i, i, i, i); - test_uarts[i].serial.onReceive([]() { + test_uarts[i].serial->onReceive([]() { onReceive_cb(i); }); uart_internal_loopback(i, true); @@ -175,18 +175,18 @@ void tearDown(void) { // This is a callback function that will be activated on UART RX events void onReceive_cb(int uart_num) { - HardwareSerial &selected_serial = test_uarts[uart_num].serial; + HardwareSerial *selected_serial = test_uarts[uart_num].serial; char c; recv_msg = ""; - size_t available = selected_serial.available(); + size_t available = selected_serial->available(); if (available != 0) { - peeked_char[uart_num] = selected_serial.peek(); + peeked_char[uart_num] = selected_serial->peek(); } while (available--) { - c = (char)selected_serial.read(); + c = (char)selected_serial->read(); recv_msg += c; } @@ -209,10 +209,10 @@ void change_baudrate_test(void) { //Test first using the updateBaudRate method and then using the begin method log_d("Changing baudrate to 9600"); - for (i = 1; i < SOC_UART_NUM; i++) { - test_uarts[i].serial.updateBaudRate(9600); + for (i = 1; i < SOC_UART_HP_NUM; i++) { + test_uarts[i].serial->updateBaudRate(9600); //Baudrate error should be within 2% of the target baudrate - TEST_ASSERT_UINT_WITHIN(192, 9600, test_uarts[i].serial.baudRate()); + TEST_ASSERT_UINT_WITHIN(192, 9600, test_uarts[i].serial->baudRate()); } log_d("Sending string using 9600 baudrate"); @@ -221,9 +221,9 @@ void change_baudrate_test(void) { log_d("Changing baudrate back to 115200"); start_serial(115200); - for (i = 1; i < SOC_UART_NUM; i++) { + for (i = 1; i < SOC_UART_HP_NUM; i++) { //Baudrate error should be within 2% of the target baudrate - TEST_ASSERT_UINT_WITHIN(2304, 115200, test_uarts[i].serial.baudRate()); + TEST_ASSERT_UINT_WITHIN(2304, 115200, test_uarts[i].serial->baudRate()); } log_d("Sending string using 115200 baudrate"); @@ -401,22 +401,22 @@ void change_pins_test(void) { log_d("Disabling UART loopback"); //stop_serial(); - for (i = 1; i < SOC_UART_NUM; i++) { + for (i = 1; i < SOC_UART_HP_NUM; i++) { uart_internal_loopback(i, false); } log_d("Swapping UART pins"); -#if SOC_UART_NUM == 2 +#if SOC_UART_HP_NUM == 2 Serial1.setPins(TX1, RX1); // Invert TX and RX pins TEST_ASSERT_EQUAL(TX1, uart_get_RxPin(1)); // TX1 is now RX pin TEST_ASSERT_EQUAL(RX1, uart_get_TxPin(1)); // RX1 is now TX pin -#elif SOC_UART_NUM >= 3 - for (i = 1; i < SOC_UART_NUM; i++) { +#elif SOC_UART_HP_NUM >= 3 + for (i = 1; i < SOC_UART_HP_NUM; i++) { // Swapping pins with the next available UART - int uart_replacement = (i + 1) % SOC_UART_NUM; + int uart_replacement = (i + 1) % SOC_UART_HP_NUM; uart_replacement = uart_replacement == 0 ? 1 : uart_replacement; - test_uarts[i].serial.setPins(test_uarts[uart_replacement].rx_pin, test_uarts[uart_replacement].tx_pin); + test_uarts[i].serial->setPins(test_uarts[uart_replacement].rx_pin, test_uarts[uart_replacement].tx_pin); TEST_ASSERT_EQUAL(test_uarts[uart_replacement].tx_pin, uart_get_TxPin(i)); TEST_ASSERT_EQUAL(test_uarts[uart_replacement].rx_pin, uart_get_RxPin(i)); } @@ -426,7 +426,7 @@ void change_pins_test(void) { log_d("Re-enabling UART loopback"); - for (i = 1; i < SOC_UART_NUM; i++) { + for (i = 1; i < SOC_UART_HP_NUM; i++) { uart_internal_loopback(i, true); } @@ -437,11 +437,11 @@ void change_pins_test(void) { /* -// The new loopback API does not allow cross connecting UARTs. This test is disabled for now. + // The new loopback API does not allow cross connecting UARTs. This test is disabled for now. -// This test checks if the auto baudrate detection works on ESP32 and ESP32-S2 -#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 -void auto_baudrate_test(void) { + // This test checks if the auto baudrate detection works on ESP32 and ESP32-S2 + #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 + void auto_baudrate_test(void) { log_d("Starting auto baudrate test"); HardwareSerial *selected_serial; @@ -449,12 +449,12 @@ void auto_baudrate_test(void) { log_d("Stopping test serial. Using Serial2 for ESP32 and Serial1 for ESP32-S2."); -#if SOC_UART_HP_NUM == 2 + #if SOC_UART_HP_NUM == 2 selected_serial = &Serial1; uart_internal_loopback(0, true); // it was suppose to cross connect TX0 to RX1 -#elif SOC_UART_NUM >= 3 + #elif SOC_UART_HP_NUM >= 3 selected_serial = &Serial2; -#endif + #endif //selected_serial->end(false); @@ -467,31 +467,31 @@ void auto_baudrate_test(void) { selected_serial->begin(0); baudrate = selected_serial->baudRate(); -#if SOC_UART_HP_NUM == 2 + #if SOC_UART_HP_NUM == 2 Serial.end(); Serial.begin(115200); -#endif + #endif TEST_ASSERT_UINT_WITHIN(2304, 115200, baudrate); Serial.println("Auto baudrate test successful"); -} -#endif + } + #endif */ // This test checks if the peripheral manager can properly manage UART pins void periman_test(void) { log_d("Checking if peripheral manager can properly manage UART pins"); - for (i = 1; i < SOC_UART_NUM; i++) { + for (i = 1; i < SOC_UART_HP_NUM; i++) { log_d("Setting up I2C on the same pins as UART%d", i); Wire.begin(test_uarts[i].rx_pin, test_uarts[i].tx_pin); recv_msg = ""; log_d("Trying to send message using UART%d with I2C enabled", i); - test_uarts[i].serial.print("Hello from Serial" + String(i) + " (UART" + String(i) + ") >>> via loopback >>> Serial" + String(i) + " (UART" + String(i) + ") while used by I2C"); - test_uarts[i].serial.flush(); + test_uarts[i].serial->print("Hello from Serial" + String(i) + " (UART" + String(i) + ") >>> via loopback >>> Serial" + String(i) + " (UART" + String(i) + ") while used by I2C"); + test_uarts[i].serial->flush(); delay(100); TEST_ASSERT_EQUAL_STRING("", recv_msg.c_str()); @@ -542,16 +542,16 @@ void setup() { } log_d("SOC_UART_HP_NUM = %d", SOC_UART_HP_NUM); - for (i = 0; i < SOC_UART_NUM; i++) { + for (i = 0; i < SOC_UART_HP_NUM; i++) { peeked_char[i] = -1; } // Begin needs to be called before setting up the loopback because it creates the serial object start_serial(115200); - for (i = 1; i < SOC_UART_NUM; i++) { + for (i = 1; i < SOC_UART_HP_NUM; i++) { log_d("Setup internal loop-back from and back to Serial%d (UART%d) TX >> Serial%d (UART%d) RX", i, i, i, i); - test_uarts[i].serial.onReceive([]() { + test_uarts[i].serial->onReceive([]() { onReceive_cb(i); }); uart_internal_loopback(i, true); @@ -567,11 +567,11 @@ void setup() { RUN_TEST(change_cpu_frequency_test); RUN_TEST(disabled_uart_calls_test); RUN_TEST(enabled_uart_calls_test); -/* -#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 - RUN_TEST(auto_baudrate_test); -#endif -*/ + /* + #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 + RUN_TEST(auto_baudrate_test); + #endif + */ RUN_TEST(periman_test); RUN_TEST(change_pins_test); RUN_TEST(end_when_stopped_test); From d9c398781a7c0549c886881c9835b1a83ffdb180 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Thu, 24 Oct 2024 13:52:26 -0300 Subject: [PATCH 23/30] fix(uart): keep previous pins as is in case -1 is used --- cores/esp32/HardwareSerial.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/cores/esp32/HardwareSerial.cpp b/cores/esp32/HardwareSerial.cpp index 87177b468b4..8da31427476 100644 --- a/cores/esp32/HardwareSerial.cpp +++ b/cores/esp32/HardwareSerial.cpp @@ -343,9 +343,13 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in // do not change RX2/TX2 if it has already been set before #ifdef RX2 rxPin = _rxPin < 0 ? (int8_t)RX2 : _rxPin; +#else + rxPin = _rxPin; #endif #ifdef TX2 txPin = _txPin < 0 ? (int8_t)TX2 : _txPin; +#else + txPin = _txPin; #endif } break; @@ -356,9 +360,13 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in // do not change RX2/TX2 if it has already been set before #ifdef RX3 rxPin = _rxPin < 0 ? (int8_t)RX3 : _rxPin; +#else + rxPin = _rxPin; #endif #ifdef TX3 txPin = _txPin < 0 ? (int8_t)TX3 : _txPin; +#else + txPin = _txPin; #endif } break; @@ -369,9 +377,13 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in // do not change RX2/TX2 if it has already been set before #ifdef RX4 rxPin = _rxPin < 0 ? (int8_t)RX4 : _rxPin; +#else + rxPin = _rxPin; #endif #ifdef TX4 txPin = _txPin < 0 ? (int8_t)TX4 : _txPin; +#else + txPin = _txPin; #endif } break; From b1428b3cf0de3d93c40b21d9c0affc2df3b8dd5c Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Fri, 25 Oct 2024 09:17:53 -0300 Subject: [PATCH 24/30] fix(uart): making the source code the same as in the branch --- cores/esp32/HardwareSerial.cpp | 8 -------- 1 file changed, 8 deletions(-) diff --git a/cores/esp32/HardwareSerial.cpp b/cores/esp32/HardwareSerial.cpp index 08cdddf7194..fb93dad1c47 100644 --- a/cores/esp32/HardwareSerial.cpp +++ b/cores/esp32/HardwareSerial.cpp @@ -364,7 +364,6 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in #endif #ifdef TX3 txPin = _txPin < 0 ? (int8_t)TX3 : _txPin; -#else #endif } break; @@ -384,13 +383,6 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in #endif } } - - // if no RX/TX pins are defined, it will not start the UART driver - if (rxPin < 0 && txPin < 0) { - log_e("No RX/TX pins defined. Please set RX/TX pins."); - HSERIAL_MUTEX_UNLOCK(); - return; - } // if no RX/TX pins are defined, it will not start the UART driver if (rxPin < 0 && txPin < 0) { From 520ea669ad18ada41103715c050c9e76f605a3cf Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Sat, 26 Oct 2024 10:48:43 -0300 Subject: [PATCH 25/30] feat(ci): simplification of uart testing sketch --- tests/validation/uart/uart.ino | 342 ++++++++------------------------- 1 file changed, 83 insertions(+), 259 deletions(-) diff --git a/tests/validation/uart/uart.ino b/tests/validation/uart/uart.ino index 55811c1c19b..f729bf02745 100644 --- a/tests/validation/uart/uart.ino +++ b/tests/validation/uart/uart.ino @@ -2,7 +2,7 @@ This test is using UART0 (Serial) only for reporting test status and helping with the auto baudrate detection test. - UART1 (Serial1) and UART2 (Serial2), where available, are used for testing. + UART1 (Serial1) is used for testing. */ #include @@ -16,11 +16,9 @@ // UART0 TX | SOC_TX0 | 1 | 43 | 43 | 21 | 16 | 24 | 37 | // UART1 RX | RX1 | 26 | 4 | 15 | 18 | 4 | 0 | 11 | // UART1 TX | TX1 | 27 | 5 | 16 | 19 | 5 | 1 | 10 | -// UART2 RX | RX2 | 4 | -- | 19 | -- | -- | -- | UU | -// UART2 TX | TX2 | 25 | -- | 20 | -- | -- | -- | UU | /* - For each UART: + Using 2 UARTS: terminal | ^ @@ -30,78 +28,16 @@ report status | TX <---> RX - UARTx + UART1 */ -typedef struct test_uart_t -{ - uint8_t rx_pin; - uint8_t tx_pin; - HardwareSerial *serial; -} test_uart_t; - -const test_uart_t test_uarts[SOC_UART_HP_NUM] = { - { - .rx_pin = SOC_RX0, - .tx_pin = SOC_TX0, - .serial = &Serial, - }, - { - .rx_pin = RX1, - .tx_pin = TX1, - .serial = &Serial1, - }, -#if SOC_UART_HP_NUM >= 3 - { -#ifdef RX2 - .rx_pin = RX2, - .tx_pin = TX2, -#else - .rx_pin = RX1, - .tx_pin = TX1, -#endif - .serial = &Serial2, - }, -#endif // SOC_UART_HP_NUM >= 3 -#if SOC_UART_HP_NUM >= 4 - { -#ifdef RX3 - .rx_pin = RX3, - .tx_pin = TX3, -#else - .rx_pin = RX1, - .tx_pin = TX1, -#endif - .serial = &Serial3, - }, -#endif // SOC_UART_HP_NUM >= 4 -#if SOC_UART_HP_NUM >= 5 - { -#ifdef RX4 - .rx_pin = RX4, - .tx_pin = TX4, -#else - .rx_pin = RX1, - .tx_pin = TX1, -#endif - .serial = &Serial4, - } -#endif // SOC_UART_HP_NUM >= 5 -}; - -// ESP32-P4 has no UART pin definition for RX2, TX2, RX3, TX3, RX4, TX4 -#ifndef RX2 -#define RX2 RX1 -#endif -#ifndef TX2 -#define TX2 RX1 -#endif +// Used for the pin swap test +#define NEW_RX1 10 +#define NEW_TX1 11 /* Utility global variables */ - -uint8_t i; static String recv_msg = ""; -static int peeked_char[SOC_UART_HP_NUM]; +static int peeked_char = -1; /* Utility functions */ @@ -109,69 +45,39 @@ extern int8_t uart_get_RxPin(uint8_t uart_num); extern int8_t uart_get_TxPin(uint8_t uart_num); // This function starts all the available test UARTs -void start_serial(unsigned long baudrate = 115200) { - for (i = 1; i < SOC_UART_HP_NUM; i++) { - test_uarts[i].serial->begin(baudrate, SERIAL_8N1, test_uarts[i].rx_pin, test_uarts[i].tx_pin); - while (!test_uarts[i].serial) { - delay(10); - } +void start_serial1(unsigned long baudrate = 115200) { + Serial1.begin(baudrate); + while (!Serial1) { + delay(10); } } // This function stops all the available test UARTs void stop_serial() { - for (i = 1; i < SOC_UART_HP_NUM; i++) { - test_uarts[i].serial->end(); - } + Serial1.end(); } // This function transmits a message and checks if it was received correctly void transmit_and_check_msg(const String msg_append, bool perform_assert = true) { delay(100); // Wait for some settings changes to take effect - for (i = 1; i < SOC_UART_HP_NUM; i++) { - test_uarts[i].serial->print("Hello from Serial" + String(i) + " (UART" + String(i) + ") >>> via loopback >>> Serial" + String(i) + " (UART" + String(i) + ") " + msg_append); - test_uarts[i].serial->flush(); - delay(100); - if (perform_assert) { - TEST_ASSERT_EQUAL_STRING(("Hello from Serial" + String(i) + " (UART" + String(i) + ") >>> via loopback >>> Serial" + String(i) + " (UART" + String(i) + ") " + msg_append).c_str(), recv_msg.c_str()); - } + Serial1.print("Hello from Serial1 (UART1) >>> via loopback >>> Serial1 (UART1) " + msg_append); + Serial1.flush(); + delay(100); + if (perform_assert) { + TEST_ASSERT_EQUAL_STRING(("Hello from Serial1 (UART1) >>> via loopback >>> Serial1 (UART1) " + msg_append).c_str(), recv_msg.c_str()); } } -/* Tasks */ - -/* - // This task is used to send a message after a delay to test the auto baudrate detection - void task_delayed_msg(void *pvParameters) { - HardwareSerial *selected_serial; - - #if SOC_UART_HP_NUM >= 3 - selected_serial = &Serial1; - #else - selected_serial = &Serial; - #endif - - delay(2000); - selected_serial->println("Hello from Serial to detect baudrate"); - selected_serial->flush(); - vTaskDelete(NULL); - } -*/ - /* Unity functions */ // This function is automatically called by unity before each test is run void setUp(void) { - start_serial(115200); - - for (i = 1; i < SOC_UART_HP_NUM; i++) { - log_d("Setup internal loop-back from and back to Serial%d (UART%d) TX >> Serial%d (UART%d) RX", i, i, i, i); - - test_uarts[i].serial->onReceive([]() { - onReceive_cb(i); - }); - uart_internal_loopback(i, true); - } + start_serial1(115200); + log_d("Setup internal loop-back from and back to Serial1 (UART1) TX >> Serial1 (UART1) RX"); + Serial1.onReceive([]() { + onReceive_cb(Serial1, 1); + }); + uart_internal_loopback(1, true); } // This function is automatically called by unity after each test is run @@ -182,57 +88,49 @@ void tearDown(void) { /* Callback functions */ // This is a callback function that will be activated on UART RX events -void onReceive_cb(int uart_num) { - HardwareSerial *selected_serial = test_uarts[uart_num].serial; +void onReceive_cb(HardwareSerial &HWSerial, uint8_t uartNum) { char c; recv_msg = ""; - size_t available = selected_serial->available(); - + size_t available = HWSerial.available(); if (available != 0) { - peeked_char[uart_num] = selected_serial->peek(); + peeked_char = HWSerial.peek(); } while (available--) { - c = (char)selected_serial->read(); + c = (char)HWSerial.read(); recv_msg += c; } - log_d("UART%d received message: %s\n", uart_num, recv_msg.c_str()); + log_d("UART%d received message: %s\n", uartNum, recv_msg.c_str()); } /* Test functions */ // This test checks if a message can be transmitted and received correctly using the default settings -void basic_transmission_test(void) { +void basic_transmission_test() { log_d("Performing basic transmission test"); - transmit_and_check_msg(""); - Serial.println("Basic transmission test successful"); } // This test checks if the baudrate can be changed and if the message can be transmitted and received correctly after the change -void change_baudrate_test(void) { +void change_baudrate_test() { //Test first using the updateBaudRate method and then using the begin method log_d("Changing baudrate to 9600"); - for (i = 1; i < SOC_UART_HP_NUM; i++) { - test_uarts[i].serial->updateBaudRate(9600); - //Baudrate error should be within 2% of the target baudrate - TEST_ASSERT_UINT_WITHIN(192, 9600, test_uarts[i].serial->baudRate()); - } + //Baudrate error should be within 2% of the target baudrate + Serial1.updateBaudRate(9600); + TEST_ASSERT_UINT_WITHIN(192, 9600, Serial1.baudRate()); log_d("Sending string using 9600 baudrate"); transmit_and_check_msg("using 9600 baudrate"); log_d("Changing baudrate back to 115200"); - start_serial(115200); + start_serial1(115200); - for (i = 1; i < SOC_UART_HP_NUM; i++) { - //Baudrate error should be within 2% of the target baudrate - TEST_ASSERT_UINT_WITHIN(2304, 115200, test_uarts[i].serial->baudRate()); - } + //Baudrate error should be within 2% of the target baudrate + TEST_ASSERT_UINT_WITHIN(2304, 115200, Serial1.baudRate()); log_d("Sending string using 115200 baudrate"); transmit_and_check_msg("using 115200 baudrate"); @@ -241,7 +139,7 @@ void change_baudrate_test(void) { } // This test checks if the buffers can be resized properly -void resize_buffers_test(void) { +void resize_buffers_test() { size_t ret; log_d("Trying to resize RX buffer while running."); @@ -266,17 +164,31 @@ void resize_buffers_test(void) { } // This test checks if the begin function can be called when the UART is already running -void begin_when_running_test(void) { +void begin_when_running_test() { log_d("Trying to set up serial twice"); - start_serial(115200); + start_serial1(115200); + start_serial1(115200); + // starting other Serial ports as part of the test +#if SOC_UART_HP_NUM > 2 + Serial2.begin(115200, SERIAL_8N1, RX1, TX1); + Serial2.begin(115200); +#endif +#if SOC_UART_HP_NUM > 3 + Serial3.begin(115200, SERIAL_8N1, RX1, TX1); + Serial3.begin(115200); +#endif +#if SOC_UART_HP_NUM > 4 + Serial4.begin(115200, SERIAL_8N1, RX1, TX1); + Serial4.begin(115200); +#endif Serial.println("Begin when running test successful"); } // This test checks if the end function can be called when the UART is already stopped -void end_when_stopped_test(void) { +void end_when_stopped_test() { log_d("Trying to end serial twice"); - // Calling end() twice should not crash + // Calling end(true) twice should not crash stop_serial(); stop_serial(); @@ -284,7 +196,7 @@ void end_when_stopped_test(void) { } // This test checks if all the UART methods work when the UART is running -void enabled_uart_calls_test(void) { +void enabled_uart_calls_test() { bool boolean_ret; long int integer_ret; uint8_t test_buf[1]; @@ -302,7 +214,7 @@ void enabled_uart_calls_test(void) { TEST_ASSERT_EQUAL(true, boolean_ret); log_d("Checking if Serial 1 is peekable while running"); - TEST_ASSERT_GREATER_OR_EQUAL(0, peeked_char[1]); + TEST_ASSERT_GREATER_OR_EQUAL(0, peeked_char); log_d("Checking if Serial 1 can read bytes while running"); integer_ret = Serial1.readBytes(test_buf, 1); @@ -333,7 +245,7 @@ void enabled_uart_calls_test(void) { } // This test checks if all the UART methods work when the UART is stopped -void disabled_uart_calls_test(void) { +void disabled_uart_calls_test() { bool boolean_ret; int integer_ret; uint8_t test_buf[1]; @@ -405,118 +317,56 @@ void disabled_uart_calls_test(void) { } // This test checks if the pins can be changed and if the message can be transmitted and received correctly after the change -void change_pins_test(void) { - log_d("Disabling UART loopback"); +void change_pins_test() { //stop_serial(); - for (i = 1; i < SOC_UART_HP_NUM; i++) { - uart_internal_loopback(i, false); - } + log_d("Disabling UART loopback"); + uart_internal_loopback(0, false); log_d("Swapping UART pins"); -#if SOC_UART_HP_NUM == 2 - Serial1.setPins(TX1, RX1); // Invert TX and RX pins - TEST_ASSERT_EQUAL(TX1, uart_get_RxPin(1)); // TX1 is now RX pin - TEST_ASSERT_EQUAL(RX1, uart_get_TxPin(1)); // RX1 is now TX pin -#elif SOC_UART_HP_NUM >= 3 - for (i = 1; i < SOC_UART_HP_NUM; i++) { - // Swapping pins with the next available UART - int uart_replacement = (i + 1) % SOC_UART_HP_NUM; - uart_replacement = uart_replacement == 0 ? 1 : uart_replacement; - test_uarts[i].serial->setPins(test_uarts[uart_replacement].rx_pin, test_uarts[uart_replacement].tx_pin); - TEST_ASSERT_EQUAL(test_uarts[uart_replacement].tx_pin, uart_get_TxPin(i)); - TEST_ASSERT_EQUAL(test_uarts[uart_replacement].rx_pin, uart_get_RxPin(i)); - } -#endif + Serial1.setPins(NEW_RX1, NEW_TX1); + TEST_ASSERT_EQUAL(NEW_RX1, uart_get_RxPin(1)); + TEST_ASSERT_EQUAL(NEW_TX1, uart_get_TxPin(1)); - start_serial(115200); + start_serial1(115200); log_d("Re-enabling UART loopback"); - for (i = 1; i < SOC_UART_HP_NUM; i++) { - uart_internal_loopback(i, true); - } + uart_internal_loopback(1, true); transmit_and_check_msg("using new pins"); Serial.println("Change pins test successful"); } -/* - - // The new loopback API does not allow cross connecting UARTs. This test is disabled for now. - - // This test checks if the auto baudrate detection works on ESP32 and ESP32-S2 - #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 - void auto_baudrate_test(void) { - log_d("Starting auto baudrate test"); - - HardwareSerial *selected_serial; - unsigned long baudrate; - - log_d("Stopping test serial. Using Serial2 for ESP32 and Serial1 for ESP32-S2."); - - #if SOC_UART_HP_NUM == 2 - selected_serial = &Serial1; - uart_internal_loopback(0, true); // it was suppose to cross connect TX0 to RX1 - #elif SOC_UART_HP_NUM >= 3 - selected_serial = &Serial2; - #endif - - //selected_serial->end(false); - - log_d("Starting delayed task to send message"); - - xTaskCreate(task_delayed_msg, "task_delayed_msg", 2048, NULL, 2, NULL); - - log_d("Starting serial with auto baudrate detection"); - - selected_serial->begin(0); - baudrate = selected_serial->baudRate(); - - #if SOC_UART_HP_NUM == 2 - Serial.end(); - Serial.begin(115200); - #endif - - TEST_ASSERT_UINT_WITHIN(2304, 115200, baudrate); - - Serial.println("Auto baudrate test successful"); - } - #endif -*/ - // This test checks if the peripheral manager can properly manage UART pins -void periman_test(void) { +void periman_test() { log_d("Checking if peripheral manager can properly manage UART pins"); - for (i = 1; i < SOC_UART_HP_NUM; i++) { - log_d("Setting up I2C on the same pins as UART%d", i); - Wire.begin(test_uarts[i].rx_pin, test_uarts[i].tx_pin); + log_d("Setting up I2C on the same pins as UART"); - recv_msg = ""; + Wire.begin(RX1, TX1); - log_d("Trying to send message using UART%d with I2C enabled", i); - test_uarts[i].serial->print("Hello from Serial" + String(i) + " (UART" + String(i) + ") >>> via loopback >>> Serial" + String(i) + " (UART" + String(i) + ") while used by I2C"); - test_uarts[i].serial->flush(); - delay(100); - TEST_ASSERT_EQUAL_STRING("", recv_msg.c_str()); + recv_msg = ""; - log_d("Disabling I2C and re-enabling UART"); + log_d("Trying to send message using UART with I2C enabled"); + transmit_and_check_msg("while used by I2C", false); + TEST_ASSERT_EQUAL_STRING("", recv_msg.c_str()); - Serial1.setPins(test_uarts[i].rx_pin, test_uarts[i].tx_pin); - uart_internal_loopback(i, true); + log_d("Disabling I2C and re-enabling UART"); - log_d("Trying to send message using UART with I2C disabled"); - transmit_and_check_msg("while I2C is disabled"); - } + Serial1.setPins(RX1, TX1); + uart_internal_loopback(1, true); + + log_d("Trying to send message using UART with I2C disabled"); + transmit_and_check_msg("while I2C is disabled"); Serial.println("Peripheral manager test successful"); } // This test checks if messages can be transmitted and received correctly after changing the CPU frequency -void change_cpu_frequency_test(void) { +void change_cpu_frequency_test() { uint32_t old_freq = getCpuFrequencyMhz(); uint32_t new_freq = getXtalFrequencyMhz(); @@ -524,7 +374,7 @@ void change_cpu_frequency_test(void) { Serial.flush(); setCpuFrequencyMhz(new_freq); - Serial.updateBaudRate(115200); + Serial1.updateBaudRate(115200); log_d("Trying to send message with the new CPU frequency"); transmit_and_check_msg("with new CPU frequency"); @@ -533,7 +383,7 @@ void change_cpu_frequency_test(void) { Serial.flush(); setCpuFrequencyMhz(old_freq); - Serial.updateBaudRate(115200); + Serial1.updateBaudRate(115200); log_d("Trying to send message with the original CPU frequency"); transmit_and_check_msg("with the original CPU frequency"); @@ -548,25 +398,6 @@ void setup() { while (!Serial) { delay(10); } - log_d("SOC_UART_HP_NUM = %d", SOC_UART_HP_NUM); - - for (i = 0; i < SOC_UART_HP_NUM; i++) { - peeked_char[i] = -1; - } - - // Begin needs to be called before setting up the loopback because it creates the serial object - start_serial(115200); - - for (i = 1; i < SOC_UART_HP_NUM; i++) { - log_d("Setup internal loop-back from and back to Serial%d (UART%d) TX >> Serial%d (UART%d) RX", i, i, i, i); - test_uarts[i].serial->onReceive([]() { - onReceive_cb(i); - }); - uart_internal_loopback(i, true); - } - - log_d("Setup done. Starting tests"); - UNITY_BEGIN(); RUN_TEST(begin_when_running_test); RUN_TEST(basic_transmission_test); @@ -575,17 +406,10 @@ void setup() { RUN_TEST(change_cpu_frequency_test); RUN_TEST(disabled_uart_calls_test); RUN_TEST(enabled_uart_calls_test); - /* - #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 - RUN_TEST(auto_baudrate_test); - #endif - */ RUN_TEST(periman_test); RUN_TEST(change_pins_test); RUN_TEST(end_when_stopped_test); UNITY_END(); } -void loop() { - vTaskDelete(NULL); -} +void loop() {} From be20fcbae373f56dfd859c3550258500cdca7e42 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Sat, 26 Oct 2024 10:51:10 -0300 Subject: [PATCH 26/30] feat(ci): fixing and adapting perimanager teste --- tests/validation/periman/periman.ino | 65 ++++++++++++++++++---------- 1 file changed, 42 insertions(+), 23 deletions(-) diff --git a/tests/validation/periman/periman.ino b/tests/validation/periman/periman.ino index ad15214dc57..62963329eed 100644 --- a/tests/validation/periman/periman.ino +++ b/tests/validation/periman/periman.ino @@ -1,14 +1,14 @@ /* Peripheral Manager test - * - * This test is using Serial to check if the peripheral manager is able to - * attach and detach peripherals correctly on shared pins. - * Make sure that the peripheral names contain only letters, numbers and underscores. - * - * This test skips the following peripherals: - * - USB: USB is not able to be detached - * - SDMMC: SDMMC requires a card to be mounted before the pins are attached - * - ETH: ETH requires a ethernet port to be connected before the pins are attached - */ + + This test is using Serial to check if the peripheral manager is able to + attach and detach peripherals correctly on shared pins. + Make sure that the peripheral names contain only letters, numbers and underscores. + + This test skips the following peripherals: + - USB: USB is not able to be detached + - SDMMC: SDMMC requires a card to be mounted before the pins are attached + - ETH: ETH requires a ethernet port to be connected before the pins are attached +*/ #if SOC_I2S_SUPPORTED #include "ESP_I2S.h" @@ -62,7 +62,7 @@ void onReceive_cb(void) { // This function is called by before each test is run void setup_test(String test_name, int8_t rx_pin = UART1_RX_DEFAULT, int8_t tx_pin = UART1_TX_DEFAULT) { - log_v("Setting up %s test", test_name.c_str()); + log_i("Setting up %s test", test_name.c_str()); current_test = test_name; uart1_rx_pin = rx_pin; @@ -74,12 +74,12 @@ void setup_test(String test_name, int8_t rx_pin = UART1_RX_DEFAULT, int8_t tx_pi Serial1.setPins(uart1_rx_pin, uart1_tx_pin); uart_internal_loopback(1, true); delay(100); - log_v("Running %s test", test_name.c_str()); + log_i("Running %s test", test_name.c_str()); } // This function is called after each test is run void teardown_test(void) { - log_v("Tearing down %s test", current_test.c_str()); + log_i("Tearing down %s test", current_test.c_str()); if (test_executed) { pinMode(uart1_rx_pin, INPUT_PULLUP); pinMode(uart1_tx_pin, OUTPUT); @@ -96,13 +96,15 @@ void teardown_test(void) { Serial1.println(" test: This should be printed"); Serial1.flush(); - log_v("Finished %s test", current_test.c_str()); + log_i("Finished %s test", current_test.c_str()); + Serial.println("========================================================================================="); + delay(2000); } /* Test functions */ /* These functions must call "setup_test" and "teardown_test" and set "test_executed" to true - * if the test is executed - */ + if the test is executed +*/ void gpio_test(void) { setup_test("GPIO"); @@ -130,7 +132,7 @@ void sigmadelta_test(void) { void adc_oneshot_test(void) { #if !SOC_ADC_SUPPORTED - setup_test("ADC_Oneshot"); + setup_test("ADC_Oneshot - NO ADC Support"); #else setup_test("ADC_Oneshot", ADC1_DEFAULT, ADC2_DEFAULT); test_executed = true; @@ -151,8 +153,8 @@ void ARDUINO_ISR_ATTR adcComplete() { #endif void adc_continuous_test(void) { -#if !SOC_ADC_SUPPORTED - setup_test("ADC_Continuous"); +#if !SOC_ADC_SUPPORTED || CONFIG_IDF_TARGET_ESP32P4 + setup_test("ADC_Continuous - NO ADC Support"); #else setup_test("ADC_Continuous", ADC1_DEFAULT, ADC2_DEFAULT); test_executed = true; @@ -166,8 +168,10 @@ void adc_continuous_test(void) { analogContinuous(adc_pins, adc_pins_count, 6, 20000, &adcComplete); analogContinuousStart(); + Serial.println(); while (adc_coversion_done == false) { - delay(1); + Serial.print("."); + delay(500); } if (!analogContinuousRead(&result, 0)) { @@ -226,12 +230,13 @@ void i2s_test(void) { I2SClass i2s; i2s.setPins(uart1_rx_pin, uart1_tx_pin, -1); + i2s.setTimeout(1000); if (!i2s.begin(I2S_MODE_STD, 16000, I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO)) { - Serial.println("I2S init failed"); } #endif teardown_test(); + delay(500); Serial.println("Exting I2S driver..."); delay(500); } void i2c_test(void) { @@ -282,17 +287,31 @@ void setup() { Serial1.onReceive(onReceive_cb); uart_internal_loopback(1, true); + Serial.println("starting gpio_test()"); gpio_test(); + Serial.println("starting sigmadelta_test()"); sigmadelta_test(); + Serial.println("starting ledc_test()"); ledc_test(); + Serial.println("starting rmt_test()"); rmt_test(); - i2s_test(); - i2c_test(); + Serial.println("\t ==>starting i2s_test()"); + i2s_test(); // apos esta linha temos um [E][esp32-hal-periman.c:122] perimanSetPinBus(): Invalid pin: 255 + Serial.println("\t ==>Back to setup()"); + Serial.println("\t ==>starting spi_test()"); spi_test(); + Serial.println("\t ==>starting i2c_test()"); + i2c_test(); + Serial.println("\t ==>ended i2c_test()"); + Serial.println("starting adc_oneshot_test()"); adc_oneshot_test(); + Serial.println("starting adc_continuous_test()"); adc_continuous_test(); + Serial.println("starting dac_test()"); dac_test(); + Serial.println("starting touch_test()"); touch_test(); + Serial.println("starting gpio_test()"); // Print to Serial1 to avoid buffering issues Serial1.println("Peripheral Manager test done"); From 6e5baf12c0f9e4a067ddbb3b608108a99a52db72 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Sat, 26 Oct 2024 10:58:28 -0300 Subject: [PATCH 27/30] fix(ci): cleanup code --- tests/validation/periman/periman.ino | 32 ++++++---------------------- 1 file changed, 6 insertions(+), 26 deletions(-) diff --git a/tests/validation/periman/periman.ino b/tests/validation/periman/periman.ino index 62963329eed..3bd1e79596e 100644 --- a/tests/validation/periman/periman.ino +++ b/tests/validation/periman/periman.ino @@ -62,7 +62,7 @@ void onReceive_cb(void) { // This function is called by before each test is run void setup_test(String test_name, int8_t rx_pin = UART1_RX_DEFAULT, int8_t tx_pin = UART1_TX_DEFAULT) { - log_i("Setting up %s test", test_name.c_str()); + log_d("Setting up %s test", test_name.c_str()); current_test = test_name; uart1_rx_pin = rx_pin; @@ -74,12 +74,12 @@ void setup_test(String test_name, int8_t rx_pin = UART1_RX_DEFAULT, int8_t tx_pi Serial1.setPins(uart1_rx_pin, uart1_tx_pin); uart_internal_loopback(1, true); delay(100); - log_i("Running %s test", test_name.c_str()); + log_d("Running %s test", test_name.c_str()); } // This function is called after each test is run void teardown_test(void) { - log_i("Tearing down %s test", current_test.c_str()); + log_d("Tearing down %s test", current_test.c_str()); if (test_executed) { pinMode(uart1_rx_pin, INPUT_PULLUP); pinMode(uart1_tx_pin, OUTPUT); @@ -154,7 +154,7 @@ void ARDUINO_ISR_ATTR adcComplete() { void adc_continuous_test(void) { #if !SOC_ADC_SUPPORTED || CONFIG_IDF_TARGET_ESP32P4 - setup_test("ADC_Continuous - NO ADC Support"); + setup_test("ADC_Continuous - NO Support ** P4 fails"); #else setup_test("ADC_Continuous", ADC1_DEFAULT, ADC2_DEFAULT); test_executed = true; @@ -168,10 +168,8 @@ void adc_continuous_test(void) { analogContinuous(adc_pins, adc_pins_count, 6, 20000, &adcComplete); analogContinuousStart(); - Serial.println(); while (adc_coversion_done == false) { - Serial.print("."); - delay(500); + delay(1); } if (!analogContinuousRead(&result, 0)) { @@ -230,13 +228,11 @@ void i2s_test(void) { I2SClass i2s; i2s.setPins(uart1_rx_pin, uart1_tx_pin, -1); - i2s.setTimeout(1000); if (!i2s.begin(I2S_MODE_STD, 16000, I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO)) { } #endif teardown_test(); - delay(500); Serial.println("Exting I2S driver..."); delay(500); } void i2c_test(void) { @@ -287,34 +283,18 @@ void setup() { Serial1.onReceive(onReceive_cb); uart_internal_loopback(1, true); - Serial.println("starting gpio_test()"); gpio_test(); - Serial.println("starting sigmadelta_test()"); sigmadelta_test(); - Serial.println("starting ledc_test()"); ledc_test(); - Serial.println("starting rmt_test()"); rmt_test(); - Serial.println("\t ==>starting i2s_test()"); - i2s_test(); // apos esta linha temos um [E][esp32-hal-periman.c:122] perimanSetPinBus(): Invalid pin: 255 - Serial.println("\t ==>Back to setup()"); - Serial.println("\t ==>starting spi_test()"); + i2s_test(); spi_test(); - Serial.println("\t ==>starting i2c_test()"); i2c_test(); - Serial.println("\t ==>ended i2c_test()"); - Serial.println("starting adc_oneshot_test()"); adc_oneshot_test(); - Serial.println("starting adc_continuous_test()"); adc_continuous_test(); - Serial.println("starting dac_test()"); dac_test(); - Serial.println("starting touch_test()"); touch_test(); - Serial.println("starting gpio_test()"); // Print to Serial1 to avoid buffering issues Serial1.println("Peripheral Manager test done"); } - -void loop() {} From cb7b967aa7c130b6a59f07f5f9aafe76f0e04f46 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Sat, 26 Oct 2024 11:02:28 -0300 Subject: [PATCH 28/30] fix(ci): minimal changes --- tests/validation/periman/periman.ino | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/validation/periman/periman.ino b/tests/validation/periman/periman.ino index 3bd1e79596e..49c5f03dc9f 100644 --- a/tests/validation/periman/periman.ino +++ b/tests/validation/periman/periman.ino @@ -96,9 +96,7 @@ void teardown_test(void) { Serial1.println(" test: This should be printed"); Serial1.flush(); - log_i("Finished %s test", current_test.c_str()); - Serial.println("========================================================================================="); - delay(2000); + log_d("Finished %s test", current_test.c_str()); } /* Test functions */ @@ -288,8 +286,8 @@ void setup() { ledc_test(); rmt_test(); i2s_test(); - spi_test(); i2c_test(); + spi_test(); adc_oneshot_test(); adc_continuous_test(); dac_test(); @@ -298,3 +296,5 @@ void setup() { // Print to Serial1 to avoid buffering issues Serial1.println("Peripheral Manager test done"); } + +void loop() {} From 5ff67d6de78f880475e82b2e7538577246d4447e Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Sat, 26 Oct 2024 11:05:21 -0300 Subject: [PATCH 29/30] fix(ci): restore log level to debug --- tests/validation/periman/periman.ino | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/validation/periman/periman.ino b/tests/validation/periman/periman.ino index 49c5f03dc9f..3a885f673da 100644 --- a/tests/validation/periman/periman.ino +++ b/tests/validation/periman/periman.ino @@ -62,7 +62,7 @@ void onReceive_cb(void) { // This function is called by before each test is run void setup_test(String test_name, int8_t rx_pin = UART1_RX_DEFAULT, int8_t tx_pin = UART1_TX_DEFAULT) { - log_d("Setting up %s test", test_name.c_str()); + log_v("Setting up %s test", test_name.c_str()); current_test = test_name; uart1_rx_pin = rx_pin; @@ -74,12 +74,12 @@ void setup_test(String test_name, int8_t rx_pin = UART1_RX_DEFAULT, int8_t tx_pi Serial1.setPins(uart1_rx_pin, uart1_tx_pin); uart_internal_loopback(1, true); delay(100); - log_d("Running %s test", test_name.c_str()); + log_v("Running %s test", test_name.c_str()); } // This function is called after each test is run void teardown_test(void) { - log_d("Tearing down %s test", current_test.c_str()); + log_v("Tearing down %s test", current_test.c_str()); if (test_executed) { pinMode(uart1_rx_pin, INPUT_PULLUP); pinMode(uart1_tx_pin, OUTPUT); @@ -96,7 +96,7 @@ void teardown_test(void) { Serial1.println(" test: This should be printed"); Serial1.flush(); - log_d("Finished %s test", current_test.c_str()); + log_v("Finished %s test", current_test.c_str()); } /* Test functions */ From b342545762ab8e811600d5178433e98f1b7183b0 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Sat, 26 Oct 2024 11:07:21 -0300 Subject: [PATCH 30/30] fix(ci): missing error message --- tests/validation/periman/periman.ino | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/validation/periman/periman.ino b/tests/validation/periman/periman.ino index 3a885f673da..5e7fad90e61 100644 --- a/tests/validation/periman/periman.ino +++ b/tests/validation/periman/periman.ino @@ -228,6 +228,7 @@ void i2s_test(void) { i2s.setPins(uart1_rx_pin, uart1_tx_pin, -1); i2s.setTimeout(1000); if (!i2s.begin(I2S_MODE_STD, 16000, I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO)) { + Serial.println("I2S init failed"); } #endif teardown_test();