core_usb_ll_driver.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934
  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 = 0;
  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. DBG("to much fifo!");
  1445. return -1;
  1446. }
  1447. if (hwUSB->EpCtrl[i].ToHostEnable)
  1448. {
  1449. USB_OTG->CSR[i].TXMAXP = hwUSB->EpCtrl[i].MaxPacketLen;
  1450. USB_OTG->TXFIFOADD = FifoStart;
  1451. USB_OTG->TXFIFOSZ = Len;
  1452. USB_OTG->CSR[i].TXCSRL = (1 << 3);
  1453. //DBG("%d,%d,%x,%d,%x", i, USB_OTG->CSR[i].TXMAXP, USB_OTG->TXFIFOADD, USB_OTG->TXFIFOSZ, USB_OTG->CSR[i].TXCSRL);
  1454. FifoStart += (hwUSB->EpCtrl[i].MaxPacketLen >> 3);
  1455. UseFifo += hwUSB->EpCtrl[i].MaxPacketLen;
  1456. }
  1457. if (UseFifo > 512)
  1458. {
  1459. DBG("to much fifo!");
  1460. return -1;
  1461. }
  1462. }
  1463. USB_OTG->INDEX = 0;
  1464. return 0;
  1465. }
  1466. void USB_GlobalInit(void)
  1467. {
  1468. USB_HWCapsStruct Caps;
  1469. ISR_SetHandler(prvUSB.IrqLine, prvUSB_IrqHandle, USB_ID0);
  1470. #ifdef __BUILD_OS__
  1471. ISR_SetPriority(prvUSB.IrqLine, IRQ_LOWEST_PRIORITY);
  1472. #else
  1473. ISR_SetPriority(prvUSB.IrqLine, 7);
  1474. #endif
  1475. prvUSB.IsConnect = 0;
  1476. Caps.EpBufMaxLen = 4096;
  1477. Caps.Feature = 0;
  1478. Caps.FEATURE_b.FullSpeed = 1;
  1479. Caps.CtrlMode = USB_MODE_DUAL;
  1480. USB_StackSetControl(USB_ID0, &prvUSB, prvUSB.EpCtrl, &Caps);
  1481. USB_StackSetDeviceSpeed(USB_ID0, USB_DEVICE_SPEED_FULL_SPEED);
  1482. USB_OTG->INTRRXE = 0;
  1483. USB_OTG->INTRTXE = 0;
  1484. USB_OTG->INTRUSBE = 0;
  1485. prvUSB.pRxData.pu32 = prvUSB.RxData;
  1486. SYSCTRL->CG_CTRL2 &= ~SYSCTRL_AHBPeriph_USB;
  1487. }
  1488. void USB_PowerOnOff(HANDLE hUSB, uint8_t OnOff)
  1489. {
  1490. if (OnOff)
  1491. {
  1492. SYSCTRL->CG_CTRL2 |= SYSCTRL_AHBPeriph_USB;
  1493. }
  1494. else
  1495. {
  1496. SYSCTRL->CG_CTRL2 &= ~SYSCTRL_AHBPeriph_USB;
  1497. }
  1498. }
  1499. void USB_Stop(HANDLE hUSB)
  1500. {
  1501. uint8_t dummy;
  1502. USB_HWCtrlStruct *hwUSB = (USB_HWCtrlStruct *)hUSB;
  1503. ISR_OnOff(hwUSB->IrqLine, 0);
  1504. USB_OTG->POWER = 0;
  1505. USB_OTG->INTRRXE = 0;
  1506. USB_OTG->INTRTXE = 0;
  1507. USB_OTG->INTRUSBE = 0;
  1508. USB_OTG->INTRTX = 0;
  1509. USB_OTG->INTRRX = 0;
  1510. USB_OTG->INTRUSB = 0;
  1511. dummy = USB_OTG->INTRUSB;
  1512. }
  1513. void USB_ResetStart(HANDLE hUSB)
  1514. {
  1515. SYSCTRL->LOCK_R &= ~SYSCTRL_USB_RESET;
  1516. SYSCTRL->SOFT_RST2 |= SYSCTRL_USB_RESET;
  1517. SYSCTRL->LOCK_R |= SYSCTRL_USB_RESET;
  1518. }
  1519. void USB_ResetEnd(HANDLE hUSB)
  1520. {
  1521. SYSCTRL->LOCK_R &= ~SYSCTRL_USB_RESET;
  1522. SYSCTRL->SOFT_RST2 &= ~SYSCTRL_USB_RESET;
  1523. SYSCTRL->LOCK_R |= SYSCTRL_USB_RESET;
  1524. }
  1525. void USB_SetWorkMode(HANDLE hUSB, uint8_t Mode)
  1526. {
  1527. USB_HWCtrlStruct *hwUSB = (USB_HWCtrlStruct *)hUSB;
  1528. USBPHY_CR1_TypeDef CR1;
  1529. USBPHY_CR3_TypeDef CR3;
  1530. CR1.d32 = SYSCTRL->USBPHY_CR1;
  1531. CR1.b.commononn = 0;
  1532. CR1.b.stop_ck_for_suspend = 0;
  1533. SYSCTRL->USBPHY_CR1 = CR1.d32;
  1534. switch (Mode)
  1535. {
  1536. case USB_MODE_HOST:
  1537. USB_OTG->POWER = 1;
  1538. USB_OTG->DEVCTL |= (1 << 0)|(1 << 2);
  1539. CR3.d32 = SYSCTRL->USBPHY_CR3;
  1540. CR3.b.idpullup = 1;
  1541. SYSCTRL->USBPHY_CR3 = CR3.d32;
  1542. hwUSB->IsHost = 1;
  1543. break;
  1544. default:
  1545. USB_OTG->DEVCTL &= ~((1 << 0)|(1 << 2));
  1546. USB_OTG->POWER = 1|(1 << 6);
  1547. CR3.d32 = SYSCTRL->USBPHY_CR3;
  1548. CR3.b.idpullup = 0;
  1549. SYSCTRL->USBPHY_CR3 = CR3.d32;
  1550. hwUSB->IsHost = 0;
  1551. break;
  1552. }
  1553. }
  1554. void USB_SetDeviceAddress(HANDLE hUSB, uint8_t Address)
  1555. {
  1556. prvUSB.NewDeviceAddress = 0x80 | Address;
  1557. }
  1558. int USB_ReInitEpCfg(HANDLE hUSB)
  1559. {
  1560. return -1;
  1561. }
  1562. int USB_InitEpCfg(HANDLE hUSB)
  1563. {
  1564. return prvUSB_SetupEPFifo(hUSB);
  1565. }
  1566. void USB_Start(HANDLE hUSB)
  1567. {
  1568. USB_HWCtrlStruct *hwUSB = (USB_HWCtrlStruct *)hUSB;
  1569. USB_OTG->INTRUSBE = 0xff;
  1570. USB_OTG->INTRUSBE_b.en_sof = 0;
  1571. USB_OTG->INTRTXE = 0xff;
  1572. USB_OTG->INTRRXE = 0xfe;
  1573. ISR_OnOff(hwUSB->IrqLine, 1);
  1574. }
  1575. void USB_SendZeroPacket(HANDLE hUSB, uint8_t EpIndex)
  1576. {
  1577. if (EpIndex)
  1578. {
  1579. USB_OTG->CSR[EpIndex].TXCSRL_DEV_b.tx_pkt_rdy = 1;
  1580. }
  1581. else
  1582. {
  1583. USB_OTG->CSR0L_DEV_b.tx_pkt_rdy = 1;
  1584. }
  1585. }
  1586. void USB_DeviceXferStop(HANDLE hUSB, uint8_t EpIndex)
  1587. {
  1588. USB_HWCtrlStruct *hwUSB = (USB_HWCtrlStruct *)hUSB;
  1589. USB_OTG->INTRTXE &= ~(1 << EpIndex);
  1590. USB_FlushFifo(hUSB, EpIndex, 0);
  1591. USB_OTG->CSR[EpIndex].TXCSRL_DEV_b.send_stall = 0;
  1592. USB_OTG->CSR[EpIndex].TXCSRL_DEV_b.sent_stall = 0;
  1593. USB_OTG->CSR[EpIndex].TXCSRL_DEV_b.under_run = 0;
  1594. USB_OTG->INTRTXE |= (1 << EpIndex);
  1595. }
  1596. void USB_EpIntOnOff(HANDLE hUSB, uint8_t EpIndex, uint8_t IsToDevice, uint8_t OnOff)
  1597. {
  1598. USB_HWCtrlStruct *hwUSB = (USB_HWCtrlStruct *)hUSB;
  1599. if (IsToDevice)
  1600. {
  1601. if (OnOff)
  1602. {
  1603. USB_OTG->INTRRXE |= (1 << EpIndex);
  1604. }
  1605. else
  1606. {
  1607. USB_OTG->INTRRXE &= ~(1 << EpIndex);
  1608. }
  1609. }
  1610. else
  1611. {
  1612. if (OnOff)
  1613. {
  1614. USB_OTG->INTRTXE |= (1 << EpIndex);
  1615. }
  1616. else
  1617. {
  1618. USB_OTG->INTRTXE &= ~(1 << EpIndex);
  1619. }
  1620. }
  1621. }
  1622. void USB_DeviceXfer(HANDLE hUSB, uint8_t EpIndex)
  1623. {
  1624. USB_HWCtrlStruct *hwUSB = (USB_HWCtrlStruct *)hUSB;
  1625. USB_EndpointCtrlStruct *pEpCtrl = &hwUSB->EpCtrl[EpIndex];
  1626. USB_EndpointDataStruct EpData;
  1627. uint16_t TxLen, i;
  1628. if (pEpCtrl->TxBuf.Data)
  1629. {
  1630. if (pEpCtrl->TxBuf.Pos >= pEpCtrl->TxBuf.MaxLen)
  1631. {
  1632. if (pEpCtrl->NeedZeroPacket)
  1633. {
  1634. // DBG("Ep%d %d %d %d need send 0 packet", EpIndex,
  1635. // pEpCtrl->ForceZeroPacket, pEpCtrl->TxBuf.Pos, pEpCtrl->XferMaxLen);
  1636. if (!pEpCtrl->TxZeroPacket)
  1637. {
  1638. pEpCtrl->TxZeroPacket = 1;
  1639. if (EpIndex)
  1640. {
  1641. USB_OTG->CSR[EpIndex].TXCSRL_DEV_b.tx_pkt_rdy = 1;
  1642. }
  1643. else
  1644. {
  1645. USB_OTG->CSR0L_DEV_b.tx_pkt_rdy = 1;
  1646. }
  1647. }
  1648. else
  1649. {
  1650. DBG("!");
  1651. }
  1652. }
  1653. goto XFER_DONE;
  1654. }
  1655. else
  1656. {
  1657. pEpCtrl->TxZeroPacket = 0;
  1658. if ((pEpCtrl->TxBuf.MaxLen - pEpCtrl->TxBuf.Pos) > pEpCtrl->MaxPacketLen)
  1659. {
  1660. TxLen = pEpCtrl->MaxPacketLen;
  1661. }
  1662. else
  1663. {
  1664. if (pEpCtrl->ForceZeroPacket)
  1665. {
  1666. pEpCtrl->NeedZeroPacket = 1;
  1667. }
  1668. if ((pEpCtrl->TxBuf.MaxLen - pEpCtrl->TxBuf.Pos) == pEpCtrl->MaxPacketLen)
  1669. {
  1670. if ( pEpCtrl->TxBuf.MaxLen < pEpCtrl->XferMaxLen )
  1671. {
  1672. pEpCtrl->NeedZeroPacket = 1;
  1673. }
  1674. }
  1675. TxLen = pEpCtrl->TxBuf.MaxLen - pEpCtrl->TxBuf.Pos;
  1676. }
  1677. for(i = pEpCtrl->TxBuf.Pos; i < pEpCtrl->TxBuf.Pos + TxLen; i++)
  1678. {
  1679. USB_OTG->FIFOX[EpIndex].byte = pEpCtrl->TxBuf.Data[i];
  1680. }
  1681. pEpCtrl->TxBuf.Pos += TxLen;
  1682. if (EpIndex)
  1683. {
  1684. USB_OTG->CSR[EpIndex].TXCSRL_DEV_b.tx_pkt_rdy = 1;
  1685. }
  1686. else
  1687. {
  1688. USB_OTG->CSR0L_DEV_b.tx_pkt_rdy = 1;
  1689. }
  1690. }
  1691. }
  1692. return;
  1693. XFER_DONE:
  1694. memset(&pEpCtrl->TxBuf, 0, sizeof(Buffer_Struct));
  1695. if (EpIndex)
  1696. {
  1697. memset(&EpData, 0, sizeof(EpData));
  1698. EpData.USB_ID = USB_ID0;
  1699. EpData.EpIndex = EpIndex;
  1700. pEpCtrl->CB(&EpData, pEpCtrl->pData);
  1701. }
  1702. else
  1703. {
  1704. USB_StackDeviceEp0TxDone(USB_ID0);
  1705. }
  1706. }
  1707. void USB_SetDeviceNoDataSetup(HANDLE hUSB)
  1708. {
  1709. {
  1710. USB_OTG->CSR0L_DEV_b.data_end = 1;
  1711. }
  1712. }
  1713. void USB_FlushFifo(HANDLE hUSB, uint8_t EpIndex, uint8_t IsToDevice)
  1714. {
  1715. if (EpIndex)
  1716. {
  1717. if (IsToDevice)
  1718. {
  1719. USB_OTG->CSR[EpIndex].RXCSRL = (1 << 4);
  1720. }
  1721. else
  1722. {
  1723. USB_OTG->CSR[EpIndex].TXCSRL = (1 << 3);
  1724. }
  1725. }
  1726. else
  1727. {
  1728. if (USB_OTG->CSR0L_DEV_b.rx_pkt_rdy || USB_OTG->CSR0L_DEV_b.tx_pkt_rdy)
  1729. {
  1730. USB_OTG->CSR0H_DEV_b.flush_fifo = 1;
  1731. USB_OTG->CSR0L_DEV_b.serviced_rxpktrdy = 1;
  1732. USB_OTG->CSR0L_DEV_b.tx_pkt_rdy = 0;
  1733. }
  1734. }
  1735. }
  1736. /**
  1737. * @brief returns the EP Status
  1738. * @param pdev : Selected device
  1739. * ep : endpoint structure
  1740. * @retval : EP status
  1741. */
  1742. uint8_t USB_GetDeviceEPStatus(HANDLE hUSB, uint8_t Index, uint8_t IsToDevice)
  1743. {
  1744. if (Index)
  1745. {
  1746. if (IsToDevice)
  1747. {
  1748. if (USB_OTG->CSR[Index].RXCSRL_DEV_b.sent_stall)
  1749. {
  1750. return USB_EP_STATE_STALL;
  1751. }
  1752. if (USB_OTG->CSR[Index].RXCSRL_DEV_b.data_error || USB_OTG->CSR[Index].RXCSRL_DEV_b.over_run)
  1753. {
  1754. return USB_EP_STATE_NAK;
  1755. }
  1756. return USB_EP_STATE_ACK;
  1757. }
  1758. else
  1759. {
  1760. if (USB_OTG->CSR[Index].TXCSRL_DEV_b.sent_stall)
  1761. {
  1762. return USB_EP_STATE_STALL;
  1763. }
  1764. if (USB_OTG->CSR[Index].TXCSRL_DEV_b.under_run || USB_OTG->CSR[Index].TXCSRL_DEV_b.tx_pkt_rdy)
  1765. {
  1766. return USB_EP_STATE_NAK;
  1767. }
  1768. return USB_EP_STATE_ACK;
  1769. }
  1770. }
  1771. else
  1772. {
  1773. if (USB_OTG->CSR0L_DEV_b.sent_stall)
  1774. {
  1775. return USB_EP_STATE_STALL;
  1776. }
  1777. return USB_EP_STATE_ACK;
  1778. }
  1779. }
  1780. /**
  1781. * @brief Set the EP Status
  1782. * @param pdev : Selected device
  1783. * Status : new Status
  1784. * ep : EP structure
  1785. * @retval : None
  1786. */
  1787. void USB_SetDeviceEPStatus (HANDLE hUSB, uint8_t Index, uint8_t IsToDevice, uint8_t Status)
  1788. {
  1789. if (Index)
  1790. {
  1791. if (IsToDevice)
  1792. {
  1793. switch(Status)
  1794. {
  1795. case USB_EP_STATE_DISABLE:
  1796. case USB_EP_STATE_NAK:
  1797. case USB_EP_STATE_NYET:
  1798. break;
  1799. case USB_EP_STATE_STALL:
  1800. DBG("%d", Index);
  1801. USB_OTG->CSR[Index].RXCSRL_DEV_b.send_stall = 1;
  1802. break;
  1803. case USB_EP_STATE_ENABLE:
  1804. case USB_EP_STATE_ACK:
  1805. USB_OTG->CSR[Index].RXCSRL_DEV_b.send_stall = 0;
  1806. USB_OTG->CSR[Index].RXCSRL_DEV_b.sent_stall = 0;
  1807. USB_OTG->CSR[Index].RXCSRL_DEV_b.over_run = 0;
  1808. USB_OTG->CSR[Index].RXCSRL_DEV_b.data_error = 0;
  1809. break;
  1810. }
  1811. }
  1812. else
  1813. {
  1814. switch(Status)
  1815. {
  1816. case USB_EP_STATE_DISABLE:
  1817. case USB_EP_STATE_NAK:
  1818. case USB_EP_STATE_NYET:
  1819. break;
  1820. case USB_EP_STATE_STALL:
  1821. DBG("%d", Index);
  1822. USB_OTG->CSR[Index].TXCSRL_DEV_b.send_stall = 1;
  1823. break;
  1824. case USB_EP_STATE_ENABLE:
  1825. case USB_EP_STATE_ACK:
  1826. USB_OTG->CSR[Index].TXCSRL_DEV_b.send_stall = 0;
  1827. USB_OTG->CSR[Index].TXCSRL_DEV_b.sent_stall = 0;
  1828. USB_OTG->CSR[Index].TXCSRL_DEV_b.under_run = 0;
  1829. break;
  1830. }
  1831. }
  1832. }
  1833. else
  1834. {
  1835. switch(Status)
  1836. {
  1837. case USB_EP_STATE_DISABLE:
  1838. case USB_EP_STATE_NYET:
  1839. case USB_EP_STATE_NAK:
  1840. break;
  1841. case USB_EP_STATE_STALL:
  1842. USB_OTG->CSR0L_DEV_b.send_stall = 1;
  1843. break;
  1844. case USB_EP_STATE_ACK:
  1845. case USB_EP_STATE_ENABLE:
  1846. USB_OTG->CSR0L_DEV_b.send_stall = 0;
  1847. USB_OTG->CSR0L_DEV_b.sent_stall = 0;
  1848. break;
  1849. }
  1850. }
  1851. }
  1852. void USB_ResumeStart(HANDLE hUSB)
  1853. {
  1854. USB_OTG->POWER_b.resume = 1;
  1855. }
  1856. void USB_ResumeEnd(HANDLE hUSB)
  1857. {
  1858. USB_OTG->POWER_b.resume = 0;
  1859. }
  1860. int32_t USB_SetISOCHDelay(HANDLE hUSB, uint16_t DelayNS)
  1861. {
  1862. return -1;
  1863. }
  1864. int32_t USB_ExitLatency(HANDLE hUSB, uint8_t Config[6])
  1865. {
  1866. return -1;
  1867. }
  1868. #ifdef __BUILD_APP__
  1869. INIT_DRV_EXPORT(USB_GlobalInit, "0");
  1870. #endif