14#include "esp_idf_version.h"
15#include "esp_rom_sys.h"
16#include "esp_system.h"
18#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0) && defined(CONFIG_SOC_USB_OTG_SUPPORTED) && CONFIG_SOC_USB_OTG_SUPPORTED
19#include "esp_private/usb_phy.h"
22#include "freertos/FreeRTOS.h"
23#include "freertos/task.h"
27#include "class/cdc/cdc.h"
32static const char*
TAG =
"USB";
42#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0) && defined(CONFIG_SOC_USB_OTG_SUPPORTED) && CONFIG_SOC_USB_OTG_SUPPORTED
43static usb_phy_handle_t g_usb_phy =
nullptr;
49static bool usb_phy_init_once(
void) {
50 if (g_usb_phy)
return true;
52 usb_phy_config_t phy_conf = {
53 .controller = USB_PHY_CTRL_OTG,
54 .target = USB_PHY_TARGET_INT,
55 .otg_mode = USB_OTG_MODE_DEVICE,
56 .otg_speed = USB_PHY_SPEED_UNDEFINED,
57 .ext_io_conf =
nullptr,
58 .otg_io_conf =
nullptr,
61 esp_err_t err = usb_new_phy(&phy_conf, &g_usb_phy);
63 LOG_E(
TAG,
"USB PHY init failed: %s", esp_err_to_name(err));
97 esp_rom_delay_us(50000);
99#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0) && defined(CONFIG_SOC_USB_OTG_SUPPORTED) && CONFIG_SOC_USB_OTG_SUPPORTED
101 usb_del_phy(g_usb_phy);
105 esp_rom_delay_us(50000);
116 vTaskDelay(pdMS_TO_TICKS(10));
138#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0) && defined(CONFIG_SOC_USB_OTG_SUPPORTED) && CONFIG_SOC_USB_OTG_SUPPORTED
139 if (!usb_phy_init_once()) {
147#ifdef CONFIG_USB_EARLY_DEBUG
149 LOG_I(
TAG,
"USB CDC init (early debug mode)");
153 LOG_I(
TAG,
"USB CDC ready (will re-enumerate after modules load)");
173#ifdef CONFIG_USB_EARLY_DEBUG
175 LOG_I(
TAG,
"USB re-enumerating with final configuration...");
177 vTaskDelay(pdMS_TO_TICKS(50));
210 while (written < len) {
211 size_t avail = tud_cdc_write_available();
213 tud_cdc_write_flush();
218 size_t chunk = (len - written > avail) ? avail : (len - written);
219 size_t sent = tud_cdc_write(data + written, chunk);
222 if (sent == 0)
break;
225 tud_cdc_write_flush();
247 return tud_cdc_read(data, len);
258 if (tud_cdc_read(&c, 1) == 1) {
270 return tud_cdc_available();
278 tud_cdc_write_flush();
279 vTaskDelay(pdMS_TO_TICKS(10));
CDC Log: logging over TinyUSB CDC and UART.
#define LOG_I(tag, fmt,...)
#define LOG_E(tag, fmt,...)
void usb_cdc_flush(void)
Flushes pending USB CDC writes.
static void usb_shutdown_handler(void)
Public USB CDC API implementation.
static bool g_usb_started
size_t usb_cdc_available(void)
Returns number of bytes available for read.
static bool usb_start_stack(void)
static bool g_usb_prepared
Internal USB CDC startup and task state.
bool usb_cdc_ready(void)
Returns whether USB CDC is connected and ready.
static TaskHandle_t g_usb_task
size_t usb_cdc_write(const uint8_t *data, size_t len)
Writes byte buffer to USB CDC endpoint.
static void usb_device_task(void *arg)
TinyUSB device task.
bool usb_cdc_start(void)
Starts USB CDC runtime (or triggers re-enumeration in early-debug mode).
size_t usb_cdc_read(uint8_t *data, size_t len)
Reads bytes from USB CDC endpoint.
size_t usb_cdc_print(const char *str)
Writes null-terminated string to USB CDC.
int usb_cdc_getchar(void)
Reads one character from USB CDC stream.
bool usb_hid_init(void)
Public USB HID/composite configuration API implementation.