stm32rcos
 
読み取り中…
検索中…
一致する文字列を見つけられません
uart_it.hpp
[詳解]
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5
6#include "stm32rcos/core.hpp"
7#include "stm32rcos/hal.hpp"
8
9#include "../uart_type.hpp"
10
11namespace stm32rcos {
12namespace peripheral {
13namespace detail {
14
15template <UartType TxType> class UartTx;
16template <UartType RxType> class UartRx;
17
18template <> class UartTx<UartType::IT> {
19public:
20 UartTx(UART_HandleTypeDef *huart) : huart_{huart} {}
21
22 bool transmit(const uint8_t *data, size_t size, uint32_t timeout) {
23 if (HAL_UART_Transmit_IT(huart_, data, size) != HAL_OK) {
24 HAL_UART_AbortTransmit_IT(huart_);
25 return false;
26 }
27 core::TimeoutHelper timeout_helper;
28 while (huart_->gState != HAL_UART_STATE_READY) {
29 if (timeout_helper.is_timeout(timeout)) {
30 HAL_UART_AbortTransmit_IT(huart_);
31 return false;
32 }
33 osDelay(1);
34 }
35 return true;
36 }
37
38private:
39 UART_HandleTypeDef *huart_;
40
41 UartTx(const UartTx &) = delete;
42 UartTx &operator=(const UartTx &) = delete;
43};
44
45template <> class UartRx<UartType::IT> {
46public:
47 UartRx(UART_HandleTypeDef *huart, size_t buf_size)
48 : huart_{huart}, queue_{buf_size} {
49 hal::set_uart_context(huart_, this);
50 HAL_UART_RegisterCallback(
51 huart_, HAL_UART_RX_COMPLETE_CB_ID, [](UART_HandleTypeDef *huart) {
52 auto uart = reinterpret_cast<UartRx *>(hal::get_uart_context(huart));
53 uart->queue_.push(uart->buf_, 0);
54 HAL_UART_Receive_IT(huart, &uart->buf_, 1);
55 });
56 HAL_UART_RegisterCallback(
57 huart_, HAL_UART_ERROR_CB_ID,
58 [](UART_HandleTypeDef *huart) { HAL_UART_Abort_IT(huart); });
59 HAL_UART_RegisterCallback(
60 huart_, HAL_UART_ABORT_COMPLETE_CB_ID, [](UART_HandleTypeDef *huart) {
61 auto uart = reinterpret_cast<UartRx *>(hal::get_uart_context(huart));
62 HAL_UART_Receive_IT(huart, &uart->buf_, 1);
63 });
64 HAL_UART_Receive_IT(huart, &buf_, 1);
65 }
66
68 HAL_UART_Abort_IT(huart_);
69 HAL_UART_UnRegisterCallback(huart_, HAL_UART_RX_COMPLETE_CB_ID);
70 HAL_UART_UnRegisterCallback(huart_, HAL_UART_ERROR_CB_ID);
71 HAL_UART_UnRegisterCallback(huart_, HAL_UART_ABORT_COMPLETE_CB_ID);
72 hal::set_uart_context(huart_, nullptr);
73 }
74
75 bool receive(uint8_t *data, size_t size, uint32_t timeout) {
76 core::TimeoutHelper timeout_helper;
77 while (queue_.size() < size) {
78 if (timeout_helper.is_timeout(timeout)) {
79 return false;
80 }
81 osDelay(1);
82 }
83 for (size_t i = 0; i < size; ++i) {
84 queue_.pop(data[i], 0);
85 }
86 return true;
87 }
88
89 void flush() { queue_.clear(); }
90
91 size_t available() { return queue_.size(); }
92
93private:
94 UART_HandleTypeDef *huart_;
96 uint8_t buf_;
97
98 UartRx(const UartRx &) = delete;
99 UartRx &operator=(const UartRx &) = delete;
100};
101
102} // namespace detail
103} // namespace peripheral
104} // namespace stm32rcos
Definition queue.hpp:13
Definition utility.hpp:12
bool is_timeout(uint32_t &timeout)
Definition utility.hpp:16
size_t available()
Definition uart_it.hpp:91
bool receive(uint8_t *data, size_t size, uint32_t timeout)
Definition uart_it.hpp:75
UartRx(UART_HandleTypeDef *huart, size_t buf_size)
Definition uart_it.hpp:47
UartTx(UART_HandleTypeDef *huart)
Definition uart_it.hpp:20
bool transmit(const uint8_t *data, size_t size, uint32_t timeout)
Definition uart_it.hpp:22
Definition uart.hpp:21
Definition can.hpp:18
UartType
Definition uart_type.hpp:6
@ IT
Definition uart_type.hpp:8
Definition mutex.hpp:9