usb_device.rst 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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} number of 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. Hardware USB Connection
  19. -----------------------
  20. - Any board with the {IDF_TARGET_NAME} chip with USB connectors or with exposed USB's D+ and D- (DATA+/DATA-) pins.
  21. 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).
  22. On {IDF_TARGET_NAME}, connect GPIO {IDF_TARGET_USB_DP_GPIO_NUM} and {IDF_TARGET_USB_DM_GPIO_NUM} to D+/D- respectively:
  23. .. figure:: ../../../_static/usb-board-connection.png
  24. :align: center
  25. :alt: Connection of a board to a host ESP chip
  26. :figclass: align-center
  27. Driver Structure
  28. ----------------
  29. As the basis is used the TinyUSB stack.
  30. On top of it the driver implements:
  31. - Customization of USB descriptors
  32. - Serial device support
  33. - Redirecting of standard streams through the Serial device
  34. - Encapsulated driver's task servicing the TinyUSB
  35. Configuration
  36. -------------
  37. Via Menuconfig options you can specify:
  38. - Several of descriptor's parameters (see: Descriptors Configuration bellow)
  39. - USB Serial low-level Configuration
  40. - The verbosity of the TinyUSB's log
  41. - Disable the TinyUSB main task (for the custom implementation)
  42. Descriptors Configuration
  43. ^^^^^^^^^^^^^^^^^^^^^^^^^
  44. 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.
  45. 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:
  46. - PID
  47. - VID
  48. - bcdDevice
  49. - Manufacturer
  50. - Product name
  51. - Name of CDC device if it is On
  52. - Serial number
  53. If you want to use your own descriptors with extended modification, you can define them during the driver installation process.
  54. Install Driver
  55. --------------
  56. 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`.
  57. 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)
  58. .. code-block:: c
  59. const tinyusb_config_t partial_init = {
  60. .device_descriptor = NULL, // Use default device descriptor specified in Menuconfig
  61. .string_descriptor = NULL, // Use default string descriptors specified in Menuconfig
  62. .external_phy = false, // Use internal USB PHY
  63. .configuration_descriptor = NULL, // Use default configuration descriptor according to settings in Menuconfig
  64. };
  65. USB Serial Device (CDC-ACM)
  66. ---------------------------
  67. 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).
  68. .. code-block:: c
  69. const tinyusb_config_cdcacm_t acm_cfg = {
  70. .usb_dev = TINYUSB_USBDEV_0,
  71. .cdc_port = TINYUSB_CDC_ACM_0,
  72. .rx_unread_buf_sz = 64,
  73. .callback_rx = NULL,
  74. .callback_rx_wanted_char = NULL,
  75. .callback_line_state_changed = NULL,
  76. .callback_line_coding_changed = NULL
  77. };
  78. tusb_cdc_acm_init(&acm_cfg);
  79. 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.
  80. USB Serial Console
  81. ^^^^^^^^^^^^^^^^^^
  82. 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.
  83. Application Examples
  84. --------------------
  85. The table below describes the code examples available in the directory :example:`peripherals/usb/`.
  86. .. list-table::
  87. :widths: 35 65
  88. :header-rows: 1
  89. * - Code Example
  90. - Description
  91. * - :example:`peripherals/usb/device/tusb_console`
  92. - How to set up {IDF_TARGET_NAME} chip to get log output via Serial Device connection
  93. * - :example:`peripherals/usb/device/tusb_serial_device`
  94. - How to set up {IDF_TARGET_NAME} chip to work as a USB Serial Device
  95. * - :example:`peripherals/usb/device/tusb_midi`
  96. - How to set up {IDF_TARGET_NAME} chip to work as a USB MIDI Device
  97. * - :example:`peripherals/usb/device/tusb_hid`
  98. - How to set up {IDF_TARGET_NAME} chip to work as a USB Human Interface Device
  99. API Reference
  100. -------------
  101. .. include-build-file:: inc/tinyusb.inc
  102. .. include-build-file:: inc/tinyusb_types.inc
  103. .. include-build-file:: inc/tusb_cdc_acm.inc
  104. .. include-build-file:: inc/tusb_console.inc
  105. .. include-build-file:: inc/tusb_tasks.inc
  106. .. include-build-file:: inc/vfs_tinyusb.inc