libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
esp_uart.hpp
1#pragma once
2
3#include "driver/uart.h"
4#include "freertos/queue.h"
5#include "freertos/semphr.h"
6#include "libxr_def.hpp"
7#include "libxr_rw.hpp"
8#include "uart.hpp"
9
10namespace LibXR
11{
12class ESP32UART;
14{
15 public:
16 ESP32UARTReadPort(size_t buffer_size, ESP32UART *uart)
17 : ReadPort(buffer_size), uart_(uart)
18 {
19 }
20
21 size_t EmptySize();
22
23 size_t Size();
24
25 void ProcessPendingReads(bool in_isr = true);
26
27 void Reset() { read_size_ = 0; }
28
29 using ReadPort::operator=;
30
31 LibXR::Mutex mutex_;
32 ESP32UART *uart_;
33};
34
36{
37 public:
38 ESP32UARTWritePort(size_t queue_size, size_t buffer_size, ESP32UART *uart)
39 : WritePort(queue_size, buffer_size), uart_(uart)
40 {
41 }
42
43 size_t EmptySize();
44
45 size_t Size();
46
47 void Reset() { write_size_ = 0; }
48
49 using WritePort::operator=;
50
51 private:
52 ESP32UART *uart_;
53};
54
55class ESP32UART : public UART
56{
57 public:
58 ESP32UART(uart_port_t port, int tx_pin, int rx_pin, uint32_t buffer_size = 256,
59 uint32_t rx_thread_stack_depth = 1024, uint32_t rx_thread_priority = 10);
60
61 static ErrorCode WriteFun(WritePort &port);
62 static ErrorCode ReadFun(ReadPort &port);
63
64 ErrorCode SetConfig(UART::Configuration config) override;
65
66 friend class ESP32UARTReadPort;
67 friend class ESP32UARTWritePort;
68
69 private:
70 static void RxTask(void *param);
71 static void TxTask(void *param);
72 void HandleEvent(const uart_event_t &event);
73
74 uart_port_t port_;
75 QueueHandle_t event_queue_;
76 RawData rx_buff_;
77 RawData tx_buff_;
78
79 ESP32UARTReadPort _read_port;
80 ESP32UARTWritePort _write_port;
81};
82
83} // namespace LibXR
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 Reset()
Resets the ReadPort.
Definition esp_uart.hpp:27
void ProcessPendingReads(bool in_isr=true)
Processes pending reads.
Definition esp_uart.cpp:21
void Reset()
Resets the WritePort.
Definition esp_uart.hpp:47
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
互斥锁类,提供线程同步机制 (Mutex class providing thread synchronization mechanisms).
Definition mutex.hpp:18
原始数据封装类。 A class for encapsulating raw data.
ReadPort class for handling read operations.
Definition libxr_rw.hpp:268
ReadPort(size_t buffer_size=128)
Constructs a ReadPort with queue sizes.
Definition libxr_rw.cpp:10
通用异步收发传输(UART)基类 / Abstract base class for Universal Asynchronous Receiver-Transmitter (UART)
Definition uart.hpp:19
WritePort class for handling write operations.
Definition libxr_rw.hpp:402
WritePort(size_t queue_size=3, size_t buffer_size=128)
构造一个新的 WritePort 对象。 Constructs a new WritePort object.
Definition libxr_rw.cpp:179
LibXR 命名空间
Definition ch32_gpio.hpp:9
ErrorCode(* ReadFun)(ReadPort &port)
Function pointer type for read operations.
Definition libxr_rw.hpp:245
ErrorCode(* WriteFun)(WritePort &port)
Function pointer type for write operations.
Definition libxr_rw.hpp:241
UART 配置结构体 / UART configuration structure.
Definition uart.hpp:44