usbd.h 21 KB

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