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.h"
4#include "driver/usb_serial_jtag_select.h"
5#include "esp_log.h"
6#include "freertos/FreeRTOS.h"
7#include "freertos/semphr.h"
8#include "freertos/task.h"
9#include "uart.hpp"
10
11namespace LibXR
12{
13template <size_t BUFFER_SIZE = 256>
14class ESP32VirtualUART : public UART
15{
16 public:
17 ESP32VirtualUART(uint32_t tx_queue_size = 5, int tx_task_prio = 10,
18 uint32_t tx_stack_depth = 2048, int rx_task_prio = 10,
19 uint32_t rx_stack_depth = 2048)
20 : UART(&_read_port, &_write_port),
21 _read_port(BUFFER_SIZE),
22 _write_port(tx_queue_size, BUFFER_SIZE)
23 {
24 usb_serial_jtag_driver_config_t cfg = {
25 .tx_buffer_size = BUFFER_SIZE,
26 .rx_buffer_size = BUFFER_SIZE,
27 };
28 ESP_ERROR_CHECK(usb_serial_jtag_driver_install(&cfg));
29
30 _write_port = WriteFun;
31 _read_port = ReadFun;
32
33 xTaskCreate(TxTaskWrapper, "esp32_vuart_tx", tx_stack_depth, this, tx_task_prio,
34 nullptr);
35 xTaskCreate(RxTaskWrapper, "esp32_vuart_rx", rx_stack_depth, this, rx_task_prio,
36 nullptr);
37 }
38
39 static void TxTaskWrapper(void *arg)
40 {
41 auto *self = static_cast<ESP32VirtualUART *>(arg);
42 self->TxTask(self);
43 }
44
45 static void RxTaskWrapper(void *arg)
46 {
47 auto *self = static_cast<ESP32VirtualUART *>(arg);
48 self->RxTask(self);
49 }
50
51 void TxTask(ESP32VirtualUART *uart)
52 {
53 WriteInfoBlock info;
54
55 while (true)
56 {
57 if (uart->write_sem_.Wait() != ErrorCode::OK)
58 {
59 continue;
60 }
61 if (uart->write_port_->queue_info_->Pop(info) != ErrorCode::OK)
62 {
63 continue;
64 }
65
66 if (uart->write_port_->queue_data_->PopBatch(uart->tx_buffer_, info.data.size_) !=
67 ErrorCode::OK)
68 {
69 uart->write_port_->Finish(false, ErrorCode::FAILED, info, info.data.size_);
70 continue;
71 }
72
73 int sent =
74 usb_serial_jtag_write_bytes(uart->tx_buffer_, info.data.size_, portMAX_DELAY);
75 if (sent == info.data.size_)
76 {
77 uart->write_port_->Finish(false, ErrorCode::OK, info, sent);
78 continue;
79 }
80 else
81 {
82 uart->write_port_->Finish(false, ErrorCode::FAILED, info, sent);
83 continue;
84 }
85 }
86 }
87
88 void RxTask(ESP32VirtualUART *uart)
89 {
90 ReadInfoBlock block;
91
92 while (true)
93 {
94 auto avail = usb_serial_jtag_read_ready();
95 int len = 0;
96 if (!avail)
97 {
98 len = usb_serial_jtag_read_bytes(uart->rx_buffer_, 1, portMAX_DELAY);
99 }
100 else
101 {
102 len = usb_serial_jtag_read_bytes(uart->rx_buffer_, BUFFER_SIZE, 0);
103 }
104 if (len > 0)
105 {
106 uart->read_port_->queue_data_->PushBatch(uart->rx_buffer_, len);
107 uart->read_port_->ProcessPendingReads(false);
108 }
109 }
110 }
111
112 static ErrorCode WriteFun(WritePort &port)
113 {
114 ESP32VirtualUART *uart = CONTAINER_OF(&port, ESP32VirtualUART, _write_port);
115 uart->write_sem_.Post();
116
117 return ErrorCode::FAILED;
118 }
119
120 static ErrorCode ReadFun(ReadPort &port)
121 {
122 UNUSED(port);
123
124 return ErrorCode::EMPTY;
125 }
126
127 ErrorCode SetConfig(UART::Configuration) override { return ErrorCode::OK; }
128
129 private:
130 uint8_t tx_buffer_[BUFFER_SIZE];
131 uint8_t rx_buffer_[BUFFER_SIZE];
132
133 LibXR::Semaphore write_sem_;
134
135 ReadPort _read_port;
136 WritePort _write_port;
137};
138
139} // namespace LibXR
size_t size_
数据大小(字节)。 The size of the data (in bytes).
ErrorCode SetConfig(UART::Configuration) override
设置 UART 配置 / Sets the UART configuration
Definition esp_usb.hpp:127
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:270
virtual void ProcessPendingReads(bool in_isr)
Processes pending reads.
Definition libxr_rw.hpp:501
信号量类,实现线程同步机制 Semaphore class implementing thread synchronization
Definition semaphore.hpp:23
void Post()
释放(增加)信号量 Releases (increments) the semaphore
Definition semaphore.cpp:23
ErrorCode Wait(uint32_t timeout=UINT32_MAX)
等待(减少)信号量 Waits (decrements) the semaphore
Definition semaphore.cpp:25
通用异步收发传输(UART)基类 / Abstract base class for Universal Asynchronous Receiver-Transmitter (UART)
Definition uart.hpp:19
ReadPort * read_port_
读取端口 / Read port
Definition uart.hpp:51
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
void Finish(bool in_isr, ErrorCode ans, WriteInfoBlock &info, uint32_t size)
更新写入操作的状态。 Updates the status of the write operation.
Definition libxr_rw.hpp:665
LibXR 命名空间
Definition ch32_gpio.hpp:9
ErrorCode(* ReadFun)(ReadPort &port)
Function pointer type for read operations.
Definition libxr_rw.hpp:247
ErrorCode(* WriteFun)(WritePort &port)
Function pointer type for write operations.
Definition libxr_rw.hpp:243
Read information block structure.
Definition libxr_rw.hpp:254
UART 配置结构体 / UART configuration structure.
Definition uart.hpp:44