libxr 1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
esp_usb.hpp
1#pragma once
2
3#include "driver/usb_serial_jtag_select.h"
4
5#include "driver/usb_serial_jtag.h"
6#include "esp_log.h"
7#include "freertos/FreeRTOS.h"
8#include "freertos/semphr.h"
9#include "freertos/task.h"
10#include "uart.hpp"
11
12namespace LibXR
13{
14template <size_t BUFFER_SIZE = 256>
15class ESP32VirtualUART : public UART
16{
17 public:
18 ESP32VirtualUART(uint32_t rx_queue_size = 5, uint32_t tx_queue_size = 5,
19 int tx_task_prio = 10, uint32_t tx_stack_depth = 2048,
20 int rx_task_prio = 10, uint32_t rx_stack_depth = 2048)
21 : UART(rx_queue_size, BUFFER_SIZE, tx_queue_size, BUFFER_SIZE)
22 {
23 usb_serial_jtag_driver_config_t cfg = {
24 .tx_buffer_size = BUFFER_SIZE,
25 .rx_buffer_size = BUFFER_SIZE,
26 };
27 ESP_ERROR_CHECK(usb_serial_jtag_driver_install(&cfg));
28
31
32 xTaskCreate(TxTaskWrapper, "esp32_vuart_tx", tx_stack_depth, this, tx_task_prio,
33 nullptr);
34 xTaskCreate(RxTaskWrapper, "esp32_vuart_rx", rx_stack_depth, this, rx_task_prio,
35 nullptr);
36 }
37
38 static void TxTaskWrapper(void *arg)
39 {
40 auto *self = static_cast<ESP32VirtualUART *>(arg);
41 self->TxTask(self);
42 }
43
44 static void RxTaskWrapper(void *arg)
45 {
46 auto *self = static_cast<ESP32VirtualUART *>(arg);
47 self->RxTask(self);
48 }
49
50 void TxTask(ESP32VirtualUART *uart)
51 {
53
54 while (true)
55 {
56 if (uart->write_sem_.Wait() != ErrorCode::OK)
57 {
58 continue;
59 }
60 if (uart->write_port_.queue_info_->Pop(info) != ErrorCode::OK)
61 {
62 continue;
63 }
64
65 if (uart->write_port_.queue_data_->PopBatch(uart->tx_buffer_, info.size) !=
66 ErrorCode::OK)
67 {
68 info.op.UpdateStatus(false, ErrorCode::EMPTY);
69 continue;
70 }
71
72 int sent = usb_serial_jtag_write_bytes(uart->tx_buffer_, info.size, portMAX_DELAY);
73 if (sent > 0)
74 {
75 info.op.UpdateStatus(true, ErrorCode::OK);
76 continue;
77 }
78 else
79 {
80 info.op.UpdateStatus(false, ErrorCode::FAILED);
81 continue;
82 }
83 }
84 }
85
86 void RxTask(ESP32VirtualUART *uart)
87 {
88 ReadInfoBlock block;
89
90 while (true)
91 {
92 auto avail = usb_serial_jtag_read_ready();
93 int len = 0;
94 if (!avail)
95 {
96 len = usb_serial_jtag_read_bytes(uart->rx_buffer_, 1, portMAX_DELAY);
97 }
98 else
99 {
100 len = usb_serial_jtag_read_bytes(uart->rx_buffer_, BUFFER_SIZE, 0);
101 }
102 if (len > 0)
103 {
104 LibXR::Mutex::LockGuard guard(uart->read_mutex_);
105 uart->read_port_.queue_data_->PushBatch(uart->rx_buffer_, len);
107 }
108 }
109 }
110
111 static ErrorCode WriteFun(WritePort &port)
112 {
113 ESP32VirtualUART *uart = CONTAINER_OF(&port, ESP32VirtualUART, write_port_);
114 uart->write_sem_.Post();
115
116 return ErrorCode::OK;
117 }
118
119 static ErrorCode ReadFun(ReadPort &port)
120 {
121 ESP32VirtualUART *uart = CONTAINER_OF(&port, ESP32VirtualUART, read_port_);
122
123 LibXR::Mutex::LockGuard guard(uart->read_mutex_);
124
125 port.ProcessPendingReads();
126
127 return ErrorCode::OK;
128 }
129
130 ErrorCode SetConfig(UART::Configuration) override { return ErrorCode::OK; }
131
132 private:
133 uint8_t tx_buffer_[BUFFER_SIZE];
134 uint8_t rx_buffer_[BUFFER_SIZE];
135
136 LibXR::Semaphore write_sem_;
137 LibXR::Mutex read_mutex_;
138};
139
140} // namespace LibXR
ErrorCode PushBatch(const void *data, size_t size)
批量推入多个元素 (Push multiple elements into the queue).
Definition queue.hpp:175
ErrorCode SetConfig(UART::Configuration) override
设置 UART 配置 / Sets the UART configuration
Definition esp_usb.hpp:130
ErrorCode PopBatch(Data *data, size_t batch_size)
批量弹出数据 / Pops multiple elements from the queue
互斥锁的 RAII 机制封装 (RAII-style mechanism for automatic mutex management).
Definition mutex.hpp:95
互斥锁类,提供线程同步机制 (Mutex class providing thread synchronization mechanisms).
Definition mutex.hpp:18
void UpdateStatus(bool in_isr, Args &&...args)
Updates operation status based on type.
Definition libxr_rw.hpp:207
Read information block structure.
Definition libxr_rw.hpp:288
ReadPort class for handling read operations.
Definition libxr_rw.hpp:311
void ProcessPendingReads()
Processes pending reads.
Definition libxr_rw.hpp:471
信号量类,实现线程同步机制 Semaphore class implementing thread synchronization
Definition semaphore.hpp:23
void Post()
释放(增加)信号量 Releases (increments) the semaphore
Definition semaphore.cpp:13
ErrorCode Wait(uint32_t timeout=UINT32_MAX)
等待(减少)信号量 Waits (decrements) the semaphore
Definition semaphore.cpp:15
通用异步收发传输(UART)基类 / Abstract base class for Universal Asynchronous Receiver-Transmitter (UART)
Definition uart.hpp:19
WritePort write_port_
写入端口 / Write port
Definition uart.hpp:52
ReadPort read_port_
读取端口 / Read port
Definition uart.hpp:51
WritePort class for handling write operations.
Definition libxr_rw.hpp:505
LibXR Color Control Library / LibXR终端颜色控制库
Definition esp_gpio.hpp:8
ErrorCode(* ReadFun)(ReadPort &port)
Function pointer type for read operations.
Definition libxr_rw.hpp:281
ErrorCode(* WriteFun)(WritePort &port)
Function pointer type for write operations.
Definition libxr_rw.hpp:277
UART 配置结构体 / UART configuration structure.
Definition uart.hpp:44