uart: Add missing critical section wrappers around rx_buffered_len

The missing barriers caused uart_get_buffered_data_len() to (very rarely)
return a garbage value. When used in MicroPython, though, this caused
select() to return and a subsequent read() to stall indefinitely until
a char was actually available.

Signed-off-by: Chen Yi Qun <chenyiqun@espressif.com>

Closes https://github.com/espressif/esp-idf/issues/6397
Merges https://github.com/espressif/esp-idf/pull/6396
This commit is contained in:
Luca Burelli
2021-01-13 18:42:04 +01:00
committed by Omar Chebib
parent 2c49af9e75
commit e41e67f2f1

View File

@@ -1295,7 +1295,9 @@ esp_err_t uart_get_buffered_data_len(uart_port_t uart_num, size_t *size)
{ {
ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error"); ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
ESP_RETURN_ON_FALSE((p_uart_obj[uart_num]), ESP_FAIL, UART_TAG, "uart driver error"); ESP_RETURN_ON_FALSE((p_uart_obj[uart_num]), ESP_FAIL, UART_TAG, "uart driver error");
UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
*size = p_uart_obj[uart_num]->rx_buffered_len; *size = p_uart_obj[uart_num]->rx_buffered_len;
UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
return ESP_OK; return ESP_OK;
} }
@@ -1336,12 +1338,15 @@ esp_err_t uart_flush_input(uart_port_t uart_num)
} }
data = (uint8_t*) xRingbufferReceive(p_uart->rx_ring_buf, &size, (portTickType) 0); data = (uint8_t*) xRingbufferReceive(p_uart->rx_ring_buf, &size, (portTickType) 0);
if(data == NULL) { if(data == NULL) {
UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
if( p_uart_obj[uart_num]->rx_buffered_len != 0 ) { if( p_uart_obj[uart_num]->rx_buffered_len != 0 ) {
ESP_LOGE(UART_TAG, "rx_buffered_len error");
p_uart_obj[uart_num]->rx_buffered_len = 0; p_uart_obj[uart_num]->rx_buffered_len = 0;
UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
// this must be called outside the critical section
ESP_LOGE(UART_TAG, "rx_buffered_len error");
UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
} }
//We also need to clear the `rx_buffer_full_flg` here. //We also need to clear the `rx_buffer_full_flg` here.
UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
p_uart_obj[uart_num]->rx_buffer_full_flg = false; p_uart_obj[uart_num]->rx_buffer_full_flg = false;
UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock)); UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
break; break;