1
1
/* UART test
2
- *
3
- * This test is using UART0 (Serial) only for reporting test status and helping with the auto
4
- * baudrate detection test.
5
- * UART1 (Serial1) and UART2 (Serial2), where available, are used for testing.
6
- */
2
+
3
+ This test is using UART0 (Serial) only for reporting test status and helping with the auto
4
+ baudrate detection test.
5
+ UART1 (Serial1) and UART2 (Serial2), where available, are used for testing.
6
+ */
7
7
8
8
#include < unity.h>
9
9
#include " HardwareSerial.h"
16
16
// UART0 TX | SOC_TX0 | 1 | 43 | 43 | 21 | 16 | 24 | 37 |
17
17
// UART1 RX | RX1 | 26 | 4 | 15 | 18 | 4 | 0 | 11 |
18
18
// UART1 TX | TX1 | 27 | 5 | 16 | 19 | 5 | 1 | 10 |
19
- // UART2 RX | RX2 | 4 | -- | 19 | -- | -- | -- | -- |
20
- // UART2 TX | TX2 | 25 | -- | 20 | -- | -- | -- | -- |
19
+ // UART2 RX | RX2 | 4 | -- | 19 | -- | -- | -- | UU |
20
+ // UART2 TX | TX2 | 25 | -- | 20 | -- | -- | -- | UU |
21
21
22
22
/*
23
- * For each UART:
24
- *
25
- * terminal
26
- * | ^
27
- * v UART0 |
28
- * RX ^ TX
29
- * |
30
- * report status
31
- * |
32
- * TX <---> RX
33
- * UARTx
34
- */
23
+ For each UART:
24
+
25
+ terminal
26
+ | ^
27
+ v UART0 |
28
+ RX ^ TX
29
+ |
30
+ report status
31
+ |
32
+ TX <---> RX
33
+ UARTx
34
+ */
35
35
36
36
typedef struct test_uart_t
37
37
{
38
38
uint8_t rx_pin;
39
39
uint8_t tx_pin;
40
- HardwareSerial & serial;
40
+ HardwareSerial * serial;
41
41
} test_uart_t ;
42
42
43
- const test_uart_t test_uarts[SOC_UART_NUM ] = {
43
+ const test_uart_t test_uarts[SOC_UART_HP_NUM ] = {
44
44
{
45
45
.rx_pin = SOC_RX0,
46
46
.tx_pin = SOC_TX0,
47
- .serial = Serial,
47
+ .serial = & Serial,
48
48
},
49
49
{
50
50
.rx_pin = RX1,
51
51
.tx_pin = TX1,
52
- .serial = Serial1,
52
+ .serial = & Serial1,
53
53
},
54
- #if SOC_UART_NUM >= 3
54
+ #if SOC_UART_HP_NUM >= 3
55
55
{
56
56
#ifdef RX2
57
57
.rx_pin = RX2,
@@ -60,10 +60,10 @@ const test_uart_t test_uarts[SOC_UART_NUM] = {
60
60
.rx_pin = RX1,
61
61
.tx_pin = TX1,
62
62
#endif
63
- .serial = Serial2,
63
+ .serial = & Serial2,
64
64
},
65
- #endif // SOC_UART_NUM >= 3
66
- #if SOC_UART_NUM >= 4
65
+ #endif // SOC_UART_HP_NUM >= 3
66
+ #if SOC_UART_HP_NUM >= 4
67
67
{
68
68
#ifdef RX3
69
69
.rx_pin = RX3,
@@ -72,10 +72,10 @@ const test_uart_t test_uarts[SOC_UART_NUM] = {
72
72
.rx_pin = RX1,
73
73
.tx_pin = TX1,
74
74
#endif
75
- .serial = Serial3,
75
+ .serial = & Serial3,
76
76
},
77
- #endif // SOC_UART_NUM >= 4
78
- #if SOC_UART_NUM >= 5
77
+ #endif // SOC_UART_HP_NUM >= 4
78
+ #if SOC_UART_HP_NUM >= 5
79
79
{
80
80
#ifdef RX4
81
81
.rx_pin = RX4,
@@ -84,16 +84,16 @@ const test_uart_t test_uarts[SOC_UART_NUM] = {
84
84
.rx_pin = RX1,
85
85
.tx_pin = TX1,
86
86
#endif
87
- .serial = Serial4,
87
+ .serial = & Serial4,
88
88
}
89
- #endif // SOC_UART_NUM >= 5
89
+ #endif // SOC_UART_HP_NUM >= 5
90
90
};
91
91
92
92
/* Utility global variables */
93
93
94
94
uint8_t i;
95
95
static String recv_msg = " " ;
96
- static int peeked_char[SOC_UART_NUM ];
96
+ static int peeked_char[SOC_UART_HP_NUM ];
97
97
98
98
/* Utility functions */
99
99
@@ -102,8 +102,8 @@ extern int8_t uart_get_TxPin(uint8_t uart_num);
102
102
103
103
// This function starts all the available test UARTs
104
104
void start_serial (unsigned long baudrate = 115200 ) {
105
- for (i = 1 ; i < SOC_UART_NUM ; i++) {
106
- test_uarts[i].serial . begin (baudrate, SERIAL_8N1, test_uarts[i].rx_pin , test_uarts[i].tx_pin );
105
+ for (i = 1 ; i < SOC_UART_HP_NUM ; i++) {
106
+ test_uarts[i].serial -> begin (baudrate, SERIAL_8N1, test_uarts[i].rx_pin , test_uarts[i].tx_pin );
107
107
while (!test_uarts[i].serial ) {
108
108
delay (10 );
109
109
}
@@ -112,17 +112,17 @@ void start_serial(unsigned long baudrate = 115200) {
112
112
113
113
// This function stops all the available test UARTs
114
114
void stop_serial () {
115
- for (i = 1 ; i < SOC_UART_NUM ; i++) {
116
- test_uarts[i].serial . end ();
115
+ for (i = 1 ; i < SOC_UART_HP_NUM ; i++) {
116
+ test_uarts[i].serial -> end ();
117
117
}
118
118
}
119
119
120
120
// This function transmits a message and checks if it was received correctly
121
121
void transmit_and_check_msg (const String msg_append, bool perform_assert = true ) {
122
122
delay (100 ); // Wait for some settings changes to take effect
123
- for (i = 1 ; i < SOC_UART_NUM ; i++) {
124
- test_uarts[i].serial . print (" Hello from Serial" + String (i) + " (UART" + String (i) + " ) >>> via loopback >>> Serial" + String (i) + " (UART" + String (i) + " ) " + msg_append);
125
- test_uarts[i].serial . flush ();
123
+ for (i = 1 ; i < SOC_UART_HP_NUM ; i++) {
124
+ test_uarts[i].serial -> print (" Hello from Serial" + String (i) + " (UART" + String (i) + " ) >>> via loopback >>> Serial" + String (i) + " (UART" + String (i) + " ) " + msg_append);
125
+ test_uarts[i].serial -> flush ();
126
126
delay (100 );
127
127
if (perform_assert) {
128
128
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)
133
133
/* Tasks */
134
134
135
135
/*
136
- // This task is used to send a message after a delay to test the auto baudrate detection
137
- void task_delayed_msg(void *pvParameters) {
136
+ // This task is used to send a message after a delay to test the auto baudrate detection
137
+ void task_delayed_msg(void *pvParameters) {
138
138
HardwareSerial *selected_serial;
139
139
140
- #if SOC_UART_NUM >= 3
140
+ #if SOC_UART_HP_NUM >= 3
141
141
selected_serial = &Serial1;
142
- #else
142
+ #else
143
143
selected_serial = &Serial;
144
- #endif
144
+ #endif
145
145
146
146
delay(2000);
147
147
selected_serial->println("Hello from Serial to detect baudrate");
148
148
selected_serial->flush();
149
149
vTaskDelete(NULL);
150
- }
150
+ }
151
151
*/
152
152
153
153
/* Unity functions */
@@ -156,10 +156,10 @@ void task_delayed_msg(void *pvParameters) {
156
156
void setUp (void ) {
157
157
start_serial (115200 );
158
158
159
- for (i = 1 ; i < SOC_UART_NUM ; i++) {
159
+ for (i = 1 ; i < SOC_UART_HP_NUM ; i++) {
160
160
log_d (" Setup internal loop-back from and back to Serial%d (UART%d) TX >> Serial%d (UART%d) RX" , i, i, i, i);
161
161
162
- test_uarts[i].serial . onReceive ([]() {
162
+ test_uarts[i].serial -> onReceive ([]() {
163
163
onReceive_cb (i);
164
164
});
165
165
uart_internal_loopback (i, true );
@@ -175,18 +175,18 @@ void tearDown(void) {
175
175
176
176
// This is a callback function that will be activated on UART RX events
177
177
void onReceive_cb (int uart_num) {
178
- HardwareSerial & selected_serial = test_uarts[uart_num].serial ;
178
+ HardwareSerial * selected_serial = test_uarts[uart_num].serial ;
179
179
char c;
180
180
181
181
recv_msg = " " ;
182
- size_t available = selected_serial. available ();
182
+ size_t available = selected_serial-> available ();
183
183
184
184
if (available != 0 ) {
185
- peeked_char[uart_num] = selected_serial. peek ();
185
+ peeked_char[uart_num] = selected_serial-> peek ();
186
186
}
187
187
188
188
while (available--) {
189
- c = (char )selected_serial. read ();
189
+ c = (char )selected_serial-> read ();
190
190
recv_msg += c;
191
191
}
192
192
@@ -209,10 +209,10 @@ void change_baudrate_test(void) {
209
209
// Test first using the updateBaudRate method and then using the begin method
210
210
log_d (" Changing baudrate to 9600" );
211
211
212
- for (i = 1 ; i < SOC_UART_NUM ; i++) {
213
- test_uarts[i].serial . updateBaudRate (9600 );
212
+ for (i = 1 ; i < SOC_UART_HP_NUM ; i++) {
213
+ test_uarts[i].serial -> updateBaudRate (9600 );
214
214
// Baudrate error should be within 2% of the target baudrate
215
- TEST_ASSERT_UINT_WITHIN (192 , 9600 , test_uarts[i].serial . baudRate ());
215
+ TEST_ASSERT_UINT_WITHIN (192 , 9600 , test_uarts[i].serial -> baudRate ());
216
216
}
217
217
218
218
log_d (" Sending string using 9600 baudrate" );
@@ -221,9 +221,9 @@ void change_baudrate_test(void) {
221
221
log_d (" Changing baudrate back to 115200" );
222
222
start_serial (115200 );
223
223
224
- for (i = 1 ; i < SOC_UART_NUM ; i++) {
224
+ for (i = 1 ; i < SOC_UART_HP_NUM ; i++) {
225
225
// Baudrate error should be within 2% of the target baudrate
226
- TEST_ASSERT_UINT_WITHIN (2304 , 115200 , test_uarts[i].serial . baudRate ());
226
+ TEST_ASSERT_UINT_WITHIN (2304 , 115200 , test_uarts[i].serial -> baudRate ());
227
227
}
228
228
229
229
log_d (" Sending string using 115200 baudrate" );
@@ -401,22 +401,22 @@ void change_pins_test(void) {
401
401
log_d (" Disabling UART loopback" );
402
402
// stop_serial();
403
403
404
- for (i = 1 ; i < SOC_UART_NUM ; i++) {
404
+ for (i = 1 ; i < SOC_UART_HP_NUM ; i++) {
405
405
uart_internal_loopback (i, false );
406
406
}
407
407
408
408
log_d (" Swapping UART pins" );
409
409
410
- #if SOC_UART_NUM == 2
410
+ #if SOC_UART_HP_NUM == 2
411
411
Serial1.setPins (TX1, RX1); // Invert TX and RX pins
412
412
TEST_ASSERT_EQUAL (TX1, uart_get_RxPin (1 )); // TX1 is now RX pin
413
413
TEST_ASSERT_EQUAL (RX1, uart_get_TxPin (1 )); // RX1 is now TX pin
414
- #elif SOC_UART_NUM >= 3
415
- for (i = 1 ; i < SOC_UART_NUM ; i++) {
414
+ #elif SOC_UART_HP_NUM >= 3
415
+ for (i = 1 ; i < SOC_UART_HP_NUM ; i++) {
416
416
// Swapping pins with the next available UART
417
- int uart_replacement = (i + 1 ) % SOC_UART_NUM ;
417
+ int uart_replacement = (i + 1 ) % SOC_UART_HP_NUM ;
418
418
uart_replacement = uart_replacement == 0 ? 1 : uart_replacement;
419
- test_uarts[i].serial . setPins (test_uarts[uart_replacement].rx_pin , test_uarts[uart_replacement].tx_pin );
419
+ test_uarts[i].serial -> setPins (test_uarts[uart_replacement].rx_pin , test_uarts[uart_replacement].tx_pin );
420
420
TEST_ASSERT_EQUAL (test_uarts[uart_replacement].tx_pin , uart_get_TxPin (i));
421
421
TEST_ASSERT_EQUAL (test_uarts[uart_replacement].rx_pin , uart_get_RxPin (i));
422
422
}
@@ -426,7 +426,7 @@ void change_pins_test(void) {
426
426
427
427
log_d (" Re-enabling UART loopback" );
428
428
429
- for (i = 1 ; i < SOC_UART_NUM ; i++) {
429
+ for (i = 1 ; i < SOC_UART_HP_NUM ; i++) {
430
430
uart_internal_loopback (i, true );
431
431
}
432
432
@@ -437,24 +437,24 @@ void change_pins_test(void) {
437
437
438
438
/*
439
439
440
- // The new loopback API does not allow cross connecting UARTs. This test is disabled for now.
440
+ // The new loopback API does not allow cross connecting UARTs. This test is disabled for now.
441
441
442
- // This test checks if the auto baudrate detection works on ESP32 and ESP32-S2
443
- #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
444
- void auto_baudrate_test(void) {
442
+ // This test checks if the auto baudrate detection works on ESP32 and ESP32-S2
443
+ #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
444
+ void auto_baudrate_test(void) {
445
445
log_d("Starting auto baudrate test");
446
446
447
447
HardwareSerial *selected_serial;
448
448
unsigned long baudrate;
449
449
450
450
log_d("Stopping test serial. Using Serial2 for ESP32 and Serial1 for ESP32-S2.");
451
451
452
- #if SOC_UART_HP_NUM == 2
452
+ #if SOC_UART_HP_NUM == 2
453
453
selected_serial = &Serial1;
454
454
uart_internal_loopback(0, true); // it was suppose to cross connect TX0 to RX1
455
- #elif SOC_UART_NUM >= 3
455
+ #elif SOC_UART_HP_NUM >= 3
456
456
selected_serial = &Serial2;
457
- #endif
457
+ #endif
458
458
459
459
//selected_serial->end(false);
460
460
@@ -467,31 +467,31 @@ void auto_baudrate_test(void) {
467
467
selected_serial->begin(0);
468
468
baudrate = selected_serial->baudRate();
469
469
470
- #if SOC_UART_HP_NUM == 2
470
+ #if SOC_UART_HP_NUM == 2
471
471
Serial.end();
472
472
Serial.begin(115200);
473
- #endif
473
+ #endif
474
474
475
475
TEST_ASSERT_UINT_WITHIN(2304, 115200, baudrate);
476
476
477
477
Serial.println("Auto baudrate test successful");
478
- }
479
- #endif
478
+ }
479
+ #endif
480
480
*/
481
481
482
482
// This test checks if the peripheral manager can properly manage UART pins
483
483
void periman_test (void ) {
484
484
log_d (" Checking if peripheral manager can properly manage UART pins" );
485
485
486
- for (i = 1 ; i < SOC_UART_NUM ; i++) {
486
+ for (i = 1 ; i < SOC_UART_HP_NUM ; i++) {
487
487
log_d (" Setting up I2C on the same pins as UART%d" , i);
488
488
Wire.begin (test_uarts[i].rx_pin , test_uarts[i].tx_pin );
489
489
490
490
recv_msg = " " ;
491
491
492
492
log_d (" Trying to send message using UART%d with I2C enabled" , i);
493
- 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" );
494
- test_uarts[i].serial . flush ();
493
+ 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" );
494
+ test_uarts[i].serial -> flush ();
495
495
delay (100 );
496
496
TEST_ASSERT_EQUAL_STRING (" " , recv_msg.c_str ());
497
497
@@ -542,16 +542,16 @@ void setup() {
542
542
}
543
543
log_d (" SOC_UART_HP_NUM = %d" , SOC_UART_HP_NUM);
544
544
545
- for (i = 0 ; i < SOC_UART_NUM ; i++) {
545
+ for (i = 0 ; i < SOC_UART_HP_NUM ; i++) {
546
546
peeked_char[i] = -1 ;
547
547
}
548
548
549
549
// Begin needs to be called before setting up the loopback because it creates the serial object
550
550
start_serial (115200 );
551
551
552
- for (i = 1 ; i < SOC_UART_NUM ; i++) {
552
+ for (i = 1 ; i < SOC_UART_HP_NUM ; i++) {
553
553
log_d (" Setup internal loop-back from and back to Serial%d (UART%d) TX >> Serial%d (UART%d) RX" , i, i, i, i);
554
- test_uarts[i].serial . onReceive ([]() {
554
+ test_uarts[i].serial -> onReceive ([]() {
555
555
onReceive_cb (i);
556
556
});
557
557
uart_internal_loopback (i, true );
@@ -567,11 +567,11 @@ void setup() {
567
567
RUN_TEST (change_cpu_frequency_test);
568
568
RUN_TEST (disabled_uart_calls_test);
569
569
RUN_TEST (enabled_uart_calls_test);
570
- /*
571
- #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
572
- RUN_TEST(auto_baudrate_test);
573
- #endif
574
- */
570
+ /*
571
+ #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
572
+ RUN_TEST(auto_baudrate_test);
573
+ #endif
574
+ */
575
575
RUN_TEST (periman_test);
576
576
RUN_TEST (change_pins_test);
577
577
RUN_TEST (end_when_stopped_test);
0 commit comments