usb_device.rst 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. 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) - find more information in `technical reference manual <{IDF_TARGET_TRM_EN_URL}>`_.
  12. Features
  13. --------
  14. - Configuration of device and string USB descriptors
  15. - USB Serial Device (CDC-ACM)
  16. - Input and output streams through USB Serial Device
  17. - Other USB classes (MIDI, MSC, HID...) support directly via TinyUSB
  18. - VBUS monitoring for self-powered devices
  19. Hardware USB Connection
  20. -----------------------
  21. - Any board with the {IDF_TARGET_NAME} chip with USB connectors or with exposed USB's D+ and D- (DATA+/DATA-) pins.
  22. 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).
  23. On {IDF_TARGET_NAME}, connect GPIO {IDF_TARGET_USB_DP_GPIO_NUM} and {IDF_TARGET_USB_DM_GPIO_NUM} to D+/D- respectively:
  24. .. figure:: ../../../_static/usb-board-connection.png
  25. :align: center
  26. :alt: Connection of an ESP board to a USB host
  27. :figclass: align-center
  28. Self-powered devices must also connect VBUS through voltage divider or comparator, more details in :ref:`self-powered-device` subchapter.
  29. Driver Structure
  30. ----------------
  31. As the basis is used the TinyUSB stack.
  32. On top of it the driver implements:
  33. - Customization of USB descriptors
  34. - Serial device support
  35. - Redirecting of standard streams through the Serial device
  36. - Encapsulated driver's task servicing the TinyUSB
  37. Configuration
  38. -------------
  39. Via Menuconfig options you can specify:
  40. - Several of descriptor's parameters (see: Descriptors Configuration bellow)
  41. - USB Serial low-level Configuration
  42. - The verbosity of the TinyUSB's log
  43. - Disable the TinyUSB main task (for the custom implementation)
  44. Descriptors Configuration
  45. ^^^^^^^^^^^^^^^^^^^^^^^^^
  46. 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.
  47. 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:
  48. - PID
  49. - VID
  50. - bcdDevice
  51. - Manufacturer
  52. - Product name
  53. - Name of CDC device if it is On
  54. - Serial number
  55. If you want to use your own descriptors with extended modification, you can define them during the driver installation process.
  56. Install Driver
  57. --------------
  58. 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`.
  59. 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)
  60. .. code-block:: c
  61. const tinyusb_config_t partial_init = {
  62. .device_descriptor = NULL, // Use default device descriptor specified in Menuconfig
  63. .string_descriptor = NULL, // Use default string descriptors specified in Menuconfig
  64. .external_phy = false, // Use internal USB PHY
  65. .configuration_descriptor = NULL, // Use default configuration descriptor according to settings in Menuconfig
  66. };
  67. .. _self-powered-device:
  68. Self-Powered Device
  69. -------------------
  70. 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.75V and invalid if it falls below 4.35V.
  71. No {IDF_TARGET_NAME} pin is 5V 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.4V (see figure below). In both cases, voltage on the sensing pin must be logic low within 3ms after the device is unplugged from USB host.
  72. .. figure:: ../../../_static/diagrams/usb/usb_vbus_voltage_monitor.png
  73. :align: center
  74. :alt: Simple voltage divider for VBUS monitoring
  75. :figclass: align-center
  76. Simple voltage divider for VBUS monitoring
  77. 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.
  78. USB Serial Device (CDC-ACM)
  79. ---------------------------
  80. 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).
  81. .. code-block:: c
  82. const tinyusb_config_cdcacm_t acm_cfg = {
  83. .usb_dev = TINYUSB_USBDEV_0,
  84. .cdc_port = TINYUSB_CDC_ACM_0,
  85. .rx_unread_buf_sz = 64,
  86. .callback_rx = NULL,
  87. .callback_rx_wanted_char = NULL,
  88. .callback_line_state_changed = NULL,
  89. .callback_line_coding_changed = NULL
  90. };
  91. tusb_cdc_acm_init(&acm_cfg);
  92. 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.
  93. USB Serial Console
  94. ^^^^^^^^^^^^^^^^^^
  95. The driver allows to redirect all standard application streams (stdinm 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.
  96. Application Examples
  97. --------------------
  98. The table below describes the code examples available in the directory :example:`peripherals/usb/`.
  99. .. list-table::
  100. :widths: 35 65
  101. :header-rows: 1
  102. * - Code Example
  103. - Description
  104. * - :example:`peripherals/usb/device/tusb_console`
  105. - How to set up {IDF_TARGET_NAME} chip to get log output via Serial Device connection
  106. * - :example:`peripherals/usb/device/tusb_serial_device`
  107. - How to set up {IDF_TARGET_NAME} chip to work as a USB Serial Device
  108. * - :example:`peripherals/usb/device/tusb_midi`
  109. - How to set up {IDF_TARGET_NAME} chip to work as a USB MIDI Device
  110. * - :example:`peripherals/usb/device/tusb_hid`
  111. - How to set up {IDF_TARGET_NAME} chip to work as a USB Human Interface Device
  112. API Reference
  113. -------------
  114. .. include-build-file:: inc/tinyusb.inc
  115. .. include-build-file:: inc/tinyusb_types.inc
  116. .. include-build-file:: inc/tusb_cdc_acm.inc
  117. .. include-build-file:: inc/tusb_console.inc
  118. .. include-build-file:: inc/tusb_tasks.inc
  119. .. include-build-file:: inc/vfs_tinyusb.inc