libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
esp_usb_dev.hpp
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5#include <initializer_list>
6
7#include "esp_def.hpp"
8#include "esp_intr_alloc.h"
9#include "libxr_type.hpp"
10#include "usb/core/ep_pool.hpp"
11#include "usb/device/dev_core.hpp"
12
13#if (SOC_USB_OTG_SUPPORTED && defined(CONFIG_IDF_TARGET_ESP32S3) && \
14 CONFIG_IDF_TARGET_ESP32S3) || \
15 defined(__DOXYGEN__)
16
17namespace LibXR
18{
19
20class ESP32USBEndpoint;
21
26{
27 public:
31 struct EPConfig
32 {
37 enum class DirectionHint : int8_t
38 {
40 -1,
41 OutOnly = 0,
42 InOnly = 1,
43 };
44
50
51 EPConfig() = delete;
52
58
68 };
69
83 const std::initializer_list<EPConfig> ep_cfgs,
84 USB::DeviceDescriptor::PacketSize0 packet_size, uint16_t vid, uint16_t pid,
85 uint16_t bcd,
86 const std::initializer_list<const USB::DescriptorStrings::LanguagePack*> lang_list,
87 const std::initializer_list<const std::initializer_list<USB::ConfigDescriptorItem*>>
88 configs,
89 ConstRawData uid = {nullptr, 0});
90
94 void Init(bool in_isr) override;
95
99 void Deinit(bool in_isr) override;
100
105 ErrorCode SetAddress(uint8_t address, USB::DeviceCore::Context context) override;
106
110 void Start(bool in_isr) override;
111
115 void Stop(bool in_isr) override;
116
117 private:
118 friend class ESP32USBEndpoint;
119
120 static constexpr uint8_t ENDPOINT_COUNT = 7;
121 static constexpr uint8_t IN_ENDPOINT_LIMIT = 5;
122 static constexpr uint32_t INTERRUPT_DISPATCH_GUARD = 64U;
123 static constexpr size_t SETUP_PACKET_BYTES = 8U;
124 static constexpr size_t SETUP_DMA_BUFFER_BYTES = 64U;
125
129 struct ControlState
130 {
131 bool setup_direction_out =
132 false;
134 };
135
140 struct EndpointMap
141 {
142 USB::Endpoint* in[ENDPOINT_COUNT] =
143 {};
144 USB::Endpoint* out[ENDPOINT_COUNT] =
145 {};
146 };
147
152 struct FifoState
153 {
154 uint16_t depth_words =
155 0U;
156 uint16_t rx_words =
157 0U;
158 uint16_t tx_next_words =
159 0U;
160 uint16_t tx_words[ENDPOINT_COUNT] =
161 {};
163 bool tx_bound[ENDPOINT_COUNT] = {};
165 uint8_t allocated_in = 0U;
167 };
168
172 struct RuntimeState
173 {
174 intr_handle_t intr_handle =
175 nullptr;
176 void* phy_handle = nullptr;
177 bool phy_ready = false;
178 bool irq_ready =
179 false;
180 bool started = false;
181 bool rom_usb_cleaned = false;
183 };
184
188 static void IRAM_ATTR IsrEntry(void* arg);
189
193 bool EnsurePhyReady();
194
198 bool EnsureInterruptReady();
199
203 void EnsureRomUsbCleaned();
204
208 void InitializeCore();
209
213 void ClearTxFifoRegisters();
214
218 void FlushFifos();
219
223 void ResetFifoState();
224
228 void ResetDeviceState();
229
233 void ResetEndpointHardwareState();
234
238 void ReloadSetupPacketCount();
239
243 void HandleInterrupt();
244
248 void HandleBusReset(bool in_isr);
249
253 void HandleEndpointInterrupt(bool in_isr, bool in_dir);
254
258 void HandleRxFifoLevel();
259
263 void ResetControlState();
264
268 void UpdateSetupState(const uint8_t* setup);
269
274 bool LastSetupDirectionOut() const { return control_.setup_direction_out; }
275
279 bool DmaEnabled() const { return true; }
280
284 bool AllocateTxFifo(uint8_t ep_num, uint16_t packet_size, bool is_bulk,
285 uint16_t& fifo_words);
286
291 bool EnsureRxFifo(uint16_t packet_size);
292
293 EndpointMap endpoint_map_ = {};
294 FifoState fifo_state_ =
295 {};
296 RuntimeState runtime_ =
297 {};
298 alignas(SETUP_DMA_BUFFER_BYTES) uint8_t setup_packet_[SETUP_DMA_BUFFER_BYTES] =
299 {};
300 ControlState control_ = {};
302};
303
304} // namespace LibXR
305
306#endif
只读原始数据视图 / Immutable raw data view
ESP32-S3 USB OTG 设备核心实现 / ESP32-S3 USB OTG device-core implementation.
void Start(bool in_isr) override
启动 USB device controller / Start the USB device controller
void Deinit(bool in_isr) override
去初始化 USB device core 状态 / Deinitialize the USB device-core state
ESP32USBDevice(const std::initializer_list< EPConfig > ep_cfgs, USB::DeviceDescriptor::PacketSize0 packet_size, uint16_t vid, uint16_t pid, uint16_t bcd, const std::initializer_list< const USB::DescriptorStrings::LanguagePack * > lang_list, const std::initializer_list< const std::initializer_list< USB::ConfigDescriptorItem * > > configs, ConstRawData uid={nullptr, 0})
构造 ESP32-S3 USB device core / Construct the ESP32-S3 USB device core
void Stop(bool in_isr) override
停止 USB device controller / Stop the USB device controller
void Init(bool in_isr) override
初始化 USB device core 状态 / Initialize the USB device-core state
ErrorCode SetAddress(uint8_t address, USB::DeviceCore::Context context) override
在控制传输阶段写入设备地址 / Write the device address during control-transfer sequencing
ESP32-S3 USB endpoint implementation.
可写原始数据视图 / Mutable raw data view
USB 设备协议栈核心:EP0 控制传输、描述符、配置、标准/类/厂商请求 USB device core: EP0 control transfer, descriptors,...
Definition dev_core.hpp:22
Context
控制传输上下文 / Control transfer context
Definition dev_core.hpp:31
PacketSize0
控制端点0最大包长度枚举 Packet size for endpoint 0 (bMaxPacketSize0)
Definition desc_dev.hpp:74
USB 端点基类 / USB Endpoint base class.
Definition ep.hpp:24
USB端点池类 / USB endpoint pool class.
Definition ep_pool.hpp:29
LibXR 命名空间 / LibXR namespace.
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举 / Defines an enumeration for error codes
ESP32-S3 USB endpoint 配置项 / ESP32-S3 USB endpoint configuration entry.
EPConfig(RawData buffer)
使用同一块 buffer 同时生成 IN/OUT endpoint / Create both IN and OUT endpoints from one shared buffer
DirectionHint
该配置项应如何生成 endpoint 方向 / How this config entry should populate endpoint directions
@ BothDirections
同时生成 IN/OUT endpoint / Create both IN and OUT endpoints
@ OutOnly
仅生成 OUT endpoint / Create only the OUT endpoint
@ InOnly
仅生成 IN endpoint / Create only the IN endpoint
EPConfig(RawData buffer, bool is_in)
使用同一块 buffer 生成单方向 endpoint / Create a single-direction endpoint from one shared buffer