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