ESPHome 2025.12.0-dev
Loading...
Searching...
No Matches
usb_host.h
Go to the documentation of this file.
1#pragma once
2
3// Should not be needed, but it's required to pass CI clang-tidy checks
4#if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3) || defined(USE_ESP32_VARIANT_ESP32P4)
7#include <vector>
8#include "usb/usb_host.h"
9#include <freertos/FreeRTOS.h>
10#include <freertos/task.h>
13#include <atomic>
14
15namespace esphome {
16namespace usb_host {
17
18// THREADING MODEL:
19// This component uses a dedicated USB task for event processing to prevent data loss.
20// - USB Task (high priority): Handles USB events, executes transfer callbacks, releases transfer slots
21// - Main Loop Task: Initiates transfers, processes device connect/disconnect events
22//
23// Thread-safe communication:
24// - Lock-free queues for USB task -> main loop events (SPSC pattern)
25// - Lock-free TransferRequest pool using atomic bitmask (MCMP pattern - multi-consumer, multi-producer)
26//
27// TransferRequest pool access pattern:
28// - get_trq_() [allocate]: Called from BOTH USB task and main loop threads
29// * USB task: via USB UART input callbacks that restart transfers immediately
30// * Main loop: for output transfers and flow-controlled input restarts
31// - release_trq() [deallocate]: Called from BOTH USB task and main loop threads
32// * USB task: immediately after transfer callback completes (critical for preventing slot exhaustion)
33// * Main loop: when transfer submission fails
34//
35// The multi-threaded allocation/deallocation is intentional for performance:
36// - USB task can immediately restart input transfers and release slots without context switching
37// - Main loop controls backpressure by deciding when to restart after consuming data
38// The atomic bitmask ensures thread-safe allocation/deallocation without mutex blocking.
39
40static const char *const TAG = "usb_host";
41
42// Forward declarations
43struct TransferRequest;
44class USBClient;
45
46// constants for setup packet type
47static const uint8_t USB_RECIP_DEVICE = 0;
48static const uint8_t USB_RECIP_INTERFACE = 1;
49static const uint8_t USB_RECIP_ENDPOINT = 2;
50static const uint8_t USB_TYPE_STANDARD = 0 << 5;
51static const uint8_t USB_TYPE_CLASS = 1 << 5;
52static const uint8_t USB_TYPE_VENDOR = 2 << 5;
53static const uint8_t USB_DIR_MASK = 1 << 7;
54static const uint8_t USB_DIR_IN = 1 << 7;
55static const uint8_t USB_DIR_OUT = 0;
56static const size_t SETUP_PACKET_SIZE = 8;
57
58static constexpr size_t MAX_REQUESTS = USB_HOST_MAX_REQUESTS; // maximum number of outstanding requests possible.
59static_assert(MAX_REQUESTS >= 1 && MAX_REQUESTS <= 32, "MAX_REQUESTS must be between 1 and 32");
60
61// Select appropriate bitmask type for tracking allocation of TransferRequest slots.
62// The bitmask must have at least as many bits as MAX_REQUESTS, so:
63// - Use uint16_t for up to 16 requests (MAX_REQUESTS <= 16)
64// - Use uint32_t for 17-32 requests (MAX_REQUESTS > 16)
65// This is tied to the static_assert above, which enforces MAX_REQUESTS is between 1 and 32.
66// If MAX_REQUESTS is increased above 32, this logic and the static_assert must be updated.
67using trq_bitmask_t = std::conditional<(MAX_REQUESTS <= 16), uint16_t, uint32_t>::type;
68static constexpr trq_bitmask_t ALL_REQUESTS_IN_USE = MAX_REQUESTS == 32 ? ~0 : (1 << MAX_REQUESTS) - 1;
69
70static constexpr size_t USB_EVENT_QUEUE_SIZE = 32; // Size of event queue between USB task and main loop
71static constexpr size_t USB_TASK_STACK_SIZE = 4096; // Stack size for USB task (same as ESP-IDF USB examples)
72static constexpr UBaseType_t USB_TASK_PRIORITY = 5; // Higher priority than main loop (tskIDLE_PRIORITY + 5)
73
74// used to report a transfer status
76 bool success;
77 uint16_t error_code;
78 uint8_t *data;
79 size_t data_len;
80 uint8_t endpoint;
81 void *user_data;
82};
83
84using transfer_cb_t = std::function<void(const TransferStatus &)>;
85
86class USBClient;
87
88// struct used to capture all data needed for a transfer
95
100
101struct UsbEvent {
103 union {
104 struct {
105 uint8_t address;
107 struct {
108 usb_device_handle_t handle;
111
112 // Required for EventPool - no cleanup needed for POD types
113 void release() {}
114};
115
116// callback function type.
117
126class USBClient : public Component {
127 friend class USBHost;
128
129 public:
130 USBClient(uint16_t vid, uint16_t pid) : vid_(vid), pid_(pid), trq_in_use_(0) {}
131 void setup() override;
132 void loop() override;
133 // setup must happen after the host bus has been setup
134 float get_setup_priority() const override { return setup_priority::IO; }
135 void on_opened(uint8_t addr);
136 void on_removed(usb_device_handle_t handle);
137 bool transfer_in(uint8_t ep_address, const transfer_cb_t &callback, uint16_t length);
138 bool transfer_out(uint8_t ep_address, const transfer_cb_t &callback, const uint8_t *data, uint16_t length);
139 void dump_config() override;
140 void release_trq(TransferRequest *trq);
142 bool control_transfer(uint8_t type, uint8_t request, uint16_t value, uint16_t index, const transfer_cb_t &callback,
143 const std::vector<uint8_t> &data = {});
144
145 // Lock-free event queue and pool for USB task to main loop communication
146 // Must be public for access from static callbacks
149
150 protected:
151 TransferRequest *get_trq_(); // Lock-free allocation using atomic bitmask (multi-consumer safe)
152 virtual void disconnect();
153 virtual void on_connected() {}
154 virtual void on_disconnected() {
155 // Reset all requests to available (all bits to 0)
156 this->trq_in_use_.store(0);
157 }
158
159 // USB task management
160 static void usb_task_fn(void *arg);
161 [[noreturn]] void usb_task_loop() const;
162
163 TaskHandle_t usb_task_handle_{nullptr};
164
165 usb_host_client_handle_t handle_{};
166 usb_device_handle_t device_handle_{};
169 uint16_t vid_{};
170 uint16_t pid_{};
171 // Lock-free pool management using atomic bitmask (no dynamic allocation)
172 // Bit i = 1: requests_[i] is in use, Bit i = 0: requests_[i] is available
173 // Supports multiple concurrent consumers and producers (both threads can allocate/deallocate)
174 // Bitmask type automatically selected: uint16_t for <= 16 slots, uint32_t for 17-32 slots
175 std::atomic<trq_bitmask_t> trq_in_use_;
176 TransferRequest requests_[MAX_REQUESTS]{};
177};
178class USBHost : public Component {
179 public:
180 float get_setup_priority() const override { return setup_priority::BUS; }
181 void loop() override;
182 void setup() override;
183
184 protected:
185 std::vector<USBClient *> clients_{};
186};
187
188} // namespace usb_host
189} // namespace esphome
190
191#endif // USE_ESP32_VARIANT_ESP32S2 || USE_ESP32_VARIANT_ESP32S3
USBClient(uint16_t vid, uint16_t pid)
Definition usb_host.h:130
trq_bitmask_t get_trq_in_use() const
Definition usb_host.h:141
usb_host_client_handle_t handle_
Definition usb_host.h:165
TransferRequest requests_[MAX_REQUESTS]
Definition usb_host.h:176
float get_setup_priority() const override
Definition usb_host.h:134
bool transfer_out(uint8_t ep_address, const transfer_cb_t &callback, const uint8_t *data, uint16_t length)
Performs an output transfer operation.
void release_trq(TransferRequest *trq)
static void usb_task_fn(void *arg)
virtual void on_connected()
Definition usb_host.h:153
TaskHandle_t usb_task_handle_
Definition usb_host.h:163
EventPool< UsbEvent, USB_EVENT_QUEUE_SIZE > event_pool
Definition usb_host.h:148
virtual void on_disconnected()
Definition usb_host.h:154
bool control_transfer(uint8_t type, uint8_t request, uint16_t value, uint16_t index, const transfer_cb_t &callback, const std::vector< uint8_t > &data={})
bool transfer_in(uint8_t ep_address, const transfer_cb_t &callback, uint16_t length)
Performs a transfer input operation.
void on_removed(usb_device_handle_t handle)
usb_device_handle_t device_handle_
Definition usb_host.h:166
std::atomic< trq_bitmask_t > trq_in_use_
Definition usb_host.h:175
LockFreeQueue< UsbEvent, USB_EVENT_QUEUE_SIZE > event_queue
Definition usb_host.h:147
std::vector< USBClient * > clients_
Definition usb_host.h:185
float get_setup_priority() const override
Definition usb_host.h:180
uint16_t type
const float BUS
For communication buses like i2c/spi.
Definition component.cpp:56
const float IO
For components that represent GPIO pins like PCF8573.
Definition component.cpp:57
std::conditional<(MAX_REQUESTS<=16), uint16_t, uint32_t >::type trq_bitmask_t
Definition usb_host.h:67
std::function< void(const TransferStatus &)> transfer_cb_t
Definition usb_host.h:84
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
union esphome::usb_host::UsbEvent::@159 data
usb_device_handle_t handle
Definition usb_host.h:108
struct esphome::usb_host::UsbEvent::@159::@161 device_gone
struct esphome::usb_host::UsbEvent::@159::@160 device_new
uint16_t length
Definition tt21100.cpp:0