Skip to content

Commit cce3293

Browse files
authored
Merge pull request #9193 from dhalbert/espressif-uart-fix
espressif UART: clear input buf on creation to avoid stray byte
2 parents 5410b0e + b27bba9 commit cce3293

File tree

1 file changed

+7
-0
lines changed
  • ports/espressif/common-hal/busio

1 file changed

+7
-0
lines changed

ports/espressif/common-hal/busio/UART.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,13 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
277277
if (uart_set_pin(self->uart_num, tx_num, rx_num, rts_num, cts_num) != ESP_OK) {
278278
raise_ValueError_invalid_pins();
279279
}
280+
if (have_rx) {
281+
// On ESP32-C3 and ESP32-S3 (at least), a junk byte with zero or more consecutive 1's can be
282+
// generated, even if the pin is pulled high (normal UART resting state) to begin with.
283+
// Wait one byte time, but at least 1 msec, and clear the input buffer to discard it.
284+
mp_hal_delay_ms(1 + (1000 * (bits + stop)) / baudrate);
285+
common_hal_busio_uart_clear_rx_buffer(self);
286+
}
280287
}
281288

282289
bool common_hal_busio_uart_deinited(busio_uart_obj_t *self) {

0 commit comments

Comments
 (0)