usb.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. // Copyright 2015-2020 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /*
  15. Note: This header file contains the types and macros belong/relate to the USB2.0 protocol and are HW implementation
  16. agnostic. In other words, this header is only meant to be used in the HCD layer and above of the USB Host stack. For
  17. types and macros that are HW implementation specific (i.e., HAL layer and below), add them to the
  18. "hal/usb_types_private.h" header instead.
  19. */
  20. #pragma once
  21. #include <stdint.h>
  22. #ifdef __cplusplus
  23. extern "C"
  24. {
  25. #endif
  26. #define USB_CTRL_REQ_ATTR __attribute__((packed))
  27. #define USB_DESC_ATTR __attribute__((packed))
  28. // ------------------------------------------------ Common USB Types ---------------------------------------------------
  29. // --------------------- Bus Related -----------------------
  30. /**
  31. * @brief USB Standard Speeds
  32. */
  33. typedef enum {
  34. USB_SPEED_LOW = 0, /**< USB Low Speed (1.5 Mbit/s) */
  35. USB_SPEED_FULL, /**< USB Full Speed (12 Mbit/s) */
  36. } usb_speed_t;
  37. // ------------------ Transfer Related ---------------------
  38. /**
  39. * @brief The type of USB transfer
  40. *
  41. * @note The enum values need to match the bmAttributes field of an EP descriptor
  42. */
  43. typedef enum {
  44. USB_TRANSFER_TYPE_CTRL = 0,
  45. USB_TRANSFER_TYPE_ISOCHRONOUS,
  46. USB_TRANSFER_TYPE_BULK,
  47. USB_TRANSFER_TYPE_INTR,
  48. } usb_transfer_type_t;
  49. /**
  50. * @brief The status of a particular transfer
  51. */
  52. typedef enum {
  53. USB_TRANSFER_STATUS_COMPLETED, /**< The transfer was successful (but may be short) */
  54. USB_TRANSFER_STATUS_ERROR, /**< The transfer failed because due to excessive errors (e.g. no response or CRC error) */
  55. USB_TRANSFER_STATUS_TIMED_OUT, /**< The transfer failed due to a time out */
  56. USB_TRANSFER_STATUS_CANCELED, /**< The transfer was canceled */
  57. USB_TRANSFER_STATUS_STALL, /**< The transfer was stalled */
  58. USB_TRANSFER_STATUS_NO_DEVICE, /**< The transfer failed because the device is no longer valid (e.g., disconnected */
  59. USB_TRANSFER_STATUS_OVERFLOW, /**< The transfer as more data was sent than was requested */
  60. USB_TRANSFER_STATUS_SKIPPED, /**< ISOC packets only. The packet was skipped due to system latency or bus overload */
  61. } usb_transfer_status_t;
  62. #define USB_TRANSFER_FLAG_ZERO_PACK 0x01 /**< (For bulk OUT only). Indicates that a bulk OUT transfers should always terminate with a short packet, even if it means adding an extra zero length packet */
  63. /**
  64. * @brief Isochronous packet descriptor
  65. *
  66. * If the number of bytes in an Isochronous transfer is larger than the MPS of the endpoint, the transfer is split
  67. * into multiple packets transmitted at the endpoint's specified interval. An array of Isochronous packet descriptors
  68. * describes how an Isochronous transfer should be split into multiple packets.
  69. */
  70. typedef struct {
  71. int num_bytes; /**< Number of bytes to transmit/receive in the packet */
  72. int actual_num_bytes; /**< Actual number of bytes transmitted/received in the packet */
  73. usb_transfer_status_t status; /**< Status of the packet */
  74. } usb_isoc_packet_desc_t;
  75. /**
  76. * @brief USB transfer structure
  77. *
  78. * This structure is used to represent a transfer from a software client to an endopint over the USB bus. Some of the
  79. * fields are made const on purpose as they are fixed on allocation. Users should call the appropriate USB Host Driver
  80. * function to allocate a USB transfer structure instead of allocating this structure themselves.
  81. *
  82. * The transfer type is inferred from the endpoint this transfer is sent to. Depending on the transfer type, users
  83. * should note the following:
  84. *
  85. * - Bulk: This structure represents a single bulk transfer. If the number of bytes exceeds the endpoint's MPS, the
  86. * transfer will be split into multiple MPS sized packets followed by a short packet.
  87. * - Control: This structure represents a single control transfer. This first 8 bytes of the data_buffer must be filled
  88. * with the setup packet. The num_bytes field should exclude the size of the setup packet (i.e., set to 0 if
  89. * the control transfer has no data stage).
  90. * - Interrupt: Represents an interrupt transfer. If num_bytes exceeds the MPS of the endpoint, the transfer will be
  91. * split into multiple packets, and each packet is transferred at the endpoint's specified interval.
  92. * - Isochronous: Represents a stream of bytes that should be transferred to an endpoint at a fixed rate. The transfer
  93. * is split into packets according to the each isoc_packet_desc. A packet is transferred at each interval
  94. * of the endpoint.
  95. *
  96. * @note For Bulk/Control/Interrupt IN transfers, the num_bytes must be a integer multiple of the endpoint's MPS
  97. * @note This structure should be allocated via __insert_func_name__()
  98. * @note Once the transfer has be submitted, users should not modify the structure until the transfer has completed
  99. */
  100. typedef struct usb_transfer_obj usb_transfer_t;
  101. /**
  102. * @brief USB transfer completion callback
  103. */
  104. typedef void (*usb_transfer_cb_t)(usb_transfer_t *transfer);
  105. struct usb_transfer_obj{
  106. uint8_t *const data_buffer; /**< Pointer to data buffer */
  107. const size_t data_buffer_size; /**< Size of the data buffer in bytes */
  108. int num_bytes; /**< Number of bytes to transfer. Control transfers should exclude size of setup packet. IN transfers should be integer multiple of MPS */
  109. int actual_num_bytes; /**< Actual number of bytes transferred */
  110. uint32_t flags; /**< Transfer flags */
  111. usb_transfer_status_t status; /**< Status of the transfer */
  112. uint32_t timeout; /**< Timeout (in milliseconds) of the packet (currently not supported yet) */
  113. usb_transfer_cb_t callback; /**< Transfer callback */
  114. void *context; /**< Context variable for transfer to associate transfer with something */
  115. const int num_isoc_packets; /**< Only relevant to Isochronous. Number of service periods (i.e., intervals) to transfer data buffer over. */
  116. usb_isoc_packet_desc_t isoc_packet_desc[0]; /**< Descriptors for each Isochronous packet */
  117. };
  118. // ---------------------------------------------------- Chapter 9 ------------------------------------------------------
  119. /**
  120. * @brief Descriptor types from USB2.0 specification table 9.5
  121. */
  122. #define USB_B_DESCRIPTOR_TYPE_DEVICE 0x01
  123. #define USB_B_DESCRIPTOR_TYPE_CONFIGURATION 0x02
  124. #define USB_B_DESCRIPTOR_TYPE_STRING 0x03
  125. #define USB_B_DESCRIPTOR_TYPE_INTERFACE 0x04
  126. #define USB_B_DESCRIPTOR_TYPE_ENDPOINT 0x05
  127. #define USB_B_DESCRIPTOR_TYPE_DEVICE_QUALIFIER 0x06
  128. #define USB_B_DESCRIPTOR_TYPE_OTHER_SPEED_CONFIGURATION 0x07
  129. #define USB_B_DESCRIPTOR_TYPE_INTERFACE_POWER 0x08
  130. /**
  131. * @brief Descriptor types from USB 2.0 ECN
  132. */
  133. #define USB_B_DESCRIPTOR_TYPE_OTG 0x09
  134. #define USB_B_DESCRIPTOR_TYPE_DEBUG 0x0a
  135. #define USB_B_DESCRIPTOR_TYPE_INTERFACE_ASSOCIATION 0x0b
  136. /**
  137. * @brief Descriptor types from Wireless USB spec
  138. */
  139. #define USB_B_DESCRIPTOR_TYPE_SECURITY 0x0c
  140. #define USB_B_DESCRIPTOR_TYPE_KEY 0x0d
  141. #define USB_B_DESCRIPTOR_TYPE_ENCRYPTION_TYPE 0x0e
  142. #define USB_B_DESCRIPTOR_TYPE_BOS 0x0f
  143. #define USB_B_DESCRIPTOR_TYPE_DEVICE_CAPABILITY 0x10
  144. #define USB_B_DESCRIPTOR_TYPE_WIRELESS_ENDPOINT_COMP 0x11
  145. #define USB_B_DESCRIPTOR_TYPE_WIRE_ADAPTER 0x21
  146. #define USB_B_DESCRIPTOR_TYPE_RPIPE 0x22
  147. #define USB_B_DESCRIPTOR_TYPE_CS_RADIO_CONTROL 0x23
  148. /**
  149. * @brief Descriptor types from UAS specification
  150. */
  151. #define USB_B_DESCRIPTOR_TYPE_PIPE_USAGE 0x24
  152. // ------------------- Control Request ---------------------
  153. /**
  154. * @brief Size of a USB control transfer setup packet in bytes
  155. */
  156. #define USB_CTRL_REQ_SIZE 8
  157. /**
  158. * @brief Structure representing a USB control transfer setup packet
  159. */
  160. typedef union {
  161. struct {
  162. uint8_t bRequestType;
  163. uint8_t bRequest;
  164. uint16_t wValue;
  165. uint16_t wIndex;
  166. uint16_t wLength;
  167. } USB_CTRL_REQ_ATTR;
  168. uint8_t val[USB_CTRL_REQ_SIZE];
  169. } usb_ctrl_req_t;
  170. _Static_assert(sizeof(usb_ctrl_req_t) == USB_CTRL_REQ_SIZE, "Size of usb_ctrl_req_t incorrect");
  171. /**
  172. * @brief Bit masks belonging to the bRequestType field of a setup packet
  173. */
  174. #define USB_B_REQUEST_TYPE_DIR_OUT (0X00 << 7)
  175. #define USB_B_REQUEST_TYPE_DIR_IN (0x01 << 7)
  176. #define USB_B_REQUEST_TYPE_TYPE_STANDARD (0x00 << 5)
  177. #define USB_B_REQUEST_TYPE_TYPE_CLASS (0x01 << 5)
  178. #define USB_B_REQUEST_TYPE_TYPE_VENDOR (0x02 << 5)
  179. #define USB_B_REQUEST_TYPE_TYPE_RESERVED (0x03 << 5)
  180. #define USB_B_REQUEST_TYPE_TYPE_MASK (0x03 << 5)
  181. #define USB_B_REQUEST_TYPE_RECIP_DEVICE (0x00 << 0)
  182. #define USB_B_REQUEST_TYPE_RECIP_INTERFACE (0x01 << 0)
  183. #define USB_B_REQUEST_TYPE_RECIP_ENDPOINT (0x02 << 0)
  184. #define USB_B_REQUEST_TYPE_RECIP_OTHER (0x03 << 0)
  185. #define USB_B_REQUEST_TYPE_RECIP_MASK (0x1f << 0)
  186. /**
  187. * @brief Bit masks belonging to the bRequest field of a setup packet
  188. */
  189. #define USB_B_REQUEST_GET_STATUS 0x00
  190. #define USB_B_REQUEST_CLEAR_FEATURE 0x01
  191. #define USB_B_REQUEST_SET_FEATURE 0x03
  192. #define USB_B_REQUEST_SET_ADDRESS 0x05
  193. #define USB_B_REQUEST_GET_DESCRIPTOR 0x06
  194. #define USB_B_REQUEST_SET_DESCRIPTOR 0x07
  195. #define USB_B_REQUEST_GET_CONFIGURATION 0x08
  196. #define USB_B_REQUEST_SET_CONFIGURATION 0x09
  197. #define USB_B_REQUEST_GET_INTERFACE 0x0A
  198. #define USB_B_REQUEST_SET_INTERFACE 0x0B
  199. #define USB_B_REQUEST_SYNCH_FRAME 0x0C
  200. /**
  201. * @brief Bit masks belonging to the wValue field of a setup packet
  202. */
  203. #define USB_W_VALUE_DT_DEVICE 0x01
  204. #define USB_W_VALUE_DT_CONFIG 0x02
  205. #define USB_W_VALUE_DT_STRING 0x03
  206. #define USB_W_VALUE_DT_INTERFACE 0x04
  207. #define USB_W_VALUE_DT_ENDPOINT 0x05
  208. #define USB_W_VALUE_DT_DEVICE_QUALIFIER 0x06
  209. #define USB_W_VALUE_DT_OTHER_SPEED_CONFIG 0x07
  210. #define USB_W_VALUE_DT_INTERFACE_POWER 0x08
  211. /**
  212. * @brief Initializer for a SET_ADDRESS request
  213. *
  214. * Sets the address of a connected device
  215. */
  216. #define USB_CTRL_REQ_INIT_SET_ADDR(ctrl_req_ptr, addr) ({ \
  217. (ctrl_req_ptr)->bRequestType = USB_B_REQUEST_TYPE_DIR_OUT | USB_B_REQUEST_TYPE_TYPE_STANDARD |USB_B_REQUEST_TYPE_RECIP_DEVICE; \
  218. (ctrl_req_ptr)->bRequest = USB_B_REQUEST_SET_ADDRESS; \
  219. (ctrl_req_ptr)->wValue = (addr); \
  220. (ctrl_req_ptr)->wIndex = 0; \
  221. (ctrl_req_ptr)->wLength = 0; \
  222. })
  223. /**
  224. * @brief Initializer for a request to get a device's device descriptor
  225. */
  226. #define USB_CTRL_REQ_INIT_GET_DEVICE_DESC(ctrl_req_ptr) ({ \
  227. (ctrl_req_ptr)->bRequestType = USB_B_REQUEST_TYPE_DIR_IN | USB_B_REQUEST_TYPE_TYPE_STANDARD | USB_B_REQUEST_TYPE_RECIP_DEVICE; \
  228. (ctrl_req_ptr)->bRequest = USB_B_REQUEST_GET_DESCRIPTOR; \
  229. (ctrl_req_ptr)->wValue = (USB_W_VALUE_DT_DEVICE << 8); \
  230. (ctrl_req_ptr)->wIndex = 0; \
  231. (ctrl_req_ptr)->wLength = 18; \
  232. })
  233. /**
  234. * @brief Initializer for a request to get a device's current configuration number
  235. */
  236. #define USB_CTRL_REQ_INIT_GET_CONFIG(ctrl_req_ptr) ({ \
  237. (ctrl_req_ptr)->bRequestType = USB_B_REQUEST_TYPE_DIR_IN | USB_B_REQUEST_TYPE_TYPE_STANDARD | USB_B_REQUEST_TYPE_RECIP_DEVICE; \
  238. (ctrl_req_ptr)->bRequest = USB_B_REQUEST_GET_CONFIGURATION; \
  239. (ctrl_req_ptr)->wValue = 0; \
  240. (ctrl_req_ptr)->wIndex = 0; \
  241. (ctrl_req_ptr)->wLength = 1; \
  242. })
  243. /**
  244. * @brief Initializer for a request to get one of the device's current configuration descriptor
  245. *
  246. * - desc_index indicates the configuration's index number
  247. * - Number of bytes of the configuration descriptor to get
  248. */
  249. #define USB_CTRL_REQ_INIT_GET_CONFIG_DESC(ctrl_req_ptr, desc_index, desc_len) ({ \
  250. (ctrl_req_ptr)->bRequestType = USB_B_REQUEST_TYPE_DIR_IN | USB_B_REQUEST_TYPE_TYPE_STANDARD | USB_B_REQUEST_TYPE_RECIP_DEVICE; \
  251. (ctrl_req_ptr)->bRequest = USB_B_REQUEST_GET_DESCRIPTOR; \
  252. (ctrl_req_ptr)->wValue = (USB_W_VALUE_DT_CONFIG << 8) | ((desc_index) & 0xFF); \
  253. (ctrl_req_ptr)->wIndex = 0; \
  254. (ctrl_req_ptr)->wLength = (desc_len); \
  255. })
  256. /**
  257. * @brief Initializer for a request to set a device's current configuration number
  258. */
  259. #define USB_CTRL_REQ_INIT_SET_CONFIG(ctrl_req_ptr, config_num) ({ \
  260. (ctrl_req_ptr)->bRequestType = USB_B_REQUEST_TYPE_DIR_OUT | USB_B_REQUEST_TYPE_TYPE_STANDARD | USB_B_REQUEST_TYPE_RECIP_DEVICE; \
  261. (ctrl_req_ptr)->bRequest = USB_B_REQUEST_SET_CONFIGURATION; \
  262. (ctrl_req_ptr)->wValue = (config_num); \
  263. (ctrl_req_ptr)->wIndex = 0; \
  264. (ctrl_req_ptr)->wLength = 0; \
  265. })
  266. /**
  267. * @brief Initializer for a request to set an interface's alternate setting
  268. */
  269. #define USB_CTRL_REQ_INIT_SET_INTERFACE(ctrl_req_ptr, intf_num, alt_setting_num) ({ \
  270. (ctrl_req_ptr)->bRequestType = USB_B_REQUEST_TYPE_DIR_OUT | USB_B_REQUEST_TYPE_TYPE_STANDARD | USB_B_REQUEST_TYPE_RECIP_INTERFACE; \
  271. (ctrl_req_ptr)->bRequest = USB_B_REQUEST_SET_INTERFACE; \
  272. (ctrl_req_ptr)->wValue = (alt_setting_num); \
  273. (ctrl_req_ptr)->wIndex = (intf_num); \
  274. (ctrl_req_ptr)->wLength = 0; \
  275. })
  276. // ---------------- Standard Descriptor --------------------
  277. /**
  278. * @brief Size of dummy USB standard descriptor
  279. */
  280. #define USB_DESC_STANDARD_SIZE 2
  281. /**
  282. * @brief Dummy USB standard descriptor
  283. *
  284. * All USB standard descriptors start with these two bytes. Use this type traversing over descriptors
  285. */
  286. typedef union {
  287. struct {
  288. uint8_t bLength;
  289. uint8_t bDescriptorType;
  290. } USB_DESC_ATTR;
  291. uint8_t val[USB_DESC_STANDARD_SIZE];
  292. } usb_desc_standard_t;
  293. _Static_assert(sizeof(usb_desc_standard_t) == USB_DESC_STANDARD_SIZE, "Size of usb_desc_standard_t incorrect");
  294. // ------------------ Device Descriptor --------------------
  295. /**
  296. * @brief Size of a USB device descriptor in bytes
  297. */
  298. #define USB_DESC_DEVICE_SIZE 18
  299. /**
  300. * @brief Structure representing a USB device descriptor
  301. */
  302. typedef union {
  303. struct {
  304. uint8_t bLength;
  305. uint8_t bDescriptorType;
  306. uint16_t bcdUSB;
  307. uint8_t bDeviceClass;
  308. uint8_t bDeviceSubClass;
  309. uint8_t bDeviceProtocol;
  310. uint8_t bMaxPacketSize0;
  311. uint16_t idVendor;
  312. uint16_t idProduct;
  313. uint16_t bcdDevice;
  314. uint8_t iManufacturer;
  315. uint8_t iProduct;
  316. uint8_t iSerialNumber;
  317. uint8_t bNumConfigurations;
  318. } USB_DESC_ATTR;
  319. uint8_t val[USB_DESC_DEVICE_SIZE];
  320. } usb_desc_device_t;
  321. _Static_assert(sizeof(usb_desc_device_t) == USB_DESC_DEVICE_SIZE, "Size of usb_desc_device_t incorrect");
  322. /**
  323. * @brief Possible base class values of the bDeviceClass field of a USB device descriptor
  324. */
  325. #define USB_CLASS_PER_INTERFACE 0x00
  326. #define USB_CLASS_AUDIO 0x01
  327. #define USB_CLASS_COMM 0x02
  328. #define USB_CLASS_HID 0x03
  329. #define USB_CLASS_PHYSICAL 0x05
  330. #define USB_CLASS_STILL_IMAGE 0x06
  331. #define USB_CLASS_PRINTER 0x07
  332. #define USB_CLASS_MASS_STORAGE 0x08
  333. #define USB_CLASS_HUB 0x09
  334. #define USB_CLASS_CDC_DATA 0x0a
  335. #define USB_CLASS_CSCID 0x0b
  336. #define USB_CLASS_CONTENT_SEC 0x0d
  337. #define USB_CLASS_VIDEO 0x0e
  338. #define USB_CLASS_WIRELESS_CONTROLLER 0xe0
  339. #define USB_CLASS_PERSONAL_HEALTHCARE 0x0f
  340. #define USB_CLASS_AUDIO_VIDEO 0x10
  341. #define USB_CLASS_BILLBOARD 0x11
  342. #define USB_CLASS_USB_TYPE_C_BRIDGE 0x12
  343. #define USB_CLASS_MISC 0xef
  344. #define USB_CLASS_APP_SPEC 0xfe
  345. #define USB_CLASS_VENDOR_SPEC 0xff
  346. /**
  347. * @brief Vendor specific subclass code
  348. */
  349. #define USB_SUBCLASS_VENDOR_SPEC 0xff
  350. // -------------- Configuration Descriptor -----------------
  351. /**
  352. * @brief Size of a short USB configuration descriptor in bytes
  353. *
  354. * @note The size of a full USB configuration includes all the interface and endpoint
  355. * descriptors of that configuration.
  356. */
  357. #define USB_DESC_CONFIG_SIZE 9
  358. /**
  359. * @brief Structure representing a short USB configuration descriptor
  360. *
  361. * @note The full USB configuration includes all the interface and endpoint
  362. * descriptors of that configuration.
  363. */
  364. typedef union {
  365. struct {
  366. uint8_t bLength;
  367. uint8_t bDescriptorType;
  368. uint16_t wTotalLength;
  369. uint8_t bNumInterfaces;
  370. uint8_t bConfigurationValue;
  371. uint8_t iConfiguration;
  372. uint8_t bmAttributes;
  373. uint8_t bMaxPower;
  374. } USB_DESC_ATTR;
  375. uint8_t val[USB_DESC_CONFIG_SIZE];
  376. } usb_desc_config_t;
  377. _Static_assert(sizeof(usb_desc_config_t) == USB_DESC_CONFIG_SIZE, "Size of usb_desc_config_t incorrect");
  378. /**
  379. * @brief Bit masks belonging to the bmAttributes field of a configuration descriptor
  380. */
  381. #define USB_BM_ATTRIBUTES_ONE (1 << 7) //Must be set
  382. #define USB_BM_ATTRIBUTES_SELFPOWER (1 << 6) //Self powered
  383. #define USB_BM_ATTRIBUTES_WAKEUP (1 << 5) //Can wake-up
  384. #define USB_BM_ATTRIBUTES_BATTERY (1 << 4) //Battery powered
  385. // ---------- Interface Association Descriptor -------------
  386. /**
  387. * @brief Size of a USB interface association descriptor in bytes
  388. */
  389. #define USB_DESC_INTF_ASSOC_SIZE 9
  390. /**
  391. * @brief Structure representing a USB interface association descriptor
  392. */
  393. typedef union {
  394. struct {
  395. uint8_t bLength;
  396. uint8_t bDescriptorType;
  397. uint8_t bFirstInterface;
  398. uint8_t bInterfaceCount;
  399. uint8_t bFunctionClass;
  400. uint8_t bFunctionSubClass;
  401. uint8_t bFunctionProtocol;
  402. uint8_t iFunction;
  403. } USB_DESC_ATTR;
  404. uint8_t val[USB_DESC_INTF_ASSOC_SIZE];
  405. } usb_desc_iad_t;
  406. _Static_assert(sizeof(usb_desc_iad_t) == USB_DESC_INTF_ASSOC_SIZE, "Size of usb_desc_iad_t incorrect");
  407. // ---------------- Interface Descriptor -------------------
  408. /**
  409. * @brief Size of a USB interface descriptor in bytes
  410. */
  411. #define USB_DESC_INTF_SIZE 9
  412. /**
  413. * @brief Structure representing a USB interface descriptor
  414. */
  415. typedef union {
  416. struct {
  417. uint8_t bLength;
  418. uint8_t bDescriptorType;
  419. uint8_t bInterfaceNumber;
  420. uint8_t bAlternateSetting;
  421. uint8_t bNumEndpoints;
  422. uint8_t bInterfaceClass;
  423. uint8_t bInterfaceSubClass;
  424. uint8_t bInterfaceProtocol;
  425. uint8_t iInterface;
  426. } USB_DESC_ATTR;
  427. uint8_t val[USB_DESC_INTF_SIZE];
  428. } usb_desc_intf_t;
  429. _Static_assert(sizeof(usb_desc_intf_t) == USB_DESC_INTF_SIZE, "Size of usb_desc_intf_t incorrect");
  430. // ----------------- Endpoint Descriptor -------------------
  431. /**
  432. * @brief Size of a USB endpoint descriptor in bytes
  433. */
  434. #define USB_DESC_EP_SIZE 7
  435. /**
  436. * @brief Structure representing a USB endpoint descriptor
  437. */
  438. typedef union {
  439. struct {
  440. uint8_t bLength;
  441. uint8_t bDescriptorType;
  442. uint8_t bEndpointAddress;
  443. uint8_t bmAttributes;
  444. uint16_t wMaxPacketSize;
  445. uint8_t bInterval;
  446. } USB_DESC_ATTR;
  447. uint8_t val[USB_DESC_EP_SIZE];
  448. } usb_desc_ep_t;
  449. _Static_assert(sizeof(usb_desc_ep_t) == USB_DESC_EP_SIZE, "Size of usb_desc_ep_t incorrect");
  450. /**
  451. * @brief Bit masks belonging to the bEndpointAddress field of an endpoint descriptor
  452. */
  453. #define USB_B_ENDPOINT_ADDRESS_EP_NUM_MASK 0x0f
  454. #define USB_B_ENDPOINT_ADDRESS_EP_DIR_MASK 0x80
  455. /**
  456. * @brief Bit masks belonging to the bmAttributes field of an endpoint descriptor
  457. */
  458. #define USB_BM_ATTRIBUTES_XFERTYPE_MASK 0x03
  459. #define USB_BM_ATTRIBUTES_XFER_CONTROL (0 << 0)
  460. #define USB_BM_ATTRIBUTES_XFER_ISOC (1 << 0)
  461. #define USB_BM_ATTRIBUTES_XFER_BULK (2 << 0)
  462. #define USB_BM_ATTRIBUTES_XFER_INT (3 << 0)
  463. #define USB_BM_ATTRIBUTES_SYNCTYPE_MASK 0x0C /* in bmAttributes */
  464. #define USB_BM_ATTRIBUTES_SYNC_NONE (0 << 2)
  465. #define USB_BM_ATTRIBUTES_SYNC_ASYNC (1 << 2)
  466. #define USB_BM_ATTRIBUTES_SYNC_ADAPTIVE (2 << 2)
  467. #define USB_BM_ATTRIBUTES_SYNC_SYNC (3 << 2)
  468. #define USB_BM_ATTRIBUTES_USAGETYPE_MASK 0x30
  469. #define USB_BM_ATTRIBUTES_USAGE_DATA (0 << 4)
  470. #define USB_BM_ATTRIBUTES_USAGE_FEEDBACK (1 << 4)
  471. #define USB_BM_ATTRIBUTES_USAGE_IMPLICIT_FB (2 << 4)
  472. /**
  473. * @brief Macro helpers to get information about an endpoint from its descriptor
  474. */
  475. #define USB_DESC_EP_GET_XFERTYPE(desc_ptr) ((usb_transfer_type_t) ((desc_ptr)->bmAttributes & USB_BM_ATTRIBUTES_XFERTYPE_MASK))
  476. #define USB_DESC_EP_GET_EP_NUM(desc_ptr) ((desc_ptr)->bEndpointAddress & USB_B_ENDPOINT_ADDRESS_EP_NUM_MASK)
  477. #define USB_DESC_EP_GET_EP_DIR(desc_ptr) (((desc_ptr)->bEndpointAddress & USB_B_ENDPOINT_ADDRESS_EP_DIR_MASK) ? 1 : 0)
  478. #define USB_DESC_EP_GET_MPS(desc_ptr) ((desc_ptr)->wMaxPacketSize & 0x7FF)
  479. // ------------------ String Descriptor --------------------
  480. /**
  481. * @brief Size of a short USB string descriptor in bytes
  482. */
  483. #define USB_DESC_STR_SIZE 4
  484. /**
  485. * @brief Structure representing a USB string descriptor
  486. */
  487. typedef union {
  488. struct {
  489. uint8_t bLength;
  490. uint8_t bDescriptorType;
  491. uint16_t wData[1]; /* UTF-16LE encoded */
  492. } USB_DESC_ATTR;
  493. uint8_t val[USB_DESC_STR_SIZE];
  494. } usb_desc_str_t;
  495. _Static_assert(sizeof(usb_desc_str_t) == USB_DESC_STR_SIZE, "Size of usb_desc_str_t incorrect");
  496. #ifdef __cplusplus
  497. }
  498. #endif