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: lpUart peripheral table was not used correctly with UartStmDma #430

Merged
merged 3 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 1 addition & 2 deletions hal_st/stm32fxxx/UartStm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ namespace hal
protected:
uint8_t uartIndex;
infra::Function<void(infra::ConstByteRange data)> dataReceived;
infra::MemoryRange<USART_TypeDef* const> uartArray;

private:
void UartStmHalInit(const Config& config, bool hasFlowControl);
Expand All @@ -76,8 +77,6 @@ namespace hal

infra::MemoryRange<const uint8_t> sendData;
bool sending = false;

infra::MemoryRange<USART_TypeDef* const> uartArray;
infra::MemoryRange<IRQn_Type const> uartIrqArray;
};
}
Expand Down
34 changes: 11 additions & 23 deletions hal_st/stm32fxxx/UartStmDma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,51 @@

namespace hal
{
namespace
{
volatile void* TransmitRegister(uint8_t uartIndex)
{
#if defined(USART_TDR_TDR)
return &peripheralUart[uartIndex]->TDR;
#else
return &peripheralUart[uartIndex]->DR;
#endif
}
}

UartStmDma::UartStmDma(DmaStm::TransmitStream& transmitStream, uint8_t oneBasedIndex, GpioPinStm& uartTx, GpioPinStm& uartRx, const Config& config)
: UartStm(oneBasedIndex, uartTx, uartRx, config)
, transmitDmaChannel{ transmitStream, TransmitRegister(uartIndex), 1, [this]
, transmitDmaChannel{ transmitStream, transmitRegister, 1, [this]
{
TransferComplete();
} }
{
peripheralUart[uartIndex]->CR3 |= USART_CR3_DMAT;
uartArray[uartIndex]->CR3 |= USART_CR3_DMAT;
}

UartStmDma::UartStmDma(DmaStm::TransmitStream& transmitStream, uint8_t oneBasedIndex, GpioPinStm& uartTx, GpioPinStm& uartRx, GpioPinStm& uartRts, GpioPinStm& uartCts, const Config& config)
: UartStm(oneBasedIndex, uartTx, uartRx, uartRts, uartCts, config)
, transmitDmaChannel{ transmitStream, TransmitRegister(uartIndex), 1, [this]
, transmitDmaChannel{ transmitStream, transmitRegister, 1, [this]
{
TransferComplete();
} }
{
peripheralUart[uartIndex]->CR3 |= USART_CR3_DMAT;
uartArray[uartIndex]->CR3 |= USART_CR3_DMAT;
}

#if defined(HAS_PERIPHERAL_LPUART)
UartStmDma::UartStmDma(DmaStm::TransmitStream& transmitStream, uint8_t oneBasedIndex, GpioPinStm& uartTx, GpioPinStm& uartRx, LpUart lpUart, const Config& config)
: UartStm(oneBasedIndex, uartTx, uartRx, lpUart, config)
, transmitDmaChannel{ transmitStream, TransmitRegister(uartIndex), 1, [this]
, transmitDmaChannel{ transmitStream, transmitRegister, 1, [this]
{
TransferComplete();
} }
{
peripheralUart[uartIndex]->CR3 |= USART_CR3_DMAT;
uartArray[uartIndex]->CR3 |= USART_CR3_DMAT;
}

UartStmDma::UartStmDma(DmaStm::TransmitStream& transmitStream, uint8_t oneBasedIndex, GpioPinStm& uartTx, GpioPinStm& uartRx, GpioPinStm& uartRts, GpioPinStm& uartCts, LpUart lpUart, const Config& config)
: UartStm(oneBasedIndex, uartTx, uartRx, uartRts, uartCts, lpUart, config)
, transmitDmaChannel{ transmitStream, TransmitRegister(uartIndex), 1, [this]
, transmitDmaChannel{ transmitStream, transmitRegister, 1, [this]
{
TransferComplete();
} }
{
peripheralUart[uartIndex]->CR3 |= USART_CR3_DMAT;
uartArray[uartIndex]->CR3 |= USART_CR3_DMAT;
}
#endif

UartStmDma::~UartStmDma()
{
peripheralUart[uartIndex]->CR1 &= ~(USART_CR1_TE | USART_CR1_RE);
uartArray[uartIndex]->CR1 &= ~(USART_CR1_TE | USART_CR1_RE);
DisableClockUart(uartIndex);
}

Expand All @@ -80,9 +68,9 @@ namespace hal
this->dataReceived = dataReceived;

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

Expand Down
6 changes: 6 additions & 0 deletions hal_st/stm32fxxx/UartStmDma.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ namespace hal
private:
void TransferComplete();

volatile void* transmitRegister =
#if defined(USART_TDR_TDR)
&uartArray[uartIndex]->TDR;
#else
&uartArray[uartIndex]->DR;
#endif
TransmitDmaChannel transmitDmaChannel;
infra::Function<void()> transferDataComplete;
};
Expand Down
33 changes: 12 additions & 21 deletions hal_st/stm32fxxx/UartStmDuplexDma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,12 @@ namespace hal
namespace
{
uint32_t defaultRxTimeout = 16;

volatile void* ReceiveRegister(uint8_t uartIndex)
{
#if defined(USART_RDR_RDR)
return &peripheralUart[uartIndex]->RDR;
#else
return &peripheralUart[uartIndex]->DR;
#endif
}
}

UartStmDuplexDma::UartStmDuplexDma(infra::MemoryRange<uint8_t> rxBuffer, hal::DmaStm::TransmitStream& transmitStream, hal::DmaStm::ReceiveStream& receiveStream, uint8_t oneBasedIndex, GpioPinStm& uartTx, GpioPinStm& uartRx, const Config& config)
: UartStmDma(transmitStream, oneBasedIndex, uartTx, uartRx, config)
, rxBuffer{ rxBuffer }
, receiveDmaChannel{ receiveStream, ReceiveRegister(uartIndex), 1, [this]
, receiveDmaChannel{ receiveStream, receiveRegister, 1, [this]
{
HalfReceiveComplete();
},
Expand All @@ -30,13 +21,13 @@ namespace hal
FullReceiveComplete();
} }
{
peripheralUart[uartIndex]->CR3 |= USART_CR3_DMAT | USART_CR3_DMAR;
uartArray[uartIndex]->CR3 |= USART_CR3_DMAT | USART_CR3_DMAR;
}

UartStmDuplexDma::UartStmDuplexDma(infra::MemoryRange<uint8_t> rxBuffer, hal::DmaStm::TransmitStream& transmitStream, hal::DmaStm::ReceiveStream& receiveStream, uint8_t oneBasedIndex, GpioPinStm& uartTx, GpioPinStm& uartRx, GpioPinStm& uartRts, GpioPinStm& uartCts, const Config& config)
: UartStmDma(transmitStream, oneBasedIndex, uartTx, uartRx, uartRts, uartCts, config)
, rxBuffer{ rxBuffer }
, receiveDmaChannel{ receiveStream, ReceiveRegister(uartIndex), 1, [this]
, receiveDmaChannel{ receiveStream, receiveRegister, 1, [this]
{
HalfReceiveComplete();
},
Expand All @@ -45,13 +36,13 @@ namespace hal
FullReceiveComplete();
} }
{
peripheralUart[uartIndex]->CR3 |= USART_CR3_DMAT | USART_CR3_DMAR;
uartArray[uartIndex]->CR3 |= USART_CR3_DMAT | USART_CR3_DMAR;
}

UartStmDuplexDma::UartStmDuplexDma(infra::MemoryRange<uint8_t> rxBuffer, hal::DmaStm::TransmitStream& transmitStream, hal::DmaStm::ReceiveStream& receiveStream, uint8_t oneBasedIndex, GpioPinStm& uartTx, GpioPinStm& uartRx, GpioPinStm& uartRts, GpioPinStm& uartCts, const Config& config, bool hasFlowControl)
: UartStmDma(transmitStream, oneBasedIndex, uartTx, uartRx, uartRts, uartCts, config)
, rxBuffer{ rxBuffer }
, receiveDmaChannel{ receiveStream, ReceiveRegister(uartIndex), 1, [this]
, receiveDmaChannel{ receiveStream, receiveRegister, 1, [this]
{
HalfReceiveComplete();
},
Expand All @@ -60,13 +51,13 @@ namespace hal
FullReceiveComplete();
} }
{
peripheralUart[uartIndex]->CR3 |= USART_CR3_DMAT | USART_CR3_DMAR;
uartArray[uartIndex]->CR3 |= USART_CR3_DMAT | USART_CR3_DMAR;
}

UartStmDuplexDma::~UartStmDuplexDma()
{
receiveDmaChannel.StopTransfer();
peripheralUart[uartIndex]->CR1 &= ~(USART_CR1_TE | USART_CR1_RE);
uartArray[uartIndex]->CR1 &= ~(USART_CR1_TE | USART_CR1_RE);
DisableClockUart(uartIndex);
}

Expand All @@ -80,9 +71,9 @@ namespace hal
{
receiveDmaChannel.StartReceive(rxBuffer);

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

Expand Down Expand Up @@ -112,9 +103,9 @@ namespace hal

void UartStmDuplexDma::Invoke()
{
if (peripheralUart[uartIndex]->ISR & USART_ISR_RTOF)
if (uartArray[uartIndex]->ISR & USART_ISR_RTOF)
{
peripheralUart[uartIndex]->ICR = USART_ICR_RTOCF;
uartArray[uartIndex]->ICR = USART_ICR_RTOCF;

const auto receivedSize = receiveDmaChannel.ReceivedSize();

Expand Down
6 changes: 6 additions & 0 deletions hal_st/stm32fxxx/UartStmDuplexDma.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ namespace hal
void Invoke() override;

private:
volatile void* receiveRegister =
#if defined(USART_RDR_RDR)
&peripheralUart[uartIndex]->RDR;
#else
&peripheralUart[uartIndex]->DR;
#endif
infra::MemoryRange<uint8_t> rxBuffer;
hal::CircularReceiveDmaChannel receiveDmaChannel;
std::atomic<size_t> lastReceivedPosition{};
Expand Down
Loading