|
| 1 | +####### |
| 2 | +USB API |
| 3 | +####### |
| 4 | + |
| 5 | +.. note:: This feature is only supported on ESP chips that have USB peripheral, like the ESP32-S2 and ESP32-S3. Some chips, like the ESP32-C3 include native CDC+JTAG peripheral that is not covered here. |
| 6 | + |
| 7 | +About |
| 8 | +----- |
| 9 | + |
| 10 | +The **Universal Serial Bus** is a widely used peripheral to exchange data between devices. USB was introduced on the ESP32, supporting both device and host mode. |
| 11 | + |
| 12 | +To learn about the USB, see the `USB.org`_ for developers. |
| 13 | + |
| 14 | +USB as Device |
| 15 | +************* |
| 16 | + |
| 17 | +In the device mode, the ESP32 acts as an USB device, like a mouse or keyboard to be connected to a host device, like your computer or smartphone. |
| 18 | + |
| 19 | +USB as Host |
| 20 | +*********** |
| 21 | + |
| 22 | +The USB host mode, you can connect devices on the ESP32, like external modems, mouse and keyboards. |
| 23 | + |
| 24 | +.. note:: This mode is still under development for the ESP32. |
| 25 | + |
| 26 | +API Description |
| 27 | +--------------- |
| 28 | + |
| 29 | +This is the common USB API description. |
| 30 | + |
| 31 | +For more supported USB classes implementation, see the following sections: |
| 32 | + |
| 33 | +.. toctree:: |
| 34 | + :maxdepth: 1 |
| 35 | + :caption: Classes: |
| 36 | + |
| 37 | + USB CDC <usb_cdc> |
| 38 | + USB MSC <usb_msc> |
| 39 | + |
| 40 | +USB Common |
| 41 | +********** |
| 42 | + |
| 43 | +These are the common APIs for the USB driver. |
| 44 | + |
| 45 | +onEvent |
| 46 | +^^^^^^^ |
| 47 | + |
| 48 | +Event handling function to set the callback. |
| 49 | + |
| 50 | +.. code-block:: arduino |
| 51 | +
|
| 52 | + void onEvent(esp_event_handler_t callback); |
| 53 | +
|
| 54 | +Event handling function for the specific event. |
| 55 | + |
| 56 | +.. code-block:: arduino |
| 57 | +
|
| 58 | + void onEvent(arduino_usb_event_t event, esp_event_handler_t callback); |
| 59 | +
|
| 60 | +Where ``event`` can be: |
| 61 | + |
| 62 | +* ARDUINO_USB_ANY_EVENT |
| 63 | +* ARDUINO_USB_STARTED_EVENT |
| 64 | +* ARDUINO_USB_STOPPED_EVENT |
| 65 | +* ARDUINO_USB_SUSPEND_EVENT |
| 66 | +* ARDUINO_USB_RESUME_EVENT |
| 67 | +* ARDUINO_USB_MAX_EVENT |
| 68 | + |
| 69 | +VID |
| 70 | +^^^ |
| 71 | + |
| 72 | +Set the Vendor ID. This 16 bits identification is used to identify the company that develops the product. |
| 73 | + |
| 74 | +.. note:: You can't define your own VID. If you need your own VID, you need to buy one. See https://www.usb.org/getting-vendor-id for more details. |
| 75 | + |
| 76 | +.. code-block:: arduino |
| 77 | +
|
| 78 | + bool VID(uint16_t v); |
| 79 | +
|
| 80 | +
|
| 81 | +Get the Vendor ID. |
| 82 | + |
| 83 | +.. code-block:: arduino |
| 84 | +
|
| 85 | + uint16_t VID(void); |
| 86 | +
|
| 87 | +Returns the Vendor ID. The default value for the VID is: ``0x303A``. |
| 88 | + |
| 89 | +PID |
| 90 | +^^^ |
| 91 | + |
| 92 | +Set the Product ID. This 16 bits identification is used to identify the product. |
| 93 | + |
| 94 | +.. code-block:: arduino |
| 95 | +
|
| 96 | + bool PID(uint16_t p); |
| 97 | +
|
| 98 | +Get the Product ID. |
| 99 | + |
| 100 | +.. code-block:: arduino |
| 101 | +
|
| 102 | + uint16_t PID(void); |
| 103 | +
|
| 104 | +Returns the Product ID. The default PID is: ``0x0002``. |
| 105 | + |
| 106 | +firmwareVersion |
| 107 | +^^^^^^^^^^^^^^^ |
| 108 | + |
| 109 | +Set the firmware version. This is a 16 bits unsigned value. |
| 110 | + |
| 111 | +.. code-block:: arduino |
| 112 | + |
| 113 | + bool firmwareVersion(uint16_t version); |
| 114 | +
|
| 115 | +Get the firmware version. |
| 116 | + |
| 117 | +.. code-block:: arduino |
| 118 | +
|
| 119 | + uint16_t firmwareVersion(void); |
| 120 | +
|
| 121 | +Return the 16 bits unsigned value. The default value is: ``0x100``. |
| 122 | + |
| 123 | +usbVersion |
| 124 | +^^^^^^^^^^ |
| 125 | + |
| 126 | +Set the USB version. |
| 127 | + |
| 128 | +.. code-block:: arduino |
| 129 | +
|
| 130 | + bool usbVersion(uint16_t version); |
| 131 | +
|
| 132 | +Get the USB version. |
| 133 | + |
| 134 | +.. code-block:: arduino |
| 135 | +
|
| 136 | + uint16_t usbVersion(void); |
| 137 | +
|
| 138 | +Return the USB version. The default value is: ``0x200`` (USB 2.0). |
| 139 | + |
| 140 | +usbPower |
| 141 | +^^^^^^^^ |
| 142 | + |
| 143 | +Set the USB power as mA (current). |
| 144 | + |
| 145 | +.. note:: This configuration does not change the physical power output. This is only used for the USB device information. |
| 146 | + |
| 147 | +.. code-block:: arduino |
| 148 | +
|
| 149 | + bool usbPower(uint16_t mA); |
| 150 | +
|
| 151 | +Get the USB power configuration. |
| 152 | + |
| 153 | +.. code-block:: arduino |
| 154 | +
|
| 155 | + uint16_t usbPower(void); |
| 156 | +
|
| 157 | +Return the current in mA. The default value is: ``0x500`` (500mA). |
| 158 | + |
| 159 | +usbClass |
| 160 | +^^^^^^^^ |
| 161 | + |
| 162 | +Set the USB class. |
| 163 | + |
| 164 | +.. code-block:: arduino |
| 165 | +
|
| 166 | + bool usbClass(uint8_t _class); |
| 167 | +
|
| 168 | +Get the USB class. |
| 169 | + |
| 170 | +.. code-block:: arduino |
| 171 | +
|
| 172 | + uint8_t usbClass(void); |
| 173 | +
|
| 174 | +Return the USB class. The default value is: ``TUSB_CLASS_MISC``. |
| 175 | + |
| 176 | +usbSubClass |
| 177 | +^^^^^^^^^^^ |
| 178 | + |
| 179 | +Set the USB sub-class. |
| 180 | + |
| 181 | +.. code-block:: arduino |
| 182 | +
|
| 183 | + bool usbSubClass(uint8_t subClass); |
| 184 | +
|
| 185 | +Get the USB sub-class. |
| 186 | + |
| 187 | +.. code-block:: arduino |
| 188 | +
|
| 189 | + uint8_t usbSubClass(void); |
| 190 | +
|
| 191 | +Return the USB sub-class. The default value is: ``MISC_SUBCLASS_COMMON``. |
| 192 | + |
| 193 | +usbProtocol |
| 194 | +^^^^^^^^^^^ |
| 195 | + |
| 196 | +Define the USB protocol. |
| 197 | + |
| 198 | +.. code-block:: arduino |
| 199 | +
|
| 200 | + bool usbProtocol(uint8_t protocol); |
| 201 | +
|
| 202 | +Get the USB protocol. |
| 203 | + |
| 204 | +.. code-block:: arduino |
| 205 | +
|
| 206 | + uint8_t usbProtocol(void); |
| 207 | +
|
| 208 | +Return the USB protocol. The default value is: ``MISC_PROTOCOL_IAD`` |
| 209 | + |
| 210 | +usbAttributes |
| 211 | +^^^^^^^^^^^^^ |
| 212 | + |
| 213 | +Set the USB attributes. |
| 214 | + |
| 215 | +.. code-block:: arduino |
| 216 | +
|
| 217 | + bool usbAttributes(uint8_t attr); |
| 218 | +
|
| 219 | +Get the USB attributes. |
| 220 | + |
| 221 | +.. code-block:: arduino |
| 222 | + |
| 223 | + uint8_t usbAttributes(void); |
| 224 | +
|
| 225 | +Return the USB attributes. The default value is: ``TUSB_DESC_CONFIG_ATT_SELF_POWERED`` |
| 226 | + |
| 227 | +webUSB |
| 228 | +^^^^^^ |
| 229 | + |
| 230 | +This function is used to enable the ``webUSB`` functionality. |
| 231 | + |
| 232 | +.. code-block:: arduino |
| 233 | +
|
| 234 | + bool webUSB(bool enabled); |
| 235 | +
|
| 236 | +This function is used to get the ``webUSB`` setting. |
| 237 | + |
| 238 | +.. code-block:: arduino |
| 239 | + |
| 240 | + bool webUSB(void); |
| 241 | +
|
| 242 | +Return the ``webUSB`` setting (`Enabled` or `Disabled`) |
| 243 | + |
| 244 | +productName |
| 245 | +^^^^^^^^^^^ |
| 246 | + |
| 247 | +This function is used to define the product name. |
| 248 | + |
| 249 | +.. code-block:: arduino |
| 250 | +
|
| 251 | + bool productName(const char * name); |
| 252 | +
|
| 253 | +This function is used to get the product's name. |
| 254 | + |
| 255 | +.. code-block:: arduino |
| 256 | +
|
| 257 | + const char * productName(void); |
| 258 | +
|
| 259 | +manufacturerName |
| 260 | +^^^^^^^^^^^^^^^^ |
| 261 | + |
| 262 | +This function is used to define the manufacturer name. |
| 263 | + |
| 264 | +.. code-block:: arduino |
| 265 | +
|
| 266 | + bool manufacturerName(const char * name); |
| 267 | +
|
| 268 | +This function is used to get the manufacturer's name. |
| 269 | + |
| 270 | +.. code-block:: arduino |
| 271 | + |
| 272 | + const char * manufacturerName(void); |
| 273 | +
|
| 274 | +serialNumber |
| 275 | +^^^^^^^^^^^^ |
| 276 | + |
| 277 | +This function is used to define the serial number. |
| 278 | + |
| 279 | +.. code-block:: arduino |
| 280 | +
|
| 281 | + bool serialNumber(const char * name); |
| 282 | +
|
| 283 | +This function is used to get the serial number. |
| 284 | + |
| 285 | +.. code-block:: arduino |
| 286 | +
|
| 287 | + const char * serialNumber(void); |
| 288 | +
|
| 289 | +The default serial number is: ``0``. |
| 290 | + |
| 291 | +webUSBURL |
| 292 | +^^^^^^^^^ |
| 293 | + |
| 294 | +This function is used to define the ``webUSBURL``. |
| 295 | + |
| 296 | +.. code-block:: arduino |
| 297 | +
|
| 298 | + bool webUSBURL(const char * name); |
| 299 | +
|
| 300 | +This function is used to get the ``webUSBURL``. |
| 301 | + |
| 302 | +.. code-block:: arduino |
| 303 | +
|
| 304 | + const char * webUSBURL(void); |
| 305 | +
|
| 306 | +The default ``webUSBURL`` is: https://espressif.github.io/arduino-esp32/webusb.html |
| 307 | + |
| 308 | +enableDFU |
| 309 | +^^^^^^^^^ |
| 310 | + |
| 311 | +This function is used to enable the DFU capability. |
| 312 | + |
| 313 | +.. code-block:: arduino |
| 314 | +
|
| 315 | + bool enableDFU(); |
| 316 | +
|
| 317 | +begin |
| 318 | +^^^^^ |
| 319 | + |
| 320 | +This function is used to start the peripheral using the default configuration. |
| 321 | + |
| 322 | +.. code-block:: arduino |
| 323 | +
|
| 324 | + bool begin(); |
| 325 | +
|
| 326 | +Example Code |
| 327 | +------------ |
| 328 | + |
| 329 | +There are a collection of USB device examples on the project GitHub, including Firmware MSC update, USB CDC, HID and composite device. |
| 330 | + |
| 331 | +.. _USB.org: https://www.usb.org/developers |
0 commit comments