libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
esp_uart.cpp
1#include "esp_uart.hpp"
2
3#include "libxr_rw.hpp"
4
5using namespace LibXR;
6
8{
9 size_t ans = 0;
10 uart_get_buffered_data_len(uart_->port_, &ans);
11 return uart_->rx_buff_.size_ - ans;
12}
13
15{
16 size_t ans = 0;
17 uart_get_buffered_data_len(uart_->port_, &ans);
18 return ans;
19}
20
22{
23 LibXR::Mutex::LockGuard guard(mutex_);
24 if (busy_.load(std::memory_order_relaxed) == BusyState::Pending)
25 {
26 if (Size() >= info_.data.size_)
27 {
28 int len = uart_read_bytes(uart_->port_, info_.data.addr_, info_.data.size_, 0);
29 busy_.store(BusyState::Idle, std::memory_order_relaxed);
30 if (len == info_.data.size_)
31 {
32 Finish(in_isr, ErrorCode::OK, info_, info_.data.size_);
33 }
34 else
35 {
36 Finish(in_isr, ErrorCode::EMPTY, info_, len);
37 }
38 }
39 }
40}
41
43{
44 size_t ans = 0;
45 uart_get_tx_buffer_free_size(uart_->port_, &ans);
46 return ans;
47}
48
50{
51 size_t ans = 0;
52 uart_get_tx_buffer_free_size(uart_->port_, &ans);
53 return uart_->tx_buff_.size_ - ans;
54}
55
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),
59 port_(port),
60 rx_buff_(new uint8_t[buffer_size], buffer_size),
61 tx_buff_(new uint8_t[buffer_size], buffer_size),
62 _read_port(0, this),
63 _write_port(1, 0, this)
64{
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;
72
73 ESP_ERROR_CHECK(uart_param_config(port_, &config));
74 ESP_ERROR_CHECK(
75 uart_set_pin(port_, tx_pin, rx_pin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
76 ESP_ERROR_CHECK(
77 uart_driver_install(port_, rx_buff_.size_, tx_buff_.size_, 20, &event_queue_, 0));
78
79 _read_port = ReadFun;
80 _write_port = WriteFun;
81
82 xTaskCreate(RxTask, "uart_rx_task", rx_thread_stack_depth, this, rx_thread_priority,
83 nullptr);
84}
85
86void ESP32UART::RxTask(void *param)
87{
88 auto *self = static_cast<ESP32UART *>(param);
89 uart_event_t event;
90 while (true)
91 {
92 if (xQueueReceive(self->event_queue_, &event, portMAX_DELAY))
93 {
94 self->HandleEvent(event);
95 }
96 }
97}
98
99void ESP32UART::HandleEvent(const uart_event_t &event)
100{
101 switch (event.type)
102 {
103 case UART_DATA:
104 {
106 break;
107 }
108 case UART_BUFFER_FULL:
109 case UART_FIFO_OVF:
110 case UART_FRAME_ERR:
111 case UART_PARITY_ERR:
112 {
113 LibXR::Mutex::LockGuard guard(read_port_->mutex_);
114
115 if (read_port_->busy_.load(std::memory_order_relaxed) ==
116 LibXR::ReadPort::BusyState::Pending)
117 {
118 read_port_->info_.op.UpdateStatus(false, ErrorCode::FAILED);
119 }
120 uart_flush_input(port_);
121 read_port_->Reset();
123 break;
124 }
125 default:
126 break;
127 }
128}
129
130ErrorCode ESP32UART::WriteFun(WritePort &port)
131{
132 ESP32UART *self = CONTAINER_OF(&port, ESP32UART, _write_port);
133 WriteInfoBlock info;
134 if (port.queue_info_->Pop(info) != ErrorCode::OK)
135 {
136 return ErrorCode::EMPTY;
137 }
138
139 size_t space = 0;
140
141 uart_get_tx_buffer_free_size(self->port_, &space);
142 if (space < info.data.size_)
143 {
144 return ErrorCode::FULL;
145 }
146
147 uart_write_bytes(self->port_, static_cast<const char *>(info.data.addr_),
148 info.data.size_);
149
150 return ErrorCode::OK;
151}
152
153ErrorCode ESP32UART::ReadFun(ReadPort &port)
154{
155 ESP32UART *self = CONTAINER_OF(&port, ESP32UART, _read_port);
156 UNUSED(self);
157
158 if (port.Size() >= port.info_.data.size_)
159 {
160 int len =
161 uart_read_bytes(self->port_, port.info_.data.addr_, port.info_.data.size_, 0);
162
163 port.read_size_ = len;
164 if (len == port.info_.data.size_)
165 {
166 return ErrorCode::OK;
167 }
168 }
169
170 return ErrorCode::EMPTY;
171}
172
174{
175 uart_config_t uart_cfg = {};
176
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;
183
184 switch (config.parity)
185 {
187 uart_cfg.parity = UART_PARITY_DISABLE;
188 uart_cfg.data_bits = UART_DATA_8_BITS;
189 break;
191 uart_cfg.parity = UART_PARITY_EVEN;
192 uart_cfg.data_bits = UART_DATA_8_BITS;
193 break;
195 uart_cfg.parity = UART_PARITY_ODD;
196 uart_cfg.data_bits = UART_DATA_8_BITS;
197 break;
198 default:
199 return ErrorCode::ARG_ERR;
200 }
201
202 switch (config.stop_bits)
203 {
204 case 1:
205 uart_cfg.stop_bits = UART_STOP_BITS_1;
206 break;
207 case 2:
208 uart_cfg.stop_bits = UART_STOP_BITS_2;
209 break;
210 default:
211 return ErrorCode::ARG_ERR;
212 }
213
214 if (uart_param_config(port_, &uart_cfg) != ESP_OK)
215 {
216 return ErrorCode::FAILED;
217 }
218
219 return ErrorCode::OK;
220}
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
Definition esp_uart.cpp:173
size_t Size()
获取当前队列的已使用大小。 Gets the currently used size of the queue.
Definition esp_uart.cpp:14
size_t EmptySize()
获取队列的剩余可用空间。 Gets the remaining available space in the queue.
Definition esp_uart.cpp:7
void ProcessPendingReads(bool in_isr=true)
Processes pending reads.
Definition esp_uart.cpp:21
size_t Size()
获取当前数据队列的已使用大小。 Gets the used size of the current data queue.
Definition esp_uart.cpp:49
size_t EmptySize()
获取数据队列的剩余可用空间。 Gets the remaining available space in the data queue.
Definition esp_uart.cpp:42
互斥锁的 RAII 机制封装 (RAII-style mechanism for automatic mutex management).
Definition mutex.hpp:65
void UpdateStatus(bool in_isr, Status &&...status)
Updates operation status based on type.
Definition libxr_rw.hpp:173
size_t size_
数据大小(字节)。 The size of the data (in bytes).
void * addr_
数据存储地址。 The storage address of the data.
ReadPort class for handling read operations.
Definition libxr_rw.hpp:270
void Finish(bool in_isr, ErrorCode ans, ReadInfoBlock &info, uint32_t size)
更新读取操作的状态。 Updates the status of the read operation.
Definition libxr_rw.hpp:371
virtual size_t Size()
获取当前队列的已使用大小。 Gets the currently used size of the queue.
Definition libxr_rw.hpp:325
virtual void ProcessPendingReads(bool in_isr)
Processes pending reads.
Definition libxr_rw.hpp:501
virtual void Reset()
Resets the ReadPort.
Definition libxr_rw.hpp:550
通用异步收发传输(UART)基类 / Abstract base class for Universal Asynchronous Receiver-Transmitter (UART)
Definition uart.hpp:19
ReadPort * read_port_
读取端口 / Read port
Definition uart.hpp:51
@ NO_PARITY
无校验 / No parity
@ ODD
奇校验 / Odd parity
@ EVEN
偶校验 / Even parity
WritePort * write_port_
写入端口 / Write port
Definition uart.hpp:52
WritePort class for handling write operations.
Definition libxr_rw.hpp:564
virtual void Reset()
Resets the WritePort.
Definition libxr_rw.hpp:803
LibXR 命名空间
Definition ch32_gpio.hpp:9
ErrorCode(* ReadFun)(ReadPort &port)
Function pointer type for read operations.
Definition libxr_rw.hpp:247
ErrorCode(* WriteFun)(WritePort &port)
Function pointer type for write operations.
Definition libxr_rw.hpp:243
RawData data
Data buffer. 数据缓冲区。
Definition libxr_rw.hpp:255
ReadOperation op
Read operation instance. 读取操作实例。
Definition libxr_rw.hpp:256
UART 配置结构体 / UART configuration structure.
Definition uart.hpp:44
uint8_t stop_bits
停止位长度 / Number of stop bits
Definition uart.hpp:48
Parity parity
校验模式 / Parity mode
Definition uart.hpp:46
uint32_t baudrate
波特率 / Baud rate
Definition uart.hpp:45