cmem7_usb.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /**
  2. *****************************************************************************
  3. * @file cmem7_usb.h
  4. *
  5. * @brief CMEM7 USB header file
  6. *
  7. *
  8. * @version V1.0
  9. * @date 3. September 2013
  10. *
  11. * @note
  12. *
  13. *****************************************************************************
  14. * @attention
  15. *
  16. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  17. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  18. * TIME. AS A RESULT, CAPITAL-MICRO SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
  19. * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  20. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  21. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  22. *
  23. * <h2><center>&copy; COPYRIGHT 2013 Capital-micro </center></h2>
  24. *****************************************************************************
  25. */
  26. #ifndef __CMEM7_USB_H
  27. #define __CMEM7_USB_H
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. #include "cmem7.h"
  32. #include "cmem7_conf.h"
  33. #include "string.h"
  34. /**
  35. *
  36. */
  37. #define SET_HCDMA_DESC_ADDR(a) (((uint32_t)(a)) >> 9)
  38. #define MIN(a, b) (((a) <= (b)) ? (a) : (b))
  39. #define MAX(a, b) (((a) >= (b)) ? (a) : (b))
  40. #define BIT(b) (0x1u << (b))
  41. /** @defgroup USB_HOST_PID
  42. * @{
  43. */
  44. #define USB_HOST_PID_DATA0 0x0 /*!< Indicates the Data PID is DATA0 */
  45. #define USB_HOST_PID_DATA2 0x1 /*!< Indicates the Data PID is DATA2 */
  46. #define USB_HOST_PID_DATA1 0x2 /*!< Indicates the Data PID is DATA1 */
  47. #define USB_HOST_PID_MDATA 0x3 /*!< Indicates the Data PID is MDATA (non-control) */
  48. #define USB_HOST_PID_SETUP 0x3 /*!< Indicates the Data PID is SETUP (control) */
  49. /**
  50. * @}
  51. */
  52. /** @defgroup USB_EP_TYPE
  53. * @{
  54. */
  55. typedef enum {
  56. USB_EP_TYPE_CONTROL = 0x0, /*!< Control */
  57. USB_EP_TYPE_ISO = 0x1, /*!< Isochronous */
  58. USB_EP_TYPE_BULK = 0x2, /*!< Bulk */
  59. USB_EP_TYPE_INT = 0x3, /*!< Interrupt */
  60. } USB_EP_TYPE;
  61. /**
  62. * @}
  63. */
  64. /** @defgroup USB_ENUM_SPEED
  65. * @{
  66. */
  67. typedef enum {
  68. USB_ENUM_SPEED_HS = 0x0, /*!< Enumerated Speed is High Speed */
  69. USB_ENUM_SPEED_FS = 0x1, /*!< Enumerated Speed is Full Speed */
  70. USB_ENUM_SPEED_LS = 0x2, /*!< Enumerated Speed is Low Speed */
  71. USB_ENUM_SPEED_FS_48M = 0x3, /*!< Enumerated Speed is Full Speed (PHY clock is running at 48MHz) */
  72. } USB_ENUM_SPEED;
  73. /**
  74. * @}
  75. */
  76. /** @defgroup USB_INT_GP
  77. * @{
  78. */
  79. typedef enum {
  80. USB_INT_GP_HOST_DISC, /*!< Device disconnection interrupt (Only for HOST Mode) */
  81. USB_INT_GP_DEV_RESET, /*!< USB Port Reset Interrupt (Only for DEVICE Mode) */
  82. USB_INT_GP_DEV_ENUMDONE, /*!< Enumeration Done Interrupt (Only for DEVICE Mode) */
  83. USB_INT_GP_DEV_SUSP, /*!< USB Suspend Interrupt (Only for DEVICE Mode) */
  84. USB_INT_GP_DEV_EARLY, /*!< USB Idle Interrupt (Only for DEVICE Mode) */
  85. USB_INT_GP_SOF, /*!< SOF Interrupt */
  86. USB_INT_GP_MIS, /*!< USB access overstep the boundary Interrupt */
  87. USB_INT_GP_IDCHG, /*!< OTG Connector ID Status Change Interrupt */
  88. USB_INT_GP_SESSREQ, /*!< Session Request / Create Interrupt */
  89. } USB_INT_GP;
  90. /**
  91. * @}
  92. */
  93. /** @defgroup USB_INT_OTG
  94. * @{
  95. */
  96. typedef enum {
  97. USB_INT_OTG_SESEND, /*!< Session End Interrupt */
  98. USB_INT_OTG_STANDAUP, /*!< B Device timeout to connect Interrupt */
  99. USB_INT_OTG_HNDETECT, /*!< Host Negotiation Detected Interrupt */
  100. USB_INT_OTG_HNSUCCHG, /*!< Host Negotiation Success Status Change Interrupt */
  101. USB_INT_OTG_KEEPAPP, /*!< Debounce Done Interrupt (Only for HOST Mode) */
  102. } USB_INT_OTG;
  103. /**
  104. * @}
  105. */
  106. /** @defgroup USB_OTG_CTL
  107. * @{
  108. */
  109. typedef enum {
  110. USB_OTG_DEV_HNSUCC = 8, /*!< Host Negotiation Success (Only for DEVICE Mode, Read Only) */
  111. USB_OTG_DEV_HNPREQ = 9, /*!< HNP Request (Only for DEVICE Mode) */
  112. USB_OTG_HST_HNPENABLE = 10, /*!< Host Set HNP Enable (Only for HOST Mode) */
  113. USB_OTG_DEV_HNPENABLE = 11, /*!< Device HNP Enabled (Only for DEVICE Mode) */
  114. } USB_OTG_CTL;
  115. /**
  116. * @}
  117. */
  118. typedef union {
  119. __IO uint32_t HPRT; /*!< Host Port Control and Status Register */
  120. struct {
  121. __I uint32_t PCS : 1; /*!< If a device is attached to the port */
  122. __IO uint32_t PCD : 1; /*!< A device connection is detected */
  123. __IO uint32_t PE : 1; /*!< Port Enable */
  124. __IO uint32_t PEDC : 1; /*!< Set if when the status of the Port Enable (bit 2) of this register
  125. changes */
  126. __I uint32_t POA : 1; /*!< Indicates the overcurrent condition of the port */
  127. __IO uint32_t POC : 1; /*!< Set if when the status of the Port Overcurrent Active bit (bit
  128. 4) in this register changes */
  129. __IO uint32_t PR : 1; /*!< Application and Core all can perform resume by setting, then
  130. clear it whatever resume is success or not */
  131. __IO uint32_t PS : 1; /*!< Sets this bit to put this port in Suspend mode */
  132. __IO uint32_t PRESET : 1; /*!< sets this bit, a reset sequence is started on this port */
  133. uint32_t : 1;
  134. __IO uint32_t PLSDP : 1; /*!< Logic level of D+ */
  135. __IO uint32_t PLSDN : 1; /*!< Logic level of D- */
  136. __IO uint32_t PP : 1; /*!< this field to control power to this port. 1, power on */
  137. __IO uint32_t PTC : 4; /*!< The application writes a nonzero value to this field to put
  138. the port into a Test mode */
  139. __I uint32_t SPEED : 2; /*!< Indicates the speed of the device attached to this port */
  140. } HPRT_b; /*!< BitSize */
  141. } USB_REG_HPRT;
  142. typedef struct {
  143. union {
  144. uint32_t VALUE; /*!< provide the status of the buffer */
  145. struct {
  146. __IO uint32_t SIZE : 17; /*!< Total bytes to transfer for OUT; the expected transfer size for IN */
  147. __IO uint32_t AQTD : 6; /*!< IN Only, Alternated Queue Transfer Descriptor Valid */
  148. __IO uint32_t AQTD_VLD : 1; /*!< IN Only, Alternated Queue Transfer Descriptor Valid */
  149. __IO uint32_t SUP : 1; /*!< OUT Only, it indicates that the buffer data pointed by this descriptor
  150. is a setup packet of 8 bytes */
  151. __IO uint32_t IOC : 1; /*!< It indicates that that the core must generate a XferCompl interrupt */
  152. __IO uint32_t EOL : 1; /*!< It indicates that this is the last descriptor in the list */
  153. uint32_t : 1;
  154. __IO uint32_t STS : 2; /*!< The status of the rx/tx data:
  155. 00=Success; 01=PKTERR; 10=Reserved; 11=Reserved */
  156. uint32_t : 1;
  157. __IO uint32_t A : 1; /*!< Active: 0=descriptor is not ready; 1=descriptor is ready */
  158. } HOST_NISO_b; /*!< BitSize */
  159. struct {
  160. __IO uint32_t SIZE : 16; /*!< Total bytes to transfer for OUT; the expected transfer size for IN */
  161. uint32_t : 7;
  162. __IO uint32_t MTRF : 1; /*!< IN Only, Alternated Queue Transfer Descriptor Valid */
  163. __IO uint32_t SR : 1; /*!< IN Only, Alternated Queue Transfer Descriptor Valid */
  164. __IO uint32_t IOC : 1; /*!< It indicates that that the core must generate a XferCompl interrupt */
  165. __IO uint32_t SP : 1; /*!< It indicates that this is the last descriptor in the list */
  166. __IO uint32_t L : 1; /*!< It indicates that this is the last descriptor in the list */
  167. __IO uint32_t STS : 2; /*!< The status of the rx/tx data:
  168. 00=Success; 01=PKTERR; 10=Reserved; 11=Reserved */
  169. __IO uint32_t BS : 2; /*!< Active: 0=descriptor is not ready; 1=descriptor is ready */
  170. } DEV_NISO_OUT_b; /*!< BitSize */
  171. struct {
  172. __IO uint32_t SIZE : 16; /*!< Total bytes to transfer for OUT; the expected transfer size for IN */
  173. uint32_t : 7;
  174. __IO uint32_t PID : 2; /*!< IN Only, Alternated Queue Transfer Descriptor Valid */
  175. __IO uint32_t IOC : 1; /*!< It indicates that that the core must generate a XferCompl interrupt */
  176. __IO uint32_t SP : 1; /*!< It indicates that this is the last descriptor in the list */
  177. __IO uint32_t L : 1; /*!< It indicates that this is the last descriptor in the list */
  178. __IO uint32_t STS : 2; /*!< The status of the rx/tx data:
  179. 00=Success; 01=PKTERR; 10=Reserved; 11=Reserved */
  180. __IO uint32_t BS : 2; /*!< Active: 0=descriptor is not ready; 1=descriptor is ready */
  181. } DEV_NISO_IN_b; /*!< BitSize */
  182. } QUADLET;
  183. uint32_t BUIFFER; /*!< points to a data buffer */
  184. } OTG_DESCRIPTOR;
  185. /**
  186. * @brief Get OTG Connector ID Status (Is B-Device)
  187. * @retval BOOL TRUE: High; FALSE: Low
  188. */
  189. BOOL USB_ogtIsBdevID(void);
  190. /**
  191. * @brief Set USB Global Interrupt Enable
  192. * @param[in] enable TRUE: Enable; FALSE: Disable
  193. * @retval void
  194. */
  195. void USB_EnableInt(BOOL enable);
  196. /**
  197. * @brief Flush TX/RX FIFO
  198. * @param[in] num Flush FIFO£¬0: non-periodic TX FIFO (HOST Mode) or FIFO 0 (DEVICE Mode); 1: Periodic TX FIFO (HOST Mode) or FIFO 1 (DEVICE Mode); 2-15: FIFO n in DEVICE Mode; 16: Flush All TX FIFO; otherwise: Flush RX FIFO
  199. * @retval void
  200. */
  201. void USB_FlushFIFO(uint32_t num);
  202. /**
  203. * @brief Initialize USB
  204. * @param[in] type USB Mode, Bit0: Force HOST Mode; Bit1: Force DEVICE Mode; Bit4: Enable SRP; Bit5: Enable HNP
  205. * @retval void
  206. */
  207. void USB_coreInit(uint32_t type);
  208. /**
  209. * @brief Get USB Mode (Is Host Mode?)
  210. * @retval BOOL TRUE: HOST Mode; FALSE: DEVICE Mode
  211. */
  212. BOOL USB_roleIsHost(void);
  213. /**
  214. * @brief Control and get VBus Status (Only for HOST Mode)
  215. * @param[in] opt Bit1: Set VBus using Bit0; Bit0: Turn VBus On or Off
  216. * @retval BOOL TRUE: VBus is on; FALSE£ºVBus is off
  217. * @note It cannot control VBus actually due to HW problem
  218. */
  219. BOOL USB_hostVBus(uint32_t opt);
  220. /**
  221. * @brief Initialize USB HOST Module (Only for HOST Mode)
  222. * @retval void
  223. */
  224. void USB_hostInit(void);
  225. /**
  226. * @brief Set USB Port Reset Status (Only for HOST Mode)
  227. * @param[in] rst TRUE: Port is reseting; FALSE: Port stop reseting
  228. * @retval void
  229. */
  230. void USB_HostResetPort(BOOL rst);
  231. /**
  232. * @brief Frame Number of the next SOF (Only for HOST Mode)
  233. * @retval uint16_t Frame Number of the next SOF will be send
  234. */
  235. uint16_t USB_HostGetCurFrame(void);
  236. /**
  237. * @brief Suspend USP Port (Only for HOST Mode)
  238. * @retval void
  239. */
  240. void USB_HostSuspendPort(void);
  241. /**
  242. * @brief Get the device enumerated speed (Only for HOST Mode)
  243. * @retval USB_ENUM_SPEED A value of @ref USB_ENUM_SPEED defined
  244. */
  245. USB_ENUM_SPEED USB_hostGetEnumSpd(void);
  246. /**
  247. * @brief Get USB Port Connection Status (Only for HOST Mode)
  248. * @retval BOOL TRUE: A device is attached; FALSE: No device is attached
  249. */
  250. BOOL USB_hostPrtConn(void);
  251. /**
  252. * @brief Channel 0 Transaction (Only for HOST Mode)
  253. * @param[in] devaddr USB Device Address
  254. * @param[in] desc A pointer to DMA Descriptor (512-byte alignment)
  255. * @param[in] ctd In terms of number of DMA descriptors (0 means start from 1st DMA descriptor)
  256. * @param[in] ntd Number of Transfer Descriptors (from 0 to 63 which means from 1 to 64 descriptors)
  257. * @param[in] ping TRUE: Do PING protocol (Only for OUT Transfer); Must be 0 for IN Transfer
  258. * @param[in] pid PID: A value of @ref USB_HOST_PID defined: USB_HOST_PID_DATA0, USB_HOST_PID_DATA2, USB_HOST_PID_DATA1, USB_HOST_PID_MDATA or USB_HOST_PID_SETUP
  259. * @param[in] mps Maximum Packet Size (in bytes)
  260. * @param[in] epnum Endpoint Address
  261. * @param[in] in Is IN Transfer, TRUE: IN Transfer; FALSE: OUT Transfer
  262. * @param[in] eptype Endpoint Type, A value of @ref USB_EP_TYPE defined
  263. * @param[in] speed Device Speed, A value of @ref USB_ENUM_SPEED defined
  264. */
  265. void USB_hostCH0(uint32_t devaddr, OTG_DESCRIPTOR *desc, uint32_t ctd, uint32_t ntd, BOOL ping, uint32_t pid, uint32_t mps, uint32_t epnum, BOOL in, USB_EP_TYPE eptype, USB_ENUM_SPEED speed);
  266. /**
  267. * @brief Channel 1 Transaction (Only for HOST Mode)
  268. * @see USB_hostCH0
  269. */
  270. void USB_hostCH1(uint32_t devaddr, OTG_DESCRIPTOR *desc, uint32_t ctd, uint32_t ntd, BOOL ping, uint32_t pid, uint32_t mps, uint32_t epnum, BOOL in, USB_EP_TYPE eptype, USB_ENUM_SPEED speed);
  271. /**
  272. * @brief Channel 2 Transaction (Only for HOST Mode)
  273. * @see USB_hostCH0
  274. */
  275. void USB_hostCH2(uint32_t devaddr, OTG_DESCRIPTOR *desc, uint32_t ctd, uint32_t ntd, BOOL ping, uint32_t pid, uint32_t mps, uint32_t epnum, BOOL in, USB_EP_TYPE eptype, USB_ENUM_SPEED speed);
  276. /**
  277. * @brief Channel n Transaction (Only for HOST Mode)
  278. * @param[in] ch Channel number will be used
  279. * @param[in] devaddr USB Device Address
  280. * @param[in] desc A pointer to DMA Descriptor (512-byte alignment)
  281. * @param[in] ctd In terms of number of DMA descriptors (0 means start from 1st DMA descriptor)
  282. * @param[in] ntd Number of Transfer Descriptors (from 0 to 63 which means from 1 to 64 descriptors)
  283. * @param[in] ping TRUE: Do PING protocol (Only for OUT Transfer); Must be 0 for IN Transfer
  284. * @param[in] pid PID: A value of @ref USB_HOST_PID defined: USB_HOST_PID_DATA0, USB_HOST_PID_DATA2, USB_HOST_PID_DATA1, USB_HOST_PID_MDATA or USB_HOST_PID_SETUP
  285. * @param[in] mps Maximum Packet Size (in bytes)
  286. * @param[in] epnum Endpoint Address
  287. * @param[in] in Is IN Transfer, TRUE: IN Transfer; FALSE: OUT Transfer
  288. * @param[in] eptype Endpoint Type, A value of @ref USB_EP_TYPE defined
  289. * @param[in] speed Device Speed, A value of @ref USB_ENUM_SPEED defined
  290. * @retval int returns 0 if success, otherwise returns a negative value
  291. */
  292. int USB_hostCHn(uint32_t ch, uint32_t devaddr, OTG_DESCRIPTOR *desc, uint32_t ctd, uint32_t ntd, BOOL ping, uint32_t pid, uint32_t mps, uint32_t epnum, BOOL in, USB_EP_TYPE eptype, USB_ENUM_SPEED speed);
  293. /**
  294. * @brief Halt Channel n (Only for HOST Mode)
  295. * @param[in] ch Channel number will be halted
  296. * @retval int returns 0 if success, otherwise returns a negative value
  297. */
  298. int USB_hostCHnHalt(uint32_t ch);
  299. /**
  300. * @brief Disable USB Port (Only for HOST Mode)
  301. * @param[in] dis TRUE: Disable USB Port; FALSE: Do NOT Disable USB Port
  302. * @retval BOOL If USB Port is disabled, TRUE: USB Port Disabled; FALSE: USB Port Enabled
  303. * @note It CANNOT Enable USB Port; Port will be enabled automatically after the port was reset successfully.
  304. */
  305. BOOL USB_hostPortDisable(BOOL dis);
  306. /**
  307. * @brief Enable Connection Interrupt (Only for HOST Mode)
  308. * @param[in] en TRUE: Enable Interrupt; FALSE: Mask the Interrupt
  309. * @retval void
  310. */
  311. void USB_hostINT_enConn(BOOL en);
  312. /**
  313. * @brief Connection Interrupt Asserted (Only for HOST Mode)
  314. * @retval BOOL TRUE: Interrupt Asserted; FALSE: Interrupt is NOT asserted
  315. * @note PCD or PEDC asserted, please call USB_hostINT_clrPCD or USB_hostINT_clrPEDC function to clear it.
  316. */
  317. BOOL USB_hostINT_isConn(void);
  318. /**
  319. * @brief Port Connection Detected (PCD) Interrupt Asserted (Only for HOST Mode)
  320. * @retval BOOL TRUE: Interrupt Asserted; FALSE: Interrupt is NOT asserted
  321. */
  322. BOOL USB_hostINT_isPCD(void);
  323. /**
  324. * @brief Clear Port Connection Detected (PCD) Interrupt Flag (Only for HOST Mode)
  325. * @retval void
  326. */
  327. void USB_hostINT_clrPCD(void);
  328. /**
  329. * @brief Port Enable/Disable Change£¨PEDC£©Interrupt Asserted (Only for HOST Mode)
  330. * @retval BOOL TRUE: Interrupt Asserted; FALSE: Interrupt is NOT asserted
  331. */
  332. BOOL USB_hostINT_isPEDC(void);
  333. /**
  334. * @brief Clear Port Enable/Disable Change£¨PEDC£©Interrupt Flag (Only for HOST Mode)
  335. * @retval void
  336. */
  337. void USB_hostINT_clrPEDC(void);
  338. /**
  339. * @brief Enable Transaction Done Interrupt (Only for HOST Mode)
  340. * @param[in] ch Channel to enable
  341. * @para in en TRUE: Enable Interrupt; FALSE: Mask the Interrupt
  342. * @retval int returns 0 if success, otherwise returns a negative value
  343. */
  344. int USB_hostINT_enDone(uint32_t ch, BOOL en);
  345. /**
  346. * @brief Transaction Done Interrupt Asserted (Only for HOST Mode)
  347. * @param[in] ch Channel to check
  348. * @retval uint32_t result, Bit0: Done; Bit1: Buffer Not Available Error; Bit2: Channel Idle; Bit3: Transaction Error
  349. */
  350. uint32_t USB_hostINT_isDone(uint32_t ch);
  351. /**
  352. * @brief Transaction Done Interrupt Asserted (Only for HOST Mode)
  353. * @param[in] ch Channel to check
  354. * @retval uint32_t result, 0: No interrupt is pendding
  355. */
  356. uint32_t USB_hostINT_isPend(uint32_t ch);
  357. /**
  358. * @brief Initialize USB DEVICE Module (Only for DEVICE Mode)
  359. * @retval void
  360. */
  361. void USB_devInit(void);
  362. /**
  363. * @brief Get the device enumerated speed (Only for DEVICE Mode)
  364. * @retval USB_ENUM_SPEED A value of @ref USB_ENUM_SPEED defined
  365. */
  366. USB_ENUM_SPEED USB_devGetEnumSpd(void);
  367. /**
  368. * @brief Set Device Address (Only for DEVICE Mode)
  369. * @param[in] Device Address
  370. * @retval void
  371. */
  372. void USB_devSetAddress(uint32_t addr);
  373. /**
  374. * @brief Endpoint 0 OUT Transation (Only for DEVICE Mode)
  375. * @param[in] size Transation length (in bytes)
  376. * @param[in] pktcnt Packet Count
  377. * @param[in] stpcnt The number of back-to-back SETUP data packets the endpoint can receive. (0 - 3)
  378. * @param[in] desc A pointer to DMA descriptors
  379. * @param[in] snoop Snoop Mode, TRUE: Enable Snoop Mode, which means it does not check if the OUT packets are correct before transferring them to application memory; FLASE: Disable Snoop Mode
  380. * @retval void
  381. */
  382. void USB_devEP0out(uint32_t size, uint32_t pktcnt, uint32_t stpcnt, void *desc, BOOL snoop);
  383. /**
  384. * @brief Endpoint 0 IN Transation (Only for DEVICE Mode)
  385. * @param[in] size Transation length (in bytes)
  386. * @param[in] pktcnt Packet Count
  387. * @param[in] desc A pointer to DMA descriptors
  388. * @param[in] mps Maximum Packet Size (in bytes), only 8,, 32, 64 is valid
  389. * @retval BOOL TRUE: Parameter mps is valid; FLASE: Parameter mps is invalid
  390. */
  391. BOOL USB_devEP0in(uint32_t size, uint32_t pktcnt, void *desc, uint32_t mps/*8,16,32,64-byte*/);
  392. /**
  393. * @brief Active Endpoint 1 (Only for DEVICE Mode)
  394. * @param[in] in Endpoint direction, TRUE: IN; FALSE: OUT
  395. * @param[in] mps Maximum Packet Size (in bytes), 0 means inactive the endpoint
  396. * @param[in] type Endpoint type, A value of @ref USB_EP_TYPE defined
  397. * @retval void
  398. * @note All Endpoint but EP0 will be inactived after USB Port reseted
  399. */
  400. void USB_devActEP1(const BOOL in, const uint32_t mps, USB_EP_TYPE type);
  401. /**
  402. * @brief Active Endpoint 2 (Only for DEVICE Mode)
  403. * @see USB_devActEP1
  404. */
  405. void USB_devActEP2(const BOOL in, const uint32_t mps, USB_EP_TYPE type);
  406. /**
  407. * @brief Endpoint 1 IN Transation (Only for DEVICE Mode)
  408. * @param[in] size Maximum Packet Size (in bytes)
  409. * @param[in] pktcnt Packet Count
  410. * @param[in] pid PID (only for interrupt/bulk), 0x1 means DATA0; 0x2 means DATA1
  411. * @param[in] desc A pointer to DMA descriptors
  412. * @retval void
  413. * @see
  414. */
  415. void USB_devEP1in(uint32_t size, uint32_t pktcnt, uint32_t pid, void *desc);
  416. /**
  417. * @brief Endpoint 1 OUT Transation (Only for DEVICE Mode)
  418. * @param[in] size Data length (in bytes)
  419. * @param[in] pktcnt Packet Count
  420. * @param[in] PID (only for interrupt/bulk), 0x1 means DATA0; 0x2 means DATA1
  421. * @param[in] stpcnt The number of back-to-back SETUP data packets the endpoint can receive. (0 - 3)
  422. * @param[in] desc A pointer to DMA descriptors
  423. * @param[in] snoop Snoop Mode, TRUE: Enable Snoop Mode, which means it does not check if the OUT packets are correct before transferring them to application memory; FLASE: Disable Snoop Mode
  424. * @retval void
  425. */
  426. void USB_devEP1out(uint32_t size, uint32_t pktcnt, uint32_t pid, uint32_t stpcnt, void *desc, BOOL snoop);
  427. /**
  428. * @brief Endpoint 2 IN Transation (Only for DEVICE Mode)
  429. * @see USB_devEP1in
  430. */
  431. void USB_devEP2in(uint32_t size, uint32_t pktcnt, uint32_t pid, void *desc);
  432. /**
  433. * @brief Endpoint 2 OUT Transation (Only for DEVICE Mode)
  434. * @see USB_devEP1out
  435. */
  436. void USB_devEP2out(uint32_t size, uint32_t pktcnt, uint32_t pid, uint32_t stpcnt, void *desc, BOOL snoop);
  437. /**
  438. * @brief Set NAK handshake (Only for DEVICE Mode)
  439. * @param[in] ep Endpoint
  440. * @param[in] in Endpoint Direction, TRUE: IN; FALSE: OUT
  441. * @param[in] en TRUE: Enable NAK handshake; FALSE: Disable NAK handshake
  442. * @retval void
  443. */
  444. void USB_devNAKhandshake(uint32_t ep, BOOL in, BOOL en);
  445. /**
  446. * @brief Set STALL handshake (Only for DEVICE Mode)
  447. * @param[in] ep Endpoint
  448. * @param[in] in Endpoint Direction, TRUE: IN; FALSE: OUT
  449. * @param[in] en TRUE: Enable STALL handshake; FALSE: Disable STALL handshake
  450. * @retval BOOL Return the old status before en is set.
  451. * @note Not for ISO Endpoint; For Endpoint 0 it clears itself when a SETUP token is received.
  452. */
  453. BOOL USB_devSTALLhandshake(uint32_t ep, BOOL in, BOOL en);
  454. /**
  455. * @brief Enable Transaction Done Interrupt (Only for DEVICE Mode)
  456. * @param[in] ep Endpoint
  457. * @param[in] in Endpoint Direction, TRUE: IN; FALSE: OUT
  458. * @para in en TRUE: Enable Interrupt; FALSE: Mask the Interrupt
  459. * @retval void
  460. */
  461. void USB_devINT_enDone(uint32_t ep, BOOL in, BOOL en);
  462. /**
  463. * @brief Transaction Done Interrupt Asserted (Only for DEVICE Mode)
  464. * @param[in] ep Endpoint
  465. * @param[in] in Endpoint Direction, TRUE: IN; FALSE: OUT
  466. * @retval uint32_t Result, Bit0: Done; Bit1£ºBuffer Not Available Error; Bit2: SETUP Phase Done
  467. */
  468. uint32_t USB_devINT_isDone(uint32_t ep, BOOL in);
  469. /**
  470. * @brief Enable OTG Interrupt (Only for DEVICE Mode)
  471. * @para in en TRUE: Enable Interrupt; FALSE: Mask the Interrupt
  472. * @retval void
  473. */
  474. void USB_INT_enOTG(BOOL en);
  475. /**
  476. * @brief OTG Interrupt Asserted
  477. * @retval BOOL TRUE: Interrupt Asserted; FALSE: Interrupt is NOT asserted
  478. */
  479. BOOL USB_INT_isOTG(void);
  480. /**
  481. * @brief OTG Function Interrupt Asserted
  482. * @param[in] otg OTG Function Interrupt to check, A value of @ref USB_INT_OTG defined
  483. * @retval BOOL TRUE: Interrupt Asserted; FALSE: Interrupt is NOT asserted
  484. */
  485. BOOL USB_INT_isOTGon(USB_INT_OTG otg);
  486. /**
  487. * @brief Clear OTG Function Interrupt Flag
  488. * @param[in] otg OTG Function Interrupt to clear, A value of @ref USB_INT_OTG defined
  489. * @retval void
  490. */
  491. void USB_INT_clrOTGon(USB_INT_OTG otg);
  492. /**
  493. * @brief Enable @ref USB_INT_GP Interrupt
  494. * @para in name Interrupt to operate, A value of @ref USB_INT_GP defined
  495. * @para in en TRUE: Enable Interrupt; FALSE: Mask the Interrupt
  496. * @retval void
  497. */
  498. void USB_INT_enGP(USB_INT_GP name, BOOL en);
  499. /**
  500. * @brief @ref USB_INT_GP Interrupt Asserted
  501. * @para in name Interrupt to check, A value of @ref USB_INT_GP defined
  502. * @retval BOOL TRUE: Interrupt Asserted; FALSE: Interrupt is NOT asserted
  503. */
  504. BOOL USB_INT_isGP(USB_INT_GP name);
  505. /**
  506. * @brief Clear @ref USB_INT_GP Interrupt Flag
  507. * @para in name Interrupt to clear, A value of @ref USB_INT_GP defined
  508. * @retval void
  509. */
  510. void USB_INT_clrGP(USB_INT_GP name);
  511. /**
  512. * @brief Enable OTG Function
  513. * @param[in] ctl OTG Function to operate, A value of @ref USB_OTG_CTL defined
  514. * @param[in] val TRUE: Enable the function; FALSE: Disable the function
  515. * @retval BOOL The old value before it configure
  516. */
  517. BOOL USB_otgControl(USB_OTG_CTL ctl, BOOL val);
  518. #ifdef __cplusplus
  519. }
  520. #endif
  521. #endif /* __CMEM7_USB_H */