10 uart_get_buffered_data_len(uart_->port_, &ans);
11 return uart_->rx_buff_.
size_ - ans;
17 uart_get_buffered_data_len(uart_->port_, &ans);
24 if (busy_.load(std::memory_order_relaxed) == BusyState::Pending)
29 busy_.store(BusyState::Idle, std::memory_order_relaxed);
36 Finish(in_isr, ErrorCode::EMPTY, info_, len);
45 uart_get_tx_buffer_free_size(uart_->port_, &ans);
52 uart_get_tx_buffer_free_size(uart_->port_, &ans);
53 return uart_->tx_buff_.
size_ - ans;
56ESP32UART::ESP32UART(uart_port_t port,
int tx_pin,
int rx_pin, uint32_t buffer_size,
57 uint32_t rx_thread_stack_depth, uint32_t rx_thread_priority)
58 :
UART(&_read_port, &_write_port),
60 rx_buff_(new uint8_t[buffer_size], buffer_size),
61 tx_buff_(new uint8_t[buffer_size], buffer_size),
63 _write_port(1, 0, this)
65 uart_config_t config = {};
66 config.baud_rate = 115200;
67 config.data_bits = UART_DATA_8_BITS;
68 config.parity = UART_PARITY_DISABLE;
69 config.stop_bits = UART_STOP_BITS_1;
70 config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE;
71 config.source_clk = UART_SCLK_APB;
73 ESP_ERROR_CHECK(uart_param_config(port_, &config));
75 uart_set_pin(port_, tx_pin, rx_pin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
77 uart_driver_install(port_, rx_buff_.size_, tx_buff_.size_, 20, &event_queue_, 0));
82 xTaskCreate(RxTask,
"uart_rx_task", rx_thread_stack_depth,
this, rx_thread_priority,
86void ESP32UART::RxTask(
void *param)
88 auto *self =
static_cast<ESP32UART *
>(param);
92 if (xQueueReceive(self->event_queue_, &event, portMAX_DELAY))
94 self->HandleEvent(event);
99void ESP32UART::HandleEvent(
const uart_event_t &event)
108 case UART_BUFFER_FULL:
111 case UART_PARITY_ERR:
115 if (
read_port_->busy_.load(std::memory_order_relaxed) ==
116 LibXR::ReadPort::BusyState::Pending)
120 uart_flush_input(port_);
130ErrorCode ESP32UART::WriteFun(
WritePort &port)
134 if (port.queue_info_->Pop(info) != ErrorCode::OK)
136 return ErrorCode::EMPTY;
141 uart_get_tx_buffer_free_size(self->port_, &space);
142 if (space < info.data.
size_)
144 return ErrorCode::FULL;
147 uart_write_bytes(self->port_,
static_cast<const char *
>(info.data.
addr_),
150 return ErrorCode::OK;
153ErrorCode ESP32UART::ReadFun(
ReadPort &port)
163 port.read_size_ = len;
166 return ErrorCode::OK;
170 return ErrorCode::EMPTY;
175 uart_config_t uart_cfg = {};
177 uart_cfg.baud_rate =
static_cast<int>(config.
baudrate);
178 uart_cfg.data_bits = UART_DATA_8_BITS;
179 uart_cfg.parity = UART_PARITY_DISABLE;
180 uart_cfg.stop_bits = UART_STOP_BITS_1;
181 uart_cfg.flow_ctrl = UART_HW_FLOWCTRL_DISABLE;
182 uart_cfg.source_clk = UART_SCLK_APB;
187 uart_cfg.parity = UART_PARITY_DISABLE;
188 uart_cfg.data_bits = UART_DATA_8_BITS;
191 uart_cfg.parity = UART_PARITY_EVEN;
192 uart_cfg.data_bits = UART_DATA_8_BITS;
195 uart_cfg.parity = UART_PARITY_ODD;
196 uart_cfg.data_bits = UART_DATA_8_BITS;
199 return ErrorCode::ARG_ERR;
205 uart_cfg.stop_bits = UART_STOP_BITS_1;
208 uart_cfg.stop_bits = UART_STOP_BITS_2;
211 return ErrorCode::ARG_ERR;
214 if (uart_param_config(port_, &uart_cfg) != ESP_OK)
216 return ErrorCode::FAILED;
219 return ErrorCode::OK;
size_t size_
数据大小(字节)。 The size of the data (in bytes).
const void * addr_
数据存储地址(常量)。 The storage address of the data (constant).
ErrorCode SetConfig(UART::Configuration config) override
设置 UART 配置 / Sets the UART configuration
size_t Size()
获取当前队列的已使用大小。 Gets the currently used size of the queue.
size_t EmptySize()
获取队列的剩余可用空间。 Gets the remaining available space in the queue.
void ProcessPendingReads(bool in_isr=true)
Processes pending reads.
size_t Size()
获取当前数据队列的已使用大小。 Gets the used size of the current data queue.
size_t EmptySize()
获取数据队列的剩余可用空间。 Gets the remaining available space in the data queue.
互斥锁的 RAII 机制封装 (RAII-style mechanism for automatic mutex management).
void UpdateStatus(bool in_isr, Status &&...status)
Updates operation status based on type.
size_t size_
数据大小(字节)。 The size of the data (in bytes).
void * addr_
数据存储地址。 The storage address of the data.
ReadPort class for handling read operations.
void Finish(bool in_isr, ErrorCode ans, ReadInfoBlock &info, uint32_t size)
更新读取操作的状态。 Updates the status of the read operation.
virtual size_t Size()
获取当前队列的已使用大小。 Gets the currently used size of the queue.
virtual void ProcessPendingReads(bool in_isr)
Processes pending reads.
virtual void Reset()
Resets the ReadPort.
通用异步收发传输(UART)基类 / Abstract base class for Universal Asynchronous Receiver-Transmitter (UART)
ReadPort * read_port_
读取端口 / Read port
@ NO_PARITY
无校验 / No parity
WritePort * write_port_
写入端口 / Write port
WritePort class for handling write operations.
virtual void Reset()
Resets the WritePort.
ErrorCode(* ReadFun)(ReadPort &port)
Function pointer type for read operations.
ErrorCode(* WriteFun)(WritePort &port)
Function pointer type for write operations.
RawData data
Data buffer. 数据缓冲区。
ReadOperation op
Read operation instance. 读取操作实例。
UART 配置结构体 / UART configuration structure.
uint8_t stop_bits
停止位长度 / Number of stop bits
Parity parity
校验模式 / Parity mode
uint32_t baudrate
波特率 / Baud rate