at32f413_usb.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. /**
  2. **************************************************************************
  3. * @file at32f413_usb.h
  4. * @version v2.0.5
  5. * @date 2022-05-20
  6. * @brief at32f413 usb header file
  7. **************************************************************************
  8. * Copyright notice & Disclaimer
  9. *
  10. * The software Board Support Package (BSP) that is made available to
  11. * download from Artery official website is the copyrighted work of Artery.
  12. * Artery authorizes customers to use, copy, and distribute the BSP
  13. * software and its related documentation for the purpose of design and
  14. * development in conjunction with Artery microcontrollers. Use of the
  15. * software is governed by this copyright notice and the following disclaimer.
  16. *
  17. * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  18. * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  19. * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  20. * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  21. * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  23. *
  24. **************************************************************************
  25. */
  26. /** @addtogroup AT32F413_periph_driver
  27. * @{
  28. */
  29. /** @addtogroup USB
  30. * @{
  31. */
  32. /* define to prevent recursive inclusion -------------------------------------*/
  33. #ifndef __AT32F413_USB_H
  34. #define __AT32F413_USB_H
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. /* includes ------------------------------------------------------------------*/
  39. #include "at32f413.h"
  40. /** @defgroup USB_interrupts_definition
  41. * @brief usb interrupt mask
  42. * @{
  43. */
  44. #define USB_LSOF_INT ((uint32_t)0x00000100) /*!< usb lost sof interrupt */
  45. #define USB_SOF_INT ((uint32_t)0x00000200) /*!< usb sof interrupt */
  46. #define USB_RST_INT ((uint32_t)0x00000400) /*!< usb reset interrupt */
  47. #define USB_SP_INT ((uint32_t)0x00000800) /*!< usb suspend interrupt */
  48. #define USB_WK_INT ((uint32_t)0x00001000) /*!< usb wakeup interrupt */
  49. #define USB_BE_INT ((uint32_t)0x00002000) /*!< usb bus error interrupt */
  50. #define USB_UCFOR_INT ((uint32_t)0x00004000) /*!< usb core fifo overrun interrupt */
  51. #define USB_TC_INT ((uint32_t)0x00008000) /*!< usb transmission completed interrupt */
  52. /**
  53. * @}
  54. */
  55. /** @defgroup USB_interrupt_flags_definition
  56. * @brief usb interrupt flag
  57. * @{
  58. */
  59. #define USB_EPT_NUM_FLAG ((uint32_t)0x0000000F) /*!< usb endpoint number */
  60. #define USB_INOUT_FLAG ((uint32_t)0x00000010) /*!< usb in/out transcation flag */
  61. #define USB_LSOF_FLAG ((uint32_t)0x00000100) /*!< usb lost sof flag */
  62. #define USB_SOF_FLAG ((uint32_t)0x00000200) /*!< usb sof flag */
  63. #define USB_RST_FLAG ((uint32_t)0x00000400) /*!< usb reset flag */
  64. #define USB_SP_FLAG ((uint32_t)0x00000800) /*!< usb suspend flag */
  65. #define USB_WK_FLAG ((uint32_t)0x00001000) /*!< usb wakeup flag */
  66. #define USB_BE_FLAG ((uint32_t)0x00002000) /*!< usb bus error flag */
  67. #define USB_UCFOR_FLAG ((uint32_t)0x00004000) /*!< usb core fifo overrun flag */
  68. #define USB_TC_FLAG ((uint32_t)0x00008000) /*!< usb transmission completed flag */
  69. /**
  70. * @}
  71. */
  72. /** @defgroup USB_endpoint_register_bit_definition
  73. * @brief usb endpoint register bit define
  74. * @{
  75. */
  76. #define USB_EPTADDR ((uint32_t)0x0000000F) /*!< usb endpoint address */
  77. #define USB_TXSTS ((uint32_t)0x00000030) /*!< usb tx status */
  78. #define USB_TXDTS ((uint32_t)0x00000040) /*!< usb tx data toggle synchronization */
  79. #define USB_TXTC ((uint32_t)0x00000080) /*!< usb tx transcation completed */
  80. #define USB_EXF ((uint32_t)0x00000100) /*!< usb endpoint extend funtion */
  81. #define USB_TRANS_TYPE ((uint32_t)0x00000600) /*!< usb transfer type */
  82. #define USB_SETUPTC ((uint32_t)0x00000800) /*!< usb setup transcation completed */
  83. #define USB_RXSTS ((uint32_t)0x00003000) /*!< usb rx status */
  84. #define USB_RXDTS ((uint32_t)0x00004000) /*!< usb rx data toggle synchronization */
  85. #define USB_RXTC ((uint32_t)0x00008000) /*!< usb rx transcation completed */
  86. #define USB_EPT_BIT_MASK (uint32_t)(USB_TXTC | USB_SETUPTC | USB_EPTADDR | USB_EXF | USB_RXTC | USB_TRANS_TYPE) /*!< usb bit mask */
  87. #define USB_TX_MASK (USB_TXSTS | USB_EPT_BIT_MASK) /*!< usb tx mask */
  88. #define USB_RX_MASK (USB_RXSTS | USB_EPT_BIT_MASK) /*!< usb rx mask */
  89. /**
  90. * @}
  91. */
  92. /** @defgroup USB_endpoint_tx_and_rx_status_definition
  93. * @brief usb endpoint tx and rx status
  94. * @{
  95. */
  96. #define USB_TX_DISABLE ((uint32_t)0x00000000) /*!< usb tx status disable */
  97. #define USB_TX_STALL ((uint32_t)0x00000010) /*!< usb tx status stall */
  98. #define USB_TX_NAK ((uint32_t)0x00000020) /*!< usb tx status nak */
  99. #define USB_TX_VALID ((uint32_t)0x00000030) /*!< usb tx status valid */
  100. #define USB_TXDTS0 ((uint32_t)0x00000010) /*!< usb tx data toggle bit 0 */
  101. #define USB_TXDTS1 ((uint32_t)0x00000020) /*!< usb tx data toggle bit 1 */
  102. #define USB_RX_DISABLE ((uint32_t)0x00000000) /*!< usb rx status disable */
  103. #define USB_RX_STALL ((uint32_t)0x00001000) /*!< usb rx status stall */
  104. #define USB_RX_NAK ((uint32_t)0x00002000) /*!< usb rx status nak */
  105. #define USB_RX_VALID ((uint32_t)0x00003000) /*!< usb rx status valid */
  106. #define USB_RXDTS0 ((uint32_t)0x00001000) /*!< usb rx data toggle bit 0 */
  107. #define USB_RXDTS1 ((uint32_t)0x00002000) /*!< usb rx data toggle bit 1 */
  108. /**
  109. * @}
  110. */
  111. /** @defgroup USB_device_endpoint_register_type_definition
  112. * @brief usb device endpoint register type define
  113. * @{
  114. */
  115. #define USB_EPT_CONTROL ((uint32_t)0x00000200) /*!< usb endpoint transfer type control */
  116. #define USB_EPT_BULK ((uint32_t)0x00000000) /*!< usb endpoint transfer type bulk */
  117. #define USB_EPT_INT ((uint32_t)0x00000600) /*!< usb endpoint transfer type interrupt */
  118. #define USB_EPT_ISO ((uint32_t)0x00000400) /*!< usb endpoint transfer type iso */
  119. /**
  120. * @}
  121. */
  122. /** @defgroup USB_buffer_table_default_offset_address_definition
  123. * @brief usb buffer table default offset address
  124. * @{
  125. */
  126. #define USB_BUFFER_TABLE_ADDRESS 0x0000 /*!< usb buffer table address */
  127. /**
  128. * @}
  129. */
  130. /** @defgroup USB_packet_buffer_start_address_definition
  131. * @brief usb packet buffer start address
  132. * @{
  133. */
  134. #define USB_PACKET_BUFFER_ADDRESS 0x40006000 /*!< usb buffer address */
  135. #define USB_PACKET_BUFFER_ADDRESS_EX 0x40007800 /*!< usb buffer extend address */
  136. /**
  137. * @}
  138. */
  139. /** @defgroup USB_exported_enum_types
  140. * @{
  141. */
  142. /**
  143. * @brief usb endpoint number define
  144. */
  145. typedef enum
  146. {
  147. USB_EPT0 = 0x00, /*!< usb endpoint 0 */
  148. USB_EPT1 = 0x01, /*!< usb endpoint 1 */
  149. USB_EPT2 = 0x02, /*!< usb endpoint 2 */
  150. USB_EPT3 = 0x03, /*!< usb endpoint 3 */
  151. USB_EPT4 = 0x04, /*!< usb endpoint 4 */
  152. USB_EPT5 = 0x05, /*!< usb endpoint 5 */
  153. USB_EPT6 = 0x06, /*!< usb endpoint 6 */
  154. USB_EPT7 = 0x07 /*!< usb endpoint 7 */
  155. } usb_ept_number_type;
  156. /**
  157. * @brief usb endpoint max num define
  158. */
  159. #ifndef USB_EPT_MAX_NUM
  160. #define USB_EPT_MAX_NUM 8 /*!< usb device support endpoint number */
  161. #endif
  162. /**
  163. * @brief endpoint transfer type define
  164. */
  165. typedef enum
  166. {
  167. EPT_CONTROL_TYPE = 0x00, /*!< usb transfer type control */
  168. EPT_BULK_TYPE = 0x01, /*!< usb transfer type bulk */
  169. EPT_INT_TYPE = 0x02, /*!< usb transfer type interrut */
  170. EPT_ISO_TYPE = 0x03 /*!< usb transfer type iso */
  171. }ept_trans_type;
  172. /**
  173. * @brief endpoint endpoint direction define
  174. */
  175. typedef enum
  176. {
  177. EPT_IN = 0x00, /*!< usb endpoint direction in */
  178. EPT_OUT = 0x01 /*!< usb endpoint direction out */
  179. }ept_inout_type;
  180. /**
  181. * @brief data transfer direction
  182. */
  183. typedef enum
  184. {
  185. DATA_TRANS_OUT = 0x00, /*!< usb data transfer direction out */
  186. DATA_TRANS_IN = 0x01 /*!< usb data transfer direction in */
  187. }data_trans_dir;
  188. /**
  189. * @brief usb clock select
  190. */
  191. typedef enum
  192. {
  193. USB_CLK_HICK,
  194. USB_CLK_HEXT
  195. }usb_clk48_s;
  196. /**
  197. * @}
  198. */
  199. /** @defgroup USB_macro_definition
  200. * @{
  201. */
  202. /**
  203. * @brief set usb endpoint tx status
  204. * @param ept_num: endpoint number
  205. * @param new_sts: the new tx status of this endpoint number
  206. * @retval none
  207. */
  208. #define USB_SET_TXSTS(ept_num, new_sts) { \
  209. register uint16_t epsts = (USB->ept[ept_num]) & USB_TX_MASK; \
  210. if((new_sts & USB_TXDTS0) != 0) \
  211. epsts ^= USB_TXDTS0; \
  212. if((new_sts & USB_TXDTS1) != 0) \
  213. epsts ^= USB_TXDTS1; \
  214. USB->ept[ept_num] = epsts | USB_RXTC | USB_TXTC; \
  215. }
  216. /**
  217. * @brief set usb endpoint rx status
  218. * @param ept_num: endpoint number
  219. * @param new_sts: the new rx status of this endpoint number
  220. * @retval none
  221. */
  222. #define USB_SET_RXSTS(ept_num, new_sts) { \
  223. register uint16_t epsts = (USB->ept[ept_num]) & USB_RX_MASK; \
  224. if((new_sts & USB_RXDTS0) != 0) \
  225. epsts ^= USB_RXDTS0; \
  226. if((new_sts & USB_RXDTS1) != 0) \
  227. epsts ^= USB_RXDTS1; \
  228. USB->ept[ept_num] = epsts | USB_RXTC | USB_TXTC; \
  229. }
  230. /**
  231. * @brief get usb endpoint tx/rx length address
  232. * @param eptn: endpoint number
  233. * @retval the length address of tx/rx
  234. */
  235. #define GET_TX_LEN_ADDR(eptn) (uint32_t *)((USB->buftbl + eptn * 8 + 2) * 2 + g_usb_packet_address)
  236. #define GET_RX_LEN_ADDR(eptn) (uint32_t *)((USB->buftbl + eptn * 8 + 6) * 2 + g_usb_packet_address)
  237. /**
  238. * @brief get usb endpoint tx/rx data length
  239. * @param eptn: endpoint number
  240. * @retval the length of tx/rx
  241. */
  242. #define USB_GET_TX_LEN(eptn) ((uint16_t)(*GET_TX_LEN_ADDR(eptn)) & 0x3ff)
  243. #define USB_GET_RX_LEN(eptn) ((uint16_t)(*GET_RX_LEN_ADDR(eptn)) & 0x3ff)
  244. /**
  245. * @brief double buffer mode get endpoint buf0/buf1 data length
  246. * @param eptn: endpoint number
  247. * @retval the length of buf0/buf1
  248. */
  249. #define USB_DBUF0_GET_LEN(eptn) USB_GET_TX_LEN(eptn)
  250. #define USB_DBUF1_GET_LEN(eptn) USB_GET_RX_LEN(eptn)
  251. /**
  252. * @brief set usb length of rx buffer
  253. * @param reg: usb rx length register
  254. * @param len: rx max length
  255. * @param blks: number of blocks
  256. */
  257. #define BLK32(reg, len, blks) { \
  258. blks = (len) >> 5; \
  259. if(((len) & 0x1F) == 0) \
  260. blks --; \
  261. *reg = ((uint16_t)((blks) << 10) | 0x8000); \
  262. }
  263. #define BLK2(reg, len, blks) { \
  264. blks = (len) >> 1; \
  265. if(((len) & 0x1) == 0) \
  266. blks ++; \
  267. *reg = (uint16_t)((blks) << 10); \
  268. }
  269. #define USB_SET_RXLEN_REG(reg, len) { \
  270. uint16_t blks; \
  271. if(len > 62) \
  272. { \
  273. BLK32(reg, len, blks); \
  274. } \
  275. else \
  276. { \
  277. BLK2(reg, len, blks); \
  278. } \
  279. }
  280. /**
  281. * @brief set endpoint tx/rx transfer length
  282. * @param eptn: endpoint number
  283. * @param len: transfer length
  284. * @retval none
  285. */
  286. #define USB_SET_TXLEN(eptn, len) (*(GET_TX_LEN_ADDR(eptn)) = (len))
  287. #define USB_SET_RXLEN(eptn, len) { \
  288. uint32_t *rx_reg = GET_RX_LEN_ADDR(eptn); \
  289. USB_SET_RXLEN_REG(rx_reg, (len)); \
  290. }
  291. /**
  292. * @brief double buffer mode set endpoint rx buf0 length
  293. * @param eptn: endpoint number
  294. * @param len: transfer length
  295. * @retval none
  296. */
  297. #define USB_OUT_EPT_DOUBLE_BUF0(eptn, len) { \
  298. uint32_t *rx_reg = GET_TX_LEN_ADDR(eptn); \
  299. USB_SET_RXLEN_REG(rx_reg, (len)); \
  300. }
  301. /**
  302. * @brief double buffer mode set endpoint buf0 length
  303. * @param eptn: endpoint number
  304. * @param len: transfer length
  305. * @param dir: transfer direction(in/out)
  306. * @retval none
  307. */
  308. #define USB_SET_EPT_DOUBLE_BUF0_LEN(eptn, len, dir) { \
  309. if(dir == DATA_TRANS_OUT) \
  310. { \
  311. USB_OUT_EPT_DOUBLE_BUF0(eptn, len); \
  312. } \
  313. else \
  314. { \
  315. *(GET_TX_LEN_ADDR(eptn)) = (len); \
  316. } \
  317. }
  318. /**
  319. * @brief double buffer mode set endpoint buf1 length
  320. * @param eptn: endpoint number
  321. * @param len: transfer length
  322. * @param dir: transfer direction(in/out)
  323. * @retval none
  324. */
  325. #define USB_SET_EPT_DOUBLE_BUF1_LEN(eptn, len, dir) { \
  326. if(dir == DATA_TRANS_OUT) \
  327. { \
  328. USB_SET_RXLEN(eptn, len); \
  329. } \
  330. else \
  331. { \
  332. *(GET_RX_LEN_ADDR(eptn)) = (len); \
  333. } \
  334. }
  335. /**
  336. * @brief set usb endpoint tx/rx fifo address
  337. * @param eptn: endpoint number
  338. * @param address: offset of the fifo address
  339. * @retval none
  340. */
  341. #define USB_SET_TX_ADDRESS(eptn, address) (*(uint32_t *)((USB->buftbl + eptn * 8) * 2 + g_usb_packet_address) = address)
  342. #define USB_SET_RX_ADDRESS(eptn, address) (*(uint32_t *)((USB->buftbl + eptn * 8 + 4) * 2 + g_usb_packet_address) = address)
  343. /**
  344. * @brief set double buffer mode usb endpoint buf0/buf1 fifo address
  345. * @param eptn: endpoint number
  346. * @param address: offset of the fifo address
  347. * @retval none
  348. */
  349. #define USB_SET_DOUBLE_BUFF0_ADDRESS(eptn, address) (USB_SET_TX_ADDRESS(eptn, address))
  350. #define USB_SET_DOUBLE_BUFF1_ADDRESS(eptn, address) (USB_SET_RX_ADDRESS(eptn, address))
  351. /**
  352. * @brief set usb tx/rx toggle
  353. * @param eptn: endpoint number
  354. * @retval none
  355. */
  356. #define USB_TOGGLE_TXDTS(eptn) (USB->ept[eptn] = ((USB->ept[eptn] & USB_EPT_BIT_MASK) | USB_TXDTS | USB_RXTC | USB_TXTC))
  357. #define USB_TOGGLE_RXDTS(eptn) (USB->ept[eptn] = ((USB->ept[eptn] & USB_EPT_BIT_MASK) | USB_RXDTS | USB_RXTC | USB_TXTC))
  358. /**
  359. * @brief clear usb tx/rx toggle
  360. * @param eptn: endpoint number
  361. * @retval none
  362. */
  363. #define USB_CLEAR_TXDTS(eptn) { \
  364. if(USB->ept_bit[eptn].txdts != 0) \
  365. USB_TOGGLE_TXDTS(eptn); \
  366. }
  367. #define USB_CLEAR_RXDTS(eptn) { \
  368. if(USB->ept_bit[eptn].rxdts != 0) \
  369. USB_TOGGLE_RXDTS(eptn); \
  370. }
  371. /**
  372. * @brief set usb endpoint type
  373. */
  374. /**
  375. * @brief set usb transfer type
  376. * @param eptn: endpoint number
  377. * @param type: transfer type
  378. * @retval none
  379. */
  380. #define USB_SET_TRANS_TYPE(eptn, type) (USB->ept[eptn] = (USB->ept[eptn] & USB_EPT_BIT_MASK & (~USB_TRANS_TYPE)) | type)
  381. /**
  382. * @brief set/clear usb extend function
  383. * @param eptn: endpoint number
  384. * @retval none
  385. */
  386. #define USB_SET_EXF(eptn) (USB->ept[eptn] = USB_TXTC | USB_RXTC | ((USB->ept[eptn] | USB_EXF) & USB_EPT_BIT_MASK))
  387. #define USB_CLEAR_EXF(eptn) (USB->ept[eptn] = USB_TXTC | USB_RXTC | (USB->ept[eptn] & ((~USB_EXF) & USB_EPT_BIT_MASK)))
  388. /**
  389. * @brief set usb device address
  390. * @param eptn: endpoint number
  391. * @param address: device address
  392. * @retval none
  393. */
  394. #define USB_SET_EPT_ADDRESS(eptn, address) (USB->ept[eptn] = ((USB->ept[eptn] & USB_EPT_BIT_MASK & (~USB_EPTADDR)) | address))
  395. /**
  396. * @brief free buffer used by application
  397. * @param eptn: endpoint number
  398. * @param inout: transfer direction
  399. * @retval none
  400. */
  401. #define USB_FREE_DB_USER_BUFFER(eptn, inout) { \
  402. if(inout == DATA_TRANS_IN) \
  403. { \
  404. USB_TOGGLE_RXDTS(eptn); \
  405. } \
  406. else \
  407. { \
  408. USB_TOGGLE_TXDTS(eptn); \
  409. } \
  410. }
  411. /**
  412. * @brief clear tx/rx transfer completed flag
  413. * @param eptn: endpoint number
  414. * @retval none
  415. */
  416. #define USB_CLEAR_TXTC(eptn) (USB->ept[eptn] &= 0xFF7F & USB_EPT_BIT_MASK)
  417. #define USB_CLEAR_RXTC(eptn) (USB->ept[eptn] &= 0x7FFF & USB_EPT_BIT_MASK)
  418. /**
  419. * @brief set/clear endpoint double buffer mode
  420. * @param eptn: endpoint number
  421. * @retval none
  422. */
  423. #define USB_SET_EPT_DOUBLE_BUFFER(eptn) USB_SET_EXF(eptn)
  424. #define USB_CLEAR_EPT_DOUBLE_BUFFER(eptn) USB_CLEAR_EXF(eptn)
  425. /**
  426. * @}
  427. */
  428. /** @defgroup USB_exported_types
  429. * @{
  430. */
  431. /**
  432. * @brief usb endpoint infomation structure definition
  433. */
  434. typedef struct
  435. {
  436. uint8_t eptn; /*!< endpoint register number (0~7) */
  437. uint8_t ept_address; /*!< endpoint address */
  438. uint8_t inout; /*!< endpoint dir DATA_TRANS_IN or DATA_TRANS_OUT */
  439. uint8_t trans_type; /*!< endpoint type:
  440. EPT_CONTROL_TYPE, EPT_BULK_TYPE, EPT_INT_TYPE, EPT_ISO_TYPE*/
  441. uint16_t tx_addr; /*!< endpoint tx buffer offset address */
  442. uint16_t rx_addr; /*!< endpoint rx buffer offset address */
  443. uint16_t maxpacket; /*!< endpoint max packet*/
  444. uint8_t is_double_buffer; /*!< endpoint double buffer flag */
  445. uint8_t stall; /*!< endpoint is stall state */
  446. uint16_t status; /*!< endpoint status */
  447. /* transmission buffer and count */
  448. uint16_t total_len; /*!< endpoint transmission total length */
  449. uint16_t trans_len; /*!< endpoint transmission length*/
  450. uint8_t *trans_buf; /*!< endpoint transmission buffer */
  451. uint16_t last_len; /*!< last transfer length */
  452. uint16_t rem0_len; /*!< rem transfer length */
  453. uint16_t ept0_slen; /*!< endpoint 0 transfer sum length */
  454. }usb_ept_info;
  455. /**
  456. * @brief type define usb register all
  457. */
  458. typedef struct
  459. {
  460. /**
  461. * @brief usb endpoint register, offset:0x00
  462. */
  463. union
  464. {
  465. __IO uint32_t ept[8];
  466. struct
  467. {
  468. __IO uint32_t eptaddr : 4; /* [3:0] */
  469. __IO uint32_t txsts : 2; /* [5:4] */
  470. __IO uint32_t txdts : 1; /* [6] */
  471. __IO uint32_t txtc : 1; /* [7] */
  472. __IO uint32_t exf : 1; /* [8] */
  473. __IO uint32_t trans_type : 2; /* [10:9] */
  474. __IO uint32_t setuptc : 1; /* [11] */
  475. __IO uint32_t rxsts : 2; /* [13:12] */
  476. __IO uint32_t rxdts : 1; /* [14] */
  477. __IO uint32_t rxtc : 1; /* [15] */
  478. __IO uint32_t reserved1 : 16; /* [31:16] */
  479. } ept_bit[8];
  480. };
  481. __IO uint32_t reserved1[8];
  482. /**
  483. * @brief usb control register, offset:0x40
  484. */
  485. union
  486. {
  487. __IO uint32_t ctrl;
  488. struct
  489. {
  490. __IO uint32_t csrst : 1; /* [0] */
  491. __IO uint32_t disusb : 1; /* [1] */
  492. __IO uint32_t lpm : 1; /* [2] */
  493. __IO uint32_t ssp : 1; /* [3] */
  494. __IO uint32_t gresume : 1; /* [4] */
  495. __IO uint32_t reserved1 : 3; /* [7:5] */
  496. __IO uint32_t lsofien : 1; /* [8] */
  497. __IO uint32_t sofien : 1; /* [9] */
  498. __IO uint32_t rstien : 1; /* [10] */
  499. __IO uint32_t spien : 1; /* [11] */
  500. __IO uint32_t wkien : 1; /* [12] */
  501. __IO uint32_t beien : 1; /* [13] */
  502. __IO uint32_t ucforien : 1; /* [14] */
  503. __IO uint32_t tcien : 1; /* [15] */
  504. __IO uint32_t reserved2 : 16; /* [31:16] */
  505. } ctrl_bit;
  506. };
  507. /**
  508. * @brief usb interrupt status register, offset:0x44
  509. */
  510. union
  511. {
  512. __IO uint32_t intsts;
  513. struct
  514. {
  515. __IO uint32_t ept_num : 4; /* [3:0] */
  516. __IO uint32_t inout : 1; /* [4] */
  517. __IO uint32_t reserved1 : 3; /* [7:5] */
  518. __IO uint32_t lsof : 1; /* [8] */
  519. __IO uint32_t sof : 1; /* [9] */
  520. __IO uint32_t rst : 1; /* [10] */
  521. __IO uint32_t sp : 1; /* [11] */
  522. __IO uint32_t wk : 1; /* [12] */
  523. __IO uint32_t be : 1; /* [13] */
  524. __IO uint32_t ucfor : 1; /* [14] */
  525. __IO uint32_t tc : 1; /* [15] */
  526. __IO uint32_t reserved2 : 16; /* [31:16] */
  527. } intsts_bit;
  528. };
  529. /**
  530. * @brief usb frame number register, offset:0x48
  531. */
  532. union
  533. {
  534. __IO uint32_t sofrnum;
  535. struct
  536. {
  537. __IO uint32_t sofnum : 11; /* [10:0] */
  538. __IO uint32_t lsofnum : 2; /* [12:11] */
  539. __IO uint32_t clck : 1; /* [13] */
  540. __IO uint32_t dmsts : 1; /* [14] */
  541. __IO uint32_t dpsts : 1; /* [15] */
  542. __IO uint32_t reserved1 : 16; /* [31:16] */
  543. } sofrnum_bit;
  544. };
  545. /**
  546. * @brief usb device address register, offset:0x4c
  547. */
  548. union
  549. {
  550. __IO uint32_t devaddr;
  551. struct
  552. {
  553. __IO uint32_t addr : 7; /* [6:0] */
  554. __IO uint32_t cen : 1; /* [7] */
  555. __IO uint32_t reserved1 : 24; /* [31:8] */
  556. } devaddr_bit;
  557. };
  558. /**
  559. * @brief usb buffer address register, offset:0x50
  560. */
  561. union
  562. {
  563. __IO uint32_t buftbl;
  564. struct
  565. {
  566. __IO uint32_t reserved1 : 3; /* [2:0] */
  567. __IO uint32_t btaddr : 13; /* [15:3] */
  568. __IO uint32_t reserved2 : 16; /* [31:16] */
  569. } buftbl_bit;
  570. };
  571. __IO uint32_t reserved2[3];
  572. /**
  573. * @brief usb cfg control register, offset:0x60
  574. */
  575. union
  576. {
  577. __IO uint32_t cfg;
  578. struct
  579. {
  580. __IO uint32_t sofouten : 1; /* [0] */
  581. __IO uint32_t reserved1 : 31; /* [31:1] */
  582. } cfg_bit;
  583. };
  584. } usbd_type;
  585. /**
  586. * @}
  587. */
  588. #define USB ((usbd_type *) USBFS_BASE)
  589. typedef usbd_type usb_reg_type;
  590. extern uint32_t g_usb_packet_address;
  591. /** @defgroup USB_exported_functions
  592. * @{
  593. */
  594. void usb_dev_init(usbd_type *usbx);
  595. void usb_connect(usbd_type *usbx);
  596. void usb_disconnect(usbd_type *usbx);
  597. void usb_usbbufs_enable(usbd_type *usbx, confirm_state state);
  598. void usb_ept_open(usbd_type *usbx, usb_ept_info *ept_info);
  599. void usb_ept_close(usbd_type *usbx, usb_ept_info *ept_info);
  600. void usb_write_packet(uint8_t *pusr_buf, uint16_t offset_addr, uint16_t nbytes);
  601. void usb_read_packet(uint8_t *pusr_buf, uint16_t offset_addr, uint16_t nbytes);
  602. void usb_interrupt_enable(usbd_type *usbx, uint16_t interrupt, confirm_state new_state);
  603. void usb_set_address(usbd_type *usbx, uint8_t address);
  604. void usb_ept_stall(usbd_type *usbx, usb_ept_info *ept_info);
  605. void usb_enter_suspend(usbd_type *usbx);
  606. void usb_exit_suspend(usbd_type *usbx);
  607. void usb_remote_wkup_set(usbd_type *usbx);
  608. void usb_remote_wkup_clear(usbd_type *usbx);
  609. uint16_t usb_buffer_malloc(uint16_t maxpacket);
  610. void usb_buffer_free(void);
  611. flag_status usb_flag_get(usbd_type *usbx, uint16_t flag);
  612. void usb_flag_clear(usbd_type *usbx, uint16_t flag);
  613. #ifdef __cplusplus
  614. }
  615. #endif
  616. #endif
  617. /**
  618. * @}
  619. */
  620. /**
  621. * @}
  622. */
  623. /**
  624. * @}
  625. */