1
0
mirror of https://github.com/esphome/esphome.git synced 2025-11-03 08:31:47 +00:00

[usb_host] Add wake_loop_threadsafe() for low-latency USB event processing

This commit is contained in:
J. Nick Koston
2025-11-02 22:43:00 -06:00
parent 8e0721318c
commit 9da3c08f3b
2 changed files with 13 additions and 1 deletions

View File

@@ -1,4 +1,5 @@
import esphome.codegen as cg
from esphome.components import socket
from esphome.components.esp32 import (
VARIANT_ESP32P4,
VARIANT_ESP32S2,
@@ -11,7 +12,7 @@ from esphome.const import CONF_DEVICES, CONF_ID
from esphome.cpp_types import Component
from esphome.types import ConfigType
AUTO_LOAD = ["bytebuffer"]
AUTO_LOAD = ["bytebuffer", "socket"]
CODEOWNERS = ["@clydebarrow"]
DEPENDENCIES = ["esp32"]
usb_host_ns = cg.esphome_ns.namespace("usb_host")
@@ -71,6 +72,11 @@ async def to_code(config: ConfigType) -> None:
max_requests = config[CONF_MAX_TRANSFER_REQUESTS]
cg.add_define("USB_HOST_MAX_REQUESTS", max_requests)
# USB uses the socket wake_loop_threadsafe() mechanism to wake the main loop from USB task
# This enables low-latency (~12μs) USB event processing instead of waiting for
# select() timeout (0-16ms). The wake socket is shared across all components.
socket.require_wake_loop_threadsafe()
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
for device in config.get(CONF_DEVICES) or ():

View File

@@ -3,6 +3,7 @@
#include "usb_host.h"
#include "esphome/core/log.h"
#include "esphome/core/hal.h"
#include "esphome/core/application.h"
#include "esphome/components/bytebuffer/bytebuffer.h"
#include <cinttypes>
@@ -174,6 +175,11 @@ static void client_event_cb(const usb_host_client_event_msg_t *event_msg, void *
// Push to lock-free queue (always succeeds since pool size == queue size)
client->event_queue.push(event);
// Wake main loop immediately to process USB event instead of waiting for select() timeout
#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE)
App.wake_loop_threadsafe();
#endif
}
void USBClient::setup() {
usb_host_client_config_t config{.is_synchronous = false,