From 355f209dba8394c9efbd0cad55712efd6ba6b32c Mon Sep 17 00:00:00 2001 From: Konstantin Kondrashov Date: Fri, 17 May 2019 21:29:45 +0800 Subject: [PATCH] uart: Fix ticks_to_wait when 0 or expired Closes: https://github.com/espressif/esp-idf/issues/3301 Closes: IDFGH-964 --- components/driver/uart.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/components/driver/uart.c b/components/driver/uart.c index 411279e223..806998820c 100644 --- a/components/driver/uart.c +++ b/components/driver/uart.c @@ -1040,20 +1040,25 @@ esp_err_t uart_wait_tx_done(uart_port_t uart_num, TickType_t ticks_to_wait) UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error", ESP_FAIL); UART_CHECK((p_uart_obj[uart_num]), "uart driver error", ESP_FAIL); BaseType_t res; - portTickType ticks_end = xTaskGetTickCount() + ticks_to_wait; + portTickType ticks_start = xTaskGetTickCount(); //Take tx_mux res = xSemaphoreTake(p_uart_obj[uart_num]->tx_mux, (portTickType)ticks_to_wait); if(res == pdFALSE) { return ESP_ERR_TIMEOUT; } - ticks_to_wait = ticks_end - xTaskGetTickCount(); xSemaphoreTake(p_uart_obj[uart_num]->tx_done_sem, 0); - ticks_to_wait = ticks_end - xTaskGetTickCount(); if(UART[uart_num]->status.txfifo_cnt == 0) { xSemaphoreGive(p_uart_obj[uart_num]->tx_mux); return ESP_OK; } uart_enable_intr_mask(uart_num, UART_TX_DONE_INT_ENA_M); + + TickType_t ticks_end = xTaskGetTickCount(); + if (ticks_end - ticks_start > ticks_to_wait) { + ticks_to_wait = 0; + } else { + ticks_to_wait = ticks_to_wait - (ticks_end - ticks_start); + } //take 2nd tx_done_sem, wait given from ISR res = xSemaphoreTake(p_uart_obj[uart_num]->tx_done_sem, (portTickType)ticks_to_wait); if(res == pdFALSE) {