usbd.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /**************************************************************************//**
  2. * @file usbd.h
  3. * @version V1.00
  4. * $Revision: 24 $
  5. * $Date: 15/12/01 10:14a $
  6. * @brief NUC472/NUC442 USBD driver header file
  7. *
  8. * @note
  9. * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
  10. *****************************************************************************/
  11. #ifndef __USBD_H__
  12. #define __USBD_H__
  13. #ifdef __cplusplus
  14. extern "C"
  15. {
  16. #endif
  17. /** @addtogroup NUC472_442_Device_Driver NUC472/NUC442 Device Driver
  18. @{
  19. */
  20. /** @addtogroup NUC472_442_USBD_Driver USBD Driver
  21. @{
  22. */
  23. /** @addtogroup NUC472_442_USBD_EXPORTED_CONSTANTS USBD Exported Constants
  24. @{
  25. */
  26. /// @cond HIDDEN_SYMBOLS
  27. #define USBD_MAX_EP 12
  28. #define Maximum(a,b) (a)>(b) ? (a) : (b)
  29. #define Minimum(a,b) (a)<(b) ? (a) : (b)
  30. #define CEP 0xff /*!< Control Endpoint \hideinitializer */
  31. #define EPA 0 /*!< Endpoint A \hideinitializer */
  32. #define EPB 1 /*!< Endpoint B \hideinitializer */
  33. #define EPC 2 /*!< Endpoint C \hideinitializer */
  34. #define EPD 3 /*!< Endpoint D \hideinitializer */
  35. #define EPE 4 /*!< Endpoint E \hideinitializer */
  36. #define EPF 5 /*!< Endpoint F \hideinitializer */
  37. #define EPG 6 /*!< Endpoint G \hideinitializer */
  38. #define EPH 7 /*!< Endpoint H \hideinitializer */
  39. #define EPI 8 /*!< Endpoint I \hideinitializer */
  40. #define EPJ 9 /*!< Endpoint J \hideinitializer */
  41. #define EPK 10 /*!< Endpoint K \hideinitializer */
  42. #define EPL 11 /*!< Endpoint L \hideinitializer */
  43. /* USB Request Type */
  44. #define REQ_STANDARD 0x00
  45. #define REQ_CLASS 0x20
  46. #define REQ_VENDOR 0x40
  47. /* USB Standard Request */
  48. #define GET_STATUS 0x00
  49. #define CLEAR_FEATURE 0x01
  50. #define SET_FEATURE 0x03
  51. #define SET_ADDRESS 0x05
  52. #define GET_DESCRIPTOR 0x06
  53. #define SET_DESCRIPTOR 0x07
  54. #define GET_CONFIGURATION 0x08
  55. #define SET_CONFIGURATION 0x09
  56. #define GET_INTERFACE 0x0A
  57. #define SET_INTERFACE 0x0B
  58. #define SYNC_FRAME 0x0C
  59. /* USB Descriptor Type */
  60. #define DESC_DEVICE 0x01
  61. #define DESC_CONFIG 0x02
  62. #define DESC_STRING 0x03
  63. #define DESC_INTERFACE 0x04
  64. #define DESC_ENDPOINT 0x05
  65. #define DESC_QUALIFIER 0x06
  66. #define DESC_OTHERSPEED 0x07
  67. #define DESC_IFPOWER 0x08
  68. #define DESC_OTG 0x09
  69. /* USB HID Descriptor Type */
  70. #define DESC_HID 0x21
  71. #define DESC_HID_RPT 0x22
  72. /* USB Descriptor Length */
  73. #define LEN_DEVICE 18
  74. #define LEN_QUALIFIER 10
  75. #define LEN_CONFIG 9
  76. #define LEN_INTERFACE 9
  77. #define LEN_ENDPOINT 7
  78. #define LEN_OTG 5
  79. #define LEN_HID 9
  80. /* USB Endpoint Type */
  81. #define EP_ISO 0x01
  82. #define EP_BULK 0x02
  83. #define EP_INT 0x03
  84. #define EP_INPUT 0x80
  85. #define EP_OUTPUT 0x00
  86. /* USB Feature Selector */
  87. #define FEATURE_DEVICE_REMOTE_WAKEUP 0x01
  88. #define FEATURE_ENDPOINT_HALT 0x00
  89. /// @endcond HIDDEN_SYMBOLS
  90. /********************* Bit definition of CEPCTL register **********************/
  91. #define USB_CEPCTL_NAKCLR ((uint32_t)0x00000000) /*!<NAK clear \hideinitializer */
  92. #define USB_CEPCTL_STALL ((uint32_t)0x00000002) /*!<Stall \hideinitializer */
  93. #define USB_CEPCTL_ZEROLEN ((uint32_t)0x00000004) /*!<Zero length packet \hideinitializer */
  94. #define USB_CEPCTL_FLUSH ((uint32_t)0x00000008) /*!<CEP flush \hideinitializer */
  95. /********************* Bit definition of EPxRSPCTL register **********************/
  96. #define USB_EP_RSPCTL_FLUSH ((uint32_t)0x00000001) /*!<Buffer Flush \hideinitializer */
  97. #define USB_EP_RSPCTL_MODE_AUTO ((uint32_t)0x00000000) /*!<Auto-Validate Mode \hideinitializer */
  98. #define USB_EP_RSPCTL_MODE_MANUAL ((uint32_t)0x00000002) /*!<Manual-Validate Mode \hideinitializer */
  99. #define USB_EP_RSPCTL_MODE_FLY ((uint32_t)0x00000004) /*!<Fly Mode \hideinitializer */
  100. #define USB_EP_RSPCTL_MODE_MASK ((uint32_t)0x00000006) /*!<Mode Mask \hideinitializer */
  101. #define USB_EP_RSPCTL_TOGGLE ((uint32_t)0x00000008) /*!<Clear Toggle bit \hideinitializer */
  102. #define USB_EP_RSPCTL_HALT ((uint32_t)0x00000010) /*!<Endpoint halt \hideinitializer */
  103. #define USB_EP_RSPCTL_ZEROLEN ((uint32_t)0x00000020) /*!<Zero length packet IN \hideinitializer */
  104. #define USB_EP_RSPCTL_SHORTTXEN ((uint32_t)0x00000040) /*!<Packet end \hideinitializer */
  105. #define USB_EP_RSPCTL_DISBUF ((uint32_t)0x00000080) /*!<Disable buffer \hideinitializer */
  106. /********************* Bit definition of EPxCFG register **********************/
  107. #define USB_EP_CFG_VALID ((uint32_t)0x00000001) /*!<Endpoint Valid \hideinitializer */
  108. #define USB_EP_CFG_TYPE_BULK ((uint32_t)0x00000002) /*!<Endpoint type - bulk \hideinitializer */
  109. #define USB_EP_CFG_TYPE_INT ((uint32_t)0x00000004) /*!<Endpoint type - interrupt \hideinitializer */
  110. #define USB_EP_CFG_TYPE_ISO ((uint32_t)0x00000006) /*!<Endpoint type - isochronous \hideinitializer */
  111. #define USB_EP_CFG_TYPE_MASK ((uint32_t)0x00000006) /*!<Endpoint type mask \hideinitializer */
  112. #define USB_EP_CFG_DIR_OUT ((uint32_t)0x00000000) /*!<OUT endpoint \hideinitializer */
  113. #define USB_EP_CFG_DIR_IN ((uint32_t)0x00000008) /*!<IN endpoint \hideinitializer */
  114. /*@}*/ /* end of group NUC472_442_USBD_EXPORTED_CONSTANTS */
  115. /** @addtogroup NUC472_442_USBD_EXPORTED_STRUCT USBD Exported Struct
  116. @{
  117. */
  118. typedef struct USBD_CMD_STRUCT {
  119. uint8_t bmRequestType;
  120. uint8_t bRequest;
  121. uint16_t wValue;
  122. uint16_t wIndex;
  123. uint16_t wLength;
  124. } S_USBD_CMD_T; /*!<USB Setup Packet Structure */
  125. typedef struct s_usbd_info {
  126. uint8_t *gu8DevDesc; /*!< Device descriptor */
  127. uint8_t *gu8ConfigDesc; /*!< Config descriptor */
  128. uint8_t **gu8StringDesc; /*!< Pointer for USB String Descriptor pointers */
  129. uint8_t *gu8QualDesc; /*!< Qualifier descriptor */
  130. uint8_t *gu8OtherConfigDesc; /*!< Other Speed Config descriptor */
  131. uint8_t **gu8HidReportDesc; /*!< Pointer for HID Report descriptor */
  132. uint32_t *gu32HidReportSize; /*!< Pointer for HID Report descriptor Size */
  133. } S_USBD_INFO_T; /*!<USB Information Structure */
  134. /*@}*/ /* end of group NUC472_442_USBD_EXPORTED_STRUCT */
  135. /// @cond HIDDEN_SYMBOLS
  136. extern uint32_t g_u32EpStallLock;
  137. extern uint8_t g_usbd_Configured;
  138. extern uint8_t g_usbd_ShortPacket;
  139. extern uint8_t g_usbd_CtrlZero;
  140. extern uint8_t g_usbd_UsbAddr;
  141. extern uint8_t g_usbd_EpHalt[];
  142. extern uint32_t volatile g_usbd_DmaDone;
  143. extern uint32_t g_usbd_CtrlInSize;
  144. extern S_USBD_INFO_T gsInfo;
  145. extern S_USBD_CMD_T gUsbCmd;
  146. /// @endcond /* HIDDEN_SYMBOLS */
  147. /** @addtogroup NUC472_442_USBD_EXPORTED_MACROS USBD Exported Macros
  148. @{
  149. */
  150. #define USBD_ENABLE_USB() ((uint32_t)(USBD->PHYCTL |= (USBD_PHYCTL_PHYEN_Msk|USBD_PHYCTL_DPPUEN_Msk))) /*!<Enable USB \hideinitializer */
  151. #define USBD_DISABLE_USB() ((uint32_t)(USBD->PHYCTL &= ~USBD_PHYCTL_DPPUEN_Msk)) /*!<Disable USB \hideinitializer */
  152. #define USBD_ENABLE_PHY() ((uint32_t)(USBD->PHYCTL |= (USBD_PHYCTL_PHYEN_Msk|USBD_PHYCTL_DPPUEN_Msk))) /*!<Enable PHY \hideinitializer */
  153. #define USBD_DISABLE_PHY() ((uint32_t)(USBD->PHYCTL &= ~USBD_PHYCTL_PHYEN_Msk)) /*!<Disable PHY \hideinitializer */
  154. #define USBD_SET_SE0() ((uint32_t)(USBD->PHYCTL &= ~USBD_PHYCTL_DPPUEN_Msk)) /*!<Enable SE0, Force USB PHY Transceiver to Drive SE0 \hideinitializer */
  155. #define USBD_CLR_SE0() ((uint32_t)(USBD->PHYCTL |= USBD_PHYCTL_DPPUEN_Msk)) /*!<Disable SE0 \hideinitializer */
  156. #define USBD_SET_ADDR(addr) (USBD->FADDR = (addr)) /*!<Set USB address \hideinitializer */
  157. #define USBD_GET_ADDR() ((uint32_t)(USBD->FADDR)) /*!<Get USB address \hideinitializer */
  158. #define USBD_ENABLE_USB_INT(intr) (USBD->GINTEN = (intr)) /*!<Enable USB Interrupt \hideinitializer */
  159. #define USBD_ENABLE_BUS_INT(intr) (USBD->BUSINTEN = (intr)) /*!<Enable BUS Interrupt \hideinitializer */
  160. #define USBD_GET_BUS_INT_FLAG() (USBD->BUSINTSTS) /*!<Clear Bus interrupt flag \hideinitializer */
  161. #define USBD_CLR_BUS_INT_FLAG(flag) (USBD->BUSINTSTS = flag) /*!<Clear Bus interrupt flag \hideinitializer */
  162. #define USBD_ENABLE_CEP_INT(intr) (USBD->CEPINTEN = (intr)) /*!<Enable CEP Interrupt \hideinitializer */
  163. #define USBD_CLR_CEP_INT_FLAG(flag) (USBD->CEPINTSTS = flag) /*!<Clear CEP interrupt flag \hideinitializer */
  164. #define USBD_SET_CEP_STATE(flag) (USBD->CEPCTL = flag) /*!<Set CEP state \hideinitializer */
  165. #define USBD_START_CEP_IN(size) (USBD->CEPTXCNT = size) /*!<Start CEP IN Transfer \hideinitializer */
  166. #define USBD_SET_MAX_PAYLOAD(ep, size) (USBD->EP[ep].EPMPS = (size)) /*!<Set EPx Maximum Packet Size \hideinitializer */
  167. #define USBD_ENABLE_EP_INT(ep, intr) (USBD->EP[ep].EPINTEN = (intr)) /*!<Enable EPx Interrupt \hideinitializer */
  168. #define USBD_GET_EP_INT_FLAG(ep) (USBD->EP[ep].EPINTSTS) /*!<Get EPx interrupt flag \hideinitializer */
  169. #define USBD_CLR_EP_INT_FLAG(ep, flag) (USBD->EP[ep].EPINTSTS = (flag)) /*!<Clear EPx interrupt flag \hideinitializer */
  170. #define USBD_SET_DMA_LEN(len) (USBD->DMACNT = len) /*!<Set DMA transfer length \hideinitializer */
  171. #define USBD_SET_DMA_ADDR(addr) (USBD->DMAADDR = addr) /*!<Set DMA transfer address \hideinitializer */
  172. #define USBD_SET_DMA_READ(epnum) (USBD->DMACTL = (USBD->DMACTL & ~USBD_DMACTL_EPNUM_Msk) | USBD_DMACTL_DMARD_Msk | epnum) /*!<Set DMA transfer type to read \hideinitializer */
  173. #define USBD_SET_DMA_WRITE(epnum) (USBD->DMACTL = (USBD->DMACTL & ~(USBD_DMACTL_EPNUM_Msk | USBD_DMACTL_DMARD_Msk)) | epnum) /*!<Set DMA transfer type to write \hideinitializer */
  174. #define USBD_ENABLE_DMA() (USBD->DMACTL |= USBD_DMACTL_DMAEN_Msk) /*!<Enable DMA transfer \hideinitializer */
  175. #define USBD_IS_ATTACHED() ((uint32_t)(USBD->PHYCTL & USBD_PHYCTL_VBUSDET_Msk)) /*!<Check cable connect state \hideinitializer */
  176. /*@}*/ /* end of group NUC472_442_USBD_EXPORTED_MACROS */
  177. /** @addtogroup NUC472_442_USBD_EXPORTED_FUNCTIONS USBD Exported Functions
  178. @{
  179. */
  180. /**
  181. * @brief USBD_memcpy, Copy bytes hardware limitation
  182. * @param[in] u8Dst Destination pointer.
  183. * @param[in] u8Src Source pointer.
  184. * @param[in] i32Size Copy size.
  185. * @retval None.
  186. */
  187. static __INLINE void USBD_MemCopy(uint8_t *u8Dst, uint8_t *u8Src, int32_t i32Size)
  188. {
  189. while (i32Size--) *u8Dst++ = *u8Src++;
  190. }
  191. /**
  192. * @brief USBD_ResetDMA
  193. * @param None
  194. * @retval None.
  195. */
  196. static __INLINE void USBD_ResetDMA(void)
  197. {
  198. USBD->DMACNT = 0;
  199. USBD->DMACTL = 0x80;
  200. USBD->DMACTL = 0x00;
  201. }
  202. /**
  203. * @brief USBD_SetEpBufAddr, Set Endpoint buffer address
  204. * @param[in] u32Ep Endpoint Number
  205. * @param[in] u32Base Buffer Start Address
  206. * @param[in] u32Len Buffer length
  207. * @retval None.
  208. */
  209. static __INLINE void USBD_SetEpBufAddr(uint32_t u32Ep, uint32_t u32Base, uint32_t u32Len)
  210. {
  211. if (u32Ep == CEP) {
  212. USBD->CEPBUFSTART = u32Base;
  213. USBD->CEPBUFEND = u32Base + u32Len - 1;
  214. } else {
  215. USBD->EP[u32Ep].EPBUFSTART = u32Base;
  216. USBD->EP[u32Ep].EPBUFEND = u32Base + u32Len - 1;
  217. }
  218. }
  219. /**
  220. * @brief USBD_ConfigEp, Config Endpoint
  221. * @param[in] u32Ep USB endpoint
  222. * @param[in] u32EpNum Endpoint number
  223. * @param[in] u32EpType Endpoint type
  224. * @param[in] u32EpDir Endpoint direction
  225. * @retval None.
  226. */
  227. static __INLINE void USBD_ConfigEp(uint32_t u32Ep, uint32_t u32EpNum, uint32_t u32EpType, uint32_t u32EpDir)
  228. {
  229. if (u32EpType == USB_EP_CFG_TYPE_BULK)
  230. USBD->EP[u32Ep].EPRSPCTL = (USB_EP_RSPCTL_FLUSH|USB_EP_RSPCTL_MODE_AUTO);
  231. else if (u32EpType == USB_EP_CFG_TYPE_INT)
  232. USBD->EP[u32Ep].EPRSPCTL = (USB_EP_RSPCTL_FLUSH|USB_EP_RSPCTL_MODE_MANUAL);
  233. else if (u32EpType == USB_EP_CFG_TYPE_ISO)
  234. USBD->EP[u32Ep].EPRSPCTL = (USB_EP_RSPCTL_FLUSH|USB_EP_RSPCTL_MODE_FLY);
  235. USBD->EP[u32Ep].EPCFG = (u32EpType|u32EpDir|USB_EP_CFG_VALID|(u32EpNum << 4));
  236. }
  237. /**
  238. * @brief Set USB endpoint stall state
  239. * @param[in] u32Ep The USB endpoint ID.
  240. * @return None
  241. * @details Set USB endpoint stall state for the specified endpoint ID. Endpoint will respond STALL token automatically.
  242. */
  243. static __INLINE void USBD_SetEpStall(uint32_t u32Ep)
  244. {
  245. if (u32Ep == CEP)
  246. USBD_SET_CEP_STATE(USB_CEPCTL_STALL);
  247. else {
  248. USBD->EP[u32Ep].EPRSPCTL = USBD->EP[u32Ep].EPRSPCTL & 0xf7 | USB_EP_RSPCTL_HALT;
  249. }
  250. }
  251. /**
  252. * @brief Set USB endpoint stall state
  253. *
  254. * @param[in] u32EpNum USB endpoint
  255. * @return None
  256. *
  257. * @details Set USB endpoint stall state, endpoint will return STALL token.
  258. */
  259. static __INLINE void USBD_SetStall(uint32_t u32EpNum)
  260. {
  261. int i;
  262. if (u32EpNum == 0)
  263. USBD_SET_CEP_STATE(USB_CEPCTL_STALL);
  264. else {
  265. for (i=0; i<USBD_MAX_EP; i++) {
  266. if (((USBD->EP[i].EPCFG & 0xf0) >> 4) == u32EpNum)
  267. {
  268. USBD->EP[i].EPRSPCTL = USBD->EP[i].EPRSPCTL & 0xf7 | USB_EP_RSPCTL_HALT;
  269. }
  270. }
  271. }
  272. }
  273. /**
  274. * @brief Clear USB endpoint stall state
  275. * @param[in] u32Ep The USB endpoint ID.
  276. * @return None
  277. * @details Clear USB endpoint stall state for the specified endpoint ID. Endpoint will respond ACK/NAK token.
  278. */
  279. static __INLINE void USBD_ClearEpStall(uint32_t u32Ep)
  280. {
  281. USBD->EP[u32Ep].EPRSPCTL = USB_EP_RSPCTL_TOGGLE;
  282. }
  283. /**
  284. * @brief Clear USB endpoint stall state
  285. *
  286. * @param[in] u32EpNum USB endpoint
  287. * @return None
  288. *
  289. * @details Clear USB endpoint stall state, endpoint will return ACK/NAK token.
  290. */
  291. static __INLINE void USBD_ClearStall(uint32_t u32EpNum)
  292. {
  293. int i;
  294. for (i=0; i<USBD_MAX_EP; i++) {
  295. if (((USBD->EP[i].EPCFG & 0xf0) >> 4) == u32EpNum)
  296. {
  297. USBD->EP[i].EPRSPCTL = USB_EP_RSPCTL_TOGGLE;
  298. }
  299. }
  300. }
  301. /**
  302. * @brief Get USB endpoint stall state
  303. * @param[in] u32Ep The USB endpoint ID.
  304. * @retval 0 USB endpoint is not stalled.
  305. * @retval Others USB endpoint is stalled.
  306. * @details Get USB endpoint stall state of the specified endpoint ID.
  307. */
  308. static __INLINE uint32_t USBD_GetEpStall(uint32_t u32Ep)
  309. {
  310. return (USBD->EP[u32Ep].EPRSPCTL & USB_EP_RSPCTL_HALT);
  311. }
  312. /**
  313. * @brief Get USB endpoint stall state
  314. *
  315. * @param[in] u32EpNum USB endpoint
  316. * @retval 0: USB endpoint is not stalled.
  317. * @retval non-0: USB endpoint is stalled.
  318. *
  319. * @details Get USB endpoint stall state.
  320. */
  321. static __INLINE uint32_t USBD_GetStall(uint32_t u32EpNum)
  322. {
  323. int i;
  324. for (i=0; i<USBD_MAX_EP; i++) {
  325. if (((USBD->EP[i].EPCFG & 0xf0) >> 4) == u32EpNum)
  326. {
  327. return (USBD->EP[i].EPRSPCTL & USB_EP_RSPCTL_HALT);
  328. }
  329. }
  330. return 0;
  331. }
  332. /*-------------------------------------------------------------------------------------------*/
  333. typedef void (*VENDOR_REQ)(void); /*!<USB Vendor request callback function */
  334. typedef void (*CLASS_REQ)(void); /*!<USB Class request callback function */
  335. typedef void (*SET_INTERFACE_REQ)(uint32_t u32AltInterface); /*!<USB Standard request "Set Interface" callback function */
  336. void USBD_Open(S_USBD_INFO_T *param, CLASS_REQ pfnClassReq, SET_INTERFACE_REQ pfnSetInterface);
  337. void USBD_Start(void);
  338. void USBD_ProcessSetupPacket(void);
  339. void USBD_StandardRequest(void);
  340. void USBD_UpdateDeviceState(void);
  341. void USBD_PrepareCtrlIn(uint8_t *pu8Buf, uint32_t u32Size);
  342. void USBD_CtrlIn(void);
  343. void USBD_CtrlOut(uint8_t *pu8Buf, uint32_t u32Size);
  344. void USBD_SwReset(void);
  345. void USBD_SetVendorRequest(VENDOR_REQ pfnVendorReq);
  346. /*@}*/ /* end of group NUC472_442_USBD_EXPORTED_FUNCTIONS */
  347. /*@}*/ /* end of group NUC472_442_USBD_Driver */
  348. /*@}*/ /* end of group NUC472_442_Device_Driver */
  349. #ifdef __cplusplus
  350. }
  351. #endif
  352. #endif //__USBD_H__
  353. /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/