usb.rst 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. USB Driver
  2. ==========
  3. Overview
  4. --------
  5. The driver allows users to use {IDF_TARGET_NAME} chips to develop USB devices on top the TinyUSB stack. TinyUSB is integrating with ESP-IDF to provide USB features of the framework. Using this driver the chip works as a composite device supporting to represent several USB devices simultaneously. Currently, only the communications device class (CDC) type of the device with the ACM (Abstract Control Model) subclass is supported.
  6. Features
  7. --------
  8. - Configuration of device and string USB descriptors
  9. - USB Serial Device (CDC-ACM)
  10. - Input and output through USB Serial Device
  11. Hardware USB Connection
  12. -----------------------
  13. - Any board with the {IDF_TARGET_NAME} chip with USB connectors or with exposed USB's D+ and D- (DATA+/DATA-) pins.
  14. 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). For example, connect GPIO19/20 to D-/D+ respectively for an ESP32-S2 board:
  15. .. figure:: ../../../_static/usb-board-connection.png
  16. :align: center
  17. :alt: Connection of a board to a host ESP32-S2
  18. :figclass: align-center
  19. Driver Structure
  20. ----------------
  21. As the basis is used the TinyUSB stack.
  22. On top of it the driver implements:
  23. - Customization of USB descriptors
  24. - Serial device support
  25. - Redirecting of standard streams through the Serial device
  26. - Encapsulated driver's task servicing the TinyuSB
  27. Configuration
  28. -------------
  29. Via Menuconfig options you can specify:
  30. - Several of descriptor's parameters (see: Descriptors Configuration bellow)
  31. - USB Serial low-level Configuration
  32. - The verbosity of the TinyUSB's log
  33. - Disable the TinyUSB main task (for the custom implementation)
  34. Descriptors Configuration
  35. ^^^^^^^^^^^^^^^^^^^^^^^^^
  36. The driver's descriptors are provided by the :cpp:type:`tinyusb_config_t` structure's :cpp:member:`descriptor` and :cpp:member:`string_descriptor` members. Therefore, users should initialize :cpp:type:`tinyusb_config_t` to their desired descriptor before calling :cpp:func:`tinyusb_driver_install` to install driver.
  37. However, the driver also provides a default descriptor. The driver can be installed with the default descriptor by setting the :cpp:member:`descriptor` and :cpp:member:`string_descriptor` members of :cpp:type:`tinyusb_config_t` to `NULL` before calling :cpp:func:`tinyusb_driver_install`. The driver's default descriptor is specified using Menuconfig, where the following fields should be configured:
  38. - PID
  39. - VID
  40. - bcdDevice
  41. - Manufacturer
  42. - Product name
  43. - Name of CDC device if it is On
  44. - Serial number
  45. If you want to use own descriptors with extended modification, you can define them during the driver installation process
  46. Install Driver
  47. --------------
  48. 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`.
  49. Note that the :cpp:type:`tinyusb_config_t` structure can be zero initialized (e.g. ``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)
  50. .. code-block:: c
  51. tinyusb_config_t partial_init = {
  52. .descriptor = NULL; //Uses default descriptor specified in Menuconfig
  53. .string_descriptor = NULL; //Uses default string specified in Menuconfig
  54. .external_phy = false;
  55. }
  56. USB Serial Device (CDC-ACM)
  57. ---------------------------
  58. If the CDC option is enabled in Menuconfig, the USB Serial Device could be initialized with :cpp:func:`tusb_cdc_acm_init` according to the settings from :cpp:type:`tinyusb_config_cdcacm_t` (see example below).
  59. .. code-block:: c
  60. tinyusb_config_cdcacm_t amc_cfg = {
  61. .usb_dev = TINYUSB_USBDEV_0,
  62. .cdc_port = TINYUSB_CDC_ACM_0,
  63. .rx_unread_buf_sz = 64,
  64. .callback_rx = NULL,
  65. .callback_rx_wanted_char = NULL,
  66. .callback_line_state_changed = NULL,
  67. .callback_line_coding_changed = NULL
  68. };
  69. tusb_cdc_acm_init(&amc_cfg);
  70. 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.
  71. USB Serial Console
  72. ^^^^^^^^^^^^^^^^^^
  73. The driver allows to redirect all standard application strings (stdin/out/err) to the USB Serial Device and return them to UART using :cpp:func:`esp_tusb_init_console`/:cpp:func:`esp_tusb_deinit_console` functions.
  74. Application Examples
  75. --------------------
  76. The table below describes the code examples available in the directory :example:`peripherals/usb/`.
  77. .. list-table::
  78. :widths: 35 65
  79. :header-rows: 1
  80. * - Code Example
  81. - Description
  82. * - :example:`peripherals/usb/tusb_console`
  83. - How to set up {IDF_TARGET_NAME} chip to get log output via Serial Device connection
  84. * - :example:`peripherals/usb/tusb_sample_descriptor`
  85. - How to set up {IDF_TARGET_NAME} chip to work as a Generic USB Device with a user-defined descriptor
  86. * - :example:`peripherals/usb/tusb_serial_device`
  87. - How to set up {IDF_TARGET_NAME} chip to work as a USB Serial Device
  88. API Reference
  89. -------------
  90. .. include-build-file:: inc/tinyusb.inc
  91. .. include-build-file:: inc/tinyusb_types.inc
  92. .. include-build-file:: inc/tusb_cdc_acm.inc
  93. .. include-build-file:: inc/tusb_console.inc
  94. .. include-build-file:: inc/tusb_tasks.inc
  95. .. include-build-file:: inc/vfs_tinyusb.inc