|
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 |
| - // 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 |
| - } |
| 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); |
62 | 61 | }
|
| 62 | + } |
63 | 63 |
|
64 |
| - void end() { |
65 |
| - serial.end(); |
66 |
| - } |
| 64 | + void end() { |
| 65 | + serial.end(); |
| 66 | + } |
67 | 67 |
|
68 |
| - void reset_buffers() { |
69 |
| - recv_msg = ""; |
70 |
| - peeked_char = -1; |
71 |
| - } |
| 68 | + void reset_buffers() { |
| 69 | + recv_msg = ""; |
| 70 | + peeked_char = -1; |
| 71 | + } |
72 | 72 |
|
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 |
| - } |
| 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()); |
83 | 82 | }
|
| 83 | + } |
84 | 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 |
| - } |
| 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; |
95 | 94 | }
|
| 95 | + } |
96 | 96 | };
|
97 | 97 |
|
98 | 98 | /* Utility global variables */
|
@@ -450,8 +450,8 @@ void periman_test(void) {
|
450 | 450 | TEST_ASSERT_EQUAL_STRING("", config.recv_msg.c_str());
|
451 | 451 |
|
452 | 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); |
454 | 453 |
|
| 454 | + config.serial.setPins(config.default_rx_pin, config.default_tx_pin); |
455 | 455 | uart_internal_loopback(config.uart_num, config.default_rx_pin);
|
456 | 456 |
|
457 | 457 | log_d("Trying to send message using UART%d with I2C disabled", config.uart_num);
|
|
0 commit comments