From 63a0b2ffa9f8f54cc35074fcc3f05e8f13cec8d3 Mon Sep 17 00:00:00 2001 From: Technobly Date: Mon, 15 Jul 2024 20:47:29 -0500 Subject: [PATCH 1/2] [rtl872x] fixes DMA enabled USART flush --- hal/src/rtl872x/usart_hal.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/hal/src/rtl872x/usart_hal.cpp b/hal/src/rtl872x/usart_hal.cpp index acd31451d7..20fca93b6c 100644 --- a/hal/src/rtl872x/usart_hal.cpp +++ b/hal/src/rtl872x/usart_hal.cpp @@ -288,6 +288,7 @@ class Usart { txBuffer_.reset(); curTxCount_ = 0; transmitting_ = false; + busy_ = false; receiving_ = false; state_ = HAL_USART_STATE_DISABLED; return SYSTEM_ERROR_NONE; @@ -300,6 +301,7 @@ class Usart { dataInFlight(true /* commit */); CHECK(disable(false)); transmitting_ = false; + busy_ = false; receiving_ = 0; rxBuffer_.prune(); txBuffer_.reset(); @@ -344,7 +346,7 @@ class Usart { } { AtomicBlock atomic(this); - if (!isEnabled() || txBuffer_.empty()) { + if ((!isEnabled() || txBuffer_.empty()) && !busy_) { break; } } @@ -572,6 +574,7 @@ class Usart { } else { UART_TXDMACmd(uartInstance, ENABLE); BaseType_t yield = pdFALSE; + uart->busy_ = false; if (xEventGroupSetBitsFromISR(uart->evGroup_, HAL_USART_PVT_EVENT_WRITABLE, &yield) != pdFAIL) { portYIELD_FROM_ISR(yield); } @@ -618,6 +621,7 @@ class Usart { curTxCount_(0), state_(HAL_USART_STATE_DISABLED), transmitting_(false), + busy_(false), receiving_(false), configured_(false), index_(index), @@ -831,12 +835,15 @@ class Usart { curTxCount_ = consumable; GDMA_Init(txDmaInitStruct_.GDMA_Index, txDmaInitStruct_.GDMA_ChNum, &txDmaInitStruct_); GDMA_Cmd(txDmaInitStruct_.GDMA_Index, txDmaInitStruct_.GDMA_ChNum, ENABLE); + busy_ = true; + UART_INTConfig(uartInstance, RUART_IER_ETBEI, ENABLE); } else { // LOG UART doesn't support DMA transmission consumable = std::min(MAX_UART_FIFO_SIZE, consumable); auto ptr = txBuffer_.consume(consumable); for (size_t i = 0; i < consumable; i++, ptr++) { UART_CharPut(uartInstance, *ptr); + UART_INTConfig(uartInstance, RUART_IER_ETBEI, ENABLE); } curTxCount_ = consumable; UART_INTConfig(uartInstance, RUART_IER_ETBEI, ENABLE); @@ -982,6 +989,7 @@ class Usart { volatile hal_usart_state_t state_; volatile bool transmitting_; + volatile bool busy_; volatile bool receiving_; bool configured_; From 81e85fce0a252fae5b695d8a7eb102344faaf89a Mon Sep 17 00:00:00 2001 From: Technobly Date: Tue, 16 Jul 2024 12:22:11 -0500 Subject: [PATCH 2/2] review comments --- hal/src/rtl872x/usart_hal.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/hal/src/rtl872x/usart_hal.cpp b/hal/src/rtl872x/usart_hal.cpp index 20fca93b6c..54354a9516 100644 --- a/hal/src/rtl872x/usart_hal.cpp +++ b/hal/src/rtl872x/usart_hal.cpp @@ -341,7 +341,7 @@ class Usart { ssize_t flush() { CHECK_TRUE(isEnabled(), SYSTEM_ERROR_INVALID_STATE); while (true) { - while (transmitting_) { + while (transmitting_ || busy_) { // FIXME: busy loop } { @@ -472,6 +472,7 @@ class Usart { if (event & HAL_USART_PVT_EVENT_WRITABLE) { if (space() <= 0) { TxLock lk(this); + xEventGroupClearBits(evGroup_, HAL_USART_PVT_EVENT_WRITABLE); UART_INTConfig(uartInstance, RUART_IER_ETBEI, ENABLE); // Temporarily disable TX DMA, otherwise, the TX FIFO won't be empty until all data is transferred by DMA. UART_TXDMACmd(uartInstance, DISABLE); @@ -574,7 +575,9 @@ class Usart { } else { UART_TXDMACmd(uartInstance, ENABLE); BaseType_t yield = pdFALSE; - uart->busy_ = false; + if (!uart->transmitting_) { + uart->busy_ = false; // All bytes sent if no new DMA transfers are in progress + } if (xEventGroupSetBitsFromISR(uart->evGroup_, HAL_USART_PVT_EVENT_WRITABLE, &yield) != pdFAIL) { portYIELD_FROM_ISR(yield); } @@ -843,7 +846,6 @@ class Usart { auto ptr = txBuffer_.consume(consumable); for (size_t i = 0; i < consumable; i++, ptr++) { UART_CharPut(uartInstance, *ptr); - UART_INTConfig(uartInstance, RUART_IER_ETBEI, ENABLE); } curTxCount_ = consumable; UART_INTConfig(uartInstance, RUART_IER_ETBEI, ENABLE);