Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e159bf6

Browse files
lucasssvazSuGliderpre-commit-ci-lite[bot]
authoredDec 9, 2024··
refactor(uart): Refactor UART test to work with any number of UARTs (#10593)
* refactor(uart): Refactor UART test to work with any number of UARTs Co-authored-by: Rodrigo Garcia <rodrigo.garcia@espressif.com> * fix(uart): Set CPU freq on ESP32 * ci(pre-commit): Apply automatic fixes * fix(spelling): Fix codespell error --------- Co-authored-by: Rodrigo Garcia <rodrigo.garcia@espressif.com> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent af84da6 commit e159bf6

File tree

2 files changed

+240
-285
lines changed

2 files changed

+240
-285
lines changed
 
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"version": 1,
3+
"author": "lucasssvaz",
4+
"editor": "wokwi",
5+
"parts": [
6+
{
7+
"type": "board-esp32-devkit-c-v4",
8+
"id": "esp",
9+
"attrs": { "cpuFrequency": "40" }
10+
}
11+
],
12+
"connections": [
13+
[
14+
"esp:TX",
15+
"$serialMonitor:RX",
16+
""
17+
],
18+
[
19+
"esp:RX",
20+
"$serialMonitor:TX",
21+
""
22+
]
23+
]
24+
}

‎tests/validation/uart/uart.ino

Lines changed: 216 additions & 285 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,20 @@
22
*
33
* This test is using UART0 (Serial) only for reporting test status and helping with the auto
44
* baudrate detection test.
5-
* UART1 (Serial1) and UART2 (Serial2), where available, are used for testing.
5+
* The other serials are used for testing.
66
*/
77

8-
#include <unity.h>
9-
#include "HardwareSerial.h"
10-
#include "esp_rom_gpio.h"
11-
#include "Wire.h"
12-
138
// Default pins:
14-
// | Name | ESP32 | S2 | S3 | C3 | C6 | H2 |
15-
// UART0 RX | SOC_RX0 | 3 | 44 | 44 | 20 | 17 | 23 |
16-
// UART0 TX | SOC_TX0 | 1 | 43 | 43 | 21 | 16 | 24 |
17-
// UART1 RX | RX1 | 26 | 4 | 15 | 18 | 4 | 0 |
18-
// UART1 TX | TX1 | 27 | 5 | 16 | 19 | 5 | 1 |
19-
// UART2 RX | RX2 | 4 | -- | 19 | -- | -- | -- |
20-
// UART2 TX | TX2 | 25 | -- | 20 | -- | -- | -- |
9+
// | Name | ESP32 | S2 | S3 | C3 | C6 | H2 | P4 |
10+
// UART0 RX | SOC_RX0 | 3 | 44 | 44 | 20 | 17 | 23 | 38 |
11+
// UART0 TX | SOC_TX0 | 1 | 43 | 43 | 21 | 16 | 24 | 37 |
12+
// UART1 RX | RX1 | 26 | 4 | 15 | 18 | 4 | 0 | 11 |
13+
// UART1 TX | TX1 | 27 | 5 | 16 | 19 | 5 | 1 | 10 |
14+
// UART2 RX | RX2 | 4 | -- | 19 | -- | -- | -- | -- |
15+
// UART2 TX | TX2 | 25 | -- | 20 | -- | -- | -- | -- |
2116

2217
/*
23-
* For 2 UARTS:
18+
* For each UART:
2419
*
2520
* terminal
2621
* | ^
@@ -30,187 +25,119 @@
3025
* report status
3126
* |
3227
* TX <---> RX
33-
* UART1
34-
*
35-
* For 3 UARTS:
36-
*
37-
* =====terminal======
38-
* ^ | ^ ^
39-
* | v UART0 | |
40-
* | RX TX |
41-
* | |
42-
* ^ report status ^
43-
* | |
44-
* | TX ---> RX |
45-
* UART2 RX <--- TX UART1
46-
*
28+
* UARTx
4729
*/
4830

49-
#if SOC_UART_HP_NUM == 2
50-
// Used for the pin swap test
51-
#define NEW_RX1 9
52-
#define NEW_TX1 10
53-
#endif
31+
#include <vector>
32+
#include <unity.h>
33+
#include "HardwareSerial.h"
34+
#include "esp_rom_gpio.h"
35+
#include "Wire.h"
5436

55-
// ESP32-P4 has no UART pin definition for RX2, TX2, RX3, TX3, RX4, TX4
56-
#ifndef RX2
57-
#define RX2 RX1
58-
#endif
59-
#ifndef TX2
60-
#define TX2 RX1
61-
#endif
37+
/* Utility defines */
6238

63-
/* Utility global variables */
39+
#define TEST_UART_NUM (uart_test_configs.size())
6440

65-
static String recv_msg = "";
66-
static int peeked_char = -1;
41+
/* Utility classes */
6742

68-
/* Utility functions */
43+
class UARTTestConfig {
44+
public:
45+
int uart_num;
46+
HardwareSerial &serial;
47+
int peeked_char;
48+
int8_t default_rx_pin;
49+
int8_t default_tx_pin;
50+
String recv_msg;
6951

70-
extern int8_t uart_get_RxPin(uint8_t uart_num);
71-
extern int8_t uart_get_TxPin(uint8_t uart_num);
52+
UARTTestConfig(int num, HardwareSerial &serial_ref, int8_t rx_pin, int8_t tx_pin)
53+
: uart_num(num), serial(serial_ref), peeked_char(-1), default_rx_pin(rx_pin), default_tx_pin(tx_pin), recv_msg("") {}
7254

73-
// This function starts all the available test UARTs
74-
void start_serial(unsigned long baudrate = 115200) {
75-
#if SOC_UART_HP_NUM >= 2
76-
Serial1.begin(baudrate);
77-
while (!Serial1) {
78-
delay(10);
55+
void begin(unsigned long baudrate) {
56+
serial.begin(baudrate, SERIAL_8N1, default_rx_pin, default_tx_pin);
57+
while (!serial) {
58+
delay(10);
59+
}
7960
}
80-
#endif
8161

82-
#if SOC_UART_HP_NUM >= 3
83-
Serial2.begin(baudrate);
84-
while (!Serial2) {
85-
delay(10);
62+
void end() {
63+
serial.end();
8664
}
87-
#endif
88-
}
89-
90-
// This function stops all the available test UARTs
91-
void stop_serial(bool hard_stop = false) {
92-
#if SOC_UART_HP_NUM >= 2
93-
Serial1.end(/*hard_stop*/);
94-
#endif
95-
96-
#if SOC_UART_HP_NUM >= 3
97-
Serial2.end(/*hard_stop*/);
98-
#endif
99-
}
10065

101-
// This function transmits a message and checks if it was received correctly
102-
void transmit_and_check_msg(const String msg_append, bool perform_assert = true) {
103-
delay(100); // Wait for some settings changes to take effect
104-
#if SOC_UART_HP_NUM == 2
105-
Serial1.print("Hello from Serial1 (UART1) >>> via loopback >>> Serial1 (UART1) " + msg_append);
106-
Serial1.flush();
107-
delay(100);
108-
if (perform_assert) {
109-
TEST_ASSERT_EQUAL_STRING(("Hello from Serial1 (UART1) >>> via loopback >>> Serial1 (UART1) " + msg_append).c_str(), recv_msg.c_str());
66+
void reset_buffers() {
67+
recv_msg = "";
68+
peeked_char = -1;
11069
}
111-
#elif SOC_UART_HP_NUM >= 3
112-
Serial1.print("Hello from Serial1 (UART1) >>> to >>> Serial2 (UART2) " + msg_append);
113-
Serial1.flush();
114-
delay(100);
115-
if (perform_assert) {
116-
TEST_ASSERT_EQUAL_STRING(("Hello from Serial1 (UART1) >>> to >>> Serial2 (UART2) " + msg_append).c_str(), recv_msg.c_str());
70+
71+
void transmit_and_check_msg(const String &msg_append, bool perform_assert = true) {
72+
reset_buffers();
73+
delay(100);
74+
serial.print("Hello from Serial" + String(uart_num) + " " + msg_append);
75+
serial.flush();
76+
delay(100);
77+
if (perform_assert) {
78+
TEST_ASSERT_EQUAL_STRING(("Hello from Serial" + String(uart_num) + " " + msg_append).c_str(), recv_msg.c_str());
79+
log_d("UART%d received message: %s\n", uart_num, recv_msg.c_str());
80+
}
11781
}
11882

119-
Serial2.print("Hello from Serial2 (UART2) >>> to >>> Serial1 (UART1) " + msg_append);
120-
Serial2.flush();
121-
delay(100);
122-
if (perform_assert) {
123-
TEST_ASSERT_EQUAL_STRING(("Hello from Serial2 (UART2) >>> to >>> Serial1 (UART1) " + msg_append).c_str(), recv_msg.c_str());
83+
void onReceive() {
84+
char c;
85+
size_t available = serial.available();
86+
if (peeked_char == -1) {
87+
peeked_char = serial.peek();
88+
}
89+
while (available--) {
90+
c = (char)serial.read();
91+
recv_msg += c;
92+
}
12493
}
125-
#else
126-
log_d("No UARTs available for transmission");
127-
TEST_FAIL();
128-
#endif
129-
}
94+
};
95+
96+
/* Utility global variables */
97+
98+
[[maybe_unused]]
99+
static const int NEW_RX1 = 9;
100+
[[maybe_unused]]
101+
static const int NEW_TX1 = 10;
102+
std::vector<UARTTestConfig *> uart_test_configs;
103+
104+
/* Utility functions */
105+
106+
extern "C" int8_t uart_get_RxPin(uint8_t uart_num);
107+
extern "C" int8_t uart_get_TxPin(uint8_t uart_num);
130108

131109
/* Tasks */
132110

133111
// This task is used to send a message after a delay to test the auto baudrate detection
134112
void task_delayed_msg(void *pvParameters) {
135-
HardwareSerial *selected_serial;
136-
137-
#if SOC_UART_HP_NUM == 2
138-
selected_serial = &Serial;
139-
#elif SOC_UART_HP_NUM >= 3
140-
selected_serial = &Serial1;
141-
#endif
142-
113+
HardwareSerial &selected_serial = uart_test_configs.size() == 1 ? Serial : Serial1;
143114
delay(2000);
144-
selected_serial->println("Hello from Serial1 to detect baudrate");
145-
selected_serial->flush();
115+
selected_serial.println("Hello to detect baudrate");
116+
selected_serial.flush();
146117
vTaskDelete(NULL);
147118
}
148119

149120
/* Unity functions */
150121

151122
// This function is automatically called by unity before each test is run
152123
void setUp(void) {
153-
start_serial(115200);
154-
#if SOC_UART_HP_NUM == 2
155-
log_d("Setup internal loop-back from and back to Serial1 (UART1) TX >> Serial1 (UART1) RX");
156-
157-
Serial1.onReceive([]() {
158-
onReceive_cb(Serial1);
159-
});
160-
uart_internal_loopback(1, RX1);
161-
#elif SOC_UART_HP_NUM >= 3
162-
log_d("Setup internal loop-back between Serial1 (UART1) <<--->> Serial2 (UART2)");
163-
164-
Serial1.onReceive([]() {
165-
onReceive_cb(Serial1);
166-
});
167-
Serial2.onReceive([]() {
168-
onReceive_cb(Serial2);
169-
});
170-
uart_internal_loopback(1, RX2);
171-
uart_internal_loopback(2, RX1);
172-
#endif
124+
for (auto *ref : uart_test_configs) {
125+
UARTTestConfig &config = *ref;
126+
//log_d("Setup internal loop-back from and back to UART%d TX >> UART%d RX", config.uart_num, config.uart_num);
127+
config.begin(115200);
128+
config.serial.onReceive([&config]() {
129+
config.onReceive();
130+
});
131+
uart_internal_loopback(config.uart_num, uart_get_RxPin(config.uart_num));
132+
}
173133
}
174134

175135
// This function is automatically called by unity after each test is run
176136
void tearDown(void) {
177-
stop_serial();
178-
}
179-
180-
/* Callback functions */
181-
182-
// This is a callback function that will be activated on UART RX events
183-
void onReceive_cb(HardwareSerial &selected_serial) {
184-
int uart_num = -1;
185-
char c;
186-
187-
(void)uart_num; // Avoid compiler warning when debug level is set to none
188-
189-
if (&selected_serial == &Serial) {
190-
uart_num = 0;
191-
#if SOC_UART_HP_NUM >= 2
192-
} else if (&selected_serial == &Serial1) {
193-
uart_num = 1;
194-
#endif
195-
#if SOC_UART_HP_NUM >= 3
196-
} else if (&selected_serial == &Serial2) {
197-
uart_num = 2;
198-
#endif
199-
}
200-
201-
recv_msg = "";
202-
size_t available = selected_serial.available();
203-
204-
if (available != 0) {
205-
peeked_char = selected_serial.peek();
137+
for (auto *ref : uart_test_configs) {
138+
UARTTestConfig &config = *ref;
139+
config.end();
206140
}
207-
208-
while (available--) {
209-
c = (char)selected_serial.read();
210-
recv_msg += c;
211-
}
212-
213-
log_d("UART %d received message: %s\n", uart_num, recv_msg.c_str());
214141
}
215142

216143
/* Test functions */
@@ -219,40 +146,33 @@ void onReceive_cb(HardwareSerial &selected_serial) {
219146
void basic_transmission_test(void) {
220147
log_d("Performing basic transmission test");
221148

222-
transmit_and_check_msg("");
149+
for (auto *ref : uart_test_configs) {
150+
UARTTestConfig &config = *ref;
151+
config.transmit_and_check_msg("");
152+
}
223153

224154
Serial.println("Basic transmission test successful");
225155
}
226156

227157
// This test checks if the baudrate can be changed and if the message can be transmitted and received correctly after the change
228158
void change_baudrate_test(void) {
229-
//Test first using the updateBaudRate method and then using the begin method
230-
log_d("Changing baudrate to 9600");
231-
232-
//Baudrate error should be within 2% of the target baudrate
233-
Serial1.updateBaudRate(9600);
234-
TEST_ASSERT_UINT_WITHIN(192, 9600, Serial1.baudRate());
159+
for (auto *ref : uart_test_configs) {
160+
UARTTestConfig &config = *ref;
161+
log_d("Changing baudrate of UART%d to 9600", config.uart_num);
235162

236-
#if SOC_UART_HP_NUM >= 3
237-
Serial2.updateBaudRate(9600);
238-
TEST_ASSERT_UINT_WITHIN(192, 9600, Serial2.baudRate());
239-
#endif
240-
241-
log_d("Sending string using 9600 baudrate");
242-
transmit_and_check_msg("using 9600 baudrate");
163+
//Baudrate error should be within 2% of the target baudrate
164+
config.serial.updateBaudRate(9600);
165+
TEST_ASSERT_UINT_WITHIN(192, 9600, config.serial.baudRate());
243166

244-
log_d("Changing baudrate back to 115200");
245-
start_serial(115200);
167+
log_d("Sending string on UART%d using 9600 baudrate", config.uart_num);
168+
config.transmit_and_check_msg("using 9600 baudrate");
246169

247-
//Baudrate error should be within 2% of the target baudrate
248-
TEST_ASSERT_UINT_WITHIN(2304, 115200, Serial1.baudRate());
170+
config.serial.begin(115200);
171+
TEST_ASSERT_UINT_WITHIN(2304, 115200, config.serial.baudRate());
249172

250-
#if SOC_UART_HP_NUM >= 3
251-
TEST_ASSERT_UINT_WITHIN(2304, 115200, Serial2.baudRate());
252-
#endif
253-
254-
log_d("Sending string using 115200 baudrate");
255-
transmit_and_check_msg("using 115200 baudrate");
173+
log_d("Sending string on UART%d using 115200 baudrate", config.uart_num);
174+
config.transmit_and_check_msg("using 115200 baudrate");
175+
}
256176

257177
Serial.println("Change baudrate test successful");
258178
}
@@ -269,7 +189,7 @@ void resize_buffers_test(void) {
269189
ret = Serial1.setTxBufferSize(256);
270190
TEST_ASSERT_EQUAL(0, ret);
271191

272-
stop_serial();
192+
Serial1.end();
273193

274194
log_d("Trying to resize RX buffer while stopped.");
275195
ret = Serial1.setRxBufferSize(256);
@@ -285,17 +205,25 @@ void resize_buffers_test(void) {
285205
// This test checks if the begin function can be called when the UART is already running
286206
void begin_when_running_test(void) {
287207
log_d("Trying to set up serial twice");
288-
start_serial(115200);
208+
for (auto *ref : uart_test_configs) {
209+
UARTTestConfig &config = *ref;
210+
// Calling twice should not crash
211+
config.begin(115200);
212+
config.begin(115200);
213+
}
289214
Serial.println("Begin when running test successful");
290215
}
291216

292217
// This test checks if the end function can be called when the UART is already stopped
293218
void end_when_stopped_test(void) {
294219
log_d("Trying to end serial twice");
295220

296-
// Calling end(true) twice should not crash
297-
stop_serial(true);
298-
stop_serial(true);
221+
for (auto *ref : uart_test_configs) {
222+
UARTTestConfig &config = *ref;
223+
// Calling twice should not crash
224+
config.end();
225+
config.end();
226+
}
299227

300228
Serial.println("End when stopped test successful");
301229
}
@@ -319,7 +247,7 @@ void enabled_uart_calls_test(void) {
319247
TEST_ASSERT_EQUAL(true, boolean_ret);
320248

321249
log_d("Checking if Serial 1 is peekable while running");
322-
TEST_ASSERT_GREATER_OR_EQUAL(0, peeked_char);
250+
TEST_ASSERT_GREATER_OR_EQUAL(0, uart_test_configs[0]->peeked_char);
323251

324252
log_d("Checking if Serial 1 can read bytes while running");
325253
integer_ret = Serial1.readBytes(test_buf, 1);
@@ -355,7 +283,10 @@ void disabled_uart_calls_test(void) {
355283
int integer_ret;
356284
uint8_t test_buf[1];
357285

358-
stop_serial();
286+
for (auto *ref : uart_test_configs) {
287+
UARTTestConfig &config = *ref;
288+
config.end();
289+
}
359290

360291
log_d("Checking if Serial 1 can set the RX timeout when stopped");
361292
boolean_ret = Serial1.setRxTimeout(1);
@@ -423,44 +354,35 @@ void disabled_uart_calls_test(void) {
423354

424355
// This test checks if the pins can be changed and if the message can be transmitted and received correctly after the change
425356
void change_pins_test(void) {
426-
//stop_serial();
427-
428357
log_d("Disabling UART loopback");
429358

430-
#if SOC_UART_HP_NUM == 2
431-
esp_rom_gpio_connect_out_signal(SOC_RX0, SIG_GPIO_OUT_IDX, false, false);
432-
#elif SOC_UART_HP_NUM >= 3
433-
esp_rom_gpio_connect_out_signal(RX1, SIG_GPIO_OUT_IDX, false, false);
434-
esp_rom_gpio_connect_out_signal(RX2, SIG_GPIO_OUT_IDX, false, false);
435-
#endif
436-
437-
log_d("Swapping UART pins");
438-
439-
#if SOC_UART_HP_NUM == 2
440-
Serial1.setPins(NEW_RX1, NEW_TX1);
441-
TEST_ASSERT_EQUAL(NEW_RX1, uart_get_RxPin(1));
442-
TEST_ASSERT_EQUAL(NEW_TX1, uart_get_TxPin(1));
443-
#elif SOC_UART_HP_NUM >= 3
444-
Serial1.setPins(RX2, TX2);
445-
Serial2.setPins(RX1, TX1);
446-
TEST_ASSERT_EQUAL(RX2, uart_get_RxPin(1));
447-
TEST_ASSERT_EQUAL(TX2, uart_get_TxPin(1));
448-
TEST_ASSERT_EQUAL(RX1, uart_get_RxPin(2));
449-
TEST_ASSERT_EQUAL(TX1, uart_get_TxPin(2));
450-
#endif
451-
452-
start_serial(115200);
453-
454-
log_d("Re-enabling UART loopback");
455-
456-
#if SOC_UART_HP_NUM == 2
457-
uart_internal_loopback(1, NEW_RX1);
458-
#elif SOC_UART_HP_NUM >= 3
459-
uart_internal_loopback(1, RX1);
460-
uart_internal_loopback(2, RX2);
461-
#endif
359+
for (auto *ref : uart_test_configs) {
360+
UARTTestConfig &config = *ref;
361+
esp_rom_gpio_connect_out_signal(config.default_rx_pin, SIG_GPIO_OUT_IDX, false, false);
362+
}
462363

463-
transmit_and_check_msg("using new pins");
364+
log_d("Swapping UART pins and testing transmission");
365+
366+
if (TEST_UART_NUM == 1) {
367+
UARTTestConfig &config = *uart_test_configs[0];
368+
config.serial.setPins(NEW_RX1, NEW_TX1);
369+
TEST_ASSERT_EQUAL(NEW_RX1, uart_get_RxPin(config.uart_num));
370+
TEST_ASSERT_EQUAL(NEW_TX1, uart_get_TxPin(config.uart_num));
371+
372+
uart_internal_loopback(config.uart_num, NEW_RX1);
373+
config.transmit_and_check_msg("using new pins");
374+
} else {
375+
for (int i = 0; i < TEST_UART_NUM; i++) {
376+
UARTTestConfig &config = *uart_test_configs[i];
377+
UARTTestConfig &next_uart = *uart_test_configs[(i + 1) % TEST_UART_NUM];
378+
config.serial.setPins(next_uart.default_rx_pin, next_uart.default_tx_pin);
379+
TEST_ASSERT_EQUAL(uart_get_RxPin(config.uart_num), next_uart.default_rx_pin);
380+
TEST_ASSERT_EQUAL(uart_get_TxPin(config.uart_num), next_uart.default_tx_pin);
381+
382+
uart_internal_loopback(config.uart_num, next_uart.default_rx_pin);
383+
config.transmit_and_check_msg("using new pins");
384+
}
385+
}
464386

465387
Serial.println("Change pins test successful");
466388
}
@@ -475,12 +397,15 @@ void auto_baudrate_test(void) {
475397

476398
log_d("Stopping test serial. Using Serial2 for ESP32 and Serial1 for ESP32-S2.");
477399

478-
#if SOC_UART_HP_NUM == 2
479-
selected_serial = &Serial1;
480-
uart_internal_loopback(0, RX1);
481-
#elif SOC_UART_HP_NUM >= 3
482-
selected_serial = &Serial2;
400+
if (TEST_UART_NUM == 1) {
401+
selected_serial = &Serial1;
402+
uart_internal_loopback(0, RX1);
403+
} else {
404+
#ifdef RX2
405+
selected_serial = &Serial2;
406+
uart_internal_loopback(1, RX2);
483407
#endif
408+
}
484409

485410
//selected_serial->end(false);
486411

@@ -493,10 +418,10 @@ void auto_baudrate_test(void) {
493418
selected_serial->begin(0);
494419
baudrate = selected_serial->baudRate();
495420

496-
#if SOC_UART_HP_NUM == 2
497-
Serial.end();
498-
Serial.begin(115200);
499-
#endif
421+
if (TEST_UART_NUM == 1) {
422+
Serial.end();
423+
Serial.begin(115200);
424+
}
500425

501426
TEST_ASSERT_UINT_WITHIN(2304, 115200, baudrate);
502427

@@ -510,32 +435,23 @@ void periman_test(void) {
510435

511436
log_d("Setting up I2C on the same pins as UART");
512437

513-
Wire.begin(RX1, TX1);
514-
515-
#if SOC_UART_HP_NUM >= 3
516-
Wire1.begin(RX2, TX2);
517-
#endif
518-
519-
recv_msg = "";
520-
521-
log_d("Trying to send message using UART with I2C enabled");
522-
transmit_and_check_msg("while used by I2C", false);
523-
TEST_ASSERT_EQUAL_STRING("", recv_msg.c_str());
438+
for (auto *ref : uart_test_configs) {
439+
UARTTestConfig &config = *ref;
440+
Wire.begin(config.default_rx_pin, config.default_tx_pin);
441+
config.recv_msg = "";
524442

525-
log_d("Disabling I2C and re-enabling UART");
443+
log_d("Trying to send message using UART%d with I2C enabled", config.uart_num);
444+
config.transmit_and_check_msg("while used by I2C", false);
445+
TEST_ASSERT_EQUAL_STRING("", config.recv_msg.c_str());
526446

527-
Serial1.setPins(RX1, TX1);
447+
log_d("Disabling I2C and re-enabling UART%d", config.uart_num);
528448

529-
#if SOC_UART_HP_NUM >= 3
530-
Serial2.setPins(RX2, TX2);
531-
uart_internal_loopback(1, RX2);
532-
uart_internal_loopback(2, RX1);
533-
#elif SOC_UART_HP_NUM == 2
534-
uart_internal_loopback(1, RX1);
535-
#endif
449+
config.serial.setPins(config.default_rx_pin, config.default_tx_pin);
450+
uart_internal_loopback(config.uart_num, config.default_rx_pin);
536451

537-
log_d("Trying to send message using UART with I2C disabled");
538-
transmit_and_check_msg("while I2C is disabled");
452+
log_d("Trying to send message using UART%d with I2C disabled", config.uart_num);
453+
config.transmit_and_check_msg("while I2C is disabled");
454+
}
539455

540456
Serial.println("Peripheral manager test successful");
541457
}
@@ -551,17 +467,23 @@ void change_cpu_frequency_test(void) {
551467

552468
Serial.updateBaudRate(115200);
553469

554-
log_d("Trying to send message with the new CPU frequency");
555-
transmit_and_check_msg("with new CPU frequency");
470+
for (auto *ref : uart_test_configs) {
471+
UARTTestConfig &config = *ref;
472+
log_d("Trying to send message with the new CPU frequency on UART%d", config.uart_num);
473+
config.transmit_and_check_msg("with new CPU frequency");
474+
}
556475

557476
log_d("Changing CPU frequency back to %dMHz", old_freq);
558477
Serial.flush();
559478
setCpuFrequencyMhz(old_freq);
560479

561480
Serial.updateBaudRate(115200);
562481

563-
log_d("Trying to send message with the original CPU frequency");
564-
transmit_and_check_msg("with the original CPU frequency");
482+
for (auto *ref : uart_test_configs) {
483+
UARTTestConfig &config = *ref;
484+
log_d("Trying to send message with the original CPU frequency on UART%d", config.uart_num);
485+
config.transmit_and_check_msg("with the original CPU frequency");
486+
}
565487

566488
Serial.println("Change CPU frequency test successful");
567489
}
@@ -573,30 +495,39 @@ void setup() {
573495
while (!Serial) {
574496
delay(10);
575497
}
576-
log_d("SOC_UART_HP_NUM = %d", SOC_UART_HP_NUM);
577-
578-
// Begin needs to be called before setting up the loopback because it creates the serial object
579-
start_serial(115200);
580-
581-
#if SOC_UART_HP_NUM == 2
582-
log_d("Setup internal loop-back from and back to Serial1 (UART1) TX >> Serial1 (UART1) RX");
583-
584-
Serial1.onReceive([]() {
585-
onReceive_cb(Serial1);
586-
});
587-
uart_internal_loopback(1, RX1);
588-
#elif SOC_UART_HP_NUM >= 3
589-
log_d("Setup internal loop-back between Serial1 (UART1) <<--->> Serial2 (UART2)");
590-
591-
Serial1.onReceive([]() {
592-
onReceive_cb(Serial1);
593-
});
594-
Serial2.onReceive([]() {
595-
onReceive_cb(Serial2);
596-
});
597-
uart_internal_loopback(1, RX2);
598-
uart_internal_loopback(2, RX1);
498+
499+
uart_test_configs = {
500+
#if SOC_UART_HP_NUM >= 2 && defined(RX1) && defined(TX1)
501+
// inverting RX1<->TX1 because ESP32-P4 has a problem with loopback on RX1 :: GPIO11 <-- UART_TX SGINAL
502+
new UARTTestConfig(1, Serial1, TX1, RX1),
503+
#endif
504+
#if SOC_UART_HP_NUM >= 3 && defined(RX2) && defined(TX2)
505+
new UARTTestConfig(2, Serial2, RX2, TX2),
506+
#endif
507+
#if SOC_UART_HP_NUM >= 4 && defined(RX3) && defined(TX3)
508+
new UARTTestConfig(3, Serial3, RX3, TX3),
599509
#endif
510+
#if SOC_UART_HP_NUM >= 5 && defined(RX4) && defined(TX4)
511+
new UARTTestConfig(4, Serial4, RX4, TX4)
512+
#endif
513+
};
514+
515+
if (TEST_UART_NUM == 0) {
516+
log_e("This test requires at least one UART besides UART0 configured");
517+
abort();
518+
}
519+
520+
log_d("TEST_UART_NUM = %d", TEST_UART_NUM);
521+
522+
for (auto *ref : uart_test_configs) {
523+
UARTTestConfig &config = *ref;
524+
config.begin(115200);
525+
log_d("Setup internal loop-back from and back to UART%d TX >> UART%d RX", config.uart_num, config.uart_num);
526+
config.serial.onReceive([&config]() {
527+
config.onReceive();
528+
});
529+
uart_internal_loopback(config.uart_num, uart_get_RxPin(config.uart_num));
530+
}
600531

601532
log_d("Setup done. Starting tests");
602533

0 commit comments

Comments
 (0)
Please sign in to comment.