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 <algorithm>
17#include <filesystem>
18#include <system_error>
19#include <vector>
20
21#include "libxr_def.hpp"
22#include "libxr_rw.hpp"
23#include "semaphore.hpp"
24#include "uart.hpp"
25
26namespace LibXR
27{
36class LinuxUART : public UART
37{
38 public:
42 LinuxUART(const char* dev_path, unsigned int baudrate = 115200,
43 Parity parity = Parity::NO_PARITY, uint8_t data_bits = 8,
44 uint8_t stop_bits = 1, uint32_t tx_queue_size = 5, size_t buffer_size = 512,
45 size_t thread_stack_size = 65536)
46 : UART(&_read_port, &_write_port),
47 rx_buff_(new uint8_t[buffer_size]),
48 tx_buff_(new uint8_t[buffer_size]),
49 buff_size_(buffer_size),
50 _read_port(buffer_size),
51 _write_port(tx_queue_size, buffer_size)
52 {
53 ASSERT(buff_size_ > 0);
54
55 while (!std::filesystem::exists(dev_path))
56 {
57 XR_LOG_WARN("Cannot find UART device: %s, retrying...", dev_path);
58 Thread::Sleep(100);
59 }
60
61 device_path_ = GetByPathForTTY(dev_path);
62
63 fd_ = open(device_path_.c_str(), O_RDWR | O_NOCTTY);
64 if (fd_ < 0)
65 {
66 XR_LOG_ERROR("Cannot open UART device: %s", device_path_.c_str());
67 ASSERT(false);
68 }
69 else
70 {
71 XR_LOG_PASS("Open UART device: %s", device_path_.c_str());
72 }
73
74 config_ = {};
75 config_.baudrate = baudrate;
76 config_.parity = parity;
77 config_.data_bits = data_bits;
78 config_.stop_bits = stop_bits;
79
80 SetConfig(config_);
81
82 _read_port = ReadFun;
83 _write_port = WriteFun;
84
85 rx_thread_.Create<LinuxUART*>(
86 this, [](LinuxUART* self) { self->RxLoop(); }, "rx_uart", thread_stack_size,
88
89 tx_thread_.Create<LinuxUART*>(
90 this, [](LinuxUART* self) { self->TxLoop(); }, "tx_uart", thread_stack_size,
92 }
93
97 LinuxUART(const std::string& vid, const std::string& pid,
98 unsigned int baudrate = 115200, Parity parity = Parity::NO_PARITY,
99 uint8_t data_bits = 8, uint8_t stop_bits = 1, uint32_t tx_queue_size = 5,
100 size_t buffer_size = 512, size_t thread_stack_size = 65536)
101 : LinuxUART(vid, pid, "", "", baudrate, parity, data_bits, stop_bits,
102 tx_queue_size, buffer_size, thread_stack_size)
103 {
104 }
105
116 LinuxUART(const std::string& vid, const std::string& pid,
117 const std::string& control_interface_name,
118 unsigned int baudrate = 115200, Parity parity = Parity::NO_PARITY,
119 uint8_t data_bits = 8, uint8_t stop_bits = 1, uint32_t tx_queue_size = 5,
120 size_t buffer_size = 512, size_t thread_stack_size = 65536)
121 : LinuxUART(vid, pid, control_interface_name, "", baudrate, parity, data_bits,
122 stop_bits, tx_queue_size, buffer_size, thread_stack_size)
123 {
124 }
125
136 LinuxUART(const std::string& vid, const std::string& pid,
137 const std::string& control_interface_name, const std::string& serial,
138 unsigned int baudrate = 115200, Parity parity = Parity::NO_PARITY,
139 uint8_t data_bits = 8, uint8_t stop_bits = 1, uint32_t tx_queue_size = 5,
140 size_t buffer_size = 512, size_t thread_stack_size = 65536)
141 : UART(&_read_port, &_write_port),
142 rx_buff_(new uint8_t[buffer_size]),
143 tx_buff_(new uint8_t[buffer_size]),
144 buff_size_(buffer_size),
145 _read_port(buffer_size),
146 _write_port(tx_queue_size, buffer_size)
147 {
148 while (!FindUSBTTYByVidPid(vid, pid, control_interface_name, serial, device_path_))
149 {
150 XR_LOG_WARN(
151 "Cannot find USB TTY device with VID=%s PID=%s SERIAL=%s CONTROL_INTERFACE=%s, retrying...",
152 vid.c_str(), pid.c_str(), serial.empty() ? "*" : serial.c_str(),
153 control_interface_name.empty() ? "*" : control_interface_name.c_str());
154 Thread::Sleep(100);
155 }
156
157 XR_LOG_PASS("Found USB TTY: %s", device_path_.c_str());
158
159 if (std::filesystem::exists(device_path_) == false)
160 {
161 XR_LOG_ERROR("Cannot find UART device: %s", device_path_.c_str());
162 ASSERT(false);
163 return;
164 }
165
166 device_path_ = GetByPathForTTY(device_path_);
167
168 fd_ = open(device_path_.c_str(), O_RDWR | O_NOCTTY);
169 if (fd_ < 0)
170 {
171 XR_LOG_ERROR("Cannot open UART device: %s", device_path_.c_str());
172 ASSERT(false);
173 }
174 else
175 {
176 XR_LOG_PASS("Open UART device: %s", device_path_.c_str());
177 }
178
179 config_ = {};
180 config_.baudrate = baudrate;
181 config_.parity = parity;
182 config_.data_bits = data_bits;
183 config_.stop_bits = stop_bits;
184
185 SetConfig(config_);
186
187 _read_port = ReadFun;
188 _write_port = WriteFun;
189
190 rx_thread_.Create<LinuxUART*>(
191 this, [](LinuxUART* self) { self->RxLoop(); }, "rx_uart", thread_stack_size,
193
194 tx_thread_.Create<LinuxUART*>(
195 this, [](LinuxUART* self) { self->TxLoop(); }, "tx_uart", thread_stack_size,
197 }
198
199 std::string GetByPathForTTY(const std::string& tty_name)
200 {
201 const std::string BASE = "/dev/serial/by-path";
202 if (strncmp(tty_name.c_str(), BASE.c_str(), BASE.length()) == 0 ||
203 !std::filesystem::exists(BASE))
204 {
205 return tty_name;
206 }
207 for (const auto& entry : std::filesystem::directory_iterator(BASE))
208 {
209 std::error_code ec;
210 const auto full = std::filesystem::canonical(entry.path(), ec);
211 if (ec)
212 {
213 continue;
214 }
215 if (full == tty_name)
216 {
217 return entry.path().string(); // 返回符号链接路径
218 }
219 }
220 return tty_name; // 未命中 by-path 时保留原始 tty 路径
221 }
222
223 static bool FindUSBTTYByVidPid(const std::string& target_vid,
224 const std::string& target_pid,
225 std::string& tty_path)
226 {
227 return FindUSBTTYByVidPid(target_vid, target_pid, "", "", tty_path);
228 }
229
230 static bool FindUSBTTYByVidPid(const std::string& target_vid,
231 const std::string& target_pid,
232 const std::string& target_control_interface_name,
233 std::string& tty_path)
234 {
235 return FindUSBTTYByVidPid(target_vid, target_pid, target_control_interface_name, "",
236 tty_path);
237 }
238
239 static bool FindUSBTTYByVidPid(const std::string& target_vid,
240 const std::string& target_pid,
241 const std::string& target_control_interface_name,
242 const std::string& target_serial, std::string& tty_path)
243 {
244 struct udev* udev = udev_new();
245 if (!udev)
246 {
247 XR_LOG_ERROR("Cannot create udev context");
248 return false;
249 }
250
251 struct udev_enumerate* enumerate = udev_enumerate_new(udev);
252 udev_enumerate_add_match_subsystem(enumerate, "tty");
253 udev_enumerate_scan_devices(enumerate);
254
255 struct udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate);
256 struct udev_list_entry* entry = nullptr;
257 std::vector<std::string> matches;
258
259 udev_list_entry_foreach(entry, devices)
260 {
261 const char* path = udev_list_entry_get_name(entry);
262 struct udev_device* tty_dev = udev_device_new_from_syspath(udev, path);
263 if (!tty_dev)
264 {
265 continue;
266 }
267
268 struct udev_device* usb_dev =
269 udev_device_get_parent_with_subsystem_devtype(tty_dev, "usb", "usb_device");
270 struct udev_device* usb_interface = udev_device_get_parent_with_subsystem_devtype(
271 tty_dev, "usb", "usb_interface");
272
273 if (usb_dev)
274 {
275 const char* vid = udev_device_get_sysattr_value(usb_dev, "idVendor");
276 const char* pid = udev_device_get_sysattr_value(usb_dev, "idProduct");
277 const char* serial = udev_device_get_sysattr_value(usb_dev, "serial");
278 const char* control_interface_name = nullptr;
279 if (usb_interface)
280 {
281 // For Linux cdc_acm tty nodes this parent is the CDC control interface
282 // (for example if00 / if02), not the CDC data interface.
283 control_interface_name =
284 udev_device_get_sysattr_value(usb_interface, "interface");
285 }
286
287 if (vid && pid && target_vid == vid && target_pid == pid &&
288 (target_serial.empty() || (serial && target_serial == serial)) &&
289 (target_control_interface_name.empty() ||
290 (control_interface_name &&
291 target_control_interface_name == control_interface_name)))
292 {
293 const char* devnode = udev_device_get_devnode(tty_dev);
294 if (devnode)
295 {
296 matches.emplace_back(devnode);
297 }
298 }
299 }
300
301 udev_device_unref(tty_dev);
302 }
303
304 udev_enumerate_unref(enumerate);
305 udev_unref(udev);
306 if (matches.empty())
307 {
308 return false;
309 }
310
311 std::sort(matches.begin(), matches.end());
312 tty_path = matches.front();
313
314 if (matches.size() > 1)
315 {
316 XR_LOG_WARN(
317 "Multiple USB TTY devices found with VID=%s PID=%s SERIAL=%s CONTROL_INTERFACE=%s, using %s. Specify serial or control interface name to disambiguate.",
318 target_vid.c_str(), target_pid.c_str(),
319 target_serial.empty() ? "*" : target_serial.c_str(),
320 target_control_interface_name.empty() ? "*"
321 : target_control_interface_name.c_str(),
322 tty_path.c_str());
323 }
324
325 return true;
326 }
327
328 void SetLowLatency(int fd)
329 {
330 struct serial_struct serinfo;
331 ioctl(fd, TIOCGSERIAL, &serinfo);
332 serinfo.flags |= ASYNC_LOW_LATENCY;
333 ioctl(fd, TIOCSSERIAL, &serinfo);
334 }
335
337 {
338 if (&config != &config_)
339 {
340 config_ = config;
341 }
342
343 struct termios2 tio{};
344 if (ioctl(fd_, TCGETS2, &tio) != 0)
345 {
346 return ErrorCode::INIT_ERR;
347 }
348
349 // 设置自定义波特率
350 tio.c_cflag &= ~CBAUD;
351 tio.c_cflag |= BOTHER;
352 tio.c_ispeed = config.baudrate;
353 tio.c_ospeed = config.baudrate;
354
355 // 输入模式:关闭软件流控、特殊字符处理
356 tio.c_iflag &= ~(IXON | IXOFF | IXANY | ISTRIP | IGNCR | INLCR | ICRNL
357#ifdef IUCLC
358 | IUCLC
359#endif
360 );
361
362 // 输出模式:关闭所有加工
363 tio.c_oflag &= ~(OPOST
364#ifdef ONLCR
365 | ONLCR
366#endif
367#ifdef OCRNL
368 | OCRNL
369#endif
370#ifdef ONOCR
371 | ONOCR
372#endif
373#ifdef ONLRET
374 | ONLRET
375#endif
376 );
377
378 // 本地模式:禁用行缓冲、回显、信号中断
379 tio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
380
381 // 控制模式:设置数据位、校验、停止位、流控
382 tio.c_cflag &= ~CSIZE;
383 switch (config.data_bits)
384 {
385 case 5:
386 tio.c_cflag |= CS5;
387 break;
388 case 6:
389 tio.c_cflag |= CS6;
390 break;
391 case 7:
392 tio.c_cflag |= CS7;
393 break;
394 case 8:
395 tio.c_cflag |= CS8;
396 break;
397 default:
398 return ErrorCode::ARG_ERR;
399 }
400
401 // 停止位
402 tio.c_cflag &= ~CSTOPB;
403 if (config.stop_bits == 2)
404 {
405 tio.c_cflag |= CSTOPB;
406 }
407
408 // 奇偶校验
409 switch (config.parity)
410 {
412 tio.c_cflag &= ~PARENB;
413 break;
415 tio.c_cflag |= PARENB;
416 tio.c_cflag &= ~PARODD;
417 break;
419 tio.c_cflag |= PARENB;
420 tio.c_cflag |= PARODD;
421 break;
422 }
423
424 // 禁用硬件流控
425 tio.c_cflag &= ~CRTSCTS;
426
427 // 启用本地模式、读功能
428 tio.c_cflag |= (CLOCAL | CREAD);
429
430 // 控制字符配置:阻塞直到读到 1 字节
431 // for (int i = 0; i < NCCS; ++i) tio.c_cc[i] = 0;
432 tio.c_cc[VTIME] = 0;
433 tio.c_cc[VMIN] = 1;
434
435 if (ioctl(fd_, TCSETS2, &tio) != 0)
436 {
437 return ErrorCode::INIT_ERR;
438 }
439
440 SetLowLatency(fd_);
441
442 tcflush(fd_, TCIOFLUSH);
443
444 return ErrorCode::OK;
445 }
446
447 static ErrorCode ReadFun(ReadPort&, bool) { return ErrorCode::PENDING; }
448
449 static ErrorCode WriteFun(WritePort& port, bool)
450 {
451 auto* uart = LibXR::ContainerOf(&port, &LinuxUART::_write_port);
452 uart->write_sem_.Post();
453 return ErrorCode::PENDING;
454 }
455
456 private:
457 void RxLoop()
458 {
459 while (true)
460 {
461 if (!connected_)
462 {
463 close(fd_);
464 fd_ = open(device_path_.c_str(), O_RDWR | O_NOCTTY);
465
466 if (fd_ < 0)
467 {
468 XR_LOG_WARN("Cannot open UART device: %s", device_path_.c_str());
469 Thread::Sleep(1000);
470 }
471 else
472 {
473 SetConfig(config_);
474 XR_LOG_PASS("Reopen UART device: %s", device_path_.c_str());
475 connected_ = true;
476 }
477 }
478 auto n = read(fd_, rx_buff_, buff_size_);
479 if (n > 0)
480 {
481 read_port_->queue_data_->PushBatch(rx_buff_, n);
483 }
484 else
485 {
486 XR_LOG_WARN("Cannot read UART device: %s", device_path_.c_str());
487 connected_ = false;
488 }
489 }
490 }
491
492 void TxLoop()
493 {
494 WriteInfoBlock info;
495 while (true)
496 {
497 if (!connected_)
498 {
499 Thread::Sleep(1);
500 continue;
501 }
502
503 if (write_sem_.Wait() != ErrorCode::OK)
504 {
505 continue;
506 }
507
508 if (write_port_->queue_info_->Pop(info) == ErrorCode::OK)
509 {
510 if (write_port_->queue_data_->PopBatch(tx_buff_, info.data.size_) ==
512 {
513 auto written = write(fd_, tx_buff_, info.data.size_);
514 if (written < 0)
515 {
516 XR_LOG_WARN("Cannot write UART device: %s", device_path_.c_str());
517 connected_ = false;
518 }
519 write_port_->Finish(false,
520 (written == static_cast<int>(info.data.size_))
523 info);
524 }
525 else
526 {
527 info.op.UpdateStatus(false, ErrorCode::FAILED);
528 }
529 }
530 }
531 }
532
533 int fd_ = -1;
534 bool connected_ = true;
535 Configuration config_;
536 std::string device_path_;
537 Thread rx_thread_;
538 Thread tx_thread_;
539 uint8_t* rx_buff_ = nullptr;
540 uint8_t* tx_buff_ = nullptr;
541 size_t buff_size_ = 0;
542 Semaphore write_sem_;
543 Mutex read_mutex_;
544
545 ReadPort _read_port; // NOLINT
546 WritePort _write_port; // NOLINT
547};
548
549} // 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, const std::string &control_interface_name, const std::string &serial, 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/control interface name/serial 构造 UART Construct UART by USB VID/PID/control interface ...
LinuxUART(const std::string &vid, const std::string &pid, const std::string &control_interface_name, 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/control interface name 构造 UART Construct UART by USB VID/PID/control interface name
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 read_port.hpp:18
LockFreeQueue< uint8_t > * queue_data_
RX payload queue. 接收数据字节队列。
Definition read_port.hpp:50
void ProcessPendingReads(bool in_isr)
Processes pending reads.
ErrorCode Wait(uint32_t timeout=UINT32_MAX)
等待(减少)信号量 Waits (decrements) the semaphore
Definition semaphore.cpp:53
@ 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:68
static void Sleep(uint32_t milliseconds)
让线程进入休眠状态 Puts the thread to sleep
Definition thread.cpp:15
通用异步收发传输(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.
LockFreeQueue< uint8_t > * queue_data_
Payload queue for pending write bytes. 挂起写入字节的数据队列。
LockFreeQueue< WriteInfoBlock > * queue_info_
Metadata queue for pending write batches. 挂起写批次的元数据队列。
LibXR 命名空间
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举
@ INIT_ERR
初始化错误 | Initialization error
@ FAILED
操作失败 | Operation failed
@ PENDING
等待中 | Pending
@ OK
操作成功 | Operation successful
@ ARG_ERR
参数错误 | Argument error
ErrorCode(* ReadFun)(ReadPort &port, bool in_isr)
Function pointer type for read notifications.
ErrorCode(* WriteFun)(WritePort &port, bool in_isr)
Function pointer type for write operations.
OwnerType * ContainerOf(MemberType *ptr, MemberType OwnerType::*member) noexcept
通过成员指针恢复其所属对象指针
Definition libxr_def.hpp:79
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