nu_hsusbd.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /**************************************************************************//**
  2. * @file nu_hsusbd.h
  3. * @version V1.00
  4. * @brief M480 HSUSBD driver header file
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. * @copyright (C) 2016-2020 Nuvoton Technology Corp. All rights reserved.
  8. *****************************************************************************/
  9. #ifndef __NU_HSUSBD_H__
  10. #define __NU_HSUSBD_H__
  11. #ifdef __cplusplus
  12. extern "C"
  13. {
  14. #endif
  15. /** @addtogroup Standard_Driver Standard Driver
  16. @{
  17. */
  18. /** @addtogroup HSUSBD_Driver HSUSBD Driver
  19. @{
  20. */
  21. /** @addtogroup HSUSBD_EXPORTED_CONSTANTS HSUSBD Exported Constants
  22. @{
  23. */
  24. /** @cond HIDDEN_SYMBOLS */
  25. #define HSUSBD_MAX_EP 12ul
  26. #define Maximum(a,b) (a)>(b) ? (a) : (b)
  27. #define Minimum(a,b) (((a)<(b)) ? (a) : (b))
  28. #define CEP 0xfful /*!< Control Endpoint \hideinitializer */
  29. #define EPA 0ul /*!< Endpoint A \hideinitializer */
  30. #define EPB 1ul /*!< Endpoint B \hideinitializer */
  31. #define EPC 2ul /*!< Endpoint C \hideinitializer */
  32. #define EPD 3ul /*!< Endpoint D \hideinitializer */
  33. #define EPE 4ul /*!< Endpoint E \hideinitializer */
  34. #define EPF 5ul /*!< Endpoint F \hideinitializer */
  35. #define EPG 6ul /*!< Endpoint G \hideinitializer */
  36. #define EPH 7ul /*!< Endpoint H \hideinitializer */
  37. #define EPI 8ul /*!< Endpoint I \hideinitializer */
  38. #define EPJ 9ul /*!< Endpoint J \hideinitializer */
  39. #define EPK 10ul /*!< Endpoint K \hideinitializer */
  40. #define EPL 11ul /*!< Endpoint L \hideinitializer */
  41. /** @endcond HIDDEN_SYMBOLS */
  42. /********************* Bit definition of CEPCTL register **********************/
  43. #define HSUSBD_CEPCTL_NAKCLR ((uint32_t)0x00000000ul) /*!<NAK clear \hideinitializer */
  44. #define HSUSBD_CEPCTL_STALL ((uint32_t)0x00000002ul) /*!<Stall \hideinitializer */
  45. #define HSUSBD_CEPCTL_ZEROLEN ((uint32_t)0x00000004ul) /*!<Zero length packet \hideinitializer */
  46. #define HSUSBD_CEPCTL_FLUSH ((uint32_t)0x00000008ul) /*!<CEP flush \hideinitializer */
  47. /********************* Bit definition of EPxRSPCTL register **********************/
  48. #define HSUSBD_EP_RSPCTL_FLUSH ((uint32_t)0x00000001ul) /*!<Buffer Flush \hideinitializer */
  49. #define HSUSBD_EP_RSPCTL_MODE_AUTO ((uint32_t)0x00000000ul) /*!<Auto-Validate Mode \hideinitializer */
  50. #define HSUSBD_EP_RSPCTL_MODE_MANUAL ((uint32_t)0x00000002ul) /*!<Manual-Validate Mode \hideinitializer */
  51. #define HSUSBD_EP_RSPCTL_MODE_FLY ((uint32_t)0x00000004ul) /*!<Fly Mode \hideinitializer */
  52. #define HSUSBD_EP_RSPCTL_MODE_MASK ((uint32_t)0x00000006ul) /*!<Mode Mask \hideinitializer */
  53. #define HSUSBD_EP_RSPCTL_TOGGLE ((uint32_t)0x00000008ul) /*!<Clear Toggle bit \hideinitializer */
  54. #define HSUSBD_EP_RSPCTL_HALT ((uint32_t)0x00000010ul) /*!<Endpoint halt \hideinitializer */
  55. #define HSUSBD_EP_RSPCTL_ZEROLEN ((uint32_t)0x00000020ul) /*!<Zero length packet IN \hideinitializer */
  56. #define HSUSBD_EP_RSPCTL_SHORTTXEN ((uint32_t)0x00000040ul) /*!<Packet end \hideinitializer */
  57. #define HSUSBD_EP_RSPCTL_DISBUF ((uint32_t)0x00000080ul) /*!<Disable buffer \hideinitializer */
  58. /********************* Bit definition of EPxCFG register **********************/
  59. #define HSUSBD_EP_CFG_VALID ((uint32_t)0x00000001ul) /*!<Endpoint Valid \hideinitializer */
  60. #define HSUSBD_EP_CFG_TYPE_BULK ((uint32_t)0x00000002ul) /*!<Endpoint type - bulk \hideinitializer */
  61. #define HSUSBD_EP_CFG_TYPE_INT ((uint32_t)0x00000004ul) /*!<Endpoint type - interrupt \hideinitializer */
  62. #define HSUSBD_EP_CFG_TYPE_ISO ((uint32_t)0x00000006ul) /*!<Endpoint type - isochronous \hideinitializer */
  63. #define HSUSBD_EP_CFG_TYPE_MASK ((uint32_t)0x00000006ul) /*!<Endpoint type mask \hideinitializer */
  64. #define HSUSBD_EP_CFG_DIR_OUT ((uint32_t)0x00000000ul) /*!<OUT endpoint \hideinitializer */
  65. #define HSUSBD_EP_CFG_DIR_IN ((uint32_t)0x00000008ul) /*!<IN endpoint \hideinitializer */
  66. /*@}*/ /* end of group HSUSBD_EXPORTED_CONSTANTS */
  67. /** @addtogroup HSUSBD_EXPORTED_STRUCT HSUSBD Exported Struct
  68. @{
  69. */
  70. typedef struct HSUSBD_CMD_STRUCT
  71. {
  72. uint8_t bmRequestType;
  73. uint8_t bRequest;
  74. uint16_t wValue;
  75. uint16_t wIndex;
  76. uint16_t wLength;
  77. } S_HSUSBD_CMD_T; /*!<USB Setup Packet Structure */
  78. typedef struct s_hsusbd_info
  79. {
  80. uint8_t *gu8DevDesc; /*!< Device descriptor */
  81. uint8_t *gu8ConfigDesc; /*!< Config descriptor */
  82. uint8_t **gu8StringDesc; /*!< Pointer for USB String Descriptor pointers */
  83. uint8_t *gu8QualDesc; /*!< Qualifier descriptor */
  84. uint8_t *gu8FullConfigDesc; /*!< Full Speed Config descriptor */
  85. uint8_t *gu8HSOtherConfigDesc; /*!< Other Speed Config descriptor */
  86. uint8_t *gu8FSOtherConfigDesc; /*!< Other Speed Config descriptor */
  87. uint8_t **gu8HidReportDesc; /*!< Pointer for HID Report descriptor */
  88. uint32_t *gu32HidReportSize; /*!< Pointer for HID Report descriptor Size */
  89. uint32_t *gu32ConfigHidDescIdx; /*!< Pointer for HID Descriptor start index */
  90. } S_HSUSBD_INFO_T; /*!<USB Information Structure */
  91. /*@}*/ /* end of group HSUSBD_EXPORTED_STRUCT */
  92. /** @cond HIDDEN_SYMBOLS */
  93. extern uint32_t g_u32HsEpStallLock;
  94. extern uint8_t volatile g_hsusbd_Configured;
  95. extern uint8_t g_hsusbd_ShortPacket;
  96. extern uint8_t g_hsusbd_CtrlZero;
  97. extern uint8_t g_hsusbd_UsbAddr;
  98. extern uint32_t volatile g_hsusbd_DmaDone;
  99. extern uint32_t g_hsusbd_CtrlInSize;
  100. extern S_HSUSBD_INFO_T gsHSInfo;
  101. extern S_HSUSBD_CMD_T gUsbCmd;
  102. /** @endcond HIDDEN_SYMBOLS */
  103. /** @addtogroup HSUSBD_EXPORTED_FUNCTIONS HSUSBD Exported Functions
  104. @{
  105. */
  106. #define HSUSBD_ENABLE_USB() ((uint32_t)(HSUSBD->PHYCTL |= (HSUSBD_PHYCTL_PHYEN_Msk|HSUSBD_PHYCTL_DPPUEN_Msk))) /*!<Enable USB \hideinitializer */
  107. #define HSUSBD_DISABLE_USB() ((uint32_t)(HSUSBD->PHYCTL &= ~HSUSBD_PHYCTL_DPPUEN_Msk)) /*!<Disable USB \hideinitializer */
  108. #define HSUSBD_ENABLE_PHY() ((uint32_t)(HSUSBD->PHYCTL |= HSUSBD_PHYCTL_PHYEN_Msk)) /*!<Enable PHY \hideinitializer */
  109. #define HSUSBD_DISABLE_PHY() ((uint32_t)(HSUSBD->PHYCTL &= ~HSUSBD_PHYCTL_PHYEN_Msk)) /*!<Disable PHY \hideinitializer */
  110. #define HSUSBD_SET_SE0() ((uint32_t)(HSUSBD->PHYCTL &= ~HSUSBD_PHYCTL_DPPUEN_Msk)) /*!<Enable SE0, Force USB PHY Transceiver to Drive SE0 \hideinitializer */
  111. #define HSUSBD_CLR_SE0() ((uint32_t)(HSUSBD->PHYCTL |= HSUSBD_PHYCTL_DPPUEN_Msk)) /*!<Disable SE0 \hideinitializer */
  112. #define HSUSBD_SET_ADDR(addr) (HSUSBD->FADDR = (addr)) /*!<Set USB address \hideinitializer */
  113. #define HSUSBD_GET_ADDR() ((uint32_t)(HSUSBD->FADDR)) /*!<Get USB address \hideinitializer */
  114. #define HSUSBD_ENABLE_USB_INT(intr) (HSUSBD->GINTEN = (intr)) /*!<Enable USB Interrupt \hideinitializer */
  115. #define HSUSBD_ENABLE_BUS_INT(intr) (HSUSBD->BUSINTEN = (intr)) /*!<Enable BUS Interrupt \hideinitializer */
  116. #define HSUSBD_GET_BUS_INT_FLAG() (HSUSBD->BUSINTSTS) /*!<Get Bus interrupt flag \hideinitializer */
  117. #define HSUSBD_CLR_BUS_INT_FLAG(flag) (HSUSBD->BUSINTSTS = (flag)) /*!<Clear Bus interrupt flag \hideinitializer */
  118. #define HSUSBD_ENABLE_CEP_INT(intr) (HSUSBD->CEPINTEN = (intr)) /*!<Enable CEP Interrupt \hideinitializer */
  119. #define HSUSBD_CLR_CEP_INT_FLAG(flag) (HSUSBD->CEPINTSTS = (flag)) /*!<Clear CEP interrupt flag \hideinitializer */
  120. #define HSUSBD_SET_CEP_STATE(flag) (HSUSBD->CEPCTL = (flag)) /*!<Set CEP state \hideinitializer */
  121. #define HSUSBD_START_CEP_IN(size) (HSUSBD->CEPTXCNT = (size)) /*!<Start CEP IN Transfer \hideinitializer */
  122. #define HSUSBD_SET_MAX_PAYLOAD(ep, size) (HSUSBD->EP[(ep)].EPMPS = (size)) /*!<Set EPx Maximum Packet Size \hideinitializer */
  123. #define HSUSBD_ENABLE_EP_INT(ep, intr) (HSUSBD->EP[(ep)].EPINTEN = (intr)) /*!<Enable EPx Interrupt \hideinitializer */
  124. #define HSUSBD_GET_EP_INT_FLAG(ep) (HSUSBD->EP[(ep)].EPINTSTS) /*!<Get EPx interrupt flag \hideinitializer */
  125. #define HSUSBD_CLR_EP_INT_FLAG(ep, flag) (HSUSBD->EP[(ep)].EPINTSTS = (flag)) /*!<Clear EPx interrupt flag \hideinitializer */
  126. #define HSUSBD_SET_DMA_LEN(len) (HSUSBD->DMACNT = (len)) /*!<Set DMA transfer length \hideinitializer */
  127. #define HSUSBD_SET_DMA_ADDR(addr) (HSUSBD->DMAADDR = (addr)) /*!<Set DMA transfer address \hideinitializer */
  128. #define HSUSBD_SET_DMA_READ(epnum) (HSUSBD->DMACTL = (HSUSBD->DMACTL & ~HSUSBD_DMACTL_EPNUM_Msk) | HSUSBD_DMACTL_DMARD_Msk | (epnum) | 0x100) /*!<Set DMA transfer type to read \hideinitializer */
  129. #define HSUSBD_SET_DMA_WRITE(epnum) (HSUSBD->DMACTL = (HSUSBD->DMACTL & ~(HSUSBD_DMACTL_EPNUM_Msk | HSUSBD_DMACTL_DMARD_Msk | 0x100)) | (epnum)) /*!<Set DMA transfer type to write \hideinitializer */
  130. #define HSUSBD_ENABLE_DMA() (HSUSBD->DMACTL |= HSUSBD_DMACTL_DMAEN_Msk) /*!<Enable DMA transfer \hideinitializer */
  131. #define HSUSBD_IS_ATTACHED() ((uint32_t)(HSUSBD->PHYCTL & HSUSBD_PHYCTL_VBUSDET_Msk)) /*!<Check cable connect state \hideinitializer */
  132. /**
  133. * @brief HSUSBD_memcpy, Copy bytes hardware limitation
  134. * @param[in] u8Dst Destination pointer.
  135. * @param[in] u8Src Source pointer.
  136. * @param[in] u32Size Copy size.
  137. * @retval None.
  138. */
  139. __STATIC_INLINE void HSUSBD_MemCopy(uint8_t u8Dst[], uint8_t u8Src[], uint32_t u32Size)
  140. {
  141. uint32_t i = 0ul;
  142. while (u32Size--)
  143. {
  144. u8Dst[i] = u8Src[i];
  145. i++;
  146. }
  147. }
  148. /**
  149. * @brief HSUSBD_ResetDMA
  150. * @param None
  151. * @retval None.
  152. */
  153. __STATIC_INLINE void HSUSBD_ResetDMA(void)
  154. {
  155. HSUSBD->DMACNT = 0ul;
  156. HSUSBD->DMACTL = 0x80ul;
  157. HSUSBD->DMACTL = 0x00ul;
  158. }
  159. /**
  160. * @brief HSUSBD_SetEpBufAddr, Set Endpoint buffer address
  161. * @param[in] u32Ep Endpoint Number
  162. * @param[in] u32Base Buffer Start Address
  163. * @param[in] u32Len Buffer length
  164. * @retval None.
  165. */
  166. __STATIC_INLINE void HSUSBD_SetEpBufAddr(uint32_t u32Ep, uint32_t u32Base, uint32_t u32Len)
  167. {
  168. if (u32Ep == CEP)
  169. {
  170. HSUSBD->CEPBUFST = u32Base;
  171. HSUSBD->CEPBUFEND = u32Base + u32Len - 1ul;
  172. }
  173. else
  174. {
  175. HSUSBD->EP[u32Ep].EPBUFST = u32Base;
  176. HSUSBD->EP[u32Ep].EPBUFEND = u32Base + u32Len - 1ul;
  177. }
  178. }
  179. /**
  180. * @brief HSUSBD_ConfigEp, Config Endpoint
  181. * @param[in] u32Ep USB endpoint
  182. * @param[in] u32EpNum Endpoint number
  183. * @param[in] u32EpType Endpoint type
  184. * @param[in] u32EpDir Endpoint direction
  185. * @retval None.
  186. */
  187. __STATIC_INLINE void HSUSBD_ConfigEp(uint32_t u32Ep, uint32_t u32EpNum, uint32_t u32EpType, uint32_t u32EpDir)
  188. {
  189. if (u32EpType == HSUSBD_EP_CFG_TYPE_BULK)
  190. {
  191. HSUSBD->EP[u32Ep].EPRSPCTL = (HSUSBD_EP_RSPCTL_FLUSH|HSUSBD_EP_RSPCTL_MODE_AUTO);
  192. }
  193. else if (u32EpType == HSUSBD_EP_CFG_TYPE_INT)
  194. {
  195. HSUSBD->EP[u32Ep].EPRSPCTL = (HSUSBD_EP_RSPCTL_FLUSH|HSUSBD_EP_RSPCTL_MODE_MANUAL);
  196. }
  197. else if (u32EpType == HSUSBD_EP_CFG_TYPE_ISO)
  198. {
  199. HSUSBD->EP[u32Ep].EPRSPCTL = (HSUSBD_EP_RSPCTL_FLUSH|HSUSBD_EP_RSPCTL_MODE_FLY);
  200. }
  201. HSUSBD->EP[u32Ep].EPCFG = (u32EpType|u32EpDir|HSUSBD_EP_CFG_VALID|(u32EpNum << 4));
  202. }
  203. /**
  204. * @brief Set USB endpoint stall state
  205. * @param[in] u32Ep The USB endpoint ID.
  206. * @return None
  207. * @details Set USB endpoint stall state for the specified endpoint ID. Endpoint will respond STALL token automatically.
  208. */
  209. __STATIC_INLINE void HSUSBD_SetEpStall(uint32_t u32Ep)
  210. {
  211. if (u32Ep == CEP)
  212. {
  213. HSUSBD_SET_CEP_STATE(HSUSBD_CEPCTL_STALL);
  214. }
  215. else
  216. {
  217. HSUSBD->EP[u32Ep].EPRSPCTL = (HSUSBD->EP[u32Ep].EPRSPCTL & 0xf7ul) | HSUSBD_EP_RSPCTL_HALT;
  218. }
  219. }
  220. /**
  221. * @brief Set USB endpoint stall state
  222. *
  223. * @param[in] u32EpNum USB endpoint
  224. * @return None
  225. *
  226. * @details Set USB endpoint stall state, endpoint will return STALL token.
  227. */
  228. __STATIC_INLINE void HSUSBD_SetStall(uint32_t u32EpNum)
  229. {
  230. uint32_t i;
  231. if (u32EpNum == 0ul)
  232. {
  233. HSUSBD_SET_CEP_STATE(HSUSBD_CEPCTL_STALL);
  234. }
  235. else
  236. {
  237. for (i=0ul; i<HSUSBD_MAX_EP; i++)
  238. {
  239. if (((HSUSBD->EP[i].EPCFG & 0xf0ul) >> 4) == u32EpNum)
  240. {
  241. HSUSBD->EP[i].EPRSPCTL = (HSUSBD->EP[i].EPRSPCTL & 0xf7ul) | HSUSBD_EP_RSPCTL_HALT;
  242. }
  243. }
  244. }
  245. }
  246. /**
  247. * @brief Clear USB endpoint stall state
  248. * @param[in] u32Ep The USB endpoint ID.
  249. * @return None
  250. * @details Clear USB endpoint stall state for the specified endpoint ID. Endpoint will respond ACK/NAK token.
  251. */
  252. __STATIC_INLINE void HSUSBD_ClearEpStall(uint32_t u32Ep)
  253. {
  254. HSUSBD->EP[u32Ep].EPRSPCTL = HSUSBD_EP_RSPCTL_TOGGLE;
  255. }
  256. /**
  257. * @brief Clear USB endpoint stall state
  258. *
  259. * @param[in] u32EpNum USB endpoint
  260. * @return None
  261. *
  262. * @details Clear USB endpoint stall state, endpoint will return ACK/NAK token.
  263. */
  264. __STATIC_INLINE void HSUSBD_ClearStall(uint32_t u32EpNum)
  265. {
  266. uint32_t i;
  267. for (i=0ul; i<HSUSBD_MAX_EP; i++)
  268. {
  269. if (((HSUSBD->EP[i].EPCFG & 0xf0ul) >> 4) == u32EpNum)
  270. {
  271. HSUSBD->EP[i].EPRSPCTL = HSUSBD_EP_RSPCTL_TOGGLE;
  272. }
  273. }
  274. }
  275. /**
  276. * @brief Get USB endpoint stall state
  277. * @param[in] u32Ep The USB endpoint ID.
  278. * @retval 0 USB endpoint is not stalled.
  279. * @retval Others USB endpoint is stalled.
  280. * @details Get USB endpoint stall state of the specified endpoint ID.
  281. */
  282. __STATIC_INLINE uint32_t HSUSBD_GetEpStall(uint32_t u32Ep)
  283. {
  284. return (HSUSBD->EP[u32Ep].EPRSPCTL & HSUSBD_EP_RSPCTL_HALT);
  285. }
  286. /**
  287. * @brief Get USB endpoint stall state
  288. *
  289. * @param[in] u32EpNum USB endpoint
  290. * @retval 0: USB endpoint is not stalled.
  291. * @retval non-0: USB endpoint is stalled.
  292. *
  293. * @details Get USB endpoint stall state.
  294. */
  295. __STATIC_INLINE uint32_t HSUSBD_GetStall(uint32_t u32EpNum)
  296. {
  297. uint32_t i;
  298. uint32_t val = 0ul;
  299. for (i=0ul; i<HSUSBD_MAX_EP; i++)
  300. {
  301. if (((HSUSBD->EP[i].EPCFG & 0xf0ul) >> 4) == u32EpNum)
  302. {
  303. val = (HSUSBD->EP[i].EPRSPCTL & HSUSBD_EP_RSPCTL_HALT);
  304. break;
  305. }
  306. }
  307. return val;
  308. }
  309. /*-------------------------------------------------------------------------------------------*/
  310. typedef void (*HSUSBD_VENDOR_REQ)(void); /*!<USB Vendor request callback function */
  311. typedef void (*HSUSBD_CLASS_REQ)(void); /*!<USB Class request callback function */
  312. typedef void (*HSUSBD_SET_INTERFACE_REQ)(uint32_t u32AltInterface); /*!<USB Standard request "Set Interface" callback function */
  313. void HSUSBD_Open(S_HSUSBD_INFO_T *param, HSUSBD_CLASS_REQ pfnClassReq, HSUSBD_SET_INTERFACE_REQ pfnSetInterface);
  314. void HSUSBD_Start(void);
  315. void HSUSBD_ProcessSetupPacket(void);
  316. void HSUSBD_StandardRequest(void);
  317. void HSUSBD_UpdateDeviceState(void);
  318. void HSUSBD_PrepareCtrlIn(uint8_t pu8Buf[], uint32_t u32Size);
  319. void HSUSBD_CtrlIn(void);
  320. void HSUSBD_CtrlOut(uint8_t pu8Buf[], uint32_t u32Size);
  321. void HSUSBD_SwReset(void);
  322. void HSUSBD_SetVendorRequest(HSUSBD_VENDOR_REQ pfnVendorReq);
  323. /*@}*/ /* end of group HSUSBD_EXPORTED_FUNCTIONS */
  324. /*@}*/ /* end of group HSUSBD_Driver */
  325. /*@}*/ /* end of group Standard_Driver */
  326. #ifdef __cplusplus
  327. }
  328. #endif
  329. #endif /*__NU_HSUSBD_H__ */
  330. /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/