at32f413_usb.h 24 KB

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