libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
LibXR::ESP32UART Class Reference

支持 FIFO 和 GDMA 的 ESP UART 后端 / ESP UART backend with FIFO and GDMA. More...

#include <esp_uart.hpp>

Inheritance diagram for LibXR::ESP32UART:
[legend]
Collaboration diagram for LibXR::ESP32UART:
[legend]

Public Member Functions

 ESP32UART (uart_port_t uart_num, int tx_pin, int rx_pin, int rts_pin=PIN_NO_CHANGE, int cts_pin=PIN_NO_CHANGE, size_t rx_buffer_size=1024, size_t tx_buffer_size=512, uint32_t tx_queue_size=5, UART::Configuration config={115200, UART::Parity::NO_PARITY, 8, 1}, bool enable_dma=true)
 构造并初始化 ESP UART / Construct and initialize an ESP UART.
 
ErrorCode SetConfig (UART::Configuration config, bool in_isr=false) override
 提交帧格式和波特率配置 / Submit framing and baud configuration.
 
ErrorCode SetLoopback (bool enable)
 切换 UART 内部环回模式 / Toggle UART internal loopback.
 
- Public Member Functions inherited from LibXR::UART
template<typename ReadPortType = ReadPort, typename WritePortType = WritePort>
 UART (ReadPortType *read_port, WritePortType *write_port)
 UART 构造函数 / UART constructor.
 
template<typename OperationType , typename = std::enable_if_t<std::is_base_of_v< WriteOperation, std::decay_t<OperationType>>>>
ErrorCode Write (ConstRawData data, OperationType &&op, bool in_isr=false)
 
template<typename OperationType , typename = std::enable_if_t<std::is_base_of_v< ReadOperation, std::decay_t<OperationType>>>>
ErrorCode Read (RawData data, OperationType &&op, bool in_isr=false)
 

Static Public Member Functions

static void WriteFun (WritePort &port, bool in_isr)
 WritePort 使用的队列驱动 TX 入口 / Queue-driven TX entry used by WritePort.
 

Static Public Attributes

static constexpr int PIN_NO_CHANGE
 保持引脚映射不变 / Keep pin mapping unchanged.
 

Friends

class ESP32UARTReadPort
 

Additional Inherited Members

- Public Types inherited from LibXR::UART
enum class  Parity : uint8_t { NO_PARITY = 0 , EVEN = 1 , ODD = 2 }
 奇偶校验模式 / Parity mode More...
 
- Public Attributes inherited from LibXR::UART
ReadPort * read_port_
 读取端口 / Read port
 
WritePort * write_port_
 写入端口 / Write port
 

Detailed Description

支持 FIFO 和 GDMA 的 ESP UART 后端 / ESP UART backend with FIFO and GDMA.

FIFO 按硬件实际接收量消费 WriteQueue;GDMA 将完整请求复制到发送半区后完成请求, DMA 完成仅释放半区。接收通过 ReadQueue 发布,配置和中断进展由同一处理器串行执行。 FIFO consumes actual accepted prefixes; GDMA completes requests after copying them into TX halves, with DMA completion only releasing storage. RX uses ReadQueue; one service serializes configuration and interrupt progress.

Note
UART 使用普通中断,Flash 擦写导致缓存关闭时暂缓执行;持续接收可能溢出。 UART interrupts are deferred while flash operations disable cache; sustained input may overflow hardware storage.
接收 FIFO 满时保留数据等待软件空间;GDMA 则回收已完成描述符,丢弃放不下的尾部。 FIFO RX retains bytes until software space is available; GDMA recycles completed descriptors and discards excess bytes that do not fit.

Definition at line 80 of file esp_uart.hpp.

Constructor & Destructor Documentation

◆ ESP32UART()

LibXR::ESP32UART::ESP32UART ( uart_port_t uart_num,
int tx_pin,
int rx_pin,
int rts_pin = PIN_NO_CHANGE,
int cts_pin = PIN_NO_CHANGE,
size_t rx_buffer_size = 1024,
size_t tx_buffer_size = 512,
uint32_t tx_queue_size = 5,
UART::Configuration config = {115200, UART::Parity::NO_PARITY, 8, 1},
bool enable_dma = true )

构造并初始化 ESP UART / Construct and initialize an ESP UART.

Parameters
uart_numUART 外设编号 / UART peripheral number.
tx_pin发送 GPIO / TX GPIO.
rx_pin接收 GPIO / RX GPIO.
rts_pinRTS GPIO,PIN_NO_CHANGE 表示不修改 / RTS GPIO, or PIN_NO_CHANGE.
cts_pinCTS GPIO,PIN_NO_CHANGE 表示不修改 / CTS GPIO, or PIN_NO_CHANGE.
rx_buffer_size接收软件队列容量 / RX software queue capacity.
tx_buffer_size发送软件队列和单个 DMA 半区容量 / TX queue and DMA half capacity.
tx_queue_size写请求队列容量,须大于零 / Positive write request capacity.
config初始帧格式和波特率 / Initial framing and baud rate.
enable_dma在芯片支持时使用 GDMA,否则使用 FIFO / Use GDMA if supported, else FIFO.

Definition at line 146 of file esp_uart.cpp.

149 : UART(&_read_port, &_write_port),
150 uart_num_(uart_num),
151 tx_pin_(tx_pin),
152 rx_pin_(rx_pin),
153 rts_pin_(rts_pin),
154 cts_pin_(cts_pin),
155 config_(config),
156 rx_isr_buffer_(new uint8_t[rx_buffer_size]),
157 rx_isr_buffer_size_(rx_buffer_size),
158 tx_storage_(AllocateTxStorage(tx_buffer_size * 2)),
159 dma_requested_(enable_dma),
160 _read_port(rx_buffer_size, *this),
161 _write_port(tx_queue_size, tx_buffer_size)
162{
163 ASSERT(!IsConsoleUartInUse(uart_num_));
164 ASSERT(uart_num_ < UART_NUM_MAX);
165 ASSERT(uart_num_ < SOC_UART_HP_NUM);
166 ASSERT(rx_isr_buffer_size_ > 0);
167 ASSERT(tx_buffer_size > 0);
168 ASSERT(tx_queue_size > 0U);
169
170 INIT_CRIT_SECTION_LOCK_RUNTIME(&irq_lock_);
171
172 tx_dma_buffer_.Init({tx_storage_, tx_buffer_size * 2U});
173
174 _write_port = WriteFun;
175
176 if (InitUartHardware() != ErrorCode::OK)
177 {
178 REQUIRE(false);
179 return;
180 }
181
182#if SOC_GDMA_SUPPORTED && SOC_UHCI_SUPPORTED
183 if (dma_requested_)
184 {
185 if (InitDmaBackend() != ErrorCode::OK)
186 {
187 REQUIRE(false);
188 return;
189 }
190 }
191
192 if (!dma_backend_enabled_)
193 {
194 if (InstallUartIsr() != ErrorCode::OK)
195 {
196 REQUIRE(false);
197 return;
198 }
199 ConfigureRxInterruptPath();
200 }
201#else
202 if (InstallUartIsr() != ErrorCode::OK)
203 {
204 REQUIRE(false);
205 return;
206 }
207 ConfigureRxInterruptPath();
208#endif
209}
void Init(const LibXR::RawData &raw_data)
绑定连续 backing storage 并重置双缓冲状态 Binds continuous backing storage and resets double-buffer state
UART(ReadPortType *read_port, WritePortType *write_port)
UART 构造函数 / UART constructor.
Definition uart.hpp:68
@ OK
操作成功 / Operation successful
void(* WriteFun)(WritePort &port, bool in_isr)
写入进度通知函数类型 / Write progress notification function type.

Member Function Documentation

◆ SetConfig()

ErrorCode LibXR::ESP32UART::SetConfig ( UART::Configuration config,
bool in_isr = false )
overridevirtual

提交帧格式和波特率配置 / Submit framing and baud configuration.

Parameters
config目标配置 / Requested configuration.
in_isr当前是否在中断中 / Whether called in an ISR.
Returns
接纳返回 OK,配置槽被占用返回 BUSY,参数不支持返回 ARG_ERR; 硬件尚未初始化时返回 STATE_ERR。 OK on admission, BUSY for an occupied slot, ARG_ERR for unsupported settings, STATE_ERR before hardware initialization.
Note
GDMA 等已有半区传输结束后应用;FIFO 等软件队列和硬件 FIFO 空闲后应用。 GDMA waits for claimed transfers; FIFO waits for software and hardware drain.
初始化时选定的源时钟频率须保持不变 / Keep the selected source clock frequency fixed.

Implements LibXR::UART.

Definition at line 226 of file esp_uart.cpp.

227{
228 if (!uart_hw_enabled_)
229 {
231 }
232
233 uart_word_length_t word_length = UART_DATA_8_BITS;
234 uart_stop_bits_t stop_bits = UART_STOP_BITS_1;
235 if (config.baudrate == 0U || !ResolveWordLength(config.data_bits, word_length) ||
236 !ResolveStopBits(config.stop_bits, stop_bits) ||
237 (config.parity != UART::Parity::NO_PARITY && config.parity != UART::Parity::EVEN &&
238 config.parity != UART::Parity::ODD))
239 {
240 return ErrorCode::ARG_ERR;
241 }
242
243 // 使用初始化时的时钟快照,校验时不访问硬件。
244 // Validate against the initial clock snapshot without touching hardware.
245 if (!IsBaudrateRepresentable(config.baudrate, uart_sclk_hz_))
246 {
247 return ErrorCode::ARG_ERR;
248 }
249
250 uint8_t expected = static_cast<uint8_t>(ConfigState::EMPTY);
251 if (!config_state_.compare_exchange_strong(
252 expected, static_cast<uint8_t>(ConfigState::RESERVED),
253 std::memory_order_acq_rel, std::memory_order_acquire))
254 {
255 return ErrorCode::BUSY;
256 }
257
258 requested_config_ = config;
259 config_state_.store(static_cast<uint8_t>(ConfigState::PUBLISHED),
260 std::memory_order_release);
261 service_.Invoke(EVENT_CONFIG, in_isr, [this](uint32_t events, bool owner_isr)
262 { ServiceEvents(events, owner_isr); });
263 return ErrorCode::OK;
264}
bool Invoke(uint32_t events, bool in_isr, Handler &&handler) noexcept
发布事件并尝试取得执行权 / Publish events and attempt to claim execution.
@ NO_PARITY
无校验 / No parity
@ ODD
奇校验 / Odd parity
@ EVEN
偶校验 / Even parity
@ STATE_ERR
状态错误 / State error
@ BUSY
忙碌 / Busy
@ ARG_ERR
参数错误 / Argument error

◆ SetLoopback()

ErrorCode LibXR::ESP32UART::SetLoopback ( bool enable)

切换 UART 内部环回模式 / Toggle UART internal loopback.

Parameters
enable是否启用 / Whether to enable loopback.
Returns
成功为 OK,未初始化为 STATE_ERR / OK on success, STATE_ERR before initialization.

Definition at line 460 of file esp_uart.cpp.

461{
462 if (!uart_hw_enabled_)
463 {
465 }
466
467 uart_ll_set_loop_back(uart_hal_.dev, enable);
468 return ErrorCode::OK;
469}

◆ WriteFun()

void IRAM_ATTR LibXR::ESP32UART::WriteFun ( WritePort & port,
bool in_isr )
static

WritePort 使用的队列驱动 TX 入口 / Queue-driven TX entry used by WritePort.

Definition at line 473 of file esp_uart.cpp.

474{
475 auto* uart = LibXR::ContainerOf(&port, &ESP32UART::_write_port);
476 uart->service_.Invoke(ESP32UART::EVENT_WRITE, in_isr,
477 [uart](uint32_t events, bool owner_isr)
478 { uart->ServiceEvents(events, owner_isr); });
479}
OwnerType * ContainerOf(MemberType *ptr, MemberType OwnerType::*member) noexcept
通过成员指针恢复其所属对象指针 Recover the owning object pointer from a member pointer

Friends And Related Symbol Documentation

◆ ESP32UARTReadPort

friend class ESP32UARTReadPort
friend

Definition at line 82 of file esp_uart.hpp.

Member Data Documentation

◆ PIN_NO_CHANGE

int LibXR::ESP32UART::PIN_NO_CHANGE
staticconstexpr
Initial value:
=
-1

保持引脚映射不变 / Keep pin mapping unchanged.

Definition at line 85 of file esp_uart.hpp.


The documentation for this class was generated from the following files: