apm32f10x_usb.h 12 KB

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