libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
esp_uart.hpp
1#pragma once
2
3#include "esp_def.hpp"
4
5#include <atomic>
6#include <cstddef>
7#include <cstdint>
8
9#include "driver/gpio.h"
10#include "esp_intr_alloc.h"
11#include "flag.hpp"
12#include "hal/uart_hal.h"
13#include "hal/uart_types.h"
14#include "soc/periph_defs.h"
15#include "soc/soc_caps.h"
16#include "uart.hpp"
17
18#if SOC_GDMA_SUPPORTED && SOC_UHCI_SUPPORTED
19#include "esp_private/gdma.h"
20#include "esp_private/gdma_link.h"
21#include "hal/uhci_hal.h"
22#endif
23
24namespace LibXR
25{
26
27class ESP32UART : public UART
28{
29 public:
30 static constexpr int PIN_NO_CHANGE = -1;
31
32 ESP32UART(uart_port_t uart_num, int tx_pin, int rx_pin,
33 int rts_pin = PIN_NO_CHANGE, int cts_pin = PIN_NO_CHANGE,
34 size_t rx_buffer_size = 1024, size_t tx_buffer_size = 512,
35 uint32_t tx_queue_size = 5,
36 UART::Configuration config = {115200, UART::Parity::NO_PARITY, 8, 1},
37 bool enable_dma = true);
38
39
41
42 ErrorCode SetLoopback(bool enable);
43
44 static ErrorCode WriteFun(WritePort& port, bool in_isr);
45
46 static ErrorCode ReadFun(ReadPort& port, bool in_isr);
47
48 private:
49 static uint8_t* AllocateTxStorage(size_t size);
50
51
52 static ErrorCode ResolveUartPeriph(uart_port_t uart_num, periph_module_t& out);
53
54 static bool ResolveWordLength(uint8_t data_bits, uart_word_length_t& out);
55
56 static bool ResolveStopBits(uint8_t stop_bits, uart_stop_bits_t& out);
57
58 static uart_parity_t ResolveParity(UART::Parity parity);
59
60 static void UartIsrEntry(void* arg);
61
62#if SOC_GDMA_SUPPORTED && SOC_UHCI_SUPPORTED
63 static bool DmaTxEofCallback(gdma_channel_handle_t dma_chan,
64 gdma_event_data_t* event_data,
65 void* user_data);
66
67 static bool DmaTxDescrErrCallback(gdma_channel_handle_t dma_chan,
68 gdma_event_data_t* event_data,
69 void* user_data);
70
71 static bool DmaRxDoneCallback(gdma_channel_handle_t dma_chan,
72 gdma_event_data_t* event_data,
73 void* user_data);
74
75 static bool DmaRxDescrErrCallback(gdma_channel_handle_t dma_chan,
76 gdma_event_data_t* event_data,
77 void* user_data);
78#endif
79
80 ErrorCode InitUartHardware();
81
82
83 ErrorCode ConfigurePins();
84
85 ErrorCode InstallUartIsr();
86
87
88 void ConfigureRxInterruptPath();
89
90#if SOC_GDMA_SUPPORTED && SOC_UHCI_SUPPORTED
91 ErrorCode InitDmaBackend();
92
93
94 bool StartDmaTx();
95
96 void HandleDmaRxDone(gdma_event_data_t* event_data);
97
98 void HandleDmaRxError();
99
100 void HandleDmaTxError();
101
102 void PushDmaRxData(size_t recv_size, bool in_isr);
103#endif
104
105 ErrorCode TryStartTx(bool in_isr);
106
107 bool LoadActiveTxFromQueue(bool in_isr);
108
109 bool LoadPendingTxFromQueue(bool in_isr);
110
111 bool DequeueTxToBuffer(uint8_t* buffer, size_t& size, WriteInfoBlock& info,
112 bool in_isr);
113
114 bool StartActiveTransfer(bool in_isr);
115 bool StartAndReportActive(bool in_isr);
116 void ClearActiveTx();
117 void ClearPendingTx();
118
119 void FillTxFifo(bool in_isr);
120
121 void PushRxBytes(const uint8_t* data, size_t size, bool in_isr);
122
123 void DrainRxFifoFromIsr();
124
125 void HandleRxInterrupt(uint32_t uart_intr_status);
126
127 void HandleTxInterrupt(uint32_t uart_intr_status);
128
129 void HandleUartInterrupt();
130
131 void OnTxTransferDone(bool in_isr, ErrorCode result);
132
133 uart_port_t uart_num_;
134 int tx_pin_;
135 int rx_pin_;
136 int rts_pin_;
137 int cts_pin_;
138
139 UART::Configuration config_;
140
141 uint8_t* rx_isr_buffer_ = nullptr;
142 size_t rx_isr_buffer_size_ = 0;
143
144 uint8_t* tx_storage_ = nullptr;
145 size_t tx_storage_size_ = 0;
146 size_t tx_buffer_size_ = 0;
147 uint8_t* tx_active_buffer_ = nullptr;
148 uint8_t* tx_pending_buffer_ = nullptr;
149 size_t tx_active_length_ = 0;
150 size_t tx_pending_length_ = 0;
151 size_t tx_active_offset_ = 0;
152
153 WriteInfoBlock tx_active_info_ = {};
154 WriteInfoBlock tx_pending_info_ = {};
155 Flag::Plain tx_busy_;
156 Flag::Plain in_tx_isr_;
157 bool tx_active_valid_ = false;
158 bool tx_pending_valid_ = false;
159
160 bool uart_hw_enabled_ = false;
161 uart_hal_context_t uart_hal_ = {};
162 intr_handle_t uart_intr_handle_ = nullptr;
163 bool uart_isr_installed_ = false;
164 bool dma_requested_ = true;
165
166 ReadPort _read_port;
167 WritePort _write_port;
168
169#if SOC_GDMA_SUPPORTED && SOC_UHCI_SUPPORTED
170 bool dma_backend_enabled_ = false;
171 uhci_hal_context_t uhci_hal_ = {};
172 gdma_channel_handle_t tx_dma_channel_ = nullptr;
173 gdma_channel_handle_t rx_dma_channel_ = nullptr;
174 gdma_link_list_handle_t tx_dma_links_[2] = {nullptr, nullptr};
175 uintptr_t tx_dma_head_addr_[2] = {0U, 0U};
176 uint8_t* tx_dma_buffer_addr_[2] = {nullptr, nullptr};
177 gdma_link_list_handle_t rx_dma_link_ = nullptr;
178 size_t tx_dma_alignment_ = 1;
179 size_t rx_dma_alignment_ = 1;
180 uint8_t* rx_dma_storage_ = nullptr;
181 size_t rx_dma_chunk_size_ = 0;
182 uint32_t rx_dma_node_count_ = 0;
183 uint32_t rx_dma_node_index_ = 0;
184#endif
185};
186
187} // namespace LibXR
ErrorCode SetConfig(UART::Configuration config) override
设置 UART 配置 / Sets the UART configuration
Definition esp_uart.cpp:157
普通标志位(非原子)/ Non-atomic flag
Definition flag.hpp:115
ReadPort class for handling read operations.
Definition libxr_rw.hpp:379
通用异步收发传输(UART)基类 / Abstract base class for Universal Asynchronous Receiver-Transmitter (UART)
Definition uart.hpp:19
Parity
奇偶校验模式 / Parity mode
Definition uart.hpp:29
@ NO_PARITY
无校验 / No parity
WritePort class for handling write operations.
Definition libxr_rw.hpp:538
LibXR 命名空间
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举
Definition libxr_def.hpp:64
ErrorCode(* ReadFun)(ReadPort &port, bool in_isr)
Function pointer type for read operations.
Definition libxr_rw.hpp:356
ErrorCode(* WriteFun)(WritePort &port, bool in_isr)
Function pointer type for write operations.
Definition libxr_rw.hpp:352
UART 配置结构体 / UART configuration structure.
Definition uart.hpp:44