2
2
*
3
3
* This test is using UART0 (Serial) only for reporting test status and helping with the auto
4
4
* baudrate detection test.
5
- * UART1 (Serial1) and UART2 (Serial2), where available, are used for testing.
5
+ * The other serials are used for testing.
6
6
*/
7
7
8
- #include < unity.h>
9
- #include " HardwareSerial.h"
10
- #include " esp_rom_gpio.h"
11
- #include " Wire.h"
12
-
13
8
// 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 | -- | -- | -- | -- |
21
16
22
17
/*
23
- * For 2 UARTS :
18
+ * For each UART :
24
19
*
25
20
* terminal
26
21
* | ^
30
25
* report status
31
26
* |
32
27
* 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
47
29
*/
48
30
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 "
54
36
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 */
62
38
63
- /* Utility global variables */
39
+ # define TEST_UART_NUM (uart_test_configs.size())
64
40
65
- static String recv_msg = " " ;
66
- static int peeked_char = -1 ;
41
+ /* Utility classes */
67
42
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;
69
51
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( " " ) {}
72
54
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
+ }
79
60
}
80
- #endif
81
61
82
- #if SOC_UART_HP_NUM >= 3
83
- Serial2.begin (baudrate);
84
- while (!Serial2) {
85
- delay (10 );
62
+ void end () {
63
+ serial.end ();
86
64
}
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
- }
100
65
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 ;
110
69
}
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
+ }
117
81
}
118
82
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
+ }
124
93
}
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]] static const int NEW_RX1 = 9 ;
99
+ [[maybe_unused]] static const int NEW_TX1 = 10 ;
100
+ std::vector<UARTTestConfig*> uart_test_configs;
101
+
102
+ /* Utility functions */
103
+
104
+ extern " C" int8_t uart_get_RxPin (uint8_t uart_num);
105
+ extern " C" int8_t uart_get_TxPin (uint8_t uart_num);
130
106
131
107
/* Tasks */
132
108
133
109
// This task is used to send a message after a delay to test the auto baudrate detection
134
110
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
-
111
+ HardwareSerial &selected_serial = uart_test_configs.size () == 1 ? Serial : Serial1;
143
112
delay (2000 );
144
- selected_serial-> println (" Hello from Serial1 to detect baudrate" );
145
- selected_serial-> flush ();
113
+ selected_serial. println (" Hello to detect baudrate" );
114
+ selected_serial. flush ();
146
115
vTaskDelete (NULL );
147
116
}
148
117
149
118
/* Unity functions */
150
119
151
120
// This function is automatically called by unity before each test is run
152
121
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
122
+ for (auto * ref : uart_test_configs) {
123
+ UARTTestConfig& config = *ref;
124
+ // log_d("Setup internal loop-back from and back to UART%d TX >> UART%d RX", config.uart_num, config.uart_num);
125
+ config.begin (115200 );
126
+ config.serial .onReceive ([&config]() {
127
+ config.onReceive ();
128
+ });
129
+ uart_internal_loopback (config.uart_num , uart_get_RxPin (config.uart_num ));
130
+ }
173
131
}
174
132
175
133
// This function is automatically called by unity after each test is run
176
134
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 ();
135
+ for (auto * ref : uart_test_configs) {
136
+ UARTTestConfig& config = *ref;
137
+ config.end ();
206
138
}
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 ());
214
139
}
215
140
216
141
/* Test functions */
@@ -219,40 +144,33 @@ void onReceive_cb(HardwareSerial &selected_serial) {
219
144
void basic_transmission_test (void ) {
220
145
log_d (" Performing basic transmission test" );
221
146
222
- transmit_and_check_msg (" " );
147
+ for (auto * ref : uart_test_configs) {
148
+ UARTTestConfig& config = *ref;
149
+ config.transmit_and_check_msg (" " );
150
+ }
223
151
224
152
Serial.println (" Basic transmission test successful" );
225
153
}
226
154
227
155
// This test checks if the baudrate can be changed and if the message can be transmitted and received correctly after the change
228
156
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 ());
157
+ for (auto * ref : uart_test_configs) {
158
+ UARTTestConfig& config = *ref;
159
+ log_d (" Changing baudrate of UART%d to 9600" , config.uart_num );
235
160
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" );
161
+ // Baudrate error should be within 2% of the target baudrate
162
+ config.serial .updateBaudRate (9600 );
163
+ TEST_ASSERT_UINT_WITHIN (192 , 9600 , config.serial .baudRate ());
243
164
244
- log_d (" Changing baudrate back to 115200 " );
245
- start_serial ( 115200 );
165
+ log_d (" Sending string on UART%d using 9600 baudrate " , config. uart_num );
166
+ config. transmit_and_check_msg ( " using 9600 baudrate " );
246
167
247
- // Baudrate error should be within 2% of the target baudrate
248
- TEST_ASSERT_UINT_WITHIN (2304 , 115200 , Serial1 .baudRate ());
168
+ config. serial . begin ( 115200 );
169
+ TEST_ASSERT_UINT_WITHIN (2304 , 115200 , config. serial .baudRate ());
249
170
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" );
171
+ log_d (" Sending string on UART%d using 115200 baudrate" , config.uart_num );
172
+ config.transmit_and_check_msg (" using 115200 baudrate" );
173
+ }
256
174
257
175
Serial.println (" Change baudrate test successful" );
258
176
}
@@ -269,7 +187,7 @@ void resize_buffers_test(void) {
269
187
ret = Serial1.setTxBufferSize (256 );
270
188
TEST_ASSERT_EQUAL (0 , ret);
271
189
272
- stop_serial ();
190
+ Serial1. end ();
273
191
274
192
log_d (" Trying to resize RX buffer while stopped." );
275
193
ret = Serial1.setRxBufferSize (256 );
@@ -285,17 +203,25 @@ void resize_buffers_test(void) {
285
203
// This test checks if the begin function can be called when the UART is already running
286
204
void begin_when_running_test (void ) {
287
205
log_d (" Trying to set up serial twice" );
288
- start_serial (115200 );
206
+ for (auto * ref : uart_test_configs) {
207
+ UARTTestConfig& config = *ref;
208
+ // Calling twice should not crash
209
+ config.begin (115200 );
210
+ config.begin (115200 );
211
+ }
289
212
Serial.println (" Begin when running test successful" );
290
213
}
291
214
292
215
// This test checks if the end function can be called when the UART is already stopped
293
216
void end_when_stopped_test (void ) {
294
217
log_d (" Trying to end serial twice" );
295
218
296
- // Calling end(true) twice should not crash
297
- stop_serial (true );
298
- stop_serial (true );
219
+ for (auto * ref : uart_test_configs) {
220
+ UARTTestConfig& config = *ref;
221
+ // Calling twice should not crash
222
+ config.end ();
223
+ config.end ();
224
+ }
299
225
300
226
Serial.println (" End when stopped test successful" );
301
227
}
@@ -319,7 +245,7 @@ void enabled_uart_calls_test(void) {
319
245
TEST_ASSERT_EQUAL (true , boolean_ret);
320
246
321
247
log_d (" Checking if Serial 1 is peekable while running" );
322
- TEST_ASSERT_GREATER_OR_EQUAL (0 , peeked_char);
248
+ TEST_ASSERT_GREATER_OR_EQUAL (0 , uart_test_configs[ 0 ]-> peeked_char );
323
249
324
250
log_d (" Checking if Serial 1 can read bytes while running" );
325
251
integer_ret = Serial1.readBytes (test_buf, 1 );
@@ -355,7 +281,10 @@ void disabled_uart_calls_test(void) {
355
281
int integer_ret;
356
282
uint8_t test_buf[1 ];
357
283
358
- stop_serial ();
284
+ for (auto * ref : uart_test_configs) {
285
+ UARTTestConfig& config = *ref;
286
+ config.end ();
287
+ }
359
288
360
289
log_d (" Checking if Serial 1 can set the RX timeout when stopped" );
361
290
boolean_ret = Serial1.setRxTimeout (1 );
@@ -423,44 +352,35 @@ void disabled_uart_calls_test(void) {
423
352
424
353
// This test checks if the pins can be changed and if the message can be transmitted and received correctly after the change
425
354
void change_pins_test (void ) {
426
- // stop_serial();
427
-
428
355
log_d (" Disabling UART loopback" );
429
356
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
357
+ for (auto * ref : uart_test_configs) {
358
+ UARTTestConfig& config = *ref;
359
+ esp_rom_gpio_connect_out_signal (config.default_rx_pin , SIG_GPIO_OUT_IDX, false , false );
360
+ }
462
361
463
- transmit_and_check_msg (" using new pins" );
362
+ log_d (" Swapping UART pins and testing transmission" );
363
+
364
+ if (TEST_UART_NUM == 1 ) {
365
+ UARTTestConfig& config = *uart_test_configs[0 ];
366
+ config.serial .setPins (NEW_RX1, NEW_TX1);
367
+ TEST_ASSERT_EQUAL (NEW_RX1, uart_get_RxPin (config.uart_num ));
368
+ TEST_ASSERT_EQUAL (NEW_TX1, uart_get_TxPin (config.uart_num ));
369
+
370
+ uart_internal_loopback (config.uart_num , NEW_RX1);
371
+ config.transmit_and_check_msg (" using new pins" );
372
+ } else {
373
+ for (int i = 0 ; i < TEST_UART_NUM; i++) {
374
+ UARTTestConfig& config = *uart_test_configs[i];
375
+ UARTTestConfig& next_uart = *uart_test_configs[(i + 1 ) % TEST_UART_NUM];
376
+ config.serial .setPins (next_uart.default_rx_pin , next_uart.default_tx_pin );
377
+ TEST_ASSERT_EQUAL (uart_get_RxPin (config.uart_num ), next_uart.default_rx_pin );
378
+ TEST_ASSERT_EQUAL (uart_get_TxPin (config.uart_num ), next_uart.default_tx_pin );
379
+
380
+ uart_internal_loopback (config.uart_num , next_uart.default_rx_pin );
381
+ config.transmit_and_check_msg (" using new pins" );
382
+ }
383
+ }
464
384
465
385
Serial.println (" Change pins test successful" );
466
386
}
@@ -475,12 +395,15 @@ void auto_baudrate_test(void) {
475
395
476
396
log_d (" Stopping test serial. Using Serial2 for ESP32 and Serial1 for ESP32-S2." );
477
397
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;
398
+ if (TEST_UART_NUM == 1 ) {
399
+ selected_serial = &Serial1;
400
+ uart_internal_loopback (0 , RX1);
401
+ } else {
402
+ #ifdef RX2
403
+ selected_serial = &Serial2;
404
+ uart_internal_loopback (1 , RX2);
483
405
#endif
406
+ }
484
407
485
408
// selected_serial->end(false);
486
409
@@ -493,10 +416,10 @@ void auto_baudrate_test(void) {
493
416
selected_serial->begin (0 );
494
417
baudrate = selected_serial->baudRate ();
495
418
496
- # if SOC_UART_HP_NUM == 2
497
- Serial.end ();
498
- Serial.begin (115200 );
499
- # endif
419
+ if (TEST_UART_NUM == 1 ) {
420
+ Serial.end ();
421
+ Serial.begin (115200 );
422
+ }
500
423
501
424
TEST_ASSERT_UINT_WITHIN (2304 , 115200 , baudrate);
502
425
@@ -510,32 +433,23 @@ void periman_test(void) {
510
433
511
434
log_d (" Setting up I2C on the same pins as UART" );
512
435
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 ());
436
+ for (auto * ref : uart_test_configs) {
437
+ UARTTestConfig& config = *ref;
438
+ Wire.begin (config.default_rx_pin , config.default_tx_pin );
439
+ config.recv_msg = " " ;
524
440
525
- log_d (" Disabling I2C and re-enabling UART" );
441
+ log_d (" Trying to send message using UART%d with I2C enabled" , config.uart_num );
442
+ config.transmit_and_check_msg (" while used by I2C" , false );
443
+ TEST_ASSERT_EQUAL_STRING (" " , config.recv_msg .c_str ());
526
444
527
- Serial1. setPins (RX1, TX1 );
445
+ log_d ( " Disabling I2C and re-enabling UART%d " , config. uart_num );
528
446
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
447
+ config.serial .setPins (config.default_rx_pin , config.default_tx_pin );
448
+ uart_internal_loopback (config.uart_num , config.default_rx_pin );
536
449
537
- log_d (" Trying to send message using UART with I2C disabled" );
538
- transmit_and_check_msg (" while I2C is disabled" );
450
+ log_d (" Trying to send message using UART%d with I2C disabled" , config.uart_num );
451
+ config.transmit_and_check_msg (" while I2C is disabled" );
452
+ }
539
453
540
454
Serial.println (" Peripheral manager test successful" );
541
455
}
@@ -551,17 +465,23 @@ void change_cpu_frequency_test(void) {
551
465
552
466
Serial.updateBaudRate (115200 );
553
467
554
- log_d (" Trying to send message with the new CPU frequency" );
555
- transmit_and_check_msg (" with new CPU frequency" );
468
+ for (auto * ref : uart_test_configs) {
469
+ UARTTestConfig& config = *ref;
470
+ log_d (" Trying to send message with the new CPU frequency on UART%d" , config.uart_num );
471
+ config.transmit_and_check_msg (" with new CPU frequency" );
472
+ }
556
473
557
474
log_d (" Changing CPU frequency back to %dMHz" , old_freq);
558
475
Serial.flush ();
559
476
setCpuFrequencyMhz (old_freq);
560
477
561
478
Serial.updateBaudRate (115200 );
562
479
563
- log_d (" Trying to send message with the original CPU frequency" );
564
- transmit_and_check_msg (" with the original CPU frequency" );
480
+ for (auto * ref : uart_test_configs) {
481
+ UARTTestConfig& config = *ref;
482
+ log_d (" Trying to send message with the original CPU frequency on UART%d" , config.uart_num );
483
+ config.transmit_and_check_msg (" with the original CPU frequency" );
484
+ }
565
485
566
486
Serial.println (" Change CPU frequency test successful" );
567
487
}
@@ -573,30 +493,39 @@ void setup() {
573
493
while (!Serial) {
574
494
delay (10 );
575
495
}
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);
496
+
497
+ uart_test_configs = {
498
+ #if SOC_UART_HP_NUM >= 2 && defined(RX1) && defined(TX1)
499
+ // inverting RX1<->TX1 because ESP32-P4 has a problem with loopback on RX1 :: GPIO11 <-- UART_TX SGINAL
500
+ new UARTTestConfig (1 , Serial1, TX1, RX1),
501
+ #endif
502
+ #if SOC_UART_HP_NUM >= 3 && defined(RX2) && defined(TX2)
503
+ new UARTTestConfig (2 , Serial2, RX2, TX2),
504
+ #endif
505
+ #if SOC_UART_HP_NUM >= 4 && defined(RX3) && defined(TX3)
506
+ new UARTTestConfig (3 , Serial3, RX3, TX3),
599
507
#endif
508
+ #if SOC_UART_HP_NUM >= 5 && defined(RX4) && defined(TX4)
509
+ new UARTTestConfig (4 , Serial4, RX4, TX4)
510
+ #endif
511
+ };
512
+
513
+ if (TEST_UART_NUM == 0 ) {
514
+ log_e (" This test requires at least one UART besides UART0 configured" );
515
+ abort ();
516
+ }
517
+
518
+ log_d (" TEST_UART_NUM = %d" , TEST_UART_NUM);
519
+
520
+ for (auto * ref : uart_test_configs) {
521
+ UARTTestConfig& config = *ref;
522
+ config.begin (115200 );
523
+ log_d (" Setup internal loop-back from and back to UART%d TX >> UART%d RX" , config.uart_num , config.uart_num );
524
+ config.serial .onReceive ([&config]() {
525
+ config.onReceive ();
526
+ });
527
+ uart_internal_loopback (config.uart_num , uart_get_RxPin (config.uart_num ));
528
+ }
600
529
601
530
log_d (" Setup done. Starting tests" );
602
531
0 commit comments