|
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 |
| - * The other serials 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 | + The other serials are used for testing. |
| 6 | +*/ |
7 | 7 |
|
8 | 8 | // Default pins:
|
9 | 9 | // | Name | ESP32 | S2 | S3 | C3 | C6 | H2 | P4 |
|
|
15 | 15 | // UART2 TX | TX2 | 25 | -- | 20 | -- | -- | -- | -- |
|
16 | 16 |
|
17 | 17 | /*
|
18 |
| - * For each UART: |
19 |
| - * |
20 |
| - * terminal |
21 |
| - * | ^ |
22 |
| - * v UART0 | |
23 |
| - * RX ^ TX |
24 |
| - * | |
25 |
| - * report status |
26 |
| - * | |
27 |
| - * TX <---> RX |
28 |
| - * UARTx |
29 |
| - */ |
| 18 | + For each UART: |
| 19 | +
|
| 20 | + terminal |
| 21 | + | ^ |
| 22 | + v UART0 | |
| 23 | + RX ^ TX |
| 24 | + | |
| 25 | + report status |
| 26 | + | |
| 27 | + TX <---> RX |
| 28 | + UARTx |
| 29 | +*/ |
30 | 30 |
|
31 | 31 | #include <vector>
|
32 | 32 | #include <unity.h>
|
|
41 | 41 | /* Utility classes */
|
42 | 42 |
|
43 | 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; |
51 |
| - |
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("") {} |
54 |
| - |
55 |
| - void begin(unsigned long baudrate) { |
56 |
| - serial.begin(baudrate, SERIAL_8N1, default_rx_pin, default_tx_pin); |
57 |
| - while (!serial) { |
58 |
| - delay(10); |
| 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; |
| 51 | + |
| 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("") {} |
| 54 | + |
| 55 | + void begin(unsigned long baudrate) { |
| 56 | + // pinMode will force enabing the internal pullup resistor (IDF 5.3.2 Change) |
| 57 | + pinMode(default_rx_pin, INPUT_PULLUP); |
| 58 | + serial.begin(baudrate, SERIAL_8N1, default_rx_pin, default_tx_pin); |
| 59 | + while (!serial) { |
| 60 | + delay(10); |
| 61 | + } |
59 | 62 | }
|
60 |
| - } |
61 | 63 |
|
62 |
| - void end() { |
63 |
| - serial.end(); |
64 |
| - } |
65 |
| - |
66 |
| - void reset_buffers() { |
67 |
| - recv_msg = ""; |
68 |
| - peeked_char = -1; |
69 |
| - } |
| 64 | + void end() { |
| 65 | + serial.end(); |
| 66 | + } |
70 | 67 |
|
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()); |
| 68 | + void reset_buffers() { |
| 69 | + recv_msg = ""; |
| 70 | + peeked_char = -1; |
80 | 71 | }
|
81 |
| - } |
82 | 72 |
|
83 |
| - void onReceive() { |
84 |
| - char c; |
85 |
| - size_t available = serial.available(); |
86 |
| - if (peeked_char == -1) { |
87 |
| - peeked_char = serial.peek(); |
| 73 | + void transmit_and_check_msg(const String &msg_append, bool perform_assert = true) { |
| 74 | + reset_buffers(); |
| 75 | + delay(100); |
| 76 | + serial.print("Hello from Serial" + String(uart_num) + " " + msg_append); |
| 77 | + serial.flush(); |
| 78 | + delay(100); |
| 79 | + if (perform_assert) { |
| 80 | + TEST_ASSERT_EQUAL_STRING(("Hello from Serial" + String(uart_num) + " " + msg_append).c_str(), recv_msg.c_str()); |
| 81 | + log_d("UART%d received message: %s\n", uart_num, recv_msg.c_str()); |
| 82 | + } |
88 | 83 | }
|
89 |
| - while (available--) { |
90 |
| - c = (char)serial.read(); |
91 |
| - recv_msg += c; |
| 84 | + |
| 85 | + void onReceive() { |
| 86 | + char c; |
| 87 | + size_t available = serial.available(); |
| 88 | + if (peeked_char == -1) { |
| 89 | + peeked_char = serial.peek(); |
| 90 | + } |
| 91 | + while (available--) { |
| 92 | + c = (char)serial.read(); |
| 93 | + recv_msg += c; |
| 94 | + } |
92 | 95 | }
|
93 |
| - } |
94 | 96 | };
|
95 | 97 |
|
96 | 98 | /* Utility global variables */
|
@@ -365,30 +367,27 @@ void change_pins_test(void) {
|
365 | 367 |
|
366 | 368 | if (TEST_UART_NUM == 1) {
|
367 | 369 | UARTTestConfig &config = *uart_test_configs[0];
|
368 |
| - // internal loopback creates a BREAK on ESP32 and ESP32-S2 |
369 |
| - // setting it before changing the pins solves it |
370 |
| - uart_internal_loopback(config.uart_num, NEW_RX1); |
371 |
| - delay(5); // wait for internal circuit to settle |
| 370 | + // pinMode will force enabing the internal pullup resistor (IDF 5.3.2 Change) |
| 371 | + pinMode(NEW_RX1, INPUT_PULLUP); |
372 | 372 | config.serial.setPins(NEW_RX1, NEW_TX1);
|
373 | 373 | TEST_ASSERT_EQUAL(NEW_RX1, uart_get_RxPin(config.uart_num));
|
374 | 374 | TEST_ASSERT_EQUAL(NEW_TX1, uart_get_TxPin(config.uart_num));
|
375 |
| - config.transmit_and_check_msg("using new UART#1 pins"); |
| 375 | + |
| 376 | + uart_internal_loopback(config.uart_num, NEW_RX1); |
| 377 | + config.transmit_and_check_msg("using new pins"); |
376 | 378 | } else {
|
377 | 379 | for (int i = 0; i < TEST_UART_NUM; i++) {
|
378 | 380 | UARTTestConfig &config = *uart_test_configs[i];
|
379 | 381 | UARTTestConfig &next_uart = *uart_test_configs[(i + 1) % TEST_UART_NUM];
|
380 |
| - // internal loopback creates a BREAK on ESP32 and ESP32-S2 |
381 |
| - // setting it before changing the pins solves it |
382 |
| - uart_internal_loopback(config.uart_num, next_uart.default_rx_pin); |
383 |
| - delay(5); // wait for internal circuit to settle |
384 | 382 | config.serial.setPins(next_uart.default_rx_pin, next_uart.default_tx_pin);
|
385 | 383 | TEST_ASSERT_EQUAL(uart_get_RxPin(config.uart_num), next_uart.default_rx_pin);
|
386 | 384 | TEST_ASSERT_EQUAL(uart_get_TxPin(config.uart_num), next_uart.default_tx_pin);
|
387 |
| - String msg = String("using UART#") + config.uart_num + " pins"; |
388 |
| - config.transmit_and_check_msg(msg.c_str()); |
| 385 | + |
| 386 | + uart_internal_loopback(config.uart_num, next_uart.default_rx_pin); |
| 387 | + config.transmit_and_check_msg("using new pins"); |
389 | 388 | }
|
390 | 389 | }
|
391 |
| - |
| 390 | + |
392 | 391 | Serial.println("Change pins test successful");
|
393 | 392 | }
|
394 | 393 |
|
@@ -443,23 +442,17 @@ void periman_test(void) {
|
443 | 442 |
|
444 | 443 | for (auto *ref : uart_test_configs) {
|
445 | 444 | UARTTestConfig &config = *ref;
|
446 |
| - //Wire.begin(config.default_rx_pin, config.default_tx_pin); |
447 |
| - pinMode(config.default_rx_pin, INPUT); |
448 |
| - pinMode(config.default_tx_pin, OUTPUT); |
449 |
| - |
| 445 | + Wire.begin(config.default_rx_pin, config.default_tx_pin); |
450 | 446 | config.recv_msg = "";
|
451 | 447 |
|
452 | 448 | log_d("Trying to send message using UART%d with I2C enabled", config.uart_num);
|
453 | 449 | config.transmit_and_check_msg("while used by I2C", false);
|
454 | 450 | TEST_ASSERT_EQUAL_STRING("", config.recv_msg.c_str());
|
455 | 451 |
|
456 | 452 | log_d("Disabling I2C and re-enabling UART%d", config.uart_num);
|
| 453 | + config.serial.setPins(config.default_rx_pin, config.default_tx_pin); |
457 | 454 |
|
458 |
| - // internal loopback creates a BREAK on ESP32 and ESP32-S2 |
459 |
| - // setting it before changing the pins solves it |
460 | 455 | uart_internal_loopback(config.uart_num, config.default_rx_pin);
|
461 |
| - delay(5); // wait for internal circuit to settle |
462 |
| - config.serial.setPins(config.default_rx_pin, config.default_tx_pin); |
463 | 456 |
|
464 | 457 | log_d("Trying to send message using UART%d with I2C disabled", config.uart_num);
|
465 | 458 | config.transmit_and_check_msg("while I2C is disabled");
|
|
0 commit comments