usb_device.rst 5.6 KB

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