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 c83948e

Browse files
committedNov 13, 2024
refactor(uart): Refactor UART test to work with any number of UARTs
1 parent 0dfb386 commit c83948e

File tree

1 file changed

+211
-281
lines changed

1 file changed

+211
-281
lines changed
 

‎tests/validation/uart/uart.ino

Lines changed: 211 additions & 281 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,112 @@
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 &ser, int8_t rx_pin, int8_t tx_pin)
53+
: uart_num(num), serial(ser), 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);
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-
}
8965

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-
}
100-
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());
110-
}
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());
66+
void transmit_and_check_msg(const String &msg_append, bool perform_assert = true) {
67+
delay(100);
68+
serial.print("Hello from Serial" + String(uart_num) + " " + msg_append);
69+
serial.flush();
70+
delay(100);
71+
if (perform_assert) {
72+
TEST_ASSERT_EQUAL_STRING(("Hello from Serial" + String(uart_num) + " " + msg_append).c_str(), recv_msg.c_str());
73+
}
11774
}
11875

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());
76+
void onReceive() {
77+
char c;
78+
recv_msg = "";
79+
size_t available = serial.available();
80+
if (available != 0) {
81+
peeked_char = serial.peek();
82+
}
83+
while (available--) {
84+
c = (char)serial.read();
85+
recv_msg += c;
86+
}
87+
log_d("UART%d received message: %s\n", uart_num, recv_msg.c_str());
12488
}
125-
#else
126-
log_d("No UARTs available for transmission");
127-
TEST_FAIL();
128-
#endif
129-
}
89+
};
90+
91+
/* Utility global variables */
92+
93+
[[maybe_unused]] static const int NEW_RX1 = 9;
94+
[[maybe_unused]] static const int NEW_TX1 = 10;
95+
std::vector<UARTTestConfig*> uart_test_configs;
96+
97+
/* Utility functions */
98+
99+
extern "C" int8_t uart_get_RxPin(uint8_t uart_num);
100+
extern "C" int8_t uart_get_TxPin(uint8_t uart_num);
130101

131102
/* Tasks */
132103

133104
// This task is used to send a message after a delay to test the auto baudrate detection
134105
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-
106+
HardwareSerial &selected_serial = uart_test_configs.size() == 1 ? Serial : Serial1;
143107
delay(2000);
144-
selected_serial->println("Hello from Serial1 to detect baudrate");
145-
selected_serial->flush();
108+
selected_serial.println("Hello to detect baudrate");
109+
selected_serial.flush();
146110
vTaskDelete(NULL);
147111
}
148112

149113
/* Unity functions */
150114

151115
// This function is automatically called by unity before each test is run
152116
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
117+
for (auto* ref : uart_test_configs) {
118+
UARTTestConfig& config = *ref;
119+
//log_d("Setup internal loop-back from and back to UART%d TX >> UART%d RX", config.uart_num, config.uart_num);
120+
config.begin(115200);
121+
config.serial.onReceive([&config]() {
122+
config.onReceive();
123+
});
124+
uart_internal_loopback(config.uart_num, uart_get_RxPin(config.uart_num));
125+
}
173126
}
174127

175128
// This function is automatically called by unity after each test is run
176129
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();
206-
}
207-
208-
while (available--) {
209-
c = (char)selected_serial.read();
210-
recv_msg += c;
130+
for (auto* ref : uart_test_configs) {
131+
UARTTestConfig& config = *ref;
132+
config.end();
211133
}
212-
213-
log_d("UART %d received message: %s\n", uart_num, recv_msg.c_str());
214134
}
215135

216136
/* Test functions */
@@ -219,40 +139,33 @@ void onReceive_cb(HardwareSerial &selected_serial) {
219139
void basic_transmission_test(void) {
220140
log_d("Performing basic transmission test");
221141

222-
transmit_and_check_msg("");
142+
for (auto* ref : uart_test_configs) {
143+
UARTTestConfig& config = *ref;
144+
config.transmit_and_check_msg("");
145+
}
223146

224147
Serial.println("Basic transmission test successful");
225148
}
226149

227150
// This test checks if the baudrate can be changed and if the message can be transmitted and received correctly after the change
228151
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());
235-
236-
#if SOC_UART_HP_NUM >= 3
237-
Serial2.updateBaudRate(9600);
238-
TEST_ASSERT_UINT_WITHIN(192, 9600, Serial2.baudRate());
239-
#endif
152+
for (auto* ref : uart_test_configs) {
153+
UARTTestConfig& config = *ref;
154+
log_d("Changing baudrate of UART%d to 9600", config.uart_num);
240155

241-
log_d("Sending string using 9600 baudrate");
242-
transmit_and_check_msg("using 9600 baudrate");
156+
//Baudrate error should be within 2% of the target baudrate
157+
config.serial.updateBaudRate(9600);
158+
TEST_ASSERT_UINT_WITHIN(192, 9600, config.serial.baudRate());
243159

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

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

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");
166+
log_d("Sending string on UART%d using 115200 baudrate", config.uart_num);
167+
config.transmit_and_check_msg("using 115200 baudrate");
168+
}
256169

257170
Serial.println("Change baudrate test successful");
258171
}
@@ -269,7 +182,7 @@ void resize_buffers_test(void) {
269182
ret = Serial1.setTxBufferSize(256);
270183
TEST_ASSERT_EQUAL(0, ret);
271184

272-
stop_serial();
185+
Serial1.end();
273186

274187
log_d("Trying to resize RX buffer while stopped.");
275188
ret = Serial1.setRxBufferSize(256);
@@ -285,17 +198,25 @@ void resize_buffers_test(void) {
285198
// This test checks if the begin function can be called when the UART is already running
286199
void begin_when_running_test(void) {
287200
log_d("Trying to set up serial twice");
288-
start_serial(115200);
201+
for (auto* ref : uart_test_configs) {
202+
UARTTestConfig& config = *ref;
203+
// Calling twice should not crash
204+
config.begin(115200);
205+
config.begin(115200);
206+
}
289207
Serial.println("Begin when running test successful");
290208
}
291209

292210
// This test checks if the end function can be called when the UART is already stopped
293211
void end_when_stopped_test(void) {
294212
log_d("Trying to end serial twice");
295213

296-
// Calling end(true) twice should not crash
297-
stop_serial(true);
298-
stop_serial(true);
214+
for (auto* ref : uart_test_configs) {
215+
UARTTestConfig& config = *ref;
216+
// Calling twice should not crash
217+
config.end();
218+
config.end();
219+
}
299220

300221
Serial.println("End when stopped test successful");
301222
}
@@ -319,7 +240,7 @@ void enabled_uart_calls_test(void) {
319240
TEST_ASSERT_EQUAL(true, boolean_ret);
320241

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

324245
log_d("Checking if Serial 1 can read bytes while running");
325246
integer_ret = Serial1.readBytes(test_buf, 1);
@@ -355,7 +276,10 @@ void disabled_uart_calls_test(void) {
355276
int integer_ret;
356277
uint8_t test_buf[1];
357278

358-
stop_serial();
279+
for (auto* ref : uart_test_configs) {
280+
UARTTestConfig& config = *ref;
281+
config.end();
282+
}
359283

360284
log_d("Checking if Serial 1 can set the RX timeout when stopped");
361285
boolean_ret = Serial1.setRxTimeout(1);
@@ -423,44 +347,42 @@ void disabled_uart_calls_test(void) {
423347

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

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
352+
for (auto* ref : uart_test_configs) {
353+
UARTTestConfig& config = *ref;
354+
esp_rom_gpio_connect_out_signal(config.default_rx_pin, SIG_GPIO_OUT_IDX, false, false);
355+
}
436356

437357
log_d("Swapping UART pins");
438358

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);
359+
if (TEST_UART_NUM == 1) {
360+
Serial1.setPins(NEW_RX1, NEW_TX1);
361+
TEST_ASSERT_EQUAL(NEW_RX1, uart_get_RxPin(1));
362+
TEST_ASSERT_EQUAL(NEW_TX1, uart_get_TxPin(1));
363+
} else {
364+
for (int i = 0; i < TEST_UART_NUM; i++) {
365+
UARTTestConfig& config = *uart_test_configs[i];
366+
UARTTestConfig& next_uart = *uart_test_configs[(i + 1) % TEST_UART_NUM];
367+
config.serial.setPins(next_uart.default_rx_pin, next_uart.default_tx_pin);
368+
TEST_ASSERT_EQUAL(uart_get_RxPin(config.uart_num), next_uart.default_rx_pin);
369+
TEST_ASSERT_EQUAL(uart_get_TxPin(config.uart_num), next_uart.default_tx_pin);
370+
config.begin(115200);
371+
}
372+
}
453373

454374
log_d("Re-enabling UART loopback");
455375

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
462-
463-
transmit_and_check_msg("using new pins");
376+
if (TEST_UART_NUM == 1) {
377+
uart_internal_loopback(1, NEW_RX1);
378+
} else {
379+
for (int i = 0; i < TEST_UART_NUM; i++) {
380+
UARTTestConfig& config = *uart_test_configs[i];
381+
UARTTestConfig& next_uart = *uart_test_configs[(i + 1) % TEST_UART_NUM];
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,38 @@ 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+
new UARTTestConfig(1, Serial1, RX1, TX1),
502+
#endif
503+
#if SOC_UART_HP_NUM >= 3 && defined(RX2) && defined(TX2)
504+
new UARTTestConfig(2, Serial2, RX2, TX2),
505+
#endif
506+
#if SOC_UART_HP_NUM >= 4 && defined(RX3) && defined(TX3)
507+
new UARTTestConfig(3, Serial3, RX3, TX3),
599508
#endif
509+
#if SOC_UART_HP_NUM >= 5 && defined(RX4) && defined(TX4)
510+
new UARTTestConfig(4, Serial4, RX4, TX4)
511+
#endif
512+
};
513+
514+
if (TEST_UART_NUM == 0) {
515+
log_e("This test requires at least one UART besides UART0 configured");
516+
abort();
517+
}
518+
519+
log_d("TEST_UART_NUM = %d", TEST_UART_NUM);
520+
521+
for (auto* ref : uart_test_configs) {
522+
UARTTestConfig& config = *ref;
523+
config.begin(115200);
524+
log_d("Setup internal loop-back from and back to UART%d TX >> UART%d RX", config.uart_num, config.uart_num);
525+
config.serial.onReceive([&config]() {
526+
config.onReceive();
527+
});
528+
uart_internal_loopback(config.uart_num, uart_get_RxPin(config.uart_num));
529+
}
600530

601531
log_d("Setup done. Starting tests");
602532

0 commit comments

Comments
 (0)
Please sign in to comment.