usbd.h 24 KB

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