@@ -157,21 +157,69 @@ void mcu_rx_test_string()
157157 TEST_ASSERT_EQUAL_UINT32 (totalBytesRead, TEST_STRING_LEN);
158158}
159159
160+ // Test the UART's ability to handle junk on the line without losing its ability to receive characters.
161+ // We do this by configuring the UART for 115200 baud, then having the host send the test string at 9600 baud
162+ // and 921600 baud
163+ void handle_junk_on_line () {
164+
165+ // Set our Rx baudrate
166+ uart->set_baud (115200 );
167+ uart->set_blocking (false );
168+
169+ // Have the host send a test string at 9600 baud
170+ greentea_send_kv (" setup_port_at_baud" , 9600 );
171+ assert_next_message_from_host (" setup_port_at_baud" , " complete" );
172+ greentea_send_kv (" send_test_string" , 1 );
173+ assert_next_message_from_host (" send_test_string" , " started" );
174+ wait_us (get_time_to_transmit (9600 , TEST_STRING_LEN));
175+
176+ // Have the host send a test string at 921600 baud
177+ greentea_send_kv (" setup_port_at_baud" , 921600 );
178+ assert_next_message_from_host (" setup_port_at_baud" , " complete" );
179+ greentea_send_kv (" send_test_string" , 1 );
180+ assert_next_message_from_host (" send_test_string" , " started" );
181+ wait_us (get_time_to_transmit (921600 , TEST_STRING_LEN));
182+
183+ // It's OK if we did get some chars, just remove them.
184+ size_t totalCharsRemoved = 0 ;
185+ while (true ) {
186+ ssize_t readResult = uart->read (rxBuffer, sizeof (rxBuffer));
187+ if (readResult == -EAGAIN)
188+ {
189+ // Nothing to read
190+ break ;
191+ }
192+ else if (readResult > 0 )
193+ {
194+ totalCharsRemoved += readResult;
195+ }
196+ else
197+ {
198+ TEST_FAIL_MESSAGE (" Unexpected read result." );
199+ return ;
200+ }
201+ }
202+ printf (" UART received %zu junk chars\n " , totalCharsRemoved);
203+
204+ // Now see if we can properly receive data still
205+ mcu_rx_test_string<115200 >();
206+ }
207+
160208utest::v1::status_t test_setup (const size_t number_of_cases) {
161209 // Setup Greentea using a reasonable timeout in seconds
162210 GREENTEA_SETUP (30 , " uart_test" );
163211
212+ // Set up mux for UART
213+ static BusOut funcSelPins (PIN_FUNC_SEL0, PIN_FUNC_SEL1, PIN_FUNC_SEL2);
214+ funcSelPins = 0b000 ;
215+
164216 // Use static pinmap if supported for this device
165217#if STATIC_PINMAP_READY
166218 uart = new BufferedSerial (serialPinmap);
167219#else
168220 uart = new BufferedSerial (PIN_UART_MCU_TX, PIN_UART_MCU_RX);
169221#endif
170222
171- // Set up mux for UART
172- static BusOut funcSelPins (PIN_FUNC_SEL0, PIN_FUNC_SEL1, PIN_FUNC_SEL2);
173- funcSelPins = 0b000 ;
174-
175223 return utest::v1::verbose_test_setup_handler (number_of_cases);
176224}
177225
@@ -189,6 +237,8 @@ utest::v1::Case cases[] = {
189237 utest::v1::Case (" Receive test string from PC once (921600 baud)" , mcu_rx_test_string<921600 >),
190238 utest::v1::Case (" Send test string from MCU once (3000000 baud)" , mcu_tx_test_string<3000000 >),
191239 utest::v1::Case (" Receive test string from PC once (3000000 baud)" , mcu_rx_test_string<3000000 >),
240+
241+ utest::v1::Case (" Handle Junk on Serial Rx Line" , handle_junk_on_line)
192242};
193243
194244utest::v1::Specification specification (test_setup, cases, utest::v1::greentea_continue_handlers);
0 commit comments