diff --git a/examples/peripherals/dedicated_gpio/soft_uart/components/soft_uart/include/soft_uart.h b/examples/peripherals/dedicated_gpio/soft_uart/components/soft_uart/include/soft_uart.h index d35dc6362f..8d3e9f1601 100644 --- a/examples/peripherals/dedicated_gpio/soft_uart/components/soft_uart/include/soft_uart.h +++ b/examples/peripherals/dedicated_gpio/soft_uart/components/soft_uart/include/soft_uart.h @@ -65,6 +65,10 @@ esp_err_t soft_uart_del(soft_uart_port_t port); /** * @brief Send the given bytes on the software UART port. * + * @note Since toggling fast GPIOs for the first time may be slow (~1us), the first byte may be corrupted. + * If you are seeing this issue, prepend a dummy byte to the buffer to send when calling this + * function for the first time. + * * @param port Software UART port to send data on. * @param write_buffer Buffer containing the bytes to send on the buffer. Must not be NULL. * @param write_size Size of the write buffer. Must not be 0. diff --git a/examples/peripherals/dedicated_gpio/soft_uart/components/soft_uart/riscv/soft_uart.S b/examples/peripherals/dedicated_gpio/soft_uart/components/soft_uart/riscv/soft_uart.S index 4440886f7f..afb0f56be8 100644 --- a/examples/peripherals/dedicated_gpio/soft_uart/components/soft_uart/riscv/soft_uart.S +++ b/examples/peripherals/dedicated_gpio/soft_uart/components/soft_uart/riscv/soft_uart.S @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2010-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: CC0-1.0 */ @@ -33,11 +33,6 @@ emulate_uart_send: sll a2, t0, a2 /* Save return address in a4 */ mv a4, ra - /* As UART is very time sensitive, we want each bit sent to be very precise in terms of duration. - * The first toggle of the fast GPIO register may be slow, ~1us, so let's send a dummy byte here - * before the actual UART emulation start, else the first byte sent would be corrupted.*/ - li t0, 0 - call uart_send_byte /* Reading the characters 4 by 4 would be much faster, but in our case, we don't need * the process to be fast as the bottleneck is the UART speed */ uart_read_next: diff --git a/examples/peripherals/dedicated_gpio/soft_uart/main/soft_uart_main.c b/examples/peripherals/dedicated_gpio/soft_uart/main/soft_uart_main.c index e21018dcb9..860ee07b75 100644 --- a/examples/peripherals/dedicated_gpio/soft_uart/main/soft_uart_main.c +++ b/examples/peripherals/dedicated_gpio/soft_uart/main/soft_uart_main.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2010-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: CC0-1.0 */ @@ -18,6 +18,7 @@ void app_main(void) esp_err_t ret = ESP_OK; uint8_t read_buffer[READ_COUNT] = { 0 }; const uint8_t write_buffer[] = "Hello, world! This is a message.\r\n"; + uint8_t dummy = 0; soft_uart_port_t port = NULL; soft_uart_config_t config = { .tx_pin = CONFIG_EMULATE_UART_GPIO_TX, @@ -29,6 +30,10 @@ void app_main(void) ret = soft_uart_new(&config, &port); ESP_GOTO_ON_ERROR(ret, error, EXAMPLE_TAG, "Error configuring software UART"); + /* Send a dummy byte as the software UART driver may corrupt the first byte */ + ret = soft_uart_send(port, &dummy, sizeof(uint8_t)); + ESP_GOTO_ON_ERROR(ret, error, EXAMPLE_TAG, "Error writing a dummy byte"); + /* Write few bytes to the UART */ ret = soft_uart_send(port, write_buffer, sizeof(write_buffer)); ESP_GOTO_ON_ERROR(ret, error, EXAMPLE_TAG, "Error writing to the software UART");