libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
mspm0_uart.hpp
1#pragma once
2
3#include <ti/driverlib/dl_uart_main.h>
4
5#include "ti_msp_dl_config.h"
6#include "uart.hpp"
7
8namespace LibXR
9{
10
11class MSPM0UART : public UART
12{
13 public:
14 enum class RxTimeoutMode : uint8_t
15 {
16 LIN_COMPARE,
17 BYTE_INTERRUPT
18 };
19
20 struct Resources
21 {
22 UART_Regs* instance;
23 IRQn_Type irqn;
24 uint32_t clock_freq;
25 uint8_t index;
26 };
27
28 MSPM0UART(Resources res, RawData rx_stage_buffer, uint32_t tx_queue_size = 5,
29 uint32_t tx_buffer_size = 128,
30 UART::Configuration config = {115200, UART::Parity::NO_PARITY, 8, 1});
31
33
34 static ErrorCode WriteFun(WritePort& port, bool in_isr);
35
36 static ErrorCode ReadFun(ReadPort& port, bool in_isr);
37
38 static void OnInterrupt(uint8_t index);
39 static UART::Configuration BuildConfigFromSysCfg(UART_Regs* instance,
40 uint32_t baudrate);
41
42 RxTimeoutMode GetRxTimeoutMode() const { return rx_timeout_mode_; }
43 uint32_t GetRxTimeoutCount() const { return rx_timeout_count_; }
44 uint32_t GetRxDropCount() const { return rx_drop_count_; }
45 uint32_t GetTimeoutInterruptEnabledMask() const;
46 uint32_t GetTimeoutInterruptMaskedStatus() const;
47 uint32_t GetTimeoutInterruptRawStatus() const;
48 uint32_t GetRxInterruptTimeoutValue() const;
49 uint32_t GetRxFifoThresholdValue() const;
50
51 ReadPort _read_port; // NOLINT
52 WritePort _write_port; // NOLINT
53
54 static constexpr uint8_t ResolveIndex(IRQn_Type irqn)
55 {
56 switch (irqn)
57 {
58#if defined(UART0_BASE)
59 case UART0_INT_IRQn:
60 return 0;
61#endif
62#if defined(UART1_BASE)
63 case UART1_INT_IRQn:
64 return 1;
65#endif
66#if defined(UART2_BASE)
67 case UART2_INT_IRQn:
68 return 2;
69#endif
70#if defined(UART3_BASE)
71 case UART3_INT_IRQn:
72 return 3;
73#endif
74#if defined(UART4_BASE)
75 case UART4_INT_IRQn:
76 return 4;
77#endif
78#if defined(UART5_BASE)
79 case UART5_INT_IRQn:
80 return 5;
81#endif
82#if defined(UART6_BASE)
83 case UART6_INT_IRQn:
84 return 6;
85#endif
86#if defined(UART7_BASE)
87 case UART7_INT_IRQn:
88 return 7;
89#endif
90 default:
91 return INVALID_INSTANCE_INDEX;
92 }
93 }
94
95 private:
96 static constexpr uint8_t MAX_UART_INSTANCES = 8;
97 static constexpr uint8_t INVALID_INSTANCE_INDEX = 0xFF;
98
99 void HandleInterrupt();
100
101 void HandleRxInterrupt(uint32_t timeout_mask);
102
103 void HandleRxTimeoutInterrupt(uint32_t pending, uint32_t timeout_mask);
104
105 void DrainRxFIFO(bool& received, bool& pushed);
106
107 void HandleTxInterrupt(bool in_isr);
108
109 void HandleErrorInterrupt(DL_UART_IIDX iidx);
110
111 void ApplyRxTimeoutMode();
112
113 RxTimeoutMode ResolveRxTimeoutMode() const;
114
115 uint32_t GetTimeoutInterruptMask() const;
116
117 void ResetLinCounter();
118
119 void DisableTxInterrupt();
120
121 Resources res_;
122 WriteInfoBlock tx_active_info_;
123 bool tx_active_valid_ = false;
124 size_t tx_active_remaining_ = 0;
125 size_t tx_active_total_ = 0;
126 RxTimeoutMode rx_timeout_mode_ = RxTimeoutMode::BYTE_INTERRUPT;
127 uint32_t rx_drop_count_ = 0;
128 uint32_t rx_timeout_count_ = 0;
129
130 static MSPM0UART* instance_map_[MAX_UART_INSTANCES];
131};
132
133// Helper macro to initialize MSPM0UART from SysConfig in one shot
134#define MSPM0_UART_INIT(name, rx_stage_addr, rx_stage_size, tx_queue_size, \
135 tx_buffer_size) \
136 ::LibXR::MSPM0UART::Resources{name##_INST, name##_INST_INT_IRQN, \
137 name##_INST_FREQUENCY, \
138 ::LibXR::MSPM0UART::ResolveIndex(name##_INST_INT_IRQN)}, \
139 ::LibXR::RawData{(rx_stage_addr), (rx_stage_size)}, (tx_queue_size), \
140 (tx_buffer_size), \
141 ::LibXR::MSPM0UART::BuildConfigFromSysCfg(name##_INST, \
142 static_cast<uint32_t>(name##_BAUD_RATE))
143
144} // namespace LibXR
ErrorCode SetConfig(UART::Configuration config) override
设置 UART 配置 / Sets the UART configuration
可写原始数据视图 / Mutable raw data view
ReadPort class for handling read operations.
Definition read_port.hpp:18
通用异步收发传输(UART)基类 / Abstract base class for Universal Asynchronous Receiver-Transmitter (UART)
Definition uart.hpp:19
@ NO_PARITY
无校验 / No parity
WritePort class for handling write operations.
LibXR 命名空间
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举
ErrorCode(* ReadFun)(ReadPort &port, bool in_isr)
Function pointer type for read notifications.
ErrorCode(* WriteFun)(WritePort &port, bool in_isr)
Function pointer type for write operations.
UART 配置结构体 / UART configuration structure.
Definition uart.hpp:44