libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
linux_uart.hpp
1#pragma once
2
3#define termios asmtermios
4#include <asm/termbits.h>
5#undef termios
6#include <fcntl.h>
7#include <libudev.h>
8#include <linux/serial.h>
9#include <sys/ioctl.h>
10#include <sys/select.h>
11#include <termios.h>
12#include <unistd.h>
13
14#include <cstddef>
15#include <cstdint>
16#include <filesystem>
17
18#include "libxr_def.hpp"
19#include "libxr_rw.hpp"
20#include "semaphore.hpp"
21#include "uart.hpp"
22
23namespace LibXR
24{
25
32class LinuxUART : public UART
33{
34 public:
38 LinuxUART(const char* dev_path, unsigned int baudrate = 115200,
39 Parity parity = Parity::NO_PARITY, uint8_t data_bits = 8,
40 uint8_t stop_bits = 1, uint32_t tx_queue_size = 5, size_t buffer_size = 512,
41 size_t thread_stack_size = 65536)
42 : UART(&_read_port, &_write_port),
43 rx_buff_(new uint8_t[buffer_size]),
44 tx_buff_(new uint8_t[buffer_size]),
45 buff_size_(buffer_size),
46 _read_port(buffer_size),
47 _write_port(tx_queue_size, buffer_size)
48 {
49 ASSERT(buff_size_ > 0);
50
51 while (!std::filesystem::exists(dev_path))
52 {
53 XR_LOG_WARN("Cannot find UART device: %s, retrying...", dev_path);
54 Thread::Sleep(100);
55 }
56
57 device_path_ = GetByPathForTTY(dev_path);
58
59 fd_ = open(device_path_.c_str(), O_RDWR | O_NOCTTY);
60 if (fd_ < 0)
61 {
62 XR_LOG_ERROR("Cannot open UART device: %s", device_path_.c_str());
63 ASSERT(false);
64 }
65 else
66 {
67 XR_LOG_PASS("Open UART device: %s", device_path_.c_str());
68 }
69
70 config_ = {.baudrate = baudrate,
71 .parity = parity,
72 .data_bits = data_bits,
73 .stop_bits = stop_bits};
74
75 SetConfig(config_);
76
77 _read_port = ReadFun;
78 _write_port = WriteFun;
79
80 rx_thread_.Create<LinuxUART*>(
81 this, [](LinuxUART* self) { self->RxLoop(); }, "rx_uart", thread_stack_size,
83
84 tx_thread_.Create<LinuxUART*>(
85 this, [](LinuxUART* self) { self->TxLoop(); }, "tx_uart", thread_stack_size,
87 }
88
92 LinuxUART(const std::string& vid, const std::string& pid,
93 unsigned int baudrate = 115200, Parity parity = Parity::NO_PARITY,
94 uint8_t data_bits = 8, uint8_t stop_bits = 1, uint32_t tx_queue_size = 5,
95 size_t buffer_size = 512, size_t thread_stack_size = 65536)
96 : UART(&_read_port, &_write_port),
97 rx_buff_(new uint8_t[buffer_size]),
98 tx_buff_(new uint8_t[buffer_size]),
99 buff_size_(buffer_size),
100 _read_port(buffer_size),
101 _write_port(tx_queue_size, buffer_size)
102 {
103 while (!FindUSBTTYByVidPid(vid, pid, device_path_))
104 {
105 XR_LOG_WARN("Cannot find USB TTY device with VID=%s PID=%s, retrying...",
106 vid.c_str(), pid.c_str());
107 Thread::Sleep(100);
108 }
109
110 XR_LOG_PASS("Found USB TTY: %s", device_path_.c_str());
111
112 if (std::filesystem::exists(device_path_) == false)
113 {
114 XR_LOG_ERROR("Cannot find UART device: %s", device_path_.c_str());
115 ASSERT(false);
116 return;
117 }
118
119 device_path_ = GetByPathForTTY(device_path_);
120
121 fd_ = open(device_path_.c_str(), O_RDWR | O_NOCTTY);
122 if (fd_ < 0)
123 {
124 XR_LOG_ERROR("Cannot open UART device: %s", device_path_.c_str());
125 ASSERT(false);
126 }
127 else
128 {
129 XR_LOG_PASS("Open UART device: %s", device_path_.c_str());
130 }
131
132 config_ = {.baudrate = baudrate,
133 .parity = parity,
134 .data_bits = data_bits,
135 .stop_bits = stop_bits};
136
137 SetConfig(config_);
138
139 _read_port = ReadFun;
140 _write_port = WriteFun;
141
142 rx_thread_.Create<LinuxUART*>(
143 this, [](LinuxUART* self) { self->RxLoop(); }, "rx_uart", thread_stack_size,
145
146 tx_thread_.Create<LinuxUART*>(
147 this, [](LinuxUART* self) { self->TxLoop(); }, "tx_uart", thread_stack_size,
149 }
150
151 std::string GetByPathForTTY(const std::string& tty_name)
152 {
153 const std::string BASE = "/dev/serial/by-path";
154 if (strncmp(tty_name.c_str(), BASE.c_str(), BASE.length()) == 0 ||
155 !std::filesystem::exists(BASE))
156 {
157 return tty_name;
158 }
159 for (const auto& entry : std::filesystem::directory_iterator(BASE))
160 {
161 std::string full = std::filesystem::canonical(entry.path());
162 if (full == tty_name)
163 {
164 return entry.path(); // 返回符号链接路径
165 }
166 }
167 return ""; // 没找到
168 }
169
170 static bool FindUSBTTYByVidPid(const std::string& target_vid,
171 const std::string& target_pid, std::string& tty_path)
172 {
173 struct udev* udev = udev_new();
174 if (!udev)
175 {
176 XR_LOG_ERROR("Cannot create udev context");
177 return false;
178 }
179
180 struct udev_enumerate* enumerate = udev_enumerate_new(udev);
181 udev_enumerate_add_match_subsystem(enumerate, "tty");
182 udev_enumerate_scan_devices(enumerate);
183
184 struct udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate);
185 struct udev_list_entry* entry = nullptr;
186 bool found = false;
187
188 udev_list_entry_foreach(entry, devices)
189 {
190 const char* path = udev_list_entry_get_name(entry);
191 struct udev_device* tty_dev = udev_device_new_from_syspath(udev, path);
192 if (!tty_dev)
193 {
194 continue;
195 }
196
197 struct udev_device* usb_dev =
198 udev_device_get_parent_with_subsystem_devtype(tty_dev, "usb", "usb_device");
199
200 if (usb_dev)
201 {
202 const char* vid = udev_device_get_sysattr_value(usb_dev, "idVendor");
203 const char* pid = udev_device_get_sysattr_value(usb_dev, "idProduct");
204
205 if (vid && pid && target_vid == vid && target_pid == pid)
206 {
207 const char* devnode = udev_device_get_devnode(tty_dev);
208 if (devnode)
209 {
210 tty_path = devnode;
211 found = true;
212 udev_device_unref(tty_dev);
213 break;
214 }
215 }
216 }
217
218 udev_device_unref(tty_dev);
219 }
220
221 udev_enumerate_unref(enumerate);
222 udev_unref(udev);
223 return found;
224 }
225
226 void SetLowLatency(int fd)
227 {
228 struct serial_struct serinfo;
229 ioctl(fd, TIOCGSERIAL, &serinfo);
230 serinfo.flags |= ASYNC_LOW_LATENCY;
231 ioctl(fd, TIOCSSERIAL, &serinfo);
232 }
233
234 ErrorCode SetConfig(UART::Configuration config) override
235 {
236 if (&config != &config_)
237 {
238 config_ = config;
239 }
240
241 struct termios2 tio{};
242 if (ioctl(fd_, TCGETS2, &tio) != 0)
243 {
244 return ErrorCode::INIT_ERR;
245 }
246
247 // 设置自定义波特率
248 tio.c_cflag &= ~CBAUD;
249 tio.c_cflag |= BOTHER;
250 tio.c_ispeed = config.baudrate;
251 tio.c_ospeed = config.baudrate;
252
253 // 输入模式:关闭软件流控、特殊字符处理
254 tio.c_iflag &= ~(IXON | IXOFF | IXANY | ISTRIP | IGNCR | INLCR | ICRNL
255#ifdef IUCLC
256 | IUCLC
257#endif
258 );
259
260 // 输出模式:关闭所有加工
261 tio.c_oflag &= ~(OPOST
262#ifdef ONLCR
263 | ONLCR
264#endif
265#ifdef OCRNL
266 | OCRNL
267#endif
268#ifdef ONOCR
269 | ONOCR
270#endif
271#ifdef ONLRET
272 | ONLRET
273#endif
274 );
275
276 // 本地模式:禁用行缓冲、回显、信号中断
277 tio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
278
279 // 控制模式:设置数据位、校验、停止位、流控
280 tio.c_cflag &= ~CSIZE;
281 switch (config.data_bits)
282 {
283 case 5:
284 tio.c_cflag |= CS5;
285 break;
286 case 6:
287 tio.c_cflag |= CS6;
288 break;
289 case 7:
290 tio.c_cflag |= CS7;
291 break;
292 case 8:
293 tio.c_cflag |= CS8;
294 break;
295 default:
296 return ErrorCode::ARG_ERR;
297 }
298
299 // 停止位
300 tio.c_cflag &= ~CSTOPB;
301 if (config.stop_bits == 2)
302 {
303 tio.c_cflag |= CSTOPB;
304 }
305
306 // 奇偶校验
307 switch (config.parity)
308 {
310 tio.c_cflag &= ~PARENB;
311 break;
313 tio.c_cflag |= PARENB;
314 tio.c_cflag &= ~PARODD;
315 break;
317 tio.c_cflag |= PARENB;
318 tio.c_cflag |= PARODD;
319 break;
320 }
321
322 // 禁用硬件流控
323 tio.c_cflag &= ~CRTSCTS;
324
325 // 启用本地模式、读功能
326 tio.c_cflag |= (CLOCAL | CREAD);
327
328 // 控制字符配置:阻塞直到读到 1 字节
329 // for (int i = 0; i < NCCS; ++i) tio.c_cc[i] = 0;
330 tio.c_cc[VTIME] = 0;
331 tio.c_cc[VMIN] = 1;
332
333 if (ioctl(fd_, TCSETS2, &tio) != 0)
334 {
335 return ErrorCode::INIT_ERR;
336 }
337
338 SetLowLatency(fd_);
339
340 tcflush(fd_, TCIOFLUSH);
341
342 return ErrorCode::OK;
343 }
344
345 static ErrorCode ReadFun(ReadPort&) { return ErrorCode::EMPTY; }
346
347 static ErrorCode WriteFun(WritePort& port)
348 {
349 auto uart = CONTAINER_OF(&port, LinuxUART, _write_port);
350 uart->write_sem_.Post();
351 return ErrorCode::OK;
352 }
353
354 private:
355 void RxLoop()
356 {
357 while (true)
358 {
359 if (!connected_)
360 {
361 close(fd_);
362 fd_ = open(device_path_.c_str(), O_RDWR | O_NOCTTY);
363
364 if (fd_ < 0)
365 {
366 XR_LOG_WARN("Cannot open UART device: %s", device_path_.c_str());
367 Thread::Sleep(1000);
368 }
369 else
370 {
371 SetConfig(config_);
372 XR_LOG_PASS("Reopen UART device: %s", device_path_.c_str());
373 connected_ = true;
374 }
375 }
376 auto n = read(fd_, rx_buff_, buff_size_);
377 if (n > 0)
378 {
379 read_port_->queue_data_->PushBatch(rx_buff_, n);
381 }
382 else
383 {
384 XR_LOG_WARN("Cannot read UART device: %s", device_path_.c_str());
385 connected_ = false;
386 }
387 }
388 }
389
390 void TxLoop()
391 {
392 WriteInfoBlock info;
393 while (true)
394 {
395 if (!connected_)
396 {
397 Thread::Sleep(1);
398 continue;
399 }
400
401 if (write_sem_.Wait() != ErrorCode::OK)
402 {
403 continue;
404 }
405
406 if (write_port_->queue_info_->Pop(info) == ErrorCode::OK)
407 {
408 if (write_port_->queue_data_->PopBatch(tx_buff_, info.data.size_) ==
409 ErrorCode::OK)
410 {
411 auto written = write(fd_, tx_buff_, info.data.size_);
412 if (written < 0)
413 {
414 XR_LOG_WARN("Cannot write UART device: %s", device_path_.c_str());
415 connected_ = false;
416 }
417 write_port_->Finish(false,
418 (written == static_cast<int>(info.data.size_))
419 ? ErrorCode::OK
420 : ErrorCode::FAILED,
421 info, written);
422 }
423 else
424 {
425 info.op.UpdateStatus(false, ErrorCode::FAILED);
426 }
427 }
428 }
429 }
430
431 int fd_ = -1;
432 bool connected_ = true;
433 Configuration config_;
434 std::string device_path_;
435 Thread rx_thread_;
436 Thread tx_thread_;
437 uint8_t* rx_buff_ = nullptr;
438 uint8_t* tx_buff_ = nullptr;
439 size_t buff_size_ = 0;
440 Semaphore write_sem_;
441 Mutex read_mutex_;
442
443 ReadPort _read_port; // NOLINT
444 WritePort _write_port; // NOLINT
445};
446
447} // namespace LibXR
Linux UART 串口驱动实现 / Linux UART driver implementation.
ErrorCode SetConfig(UART::Configuration config) override
设置 UART 配置 / Sets the UART configuration
LinuxUART(const char *dev_path, unsigned int baudrate=115200, Parity parity=Parity::NO_PARITY, uint8_t data_bits=8, uint8_t stop_bits=1, uint32_t tx_queue_size=5, size_t buffer_size=512, size_t thread_stack_size=65536)
通过设备路径构造 UART / Construct UART by device path
LinuxUART(const std::string &vid, const std::string &pid, unsigned int baudrate=115200, Parity parity=Parity::NO_PARITY, uint8_t data_bits=8, uint8_t stop_bits=1, uint32_t tx_queue_size=5, size_t buffer_size=512, size_t thread_stack_size=65536)
通过 USB VID/PID 构造 UART / Construct UART by USB VID/PID
ErrorCode PushBatch(const Data *data, size_t size)
批量推入数据 / Pushes multiple elements into the queue
ErrorCode PopBatch(Data *data, size_t size)
批量弹出数据 / Pops multiple elements from the queue
ReadPort class for handling read operations.
Definition libxr_rw.hpp:272
void ProcessPendingReads(bool in_isr)
Processes pending reads.
Definition libxr_rw.cpp:127
ErrorCode Wait(uint32_t timeout=UINT32_MAX)
等待(减少)信号量 Waits (decrements) the semaphore
Definition semaphore.cpp:25
@ REALTIME
实时优先级 Realtime priority
void Create(ArgType arg, void(*function)(ArgType arg), const char *name, size_t stack_depth, Thread::Priority priority)
创建新线程 Creates a new thread
Definition thread.hpp:66
static void Sleep(uint32_t milliseconds)
让线程进入休眠状态 Puts the thread to sleep
Definition thread.cpp:16
通用异步收发传输(UART)基类 / Abstract base class for Universal Asynchronous Receiver-Transmitter (UART)
Definition uart.hpp:19
ReadPort * read_port_
读取端口 / Read port
Definition uart.hpp:53
Parity
奇偶校验模式 / Parity mode
Definition uart.hpp:29
@ NO_PARITY
无校验 / No parity
@ ODD
奇校验 / Odd parity
@ EVEN
偶校验 / Even parity
WritePort * write_port_
写入端口 / Write port
Definition uart.hpp:54
void Finish(bool in_isr, ErrorCode ans, WriteInfoBlock &info)
更新写入操作的状态。 Updates the status of the write operation.
Definition libxr_rw.cpp:191
LibXR 命名空间
Definition ch32_can.hpp:14
ErrorCode(* ReadFun)(ReadPort &port, bool in_isr)
Function pointer type for read operations.
Definition libxr_rw.hpp:249
ErrorCode(* WriteFun)(WritePort &port, bool in_isr)
Function pointer type for write operations.
Definition libxr_rw.hpp:245
UART 配置结构体 / UART configuration structure.
Definition uart.hpp:44
uint8_t stop_bits
停止位长度 / Number of stop bits
Definition uart.hpp:50
Parity parity
校验模式 / Parity mode
Definition uart.hpp:47
uint8_t data_bits
数据位长度 / Number of data bits
Definition uart.hpp:48
uint32_t baudrate
波特率 / Baud rate
Definition uart.hpp:45