usb_device.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. USB Device Driver
  2. =================
  3. {IDF_TARGET_USB_DP_GPIO_NUM:default="20"}
  4. {IDF_TARGET_USB_DM_GPIO_NUM:default="19"}
  5. {IDF_TARGET_USB_EP_NUM:default="6"}
  6. {IDF_TARGET_USB_EP_NUM_INOUT:default="5"}
  7. {IDF_TARGET_USB_EP_NUM_IN:default="1"}
  8. Overview
  9. --------
  10. The driver allows you to use {IDF_TARGET_NAME} chips to develop USB devices on a top of TinyUSB stack. TinyUSB is integrated with ESP-IDF to provide USB features of the framework. Using this driver the chip works as simple or composite device supporting several USB devices simultaneously.
  11. TinyUSB stack is distributed via `IDF Component Registry <https://components.espressif.com/components/espressif/esp_tinyusb>`__.
  12. Our USB-OTG implementation is limited to {IDF_TARGET_USB_EP_NUM} USB endpoints ({IDF_TARGET_USB_EP_NUM_INOUT} IN/OUT endpoints and {IDF_TARGET_USB_EP_NUM_IN} IN endpoint) . Please note that enabling Secure Boot or flash encryption disables the USB-OTG USB stack in the ROM, disallowing updates via the serial emulation or Device Firmware Update (DFU) on that port. For more details, please refer to `technical reference manual <{IDF_TARGET_TRM_EN_URL}>`_.
  13. Features
  14. --------
  15. - Configuration of device and string USB descriptors
  16. - USB Serial Device (CDC-ACM)
  17. - Input and output streams through USB Serial Device
  18. - Other USB classes (MIDI, MSC, HID...) support directly via TinyUSB
  19. - USB Composite Device (MSC + CDC)
  20. - VBUS monitoring for self-powered devices
  21. Hardware USB Connection
  22. -----------------------
  23. - Any board with the {IDF_TARGET_NAME} chip with USB connectors or with exposed USB's D+ and D- (DATA+/DATA-) pins.
  24. If the board has no USB connector but has the pins, connect pins directly to the host (e.g., with do-it-yourself cable from any USB connection cable).
  25. On {IDF_TARGET_NAME}, connect GPIO {IDF_TARGET_USB_DP_GPIO_NUM} and {IDF_TARGET_USB_DM_GPIO_NUM} to D+/D- respectively:
  26. .. figure:: ../../../_static/usb-board-connection.png
  27. :align: center
  28. :alt: Connection of an ESP board to a USB host
  29. :figclass: align-center
  30. Self-powered devices must also connect VBUS through voltage divider or comparator, more details in :ref:`self-powered-device` subchapter.
  31. Driver Structure
  32. ----------------
  33. As the basis is used the TinyUSB stack.
  34. On top of it the driver implements:
  35. - Customization of USB descriptors
  36. - Serial device support
  37. - Redirecting of standard streams through the Serial device
  38. - Storage Media (SPI-Flash and SD-Card) for USB Device MSC Class.
  39. - Encapsulated driver's task servicing the TinyUSB
  40. Configuration
  41. -------------
  42. To use the component, you need to add it as a dependency via the following command. For more details, please refer to `IDF Component Registry <https://components.espressif.com/components/espressif/esp_tinyusb>`__.
  43. .. code:: bash
  44. idf.py add-dependency esp_tinyusb
  45. Via Menuconfig options you can specify:
  46. - Several descriptor's parameters (see Descriptors Configuration below)
  47. - USB Serial low-level configuration
  48. - The verbosity of the TinyUSB's log
  49. - Disable the TinyUSB main task (for the custom implementation)
  50. Descriptors Configuration
  51. ^^^^^^^^^^^^^^^^^^^^^^^^^
  52. The driver's descriptors are provided by :cpp:type:`tinyusb_config_t` structure's :cpp:member:`device_descriptor`, :cpp:member:`configuration_descriptor` and :cpp:member:`string_descriptor` members. Therefore, you should initialize :cpp:type:`tinyusb_config_t` with your desired descriptors before calling :cpp:func:`tinyusb_driver_install` to install the driver.
  53. However, the driver also provides default descriptors. You can install the driver with default device and string descriptors by setting the :cpp:member:`device_descriptor` and :cpp:member:`string_descriptor` members of :cpp:type:`tinyusb_config_t` to `NULL` before calling :cpp:func:`tinyusb_driver_install`. To lower your development effort we also provide default configuration descriptor for CDC and MSC class, as these classes rarely require custom configuration. The driver's default device descriptor is specified using Menuconfig, where the following fields should be configured:
  54. - PID
  55. - VID
  56. - bcdDevice
  57. - Manufacturer
  58. - Product name
  59. - Name of CDC or MSC device if it is On
  60. - Serial number
  61. If you want to use your own descriptors with extended modification, you can define them during the driver installation process.
  62. Install Driver
  63. --------------
  64. To initialize the driver, users should call :cpp:func:`tinyusb_driver_install`. The driver's configuration is specified in a :cpp:type:`tinyusb_config_t` structure that is passed as an argument to :cpp:func:`tinyusb_driver_install`.
  65. Note that the :cpp:type:`tinyusb_config_t` structure can be zero initialized (e.g., ``const tinyusb_config_t tusb_cfg = { 0 };``) or partially (as shown below). For any member that is initialized to `0` or `NULL`, the driver will use its default configuration values for that member (see example below)
  66. .. code-block:: c
  67. const tinyusb_config_t partial_init = {
  68. .device_descriptor = NULL, // Use default device descriptor specified in Menuconfig
  69. .string_descriptor = NULL, // Use default string descriptors specified in Menuconfig
  70. .external_phy = false, // Use internal USB PHY
  71. .configuration_descriptor = NULL, // Use default configuration descriptor according to settings in Menuconfig
  72. };
  73. .. _self-powered-device:
  74. Self-Powered Device
  75. -------------------
  76. USB specification mandates self-powered devices to monitor voltage level on USB's VBUS signal. As opposed to bus-powered devices, a self-powered device can be fully functional even without USB connection. The self-powered device detects connection and disconnection events by monitoring the VBUS voltage level. VBUS is considered valid if it rises above 4.75 V and invalid if it falls below 4.35 V.
  77. No {IDF_TARGET_NAME} pin is 5 V tolerant, so you must connect the VBUS to {IDF_TARGET_NAME} via a comparator with voltage thresholds as described above, or use a simple resistor voltage divider that will output (0.75 x Vdd) if VBUS is 4.4 V (see figure below). In both cases, voltage on the sensing pin must be logic low within 3 ms after the device is unplugged from USB host.
  78. .. figure:: ../../../_static/diagrams/usb/usb_vbus_voltage_monitor.png
  79. :align: center
  80. :alt: Simple voltage divider for VBUS monitoring
  81. :figclass: align-center
  82. Simple voltage divider for VBUS monitoring
  83. To use this feature, in :cpp:type:`tinyusb_config_t` you must set :cpp:member:`self_powered` to ``true`` and :cpp:member:`vbus_monitor_io` to GPIO number that will be used for VBUS monitoring.
  84. USB Serial Device (CDC-ACM)
  85. ---------------------------
  86. If the CDC option is enabled in Menuconfig, the USB Serial Device can be initialized with :cpp:func:`tusb_cdc_acm_init` according to the settings from :cpp:type:`tinyusb_config_cdcacm_t` (see example below).
  87. .. code-block:: c
  88. const tinyusb_config_cdcacm_t acm_cfg = {
  89. .usb_dev = TINYUSB_USBDEV_0,
  90. .cdc_port = TINYUSB_CDC_ACM_0,
  91. .rx_unread_buf_sz = 64,
  92. .callback_rx = NULL,
  93. .callback_rx_wanted_char = NULL,
  94. .callback_line_state_changed = NULL,
  95. .callback_line_coding_changed = NULL
  96. };
  97. tusb_cdc_acm_init(&acm_cfg);
  98. To specify callbacks you can either set the pointer to your :cpp:type:`tusb_cdcacm_callback_t` function in the configuration structure or call :cpp:func:`tinyusb_cdcacm_register_callback` after initialization.
  99. USB Serial Console
  100. ^^^^^^^^^^^^^^^^^^
  101. The driver allows to redirect all standard application streams (stdin, stdout, stderr) to the USB Serial Device and return them to UART using :cpp:func:`esp_tusb_init_console`/:cpp:func:`esp_tusb_deinit_console` functions.
  102. USB Mass Storage Device (MSC)
  103. -----------------------------
  104. If the MSC CONFIG_TINYUSB_MSC_ENABLED option is enabled in Menuconfig, the ESP Chip can be used as USB MSC Device. The storage media (spi-flash or sd-card) can be initialized as shown below (see example below).
  105. - SPI-Flash
  106. .. code-block:: c
  107. static esp_err_t storage_init_spiflash(wl_handle_t *wl_handle)
  108. {
  109. ***
  110. esp_partition_t *data_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, NULL);
  111. ***
  112. wl_mount(data_partition, wl_handle);
  113. ***
  114. }
  115. storage_init_spiflash(&wl_handle);
  116. const tinyusb_msc_spiflash_config_t config_spi = {
  117. .wl_handle = wl_handle
  118. };
  119. tinyusb_msc_storage_init_spiflash(&config_spi);
  120. - SD-Card
  121. .. code-block:: c
  122. static esp_err_t storage_init_sdmmc(sdmmc_card_t **card)
  123. {
  124. ***
  125. sdmmc_host_t host = SDMMC_HOST_DEFAULT();
  126. sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
  127. // For SD Card, set bus width to use
  128. slot_config.width = 4;
  129. slot_config.clk = CONFIG_EXAMPLE_PIN_CLK;
  130. slot_config.cmd = CONFIG_EXAMPLE_PIN_CMD;
  131. slot_config.d0 = CONFIG_EXAMPLE_PIN_D0;
  132. slot_config.d1 = CONFIG_EXAMPLE_PIN_D1;
  133. slot_config.d2 = CONFIG_EXAMPLE_PIN_D2;
  134. slot_config.d3 = CONFIG_EXAMPLE_PIN_D3;
  135. slot_config.flags |= SDMMC_SLOT_FLAG_INTERNAL_PULLUP;
  136. sd_card = (sdmmc_card_t *)malloc(sizeof(sdmmc_card_t));
  137. (*host.init)();
  138. sdmmc_host_init_slot(host.slot, (const sdmmc_slot_config_t *) &slot_config);
  139. sdmmc_card_init(&host, sd_card);
  140. ***
  141. }
  142. storage_init_sdmmc(&card);
  143. const tinyusb_msc_sdmmc_config_t config_sdmmc = {
  144. .card = card
  145. };
  146. tinyusb_msc_storage_init_sdmmc(&config_sdmmc);
  147. Application Examples
  148. --------------------
  149. The table below describes the code examples available in the directory :example:`peripherals/usb/`.
  150. .. list-table::
  151. :widths: 35 65
  152. :header-rows: 1
  153. * - Code Example
  154. - Description
  155. * - :example:`peripherals/usb/device/tusb_console`
  156. - How to set up {IDF_TARGET_NAME} chip to get log output via Serial Device connection
  157. * - :example:`peripherals/usb/device/tusb_serial_device`
  158. - How to set up {IDF_TARGET_NAME} chip to work as a USB Serial Device
  159. * - :example:`peripherals/usb/device/tusb_midi`
  160. - How to set up {IDF_TARGET_NAME} chip to work as a USB MIDI Device
  161. * - :example:`peripherals/usb/device/tusb_hid`
  162. - How to set up {IDF_TARGET_NAME} chip to work as a USB Human Interface Device
  163. * - :example:`peripherals/usb/device/tusb_msc`
  164. - How to set up {IDF_TARGET_NAME} chip to work as a USB Mass Storage Device
  165. * - :example:`peripherals/usb/device/tusb_composite_msc_serialdevice`
  166. - How to set up {IDF_TARGET_NAME} chip to work as a Composite USB Device (MSC + CDC)