From 9af218dbbe48653b496f871f2197fc8fa5db8383 Mon Sep 17 00:00:00 2001 From: Erik Olof Gunnar Andersson Date: Thu, 14 Nov 2024 20:46:43 +0100 Subject: [PATCH] Implement serial wait on tx --- components/asic/bm1370.c | 2 +- components/asic/include/serial.h | 2 +- components/asic/serial.c | 10 ++++++++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/components/asic/bm1370.c b/components/asic/bm1370.c index 7ab12d506..4d2680323 100644 --- a/components/asic/bm1370.c +++ b/components/asic/bm1370.c @@ -100,7 +100,7 @@ static void _send_BM1370(uint8_t header, uint8_t * data, uint8_t data_len, bool } // send serial data - if (SERIAL_send(buf, total_length, debug) == 0) { + if (SERIAL_send(buf, total_length, debug) == ESP_FAIL) { ESP_LOGE(TAG, "Failed to send data to BM1370"); } diff --git a/components/asic/include/serial.h b/components/asic/include/serial.h index a8adef983..81d9b3447 100644 --- a/components/asic/include/serial.h +++ b/components/asic/include/serial.h @@ -4,7 +4,7 @@ #define SERIAL_BUF_SIZE 16 #define CHUNK_SIZE 1024 -int SERIAL_send(uint8_t *, int, bool); +esp_err_t SERIAL_send(uint8_t *, int, bool); void SERIAL_init(void); void SERIAL_debug_rx(void); int16_t SERIAL_rx(uint8_t *, uint16_t, uint16_t); diff --git a/components/asic/serial.c b/components/asic/serial.c index 814f72ccc..bf3361f2c 100644 --- a/components/asic/serial.c +++ b/components/asic/serial.c @@ -49,8 +49,10 @@ void SERIAL_set_baud(int baud) uart_set_baudrate(UART_NUM_1, baud); } -int SERIAL_send(uint8_t *data, int len, bool debug) +esp_err_t SERIAL_send(uint8_t *data, int len, bool debug) { + int written = uart_write_bytes(UART_NUM_1, (const char *)data, len); + if (debug) { printf("tx: "); @@ -58,7 +60,11 @@ int SERIAL_send(uint8_t *data, int len, bool debug) printf("\n"); } - return uart_write_bytes(UART_NUM_1, (const char *)data, len); + if (written == 0) { + return ESP_FAIL; + } + + return uart_wait_tx_done(UART_NUM_1, 1000 / portTICK_PERIOD_MS); } /// @brief waits for a serial response from the device