nu_usbd.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. /**************************************************************************//**
  2. * @file nu_usbd.h
  3. * @version V1.00
  4. * @brief M480 series USB 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_USBD_H__
  10. #define __NU_USBD_H__
  11. #ifdef __cplusplus
  12. extern "C"
  13. {
  14. #endif
  15. /** @addtogroup Standard_Driver Standard Driver
  16. @{
  17. */
  18. /** @addtogroup USBD_Driver USBD Driver
  19. @{
  20. */
  21. /** @addtogroup USBD_EXPORTED_STRUCT USBD Exported Struct
  22. @{
  23. */
  24. typedef struct s_usbd_info
  25. {
  26. uint8_t *gu8DevDesc; /*!< Pointer for USB Device Descriptor */
  27. uint8_t *gu8ConfigDesc; /*!< Pointer for USB Configuration Descriptor */
  28. uint8_t **gu8StringDesc; /*!< Pointer for USB String Descriptor pointers */
  29. uint8_t **gu8HidReportDesc; /*!< Pointer for USB HID Report Descriptor */
  30. uint8_t *gu8BosDesc; /*!< Pointer for USB BOS Descriptor */
  31. uint32_t *gu32HidReportSize; /*!< Pointer for HID Report descriptor Size */
  32. uint32_t *gu32ConfigHidDescIdx; /*!< Pointer for HID Descriptor start index */
  33. } S_USBD_INFO_T; /*!< Device description structure */
  34. extern const S_USBD_INFO_T gsInfo;
  35. /*@}*/ /* end of group USBD_EXPORTED_STRUCT */
  36. /** @addtogroup USBD_EXPORTED_CONSTANTS USBD Exported Constants
  37. @{
  38. */
  39. #define USBD_BUF_BASE (USBD_BASE+0x100ul) /*!< USBD buffer base address \hideinitializer */
  40. #define USBD_MAX_EP 12ul /*!< Total EP number \hideinitializer */
  41. #define EP0 0ul /*!< Endpoint 0 \hideinitializer */
  42. #define EP1 1ul /*!< Endpoint 1 \hideinitializer */
  43. #define EP2 2ul /*!< Endpoint 2 \hideinitializer */
  44. #define EP3 3ul /*!< Endpoint 3 \hideinitializer */
  45. #define EP4 4ul /*!< Endpoint 4 \hideinitializer */
  46. #define EP5 5ul /*!< Endpoint 5 \hideinitializer */
  47. #define EP6 6ul /*!< Endpoint 6 \hideinitializer */
  48. #define EP7 7ul /*!< Endpoint 7 \hideinitializer */
  49. #define EP8 8ul /*!< Endpoint 8 \hideinitializer */
  50. #define EP9 9ul /*!< Endpoint 9 \hideinitializer */
  51. #define EP10 10ul /*!< Endpoint 10 \hideinitializer */
  52. #define EP11 11ul /*!< Endpoint 11 \hideinitializer */
  53. /** @cond HIDDEN_SYMBOLS */
  54. /* USB Request Type */
  55. #define REQ_STANDARD 0x00ul
  56. #define REQ_CLASS 0x20ul
  57. #define REQ_VENDOR 0x40ul
  58. /* USB Standard Request */
  59. #define GET_STATUS 0x00ul
  60. #define CLEAR_FEATURE 0x01ul
  61. #define SET_FEATURE 0x03ul
  62. #define SET_ADDRESS 0x05ul
  63. #define GET_DESCRIPTOR 0x06ul
  64. #define SET_DESCRIPTOR 0x07ul
  65. #define GET_CONFIGURATION 0x08ul
  66. #define SET_CONFIGURATION 0x09ul
  67. #define GET_INTERFACE 0x0Aul
  68. #define SET_INTERFACE 0x0Bul
  69. #define SYNC_FRAME 0x0Cul
  70. /* USB Descriptor Type */
  71. #define DESC_DEVICE 0x01ul
  72. #define DESC_CONFIG 0x02ul
  73. #define DESC_STRING 0x03ul
  74. #define DESC_INTERFACE 0x04ul
  75. #define DESC_ENDPOINT 0x05ul
  76. #define DESC_QUALIFIER 0x06ul
  77. #define DESC_OTHERSPEED 0x07ul
  78. #define DESC_IFPOWER 0x08ul
  79. #define DESC_OTG 0x09ul
  80. #define DESC_BOS 0x0Ful
  81. #define DESC_CAPABILITY 0x10ul
  82. /* USB Device Capability Type */
  83. #define CAP_WIRELESS 0x01ul
  84. #define CAP_USB20_EXT 0x02ul
  85. /* USB HID Descriptor Type */
  86. #define DESC_HID 0x21ul
  87. #define DESC_HID_RPT 0x22ul
  88. /* USB Descriptor Length */
  89. #define LEN_DEVICE 18ul
  90. #define LEN_QUALIFIER 10ul
  91. #define LEN_CONFIG 9ul
  92. #define LEN_INTERFACE 9ul
  93. #define LEN_ENDPOINT 7ul
  94. #define LEN_OTG 5ul
  95. #define LEN_BOS 5ul
  96. #define LEN_HID 9ul
  97. #define LEN_CCID 0x36ul
  98. #define LEN_BOSCAP 7ul
  99. /* USB Endpoint Type */
  100. #define EP_ISO 0x01
  101. #define EP_BULK 0x02
  102. #define EP_INT 0x03
  103. #define EP_INPUT 0x80
  104. #define EP_OUTPUT 0x00
  105. /* USB Feature Selector */
  106. #define FEATURE_DEVICE_REMOTE_WAKEUP 0x01ul
  107. #define FEATURE_ENDPOINT_HALT 0x00ul
  108. /** @endcond HIDDEN_SYMBOLS */
  109. /******************************************************************************/
  110. /* USB Specific Macros */
  111. /******************************************************************************/
  112. #define USBD_WAKEUP_EN USBD_INTEN_WKEN_Msk /*!< USB Wake-up Enable \hideinitializer */
  113. #define USBD_DRVSE0 USBD_SE0_SE0_Msk /*!< Drive SE0 \hideinitializer */
  114. #define USBD_DPPU_EN USBD_ATTR_DPPUEN_Msk /*!< USB D+ Pull-up Enable \hideinitializer */
  115. #define USBD_PWRDN USBD_ATTR_PWRDN_Msk /*!< PHY Turn-On \hideinitializer */
  116. #define USBD_PHY_EN USBD_ATTR_PHYEN_Msk /*!< PHY Enable \hideinitializer */
  117. #define USBD_USB_EN USBD_ATTR_USBEN_Msk /*!< USB Enable \hideinitializer */
  118. #define USBD_INT_BUS USBD_INTEN_BUSIEN_Msk /*!< USB Bus Event Interrupt \hideinitializer */
  119. #define USBD_INT_USB USBD_INTEN_USBIEN_Msk /*!< USB Event Interrupt \hideinitializer */
  120. #define USBD_INT_FLDET USBD_INTEN_VBDETIEN_Msk /*!< USB VBUS Detection Interrupt \hideinitializer */
  121. #define USBD_INT_WAKEUP (USBD_INTEN_NEVWKIEN_Msk | USBD_INTEN_WKEN_Msk) /*!< USB No-Event-Wake-Up Interrupt \hideinitializer */
  122. #define USBD_INTSTS_WAKEUP USBD_INTSTS_NEVWKIF_Msk /*!< USB No-Event-Wake-Up Interrupt Status \hideinitializer */
  123. #define USBD_INTSTS_FLDET USBD_INTSTS_VBDETIF_Msk /*!< USB Float Detect Interrupt Status \hideinitializer */
  124. #define USBD_INTSTS_BUS USBD_INTSTS_BUSIF_Msk /*!< USB Bus Event Interrupt Status \hideinitializer */
  125. #define USBD_INTSTS_USB USBD_INTSTS_USBIF_Msk /*!< USB Event Interrupt Status \hideinitializer */
  126. #define USBD_INTSTS_SETUP USBD_INTSTS_SETUP_Msk /*!< USB Setup Event \hideinitializer */
  127. #define USBD_INTSTS_EP0 USBD_INTSTS_EPEVT0_Msk /*!< USB Endpoint 0 Event \hideinitializer */
  128. #define USBD_INTSTS_EP1 USBD_INTSTS_EPEVT1_Msk /*!< USB Endpoint 1 Event \hideinitializer */
  129. #define USBD_INTSTS_EP2 USBD_INTSTS_EPEVT2_Msk /*!< USB Endpoint 2 Event \hideinitializer */
  130. #define USBD_INTSTS_EP3 USBD_INTSTS_EPEVT3_Msk /*!< USB Endpoint 3 Event \hideinitializer */
  131. #define USBD_INTSTS_EP4 USBD_INTSTS_EPEVT4_Msk /*!< USB Endpoint 4 Event \hideinitializer */
  132. #define USBD_INTSTS_EP5 USBD_INTSTS_EPEVT5_Msk /*!< USB Endpoint 5 Event \hideinitializer */
  133. #define USBD_INTSTS_EP6 USBD_INTSTS_EPEVT6_Msk /*!< USB Endpoint 6 Event \hideinitializer */
  134. #define USBD_INTSTS_EP7 USBD_INTSTS_EPEVT7_Msk /*!< USB Endpoint 7 Event \hideinitializer */
  135. #define USBD_INTSTS_EP8 USBD_INTSTS_EPEVT8_Msk /*!< USB Endpoint 8 Event \hideinitializer */
  136. #define USBD_INTSTS_EP9 USBD_INTSTS_EPEVT9_Msk /*!< USB Endpoint 9 Event \hideinitializer */
  137. #define USBD_INTSTS_EP10 USBD_INTSTS_EPEVT10_Msk /*!< USB Endpoint 10 Event \hideinitializer */
  138. #define USBD_INTSTS_EP11 USBD_INTSTS_EPEVT11_Msk /*!< USB Endpoint 11 Event \hideinitializer */
  139. #define USBD_STATE_USBRST USBD_ATTR_USBRST_Msk /*!< USB Bus Reset \hideinitializer */
  140. #define USBD_STATE_SUSPEND USBD_ATTR_SUSPEND_Msk /*!< USB Bus Suspend \hideinitializer */
  141. #define USBD_STATE_RESUME USBD_ATTR_RESUME_Msk /*!< USB Bus Resume \hideinitializer */
  142. #define USBD_STATE_TIMEOUT USBD_ATTR_TOUT_Msk /*!< USB Bus Timeout \hideinitializer */
  143. #define USBD_CFGP_SSTALL USBD_CFGP_SSTALL_Msk /*!< Set Stall \hideinitializer */
  144. #define USBD_CFG_CSTALL USBD_CFG_CSTALL_Msk /*!< Clear Stall \hideinitializer */
  145. #define USBD_CFG_EPMODE_DISABLE (0ul << USBD_CFG_STATE_Pos)/*!< Endpoint Disable \hideinitializer */
  146. #define USBD_CFG_EPMODE_OUT (1ul << USBD_CFG_STATE_Pos)/*!< Out Endpoint \hideinitializer */
  147. #define USBD_CFG_EPMODE_IN (2ul << USBD_CFG_STATE_Pos)/*!< In Endpoint \hideinitializer */
  148. #define USBD_CFG_TYPE_ISO (1ul << USBD_CFG_ISOCH_Pos) /*!< Isochronous \hideinitializer */
  149. /*@}*/ /* end of group USBD_EXPORTED_CONSTANTS */
  150. /** @addtogroup USBD_EXPORTED_FUNCTIONS USBD Exported Functions
  151. @{
  152. */
  153. /**
  154. * @brief Compare two input numbers and return maximum one.
  155. *
  156. * @param[in] a First number to be compared.
  157. * @param[in] b Second number to be compared.
  158. *
  159. * @return Maximum value between a and b.
  160. *
  161. * @details If a > b, then return a. Otherwise, return b.
  162. * \hideinitializer
  163. */
  164. #define USBD_Maximum(a,b) ((a)>(b) ? (a) : (b))
  165. /**
  166. * @brief Compare two input numbers and return minimum one
  167. *
  168. * @param[in] a First number to be compared
  169. * @param[in] b Second number to be compared
  170. *
  171. * @return Minimum value between a and b
  172. *
  173. * @details If a < b, then return a. Otherwise, return b.
  174. * \hideinitializer
  175. */
  176. #define USBD_Minimum(a,b) ((a)<(b) ? (a) : (b))
  177. /**
  178. * @brief Enable USB
  179. *
  180. * @param None
  181. *
  182. * @return None
  183. *
  184. * @details To set USB ATTR control register to enable USB and PHY.
  185. * \hideinitializer
  186. */
  187. #define USBD_ENABLE_USB() ((uint32_t)(USBD->ATTR |= 0x7D0))
  188. /**
  189. * @brief Disable USB
  190. *
  191. * @param None
  192. *
  193. * @return None
  194. *
  195. * @details To set USB ATTR control register to disable USB.
  196. * \hideinitializer
  197. */
  198. #define USBD_DISABLE_USB() ((uint32_t)(USBD->ATTR &= ~USBD_USB_EN))
  199. /**
  200. * @brief Enable USB PHY
  201. *
  202. * @param None
  203. *
  204. * @return None
  205. *
  206. * @details To set USB ATTR control register to enable USB PHY.
  207. * \hideinitializer
  208. */
  209. #define USBD_ENABLE_PHY() ((uint32_t)(USBD->ATTR |= USBD_PHY_EN))
  210. /**
  211. * @brief Disable USB PHY
  212. *
  213. * @param None
  214. *
  215. * @return None
  216. *
  217. * @details To set USB ATTR control register to disable USB PHY.
  218. * \hideinitializer
  219. */
  220. #define USBD_DISABLE_PHY() ((uint32_t)(USBD->ATTR &= ~USBD_PHY_EN))
  221. /**
  222. * @brief Enable SE0. Force USB PHY transceiver to drive SE0.
  223. *
  224. * @param None
  225. *
  226. * @return None
  227. *
  228. * @details Set DRVSE0 bit of USB_DRVSE0 register to enable software-disconnect function. Force USB PHY transceiver to drive SE0 to bus.
  229. * \hideinitializer
  230. */
  231. #define USBD_SET_SE0() ((uint32_t)(USBD->SE0 |= USBD_DRVSE0))
  232. /**
  233. * @brief Disable SE0
  234. *
  235. * @param None
  236. *
  237. * @return None
  238. *
  239. * @details Clear DRVSE0 bit of USB_DRVSE0 register to disable software-disconnect function.
  240. * \hideinitializer
  241. */
  242. #define USBD_CLR_SE0() ((uint32_t)(USBD->SE0 &= ~USBD_DRVSE0))
  243. /**
  244. * @brief Set USB device address
  245. *
  246. * @param[in] addr The USB device address.
  247. *
  248. * @return None
  249. *
  250. * @details Write USB device address to USB_FADDR register.
  251. * \hideinitializer
  252. */
  253. #define USBD_SET_ADDR(addr) (USBD->FADDR = (addr))
  254. /**
  255. * @brief Get USB device address
  256. *
  257. * @param None
  258. *
  259. * @return USB device address
  260. *
  261. * @details Read USB_FADDR register to get USB device address.
  262. * \hideinitializer
  263. */
  264. #define USBD_GET_ADDR() ((uint32_t)(USBD->FADDR))
  265. /**
  266. * @brief Enable USB interrupt function
  267. *
  268. * @param[in] intr The combination of the specified interrupt enable bits.
  269. * Each bit corresponds to a interrupt enable bit.
  270. * This parameter decides which interrupts will be enabled.
  271. * (USBD_INT_WAKEUP, USBD_INT_FLDET, USBD_INT_USB, USBD_INT_BUS)
  272. *
  273. * @return None
  274. *
  275. * @details Enable USB related interrupt functions specified by intr parameter.
  276. * \hideinitializer
  277. */
  278. #define USBD_ENABLE_INT(intr) (USBD->INTEN |= (intr))
  279. /**
  280. * @brief Get interrupt status
  281. *
  282. * @param None
  283. *
  284. * @return The value of USB_INTSTS register
  285. *
  286. * @details Return all interrupt flags of USB_INTSTS register.
  287. * \hideinitializer
  288. */
  289. #define USBD_GET_INT_FLAG() ((uint32_t)(USBD->INTSTS))
  290. /**
  291. * @brief Clear USB interrupt flag
  292. *
  293. * @param[in] flag The combination of the specified interrupt flags.
  294. * Each bit corresponds to a interrupt source.
  295. * This parameter decides which interrupt flags will be cleared.
  296. * (USBD_INTSTS_WAKEUP, USBD_INTSTS_FLDET, USBD_INTSTS_BUS, USBD_INTSTS_USB)
  297. *
  298. * @return None
  299. *
  300. * @details Clear USB related interrupt flags specified by flag parameter.
  301. * \hideinitializer
  302. */
  303. #define USBD_CLR_INT_FLAG(flag) (USBD->INTSTS = (flag))
  304. /**
  305. * @brief Get endpoint status
  306. *
  307. * @param None
  308. *
  309. * @return The value of USB_EPSTS register.
  310. *
  311. * @details Return all endpoint status.
  312. * \hideinitializer
  313. */
  314. #define USBD_GET_EP_FLAG() ((uint32_t)(USBD->EPSTS))
  315. /**
  316. * @brief Get USB bus state
  317. *
  318. * @param None
  319. *
  320. * @return The value of USB_ATTR[3:0].
  321. * Bit 0 indicates USB bus reset status.
  322. * Bit 1 indicates USB bus suspend status.
  323. * Bit 2 indicates USB bus resume status.
  324. * Bit 3 indicates USB bus time-out status.
  325. *
  326. * @details Return USB_ATTR[3:0] for USB bus events.
  327. * \hideinitializer
  328. */
  329. #define USBD_GET_BUS_STATE() ((uint32_t)(USBD->ATTR & 0xf))
  330. /**
  331. * @brief Check cable connection state
  332. *
  333. * @param None
  334. *
  335. * @retval 0 USB cable is not attached.
  336. * @retval 1 USB cable is attached.
  337. *
  338. * @details Check the connection state by FLDET bit of USB_FLDET register.
  339. * \hideinitializer
  340. */
  341. #define USBD_IS_ATTACHED() ((uint32_t)(USBD->VBUSDET & USBD_VBUSDET_VBUSDET_Msk))
  342. /**
  343. * @brief Stop USB transaction of the specified endpoint ID
  344. *
  345. * @param[in] ep The USB endpoint ID. M480 Series supports 8 hardware endpoint ID. This parameter could be 0 ~ 11.
  346. *
  347. * @return None
  348. *
  349. * @details Write 1 to CLRRDY bit of USB_CFGPx register to stop USB transaction of the specified endpoint ID.
  350. * \hideinitializer
  351. */
  352. #define USBD_STOP_TRANSACTION(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFGP + (uint32_t)((ep) << 4))) |= USBD_CFGP_CLRRDY_Msk)
  353. /**
  354. * @brief Set USB DATA1 PID for the specified endpoint ID
  355. *
  356. * @param[in] ep The USB endpoint ID. M480 Series supports 8 hardware endpoint ID. This parameter could be 0 ~ 11.
  357. *
  358. * @return None
  359. *
  360. * @details Set DSQ_SYNC bit of USB_CFGx register to specify the DATA1 PID for the following IN token transaction.
  361. * Base on this setting, hardware will toggle PID between DATA0 and DATA1 automatically for IN token transactions.
  362. * \hideinitializer
  363. */
  364. #define USBD_SET_DATA1(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFG + (uint32_t)((ep) << 4))) |= USBD_CFG_DSQSYNC_Msk)
  365. /**
  366. * @brief Set USB DATA0 PID for the specified endpoint ID
  367. *
  368. * @param[in] ep The USB endpoint ID. M480 Series supports 8 hardware endpoint ID. This parameter could be 0 ~ 11.
  369. *
  370. * @return None
  371. *
  372. * @details Clear DSQ_SYNC bit of USB_CFGx register to specify the DATA0 PID for the following IN token transaction.
  373. * Base on this setting, hardware will toggle PID between DATA0 and DATA1 automatically for IN token transactions.
  374. * \hideinitializer
  375. */
  376. #define USBD_SET_DATA0(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFG + (uint32_t)((ep) << 4))) &= (~USBD_CFG_DSQSYNC_Msk))
  377. /**
  378. * @brief Set USB payload size (IN data)
  379. *
  380. * @param[in] ep The USB endpoint ID. M480 Series supports 8 hardware endpoint ID. This parameter could be 0 ~ 11.
  381. *
  382. * @param[in] size The transfer length.
  383. *
  384. * @return None
  385. *
  386. * @details This macro will write the transfer length to USB_MXPLDx register for IN data transaction.
  387. * \hideinitializer
  388. */
  389. #define USBD_SET_PAYLOAD_LEN(ep, size) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].MXPLD + (uint32_t)((ep) << 4))) = (size))
  390. /**
  391. * @brief Get USB payload size (OUT data)
  392. *
  393. * @param[in] ep The USB endpoint ID. M480 Series supports 8 endpoint ID. This parameter could be 0 ~ 11.
  394. *
  395. * @return The value of USB_MXPLDx register.
  396. *
  397. * @details Get the data length of OUT data transaction by reading USB_MXPLDx register.
  398. * \hideinitializer
  399. */
  400. #define USBD_GET_PAYLOAD_LEN(ep) ((uint32_t)*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].MXPLD + (uint32_t)((ep) << 4))))
  401. /**
  402. * @brief Configure endpoint
  403. *
  404. * @param[in] ep The USB endpoint ID. M480 Series supports 8 hardware endpoint ID. This parameter could be 0 ~ 11.
  405. *
  406. * @param[in] config The USB configuration.
  407. *
  408. * @return None
  409. *
  410. * @details This macro will write config parameter to USB_CFGx register of specified endpoint ID.
  411. * \hideinitializer
  412. */
  413. #define USBD_CONFIG_EP(ep, config) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFG + (uint32_t)((ep) << 4))) = (config))
  414. /**
  415. * @brief Set USB endpoint buffer
  416. *
  417. * @param[in] ep The USB endpoint ID. M480 Series supports 8 hardware endpoint ID. This parameter could be 0 ~ 11.
  418. *
  419. * @param[in] offset The SRAM offset.
  420. *
  421. * @return None
  422. *
  423. * @details This macro will set the SRAM offset for the specified endpoint ID.
  424. * \hideinitializer
  425. */
  426. #define USBD_SET_EP_BUF_ADDR(ep, offset) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].BUFSEG + (uint32_t)((ep) << 4))) = (offset))
  427. /**
  428. * @brief Get the offset of the specified USB endpoint buffer
  429. *
  430. * @param[in] ep The USB endpoint ID. M480 Series supports 8 hardware endpoint ID. This parameter could be 0 ~ 11.
  431. *
  432. * @return The offset of the specified endpoint buffer.
  433. *
  434. * @details This macro will return the SRAM offset of the specified endpoint ID.
  435. * \hideinitializer
  436. */
  437. #define USBD_GET_EP_BUF_ADDR(ep) ((uint32_t)*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].BUFSEG + (uint32_t)((ep) << 4))))
  438. /**
  439. * @brief Set USB endpoint stall state
  440. *
  441. * @param[in] ep The USB endpoint ID. M480 Series supports 8 hardware endpoint ID. This parameter could be 0 ~ 11.
  442. *
  443. * @return None
  444. *
  445. * @details Set USB endpoint stall state for the specified endpoint ID. Endpoint will respond STALL token automatically.
  446. * \hideinitializer
  447. */
  448. #define USBD_SET_EP_STALL(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0ul].CFGP + (uint32_t)((ep) << 4))) |= USBD_CFGP_SSTALL_Msk)
  449. /**
  450. * @brief Clear USB endpoint stall state
  451. *
  452. * @param[in] ep The USB endpoint ID. M480 Series supports 8 hardware endpoint ID. This parameter could be 0 ~ 11.
  453. *
  454. * @return None
  455. *
  456. * @details Clear USB endpoint stall state for the specified endpoint ID. Endpoint will respond ACK/NAK token.
  457. * \hideinitializer
  458. */
  459. #define USBD_CLR_EP_STALL(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFGP + (uint32_t)((ep) << 4))) &= ~USBD_CFGP_SSTALL_Msk)
  460. /**
  461. * @brief Get USB endpoint stall state
  462. *
  463. * @param[in] ep The USB endpoint ID. M480 Series supports 8 hardware endpoint ID. This parameter could be 0 ~ 11.
  464. *
  465. * @retval 0 USB endpoint is not stalled.
  466. * @retval Others USB endpoint is stalled.
  467. *
  468. * @details Get USB endpoint stall state of the specified endpoint ID.
  469. * \hideinitializer
  470. */
  471. #define USBD_GET_EP_STALL(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFGP + (uint32_t)((ep) << 4))) & USBD_CFGP_SSTALL_Msk)
  472. /**
  473. * @brief To support byte access between USB SRAM and system SRAM
  474. *
  475. * @param[in] dest Destination pointer.
  476. *
  477. * @param[in] src Source pointer.
  478. *
  479. * @param[in] size Byte count.
  480. *
  481. * @return None
  482. *
  483. * @details This function will copy the number of data specified by size and src parameters to the address specified by dest parameter.
  484. *
  485. */
  486. __STATIC_INLINE void USBD_MemCopy(uint8_t dest[], uint8_t src[], uint32_t size)
  487. {
  488. uint32_t volatile i=0ul;
  489. while(size--)
  490. {
  491. dest[i] = src[i];
  492. i++;
  493. }
  494. }
  495. /**
  496. * @brief Set USB endpoint stall state
  497. *
  498. * @param[in] epnum USB endpoint number
  499. *
  500. * @return None
  501. *
  502. * @details Set USB endpoint stall state. Endpoint will respond STALL token automatically.
  503. *
  504. */
  505. __STATIC_INLINE void USBD_SetStall(uint8_t epnum)
  506. {
  507. uint32_t u32CfgAddr;
  508. uint32_t u32Cfg;
  509. uint32_t i;
  510. for(i = 0ul; i < USBD_MAX_EP; i++)
  511. {
  512. u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFG; /* USBD_CFG0 */
  513. u32Cfg = *((__IO uint32_t *)(u32CfgAddr));
  514. if((u32Cfg & 0xful) == epnum)
  515. {
  516. u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFGP; /* USBD_CFGP0 */
  517. u32Cfg = *((__IO uint32_t *)(u32CfgAddr));
  518. *((__IO uint32_t *)(u32CfgAddr)) = (u32Cfg | USBD_CFGP_SSTALL);
  519. break;
  520. }
  521. }
  522. }
  523. /**
  524. * @brief Clear USB endpoint stall state
  525. *
  526. * @param[in] epnum USB endpoint number
  527. *
  528. * @return None
  529. *
  530. * @details Clear USB endpoint stall state. Endpoint will respond ACK/NAK token.
  531. */
  532. __STATIC_INLINE void USBD_ClearStall(uint8_t epnum)
  533. {
  534. uint32_t u32CfgAddr;
  535. uint32_t u32Cfg;
  536. uint32_t i;
  537. for(i = 0ul; i < USBD_MAX_EP; i++)
  538. {
  539. u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFG; /* USBD_CFG0 */
  540. u32Cfg = *((__IO uint32_t *)(u32CfgAddr));
  541. if((u32Cfg & 0xful) == epnum)
  542. {
  543. u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFGP; /* USBD_CFGP0 */
  544. u32Cfg = *((__IO uint32_t *)(u32CfgAddr));
  545. *((__IO uint32_t *)(u32CfgAddr)) = (u32Cfg & ~USBD_CFGP_SSTALL);
  546. break;
  547. }
  548. }
  549. }
  550. /**
  551. * @brief Get USB endpoint stall state
  552. *
  553. * @param[in] epnum USB endpoint number
  554. *
  555. * @retval 0 USB endpoint is not stalled.
  556. * @retval Others USB endpoint is stalled.
  557. *
  558. * @details Get USB endpoint stall state.
  559. *
  560. */
  561. __STATIC_INLINE uint32_t USBD_GetStall(uint8_t epnum)
  562. {
  563. uint32_t u32CfgAddr;
  564. uint32_t u32Cfg;
  565. uint32_t i;
  566. for(i = 0ul; i < USBD_MAX_EP; i++)
  567. {
  568. u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFG; /* USBD_CFG0 */
  569. u32Cfg = *((__IO uint32_t *)(u32CfgAddr));
  570. if((u32Cfg & 0xful) == epnum)
  571. {
  572. u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFGP; /* USBD_CFGP0 */
  573. break;
  574. }
  575. }
  576. return ((*((__IO uint32_t *)(u32CfgAddr))) & USBD_CFGP_SSTALL);
  577. }
  578. extern volatile uint8_t g_usbd_RemoteWakeupEn;
  579. typedef void (*VENDOR_REQ)(void); /*!< Functional pointer type definition for Vendor class */
  580. typedef void (*CLASS_REQ)(void); /*!< Functional pointer type declaration for USB class request callback handler */
  581. typedef void (*SET_INTERFACE_REQ)(uint32_t u32AltInterface); /*!< Functional pointer type declaration for USB set interface request callback handler */
  582. typedef void (*SET_CONFIG_CB)(void); /*!< Functional pointer type declaration for USB set configuration request callback handler */
  583. /*--------------------------------------------------------------------*/
  584. void USBD_Open(const S_USBD_INFO_T *param, CLASS_REQ pfnClassReq, SET_INTERFACE_REQ pfnSetInterface);
  585. void USBD_Start(void);
  586. void USBD_GetSetupPacket(uint8_t *buf);
  587. void USBD_ProcessSetupPacket(void);
  588. void USBD_StandardRequest(void);
  589. void USBD_PrepareCtrlIn(uint8_t pu8Buf[], uint32_t u32Size);
  590. void USBD_CtrlIn(void);
  591. void USBD_PrepareCtrlOut(uint8_t *pu8Buf, uint32_t u32Size);
  592. void USBD_CtrlOut(void);
  593. void USBD_SwReset(void);
  594. void USBD_SetVendorRequest(VENDOR_REQ pfnVendorReq);
  595. void USBD_SetConfigCallback(SET_CONFIG_CB pfnSetConfigCallback);
  596. void USBD_LockEpStall(uint32_t u32EpBitmap);
  597. /*@}*/ /* end of group USBD_EXPORTED_FUNCTIONS */
  598. /*@}*/ /* end of group USBD_Driver */
  599. /*@}*/ /* end of group Standard_Driver */
  600. #ifdef __cplusplus
  601. }
  602. #endif
  603. #endif /*__NU_USBD_H__*/
  604. /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/