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

fix(UartStm): use correct registers & fix overrun assert; enable FIFO #68

Merged
merged 3 commits into from
Feb 2, 2023
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
34 changes: 13 additions & 21 deletions hal_st/stm32fxxx/UartStm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@ namespace hal
#else
uartHandle.Init.OverSampling = UART_OVERSAMPLING_8;
#endif

HAL_UART_Init(&uartHandle);

#if defined(STM32WB)
// Enable FIFO. FIFO can only be enabled after 'HAL_UART_Init'; but UART must be disabled first.
peripheralUart[uartIndex]->CR1 &= ~USART_CR1_UE;
peripheralUart[uartIndex]->CR1 |= USART_CR1_FIFOEN | USART_CR1_UE;

peripheralUart[uartIndex]->CR2 &= ~USART_CLOCK_ENABLED;
#endif
}

UartStm::~UartStm()
Expand All @@ -44,28 +51,17 @@ namespace hal
sendData = data;
sending = true;

#if defined(STM32F0) || defined(STM32F1) || defined(STM32F3) || defined(STM32F7) || defined(STM32WB)
peripheralUart[uartIndex]->CR1 |= 1 << (USART_IT_TXE & USART_IT_MASK);
#else
peripheralUart[uartIndex]->CR1 |= USART_IT_TXE & USART_IT_MASK;
#endif
peripheralUart[uartIndex]->CR1 |= USART_CR1_TXEIE;
}

void UartStm::ReceiveData(infra::Function<void(infra::ConstByteRange data)> dataReceived)
{
this->dataReceived = dataReceived;

#if defined(STM32F0) || defined(STM32F1) || defined(STM32F3) || defined(STM32F7) || defined(STM32WB)
if (dataReceived == nullptr)
peripheralUart[uartIndex]->CR1 &= ~(1 << (USART_IT_RXNE & USART_IT_MASK));
peripheralUart[uartIndex]->CR1 &= ~USART_CR1_RXNEIE;
else
peripheralUart[uartIndex]->CR1 |= 1 << (USART_IT_RXNE & USART_IT_MASK);
#else
if (dataReceived == nullptr)
peripheralUart[uartIndex]->CR1 &= ~(USART_IT_RXNE & USART_IT_MASK);
else
peripheralUart[uartIndex]->CR1 |= USART_IT_RXNE & USART_IT_MASK;
#endif
peripheralUart[uartIndex]->CR1 |= USART_CR1_RXNEIE;
}

void UartStm::RegisterInterrupt(const Config& config)
Expand Down Expand Up @@ -109,9 +105,9 @@ namespace hal

// If buffer is empty then interrupt was raised by Overrun Error (ORE) and we miss data.
#if defined(STM32F0) || defined(STM32F3) || defined(STM32F7) || defined(STM32WB)
really_assert(!(buffer.empty() && peripheralUart[uartIndex]->ISR & USART_ISR_ORE));
really_assert(!(peripheralUart[uartIndex]->ISR & USART_ISR_ORE));
#else
really_assert(!(buffer.empty() && peripheralUart[uartIndex]->SR & USART_SR_ORE));
really_assert(!(peripheralUart[uartIndex]->SR & USART_SR_ORE));
#endif

if (dataReceived != nullptr)
Expand All @@ -137,11 +133,7 @@ namespace hal
if (sendData.empty())
{
TransferComplete();
#if defined(STM32F0) || defined(STM32F1) || defined(STM32F3) || defined(STM32F7) || defined(STM32WB)
peripheralUart[uartIndex]->CR1 &= ~(1 << (USART_IT_TXE & USART_IT_MASK));
#else
peripheralUart[uartIndex]->CR1 &= ~(USART_IT_TXE & USART_IT_MASK);
#endif
peripheralUart[uartIndex]->CR1 &= ~USART_CR1_TXEIE;
sending = false;
}
}
Expand Down