libxr  1.0
Want to be the best embedded framework
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
16namespace LibXR
17{
18
19class ESP32USBEndpoint;
20
24class ESP32USBDevice : public USB::EndpointPool, public USB::DeviceCore
25{
26 public:
30 struct EPConfig
31 {
36 enum class DirectionHint : int8_t
37 {
38 BothDirections =
39 -1,
40 OutOnly = 0,
41 InOnly = 1,
42 };
43
44 RawData buffer;
46 DirectionHint direction_hint =
47 DirectionHint::BothDirections;
49
50 EPConfig() = delete;
51
56 explicit EPConfig(RawData buffer) : buffer(buffer) {}
57
62 EPConfig(RawData buffer, bool is_in)
63 : buffer(buffer),
64 direction_hint(is_in ? DirectionHint::InOnly : DirectionHint::OutOnly)
65 {
66 }
67 };
68
81 ESP32USBDevice(
82 const std::initializer_list<EPConfig> ep_cfgs,
83 USB::DeviceDescriptor::PacketSize0 packet_size, uint16_t vid, uint16_t pid,
84 uint16_t bcd,
85 const std::initializer_list<const USB::DescriptorStrings::LanguagePack*> lang_list,
86 const std::initializer_list<const std::initializer_list<USB::ConfigDescriptorItem*>>
87 configs,
88 ConstRawData uid = {nullptr, 0});
89
93 void Init(bool in_isr) override;
94
98 void Deinit(bool in_isr) override;
99
104 ErrorCode SetAddress(uint8_t address, USB::DeviceCore::Context context) override;
105
109 void Start(bool in_isr) override;
110
114 void Stop(bool in_isr) override;
115
116 private:
117 friend class ESP32USBEndpoint;
118
119 static constexpr uint8_t ENDPOINT_COUNT = 7;
120 static constexpr uint8_t IN_ENDPOINT_LIMIT = 5;
121 static constexpr uint32_t INTERRUPT_DISPATCH_GUARD = 64U;
122 static constexpr size_t SETUP_PACKET_BYTES = 8U;
123 static constexpr size_t SETUP_DMA_BUFFER_BYTES = 64U;
124
128 struct ControlState
129 {
130 bool setup_direction_out =
131 false;
133 };
134
139 struct EndpointMap
140 {
141 USB::Endpoint* in[ENDPOINT_COUNT] =
142 {};
143 USB::Endpoint* out[ENDPOINT_COUNT] =
144 {};
145 };
146
151 struct FifoState
152 {
153 uint16_t depth_words =
154 0U;
155 uint16_t rx_words =
156 0U;
157 uint16_t tx_next_words =
158 0U;
159 uint16_t tx_words[ENDPOINT_COUNT] =
160 {};
162 bool tx_bound[ENDPOINT_COUNT] = {};
164 uint8_t allocated_in = 0U;
166 };
167
171 struct RuntimeState
172 {
173 intr_handle_t intr_handle =
174 nullptr;
175 void* phy_handle = nullptr;
176 bool phy_ready = false;
177 bool irq_ready =
178 false;
179 bool started = false;
180 bool rom_usb_cleaned = false;
182 };
183
187 static void IRAM_ATTR IsrEntry(void* arg);
188
192 bool EnsurePhyReady();
193
197 bool EnsureInterruptReady();
198
202 void EnsureRomUsbCleaned();
203
207 void InitializeCore();
208
212 void ClearTxFifoRegisters();
213
217 void FlushFifos();
218
222 void ResetFifoState();
223
227 void ResetDeviceState();
228
232 void ResetEndpointHardwareState();
233
237 void ReloadSetupPacketCount();
238
242 void HandleInterrupt();
243
247 void HandleBusReset(bool in_isr);
248
252 void HandleEndpointInterrupt(bool in_isr, bool in_dir);
253
257 void HandleRxFifoLevel();
258
262 void ResetControlState();
263
267 void UpdateSetupState(const uint8_t* setup);
268
273 bool LastSetupDirectionOut() const { return control_.setup_direction_out; }
274
278 bool DmaEnabled() const { return true; }
279
283 bool AllocateTxFifo(uint8_t ep_num, uint16_t packet_size, bool is_bulk,
284 uint16_t& fifo_words);
285
290 bool EnsureRxFifo(uint16_t packet_size);
291
292 EndpointMap endpoint_map_ = {};
293 FifoState fifo_state_ =
294 {};
295 RuntimeState runtime_ =
296 {};
297 alignas(SETUP_DMA_BUFFER_BYTES) uint8_t setup_packet_[SETUP_DMA_BUFFER_BYTES] =
298 {};
299 ControlState control_ = {};
301};
302
303} // namespace LibXR
304
305#endif
LibXR 命名空间
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举