libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
uart.hpp
1#pragma once
2
3#include "libxr.hpp"
4#include "libxr_rw.hpp"
5
6namespace LibXR
7{
8
18class UART
19{
20 public:
28 enum class Parity : uint8_t
29 {
30 NO_PARITY = 0,
31 EVEN = 1,
32 ODD = 2
33 };
34
44 {
45 uint32_t baudrate;
47 uint8_t data_bits;
48 uint8_t stop_bits;
49 };
50
53
63 template <typename ReadPortType = ReadPort, typename WritePortType = WritePort>
64 UART(ReadPortType* read_port, WritePortType* write_port)
65 : read_port_(read_port), write_port_(write_port)
66 {
67 }
68
79 virtual ErrorCode SetConfig(Configuration config) = 0;
80
81 template <typename OperationType, typename = std::enable_if_t<std::is_base_of_v<
82 WriteOperation, std::decay_t<OperationType>>>>
83 ErrorCode Write(ConstRawData data, OperationType&& op)
84 {
85 return (*write_port_)(data, std::forward<OperationType>(op));
86 }
87
88 template <typename OperationType, typename = std::enable_if_t<std::is_base_of_v<
89 ReadOperation, std::decay_t<OperationType>>>>
90 ErrorCode Read(RawData data, OperationType&& op)
91 {
92 return (*read_port_)(data, std::forward<OperationType>(op));
93 }
94};
95
96} // namespace LibXR
常量原始数据封装类。 A class for encapsulating constant raw data.
原始数据封装类。 A class for encapsulating raw data.
ReadPort class for handling read operations.
Definition libxr_rw.hpp:270
通用异步收发传输(UART)基类 / Abstract base class for Universal Asynchronous Receiver-Transmitter (UART)
Definition uart.hpp:19
ReadPort * read_port_
读取端口 / Read port
Definition uart.hpp:51
Parity
奇偶校验模式 / Parity mode
Definition uart.hpp:29
@ NO_PARITY
无校验 / No parity
@ ODD
奇校验 / Odd parity
@ EVEN
偶校验 / Even parity
virtual ErrorCode SetConfig(Configuration config)=0
设置 UART 配置 / Sets the UART configuration
WritePort * write_port_
写入端口 / Write port
Definition uart.hpp:52
UART(ReadPortType *read_port, WritePortType *write_port)
UART 构造函数 / UART constructor.
Definition uart.hpp:64
WritePort class for handling write operations.
Definition libxr_rw.hpp:564
LibXR 命名空间
Definition ch32_gpio.hpp:9
Operation< ErrorCode > ReadOperation
Read operation type.
Definition libxr_rw.hpp:235
Operation< ErrorCode > WriteOperation
Write operation type.
Definition libxr_rw.hpp:239
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
uint8_t data_bits
数据位长度 / Number of data bits
Definition uart.hpp:47
uint32_t baudrate
波特率 / Baud rate
Definition uart.hpp:45