usb_std.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /**
  2. **************************************************************************
  3. * @file usb_std.h
  4. * @version v2.0.6
  5. * @date 2021-12-31
  6. * @brief usb standard header file
  7. **************************************************************************
  8. * Copyright notice & Disclaimer
  9. *
  10. * The software Board Support Package (BSP) that is made available to
  11. * download from Artery official website is the copyrighted work of Artery.
  12. * Artery authorizes customers to use, copy, and distribute the BSP
  13. * software and its related documentation for the purpose of design and
  14. * development in conjunction with Artery microcontrollers. Use of the
  15. * software is governed by this copyright notice and the following disclaimer.
  16. *
  17. * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  18. * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  19. * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  20. * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  21. * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  23. *
  24. **************************************************************************
  25. */
  26. /* define to prevent recursive inclusion -------------------------------------*/
  27. #ifndef __USB_STD_H
  28. #define __USB_STD_H
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. /* includes ------------------------------------------------------------------*/
  33. #include "usb_conf.h"
  34. /** @addtogroup AT32F403A_407_middlewares_usbd_drivers
  35. * @{
  36. */
  37. /** @addtogroup USB_standard
  38. * @{
  39. */
  40. /** @defgroup USB_standard_define
  41. * @{
  42. */
  43. /**
  44. * @brief usb request recipient
  45. */
  46. #define USB_REQ_RECIPIENT_DEVICE 0x00 /*!< usb request recipient device */
  47. #define USB_REQ_RECIPIENT_INTERFACE 0x01 /*!< usb request recipient interface */
  48. #define USB_REQ_RECIPIENT_ENDPOINT 0x02 /*!< usb request recipient endpoint */
  49. #define USB_REQ_RECIPIENT_OTHER 0x03 /*!< usb request recipient other */
  50. #define USB_REQ_RECIPIENT_MASK 0x1F /*!< usb request recipient mask */
  51. /**
  52. * @brief usb request type
  53. */
  54. #define USB_REQ_TYPE_STANDARD 0x00 /*!< usb request type standard */
  55. #define USB_REQ_TYPE_CLASS 0x20 /*!< usb request type class */
  56. #define USB_REQ_TYPE_VENDOR 0x40 /*!< usb request type vendor */
  57. #define USB_REQ_TYPE_RESERVED 0x60 /*!< usb request type reserved */
  58. /**
  59. * @brief usb request data transfer direction
  60. */
  61. #define USB_REQ_DIR_HTD 0x00 /*!< usb request data transfer direction host to device */
  62. #define USB_REQ_DIR_DTH 0x80 /*!< usb request data transfer direction device to host */
  63. /**
  64. * @brief usb standard device requests codes
  65. */
  66. #define USB_STD_REQ_GET_STATUS 0 /*!< usb request code status */
  67. #define USB_STD_REQ_CLEAR_FEATURE 1 /*!< usb request code clear feature */
  68. #define USB_STD_REQ_SET_FEATURE 3 /*!< usb request code feature */
  69. #define USB_STD_REQ_SET_ADDRESS 5 /*!< usb request code address */
  70. #define USB_STD_REQ_GET_DESCRIPTOR 6 /*!< usb request code get descriptor */
  71. #define USB_STD_REQ_SET_DESCRIPTOR 7 /*!< usb request code set descriptor */
  72. #define USB_STD_REQ_GET_CONFIGURATION 8 /*!< usb request code get configuration */
  73. #define USB_STD_REQ_SET_CONFIGURATION 9 /*!< usb request code set configuration */
  74. #define USB_STD_REQ_GET_INTERFACE 10 /*!< usb request code get interface */
  75. #define USB_STD_REQ_SET_INTERFACE 11 /*!< usb request code set interface */
  76. #define USB_STD_REQ_SYNCH_FRAME 12 /*!< usb request code synch frame */
  77. /**
  78. * @brief usb standard device type
  79. */
  80. #define USB_DESCIPTOR_TYPE_DEVICE 1 /*!< usb standard device type device */
  81. #define USB_DESCIPTOR_TYPE_CONFIGURATION 2 /*!< usb standard device type configuration */
  82. #define USB_DESCIPTOR_TYPE_STRING 3 /*!< usb standard device type string */
  83. #define USB_DESCIPTOR_TYPE_INTERFACE 4 /*!< usb standard device type interface */
  84. #define USB_DESCIPTOR_TYPE_ENDPOINT 5 /*!< usb standard device type endpoint */
  85. #define USB_DESCIPTOR_TYPE_DEVICE_QUALIFIER 6 /*!< usb standard device type qualifier */
  86. #define USB_DESCIPTOR_TYPE_OTHER_SPEED 7 /*!< usb standard device type other speed */
  87. #define USB_DESCIPTOR_TYPE_INTERFACE_POWER 8 /*!< usb standard device type interface power */
  88. /**
  89. * @brief usb standard string type
  90. */
  91. #define USB_LANGID_STRING 0 /*!< usb standard string type lang id */
  92. #define USB_MFC_STRING 1 /*!< usb standard string type mfc */
  93. #define USB_PRODUCT_STRING 2 /*!< usb standard string type product */
  94. #define USB_SERIAL_STRING 3 /*!< usb standard string type serial */
  95. #define USB_CONFIG_STRING 4 /*!< usb standard string type config */
  96. #define USB_INTERFACE_STRING 5 /*!< usb standard string type interface */
  97. /**
  98. * @brief usb configuration attributes
  99. */
  100. #define USB_CONF_REMOTE_WAKEUP 2 /*!< usb configuration attributes remote wakeup */
  101. #define USB_CONF_SELF_POWERED 1 /*!< usb configuration attributes self powered */
  102. /**
  103. * @brief usb standard feature selectors
  104. */
  105. #define USB_FEATURE_EPT_HALT 0 /*!< usb standard feature selectors endpoint halt */
  106. #define USB_FEATURE_REMOTE_WAKEUP 1 /*!< usb standard feature selectors remote wakeup */
  107. #define USB_FEATURE_TEST_MODE 2 /*!< usb standard feature selectors test mode */
  108. /**
  109. * @brief usb device connect state
  110. */
  111. typedef enum
  112. {
  113. USB_CONN_STATE_DEFAULT =1, /*!< usb device connect state default */
  114. USB_CONN_STATE_ADDRESSED, /*!< usb device connect state address */
  115. USB_CONN_STATE_CONFIGURED, /*!< usb device connect state configured */
  116. USB_CONN_STATE_SUSPENDED /*!< usb device connect state suspend */
  117. }usbd_conn_state;
  118. /**
  119. * @brief endpoint 0 state
  120. */
  121. #define USB_EPT0_IDLE 0 /*!< usb endpoint state idle */
  122. #define USB_EPT0_SETUP 1 /*!< usb endpoint state setup */
  123. #define USB_EPT0_DATA_IN 2 /*!< usb endpoint state data in */
  124. #define USB_EPT0_DATA_OUT 3 /*!< usb endpoint state data out */
  125. #define USB_EPT0_STATUS_IN 4 /*!< usb endpoint state status in */
  126. #define USB_EPT0_STATUS_OUT 5 /*!< usb endpoint state status out */
  127. #define USB_EPT0_STALL 6 /*!< usb endpoint state stall */
  128. /**
  129. * @brief usb descriptor length
  130. */
  131. #define USB_DEVICE_QUALIFIER_DESC_LEN 0x0A /*!< usb qualifier descriptor length */
  132. #define USB_DEVICE_DESC_LEN 0x12 /*!< usb device descriptor length */
  133. #define USB_DEVICE_CFG_DESC_LEN 0x09 /*!< usb configuration descriptor length */
  134. #define USB_DEVICE_IF_DESC_LEN 0x09 /*!< usb interface descriptor length */
  135. #define USB_DEVICE_EPT_LEN 0x07 /*!< usb endpoint descriptor length */
  136. #define USB_DEVICE_OTG_DESC_LEN 0x03 /*!< usb otg descriptor length */
  137. #define USB_DEVICE_LANGID_STR_DESC_LEN 0x04 /*!< usb lang id string descriptor length */
  138. #define USB_DEVICE_OTHER_SPEED_DESC_SIZ_LEN 0x09 /*!< usb other speed descriptor length */
  139. /**
  140. * @brief usb class code
  141. */
  142. #define USB_CLASS_CODE_AUDIO 0x01 /*!< usb class code audio */
  143. #define USB_CLASS_CODE_CDC 0x02 /*!< usb class code cdc */
  144. #define USB_CLASS_CODE_HID 0x03 /*!< usb class code hid */
  145. #define USB_CLASS_CODE_PRINTER 0x07 /*!< usb class code printer */
  146. #define USB_CLASS_CODE_MSC 0x08 /*!< usb class code msc */
  147. #define USB_CLASS_CODE_HUB 0x09 /*!< usb class code hub */
  148. #define USB_CLASS_CODE_CDCDATA 0x0A /*!< usb class code cdc data */
  149. #define USB_CLASS_CODE_CCID 0x0B /*!< usb class code ccid */
  150. #define USB_CLASS_CODE_VIDEO 0x0E /*!< usb class code video */
  151. #define USB_CLASS_CODE_VENDOR 0xFF /*!< usb class code vendor */
  152. /**
  153. * @brief usb endpoint type
  154. */
  155. #define USB_EPT_DESC_CONTROL 0x00 /*!< usb endpoint description type control */
  156. #define USB_EPT_DESC_ISO 0x01 /*!< usb endpoint description type iso */
  157. #define USB_EPT_DESC_BULK 0x02 /*!< usb endpoint description type bulk */
  158. #define USB_EPT_DESC_INTERRUPT 0x03 /*!< usb endpoint description type interrupt */
  159. #define USB_EPT_DESC_NSYNC 0x00 /*!< usb endpoint description nsync */
  160. #define USB_ETP_DESC_ASYNC 0x04 /*!< usb endpoint description async */
  161. #define USB_ETP_DESC_ADAPTIVE 0x08 /*!< usb endpoint description adaptive */
  162. #define USB_ETP_DESC_SYNC 0x0C /*!< usb endpoint description sync */
  163. #define USB_EPT_DESC_DATA_EPT 0x00 /*!< usb endpoint description data */
  164. #define USB_EPT_DESC_FD_EPT 0x10 /*!< usb endpoint description fd */
  165. #define USB_EPT_DESC_FDDATA_EPT 0x20 /*!< usb endpoint description fddata */
  166. /**
  167. * @brief endpoint 0 max size
  168. */
  169. #define USB_MAX_EP0_SIZE 64 /*!< usb endpoint 0 max size */
  170. /**
  171. * @brief usb swap address
  172. */
  173. #define SWAPBYTE(addr) (uint16_t)(((uint16_t)(*((uint8_t *)(addr)))) + \
  174. (((uint16_t)(*(((uint8_t *)(addr)) + 1))) << 8)) /*!< swap address */
  175. /**
  176. * @brief min and max define
  177. */
  178. #define MIN(a, b) (uint16_t)(((a) < (b)) ? (a) : (b)) /*!< min define*/
  179. #define MAX(a, b) (uint16_t)(((a) > (b)) ? (a) : (b)) /*!< max define*/
  180. /**
  181. * @brief low byte and high byte define
  182. */
  183. #define LBYTE(x) ((uint8_t)(x & 0x00FF)) /*!< low byte define */
  184. #define HBYTE(x) ((uint8_t)((x & 0xFF00) >>8)) /*!< high byte define*/
  185. /**
  186. * @}
  187. */
  188. /** @defgroup USB_standard_exported_types
  189. * @{
  190. */
  191. /**
  192. * @brief usb return status
  193. */
  194. typedef enum
  195. {
  196. USB_OK, /*!< usb status ok */
  197. USB_FAIL, /*!< usb status fail */
  198. USB_WAIT, /*!< usb status wait */
  199. USB_NOT_SUPPORT, /*!< usb status not support */
  200. USB_ERROR, /*!< usb status error */
  201. }usb_sts_type;
  202. /**
  203. * @brief format of usb setup data
  204. */
  205. typedef struct
  206. {
  207. uint8_t bmRequestType; /*!< characteristics of request */
  208. uint8_t bRequest; /*!< specific request */
  209. uint16_t wValue; /*!< word-sized field that varies according to request */
  210. uint16_t wIndex; /*!< word-sized field that varies according to request
  211. typically used to pass an index or offset */
  212. uint16_t wLength; /*!< number of bytes to transfer if there is a data stage */
  213. }usb_setup_type;
  214. /**
  215. * @brief format of standard device descriptor
  216. */
  217. typedef struct
  218. {
  219. uint8_t bLength; /*!< size of this descriptor in bytes */
  220. uint8_t bDescriptorType; /*!< device descriptor type */
  221. uint16_t bcdUSB; /*!< usb specification release number */
  222. uint8_t bDeviceClass; /*!< class code (assigned by the usb-if) */
  223. uint8_t bDeviceSubClass; /*!< subclass code (assigned by the usb-if) */
  224. uint8_t bDeviceProtocol; /*!< protocol code ((assigned by the usb-if)) */
  225. uint8_t bMaxPacketSize0; /*!< maximum packet size for endpoint zero */
  226. uint16_t idVendor; /*!< verndor id ((assigned by the usb-if)) */
  227. uint16_t idProduct; /*!< product id ((assigned by the usb-if)) */
  228. uint16_t bcdDevice; /*!< device release number in binary-coded decimal */
  229. uint8_t iManufacturer; /*!< index of string descriptor describing manufacturer */
  230. uint8_t iProduct; /*!< index of string descriptor describing product */
  231. uint8_t iSerialNumber; /*!< index of string descriptor describing serial number */
  232. uint8_t bNumConfigurations; /*!< number of possible configurations */
  233. }usb_device_desc_type;
  234. /**
  235. * @brief format of standard configuration descriptor
  236. */
  237. typedef struct
  238. {
  239. uint8_t bLength; /*!< size of this descriptor in bytes */
  240. uint8_t bDescriptorType; /*!< configuration descriptor type */
  241. uint16_t wTotalLength; /*!< total length of data returned for this configuration */
  242. uint8_t bNumInterfaces; /*!< number of interfaces supported by this configuration */
  243. uint8_t bConfigurationValue; /*!< value to use as an argument to the SetConfiguration() request */
  244. uint8_t iConfiguration; /*!< index of string descriptor describing this configuration */
  245. uint8_t bmAttributes; /*!< configuration characteristics
  246. D7 reserved
  247. D6 self-powered
  248. D5 remote wakeup
  249. D4~D0 reserved */
  250. uint8_t bMaxPower; /*!< maximum power consumption of the usb device from the bus */
  251. }usb_configuration_desc_type;
  252. /**
  253. * @brief format of standard interface descriptor
  254. */
  255. typedef struct
  256. {
  257. uint8_t bLength; /*!< size of this descriptor in bytes */
  258. uint8_t bDescriptorType; /*!< interface descriptor type */
  259. uint8_t bInterfaceNumber; /*!< number of this interface */
  260. uint8_t bAlternateSetting; /*!< value used to select this alternate setting for the interface */
  261. uint8_t bNumEndpoints; /*!< number of endpoints used by this interface */
  262. uint8_t bInterfaceClass; /*!< class code (assigned by the usb-if) */
  263. uint8_t bInterfaceSubClass; /*!< subclass code (assigned by the usb-if) */
  264. uint8_t bInterfaceProtocol; /*!< protocol code (assigned by the usb-if) */
  265. uint8_t iInterface; /*!< index of string descriptor describing this interface */
  266. }usb_interface_desc_type;
  267. /**
  268. * @brief format of standard endpoint descriptor
  269. */
  270. typedef struct
  271. {
  272. uint8_t bLength; /*!< size of this descriptor in bytes */
  273. uint8_t bDescriptorType; /*!< endpoint descriptor type */
  274. uint8_t bEndpointAddress; /*!< the address of the endpoint on the usb device described by this descriptor */
  275. uint8_t bmAttributes; /*!< describes the endpoints attributes when it is configured using bConfiguration value */
  276. uint16_t wMaxPacketSize; /*!< maximum packet size this endpoint */
  277. uint8_t bInterval; /*!< interval for polling endpoint for data transfers */
  278. }usb_endpoint_desc_type;
  279. /**
  280. * @brief format of header
  281. */
  282. typedef struct
  283. {
  284. uint8_t bLength; /*!< size of this descriptor in bytes */
  285. uint8_t bDescriptorType; /*!< descriptor type */
  286. }usb_header_desc_type;
  287. /**
  288. * @}
  289. */
  290. /**
  291. * @}
  292. */
  293. /**
  294. * @}
  295. */
  296. #ifdef __cplusplus
  297. }
  298. #endif
  299. #endif