ESP-IDF Firmware
Firmware architecture and call graph
Loading...
Searching...
No Matches
tinyusb.c File Reference
#include "sdkconfig.h"
#include "esp_log.h"
#include "esp_check.h"
#include "esp_err.h"
#include "esp_private/periph_ctrl.h"
#include "esp_private/usb_phy.h"
#include "soc/usb_pins.h"
#include "tinyusb.h"
#include "descriptors_control.h"
#include "usb_descriptors.h"
#include "tusb.h"
#include "tusb_tasks.h"
Include dependency graph for tinyusb.c:

Go to the source code of this file.

Macros

#define USBPHY_VP_NUM   (-1)
#define USBPHY_VM_NUM   (-1)
#define USBPHY_RCV_NUM   (-1)
#define USBPHY_OEN_NUM   (-1)
#define USBPHY_VPO_NUM   (-1)
#define USBPHY_VMO_NUM   (-1)

Functions

esp_err_t tinyusb_driver_install (const tinyusb_config_t *config)
 This is an all-in-one helper function, including:

Variables

static const char * TAG = "TinyUSB"
static usb_phy_handle_t phy_hdl

Macro Definition Documentation

◆ USBPHY_OEN_NUM

#define USBPHY_OEN_NUM   (-1)

Definition at line 24 of file tinyusb.c.

Referenced by tinyusb_driver_install().

◆ USBPHY_RCV_NUM

#define USBPHY_RCV_NUM   (-1)

Definition at line 23 of file tinyusb.c.

Referenced by tinyusb_driver_install().

◆ USBPHY_VM_NUM

#define USBPHY_VM_NUM   (-1)

Definition at line 22 of file tinyusb.c.

Referenced by tinyusb_driver_install().

◆ USBPHY_VMO_NUM

#define USBPHY_VMO_NUM   (-1)

Definition at line 26 of file tinyusb.c.

Referenced by tinyusb_driver_install().

◆ USBPHY_VP_NUM

#define USBPHY_VP_NUM   (-1)

Definition at line 21 of file tinyusb.c.

Referenced by tinyusb_driver_install().

◆ USBPHY_VPO_NUM

#define USBPHY_VPO_NUM   (-1)

Definition at line 25 of file tinyusb.c.

Referenced by tinyusb_driver_install().

Function Documentation

◆ tinyusb_driver_install()

esp_err_t tinyusb_driver_install ( const tinyusb_config_t * config)

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,
Parameters
configtinyusb stack specific configuration
Return values
ESP_ERR_INVALID_ARGInstall driver and tinyusb stack failed because of invalid argument
ESP_FAILInstall driver and tinyusb stack failed because of internal error
ESP_OKInstall driver and tinyusb stack successfully

Definition at line 32 of file tinyusb.c.

33{
34 const tusb_desc_device_t *dev_descriptor;
35 const char **string_descriptor;
36 const uint8_t *cfg_descriptor;
37 ESP_RETURN_ON_FALSE(config, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
38
39 // Configure USB PHY
40 usb_phy_config_t phy_conf = {
41 .controller = USB_PHY_CTRL_OTG,
42 .otg_mode = USB_OTG_MODE_DEVICE,
43 };
44
45 // External PHY IOs config
46 usb_phy_ext_io_conf_t ext_io_conf = {
47 .vp_io_num = USBPHY_VP_NUM,
48 .vm_io_num = USBPHY_VM_NUM,
49 .rcv_io_num = USBPHY_RCV_NUM,
50 .oen_io_num = USBPHY_OEN_NUM,
51 .vpo_io_num = USBPHY_VPO_NUM,
52 .vmo_io_num = USBPHY_VMO_NUM,
53 };
54 if (config->external_phy) {
55 phy_conf.target = USB_PHY_TARGET_EXT;
56 phy_conf.ext_io_conf = &ext_io_conf;
57 } else {
58 phy_conf.target = USB_PHY_TARGET_INT;
59 }
60
61 // OTG IOs config
62 const usb_phy_otg_io_conf_t otg_io_conf = USB_PHY_SELF_POWERED_DEVICE(config->vbus_monitor_io);
63 if (config->self_powered) {
64 phy_conf.otg_io_conf = &otg_io_conf;
65 }
66 ESP_RETURN_ON_ERROR(usb_new_phy(&phy_conf, &phy_hdl), TAG, "Install USB PHY failed");
67
68 if (config->configuration_descriptor) {
69 cfg_descriptor = config->configuration_descriptor;
70 } else {
71#if (CONFIG_TINYUSB_HID_COUNT > 0 || CONFIG_TINYUSB_MIDI_COUNT > 0)
72 // For HID device, configuration descriptor must be provided
73 ESP_RETURN_ON_FALSE(config->configuration_descriptor, ESP_ERR_INVALID_ARG, TAG, "Configuration descriptor must be provided for this device");
74#else
75 cfg_descriptor = descriptor_cfg_kconfig;
76 ESP_LOGW(TAG, "The device's configuration descriptor is not provided by user, using default.");
77#endif
78 }
79
80 if (config->string_descriptor) {
81 string_descriptor = config->string_descriptor;
82 } else {
83 string_descriptor = descriptor_str_kconfig;
84 ESP_LOGW(TAG, "The device's string descriptor is not provided by user, using default.");
85 }
86
87 if (config->device_descriptor) {
88 dev_descriptor = config->device_descriptor;
89 } else {
90 dev_descriptor = &descriptor_dev_kconfig;
91 ESP_LOGW(TAG, "The device's device descriptor is not provided by user, using default.");
92 }
93
94 tusb_set_descriptor(dev_descriptor, string_descriptor, cfg_descriptor);
95
96 ESP_RETURN_ON_FALSE(tusb_init(), ESP_FAIL, TAG, "Init TinyUSB stack failed");
97#if !CONFIG_TINYUSB_NO_DEFAULT_TASK
98 ESP_RETURN_ON_ERROR(tusb_run_task(), TAG, "Run TinyUSB task failed");
99#endif
100 ESP_LOGI(TAG, "TinyUSB Driver installed");
101 return ESP_OK;
102}
#define ESP_RETURN_ON_ERROR(x, log_tag, format,...)
void tusb_set_descriptor(const tusb_desc_device_t *dev_desc, const char **str_desc, const uint8_t *cfg_desc)
#define ESP_OK
Definition esp_err.h:23
static const char * TAG
Definition main/main.c:31
bool external_phy
Definition tinyusb.h:35
bool self_powered
Definition tinyusb.h:37
const char ** string_descriptor
Definition tinyusb.h:34
const uint8_t * configuration_descriptor
Definition tinyusb.h:36
const tusb_desc_device_t * device_descriptor
Definition tinyusb.h:31
int vbus_monitor_io
Definition tinyusb.h:38
#define USBPHY_OEN_NUM
Definition tinyusb.c:24
#define USBPHY_VP_NUM
Definition tinyusb.c:21
#define USBPHY_VMO_NUM
Definition tinyusb.c:26
#define USBPHY_RCV_NUM
Definition tinyusb.c:23
static usb_phy_handle_t phy_hdl
Definition tinyusb.c:30
#define USBPHY_VM_NUM
Definition tinyusb.c:22
#define USBPHY_VPO_NUM
Definition tinyusb.c:25
esp_err_t tusb_run_task(void)
This helper function creates and starts a task which wraps tud_task().
Definition tusb_tasks.c:29
tusb_desc_strarray_device_t descriptor_str_kconfig
const tusb_desc_device_t descriptor_dev_kconfig
const uint8_t descriptor_cfg_kconfig[]

References tinyusb_config_t::configuration_descriptor, descriptor_cfg_kconfig, descriptor_dev_kconfig, descriptor_str_kconfig, tinyusb_config_t::device_descriptor, ESP_OK, ESP_RETURN_ON_ERROR, tinyusb_config_t::external_phy, phy_hdl, tinyusb_config_t::self_powered, tinyusb_config_t::string_descriptor, TAG, tusb_run_task(), tusb_set_descriptor(), USBPHY_OEN_NUM, USBPHY_RCV_NUM, USBPHY_VM_NUM, USBPHY_VMO_NUM, USBPHY_VP_NUM, USBPHY_VPO_NUM, and tinyusb_config_t::vbus_monitor_io.

Referenced by app_main().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ phy_hdl

usb_phy_handle_t phy_hdl
static

Definition at line 30 of file tinyusb.c.

Referenced by tinyusb_driver_install().

◆ TAG

const char* TAG = "TinyUSB"
static

Definition at line 29 of file tinyusb.c.