-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathSynchronousUartStm.hpp
95 lines (79 loc) · 3.47 KB
/
SynchronousUartStm.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#ifndef SYNCHRONOUS_HAL_SYNCHRONOUS_UART_STM_HPP
#define SYNCHRONOUS_HAL_SYNCHRONOUS_UART_STM_HPP
#include "hal/synchronous_interfaces/SynchronousSerialCommunication.hpp"
#include "hal/synchronous_interfaces/TimeKeeper.hpp"
#include "hal_st/cortex/InterruptCortex.hpp"
#include "hal_st/stm32fxxx/GpioStm.hpp"
#include "infra/util/WithStorage.hpp"
#include <atomic>
namespace hal
{
#if defined(STM32WB)
struct SyncLpUart
{};
extern const SyncLpUart syncLpUart;
#endif
class SynchronousUartStm
: public SynchronousSerialCommunication
, private InterruptHandler
{
public:
enum HwFlowControl : uint32_t
{
hwControlDisable = UART_HWCONTROL_NONE,
hwControlRtsEnable = UART_HWCONTROL_RTS,
hwControlCtsEnable = UART_HWCONTROL_CTS,
hwControlRtsCtsEnable = UART_HWCONTROL_RTS_CTS
};
template<std::size_t Size>
using WithStorage = infra::WithStorage<SynchronousUartStm, std::array<uint8_t, Size>>;
SynchronousUartStm(infra::ByteRange readBuffer, uint8_t aUartIndex, GpioPinStm& uartTx, GpioPinStm& uartRx, TimeKeeper& timeKeeper, uint32_t baudrate = 115200);
SynchronousUartStm(infra::ByteRange readBuffer, uint8_t aUartIndex, GpioPinStm& uartTx, GpioPinStm& uartRx, GpioPinStm& uartRts, GpioPinStm& uartCts, TimeKeeper& timeKeeper,
HwFlowControl flowControl = HwFlowControl::hwControlRtsCtsEnable, uint32_t baudrate = 115200);
~SynchronousUartStm();
void SendData(infra::ConstByteRange data) override;
bool ReceiveData(infra::ByteRange data) override;
private:
virtual void Invoke() override;
bool Full() const;
bool Empty() const;
private:
uint8_t uartIndex;
PeripheralPinStm uartTx;
PeripheralPinStm uartRx;
infra::Optional<PeripheralPinStm> uartRts;
infra::Optional<PeripheralPinStm> uartCts;
TimeKeeper& timeKeeper;
infra::ByteRange readBuffer;
std::atomic<uint8_t*> contentsBegin;
std::atomic<uint8_t*> contentsEnd;
};
class SynchronousUartStmSendOnly
: public SynchronousSerialCommunication
{
public:
enum HwFlowControl : uint32_t
{
hwControlDisable = UART_HWCONTROL_NONE,
hwControlRtsEnable = UART_HWCONTROL_RTS,
hwControlCtsEnable = UART_HWCONTROL_CTS,
hwControlRtsCtsEnable = UART_HWCONTROL_RTS_CTS
};
SynchronousUartStmSendOnly(uint8_t aUartIndex, GpioPinStm& uartTx, uint32_t baudrate = 115200);
SynchronousUartStmSendOnly(uint8_t aUartIndex, GpioPinStm& uartTx, GpioPinStm& uartRts,
HwFlowControl flowControl = HwFlowControl::hwControlRtsCtsEnable, uint32_t baudrate = 115200);
#if defined(STM32WB)
SynchronousUartStmSendOnly(uint8_t aUartIndex, GpioPinStm& uartTx, SyncLpUart lpUart, uint32_t baudrate = 115200);
SynchronousUartStmSendOnly(uint8_t aUartIndex, GpioPinStm& uartTx, GpioPinStm& uartRts, SyncLpUart lpUart, HwFlowControl flowControl = HwFlowControl::hwControlRtsCtsEnable, uint32_t baudrate = 115200);
#endif
~SynchronousUartStmSendOnly();
void SendData(infra::ConstByteRange data) override;
bool ReceiveData(infra::ByteRange data) override;
private:
void UartStmHalInit(HwFlowControl flowControl, uint32_t baudrate);
USART_TypeDef* const uartBase;
PeripheralPinStm uartTx;
infra::Optional<PeripheralPinStm> uartRts;
};
}
#endif