usbd_def.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /**
  2. ******************************************************************************
  3. * @file usbd_def.h
  4. * @author MCD Application Team
  5. * @version V2.4.2
  6. * @date 11-December-2015
  7. * @brief General defines for the usb device library
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT 2015 STMicroelectronics</center></h2>
  12. *
  13. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  14. * You may not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at:
  16. *
  17. * http://www.st.com/software_license_agreement_liberty_v2
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS,
  21. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. ******************************************************************************
  26. */
  27. /* Define to prevent recursive inclusion -------------------------------------*/
  28. #ifndef __USBD_DEF_H
  29. #define __USBD_DEF_H
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. /* Includes ------------------------------------------------------------------*/
  34. #include "usbd_conf.h"
  35. /** @addtogroup STM32_USBD_DEVICE_LIBRARY
  36. * @{
  37. */
  38. /** @defgroup USB_DEF
  39. * @brief general defines for the usb device library file
  40. * @{
  41. */
  42. /** @defgroup USB_DEF_Exported_Defines
  43. * @{
  44. */
  45. #ifndef NULL
  46. #define NULL 0
  47. #endif
  48. #define USB_LEN_DEV_QUALIFIER_DESC 0x0A
  49. #define USB_LEN_DEV_DESC 0x12
  50. #define USB_LEN_CFG_DESC 0x09
  51. #define USB_LEN_IF_DESC 0x09
  52. #define USB_LEN_EP_DESC 0x07
  53. #define USB_LEN_OTG_DESC 0x03
  54. #define USB_LEN_LANGID_STR_DESC 0x04
  55. #define USB_LEN_OTHER_SPEED_DESC_SIZ 0x09
  56. #define USBD_IDX_LANGID_STR 0x00
  57. #define USBD_IDX_MFC_STR 0x01
  58. #define USBD_IDX_PRODUCT_STR 0x02
  59. #define USBD_IDX_SERIAL_STR 0x03
  60. #define USBD_IDX_CONFIG_STR 0x04
  61. #define USBD_IDX_INTERFACE_STR 0x05
  62. #define USB_REQ_TYPE_STANDARD 0x00
  63. #define USB_REQ_TYPE_CLASS 0x20
  64. #define USB_REQ_TYPE_VENDOR 0x40
  65. #define USB_REQ_TYPE_MASK 0x60
  66. #define USB_REQ_RECIPIENT_DEVICE 0x00
  67. #define USB_REQ_RECIPIENT_INTERFACE 0x01
  68. #define USB_REQ_RECIPIENT_ENDPOINT 0x02
  69. #define USB_REQ_RECIPIENT_MASK 0x1F
  70. #define USB_REQ_GET_STATUS 0x00
  71. #define USB_REQ_CLEAR_FEATURE 0x01
  72. #define USB_REQ_SET_FEATURE 0x03
  73. #define USB_REQ_SET_ADDRESS 0x05
  74. #define USB_REQ_GET_DESCRIPTOR 0x06
  75. #define USB_REQ_SET_DESCRIPTOR 0x07
  76. #define USB_REQ_GET_CONFIGURATION 0x08
  77. #define USB_REQ_SET_CONFIGURATION 0x09
  78. #define USB_REQ_GET_INTERFACE 0x0A
  79. #define USB_REQ_SET_INTERFACE 0x0B
  80. #define USB_REQ_SYNCH_FRAME 0x0C
  81. #define USB_REQ_VENDOR_FEATURE 0x21
  82. #define USB_REQ_MS_OS_STRING_DESCRIPTOR 0xEE
  83. #define USB_REQ_EXT_COMPAT_ID_OS_FEATURE_DESCRIPTOR 0x04
  84. #define USB_REQ_EXT_PROPERTIES_OS_FEATURE_DESCRIPTOR 0x05
  85. #define USB_DESC_TYPE_DEVICE 1
  86. #define USB_DESC_TYPE_CONFIGURATION 2
  87. #define USB_DESC_TYPE_STRING 3
  88. #define USB_DESC_TYPE_INTERFACE 4
  89. #define USB_DESC_TYPE_ENDPOINT 5
  90. #define USB_DESC_TYPE_DEVICE_QUALIFIER 6
  91. #define USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION 7
  92. #define USB_DESC_TYPE_BOS 0x0F
  93. #define USB_CONFIG_REMOTE_WAKEUP 2
  94. #define USB_CONFIG_SELF_POWERED 1
  95. #define USB_FEATURE_EP_HALT 0
  96. #define USB_FEATURE_REMOTE_WAKEUP 1
  97. #define USB_FEATURE_TEST_MODE 2
  98. #define USB_DEVICE_CAPABITY_TYPE 0x10
  99. //#define USB_HS_MAX_PACKET_SIZE 512
  100. //#define USB_FS_MAX_PACKET_SIZE 64
  101. #define USB_MAX_EP0_SIZE 64
  102. /* Device Status */
  103. #define USBD_STATE_DEFAULT 1
  104. #define USBD_STATE_ADDRESSED 2
  105. #define USBD_STATE_CONFIGURED 3
  106. #define USBD_STATE_SUSPENDED 4
  107. /* EP0 State */
  108. #define USBD_EP0_IDLE 0
  109. #define USBD_EP0_SETUP 1
  110. #define USBD_EP0_DATA_IN 2
  111. #define USBD_EP0_DATA_OUT 3
  112. #define USBD_EP0_STATUS_IN 4
  113. #define USBD_EP0_STATUS_OUT 5
  114. #define USBD_EP0_STALL 6
  115. #define USBD_EP_TYPE_CTRL 0
  116. #define USBD_EP_TYPE_ISOC 1
  117. #define USBD_EP_TYPE_BULK 2
  118. #define USBD_EP_TYPE_INTR 3
  119. /**
  120. * @}
  121. */
  122. /** @defgroup USBD_DEF_Exported_TypesDefinitions
  123. * @{
  124. */
  125. typedef struct usb_setup_req
  126. {
  127. uint8_t bmRequest;
  128. uint8_t bRequest;
  129. uint16_t wValue;
  130. uint16_t wIndex;
  131. uint16_t wLength;
  132. }USBD_SetupReqTypedef;
  133. struct _USBD_HandleTypeDef;
  134. /* Following USB Device Speed */
  135. typedef enum
  136. {
  137. USBD_SPEED_HIGH = 0, // not supported
  138. USBD_SPEED_FULL = 1,
  139. USBD_SPEED_LOW = 2,
  140. }USBD_SpeedTypeDef;
  141. /* Following USB Device status */
  142. typedef enum {
  143. USBD_OK = 0,
  144. USBD_BUSY,
  145. USBD_FAIL,
  146. USBD_CONTINUE, // added for vendor driver
  147. }USBD_StatusTypeDef;
  148. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  149. #if USBD_GETDESCRIPTORS_ALTMETHOD == 0
  150. typedef enum
  151. {
  152. eGetDescriptorHandlerType_Device = 0, // refer to USBD_DescriptorsTypeDef.GetDeviceDescriptor
  153. eGetDescriptorHandlerType_LangIdStr = 1, // refer to USBD_DescriptorsTypeDef.GetLangIDStrDescriptor
  154. eGetDescriptorHandlerType_ManufacturerStr = 2, // refer to USBD_DescriptorsTypeDef.GetManufacturerStrDescriptor
  155. eGetDescriptorHandlerType_ProductStr = 3, // refer to USBD_DescriptorsTypeDef.GetProductStrDescriptor
  156. eGetDescriptorHandlerType_SerialStr = 4, // refer to USBD_DescriptorsTypeDef.GetSerialStrDescriptor
  157. eGetDescriptorHandlerType_ConfigurationStr = 5, // refer to USBD_DescriptorsTypeDef.GetConfigurationStrDescriptor
  158. eGetDescriptorHandlerType_InterfaceStr = 6, // refer to USBD_DescriptorsTypeDef.GetInterfaceStrDescriptor
  159. eGetDescriptorHandlerType_Config = 7, // refer to USBD_DescriptorsTypeDef.GetConfigDescriptor
  160. eGetDescriptorHandlerType_MsOsString = 8, // refer to USBD_DescriptorsTypeDef.GetMsOsStringDescriptor
  161. eGetDescriptorHandlerType_ExtCompatIdOsFeat = 9, // refer to USBD_DescriptorsTypeDef.GetExtCompatIdOsFeatureDescriptor
  162. eGetDescriptorHandlerType_ExtPropertiesOsFeat = 10, // refer to USBD_DescriptorsTypeDef.GetMsOsStringDescriptor
  163. eGetDescriptorHandlerType_MAX
  164. // DO NOT ADD @GetUsrStrDescriptor, this is the other type routine (see prototype!)
  165. }
  166. eGetDescriptorHandlerType_t;
  167. typedef const uint8_t *(*fGetDescriptorRoutine_t)( USBD_SpeedTypeDef speed , uint16_t *length);
  168. // USB Device descriptors structure
  169. typedef union
  170. {
  171. struct
  172. {
  173. // NOTE: DO NOT CHANGE THIS STRUCTURE CONTENTS AND FILEDS ORDER WITHOUT
  174. // CHANGING THE @eGetDescriptorHandlerType_t TYPE!
  175. // These routines are used indirectly by position in @arrayGetDescriptorRoutines field!
  176. const uint8_t *(*GetDeviceDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length);
  177. const uint8_t *(*GetLangIDStrDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length);
  178. const uint8_t *(*GetManufacturerStrDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length);
  179. const uint8_t *(*GetProductStrDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length);
  180. const uint8_t *(*GetSerialStrDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length);
  181. const uint8_t *(*GetConfigurationStrDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length);
  182. const uint8_t *(*GetInterfaceStrDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length);
  183. const uint8_t *(*GetConfigDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length);
  184. #if (USBD_SUPPORT_USER_STRING == 1)
  185. // This routine is optional, it is used directly only ( pDesc->GetUsrStrDescriptor(...) )
  186. const uint8_t *(*GetUsrStrDescriptor)(struct _USBD_HandleTypeDef *pdev ,uint8_t index, uint16_t *length);
  187. #endif
  188. };
  189. // @arrayGetDescriptorRoutines
  190. // used by @GetDescriptorHandler
  191. fGetDescriptorRoutine_t arrayGetDescriptorRoutines[ eGetDescriptorHandlerType_MAX ];
  192. } USBD_DescriptorsTypeDef;
  193. // @GetDescriptorHandler
  194. // This function returns the routine address from the @USBD_DescriptorsTypeDef descriptor (@pGetDescriptorHanders)
  195. // by the specified type (@desc_type)
  196. // @pGetDescriptorHanders - the pointer to the get-descriptor routines set, MUST NOT BE NULL!
  197. // @desc_type - the descriptor type
  198. static inline fGetDescriptorRoutine_t GetDescriptorHandler( const USBD_DescriptorsTypeDef * pGetDescriptorHanders, eGetDescriptorHandlerType_t desc_type )
  199. {
  200. return pGetDescriptorHanders->arrayGetDescriptorRoutines[ desc_type % eGetDescriptorHandlerType_MAX ];
  201. }
  202. // @ChooseDescriptorHandler
  203. // This function returns the routine address from the @USBD_DescriptorsTypeDef descriptor (@pGetDescriptorHanders)
  204. // by the specified type (@desc_type).
  205. // @pGetDescriptorHandersSetA - the pointer to the first get-descriptor routines set, can be NULL
  206. // @pGetDescriptorHandersSetB - the pointer to the second get-descriptor routines set, can be NULL
  207. // @desc_type - the descriptor type
  208. // The function chooses the valid non-null handler between two options: either from the first set,
  209. // or the second set.
  210. // If the @pGetDescriptorHandersSetA is not NULL, and the specified handler is not null,
  211. // this handler will be returned.
  212. // If the @pGetDescriptorHandersSetA is not NULL, and the specified handler is null,
  213. // the function will try to get handler from the @pGetDescriptorHandersSetB.
  214. // If the @pGetDescriptorHandersSetA is NULL, or the specified handler in @pGetDescriptorHandersSetA is NULL,
  215. // and @pGetDescriptorHandersSetB is not NULL, the function returns the specified handler from
  216. // the @pGetDescriptorHandersSetB set without cheching for NULL.
  217. // Returns: the first non-null specified handler either from @pGetDescriptorHandersSetA
  218. // or pGetDescriptorHandersSetB set.
  219. static inline fGetDescriptorRoutine_t ChooseDescriptorHandler(
  220. const USBD_DescriptorsTypeDef * pGetDescriptorHandersSetA,
  221. const USBD_DescriptorsTypeDef * pGetDescriptorHandersSetB,
  222. eGetDescriptorHandlerType_t desc_type )
  223. {
  224. fGetDescriptorRoutine_t routine = NULL;
  225. if( NULL != pGetDescriptorHandersSetA )
  226. {
  227. routine = GetDescriptorHandler( pGetDescriptorHandersSetA, desc_type );
  228. }
  229. if( (NULL == routine) && (NULL != pGetDescriptorHandersSetA) )
  230. {
  231. routine = GetDescriptorHandler( pGetDescriptorHandersSetB, desc_type );
  232. }
  233. return routine;
  234. }
  235. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  236. // USB Device descriptors structure
  237. // for compatibility with option @USBD_GETDESCRIPTORS_ALTMETHOD=1
  238. #define USBD_GETDESCRIPTOR_DEFAULT_HANDLERS_SET // empty
  239. #else
  240. typedef struct
  241. {
  242. const uint8_t *(*GetDeviceDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length);
  243. const uint8_t *(*GetLangIDStrDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length);
  244. const uint8_t *(*GetManufacturerStrDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length);
  245. const uint8_t *(*GetProductStrDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length);
  246. const uint8_t *(*GetSerialStrDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length);
  247. const uint8_t *(*GetConfigurationStrDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length);
  248. const uint8_t *(*GetInterfaceStrDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length);
  249. const uint8_t *(*GetConfigDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length);
  250. const uint8_t *(*GetMsOsStringDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length);
  251. const uint8_t *(*GetExtCompatIdOsFeatureDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length);
  252. const uint8_t *(*GetExtPropertiesOsFeatureDescriptor)(USBD_SpeedTypeDef speed , uint16_t *length);
  253. #if (USBD_SUPPORT_USER_STRING == 1)
  254. // This routine is optional, it is used directly only ( pDesc->GetUsrStrDescriptor(...) )
  255. const uint8_t *(*GetUsrStrDescriptor)(struct _USBD_HandleTypeDef *pdev ,uint8_t index, uint16_t *length);
  256. #endif
  257. }
  258. USBD_DescriptorsTypeDef;
  259. #endif
  260. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  261. typedef struct _Device_cb
  262. {
  263. uint8_t (*Init) (struct _USBD_HandleTypeDef *pdev , uint8_t cfgidx);
  264. uint8_t (*DeInit) (struct _USBD_HandleTypeDef *pdev , uint8_t cfgidx);
  265. uint8_t (*Error) (struct _USBD_HandleTypeDef *pdev , uint8_t epnum);
  266. /* Control Endpoints*/
  267. uint8_t (*Setup) (struct _USBD_HandleTypeDef *pdev , USBD_SetupReqTypedef *req);
  268. uint8_t (*EP0_TxSent) (struct _USBD_HandleTypeDef *pdev , size_t * pRollbackBytes );
  269. uint8_t (*EP0_RxReady) (struct _USBD_HandleTypeDef *pdev , size_t * pRollbackBytes );
  270. /* Class Specific Endpoints*/
  271. uint8_t (*DataIn) (struct _USBD_HandleTypeDef *pdev , uint8_t epnum);
  272. uint8_t (*DataOut) (struct _USBD_HandleTypeDef *pdev , uint8_t epnum, bool bShortPacketRcvd );
  273. uint8_t (*SOF) (struct _USBD_HandleTypeDef *pdev);
  274. const USBD_DescriptorsTypeDef * pDesc; // class-defined GetDescriptor-handlers
  275. } USBD_ClassTypeDef;
  276. /* USB Device handle structure */
  277. typedef struct
  278. {
  279. uint32_t status;
  280. uint32_t total_length;
  281. uint32_t rem_length;
  282. uint32_t maxpacket;
  283. } USBD_EndpointTypeDef;
  284. /* USB Device handle structure */
  285. typedef struct _USBD_HandleTypeDef
  286. {
  287. uint8_t id;
  288. uint32_t dev_config;
  289. uint32_t dev_default_config;
  290. uint32_t dev_config_status;
  291. USBD_SpeedTypeDef dev_speed;
  292. USBD_EndpointTypeDef ep_in[15];
  293. USBD_EndpointTypeDef ep_out[15];
  294. uint32_t ep0_state;
  295. uint32_t ep0_data_len;
  296. uint8_t dev_state;
  297. uint8_t dev_old_state;
  298. uint8_t dev_address;
  299. uint8_t dev_connection_status;
  300. uint8_t dev_test_mode;
  301. uint32_t dev_remote_wakeup;
  302. USBD_SetupReqTypedef request;
  303. #if USBD_GETDESCRIPTORS_ALTMETHOD == 0
  304. USBD_DescriptorsTypeDef *pDesc;
  305. #endif
  306. #if USBD_CONSTCLASS_IFACE
  307. const USBD_ClassTypeDef *pClass;
  308. #else
  309. USBD_ClassTypeDef *pClass;
  310. #endif
  311. void *pClassData;
  312. #if USBD_CONSTCLASS_IFACE
  313. const void *pUserData;
  314. #else
  315. void *pUserData;
  316. #endif
  317. void *pData;
  318. } USBD_HandleTypeDef;
  319. /**
  320. * @}
  321. */
  322. /** @defgroup USBD_DEF_Exported_Macros
  323. * @{
  324. */
  325. #define SWAPBYTE(addr) (((uint16_t)(*((uint8_t *)(addr)))) + \
  326. (((uint16_t)(*(((uint8_t *)(addr)) + 1))) << 8))
  327. #define LOBYTE(x) ((uint8_t)(x & 0x00FF))
  328. #define HIBYTE(x) ((uint8_t)((x & 0xFF00) >>8))
  329. #define MIN(a, b) (((a) < (b)) ? (a) : (b))
  330. #define MAX(a, b) (((a) > (b)) ? (a) : (b))
  331. #if defined ( __GNUC__ )
  332. #ifndef __weak
  333. #define __weak __attribute__((weak))
  334. #endif /* __weak */
  335. #ifndef __packed
  336. #define __packed __attribute__((__packed__))
  337. #endif /* __packed */
  338. #endif /* __GNUC__ */
  339. /* In HS mode and when the DMA is used, all variables and data structures dealing
  340. with the DMA during the transaction process should be 4-bytes aligned */
  341. #if defined (__GNUC__) /* GNU Compiler */
  342. #define __ALIGN_END __attribute__ ((aligned (4)))
  343. #define __ALIGN_BEGIN
  344. #else
  345. #define __ALIGN_END
  346. #if defined (__CC_ARM) /* ARM Compiler */
  347. #define __ALIGN_BEGIN __align(4)
  348. #elif defined (__ICCARM__) /* IAR Compiler */
  349. #define __ALIGN_BEGIN
  350. #elif defined (__TASKING__) /* TASKING Compiler */
  351. #define __ALIGN_BEGIN __align(4)
  352. #endif /* __CC_ARM */
  353. #endif /* __GNUC__ */
  354. /**
  355. * @}
  356. */
  357. /** @defgroup USBD_DEF_Exported_Variables
  358. * @{
  359. */
  360. /**
  361. * @}
  362. */
  363. /** @defgroup USBD_DEF_Exported_FunctionsPrototype
  364. * @{
  365. */
  366. /**
  367. * @}
  368. */
  369. #ifdef __cplusplus
  370. }
  371. #endif
  372. #endif /* __USBD_DEF_H */
  373. /**
  374. * @}
  375. */
  376. /**
  377. * @}
  378. */
  379. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/