halx
読み取り中…
検索中…
一致する文字列を見つけられません
uart.hpp
[詳解]
1#pragma once
2
3#include "uart/common.hpp"
4
5#ifdef HAL_UART_MODULE_ENABLED
6#include "uart/uart_dma.hpp"
7#include "uart/uart_it.hpp"
8#include "uart/uart_poll.hpp"
9#endif
10
11namespace halx::peripheral {
12
13#ifdef HAL_UART_MODULE_ENABLED
14
15template <UART_HandleTypeDef *Handle, UartType TxType> class UartTx;
16
17template <UART_HandleTypeDef *Handle> class UartTx<Handle, UartType::POLL> {
18public:
19 bool transmit(const uint8_t *data, size_t size, uint32_t timeout) {
20 return tx_.transmit(data, size, timeout);
21 }
22
23private:
24 UartTxPoll<Handle> tx_;
25};
26
27template <UART_HandleTypeDef *Handle> class UartTx<Handle, UartType::IT> {
28public:
29 bool transmit(const uint8_t *data, size_t size, uint32_t timeout) {
30 return tx_.transmit(data, size, timeout);
31 }
32
33private:
34 UartTxIt<Handle> tx_;
35};
36
37template <UART_HandleTypeDef *Handle> class UartTx<Handle, UartType::DMA> {
38public:
39 bool transmit(const uint8_t *data, size_t size, uint32_t timeout) {
40 return tx_.transmit(data, size, timeout);
41 }
42
43private:
44 UartTxDma<Handle> tx_;
45};
46
47template <UART_HandleTypeDef *Handle, UartType RxType> class UartRx;
48
49template <UART_HandleTypeDef *Handle> class UartRx<Handle, UartType::POLL> {
50public:
51 UartRx(size_t buf_size) : rx_{buf_size} {}
52 bool receive(uint8_t *data, size_t size, uint32_t timeout) {
53 return rx_.receive(data, size, timeout);
54 }
55 void flush() { rx_.flush(); }
56 size_t available() const { return rx_.available(); }
57
58private:
59 UartRxPoll<Handle> rx_;
60};
61
62template <UART_HandleTypeDef *Handle> class UartRx<Handle, UartType::IT> {
63public:
64 UartRx(size_t buf_size) : rx_{buf_size} {}
65 bool receive(uint8_t *data, size_t size, uint32_t timeout) {
66 return rx_.receive(data, size, timeout);
67 }
68 void flush() { rx_.flush(); }
69 size_t available() const { return rx_.available(); }
70
71private:
72 UartRxIt<Handle> rx_;
73};
74
75template <UART_HandleTypeDef *Handle> class UartRx<Handle, UartType::DMA> {
76public:
77 UartRx(size_t buf_size) : rx_{buf_size} {}
78 bool receive(uint8_t *data, size_t size, uint32_t timeout) {
79 return rx_.receive(data, size, timeout);
80 }
81 void flush() { rx_.flush(); }
82 size_t available() const { return rx_.available(); }
83
84private:
85 UartRxDma<Handle> rx_;
86};
87
88template <UART_HandleTypeDef *Handle, UartType TxType = UartType::IT,
89 UartType RxType = UartType::IT>
90class Uart : public UartBase {
91public:
92 Uart(size_t rx_buf_size = 64) : rx_{rx_buf_size} {}
93 bool transmit(const uint8_t *data, size_t size, uint32_t timeout) {
94 return tx_.transmit(data, size, timeout);
95 }
96 bool receive(uint8_t *data, size_t size, uint32_t timeout) {
97 return rx_.receive(data, size, timeout);
98 }
99 void flush() { rx_.flush(); }
100 size_t available() const { return rx_.available(); }
101
102private:
103 UartTx<Handle, TxType> tx_;
104 UartRx<Handle, RxType> rx_;
105};
106
107#endif
108
109} // namespace halx::peripheral
Definition common.hpp:47
Definition can.hpp:13
UartType
Definition common.hpp:8
@ DMA
Definition common.hpp:11
@ POLL
Definition common.hpp:9
@ IT
Definition common.hpp:10