libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
uart_rx_config_gate.hpp
1#pragma once
2
3#include <atomic>
4#include <cstdint>
5
6#include "libxr_assert.hpp"
7
8namespace LibXR
9{
10
23{
24 public:
25 void RequestConfig() { state_.fetch_or(CONFIG_PENDING, std::memory_order_release); }
26
27 [[nodiscard]] bool TryEnterRx()
28 {
29 uint32_t expected = 0U;
30 return state_.compare_exchange_strong(expected, RX_ACTIVE, std::memory_order_acquire,
31 std::memory_order_relaxed);
32 }
33
37 [[nodiscard]] bool LeaveRx()
38 {
39 uint32_t observed = state_.load(std::memory_order_relaxed);
40 while (true)
41 {
42 ASSERT((observed & RX_ACTIVE) != 0U);
43 const uint32_t desired = observed & ~RX_ACTIVE;
44 if (state_.compare_exchange_weak(observed, desired, std::memory_order_release,
45 std::memory_order_relaxed))
46 {
47 return (observed & CONFIG_PENDING) != 0U;
48 }
49 }
50 }
51
52 [[nodiscard]] bool TryEnterConfig()
53 {
54 uint32_t expected = CONFIG_PENDING;
55 return state_.compare_exchange_strong(
56 expected, CONFIG_ACTIVE, std::memory_order_acquire, std::memory_order_relaxed);
57 }
58
59 void LeaveConfig()
60 {
61 uint32_t observed = state_.load(std::memory_order_relaxed);
62 while (true)
63 {
64 ASSERT((observed & CONFIG_ACTIVE) != 0U);
65 const uint32_t desired = observed & ~CONFIG_ACTIVE;
66 if (state_.compare_exchange_weak(observed, desired, std::memory_order_release,
67 std::memory_order_relaxed))
68 {
69 return;
70 }
71 }
72 }
73
74 [[nodiscard]] bool ConfigRequested() const
75 {
76 return (state_.load(std::memory_order_acquire) & (CONFIG_PENDING | CONFIG_ACTIVE)) !=
77 0U;
78 }
79
80 UartRxConfigGate() = default;
81 UartRxConfigGate(const UartRxConfigGate&) = delete;
82 UartRxConfigGate& operator=(const UartRxConfigGate&) = delete;
83
84 private:
85 static constexpr uint32_t RX_ACTIVE = 1U << 0U;
86 static constexpr uint32_t CONFIG_PENDING = 1U << 1U;
87 static constexpr uint32_t CONFIG_ACTIVE = 1U << 2U;
88
89 std::atomic<uint32_t> state_{0U};
90};
91
92} // namespace LibXR
Serialize UART RX hardware callbacks against configuration / 串行化 UART RX 硬件回调与配置事务
LibXR 命名空间
Definition ch32_can.hpp:14