at32f423_usb.h 54 KB

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