nu_usbd.h 25 KB

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