usb_std.h 18 KB

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