apm32e10x_usb.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /*!
  2. * @file apm32e10x_usb.h
  3. *
  4. * @brief This file contains all the prototypes,enumeration and macros for USBD peripheral
  5. *
  6. * @version V1.0.2
  7. *
  8. * @date 2022-12-31
  9. *
  10. * @attention
  11. *
  12. * Copyright (C) 2021-2023 Geehy Semiconductor
  13. *
  14. * You may not use this file except in compliance with the
  15. * GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
  16. *
  17. * The program is only for reference, which is distributed in the hope
  18. * that it will be useful and instructional for customers to develop
  19. * their software. Unless required by applicable law or agreed to in
  20. * writing, the program is distributed on an "AS IS" BASIS, WITHOUT
  21. * ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
  23. * and limitations under the License.
  24. */
  25. /* Define to prevent recursive inclusion */
  26. #ifndef __APM32E10X_USBD_H_
  27. #define __APM32E10X_USBD_H_
  28. /* Includes */
  29. #include "apm32e10x.h"
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. /** @addtogroup APM32E10x_StdPeriphDriver
  34. @{
  35. */
  36. /** @addtogroup USBD_Driver
  37. @{
  38. */
  39. /** @defgroup USBD_Macros Macros
  40. @{
  41. */
  42. /* USBD packet memory area base address */
  43. #define USBD_PMA_ADDR (0x40006000L)
  44. /* Endpoint register mask value default */
  45. #define USBD_EP_MASK_DEFAULT (USBD_EP_BIT_CTFR | USBD_EP_BIT_SETUP | USBD_EP_BIT_TYPE | USBD_EP_BIT_KIND | USBD_EP_BIT_CTFT |USBD_EP_BIT_ADDR)
  46. /**
  47. * @brief USBD interrupt source
  48. */
  49. #define USBD_INT_ESOF 0X100
  50. #define USBD_INT_SOF 0X200
  51. #define USBD_INT_RST 0X400
  52. #define USBD_INT_SUS 0x800
  53. #define USBD_INT_WKUP 0X1000
  54. #define USBD_INT_ERR 0X2000
  55. #define USBD_INT_PMAOU 0X4000
  56. #define USBD_INT_CTR 0X8000
  57. #define USBD_INT_ALL 0XFF00
  58. /*!
  59. * @brief Set CTRL register
  60. *
  61. * @param val: Register value
  62. *
  63. * @retval None
  64. *
  65. */
  66. #define USBD_SetRegCTRL(val) (USBD->CTRL = val)
  67. /*!
  68. * @brief Set INTSTS register
  69. *
  70. * @param val: Register value
  71. *
  72. * @retval None
  73. */
  74. #define USBD_SetRegINTSTS(val) (USBD->INTSTS = val)
  75. /*!
  76. * @brief Set force reset
  77. *
  78. * @param None
  79. *
  80. * @retval None
  81. */
  82. #define USBD_SetForceReset() (USBD->CTRL_B.FORRST = BIT_SET)
  83. /*!
  84. * @brief Reset force reset
  85. *
  86. * @param None
  87. *
  88. * @retval None
  89. */
  90. #define USBD_ResetForceReset() (USBD->CTRL_B.FORRST = BIT_RESET)
  91. /*!
  92. * @brief Set power down
  93. *
  94. * @param None
  95. *
  96. * @retval None
  97. */
  98. #define USBD_SetPowerDown() (USBD->CTRL_B.PWRDOWN = BIT_SET)
  99. /*!
  100. * @brief Reset power down
  101. *
  102. * @param None
  103. *
  104. * @retval None
  105. */
  106. #define USBD_ResetPowerDown() (USBD->CTRL_B.PWRDOWN = BIT_RESET)
  107. /*!
  108. * @brief Set low power mode
  109. *
  110. * @param None
  111. *
  112. * @retval None
  113. */
  114. #define USBD_SetLowerPowerMode() (USBD->CTRL_B.LPWREN = BIT_SET)
  115. /*!
  116. * @brief Ret low power mode
  117. *
  118. * @param None
  119. *
  120. * @retval None
  121. */
  122. #define USBD_ResetLowerPowerMode() (USBD->CTRL_B.LPWREN = BIT_RESET)
  123. /*!
  124. * @brief Set force suspend
  125. *
  126. * @param None
  127. *
  128. * @retval None
  129. */
  130. #define USBD_SetForceSuspend() (USBD->CTRL_B.FORSUS = BIT_SET)
  131. /*!
  132. * @brief Reset force suspend
  133. *
  134. * @param None
  135. *
  136. * @retval None
  137. */
  138. #define USBD_ResetForceSuspend() (USBD->CTRL_B.FORSUS = BIT_RESET)
  139. /*!
  140. * @brief Read force suspend status
  141. *
  142. * @param None
  143. *
  144. * @retval None
  145. */
  146. #define USBD_ReadForceSuspend() (USBD->CTRL_B.FORSUS)
  147. /*!
  148. * @brief Set resume
  149. *
  150. * @param None
  151. *
  152. * @retval None
  153. */
  154. #define USBD_SetResume() (USBD->CTRL_B.WUPREQ = BIT_SET)
  155. /*!
  156. * @brief Reset resume
  157. *
  158. * @param None
  159. *
  160. * @retval None
  161. */
  162. #define USBD_ResetResume() (USBD->CTRL_B.WUPREQ = BIT_RESET)
  163. /*!
  164. * @brief Enable interrupt
  165. *
  166. * @param int: Interrupt source
  167. *
  168. * @retval None
  169. */
  170. #define USBD_EnableInterrupt(int) (USBD->CTRL |= int)
  171. /*!
  172. * @brief Disable interrupt
  173. *
  174. * @param int: Interrupt source
  175. *
  176. * @retval None
  177. */
  178. #define USBD_DisableInterrupt(int) (USBD->CTRL &= (uint32_t)~int)
  179. /*!
  180. * @brief Read the specified interrupt flag status
  181. *
  182. * @param int: Interrupt source
  183. *
  184. * @retval Flag status.0 or not 0
  185. */
  186. #define USBD_ReadIntFlag(int) (USBD->INTSTS & int)
  187. /*!
  188. * @brief Clear the specified interrupt flag status
  189. *
  190. * @param int: Interrupt source
  191. *
  192. * @retval None
  193. */
  194. #define USBD_ClearIntFlag(int) (USBD->INTSTS &= (uint32_t)~int)
  195. /*!
  196. * @brief Read DOT field value in INTSTS rigister
  197. *
  198. * @param None
  199. *
  200. * @retval DOT field value
  201. */
  202. #define USBD_ReadDir() (USBD->INTSTS_B.DOT)
  203. /*!
  204. * @brief Read EPID field value in INTSTS rigister
  205. *
  206. * @param None
  207. *
  208. * @retval EPIDfield value
  209. */
  210. #define USBD_ReadEP() ((USBD_EP_T)(USBD->INTSTS_B.EPID))
  211. /*!
  212. * @brief Read EP type
  213. *
  214. * @param ep: EP number
  215. *
  216. * @retval EP type
  217. */
  218. #define USBD_ReadEPType(ep) (USBD->EP[ep].EP_B.TYPE)
  219. /*!
  220. * @brief Read EP Tx status
  221. *
  222. * @param ep: EP number
  223. *
  224. * @retval EP Tx status
  225. */
  226. #define USBD_ReadEPTxStatus(ep) ((USBD_EP_STATUS_T)(USBD->EP[ep].EP_B.TXSTS))
  227. /*!
  228. * @brief Read EP Rx status
  229. *
  230. * @param ep: EP number
  231. *
  232. * @retval EP Rx status
  233. */
  234. #define USBD_ReadEPRxStatus(ep) ((USBD_EP_STATUS_T)(USBD->EP[ep].EP_B.RXSTS))
  235. /*!
  236. * @brief Read EP Tx address pointer
  237. *
  238. * @param ep: EP number
  239. *
  240. * @retval EP Tx address pointer
  241. */
  242. #define USBD_ReadEPTxAddrPointer(ep) (uint32_t *)((USBD->BUFFTB + ep * 8) * 2 + USBD_PMA_ADDR)
  243. /*!
  244. * @brief Read EP Tx count pointer
  245. *
  246. * @param ep: EP number
  247. *
  248. * @retval EP Tx count pointer
  249. */
  250. #define USBD_ReadEPTxCntPointer(ep) (uint32_t *)((USBD->BUFFTB + ep * 8 + 2) * 2 + USBD_PMA_ADDR)
  251. /*!
  252. * @brief Read EP Rx address pointer
  253. *
  254. * @param ep: EP number
  255. *
  256. * @retval EP Rx address pointer
  257. */
  258. #define USBD_ReadEPRxAddrPointer(ep) (uint32_t *)((USBD->BUFFTB + ep * 8 + 4) * 2 + USBD_PMA_ADDR)
  259. /*!
  260. * @brief Read EP Rx count pointer
  261. *
  262. * @param ep: EP number
  263. *
  264. * @retval EP Rx count pointer
  265. */
  266. #define USBD_ReadEPRxCntPointer(ep) (uint32_t *)((USBD->BUFFTB + ep * 8 + 6) * 2 + USBD_PMA_ADDR)
  267. /*!
  268. * @brief Set EP Tx addr
  269. *
  270. * @param ep: EP number
  271. *
  272. * @param addr: Tx addr
  273. *
  274. * @retval None
  275. */
  276. #define USBD_SetEPTxAddr(ep, addr) (*USBD_ReadEPTxAddrPointer(ep) = (addr >> 1) << 1)
  277. /*!
  278. * @brief Set EP Rx addr
  279. *
  280. * @param ep: EP number
  281. *
  282. * @param addr: Rx addr
  283. *
  284. * @retval None
  285. */
  286. #define USBD_SetEPRxAddr(ep, addr) (*USBD_ReadEPRxAddrPointer(ep) = (addr >> 1) << 1)
  287. /*!
  288. * @brief Read EP Tx addr
  289. *
  290. * @param ep: EP number
  291. *
  292. * @retval EP Tx addr
  293. */
  294. #define USBD_ReadEPTxAddr(ep) ((uint16_t)*USBD_ReadEPTxAddrPointer(ep))
  295. /*!
  296. * @brief Read EP Rx addr
  297. *
  298. * @param ep: EP number
  299. *
  300. * @retval EP Rx addr
  301. */
  302. #define USBD_ReadEPRxAddr(ep) ((uint16_t)*USBD_ReadEPRxAddrPointer(ep))
  303. /*!
  304. * @brief Set EP Rx Count
  305. *
  306. * @param ep: EP number
  307. *
  308. * @param cnt: Tx count
  309. *
  310. * @retval None
  311. */
  312. #define USBD_SetEPTxCnt(ep, cnt) (*USBD_ReadEPTxCntPointer(ep) = cnt)
  313. /*!
  314. * @brief Read EP Tx count
  315. *
  316. * @param ep: EP number
  317. *
  318. * @retval EP Tx count
  319. */
  320. #define USBD_ReadEPTxCnt(ep) ((uint16_t)*USBD_ReadEPTxCntPointer(ep) & 0x3ff)
  321. /*!
  322. * @brief Read EP Rx count
  323. *
  324. * @param ep: EP number
  325. *
  326. * @retval EP Rx count
  327. */
  328. #define USBD_ReadEPRxCnt(ep) ((uint16_t)*USBD_ReadEPRxCntPointer(ep) & 0x3ff)
  329. /*!
  330. * @brief Read SETUP field value in EP register
  331. *
  332. * @param ep: EP number
  333. *
  334. * @retval SETUP field value
  335. */
  336. #define USBD_ReadEPSetup(ep) (USBD->EP[ep].EP_B.SETUP)
  337. /*!
  338. * @brief Set buffer table value
  339. *
  340. * @param tab: Buffer table value
  341. *
  342. * @retval None
  343. */
  344. #define USBD_SetBufferTable(tab) (USBD->BUFFTB_B.BUFFTB = tab)
  345. /*!
  346. * @brief Set device address
  347. *
  348. * @param addr: Device address
  349. *
  350. * @retval None
  351. */
  352. #define USBD_SetDeviceAddr(addr) (USBD->ADDR_B.ADDR = addr)
  353. /*!
  354. * @brief Read CTFR field value in EP register
  355. *
  356. * @param ep: Endpoint number
  357. *
  358. * @retval CTFR field value
  359. */
  360. #define USBD_ReadEPRxFlag(ep) (USBD->EP[ep].EP_B.CTFR)
  361. /*!
  362. * @brief Read CTFT field value in EP register
  363. *
  364. * @param ep: Endpoint number
  365. *
  366. * @retval CTFT field value
  367. */
  368. #define USBD_ReadEPTxFlag(ep) (USBD->EP[ep].EP_B.CTFT)
  369. /*!
  370. * @brief Enable USBD peripheral
  371. *
  372. * @param None
  373. *
  374. * @retval None
  375. */
  376. #define USBD_Enable() (USBD->ADDR_B.USBDEN = BIT_SET)
  377. /*!
  378. * @brief Disable USBD peripheral
  379. *
  380. * @param None
  381. *
  382. * @retval None
  383. */
  384. #define USBD_Disable() (USBD->ADDR_B.USBDEN = BIT_RESET)
  385. /*!
  386. * @brief Enable USBD2 peripheral
  387. *
  388. * @param None
  389. *
  390. * @retval None
  391. */
  392. #define USBD2_Enable() (USBD->USB_SWITCH = BIT_SET)
  393. /*!
  394. * @brief Disable USBD2 peripheral
  395. *
  396. * @param None
  397. *
  398. * @retval None
  399. */
  400. #define USBD2_Disable() (USBD->USB_SWITCH = BIT_RESET)
  401. /*!
  402. * @brief Read RXDPSTS field value in FRANUM register
  403. *
  404. * @param None
  405. *
  406. * @retval RXDPSTS field value
  407. */
  408. #define USBD_ReadRDPS() (USBD->FRANUM_B.RXDPSTS)
  409. /*!
  410. * @brief Read RXDMSTS field value in FRANUM register
  411. *
  412. * @param None
  413. *
  414. * @retval RXDMSTS field value
  415. */
  416. #define USBD_ReadRDMS() (USBD->FRANUM_B.RXDMSTS)
  417. /*!
  418. * @brief Read LOCK field value in FRANUM register
  419. *
  420. * @param None
  421. *
  422. * @retval LOCK field value
  423. */
  424. #define USBD_ReadLOCK() (USBD->FRANUM_B.LOCK)
  425. /*!
  426. * @brief Read LSOFNUM field value in FRANUM register
  427. *
  428. * @param None
  429. *
  430. * @retval LSOFNUM field value
  431. */
  432. #define USBD_ReadLSOF() (USBD->FRANUM_B.LSOFNUM)
  433. /*!
  434. * @brief Read FRANUM field value in FRANUM register
  435. *
  436. * @param None
  437. *
  438. * @retval FRANUM field value
  439. */
  440. #define USBD_ReadFRANUM() (USBD->FRANUM_B.FRANUM)
  441. /**@} end of group USBD_Macros */
  442. /** @defgroup USBD_Enumerations Enumerations
  443. @{
  444. */
  445. /**
  446. * @brief USBD Endpoint register bit definition
  447. */
  448. typedef enum
  449. {
  450. USBD_EP_BIT_ADDR = (uint32_t)(BIT0 | BIT1 | BIT2 | BIT3),
  451. USBD_EP_BIT_TXSTS = (uint32_t)(BIT4 | BIT5),
  452. USBD_EP_BIT_TXDTOG = (uint32_t)(BIT6),
  453. USBD_EP_BIT_CTFT = (uint32_t)(BIT7),
  454. USBD_EP_BIT_KIND = (uint32_t)(BIT8),
  455. USBD_EP_BIT_TYPE = (uint32_t)(BIT9 | BIT10),
  456. USBD_EP_BIT_SETUP = (uint32_t)(BIT11),
  457. USBD_EP_BIT_RXSTS = (uint32_t)(BIT12 | BIT13),
  458. USBD_EP_BIT_RXDTOG = (uint32_t)(BIT14),
  459. USBD_EP_BIT_CTFR = (uint32_t)(BIT15)
  460. }USBD_EP_BIT_T;
  461. /**
  462. * @brief Endpoint id
  463. */
  464. typedef enum
  465. {
  466. USBD_EP_0,
  467. USBD_EP_1,
  468. USBD_EP_2,
  469. USBD_EP_3,
  470. USBD_EP_4,
  471. USBD_EP_5,
  472. USBD_EP_6,
  473. USBD_EP_7
  474. }USBD_EP_T;
  475. /**
  476. * @brief Endpoint status
  477. */
  478. typedef enum
  479. {
  480. USBD_EP_STATUS_DISABLE = ((uint32_t)0),
  481. USBD_EP_STATUS_STALL = ((uint32_t)1),
  482. USBD_EP_STATUS_NAK = ((uint32_t)2),
  483. USBD_EP_STATUS_VALID = ((uint32_t)3)
  484. }USBD_EP_STATUS_T;
  485. /**
  486. * @brief USBD Endpoint type
  487. */
  488. typedef enum
  489. {
  490. USBD_EP_TYPE_BULK,
  491. USBD_EP_TYPE_CONTROL,
  492. USBD_EP_TYPE_ISO,
  493. USBD_EP_TYPE_INTERRUPT
  494. }USBD_EP_TYPE_T;
  495. /**@} end of group USBD_Enumerations */
  496. /** @defgroup USBD_Functions Functions
  497. @{
  498. */
  499. void USBD_SetEPType(USBD_EP_T ep, USBD_EP_TYPE_T type);
  500. void USBD_SetEPKind(USBD_EP_T ep);
  501. void USBD_ResetEPKind(USBD_EP_T ep);
  502. void USBD_ResetEPRxFlag(USBD_EP_T ep);
  503. void USBD_ResetEPTxFlag(USBD_EP_T ep);
  504. void USBD_ToggleTx(USBD_EP_T ep);
  505. void USBD_ToggleRx(USBD_EP_T ep);
  506. void USBD_ResetTxToggle(USBD_EP_T ep);
  507. void USBD_ResetRxToggle(USBD_EP_T ep);
  508. void USBD_SetEpAddr(USBD_EP_T ep, uint8_t addr);
  509. void USBD_SetEPTxStatus(USBD_EP_T ep, USBD_EP_STATUS_T status);
  510. void USBD_SetEPRxStatus(USBD_EP_T ep, USBD_EP_STATUS_T status);
  511. void USBD_SetEPRxTxStatus(USBD_EP_T ep, USBD_EP_STATUS_T txStatus, USBD_EP_STATUS_T rxStatus);
  512. void USBD_SetEPRxCnt(USBD_EP_T ep, uint32_t cnt);
  513. void USBD_WriteDataToEP(USBD_EP_T ep, uint8_t *wBuf, uint32_t wLen);
  514. void USBD_ReadDataFromEP(USBD_EP_T ep, uint8_t *rBuf, uint32_t rLen);
  515. /**@} end of group USBD_Functions */
  516. /**@} end of group USBD_Driver */
  517. /**@} end of group APM32E10x_StdPeriphDriver */
  518. #ifdef __cplusplus
  519. }
  520. #endif
  521. #endif /* __APM32E10X_USBD_H */