-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathUartStmDuplexDma.hpp
67 lines (54 loc) · 2.38 KB
/
UartStmDuplexDma.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#ifndef HAL_UART_STM_DUPLEX_DMA_HPP
#define HAL_UART_STM_DUPLEX_DMA_HPP
#include "hal/interfaces/SerialCommunication.hpp"
#include "hal_st/cortex/InterruptCortex.hpp"
#include "hal_st/stm32fxxx/DmaStm.hpp"
#include "hal_st/stm32fxxx/GpioStm.hpp"
#include <atomic>
namespace hal
{
class UartStmDuplexDma
: public SerialCommunication
, private InterruptHandler
{
public:
struct Config
{
constexpr Config()
{}
uint32_t baudrate = 115200;
uint32_t hwFlowControl = UART_HWCONTROL_NONE;
infra::Optional<DmaChannelId> dmaChannelTx;
infra::Optional<DmaChannelId> dmaChannelRx;
infra::Optional<InterruptPriority> priority;
};
template<std::size_t RxBufferSize>
using WithRxBuffer = infra::WithStorage<UartStmDuplexDma, std::array<uint8_t, RxBufferSize>>;
UartStmDuplexDma(infra::MemoryRange<uint8_t> rxBuffer, hal::DmaStm& dmaStm, uint8_t uartIndex, GpioPinStm& uartTx, GpioPinStm& uartRx, const Config& config = Config());
UartStmDuplexDma(infra::MemoryRange<uint8_t> rxBuffer, hal::DmaStm& dmaStm, uint8_t uartIndex, GpioPinStm& uartTx, GpioPinStm& uartRx, GpioPinStm& uartRts, GpioPinStm& uartCts, const Config& config = Config());
~UartStmDuplexDma();
virtual void SendData(infra::MemoryRange<const uint8_t> data, infra::Function<void()> actionOnCompletion = infra::emptyFunction) override;
virtual void ReceiveData(infra::Function<void(infra::ConstByteRange data)> dataReceived) override;
private:
void Configure(const Config& config);
void ReceiveComplete(size_t currentPosition);
void RegisterInterrupt(const Config& config);
void TransferComplete();
virtual void Invoke() override;
private:
infra::MemoryRange<uint8_t> rxBuffer;
uint8_t uartIndex;
PeripheralPinStm uartTx;
PeripheralPinStm uartRx;
infra::Optional<PeripheralPinStm> uartRts;
infra::Optional<PeripheralPinStm> uartCts;
UART_HandleTypeDef uartHandle = {};
hal::DmaStm& dma;
DmaStm::Stream transmitDmaChannel;
DmaStm::CircularStream receiveDmaChannel;
infra::Function<void()> transferDataComplete;
infra::Function<void(infra::ConstByteRange data)> dataReceived;
std::atomic<size_t> lastReceivedPosition{};
};
}
#endif