tiny_usb: rename Kconfig name

1. Renamed Kconfig file of tinyusb (distinguish tinyusb stack from usb
   peripheral)
2. bugfix/typofix/doc update of tinyusb
This commit is contained in:
morris 2021-07-09 11:15:26 +08:00
parent 2218204aa7
commit 81448dcae8
25 changed files with 589 additions and 497 deletions

View File

@ -201,9 +201,8 @@ menu "ESP System Settings"
bool "Default: UART0" bool "Default: UART0"
config ESP_CONSOLE_USB_CDC config ESP_CONSOLE_USB_CDC
bool "USB CDC" bool "USB CDC"
# The naming is confusing: USB_ENABLED means that TinyUSB driver is enabled, not USB in general. # && !TINY_USB is because the ROM CDC driver is currently incompatible with TinyUSB.
# && !USB_ENABLED is because the ROM CDC driver is currently incompatible with TinyUSB. depends on (IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32S3) && !TINY_USB
depends on (IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32S3) && !USB_ENABLED
config ESP_CONSOLE_USB_SERIAL_JTAG config ESP_CONSOLE_USB_SERIAL_JTAG
bool "USB Serial/JTAG Controller" bool "USB Serial/JTAG Controller"
select ESPTOOLPY_NO_STUB if IDF_TARGET_ESP32S3 #ESPTOOL-251 select ESPTOOLPY_NO_STUB if IDF_TARGET_ESP32S3 #ESPTOOL-251

View File

@ -1,78 +1,81 @@
idf_build_get_property(target IDF_TARGET) idf_build_get_property(target IDF_TARGET)
idf_component_register(REQUIRES esp_rom freertos vfs soc)
if(CONFIG_USB_ENABLED) set(srcs)
set(includes_public)
set(includes_private)
set(compile_options)
if(target STREQUAL "esp32s3") if(CONFIG_TINYUSB)
set(tusb_mcu "OPT_MCU_ESP32S3") if(target STREQUAL "esp32s3")
set(tusb_family "esp32sx") set(tusb_mcu "OPT_MCU_ESP32S3")
elseif(target STREQUAL "esp32s2") set(tusb_family "esp32sx")
set(tusb_mcu "OPT_MCU_ESP32S2") elseif(target STREQUAL "esp32s2")
set(tusb_family "esp32sx") set(tusb_mcu "OPT_MCU_ESP32S2")
else() set(tusb_family "esp32sx")
message("TinyUSB does not support ${target}.") else()
return() # CONFIG_TINYUSB dependency has been garanteed by Kconfig logic,
endif() # So it's not possible that cmake goes here
message(FATAL_ERROR "TinyUSB is not support on ${target}.")
return()
endif()
### variables ### list(APPEND compile_options
################# "-DCFG_TUSB_MCU=${tusb_mcu}"
set(compile_options "-DCFG_TUSB_DEBUG=${CONFIG_TINYUSB_DEBUG_LEVEL}"
"-DCFG_TUSB_MCU=${tusb_mcu}" )
"-DCFG_TUSB_DEBUG=${CONFIG_USB_DEBUG_LEVEL}"
"-Wno-type-limits" # needed for the vanila tinyusb with turned off classes
)
idf_component_get_property(FREERTOS_ORIG_INCLUDE_PATH freertos idf_component_get_property(freertos_component_dir freertos COMPONENT_DIR)
ORIG_INCLUDE_PATH)
set(includes_private list(APPEND includes_private
# tusb: "tinyusb/hw/bsp/"
"${COMPONENT_DIR}/tinyusb/hw/bsp/" "tinyusb/src/"
"${COMPONENT_DIR}/tinyusb/src/" "tinyusb/src/device"
"${COMPONENT_DIR}/tinyusb/src/device" "additions/include_private"
# espressif: )
"${COMPONENT_DIR}/additions/include_private"
) list(APPEND includes_public
"tinyusb/src/"
"additions/include"
# The FreeRTOS API include convention in tinyusb is different from esp-idf
"${freertos_component_dir}/include/freertos"
)
set(includes_public
# tusb:
"${FREERTOS_ORIG_INCLUDE_PATH}"
"${COMPONENT_DIR}/tinyusb/src/"
# espressif:
"${COMPONENT_DIR}/additions/include")
set(srcs
# espressif:
"${COMPONENT_DIR}/additions/src/descriptors_control.c"
"${COMPONENT_DIR}/additions/src/tinyusb.c"
"${COMPONENT_DIR}/additions/src/tusb_tasks.c"
"${COMPONENT_DIR}/additions/src/usb_descriptors.c"
# tusb:
"${COMPONENT_DIR}/tinyusb/src/portable/espressif/${tusb_family}/dcd_${tusb_family}.c"
"${COMPONENT_DIR}/tinyusb/src/class/cdc/cdc_device.c"
"${COMPONENT_DIR}/tinyusb/src/class/hid/hid_device.c"
"${COMPONENT_DIR}/tinyusb/src/class/midi/midi_device.c"
"${COMPONENT_DIR}/tinyusb/src/class/msc/msc_device.c"
"${COMPONENT_DIR}/tinyusb/src/class/vendor/vendor_device.c"
"${COMPONENT_DIR}/tinyusb/src/common/tusb_fifo.c"
"${COMPONENT_DIR}/tinyusb/src/device/usbd_control.c"
"${COMPONENT_DIR}/tinyusb/src/device/usbd.c"
"${COMPONENT_DIR}/tinyusb/src/tusb.c")
# cdc stuff if turned on
if(CONFIG_USB_CDC_ENABLED)
list(APPEND srcs list(APPEND srcs
"${COMPONENT_DIR}/additions/src/cdc.c" "tinyusb/src/portable/espressif/${tusb_family}/dcd_${tusb_family}.c"
"${COMPONENT_DIR}/additions/src/tusb_cdc_acm.c" "tinyusb/src/class/cdc/cdc_device.c"
"${COMPONENT_DIR}/additions/src/tusb_console.c" "tinyusb/src/class/hid/hid_device.c"
"${COMPONENT_DIR}/additions/src/vfs_tinyusb.c") "tinyusb/src/class/midi/midi_device.c"
endif() "tinyusb/src/class/msc/msc_device.c"
"tinyusb/src/class/vendor/vendor_device.c"
"tinyusb/src/common/tusb_fifo.c"
"tinyusb/src/device/usbd_control.c"
"tinyusb/src/device/usbd.c"
"tinyusb/src/tusb.c"
"additions/src/descriptors_control.c"
"additions/src/tinyusb.c"
"additions/src/tusb_tasks.c"
"additions/src/usb_descriptors.c"
)
### tinyusb lib ### # when no builtin class driver is enabled, an uint8_t data compared with `BUILTIN_DRIVER_COUNT` will always be false
################### set_source_files_properties("tinyusb/src/device/usbd.c" PROPERTIES COMPILE_FLAGS "-Wno-type-limits")
add_library(tinyusb STATIC ${srcs})
target_include_directories(
tinyusb
PUBLIC ${includes_public}
PRIVATE ${includes_private})
target_compile_options(tinyusb PRIVATE ${compile_options})
target_link_libraries(${COMPONENT_TARGET} INTERFACE tinyusb)
if(CONFIG_TINYUSB_CDC_ENABLED)
list(APPEND srcs
"additions/src/cdc.c"
"additions/src/tusb_cdc_acm.c"
"additions/src/tusb_console.c"
"additions/src/vfs_tinyusb.c"
)
endif() # CONFIG_TINYUSB_CDC_ENABLED
endif() # CONFIG_TINYUSB
idf_component_register(SRCS ${srcs}
INCLUDE_DIRS ${includes_public}
PRIV_INCLUDE_DIRS ${includes_private}
PRIV_REQUIRES "vfs"
)
if(CONFIG_TINYUSB)
target_compile_options(${COMPONENT_LIB} PRIVATE ${compile_options})
endif() endif()

View File

@ -1,150 +1,155 @@
menu "TinyUSB" menu "TinyUSB Stack"
visible if USB_OTG_SUPPORTED
config USB_ENABLED config TINYUSB
bool "Enable TinyUSB driver" bool "Use TinyUSB Stack"
depends on USB_OTG_SUPPORTED
default n default n
depends on IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32S3
select FREERTOS_USE_AUTHENTIC_INCLUDE_PATHS
help help
Adds support for TinyUSB Enable TinyUSB stack support.
Note that, esp-idf only uses the device stack provided by TinyUSB.
menu "USB task configuration" if TINYUSB
depends on USB_ENABLED config TINYUSB_DEBUG_LEVEL
int "TinyUSB log level (0-3)"
config USB_DO_NOT_CREATE_TASK default 0
bool "Do not create a TinyUSB task" range 0 3
default n
help help
This option allows to not create the FreeRTOS task during the driver initialization. User will have Specify verbosity of TinyUSB log output.
to handle TinyUSB events manually
config USB_TASK_PRIORITY menu "TinyUSB task configuration"
int "Set a priority of the TinyUSB task" config TINYUSB_NO_DEFAULT_TASK
default 5 bool "Do not create a TinyUSB task"
depends on !USB_DO_NOT_CREATE_TASK default n
help help
User can change the priority of the main task according the application needs This option allows to not create the FreeRTOS task during the driver initialization.
User will have to handle TinyUSB events manually.
endmenu config TINYUSB_TASK_PRIORITY
int "TinyUSB task priority"
default 5
depends on !TINYUSB_NO_DEFAULT_TASK
help
Set the priority of the default TinyUSB main task.
menu "Descriptor configuration" config TINYUSB_TASK_STACK_SIZE
depends on USB_ENABLED int "TinyUSB task stack size (bytes)"
default 4096
depends on !TINYUSB_NO_DEFAULT_TASK
help
Set the stack size of the default TinyUSB main task.
endmenu
config USB_DESC_USE_ESPRESSIF_VID menu "Descriptor configuration"
bool "VID: Use an Espressif's default value" config TINYUSB_DESC_USE_ESPRESSIF_VID
default y bool "VID: Use Espressif's vendor ID"
help default y
Long description help
Enable this option, USB device will use Espressif's vendor ID as its VID.
This is helpful at product develop stage.
config USB_DESC_CUSTOM_VID config TINYUSB_DESC_CUSTOM_VID
hex "Custom VID value" hex "VID: Custom vendor ID"
default 0x1234 default 0x1234
depends on !USB_DESC_USE_ESPRESSIF_VID depends on !TINYUSB_DESC_USE_ESPRESSIF_VID
help help
Custom Vendor ID Custom Vendor ID.
config USB_DESC_USE_DEFAULT_PID config TINYUSB_DESC_USE_DEFAULT_PID
bool "PID: Use a default PID assigning" bool "PID: Use a default PID assigned to TinyUSB"
default y default y
help help
Default TinyUSB PID assigning uses values 0x4000...0x4007 Default TinyUSB PID assigning uses values 0x4000...0x4007.
config USB_DESC_CUSTOM_PID config TINYUSB_DESC_CUSTOM_PID
hex "Custom PID value" hex "PID: Custom product ID"
default 0x5678 default 0x5678
depends on !USB_DESC_USE_DEFAULT_PID depends on !TINYUSB_DESC_USE_DEFAULT_PID
help help
Custom Product ID Custom Product ID.
config USB_DESC_BCDDEVICE config TINYUSB_DESC_BCD_DEVICE
hex "bcdDevice" hex "bcdDevice"
default 0x0100 default 0x0100
help help
Version of the firmware of the USB device Version of the firmware of the USB device.
config USB_DESC_MANUFACTURER_STRING config TINYUSB_DESC_MANUFACTURER_STRING
string "Manufacturer" string "Manufacturer name"
default "Espressif Systems" default "Espressif Systems"
help help
Name of the manufacturer of the USB device Name of the manufacturer of the USB device.
config USB_DESC_PRODUCT_STRING config TINYUSB_DESC_PRODUCT_STRING
string "Product" string "Product name"
default "Espressif Device" default "Espressif Device"
help help
Name of the USB device Name of the USB device.
config USB_DESC_SERIAL_STRING config TINYUSB_DESC_SERIAL_STRING
string "Serial string" string "Serial string"
default "123456" default "123456"
help help
Specify serial number of the USB device Serial number of the USB device.
config USB_DESC_CDC_STRING config TINYUSB_DESC_CDC_STRING
string "CDC Device String" depends on TINYUSB_CDC_ENABLED
default "Espressif CDC Device" string "CDC Device String"
depends on USB_CDC_ENABLED default "Espressif CDC Device"
help help
Specify name of the CDC device Name of the CDC device.
config USB_DESC_MSC_STRING config TINYUSB_DESC_MSC_STRING
string "MSC Device String" depends on TINYUSB_MSC_ENABLED
default "Espressif MSC Device" string "MSC Device String"
depends on USB_MSC_ENABLED default "Espressif MSC Device"
help help
Specify name of the MSC device Name of the MSC device.
config USB_DESC_HID_STRING config TINYUSB_DESC_HID_STRING
string "HID Device String" depends on TINYUSB_HID_ENABLED
default "Espressif HID Device" string "HID Device String"
depends on USB_HID_ENABLED default "Espressif HID Device"
help help
Specify name of the HID device Name of the HID device
endmenu # "Descriptor configuration"
endmenu menu "Massive Storage Class (MSC)"
config TINYUSB_MSC_ENABLED
bool "Enable TinyUSB MSC feature"
default n
help
Enable TinyUSB MSC feature.
config USB_MSC_ENABLED config TINYUSB_MSC_BUFSIZE
bool "Enable USB MSC TinyUSB driver" depends on TINYUSB_MSC_ENABLED
default n int "MSC FIFO size"
depends on USB_ENABLED default 512
help help
Enable USB MSC TinyUSB driver. MSC FIFO size, in bytes.
endmenu # "Massive Storage Class"
config USB_MSC_BUFSIZE menu "Communication Device Class (CDC)"
int "MSC FIFO size" config TINYUSB_CDC_ENABLED
default 512 bool "Enable TinyUSB CDC feature"
depends on USB_MSC_ENABLED default n
help help
MSC FIFO size Enable TinyUSB CDC feature.
config USB_CDC_ENABLED config TINYUSB_CDC_RX_BUFSIZE
bool "Enable USB Serial (CDC) TinyUSB driver" depends on TINYUSB_CDC_ENABLED
default n int "CDC FIFO size of RX channel"
depends on USB_ENABLED default 64
help help
Enable USB Serial (CDC) TinyUSB driver. CDC FIFO size of RX channel.
config USB_CDC_RX_BUFSIZE config TINYUSB_CDC_TX_BUFSIZE
int "CDC FIFO size of RX" depends on TINYUSB_CDC_ENABLED
default 64 int "CDC FIFO size of TX channel"
depends on USB_CDC_ENABLED default 64
help help
CDC FIFO size of RX CDC FIFO size of TX channel.
endmenu # "Communication Device Class"
endif # TINYUSB
config USB_CDC_TX_BUFSIZE endmenu # "TinyUSB Stack"
int "CDC FIFO size of TX"
default 64
depends on USB_CDC_ENABLED
help
CDC FIFO size of TX
config USB_DEBUG_LEVEL
int "TinyUSB log level (0-3)"
default 0
range 0 3
depends on USB_ENABLED
help
Define amount of log output from TinyUSB
endmenu

View File

@ -27,9 +27,9 @@ extern "C" {
/* tinyusb uses buffers with type of uint8_t[] but in our driver we are reading them as a 32-bit word */ /* tinyusb uses buffers with type of uint8_t[] but in our driver we are reading them as a 32-bit word */
#if (CFG_TUD_ENDOINT0_SIZE < 4) #if (CFG_TUD_ENDPOINT0_SIZE < 4)
# define CFG_TUD_ENDOINT0_SIZE 4 # define CFG_TUD_ENDPOINT0_SIZE 4
# warning "CFG_TUD_ENDOINT0_SIZE was too low and was set to 4" # warning "CFG_TUD_ENDPOINT0_SIZE was too low and was set to 4"
#endif #endif
#if TUSB_OPT_DEVICE_ENABLED #if TUSB_OPT_DEVICE_ENABLED
@ -72,13 +72,30 @@ extern "C" {
*/ */
typedef struct { typedef struct {
tusb_desc_device_t *descriptor; /*!< Pointer to a device descriptor */ tusb_desc_device_t *descriptor; /*!< Pointer to a device descriptor */
char **string_descriptor; /*!< Pointer to an array of string descriptors */ const char **string_descriptor; /*!< Pointer to an array of string descriptors */
bool external_phy; /*!< Should USB use an external PHY */ bool external_phy; /*!< Should USB use an external PHY */
} tinyusb_config_t; } tinyusb_config_t;
/**
* @brief This is an all-in-one helper function, including:
* 1. USB device driver initialization
* 2. Descriptors preparation
* 3. TinyUSB stack initialization
* 4. Creates and start a task to handle usb events
*
* @note Don't change Custom descriptor, but if it has to be done,
* Suggest to define as follows in order to match the Interface Association Descriptor (IAD):
* bDeviceClass = TUSB_CLASS_MISC,
* bDeviceSubClass = MISC_SUBCLASS_COMMON,
*
* @param config tinyusb stack specific configuration
* @retval ESP_ERR_INVALID_ARG Install driver and tinyusb stack failed because of invalid argument
* @retval ESP_FAIL Install driver and tinyusb stack failed because of internal error
* @retval ESP_OK Install driver and tinyusb stack successfully
*/
esp_err_t tinyusb_driver_install(const tinyusb_config_t *config); esp_err_t tinyusb_driver_install(const tinyusb_config_t *config);
// TODO esp_err_t tinyusb_driver_uninstall(void); (IDF-1474)
// TODO esp_err_t tinyusb_driver_uninstall(void); (IDF-1474)
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -25,7 +25,7 @@ typedef enum{
TINYUSB_USBDEV_0, TINYUSB_USBDEV_0,
} tinyusb_usbdev_t; } tinyusb_usbdev_t;
typedef char *tusb_desc_strarray_device_t[USB_STRING_DESCRIPTOR_ARRAY_SIZE]; typedef const char *tusb_desc_strarray_device_t[USB_STRING_DESCRIPTOR_ARRAY_SIZE];
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -25,6 +25,7 @@
*/ */
#pragma once #pragma once
#include "tusb_option.h" #include "tusb_option.h"
#include "sdkconfig.h" #include "sdkconfig.h"
@ -32,11 +33,27 @@
extern "C" { extern "C" {
#endif #endif
/* */ #ifndef CONFIG_TINYUSB_CDC_ENABLED
/* COMMON CONFIGURATION */ # define CONFIG_TINYUSB_CDC_ENABLED 0
/* */ #endif
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE #ifndef CONFIG_TINYUSB_MSC_ENABLED
# define CONFIG_TINYUSB_MSC_ENABLED 0
#endif
#ifndef CONFIG_TINYUSB_HID_ENABLED
# define CONFIG_TINYUSB_HID_ENABLED 0
#endif
#ifndef CONFIG_TINYUSB_MIDI_ENABLED
# define CONFIG_TINYUSB_MIDI_ENABLED 0
#endif
#ifndef CONFIG_TINYUSB_CUSTOM_CLASS_ENABLED
# define CONFIG_TINYUSB_CUSTOM_CLASS_ENABLED 0
#endif
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE | OPT_MODE_FULL_SPEED
#define CFG_TUSB_OS OPT_OS_FREERTOS #define CFG_TUSB_OS OPT_OS_FREERTOS
/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment. /* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment.
@ -51,55 +68,29 @@ extern "C" {
#endif #endif
#ifndef CFG_TUSB_MEM_ALIGN #ifndef CFG_TUSB_MEM_ALIGN
# define CFG_TUSB_MEM_ALIGN TU_ATTR_ALIGNED(4) # define CFG_TUSB_MEM_ALIGN TU_ATTR_ALIGNED(4)
#endif #endif
/* */ #ifndef CFG_TUD_ENDPOINT0_SIZE
/* DRIVER CONFIGURATION */ #define CFG_TUD_ENDPOINT0_SIZE 64
/* */ #endif
#define CFG_TUD_MAINTASK_SIZE 4096
#define CFG_TUD_ENDOINT0_SIZE 64
// CDC FIFO size of TX and RX // CDC FIFO size of TX and RX
#define CFG_TUD_CDC_RX_BUFSIZE CONFIG_USB_CDC_RX_BUFSIZE #define CFG_TUD_CDC_RX_BUFSIZE CONFIG_TINYUSB_CDC_RX_BUFSIZE
#define CFG_TUD_CDC_TX_BUFSIZE CONFIG_USB_CDC_TX_BUFSIZE #define CFG_TUD_CDC_TX_BUFSIZE CONFIG_TINYUSB_CDC_TX_BUFSIZE
// MSC Buffer size of Device Mass storage: // MSC Buffer size of Device Mass storage
#define CFG_TUD_MSC_BUFSIZE CONFIG_USB_MSC_BUFSIZE #define CFG_TUD_MSC_BUFSIZE CONFIG_TINYUSB_MSC_BUFSIZE
// HID buffer size Should be sufficient to hold ID (if any) + Data // HID buffer size Should be sufficient to hold ID (if any) + Data
#define CFG_TUD_HID_BUFSIZE CONFIG_USB_HID_BUFSIZE #define CFG_TUD_HID_BUFSIZE CONFIG_TINYUSB_HID_BUFSIZE
#define CFG_TUD_CDC CONFIG_USB_CDC_ENABLED // Enabled device class driver
#define CFG_TUD_MSC CONFIG_USB_MSC_ENABLED #define CFG_TUD_CDC CONFIG_TINYUSB_CDC_ENABLED
#define CFG_TUD_HID CONFIG_USB_HID_ENABLED #define CFG_TUD_MSC CONFIG_TINYUSB_MSC_ENABLED
#define CFG_TUD_MIDI CONFIG_USB_MIDI_ENABLED #define CFG_TUD_HID CONFIG_TINYUSB_HID_ENABLED
#define CFG_TUD_CUSTOM_CLASS CONFIG_USB_CUSTOM_CLASS_ENABLED #define CFG_TUD_MIDI CONFIG_TINYUSB_MIDI_ENABLED
#define CFG_TUD_CUSTOM_CLASS CONFIG_TINYUSB_CUSTOM_CLASS_ENABLED
/* */
/* KCONFIG */
/* */
#ifndef CONFIG_USB_CDC_ENABLED
# define CONFIG_USB_CDC_ENABLED 0
#endif
#ifndef CONFIG_USB_MSC_ENABLED
# define CONFIG_USB_MSC_ENABLED 0
#endif
#ifndef CONFIG_USB_HID_ENABLED
# define CONFIG_USB_HID_ENABLED 0
#endif
#ifndef CONFIG_USB_MIDI_ENABLED
# define CONFIG_USB_MIDI_ENABLED 0
#endif
#ifndef CONFIG_USB_CUSTOM_CLASS_ENABLED
# define CONFIG_USB_CUSTOM_CLASS_ENABLED 0
#endif
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -16,26 +16,28 @@
#include "esp_err.h" #include "esp_err.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
/** /**
* @brief This API starts a task with a wrapper function of tud_task and default task parameters. * @brief This helper function creates and starts a task which wraps `tud_task()`.
* *
* The wrapper function basically wraps tud_task and some log. Default parameters: stack size and priority as configured, argument = NULL, * The wrapper function basically wraps tud_task and some log.
* not pinned to any core. * Default parameters: stack size and priority as configured, argument = NULL, not pinned to any core.
* If you have more requirements for this task, you can create your own task which calls tud_task as the last step. * If you have more requirements for this task, you can create your own task which calls tud_task as the last step.
* *
* @return ESP_OK or ESP_FAIL * @retval ESP_OK run tinyusb main task successfully
* @retval ESP_FAIL run tinyusb main task failed of internal error
* @retval ESP_ERR_INVALID_STATE tinyusb main task has been created before
*/ */
esp_err_t tusb_run_task(void); esp_err_t tusb_run_task(void);
/** /**
* @brief Stops a FreeRTOS task * @brief This helper function stops and destroys the task created by `tusb_run_task()`
* *
* @return ESP_OK or ESP_FAIL * @retval ESP_OK stop and destory tinyusb main task successfully
* @retval ESP_ERR_INVALID_STATE tinyusb main task hasn't been created yet
*/ */
esp_err_t tusb_stop_task(void); esp_err_t tusb_stop_task(void);

View File

@ -61,7 +61,7 @@ enum {
}; };
bool tusb_desc_set; bool tusb_desc_set;
void tusb_set_descriptor(tusb_desc_device_t *desc, char **str_desc); void tusb_set_descriptor(tusb_desc_device_t *desc, const char **str_desc);
tusb_desc_device_t *tusb_get_active_desc(void); tusb_desc_device_t *tusb_get_active_desc(void);
char **tusb_get_active_str_desc(void); char **tusb_get_active_str_desc(void);
void tusb_clear_descriptor(void); void tusb_clear_descriptor(void);

View File

@ -130,26 +130,40 @@ uint8_t const *tud_hid_descriptor_report_cb(void)
// Driver functions // Driver functions
// ============================================================================= // =============================================================================
void tusb_set_descriptor(tusb_desc_device_t *desc, char **str_desc) void tusb_set_descriptor(tusb_desc_device_t *dev_desc, const char **str_desc)
{ {
ESP_LOGI(TAG, "Setting of a descriptor: \n" ESP_LOGI(TAG, "\n"
".bDeviceClass = %u\n" "┌─────────────────────────────────┐\n"
".bDeviceSubClass = %u,\n" "│ USB Device Descriptor Summary │\n"
".bDeviceProtocol = %u,\n" "├───────────────────┬─────────────┤\n"
".bMaxPacketSize0 = %u,\n" "│bDeviceClass │ %-4u │\n"
".idVendor = 0x%08x,\n" "├───────────────────┼─────────────┤\n"
".idProduct = 0x%08x,\n" "│bDeviceSubClass │ %-4u │\n"
".bcdDevice = 0x%08x,\n" "├───────────────────┼─────────────┤\n"
".iManufacturer = 0x%02x,\n" "│bDeviceProtocol │ %-4u │\n"
".iProduct = 0x%02x,\n" "├───────────────────┼─────────────┤\n"
".iSerialNumber = 0x%02x,\n" "│bMaxPacketSize0 │ %-4u │\n"
".bNumConfigurations = 0x%02x\n", "├───────────────────┼─────────────┤\n"
desc->bDeviceClass, desc->bDeviceSubClass, "│idVendor │ %-#10x │\n"
desc->bDeviceProtocol, desc->bMaxPacketSize0, "├───────────────────┼─────────────┤\n"
desc->idVendor, desc->idProduct, desc->bcdDevice, "│idProduct │ %-#10x │\n"
desc->iManufacturer, desc->iProduct, desc->iSerialNumber, "├───────────────────┼─────────────┤\n"
desc->bNumConfigurations); "│bcdDevice │ %-#10x │\n"
s_descriptor = *desc; "├───────────────────┼─────────────┤\n"
"│iManufacturer │ %-#10x │\n"
"├───────────────────┼─────────────┤\n"
"│iProduct │ %-#10x │\n"
"├───────────────────┼─────────────┤\n"
"│iSerialNumber │ %-#10x │\n"
"├───────────────────┼─────────────┤\n"
"│bNumConfigurations │ %-#10x │\n"
"└───────────────────┴─────────────┘",
dev_desc->bDeviceClass, dev_desc->bDeviceSubClass,
dev_desc->bDeviceProtocol, dev_desc->bMaxPacketSize0,
dev_desc->idVendor, dev_desc->idProduct, dev_desc->bcdDevice,
dev_desc->iManufacturer, dev_desc->iProduct, dev_desc->iSerialNumber,
dev_desc->bNumConfigurations);
s_descriptor = *dev_desc;
if (str_desc != NULL) { if (str_desc != NULL) {
memcpy(s_str_descriptor, str_desc, memcpy(s_str_descriptor, str_desc,

View File

@ -12,12 +12,12 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "esp_log.h" #include "sdkconfig.h"
#include "esp_rom_gpio.h"
#include "driver/gpio.h" #include "driver/gpio.h"
#include "driver/periph_ctrl.h" #include "driver/periph_ctrl.h"
#include "freertos/FreeRTOS.h" #include "esp_log.h"
#include "freertos/task.h" #include "esp_check.h"
#include "esp_rom_gpio.h"
#include "hal/gpio_ll.h" #include "hal/gpio_ll.h"
#include "hal/usb_hal.h" #include "hal/usb_hal.h"
#include "soc/gpio_periph.h" #include "soc/gpio_periph.h"
@ -26,8 +26,6 @@
#include "descriptors_control.h" #include "descriptors_control.h"
#include "tusb.h" #include "tusb.h"
#include "tusb_tasks.h" #include "tusb_tasks.h"
#include "sdkconfig.h"
#include "esp_rom_gpio.h"
const static char *TAG = "TinyUSB"; const static char *TAG = "TinyUSB";
@ -57,62 +55,30 @@ static void configure_pins(usb_hal_context_t *usb)
} }
} }
/**
* @brief Initializes the tinyUSB driver.
*
* Note: Do not change any Custom descriptor, but
* if it used it is recomended to define: bDeviceClass = TUSB_CLASS_MISC,
* bDeviceSubClass = MISC_SUBCLASS_COMMON and bDeviceClass = TUSB_CLASS_MISC
* to match with Interface Association Descriptor (IAD) for CDC
*
* @param config if equal to NULL the default descriptor will be used
* @return esp_err_t Errors during the initialization
*/
esp_err_t tinyusb_driver_install(const tinyusb_config_t *config) esp_err_t tinyusb_driver_install(const tinyusb_config_t *config)
{ {
tusb_desc_device_t *descriptor; tusb_desc_device_t *dev_descriptor;
int res; const char **string_descriptor;
char **string_descriptor; ESP_RETURN_ON_FALSE(config, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
ESP_LOGI(TAG, "Driver installation..."); // Enable APB CLK to USB peripheral
periph_module_reset(PERIPH_USB_MODULE);
periph_module_enable(PERIPH_USB_MODULE); periph_module_enable(PERIPH_USB_MODULE);
periph_module_reset(PERIPH_USB_MODULE);
// Hal init // Initialize HAL layer
usb_hal_context_t hal = { usb_hal_context_t hal = {
.use_external_phy = config->external_phy .use_external_phy = config->external_phy
}; };
usb_hal_init(&hal); usb_hal_init(&hal);
configure_pins(&hal); configure_pins(&hal);
if (config->descriptor == NULL) { dev_descriptor = config->descriptor ? config->descriptor : &descriptor_kconfig;
descriptor = &descriptor_kconfig; string_descriptor = config->string_descriptor ? config->string_descriptor : descriptor_str_kconfig;
} else {
descriptor = config->descriptor;
}
tusb_set_descriptor(dev_descriptor, string_descriptor);
if (config->string_descriptor == NULL) { ESP_RETURN_ON_FALSE(tusb_init(), ESP_FAIL, TAG, "Init TinyUSB stack failed");
string_descriptor = descriptor_str_kconfig; #if !CONFIG_TINYUSB_NO_DEFAULT_TASK
} else { ESP_RETURN_ON_ERROR(tusb_run_task(), TAG, "Run TinyUSB task failed");
string_descriptor = config->string_descriptor;
}
tusb_set_descriptor(descriptor,
string_descriptor);
if (!tusb_init()) {
ESP_LOGE(TAG, "Can't initialize the TinyUSB stack.");
return ESP_FAIL;
}
#if !CONFIG_USB_DO_NOT_CREATE_TASK
res = tusb_run_task();
if (res != ESP_OK) {
ESP_LOGE(TAG, "Can't create the TinyUSB task.");
return res;
}
#endif #endif
ESP_LOGI(TAG, "Driver installed"); ESP_LOGI(TAG, "TinyUSB Driver installed");
return ESP_OK; return ESP_OK;
} }

View File

@ -103,7 +103,7 @@ void tud_cdc_rx_cb(uint8_t itf)
while (tud_cdc_n_available(itf)) { while (tud_cdc_n_available(itf)) {
int read_res = tud_cdc_n_read( itf, int read_res = tud_cdc_n_read( itf,
acm->rx_tfbuf, acm->rx_tfbuf,
CONFIG_USB_CDC_RX_BUFSIZE ); CONFIG_TINYUSB_CDC_RX_BUFSIZE );
int res = xRingbufferSend(acm->rx_unread_buf, int res = xRingbufferSend(acm->rx_unread_buf,
acm->rx_tfbuf, acm->rx_tfbuf,
read_res, 0); read_res, 0);
@ -312,7 +312,7 @@ esp_err_t tinyusb_cdcacm_write_flush(tinyusb_cdcacm_itf_t itf, uint32_t timeout_
if (!timeout_ticks) { // if no timeout - nonblocking mode if (!timeout_ticks) { // if no timeout - nonblocking mode
int res = tud_cdc_n_write_flush(itf); int res = tud_cdc_n_write_flush(itf);
if (!res) { if (!res) {
ESP_LOGW(TAG, "flush fauled (res: %d)", res); ESP_LOGW(TAG, "flush failed (res: %d)", res);
return ESP_FAIL; return ESP_FAIL;
} else { } else {
if (tud_cdc_n_write_occupied(itf)) { if (tud_cdc_n_write_occupied(itf)) {
@ -396,7 +396,7 @@ esp_err_t tusb_cdc_acm_init(const tinyusb_config_cdcacm_t *cfg)
return ESP_ERR_NO_MEM; return ESP_ERR_NO_MEM;
} }
acm->rx_tfbuf = malloc(CONFIG_USB_CDC_RX_BUFSIZE); acm->rx_tfbuf = malloc(CONFIG_TINYUSB_CDC_RX_BUFSIZE);
if (!acm->rx_tfbuf) { if (!acm->rx_tfbuf) {
ESP_LOGE(TAG, "Creation buffer error"); ESP_LOGE(TAG, "Creation buffer error");
free_obj(itf); free_obj(itf);

View File

@ -12,13 +12,13 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "esp_log.h" #include "sdkconfig.h"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
#include "hal/usb_hal.h" #include "esp_log.h"
#include "esp_check.h"
#include "tinyusb.h" #include "tinyusb.h"
#include "tusb_tasks.h" #include "tusb_tasks.h"
#include "sdkconfig.h"
const static char *TAG = "tusb_tsk"; const static char *TAG = "tusb_tsk";
static TaskHandle_t s_tusb_tskh; static TaskHandle_t s_tusb_tskh;
@ -34,29 +34,21 @@ static void tusb_device_task(void *arg)
} }
} }
esp_err_t tusb_run_task(void) esp_err_t tusb_run_task(void)
{ {
// This function is not garanteed to be thread safe, if invoked multiple times without calling `tusb_stop_task`, will cause memory leak
// doing a sanity check anyway
ESP_RETURN_ON_FALSE(!s_tusb_tskh, ESP_ERR_INVALID_STATE, TAG, "TinyUSB main task already started");
// Create a task for tinyusb device stack: // Create a task for tinyusb device stack:
xTaskCreate(tusb_device_task, "tinyUSB: main task", CFG_TUD_MAINTASK_SIZE, NULL, CONFIG_USB_TASK_PRIORITY, &s_tusb_tskh); xTaskCreate(tusb_device_task, "TinyUSB", CONFIG_TINYUSB_TASK_STACK_SIZE, NULL, CONFIG_TINYUSB_TASK_PRIORITY, &s_tusb_tskh);
if (!s_tusb_tskh) { ESP_RETURN_ON_FALSE(s_tusb_tskh, ESP_FAIL, TAG, "create TinyUSB main task failed");
return ESP_FAIL; return ESP_OK;
} else {
return ESP_OK;
}
} }
esp_err_t tusb_stop_task(void) esp_err_t tusb_stop_task(void)
{ {
if ( s_tusb_tskh != NULL ) { ESP_RETURN_ON_FALSE(s_tusb_tskh, ESP_ERR_INVALID_STATE, TAG, "TinyUSB main task not started yet");
vTaskDelete(s_tusb_tskh); vTaskDelete(s_tusb_tskh);
} else { s_tusb_tskh = NULL;
ESP_LOGE(TAG, "tinyusb task is not started"); return ESP_OK;
return ESP_FAIL;
}
if (s_tusb_tskh) {
return ESP_FAIL;
} else {
return ESP_OK;
}
} }

View File

@ -35,7 +35,7 @@ tusb_desc_device_t descriptor_tinyusb = {
.bDeviceProtocol = 0x00, .bDeviceProtocol = 0x00,
#endif #endif
.bMaxPacketSize0 = CFG_TUD_ENDOINT0_SIZE, .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
.idVendor = 0xCafe, .idVendor = 0xCafe,
.idProduct = USB_TUSB_PID, .idProduct = USB_TUSB_PID,
@ -78,21 +78,21 @@ tusb_desc_device_t descriptor_kconfig = {
.bDeviceProtocol = 0x00, .bDeviceProtocol = 0x00,
#endif #endif
.bMaxPacketSize0 = CFG_TUD_ENDOINT0_SIZE, .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
#if CONFIG_USB_DESC_USE_ESPRESSIF_VID #if CONFIG_TINYUSB_DESC_USE_ESPRESSIF_VID
.idVendor = USB_ESPRESSIF_VID, .idVendor = USB_ESPRESSIF_VID,
#else #else
.idVendor = CONFIG_USB_DESC_CUSTOM_VID, .idVendor = CONFIG_TINYUSB_DESC_CUSTOM_VID,
#endif #endif
#if CONFIG_USB_DESC_USE_DEFAULT_PID #if CONFIG_TINYUSB_DESC_USE_DEFAULT_PID
.idProduct = USB_TUSB_PID, .idProduct = USB_TUSB_PID,
#else #else
.idProduct = CONFIG_USB_DESC_CUSTOM_PID, .idProduct = CONFIG_TINYUSB_DESC_CUSTOM_PID,
#endif #endif
.bcdDevice = CONFIG_USB_DESC_BCDDEVICE, .bcdDevice = CONFIG_TINYUSB_DESC_BCD_DEVICE,
.iManufacturer = 0x01, .iManufacturer = 0x01,
.iProduct = 0x02, .iProduct = 0x02,
@ -104,24 +104,24 @@ tusb_desc_device_t descriptor_kconfig = {
tusb_desc_strarray_device_t descriptor_str_kconfig = { tusb_desc_strarray_device_t descriptor_str_kconfig = {
// array of pointer to string descriptors // array of pointer to string descriptors
(char[]){0x09, 0x04}, // 0: is supported language is English (0x0409) (char[]){0x09, 0x04}, // 0: is supported language is English (0x0409)
CONFIG_USB_DESC_MANUFACTURER_STRING, // 1: Manufacturer CONFIG_TINYUSB_DESC_MANUFACTURER_STRING, // 1: Manufacturer
CONFIG_USB_DESC_PRODUCT_STRING, // 2: Product CONFIG_TINYUSB_DESC_PRODUCT_STRING, // 2: Product
CONFIG_USB_DESC_SERIAL_STRING, // 3: Serials, should use chip ID CONFIG_TINYUSB_DESC_SERIAL_STRING, // 3: Serials, should use chip ID
#if CONFIG_USB_CDC_ENABLED #if CONFIG_TINYUSB_CDC_ENABLED
CONFIG_USB_DESC_CDC_STRING, // 4: CDC Interface CONFIG_TINYUSB_DESC_CDC_STRING, // 4: CDC Interface
#else #else
"", "",
#endif #endif
#if CONFIG_USB_MSC_ENABLED #if CONFIG_TINYUSB_MSC_ENABLED
CONFIG_USB_DESC_MSC_STRING, // 5: MSC Interface CONFIG_TINYUSB_DESC_MSC_STRING, // 5: MSC Interface
#else #else
"", "",
#endif #endif
#if CONFIG_USB_HID_ENABLED #if CONFIG_TINYUSB_HID_ENABLED
CONFIG_USB_DESC_HID_STRING // 6: HIDs CONFIG_TINYUSB_DESC_HID_STRING // 6: HIDs
#else #else
"", "",
#endif #endif

View File

@ -0,0 +1,22 @@
# sdkconfig replacement configurations for deprecated options formatted as
# CONFIG_DEPRECATED_OPTION CONFIG_NEW_OPTION
CONFIG_USB_ENABLED CONFIG_TINYUSB
CONFIG_USB_DO_NOT_CREATE_TASK CONFIG_TINYUSB_NO_DEFAULT_TASK
CONFIG_USB_TASK_PRIORITY CONFIG_TINYUSB_TASK_PRIORITY
CONFIG_USB_DESC_USE_ESPRESSIF_VID CONFIG_TINYUSB_DESC_USE_ESPRESSIF_VID
CONFIG_USB_DESC_CUSTOM_VID CONFIG_TINYUSB_DESC_CUSTOM_VID
CONFIG_USB_DESC_USE_DEFAULT_PID CONFIG_TINYUSB_DESC_USE_DEFAULT_PID
CONFIG_USB_DESC_CUSTOM_PID CONFIG_TINYUSB_DESC_CUSTOM_PID
CONFIG_USB_DESC_BCDDEVICE CONFIG_TINYUSB_DESC_BCD_DEVICE
CONFIG_USB_DESC_MANUFACTURER_STRING CONFIG_TINYUSB_DESC_MANUFACTURER_STRING
CONFIG_USB_DESC_PRODUCT_STRING CONFIG_TINYUSB_DESC_PRODUCT_STRING
CONFIG_USB_DESC_SERIAL_STRING CONFIG_TINYUSB_DESC_SERIAL_STRING
CONFIG_USB_DESC_CDC_STRING CONFIG_TINYUSB_DESC_CDC_STRING
CONFIG_USB_DESC_MSC_STRING CONFIG_TINYUSB_DESC_MSC_STRING
CONFIG_USB_DESC_HID_STRING CONFIG_TINYUSB_DESC_HID_STRING
CONFIG_USB_MSC_ENABLED CONFIG_TINYUSB_MSC_ENABLED
CONFIG_USB_MSC_BUFSIZE CONFIG_TINYUSB_MSC_BUFSIZE
CONFIG_USB_CDC_ENABLED CONFIG_TINYUSB_CDC_ENABLED
CONFIG_USB_CDC_RX_BUFSIZE CONFIG_TINYUSB_CDC_RX_BUFSIZE
CONFIG_USB_CDC_TX_BUFSIZE CONFIG_TINYUSB_CDC_TX_BUFSIZE
CONFIG_USB_DEBUG_LEVEL CONFIG_TINYUSB_DEBUG_LEVEL

View File

@ -1,11 +1,16 @@
idf_build_get_property(target IDF_TARGET) set(srcs)
set(include)
set(priv_include)
set(priv_require)
#USB Host is currently only supported on ESP32-S2, ESP32S3 chips if(CONFIG_USB_OTG_SUPPORTED)
if(NOT "${target}" MATCHES "^esp32s[2-3]") list(APPEND srcs "hcd.c")
return() list(APPEND priv_include "private_include")
list(APPEND priv_require "hal" "driver")
endif() endif()
idf_component_register(SRCS "hcd.c" idf_component_register(SRCS ${srcs}
INCLUDE_DIRS "" INCLUDE_DIRS ${include}
PRIV_INCLUDE_DIRS "private_include" PRIV_INCLUDE_DIRS ${priv_include}
PRIV_REQUIRES hal driver) PRIV_REQUIRES ${priv_require}
)

9
components/usb/Kconfig Normal file
View File

@ -0,0 +1,9 @@
menu "USB-OTG"
visible if IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32S3
# Invisible item, enabled when USB_OTG peripheral does exist
config USB_OTG_SUPPORTED
bool
default y if IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32S3
endmenu

View File

@ -0,0 +1,23 @@
# USB-OTG Examples
See the [README.md](../README.md) file in the upper level [examples](../) directory for more information about examples.
## Common Pin Assignments
Pin assignment is only needed for ESP chips that have an USB-OTG peripheral.
If your board doesn't have a USB connector connected to the USB-OTG dedicated GPIOs, you may have to DIY a cable and connect **D+** and **D-** to the pins listed below.
```
ESP BOARD USB CONNECTOR (type A)
--
| || VCC
[USBPHY_DM_NUM] ------> | || D-
[USBPHY_DP_NUM] ------> | || D+
| || GND
--
```
Refer to `soc/usb_pins.h` to find the real GPIO number of **USBPHY_DP_NUM** and **USBPHY_DM_NUM**.
| | USB_DP | USB_DM |
| ----------- | ------ | ------ |
| ESP32-S2/S3 | GPIO20 | GPIO19 |

View File

@ -1,11 +1,11 @@
| Supported Targets | ESP32-S2 | | Supported Targets | ESP32-S2 | ESP32-S3 |
| ----------------- | -------- | | ----------------- | -------- | -------- |
# TinyUSB Sample Descriptor # TinyUSB Sample Descriptor
(See the README.md file in the upper level 'examples' directory for more information about examples.) (See the README.md file in the upper level 'examples' directory for more information about examples.)
This example shows how to set up ESP32-S2 chip to get log output via Serial Device connection This example shows how to set up ESP chip to get log output via Serial Device connection.
As a USB stack, a TinyUSB component is used. As a USB stack, a TinyUSB component is used.
@ -13,21 +13,11 @@ As a USB stack, a TinyUSB component is used.
### Hardware Required ### Hardware Required
- Any board with the ESP32-S2 chip with USB connectors or with exposed USB's D+ and D- (DATA+/DATA-) pins. Any ESP boards that have USB-OTG supported.
If the board has no USB connector, but has the pins connect pins directly to the host (e.g. with DIY cable from any USB connection cable) #### Pin Assignment
``` See common pin assignments for USB Device examples from [upper level](../README.md#common-pin-assignments).
ESP32-S2 BOARD USB CONNECTOR (type A)
--
| || VCC
[GPIO 19] --------> | || D-
[GPIO 20] --------> | || D+
| || GND
--
```
You can also use power from the USB connector.
### Build and Flash ### Build and Flash
@ -54,24 +44,36 @@ Note: if you want to send data to the target see how to implement it via `tud_cd
After the flashing you should see the output at idf monitor: After the flashing you should see the output at idf monitor:
``` ```
I (340) example: USB initialization I (288) example: USB initialization
I (340) TinyUSB: Driver installation... I (288) tusb_desc:
I (340) TinyUSB - Descriptors Control: Setting of a descriptor: ┌─────────────────────────────────┐
.bDeviceClass = 239 │ USB Device Descriptor Summary │
.bDeviceSubClass = 2, ├───────────────────┬─────────────┤
.bDeviceProtocol = 1, │bDeviceClass │ 239 │
.bMaxPacketSize0 = 64, ├───────────────────┼─────────────┤
.idVendor = 0x0000303a, │bDeviceSubClass │ 2 │
.idProduct = 0x00004001, ├───────────────────┼─────────────┤
.bcdDevice = 0x00000100, │bDeviceProtocol │ 1 │
.iManufacturer = 0x01, ├───────────────────┼─────────────┤
.iProduct = 0x02, │bMaxPacketSize0 │ 64 │
.iSerialNumber = 0x03, ├───────────────────┼─────────────┤
.bNumConfigurations = 0x01 │idVendor │ 0x303a │
├───────────────────┼─────────────┤
I (373) TinyUSB: Driver installed │idProduct │ 0x4001 │
I (373) example: USB initialization DONE ├───────────────────┼─────────────┤
I (383) example: log -> UART │bcdDevice │ 0x100 │
├───────────────────┼─────────────┤
│iManufacturer │ 0x1 │
├───────────────────┼─────────────┤
│iProduct │ 0x2 │
├───────────────────┼─────────────┤
│iSerialNumber │ 0x3 │
├───────────────────┼─────────────┤
│bNumConfigurations │ 0x1 │
└───────────────────┴─────────────┘
I (458) TinyUSB: TinyUSB Driver installed
I (468) example: USB initialization DONE
I (468) example: log -> UART
example: print -> stdout example: print -> stdout
example: print -> stderr example: print -> stderr
... ...
@ -80,8 +82,7 @@ example: print -> stderr
Other log will be printed to USB: Other log will be printed to USB:
``` ```
I (5382) example: log -> USB I (3478) example: log -> USB
example: print -> stdout example: print -> stdout
example: print -> stderr example: print -> stderr
...
``` ```

View File

@ -1,3 +1,2 @@
CONFIG_IDF_TARGET="esp32s2" CONFIG_TINYUSB=y
CONFIG_USB_ENABLED=y CONFIG_TINYUSB_CDC_ENABLED=y
CONFIG_USB_CDC_ENABLED=y

View File

@ -1,11 +1,11 @@
| Supported Targets | ESP32-S2 | | Supported Targets | ESP32-S2 | ESP32-S3 |
| ----------------- | -------- | | ----------------- | -------- | -------- |
# TinyUSB Sample Descriptor # TinyUSB Sample Descriptor
(See the README.md file in the upper level 'examples' directory for more information about examples.) (See the README.md file in the upper level 'examples' directory for more information about examples.)
This example is demonstrating how to set up ESP32-S2 chip to work as a Generic USB Device with a user-defined descriptor. You can specify a manufacturer, device's name, ID and other USB-devices parameters responsible for identification by host. This example is demonstrating how to set up ESP chip to work as a Generic USB Device with a user-defined descriptor. You can specify a manufacturer, device's name, ID and other USB-devices parameters responsible for identification by host.
As a USB stack, a TinyUSB component is used. As a USB stack, a TinyUSB component is used.
@ -13,21 +13,11 @@ As a USB stack, a TinyUSB component is used.
### Hardware Required ### Hardware Required
- Any board with the ESP32-S2 chip with USB connectors or with exposed USB's D+ and D- (DATA+/DATA-) pins. Any ESP boards that have USB-OTG supported.
If the board has no USB connector, but has the pins connect pins directly to the host (e.g. with DIY cable from any USB connection cable) #### Pin Assignment
``` See common pin assignments for USB Device examples from [upper level](../README.md#common-pin-assignments).
ESP32-S2 BOARD USB CONNECTOR (type A)
--
| || VCC
[GPIO 19] --------> | || D-
[GPIO 20] --------> | || D+
| || GND
--
```
You can also use power from the USB connector.
### Configure the project ### Configure the project
@ -35,7 +25,7 @@ There are two ways to set up a descriptor - using Menuconfig tool and in-code
#### In-code setting up #### In-code setting up
For the manual descriptor's configuration use the default example's settings and modify `tusb_sample_descriptor.c` according to your needs For the manual descriptor configuration use the default example's settings and modify `my_descriptor` in [source code](tusb_sample_descriptor_main.c) according to your needs
#### Menuconfig #### Menuconfig
@ -66,20 +56,68 @@ See the Getting Started Guide for full steps to configure and use ESP-IDF to bui
After the flashing you should see the output: After the flashing you should see the output:
``` ```
I (349) TinyUSB: Driver installation... I (287) example: USB initialization
I (349) TinyUSB - Descriptors Control: Setting of a descriptor: I (287) tusb_desc:
.bDeviceClass = 0 ┌─────────────────────────────────┐
.bDeviceSubClass = 0, │ USB Device Descriptor Summary │
.bDeviceProtocol = 0, ├───────────────────┬─────────────┤
.bMaxPacketSize0 = 64, │bDeviceClass │ 0 │
.idVendor = 0x0000303a, ├───────────────────┼─────────────┤
.idProduct = 0x00003000, │bDeviceSubClass │ 0 │
.bcdDevice = 0x00000101, ├───────────────────┼─────────────┤
.iManufacturer = 0x01, │bDeviceProtocol │ 0 │
.iProduct = 0x02, ├───────────────────┼─────────────┤
.iSerialNumber = 0x03, │bMaxPacketSize0 │ 64 │
.bNumConfigurations = 0x01 ├───────────────────┼─────────────┤
│idVendor │ 0x303a │
I (389) TinyUSB: Driver installed ├───────────────────┼─────────────┤
I (389) example: USB initialization DONE │idProduct │ 0x3000 │
├───────────────────┼─────────────┤
│bcdDevice │ 0x101 │
├───────────────────┼─────────────┤
│iManufacturer │ 0x1 │
├───────────────────┼─────────────┤
│iProduct │ 0x2 │
├───────────────────┼─────────────┤
│iSerialNumber │ 0x3 │
├───────────────────┼─────────────┤
│bNumConfigurations │ 0x1 │
└───────────────────┴─────────────┘
I (457) TinyUSB: TinyUSB Driver installed
I (467) example: USB initialization DONE
```
From PC, running `lsusb -v`, you should find the device's descriptor like:
```
Bus 001 Device 007: ID 303a:3000 I My Custom Device
Device Descriptor:
bLength 18
bDescriptorType 1
bcdUSB 2.00
bDeviceClass 0
bDeviceSubClass 0
bDeviceProtocol 0
bMaxPacketSize0 64
idVendor 0x303a
idProduct 0x3000
bcdDevice 1.01
iManufacturer 1 I
iProduct 2 My Custom Device
iSerial 3 012-345
bNumConfigurations 1
Configuration Descriptor:
bLength 9
bDescriptorType 2
wTotalLength 0x0009
bNumInterfaces 0
bConfigurationValue 1
iConfiguration 0
bmAttributes 0xa0
(Bus Powered)
Remote Wakeup
MaxPower 100mA
can't get device qualifier: Resource temporarily unavailable
can't get debug descriptor: Resource temporarily unavailable
Device Status: 0x0000
(Bus Powered)
``` ```

View File

@ -29,7 +29,7 @@ void app_main(void)
.bDescriptorType = TUSB_DESC_DEVICE, .bDescriptorType = TUSB_DESC_DEVICE,
.bcdUSB = 0x0200, // USB version. 0x0200 means version 2.0 .bcdUSB = 0x0200, // USB version. 0x0200 means version 2.0
.bDeviceClass = TUSB_CLASS_UNSPECIFIED, .bDeviceClass = TUSB_CLASS_UNSPECIFIED,
.bMaxPacketSize0 = CFG_TUD_ENDOINT0_SIZE, .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
.idVendor = 0x303A, .idVendor = 0x303A,
.idProduct = 0x3000, .idProduct = 0x3000,

View File

@ -1,6 +1,5 @@
CONFIG_IDF_TARGET="esp32s2" CONFIG_TINYUSB=y
CONFIG_USB_ENABLED=y CONFIG_TINYUSB_DESC_USE_ESPRESSIF_VID=n
CONFIG_USB_DESC_USE_ESPRESSIF_VID=n CONFIG_TINYUSB_DESC_CUSTOM_VID=0x303A
CONFIG_USB_DESC_CUSTOM_VID=0x303A CONFIG_TINYUSB_DESC_USE_DEFAULT_PID=n
CONFIG_USB_DESC_USE_DEFAULT_PID=n CONFIG_TINYUSB_DESC_CUSTOM_PID=0x3000
CONFIG_USB_DESC_CUSTOM_PID=0x3000

View File

@ -1,11 +1,11 @@
| Supported Targets | ESP32-S2 | | Supported Targets | ESP32-S2 | ESP32-S3 |
| ----------------- | -------- | | ----------------- | -------- | -------- |
# TinyUSB Sample Descriptor # TinyUSB Sample Descriptor
(See the README.md file in the upper level 'examples' directory for more information about examples.) (See the README.md file in the upper level 'examples' directory for more information about examples.)
This example shows how to set up ESP32-S2 chip to work as a USB Serial Device. This example shows how to set up ESP chip to work as a USB Serial Device.
As a USB stack, a TinyUSB component is used. As a USB stack, a TinyUSB component is used.
@ -13,21 +13,11 @@ As a USB stack, a TinyUSB component is used.
### Hardware Required ### Hardware Required
- Any board with the ESP32-S2 chip with USB connectors or with exposed USB's D+ and D- (DATA+/DATA-) pins. Any ESP boards that have USB-OTG supported.
If the board has no USB connector, but has the pins connect pins directly to the host (e.g. with DIY cable from any USB connection cable) #### Pin Assignment
``` See common pin assignments for USB Device examples from [upper level](../README.md#common-pin-assignments).
ESP32-S2 BOARD USB CONNECTOR (type A)
--
| || VCC
[GPIO 19] --------> | || D-
[GPIO 20] --------> | || D+
| || GND
--
```
You can also use power from the USB connector.
### Build and Flash ### Build and Flash
@ -43,37 +33,55 @@ idf.py -p PORT flash monitor
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
## Serial Connection
After program's start and getting of the message of readiness (`Serial device is ready to connect`) you can connect to the board using any serial port terminal application (e.g. CoolTerm).
## Example Output ## Example Output
After the flashing you should see the output: After the flashing you should see the output:
``` ```
I (346) example: USB initialization I (285) example: USB initialization
I (346) TinyUSB: Driver installation... I (285) tusb_desc:
I (346) TinyUSB - Descriptors Control: Setting of a descriptor: ┌─────────────────────────────────┐
.bDeviceClass = 239 │ USB Device Descriptor Summary │
.bDeviceSubClass = 2, ├───────────────────┬─────────────┤
.bDeviceProtocol = 1, │bDeviceClass │ 239 │
.bMaxPacketSize0 = 64, ├───────────────────┼─────────────┤
.idVendor = 0x0000303a, │bDeviceSubClass │ 2 │
.idProduct = 0x00004001, ├───────────────────┼─────────────┤
.bcdDevice = 0x00000100, │bDeviceProtocol │ 1 │
.iManufacturer = 0x01, ├───────────────────┼─────────────┤
.iProduct = 0x02, │bMaxPacketSize0 │ 64 │
.iSerialNumber = 0x03, ├───────────────────┼─────────────┤
.bNumConfigurations = 0x01 │idVendor │ 0x303a │
├───────────────────┼─────────────┤
I (362) TinyUSB: Driver installed │idProduct │ 0x4001 │
I (362) example: USB initialization DONE ├───────────────────┼─────────────┤
I (922) example: Line state changed! dtr:0, rst:0 │bcdDevice │ 0x100 │
├───────────────────┼─────────────┤
│iManufacturer │ 0x1 │
├───────────────────┼─────────────┤
│iProduct │ 0x2 │
├───────────────────┼─────────────┤
│iSerialNumber │ 0x3 │
├───────────────────┼─────────────┤
│bNumConfigurations │ 0x1 │
└───────────────────┴─────────────┘
I (455) TinyUSB: TinyUSB Driver installed
I (465) example: USB initialization DONE
``` ```
Let's try to send a string "espressif" and get the return string in your console on PC: Connect to the serial port (e.g. on Linux, it should be `/dev/ttyACM0`) by any terminal application (e.g. `picocom /dev/ttyACM0`), typing a string "espressif" and you will get the exactly same string returned.
The monitor tool will also print the communication process:
``` ```
I (18346) example: Got data (9 bytes): espressif I (146186) example: Line state changed! dtr:1, rst:1
I (147936) example: Got data (1 bytes): e
I (148136) example: Got data (1 bytes): s
I (148336) example: Got data (1 bytes): p
I (148416) example: Got data (1 bytes): r
I (148446) example: Got data (1 bytes): e
I (148676) example: Got data (1 bytes): s
I (148836) example: Got data (1 bytes): s
I (148956) example: Got data (1 bytes): i
I (149066) example: Got data (1 bytes): f
``` ```

View File

@ -20,7 +20,7 @@
#include "sdkconfig.h" #include "sdkconfig.h"
static const char *TAG = "example"; static const char *TAG = "example";
static uint8_t buf[CONFIG_USB_CDC_RX_BUFSIZE + 1]; static uint8_t buf[CONFIG_TINYUSB_CDC_RX_BUFSIZE + 1];
void tinyusb_cdc_rx_callback(int itf, cdcacm_event_t *event) void tinyusb_cdc_rx_callback(int itf, cdcacm_event_t *event)
{ {
@ -28,7 +28,7 @@ void tinyusb_cdc_rx_callback(int itf, cdcacm_event_t *event)
size_t rx_size = 0; size_t rx_size = 0;
/* read */ /* read */
esp_err_t ret = tinyusb_cdcacm_read(itf, buf, CONFIG_USB_CDC_RX_BUFSIZE, &rx_size); esp_err_t ret = tinyusb_cdcacm_read(itf, buf, CONFIG_TINYUSB_CDC_RX_BUFSIZE, &rx_size);
if (ret == ESP_OK) { if (ret == ESP_OK) {
buf[rx_size] = '\0'; buf[rx_size] = '\0';
ESP_LOGI(TAG, "Got data (%d bytes): %s", rx_size, buf); ESP_LOGI(TAG, "Got data (%d bytes): %s", rx_size, buf);

View File

@ -1,3 +1,2 @@
CONFIG_IDF_TARGET="esp32s2" CONFIG_TINYUSB=y
CONFIG_USB_ENABLED=y CONFIG_TINYUSB_CDC_ENABLED=y
CONFIG_USB_CDC_ENABLED=y