Skip to content

Commit

Permalink
fix: dont enable UART receiver before actually calling ReceiveData (#446
Browse files Browse the repository at this point in the history
)

* fix: dont enable UART receiver before actually calling ReceiveData

* chore: add missing include
  • Loading branch information
daantimmer authored Oct 31, 2024
1 parent 66d25bc commit 1d0b6a2
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 21 deletions.
16 changes: 12 additions & 4 deletions hal_st/stm32fxxx/UartStm.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
#include "hal_st/stm32fxxx/UartStm.hpp"
#include "generated/stm32fxxx/PeripheralTable.hpp"
#include "hal_st/stm32fxxx/GpioStm.hpp"
#include "infra/event/EventDispatcher.hpp"
#include "infra/util/BoundedVector.hpp"
#include "infra/util/ByteRange.hpp"
#include "infra/util/Function.hpp"
#include "infra/util/MemoryRange.hpp"
#include "infra/util/ReallyAssert.hpp"
#include <cstdint>

#include DEVICE_HEADER

namespace hal
{
Expand Down Expand Up @@ -62,7 +69,7 @@ namespace hal
uartHandle.Init.WordLength = config.parity == USART_PARITY_NONE ? USART_WORDLENGTH_8B : USART_WORDLENGTH_9B;
uartHandle.Init.StopBits = USART_STOPBITS_1;
uartHandle.Init.Parity = config.parity;
uartHandle.Init.Mode = USART_MODE_TX_RX;
uartHandle.Init.Mode = USART_MODE_TX;
uartHandle.Init.HwFlowCtl = hasFlowControl ? UART_HWCONTROL_RTS_CTS : UART_HWCONTROL_NONE;
#if defined(UART_ONE_BIT_SAMPLE_ENABLE)
uartHandle.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_ENABLE;
Expand Down Expand Up @@ -112,9 +119,9 @@ namespace hal
this->dataReceived = dataReceived;

if (dataReceived == nullptr)
uartArray[uartIndex]->CR1 &= ~USART_CR1_RXNEIE;
uartArray[uartIndex]->CR1 &= ~(USART_CR1_RE | USART_CR1_RXNEIE);
else
uartArray[uartIndex]->CR1 |= USART_CR1_RXNEIE;
uartArray[uartIndex]->CR1 |= USART_CR1_RE | USART_CR1_RXNEIE;
}

void UartStm::RegisterInterrupt(const Config& config)
Expand All @@ -124,8 +131,9 @@ namespace hal

void UartStm::TransferComplete()
{
infra::EventDispatcher::Instance().Schedule(transferDataComplete);
infra::Function<void()> callback = transferDataComplete;
transferDataComplete = nullptr;
infra::EventDispatcher::Instance().Schedule(callback);
}

void UartStm::Invoke()
Expand Down
4 changes: 4 additions & 0 deletions hal_st/stm32fxxx/UartStm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#include "hal/interfaces/SerialCommunication.hpp"
#include "hal_st/cortex/InterruptCortex.hpp"
#include "hal_st/stm32fxxx/GpioStm.hpp"
#include "infra/util/ByteRange.hpp"
#include "infra/util/Function.hpp"
#include "infra/util/MemoryRange.hpp"
#include <cstdint>

namespace hal
{
Expand Down
20 changes: 8 additions & 12 deletions hal_st/stm32fxxx/UartStmDma.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
#include "hal_st/stm32fxxx/UartStmDma.hpp"
#include "generated/stm32fxxx/PeripheralTable.hpp"
#include "hal_st/stm32fxxx/DmaStm.hpp"
#include "hal_st/stm32fxxx/GpioStm.hpp"
#include "hal_st/stm32fxxx/UartStm.hpp"
#include "infra/event/EventDispatcher.hpp"
#include "infra/util/Function.hpp"
#include "infra/util/MemoryRange.hpp"
#include <cstdint>

#include DEVICE_HEADER

namespace hal
{
Expand Down Expand Up @@ -57,17 +64,6 @@ namespace hal
infra::EventDispatcher::Instance().Schedule(actionOnCompletion);
}

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

#if defined(STM32F4) || defined(STM32G4)
uartArray[uartIndex]->CR1 |= USART_IT_RXNE & USART_IT_MASK;
#else
uartArray[uartIndex]->CR1 |= 1 << (USART_IT_RXNE & USART_IT_MASK);
#endif
}

void UartStmDma::TransferComplete()
{
if (transferDataComplete)
Expand Down
5 changes: 3 additions & 2 deletions hal_st/stm32fxxx/UartStmDma.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#ifndef HAL_UART_STM_DMA_HPP
#define HAL_UART_STM_DMA_HPP

#include "generated/stm32fxxx/PeripheralTable.hpp"
#include "hal_st/stm32fxxx/DmaStm.hpp"
#include "hal_st/stm32fxxx/GpioStm.hpp"
#include "hal_st/stm32fxxx/UartStm.hpp"
#include "infra/util/Function.hpp"
#include "infra/util/MemoryRange.hpp"
#include <cstdint>

namespace hal
Expand All @@ -22,7 +24,6 @@ namespace hal
#endif

void SendData(infra::MemoryRange<const uint8_t> data, infra::Function<void()> actionOnCompletion = infra::emptyFunction) override;
void ReceiveData(infra::Function<void(infra::ConstByteRange data)> dataReceived) override;

private:
void TransferComplete();
Expand Down
18 changes: 15 additions & 3 deletions hal_st/stm32fxxx/UartStmDuplexDma.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
#include "hal_st/stm32fxxx/UartStmDuplexDma.hpp"
#include "generated/stm32fxxx/PeripheralTable.hpp"
#include "infra/event/EventDispatcher.hpp"
#include "hal_st/stm32fxxx/DmaStm.hpp"
#include "hal_st/stm32fxxx/GpioStm.hpp"
#include "hal_st/stm32fxxx/UartStmDma.hpp"
#include "infra/util/ByteRange.hpp"
#include "infra/util/Function.hpp"
#include "infra/util/MemoryRange.hpp"
#include "infra/util/ReallyAssert.hpp"
#include <cstddef>
#include <cstdint>

#include DEVICE_HEADER

namespace hal
{
Expand Down Expand Up @@ -64,14 +73,17 @@ namespace hal
this->dataReceived = dataReceived;

if (dataReceived == nullptr)
{
uartArray[uartIndex]->CR1 &= ~USART_CR1_RE;
receiveDmaChannel.StopTransfer();
}
else
{
receiveDmaChannel.StartReceive(rxBuffer);

uartArray[uartIndex]->CR2 |= USART_CR2_RTOEN;
uartArray[uartIndex]->CR1 |= USART_CR1_RE | USART_CR1_RTOIE;
uartArray[uartIndex]->RTOR = defaultRxTimeout;
uartArray[uartIndex]->CR1 |= USART_CR1_RE | USART_CR1_RTOIE;
}
}

Expand Down
9 changes: 9 additions & 0 deletions hal_st/stm32fxxx/UartStmDuplexDma.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
#ifndef HAL_UART_STM_DUPLEX_DMA_HPP
#define HAL_UART_STM_DUPLEX_DMA_HPP

#include "generated/stm32fxxx/PeripheralTable.hpp"
#include "hal_st/stm32fxxx/DmaStm.hpp"
#include "hal_st/stm32fxxx/GpioStm.hpp"
#include "hal_st/stm32fxxx/UartStm.hpp"
#include "hal_st/stm32fxxx/UartStmDma.hpp"
#include "infra/util/ByteRange.hpp"
#include "infra/util/Function.hpp"
#include "infra/util/MemoryRange.hpp"
#include "infra/util/WithStorage.hpp"
#include <array>
#include <atomic>
#include <cstddef>
#include <cstdint>

namespace hal
Expand Down

0 comments on commit 1d0b6a2

Please sign in to comment.