4#if defined(USE_ESP32_VARIANT_ESP32P4) || defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3) || \
5 defined(USE_ESP32_VARIANT_ESP32S31) || defined(USE_ESP32_VARIANT_ESP32H4)
10#include "usb/usb_host.h"
11#include <freertos/FreeRTOS.h>
12#include <freertos/task.h>
50static constexpr uint8_t USB_RECIP_DEVICE = 0;
51static constexpr uint8_t USB_RECIP_INTERFACE = 1;
52static constexpr uint8_t USB_RECIP_ENDPOINT = 2;
53static constexpr uint8_t USB_TYPE_STANDARD = 0 << 5;
54static constexpr uint8_t USB_TYPE_CLASS = 1 << 5;
55static constexpr uint8_t USB_TYPE_VENDOR = 2 << 5;
56static constexpr uint8_t USB_DIR_MASK = 1 << 7;
57static constexpr uint8_t USB_DIR_IN = 1 << 7;
58static constexpr uint8_t USB_DIR_OUT = 0;
59static constexpr size_t SETUP_PACKET_SIZE = 8;
61static constexpr size_t MAX_REQUESTS = USB_HOST_MAX_REQUESTS;
62static_assert(MAX_REQUESTS >= 1 && MAX_REQUESTS <= 32,
"MAX_REQUESTS must be between 1 and 32");
70using trq_bitmask_t = std::conditional<(MAX_REQUESTS <= 16), uint16_t, uint32_t>::type;
71static constexpr trq_bitmask_t ALL_REQUESTS_IN_USE = MAX_REQUESTS == 32 ? ~0 : (1 << MAX_REQUESTS) - 1;
73static constexpr size_t USB_MAX_PACKET_SIZE =
74 USB_HOST_MAX_PACKET_SIZE;
75static constexpr size_t USB_EVENT_QUEUE_SIZE = 32;
76static constexpr size_t USB_TASK_STACK_SIZE = 4096;
77static constexpr UBaseType_t USB_TASK_PRIORITY = 5;
124static constexpr size_t DESC_STRING_BUF_SIZE = 128;
155 void setup()
override;
156 void loop()
override;
167 const std::vector<uint8_t> &data = {});
244 void loop()
override;
245 void setup()
override;
TransferRequest * get_trq_()
USBClient(uint16_t vid, uint16_t pid)
LazyCallbackManager< void(bool)> connection_callback_
virtual bool reports_connection_itself() const
Whether the subclass reports the device as connected itself, once its own setup of the device has fin...
trq_bitmask_t get_trq_in_use() const
usb_host_client_handle_t handle_
void on_opened(uint8_t addr)
TransferRequest requests_[MAX_REQUESTS]
void handle_open_state_()
float get_setup_priority() const override
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 dump_config() override
void release_trq(TransferRequest *trq)
const char16_t * product_filter_
void usb_task_loop_() const
static void usb_task_fn(void *arg)
void set_product_filter(const char16_t *product)
virtual void on_connected()
virtual void disconnect()
EventPool< UsbEvent, USB_EVENT_QUEUE_SIZE - 1 > event_pool
bool is_connected() const
Whether a device has been opened and its setup by the subclass has finished.
TaskHandle_t usb_task_handle_
const char16_t * manufacturer_filter_
bool process_usb_events_()
bool get_device_info(UsbDeviceInfo &info) const
Copy the connected device's identity out of the cached USB descriptors.
virtual void on_disconnected()
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.
bool connection_reported_
void add_on_connection_callback(F &&callback)
Register a callback for the device this client claims being connected (true) or removed (false).
void on_removed(usb_device_handle_t handle)
usb_device_handle_t device_handle_
void set_manufacturer_filter(const char16_t *manufacturer)
std::atomic< trq_bitmask_t > trq_in_use_
void report_connected_()
Report the claimed device to the connection callbacks.
LockFreeQueue< UsbEvent, USB_EVENT_QUEUE_SIZE > event_queue
std::vector< USBClient * > clients_
float get_setup_priority() const override
constexpr float BUS
For communication buses like i2c/spi.
constexpr float IO
For components that represent GPIO pins like PCF8573.
std::conditional<(MAX_REQUESTS<=16), uint16_t, uint32_t >::type trq_bitmask_t
ESPHOME_LOG_TAG(TAG, "usb_host")
bool copy_descriptor_string(const usb_str_desc_t *desc, std::span< char, DESC_STRING_BUF_SIZE > buffer)
Copy a USB string descriptor into a NUL-terminated buffer.
std::function< void(const TransferStatus &)> transfer_cb_t
usb_transfer_t * transfer
Identity of a connected USB device, copied out of the descriptors the USB host stack caches for the l...
char serial_number[DESC_STRING_BUF_SIZE]
char product[DESC_STRING_BUF_SIZE]
char manufacturer[DESC_STRING_BUF_SIZE]
union esphome::usb_host::UsbEvent::@181 data
struct esphome::usb_host::UsbEvent::@181::@182 device_new
usb_device_handle_t handle
struct esphome::usb_host::UsbEvent::@181::@183 device_gone
spi_device_handle_t handle