Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rtl872x] fixes DMA enabled USART flush #2800

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions hal/src/rtl872x/usart_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -300,6 +301,7 @@ class Usart {
dataInFlight(true /* commit */);
CHECK(disable(false));
transmitting_ = false;
busy_ = false;
receiving_ = 0;
rxBuffer_.prune();
txBuffer_.reset();
Expand Down Expand Up @@ -339,12 +341,12 @@ class Usart {
ssize_t flush() {
CHECK_TRUE(isEnabled(), SYSTEM_ERROR_INVALID_STATE);
while (true) {
while (transmitting_) {
while (transmitting_ || busy_) {
// FIXME: busy loop
}
{
AtomicBlock atomic(this);
if (!isEnabled() || txBuffer_.empty()) {
if ((!isEnabled() || txBuffer_.empty()) && !busy_) {
technobly marked this conversation as resolved.
Show resolved Hide resolved
break;
}
}
Expand Down Expand Up @@ -470,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);
Expand Down Expand Up @@ -572,6 +575,9 @@ class Usart {
} else {
UART_TXDMACmd(uartInstance, ENABLE);
BaseType_t yield = pdFALSE;
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);
}
Expand Down Expand Up @@ -618,6 +624,7 @@ class Usart {
curTxCount_(0),
state_(HAL_USART_STATE_DISABLED),
transmitting_(false),
busy_(false),
receiving_(false),
configured_(false),
index_(index),
Expand Down Expand Up @@ -831,6 +838,8 @@ 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);
Expand Down Expand Up @@ -982,6 +991,7 @@ class Usart {

volatile hal_usart_state_t state_;
volatile bool transmitting_;
volatile bool busy_;
volatile bool receiving_;
bool configured_;

Expand Down