halx
読み取り中…
検索中…
一致する文字列を見つけられません
uart_it.hpp
[詳解]
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5
6#include "halx/core.hpp"
7
8#include "common.hpp"
9
10namespace halx::peripheral {
11
12template <UART_HandleTypeDef *Handle> class UartTxIt {
13public:
15 stm32cubemx_helper::set_context<Handle, UartTxIt>(this);
16 HAL_UART_RegisterCallback(
17 Handle, HAL_UART_TX_COMPLETE_CB_ID, [](UART_HandleTypeDef *) {
18 auto tx = stm32cubemx_helper::get_context<Handle, UartTxIt>();
19 tx->notifier_.set(0x1);
20 });
21 HAL_UART_RegisterCallback(
22 Handle, HAL_UART_ERROR_CB_ID, [](UART_HandleTypeDef *) {
23 auto tx = stm32cubemx_helper::get_context<Handle, UartTxIt>();
24 tx->notifier_.set(0x2);
25 });
26 };
27
29 HAL_UART_AbortTransmit(Handle);
30 HAL_UART_UnRegisterCallback(Handle, HAL_UART_TX_COMPLETE_CB_ID);
31 HAL_UART_UnRegisterCallback(Handle, HAL_UART_ERROR_CB_ID);
32 stm32cubemx_helper::set_context<Handle, UartTxIt>(nullptr);
33 }
34
35 bool transmit(const uint8_t *data, size_t size, uint32_t timeout) {
36 notifier_.reset();
37 if (HAL_UART_Transmit_IT(Handle, data, size) != HAL_OK) {
38 HAL_UART_AbortTransmit(Handle);
39 return false;
40 }
41 if (notifier_.wait(0x1 | 0x2, timeout) != 0x1) {
42 HAL_UART_AbortTransmit(Handle);
43 return false;
44 }
45 return true;
46 }
47
48private:
49 core::Notifier notifier_;
50
51 UartTxIt(const UartTxIt &) = delete;
52 UartTxIt &operator=(const UartTxIt &) = delete;
53};
54
55template <UART_HandleTypeDef *Handle> class UartRxIt {
56public:
57 UartRxIt(size_t buf_size) : queue_{buf_size} {
58 stm32cubemx_helper::set_context<Handle, UartRxIt>(this);
59 HAL_UART_RegisterCallback(
60 Handle, HAL_UART_RX_COMPLETE_CB_ID, [](UART_HandleTypeDef *huart) {
61 auto uart = stm32cubemx_helper::get_context<Handle, UartRxIt>();
62 uart->queue_.push(uart->buf_);
63 HAL_UART_Receive_IT(huart, &uart->buf_, 1);
64 });
65 HAL_UART_RegisterCallback(
66 Handle, HAL_UART_ERROR_CB_ID,
67 [](UART_HandleTypeDef *huart) { HAL_UART_AbortReceive_IT(huart); });
68 HAL_UART_RegisterCallback(
69 Handle, HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID,
70 [](UART_HandleTypeDef *huart) {
71 auto rx = stm32cubemx_helper::get_context<Handle, UartRxIt>();
72 HAL_UART_Receive_IT(huart, &rx->buf_, 1);
73 });
74 HAL_UART_Receive_IT(Handle, &buf_, 1);
75 }
76
78 HAL_UART_AbortReceive(Handle);
79 HAL_UART_UnRegisterCallback(Handle, HAL_UART_RX_COMPLETE_CB_ID);
80 HAL_UART_UnRegisterCallback(Handle, HAL_UART_ERROR_CB_ID);
81 HAL_UART_UnRegisterCallback(Handle, HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID);
82 stm32cubemx_helper::set_context<Handle, UartRxIt>(nullptr);
83 }
84
85 bool receive(uint8_t *data, size_t size, uint32_t timeout) {
86 core::TimeoutHelper timeout_helper{timeout};
87 while (queue_.size() < size) {
88 if (timeout_helper.is_timeout()) {
89 return false;
90 }
92 }
93 for (size_t i = 0; i < size; ++i) {
94 data[i] = *queue_.pop();
95 }
96 return true;
97 }
98
99 void flush() { queue_.clear(); }
100
101 size_t available() const { return queue_.size(); }
102
103private:
105 uint8_t buf_;
106
107 UartRxIt(const UartRxIt &) = delete;
108 UartRxIt &operator=(const UartRxIt &) = delete;
109};
110
111} // namespace halx::peripheral
Definition notifier.hpp:11
Definition ring_buffer.hpp:13
Definition timeout.hpp:9
Definition uart_it.hpp:55
~UartRxIt()
Definition uart_it.hpp:77
bool receive(uint8_t *data, size_t size, uint32_t timeout)
Definition uart_it.hpp:85
size_t available() const
Definition uart_it.hpp:101
void flush()
Definition uart_it.hpp:99
UartRxIt(size_t buf_size)
Definition uart_it.hpp:57
Definition uart_it.hpp:12
bool transmit(const uint8_t *data, size_t size, uint32_t timeout)
Definition uart_it.hpp:35
UartTxIt()
Definition uart_it.hpp:14
~UartTxIt()
Definition uart_it.hpp:28
void yield()
Definition common.hpp:46
Definition can.hpp:13