Skip to content

Commit b9d933d

Browse files
xiongyumailMr-Pi
authored andcommitted
feature(uart): uart tx done add ticks to wait
1 parent 46ccadb commit b9d933d

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

components/esp8266/driver/uart.c

+22-7
Original file line numberDiff line numberDiff line change
@@ -249,30 +249,45 @@ esp_err_t uart_get_hw_flow_ctrl(uart_port_t uart_num, uart_hw_flowcontrol_t *flo
249249
return ESP_OK;
250250
}
251251

252-
esp_err_t uart_wait_tx_done(uart_port_t uart_num)
252+
esp_err_t uart_wait_tx_done(uart_port_t uart_num, TickType_t ticks_to_wait)
253253
{
254254
UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error", ESP_ERR_INVALID_ARG);
255-
255+
UART_CHECK((p_uart_obj[uart_num]), "uart driver error", ESP_ERR_INVALID_ARG);
256+
uint32_t baudrate;
256257
uint32_t byte_delay_us = 0;
257-
uart_get_baudrate(uart_num, &byte_delay_us);
258-
byte_delay_us = (uint32_t)(10000000/byte_delay_us); // (1/baudrate)*10*1000_000 us
258+
BaseType_t res;
259+
portTickType ticks_end = xTaskGetTickCount() + ticks_to_wait;
260+
261+
// Take tx_mux
262+
res = xSemaphoreTake(p_uart_obj[uart_num]->tx_mux, (portTickType)ticks_to_wait);
263+
if(res == pdFALSE) {
264+
return ESP_ERR_TIMEOUT;
265+
}
259266

267+
uart_get_baudrate(uart_num, &baudrate);
268+
byte_delay_us = (uint32_t)(10000000 / baudrate); // (1/baudrate)*10*1000_000 us
269+
270+
ticks_to_wait = ticks_end - xTaskGetTickCount();
260271
// wait for tx done sem.
261-
if (pdTRUE == xSemaphoreTake(p_uart_obj[uart_num]->tx_done_sem, (portTickType)portMAX_DELAY)) {
272+
if (pdTRUE == xSemaphoreTake(p_uart_obj[uart_num]->tx_done_sem, ticks_to_wait)) {
262273
while (1) {
263274
if (UART[uart_num]->status.txfifo_cnt == 0) {
264275
ets_delay_us(byte_delay_us); // Delay one byte time to guarantee transmission completion
265276
break;
266277
}
267278
}
279+
} else {
280+
xSemaphoreGive(p_uart_obj[uart_num]->tx_mux);
281+
return ESP_ERR_TIMEOUT;
268282
}
283+
xSemaphoreGive(p_uart_obj[uart_num]->tx_mux);
269284
return ESP_OK;
270285
}
271286

272287
esp_err_t uart_enable_swap(void)
273288
{
274289
// wait for tx done.
275-
uart_wait_tx_done(UART_NUM_0);
290+
uart_wait_tx_done(UART_NUM_0, portMAX_DELAY);
276291

277292
UART_ENTER_CRITICAL();
278293
// MTCK -> UART0_CTS -> U0RXD
@@ -289,7 +304,7 @@ esp_err_t uart_enable_swap(void)
289304
esp_err_t uart_disable_swap(void)
290305
{
291306
// wait for tx done.
292-
uart_wait_tx_done(UART_NUM_0);
307+
uart_wait_tx_done(UART_NUM_0, portMAX_DELAY);
293308

294309
UART_ENTER_CRITICAL();
295310
// disable swap U0TXD <-> UART0_RTS and U0RXD <-> UART0_CTS

components/esp8266/include/driver/uart.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -444,12 +444,13 @@ esp_err_t uart_driver_delete(uart_port_t uart_num);
444444
* @brief Waiting for the last byte of data to be sent
445445
*
446446
* @param uart_num Uart port number.
447+
* @param ticks_to_wait Timeout, count in RTOS ticks
447448
*
448449
* @return
449450
* - ESP_OK Success
450451
* - ESP_ERR_INVALID_ARG Parameter error
451452
*/
452-
esp_err_t uart_wait_tx_done(uart_port_t uart_num);
453+
esp_err_t uart_wait_tx_done(uart_port_t uart_num, TickType_t ticks_to_wait);
453454

454455
/**
455456
* @brief Send data to the UART port from a given buffer and length.

0 commit comments

Comments
 (0)