usbd_code.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /* Define to prevent recursive inclusion -------------------------------------------------------------------*/
  2. #ifndef __USBD_CODE_H
  3. #define __USBD_CODE_H
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. /* Includes ------------------------------------------------------------------------------------------------*/
  8. #include "ht32.h"
  9. /** @addtogroup HT32_USBD_Library
  10. * @{
  11. */
  12. /** @addtogroup USBDCore
  13. * @{
  14. */
  15. /* Settings ------------------------------------------------------------------------------------------------*/
  16. /** @defgroup USBDCore_Settings USB Device Core settings
  17. * @{
  18. */
  19. /* USBD Debug mode */
  20. //<e0.0> Enable USB Debug mode
  21. //<o1.0> Dump USB Debug data
  22. #ifndef USBDCORE_DEBUG
  23. #define USBDCORE_DEBUG (0) /*!< Enable USB Debug mode */
  24. #define USBDCORE_DEBUG_DATA (0) /*!< Dump USB Debug data */
  25. #endif
  26. /**
  27. * @}
  28. */
  29. /* Exported types ------------------------------------------------------------------------------------------*/
  30. /** @defgroup USBDCore_Exported_Type USB Device Core exported types
  31. * @{
  32. */
  33. /**
  34. * @brief USB Device Request.
  35. */
  36. typedef __PACKED_H struct
  37. {
  38. uc8 bmRequestType;
  39. uc8 bRequest;
  40. uc8 wValueL;
  41. uc8 wValueH;
  42. uc16 wIndex;
  43. uc16 wLength;
  44. } __PACKED_F USBDCore_Request_TypeDef;
  45. /**
  46. * @brief USB Descriptor.
  47. */
  48. typedef struct
  49. {
  50. uc8 *pDeviceDesc; /*!< Device Descriptor */
  51. uc8 *pConfnDesc; /*!< Configuration Descriptor */
  52. uc8 **ppStringDesc; /*!< String Descriptor */
  53. u32 uStringDescNumber; /*!< Count of String Descriptor */
  54. } USBDCore_Desc_TypeDef;
  55. /**
  56. * @brief STALL, control IN or control OUT.
  57. */
  58. typedef enum
  59. {
  60. USB_ACTION_STALL = 0,
  61. USB_ACTION_DATAIN = 1,
  62. USB_ACTION_DATAOUT = 2,
  63. } USBDCore_Action_Enum;
  64. /**
  65. * @brief Call back function.
  66. */
  67. typedef struct
  68. {
  69. void (*func) (u32 uPara); /*!< Call back function pointer */
  70. u32 uPara; /*!< Parameter of call back function */
  71. } USBDCore_CallBack_TypeDef;
  72. /**
  73. * @brief Parameter for control IN/OUT Transfer.
  74. */
  75. typedef struct
  76. {
  77. u8 uBuffer[2]; /*!< Temporary buffer */
  78. uc8 *pData; /*!< Pointer of control IN/OUT Data */
  79. s32 sByteLength; /*!< Total length for control IN/OUT Transfer */
  80. USBDCore_Action_Enum Action; /*!< STALL, control IN or control OUT */
  81. USBDCore_CallBack_TypeDef CallBack_OUT; /*!< Call back function pointer for Control OUT */
  82. } USBDCore_Transfer_TypeDef;
  83. /**
  84. * @brief USB Device.
  85. */
  86. typedef struct
  87. {
  88. USBDCore_Request_TypeDef Request; /*!< USB Device Request */
  89. USBDCore_Desc_TypeDef Desc; /*!< USB Descriptor */
  90. USBDCore_Transfer_TypeDef Transfer; /*!< Parameter for control IN/OUT Transfer */
  91. } USBDCore_Device_TypeDef;
  92. /**
  93. * @brief Bit access for CurrentFeature.
  94. */
  95. typedef __PACKED_H struct _FEATURE_TYPEBIT
  96. {
  97. unsigned bSelfPowered :1; /*!< Remote Wakeup feature */
  98. unsigned bRemoteWakeup :1; /*!< Self Powered */
  99. } __PACKED_F USBDCore_Feature_TypeBit;
  100. /**
  101. * @brief For Set/ClearFeature and GetStatus request.
  102. */
  103. typedef __PACKED_H union _FEATURE_TYPEDEF
  104. {
  105. u8 uByte; /*!< Byte access for CurrentFeature */
  106. USBDCore_Feature_TypeBit Bits; /*!< Bit access for CurrentFeature */
  107. } __PACKED_F USBDCore_Feature_TypeDef;
  108. /**
  109. * @brief Device State.
  110. */
  111. typedef enum
  112. {
  113. USER_USB_STATE_UNCONNECTED = 0,
  114. USER_USB_STATE_ATTACHED = 1,
  115. USER_USB_STATE_POWERED = 2,
  116. USER_USB_STATE_SUSPENDED = 3,
  117. USER_USB_STATE_DEFAULT = 4,
  118. USER_USB_STATE_ADDRESS = 5,
  119. USER_USB_STATE_CONFIGURED = 6,
  120. } USBDCore_Status_Enum;
  121. /**
  122. * @brief USB Device information.
  123. */
  124. typedef struct
  125. {
  126. u8 uCurrentConfiguration; /*!< For Set/GetConfiguration request */
  127. u8 uCurrentInterface; /*!< For Set/GetInterface request */
  128. volatile USBDCore_Status_Enum CurrentStatus; /*!< Device State */
  129. USBDCore_Status_Enum LastStatus; /*!< Device State before SUSPEND */
  130. USBDCore_Feature_TypeDef CurrentFeature; /*!< For Set/ClearFeature and GetStatus request */
  131. u32 uIsDiscardClearFeature; /*!< Discard ClearFeature flag for Mass Storage */
  132. } USBDCore_Info_TypeDef;
  133. typedef void (*USBDCore_CallBackClass_Typedef) (USBDCore_Device_TypeDef *pDev);
  134. typedef void (*USBDCore_CallBackVendor_Typedef) (USBDCore_Device_TypeDef *pDev);
  135. typedef void (*USBDCore_CallBackEPTn_Typedef) (USBD_EPTn_Enum EPTn);
  136. /**
  137. * @brief USB Class call back function.
  138. */
  139. typedef struct
  140. {
  141. USBDCore_CallBack_TypeDef CallBack_MainRoutine; /*!< Class main routine call back function */
  142. USBDCore_CallBack_TypeDef CallBack_Reset; /*!< Class RESET call back function */
  143. USBDCore_CallBack_TypeDef CallBack_StartOfFrame; /*!< Class SOF call back function */
  144. USBDCore_CallBackClass_Typedef CallBack_ClassGetDescriptor; /*!< Class Get Descriptor call back function */
  145. USBDCore_CallBackClass_Typedef CallBack_ClassSetInterface; /*!< Set Interface call back function */
  146. USBDCore_CallBackClass_Typedef CallBack_ClassGetInterface; /*!< Get Interface call back function */
  147. USBDCore_CallBackClass_Typedef CallBack_ClassRequest; /*!< Class Request call back function */
  148. USBDCore_CallBackVendor_Typedef CallBack_VendorRequest; /*!< Vendor Request call back function */
  149. USBDCore_CallBackEPTn_Typedef CallBack_EPTn[MAX_EP_NUM]; /*!< Endpoint n call back function */
  150. } USBDCore_Class_TypeDef;
  151. /**
  152. * @brief USB Device Power related call back function.
  153. */
  154. typedef struct
  155. {
  156. USBDCore_CallBack_TypeDef CallBack_Suspend;
  157. } USBDCore_Power_TypeDef;
  158. /**
  159. * @brief USB EP TypeDef.
  160. */
  161. typedef struct
  162. {
  163. uint16_t maxpacket; /*!< endpoint max packet*/
  164. uint16_t status; /*!< endpoint status */
  165. /* transmission buffer and count */
  166. uint16_t total_len; /*!< endpoint transmission total length */
  167. uint16_t trans_len; /*!< endpoint transmission length*/
  168. uint8_t *trans_buf; /*!< endpoint transmission buffer */
  169. } usb_ept_info;
  170. /**
  171. * @brief Major structure of USB Library.
  172. */
  173. typedef struct
  174. {
  175. USBDCore_Device_TypeDef Device; /*!< USB Device */
  176. USBDCore_Info_TypeDef Info; /*!< USB Device information */
  177. USBDCore_Class_TypeDef Class; /*!< USB Class call back function */
  178. u32 *pDriver; /*!< USB Device Driver initialization structure */
  179. USBDCore_Power_TypeDef Power; /*!< USB Device Power related call back function */
  180. usb_ept_info ept_io[8]; /*!< */
  181. void *pdata; /*!< USB User private pointer */
  182. } USBDCore_TypeDef;
  183. /*----------------------------------------------------------------------------------------------------------*/
  184. /* Variable architecture of USB Library */
  185. /*----------------------------------------------------------------------------------------------------------*/
  186. /* USBCore - USBDCore_TypeDef Major structure of USB Library */
  187. /* USBCore.Device - USBDCore_Device_TypeDef USB Device */
  188. /* USBCore.Device.Request - USBDCore_Request_TypeDef USB Device Request */
  189. /* USBCore.Device.Request.bmRequestType */
  190. /* USBCore.Device.Request.bRequest */
  191. /* USBCore.Device.Request.wValueL */
  192. /* USBCore.Device.Request.wValueH */
  193. /* USBCore.Device.Request.wIndex */
  194. /* USBCore.Device.Request.wLength */
  195. /* USBCore.Device.Desc - USBDCore_Desc_TypeDef USB Descriptor */
  196. /* USBCore.Device.Desc.pDeviceDesc Device Descriptor */
  197. /* USBCore.Device.Desc.pConfnDesc Configuration Descriptor */
  198. /* USBCore.Device.Desc.pStringDesc[DESC_NUM_STRING] String Descriptor */
  199. /* USBCore.Device.Desc.uStringDescNumber Count of String Descriptor */
  200. /* USBCore.Device.Transfer - USBDCore_Transfer_TypeDef Parameter for control IN/OUT Transfer */
  201. /* USBCore.Device.Transfer.uBuffer[2] Temporary buffer */
  202. /* USBCore.Device.Transfer.pData Pointer of control IN/OUT Data */
  203. /* USBCore.Device.Transfer.sByteLength Total length for control IN/OUT Transfer */
  204. /* USBCore.Device.Transfer.Action - USBDCore_Action_Enum STALL, control IN or control OUT */
  205. /* USBCore.Device.Transfer.CallBack_OUT.func(uPara) Call back function pointer for Control OUT */
  206. /* USBCore.Device.Transfer.CallBack_OUT.uPara Parameter of Control OUT call back function */
  207. /* */
  208. /* USBCore.Info - USBDCore_Info_TypeDef USB Device information */
  209. /* USBCore.Info.uCurrentConfiguration For Set/GetConfiguration request */
  210. /* USBCore.Info.uCurrentInterface For Set/GetInterface request */
  211. /* USBCore.Info.CurrentStatus - USBDCore_Status_Enum Device State */
  212. /* USBCore.Info.LastStatus - USBDCore_Status_Enum Device State before SUSPEND */
  213. /* USBCore.Info.CurrentFeature - USBDCore_Feature_TypeDef For Set/ClearFeature and GetStatus request */
  214. /* USBCore.Info.CurrentFeature.uByte Byte access for CurrentFeature */
  215. /* USBCore.Info.CurrentFeature.Bits.bRemoteWakeup Remote Wakeup feature */
  216. /* USBCore.Info.CurrentFeature.Bits.bSelfPowered Self Powered */
  217. /* USBCore.Info.uIsDiscardClearFeature Discard ClearFeature flag for Mass Storage */
  218. /* */
  219. /* USBCore.Class - USBDCore_Class_TypeDef USB Class call back function */
  220. /* USBCore.Class.CallBack_MainRoutine.func(uPara) Class main routine call back function */
  221. /* USBCore.Class.CallBack_MainRoutine.uPara Parameter of class main routine */
  222. /* USBCore.Class.CallBack_Reset.func(uPara) Class RESET call back function */
  223. /* USBCore.Class.CallBack_Reset.uPara Parameter of RESET call back function */
  224. /* USBCore.Class.CallBack_StartOfFrame.func(uPara) Class SOF call back function */
  225. /* USBCore.Class.CallBack_StartOfFrame.uPara Parameter of SOF call back function */
  226. /* USBCore.Class.CallBack_ClassGetDescriptor(pDev) Class Get Descriptor call back function */
  227. /* USBCore.Class.CallBack_ClassSetInterface(pDev) Set Interface call back function */
  228. /* USBCore.Class.CallBack_ClassGetInterface(pDev) Get Interface call back function */
  229. /* USBCore.Class.CallBack_ClassRequest(pDev) Class Request call back function */
  230. /* USBCore.Class.CallBack_EPTn[MAX_EP_NUM](EPTn) Endpoint n call back function */
  231. /* */
  232. /* USBCore.pDriver USB Device Driver initialization structure */
  233. /* */
  234. /* USBCore.Power - USBDCore_Power_TypeDef USB Device Power related call back function */
  235. /* USBCore.Power.CallBack_Suspend.func(uPara) System low power function for SUSPEND */
  236. /* USBCore.Power.CallBack_Suspend.uPara Parameter of system low power function */
  237. /*----------------------------------------------------------------------------------------------------------*/
  238. /**
  239. * @}
  240. */
  241. /* Exported constants --------------------------------------------------------------------------------------*/
  242. /** @defgroup USBDCore_Exported_Constant USB Device Core exported constants
  243. * @{
  244. */
  245. /** @defgroup USBDCore_Descriptor Definitions for USB descriptor
  246. * @{
  247. */
  248. #define DESC_TYPE_01_DEV (0x1)
  249. #define DESC_TYPE_02_CONFN (0x2)
  250. #define DESC_TYPE_03_STR (0x3)
  251. #define DESC_TYPE_04_INF (0x4)
  252. #define DESC_TYPE_05_EPT (0x5)
  253. #define DESC_TYPE_06_DEV_QLF (0x6)
  254. #define DESC_TYPE_08_INF_PWR (0x8)
  255. #define DESC_CLASS_00_BY_INF (0x00)
  256. #define DESC_CLASS_01_AUDIO (0x01)
  257. #define DESC_CLASS_02_CDC_CTRL (0x02)
  258. #define DESC_CLASS_03_HID (0x03)
  259. #define DESC_CLASS_05_PHY (0x05)
  260. #define DESC_CLASS_06_STILL_IMG (0x06)
  261. #define DESC_CLASS_07_PRINTER (0x07)
  262. #define DESC_CLASS_08_MASS_STORAGE (0x08)
  263. #define DESC_CLASS_09_HUB (0x09)
  264. #define DESC_CLASS_0A_CDC_DATA (0x0A)
  265. #define DESC_CLASS_0B_SMART_CARD (0x0B)
  266. #define DESC_CLASS_0E_VIDEO (0x0E)
  267. #define DESC_CLASS_0F_PHD (0x0F)
  268. #define DESC_CLASS_FF_VENDOR (0xFF)
  269. #define DESC_LEN_DEV ((u32)(18))
  270. #define DESC_LEN_CONFN ((u32)(9))
  271. #define DESC_LEN_INF ((u32)(9))
  272. #define DESC_LEN_EPT ((u32)(7))
  273. /**
  274. * @}
  275. */
  276. /** @defgroup USBDCore_Request Definitions for USB Request
  277. * @{
  278. */
  279. #define REQ_DIR_00_H2D (0 << 7)
  280. #define REQ_DIR_01_D2H (1 << 7)
  281. #define REQ_TYPE_00_STD (0 << 5)
  282. #define REQ_TYPE_01_CLS (1 << 5)
  283. #define REQ_TYPE_02_VND (2 << 5)
  284. #define REQ_REC_00_DEV (0)
  285. #define REQ_REC_01_INF (1)
  286. #define REQ_REC_02_EPT (2)
  287. /**
  288. * @}
  289. */
  290. /**
  291. * @brief For USBDCore_EPTReadOUTData function.
  292. */
  293. #define USB_DISCARD_OUT_DATA (0)
  294. /**
  295. * @}
  296. */
  297. /* Exported macro ------------------------------------------------------------------------------------------*/
  298. /** @defgroup USBDCore_Exported_Macro USB Device Core exported macros
  299. * @{
  300. */
  301. #define __DBG_USBPrintf(...)
  302. #define __DBG_USBDump(a, b)
  303. #if (USBDCORE_DEBUG == 1)
  304. #ifndef RETARGET_IS_USB
  305. extern u32 __DBG_USBCount;
  306. #undef __DBG_USBPrintf
  307. #define __DBG_USBPrintf printf
  308. #if (USBDCORE_DEBUG_DATA == 1)
  309. #undef __DBG_USBDump
  310. void __DBG_USBDump(uc8 *memory, u32 len);
  311. #endif
  312. #endif
  313. #endif
  314. /**
  315. * @brief Convert Half-Word to Byte for descriptor.
  316. */
  317. #define DESC_H2B(Val) ((u8)(Val & 0x00FF)), ((u8)((Val & 0xFF00) >> 8))
  318. /**
  319. * @brief Padding 0 automatically for String descriptor.
  320. */
  321. #define DESC_CHAR(c) (c), (0)
  322. /**
  323. * @brief Calculate String length for String descriptor.
  324. */
  325. #define DESC_STRLEN(n) (n * 2 + 2)
  326. /**
  327. * @brief Calculate power for Configuration descriptor.
  328. */
  329. #define DESC_POWER(mA) (mA / 2)
  330. /**
  331. * @}
  332. */
  333. /* Exported functions --------------------------------------------------------------------------------------*/
  334. /** @defgroup USBDCore_Exported_Functions USB Device Core exported functions
  335. * @{
  336. */
  337. #define USBDCore_DeInit API_USB_DEINIT
  338. #define USBDCore_EPTReset API_USB_EPTn_RESET
  339. #define USBDCore_EPTGetBufferLen API_USB_EPTn_GET_BUFFLEN
  340. #define USBDCore_EPTGetTransferCount API_USB_EPTn_GET_CNT
  341. #define USBDCore_EPTSetSTALL API_USB_EPTn_SET_HALT
  342. #define USBDCore_EPTWaitSTALLSent API_USB_EPTn_WAIT_STALL_SENT
  343. #define USBDCore_EPTClearDataToggle API_USB_EPTn_CLR_DTG
  344. #define USBDCore_EPTWriteINData API_USB_EPTn_WRITE_IN
  345. #define USBDCore_EPTReadOUTData API_USB_EPTn_READ_OUT
  346. #define USBDCore_EPTReadMemory API_USB_EPTn_READ_MEM
  347. void usbd_irq_handler(USBDCore_TypeDef *pCore);
  348. void USBDCore_Init(USBDCore_TypeDef *pCore);
  349. void USBDCore_MainRoutine(USBDCore_TypeDef *pCore);
  350. void usbd_ep_enable(USBDCore_TypeDef *pCore, uint8_t ept_addr);
  351. void usbd_ep_disable(USBDCore_TypeDef *pCore, uint8_t ept_addr);
  352. void usbd_ept_recv(USBDCore_TypeDef *udev, uint8_t ept_addr, uint8_t *buffer, uint16_t len);
  353. //u32 USBDCore_IsSuspend(USBDCore_TypeDef *pCore);
  354. //u32 USBDCore_GetRemoteWakeUpFeature(USBDCore_TypeDef *pCore);
  355. //void USBDCore_TriggerRemoteWakeup(void);
  356. //USBDCore_Status_Enum USBDCore_GetStatus(void);
  357. void USBDCore_EPTReset(USBD_EPTn_Enum USBD_EPTn);
  358. u32 USBDCore_EPTGetBufferLen(USBD_EPTn_Enum USBD_EPTn);
  359. u32 USBDCore_EPTGetTransferCount(USBD_EPTn_Enum USBD_EPTn, USBD_TCR_Enum type);
  360. void USBDCore_EPTSetSTALL(USBD_EPTn_Enum USBD_EPTn);
  361. void USBDCore_EPTWaitSTALLSent(USBD_EPTn_Enum USBD_EPTn);
  362. void USBDCore_EPTClearDataToggle(USBD_EPTn_Enum USBD_EPTn);
  363. u32 USBDCore_EPTWriteINData(USBD_EPTn_Enum USBD_EPTn, u32 *pFrom, u32 len);
  364. u32 USBDCore_EPTReadOUTData(USBD_EPTn_Enum USBD_EPTn, u32 *pTo, u32 len);
  365. u32 USBDCore_EPTReadMemory(USBD_EPTn_Enum USBD_EPTn, u32 *pTo, u32 len);
  366. /**
  367. * @}
  368. */
  369. /**
  370. * @}
  371. */
  372. /**
  373. * @}
  374. */
  375. #ifdef __cplusplus
  376. }
  377. #endif
  378. #endif /* __USBD_CODE_H -------------------------------------------------------------------------------*/