at32f415_usb.h 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  1. /**
  2. **************************************************************************
  3. * @file at32f415_usb.h
  4. * @version v2.0.5
  5. * @date 2022-05-20
  6. * @brief at32f415 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. /* Define to prevent recursive inclusion -------------------------------------*/
  27. #ifndef __AT32F415_USB_H
  28. #define __AT32F415_USB_H
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. /* Includes ------------------------------------------------------------------*/
  33. #include "at32f415.h"
  34. /** @addtogroup AT32F415_periph_driver
  35. * @{
  36. */
  37. /** @addtogroup USB
  38. * @{
  39. */
  40. /** @defgroup USB_global_interrupts_definition
  41. * @brief usb global interrupt mask
  42. * @{
  43. */
  44. #define USB_OTG_MODEMIS_INT ((uint32_t)0x00000002) /*!< usb otg mode mismatch interrupt */
  45. #define USB_OTG_OTGINT_INT ((uint32_t)0x00000004) /*!< usb otg interrupt */
  46. #define USB_OTG_SOF_INT ((uint32_t)0x00000008) /*!< usb otg sof interrupt */
  47. #define USB_OTG_RXFLVL_INT ((uint32_t)0x00000010) /*!< usb otg receive fifo non-empty interrupt */
  48. #define USB_OTG_NPTXFEMP_INT ((uint32_t)0x00000020) /*!< usb otg non-periodic tx fifo empty interrupt */
  49. #define USB_OTG_GINNAKEFF_INT ((uint32_t)0x00000040) /*!< usb otg global non-periodic in nak effective interrupt */
  50. #define USB_OTG_GOUTNAKEFF_INT ((uint32_t)0x00000080) /*!< usb otg global out nak effective interrupt */
  51. #define USB_OTG_ERLYSUSP_INT ((uint32_t)0x00000400) /*!< usb otg early suspend interrupt */
  52. #define USB_OTG_USBSUSP_INT ((uint32_t)0x00000800) /*!< usb otg suspend interrupt */
  53. #define USB_OTG_USBRST_INT ((uint32_t)0x00001000) /*!< usb otg reset interrupt */
  54. #define USB_OTG_ENUMDONE_INT ((uint32_t)0x00002000) /*!< usb otg enumeration done interrupt */
  55. #define USB_OTG_ISOOUTDROP_INT ((uint32_t)0x00004000) /*!< usb otg isochronous out packet dropped interrut */
  56. #define USB_OTG_EOPF_INT ((uint32_t)0x00008000) /*!< usb otg eop interrupt */
  57. #define USB_OTG_IEPT_INT ((uint32_t)0x00040000) /*!< usb otg in endpoint interrupt */
  58. #define USB_OTG_OEPT_INT ((uint32_t)0x00080000) /*!< usb otg out endpoint interrupt */
  59. #define USB_OTG_INCOMISOIN_INT ((uint32_t)0x00100000) /*!< usb otg incomplete isochronous in transfer interrupt */
  60. #define USB_OTG_INCOMPIP_INCOMPISOOUT_INT ((uint32_t)0x00200000) /*!< usb otg incomplete periodic transfer/isochronous out interrupt */
  61. #define USB_OTG_PRT_INT ((uint32_t)0x01000000) /*!< usb otg host port interrupt */
  62. #define USB_OTG_HCH_INT ((uint32_t)0x02000000) /*!< usb otg host channel interrupt */
  63. #define USB_OTG_PTXFEMP_INT ((uint32_t)0x04000000) /*!< usb otg periodic txfifo empty interrupt */
  64. #define USB_OTG_CONIDSCHG_INT ((uint32_t)0x10000000) /*!< usb otg connector id status change interrupt */
  65. #define USB_OTG_DISCON_INT ((uint32_t)0x20000000) /*!< usb otg disconnect detected interrupt */
  66. #define USB_OTG_WKUP_INT ((uint32_t)0x80000000) /*!< usb otg wakeup interrupt */
  67. /**
  68. * @}
  69. */
  70. /** @defgroup USB_global_interrupt_flags_definition
  71. * @brief usb global interrupt flag
  72. * @{
  73. */
  74. #define USB_OTG_CURMODE ((uint32_t)0x00000001) /*!< usb otg current mode */
  75. #define USB_OTG_MODEMIS_FLAG ((uint32_t)0x00000002) /*!< usb otg mode mismatch flag */
  76. #define USB_OTG_OTGINT_FLAG ((uint32_t)0x00000004) /*!< usb otg flag */
  77. #define USB_OTG_SOF_FLAG ((uint32_t)0x00000008) /*!< usb otg sof flag */
  78. #define USB_OTG_RXFLVL_FLAG ((uint32_t)0x00000010) /*!< usb otg receive fifo non-empty flag */
  79. #define USB_OTG_NPTXFEMP_FLAG ((uint32_t)0x00000020) /*!< usb otg non-periodic tx fifo empty flag */
  80. #define USB_OTG_GINNAKEFF_FLAG ((uint32_t)0x00000040) /*!< usb otg global non-periodic in nak effective flag */
  81. #define USB_OTG_GOUTNAKEFF_FLAG ((uint32_t)0x00000080) /*!< usb otg global out nak effective flag */
  82. #define USB_OTG_ERLYSUSP_FLAG ((uint32_t)0x00000400) /*!< usb otg early suspend flag */
  83. #define USB_OTG_USBSUSP_FLAG ((uint32_t)0x00000800) /*!< usb otg suspend flag */
  84. #define USB_OTG_USBRST_FLAG ((uint32_t)0x00001000) /*!< usb otg reset flag */
  85. #define USB_OTG_ENUMDONE_FLAG ((uint32_t)0x00002000) /*!< usb otg enumeration done flag */
  86. #define USB_OTG_ISOOUTDROP_FLAG ((uint32_t)0x00004000) /*!< usb otg isochronous out packet dropped flag */
  87. #define USB_OTG_EOPF_FLAG ((uint32_t)0x00008000) /*!< usb otg eop flag */
  88. #define USB_OTG_IEPT_FLAG ((uint32_t)0x00040000) /*!< usb otg in endpoint flag */
  89. #define USB_OTG_OEPT_FLAG ((uint32_t)0x00080000) /*!< usb otg out endpoint flag */
  90. #define USB_OTG_INCOMISOIN_FLAG ((uint32_t)0x00100000) /*!< usb otg incomplete isochronous in transfer flag */
  91. #define USB_OTG_INCOMPIP_INCOMPISOOUT_FLAG ((uint32_t)0x00200000) /*!< usb otg incomplete periodic transfer/isochronous out flag */
  92. #define USB_OTG_PRT_FLAG ((uint32_t)0x01000000) /*!< usb otg host port flag */
  93. #define USB_OTG_HCH_FLAG ((uint32_t)0x02000000) /*!< usb otg host channel flag */
  94. #define USB_OTG_PTXFEMP_FLAG ((uint32_t)0x04000000) /*!< usb otg periodic txfifo empty flag */
  95. #define USB_OTG_CONIDSCHG_FLAG ((uint32_t)0x10000000) /*!< usb otg connector id status change flag */
  96. #define USB_OTG_DISCON_FLAG ((uint32_t)0x20000000) /*!< usb otg disconnect detected flag */
  97. #define USB_OTG_WKUP_FLAG ((uint32_t)0x80000000) /*!< usb otg wakeup flag */
  98. /**
  99. * @}
  100. */
  101. /** @defgroup USB_global_setting_definition
  102. * @brief usb global setting
  103. * @{
  104. */
  105. /**
  106. * @brief usb turnaround time
  107. */
  108. #define USB_TRDTIM_8 0x9 /*!< usb turn around time 8 */
  109. #define USB_TRDTIM_16 0x5 /*!< usb turn around time 16 */
  110. /**
  111. * @brief usb receive status
  112. */
  113. #define USB_OTG_GRXSTSP_EPTNUM ((uint32_t)0x0000000F) /*!< usb device receive packet endpoint number*/
  114. #define USB_OTG_GRXSTSP_CHNUM ((uint32_t)0x0000000F) /*!< usb host receive packet channel number*/
  115. #define USB_OTG_GRXSTSP_BCNT ((uint32_t)0x00007FF0) /*!< usb receive packet byte count */
  116. #define USB_OTG_GRXSTSP_DPID ((uint32_t)0x00018000) /*!< usb receive packet pid */
  117. #define USB_OTG_GRXSTSP_PKTSTS ((uint32_t)0x001E0000) /*!< usb receive packet status */
  118. /**
  119. * @brief usb host packet status
  120. */
  121. #define PKTSTS_IN_DATA_PACKET_RECV 0x2 /*!< usb host in data packet received */
  122. #define PKTSTS_IN_TRANSFER_COMPLETE 0x3 /*!< usb host in transfer completed */
  123. #define PKTSTS_DATA_BIT_ERROR 0x5 /*!< usb host data toggle error */
  124. #define PKTSTS_CHANNEL_STOP 0x7 /*!< usb host channel halted */
  125. /**
  126. * @brief usb device packet status
  127. */
  128. #define USB_OUT_STS_NAK 0x1 /*!< usb device global out nak */
  129. #define USB_OUT_STS_DATA 0x2 /*!< usb device out data packet received */
  130. #define USB_OUT_STS_COMP 0x3 /*!< usb device out transfer completed */
  131. #define USB_SETUP_STS_COMP 0x4 /*!< usb device setup transcation completed */
  132. #define USB_SETUP_STS_DATA 0x6 /*!< usb device setup data packet received */
  133. /**
  134. * @}
  135. */
  136. /** @defgroup USB_host_config_definition
  137. * @{
  138. */
  139. /**
  140. * @brief usb host phy clock
  141. */
  142. #define USB_HCFG_CLK_60M 0 /*!< usb host phy clock 60mhz */
  143. #define USB_HCFG_CLK_48M 1 /*!< usb host phy clock 48mhz */
  144. #define USB_HCFG_CLK_6M 2 /*!< usb host phy clock 6mhz */
  145. /**
  146. * @brief usb host port status
  147. */
  148. #define USB_OTG_HPRT_PRTCONSTS ((uint32_t)0x00000001) /*!< usb host port connect status */
  149. #define USB_OTG_HPRT_PRTCONDET ((uint32_t)0x00000002) /*!< usb host port connect detected */
  150. #define USB_OTG_HPRT_PRTENA ((uint32_t)0x00000004) /*!< usb host port enable */
  151. #define USB_OTG_HPRT_PRTENCHNG ((uint32_t)0x00000008) /*!< usb host port enable/disable change */
  152. #define USB_OTG_HPRT_PRTOVRCACT ((uint32_t)0x00000010) /*!< usb host port overcurrent active */
  153. #define USB_OTG_HPRT_PRTOVRCCHNG ((uint32_t)0x00000020) /*!< usb host port overcurrent change */
  154. #define USB_OTG_HPRT_PRTRES ((uint32_t)0x00000040) /*!< usb host port resume */
  155. #define USB_OTG_HPRT_PRTSUSP ((uint32_t)0x00000080) /*!< usb host port suspend */
  156. #define USB_OTG_HPRT_PRTRST ((uint32_t)0x00000100) /*!< usb host port reset */
  157. #define USB_OTG_HPRT_PRTLNSTS ((uint32_t)0x00000C00) /*!< usb host port line status */
  158. #define USB_OTG_HPRT_PRTPWR ((uint32_t)0x00001000) /*!< usb host port power */
  159. #define USB_OTG_HPRT_PRTSPD ((uint32_t)0x00060000) /*!< usb host port speed */
  160. /**
  161. * @brief usb port speed
  162. */
  163. #define USB_PRTSPD_HIGH_SPEED 0 /*!< usb host port high speed */
  164. #define USB_PRTSPD_FULL_SPEED 1 /*!< usb host port full speed */
  165. #define USB_PRTSPD_LOW_SPEED 2 /*!< usb host port low speed */
  166. /**
  167. * @brief usb host register hcchar bit define
  168. */
  169. #define USB_OTG_HCCHAR_MPS ((uint32_t)0x000007FF) /*!< channel maximum packet size */
  170. #define USB_OTG_HCCHAR_EPTNUM ((uint32_t)0x00007800) /*!< endpoint number */
  171. #define USB_OTG_HCCHAR_EPTDIR ((uint32_t)0x00008000) /*!< endpoint direction */
  172. #define USB_OTG_HCCHAR_LSPDDEV ((uint32_t)0x00020000) /*!< low speed device */
  173. #define USB_OTG_HCCHAR_EPTYPE ((uint32_t)0x000C0000) /*!< endpoint type */
  174. #define USB_OTG_HCCHAR_MC ((uint32_t)0x00300000) /*!< multi count */
  175. #define USB_OTG_HCCHAR_DEVADDR ((uint32_t)0x1FC00000) /*!< device address */
  176. #define USB_OTG_HCCHAR_ODDFRM ((uint32_t)0x20000000) /*!< odd frame */
  177. #define USB_OTG_HCCHAR_CHDIS ((uint32_t)0x40000000) /*!< channel disable */
  178. #define USB_OTG_HCCHAR_CHENA ((uint32_t)0x80000000) /*!< channel enable */
  179. /**
  180. * @brief usb host register hctsiz bit define
  181. */
  182. #define USB_OTG_HCTSIZ_XFERSIZE ((uint32_t)0x0007FFFF) /*!< channel transfer size */
  183. #define USB_OTG_HCTSIZ_PKTCNT ((uint32_t)0x1FF80000) /*!< channel packet count */
  184. #define USB_OTG_HCTSIZ_PID ((uint32_t)0x60000000) /*!< channel pid */
  185. /**
  186. * @brief usb host channel interrupt mask
  187. */
  188. #define USB_OTG_HC_XFERCM_INT ((uint32_t)0x00000001) /*!< channel transfer complete interrupt */
  189. #define USB_OTG_HC_CHHLTDM_INT ((uint32_t)0x00000002) /*!< channel halted interrupt */
  190. #define USB_OTG_HC_STALLM_INT ((uint32_t)0x00000008) /*!< channel stall interrupt */
  191. #define USB_OTG_HC_NAKM_INT ((uint32_t)0x00000010) /*!< channel nak interrupt */
  192. #define USB_OTG_HC_ACKM_INT ((uint32_t)0x00000020) /*!< channel ack interrupt */
  193. #define USB_OTG_HC_NYETM_INT ((uint32_t)0x00000040) /*!< channel nyet interrupt */
  194. #define USB_OTG_HC_XACTERRM_INT ((uint32_t)0x00000080) /*!< channel transaction error interrupt */
  195. #define USB_OTG_HC_BBLERRM_INT ((uint32_t)0x00000100) /*!< channel babble error interrupt */
  196. #define USB_OTG_HC_FRMOVRRUN_INT ((uint32_t)0x00000200) /*!< channel frame overrun interrupt */
  197. #define USB_OTG_HC_DTGLERRM_INT ((uint32_t)0x00000400) /*!< channel data toggle interrupt */
  198. /**
  199. * @brief usb host channel interrupt flag
  200. */
  201. #define USB_OTG_HC_XFERC_FLAG ((uint32_t)0x00000001) /*!< channel transfer complete flag */
  202. #define USB_OTG_HC_CHHLTD_FLAG ((uint32_t)0x00000002) /*!< channel halted flag */
  203. #define USB_OTG_HC_STALL_FLAG ((uint32_t)0x00000008) /*!< channel stall flag */
  204. #define USB_OTG_HC_NAK_FLAG ((uint32_t)0x00000010) /*!< channel nak flag */
  205. #define USB_OTG_HC_ACK_FLAG ((uint32_t)0x00000020) /*!< channel ack flag */
  206. #define USB_OTG_HC_NYET_FLAG ((uint32_t)0x00000040) /*!< channel nyet flag */
  207. #define USB_OTG_HC_XACTERR_FLAG ((uint32_t)0x00000080) /*!< channel transaction error flag */
  208. #define USB_OTG_HC_BBLERR_FLAG ((uint32_t)0x00000100) /*!< channel babble error flag */
  209. #define USB_OTG_HC_FRMOVRRUN_FLAG ((uint32_t)0x00000200) /*!< channel frame overrun flag */
  210. #define USB_OTG_HC_DTGLERR_FLAG ((uint32_t)0x00000400) /*!< channel data toggle flag */
  211. /**
  212. * @}
  213. */
  214. /** @defgroup USB_device_config_definition
  215. * @{
  216. */
  217. /**
  218. * @brief usb device periodic frame interval
  219. */
  220. typedef enum
  221. {
  222. DCFG_PERFRINT_80 = 0x00, /*!< periodic frame interval 80% */
  223. DCFG_PERFRINT_85 = 0x01, /*!< periodic frame interval 85% */
  224. DCFG_PERFRINT_90 = 0x02, /*!< periodic frame interval 90% */
  225. DCFG_PERFRINT_95 = 0x03 /*!< periodic frame interval 95% */
  226. } dcfg_perfrint_type;
  227. /**
  228. * @brief usb device full speed
  229. */
  230. #define USB_DCFG_FULL_SPEED 3 /*!< device full speed */
  231. /**
  232. * @brief usb device ctrl define
  233. */
  234. #define USB_OTG_DCTL_RWKUPSIG ((uint32_t)0x00000001) /*!< usb device remote wakeup signaling */
  235. #define USB_OTG_DCTL_SFTDISCON ((uint32_t)0x00000002) /*!< usb device soft disconnect */
  236. #define USB_OTG_DCTL_GNPINNAKSTS ((uint32_t)0x00000004) /*!< usb device global non-periodic in nak status */
  237. #define USB_OTG_DCTL_GOUTNAKSTS ((uint32_t)0x00000008) /*!< usb device global out nak status */
  238. #define USB_OTG_DCTL_SGNPINNAK ((uint32_t)0x00000080) /*!< usb device set global non-periodic in nak */
  239. #define USB_OTG_DCTL_CGNPINNAK ((uint32_t)0x00000100) /*!< usb device clear global non-periodic in nak */
  240. #define USB_OTG_DCTL_SGOUTNAK ((uint32_t)0x00000200) /*!< usb device set global out nak status */
  241. #define USB_OTG_DCTL_CGOUTNAK ((uint32_t)0x00000400) /*!< usb device clear global out nak status */
  242. #define USB_OTG_DCTL_PWROPRGDNE ((uint32_t)0x00000800) /*!< usb device power on programming done */
  243. /**
  244. * @brief usb device in endpoint flag
  245. */
  246. #define USB_OTG_DIEPINT_XFERC_FLAG ((uint32_t)0x00000001) /*!< usb device in transfer completed flag */
  247. #define USB_OTG_DIEPINT_EPTDISD_FLAG ((uint32_t)0x00000002) /*!< usb device endpoint disable flag */
  248. #define USB_OTG_DIEPINT_TIMEOUT_FLAG ((uint32_t)0x00000008) /*!< usb device in timeout */
  249. #define USB_OTG_DIEPINT_INTKNTXFEMP_FLAG ((uint32_t)0x00000010) /*!< usb device in token received when tx fifo is empty flag */
  250. #define USB_OTG_DIEPINT_INEPTNAK_FLAG ((uint32_t)0x00000040) /*!< usb device in endpoint nak effective flag */
  251. #define USB_OTG_DIEPINT_TXFEMP_FLAG ((uint32_t)0x00000080) /*!< usb device transmit fifo empty flag */
  252. /**
  253. * @brief usb device out endpoint flag
  254. */
  255. #define USB_OTG_DOEPINT_XFERC_FLAG ((uint32_t)0x00000001) /*!< usb device out transfer completed flag */
  256. #define USB_OTG_DOEPINT_EPTDISD_FLAG ((uint32_t)0x00000002) /*!< usb device endpoint disable flag */
  257. #define USB_OTG_DOEPINT_SETUP_FLAG ((uint32_t)0x00000008) /*!< usb device setup flag */
  258. #define USB_OTG_DOEPINT_OUTTEPD_FLAG ((uint32_t)0x00000010) /*!< usb device out token recevied when endpoint disable flag */
  259. #define USB_OTG_DOEPINT_B2BSTUP_FLAG ((uint32_t)0x00000040) /*!< back-to-back setup packets received */
  260. /**
  261. * @brief usb device in endpoint fifo space mask
  262. */
  263. #define USB_OTG_DTXFSTS_INEPTFSAV ((uint32_t)0x0000FFFF) /*!< usb device in endpoint tx fifo space avail */
  264. /**
  265. * @brief endpoint0 maximum packet size
  266. */
  267. #define USB_EPT0_MPS_64 0 /*!< usb device endpoint 0 maximum packet size 64byte */
  268. #define USB_EPT0_MPS_32 1 /*!< usb device endpoint 0 maximum packet size 32byte */
  269. #define USB_EPT0_MPS_16 2 /*!< usb device endpoint 0 maximum packet size 16byte */
  270. #define USB_EPT0_MPS_8 3 /*!< usb device endpoint 0 maximum packet size 8byte */
  271. /**
  272. * @}
  273. */
  274. /**
  275. * @brief otg fifo size (word)
  276. */
  277. #define OTG_FIFO_SIZE 320 /*!< otg usb total fifo size */
  278. /**
  279. * @brief otg host max buffer length (byte)
  280. */
  281. #define USB_MAX_DATA_LENGTH 0x200 /*!< usb host maximum buffer size */
  282. #define OTGFS_USB_GLOBAL
  283. #define OTGFS_USB_DEVICE
  284. #define OTGFS_USB_HOST
  285. /** @defgroup USB_exported_enum_types
  286. * @{
  287. */
  288. /**
  289. * @brief usb mode define(device, host, drd)
  290. */
  291. typedef enum
  292. {
  293. OTG_DEVICE_MODE, /*!< usb device mode */
  294. OTG_HOST_MODE, /*!< usb host mode */
  295. OTG_DRD_MODE /*!< usb drd mode */
  296. } otg_mode_type;
  297. /**
  298. * @brief endpoint type define
  299. */
  300. typedef enum
  301. {
  302. EPT_CONTROL_TYPE = 0x00, /*!< usb endpoint type control */
  303. EPT_ISO_TYPE = 0x01, /*!< usb endpoint type iso */
  304. EPT_BULK_TYPE = 0x02, /*!< usb endpoint type bulk */
  305. EPT_INT_TYPE = 0x03 /*!< usb endpoint type interrupt */
  306. } endpoint_trans_type;
  307. /**
  308. * @brief usb endpoint number define type
  309. */
  310. typedef enum
  311. {
  312. USB_EPT0 = 0x00, /*!< usb endpoint 0 */
  313. USB_EPT1 = 0x01, /*!< usb endpoint 1 */
  314. USB_EPT2 = 0x02, /*!< usb endpoint 2 */
  315. USB_EPT3 = 0x03, /*!< usb endpoint 3 */
  316. USB_EPT4 = 0x04, /*!< usb endpoint 4 */
  317. USB_EPT5 = 0x05, /*!< usb endpoint 5 */
  318. USB_EPT6 = 0x06, /*!< usb endpoint 6 */
  319. USB_EPT7 = 0x07 /*!< usb endpoint 7 */
  320. } usb_endpoint_number_type;
  321. /**
  322. * @brief usb endpoint max num define
  323. */
  324. #ifndef USB_EPT_MAX_NUM
  325. #define USB_EPT_MAX_NUM 4 /*!< usb device support endpoint number */
  326. #endif
  327. /**
  328. * @brief usb channel max num define
  329. */
  330. #ifndef USB_HOST_CHANNEL_NUM
  331. #define USB_HOST_CHANNEL_NUM 8 /*!< usb host support channel number */
  332. #endif
  333. /**
  334. * @brief endpoint trans dir type
  335. */
  336. typedef enum
  337. {
  338. EPT_DIR_IN = 0x00, /*!< usb transfer direction in */
  339. EPT_DIR_OUT = 0x01 /*!< usb transfer direction out */
  340. } endpoint_dir_type;
  341. /**
  342. * @brief otgfs1 and otgfs2 select type
  343. */
  344. typedef enum
  345. {
  346. USB_OTG1_ID, /*!< usb otg 1 id */
  347. USB_OTG2_ID /*!< usb otg 2 id */
  348. } otg_id_type;
  349. /**
  350. * @brief usb clock select
  351. */
  352. typedef enum
  353. {
  354. USB_CLK_HICK, /*!< usb clock use hick */
  355. USB_CLK_HEXT /*!< usb clock use hext */
  356. }usb_clk48_s;
  357. /**
  358. * @}
  359. */
  360. /** @defgroup USB_exported_types
  361. * @{
  362. */
  363. /**
  364. * @brief usb endpoint infomation structure definition
  365. */
  366. typedef struct
  367. {
  368. uint8_t eptn; /*!< endpoint register number (0~7) */
  369. uint8_t ept_address; /*!< endpoint address */
  370. uint8_t inout; /*!< endpoint dir EPT_DIR_IN or EPT_DIR_OUT */
  371. uint8_t trans_type; /*!< endpoint type:
  372. EPT_CONTROL_TYPE, EPT_BULK_TYPE, EPT_INT_TYPE, EPT_ISO_TYPE*/
  373. uint16_t tx_addr; /*!< endpoint tx buffer offset address */
  374. uint16_t rx_addr; /*!< endpoint rx buffer offset address */
  375. uint32_t maxpacket; /*!< endpoint max packet*/
  376. uint8_t is_double_buffer; /*!< endpoint double buffer flag */
  377. uint8_t stall; /*!< endpoint is stall state */
  378. uint32_t status;
  379. /* transmission buffer and count */
  380. uint8_t *trans_buf; /*!< endpoint transmission buffer */
  381. uint32_t total_len; /*!< endpoint transmission lengtg */
  382. uint32_t trans_len; /*!< endpoint transmission length*/
  383. uint32_t last_len; /*!< last transfer length */
  384. uint32_t rem0_len; /*!< rem transfer length */
  385. uint32_t ept0_slen; /*!< endpoint 0 transfer sum length */
  386. } usb_ept_info;
  387. /**
  388. * @brief usb host channel infomation structure definition
  389. */
  390. typedef struct
  391. {
  392. uint8_t ch_num; /*!< host channel number */
  393. uint8_t address; /*!< device address */
  394. uint8_t dir; /*!< transmission direction */
  395. uint8_t ept_num; /*!< device endpoint number */
  396. uint8_t ept_type; /*!< channel transmission type */
  397. uint32_t maxpacket; /*!< support max packet size */
  398. uint8_t data_pid; /*!< data pid */
  399. uint8_t speed; /*!< usb speed */
  400. uint8_t stall; /*!< channel stall flag */
  401. uint32_t status; /*!< channel status */
  402. uint32_t state; /*!< channel state */
  403. uint32_t urb_sts; /*!< usb channel request block state */
  404. uint8_t toggle_in; /*!< channel in transfer toggle */
  405. uint8_t toggle_out; /*!< channel out transfer toggle */
  406. /* transmission buffer and count */
  407. uint8_t *trans_buf; /* host channel buffer */
  408. uint32_t trans_len; /* host channel transmission len */
  409. uint32_t trans_count; /* host channel transmission count*/
  410. } usb_hch_type;
  411. typedef struct
  412. {
  413. /**
  414. * @brief otgfs control and status register, offset:0x00
  415. */
  416. union
  417. {
  418. __IO uint32_t gotgctrl;
  419. struct
  420. {
  421. __IO uint32_t reserved1 : 16; /* [15:0] */
  422. __IO uint32_t cidsts : 1; /* [16] */
  423. __IO uint32_t reserved2 : 4; /* [20:17] */
  424. __IO uint32_t curmod : 1; /* [21] */
  425. __IO uint32_t reserved3 : 10; /* [31:22] */
  426. } gotgctrl_bit;
  427. };
  428. /**
  429. * @brief otgfs interrupt register, offset:0x04
  430. */
  431. union
  432. {
  433. __IO uint32_t gotgint;
  434. struct
  435. {
  436. __IO uint32_t reserved1 : 2; /* [1:0] */
  437. __IO uint32_t sesenddet : 1; /* [2] */
  438. __IO uint32_t reserved2 : 29; /* [31:3] */
  439. } gotgint_bit;
  440. };
  441. /**
  442. * @brief otgfs gahbcfg configuration register, offset:0x08
  443. */
  444. union
  445. {
  446. __IO uint32_t gahbcfg;
  447. struct
  448. {
  449. __IO uint32_t glbintmsk : 1; /* [0] */
  450. __IO uint32_t reserved1 : 6; /* [6:1] */
  451. __IO uint32_t nptxfemplvl : 1; /* [7] */
  452. __IO uint32_t ptxfemplvl : 1; /* [8] */
  453. __IO uint32_t reserved2 : 23; /* [31:9] */
  454. } gahbcfg_bit;
  455. };
  456. /**
  457. * @brief otgfs usb configuration register, offset:0x0C
  458. */
  459. union
  460. {
  461. __IO uint32_t gusbcfg;
  462. struct
  463. {
  464. __IO uint32_t toutcal : 3; /* [2:0] */
  465. __IO uint32_t reserved1 : 7; /* [9:3] */
  466. __IO uint32_t usbtrdtim : 4; /* [13:10] */
  467. __IO uint32_t reserved2 : 15; /* [28:14] */
  468. __IO uint32_t fhstmode : 1; /* [29] */
  469. __IO uint32_t fdevmode : 1; /* [30] */
  470. __IO uint32_t cotxpkt : 1; /* [31] */
  471. } gusbcfg_bit;
  472. };
  473. /**
  474. * @brief otgfs reset register, offset:0x10
  475. */
  476. union
  477. {
  478. __IO uint32_t grstctl;
  479. struct
  480. {
  481. __IO uint32_t csftrst : 1; /* [0] */
  482. __IO uint32_t piusftrst : 1; /* [1] */
  483. __IO uint32_t frmcntrst : 1; /* [2] */
  484. __IO uint32_t reserved1 : 1; /* [3] */
  485. __IO uint32_t rxfflsh : 1; /* [4] */
  486. __IO uint32_t txfflsh : 1; /* [5] */
  487. __IO uint32_t txfnum : 5; /* [10:6] */
  488. __IO uint32_t reserved2 : 20; /* [30:11] */
  489. __IO uint32_t ahbidle : 1; /* [31] */
  490. } grstctl_bit;
  491. };
  492. /**
  493. * @brief otgfs core interrupt register, offset:0x14
  494. */
  495. union
  496. {
  497. __IO uint32_t gintsts;
  498. struct
  499. {
  500. __IO uint32_t curmode : 1; /* [0] */
  501. __IO uint32_t modemis : 1; /* [1] */
  502. __IO uint32_t otgint : 1; /* [2] */
  503. __IO uint32_t sof : 1; /* [3] */
  504. __IO uint32_t rxflvl : 1; /* [4] */
  505. __IO uint32_t nptxfemp : 1; /* [5] */
  506. __IO uint32_t ginnakeff : 1; /* [6] */
  507. __IO uint32_t goutnakeff : 1; /* [7] */
  508. __IO uint32_t reserved1 : 2; /* [9:8]] */
  509. __IO uint32_t erlysusp : 1; /* [10] */
  510. __IO uint32_t usbsusp : 1; /* [11] */
  511. __IO uint32_t usbrst : 1; /* [12] */
  512. __IO uint32_t enumdone : 1; /* [13] */
  513. __IO uint32_t isooutdrop : 1; /* [14] */
  514. __IO uint32_t eopf : 1; /* [15] */
  515. __IO uint32_t reserved2 : 2; /* [17:16]] */
  516. __IO uint32_t ieptint : 1; /* [18] */
  517. __IO uint32_t oeptint : 1; /* [19] */
  518. __IO uint32_t incompisoin : 1; /* [20] */
  519. __IO uint32_t incompip_incompisoout : 1; /* [21] */
  520. __IO uint32_t reserved3 : 2; /* [23:22] */
  521. __IO uint32_t prtint : 1; /* [24] */
  522. __IO uint32_t hchint : 1; /* [25] */
  523. __IO uint32_t ptxfemp : 1; /* [26] */
  524. __IO uint32_t reserved4 : 1; /* [27] */
  525. __IO uint32_t conidschg : 1; /* [28] */
  526. __IO uint32_t disconint : 1; /* [29] */
  527. __IO uint32_t reserved5 : 1; /* [30] */
  528. __IO uint32_t wkupint : 1; /* [31] */
  529. } gintsts_bit;
  530. };
  531. /**
  532. * @brief otgfs interrupt mask register, offset:0x18
  533. */
  534. union
  535. {
  536. __IO uint32_t gintmsk;
  537. struct
  538. {
  539. __IO uint32_t reserved1 : 1; /* [0] */
  540. __IO uint32_t modemismsk : 1; /* [1] */
  541. __IO uint32_t otgintmsk : 1; /* [2] */
  542. __IO uint32_t sofmsk : 1; /* [3] */
  543. __IO uint32_t rxflvlmsk : 1; /* [4] */
  544. __IO uint32_t nptxfempmsk : 1; /* [5] */
  545. __IO uint32_t ginnakeffmsk : 1; /* [6] */
  546. __IO uint32_t goutnakeffmsk : 1; /* [7] */
  547. __IO uint32_t reserved2 : 2; /* [9:8]] */
  548. __IO uint32_t erlysuspmsk : 1; /* [10] */
  549. __IO uint32_t usbsuspmsk : 1; /* [11] */
  550. __IO uint32_t usbrstmsk : 1; /* [12] */
  551. __IO uint32_t enumdonemsk : 1; /* [13] */
  552. __IO uint32_t isooutdropmsk : 1; /* [14] */
  553. __IO uint32_t eopfmsk : 1; /* [15] */
  554. __IO uint32_t reserved3 : 2; /* [17:16]] */
  555. __IO uint32_t ieptintmsk : 1; /* [18] */
  556. __IO uint32_t oeptintmsk : 1; /* [19] */
  557. __IO uint32_t incompisoinmsk : 1; /* [20] */
  558. __IO uint32_t incompip_incompisooutmsk : 1; /* [21] */
  559. __IO uint32_t reserved4 : 2; /* [23:22] */
  560. __IO uint32_t prtintmsk : 1; /* [24] */
  561. __IO uint32_t hchintmsk : 1; /* [25] */
  562. __IO uint32_t ptxfempmsk : 1; /* [26] */
  563. __IO uint32_t reserved5 : 1; /* [27] */
  564. __IO uint32_t conidschgmsk : 1; /* [28] */
  565. __IO uint32_t disconintmsk : 1; /* [29] */
  566. __IO uint32_t reserved6 : 1; /* [30] */
  567. __IO uint32_t wkupintmsk : 1; /* [31] */
  568. } gintmsk_bit;
  569. };
  570. /**
  571. * @brief otgfs rx status debug read register, offset:0x1C
  572. */
  573. union
  574. {
  575. __IO uint32_t grxstsr;
  576. struct
  577. {
  578. __IO uint32_t eptnum : 4; /* [3:0] */
  579. __IO uint32_t bcnt : 11; /* [14:4] */
  580. __IO uint32_t dpid : 2; /* [16:15] */
  581. __IO uint32_t pktsts : 4; /* [20:17] */
  582. __IO uint32_t fn : 4; /* [24:21] */
  583. __IO uint32_t reserved1 : 7; /* [31:25] */
  584. } grxstsr_bit;
  585. };
  586. /**
  587. * @brief otgfs rx status read and pop register, offset:0x20
  588. */
  589. union
  590. {
  591. __IO uint32_t grxstsp;
  592. struct
  593. {
  594. __IO uint32_t chnum : 4; /* [3:0] */
  595. __IO uint32_t bcnt : 11; /* [14:4] */
  596. __IO uint32_t dpid : 2; /* [16:15] */
  597. __IO uint32_t pktsts : 4; /* [20:17] */
  598. __IO uint32_t reserved1 : 11; /* [31:21] */
  599. } grxstsp_bit;
  600. };
  601. /**
  602. * @brief otgfs rx fifo size register, offset:0x24
  603. */
  604. union
  605. {
  606. __IO uint32_t grxfsiz;
  607. struct
  608. {
  609. __IO uint32_t rxfdep : 16; /* [15:0] */
  610. __IO uint32_t reserved1 : 16; /* [31:16] */
  611. } grxfsiz_bit;
  612. };
  613. /**
  614. * @brief otgfs non-periodic and ept0 tx fifo size register, offset:0x28
  615. */
  616. union
  617. {
  618. __IO uint32_t gnptxfsiz_ept0tx;
  619. struct
  620. {
  621. __IO uint32_t nptxfstaddr : 16; /* [15:0] */
  622. __IO uint32_t nptxfdep : 16; /* [31:16] */
  623. } gnptxfsiz_ept0tx_bit;
  624. };
  625. /**
  626. * @brief otgfs non-periodic tx fifo request queue status register, offset:0x2C
  627. */
  628. union
  629. {
  630. __IO uint32_t gnptxsts;
  631. struct
  632. {
  633. __IO uint32_t nptxfspcavail : 16; /* [15:0] */
  634. __IO uint32_t nptxqspcavail : 8; /* [23:16] */
  635. __IO uint32_t nptxqtop : 7; /* [30:24] */
  636. } gnptxsts_bit;
  637. };
  638. __IO uint32_t reserved2[2];
  639. /**
  640. * @brief otgfs general core configuration register, offset:0x38
  641. */
  642. union
  643. {
  644. __IO uint32_t gccfg;
  645. struct
  646. {
  647. __IO uint32_t reserved1 : 16; /* [15:0] */
  648. __IO uint32_t pwrdown : 1; /* [16] */
  649. __IO uint32_t reserved2 : 1; /* [17] */
  650. __IO uint32_t avalidsesen : 1; /* [18] */
  651. __IO uint32_t bvalidsesen : 1; /* [19] */
  652. __IO uint32_t sofouten : 1; /* [20] */
  653. /**
  654. * @brief ignore vbus detection, only available in at32f415xx revision C.
  655. * at32f415xx revision B: (not support)
  656. * the vbus detection pin (pa9) can not be used for other functionality.
  657. * vbus pin must kept at VBUS or VDD.
  658. *
  659. * at32f415xx revision C: (support)
  660. * ignore vbus detection, the internal vbus is always valid.
  661. * the vbus pin (pa9) can be used for other functionality.
  662. */
  663. __IO uint32_t vbusig : 1; /* [21] */
  664. __IO uint32_t reserved3 : 10; /* [31:22] */
  665. } gccfg_bit;
  666. };
  667. /**
  668. * @brief otgfs core id register, offset:0x3C
  669. */
  670. union
  671. {
  672. __IO uint32_t guid;
  673. struct
  674. {
  675. __IO uint32_t userid : 32; /* [31:0] */
  676. } guid_bit;
  677. };
  678. __IO uint32_t reserved3[48];
  679. /**
  680. * @brief otgfs host periodic tx fifo size register, offset:0x100
  681. */
  682. union
  683. {
  684. __IO uint32_t hptxfsiz;
  685. struct
  686. {
  687. __IO uint32_t ptxfstaddr : 16; /* [15:0] */
  688. __IO uint32_t ptxfsize : 16; /* [31:16] */
  689. } hptxfsiz_bit;
  690. };
  691. /**
  692. * @brief otgfs host periodic tx fifo size register, offset:0x100
  693. */
  694. union
  695. {
  696. __IO uint32_t dieptxfn[7];
  697. struct
  698. {
  699. __IO uint32_t ineptxfstaddr : 16; /* [15:0] */
  700. __IO uint32_t ineptxfdep : 16; /* [31:16] */
  701. } dieptxfn_bit[7];
  702. };
  703. } otg_global_type;
  704. typedef struct
  705. {
  706. /**
  707. * @brief otgfs host mode configuration register, offset:0x400
  708. */
  709. union
  710. {
  711. __IO uint32_t hcfg;
  712. struct
  713. {
  714. __IO uint32_t fslspclksel : 2; /* [1:0] */
  715. __IO uint32_t fslssupp : 1; /* [2] */
  716. __IO uint32_t reserved1 : 29; /* [31:3] */
  717. } hcfg_bit;
  718. };
  719. /**
  720. * @brief otgfs host frame interval register, offset:0x404
  721. */
  722. union
  723. {
  724. __IO uint32_t hfir;
  725. struct
  726. {
  727. __IO uint32_t frint : 16; /* [15:0] */
  728. __IO uint32_t reserved1 : 16; /* [31:15] */
  729. } hfir_bit;
  730. };
  731. /**
  732. * @brief otgfs host frame number and time remaining register, offset:0x408
  733. */
  734. union
  735. {
  736. __IO uint32_t hfnum;
  737. struct
  738. {
  739. __IO uint32_t frnum : 16; /* [15:0] */
  740. __IO uint32_t ftrem : 16; /* [31:15] */
  741. } hfnum_bit;
  742. };
  743. __IO uint32_t reserved1;
  744. /**
  745. * @brief otgfs host periodic tx fifo request queue register, offset:0x410
  746. */
  747. union
  748. {
  749. __IO uint32_t hptxsts;
  750. struct
  751. {
  752. __IO uint32_t ptxfspcavil : 16; /* [15:0] */
  753. __IO uint32_t ptxqspcavil : 8; /* [23:16] */
  754. __IO uint32_t ptxqtop : 8; /* [31:24] */
  755. } hptxsts_bit;
  756. };
  757. /**
  758. * @brief otgfs host all channel interrupt register, offset:0x414
  759. */
  760. union
  761. {
  762. __IO uint32_t haint;
  763. struct
  764. {
  765. __IO uint32_t haint : 16; /* [15:0] */
  766. __IO uint32_t reserved1 : 16; /* [32:16] */
  767. } haint_bit;
  768. };
  769. /**
  770. * @brief otgfs host all channel interrupt mask register, offset:0x418
  771. */
  772. union
  773. {
  774. __IO uint32_t haintmsk;
  775. struct
  776. {
  777. __IO uint32_t haintmsk : 16; /* [15:0] */
  778. __IO uint32_t reserved1 : 16; /* [32:16] */
  779. } haintmsk_bit;
  780. };
  781. __IO uint32_t reserved2[9];
  782. /**
  783. * @brief otgfs host port control and status register, offset:0x440
  784. */
  785. union
  786. {
  787. __IO uint32_t hprt;
  788. struct
  789. {
  790. __IO uint32_t prtconsts : 1; /* [0] */
  791. __IO uint32_t prtcondet : 1; /* [1] */
  792. __IO uint32_t prtena : 1; /* [2] */
  793. __IO uint32_t prtenchng : 1; /* [3] */
  794. __IO uint32_t prtovrcact : 1; /* [4] */
  795. __IO uint32_t prtovrcchng : 1; /* [5] */
  796. __IO uint32_t prtres : 1; /* [6] */
  797. __IO uint32_t prtsusp : 1; /* [7] */
  798. __IO uint32_t prtrst : 1; /* [8] */
  799. __IO uint32_t reserved1 : 1; /* [9] */
  800. __IO uint32_t prtlnsts : 2; /* [11:10] */
  801. __IO uint32_t prtpwr : 1; /* [12] */
  802. __IO uint32_t prttsctl : 4; /* [16:13] */
  803. __IO uint32_t prtspd : 2; /* [18:17] */
  804. __IO uint32_t reserved2 : 13; /* [31:19] */
  805. } hprt_bit;
  806. };
  807. } otg_host_type;
  808. typedef struct
  809. {
  810. /**
  811. * @brief otgfs host channel x characterisic register, offset:0x500
  812. */
  813. union
  814. {
  815. __IO uint32_t hcchar;
  816. struct
  817. {
  818. __IO uint32_t mps : 11; /* [10:0] */
  819. __IO uint32_t eptnum : 4; /* [14:11] */
  820. __IO uint32_t eptdir : 1; /* [15] */
  821. __IO uint32_t reserved1 : 1; /* [16] */
  822. __IO uint32_t lspddev : 1; /* [17] */
  823. __IO uint32_t eptype : 2; /* [19:18] */
  824. __IO uint32_t mc : 2; /* [21:20] */
  825. __IO uint32_t devaddr : 7; /* [28:22] */
  826. __IO uint32_t oddfrm : 1; /* [29] */
  827. __IO uint32_t chdis : 1; /* [30] */
  828. __IO uint32_t chena : 1; /* [31] */
  829. } hcchar_bit;
  830. };
  831. /**
  832. * @brief otgfs host channel split control register, offset:0x504
  833. */
  834. union
  835. {
  836. __IO uint32_t hcsplt;
  837. struct
  838. {
  839. __IO uint32_t prtaddr : 7; /* [6:0] */
  840. __IO uint32_t hubaddr : 7; /* [13:7] */
  841. __IO uint32_t xactpos : 2; /* [15:14] */
  842. __IO uint32_t compsplt : 1; /* [16] */
  843. __IO uint32_t reserved1 : 14; /* [30:17] */
  844. __IO uint32_t spltena : 1; /* [31] */
  845. } hcsplt_bit;
  846. };
  847. /**
  848. * @brief otgfs host channel interrupt register, offset:0x508
  849. */
  850. union
  851. {
  852. __IO uint32_t hcint;
  853. struct
  854. {
  855. __IO uint32_t xferc : 1; /* [0] */
  856. __IO uint32_t chhltd : 1; /* [1] */
  857. __IO uint32_t reserved1 : 1; /* [2] */
  858. __IO uint32_t stall : 1; /* [3] */
  859. __IO uint32_t nak : 1; /* [4] */
  860. __IO uint32_t ack : 1; /* [5] */
  861. __IO uint32_t reserved2 : 1; /* [6] */
  862. __IO uint32_t xacterr : 1; /* [7] */
  863. __IO uint32_t bblerr : 1; /* [8] */
  864. __IO uint32_t frmovrun : 1; /* [9] */
  865. __IO uint32_t dtglerr : 1; /* [10] */
  866. __IO uint32_t reserved3 : 21; /* [31:11] */
  867. } hcint_bit;
  868. };
  869. /**
  870. * @brief otgfs host channel interrupt mask register, offset:0x50C
  871. */
  872. union
  873. {
  874. __IO uint32_t hcintmsk;
  875. struct
  876. {
  877. __IO uint32_t xfercmsk : 1; /* [0] */
  878. __IO uint32_t chhltdmsk : 1; /* [1] */
  879. __IO uint32_t reserved1 : 1; /* [2] */
  880. __IO uint32_t stallmsk : 1; /* [3] */
  881. __IO uint32_t nakmsk : 1; /* [4] */
  882. __IO uint32_t ackmsk : 1; /* [5] */
  883. __IO uint32_t reserved2 : 1; /* [6] */
  884. __IO uint32_t xacterrmsk : 1; /* [7] */
  885. __IO uint32_t bblerrmsk : 1; /* [8] */
  886. __IO uint32_t frmovrunmsk : 1; /* [9] */
  887. __IO uint32_t dtglerrmsk : 1; /* [10] */
  888. __IO uint32_t reserved3 : 21; /* [31:11] */
  889. } hcintmsk_bit;
  890. };
  891. /**
  892. * @brief otgfs host channel transfer size register, offset:0x510
  893. */
  894. union
  895. {
  896. __IO uint32_t hctsiz;
  897. struct
  898. {
  899. __IO uint32_t xfersize : 19; /* [18:0] */
  900. __IO uint32_t pktcnt : 10; /* [28:19] */
  901. __IO uint32_t pid : 2; /* [30:29] */
  902. __IO uint32_t reserved1 : 1; /* [31] */
  903. } hctsiz_bit;
  904. };
  905. __IO uint32_t reserved3[3];
  906. }otg_hchannel_type;
  907. typedef struct
  908. {
  909. /**
  910. * @brief otgfs device configuration register, offset:0x800
  911. */
  912. union
  913. {
  914. __IO uint32_t dcfg;
  915. struct
  916. {
  917. __IO uint32_t devspd : 2; /* [1:0] */
  918. __IO uint32_t nzstsouthshk : 1; /* [2] */
  919. __IO uint32_t reserved1 : 1; /* [3] */
  920. __IO uint32_t devaddr : 7; /* [10:4] */
  921. __IO uint32_t perfrint : 2; /* [12:11] */
  922. __IO uint32_t reserved2 : 19; /* [31:13] */
  923. } dcfg_bit;
  924. };
  925. /**
  926. * @brief otgfs device controls register, offset:0x804
  927. */
  928. union
  929. {
  930. __IO uint32_t dctl;
  931. struct
  932. {
  933. __IO uint32_t rwkupsig : 1; /* [0] */
  934. __IO uint32_t sftdiscon : 1; /* [1] */
  935. __IO uint32_t gnpinnaksts : 1; /* [2] */
  936. __IO uint32_t goutnaksts : 1; /* [3] */
  937. __IO uint32_t tstctl : 3; /* [6:4] */
  938. __IO uint32_t sgnpinak : 1; /* [7] */
  939. __IO uint32_t cgnpinak : 1; /* [8] */
  940. __IO uint32_t sgoutnak : 1; /* [9] */
  941. __IO uint32_t cgoutnak : 1; /* [10] */
  942. __IO uint32_t pwroprgdne : 1; /* [11] */
  943. __IO uint32_t reserved1 : 20; /* [31:12] */
  944. } dctl_bit;
  945. };
  946. /**
  947. * @brief otgfs device status register, offset:0x80C
  948. */
  949. union
  950. {
  951. __IO uint32_t dsts;
  952. struct
  953. {
  954. __IO uint32_t suspsts : 1; /* [0] */
  955. __IO uint32_t enumspd : 2; /* [2:1] */
  956. __IO uint32_t eticerr : 1; /* [3] */
  957. __IO uint32_t reserved1 : 4; /* [7:4] */
  958. __IO uint32_t soffn : 14; /* [21:8] */
  959. __IO uint32_t reserved2 : 10; /* [31:22] */
  960. } dsts_bit;
  961. };
  962. __IO uint32_t reserved1;
  963. /**
  964. * @brief otgfs device in endpoint general interrupt mask register, offset:0x810
  965. */
  966. union
  967. {
  968. __IO uint32_t diepmsk;
  969. struct
  970. {
  971. __IO uint32_t xfercmsk : 1; /* [0] */
  972. __IO uint32_t eptdismsk : 1; /* [1] */
  973. __IO uint32_t reserved1 : 1; /* [2] */
  974. __IO uint32_t timeoutmsk : 1; /* [3] */
  975. __IO uint32_t intkntxfempmsk : 1; /* [4] */
  976. __IO uint32_t intkneptmismsk : 1; /* [5] */
  977. __IO uint32_t ineptnakmsk : 1; /* [6] */
  978. __IO uint32_t reserved2 : 1; /* [7] */
  979. __IO uint32_t txfifoudrmsk : 1; /* [8] */
  980. __IO uint32_t bnainmsk : 1; /* [9] */
  981. __IO uint32_t reserved3 : 22; /* [31:10] */
  982. } diepmsk_bit;
  983. };
  984. /**
  985. * @brief otgfs device out endpoint general interrupt mask register, offset:0x814
  986. */
  987. union
  988. {
  989. __IO uint32_t doepmsk;
  990. struct
  991. {
  992. __IO uint32_t xfercmsk : 1; /* [0] */
  993. __IO uint32_t eptdismsk : 1; /* [1] */
  994. __IO uint32_t reserved1 : 1; /* [2] */
  995. __IO uint32_t setupmsk : 1; /* [3] */
  996. __IO uint32_t outtepdmsk : 1; /* [4] */
  997. __IO uint32_t reserved2 : 1; /* [5] */
  998. __IO uint32_t b2bsetupmsk : 1; /* [6] */
  999. __IO uint32_t reserved3 : 1; /* [7] */
  1000. __IO uint32_t outperrmsk : 1; /* [8] */
  1001. __IO uint32_t bnaoutmsk : 1; /* [9] */
  1002. __IO uint32_t reserved4 : 22; /* [31:10] */
  1003. } doepmsk_bit;
  1004. };
  1005. /**
  1006. * @brief otgfs device all endpoint interrupt register, offset:0x818
  1007. */
  1008. union
  1009. {
  1010. __IO uint32_t daint;
  1011. struct
  1012. {
  1013. __IO uint32_t ineptint : 16; /* [15:0] */
  1014. __IO uint32_t outeptint : 16; /* [31:16] */
  1015. } daint_bit;
  1016. };
  1017. /**
  1018. * @brief otgfs device all endpoint interrupt mask register, offset:0x81C
  1019. */
  1020. union
  1021. {
  1022. __IO uint32_t daintmsk;
  1023. struct
  1024. {
  1025. __IO uint32_t ineptmsk : 16; /* [15:0] */
  1026. __IO uint32_t outeptmsk : 16; /* [31:16] */
  1027. } daintmsk_bit;
  1028. };
  1029. __IO uint32_t reserved2[5];
  1030. /**
  1031. * @brief otgfs device in endpoint fifo empty interrupt mask register, offset:0x834
  1032. */
  1033. union
  1034. {
  1035. __IO uint32_t diepempmsk;
  1036. struct
  1037. {
  1038. __IO uint32_t ineptxfemsk : 16; /* [15:0] */
  1039. __IO uint32_t reserved1 : 16; /* [31:16] */
  1040. } diepempmsk_bit;
  1041. };
  1042. } otg_device_type;
  1043. typedef struct
  1044. {
  1045. /**
  1046. * @brief otgfs device out endpoint control register, offset:0x900
  1047. */
  1048. union
  1049. {
  1050. __IO uint32_t diepctl;
  1051. struct
  1052. {
  1053. __IO uint32_t mps : 11; /* [10:0] */
  1054. __IO uint32_t reserved1 : 4; /* [14:11] */
  1055. __IO uint32_t usbacept : 1; /* [15] */
  1056. __IO uint32_t dpid : 1; /* [16] */
  1057. __IO uint32_t naksts : 1; /* [17] */
  1058. __IO uint32_t eptype : 2; /* [19:18] */
  1059. __IO uint32_t reserved2 : 1; /* [20] */
  1060. __IO uint32_t stall : 1; /* [21] */
  1061. __IO uint32_t txfnum : 4; /* [25:22] */
  1062. __IO uint32_t cnak : 1; /* [26] */
  1063. __IO uint32_t snak : 1; /* [27] */
  1064. __IO uint32_t setd0pid : 1; /* [28] */
  1065. __IO uint32_t setd1pid : 1; /* [29] */
  1066. __IO uint32_t eptdis : 1; /* [30] */
  1067. __IO uint32_t eptena : 1; /* [31] */
  1068. } diepctl_bit;
  1069. };
  1070. __IO uint32_t reserved1;
  1071. /**
  1072. * @brief otgfs device in endpoint interrupt register, offset:0x908
  1073. */
  1074. union
  1075. {
  1076. __IO uint32_t diepint;
  1077. struct
  1078. {
  1079. __IO uint32_t xferc : 1; /* [0] */
  1080. __IO uint32_t epdisd : 1; /* [1] */
  1081. __IO uint32_t reserved1 : 1; /* [2] */
  1082. __IO uint32_t timeout : 1; /* [3] */
  1083. __IO uint32_t intkntxfemp : 1; /* [4] */
  1084. __IO uint32_t reserved2 : 1; /* [5] */
  1085. __IO uint32_t ineptnak : 1; /* [6] */
  1086. __IO uint32_t txfemp : 1; /* [7] */
  1087. __IO uint32_t reserved3 : 24; /* [31:8] */
  1088. } diepint_bit;
  1089. };
  1090. __IO uint32_t reserved2;
  1091. /**
  1092. * @brief otgfs device in endpoint transfer size register, offset:0x910 + endpoint number * 0x20
  1093. */
  1094. union
  1095. {
  1096. __IO uint32_t dieptsiz;
  1097. struct
  1098. {
  1099. __IO uint32_t xfersize : 19; /* [18:0] */
  1100. __IO uint32_t pktcnt : 10; /* [28:19] */
  1101. __IO uint32_t mc : 2; /* [30:29] */
  1102. __IO uint32_t reserved1 : 1; /* [31] */
  1103. } dieptsiz_bit;
  1104. };
  1105. __IO uint32_t reserved3;
  1106. /**
  1107. * @brief otgfs device in endpoint tx fifo status register, offset:0x918 + endpoint number * 0x20
  1108. */
  1109. union
  1110. {
  1111. __IO uint32_t dtxfsts;
  1112. struct
  1113. {
  1114. __IO uint32_t ineptxfsav : 16; /* [15:0] */
  1115. __IO uint32_t reserved1 : 16; /* [31:16] */
  1116. } dtxfsts_bit;
  1117. };
  1118. } otg_eptin_type;
  1119. typedef struct
  1120. {
  1121. /**
  1122. * @brief otgfs device out endpoint control register, offset:0xb00 + endpoint number * 0x20
  1123. */
  1124. union
  1125. {
  1126. __IO uint32_t doepctl;
  1127. struct
  1128. {
  1129. __IO uint32_t mps : 11; /* [10:0] */
  1130. __IO uint32_t reserved1 : 4; /* [14:11] */
  1131. __IO uint32_t usbacept : 1; /* [15] */
  1132. __IO uint32_t dpid : 1; /* [16] */
  1133. __IO uint32_t naksts : 1; /* [17] */
  1134. __IO uint32_t eptype : 2; /* [19:18] */
  1135. __IO uint32_t snpm : 1; /* [20] */
  1136. __IO uint32_t stall : 1; /* [21] */
  1137. __IO uint32_t reserved2 : 4; /* [25:22] */
  1138. __IO uint32_t cnak : 1; /* [26] */
  1139. __IO uint32_t snak : 1; /* [27] */
  1140. __IO uint32_t setd0pid : 1; /* [28] */
  1141. __IO uint32_t setd1pid : 1; /* [29] */
  1142. __IO uint32_t eptdis : 1; /* [30] */
  1143. __IO uint32_t eptena : 1; /* [31] */
  1144. } doepctl_bit;
  1145. };
  1146. __IO uint32_t reserved1;
  1147. /**
  1148. * @brief otgfs device out endpoint interrupt register, offset:0xb08 + endpoint number * 0x20
  1149. */
  1150. union
  1151. {
  1152. __IO uint32_t doepint;
  1153. struct
  1154. {
  1155. __IO uint32_t xferc : 1; /* [0] */
  1156. __IO uint32_t epdisd : 1; /* [1] */
  1157. __IO uint32_t reserved1 : 1; /* [2] */
  1158. __IO uint32_t setup : 1; /* [3] */
  1159. __IO uint32_t outtepd : 1; /* [4] */
  1160. __IO uint32_t reserved2 : 1; /* [5] */
  1161. __IO uint32_t b2pstup : 1; /* [6] */
  1162. __IO uint32_t reserved3 : 25; /* [31:7] */
  1163. } doepint_bit;
  1164. };
  1165. __IO uint32_t reserved2;
  1166. /**
  1167. * @brief otgfs device out endpoint transfer size register, offset:0xb10 + endpoint number * 0x20
  1168. */
  1169. union
  1170. {
  1171. __IO uint32_t doeptsiz;
  1172. struct
  1173. {
  1174. __IO uint32_t xfersize : 19; /* [18:0] */
  1175. __IO uint32_t pktcnt : 10; /* [28:19] */
  1176. __IO uint32_t rxdpid_setupcnt : 2; /* [30:29] */
  1177. __IO uint32_t reserved1 : 1; /* [31] */
  1178. } doeptsiz_bit;
  1179. };
  1180. } otg_eptout_type;
  1181. typedef struct
  1182. {
  1183. /**
  1184. * @brief otgfs power and clock gating control registers, offset:0xe00
  1185. */
  1186. union
  1187. {
  1188. __IO uint32_t pcgcctl;
  1189. struct
  1190. {
  1191. __IO uint32_t stoppclk : 1; /* [0] */
  1192. __IO uint32_t reserved1 : 3; /* [3:1] */
  1193. __IO uint32_t suspendm : 1; /* [4] */
  1194. __IO uint32_t reserved2 : 27; /* [31:5] */
  1195. } pcgcctl_bit;
  1196. };
  1197. } otg_pcgcctl_type;
  1198. /**
  1199. * @}
  1200. */
  1201. /** @defgroup USB_exported_functions
  1202. * @{
  1203. */
  1204. /**
  1205. * @brief usb host and device offset address
  1206. */
  1207. #define OTG_HOST_ADDR_OFFSET 0x400 /*!< usb host register offset address */
  1208. #define OTG_HOST_CHANNEL_ADDR_OFFSET 0x500 /*!< usb host channel register offset address */
  1209. #define OTG_DEVICE_ADDR_OFFSET 0x800 /*!< usb device register offset address */
  1210. #define OTG_DEVICE_EPTIN_ADDR_OFFSET 0x900 /*!< usb device endpoint in register offset address */
  1211. #define OTG_DEVICE_EPTOUT_ADDR_OFFSET 0xB00 /*!< usb device endpoint out register offset address */
  1212. #define OTG_PCGCCTL_ADDR_OFFSET 0xE00 /*!< usb power and clock control register offset address */
  1213. #define OTG_FIFO_ADDR_OFFSET 0x1000 /*!< usb fifo offset address */
  1214. /**
  1215. * @brief usb host and device register define
  1216. */
  1217. #define OTG1_GLOBAL ((otg_global_type *)(OTGFS1_BASE)) /*!< usb otg1 global register */
  1218. //#define OTG2_GLOBAL ((otg_global_type *)(OTGFS2_BASE)) /*!< usb otg2 global register */
  1219. #define OTG_PCGCCTL(usbx) ((otg_pcgcctl_type *)((uint32_t)usbx + OTG_PCGCCTL_ADDR_OFFSET)) /*!< usb power and clock control register */
  1220. #define OTG_DEVICE(usbx) ((otg_device_type *)((uint32_t)usbx + OTG_DEVICE_ADDR_OFFSET)) /*!< usb device register */
  1221. #define OTG_HOST(usbx) ((otg_host_type *)((uint32_t)usbx + OTG_HOST_ADDR_OFFSET)) /*!< usb host register */
  1222. #define USB_CHL(usbx, n) ((otg_hchannel_type *)((uint32_t)usbx + OTG_HOST_CHANNEL_ADDR_OFFSET + n * 0x20)) /*!< usb channel n register */
  1223. #define USB_INEPT(usbx, eptn) ((otg_eptin_type *)((uint32_t)usbx + OTG_DEVICE_EPTIN_ADDR_OFFSET + eptn * 0x20)) /*!< usb device endpoint in register */
  1224. #define USB_OUTEPT(usbx, eptn) ((otg_eptout_type *)((uint32_t)usbx + OTG_DEVICE_EPTOUT_ADDR_OFFSET + eptn * 0x20)) /*!< usb device endpoint out register */
  1225. #define USB_FIFO(usbx, eptn) *(__IO uint32_t *)((uint32_t)usbx + OTG_FIFO_ADDR_OFFSET + eptn * 0x1000) /*!< usb fifo address */
  1226. typedef otg_global_type usb_reg_type;
  1227. /** @defgroup USB_exported_functions
  1228. * @{
  1229. */
  1230. #ifdef OTGFS_USB_GLOBAL
  1231. error_status usb_global_reset(otg_global_type *usbx);
  1232. void usb_global_init(otg_global_type *usbx);
  1233. otg_global_type *usb_global_select_core(uint8_t usb_id);
  1234. void usb_flush_tx_fifo(otg_global_type *usbx, uint32_t fifo_num);
  1235. void usb_flush_rx_fifo(otg_global_type *usbx);
  1236. void usb_global_interrupt_enable(otg_global_type *usbx, uint16_t interrupt, confirm_state new_state);
  1237. uint32_t usb_global_get_all_interrupt(otg_global_type *usbx);
  1238. void usb_global_clear_interrupt(otg_global_type *usbx, uint32_t flag);
  1239. void usb_interrupt_enable(otg_global_type *usbx);
  1240. void usb_interrupt_disable(otg_global_type *usbx);
  1241. void usb_set_rx_fifo(otg_global_type *usbx, uint16_t size);
  1242. void usb_set_tx_fifo(otg_global_type *usbx, uint8_t txfifo, uint16_t size);
  1243. void usb_global_set_mode(otg_global_type *usbx, uint32_t mode);
  1244. void usb_global_power_on(otg_global_type *usbx);
  1245. void usb_write_packet(otg_global_type *usbx, uint8_t *pusr_buf, uint16_t num, uint16_t nbytes);
  1246. void usb_read_packet(otg_global_type *usbx, uint8_t *pusr_buf, uint16_t num, uint16_t nbytes);
  1247. void usb_stop_phy_clk(otg_global_type *usbx);
  1248. void usb_open_phy_clk(otg_global_type *usbx);
  1249. #endif
  1250. #ifdef OTGFS_USB_DEVICE
  1251. void usb_ept_open(otg_global_type *usbx, usb_ept_info *ept_info);
  1252. void usb_ept_close(otg_global_type *usbx, usb_ept_info *ept_info);
  1253. void usb_ept_stall(otg_global_type *usbx, usb_ept_info *ept_info);
  1254. void usb_ept_clear_stall(otg_global_type *usbx, usb_ept_info *ept_info);
  1255. uint32_t usb_get_all_out_interrupt(otg_global_type *usbx);
  1256. uint32_t usb_get_all_in_interrupt(otg_global_type *usbx);
  1257. uint32_t usb_ept_out_interrupt(otg_global_type *usbx, uint32_t eptn);
  1258. uint32_t usb_ept_in_interrupt(otg_global_type *usbx, uint32_t eptn);
  1259. void usb_ept_out_clear(otg_global_type *usbx, uint32_t eptn, uint32_t flag);
  1260. void usb_ept_in_clear(otg_global_type *usbx, uint32_t eptn, uint32_t flag);
  1261. void usb_set_address(otg_global_type *usbx, uint8_t address);
  1262. void usb_ept0_start(otg_global_type *usbx);
  1263. void usb_ept0_setup(otg_global_type *usbx);
  1264. void usb_connect(otg_global_type *usbx);
  1265. void usb_disconnect(otg_global_type *usbx);
  1266. void usb_remote_wkup_set(otg_global_type *usbx);
  1267. void usb_remote_wkup_clear(otg_global_type *usbx);
  1268. uint8_t usb_suspend_status_get(otg_global_type *usbx);
  1269. #endif
  1270. #ifdef OTGFS_USB_HOST
  1271. void usb_port_power_on(otg_global_type *usbx, confirm_state state);
  1272. uint32_t usbh_get_frame(otg_global_type *usbx);
  1273. void usb_hc_enable(otg_global_type *usbx,
  1274. uint8_t chn,
  1275. uint8_t ept_num,
  1276. uint8_t dev_address,
  1277. uint8_t type,
  1278. uint16_t maxpacket,
  1279. uint8_t speed);
  1280. uint32_t usb_hch_read_interrupt(otg_global_type *usbx);
  1281. void usb_host_disable(otg_global_type *usbx);
  1282. void usb_hch_halt(otg_global_type *usbx, uint8_t chn);
  1283. void usbh_fsls_clksel(otg_global_type *usbx, uint8_t clk);
  1284. #endif
  1285. /**
  1286. * @}
  1287. */
  1288. /**
  1289. * @}
  1290. */
  1291. /**
  1292. * @}
  1293. */
  1294. /**
  1295. * @}
  1296. */
  1297. #ifdef __cplusplus
  1298. }
  1299. #endif
  1300. #endif