ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
usb_device.cpp
Go to the documentation of this file.
1#if defined(CONFIG_USB_DEVICE_STACK) || defined(CONFIG_USB_DEVICE_STACK_NEXT)
2#include "usb_device.h"
3
4#ifdef CONFIG_USB_DEVICE_STACK_NEXT
6#include <zephyr/device.h>
7#include <zephyr/usb/usbd.h>
8#include <cerrno>
9#include <initializer_list>
10#ifdef CONFIG_UDC_NRF
11#include <nrfx_power.h>
12#endif
13#ifdef USE_ZEPHYR_CDC_ACM_RATE_CALLBACK
14#include "cdc_acm.h"
15#endif
16#else
17#include <cerrno>
18#include <zephyr/usb/usb_device.h>
19#endif
20
21namespace esphome::zephyr {
22
23#ifdef CONFIG_USB_DEVICE_STACK_NEXT
24
25// Same identity and power settings as the defaults of the legacy stack, so a host sees the
26// same device on either stack.
27static constexpr uint16_t USB_VID = 0x2FE3;
28static constexpr uint16_t USB_PID = 0x0100;
29static constexpr uint8_t USB_MAX_POWER = 50; // in 2 mA units
30
31// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
32USBD_DEVICE_DEFINE(esphome_usbd, DEVICE_DT_GET(DT_NODELABEL(zephyr_udc0)), USB_VID, USB_PID);
33USBD_DESC_LANG_DEFINE(esphome_usbd_lang);
34USBD_DESC_MANUFACTURER_DEFINE(esphome_usbd_mfr, "ZEPHYR");
35USBD_DESC_PRODUCT_DEFINE(esphome_usbd_product, "USB-DEV");
37USBD_CONFIGURATION_DEFINE(esphome_usbd_fs_config, USB_SCD_SELF_POWERED, USB_MAX_POWER, nullptr);
38#if USBD_SUPPORTS_HIGH_SPEED
39USBD_CONFIGURATION_DEFINE(esphome_usbd_hs_config, USB_SCD_SELF_POWERED, USB_MAX_POWER, nullptr);
40#endif
41// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)
42
43// Runs on the system work queue, which also runs all USB transfers: keep it short
44static void usbd_msg_callback(usbd_context *const ctx, const usbd_msg *const msg) {
45 // Run the controller only while VBUS is present, so it and its clock stop when the cable is removed
46 if (usbd_can_detect_vbus(ctx)) {
47 if (msg->type == USBD_MSG_VBUS_READY) {
48 usbd_enable(ctx);
49 } else if (msg->type == USBD_MSG_VBUS_REMOVED) {
50 usbd_disable(ctx);
51 }
52 }
53#ifdef USE_ZEPHYR_CDC_ACM_RATE_CALLBACK
54 if (msg->type == USBD_MSG_CDC_ACM_LINE_CODING)
55 CdcAcm::rate_changed(msg->dev);
56#endif
57}
58
59static int usbd_add_speed(usbd_speed speed, usbd_config_node *config) {
60 int err = usbd_add_configuration(&esphome_usbd, speed, config);
61 if (err == 0)
62 err = usbd_register_all_classes(&esphome_usbd, speed, 1, nullptr);
63#ifdef CONFIG_USBD_CDC_ACM_CLASS
64 // Each CDC ACM function has an Interface Association Descriptor
65 if (err == 0)
66 err = usbd_device_set_code_triple(&esphome_usbd, speed, USB_BCC_MISCELLANEOUS, 0x02, 0x01);
67#endif
68 return err;
69}
70
71// The nRF52 controller reports a cable connected at reset only if no USB bootloader (such as UF2)
72// handled the event first, so the cable is checked here as well
73static bool usbd_vbus_present() {
74#ifdef CONFIG_UDC_NRF
75 return nrfx_power_usbstatus_get() != NRFX_POWER_USB_STATE_DISCONNECTED;
76#else
77 return false;
78#endif
79}
80
81static int usbd_setup() {
82 for (usbd_desc_node *desc : {&esphome_usbd_lang, &esphome_usbd_mfr, &esphome_usbd_product, &esphome_usbd_sn}) {
83 if (int err = usbd_add_descriptor(&esphome_usbd, desc); err != 0)
84 return err;
85 }
86#if USBD_SUPPORTS_HIGH_SPEED
87 // A high-speed controller (nRF54LM20) needs a high-speed configuration too
88 if (usbd_caps_speed(&esphome_usbd) == USBD_SPEED_HS) {
89 if (int err = usbd_add_speed(USBD_SPEED_HS, &esphome_usbd_hs_config); err != 0)
90 return err;
91 }
92#endif
93 if (int err = usbd_add_speed(USBD_SPEED_FS, &esphome_usbd_fs_config); err != 0)
94 return err;
95 usbd_self_powered(&esphome_usbd, true);
96 if (int err = usbd_msg_register_cb(&esphome_usbd, usbd_msg_callback); err != 0)
97 return err;
98 if (int err = usbd_init(&esphome_usbd); err != 0)
99 return err;
100 // Otherwise the message callback enables the device once VBUS is present
101 if (!usbd_can_detect_vbus(&esphome_usbd) || usbd_vbus_present()) {
102 int err = usbd_enable(&esphome_usbd);
103 // The message callback may have enabled it already
104 return err == -EALREADY ? 0 : err;
105 }
106 return 0;
107}
108
110 // Setup cannot be repeated after a failure: the parts added before it stay registered
111 static const int SETUP_RESULT = usbd_setup();
112 return SETUP_RESULT;
113}
114
115#else // legacy USB device stack
116
117int usb_device_enable() {
118 int err = usb_enable(nullptr);
119 // Later calls, or a board that starts USB at boot
120 return err == -EALREADY ? 0 : err;
121}
122
123#endif
124
125} // namespace esphome::zephyr
126
127#endif
static void rate_changed(const device *dev)
Called by the USB driver, from its own thread, after the host set the bit rate of dev.
Definition cdc_acm.cpp:38
USBD_DESC_LANG_DEFINE(esphome_usbd_lang)
USBD_DEVICE_DEFINE(esphome_usbd, DEVICE_DT_GET(DT_NODELABEL(zephyr_udc0)), USB_VID, USB_PID)
USBD_DESC_MANUFACTURER_DEFINE(esphome_usbd_mfr, "ZEPHYR")
int usb_device_enable()
Start the USB device on whichever Zephyr USB device stack the build uses (the legacy one or device_ne...
USBD_CONFIGURATION_DEFINE(esphome_usbd_fs_config, USB_SCD_SELF_POWERED, USB_MAX_POWER, nullptr)
USBD_DESC_PRODUCT_DEFINE(esphome_usbd_product, "USB-DEV")
USBD_DESC_SERIAL_NUMBER_DEFINE(esphome_usbd_sn)