core_usb_ll_driver.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #include "user.h"
  22. #define USB_FIFO_MAX (512)
  23. #define USB_FIFO_DW_DAX (128)
  24. /**
  25. * 结构说明:调试控制寄存器(USBPHY_CR1)结构
  26. * 偏移地址:0x0108
  27. * 初 始 值:0x004921AE
  28. * 属 性:RW
  29. * 宽 度:32bit
  30. */
  31. typedef union _USBPHY_CR1_TypeDef
  32. {
  33. uint32_t d32;
  34. struct{
  35. uint32_t txpreemphasistune : 1; /* */
  36. uint32_t txrisetune : 2; /* */
  37. uint32_t txvreftune : 4; /* */
  38. uint32_t txfslstune : 4; /* */
  39. uint32_t sqrxtune : 3; /* */
  40. uint32_t compdistune : 3; /* */
  41. uint32_t otgtune : 3; /* */
  42. uint32_t loopback_enb : 1; /* */
  43. uint32_t otg_disable : 1; /* */
  44. uint32_t commononn : 1; /* */
  45. uint32_t vatestenb : 1; /* */
  46. uint32_t lsbist : 1; /* */
  47. uint32_t fsbist : 1; /* */
  48. uint32_t hsbist : 1; /* */
  49. uint32_t bisten : 1; /* */
  50. uint32_t usb_iddq : 1; /* */
  51. uint32_t stop_ck_for_suspend : 1; /* */
  52. uint32_t bist_done : 1; /* */
  53. uint32_t bist_error : 1; /* */
  54. } b;
  55. } USBPHY_CR1_TypeDef;
  56. /**
  57. * 结构说明:调试控制寄存器(USBPHY_CR3)结构
  58. * 偏移地址:0x010C
  59. * 初 始 值:0x00000000
  60. * 属 性:RW
  61. * 宽 度:32bit
  62. */
  63. typedef union _USBPHY_CR3_TypeDef
  64. {
  65. uint32_t d32;
  66. struct{
  67. uint32_t idpullup : 1; /* 用于操作 USB PHY的idpullup,启动PHY探测ID状态 */
  68. uint32_t iddig : 1; /* PHY 输出的ID状态信号,用于软件查询 */
  69. uint32_t reserved2_31 : 29;
  70. } b;
  71. } USBPHY_CR3_TypeDef;
  72. typedef struct
  73. {
  74. // Common USB Registers. 0x00 - 0x0F.
  75. __IO uint8_t FADDR; // Peripheral Mode Only.
  76. union
  77. {
  78. __IO uint8_t POWER;
  79. struct
  80. {
  81. __IO uint8_t en_suspendM : 1;
  82. __IO uint8_t suspend_mode : 1;
  83. __IO uint8_t resume : 1;
  84. __IO uint8_t reset : 1;
  85. __IO uint8_t HS_mode : 1; //不支持
  86. __IO uint8_t HS_enab : 1; //不支持
  87. __IO uint8_t soft_conn : 1; /* Periphera mode only */
  88. __IO uint8_t ISO_update : 1; /* Periphera mode only */
  89. } POWER_b;
  90. };
  91. union
  92. {
  93. __IO uint16_t INTRTX;
  94. struct
  95. {
  96. __IO uint16_t EP0_intp : 1;
  97. __IO uint16_t EP1_tx_intp : 1;
  98. __IO uint16_t EP2_tx_intp : 1;
  99. __IO uint16_t EP3_tx_intp : 1;
  100. __IO uint16_t EP4_tx_intp : 1;
  101. __IO uint16_t EP5_tx_intp : 1;
  102. __IO uint16_t EP6_tx_intp : 1;
  103. __IO uint16_t EP7_tx_intp : 1;
  104. __IO uint16_t EP8_tx_intp : 1;
  105. __IO uint16_t EP9_tx_intp : 1;
  106. __IO uint16_t EP10_tx_intp : 1;
  107. __IO uint16_t EP11_tx_intp : 1;
  108. __IO uint16_t EP12_tx_intp : 1;
  109. __IO uint16_t EP13_tx_intp : 1;
  110. __IO uint16_t EP14_tx_intp : 1;
  111. __IO uint16_t EP15_tx_intp : 1;
  112. } INTRTX_b;
  113. };
  114. union
  115. {
  116. __IO uint16_t INTRRX;
  117. struct
  118. {
  119. uint16_t reserved0 : 1;
  120. uint16_t EP1_rx_intp : 1;
  121. uint16_t EP2_rx_intp : 1;
  122. uint16_t EP3_rx_intp : 1;
  123. uint16_t EP4_rx_intp : 1;
  124. uint16_t EP5_rx_intp : 1;
  125. uint16_t EP6_rx_intp : 1;
  126. uint16_t EP7_rx_intp : 1;
  127. uint16_t EP8_rx_intp : 1;
  128. uint16_t EP9_rx_intp : 1;
  129. uint16_t EP10_rx_intp : 1;
  130. uint16_t EP11_rx_intp : 1;
  131. uint16_t EP12_rx_intp : 1;
  132. uint16_t EP13_rx_intp : 1;
  133. uint16_t EP14_rx_intp : 1;
  134. uint16_t EP15_rx_intp : 1;
  135. } INTRRX_b;
  136. };
  137. union
  138. {
  139. __IO uint16_t INTRTXE;
  140. struct
  141. {
  142. uint16_t en_EP0_intp : 1;
  143. uint16_t en_EP1_tx_intp : 1;
  144. uint16_t en_EP2_tx_intp : 1;
  145. uint16_t en_EP3_tx_intp : 1;
  146. uint16_t en_EP4_tx_intp : 1;
  147. uint16_t en_EP5_tx_intp : 1;
  148. uint16_t en_EP6_tx_intp : 1;
  149. uint16_t en_EP7_tx_intp : 1;
  150. uint16_t en_EP8_tx_intp : 1;
  151. uint16_t en_EP9_tx_intp : 1;
  152. uint16_t en_EP10_tx_intp : 1;
  153. uint16_t en_EP11_tx_intp : 1;
  154. uint16_t en_EP12_tx_intp : 1;
  155. uint16_t en_EP13_tx_intp : 1;
  156. uint16_t en_EP14_tx_intp : 1;
  157. uint16_t en_EP15_tx_intp : 1;
  158. } INTRTXE_b;
  159. };
  160. union
  161. {
  162. __IO uint16_t INTRRXE;
  163. struct
  164. {
  165. uint16_t reserved0 : 1;
  166. uint16_t en_EP1_rx_intp : 1;
  167. uint16_t en_EP2_rx_intp : 1;
  168. uint16_t en_EP3_rx_intp : 1;
  169. uint16_t en_EP4_rx_intp : 1;
  170. uint16_t en_EP5_rx_intp : 1;
  171. uint16_t en_EP6_rx_intp : 1;
  172. uint16_t en_EP7_rx_intp : 1;
  173. uint16_t en_EP8_rx_intp : 1;
  174. uint16_t en_EP9_rx_intp : 1;
  175. uint16_t en_EP10_rx_intp : 1;
  176. uint16_t en_EP11_rx_intp : 1;
  177. uint16_t en_EP12_rx_intp : 1;
  178. uint16_t en_EP13_rx_intp : 1;
  179. uint16_t en_EP14_rx_intp : 1;
  180. uint16_t en_EP15_rx_intp : 1;
  181. } INTRRXE_b;
  182. };
  183. union
  184. {
  185. __IO uint8_t INTRUSB;
  186. struct
  187. {
  188. uint8_t suspend : 1; /* Set when Suspend signaling is detected on the bus. Only valid in Peripheral mode. */
  189. uint8_t resume : 1; /* Set when Resume signaling is detected on the bus while the MUSBMHDRC is in Suspend mode. */
  190. uint8_t reset_babble : 1; /* Reset: Set in Peripheral mode when Reset signaling is detected on the bus.
  191. * Babble: Set in Host mode when babble is detected. Note: Only active after first SOF has been sent.
  192. */
  193. uint8_t sof : 1; /* Set when a new frame starts. */
  194. uint8_t conn : 1; /* Set when a device connection is detected. Only valid in Host mode. Valid at all transaction speeds. */
  195. uint8_t discon : 1; /* Set in Host mode when a device disconnect is detected. Set in Peripheral mode when a session ends. Valid at all transaction speeds. */
  196. uint8_t sess_req : 1; /* Set when Session Request signaling has been detected. Only valid when MUSBMHDRC is A device. */
  197. uint8_t VBus_error : 1; /* Set when VBus drops below the VBus Valid threshold during a session. Only valid when MUSBMHDRC is A device. */
  198. } INTRUSB_b;
  199. };
  200. union
  201. {
  202. __IO uint8_t INTRUSBE;
  203. struct
  204. {
  205. uint8_t en_suspend : 1;
  206. uint8_t en_resume : 1;
  207. uint8_t en_reset_babble : 1;
  208. uint8_t en_sof : 1;
  209. uint8_t en_conn : 1;
  210. uint8_t en_discon : 1;
  211. uint8_t en_sess_req : 1;
  212. uint8_t en_VBus_error : 1;
  213. } INTRUSBE_b;
  214. };
  215. union
  216. {
  217. __IO uint16_t FRAME;
  218. struct
  219. {
  220. uint16_t frame_number : 11;
  221. uint16_t reserved11_15 : 5; /* Always return 0 */
  222. } FRAME_b;
  223. };
  224. __IO uint8_t INDEX;
  225. __IO uint8_t TESTMODE;
  226. // Indexed CSR. 0x10 - 0x1F.
  227. union
  228. {
  229. __IO uint16_t TXMAXP;
  230. struct
  231. {
  232. __IO uint16_t max_payload_tran : 11; /* Bits 10:0 define(in bytes)the maximum payload transmitted in single transaction.
  233. * The value set can be up to 1024 bytes but is subject to the constraints place by
  234. * the USB Specification on packet sizes for Bulk,Interrupt and Isochronous transfers
  235. * in Full-speed and High-speed operations.
  236. */
  237. __IO uint16_t ex_max : 5;
  238. } TXMAXP_b;
  239. };
  240. union
  241. {
  242. __IO uint8_t CSR0L;
  243. __IO uint8_t TXCSRL;
  244. struct
  245. {
  246. __IO uint8_t rx_pkt_rdy : 1; /* This bit is set when a data packet has been received. An interrupt is
  247. * generated when this bit is set. The CPU clear this bit by setting the
  248. * ServicedRxPktRdy bit.
  249. */
  250. __IO uint8_t tx_pkt_rdy : 1; /* The CPU sets this bits after loading a data packet into the FIFO. It is clear
  251. * automatically when a data packet has been transmitted. An interrupt is also
  252. * generated at this point(if enabled).
  253. */
  254. __IO uint8_t sent_stall : 1; /* This bit is set when a STALL handshake is transmitted.
  255. * The CPU should clear this bit.
  256. */
  257. __IO uint8_t data_end : 1; /* The CPU sets this bit:
  258. * 1. When setting TxPktRdy for the last data packet.
  259. * 2. When clearing RxPktRdy after unloading the last data packet.
  260. * 3. When setting TxPktRdy for zero length data packet.
  261. * It is cleared automatically.
  262. */
  263. __IO uint8_t setup_end : 1; /* This bit will be set when a control transaction ends before the DataEnd
  264. * bit has been set. An interrupt will be generated and the FIFO flushed at
  265. * this time.The bit is cleared by the CPU writing a 1 to the ServicedSetupEnd bit.
  266. */
  267. __IO uint8_t send_stall : 1; /* The CPU write a 1 to this bit to terminate the current transaction.
  268. * The STALL handshake will be transmitted and then this bit will be
  269. * cleared automatically.
  270. */
  271. __IO uint8_t serviced_rxpktrdy : 1; /* The CPU write a 1 to this bit to clear the RxPktRdy bit.
  272. * It is Cleared automatically.
  273. */
  274. __IO uint8_t serviced_setupend : 1; /* The CPU write a 1 to this bit to clear the SetupEnd bit.
  275. * It is Cleared automatically.
  276. */
  277. } CSR0L_DEV_b;
  278. struct
  279. {
  280. __IO uint8_t rx_pkt_rdy : 1; /* This bit is set when a data packet has been received.An interrupt is generated
  281. * (If enabled)when this bit set.The CPU should clear this bit when the packet has
  282. * been read from the FIFO.
  283. */
  284. __IO uint8_t tx_pkt_rdy : 1; /* The CPU sets this bit after loading a data packet into the FIFO.It is cleared
  285. * automatically when a data packet has been transmitted. An interrupt is also
  286. * generated at this point(If enabled).
  287. */
  288. __IO uint8_t rx_stall : 1; /* This bit is set when a STALL handshake is received. The CPU should clear this bit. */
  289. __IO uint8_t setup_pkt : 1; /* The CPU sets this bit,at the TxPktRdy bit is set,to send a SETUP token instead
  290. * of an OUT token for the transaction. Note: Setting this bit also clear the Data
  291. * Toggle.
  292. */
  293. __IO uint8_t error : 1; /* This bit will be set when three attempts have been made to perform a transaction
  294. * with no response from the peripheral.The CPU should clear this bit.An interrupt
  295. * is generated when this bit is set.
  296. */
  297. __IO uint8_t req_pkt : 1; /* The CPU sets this bit to request an IN transaction.
  298. * It is cleared when RxPktRdy is set.
  299. */
  300. __IO uint8_t status_pkt : 1; /* The CPU sets this bit at the same time as the TxPktRdy or ReqPkt bit is set,
  301. * to perform a status stage transaction. Setting this bit ensures that the data
  302. * toggle is set to 1 so that a DATA1 packet is used for the Status Stage transaction.
  303. */
  304. __IO uint8_t nak_timeout : 1; /* This bit will be set when Endpoint 0 is halted following the receipt for longer
  305. * than the time set by the NAKLimit0 register. The CPU should clear this bit to allow
  306. * the endpoint to continue.
  307. */
  308. } CSR0L_HOST_b;
  309. struct
  310. {
  311. __IO uint8_t tx_pkt_rdy : 1; /* The CPU sets this bit after loading a data packet into the FIFO. */
  312. __IO uint8_t fifo_not_empty : 1; /* The USB sets this bit when there is at least 1 packet in the TX FIFO. */
  313. __IO uint8_t under_run : 1; /* The USB sets this bit if an IN token is received when TxPktRdy is not set. The CPU should clear this bit. */
  314. __IO uint8_t flush_fifo : 1; /* The CPU writes a 1 to this bit to flush the latest packet from the endpoint TX FIFO */
  315. __IO uint8_t send_stall : 1; /* The CPU writes a 1 to this bit to issue a STALL handshake to an IN token.The CPU
  316. * clears this bit to terminate the stall condition.
  317. * Note:This bit has no effect where the endpoint is being used for Isochronous transfers.
  318. */
  319. __IO uint8_t sent_stall : 1; /* This bit is set when a STALL handshake is transmitted.The FIFO is flushed and
  320. * the TxPktRdy bit is cleared(see below).The CPU should clear this bit.
  321. */
  322. __IO uint8_t clr_data_tog : 1; /* The CPU writes a 1 to this bit to reset the endpoint data toggle to 0. */
  323. __IO uint8_t incomp_tx : 1; /* When the endpoint is being used for high-bandwidth Isochronous,this bit is set to
  324. * indicate where a large packet has been split into 2 or 3 packets for transmission
  325. * but insufficient IN tokens have been received to send all the parts.
  326. * Note:In anything other than isochronous transfers,this bit will always return 0.
  327. */
  328. } TXCSRL_DEV_b;
  329. struct
  330. {
  331. __IO uint8_t tx_pkt_rdy : 1; /* The CPU sets this bit after loading a data packet into the FIFO. */
  332. __IO uint8_t fifo_not_empty : 1; /* The USB sets this bit when there is at least 1 packet in the TX FIFO. */
  333. __IO uint8_t error : 1;
  334. __IO uint8_t flush_fifo : 1; /* The CPU writes a 1 to this bit to flush the latest packet from the endpoint TX FIFO */
  335. __IO uint8_t setup_Pkt : 1;
  336. __IO uint8_t Rx_stall : 1;
  337. __IO uint8_t clr_data_tog : 1; /* The CPU writes a 1 to this bit to reset the endpoint data toggle to 0. */
  338. __IO uint8_t NAK_timeout_incompTx : 1;
  339. } TXCSRL_HOST_b;
  340. };
  341. union
  342. {
  343. __IO uint8_t CSR0H;
  344. __IO uint8_t TXCSRH;
  345. struct
  346. {
  347. __IO uint8_t flush_fifo : 1; /* The CPU writes a 1 to this bit to flush the next packet to be transmitted/read from
  348. * the Endpoint 0 FIFO. The FIFO pointer is reset and the TxPktRdy/RxPktRdy bit(below) is
  349. * cleared. Note:FlushFIFO should only be used when TxPktRdy/RxPktRdy is set.At other
  350. * times, it may cause data to be corrupted.
  351. */
  352. __IO uint8_t reserved1_7 : 7; /* Unused. Return 0 when resd. */
  353. } CSR0H_DEV_b;
  354. struct
  355. {
  356. __IO uint8_t flush_fifo : 1; /* The CPU writes a 1 to this bit to flush the next packet to be transmitted/read from
  357. * the Endpoint 0 FIFO. The FIFO pointer is reset and the TxPktRdy/RxPktRdy bit(below) is
  358. * cleared. Note:FlushFIFO should only be used when TxPktRdy/RxPktRdy is set.At other
  359. * times, it may cause data to be corrupted.
  360. */
  361. __IO uint8_t data_toggle : 1; /* When read,this bit indicates the current state of the Endpoint 0 data toggle. If D10
  362. * is high,this bit may be written with the written with the required setting of the data
  363. * toggle.If D10 is low,any value written to this bit is ignored.
  364. */
  365. __IO uint8_t data_toggle_wr_en : 1; /* The CPU written a 1 to this bit to enable the current state of Endpoint 0 data toggle
  366. * to be written(see Data Toggle bit,below).This bit is automatically cleared once the new
  367. * value is written.
  368. */
  369. __IO uint8_t dis_ping : 1; /* The CPU writes a 1 to this bit to instruct the core not to issue PING tokens in data
  370. * and status phases of a high-speed Control transfer(for use with devices that do not
  371. * respond to PING).
  372. */
  373. __IO uint8_t reserved1_7 : 4; /* Unused. Return 0 when resd. */
  374. } CSR0H_HOST_b;
  375. struct
  376. {
  377. __IO uint8_t reserved0_1 : 2;
  378. __IO uint8_t dma_req_mode : 1;
  379. __IO uint8_t frc_data_tog : 1;
  380. __IO uint8_t dma_req_enab : 1;
  381. __IO uint8_t mode : 1;
  382. __IO uint8_t iso : 1;
  383. __IO uint8_t auto_set : 1;
  384. } TXCSRH_DEV_b;
  385. struct
  386. {
  387. __IO uint8_t data_toggle : 1;
  388. __IO uint8_t data_toggle_wren : 1;
  389. __IO uint8_t dma_req_mode : 1;
  390. __IO uint8_t frc_data_tog : 1;
  391. __IO uint8_t dma_req_enab : 1;
  392. __IO uint8_t mode : 1;
  393. __IO uint8_t reserved6 : 1;
  394. __IO uint8_t auto_set : 1;
  395. } TXCSRH_HOST_b;
  396. };
  397. union
  398. {
  399. __IO uint16_t RXMAXP;
  400. struct
  401. {
  402. __IO uint16_t max_payload_tran : 11;
  403. __IO uint16_t ex_max : 5;
  404. } RXMAXP_b;
  405. };
  406. union
  407. {
  408. __IO uint8_t RXCSRL;
  409. struct
  410. {
  411. __IO uint8_t rx_pkt_rdy : 1; /* This bit is set when a data packet has been received */
  412. __IO uint8_t fifo_full : 1; /* */
  413. __IO uint8_t over_run : 1; /* */
  414. __IO uint8_t data_error : 1; /* */
  415. __IO uint8_t flush_fifo : 1;
  416. __IO uint8_t send_stall : 1; /* The CPU writes a 1 to this bit to issue a STALL handshake. */
  417. __IO uint8_t sent_stall : 1; /* This bit is set when a STALL handshake is transmitted. The CPU should clear this bit. */
  418. __IO uint8_t clr_data_tog : 1;
  419. } RXCSRL_DEV_b;
  420. struct
  421. {
  422. __IO uint8_t rx_pkt_rdy : 1; /* This bit is set when a data packet has been received */
  423. __IO uint8_t fifo_full : 1; /* */
  424. __IO uint8_t error : 1; /* */
  425. __IO uint8_t data_error : 1; /* */
  426. __IO uint8_t flush_fifo : 1;
  427. __IO uint8_t ReqPkt : 1;
  428. __IO uint8_t RxStall : 1;
  429. __IO uint8_t clr_data_tog : 1;
  430. } RXCSRL_HOST_b;
  431. };
  432. union
  433. {
  434. __IO uint8_t RXCSRH;
  435. struct
  436. {
  437. __IO uint8_t incomp_rx : 1;
  438. __IO uint8_t reserved1_2 : 2;
  439. __IO uint8_t dma_req_mode : 1;
  440. __IO uint8_t dis_nyet_pid_error : 1;
  441. __IO uint8_t dma_req_enab : 1;
  442. __IO uint8_t iso : 1;
  443. __IO uint8_t auto_clear : 1;
  444. } RXCSRH_DEV_b;
  445. struct
  446. {
  447. __IO uint8_t incomp_rx : 1;
  448. __IO uint8_t data_toggle : 1;
  449. __IO uint8_t data_toggle_wren : 1;
  450. __IO uint8_t dma_req_mode : 1;
  451. __IO uint8_t PID_error : 1;
  452. __IO uint8_t dma_req_enab : 1;
  453. __IO uint8_t auto_req : 1;
  454. __IO uint8_t auto_clear : 1;
  455. } RXCSRH_HOST_b;
  456. };
  457. union
  458. {
  459. __IO uint8_t COUNT0;
  460. __IO uint16_t RXCOUNT;
  461. struct
  462. {
  463. __IO uint8_t ep0_rx_count : 7;
  464. __IO uint8_t reserved7 : 1;
  465. } COUNT0_b;
  466. };
  467. union
  468. {
  469. __IO uint8_t TYPE0; // Host Mode Only.
  470. __IO uint8_t TXTYPE; // Host Mode Only.
  471. struct
  472. {
  473. __IO uint8_t reserved0_5 : 6; /* Reserved */
  474. __IO uint8_t speed : 2; /* Operating speed of the target device :
  475. * 00:Unused(Note: If selected,the target will be using the same connectionn speed as the core.)
  476. * 01:High
  477. * 10:Full
  478. * 11:Low
  479. */
  480. } TYPE0_b;
  481. struct
  482. {
  483. __IO uint8_t target_EP_number : 4; /* Operating speed of the target device when the core is configured with the
  484. * multipoint option:
  485. * 00: Unused 01: High
  486. * 10: Full 11: Low
  487. * When the core is not configured with the multipoint option these bits should
  488. * not be accessed.
  489. */
  490. __IO uint8_t protocol : 2; /* The CPU should set this to select the required protocol for TX endpoint:
  491. * 00: Control 01: Isochronous
  492. * 10: Bulk 11: Interrupt
  493. */
  494. __IO uint8_t speed : 2; /* Operating speed of the target device when the core is configured with
  495. * the multipoint option:
  496. * 00:Unused 01: High 10: Full 11:Low
  497. * when the core is not configured with the multipoint option these bits
  498. * should not be accessed.
  499. */
  500. } TXTYPE_b;
  501. };
  502. union
  503. {
  504. __IO uint8_t NAKLIMIT0; // Host Mode Only.
  505. __IO uint8_t TXINTERVAL; // Host Mode Only.
  506. struct
  507. {
  508. __IO uint8_t Endpoint0_NAK_Limit : 5; /* Endpoint 0 NAK limit (m) */
  509. __IO uint8_t reserved5_7 : 3; /* Reserve */
  510. } NAKLIMIT0_b;
  511. struct
  512. {
  513. __IO uint8_t Tx_polling_interval :8;
  514. } TXINTERVAL_b;
  515. };
  516. union
  517. {
  518. __IO uint8_t RXTYPE; // Host Mode Only.
  519. struct
  520. {
  521. __IO uint8_t target_EP_number : 4; /* Operating speed of the target device when the core is configured with the
  522. * multipoint option:
  523. * 00: Unused 01: High
  524. * 10: Full 11: Low
  525. * When the core is not configured with the multipoint option these bits should
  526. * not be accessed.
  527. */
  528. __IO uint8_t protocol : 2; /* The CPU should set this to select the required protocol for TX endpoint:
  529. * 00: Control 01: Isochronous
  530. * 10: Bulk 11: Interrupt
  531. */
  532. __IO uint8_t speed : 2; /* Operating speed of the target device when the core is configured with
  533. * the multipoint option:
  534. * 00:Unused 01: High 10: Full 11:Low
  535. * when the core is not configured with the multipoint option these bits
  536. * should not be accessed.
  537. */
  538. } RXTYPE_b;
  539. };
  540. __IO uint8_t RXINTERVAL; // Host Mode Only.
  541. uint8_t UNUSED0;
  542. union
  543. {
  544. __IO uint8_t CONFIGDATA;
  545. __IO uint8_t FIFOSIZE;
  546. struct
  547. {
  548. __IO uint8_t utmi_data_width : 1; /* Indicates selected UTMI+ data width. Always 0 indicating 8 bits. */
  549. __IO uint8_t soft_cone : 1; /* Always "1" . Indicates Soft Connect/Disconnect. */
  550. __IO uint8_t dyn_fifo_sizing : 1; /* When set to "1" indicates Dynamic FIFO Sizing option selected. */
  551. __IO uint8_t hb_txe : 1; /* When set to "1" indicates High-bandwidth TX ISO Endpoint Support selected */
  552. __IO uint8_t hb_rxe : 1; /* When set to "1" indicates High-bandwidth Rx ISO Endpoint Support selected. */
  553. __IO uint8_t big_endian : 1; /* Always "0". Indicates Little Endian ordering. */
  554. __IO uint8_t mp_txe : 1; /* When set to "1" automatic splitting of bulk packets is selected */
  555. __IO uint8_t mp_rxe : 1; /* When set to "1" automatic amalgamation of bulk packets is selected */
  556. } CONFIGDATA_b;
  557. struct
  558. {
  559. __IO uint8_t tx_fifo_size : 4;
  560. __IO uint8_t rx_fifo_size : 4;
  561. } b;
  562. };
  563. union
  564. {
  565. // FIFOS, EP0 - EP15 0x20 - 0x5F.
  566. __IO uint8_t byte;
  567. __IO uint16_t word;
  568. __IO uint32_t dword;
  569. } FIFOX[16];
  570. // OTG, DynFIFO + Version. 0x60 - 0x6F.
  571. union
  572. {
  573. __IO uint8_t DEVCTL;
  574. struct
  575. {
  576. __IO uint8_t session : 1; /* When operation as an 'A' device,this bit is set or cleared by the CPU to
  577. * start or end a session. When operating as a 'B' device,this bit is set/cleared
  578. * by the MUSBMHDRC when a session starts/ends. It is also set by the CPU to initiate
  579. * the Session Request Protocol.When the MUSBMHDRC is in Suspend mode,the bit may be
  580. * cleared by the CPU to perform a software disconnect.Note:Clearing this bit when the
  581. * core is not suspended will result in undefined behavior.
  582. */
  583. __IO uint8_t host_req : 1; /* When set,the MUSBMHDRC will initiate the Host Negotiation when Suspend
  584. * mode is entered.It is cleared when Host Negotiation is completed.
  585. */
  586. __IO uint8_t host_mode : 1; /* This Read-only bit is set when the MUSBMHDRC is acting as a Host. */
  587. __IO uint8_t VBus : 2; /* These Read-only bits encode the current VBus level as follows:
  588. * D4 D3 Meaning
  589. * 0 0 Below SessionEnd
  590. * 0 1 Above SessionEnd,below AValid
  591. * 1 0 Above AValid,below VBus Valid
  592. * 1 1 Above VBus Valid
  593. */
  594. __IO uint8_t LSDev : 1; /* This Read-only bit is set when a low-speed device has been detected being
  595. * connected to the port. Only valid in Host mode.
  596. */
  597. __IO uint8_t FSDev : 1; /* This Read-only bit is set when a full-speed or high-speed device has been
  598. * detected being connected to the port.(High-speed device are distinguished
  599. * from full-speed by checking for high-speed chirps when the device is reset.)
  600. * Only valid in Host mode.
  601. */
  602. __IO uint8_t B_Device : 1; /* This Read-only bit indicates whether the MUSBMHDRC is operating
  603. * as the 'A' device or the 'B' device.
  604. * 0 => 'A' device; 1 => 'B' device.
  605. * Only valid while a session is in progress. To determine the role
  606. * when no session is in progress, set the session bit and read this bit.
  607. * NOTE: If the core is in Force_Host mode(i.e. a session has been started
  608. * with Testmode.D7 = 1),this bit will indicate the state of the HOSTDISCON
  609. * input signal from the PHY.
  610. */
  611. } DEVCTL_b;
  612. };
  613. union
  614. {
  615. __IO uint8_t MISC;
  616. struct
  617. {
  618. __IO uint8_t rx_edma : 1; /* 1'b0:DMA_REQ signal for all OUT Endpoints will be de-asserted when MAXP
  619. * bytes have been read to an endpoint.This is late mode.
  620. * 1'b1:DMA_REQ signal for all OUT Endpoints will be de-asserted when MAXP-8
  621. * bytes have been read to an endpoint.This is early mode.
  622. */
  623. __IO uint8_t tx_edma : 1; /* 1'b0:DMA_REQ signal for all IN Endpoints will be de-asserted when MAXP
  624. * bytes have been written to an endpoint.This is late mode.
  625. * 1'b1:DMA_REQ signal for all IN Endpoints will be de-asserted when MAXP-8
  626. * bytes have been written to an endpoint.This is early mode.
  627. */
  628. __IO uint8_t reserved2_7 : 6; /* These bits are reserved. */
  629. } MISC_b;
  630. };
  631. union
  632. {
  633. __IO uint8_t TXFIFOSZ;
  634. struct
  635. {
  636. __IO uint8_t size : 4;
  637. __IO uint8_t double_packet_buffer : 1;
  638. __IO uint8_t reserved5_7 : 3;
  639. } TXFIFOSZ_b;
  640. };
  641. union
  642. {
  643. __IO uint8_t RXFIFOSZ;
  644. struct
  645. {
  646. __IO uint8_t size : 4;
  647. __IO uint8_t double_packet_buffer : 1;
  648. __IO uint8_t reserved5_7 : 3;
  649. } RXFIFOSZ_b;
  650. };
  651. __IO uint16_t TXFIFOADD;
  652. __IO uint16_t RXFIFOADD;
  653. union
  654. {
  655. __O uint32_t VCONTROL;
  656. __IO uint32_t VSTATUS;
  657. }VCONTROL_VSTATUS;
  658. union
  659. {
  660. __IO uint16_t HWVERS;
  661. struct
  662. {
  663. __IO uint16_t minor_version_number : 10;
  664. __IO uint16_t major_version_number : 5;
  665. __IO uint16_t rc : 1;
  666. } HWVERS_b;
  667. };
  668. uint8_t UNUSED1[2];
  669. // ULPI & Addnl. Config. registers. 0x70 - 0x7F.
  670. __IO uint8_t ULPIVBUSCONTROL;
  671. __IO uint8_t ULPICARKITCONTROL;
  672. __IO uint8_t ULPIINTMASK;
  673. __IO uint8_t ULPIINTSRC;
  674. __IO uint8_t ULPIREGDATA;
  675. __IO uint8_t ULPIREGADDR;
  676. __IO uint8_t ULPIREGCONTROL;
  677. __IO uint8_t ULPIRAWDATA;
  678. union
  679. {
  680. __IO uint8_t EPINFO;
  681. struct
  682. {
  683. __IO uint8_t tx_endpoint : 4;
  684. __IO uint8_t rx_endpoint : 4;
  685. } EPINFO_b;
  686. };
  687. union
  688. {
  689. __IO uint8_t RAMINFO;
  690. struct
  691. {
  692. __IO uint8_t ram_bits : 4;
  693. __IO uint8_t dma_chans : 4;
  694. } RAMINFO_b;
  695. };
  696. union
  697. {
  698. __IO uint8_t LINKINFO;
  699. struct
  700. {
  701. __IO uint8_t wtid : 4;
  702. __IO uint8_t wtcon : 4;
  703. } LINKINFO_b;
  704. };
  705. __IO uint8_t VPLEN;
  706. __IO uint8_t HS_EOF1;
  707. __IO uint8_t FS_EOF1;
  708. __IO uint8_t LS_EOF1;
  709. union
  710. {
  711. __IO uint8_t SOFT_RST;
  712. struct
  713. {
  714. __IO uint8_t nrst : 1;
  715. __IO uint8_t nrstx : 1;
  716. __IO uint8_t reserved2_7 : 6;
  717. } SOFT_RST_b;
  718. };
  719. struct
  720. {
  721. // TADDR Epn (n = 0 - 15). 0x80 - 0xFF.
  722. __IO uint8_t TXFUNCADDR;
  723. uint8_t UNUSED0;
  724. __IO uint8_t TXHUBADDR;
  725. __IO uint8_t TXHUBPORT;
  726. __IO uint8_t RXFUNCADDR;
  727. uint8_t UNUSED1;
  728. __IO uint8_t RXHUBADDR;
  729. __IO uint8_t RXHUBPORT;
  730. } AMCS[16];
  731. struct
  732. {
  733. // CSR EPn (n = 0 - 15). 0x100 - 0x1FF;
  734. union
  735. {
  736. __IO uint16_t TXMAXP;
  737. struct
  738. {
  739. __IO uint16_t max_payload_tran : 11; /* Bits 10:0 define(in bytes)the maximum payload transmitted in single transaction.
  740. * The value set can be up to 1024 bytes but is subject to the constraints place by
  741. * the USB Specification on packet sizes for Bulk,Interrupt and Isochronous transfers
  742. * in Full-speed and High-speed operations.
  743. */
  744. __IO uint16_t ex_max : 5;
  745. } TXMAXP_b;
  746. };
  747. union
  748. {
  749. __IO uint8_t CSR0L;
  750. __IO uint8_t TXCSRL;
  751. struct
  752. {
  753. __IO uint8_t rx_pkt_rdy : 1; /* This bit is set when a data packet has been received. An interrupt is
  754. * generated when this bit is set. The CPU clear this bit by setting the
  755. * ServicedRxPktRdy bit.
  756. */
  757. __IO uint8_t tx_pkt_rdy : 1; /* The CPU sets this bits after loading a data packet into the FIFO. It is clear
  758. * automatically when a data packet has been transmitted. An interrupt is also
  759. * generated at this point(if enabled).
  760. */
  761. __IO uint8_t sent_stall : 1; /* This bit is set when a STALL handshake is transmitted.
  762. * The CPU should clear this bit.
  763. */
  764. __IO uint8_t data_end : 1; /* The CPU sets this bit:
  765. * 1. When setting TxPktRdy for the last data packet.
  766. * 2. When clearing RxPktRdy after unloading the last data packet.
  767. * 3. When setting TxPktRdy for zero length data packet.
  768. * It is cleared automatically.
  769. */
  770. __IO uint8_t setup_end : 1; /* This bit will be set when a control transaction ends before the DataEnd
  771. * bit has been set. An interrupt will be generated and the FIFO flushed at
  772. * this time.The bit is cleared by the CPU writing a 1 to the ServicedSetupEnd bit.
  773. */
  774. __IO uint8_t send_stall : 1; /* The CPU write a 1 to this bit to terminate the current transaction.
  775. * The STALL handshake will be transmitted and then this bit will be
  776. * cleared automatically.
  777. */
  778. __IO uint8_t serviced_rxpktrdy : 1; /* The CPU write a 1 to this bit to clear the RxPktRdy bit.
  779. * It is Cleared automatically.
  780. */
  781. __IO uint8_t serviced_setupend : 1; /* The CPU write a 1 to this bit to clear the SetupEnd bit.
  782. * It is Cleared automatically.
  783. */
  784. } CSR0L_DEV_b;
  785. struct
  786. {
  787. __IO uint8_t rx_pkt_rdy : 1; /* This bit is set when a data packet has been received.An interrupt is generated
  788. * (If enabled)when this bit set.The CPU should clear this bit when the packet has
  789. * been read from the FIFO.
  790. */
  791. __IO uint8_t tx_pkt_rdy : 1; /* The CPU sets this bit after loading a data packet into the FIFO.It is cleared
  792. * automatically when a data packet has been transmitted. An interrupt is also
  793. * generated at this point(If enabled).
  794. */
  795. __IO uint8_t rx_stall : 1; /* This bit is set when a STALL handshake is received. The CPU should clear this bit. */
  796. __IO uint8_t setup_pkt : 1; /* The CPU sets this bit,at the TxPktRdy bit is set,to send a SETUP token instead
  797. * of an OUT token for the transaction. Note: Setting this bit also clear the Data
  798. * Toggle.
  799. */
  800. __IO uint8_t error : 1; /* This bit will be set when three attempts have been made to perform a transaction
  801. * with no response from the peripheral.The CPU should clear this bit.An interrupt
  802. * is generated when this bit is set.
  803. */
  804. __IO uint8_t req_pkt : 1; /* The CPU sets this bit to request an IN transaction.
  805. * It is cleared when RxPktRdy is set.
  806. */
  807. __IO uint8_t status_pkt : 1; /* The CPU sets this bit at the same time as the TxPktRdy or ReqPkt bit is set,
  808. * to perform a status stage transaction. Setting this bit ensures that the data
  809. * toggle is set to 1 so that a DATA1 packet is used for the Status Stage transaction.
  810. */
  811. __IO uint8_t nak_timeout : 1; /* This bit will be set when Endpoint 0 is halted following the receipt for longer
  812. * than the time set by the NAKLimit0 register. The CPU should clear this bit to allow
  813. * the endpoint to continue.
  814. */
  815. } CSR0L_HOST_b;
  816. struct
  817. {
  818. __IO uint8_t tx_pkt_rdy : 1; /* The CPU sets this bit after loading a data packet into the FIFO. */
  819. __IO uint8_t fifo_not_empty : 1; /* The USB sets this bit when there is at least 1 packet in the TX FIFO. */
  820. __IO uint8_t under_run : 1; /* The USB sets this bit if an IN token is received when TxPktRdy is not set. The CPU should clear this bit. */
  821. __IO uint8_t flush_fifo : 1; /* The CPU writes a 1 to this bit to flush the latest packet from the endpoint TX FIFO */
  822. __IO uint8_t send_stall : 1; /* The CPU writes a 1 to this bit to issue a STALL handshake to an IN token.The CPU
  823. * clears this bit to terminate the stall condition.
  824. * Note:This bit has no effect where the endpoint is being used for Isochronous transfers.
  825. */
  826. __IO uint8_t sent_stall : 1; /* This bit is set when a STALL handshake is transmitted.The FIFO is flushed and
  827. * the TxPktRdy bit is cleared(see below).The CPU should clear this bit.
  828. */
  829. __IO uint8_t clr_data_tog : 1; /* The CPU writes a 1 to this bit to reset the endpoint data toggle to 0. */
  830. __IO uint8_t incomp_tx : 1; /* When the endpoint is being used for high-bandwidth Isochronous,this bit is set to
  831. * indicate where a large packet has been split into 2 or 3 packets for transmission
  832. * but insufficient IN tokens have been received to send all the parts.
  833. * Note:In anything other than isochronous transfers,this bit will always return 0.
  834. */
  835. } TXCSRL_DEV_b;
  836. struct
  837. {
  838. __IO uint8_t tx_pkt_rdy : 1; /* The CPU sets this bit after loading a data packet into the FIFO. */
  839. __IO uint8_t fifo_not_empty : 1; /* The USB sets this bit when there is at least 1 packet in the TX FIFO. */
  840. __IO uint8_t error : 1;
  841. __IO uint8_t flush_fifo : 1; /* The CPU writes a 1 to this bit to flush the latest packet from the endpoint TX FIFO */
  842. __IO uint8_t setup_Pkt : 1;
  843. __IO uint8_t Rx_stall : 1;
  844. __IO uint8_t clr_data_tog : 1; /* The CPU writes a 1 to this bit to reset the endpoint data toggle to 0. */
  845. __IO uint8_t NAK_timeout_incompTx : 1;
  846. } TXCSRL_HOST_b;
  847. };
  848. union
  849. {
  850. __IO uint8_t CSR0H;
  851. __IO uint8_t TXCSRH;
  852. struct
  853. {
  854. __IO uint8_t flush_fifo : 1; /* The CPU writes a 1 to this bit to flush the next packet to be transmitted/read from
  855. * the Endpoint 0 FIFO. The FIFO pointer is reset and the TxPktRdy/RxPktRdy bit(below) is
  856. * cleared. Note:FlushFIFO should only be used when TxPktRdy/RxPktRdy is set.At other
  857. * times, it may cause data to be corrupted.
  858. */
  859. __IO uint8_t reserved1_7 : 7; /* Unused. Return 0 when resd. */
  860. } CSR0H_DEV_b;
  861. struct
  862. {
  863. __IO uint8_t flush_fifo : 1; /* The CPU writes a 1 to this bit to flush the next packet to be transmitted/read from
  864. * the Endpoint 0 FIFO. The FIFO pointer is reset and the TxPktRdy/RxPktRdy bit(below) is
  865. * cleared. Note:FlushFIFO should only be used when TxPktRdy/RxPktRdy is set.At other
  866. * times, it may cause data to be corrupted.
  867. */
  868. __IO uint8_t data_toggle : 1; /* When read,this bit indicates the current state of the Endpoint 0 data toggle. If D10
  869. * is high,this bit may be written with the written with the required setting of the data
  870. * toggle.If D10 is low,any value written to this bit is ignored.
  871. */
  872. __IO uint8_t data_toggle_wr_en : 1; /* The CPU written a 1 to this bit to enable the current state of Endpoint 0 data toggle
  873. * to be written(see Data Toggle bit,below).This bit is automatically cleared once the new
  874. * value is written.
  875. */
  876. __IO uint8_t dis_ping : 1; /* The CPU writes a 1 to this bit to instruct the core not to issue PING tokens in data
  877. * and status phases of a high-speed Control transfer(for use with devices that do not
  878. * respond to PING).
  879. */
  880. __IO uint8_t reserved1_7 : 4; /* Unused. Return 0 when resd. */
  881. } CSR0H_HOST_b;
  882. struct
  883. {
  884. __IO uint8_t reserved0_1 : 2;
  885. __IO uint8_t dma_req_mode : 1;
  886. __IO uint8_t frc_data_tog : 1;
  887. __IO uint8_t dma_req_enab : 1;
  888. __IO uint8_t mode : 1;
  889. __IO uint8_t iso : 1;
  890. __IO uint8_t auto_set : 1;
  891. } TXCSRH_DEV_b;
  892. struct
  893. {
  894. __IO uint8_t data_toggle : 1;
  895. __IO uint8_t data_toggle_wren : 1;
  896. __IO uint8_t dma_req_mode : 1;
  897. __IO uint8_t frc_data_tog : 1;
  898. __IO uint8_t dma_req_enab : 1;
  899. __IO uint8_t mode : 1;
  900. __IO uint8_t reserved6 : 1;
  901. __IO uint8_t auto_set : 1;
  902. } TXCSRH_HOST_b;
  903. };
  904. union
  905. {
  906. __IO uint16_t RXMAXP;
  907. struct
  908. {
  909. __IO uint16_t max_payload_tran : 11;
  910. __IO uint16_t ex_max : 5;
  911. } RXMAXP_b;
  912. };
  913. union
  914. {
  915. __IO uint8_t RXCSRL;
  916. struct
  917. {
  918. __IO uint8_t rx_pkt_rdy : 1; /* This bit is set when a data packet has been received */
  919. __IO uint8_t fifo_full : 1; /* */
  920. __IO uint8_t over_run : 1; /* */
  921. __IO uint8_t data_error : 1; /* */
  922. __IO uint8_t flush_fifo : 1;
  923. __IO uint8_t send_stall : 1; /* The CPU writes a 1 to this bit to issue a STALL handshake. */
  924. __IO uint8_t sent_stall : 1; /* This bit is set when a STALL handshake is transmitted. The CPU should clear this bit. */
  925. __IO uint8_t clr_data_tog : 1;
  926. } RXCSRL_DEV_b;
  927. struct
  928. {
  929. __IO uint8_t rx_pkt_rdy : 1; /* This bit is set when a data packet has been received */
  930. __IO uint8_t fifo_full : 1; /* */
  931. __IO uint8_t error : 1; /* */
  932. __IO uint8_t data_error : 1; /* */
  933. __IO uint8_t flush_fifo : 1;
  934. __IO uint8_t ReqPkt : 1;
  935. __IO uint8_t RxStall : 1;
  936. __IO uint8_t clr_data_tog : 1;
  937. } RXCSRL_HOST_b;
  938. };
  939. union
  940. {
  941. __IO uint8_t RXCSRH;
  942. struct
  943. {
  944. __IO uint8_t incomp_rx : 1;
  945. __IO uint8_t reserved1_2 : 2;
  946. __IO uint8_t dma_req_mode : 1;
  947. __IO uint8_t dis_nyet_pid_error : 1;
  948. __IO uint8_t dma_req_enab : 1;
  949. __IO uint8_t iso : 1;
  950. __IO uint8_t auto_clear : 1;
  951. } RXCSRH_DEV_b;
  952. struct
  953. {
  954. __IO uint8_t incomp_rx : 1;
  955. __IO uint8_t data_toggle : 1;
  956. __IO uint8_t data_toggle_wren : 1;
  957. __IO uint8_t dma_req_mode : 1;
  958. __IO uint8_t PID_error : 1;
  959. __IO uint8_t dma_req_enab : 1;
  960. __IO uint8_t auto_req : 1;
  961. __IO uint8_t auto_clear : 1;
  962. } RXCSRH_HOST_b;
  963. };
  964. union
  965. {
  966. __IO uint16_t RXCOUNT;
  967. struct
  968. {
  969. __IO uint16_t ep_rx_count : 14;
  970. __IO uint16_t reserved14_15 : 2;
  971. } RXCOUNT_b;
  972. };
  973. union
  974. {
  975. __IO uint8_t TYPE0; // Host Mode Only.
  976. __IO uint8_t TXTYPE; // Host Mode Only.
  977. struct
  978. {
  979. __IO uint8_t reserved0_5 : 6; /* Reserved */
  980. __IO uint8_t speed : 2; /* Operating speed of the target device :
  981. * 00:Unused(Note: If selected,the target will be using the same connectionn speed as the core.)
  982. * 01:High
  983. * 10:Full
  984. * 11:Low
  985. */
  986. } TYPE0_b;
  987. struct
  988. {
  989. __IO uint8_t target_EP_number : 4; /* Operating speed of the target device when the core is configured with the
  990. * multipoint option:
  991. * 00: Unused 01: High
  992. * 10: Full 11: Low
  993. * When the core is not configured with the multipoint option these bits should
  994. * not be accessed.
  995. */
  996. __IO uint8_t protocol : 2; /* The CPU should set this to select the required protocol for TX endpoint:
  997. * 00: Control 01: Isochronous
  998. * 10: Bulk 11: Interrupt
  999. */
  1000. __IO uint8_t speed : 2; /* Operating speed of the target device when the core is configured with
  1001. * the multipoint option:
  1002. * 00:Unused 01: High 10: Full 11:Low
  1003. * when the core is not configured with the multipoint option these bits
  1004. * should not be accessed.
  1005. */
  1006. } TXTYPE_b;
  1007. };
  1008. union
  1009. {
  1010. __IO uint8_t NAKLIMIT0; // Host Mode Only.
  1011. __IO uint8_t TXINTERVAL; // Host Mode Only.
  1012. struct
  1013. {
  1014. __IO uint8_t Endpoint0_NAK_Limit : 5; /* Endpoint 0 NAK limit (m) */
  1015. __IO uint8_t reserved5_7 : 3; /* Reserve */
  1016. } NAKLIMIT0_b;
  1017. struct
  1018. {
  1019. __IO uint8_t Tx_polling_interval :8;
  1020. } TXINTERVAL_b;
  1021. };
  1022. union
  1023. {
  1024. __IO uint8_t RXTYPE; // Host Mode Only.
  1025. struct
  1026. {
  1027. __IO uint8_t target_EP_number : 4; /* Operating speed of the target device when the core is configured with the
  1028. * multipoint option:
  1029. * 00: Unused 01: High
  1030. * 10: Full 11: Low
  1031. * When the core is not configured with the multipoint option these bits should
  1032. * not be accessed.
  1033. */
  1034. __IO uint8_t protocol : 2; /* The CPU should set this to select the required protocol for TX endpoint:
  1035. * 00: Control 01: Isochronous
  1036. * 10: Bulk 11: Interrupt
  1037. */
  1038. __IO uint8_t speed : 2; /* Operating speed of the target device when the core is configured with
  1039. * the multipoint option:
  1040. * 00:Unused 01: High 10: Full 11:Low
  1041. * when the core is not configured with the multipoint option these bits
  1042. * should not be accessed.
  1043. */
  1044. } RXTYPE_b;
  1045. };
  1046. union
  1047. {
  1048. __IO uint8_t RXINTERVAL; // Host Mode Only.
  1049. struct
  1050. {
  1051. __IO uint8_t Tx_polling_interval :8;
  1052. } RXINTERVAL_b;
  1053. };
  1054. uint8_t UNUSED0;
  1055. union
  1056. {
  1057. __IO uint8_t CONFIGDATA;
  1058. __IO uint8_t FIFOSIZE;
  1059. struct
  1060. {
  1061. __IO uint8_t utmi_data_width : 1; /* Indicates selected UTMI+ data width. Always 0 indicating 8 bits. */
  1062. __IO uint8_t soft_cone : 1; /* Always "1" . Indicates Soft Connect/Disconnect. */
  1063. __IO uint8_t dyn_fifo_sizing : 1; /* When set to "1" indicates Dynamic FIFO Sizing option selected. */
  1064. __IO uint8_t hb_txe : 1; /* When set to "1" indicates High-bandwidth TX ISO Endpoint Support selected */
  1065. __IO uint8_t hb_rxe : 1; /* When set to "1" indicates High-bandwidth Rx ISO Endpoint Support selected. */
  1066. __IO uint8_t big_endian : 1; /* Always "0". Indicates Little Endian ordering. */
  1067. __IO uint8_t mp_txe : 1; /* When set to "1" automatic splitting of bulk packets is selected */
  1068. __IO uint8_t mp_rxe : 1; /* When set to "1" automatic amalgamation of bulk packets is selected */
  1069. } CONFIGDATA_b;
  1070. struct
  1071. {
  1072. __IO uint8_t tx_fifo_size : 4;
  1073. __IO uint8_t rx_fifo_size : 4;
  1074. } b;
  1075. };
  1076. } CSR[16];
  1077. // Optional DMA Registers. 0x200 - 0x2FF.
  1078. __IO uint32_t DMA_INTR; /* Only one DMA INTR register */
  1079. __IO uint32_t DMA_CNTL;
  1080. __IO uint32_t DMA_ADDR;
  1081. __IO uint32_t DMA_COUNT;
  1082. uint32_t UNUSED2[60];
  1083. // Extended Registers. 0x300 - 0x35F.
  1084. __IO uint16_t RQPKTCOUNT[16]; // Host Mode Only.
  1085. uint16_t UNUSEDRQPK[16];
  1086. union
  1087. {
  1088. __IO uint16_t RXDPKTBUFDIS; // Rx DPktBufDis.
  1089. struct
  1090. {
  1091. __IO uint16_t reserved0 : 1;
  1092. __IO uint16_t ep1_rx_dis : 1;
  1093. __IO uint16_t ep2_rx_dis : 1;
  1094. __IO uint16_t ep3_rx_dis : 1;
  1095. __IO uint16_t ep4_rx_dis : 1;
  1096. __IO uint16_t ep5_rx_dis : 1;
  1097. __IO uint16_t ep6_rx_dis : 1;
  1098. __IO uint16_t ep7_rx_dis : 1;
  1099. __IO uint16_t ep8_rx_dis : 1;
  1100. __IO uint16_t ep9_rx_dis : 1;
  1101. __IO uint16_t ep10_rx_dis : 1;
  1102. __IO uint16_t ep11_rx_dis : 1;
  1103. __IO uint16_t ep12_rx_dis : 1;
  1104. __IO uint16_t ep13_rx_dis : 1;
  1105. __IO uint16_t ep14_rx_dis : 1;
  1106. __IO uint16_t ep15_rx_dis : 1;
  1107. } RXDPKTBUFDIS_b;
  1108. };
  1109. union
  1110. {
  1111. __IO uint16_t TXDPKTBUFDIS; // Tx DPktBufDis.
  1112. struct
  1113. {
  1114. __IO uint16_t reserved0 : 1;
  1115. __IO uint16_t ep1_tx_dis : 1;
  1116. __IO uint16_t ep2_tx_dis : 1;
  1117. __IO uint16_t ep3_tx_dis : 1;
  1118. __IO uint16_t ep4_tx_dis : 1;
  1119. __IO uint16_t ep5_tx_dis : 1;
  1120. __IO uint16_t ep6_tx_dis : 1;
  1121. __IO uint16_t ep7_tx_dis : 1;
  1122. __IO uint16_t ep8_tx_dis : 1;
  1123. __IO uint16_t ep9_tx_dis : 1;
  1124. __IO uint16_t ep10_tx_dis : 1;
  1125. __IO uint16_t ep11_tx_dis : 1;
  1126. __IO uint16_t ep12_tx_dis : 1;
  1127. __IO uint16_t ep13_tx_dis : 1;
  1128. __IO uint16_t ep14_tx_dis : 1;
  1129. __IO uint16_t ep15_tx_dis : 1;
  1130. } TXDPKTBUFDIS_b;
  1131. };
  1132. __IO uint16_t C_T_UCH;
  1133. __IO uint16_t C_T_HSRTN;
  1134. __IO uint16_t C_T_HSBT;
  1135. uint16_t UNUSED3[11];
  1136. // LPM Registers. 0x360 - 0x365.
  1137. union
  1138. {
  1139. __IO uint16_t LPM_ATTR;
  1140. struct
  1141. {
  1142. __IO uint16_t link_state : 4; /* This value is provoided by the host to the peripheral to indicate what state the peripheral
  1143. * must transition to after the receipt and acceptance of a LPM transaction.
  1144. * LinkState = 4'h0 - Reserved
  1145. * LinkState = 4'h1 - Slep State(L1)
  1146. * LinkState = 4'h2 - Reserved
  1147. * LinkState = 4'h3 - Reserved
  1148. */
  1149. __IO uint16_t HIRD : 4; /* This is the Host Initiated Resume Duration.This value is the minimum time the host will
  1150. * drive resume on the Bus. The value in this register corresponds to an actual resume time
  1151. * of:
  1152. * Resume Time = 50us + HIRD * 75us.This results a range 50us to 1200us.
  1153. */
  1154. __IO uint16_t RmtWak : 1; /* This bit is the remote wakeup enable bit:
  1155. * RmtWak = 1'b0:Remote wakeup is not enabled.
  1156. * RmtWak = 1'b1:Remote wakeup is enabled.
  1157. * This bit is applied on a temporary basis only and is only applied to the current suspend
  1158. * state.After the current suspend cycle,the remote wakeup capability that was negotiated
  1159. * upon enumeration will apply.
  1160. */
  1161. __IO uint16_t reserved9_11 : 3; /* Reserved;Always returns 0 on read */
  1162. __IO uint16_t EndPoint : 4; /* This is the EndPnt that in the Token Packet of the LPM transaction. -*/
  1163. } LPM_ATTR_b;
  1164. };
  1165. union
  1166. {
  1167. __IO uint8_t LPM_CNTRL;
  1168. struct
  1169. {
  1170. __IO uint8_t lpmxmt : 1;
  1171. __IO uint8_t lpmres : 1;
  1172. __IO uint8_t lpmen : 2;
  1173. __IO uint8_t lpmnak : 1;
  1174. __IO uint8_t reserved5_7 : 3;
  1175. } LPM_CNTRL_DEV_b;
  1176. struct
  1177. {
  1178. __IO uint8_t lpmxmt : 1;
  1179. __IO uint8_t lpmres : 1;
  1180. __IO uint8_t reserved2_7 : 6;
  1181. } LPM_CNTRL_HOST_b;
  1182. };
  1183. union
  1184. {
  1185. __IO uint8_t LPM_INTREN;
  1186. struct
  1187. {
  1188. __IO uint8_t lpmxmt : 1;
  1189. __IO uint8_t lpmres : 1;
  1190. __IO uint8_t reserved2_7 : 6;
  1191. } LPM_INTREN_b;
  1192. };
  1193. union
  1194. {
  1195. __IO uint8_t LPM_INTR;
  1196. struct
  1197. {
  1198. __IO uint8_t lpmst : 1;
  1199. __IO uint8_t lpmny : 1;
  1200. __IO uint8_t lpmack : 1;
  1201. __IO uint8_t lpmnc : 1;
  1202. __IO uint8_t lpmres : 1;
  1203. __IO uint8_t lpmerr : 1;
  1204. __IO uint8_t reserved6_7 : 2;
  1205. } LPM_INTR_DEV_b;
  1206. struct
  1207. {
  1208. __IO uint8_t lpmst : 1;
  1209. __IO uint8_t lpmny : 1;
  1210. __IO uint8_t lpmack : 1;
  1211. __IO uint8_t lpmnc : 1;
  1212. __IO uint8_t lpmres : 1;
  1213. __IO uint8_t reserved5_7 : 3;
  1214. } LPM_INTR_HOST_b;
  1215. };
  1216. union
  1217. {
  1218. __IO uint8_t LPM_FADDR; // Relavant in Host mode only.
  1219. struct
  1220. {
  1221. __IO uint8_t function_address : 7;
  1222. __IO uint8_t reserved7 : 1;
  1223. } LPM_FADDR_b;
  1224. };
  1225. }USB_TypeDef;
  1226. #define USB_OTG ( (USB_TypeDef *) USB_BASE )
  1227. typedef struct
  1228. {
  1229. const USB_TypeDef *RegBase;
  1230. const int IrqLine;
  1231. USB_EndpointCtrlStruct EpCtrl[USB_EP_MAX];
  1232. uint32_t RxData[USB_FIFO_DW_DAX];
  1233. PV_Union pRxData;
  1234. uint8_t IsConnect;
  1235. uint8_t IsHost;
  1236. uint8_t NewDeviceAddress;
  1237. }USB_HWCtrlStruct;
  1238. static USB_HWCtrlStruct prvUSB =
  1239. {
  1240. .RegBase = USB_BASE,
  1241. .IrqLine = USB_IRQn,
  1242. };
  1243. static uint8_t USB_OTG_FifosizeReg(uint16_t fifosiz)
  1244. {
  1245. uint8_t register_value = 0;
  1246. switch (fifosiz)
  1247. {
  1248. case 8:
  1249. register_value = 0;
  1250. break;
  1251. case 16:
  1252. register_value = 1;
  1253. break;
  1254. case 32:
  1255. register_value = 2;
  1256. break;
  1257. case 64:
  1258. register_value = 3;
  1259. break;
  1260. case 128:
  1261. register_value = 4;
  1262. break;
  1263. }
  1264. return register_value;
  1265. }
  1266. static void prvUSB_IrqHandle(int32_t IrqLine, void *pData)
  1267. {
  1268. uint32_t USB_ID = (uint32_t)pData;
  1269. uint32_t i;
  1270. uint32_t Count32b;
  1271. uint16_t RxLen;
  1272. uint16_t TxBit = USB_OTG->INTRTX;
  1273. uint16_t RxBit = USB_OTG->INTRRX;
  1274. uint8_t StateBit = USB_OTG->INTRUSB;
  1275. uint8_t EpIndex = 1;
  1276. USB_OTG->INTRUSB = 0;
  1277. USB_OTG->INTRRX = 0;
  1278. USB_OTG->INTRTX = 0;
  1279. if (prvUSB.IsHost || USB_OTG->DEVCTL_b.host_mode)
  1280. {
  1281. ;
  1282. }
  1283. else
  1284. {
  1285. if (StateBit & 0x1)
  1286. {
  1287. USB_StackDeviceBusChange(USB_ID, USBD_BUS_TYPE_SUSPEND);
  1288. }
  1289. if (StateBit & 0x2)
  1290. {
  1291. USB_StackDeviceBusChange(USB_ID, USBD_BUS_TYPE_RESUME);
  1292. USB_OTG->POWER_b.resume = 1;
  1293. }
  1294. if (StateBit & 0x4)
  1295. {
  1296. USB_OTG->FADDR = 0;
  1297. USB_OTG->INDEX = 0;
  1298. USB_FlushFifo(&prvUSB, 0, 0);
  1299. for(i = 1; i < 8; i++)
  1300. {
  1301. USB_FlushFifo(&prvUSB,i, 0);
  1302. USB_FlushFifo(&prvUSB,i, 1);
  1303. }
  1304. USB_SetDeviceEPStatus(&prvUSB, 0, 0, USB_EP_STATE_ACK);
  1305. USB_StackDeviceBusChange(USB_ID, USBD_BUS_TYPE_RESET);
  1306. }
  1307. if (StateBit & 0x8)
  1308. {
  1309. USB_StackDeviceBusChange(USB_ID, USBD_BUS_TYPE_NEW_SOF);
  1310. }
  1311. if (StateBit & 0x20)
  1312. {
  1313. for(i = 0; i < 8; i++)
  1314. {
  1315. memset(&prvUSB.EpCtrl[i].TxBuf, 0, sizeof(Buffer_Struct));
  1316. }
  1317. USB_StackDeviceBusChange(USB_ID, USBD_BUS_TYPE_DISCONNECT);
  1318. }
  1319. if (TxBit & 0x01)
  1320. {
  1321. if (USB_OTG->CSR0L_DEV_b.sent_stall)
  1322. {
  1323. USB_SetDeviceEPStatus(&prvUSB, 0, 0, USB_EP_STATE_ACK);
  1324. }
  1325. if (USB_OTG->CSR0L_DEV_b.setup_end)
  1326. {
  1327. USB_OTG->CSR0L_DEV_b.serviced_setupend = 1;
  1328. }
  1329. if (prvUSB.NewDeviceAddress & 0x80)
  1330. {
  1331. USB_OTG->FADDR = prvUSB.NewDeviceAddress & 0x7f;
  1332. prvUSB.NewDeviceAddress = 0;
  1333. }
  1334. if (USB_OTG->CSR0L_DEV_b.rx_pkt_rdy)
  1335. {
  1336. RxLen = USB_OTG->COUNT0_b.ep0_rx_count;
  1337. if (RxLen)
  1338. {
  1339. Count32b = RxLen >> 2;
  1340. for (i = 0; i < Count32b; i++)
  1341. {
  1342. prvUSB.RxData[i] = USB_OTG->FIFOX[0].dword;
  1343. }
  1344. for (i = Count32b * 4; i < RxLen; i++)
  1345. {
  1346. prvUSB.pRxData.pu8[i] = USB_OTG->FIFOX[0].byte;
  1347. }
  1348. USB_StackPutRxData(USB_ID, 0, prvUSB.pRxData.pu8, RxLen);
  1349. }
  1350. USB_OTG->CSR0L_DEV_b.serviced_rxpktrdy = 1;
  1351. USB_StackAnalyzeDeviceEpRx(USB_ID, 0);
  1352. }
  1353. else if (!USB_OTG->CSR0L_DEV_b.tx_pkt_rdy)
  1354. {
  1355. USB_DeviceXfer(&prvUSB, 0);
  1356. }
  1357. }
  1358. else if (TxBit)
  1359. {
  1360. TxBit >>= 1;
  1361. EpIndex = 1;
  1362. while(TxBit)
  1363. {
  1364. if (TxBit & 0x01)
  1365. {
  1366. if (!USB_OTG->CSR[EpIndex].TXCSRL_DEV_b.tx_pkt_rdy)
  1367. {
  1368. USB_DeviceXfer(&prvUSB, EpIndex);
  1369. }
  1370. USB_OTG->CSR[EpIndex].TXCSRL_DEV_b.sent_stall = prvUSB.EpCtrl[EpIndex].INSTATUS_b.Halt?1:0;
  1371. USB_OTG->CSR[EpIndex].TXCSRL_DEV_b.send_stall = 0;
  1372. USB_OTG->CSR[EpIndex].TXCSRL_DEV_b.under_run = 0;
  1373. }
  1374. TxBit >>= 1;
  1375. EpIndex++;
  1376. }
  1377. }
  1378. if (RxBit)
  1379. {
  1380. EpIndex = 1;
  1381. RxBit >>= 1;
  1382. while(RxBit)
  1383. {
  1384. if (RxBit & 0x01)
  1385. {
  1386. if (USB_OTG->CSR[EpIndex].RXCSRL_DEV_b.rx_pkt_rdy)
  1387. {
  1388. RxLen = USB_OTG->CSR[EpIndex].RXCOUNT_b.ep_rx_count;
  1389. if (RxLen)
  1390. {
  1391. Count32b = RxLen >> 2;
  1392. for (i = 0; i < Count32b; i++)
  1393. {
  1394. prvUSB.RxData[i] = USB_OTG->FIFOX[EpIndex].dword;
  1395. }
  1396. for (i = Count32b * 4; i < RxLen; i++)
  1397. {
  1398. prvUSB.pRxData.pu8[i] = USB_OTG->FIFOX[EpIndex].byte;
  1399. }
  1400. USB_StackPutRxData(USB_ID, EpIndex, prvUSB.pRxData.pu8, RxLen);
  1401. }
  1402. }
  1403. USB_OTG->CSR[EpIndex].RXCSRL = 0;
  1404. USB_OTG->CSR[EpIndex].RXCSRL_DEV_b.sent_stall = prvUSB.EpCtrl[EpIndex].OUTSTATUS_b.Halt?1:0;
  1405. USB_StackAnalyzeDeviceEpRx(USB_ID, EpIndex);
  1406. }
  1407. RxBit >>= 1;
  1408. EpIndex++;
  1409. }
  1410. }
  1411. }
  1412. }
  1413. static int prvUSB_SetupEPFifo(HANDLE hUSB)
  1414. {
  1415. USB_HWCtrlStruct *hwUSB = (USB_HWCtrlStruct *)hUSB;
  1416. uint8_t i;
  1417. uint8_t Len;
  1418. uint16_t FifoStart = (64 >> 3);
  1419. uint16_t UseFifo = 64;
  1420. //ep0使用默认的64byte
  1421. USB_OTG->INDEX = 0;
  1422. USB_OTG->TXFIFOSZ = 3;
  1423. USB_OTG->RXFIFOSZ = 3;
  1424. USB_OTG->TXFIFOADD = 0;
  1425. USB_OTG->RXFIFOADD = 0;
  1426. USB_OTG->CSR0H_DEV_b.flush_fifo = 1; //刷新EP0的FIFO
  1427. hwUSB->EpCtrl[0].MaxPacketLen = 64;
  1428. for(i = 1; i < USB_EP_MAX; i++)
  1429. {
  1430. USB_OTG->INDEX = i;
  1431. if (hwUSB->EpCtrl[i].ToDeviceEnable)
  1432. {
  1433. Len = USB_OTG_FifosizeReg(hwUSB->EpCtrl[i].MaxPacketLen);
  1434. USB_OTG->CSR[i].RXMAXP = hwUSB->EpCtrl[i].MaxPacketLen;
  1435. USB_OTG->RXFIFOADD = FifoStart;
  1436. USB_OTG->RXFIFOSZ = Len;
  1437. USB_OTG->CSR[i].RXCSRL = (1 << 4);
  1438. //DBG("%d,%d,%x,%d,%x", i, USB_OTG->CSR[i].RXMAXP, USB_OTG->RXFIFOADD, USB_OTG->RXFIFOSZ, USB_OTG->CSR[i].RXCSRL);
  1439. FifoStart += (hwUSB->EpCtrl[i].MaxPacketLen >> 3);
  1440. UseFifo += hwUSB->EpCtrl[i].MaxPacketLen;
  1441. }
  1442. if (UseFifo > 512)
  1443. {
  1444. return -1;
  1445. }
  1446. if (hwUSB->EpCtrl[i].ToHostEnable)
  1447. {
  1448. USB_OTG->CSR[i].TXMAXP = hwUSB->EpCtrl[i].MaxPacketLen;
  1449. USB_OTG->TXFIFOADD = FifoStart;
  1450. USB_OTG->TXFIFOSZ = Len;
  1451. USB_OTG->CSR[i].TXCSRL = (1 << 3);
  1452. //DBG("%d,%d,%x,%d,%x", i, USB_OTG->CSR[i].TXMAXP, USB_OTG->TXFIFOADD, USB_OTG->TXFIFOSZ, USB_OTG->CSR[i].TXCSRL);
  1453. FifoStart += (hwUSB->EpCtrl[i].MaxPacketLen >> 3);
  1454. UseFifo += hwUSB->EpCtrl[i].MaxPacketLen;
  1455. }
  1456. if (UseFifo > 512)
  1457. {
  1458. return -1;
  1459. }
  1460. }
  1461. USB_OTG->INDEX = 0;
  1462. return 0;
  1463. }
  1464. void USB_GlobalInit(void)
  1465. {
  1466. USB_HWCapsStruct Caps;
  1467. ISR_SetHandler(prvUSB.IrqLine, prvUSB_IrqHandle, USB_ID0);
  1468. #ifdef __BUILD_OS__
  1469. ISR_SetPriority(prvUSB.IrqLine, configLIBRARY_LOWEST_INTERRUPT_PRIORITY);
  1470. #else
  1471. ISR_SetPriority(prvUSB.IrqLine, 7);
  1472. #endif
  1473. prvUSB.IsConnect = 0;
  1474. Caps.EpBufMaxLen = 4096;
  1475. Caps.Feature = 0;
  1476. Caps.FEATURE_b.FullSpeed = 1;
  1477. Caps.CtrlMode = USB_MODE_DUAL;
  1478. USB_StackSetControl(USB_ID0, &prvUSB, prvUSB.EpCtrl, &Caps);
  1479. USB_StackSetDeviceSpeed(USB_ID0, USB_DEVICE_SPEED_FULL_SPEED);
  1480. USB_OTG->INTRRXE = 0;
  1481. USB_OTG->INTRTXE = 0;
  1482. USB_OTG->INTRUSBE = 0;
  1483. prvUSB.pRxData.pu32 = prvUSB.RxData;
  1484. SYSCTRL->CG_CTRL2 &= ~SYSCTRL_AHBPeriph_USB;
  1485. }
  1486. void USB_PowerOnOff(HANDLE hUSB, uint8_t OnOff)
  1487. {
  1488. if (OnOff)
  1489. {
  1490. SYSCTRL->CG_CTRL2 |= SYSCTRL_AHBPeriph_USB;
  1491. }
  1492. else
  1493. {
  1494. SYSCTRL->CG_CTRL2 &= ~SYSCTRL_AHBPeriph_USB;
  1495. }
  1496. }
  1497. void USB_Stop(HANDLE hUSB)
  1498. {
  1499. uint8_t dummy;
  1500. USB_HWCtrlStruct *hwUSB = (USB_HWCtrlStruct *)hUSB;
  1501. ISR_OnOff(hwUSB->IrqLine, 0);
  1502. USB_OTG->POWER = 0;
  1503. USB_OTG->INTRRXE = 0;
  1504. USB_OTG->INTRTXE = 0;
  1505. USB_OTG->INTRUSBE = 0;
  1506. USB_OTG->INTRTX = 0;
  1507. USB_OTG->INTRRX = 0;
  1508. USB_OTG->INTRUSB = 0;
  1509. dummy = USB_OTG->INTRUSB;
  1510. }
  1511. void USB_ResetStart(HANDLE hUSB)
  1512. {
  1513. SYSCTRL->LOCK_R &= ~SYSCTRL_USB_RESET;
  1514. SYSCTRL->SOFT_RST2 |= SYSCTRL_USB_RESET;
  1515. SYSCTRL->LOCK_R |= SYSCTRL_USB_RESET;
  1516. }
  1517. void USB_ResetEnd(HANDLE hUSB)
  1518. {
  1519. SYSCTRL->LOCK_R &= ~SYSCTRL_USB_RESET;
  1520. SYSCTRL->SOFT_RST2 &= ~SYSCTRL_USB_RESET;
  1521. SYSCTRL->LOCK_R |= SYSCTRL_USB_RESET;
  1522. }
  1523. void USB_SetWorkMode(HANDLE hUSB, uint8_t Mode)
  1524. {
  1525. USB_HWCtrlStruct *hwUSB = (USB_HWCtrlStruct *)hUSB;
  1526. USBPHY_CR1_TypeDef CR1;
  1527. USBPHY_CR3_TypeDef CR3;
  1528. CR1.d32 = SYSCTRL->USBPHY_CR1;
  1529. CR1.b.commononn = 0;
  1530. CR1.b.stop_ck_for_suspend = 0;
  1531. SYSCTRL->USBPHY_CR1 = CR1.d32;
  1532. switch (Mode)
  1533. {
  1534. case USB_MODE_HOST:
  1535. USB_OTG->POWER = 1;
  1536. USB_OTG->DEVCTL |= (1 << 0)|(1 << 2);
  1537. CR3.d32 = SYSCTRL->USBPHY_CR3;
  1538. CR3.b.idpullup = 1;
  1539. SYSCTRL->USBPHY_CR3 = CR3.d32;
  1540. hwUSB->IsHost = 1;
  1541. break;
  1542. default:
  1543. USB_OTG->DEVCTL &= ~((1 << 0)|(1 << 2));
  1544. USB_OTG->POWER = 1|(1 << 6);
  1545. CR3.d32 = SYSCTRL->USBPHY_CR3;
  1546. CR3.b.idpullup = 0;
  1547. SYSCTRL->USBPHY_CR3 = CR3.d32;
  1548. hwUSB->IsHost = 0;
  1549. break;
  1550. }
  1551. }
  1552. void USB_SetDeviceAddress(HANDLE hUSB, uint8_t Address)
  1553. {
  1554. prvUSB.NewDeviceAddress = 0x80 | Address;
  1555. }
  1556. int USB_ReInitEpCfg(HANDLE hUSB)
  1557. {
  1558. return -1;
  1559. }
  1560. int USB_InitEpCfg(HANDLE hUSB)
  1561. {
  1562. return prvUSB_SetupEPFifo(hUSB);
  1563. }
  1564. void USB_Start(HANDLE hUSB)
  1565. {
  1566. USB_HWCtrlStruct *hwUSB = (USB_HWCtrlStruct *)hUSB;
  1567. USB_OTG->INTRUSBE = 0xff;
  1568. USB_OTG->INTRUSBE_b.en_sof = 0;
  1569. USB_OTG->INTRTXE = 0xff;
  1570. USB_OTG->INTRRXE = 0xfe;
  1571. ISR_OnOff(hwUSB->IrqLine, 1);
  1572. }
  1573. void USB_SendZeroPacket(HANDLE hUSB, uint8_t EpIndex)
  1574. {
  1575. if (EpIndex)
  1576. {
  1577. USB_OTG->CSR[EpIndex].TXCSRL_DEV_b.tx_pkt_rdy = 1;
  1578. }
  1579. else
  1580. {
  1581. USB_OTG->CSR0L_DEV_b.tx_pkt_rdy = 1;
  1582. }
  1583. }
  1584. void USB_DeviceXferStop(HANDLE hUSB, uint8_t EpIndex)
  1585. {
  1586. USB_HWCtrlStruct *hwUSB = (USB_HWCtrlStruct *)hUSB;
  1587. USB_OTG->INTRTXE &= ~(1 << EpIndex);
  1588. USB_FlushFifo(hUSB, EpIndex, 0);
  1589. USB_OTG->CSR[EpIndex].TXCSRL_DEV_b.send_stall = 0;
  1590. USB_OTG->CSR[EpIndex].TXCSRL_DEV_b.sent_stall = 0;
  1591. USB_OTG->CSR[EpIndex].TXCSRL_DEV_b.under_run = 0;
  1592. USB_OTG->INTRTXE |= (1 << EpIndex);
  1593. }
  1594. void USB_EpIntOnOff(HANDLE hUSB, uint8_t EpIndex, uint8_t IsToDevice, uint8_t OnOff)
  1595. {
  1596. USB_HWCtrlStruct *hwUSB = (USB_HWCtrlStruct *)hUSB;
  1597. if (IsToDevice)
  1598. {
  1599. if (OnOff)
  1600. {
  1601. USB_OTG->INTRRXE |= (1 << EpIndex);
  1602. }
  1603. else
  1604. {
  1605. USB_OTG->INTRRXE &= ~(1 << EpIndex);
  1606. }
  1607. }
  1608. else
  1609. {
  1610. if (OnOff)
  1611. {
  1612. USB_OTG->INTRTXE |= (1 << EpIndex);
  1613. }
  1614. else
  1615. {
  1616. USB_OTG->INTRTXE &= ~(1 << EpIndex);
  1617. }
  1618. }
  1619. }
  1620. void USB_DeviceXfer(HANDLE hUSB, uint8_t EpIndex)
  1621. {
  1622. USB_HWCtrlStruct *hwUSB = (USB_HWCtrlStruct *)hUSB;
  1623. USB_EndpointCtrlStruct *pEpCtrl = &hwUSB->EpCtrl[EpIndex];
  1624. USB_EndpointDataStruct EpData;
  1625. uint16_t TxLen, i;
  1626. EpData.USB_ID = USB_ID0;
  1627. EpData.EpIndex = EpIndex;
  1628. EpData.IsToDevice = 0;
  1629. if (pEpCtrl->TxBuf.Data)
  1630. {
  1631. if (pEpCtrl->TxBuf.Pos >= pEpCtrl->TxBuf.MaxLen)
  1632. {
  1633. if (pEpCtrl->NeedZeroPacket)
  1634. {
  1635. // DBG("Ep%d %d %d %d need send 0 packet", EpIndex,
  1636. // pEpCtrl->ForceZeroPacket, pEpCtrl->TxBuf.Pos, pEpCtrl->XferMaxLen);
  1637. if (!pEpCtrl->TxZeroPacket)
  1638. {
  1639. pEpCtrl->TxZeroPacket = 1;
  1640. if (EpIndex)
  1641. {
  1642. USB_OTG->CSR[EpIndex].TXCSRL_DEV_b.tx_pkt_rdy = 1;
  1643. }
  1644. else
  1645. {
  1646. USB_OTG->CSR0L_DEV_b.tx_pkt_rdy = 1;
  1647. }
  1648. }
  1649. else
  1650. {
  1651. DBG("!");
  1652. }
  1653. }
  1654. goto XFER_DONE;
  1655. }
  1656. else
  1657. {
  1658. pEpCtrl->TxZeroPacket = 0;
  1659. if ((pEpCtrl->TxBuf.MaxLen - pEpCtrl->TxBuf.Pos) > pEpCtrl->MaxPacketLen)
  1660. {
  1661. TxLen = pEpCtrl->MaxPacketLen;
  1662. }
  1663. else
  1664. {
  1665. if (pEpCtrl->ForceZeroPacket)
  1666. {
  1667. pEpCtrl->NeedZeroPacket = 1;
  1668. }
  1669. if ((pEpCtrl->TxBuf.MaxLen - pEpCtrl->TxBuf.Pos) == pEpCtrl->MaxPacketLen)
  1670. {
  1671. if ( pEpCtrl->TxBuf.MaxLen < pEpCtrl->XferMaxLen )
  1672. {
  1673. pEpCtrl->NeedZeroPacket = 1;
  1674. }
  1675. }
  1676. TxLen = pEpCtrl->TxBuf.MaxLen - pEpCtrl->TxBuf.Pos;
  1677. }
  1678. for(i = pEpCtrl->TxBuf.Pos; i < pEpCtrl->TxBuf.Pos + TxLen; i++)
  1679. {
  1680. USB_OTG->FIFOX[EpIndex].byte = pEpCtrl->TxBuf.Data[i];
  1681. }
  1682. pEpCtrl->TxBuf.Pos += TxLen;
  1683. if (EpIndex)
  1684. {
  1685. USB_OTG->CSR[EpIndex].TXCSRL_DEV_b.tx_pkt_rdy = 1;
  1686. }
  1687. else
  1688. {
  1689. USB_OTG->CSR0L_DEV_b.tx_pkt_rdy = 1;
  1690. }
  1691. }
  1692. }
  1693. return;
  1694. XFER_DONE:
  1695. memset(&pEpCtrl->TxBuf, 0, sizeof(Buffer_Struct));
  1696. if (EpIndex)
  1697. {
  1698. pEpCtrl->CB(&EpData, pEpCtrl->pData);
  1699. }
  1700. else
  1701. {
  1702. USB_StackDeviceEp0TxDone(USB_ID0);
  1703. }
  1704. }
  1705. void USB_SetDeviceNoDataSetup(HANDLE hUSB)
  1706. {
  1707. {
  1708. USB_OTG->CSR0L_DEV_b.data_end = 1;
  1709. }
  1710. }
  1711. void USB_FlushFifo(HANDLE hUSB, uint8_t EpIndex, uint8_t IsToDevice)
  1712. {
  1713. if (EpIndex)
  1714. {
  1715. if (IsToDevice)
  1716. {
  1717. USB_OTG->CSR[EpIndex].RXCSRL = (1 << 4);
  1718. }
  1719. else
  1720. {
  1721. USB_OTG->CSR[EpIndex].TXCSRL = (1 << 3);
  1722. }
  1723. }
  1724. else
  1725. {
  1726. if (USB_OTG->CSR0L_DEV_b.rx_pkt_rdy || USB_OTG->CSR0L_DEV_b.tx_pkt_rdy)
  1727. {
  1728. USB_OTG->CSR0H_DEV_b.flush_fifo = 1;
  1729. USB_OTG->CSR0L_DEV_b.serviced_rxpktrdy = 1;
  1730. USB_OTG->CSR0L_DEV_b.tx_pkt_rdy = 0;
  1731. }
  1732. }
  1733. }
  1734. /**
  1735. * @brief returns the EP Status
  1736. * @param pdev : Selected device
  1737. * ep : endpoint structure
  1738. * @retval : EP status
  1739. */
  1740. uint8_t USB_GetDeviceEPStatus(HANDLE hUSB, uint8_t Index, uint8_t IsToDevice)
  1741. {
  1742. if (Index)
  1743. {
  1744. if (IsToDevice)
  1745. {
  1746. if (USB_OTG->CSR[Index].RXCSRL_DEV_b.sent_stall)
  1747. {
  1748. return USB_EP_STATE_STALL;
  1749. }
  1750. if (USB_OTG->CSR[Index].RXCSRL_DEV_b.data_error || USB_OTG->CSR[Index].RXCSRL_DEV_b.over_run)
  1751. {
  1752. return USB_EP_STATE_NAK;
  1753. }
  1754. return USB_EP_STATE_ACK;
  1755. }
  1756. else
  1757. {
  1758. if (USB_OTG->CSR[Index].TXCSRL_DEV_b.sent_stall)
  1759. {
  1760. return USB_EP_STATE_STALL;
  1761. }
  1762. if (USB_OTG->CSR[Index].TXCSRL_DEV_b.under_run || USB_OTG->CSR[Index].TXCSRL_DEV_b.tx_pkt_rdy)
  1763. {
  1764. return USB_EP_STATE_NAK;
  1765. }
  1766. return USB_EP_STATE_ACK;
  1767. }
  1768. }
  1769. else
  1770. {
  1771. if (USB_OTG->CSR0L_DEV_b.sent_stall)
  1772. {
  1773. return USB_EP_STATE_STALL;
  1774. }
  1775. return USB_EP_STATE_ACK;
  1776. }
  1777. }
  1778. /**
  1779. * @brief Set the EP Status
  1780. * @param pdev : Selected device
  1781. * Status : new Status
  1782. * ep : EP structure
  1783. * @retval : None
  1784. */
  1785. void USB_SetDeviceEPStatus (HANDLE hUSB, uint8_t Index, uint8_t IsToDevice, uint8_t Status)
  1786. {
  1787. if (Index)
  1788. {
  1789. if (IsToDevice)
  1790. {
  1791. switch(Status)
  1792. {
  1793. case USB_EP_STATE_DISABLE:
  1794. case USB_EP_STATE_NAK:
  1795. case USB_EP_STATE_NYET:
  1796. break;
  1797. case USB_EP_STATE_STALL:
  1798. DBG("%d", Index);
  1799. USB_OTG->CSR[Index].RXCSRL_DEV_b.send_stall = 1;
  1800. break;
  1801. case USB_EP_STATE_ENABLE:
  1802. case USB_EP_STATE_ACK:
  1803. USB_OTG->CSR[Index].RXCSRL_DEV_b.send_stall = 0;
  1804. USB_OTG->CSR[Index].RXCSRL_DEV_b.sent_stall = 0;
  1805. USB_OTG->CSR[Index].RXCSRL_DEV_b.over_run = 0;
  1806. USB_OTG->CSR[Index].RXCSRL_DEV_b.data_error = 0;
  1807. break;
  1808. }
  1809. }
  1810. else
  1811. {
  1812. switch(Status)
  1813. {
  1814. case USB_EP_STATE_DISABLE:
  1815. case USB_EP_STATE_NAK:
  1816. case USB_EP_STATE_NYET:
  1817. break;
  1818. case USB_EP_STATE_STALL:
  1819. DBG("%d", Index);
  1820. USB_OTG->CSR[Index].TXCSRL_DEV_b.send_stall = 1;
  1821. break;
  1822. case USB_EP_STATE_ENABLE:
  1823. case USB_EP_STATE_ACK:
  1824. USB_OTG->CSR[Index].TXCSRL_DEV_b.send_stall = 0;
  1825. USB_OTG->CSR[Index].TXCSRL_DEV_b.sent_stall = 0;
  1826. USB_OTG->CSR[Index].TXCSRL_DEV_b.under_run = 0;
  1827. break;
  1828. }
  1829. }
  1830. }
  1831. else
  1832. {
  1833. switch(Status)
  1834. {
  1835. case USB_EP_STATE_DISABLE:
  1836. case USB_EP_STATE_NYET:
  1837. case USB_EP_STATE_NAK:
  1838. break;
  1839. case USB_EP_STATE_STALL:
  1840. USB_OTG->CSR0L_DEV_b.send_stall = 1;
  1841. break;
  1842. case USB_EP_STATE_ACK:
  1843. case USB_EP_STATE_ENABLE:
  1844. USB_OTG->CSR0L_DEV_b.send_stall = 0;
  1845. USB_OTG->CSR0L_DEV_b.sent_stall = 0;
  1846. break;
  1847. }
  1848. }
  1849. }
  1850. void USB_ResumeStart(HANDLE hUSB)
  1851. {
  1852. USB_OTG->POWER_b.resume = 1;
  1853. }
  1854. void USB_ResumeEnd(HANDLE hUSB)
  1855. {
  1856. USB_OTG->POWER_b.resume = 0;
  1857. }
  1858. int32_t USB_SetISOCHDelay(HANDLE hUSB, uint16_t DelayNS)
  1859. {
  1860. return -1;
  1861. }
  1862. int32_t USB_ExitLatency(HANDLE hUSB, uint8_t Config[6])
  1863. {
  1864. return -1;
  1865. }