usb.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 "usb_types.h" header
  18. instead.
  19. */
  20. #pragma once
  21. #include <stdint.h>
  22. #ifdef __cplusplus
  23. extern "C"
  24. {
  25. #endif
  26. #include <stdint.h>
  27. #include <sys/queue.h>
  28. #define USB_CTRL_REQ_ATTR __attribute__((packed))
  29. #define USB_DESC_ATTR __attribute__((packed))
  30. // ------------------------------------------------ Common USB Types ---------------------------------------------------
  31. // --------------------- Bus Related -----------------------
  32. /**
  33. * @brief USB Standard Speeds
  34. */
  35. typedef enum {
  36. USB_SPEED_LOW = 0, /**< USB Low Speed (1.5 Mbit/s) */
  37. USB_SPEED_FULL, /**< USB Full Speed (12 Mbit/s) */
  38. } usb_speed_t;
  39. // ------------------ Transfer Related ---------------------
  40. /**
  41. * @brief The type of USB transfer
  42. *
  43. * @note The enum values need to match the bmAttributes field of an EP descriptor
  44. */
  45. typedef enum {
  46. USB_TRANSFER_TYPE_CTRL = 0,
  47. USB_TRANSFER_TYPE_ISOCHRONOUS,
  48. USB_TRANSFER_TYPE_BULK,
  49. USB_TRANSFER_TYPE_INTR,
  50. } usb_transfer_type_t;
  51. /**
  52. * @brief The status of a particular transfer
  53. */
  54. typedef enum {
  55. USB_TRANSFER_STATUS_COMPLETED, /**< The transfer was successful (but may be short) */
  56. USB_TRANSFER_STATUS_ERROR, /**< The transfer failed because due to excessive errors (e.g. no response or CRC error) */
  57. USB_TRANSFER_STATUS_TIMED_OUT, /**< The transfer failed due to a time out */
  58. USB_TRANSFER_STATUS_CANCELED, /**< The transfer was canceled */
  59. USB_TRANSFER_STATUS_STALL, /**< The transfer was stalled */
  60. USB_TRANSFER_STATUS_NO_DEVICE, /**< The transfer failed because the device is no longer valid (e.g., disconnected */
  61. USB_TRANSFER_STATUS_OVERFLOW, /**< The transfer as more data was sent than was requested */
  62. USB_TRANSFER_STATUS_SKIPPED, /**< ISOC only. The packet was skipped due to system latency */
  63. } usb_transfer_status_t;
  64. /**
  65. * @brief Isochronous packet descriptor
  66. *
  67. * If the number of bytes in an IRP (I/O Request Packet, see USB2.0 Spec) is
  68. * larger than the MPS of the endpoint, the IRP is split over multiple packets
  69. * (one packet per bInterval of the endpoint). An array of Isochronous packet
  70. * descriptors describes how an IRP should be split over multiple packets.
  71. */
  72. typedef struct {
  73. int length; /**< Number of bytes to transmit/receive in the packet */
  74. int actual_length; /**< Actual number of bytes transmitted/received in the packet */
  75. usb_transfer_status_t status; /**< Status of the packet */
  76. } usb_iso_packet_desc_t;
  77. #define USB_IRP_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 */
  78. /**
  79. * @brief USB IRP (I/O Request Packet). See USB2.0 Spec
  80. *
  81. * An IRP is used to represent data transfer request form a software client to and endpoint over the USB bus. The same
  82. * IRP object type is used at each layer of the USB stack. This minimizes copying/conversion across the different layers
  83. * of the stack as each layer will pass a pointer to this type of object.
  84. *
  85. * See 10.5.3.1 os USB2.0 specification
  86. * Bulk: Represents a single bulk transfer which a pipe will transparently split into multiple MPS transactions (until
  87. * the last)
  88. * Control: Represents a single control transfer with the setup packet at the first 8 bytes of the buffer.
  89. * Interrupt: Represents a single interrupt transaction
  90. * Isochronous: Represents a buffer of a stream of bytes which the pipe will transparently transfer the stream of bytes
  91. * one or more service periods
  92. *
  93. * @note The tailq_entry and reserved variables are used by the USB Host stack internally. Users should not modify those fields.
  94. * @note Once an IRP is submitted, users should not modify the IRP as the Host stack takes ownership of the IRP.
  95. */
  96. struct usb_irp_obj {
  97. //Internal members
  98. TAILQ_ENTRY(usb_irp_obj) tailq_entry; /**< TAILQ entry that allows this object to be added to linked lists. Users should NOT modify this field */
  99. void *reserved_ptr; /**< Reserved pointer variable for internal use in the stack. Users should set this to NULL on allocation and NOT modify this afterwards */
  100. uint32_t reserved_flags; /**< Reserved variable for flags used internally in the stack. Users should set this to 0 on allocation and NOT modify this afterwards */
  101. //Public members
  102. uint8_t *data_buffer; /**< Pointer to data buffer. Must be DMA capable memory */
  103. int num_bytes; /**< Number of bytes in IRP. Control should exclude size of setup. IN should be integer multiple of MPS */
  104. int actual_num_bytes; /**< Actual number of bytes transmitted/receives in the IRP */
  105. uint32_t flags; /**< IRP flags */
  106. usb_transfer_status_t status; /**< Status of the transfer */
  107. uint32_t timeout; /**< Timeout (in milliseconds) of the packet (currently not supported yet) */
  108. void *context; /**< Context variable used to associate the IRP object with another object */
  109. int num_iso_packets; /**< Only relevant to Isochronous. Number of service periods to transfer data buffer over. Set to 0 for non-iso transfers */
  110. usb_iso_packet_desc_t iso_packet_desc[0]; /**< Descriptors for each ISO packet */
  111. };
  112. typedef struct usb_irp_obj usb_irp_t;
  113. // ---------------------------------------------------- Chapter 9 ------------------------------------------------------
  114. #define USB_B_DESCRIPTOR_TYPE_DEVICE 1
  115. #define USB_B_DESCRIPTOR_TYPE_CONFIGURATION 2
  116. #define USB_B_DESCRIPTOR_TYPE_STRING 3
  117. #define USB_B_DESCRIPTOR_TYPE_INTERFACE 4
  118. #define USB_B_DESCRIPTOR_TYPE_ENDPOINT 5
  119. #define USB_B_DESCRIPTOR_TYPE_DEVICE_QUALIFIER 6
  120. #define USB_B_DESCRIPTOR_TYPE_OTHER_SPEED_CONFIGURATION 7
  121. #define USB_B_DESCRIPTOR_TYPE_INTERFACE_POWER 8
  122. // ------------------- Control Request ---------------------
  123. /**
  124. * @brief Size of a USB control transfer setup packet in bytes
  125. */
  126. #define USB_CTRL_REQ_SIZE 8
  127. /**
  128. * @brief Structure representing a USB control transfer setup packet
  129. */
  130. typedef union {
  131. struct {
  132. uint8_t bRequestType;
  133. uint8_t bRequest;
  134. uint16_t wValue;
  135. uint16_t wIndex;
  136. uint16_t wLength;
  137. } USB_CTRL_REQ_ATTR;
  138. uint8_t val[USB_CTRL_REQ_SIZE];
  139. } usb_ctrl_req_t;
  140. _Static_assert(sizeof(usb_ctrl_req_t) == USB_CTRL_REQ_SIZE, "Size of usb_ctrl_req_t incorrect");
  141. /**
  142. * @brief Bit masks belonging to the bRequestType field of a setup packet
  143. */
  144. #define USB_B_REQUEST_TYPE_DIR_OUT (0X00 << 7)
  145. #define USB_B_REQUEST_TYPE_DIR_IN (0x01 << 7)
  146. #define USB_B_REQUEST_TYPE_TYPE_STANDARD (0x00 << 5)
  147. #define USB_B_REQUEST_TYPE_TYPE_CLASS (0x01 << 5)
  148. #define USB_B_REQUEST_TYPE_TYPE_VENDOR (0x02 << 5)
  149. #define USB_B_REQUEST_TYPE_TYPE_RESERVED (0x03 << 5)
  150. #define USB_B_REQUEST_TYPE_TYPE_MASK (0x03 << 5)
  151. #define USB_B_REQUEST_TYPE_RECIP_DEVICE (0x00 << 0)
  152. #define USB_B_REQUEST_TYPE_RECIP_INTERFACE (0x01 << 0)
  153. #define USB_B_REQUEST_TYPE_RECIP_ENDPOINT (0x02 << 0)
  154. #define USB_B_REQUEST_TYPE_RECIP_OTHER (0x03 << 0)
  155. #define USB_B_REQUEST_TYPE_RECIP_MASK (0x1f << 0)
  156. /**
  157. * @brief Bit masks belonging to the bRequest field of a setup packet
  158. */
  159. #define USB_B_REQUEST_GET_STATUS 0x00
  160. #define USB_B_REQUEST_CLEAR_FEATURE 0x01
  161. #define USB_B_REQUEST_SET_FEATURE 0x03
  162. #define USB_B_REQUEST_SET_ADDRESS 0x05
  163. #define USB_B_REQUEST_GET_DESCRIPTOR 0x06
  164. #define USB_B_REQUEST_SET_DESCRIPTOR 0x07
  165. #define USB_B_REQUEST_GET_CONFIGURATION 0x08
  166. #define USB_B_REQUEST_SET_CONFIGURATION 0x09
  167. #define USB_B_REQUEST_GET_INTERFACE 0x0A
  168. #define USB_B_REQUEST_SET_INTERFACE 0x0B
  169. #define USB_B_REQUEST_SYNCH_FRAME 0x0C
  170. /**
  171. * @brief Bit masks belonging to the wValue field of a setup packet
  172. */
  173. #define USB_W_VALUE_DT_DEVICE 0x01
  174. #define USB_W_VALUE_DT_CONFIG 0x02
  175. #define USB_W_VALUE_DT_STRING 0x03
  176. #define USB_W_VALUE_DT_INTERFACE 0x04
  177. #define USB_W_VALUE_DT_ENDPOINT 0x05
  178. #define USB_W_VALUE_DT_DEVICE_QUALIFIER 0x06
  179. #define USB_W_VALUE_DT_OTHER_SPEED_CONFIG 0x07
  180. #define USB_W_VALUE_DT_INTERFACE_POWER 0x08
  181. /**
  182. * @brief Initializer for a SET_ADDRESS request
  183. *
  184. * Sets the address of a connected device
  185. */
  186. #define USB_CTRL_REQ_INIT_SET_ADDR(ctrl_req_ptr, addr) ({ \
  187. (ctrl_req_ptr)->bRequestType = USB_B_REQUEST_TYPE_DIR_OUT | USB_B_REQUEST_TYPE_TYPE_STANDARD |USB_B_REQUEST_TYPE_RECIP_DEVICE; \
  188. (ctrl_req_ptr)->bRequest = USB_B_REQUEST_SET_ADDRESS; \
  189. (ctrl_req_ptr)->wValue = (addr); \
  190. (ctrl_req_ptr)->wIndex = 0; \
  191. (ctrl_req_ptr)->wLength = 0; \
  192. })
  193. /**
  194. * @brief Initializer for a request to get a device's device descriptor
  195. */
  196. #define USB_CTRL_REQ_INIT_GET_DEVC_DESC(ctrl_req_ptr) ({ \
  197. (ctrl_req_ptr)->bRequestType = USB_B_REQUEST_TYPE_DIR_IN | USB_B_REQUEST_TYPE_TYPE_STANDARD | USB_B_REQUEST_TYPE_RECIP_DEVICE; \
  198. (ctrl_req_ptr)->bRequest = USB_B_REQUEST_GET_DESCRIPTOR; \
  199. (ctrl_req_ptr)->wValue = (USB_W_VALUE_DT_DEVICE << 8); \
  200. (ctrl_req_ptr)->wIndex = 0; \
  201. (ctrl_req_ptr)->wLength = 18; \
  202. })
  203. /**
  204. * @brief Initializer for a request to get a device's current configuration number
  205. */
  206. #define USB_CTRL_REQ_INIT_GET_CONFIG(ctrl_req_ptr) ({ \
  207. (ctrl_req_ptr)->bRequestType = USB_B_REQUEST_TYPE_DIR_IN | USB_B_REQUEST_TYPE_TYPE_STANDARD | USB_B_REQUEST_TYPE_RECIP_DEVICE; \
  208. (ctrl_req_ptr)->bRequest = USB_B_REQUEST_GET_CONFIGURATION; \
  209. (ctrl_req_ptr)->wValue = 0; \
  210. (ctrl_req_ptr)->wIndex = 0; \
  211. (ctrl_req_ptr)->wLength = 1; \
  212. })
  213. /**
  214. * @brief Initializer for a request to get one of the device's current configuration descriptor
  215. *
  216. * - desc_index indicates the configuration's index number
  217. * - Number of bytes of the configuration descriptor to get
  218. */
  219. #define USB_CTRL_REQ_INIT_GET_CFG_DESC(ctrl_req_ptr, desc_index, desc_len) ({ \
  220. (ctrl_req_ptr)->bRequestType = USB_B_REQUEST_TYPE_DIR_IN | USB_B_REQUEST_TYPE_TYPE_STANDARD | USB_B_REQUEST_TYPE_RECIP_DEVICE; \
  221. (ctrl_req_ptr)->bRequest = USB_B_REQUEST_GET_DESCRIPTOR; \
  222. (ctrl_req_ptr)->wValue = (USB_W_VALUE_DT_CONFIG << 8) | ((desc_index) & 0xFF); \
  223. (ctrl_req_ptr)->wIndex = 0; \
  224. (ctrl_req_ptr)->wLength = (desc_len); \
  225. })
  226. /**
  227. * @brief Initializer for a request to set a device's current configuration number
  228. */
  229. #define USB_CTRL_REQ_INIT_SET_CONFIG(ctrl_req_ptr, config_num) ({ \
  230. (ctrl_req_ptr)->bRequestType = USB_B_REQUEST_TYPE_DIR_OUT | USB_B_REQUEST_TYPE_TYPE_STANDARD | USB_B_REQUEST_TYPE_RECIP_DEVICE; \
  231. (ctrl_req_ptr)->bRequest = USB_B_REQUEST_SET_CONFIGURATION; \
  232. (ctrl_req_ptr)->wValue = (config_num); \
  233. (ctrl_req_ptr)->wIndex = 0; \
  234. (ctrl_req_ptr)->wLength = 0; \
  235. })
  236. /**
  237. * @brief Initializer for a request to set an interface's alternate setting
  238. */
  239. #define USB_CTRL_REQ_INIT_SET_INTERFACE(ctrl_req_ptr, intf_num, alt_setting_num) ({ \
  240. (ctrl_req_ptr)->bRequestType = USB_B_REQUEST_TYPE_DIR_OUT | USB_B_REQUEST_TYPE_TYPE_STANDARD | USB_B_REQUEST_TYPE_RECIP_INTERFACE; \
  241. (ctrl_req_ptr)->bRequest = USB_B_REQUEST_SET_INTERFACE; \
  242. (ctrl_req_ptr)->wValue = (alt_setting_num); \
  243. (ctrl_req_ptr)->wIndex = (intf_num); \
  244. (ctrl_req_ptr)->wLength = 0; \
  245. })
  246. // ------------------ Device Descriptor --------------------
  247. /**
  248. * @brief Size of a USB device descriptor in bytes
  249. */
  250. #define USB_DESC_DEVC_SIZE 18
  251. /**
  252. * @brief Structure representing a USB device descriptor
  253. */
  254. typedef union {
  255. struct {
  256. uint8_t bLength;
  257. uint8_t bDescriptorType;
  258. uint16_t bcdUSB;
  259. uint8_t bDeviceClass;
  260. uint8_t bDeviceSubClass;
  261. uint8_t bDeviceProtocol;
  262. uint8_t bMaxPacketSize0;
  263. uint16_t idVendor;
  264. uint16_t idProduct;
  265. uint16_t bcdDevice;
  266. uint8_t iManufacturer;
  267. uint8_t iProduct;
  268. uint8_t iSerialNumber;
  269. uint8_t bNumConfigurations;
  270. } USB_DESC_ATTR;
  271. uint8_t val[USB_DESC_DEVC_SIZE];
  272. } usb_desc_devc_t;
  273. _Static_assert(sizeof(usb_desc_devc_t) == USB_DESC_DEVC_SIZE, "Size of usb_desc_devc_t incorrect");
  274. /**
  275. * @brief Possible base class values of the bDeviceClass field of a USB device descriptor
  276. */
  277. #define USB_CLASS_PER_INTERFACE 0x00
  278. #define USB_CLASS_AUDIO 0x01
  279. #define USB_CLASS_COMM 0x02
  280. #define USB_CLASS_HID 0x03
  281. #define USB_CLASS_PHYSICAL 0x05
  282. #define USB_CLASS_STILL_IMAGE 0x06
  283. #define USB_CLASS_PRINTER 0x07
  284. #define USB_CLASS_MASS_STORAGE 0x08
  285. #define USB_CLASS_HUB 0x09
  286. #define USB_CLASS_CDC_DATA 0x0a
  287. #define USB_CLASS_CSCID 0x0b
  288. #define USB_CLASS_CONTENT_SEC 0x0d
  289. #define USB_CLASS_VIDEO 0x0e
  290. #define USB_CLASS_WIRELESS_CONTROLLER 0xe0
  291. #define USB_CLASS_PERSONAL_HEALTHCARE 0x0f
  292. #define USB_CLASS_AUDIO_VIDEO 0x10
  293. #define USB_CLASS_BILLBOARD 0x11
  294. #define USB_CLASS_USB_TYPE_C_BRIDGE 0x12
  295. #define USB_CLASS_MISC 0xef
  296. #define USB_CLASS_APP_SPEC 0xfe
  297. #define USB_CLASS_VENDOR_SPEC 0xff
  298. /**
  299. * @brief Vendor specific subclass code
  300. */
  301. #define USB_SUBCLASS_VENDOR_SPEC 0xff
  302. // -------------- Configuration Descriptor -----------------
  303. /**
  304. * @brief Size of a short USB configuration descriptor in bytes
  305. *
  306. * @note The size of a full USB configuration includes all the interface and endpoint
  307. * descriptors of that configuration.
  308. */
  309. #define USB_DESC_CFG_SIZE 9
  310. /**
  311. * @brief Structure representing a short USB configuration descriptor
  312. *
  313. * @note The full USB configuration includes all the interface and endpoint
  314. * descriptors of that configuration.
  315. */
  316. typedef union {
  317. struct {
  318. uint8_t bLength;
  319. uint8_t bDescriptorType;
  320. uint16_t wTotalLength;
  321. uint8_t bNumInterfaces;
  322. uint8_t bConfigurationValue;
  323. uint8_t iConfiguration;
  324. uint8_t bmAttributes;
  325. uint8_t bMaxPower;
  326. } USB_DESC_ATTR;
  327. uint8_t val[USB_DESC_CFG_SIZE];
  328. } usb_desc_cfg_t;
  329. _Static_assert(sizeof(usb_desc_cfg_t) == USB_DESC_CFG_SIZE, "Size of usb_desc_cfg_t incorrect");
  330. /**
  331. * @brief Bit masks belonging to the bmAttributes field of a configuration descriptor
  332. */
  333. #define USB_BM_ATTRIBUTES_ONE (1 << 7) //Must be set
  334. #define USB_BM_ATTRIBUTES_SELFPOWER (1 << 6) //Self powered
  335. #define USB_BM_ATTRIBUTES_WAKEUP (1 << 5) //Can wake-up
  336. #define USB_BM_ATTRIBUTES_BATTERY (1 << 4) //Battery powered
  337. // ---------------- Interface Descriptor -------------------
  338. /**
  339. * @brief Size of a USB interface descriptor in bytes
  340. */
  341. #define USB_DESC_INTF_SIZE 9
  342. /**
  343. * @brief Structure representing a USB interface descriptor
  344. */
  345. typedef union {
  346. struct {
  347. uint8_t bLength;
  348. uint8_t bDescriptorType;
  349. uint8_t bInterfaceNumber;
  350. uint8_t bAlternateSetting;
  351. uint8_t bNumEndpoints;
  352. uint8_t bInterfaceClass;
  353. uint8_t bInterfaceSubClass;
  354. uint8_t bInterfaceProtocol;
  355. uint8_t iInterface;
  356. } USB_DESC_ATTR;
  357. uint8_t val[USB_DESC_INTF_SIZE];
  358. } usb_desc_intf_t;
  359. _Static_assert(sizeof(usb_desc_intf_t) == USB_DESC_INTF_SIZE, "Size of usb_desc_intf_t incorrect");
  360. // ----------------- Endpoint Descriptor -------------------
  361. /**
  362. * @brief Size of a USB endpoint descriptor in bytes
  363. */
  364. #define USB_DESC_EP_SIZE 7
  365. /**
  366. * @brief Structure representing a USB endpoint descriptor
  367. */
  368. typedef union {
  369. struct {
  370. uint8_t bLength;
  371. uint8_t bDescriptorType;
  372. uint8_t bEndpointAddress;
  373. uint8_t bmAttributes;
  374. uint16_t wMaxPacketSize;
  375. uint8_t bInterval;
  376. } USB_DESC_ATTR;
  377. uint8_t val[USB_DESC_EP_SIZE];
  378. } usb_desc_ep_t;
  379. _Static_assert(sizeof(usb_desc_ep_t) == USB_DESC_EP_SIZE, "Size of usb_desc_ep_t incorrect");
  380. /**
  381. * @brief Bit masks belonging to the bEndpointAddress field of an endpoint descriptor
  382. */
  383. #define USB_B_ENDPOINT_ADDRESS_EP_NUM_MASK 0x0f
  384. #define USB_B_ENDPOINT_ADDRESS_EP_DIR_MASK 0x80
  385. /**
  386. * @brief Bit masks belonging to the bmAttributes field of an endpoint descriptor
  387. */
  388. #define USB_BM_ATTRIBUTES_XFERTYPE_MASK 0x03
  389. #define USB_BM_ATTRIBUTES_XFER_CONTROL (0 << 0)
  390. #define USB_BM_ATTRIBUTES_XFER_ISOC (1 << 0)
  391. #define USB_BM_ATTRIBUTES_XFER_BULK (2 << 0)
  392. #define USB_BM_ATTRIBUTES_XFER_INT (3 << 0)
  393. #define USB_BM_ATTRIBUTES_SYNCTYPE_MASK 0x0C /* in bmAttributes */
  394. #define USB_BM_ATTRIBUTES_SYNC_NONE (0 << 2)
  395. #define USB_BM_ATTRIBUTES_SYNC_ASYNC (1 << 2)
  396. #define USB_BM_ATTRIBUTES_SYNC_ADAPTIVE (2 << 2)
  397. #define USB_BM_ATTRIBUTES_SYNC_SYNC (3 << 2)
  398. #define USB_BM_ATTRIBUTES_USAGETYPE_MASK 0x30
  399. #define USB_BM_ATTRIBUTES_USAGE_DATA (0 << 4)
  400. #define USB_BM_ATTRIBUTES_USAGE_FEEDBACK (1 << 4)
  401. #define USB_BM_ATTRIBUTES_USAGE_IMPLICIT_FB (2 << 4)
  402. /**
  403. * @brief Macro helpers to get information about an endpoint from its descriptor
  404. */
  405. #define USB_DESC_EP_GET_XFERTYPE(desc_ptr) ((usb_transfer_type_t) ((desc_ptr)->bmAttributes & USB_BM_ATTRIBUTES_XFERTYPE_MASK))
  406. #define USB_DESC_EP_GET_EP_NUM(desc_ptr) ((desc_ptr)->bEndpointAddress & USB_B_ENDPOINT_ADDRESS_EP_NUM_MASK)
  407. #define USB_DESC_EP_GET_EP_DIR(desc_ptr) (((desc_ptr)->bEndpointAddress & USB_B_ENDPOINT_ADDRESS_EP_DIR_MASK) ? 1 : 0)
  408. #define USB_DESC_EP_GET_MPS(desc_ptr) ((desc_ptr)->wMaxPacketSize & 0x7FF)
  409. // ------------------ String Descriptor --------------------
  410. /**
  411. * @brief Size of a short USB string descriptor in bytes
  412. */
  413. #define USB_DESC_STR_SIZE 4
  414. /**
  415. * @brief Structure representing a USB string descriptor
  416. */
  417. typedef union {
  418. struct {
  419. uint8_t bLength;
  420. uint8_t bDescriptorType;
  421. uint16_t wData[1]; /* UTF-16LE encoded */
  422. } USB_DESC_ATTR;
  423. uint8_t val[USB_DESC_STR_SIZE];
  424. } usb_desc_str_t;
  425. _Static_assert(sizeof(usb_desc_str_t) == USB_DESC_STR_SIZE, "Size of usb_desc_str_t incorrect");
  426. #ifdef __cplusplus
  427. }
  428. #endif