usb.c 134 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972
  1. //*****************************************************************************
  2. //
  3. // usb.c - Driver for the USB Interface.
  4. //
  5. // Copyright (c) 2007-2011 Texas Instruments Incorporated. All rights reserved.
  6. // Software License Agreement
  7. //
  8. // Texas Instruments (TI) is supplying this software for use solely and
  9. // exclusively on TI's microcontroller products. The software is owned by
  10. // TI and/or its suppliers, and is protected under applicable copyright
  11. // laws. You may not combine this software with "viral" open-source
  12. // software in order to form a larger program.
  13. //
  14. // THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
  15. // NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
  16. // NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. // A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
  18. // CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
  19. // DAMAGES, FOR ANY REASON WHATSOEVER.
  20. //
  21. // This is part of revision 8049 of the Stellaris Peripheral Driver Library.
  22. //
  23. //*****************************************************************************
  24. //*****************************************************************************
  25. //
  26. //! \addtogroup usb_api
  27. //! @{
  28. //
  29. //*****************************************************************************
  30. #include "inc/hw_ints.h"
  31. #include "inc/hw_memmap.h"
  32. #include "inc/hw_types.h"
  33. #include "inc/hw_usb.h"
  34. #include "driverlib/debug.h"
  35. #include "driverlib/interrupt.h"
  36. #include "driverlib/udma.h"
  37. #include "driverlib/usb.h"
  38. //*****************************************************************************
  39. //
  40. // Amount to shift the RX interrupt sources by in the flags used in the
  41. // interrupt calls.
  42. //
  43. //*****************************************************************************
  44. #ifndef DEPRECATED
  45. #define USB_INT_RX_SHIFT 8
  46. #endif
  47. #define USB_INTEP_RX_SHIFT 16
  48. //*****************************************************************************
  49. //
  50. // Amount to shift the status interrupt sources by in the flags used in the
  51. // interrupt calls.
  52. //
  53. //*****************************************************************************
  54. #ifndef DEPRECATED
  55. #define USB_INT_STATUS_SHIFT 24
  56. #endif
  57. //*****************************************************************************
  58. //
  59. // Amount to shift the RX endpoint status sources by in the flags used in the
  60. // calls.
  61. //
  62. //*****************************************************************************
  63. #define USB_RX_EPSTATUS_SHIFT 16
  64. //*****************************************************************************
  65. //
  66. // Converts from an endpoint specifier to the offset of the endpoint's
  67. // control/status registers.
  68. //
  69. //*****************************************************************************
  70. #define EP_OFFSET(Endpoint) (Endpoint - 0x10)
  71. //*****************************************************************************
  72. //
  73. // Sets one of the indexed registers.
  74. //
  75. // \param ulBase specifies the USB module base address.
  76. // \param ulEndpoint is the endpoint index to target for this write.
  77. // \param ulIndexedReg is the indexed register to write to.
  78. // \param ucValue is the value to write to the register.
  79. //
  80. // This function is used to access the indexed registers for each endpoint.
  81. // The only registers that are indexed are the FIFO configuration registers
  82. // which are not used after configuration.
  83. //
  84. // \return None.
  85. //
  86. //*****************************************************************************
  87. static void
  88. USBIndexWrite(unsigned long ulBase, unsigned long ulEndpoint,
  89. unsigned long ulIndexedReg, unsigned long ulValue,
  90. unsigned long ulSize)
  91. {
  92. unsigned long ulIndex;
  93. //
  94. // Check the arguments.
  95. //
  96. ASSERT(ulBase == USB0_BASE);
  97. ASSERT((ulEndpoint == 0) || (ulEndpoint == 1) || (ulEndpoint == 2) ||
  98. (ulEndpoint == 3));
  99. ASSERT((ulSize == 1) || (ulSize == 2));
  100. //
  101. // Save the old index in case it was in use.
  102. //
  103. ulIndex = HWREGB(ulBase + USB_O_EPIDX);
  104. //
  105. // Set the index.
  106. //
  107. HWREGB(ulBase + USB_O_EPIDX) = ulEndpoint;
  108. //
  109. // Determine the size of the register value.
  110. //
  111. if(ulSize == 1)
  112. {
  113. //
  114. // Set the value.
  115. //
  116. HWREGB(ulBase + ulIndexedReg) = ulValue;
  117. }
  118. else
  119. {
  120. //
  121. // Set the value.
  122. //
  123. HWREGH(ulBase + ulIndexedReg) = ulValue;
  124. }
  125. //
  126. // Restore the old index in case it was in use.
  127. //
  128. HWREGB(ulBase + USB_O_EPIDX) = ulIndex;
  129. }
  130. //*****************************************************************************
  131. //
  132. // Reads one of the indexed registers.
  133. //
  134. // \param ulBase specifies the USB module base address.
  135. // \param ulEndpoint is the endpoint index to target for this write.
  136. // \param ulIndexedReg is the indexed register to write to.
  137. //
  138. // This function is used internally to access the indexed registers for each
  139. // endpoint. The only registers that are indexed are the FIFO configuration
  140. // registers which are not used after configuration.
  141. //
  142. // \return The value in the register requested.
  143. //
  144. //*****************************************************************************
  145. static unsigned long
  146. USBIndexRead(unsigned long ulBase, unsigned long ulEndpoint,
  147. unsigned long ulIndexedReg, unsigned long ulSize)
  148. {
  149. unsigned char ulIndex;
  150. unsigned char ulValue;
  151. //
  152. // Check the arguments.
  153. //
  154. ASSERT(ulBase == USB0_BASE);
  155. ASSERT((ulEndpoint == 0) || (ulEndpoint == 1) || (ulEndpoint == 2) ||
  156. (ulEndpoint == 3));
  157. ASSERT((ulSize == 1) || (ulSize == 2));
  158. //
  159. // Save the old index in case it was in use.
  160. //
  161. ulIndex = HWREGB(ulBase + USB_O_EPIDX);
  162. //
  163. // Set the index.
  164. //
  165. HWREGB(ulBase + USB_O_EPIDX) = ulEndpoint;
  166. //
  167. // Determine the size of the register value.
  168. //
  169. if(ulSize == 1)
  170. {
  171. //
  172. // Get the value.
  173. //
  174. ulValue = HWREGB(ulBase + ulIndexedReg);
  175. }
  176. else
  177. {
  178. //
  179. // Get the value.
  180. //
  181. ulValue = HWREGH(ulBase + ulIndexedReg);
  182. }
  183. //
  184. // Restore the old index in case it was in use.
  185. //
  186. HWREGB(ulBase + USB_O_EPIDX) = ulIndex;
  187. //
  188. // Return the register's value.
  189. //
  190. return(ulValue);
  191. }
  192. //*****************************************************************************
  193. //
  194. //! Puts the USB bus in a suspended state.
  195. //!
  196. //! \param ulBase specifies the USB module base address.
  197. //!
  198. //! When used in host mode, this function will put the USB bus in the suspended
  199. //! state.
  200. //!
  201. //! \note This function should only be called in host mode.
  202. //!
  203. //! \return None.
  204. //
  205. //*****************************************************************************
  206. void
  207. USBHostSuspend(unsigned long ulBase)
  208. {
  209. //
  210. // Check the arguments.
  211. //
  212. ASSERT(ulBase == USB0_BASE);
  213. //
  214. // Send the suspend signaling to the USB bus.
  215. //
  216. HWREGB(ulBase + USB_O_POWER) |= USB_POWER_SUSPEND;
  217. }
  218. //*****************************************************************************
  219. //
  220. //! Handles the USB bus reset condition.
  221. //!
  222. //! \param ulBase specifies the USB module base address.
  223. //! \param bStart specifies whether to start or stop signaling reset on the USB
  224. //! bus.
  225. //!
  226. //! When this function is called with the \e bStart parameter set to \b true,
  227. //! this function will cause the start of a reset condition on the USB bus.
  228. //! The caller should then delay at least 20ms before calling this function
  229. //! again with the \e bStart parameter set to \b false.
  230. //!
  231. //! \note This function should only be called in host mode.
  232. //!
  233. //! \return None.
  234. //
  235. //*****************************************************************************
  236. void
  237. USBHostReset(unsigned long ulBase, tBoolean bStart)
  238. {
  239. //
  240. // Check the arguments.
  241. //
  242. ASSERT(ulBase == USB0_BASE);
  243. //
  244. // Send a reset signal to the bus.
  245. //
  246. if(bStart)
  247. {
  248. HWREGB(ulBase + USB_O_POWER) |= USB_POWER_RESET;
  249. }
  250. else
  251. {
  252. HWREGB(ulBase + USB_O_POWER) &= ~USB_POWER_RESET;
  253. }
  254. }
  255. //*****************************************************************************
  256. //
  257. //! Handles the USB bus resume condition.
  258. //!
  259. //! \param ulBase specifies the USB module base address.
  260. //! \param bStart specifies if the USB controller is entering or leaving the
  261. //! resume signaling state.
  262. //!
  263. //! When in device mode this function will bring the USB controller out of the
  264. //! suspend state. This call should first be made with the \e bStart parameter
  265. //! set to \b true to start resume signaling. The device application should
  266. //! then delay at least 10ms but not more than 15ms before calling this
  267. //! function with the \e bStart parameter set to \b false.
  268. //!
  269. //! When in host mode this function will signal devices to leave the suspend
  270. //! state. This call should first be made with the \e bStart parameter set to
  271. //! \b true to start resume signaling. The host application should then delay
  272. //! at least 20ms before calling this function with the \e bStart parameter set
  273. //! to \b false. This will cause the controller to complete the resume
  274. //! signaling on the USB bus.
  275. //!
  276. //! \return None.
  277. //
  278. //*****************************************************************************
  279. void
  280. USBHostResume(unsigned long ulBase, tBoolean bStart)
  281. {
  282. //
  283. // Check the arguments.
  284. //
  285. ASSERT(ulBase == USB0_BASE);
  286. //
  287. // Send a resume signal to the bus.
  288. //
  289. if(bStart)
  290. {
  291. HWREGB(ulBase + USB_O_POWER) |= USB_POWER_RESUME;
  292. }
  293. else
  294. {
  295. HWREGB(ulBase + USB_O_POWER) &= ~USB_POWER_RESUME;
  296. }
  297. }
  298. //*****************************************************************************
  299. //
  300. //! Returns the current speed of the USB device connected.
  301. //!
  302. //! \param ulBase specifies the USB module base address.
  303. //!
  304. //! This function will return the current speed of the USB bus.
  305. //!
  306. //! \note This function should only be called in host mode.
  307. //!
  308. //! \return Returns either \b USB_LOW_SPEED, \b USB_FULL_SPEED, or
  309. //! \b USB_UNDEF_SPEED.
  310. //
  311. //*****************************************************************************
  312. unsigned long
  313. USBHostSpeedGet(unsigned long ulBase)
  314. {
  315. //
  316. // Check the arguments.
  317. //
  318. ASSERT(ulBase == USB0_BASE);
  319. //
  320. // If the Full Speed device bit is set, then this is a full speed device.
  321. //
  322. if(HWREGB(ulBase + USB_O_DEVCTL) & USB_DEVCTL_FSDEV)
  323. {
  324. return(USB_FULL_SPEED);
  325. }
  326. //
  327. // If the Low Speed device bit is set, then this is a low speed device.
  328. //
  329. if(HWREGB(ulBase + USB_O_DEVCTL) & USB_DEVCTL_LSDEV)
  330. {
  331. return(USB_LOW_SPEED);
  332. }
  333. //
  334. // The device speed is not known.
  335. //
  336. return(USB_UNDEF_SPEED);
  337. }
  338. //*****************************************************************************
  339. //
  340. //! Returns the status of the USB interrupts.
  341. //!
  342. //! \param ulBase specifies the USB module base address.
  343. //!
  344. //! This function will read the source of the interrupt for the USB controller.
  345. //! There are three groups of interrupt sources, IN Endpoints, OUT Endpoints,
  346. //! and general status changes. This call will return the current status for
  347. //! all of these interrupts. The bit values returned should be compared
  348. //! against the \b USB_HOST_IN, \b USB_HOST_OUT, \b USB_HOST_EP0,
  349. //! \b USB_DEV_IN, \b USB_DEV_OUT, and \b USB_DEV_EP0 values.
  350. //!
  351. //! \note This call will clear the source of all of the general status
  352. //! interrupts.
  353. //!
  354. //! \note WARNING: This API cannot be used on endpoint numbers greater than
  355. //! endpoint 3 so USBIntStatusControl() or USBIntStatusEndpoint() should be
  356. //! used instead.
  357. //!
  358. //! \return Returns the status of the sources for the USB controller's
  359. //! interrupt.
  360. //
  361. //*****************************************************************************
  362. #ifndef DEPRECATED
  363. unsigned long
  364. USBIntStatus(unsigned long ulBase)
  365. {
  366. unsigned long ulStatus;
  367. //
  368. // Check the arguments.
  369. //
  370. ASSERT(ulBase == USB0_BASE);
  371. //
  372. // Get the transmit interrupt status.
  373. //
  374. ulStatus = (HWREGB(ulBase + USB_O_TXIS));
  375. //
  376. // Get the receive interrupt status, these bits go into the second byte of
  377. // the returned value.
  378. //
  379. ulStatus |= (HWREGB(ulBase + USB_O_RXIS) << USB_INT_RX_SHIFT);
  380. //
  381. // Get the general interrupt status, these bits go into the upper 8 bits
  382. // of the returned value.
  383. //
  384. ulStatus |= (HWREGB(ulBase + USB_O_IS) << USB_INT_STATUS_SHIFT);
  385. //
  386. // Add the power fault status.
  387. //
  388. if(HWREG(ulBase + USB_O_EPCISC) & USB_EPCISC_PF)
  389. {
  390. //
  391. // Indicate a power fault was detected.
  392. //
  393. ulStatus |= USB_INT_POWER_FAULT;
  394. //
  395. // Clear the power fault interrupt.
  396. //
  397. HWREGB(ulBase + USB_O_EPCISC) |= USB_EPCISC_PF;
  398. }
  399. if(HWREG(USB0_BASE + USB_O_IDVISC) & USB_IDVRIS_ID)
  400. {
  401. //
  402. // Indicate a id detection was detected.
  403. //
  404. ulStatus |= USB_INT_MODE_DETECT;
  405. //
  406. // Clear the id detection interrupt.
  407. //
  408. HWREG(USB0_BASE + USB_O_IDVISC) |= USB_IDVRIS_ID;
  409. }
  410. //
  411. // Return the combined interrupt status.
  412. //
  413. return(ulStatus);
  414. }
  415. #endif
  416. //*****************************************************************************
  417. //
  418. //! Disables the sources for USB interrupts.
  419. //!
  420. //! \param ulBase specifies the USB module base address.
  421. //! \param ulFlags specifies which interrupts to disable.
  422. //!
  423. //! This function will disable the USB controller from generating the
  424. //! interrupts indicated by the \e ulFlags parameter. There are three groups
  425. //! of interrupt sources, IN Endpoints, OUT Endpoints, and general status
  426. //! changes, specified by \b USB_INT_HOST_IN, \b USB_INT_HOST_OUT,
  427. //! \b USB_INT_DEV_IN, \b USB_INT_DEV_OUT, and \b USB_INT_STATUS. If
  428. //! \b USB_INT_ALL is specified then all interrupts are disabled.
  429. //!
  430. //! \note WARNING: This API cannot be used on endpoint numbers greater than
  431. //! endpoint 3 so USBIntDisableControl() or USBIntDisableEndpoint() should be
  432. //! used instead.
  433. //!
  434. //! \return None.
  435. //
  436. //*****************************************************************************
  437. #ifndef DEPRECATED
  438. void
  439. USBIntDisable(unsigned long ulBase, unsigned long ulFlags)
  440. {
  441. //
  442. // Check the arguments.
  443. //
  444. ASSERT(ulBase == USB0_BASE);
  445. ASSERT((ulFlags & ~(USB_INT_ALL)) == 0);
  446. //
  447. // If any transmit interrupts were disabled then write the transmit
  448. // interrupt settings out to the hardware.
  449. //
  450. if(ulFlags & (USB_INT_HOST_OUT | USB_INT_DEV_IN | USB_INT_EP0))
  451. {
  452. HWREGH(ulBase + USB_O_TXIE) &=
  453. ~(ulFlags & (USB_INT_HOST_OUT | USB_INT_DEV_IN | USB_INT_EP0));
  454. }
  455. //
  456. // If any receive interrupts were disabled then write the receive interrupt
  457. // settings out to the hardware.
  458. //
  459. if(ulFlags & (USB_INT_HOST_IN | USB_INT_DEV_OUT))
  460. {
  461. HWREGH(ulBase + USB_O_RXIE) &=
  462. ~((ulFlags & (USB_INT_HOST_IN | USB_INT_DEV_OUT)) >>
  463. USB_INT_RX_SHIFT);
  464. }
  465. //
  466. // If any general interrupts were disabled then write the general interrupt
  467. // settings out to the hardware.
  468. //
  469. if(ulFlags & USB_INT_STATUS)
  470. {
  471. HWREGB(ulBase + USB_O_IE) &=
  472. ~((ulFlags & USB_INT_STATUS) >> USB_INT_STATUS_SHIFT);
  473. }
  474. //
  475. // Disable the power fault interrupt.
  476. //
  477. if(ulFlags & USB_INT_POWER_FAULT)
  478. {
  479. HWREG(ulBase + USB_O_EPCIM) = 0;
  480. }
  481. //
  482. // Disable the ID pin detect interrupt.
  483. //
  484. if(ulFlags & USB_INT_MODE_DETECT)
  485. {
  486. HWREG(USB0_BASE + USB_O_IDVIM) = 0;
  487. }
  488. }
  489. #endif
  490. //*****************************************************************************
  491. //
  492. //! Enables the sources for USB interrupts.
  493. //!
  494. //! \param ulBase specifies the USB module base address.
  495. //! \param ulFlags specifies which interrupts to enable.
  496. //!
  497. //! This function will enable the USB controller's ability to generate the
  498. //! interrupts indicated by the \e ulFlags parameter. There are three
  499. //! groups of interrupt sources, IN Endpoints, OUT Endpoints, and
  500. //! general status changes, specified by \b USB_INT_HOST_IN,
  501. //! \b USB_INT_HOST_OUT, \b USB_INT_DEV_IN, \b USB_INT_DEV_OUT, and
  502. //! \b USB_STATUS. If \b USB_INT_ALL is specified then all interrupts are
  503. //! enabled.
  504. //!
  505. //! \note A call must be made to enable the interrupt in the main interrupt
  506. //! controller to receive interrupts. The USBIntRegister() API performs this
  507. //! controller level interrupt enable. However if static interrupt handlers
  508. //! are used then then a call to IntEnable() must be made in order to allow any
  509. //! USB interrupts to occur.
  510. //!
  511. //! \note WARNING: This API cannot be used on endpoint numbers greater than
  512. //! endpoint 3 so USBIntEnableControl() or USBIntEnableEndpoint() should be
  513. //! used instead.
  514. //!
  515. //! \return None.
  516. //
  517. //*****************************************************************************
  518. #ifndef DEPRECATED
  519. void
  520. USBIntEnable(unsigned long ulBase, unsigned long ulFlags)
  521. {
  522. //
  523. // Check the arguments.
  524. //
  525. ASSERT(ulBase == USB0_BASE);
  526. ASSERT((ulFlags & (~USB_INT_ALL)) == 0);
  527. //
  528. // If any transmit interrupts were enabled then write the transmit
  529. // interrupt settings out to the hardware.
  530. //
  531. if(ulFlags & (USB_INT_HOST_OUT | USB_INT_DEV_IN | USB_INT_EP0))
  532. {
  533. HWREGH(ulBase + USB_O_TXIE) |=
  534. ulFlags & (USB_INT_HOST_OUT | USB_INT_DEV_IN | USB_INT_EP0);
  535. }
  536. //
  537. // If any receive interrupts were enabled then write the receive interrupt
  538. // settings out to the hardware.
  539. //
  540. if(ulFlags & (USB_INT_HOST_IN | USB_INT_DEV_OUT))
  541. {
  542. HWREGH(ulBase + USB_O_RXIE) |=
  543. ((ulFlags & (USB_INT_HOST_IN | USB_INT_DEV_OUT)) >>
  544. USB_INT_RX_SHIFT);
  545. }
  546. //
  547. // If any general interrupts were enabled then write the general interrupt
  548. // settings out to the hardware.
  549. //
  550. if(ulFlags & USB_INT_STATUS)
  551. {
  552. HWREGB(ulBase + USB_O_IE) |=
  553. (ulFlags & USB_INT_STATUS) >> USB_INT_STATUS_SHIFT;
  554. }
  555. //
  556. // Enable the power fault interrupt.
  557. //
  558. if(ulFlags & USB_INT_POWER_FAULT)
  559. {
  560. HWREG(ulBase + USB_O_EPCIM) = USB_EPCIM_PF;
  561. }
  562. //
  563. // Enable the ID pin detect interrupt.
  564. //
  565. if(ulFlags & USB_INT_MODE_DETECT)
  566. {
  567. HWREG(USB0_BASE + USB_O_IDVIM) = USB_IDVIM_ID;
  568. }
  569. }
  570. #endif
  571. //*****************************************************************************
  572. //
  573. //! Disables control interrupts on a given USB controller.
  574. //!
  575. //! \param ulBase specifies the USB module base address.
  576. //! \param ulFlags specifies which control interrupts to disable.
  577. //!
  578. //! This function will disable the control interrupts for the USB controller
  579. //! specified by the \e ulBase parameter. The \e ulFlags parameter specifies
  580. //! which control interrupts to disable. The flags passed in the \e ulFlags
  581. //! parameters should be the definitions that start with \b USB_INTCTRL_* and
  582. //! not any other \b USB_INT flags.
  583. //!
  584. //! \return None.
  585. //
  586. //*****************************************************************************
  587. void
  588. USBIntDisableControl(unsigned long ulBase, unsigned long ulFlags)
  589. {
  590. //
  591. // Check the arguments.
  592. //
  593. ASSERT(ulBase == USB0_BASE);
  594. ASSERT((ulFlags & ~(USB_INTCTRL_ALL)) == 0);
  595. //
  596. // If any general interrupts were disabled then write the general interrupt
  597. // settings out to the hardware.
  598. //
  599. if(ulFlags & USB_INTCTRL_STATUS)
  600. {
  601. HWREGB(ulBase + USB_O_IE) &= ~(ulFlags & USB_INTCTRL_STATUS);
  602. }
  603. //
  604. // Disable the power fault interrupt.
  605. //
  606. if(ulFlags & USB_INTCTRL_POWER_FAULT)
  607. {
  608. HWREG(ulBase + USB_O_EPCIM) = 0;
  609. }
  610. //
  611. // Disable the ID pin detect interrupt.
  612. //
  613. if(ulFlags & USB_INTCTRL_MODE_DETECT)
  614. {
  615. HWREG(USB0_BASE + USB_O_IDVIM) = 0;
  616. }
  617. }
  618. //*****************************************************************************
  619. //
  620. //! Enables control interrupts on a given USB controller.
  621. //!
  622. //! \param ulBase specifies the USB module base address.
  623. //! \param ulFlags specifies which control interrupts to enable.
  624. //!
  625. //! This function will enable the control interrupts for the USB controller
  626. //! specified by the \e ulBase parameter. The \e ulFlags parameter specifies
  627. //! which control interrupts to enable. The flags passed in the \e ulFlags
  628. //! parameters should be the definitions that start with \b USB_INTCTRL_* and
  629. //! not any other \b USB_INT flags.
  630. //!
  631. //! \return None.
  632. //
  633. //*****************************************************************************
  634. void
  635. USBIntEnableControl(unsigned long ulBase, unsigned long ulFlags)
  636. {
  637. //
  638. // Check the arguments.
  639. //
  640. ASSERT(ulBase == USB0_BASE);
  641. ASSERT((ulFlags & (~USB_INTCTRL_ALL)) == 0);
  642. //
  643. // If any general interrupts were enabled then write the general interrupt
  644. // settings out to the hardware.
  645. //
  646. if(ulFlags & USB_INTCTRL_STATUS)
  647. {
  648. HWREGB(ulBase + USB_O_IE) |= ulFlags;
  649. }
  650. //
  651. // Enable the power fault interrupt.
  652. //
  653. if(ulFlags & USB_INTCTRL_POWER_FAULT)
  654. {
  655. HWREG(ulBase + USB_O_EPCIM) = USB_EPCIM_PF;
  656. }
  657. //
  658. // Enable the ID pin detect interrupt.
  659. //
  660. if(ulFlags & USB_INTCTRL_MODE_DETECT)
  661. {
  662. HWREG(USB0_BASE + USB_O_IDVIM) = USB_IDVIM_ID;
  663. }
  664. }
  665. //*****************************************************************************
  666. //
  667. //! Returns the control interrupt status on a given USB controller.
  668. //!
  669. //! \param ulBase specifies the USB module base address.
  670. //!
  671. //! This function will read control interrupt status for a USB controller.
  672. //! This call will return the current status for control interrupts only, the
  673. //! endpoint interrupt status is retrieved by calling USBIntStatusEndpoint().
  674. //! The bit values returned should be compared against the \b USB_INTCTRL_*
  675. //! values.
  676. //!
  677. //! The following are the meanings of all \b USB_INCTRL_ flags and the modes
  678. //! for which they are valid. These values apply to any calls to
  679. //! USBIntStatusControl(), USBIntEnableControl(), and USBIntDisableControl().
  680. //! Some of these flags are only valid in the following modes as indicated in
  681. //! the parenthesis: Host, Device, and OTG.
  682. //!
  683. //! - \b USB_INTCTRL_ALL - A full mask of all control interrupt sources.
  684. //! - \b USB_INTCTRL_VBUS_ERR - A VBUS error has occurred (Host Only).
  685. //! - \b USB_INTCTRL_SESSION - Session Start Detected on A-side of cable
  686. //! (OTG Only).
  687. //! - \b USB_INTCTRL_SESSION_END - Session End Detected (Device Only)
  688. //! - \b USB_INTCTRL_DISCONNECT - Device Disconnect Detected (Host Only)
  689. //! - \b USB_INTCTRL_CONNECT - Device Connect Detected (Host Only)
  690. //! - \b USB_INTCTRL_SOF - Start of Frame Detected.
  691. //! - \b USB_INTCTRL_BABBLE - USB controller detected a device signaling past
  692. //! the end of a frame. (Host Only)
  693. //! - \b USB_INTCTRL_RESET - Reset signaling detected by device. (Device Only)
  694. //! - \b USB_INTCTRL_RESUME - Resume signaling detected.
  695. //! - \b USB_INTCTRL_SUSPEND - Suspend signaling detected by device (Device
  696. //! Only)
  697. //! - \b USB_INTCTRL_MODE_DETECT - OTG cable mode detection has completed
  698. //! (OTG Only)
  699. //! - \b USB_INTCTRL_POWER_FAULT - Power Fault detected. (Host Only)
  700. //!
  701. //! \note This call will clear the source of all of the control status
  702. //! interrupts.
  703. //!
  704. //! \return Returns the status of the control interrupts for a USB controller.
  705. //
  706. //*****************************************************************************
  707. unsigned long
  708. USBIntStatusControl(unsigned long ulBase)
  709. {
  710. unsigned long ulStatus;
  711. //
  712. // Check the arguments.
  713. //
  714. ASSERT(ulBase == USB0_BASE);
  715. //
  716. // Get the general interrupt status, these bits go into the upper 8 bits
  717. // of the returned value.
  718. //
  719. ulStatus = HWREGB(ulBase + USB_O_IS);
  720. //
  721. // Add the power fault status.
  722. //
  723. if(HWREG(ulBase + USB_O_EPCISC) & USB_EPCISC_PF)
  724. {
  725. //
  726. // Indicate a power fault was detected.
  727. //
  728. ulStatus |= USB_INTCTRL_POWER_FAULT;
  729. //
  730. // Clear the power fault interrupt.
  731. //
  732. HWREGB(ulBase + USB_O_EPCISC) |= USB_EPCISC_PF;
  733. }
  734. if(HWREG(USB0_BASE + USB_O_IDVISC) & USB_IDVRIS_ID)
  735. {
  736. //
  737. // Indicate a id detection was detected.
  738. //
  739. ulStatus |= USB_INTCTRL_MODE_DETECT;
  740. //
  741. // Clear the id detection interrupt.
  742. //
  743. HWREG(USB0_BASE + USB_O_IDVISC) |= USB_IDVRIS_ID;
  744. }
  745. //
  746. // Return the combined interrupt status.
  747. //
  748. return(ulStatus);
  749. }
  750. //*****************************************************************************
  751. //
  752. //! Disables endpoint interrupts on a given USB controller.
  753. //!
  754. //! \param ulBase specifies the USB module base address.
  755. //! \param ulFlags specifies which endpoint interrupts to disable.
  756. //!
  757. //! This function will disable endpoint interrupts for the USB controller
  758. //! specified by the \e ulBase parameter. The \e ulFlags parameter specifies
  759. //! which endpoint interrupts to disable. The flags passed in the \e ulFlags
  760. //! parameters should be the definitions that start with \b USB_INTEP_* and not
  761. //! any other \b USB_INT flags.
  762. //!
  763. //! \return None.
  764. //
  765. //*****************************************************************************
  766. void
  767. USBIntDisableEndpoint(unsigned long ulBase, unsigned long ulFlags)
  768. {
  769. //
  770. // Check the arguments.
  771. //
  772. ASSERT(ulBase == USB0_BASE);
  773. //
  774. // If any transmit interrupts were disabled then write the transmit
  775. // interrupt settings out to the hardware.
  776. //
  777. HWREGH(ulBase + USB_O_TXIE) &=
  778. ~(ulFlags & (USB_INTEP_HOST_OUT | USB_INTEP_DEV_IN | USB_INTEP_0));
  779. //
  780. // If any receive interrupts were disabled then write the receive interrupt
  781. // settings out to the hardware.
  782. //
  783. HWREGH(ulBase + USB_O_RXIE) &=
  784. ~((ulFlags & (USB_INTEP_HOST_IN | USB_INTEP_DEV_OUT)) >>
  785. USB_INTEP_RX_SHIFT);
  786. }
  787. //*****************************************************************************
  788. //
  789. //! Enables endpoint interrupts on a given USB controller.
  790. //!
  791. //! \param ulBase specifies the USB module base address.
  792. //! \param ulFlags specifies which endpoint interrupts to enable.
  793. //!
  794. //! This function will enable endpoint interrupts for the USB controller
  795. //! specified by the \e ulBase parameter. The \e ulFlags parameter specifies
  796. //! which endpoint interrupts to enable. The flags passed in the \e ulFlags
  797. //! parameters should be the definitions that start with \b USB_INTEP_* and not
  798. //! any other \b USB_INT flags.
  799. //!
  800. //! \return None.
  801. //
  802. //*****************************************************************************
  803. void
  804. USBIntEnableEndpoint(unsigned long ulBase, unsigned long ulFlags)
  805. {
  806. //
  807. // Check the arguments.
  808. //
  809. ASSERT(ulBase == USB0_BASE);
  810. //
  811. // Enable any transmit endpoint interrupts.
  812. //
  813. HWREGH(ulBase + USB_O_TXIE) |=
  814. ulFlags & (USB_INTEP_HOST_OUT | USB_INTEP_DEV_IN | USB_INTEP_0);
  815. //
  816. // Enable any receive endpoint interrupts.
  817. //
  818. HWREGH(ulBase + USB_O_RXIE) |=
  819. ((ulFlags & (USB_INTEP_HOST_IN | USB_INTEP_DEV_OUT)) >>
  820. USB_INTEP_RX_SHIFT);
  821. }
  822. //*****************************************************************************
  823. //
  824. //! Returns the endpoint interrupt status on a given USB controller.
  825. //!
  826. //! \param ulBase specifies the USB module base address.
  827. //!
  828. //! This function will read endpoint interrupt status for a USB controller.
  829. //! This call will return the current status for endpoint interrupts only, the
  830. //! control interrupt status is retrieved by calling USBIntStatusControl().
  831. //! The bit values returned should be compared against the \b USB_INTEP_*
  832. //! values. These are grouped into classes for \b USB_INTEP_HOST_* and
  833. //! \b USB_INTEP_DEV_* values to handle both host and device modes with all
  834. //! endpoints.
  835. //!
  836. //! \note This call will clear the source of all of the endpoint interrupts.
  837. //!
  838. //! \return Returns the status of the endpoint interrupts for a USB controller.
  839. //
  840. //*****************************************************************************
  841. unsigned long
  842. USBIntStatusEndpoint(unsigned long ulBase)
  843. {
  844. unsigned long ulStatus;
  845. //
  846. // Check the arguments.
  847. //
  848. ASSERT(ulBase == USB0_BASE);
  849. //
  850. // Get the transmit interrupt status.
  851. //
  852. ulStatus = HWREGH(ulBase + USB_O_TXIS);
  853. ulStatus |= (HWREGH(ulBase + USB_O_RXIS) << USB_INTEP_RX_SHIFT);
  854. //
  855. // Return the combined interrupt status.
  856. //
  857. return(ulStatus);
  858. }
  859. //*****************************************************************************
  860. //
  861. //! Registers an interrupt handler for the USB controller.
  862. //!
  863. //! \param ulBase specifies the USB module base address.
  864. //! \param pfnHandler is a pointer to the function to be called when a USB
  865. //! interrupt occurs.
  866. //!
  867. //! This sets the handler to be called when a USB interrupt occurs. This will
  868. //! also enable the global USB interrupt in the interrupt controller. The
  869. //! specific desired USB interrupts must be enabled via a separate call to
  870. //! USBIntEnable(). It is the interrupt handler's responsibility to clear the
  871. //! interrupt sources via a calls to USBIntStatusControl() and
  872. //! USBIntStatusEndpoint().
  873. //!
  874. //! \sa IntRegister() for important information about registering interrupt
  875. //! handlers.
  876. //!
  877. //! \return None.
  878. //
  879. //*****************************************************************************
  880. void
  881. USBIntRegister(unsigned long ulBase, void(*pfnHandler)(void))
  882. {
  883. //
  884. // Check the arguments.
  885. //
  886. ASSERT(ulBase == USB0_BASE);
  887. //
  888. // Register the interrupt handler.
  889. //
  890. IntRegister(INT_USB0, pfnHandler);
  891. //
  892. // Enable the USB interrupt.
  893. //
  894. IntEnable(INT_USB0);
  895. }
  896. //*****************************************************************************
  897. //
  898. //! Unregisters an interrupt handler for the USB controller.
  899. //!
  900. //! \param ulBase specifies the USB module base address.
  901. //!
  902. //! This function unregister the interrupt handler. This function will also
  903. //! disable the USB interrupt in the interrupt controller.
  904. //!
  905. //! \sa IntRegister() for important information about registering or
  906. //! unregistering interrupt handlers.
  907. //!
  908. //! \return None.
  909. //
  910. //*****************************************************************************
  911. void
  912. USBIntUnregister(unsigned long ulBase)
  913. {
  914. //
  915. // Check the arguments.
  916. //
  917. ASSERT(ulBase == USB0_BASE);
  918. //
  919. // Disable the USB interrupt.
  920. //
  921. IntDisable(INT_USB0);
  922. //
  923. // Unregister the interrupt handler.
  924. //
  925. IntUnregister(INT_USB0);
  926. }
  927. //*****************************************************************************
  928. //
  929. //! Returns the current status of an endpoint.
  930. //!
  931. //! \param ulBase specifies the USB module base address.
  932. //! \param ulEndpoint is the endpoint to access.
  933. //!
  934. //! This function will return the status of a given endpoint. If any of these
  935. //! status bits need to be cleared, then these these values must be cleared by
  936. //! calling the USBDevEndpointStatusClear() or USBHostEndpointStatusClear()
  937. //! functions.
  938. //!
  939. //! The following are the status flags for host mode:
  940. //!
  941. //! - \b USB_HOST_IN_PID_ERROR - PID error on the given endpoint.
  942. //! - \b USB_HOST_IN_NOT_COMP - The device failed to respond to an IN request.
  943. //! - \b USB_HOST_IN_STALL - A stall was received on an IN endpoint.
  944. //! - \b USB_HOST_IN_DATA_ERROR - There was a CRC or bit-stuff error on an IN
  945. //! endpoint in Isochronous mode.
  946. //! - \b USB_HOST_IN_NAK_TO - NAKs received on this IN endpoint for more than
  947. //! the specified timeout period.
  948. //! - \b USB_HOST_IN_ERROR - Failed to communicate with a device using this IN
  949. //! endpoint.
  950. //! - \b USB_HOST_IN_FIFO_FULL - This IN endpoint's FIFO is full.
  951. //! - \b USB_HOST_IN_PKTRDY - Data packet ready on this IN endpoint.
  952. //! - \b USB_HOST_OUT_NAK_TO - NAKs received on this OUT endpoint for more than
  953. //! the specified timeout period.
  954. //! - \b USB_HOST_OUT_NOT_COMP - The device failed to respond to an OUT
  955. //! request.
  956. //! - \b USB_HOST_OUT_STALL - A stall was received on this OUT endpoint.
  957. //! - \b USB_HOST_OUT_ERROR - Failed to communicate with a device using this
  958. //! OUT endpoint.
  959. //! - \b USB_HOST_OUT_FIFO_NE - This endpoint's OUT FIFO is not empty.
  960. //! - \b USB_HOST_OUT_PKTPEND - The data transfer on this OUT endpoint has not
  961. //! completed.
  962. //! - \b USB_HOST_EP0_NAK_TO - NAKs received on endpoint zero for more than the
  963. //! specified timeout period.
  964. //! - \b USB_HOST_EP0_ERROR - The device failed to respond to a request on
  965. //! endpoint zero.
  966. //! - \b USB_HOST_EP0_IN_STALL - A stall was received on endpoint zero for an
  967. //! IN transaction.
  968. //! - \b USB_HOST_EP0_IN_PKTRDY - Data packet ready on endpoint zero for an IN
  969. //! transaction.
  970. //!
  971. //! The following are the status flags for device mode:
  972. //!
  973. //! - \b USB_DEV_OUT_SENT_STALL - A stall was sent on this OUT endpoint.
  974. //! - \b USB_DEV_OUT_DATA_ERROR - There was a CRC or bit-stuff error on an OUT
  975. //! endpoint.
  976. //! - \b USB_DEV_OUT_OVERRUN - An OUT packet was not loaded due to a full FIFO.
  977. //! - \b USB_DEV_OUT_FIFO_FULL - The OUT endpoint's FIFO is full.
  978. //! - \b USB_DEV_OUT_PKTRDY - There is a data packet ready in the OUT
  979. //! endpoint's FIFO.
  980. //! - \b USB_DEV_IN_NOT_COMP - A larger packet was split up, more data to come.
  981. //! - \b USB_DEV_IN_SENT_STALL - A stall was sent on this IN endpoint.
  982. //! - \b USB_DEV_IN_UNDERRUN - Data was requested on the IN endpoint and no
  983. //! data was ready.
  984. //! - \b USB_DEV_IN_FIFO_NE - The IN endpoint's FIFO is not empty.
  985. //! - \b USB_DEV_IN_PKTPEND - The data transfer on this IN endpoint has not
  986. //! completed.
  987. //! - \b USB_DEV_EP0_SETUP_END - A control transaction ended before Data End
  988. //! condition was sent.
  989. //! - \b USB_DEV_EP0_SENT_STALL - A stall was sent on endpoint zero.
  990. //! - \b USB_DEV_EP0_IN_PKTPEND - The data transfer on endpoint zero has not
  991. //! completed.
  992. //! - \b USB_DEV_EP0_OUT_PKTRDY - There is a data packet ready in endpoint
  993. //! zero's OUT FIFO.
  994. //!
  995. //! \return The current status flags for the endpoint depending on mode.
  996. //
  997. //*****************************************************************************
  998. unsigned long
  999. USBEndpointStatus(unsigned long ulBase, unsigned long ulEndpoint)
  1000. {
  1001. unsigned long ulStatus;
  1002. //
  1003. // Check the arguments.
  1004. //
  1005. ASSERT(ulBase == USB0_BASE);
  1006. ASSERT((ulEndpoint == USB_EP_0) || (ulEndpoint == USB_EP_1) ||
  1007. (ulEndpoint == USB_EP_2) || (ulEndpoint == USB_EP_3) ||
  1008. (ulEndpoint == USB_EP_4) || (ulEndpoint == USB_EP_5) ||
  1009. (ulEndpoint == USB_EP_6) || (ulEndpoint == USB_EP_7) ||
  1010. (ulEndpoint == USB_EP_8) || (ulEndpoint == USB_EP_9) ||
  1011. (ulEndpoint == USB_EP_10) || (ulEndpoint == USB_EP_11) ||
  1012. (ulEndpoint == USB_EP_12) || (ulEndpoint == USB_EP_13) ||
  1013. (ulEndpoint == USB_EP_14) || (ulEndpoint == USB_EP_15));
  1014. //
  1015. // Get the TX portion of the endpoint status.
  1016. //
  1017. ulStatus = HWREGH(ulBase + EP_OFFSET(ulEndpoint) + USB_O_TXCSRL1);
  1018. //
  1019. // Get the RX portion of the endpoint status.
  1020. //
  1021. ulStatus |= ((HWREGH(ulBase + EP_OFFSET(ulEndpoint) + USB_O_RXCSRL1)) <<
  1022. USB_RX_EPSTATUS_SHIFT);
  1023. //
  1024. // Return the endpoint status.
  1025. //
  1026. return(ulStatus);
  1027. }
  1028. //*****************************************************************************
  1029. //
  1030. //! Clears the status bits in this endpoint in host mode.
  1031. //!
  1032. //! \param ulBase specifies the USB module base address.
  1033. //! \param ulEndpoint is the endpoint to access.
  1034. //! \param ulFlags are the status bits that should be cleared.
  1035. //!
  1036. //! This function will clear the status of any bits that are passed in the
  1037. //! \e ulFlags parameter. The \e ulFlags parameter can take the value returned
  1038. //! from the USBEndpointStatus() call.
  1039. //!
  1040. //! \note This function should only be called in host mode.
  1041. //!
  1042. //! \return None.
  1043. //
  1044. //*****************************************************************************
  1045. void
  1046. USBHostEndpointStatusClear(unsigned long ulBase, unsigned long ulEndpoint,
  1047. unsigned long ulFlags)
  1048. {
  1049. //
  1050. // Check the arguments.
  1051. //
  1052. ASSERT(ulBase == USB0_BASE);
  1053. ASSERT((ulEndpoint == USB_EP_0) || (ulEndpoint == USB_EP_1) ||
  1054. (ulEndpoint == USB_EP_2) || (ulEndpoint == USB_EP_3) ||
  1055. (ulEndpoint == USB_EP_4) || (ulEndpoint == USB_EP_5) ||
  1056. (ulEndpoint == USB_EP_6) || (ulEndpoint == USB_EP_7) ||
  1057. (ulEndpoint == USB_EP_8) || (ulEndpoint == USB_EP_9) ||
  1058. (ulEndpoint == USB_EP_10) || (ulEndpoint == USB_EP_11) ||
  1059. (ulEndpoint == USB_EP_12) || (ulEndpoint == USB_EP_13) ||
  1060. (ulEndpoint == USB_EP_14) || (ulEndpoint == USB_EP_15));
  1061. //
  1062. // Clear the specified flags for the endpoint.
  1063. //
  1064. if(ulEndpoint == USB_EP_0)
  1065. {
  1066. HWREGB(ulBase + USB_O_CSRL0) &= ~ulFlags;
  1067. }
  1068. else
  1069. {
  1070. HWREGB(ulBase + USB_O_TXCSRL1 + EP_OFFSET(ulEndpoint)) &= ~ulFlags;
  1071. HWREGB(ulBase + USB_O_RXCSRL1 + EP_OFFSET(ulEndpoint)) &=
  1072. ~(ulFlags >> USB_RX_EPSTATUS_SHIFT);
  1073. }
  1074. }
  1075. //*****************************************************************************
  1076. //
  1077. //! Clears the status bits in this endpoint in device mode.
  1078. //!
  1079. //! \param ulBase specifies the USB module base address.
  1080. //! \param ulEndpoint is the endpoint to access.
  1081. //! \param ulFlags are the status bits that should be cleared.
  1082. //!
  1083. //! This function will clear the status of any bits that are passed in the
  1084. //! \e ulFlags parameter. The \e ulFlags parameter can take the value returned
  1085. //! from the USBEndpointStatus() call.
  1086. //!
  1087. //! \note This function should only be called in device mode.
  1088. //!
  1089. //! \return None.
  1090. //
  1091. //*****************************************************************************
  1092. void
  1093. USBDevEndpointStatusClear(unsigned long ulBase, unsigned long ulEndpoint,
  1094. unsigned long ulFlags)
  1095. {
  1096. //
  1097. // Check the arguments.
  1098. //
  1099. ASSERT(ulBase == USB0_BASE);
  1100. ASSERT((ulEndpoint == USB_EP_0) || (ulEndpoint == USB_EP_1) ||
  1101. (ulEndpoint == USB_EP_2) || (ulEndpoint == USB_EP_3) ||
  1102. (ulEndpoint == USB_EP_4) || (ulEndpoint == USB_EP_5) ||
  1103. (ulEndpoint == USB_EP_6) || (ulEndpoint == USB_EP_7) ||
  1104. (ulEndpoint == USB_EP_8) || (ulEndpoint == USB_EP_9) ||
  1105. (ulEndpoint == USB_EP_10) || (ulEndpoint == USB_EP_11) ||
  1106. (ulEndpoint == USB_EP_12) || (ulEndpoint == USB_EP_13) ||
  1107. (ulEndpoint == USB_EP_14) || (ulEndpoint == USB_EP_15));
  1108. //
  1109. // If this is endpoint 0 then the bits have different meaning and map into
  1110. // the TX memory location.
  1111. //
  1112. if(ulEndpoint == USB_EP_0)
  1113. {
  1114. //
  1115. // Set the Serviced RxPktRdy bit to clear the RxPktRdy.
  1116. //
  1117. if(ulFlags & USB_DEV_EP0_OUT_PKTRDY)
  1118. {
  1119. HWREGB(ulBase + USB_O_CSRL0) |= USB_CSRL0_RXRDYC;
  1120. }
  1121. //
  1122. // Set the serviced Setup End bit to clear the SetupEnd status.
  1123. //
  1124. if(ulFlags & USB_DEV_EP0_SETUP_END)
  1125. {
  1126. HWREGB(ulBase + USB_O_CSRL0) |= USB_CSRL0_SETENDC;
  1127. }
  1128. //
  1129. // Clear the Sent Stall status flag.
  1130. //
  1131. if(ulFlags & USB_DEV_EP0_SENT_STALL)
  1132. {
  1133. HWREGB(ulBase + USB_O_CSRL0) &= ~(USB_DEV_EP0_SENT_STALL);
  1134. }
  1135. }
  1136. else
  1137. {
  1138. //
  1139. // Clear out any TX flags that were passed in. Only
  1140. // USB_DEV_TX_SENT_STALL and USB_DEV_TX_UNDERRUN should be cleared.
  1141. //
  1142. HWREGB(ulBase + USB_O_TXCSRL1 + EP_OFFSET(ulEndpoint)) &=
  1143. ~(ulFlags & (USB_DEV_TX_SENT_STALL | USB_DEV_TX_UNDERRUN));
  1144. //
  1145. // Clear out valid RX flags that were passed in. Only
  1146. // USB_DEV_RX_SENT_STALL, USB_DEV_RX_DATA_ERROR, and USB_DEV_RX_OVERRUN
  1147. // should be cleared.
  1148. //
  1149. HWREGB(ulBase + USB_O_RXCSRL1 + EP_OFFSET(ulEndpoint)) &=
  1150. ~((ulFlags & (USB_DEV_RX_SENT_STALL | USB_DEV_RX_DATA_ERROR |
  1151. USB_DEV_RX_OVERRUN)) >> USB_RX_EPSTATUS_SHIFT);
  1152. }
  1153. }
  1154. //*****************************************************************************
  1155. //
  1156. //! Sets the value data toggle on an endpoint in host mode.
  1157. //!
  1158. //! \param ulBase specifies the USB module base address.
  1159. //! \param ulEndpoint specifies the endpoint to reset the data toggle.
  1160. //! \param bDataToggle specifies whether to set the state to DATA0 or DATA1.
  1161. //! \param ulFlags specifies whether to set the IN or OUT endpoint.
  1162. //!
  1163. //! This function is used to force the state of the data toggle in host mode.
  1164. //! If the value passed in the \e bDataToggle parameter is \b false, then the
  1165. //! data toggle is set to the DATA0 state, and if it is \b true it is set to
  1166. //! the DATA1 state. The \e ulFlags parameter can be \b USB_EP_HOST_IN or
  1167. //! \b USB_EP_HOST_OUT to access the desired portion of this endpoint. The
  1168. //! \e ulFlags parameter is ignored for endpoint zero.
  1169. //!
  1170. //! \note This function should only be called in host mode.
  1171. //!
  1172. //! \return None.
  1173. //
  1174. //*****************************************************************************
  1175. void
  1176. USBHostEndpointDataToggle(unsigned long ulBase, unsigned long ulEndpoint,
  1177. tBoolean bDataToggle, unsigned long ulFlags)
  1178. {
  1179. unsigned long ulDataToggle;
  1180. //
  1181. // Check the arguments.
  1182. //
  1183. ASSERT(ulBase == USB0_BASE);
  1184. ASSERT((ulEndpoint == USB_EP_0) || (ulEndpoint == USB_EP_1) ||
  1185. (ulEndpoint == USB_EP_2) || (ulEndpoint == USB_EP_3) ||
  1186. (ulEndpoint == USB_EP_4) || (ulEndpoint == USB_EP_5) ||
  1187. (ulEndpoint == USB_EP_6) || (ulEndpoint == USB_EP_7) ||
  1188. (ulEndpoint == USB_EP_8) || (ulEndpoint == USB_EP_9) ||
  1189. (ulEndpoint == USB_EP_10) || (ulEndpoint == USB_EP_11) ||
  1190. (ulEndpoint == USB_EP_12) || (ulEndpoint == USB_EP_13) ||
  1191. (ulEndpoint == USB_EP_14) || (ulEndpoint == USB_EP_15));
  1192. //
  1193. // The data toggle defaults to DATA0.
  1194. //
  1195. ulDataToggle = 0;
  1196. //
  1197. // See if the data toggle should be set to DATA1.
  1198. //
  1199. if(bDataToggle)
  1200. {
  1201. //
  1202. // Select the data toggle bit based on the endpoint.
  1203. //
  1204. if(ulEndpoint == USB_EP_0)
  1205. {
  1206. ulDataToggle = USB_CSRH0_DT;
  1207. }
  1208. else if(ulFlags == USB_EP_HOST_IN)
  1209. {
  1210. ulDataToggle = USB_RXCSRH1_DT;
  1211. }
  1212. else
  1213. {
  1214. ulDataToggle = USB_TXCSRH1_DT;
  1215. }
  1216. }
  1217. //
  1218. // Set the data toggle based on the endpoint.
  1219. //
  1220. if(ulEndpoint == USB_EP_0)
  1221. {
  1222. //
  1223. // Set the write enable and the bit value for endpoint zero.
  1224. //
  1225. HWREGB(ulBase + USB_O_CSRH0) =
  1226. ((HWREGB(ulBase + USB_O_CSRH0) &
  1227. ~(USB_CSRH0_DTWE | USB_CSRH0_DT)) |
  1228. (ulDataToggle | USB_CSRH0_DTWE));
  1229. }
  1230. else if(ulFlags == USB_EP_HOST_IN)
  1231. {
  1232. //
  1233. // Set the Write enable and the bit value for an IN endpoint.
  1234. //
  1235. HWREGB(ulBase + USB_O_RXCSRH1 + EP_OFFSET(ulEndpoint)) =
  1236. ((HWREGB(ulBase + USB_O_RXCSRH1 + EP_OFFSET(ulEndpoint)) &
  1237. ~(USB_RXCSRH1_DTWE | USB_RXCSRH1_DT)) |
  1238. (ulDataToggle | USB_RXCSRH1_DTWE));
  1239. }
  1240. else
  1241. {
  1242. //
  1243. // Set the Write enable and the bit value for an OUT endpoint.
  1244. //
  1245. HWREGB(ulBase + USB_O_TXCSRH1 + EP_OFFSET(ulEndpoint)) =
  1246. ((HWREGB(ulBase + USB_O_TXCSRH1 + EP_OFFSET(ulEndpoint)) &
  1247. ~(USB_TXCSRH1_DTWE | USB_TXCSRH1_DT)) |
  1248. (ulDataToggle | USB_TXCSRH1_DTWE));
  1249. }
  1250. }
  1251. //*****************************************************************************
  1252. //
  1253. //! Sets the Data toggle on an endpoint to zero.
  1254. //!
  1255. //! \param ulBase specifies the USB module base address.
  1256. //! \param ulEndpoint specifies the endpoint to reset the data toggle.
  1257. //! \param ulFlags specifies whether to access the IN or OUT endpoint.
  1258. //!
  1259. //! This function will cause the controller to clear the data toggle for an
  1260. //! endpoint. This call is not valid for endpoint zero and can be made with
  1261. //! host or device controllers.
  1262. //!
  1263. //! The \e ulFlags parameter should be one of \b USB_EP_HOST_OUT,
  1264. //! \b USB_EP_HOST_IN, \b USB_EP_DEV_OUT, or \b USB_EP_DEV_IN.
  1265. //!
  1266. //! \return None.
  1267. //
  1268. //*****************************************************************************
  1269. void
  1270. USBEndpointDataToggleClear(unsigned long ulBase, unsigned long ulEndpoint,
  1271. unsigned long ulFlags)
  1272. {
  1273. //
  1274. // Check the arguments.
  1275. //
  1276. ASSERT(ulBase == USB0_BASE);
  1277. ASSERT((ulEndpoint == USB_EP_1) || (ulEndpoint == USB_EP_2) ||
  1278. (ulEndpoint == USB_EP_3) || (ulEndpoint == USB_EP_4) ||
  1279. (ulEndpoint == USB_EP_5) || (ulEndpoint == USB_EP_6) ||
  1280. (ulEndpoint == USB_EP_7) || (ulEndpoint == USB_EP_8) ||
  1281. (ulEndpoint == USB_EP_9) || (ulEndpoint == USB_EP_10) ||
  1282. (ulEndpoint == USB_EP_11) || (ulEndpoint == USB_EP_12) ||
  1283. (ulEndpoint == USB_EP_13) || (ulEndpoint == USB_EP_14) ||
  1284. (ulEndpoint == USB_EP_15));
  1285. //
  1286. // See if the transmit or receive data toggle should be cleared.
  1287. //
  1288. if(ulFlags & (USB_EP_HOST_OUT | USB_EP_DEV_IN))
  1289. {
  1290. HWREGB(ulBase + USB_O_TXCSRL1 + EP_OFFSET(ulEndpoint)) |=
  1291. USB_TXCSRL1_CLRDT;
  1292. }
  1293. else
  1294. {
  1295. HWREGB(ulBase + USB_O_RXCSRL1 + EP_OFFSET(ulEndpoint)) |=
  1296. USB_RXCSRL1_CLRDT;
  1297. }
  1298. }
  1299. //*****************************************************************************
  1300. //
  1301. //! Stalls the specified endpoint in device mode.
  1302. //!
  1303. //! \param ulBase specifies the USB module base address.
  1304. //! \param ulEndpoint specifies the endpoint to stall.
  1305. //! \param ulFlags specifies whether to stall the IN or OUT endpoint.
  1306. //!
  1307. //! This function will cause to endpoint number passed in to go into a stall
  1308. //! condition. If the \e ulFlags parameter is \b USB_EP_DEV_IN then the stall
  1309. //! is issued on the IN portion of this endpoint. If the \e ulFlags parameter
  1310. //! is \b USB_EP_DEV_OUT then the stall is issued on the OUT portion of this
  1311. //! endpoint.
  1312. //!
  1313. //! \note This function should only be called in device mode.
  1314. //!
  1315. //! \return None.
  1316. //
  1317. //*****************************************************************************
  1318. void
  1319. USBDevEndpointStall(unsigned long ulBase, unsigned long ulEndpoint,
  1320. unsigned long ulFlags)
  1321. {
  1322. //
  1323. // Check the arguments.
  1324. //
  1325. ASSERT(ulBase == USB0_BASE);
  1326. ASSERT((ulFlags & ~(USB_EP_DEV_IN | USB_EP_DEV_OUT)) == 0)
  1327. ASSERT((ulEndpoint == USB_EP_0) || (ulEndpoint == USB_EP_1) ||
  1328. (ulEndpoint == USB_EP_2) || (ulEndpoint == USB_EP_3) ||
  1329. (ulEndpoint == USB_EP_4) || (ulEndpoint == USB_EP_5) ||
  1330. (ulEndpoint == USB_EP_6) || (ulEndpoint == USB_EP_7) ||
  1331. (ulEndpoint == USB_EP_8) || (ulEndpoint == USB_EP_9) ||
  1332. (ulEndpoint == USB_EP_10) || (ulEndpoint == USB_EP_11) ||
  1333. (ulEndpoint == USB_EP_12) || (ulEndpoint == USB_EP_13) ||
  1334. (ulEndpoint == USB_EP_14) || (ulEndpoint == USB_EP_15));
  1335. //
  1336. // Determine how to stall this endpoint.
  1337. //
  1338. if(ulEndpoint == USB_EP_0)
  1339. {
  1340. //
  1341. // Perform a stall on endpoint zero.
  1342. //
  1343. HWREGB(ulBase + USB_O_CSRL0) |=
  1344. (USB_CSRL0_STALL | USB_CSRL0_RXRDYC);
  1345. }
  1346. else if(ulFlags == USB_EP_DEV_IN)
  1347. {
  1348. //
  1349. // Perform a stall on an IN endpoint.
  1350. //
  1351. HWREGB(ulBase + USB_O_TXCSRL1 + EP_OFFSET(ulEndpoint)) |=
  1352. USB_TXCSRL1_STALL;
  1353. }
  1354. else
  1355. {
  1356. //
  1357. // Perform a stall on an OUT endpoint.
  1358. //
  1359. HWREGB(ulBase + USB_O_RXCSRL1 + EP_OFFSET(ulEndpoint)) |=
  1360. USB_RXCSRL1_STALL;
  1361. }
  1362. }
  1363. //*****************************************************************************
  1364. //
  1365. //! Clears the stall condition on the specified endpoint in device mode.
  1366. //!
  1367. //! \param ulBase specifies the USB module base address.
  1368. //! \param ulEndpoint specifies which endpoint to remove the stall condition.
  1369. //! \param ulFlags specifies whether to remove the stall condition from the IN
  1370. //! or the OUT portion of this endpoint.
  1371. //!
  1372. //! This function will cause the endpoint number passed in to exit the stall
  1373. //! condition. If the \e ulFlags parameter is \b USB_EP_DEV_IN then the stall
  1374. //! is cleared on the IN portion of this endpoint. If the \e ulFlags parameter
  1375. //! is \b USB_EP_DEV_OUT then the stall is cleared on the OUT portion of this
  1376. //! endpoint.
  1377. //!
  1378. //! \note This function should only be called in device mode.
  1379. //!
  1380. //! \return None.
  1381. //
  1382. //*****************************************************************************
  1383. void
  1384. USBDevEndpointStallClear(unsigned long ulBase, unsigned long ulEndpoint,
  1385. unsigned long ulFlags)
  1386. {
  1387. //
  1388. // Check the arguments.
  1389. //
  1390. ASSERT(ulBase == USB0_BASE);
  1391. ASSERT((ulEndpoint == USB_EP_0) || (ulEndpoint == USB_EP_1) ||
  1392. (ulEndpoint == USB_EP_2) || (ulEndpoint == USB_EP_3) ||
  1393. (ulEndpoint == USB_EP_4) || (ulEndpoint == USB_EP_5) ||
  1394. (ulEndpoint == USB_EP_6) || (ulEndpoint == USB_EP_7) ||
  1395. (ulEndpoint == USB_EP_8) || (ulEndpoint == USB_EP_9) ||
  1396. (ulEndpoint == USB_EP_10) || (ulEndpoint == USB_EP_11) ||
  1397. (ulEndpoint == USB_EP_12) || (ulEndpoint == USB_EP_13) ||
  1398. (ulEndpoint == USB_EP_14) || (ulEndpoint == USB_EP_15));
  1399. ASSERT((ulFlags & ~(USB_EP_DEV_IN | USB_EP_DEV_OUT)) == 0)
  1400. //
  1401. // Determine how to clear the stall on this endpoint.
  1402. //
  1403. if(ulEndpoint == USB_EP_0)
  1404. {
  1405. //
  1406. // Clear the stall on endpoint zero.
  1407. //
  1408. HWREGB(ulBase + USB_O_CSRL0) &= ~USB_CSRL0_STALLED;
  1409. }
  1410. else if(ulFlags == USB_EP_DEV_IN)
  1411. {
  1412. //
  1413. // Clear the stall on an IN endpoint.
  1414. //
  1415. HWREGB(ulBase + USB_O_TXCSRL1 + EP_OFFSET(ulEndpoint)) &=
  1416. ~(USB_TXCSRL1_STALL | USB_TXCSRL1_STALLED);
  1417. //
  1418. // Reset the data toggle.
  1419. //
  1420. HWREGB(ulBase + USB_O_TXCSRL1 + EP_OFFSET(ulEndpoint)) |=
  1421. USB_TXCSRL1_CLRDT;
  1422. }
  1423. else
  1424. {
  1425. //
  1426. // Clear the stall on an OUT endpoint.
  1427. //
  1428. HWREGB(ulBase + USB_O_RXCSRL1 + EP_OFFSET(ulEndpoint)) &=
  1429. ~(USB_RXCSRL1_STALL | USB_RXCSRL1_STALLED);
  1430. //
  1431. // Reset the data toggle.
  1432. //
  1433. HWREGB(ulBase + USB_O_RXCSRL1 + EP_OFFSET(ulEndpoint)) |=
  1434. USB_RXCSRL1_CLRDT;
  1435. }
  1436. }
  1437. //*****************************************************************************
  1438. //
  1439. //! Connects the USB controller to the bus in device mode.
  1440. //!
  1441. //! \param ulBase specifies the USB module base address.
  1442. //!
  1443. //! This function will cause the soft connect feature of the USB controller to
  1444. //! be enabled. Call USBDevDisconnect() to remove the USB device from the bus.
  1445. //!
  1446. //! \note This function should only be called in device mode.
  1447. //!
  1448. //! \return None.
  1449. //
  1450. //*****************************************************************************
  1451. void
  1452. USBDevConnect(unsigned long ulBase)
  1453. {
  1454. //
  1455. // Check the arguments.
  1456. //
  1457. ASSERT(ulBase == USB0_BASE);
  1458. //
  1459. // Enable connection to the USB bus.
  1460. //
  1461. HWREGB(ulBase + USB_O_POWER) |= USB_POWER_SOFTCONN;
  1462. }
  1463. //*****************************************************************************
  1464. //
  1465. //! Removes the USB controller from the bus in device mode.
  1466. //!
  1467. //! \param ulBase specifies the USB module base address.
  1468. //!
  1469. //! This function will cause the soft connect feature of the USB controller to
  1470. //! remove the device from the USB bus. A call to USBDevConnect() is needed to
  1471. //! reconnect to the bus.
  1472. //!
  1473. //! \note This function should only be called in device mode.
  1474. //!
  1475. //! \return None.
  1476. //
  1477. //*****************************************************************************
  1478. void
  1479. USBDevDisconnect(unsigned long ulBase)
  1480. {
  1481. //
  1482. // Check the arguments.
  1483. //
  1484. ASSERT(ulBase == USB0_BASE);
  1485. //
  1486. // Disable connection to the USB bus.
  1487. //
  1488. HWREGB(ulBase + USB_O_POWER) &= (~USB_POWER_SOFTCONN);
  1489. }
  1490. //*****************************************************************************
  1491. //
  1492. //! Sets the address in device mode.
  1493. //!
  1494. //! \param ulBase specifies the USB module base address.
  1495. //! \param ulAddress is the address to use for a device.
  1496. //!
  1497. //! This function will set the device address on the USB bus. This address was
  1498. //! likely received via a SET ADDRESS command from the host controller.
  1499. //!
  1500. //! \note This function should only be called in device mode.
  1501. //!
  1502. //! \return None.
  1503. //
  1504. //*****************************************************************************
  1505. void
  1506. USBDevAddrSet(unsigned long ulBase, unsigned long ulAddress)
  1507. {
  1508. //
  1509. // Check the arguments.
  1510. //
  1511. ASSERT(ulBase == USB0_BASE);
  1512. //
  1513. // Set the function address in the correct location.
  1514. //
  1515. HWREGB(ulBase + USB_O_FADDR) = (unsigned char)ulAddress;
  1516. }
  1517. //*****************************************************************************
  1518. //
  1519. //! Returns the current device address in device mode.
  1520. //!
  1521. //! \param ulBase specifies the USB module base address.
  1522. //!
  1523. //! This function will return the current device address. This address was set
  1524. //! by a call to USBDevAddrSet().
  1525. //!
  1526. //! \note This function should only be called in device mode.
  1527. //!
  1528. //! \return The current device address.
  1529. //
  1530. //*****************************************************************************
  1531. unsigned long
  1532. USBDevAddrGet(unsigned long ulBase)
  1533. {
  1534. //
  1535. // Check the arguments.
  1536. //
  1537. ASSERT(ulBase == USB0_BASE);
  1538. //
  1539. // Return the function address.
  1540. //
  1541. return(HWREGB(ulBase + USB_O_FADDR));
  1542. }
  1543. //*****************************************************************************
  1544. //
  1545. //! Sets the base configuration for a host endpoint.
  1546. //!
  1547. //! \param ulBase specifies the USB module base address.
  1548. //! \param ulEndpoint is the endpoint to access.
  1549. //! \param ulMaxPayload is the maximum payload for this endpoint.
  1550. //! \param ulNAKPollInterval is the either the NAK timeout limit or the polling
  1551. //! interval depending on the type of endpoint.
  1552. //! \param ulTargetEndpoint is the endpoint that the host endpoint is
  1553. //! targeting.
  1554. //! \param ulFlags are used to configure other endpoint settings.
  1555. //!
  1556. //! This function will set the basic configuration for the transmit or receive
  1557. //! portion of an endpoint in host mode. The \e ulFlags parameter determines
  1558. //! some of the configuration while the other parameters provide the rest. The
  1559. //! \e ulFlags parameter determines whether this is an IN endpoint
  1560. //! (USB_EP_HOST_IN or USB_EP_DEV_IN) or an OUT endpoint (USB_EP_HOST_OUT or
  1561. //! USB_EP_DEV_OUT), whether this is a Full speed endpoint (USB_EP_SPEED_FULL)
  1562. //! or a Low speed endpoint (USB_EP_SPEED_LOW).
  1563. //!
  1564. //! The \b USB_EP_MODE_ flags control the type of the endpoint.
  1565. //! - \b USB_EP_MODE_CTRL is a control endpoint.
  1566. //! - \b USB_EP_MODE_ISOC is an isochronous endpoint.
  1567. //! - \b USB_EP_MODE_BULK is a bulk endpoint.
  1568. //! - \b USB_EP_MODE_INT is an interrupt endpoint.
  1569. //!
  1570. //! The \e ulNAKPollInterval parameter has different meanings based on the
  1571. //! \b USB_EP_MODE value and whether or not this call is being made for
  1572. //! endpoint zero or another endpoint. For endpoint zero or any Bulk
  1573. //! endpoints, this value always indicates the number of frames to allow a
  1574. //! device to NAK before considering it a timeout. If this endpoint is an
  1575. //! isochronous or interrupt endpoint, this value is the polling interval for
  1576. //! this endpoint.
  1577. //!
  1578. //! For interrupt endpoints the polling interval is simply the number of
  1579. //! frames between polling an interrupt endpoint. For isochronous endpoints
  1580. //! this value represents a polling interval of 2 ^ (\e ulNAKPollInterval - 1)
  1581. //! frames. When used as a NAK timeout, the \e ulNAKPollInterval value
  1582. //! specifies 2 ^ (\e ulNAKPollInterval - 1) frames before issuing a time out.
  1583. //! There are two special time out values that can be specified when setting
  1584. //! the \e ulNAKPollInterval value. The first is \b MAX_NAK_LIMIT which is the
  1585. //! maximum value that can be passed in this variable. The other is
  1586. //! \b DISABLE_NAK_LIMIT which indicates that there should be no limit on the
  1587. //! number of NAKs.
  1588. //!
  1589. //! The \b USB_EP_DMA_MODE_ flags enables the type of DMA used to access the
  1590. //! endpoint's data FIFOs. The choice of the DMA mode depends on how the DMA
  1591. //! controller is configured and how it is being used. See the ``Using USB
  1592. //! with the uDMA Controller'' section for more information on DMA
  1593. //! configuration.
  1594. //!
  1595. //! When configuring the OUT portion of an endpoint, the \b USB_EP_AUTO_SET bit
  1596. //! is specified to cause the transmission of data on the USB bus to start
  1597. //! as soon as the number of bytes specified by \e ulMaxPayload have been
  1598. //! written into the OUT FIFO for this endpoint.
  1599. //!
  1600. //! When configuring the IN portion of an endpoint, the \b USB_EP_AUTO_REQUEST
  1601. //! bit can be specified to trigger the request for more data once the FIFO has
  1602. //! been drained enough to fit \e ulMaxPayload bytes. The \b USB_EP_AUTO_CLEAR
  1603. //! bit can be used to clear the data packet ready flag automatically once the
  1604. //! data has been read from the FIFO. If this is not used, this flag must be
  1605. //! manually cleared via a call to USBDevEndpointStatusClear() or
  1606. //! USBHostEndpointStatusClear().
  1607. //!
  1608. //! \note This function should only be called in host mode.
  1609. //!
  1610. //! \return None.
  1611. //
  1612. //*****************************************************************************
  1613. void
  1614. USBHostEndpointConfig(unsigned long ulBase, unsigned long ulEndpoint,
  1615. unsigned long ulMaxPayload,
  1616. unsigned long ulNAKPollInterval,
  1617. unsigned long ulTargetEndpoint, unsigned long ulFlags)
  1618. {
  1619. unsigned long ulRegister;
  1620. //
  1621. // Check the arguments.
  1622. //
  1623. ASSERT(ulBase == USB0_BASE);
  1624. ASSERT((ulEndpoint == USB_EP_0) || (ulEndpoint == USB_EP_1) ||
  1625. (ulEndpoint == USB_EP_2) || (ulEndpoint == USB_EP_3) ||
  1626. (ulEndpoint == USB_EP_4) || (ulEndpoint == USB_EP_5) ||
  1627. (ulEndpoint == USB_EP_6) || (ulEndpoint == USB_EP_7) ||
  1628. (ulEndpoint == USB_EP_8) || (ulEndpoint == USB_EP_9) ||
  1629. (ulEndpoint == USB_EP_10) || (ulEndpoint == USB_EP_11) ||
  1630. (ulEndpoint == USB_EP_12) || (ulEndpoint == USB_EP_13) ||
  1631. (ulEndpoint == USB_EP_14) || (ulEndpoint == USB_EP_15));
  1632. ASSERT(ulNAKPollInterval <= MAX_NAK_LIMIT);
  1633. //
  1634. // Endpoint zero is configured differently than the other endpoints, so see
  1635. // if this is endpoint zero.
  1636. //
  1637. if(ulEndpoint == USB_EP_0)
  1638. {
  1639. //
  1640. // Set the NAK timeout.
  1641. //
  1642. HWREGB(ulBase + USB_O_NAKLMT) = ulNAKPollInterval;
  1643. //
  1644. // Set the transfer type information.
  1645. //
  1646. HWREGB(ulBase + USB_O_TYPE0) =
  1647. ((ulFlags & USB_EP_SPEED_FULL) ? USB_TYPE0_SPEED_FULL :
  1648. USB_TYPE0_SPEED_LOW);
  1649. }
  1650. else
  1651. {
  1652. //
  1653. // Start with the target endpoint.
  1654. //
  1655. ulRegister = ulTargetEndpoint;
  1656. //
  1657. // Set the speed for the device using this endpoint.
  1658. //
  1659. if(ulFlags & USB_EP_SPEED_FULL)
  1660. {
  1661. ulRegister |= USB_TXTYPE1_SPEED_FULL;
  1662. }
  1663. else
  1664. {
  1665. ulRegister |= USB_TXTYPE1_SPEED_LOW;
  1666. }
  1667. //
  1668. // Set the protocol for the device using this endpoint.
  1669. //
  1670. switch(ulFlags & USB_EP_MODE_MASK)
  1671. {
  1672. //
  1673. // The bulk protocol is being used.
  1674. //
  1675. case USB_EP_MODE_BULK:
  1676. {
  1677. ulRegister |= USB_TXTYPE1_PROTO_BULK;
  1678. break;
  1679. }
  1680. //
  1681. // The isochronous protocol is being used.
  1682. //
  1683. case USB_EP_MODE_ISOC:
  1684. {
  1685. ulRegister |= USB_TXTYPE1_PROTO_ISOC;
  1686. break;
  1687. }
  1688. //
  1689. // The interrupt protocol is being used.
  1690. //
  1691. case USB_EP_MODE_INT:
  1692. {
  1693. ulRegister |= USB_TXTYPE1_PROTO_INT;
  1694. break;
  1695. }
  1696. //
  1697. // The control protocol is being used.
  1698. //
  1699. case USB_EP_MODE_CTRL:
  1700. {
  1701. ulRegister |= USB_TXTYPE1_PROTO_CTRL;
  1702. break;
  1703. }
  1704. }
  1705. //
  1706. // See if the transmit or receive endpoint is being configured.
  1707. //
  1708. if(ulFlags & USB_EP_HOST_OUT)
  1709. {
  1710. //
  1711. // Set the transfer type information.
  1712. //
  1713. HWREGB(ulBase + EP_OFFSET(ulEndpoint) + USB_O_TXTYPE1) =
  1714. ulRegister;
  1715. //
  1716. // Set the NAK timeout or polling interval.
  1717. //
  1718. HWREGB(ulBase + EP_OFFSET(ulEndpoint) + USB_O_TXINTERVAL1) =
  1719. ulNAKPollInterval;
  1720. //
  1721. // Set the Maximum Payload per transaction.
  1722. //
  1723. HWREGB(ulBase + EP_OFFSET(ulEndpoint) + USB_O_TXMAXP1) =
  1724. ulMaxPayload;
  1725. //
  1726. // Set the transmit control value to zero.
  1727. //
  1728. ulRegister = 0;
  1729. //
  1730. // Allow auto setting of TxPktRdy when max packet size has been
  1731. // loaded into the FIFO.
  1732. //
  1733. if(ulFlags & USB_EP_AUTO_SET)
  1734. {
  1735. ulRegister |= USB_TXCSRH1_AUTOSET;
  1736. }
  1737. //
  1738. // Configure the DMA Mode.
  1739. //
  1740. if(ulFlags & USB_EP_DMA_MODE_1)
  1741. {
  1742. ulRegister |= USB_TXCSRH1_DMAEN | USB_TXCSRH1_DMAMOD;
  1743. }
  1744. else if(ulFlags & USB_EP_DMA_MODE_0)
  1745. {
  1746. ulRegister |= USB_TXCSRH1_DMAEN;
  1747. }
  1748. //
  1749. // Write out the transmit control value.
  1750. //
  1751. HWREGB(ulBase + EP_OFFSET(ulEndpoint) + USB_O_TXCSRH1) =
  1752. (unsigned char)ulRegister;
  1753. }
  1754. else
  1755. {
  1756. //
  1757. // Set the transfer type information.
  1758. //
  1759. HWREGB(ulBase + EP_OFFSET(ulEndpoint) + USB_O_RXTYPE1) =
  1760. ulRegister;
  1761. //
  1762. // Set the NAK timeout or polling interval.
  1763. //
  1764. HWREGB(ulBase + EP_OFFSET(ulEndpoint) + USB_O_RXINTERVAL1) =
  1765. ulNAKPollInterval;
  1766. //
  1767. // Set the receive control value to zero.
  1768. //
  1769. ulRegister = 0;
  1770. //
  1771. // Allow auto clearing of RxPktRdy when packet of size max packet
  1772. // has been unloaded from the FIFO.
  1773. //
  1774. if(ulFlags & USB_EP_AUTO_CLEAR)
  1775. {
  1776. ulRegister |= USB_RXCSRH1_AUTOCL;
  1777. }
  1778. //
  1779. // Configure the DMA Mode.
  1780. //
  1781. if(ulFlags & USB_EP_DMA_MODE_1)
  1782. {
  1783. ulRegister |= USB_RXCSRH1_DMAEN | USB_RXCSRH1_DMAMOD;
  1784. }
  1785. else if(ulFlags & USB_EP_DMA_MODE_0)
  1786. {
  1787. ulRegister |= USB_RXCSRH1_DMAEN;
  1788. }
  1789. //
  1790. // Write out the receive control value.
  1791. //
  1792. HWREGB(ulBase + EP_OFFSET(ulEndpoint) + USB_O_RXCSRH1) =
  1793. (unsigned char)ulRegister;
  1794. }
  1795. }
  1796. }
  1797. //*****************************************************************************
  1798. //
  1799. //! Sets the configuration for an endpoint.
  1800. //!
  1801. //! \param ulBase specifies the USB module base address.
  1802. //! \param ulEndpoint is the endpoint to access.
  1803. //! \param ulMaxPacketSize is the maximum packet size for this endpoint.
  1804. //! \param ulFlags are used to configure other endpoint settings.
  1805. //!
  1806. //! This function will set the basic configuration for an endpoint in device
  1807. //! mode. Endpoint zero does not have a dynamic configuration, so this
  1808. //! function should not be called for endpoint zero. The \e ulFlags parameter
  1809. //! determines some of the configuration while the other parameters provide the
  1810. //! rest.
  1811. //!
  1812. //! The \b USB_EP_MODE_ flags define what the type is for the given endpoint.
  1813. //!
  1814. //! - \b USB_EP_MODE_CTRL is a control endpoint.
  1815. //! - \b USB_EP_MODE_ISOC is an isochronous endpoint.
  1816. //! - \b USB_EP_MODE_BULK is a bulk endpoint.
  1817. //! - \b USB_EP_MODE_INT is an interrupt endpoint.
  1818. //!
  1819. //! The \b USB_EP_DMA_MODE_ flags determines the type of DMA access to the
  1820. //! endpoint data FIFOs. The choice of the DMA mode depends on how the DMA
  1821. //! controller is configured and how it is being used. See the ``Using USB
  1822. //! with the uDMA Controller'' section for more information on DMA
  1823. //! configuration.
  1824. //!
  1825. //! When configuring an IN endpoint, the \b USB_EP_AUTO_SET bit can be
  1826. //! specified to cause the automatic transmission of data on the USB bus as
  1827. //! soon as \e ulMaxPacketSize bytes of data are written into the FIFO for
  1828. //! this endpoint. This is commonly used with DMA as no interaction is
  1829. //! required to start the transmission of data.
  1830. //!
  1831. //! When configuring an OUT endpoint, the \b USB_EP_AUTO_REQUEST bit is
  1832. //! specified to trigger the request for more data once the FIFO has been
  1833. //! drained enough to receive \e ulMaxPacketSize more bytes of data. Also for
  1834. //! OUT endpoints, the \b USB_EP_AUTO_CLEAR bit can be used to clear the data
  1835. //! packet ready flag automatically once the data has been read from the FIFO.
  1836. //! If this is not used, this flag must be manually cleared via a call to
  1837. //! USBDevEndpointStatusClear(). Both of these settings can be used to remove
  1838. //! the need for extra calls when using the controller in DMA mode.
  1839. //!
  1840. //! \note This function should only be called in device mode.
  1841. //!
  1842. //! \return None.
  1843. //
  1844. //*****************************************************************************
  1845. void
  1846. USBDevEndpointConfigSet(unsigned long ulBase, unsigned long ulEndpoint,
  1847. unsigned long ulMaxPacketSize, unsigned long ulFlags)
  1848. {
  1849. unsigned long ulRegister;
  1850. //
  1851. // Check the arguments.
  1852. //
  1853. ASSERT(ulBase == USB0_BASE);
  1854. ASSERT((ulEndpoint == USB_EP_1) || (ulEndpoint == USB_EP_2) ||
  1855. (ulEndpoint == USB_EP_3) || (ulEndpoint == USB_EP_4) ||
  1856. (ulEndpoint == USB_EP_5) || (ulEndpoint == USB_EP_6) ||
  1857. (ulEndpoint == USB_EP_7) || (ulEndpoint == USB_EP_8) ||
  1858. (ulEndpoint == USB_EP_9) || (ulEndpoint == USB_EP_10) ||
  1859. (ulEndpoint == USB_EP_11) || (ulEndpoint == USB_EP_12) ||
  1860. (ulEndpoint == USB_EP_13) || (ulEndpoint == USB_EP_14) ||
  1861. (ulEndpoint == USB_EP_15));
  1862. //
  1863. // Determine if a transmit or receive endpoint is being configured.
  1864. //
  1865. if(ulFlags & USB_EP_DEV_IN)
  1866. {
  1867. //
  1868. // Set the maximum packet size.
  1869. //
  1870. HWREGB(ulBase + EP_OFFSET(ulEndpoint) + USB_O_TXMAXP1) =
  1871. ulMaxPacketSize;
  1872. //
  1873. // The transmit control value is zero unless options are enabled.
  1874. //
  1875. ulRegister = 0;
  1876. //
  1877. // Allow auto setting of TxPktRdy when max packet size has been loaded
  1878. // into the FIFO.
  1879. //
  1880. if(ulFlags & USB_EP_AUTO_SET)
  1881. {
  1882. ulRegister |= USB_TXCSRH1_AUTOSET;
  1883. }
  1884. //
  1885. // Configure the DMA mode.
  1886. //
  1887. if(ulFlags & USB_EP_DMA_MODE_1)
  1888. {
  1889. ulRegister |= USB_TXCSRH1_DMAEN | USB_TXCSRH1_DMAMOD;
  1890. }
  1891. else if(ulFlags & USB_EP_DMA_MODE_0)
  1892. {
  1893. ulRegister |= USB_TXCSRH1_DMAEN;
  1894. }
  1895. //
  1896. // Enable isochronous mode if requested.
  1897. //
  1898. if((ulFlags & USB_EP_MODE_MASK) == USB_EP_MODE_ISOC)
  1899. {
  1900. ulRegister |= USB_TXCSRH1_ISO;
  1901. }
  1902. //
  1903. // Write the transmit control value.
  1904. //
  1905. HWREGB(ulBase + EP_OFFSET(ulEndpoint) + USB_O_TXCSRH1) =
  1906. (unsigned char)ulRegister;
  1907. //
  1908. // Reset the Data toggle to zero.
  1909. //
  1910. HWREGB(ulBase + EP_OFFSET(ulEndpoint) + USB_O_TXCSRL1) =
  1911. USB_TXCSRL1_CLRDT;
  1912. }
  1913. else
  1914. {
  1915. //
  1916. // Set the MaxPacketSize.
  1917. //
  1918. HWREGB(ulBase + EP_OFFSET(ulEndpoint) + USB_O_RXMAXP1) =
  1919. ulMaxPacketSize;
  1920. //
  1921. // The receive control value is zero unless options are enabled.
  1922. //
  1923. ulRegister = 0;
  1924. //
  1925. // Allow auto clearing of RxPktRdy when packet of size max packet
  1926. // has been unloaded from the FIFO.
  1927. //
  1928. if(ulFlags & USB_EP_AUTO_CLEAR)
  1929. {
  1930. ulRegister = USB_RXCSRH1_AUTOCL;
  1931. }
  1932. //
  1933. // Configure the DMA mode.
  1934. //
  1935. if(ulFlags & USB_EP_DMA_MODE_1)
  1936. {
  1937. ulRegister |= USB_RXCSRH1_DMAEN | USB_RXCSRH1_DMAMOD;
  1938. }
  1939. else if(ulFlags & USB_EP_DMA_MODE_0)
  1940. {
  1941. ulRegister |= USB_RXCSRH1_DMAEN;
  1942. }
  1943. //
  1944. // Enable isochronous mode if requested.
  1945. //
  1946. if((ulFlags & USB_EP_MODE_MASK) == USB_EP_MODE_ISOC)
  1947. {
  1948. ulRegister |= USB_RXCSRH1_ISO;
  1949. }
  1950. //
  1951. // Write the receive control value.
  1952. //
  1953. HWREGB(ulBase + EP_OFFSET(ulEndpoint) + USB_O_RXCSRH1) =
  1954. (unsigned char)ulRegister;
  1955. //
  1956. // Reset the Data toggle to zero.
  1957. //
  1958. HWREGB(ulBase + EP_OFFSET(ulEndpoint) + USB_O_RXCSRL1) =
  1959. USB_RXCSRL1_CLRDT;
  1960. }
  1961. }
  1962. //*****************************************************************************
  1963. //
  1964. //! Gets the current configuration for an endpoint.
  1965. //!
  1966. //! \param ulBase specifies the USB module base address.
  1967. //! \param ulEndpoint is the endpoint to access.
  1968. //! \param pulMaxPacketSize is a pointer which is written with the maximum
  1969. //! packet size for this endpoint.
  1970. //! \param pulFlags is a pointer which is written with the current endpoint
  1971. //! settings. On entry to the function, this pointer must contain either
  1972. //! \b USB_EP_DEV_IN or \b USB_EP_DEV_OUT to indicate whether the IN or OUT
  1973. //! endpoint is to be queried.
  1974. //!
  1975. //! This function will return the basic configuration for an endpoint in device
  1976. //! mode. The values returned in \e *pulMaxPacketSize and \e *pulFlags are
  1977. //! equivalent to the \e ulMaxPacketSize and \e ulFlags previously passed to
  1978. //! USBDevEndpointConfigSet() for this endpoint.
  1979. //!
  1980. //! \note This function should only be called in device mode.
  1981. //!
  1982. //! \return None.
  1983. //
  1984. //*****************************************************************************
  1985. void
  1986. USBDevEndpointConfigGet(unsigned long ulBase, unsigned long ulEndpoint,
  1987. unsigned long *pulMaxPacketSize,
  1988. unsigned long *pulFlags)
  1989. {
  1990. unsigned long ulRegister;
  1991. //
  1992. // Check the arguments.
  1993. //
  1994. ASSERT(ulBase == USB0_BASE);
  1995. ASSERT(pulMaxPacketSize && pulFlags);
  1996. ASSERT((ulEndpoint == USB_EP_1) || (ulEndpoint == USB_EP_2) ||
  1997. (ulEndpoint == USB_EP_3) || (ulEndpoint == USB_EP_4) ||
  1998. (ulEndpoint == USB_EP_5) || (ulEndpoint == USB_EP_6) ||
  1999. (ulEndpoint == USB_EP_7) || (ulEndpoint == USB_EP_8) ||
  2000. (ulEndpoint == USB_EP_9) || (ulEndpoint == USB_EP_10) ||
  2001. (ulEndpoint == USB_EP_11) || (ulEndpoint == USB_EP_12) ||
  2002. (ulEndpoint == USB_EP_13) || (ulEndpoint == USB_EP_14) ||
  2003. (ulEndpoint == USB_EP_15));
  2004. //
  2005. // Determine if a transmit or receive endpoint is being queried.
  2006. //
  2007. if(*pulFlags & USB_EP_DEV_IN)
  2008. {
  2009. //
  2010. // Clear the flags other than the direction bit.
  2011. //
  2012. *pulFlags = USB_EP_DEV_IN;
  2013. //
  2014. // Get the maximum packet size.
  2015. //
  2016. *pulMaxPacketSize = (unsigned long)HWREGB(ulBase +
  2017. EP_OFFSET(ulEndpoint) +
  2018. USB_O_TXMAXP1);
  2019. //
  2020. // Get the current transmit control register value.
  2021. //
  2022. ulRegister = (unsigned long)HWREGB(ulBase + EP_OFFSET(ulEndpoint) +
  2023. USB_O_TXCSRH1);
  2024. //
  2025. // Are we allowing auto setting of TxPktRdy when max packet size has
  2026. // been loaded into the FIFO?
  2027. //
  2028. if(ulRegister & USB_TXCSRH1_AUTOSET)
  2029. {
  2030. *pulFlags |= USB_EP_AUTO_SET;
  2031. }
  2032. //
  2033. // Get the DMA mode.
  2034. //
  2035. if(ulRegister & USB_TXCSRH1_DMAEN)
  2036. {
  2037. if(ulRegister & USB_TXCSRH1_DMAMOD)
  2038. {
  2039. *pulFlags |= USB_EP_DMA_MODE_1;
  2040. }
  2041. else
  2042. {
  2043. *pulFlags |= USB_EP_DMA_MODE_0;
  2044. }
  2045. }
  2046. //
  2047. // Are we in isochronous mode?
  2048. //
  2049. if(ulRegister & USB_TXCSRH1_ISO)
  2050. {
  2051. *pulFlags |= USB_EP_MODE_ISOC;
  2052. }
  2053. else
  2054. {
  2055. //
  2056. // The hardware doesn't differentiate between bulk, interrupt
  2057. // and control mode for the endpoint so we just set something
  2058. // that isn't isochronous. This ensures that anyone modifying
  2059. // the returned flags in preparation for a call to
  2060. // USBDevEndpointConfigSet will not see an unexpected mode change.
  2061. // If they decode the returned mode, however, they may be in for
  2062. // a surprise.
  2063. //
  2064. *pulFlags |= USB_EP_MODE_BULK;
  2065. }
  2066. }
  2067. else
  2068. {
  2069. //
  2070. // Clear the flags other than the direction bit.
  2071. //
  2072. *pulFlags = USB_EP_DEV_OUT;
  2073. //
  2074. // Get the MaxPacketSize.
  2075. //
  2076. *pulMaxPacketSize = (unsigned long)HWREGB(ulBase +
  2077. EP_OFFSET(ulEndpoint) +
  2078. USB_O_RXMAXP1);
  2079. //
  2080. // Get the current receive control register value.
  2081. //
  2082. ulRegister = (unsigned long)HWREGB(ulBase + EP_OFFSET(ulEndpoint) +
  2083. USB_O_RXCSRH1);
  2084. //
  2085. // Are we allowing auto clearing of RxPktRdy when packet of size max
  2086. // packet has been unloaded from the FIFO?
  2087. //
  2088. if(ulRegister & USB_RXCSRH1_AUTOCL)
  2089. {
  2090. *pulFlags |= USB_EP_AUTO_CLEAR;
  2091. }
  2092. //
  2093. // Get the DMA mode.
  2094. //
  2095. if(ulRegister & USB_RXCSRH1_DMAEN)
  2096. {
  2097. if(ulRegister & USB_RXCSRH1_DMAMOD)
  2098. {
  2099. *pulFlags |= USB_EP_DMA_MODE_1;
  2100. }
  2101. else
  2102. {
  2103. *pulFlags |= USB_EP_DMA_MODE_0;
  2104. }
  2105. }
  2106. //
  2107. // Are we in isochronous mode?
  2108. //
  2109. if(ulRegister & USB_RXCSRH1_ISO)
  2110. {
  2111. *pulFlags |= USB_EP_MODE_ISOC;
  2112. }
  2113. else
  2114. {
  2115. //
  2116. // The hardware doesn't differentiate between bulk, interrupt
  2117. // and control mode for the endpoint so we just set something
  2118. // that isn't isochronous. This ensures that anyone modifying
  2119. // the returned flags in preparation for a call to
  2120. // USBDevEndpointConfigSet will not see an unexpected mode change.
  2121. // If they decode the returned mode, however, they may be in for
  2122. // a surprise.
  2123. //
  2124. *pulFlags |= USB_EP_MODE_BULK;
  2125. }
  2126. }
  2127. }
  2128. //*****************************************************************************
  2129. //
  2130. //! Sets the FIFO configuration for an endpoint.
  2131. //!
  2132. //! \param ulBase specifies the USB module base address.
  2133. //! \param ulEndpoint is the endpoint to access.
  2134. //! \param ulFIFOAddress is the starting address for the FIFO.
  2135. //! \param ulFIFOSize is the size of the FIFO specified by one of the
  2136. //! USB_FIFO_SZ_ values.
  2137. //! \param ulFlags specifies what information to set in the FIFO configuration.
  2138. //!
  2139. //! This function will set the starting FIFO RAM address and size of the FIFO
  2140. //! for a given endpoint. Endpoint zero does not have a dynamically
  2141. //! configurable FIFO so this function should not be called for endpoint zero.
  2142. //! The \e ulFIFOSize parameter should be one of the values in the
  2143. //! \b USB_FIFO_SZ_ values. If the endpoint is going to use double buffering
  2144. //! it should use the values with the \b _DB at the end of the value. For
  2145. //! example, use \b USB_FIFO_SZ_16_DB to configure an endpoint to have a 16
  2146. //! byte double buffered FIFO. If a double buffered FIFO is used, then the
  2147. //! actual size of the FIFO is twice the size indicated by the \e ulFIFOSize
  2148. //! parameter. This means that the \b USB_FIFO_SZ_16_DB value will use 32
  2149. //! bytes of the USB controller's FIFO memory.
  2150. //!
  2151. //! The \e ulFIFOAddress value should be a multiple of 8 bytes and directly
  2152. //! indicates the starting address in the USB controller's FIFO RAM. For
  2153. //! example, a value of 64 indicates that the FIFO should start 64 bytes into
  2154. //! the USB controller's FIFO memory. The \e ulFlags value specifies whether
  2155. //! the endpoint's OUT or IN FIFO should be configured. If in host mode, use
  2156. //! \b USB_EP_HOST_OUT or \b USB_EP_HOST_IN, and if in device mode use
  2157. //! \b USB_EP_DEV_OUT or \b USB_EP_DEV_IN.
  2158. //!
  2159. //! \return None.
  2160. //
  2161. //*****************************************************************************
  2162. void
  2163. USBFIFOConfigSet(unsigned long ulBase, unsigned long ulEndpoint,
  2164. unsigned long ulFIFOAddress, unsigned long ulFIFOSize,
  2165. unsigned long ulFlags)
  2166. {
  2167. //
  2168. // Check the arguments.
  2169. //
  2170. ASSERT(ulBase == USB0_BASE);
  2171. ASSERT((ulEndpoint == USB_EP_1) || (ulEndpoint == USB_EP_2) ||
  2172. (ulEndpoint == USB_EP_3) || (ulEndpoint == USB_EP_4) ||
  2173. (ulEndpoint == USB_EP_5) || (ulEndpoint == USB_EP_6) ||
  2174. (ulEndpoint == USB_EP_7) || (ulEndpoint == USB_EP_8) ||
  2175. (ulEndpoint == USB_EP_9) || (ulEndpoint == USB_EP_10) ||
  2176. (ulEndpoint == USB_EP_11) || (ulEndpoint == USB_EP_12) ||
  2177. (ulEndpoint == USB_EP_13) || (ulEndpoint == USB_EP_14) ||
  2178. (ulEndpoint == USB_EP_15));
  2179. //
  2180. // See if the transmit or receive FIFO is being configured.
  2181. //
  2182. if(ulFlags & (USB_EP_HOST_OUT | USB_EP_DEV_IN))
  2183. {
  2184. //
  2185. // Set the transmit FIFO location and size for this endpoint.
  2186. //
  2187. USBIndexWrite(ulBase, ulEndpoint >> 4, USB_O_TXFIFOSZ, ulFIFOSize, 1);
  2188. USBIndexWrite(ulBase, ulEndpoint >> 4, USB_O_TXFIFOADD,
  2189. ulFIFOAddress >> 3, 2);
  2190. }
  2191. else
  2192. {
  2193. //
  2194. // Set the receive FIFO location and size for this endpoint.
  2195. //
  2196. USBIndexWrite(ulBase, ulEndpoint >> 4, USB_O_RXFIFOSZ, ulFIFOSize, 1);
  2197. USBIndexWrite(ulBase, ulEndpoint >> 4, USB_O_RXFIFOADD,
  2198. ulFIFOAddress >> 3, 2);
  2199. }
  2200. }
  2201. //*****************************************************************************
  2202. //
  2203. //! Returns the FIFO configuration for an endpoint.
  2204. //!
  2205. //! \param ulBase specifies the USB module base address.
  2206. //! \param ulEndpoint is the endpoint to access.
  2207. //! \param pulFIFOAddress is the starting address for the FIFO.
  2208. //! \param pulFIFOSize is the size of the FIFO as specified by one of the
  2209. //! USB_FIFO_SZ_ values.
  2210. //! \param ulFlags specifies what information to retrieve from the FIFO
  2211. //! configuration.
  2212. //!
  2213. //! This function will return the starting address and size of the FIFO for a
  2214. //! given endpoint. Endpoint zero does not have a dynamically configurable
  2215. //! FIFO so this function should not be called for endpoint zero. The
  2216. //! \e ulFlags parameter specifies whether the endpoint's OUT or IN FIFO should
  2217. //! be read. If in host mode, the \e ulFlags parameter should be
  2218. //! \b USB_EP_HOST_OUT or \b USB_EP_HOST_IN, and if in device mode the
  2219. //! \e ulFlags parameter should be either \b USB_EP_DEV_OUT or
  2220. //! \b USB_EP_DEV_IN.
  2221. //!
  2222. //! \return None.
  2223. //
  2224. //*****************************************************************************
  2225. void
  2226. USBFIFOConfigGet(unsigned long ulBase, unsigned long ulEndpoint,
  2227. unsigned long *pulFIFOAddress, unsigned long *pulFIFOSize,
  2228. unsigned long ulFlags)
  2229. {
  2230. //
  2231. // Check the arguments.
  2232. //
  2233. ASSERT(ulBase == USB0_BASE);
  2234. ASSERT((ulEndpoint == USB_EP_1) || (ulEndpoint == USB_EP_2) ||
  2235. (ulEndpoint == USB_EP_3) || (ulEndpoint == USB_EP_4) ||
  2236. (ulEndpoint == USB_EP_5) || (ulEndpoint == USB_EP_6) ||
  2237. (ulEndpoint == USB_EP_7) || (ulEndpoint == USB_EP_8) ||
  2238. (ulEndpoint == USB_EP_9) || (ulEndpoint == USB_EP_10) ||
  2239. (ulEndpoint == USB_EP_11) || (ulEndpoint == USB_EP_12) ||
  2240. (ulEndpoint == USB_EP_13) || (ulEndpoint == USB_EP_14) ||
  2241. (ulEndpoint == USB_EP_15));
  2242. //
  2243. // See if the transmit or receive FIFO is being configured.
  2244. //
  2245. if(ulFlags & (USB_EP_HOST_OUT | USB_EP_DEV_IN))
  2246. {
  2247. //
  2248. // Get the transmit FIFO location and size for this endpoint.
  2249. //
  2250. *pulFIFOAddress = (USBIndexRead(ulBase, ulEndpoint >> 4,
  2251. (unsigned long)USB_O_TXFIFOADD,
  2252. 2)) << 3;
  2253. *pulFIFOSize = USBIndexRead(ulBase, ulEndpoint >> 4,
  2254. (unsigned long)USB_O_TXFIFOSZ, 1);
  2255. }
  2256. else
  2257. {
  2258. //
  2259. // Get the receive FIFO location and size for this endpoint.
  2260. //
  2261. *pulFIFOAddress = (USBIndexRead(ulBase, ulEndpoint >> 4,
  2262. (unsigned long)USB_O_RXFIFOADD,
  2263. 2)) << 3;
  2264. *pulFIFOSize = USBIndexRead(ulBase, ulEndpoint >> 4,
  2265. (unsigned long)USB_O_RXFIFOSZ, 1);
  2266. }
  2267. }
  2268. //*****************************************************************************
  2269. //
  2270. //! Enable DMA on a given endpoint.
  2271. //!
  2272. //! \param ulBase specifies the USB module base address.
  2273. //! \param ulEndpoint is the endpoint to access.
  2274. //! \param ulFlags specifies which direction and what mode to use when enabling
  2275. //! DMA.
  2276. //!
  2277. //! This function will enable DMA on a given endpoint and set the mode
  2278. //! according to the values in the \e ulFlags parameter. The \e ulFlags
  2279. //! parameter should have \b USB_EP_DEV_IN or \b USB_EP_DEV_OUT set.
  2280. //!
  2281. //! \return None.
  2282. //
  2283. //*****************************************************************************
  2284. void
  2285. USBEndpointDMAEnable(unsigned long ulBase, unsigned long ulEndpoint,
  2286. unsigned long ulFlags)
  2287. {
  2288. //
  2289. // See if the transmit DMA is being enabled.
  2290. //
  2291. if(ulFlags & USB_EP_DEV_IN)
  2292. {
  2293. //
  2294. // Enable DMA on the transmit end point.
  2295. //
  2296. HWREGB(ulBase + EP_OFFSET(ulEndpoint) + USB_O_TXCSRH1) |=
  2297. USB_TXCSRH1_DMAEN;
  2298. }
  2299. else
  2300. {
  2301. //
  2302. // Enable DMA on the receive end point.
  2303. //
  2304. HWREGB(ulBase + EP_OFFSET(ulEndpoint) + USB_O_RXCSRH1) |=
  2305. USB_RXCSRH1_DMAEN;
  2306. }
  2307. }
  2308. //*****************************************************************************
  2309. //
  2310. //! Disable DMA on a given endpoint.
  2311. //!
  2312. //! \param ulBase specifies the USB module base address.
  2313. //! \param ulEndpoint is the endpoint to access.
  2314. //! \param ulFlags specifies which direction to disable.
  2315. //!
  2316. //! This function will disable DMA on a given end point to allow non-DMA
  2317. //! USB transactions to generate interrupts normally. The ulFlags should be
  2318. //! \b USB_EP_DEV_IN or \b USB_EP_DEV_OUT all other bits are ignored.
  2319. //!
  2320. //! \return None.
  2321. //
  2322. //*****************************************************************************
  2323. void
  2324. USBEndpointDMADisable(unsigned long ulBase, unsigned long ulEndpoint,
  2325. unsigned long ulFlags)
  2326. {
  2327. //
  2328. // If this was a request to disable DMA on the IN portion of the end point
  2329. // then handle it.
  2330. //
  2331. if(ulFlags & USB_EP_DEV_IN)
  2332. {
  2333. //
  2334. // Just disable DMA leave the mode setting.
  2335. //
  2336. HWREGB(ulBase + EP_OFFSET(ulEndpoint) + USB_O_TXCSRH1) &=
  2337. ~USB_TXCSRH1_DMAEN;
  2338. }
  2339. else
  2340. {
  2341. //
  2342. // Just disable DMA leave the mode setting.
  2343. //
  2344. HWREGB(ulBase + EP_OFFSET(ulEndpoint) + USB_O_RXCSRH1) &=
  2345. ~USB_RXCSRH1_DMAEN;
  2346. }
  2347. }
  2348. //*****************************************************************************
  2349. //
  2350. //! Determine the number of bytes of data available in a given endpoint's FIFO.
  2351. //!
  2352. //! \param ulBase specifies the USB module base address.
  2353. //! \param ulEndpoint is the endpoint to access.
  2354. //!
  2355. //! This function will return the number of bytes of data currently available
  2356. //! in the FIFO for the given receive (OUT) endpoint. It may be used prior to
  2357. //! calling USBEndpointDataGet() to determine the size of buffer required to
  2358. //! hold the newly-received packet.
  2359. //!
  2360. //! \return This call will return the number of bytes available in a given
  2361. //! endpoint FIFO.
  2362. //
  2363. //*****************************************************************************
  2364. unsigned long
  2365. USBEndpointDataAvail(unsigned long ulBase, unsigned long ulEndpoint)
  2366. {
  2367. unsigned long ulRegister;
  2368. //
  2369. // Check the arguments.
  2370. //
  2371. ASSERT(ulBase == USB0_BASE);
  2372. ASSERT((ulEndpoint == USB_EP_0) || (ulEndpoint == USB_EP_1) ||
  2373. (ulEndpoint == USB_EP_2) || (ulEndpoint == USB_EP_3) ||
  2374. (ulEndpoint == USB_EP_4) || (ulEndpoint == USB_EP_5) ||
  2375. (ulEndpoint == USB_EP_6) || (ulEndpoint == USB_EP_7) ||
  2376. (ulEndpoint == USB_EP_8) || (ulEndpoint == USB_EP_9) ||
  2377. (ulEndpoint == USB_EP_10) || (ulEndpoint == USB_EP_11) ||
  2378. (ulEndpoint == USB_EP_12) || (ulEndpoint == USB_EP_13) ||
  2379. (ulEndpoint == USB_EP_14) || (ulEndpoint == USB_EP_15));
  2380. //
  2381. // Get the address of the receive status register to use, based on the
  2382. // endpoint.
  2383. //
  2384. if(ulEndpoint == USB_EP_0)
  2385. {
  2386. ulRegister = USB_O_CSRL0;
  2387. }
  2388. else
  2389. {
  2390. ulRegister = USB_O_RXCSRL1 + EP_OFFSET(ulEndpoint);
  2391. }
  2392. //
  2393. // Is there a packet ready in the FIFO?
  2394. //
  2395. if((HWREGH(ulBase + ulRegister) & USB_CSRL0_RXRDY) == 0)
  2396. {
  2397. return(0);
  2398. }
  2399. //
  2400. // Return the byte count in the FIFO.
  2401. //
  2402. return(HWREGH(ulBase + USB_O_COUNT0 + ulEndpoint));
  2403. }
  2404. //*****************************************************************************
  2405. //
  2406. //! Retrieves data from the given endpoint's FIFO.
  2407. //!
  2408. //! \param ulBase specifies the USB module base address.
  2409. //! \param ulEndpoint is the endpoint to access.
  2410. //! \param pucData is a pointer to the data area used to return the data from
  2411. //! the FIFO.
  2412. //! \param pulSize is initially the size of the buffer passed into this call
  2413. //! via the \e pucData parameter. It is set to the amount of data returned in
  2414. //! the buffer.
  2415. //!
  2416. //! This function will return the data from the FIFO for the given endpoint.
  2417. //! The \e pulSize parameter should indicate the size of the buffer passed in
  2418. //! the \e pulData parameter. The data in the \e pulSize parameter is changed
  2419. //! to match the amount of data returned in the \e pucData parameter. If a
  2420. //! zero byte packet was received this call will not return a error but will
  2421. //! instead just return a zero in the \e pulSize parameter. The only error
  2422. //! case occurs when there is no data packet available.
  2423. //!
  2424. //! \return This call will return 0, or -1 if no packet was received.
  2425. //
  2426. //*****************************************************************************
  2427. long
  2428. USBEndpointDataGet(unsigned long ulBase, unsigned long ulEndpoint,
  2429. unsigned char *pucData, unsigned long *pulSize)
  2430. {
  2431. unsigned long ulRegister, ulByteCount, ulFIFO;
  2432. //
  2433. // Check the arguments.
  2434. //
  2435. ASSERT(ulBase == USB0_BASE);
  2436. ASSERT((ulEndpoint == USB_EP_0) || (ulEndpoint == USB_EP_1) ||
  2437. (ulEndpoint == USB_EP_2) || (ulEndpoint == USB_EP_3) ||
  2438. (ulEndpoint == USB_EP_4) || (ulEndpoint == USB_EP_5) ||
  2439. (ulEndpoint == USB_EP_6) || (ulEndpoint == USB_EP_7) ||
  2440. (ulEndpoint == USB_EP_8) || (ulEndpoint == USB_EP_9) ||
  2441. (ulEndpoint == USB_EP_10) || (ulEndpoint == USB_EP_11) ||
  2442. (ulEndpoint == USB_EP_12) || (ulEndpoint == USB_EP_13) ||
  2443. (ulEndpoint == USB_EP_14) || (ulEndpoint == USB_EP_15));
  2444. //
  2445. // Get the address of the receive status register to use, based on the
  2446. // endpoint.
  2447. //
  2448. if(ulEndpoint == USB_EP_0)
  2449. {
  2450. ulRegister = USB_O_CSRL0;
  2451. }
  2452. else
  2453. {
  2454. ulRegister = USB_O_RXCSRL1 + EP_OFFSET(ulEndpoint);
  2455. }
  2456. //
  2457. // Don't allow reading of data if the RxPktRdy bit is not set.
  2458. //
  2459. if((HWREGH(ulBase + ulRegister) & USB_CSRL0_RXRDY) == 0)
  2460. {
  2461. //
  2462. // Can't read the data because none is available.
  2463. //
  2464. *pulSize = 0;
  2465. //
  2466. // Return a failure since there is no data to read.
  2467. //
  2468. return(-1);
  2469. }
  2470. //
  2471. // Get the byte count in the FIFO.
  2472. //
  2473. ulByteCount = HWREGH(ulBase + USB_O_COUNT0 + ulEndpoint);
  2474. //
  2475. // Determine how many bytes we will actually copy.
  2476. //
  2477. ulByteCount = (ulByteCount < *pulSize) ? ulByteCount : *pulSize;
  2478. //
  2479. // Return the number of bytes we are going to read.
  2480. //
  2481. *pulSize = ulByteCount;
  2482. //
  2483. // Calculate the FIFO address.
  2484. //
  2485. ulFIFO = ulBase + USB_O_FIFO0 + (ulEndpoint >> 2);
  2486. //
  2487. // Read the data out of the FIFO.
  2488. //
  2489. for(; ulByteCount > 0; ulByteCount--)
  2490. {
  2491. //
  2492. // Read a byte at a time from the FIFO.
  2493. //
  2494. *pucData++ = HWREGB(ulFIFO);
  2495. }
  2496. //
  2497. // Success.
  2498. //
  2499. return(0);
  2500. }
  2501. //*****************************************************************************
  2502. //
  2503. //! Acknowledge that data was read from the given endpoint's FIFO in device
  2504. //! mode.
  2505. //!
  2506. //! \param ulBase specifies the USB module base address.
  2507. //! \param ulEndpoint is the endpoint to access.
  2508. //! \param bIsLastPacket indicates if this is the last packet.
  2509. //!
  2510. //! This function acknowledges that the data was read from the endpoint's FIFO.
  2511. //! The \e bIsLastPacket parameter is set to a \b true value if this is the
  2512. //! last in a series of data packets on endpoint zero. The \e bIsLastPacket
  2513. //! parameter is not used for endpoints other than endpoint zero. This call
  2514. //! can be used if processing is required between reading the data and
  2515. //! acknowledging that the data has been read.
  2516. //!
  2517. //! \note This function should only be called in device mode.
  2518. //!
  2519. //! \return None.
  2520. //
  2521. //*****************************************************************************
  2522. void
  2523. USBDevEndpointDataAck(unsigned long ulBase, unsigned long ulEndpoint,
  2524. tBoolean bIsLastPacket)
  2525. {
  2526. //
  2527. // Check the arguments.
  2528. //
  2529. ASSERT(ulBase == USB0_BASE);
  2530. ASSERT((ulEndpoint == USB_EP_0) || (ulEndpoint == USB_EP_1) ||
  2531. (ulEndpoint == USB_EP_2) || (ulEndpoint == USB_EP_3) ||
  2532. (ulEndpoint == USB_EP_4) || (ulEndpoint == USB_EP_5) ||
  2533. (ulEndpoint == USB_EP_6) || (ulEndpoint == USB_EP_7) ||
  2534. (ulEndpoint == USB_EP_8) || (ulEndpoint == USB_EP_9) ||
  2535. (ulEndpoint == USB_EP_10) || (ulEndpoint == USB_EP_11) ||
  2536. (ulEndpoint == USB_EP_12) || (ulEndpoint == USB_EP_13) ||
  2537. (ulEndpoint == USB_EP_14) || (ulEndpoint == USB_EP_15));
  2538. //
  2539. // Determine which endpoint is being acked.
  2540. //
  2541. if(ulEndpoint == USB_EP_0)
  2542. {
  2543. //
  2544. // Clear RxPktRdy, and optionally DataEnd, on endpoint zero.
  2545. //
  2546. HWREGB(ulBase + USB_O_CSRL0) =
  2547. USB_CSRL0_RXRDYC | (bIsLastPacket ? USB_CSRL0_DATAEND : 0);
  2548. }
  2549. else
  2550. {
  2551. //
  2552. // Clear RxPktRdy on all other endpoints.
  2553. //
  2554. HWREGB(ulBase + USB_O_RXCSRL1 + EP_OFFSET(ulEndpoint)) &=
  2555. ~(USB_RXCSRL1_RXRDY);
  2556. }
  2557. }
  2558. //*****************************************************************************
  2559. //
  2560. //! Acknowledge that data was read from the given endpoint's FIFO in host
  2561. //! mode.
  2562. //!
  2563. //! \param ulBase specifies the USB module base address.
  2564. //! \param ulEndpoint is the endpoint to access.
  2565. //!
  2566. //! This function acknowledges that the data was read from the endpoint's FIFO.
  2567. //! This call is used if processing is required between reading the data and
  2568. //! acknowledging that the data has been read.
  2569. //!
  2570. //! \note This function should only be called in host mode.
  2571. //!
  2572. //! \return None.
  2573. //
  2574. //*****************************************************************************
  2575. void
  2576. USBHostEndpointDataAck(unsigned long ulBase, unsigned long ulEndpoint)
  2577. {
  2578. //
  2579. // Check the arguments.
  2580. //
  2581. ASSERT(ulBase == USB0_BASE);
  2582. ASSERT((ulEndpoint == USB_EP_0) || (ulEndpoint == USB_EP_1) ||
  2583. (ulEndpoint == USB_EP_2) || (ulEndpoint == USB_EP_3) ||
  2584. (ulEndpoint == USB_EP_4) || (ulEndpoint == USB_EP_5) ||
  2585. (ulEndpoint == USB_EP_6) || (ulEndpoint == USB_EP_7) ||
  2586. (ulEndpoint == USB_EP_8) || (ulEndpoint == USB_EP_9) ||
  2587. (ulEndpoint == USB_EP_10) || (ulEndpoint == USB_EP_11) ||
  2588. (ulEndpoint == USB_EP_12) || (ulEndpoint == USB_EP_13) ||
  2589. (ulEndpoint == USB_EP_14) || (ulEndpoint == USB_EP_15));
  2590. //
  2591. // Clear RxPktRdy.
  2592. //
  2593. if(ulEndpoint == USB_EP_0)
  2594. {
  2595. HWREGB(ulBase + USB_O_CSRL0) &= ~USB_CSRL0_RXRDY;
  2596. }
  2597. else
  2598. {
  2599. HWREGB(ulBase + USB_O_RXCSRL1 + EP_OFFSET(ulEndpoint)) &=
  2600. ~(USB_RXCSRL1_RXRDY);
  2601. }
  2602. }
  2603. //*****************************************************************************
  2604. //
  2605. //! Puts data into the given endpoint's FIFO.
  2606. //!
  2607. //! \param ulBase specifies the USB module base address.
  2608. //! \param ulEndpoint is the endpoint to access.
  2609. //! \param pucData is a pointer to the data area used as the source for the
  2610. //! data to put into the FIFO.
  2611. //! \param ulSize is the amount of data to put into the FIFO.
  2612. //!
  2613. //! This function will put the data from the \e pucData parameter into the FIFO
  2614. //! for this endpoint. If a packet is already pending for transmission then
  2615. //! this call will not put any of the data into the FIFO and will return -1.
  2616. //! Care should be taken to not write more data than can fit into the FIFO
  2617. //! allocated by the call to USBFIFOConfigSet().
  2618. //!
  2619. //! \return This call will return 0 on success, or -1 to indicate that the FIFO
  2620. //! is in use and cannot be written.
  2621. //
  2622. //*****************************************************************************
  2623. long
  2624. USBEndpointDataPut(unsigned long ulBase, unsigned long ulEndpoint,
  2625. unsigned char *pucData, unsigned long ulSize)
  2626. {
  2627. unsigned long ulFIFO;
  2628. unsigned char ucTxPktRdy;
  2629. //
  2630. // Check the arguments.
  2631. //
  2632. ASSERT(ulBase == USB0_BASE);
  2633. ASSERT((ulEndpoint == USB_EP_0) || (ulEndpoint == USB_EP_1) ||
  2634. (ulEndpoint == USB_EP_2) || (ulEndpoint == USB_EP_3) ||
  2635. (ulEndpoint == USB_EP_4) || (ulEndpoint == USB_EP_5) ||
  2636. (ulEndpoint == USB_EP_6) || (ulEndpoint == USB_EP_7) ||
  2637. (ulEndpoint == USB_EP_8) || (ulEndpoint == USB_EP_9) ||
  2638. (ulEndpoint == USB_EP_10) || (ulEndpoint == USB_EP_11) ||
  2639. (ulEndpoint == USB_EP_12) || (ulEndpoint == USB_EP_13) ||
  2640. (ulEndpoint == USB_EP_14) || (ulEndpoint == USB_EP_15));
  2641. //
  2642. // Get the bit position of TxPktRdy based on the endpoint.
  2643. //
  2644. if(ulEndpoint == USB_EP_0)
  2645. {
  2646. ucTxPktRdy = USB_CSRL0_TXRDY;
  2647. }
  2648. else
  2649. {
  2650. ucTxPktRdy = USB_TXCSRL1_TXRDY;
  2651. }
  2652. //
  2653. // Don't allow transmit of data if the TxPktRdy bit is already set.
  2654. //
  2655. if(HWREGB(ulBase + USB_O_CSRL0 + ulEndpoint) & ucTxPktRdy)
  2656. {
  2657. return(-1);
  2658. }
  2659. //
  2660. // Calculate the FIFO address.
  2661. //
  2662. ulFIFO = ulBase + USB_O_FIFO0 + (ulEndpoint >> 2);
  2663. //
  2664. // Write the data to the FIFO.
  2665. //
  2666. for(; ulSize > 0; ulSize--)
  2667. {
  2668. HWREGB(ulFIFO) = *pucData++;
  2669. }
  2670. //
  2671. // Success.
  2672. //
  2673. return(0);
  2674. }
  2675. //*****************************************************************************
  2676. //
  2677. //! Starts the transfer of data from an endpoint's FIFO.
  2678. //!
  2679. //! \param ulBase specifies the USB module base address.
  2680. //! \param ulEndpoint is the endpoint to access.
  2681. //! \param ulTransType is set to indicate what type of data is being sent.
  2682. //!
  2683. //! This function will start the transfer of data from the FIFO for a given
  2684. //! endpoint. This is necessary if the \b USB_EP_AUTO_SET bit was not enabled
  2685. //! for the endpoint. Setting the \e ulTransType parameter will allow the
  2686. //! appropriate signaling on the USB bus for the type of transaction being
  2687. //! requested. The \e ulTransType parameter should be one of the following:
  2688. //!
  2689. //! - USB_TRANS_OUT for OUT transaction on any endpoint in host mode.
  2690. //! - USB_TRANS_IN for IN transaction on any endpoint in device mode.
  2691. //! - USB_TRANS_IN_LAST for the last IN transactions on endpoint zero in a
  2692. //! sequence of IN transactions.
  2693. //! - USB_TRANS_SETUP for setup transactions on endpoint zero.
  2694. //! - USB_TRANS_STATUS for status results on endpoint zero.
  2695. //!
  2696. //! \return This call will return 0 on success, or -1 if a transmission is
  2697. //! already in progress.
  2698. //
  2699. //*****************************************************************************
  2700. long
  2701. USBEndpointDataSend(unsigned long ulBase, unsigned long ulEndpoint,
  2702. unsigned long ulTransType)
  2703. {
  2704. unsigned long ulTxPktRdy;
  2705. //
  2706. // CHeck the arguments.
  2707. //
  2708. ASSERT(ulBase == USB0_BASE);
  2709. ASSERT((ulEndpoint == USB_EP_0) || (ulEndpoint == USB_EP_1) ||
  2710. (ulEndpoint == USB_EP_2) || (ulEndpoint == USB_EP_3) ||
  2711. (ulEndpoint == USB_EP_4) || (ulEndpoint == USB_EP_5) ||
  2712. (ulEndpoint == USB_EP_6) || (ulEndpoint == USB_EP_7) ||
  2713. (ulEndpoint == USB_EP_8) || (ulEndpoint == USB_EP_9) ||
  2714. (ulEndpoint == USB_EP_10) || (ulEndpoint == USB_EP_11) ||
  2715. (ulEndpoint == USB_EP_12) || (ulEndpoint == USB_EP_13) ||
  2716. (ulEndpoint == USB_EP_14) || (ulEndpoint == USB_EP_15));
  2717. //
  2718. // Get the bit position of TxPktRdy based on the endpoint.
  2719. //
  2720. if(ulEndpoint == USB_EP_0)
  2721. {
  2722. ulTxPktRdy = ulTransType & 0xff;
  2723. }
  2724. else
  2725. {
  2726. ulTxPktRdy = (ulTransType >> 8) & 0xff;
  2727. }
  2728. //
  2729. // Don't allow transmit of data if the TxPktRdy bit is already set.
  2730. //
  2731. if(HWREGB(ulBase + USB_O_CSRL0 + ulEndpoint) & USB_CSRL0_TXRDY)
  2732. {
  2733. return(-1);
  2734. }
  2735. //
  2736. // Set TxPktRdy in order to send the data.
  2737. //
  2738. HWREGB(ulBase + USB_O_CSRL0 + ulEndpoint) = ulTxPktRdy;
  2739. //
  2740. // Success.
  2741. //
  2742. return(0);
  2743. }
  2744. //*****************************************************************************
  2745. //
  2746. //! Forces a flush of an endpoint's FIFO.
  2747. //!
  2748. //! \param ulBase specifies the USB module base address.
  2749. //! \param ulEndpoint is the endpoint to access.
  2750. //! \param ulFlags specifies if the IN or OUT endpoint should be accessed.
  2751. //!
  2752. //! This function will force the controller to flush out the data in the FIFO.
  2753. //! The function can be called with either host or device controllers and
  2754. //! requires the \e ulFlags parameter be one of \b USB_EP_HOST_OUT,
  2755. //! \b USB_EP_HOST_IN, \b USB_EP_DEV_OUT, or \b USB_EP_DEV_IN.
  2756. //!
  2757. //! \return None.
  2758. //
  2759. //*****************************************************************************
  2760. void
  2761. USBFIFOFlush(unsigned long ulBase, unsigned long ulEndpoint,
  2762. unsigned long ulFlags)
  2763. {
  2764. //
  2765. // Check the arguments.
  2766. //
  2767. ASSERT(ulBase == USB0_BASE);
  2768. ASSERT((ulEndpoint == USB_EP_0) || (ulEndpoint == USB_EP_1) ||
  2769. (ulEndpoint == USB_EP_2) || (ulEndpoint == USB_EP_3) ||
  2770. (ulEndpoint == USB_EP_4) || (ulEndpoint == USB_EP_5) ||
  2771. (ulEndpoint == USB_EP_6) || (ulEndpoint == USB_EP_7) ||
  2772. (ulEndpoint == USB_EP_8) || (ulEndpoint == USB_EP_9) ||
  2773. (ulEndpoint == USB_EP_10) || (ulEndpoint == USB_EP_11) ||
  2774. (ulEndpoint == USB_EP_12) || (ulEndpoint == USB_EP_13) ||
  2775. (ulEndpoint == USB_EP_14) || (ulEndpoint == USB_EP_15));
  2776. //
  2777. // Endpoint zero has a different register set for FIFO flushing.
  2778. //
  2779. if(ulEndpoint == USB_EP_0)
  2780. {
  2781. //
  2782. // Nothing in the FIFO if neither of these bits are set.
  2783. //
  2784. if((HWREGB(ulBase + USB_O_CSRL0) &
  2785. (USB_CSRL0_RXRDY | USB_CSRL0_TXRDY)) != 0)
  2786. {
  2787. //
  2788. // Hit the Flush FIFO bit.
  2789. //
  2790. HWREGB(ulBase + USB_O_CSRH0) = USB_CSRH0_FLUSH;
  2791. }
  2792. }
  2793. else
  2794. {
  2795. //
  2796. // Only reset the IN or OUT FIFO.
  2797. //
  2798. if(ulFlags & (USB_EP_HOST_OUT | USB_EP_DEV_IN))
  2799. {
  2800. //
  2801. // Make sure the FIFO is not empty.
  2802. //
  2803. if(HWREGB(ulBase + USB_O_TXCSRL1 + EP_OFFSET(ulEndpoint)) &
  2804. USB_TXCSRL1_TXRDY)
  2805. {
  2806. //
  2807. // Hit the Flush FIFO bit.
  2808. //
  2809. HWREGB(ulBase + USB_O_TXCSRL1 + EP_OFFSET(ulEndpoint)) |=
  2810. USB_TXCSRL1_FLUSH;
  2811. }
  2812. }
  2813. else
  2814. {
  2815. //
  2816. // Make sure that the FIFO is not empty.
  2817. //
  2818. if(HWREGB(ulBase + USB_O_RXCSRL1 + EP_OFFSET(ulEndpoint)) &
  2819. USB_RXCSRL1_RXRDY)
  2820. {
  2821. //
  2822. // Hit the Flush FIFO bit.
  2823. //
  2824. HWREGB(ulBase + USB_O_RXCSRL1 + EP_OFFSET(ulEndpoint)) |=
  2825. USB_RXCSRL1_FLUSH;
  2826. }
  2827. }
  2828. }
  2829. }
  2830. //*****************************************************************************
  2831. //
  2832. //! Schedules a request for an IN transaction on an endpoint in host mode.
  2833. //!
  2834. //! \param ulBase specifies the USB module base address.
  2835. //! \param ulEndpoint is the endpoint to access.
  2836. //!
  2837. //! This function will schedule a request for an IN transaction. When the USB
  2838. //! device being communicated with responds the data, the data can be retrieved
  2839. //! by calling USBEndpointDataGet() or via a DMA transfer.
  2840. //!
  2841. //! \note This function should only be called in host mode and only for IN
  2842. //! endpoints.
  2843. //!
  2844. //! \return None.
  2845. //
  2846. //*****************************************************************************
  2847. void
  2848. USBHostRequestIN(unsigned long ulBase, unsigned long ulEndpoint)
  2849. {
  2850. unsigned long ulRegister;
  2851. //
  2852. // Check the arguments.
  2853. //
  2854. ASSERT(ulBase == USB0_BASE);
  2855. ASSERT((ulEndpoint == USB_EP_0) || (ulEndpoint == USB_EP_1) ||
  2856. (ulEndpoint == USB_EP_2) || (ulEndpoint == USB_EP_3) ||
  2857. (ulEndpoint == USB_EP_4) || (ulEndpoint == USB_EP_5) ||
  2858. (ulEndpoint == USB_EP_6) || (ulEndpoint == USB_EP_7) ||
  2859. (ulEndpoint == USB_EP_8) || (ulEndpoint == USB_EP_9) ||
  2860. (ulEndpoint == USB_EP_10) || (ulEndpoint == USB_EP_11) ||
  2861. (ulEndpoint == USB_EP_12) || (ulEndpoint == USB_EP_13) ||
  2862. (ulEndpoint == USB_EP_14) || (ulEndpoint == USB_EP_15));
  2863. //
  2864. // Endpoint zero uses a different offset than the other endpoints.
  2865. //
  2866. if(ulEndpoint == USB_EP_0)
  2867. {
  2868. ulRegister = USB_O_CSRL0;
  2869. }
  2870. else
  2871. {
  2872. ulRegister = USB_O_RXCSRL1 + EP_OFFSET(ulEndpoint);
  2873. }
  2874. //
  2875. // Set the request for an IN transaction.
  2876. //
  2877. HWREGB(ulBase + ulRegister) = USB_RXCSRL1_REQPKT;
  2878. }
  2879. //*****************************************************************************
  2880. //
  2881. //! Clears a scheduled IN transaction for an endpoint in host mode.
  2882. //!
  2883. //! \param ulBase specifies the USB module base address.
  2884. //! \param ulEndpoint is the endpoint to access.
  2885. //!
  2886. //! This function will clear a previously scheduled IN transaction if it is
  2887. //! still pending. This function should be used to safely disable any
  2888. //! scheduled IN transactions if the endpoint specified by \e ulEndpoint
  2889. //! is reconfigured used for communications with other devices.
  2890. //!
  2891. //! \note This function should only be called in host mode and only for IN
  2892. //! endpoints.
  2893. //!
  2894. //! \return None.
  2895. //
  2896. //*****************************************************************************
  2897. void
  2898. USBHostRequestINClear(unsigned long ulBase, unsigned long ulEndpoint)
  2899. {
  2900. unsigned long ulRegister;
  2901. //
  2902. // Check the arguments.
  2903. //
  2904. ASSERT(ulBase == USB0_BASE);
  2905. ASSERT((ulEndpoint == USB_EP_0) || (ulEndpoint == USB_EP_1) ||
  2906. (ulEndpoint == USB_EP_2) || (ulEndpoint == USB_EP_3) ||
  2907. (ulEndpoint == USB_EP_4) || (ulEndpoint == USB_EP_5) ||
  2908. (ulEndpoint == USB_EP_6) || (ulEndpoint == USB_EP_7) ||
  2909. (ulEndpoint == USB_EP_8) || (ulEndpoint == USB_EP_9) ||
  2910. (ulEndpoint == USB_EP_10) || (ulEndpoint == USB_EP_11) ||
  2911. (ulEndpoint == USB_EP_12) || (ulEndpoint == USB_EP_13) ||
  2912. (ulEndpoint == USB_EP_14) || (ulEndpoint == USB_EP_15));
  2913. //
  2914. // Endpoint zero uses a different offset than the other endpoints.
  2915. //
  2916. if(ulEndpoint == USB_EP_0)
  2917. {
  2918. ulRegister = USB_O_CSRL0;
  2919. }
  2920. else
  2921. {
  2922. ulRegister = USB_O_RXCSRL1 + EP_OFFSET(ulEndpoint);
  2923. }
  2924. //
  2925. // Clear the request for an IN transaction.
  2926. //
  2927. HWREGB(ulBase + ulRegister) &= ~USB_RXCSRL1_REQPKT;
  2928. }
  2929. //*****************************************************************************
  2930. //
  2931. //! Issues a request for a status IN transaction on endpoint zero.
  2932. //!
  2933. //! \param ulBase specifies the USB module base address.
  2934. //!
  2935. //! This function is used to cause a request for an status IN transaction from
  2936. //! a device on endpoint zero. This function can only be used with endpoint
  2937. //! zero as that is the only control endpoint that supports this ability. This
  2938. //! is used to complete the last phase of a control transaction to a device and
  2939. //! an interrupt is signaled when the status packet has been received.
  2940. //!
  2941. //! \return None.
  2942. //
  2943. //*****************************************************************************
  2944. void
  2945. USBHostRequestStatus(unsigned long ulBase)
  2946. {
  2947. //
  2948. // Check the arguments.
  2949. //
  2950. ASSERT(ulBase == USB0_BASE);
  2951. //
  2952. // Set the request for a status IN transaction.
  2953. //
  2954. HWREGB(ulBase + USB_O_CSRL0) = USB_CSRL0_REQPKT | USB_CSRL0_STATUS;
  2955. }
  2956. //*****************************************************************************
  2957. //
  2958. //! Sets the functional address for the device that is connected to an
  2959. //! endpoint in host mode.
  2960. //!
  2961. //! \param ulBase specifies the USB module base address.
  2962. //! \param ulEndpoint is the endpoint to access.
  2963. //! \param ulAddr is the functional address for the controller to use for this
  2964. //! endpoint.
  2965. //! \param ulFlags determines if this is an IN or an OUT endpoint.
  2966. //!
  2967. //! This function will set the functional address for a device that is using
  2968. //! this endpoint for communication. This \e ulAddr parameter is the address
  2969. //! of the target device that this endpoint is used to communicate with. The
  2970. //! \e ulFlags parameter indicates if the IN or OUT endpoint should be set.
  2971. //!
  2972. //! \note This function should only be called in host mode.
  2973. //!
  2974. //! \return None.
  2975. //
  2976. //*****************************************************************************
  2977. void
  2978. USBHostAddrSet(unsigned long ulBase, unsigned long ulEndpoint,
  2979. unsigned long ulAddr, unsigned long ulFlags)
  2980. {
  2981. //
  2982. // Check the arguments.
  2983. //
  2984. ASSERT(ulBase == USB0_BASE);
  2985. ASSERT((ulEndpoint == USB_EP_0) || (ulEndpoint == USB_EP_1) ||
  2986. (ulEndpoint == USB_EP_2) || (ulEndpoint == USB_EP_3) ||
  2987. (ulEndpoint == USB_EP_4) || (ulEndpoint == USB_EP_5) ||
  2988. (ulEndpoint == USB_EP_6) || (ulEndpoint == USB_EP_7) ||
  2989. (ulEndpoint == USB_EP_8) || (ulEndpoint == USB_EP_9) ||
  2990. (ulEndpoint == USB_EP_10) || (ulEndpoint == USB_EP_11) ||
  2991. (ulEndpoint == USB_EP_12) || (ulEndpoint == USB_EP_13) ||
  2992. (ulEndpoint == USB_EP_14) || (ulEndpoint == USB_EP_15));
  2993. //
  2994. // See if the transmit or receive address should be set.
  2995. //
  2996. if(ulFlags & USB_EP_HOST_OUT)
  2997. {
  2998. //
  2999. // Set the transmit address.
  3000. //
  3001. HWREGB(ulBase + USB_O_TXFUNCADDR0 + (ulEndpoint >> 1)) = ulAddr;
  3002. }
  3003. else
  3004. {
  3005. //
  3006. // Set the receive address.
  3007. //
  3008. HWREGB(ulBase + USB_O_TXFUNCADDR0 + 4 + (ulEndpoint >> 1)) = ulAddr;
  3009. }
  3010. }
  3011. //*****************************************************************************
  3012. //
  3013. //! Gets the current functional device address for an endpoint.
  3014. //!
  3015. //! \param ulBase specifies the USB module base address.
  3016. //! \param ulEndpoint is the endpoint to access.
  3017. //! \param ulFlags determines if this is an IN or an OUT endpoint.
  3018. //!
  3019. //! This function returns the current functional address that an endpoint is
  3020. //! using to communicate with a device. The \e ulFlags parameter determines if
  3021. //! the IN or OUT endpoint's device address is returned.
  3022. //!
  3023. //! \note This function should only be called in host mode.
  3024. //!
  3025. //! \return Returns the current function address being used by an endpoint.
  3026. //
  3027. //*****************************************************************************
  3028. unsigned long
  3029. USBHostAddrGet(unsigned long ulBase, unsigned long ulEndpoint,
  3030. unsigned long ulFlags)
  3031. {
  3032. //
  3033. // Check the arguments.
  3034. //
  3035. ASSERT(ulBase == USB0_BASE);
  3036. ASSERT((ulEndpoint == USB_EP_0) || (ulEndpoint == USB_EP_1) ||
  3037. (ulEndpoint == USB_EP_2) || (ulEndpoint == USB_EP_3) ||
  3038. (ulEndpoint == USB_EP_4) || (ulEndpoint == USB_EP_5) ||
  3039. (ulEndpoint == USB_EP_6) || (ulEndpoint == USB_EP_7) ||
  3040. (ulEndpoint == USB_EP_8) || (ulEndpoint == USB_EP_9) ||
  3041. (ulEndpoint == USB_EP_10) || (ulEndpoint == USB_EP_11) ||
  3042. (ulEndpoint == USB_EP_12) || (ulEndpoint == USB_EP_13) ||
  3043. (ulEndpoint == USB_EP_14) || (ulEndpoint == USB_EP_15));
  3044. //
  3045. // See if the transmit or receive address should be returned.
  3046. //
  3047. if(ulFlags & USB_EP_HOST_OUT)
  3048. {
  3049. //
  3050. // Return this endpoint's transmit address.
  3051. //
  3052. return(HWREGB(ulBase + USB_O_TXFUNCADDR0 + (ulEndpoint >> 1)));
  3053. }
  3054. else
  3055. {
  3056. //
  3057. // Return this endpoint's receive address.
  3058. //
  3059. return(HWREGB(ulBase + USB_O_TXFUNCADDR0 + 4 + (ulEndpoint >> 1)));
  3060. }
  3061. }
  3062. //*****************************************************************************
  3063. //
  3064. //! Set the hub address for the device that is connected to an endpoint.
  3065. //!
  3066. //! \param ulBase specifies the USB module base address.
  3067. //! \param ulEndpoint is the endpoint to access.
  3068. //! \param ulAddr is the hub address and port for the device using this
  3069. //! endpoint. The hub address must be defined in bits 8 through 15 with the
  3070. //! port number in bits 0 through 6.
  3071. //! \param ulFlags determines if this is an IN or an OUT endpoint.
  3072. //!
  3073. //! This function will set the hub address for a device that is using this
  3074. //! endpoint for communication. The \e ulFlags parameter determines if the
  3075. //! device address for the IN or the OUT endpoint is set by this call and sets
  3076. //! the speed of the downstream device. Valid values are one of \b
  3077. //! USB_EP_HOST_OUT or \b USB_EP_HOST_IN optionally ORed with \b
  3078. //! USB_EP_SPEED_LOW.
  3079. //!
  3080. //! \note This function should only be called in host mode.
  3081. //!
  3082. //! \return None.
  3083. //
  3084. //*****************************************************************************
  3085. void
  3086. USBHostHubAddrSet(unsigned long ulBase, unsigned long ulEndpoint,
  3087. unsigned long ulAddr, unsigned long ulFlags)
  3088. {
  3089. //
  3090. // Check the arguments.
  3091. //
  3092. ASSERT(ulBase == USB0_BASE);
  3093. ASSERT((ulEndpoint == USB_EP_0) || (ulEndpoint == USB_EP_1) ||
  3094. (ulEndpoint == USB_EP_2) || (ulEndpoint == USB_EP_3) ||
  3095. (ulEndpoint == USB_EP_4) || (ulEndpoint == USB_EP_5) ||
  3096. (ulEndpoint == USB_EP_6) || (ulEndpoint == USB_EP_7) ||
  3097. (ulEndpoint == USB_EP_8) || (ulEndpoint == USB_EP_9) ||
  3098. (ulEndpoint == USB_EP_10) || (ulEndpoint == USB_EP_11) ||
  3099. (ulEndpoint == USB_EP_12) || (ulEndpoint == USB_EP_13) ||
  3100. (ulEndpoint == USB_EP_14) || (ulEndpoint == USB_EP_15));
  3101. //
  3102. // See if the hub transmit or receive address is being set.
  3103. //
  3104. if(ulFlags & USB_EP_HOST_OUT)
  3105. {
  3106. //
  3107. // Set the hub transmit address and port number for this endpoint.
  3108. //
  3109. HWREGH(ulBase + USB_O_TXHUBADDR0 + (ulEndpoint >> 1)) = ulAddr;
  3110. }
  3111. else
  3112. {
  3113. //
  3114. // Set the hub receive address and port number for this endpoint.
  3115. //
  3116. HWREGH(ulBase + USB_O_TXHUBADDR0 + 4 + (ulEndpoint >> 1)) = ulAddr;
  3117. }
  3118. //
  3119. // Set the speed of communication for endpoint 0. This is done here since
  3120. // it will change on a transaction-by-transaction basis for EP0. For other
  3121. // endpoints, this is set in USBHostEndpointConfig().
  3122. //
  3123. if(ulEndpoint == USB_EP_0)
  3124. {
  3125. if(ulFlags & USB_EP_SPEED_FULL)
  3126. {
  3127. HWREGB(ulBase + USB_O_TYPE0) = USB_TYPE0_SPEED_FULL;
  3128. }
  3129. else
  3130. {
  3131. HWREGB(ulBase + USB_O_TYPE0) = USB_TYPE0_SPEED_LOW;
  3132. }
  3133. }
  3134. }
  3135. //*****************************************************************************
  3136. //
  3137. //! Get the current device hub address for this endpoint.
  3138. //!
  3139. //! \param ulBase specifies the USB module base address.
  3140. //! \param ulEndpoint is the endpoint to access.
  3141. //! \param ulFlags determines if this is an IN or an OUT endpoint.
  3142. //!
  3143. //! This function will return the current hub address that an endpoint is using
  3144. //! to communicate with a device. The \e ulFlags parameter determines if the
  3145. //! device address for the IN or OUT endpoint is returned.
  3146. //!
  3147. //! \note This function should only be called in host mode.
  3148. //!
  3149. //! \return This function returns the current hub address being used by an
  3150. //! endpoint.
  3151. //
  3152. //*****************************************************************************
  3153. unsigned long
  3154. USBHostHubAddrGet(unsigned long ulBase, unsigned long ulEndpoint,
  3155. unsigned long ulFlags)
  3156. {
  3157. //
  3158. // Check the arguments.
  3159. //
  3160. ASSERT(ulBase == USB0_BASE);
  3161. ASSERT((ulEndpoint == USB_EP_0) || (ulEndpoint == USB_EP_1) ||
  3162. (ulEndpoint == USB_EP_2) || (ulEndpoint == USB_EP_3) ||
  3163. (ulEndpoint == USB_EP_4) || (ulEndpoint == USB_EP_5) ||
  3164. (ulEndpoint == USB_EP_6) || (ulEndpoint == USB_EP_7) ||
  3165. (ulEndpoint == USB_EP_8) || (ulEndpoint == USB_EP_9) ||
  3166. (ulEndpoint == USB_EP_10) || (ulEndpoint == USB_EP_11) ||
  3167. (ulEndpoint == USB_EP_12) || (ulEndpoint == USB_EP_13) ||
  3168. (ulEndpoint == USB_EP_14) || (ulEndpoint == USB_EP_15));
  3169. //
  3170. // See if the hub transmit or receive address should be returned.
  3171. //
  3172. if(ulFlags & USB_EP_HOST_OUT)
  3173. {
  3174. //
  3175. // Return the hub transmit address for this endpoint.
  3176. //
  3177. return(HWREGB(ulBase + USB_O_TXHUBADDR0 + (ulEndpoint >> 1)));
  3178. }
  3179. else
  3180. {
  3181. //
  3182. // Return the hub receive address for this endpoint.
  3183. //
  3184. return(HWREGB(ulBase + USB_O_TXHUBADDR0 + 4 + (ulEndpoint >> 1)));
  3185. }
  3186. }
  3187. //*****************************************************************************
  3188. //
  3189. //! Sets the configuration for USB power fault.
  3190. //!
  3191. //! \param ulBase specifies the USB module base address.
  3192. //! \param ulFlags specifies the configuration of the power fault.
  3193. //!
  3194. //! This function controls how the USB controller uses its external power
  3195. //! control pins (USBnPFTL and USBnEPEN). The flags specify the power
  3196. //! fault level sensitivity, the power fault action, and the power enable level
  3197. //! and source.
  3198. //!
  3199. //! One of the following can be selected as the power fault level sensitivity:
  3200. //!
  3201. //! - \b USB_HOST_PWRFLT_LOW - An external power fault is indicated by the pin
  3202. //! being driven low.
  3203. //! - \b USB_HOST_PWRFLT_HIGH - An external power fault is indicated by the pin
  3204. //! being driven high.
  3205. //!
  3206. //! One of the following can be selected as the power fault action:
  3207. //!
  3208. //! - \b USB_HOST_PWRFLT_EP_NONE - No automatic action when power fault
  3209. //! detected.
  3210. //! - \b USB_HOST_PWRFLT_EP_TRI - Automatically Tri-state the USBnEPEN pin on a
  3211. //! power fault.
  3212. //! - \b USB_HOST_PWRFLT_EP_LOW - Automatically drive USBnEPEN pin low on a
  3213. //! power fault.
  3214. //! - \b USB_HOST_PWRFLT_EP_HIGH - Automatically drive USBnEPEN pin high on a
  3215. //! power fault.
  3216. //!
  3217. //! One of the following can be selected as the power enable level and source:
  3218. //!
  3219. //! - \b USB_HOST_PWREN_MAN_LOW - USBEPEN is driven low by the USB controller
  3220. //! when USBHostPwrEnable() is called.
  3221. //! - \b USB_HOST_PWREN_MAN_HIGH - USBEPEN is driven high by the USB controller
  3222. //! when USBHostPwrEnable() is called.
  3223. //! - \b USB_HOST_PWREN_AUTOLOW - USBEPEN is driven low by the USB controller
  3224. //! automatically if USBOTGSessionRequest() has
  3225. //! enabled a session.
  3226. //! - \b USB_HOST_PWREN_AUTOHIGH - USBEPEN is driven high by the USB controller
  3227. //! automatically if USBOTGSessionRequest() has
  3228. //! enabled a session.
  3229. //!
  3230. //! On devices that support the VBUS glitch filter, the
  3231. //! \b USB_HOST_PWREN_FILTER can be added to ignore small, short drops in VBUS
  3232. //! level caused by high power consumption. This is mainly used to avoid
  3233. //! causing VBUS errors caused by devices with high in-rush current.
  3234. //!
  3235. //! \note The following values have been deprecated and should no longer be
  3236. //! used.
  3237. //! - \b USB_HOST_PWREN_LOW - Automatically drive USBnEPEN low when power is
  3238. //! enabled.
  3239. //! - \b USB_HOST_PWREN_HIGH - Automatically drive USBnEPEN high when power is
  3240. //! enabled.
  3241. //! - \b USB_HOST_PWREN_VBLOW - Automatically drive USBnEPEN low when power is
  3242. //! enabled.
  3243. //! - \b USB_HOST_PWREN_VBHIGH - Automatically drive USBnEPEN high when power is
  3244. //! enabled.
  3245. //!
  3246. //! \note This function should only be called on microcontrollers that support
  3247. //! host mode or OTG operation.
  3248. //!
  3249. //! \return None.
  3250. //
  3251. //*****************************************************************************
  3252. void
  3253. USBHostPwrConfig(unsigned long ulBase, unsigned long ulFlags)
  3254. {
  3255. //
  3256. // Check the arguments.
  3257. //
  3258. ASSERT(ulBase == USB0_BASE);
  3259. ASSERT((ulFlags & ~(USB_HOST_PWREN_FILTER | USB_EPC_PFLTACT_M |
  3260. USB_EPC_PFLTAEN | USB_EPC_PFLTSEN_HIGH |
  3261. USB_EPC_EPEN_M)) == 0);
  3262. //
  3263. // If requested, enable VBUS droop detection on parts that support this
  3264. // feature.
  3265. //
  3266. HWREG(ulBase + USB_O_VDC) = ulFlags >> 16;
  3267. //
  3268. // Set the power fault configuration as specified. This will not change
  3269. // whether fault detection is enabled or not.
  3270. //
  3271. HWREGH(ulBase + USB_O_EPC) =
  3272. (ulFlags | (HWREGH(ulBase + USB_O_EPC) &
  3273. ~(USB_EPC_PFLTACT_M | USB_EPC_PFLTAEN |
  3274. USB_EPC_PFLTSEN_HIGH | USB_EPC_EPEN_M)));
  3275. }
  3276. //*****************************************************************************
  3277. //
  3278. //! Enables power fault detection.
  3279. //!
  3280. //! \param ulBase specifies the USB module base address.
  3281. //!
  3282. //! This function enables power fault detection in the USB controller. If the
  3283. //! USBPFLT pin is not in use this function should not be used.
  3284. //!
  3285. //! \note This function should only be called in host mode.
  3286. //!
  3287. //! \return None.
  3288. //
  3289. //*****************************************************************************
  3290. void
  3291. USBHostPwrFaultEnable(unsigned long ulBase)
  3292. {
  3293. //
  3294. // Check the arguments.
  3295. //
  3296. ASSERT(ulBase == USB0_BASE);
  3297. //
  3298. // Enable power fault input.
  3299. //
  3300. HWREGH(ulBase + USB_O_EPC) |= USB_EPC_PFLTEN;
  3301. }
  3302. //*****************************************************************************
  3303. //
  3304. //! Disables power fault detection.
  3305. //!
  3306. //! \param ulBase specifies the USB module base address.
  3307. //!
  3308. //! This function disables power fault detection in the USB controller.
  3309. //!
  3310. //! \note This function should only be called in host mode.
  3311. //!
  3312. //! \return None.
  3313. //
  3314. //*****************************************************************************
  3315. void
  3316. USBHostPwrFaultDisable(unsigned long ulBase)
  3317. {
  3318. //
  3319. // Check the arguments.
  3320. //
  3321. ASSERT(ulBase == USB0_BASE);
  3322. //
  3323. // Enable power fault input.
  3324. //
  3325. HWREGH(ulBase + USB_O_EPC) &= ~USB_EPC_PFLTEN;
  3326. }
  3327. //*****************************************************************************
  3328. //
  3329. //! Enables the external power pin.
  3330. //!
  3331. //! \param ulBase specifies the USB module base address.
  3332. //!
  3333. //! This function enables the USBEPEN signal to enable an external power supply
  3334. //! in host mode operation.
  3335. //!
  3336. //! \note This function should only be called in host mode.
  3337. //!
  3338. //! \return None.
  3339. //
  3340. //*****************************************************************************
  3341. void
  3342. USBHostPwrEnable(unsigned long ulBase)
  3343. {
  3344. //
  3345. // Check the arguments.
  3346. //
  3347. ASSERT(ulBase == USB0_BASE);
  3348. //
  3349. // Enable the external power supply enable signal.
  3350. //
  3351. HWREGH(ulBase + USB_O_EPC) |= USB_EPC_EPENDE;
  3352. }
  3353. //*****************************************************************************
  3354. //
  3355. //! Disables the external power pin.
  3356. //!
  3357. //! \param ulBase specifies the USB module base address.
  3358. //!
  3359. //! This function disables the USBEPEN signal to disable an external power
  3360. //! supply in host mode operation.
  3361. //!
  3362. //! \note This function should only be called in host mode.
  3363. //!
  3364. //! \return None.
  3365. //
  3366. //*****************************************************************************
  3367. void
  3368. USBHostPwrDisable(unsigned long ulBase)
  3369. {
  3370. //
  3371. // Check the arguments.
  3372. //
  3373. ASSERT(ulBase == USB0_BASE);
  3374. //
  3375. // Disable the external power supply enable signal.
  3376. //
  3377. HWREGH(ulBase + USB_O_EPC) &= ~USB_EPC_EPENDE;
  3378. }
  3379. //*****************************************************************************
  3380. //
  3381. //! Get the current frame number.
  3382. //!
  3383. //! \param ulBase specifies the USB module base address.
  3384. //!
  3385. //! This function returns the last frame number received.
  3386. //!
  3387. //! \return The last frame number received.
  3388. //
  3389. //*****************************************************************************
  3390. unsigned long
  3391. USBFrameNumberGet(unsigned long ulBase)
  3392. {
  3393. //
  3394. // Check the arguments.
  3395. //
  3396. ASSERT(ulBase == USB0_BASE);
  3397. //
  3398. // Return the most recent frame number.
  3399. //
  3400. return(HWREGH(ulBase + USB_O_FRAME));
  3401. }
  3402. //*****************************************************************************
  3403. //
  3404. //! Starts or ends a session.
  3405. //!
  3406. //! \param ulBase specifies the USB module base address.
  3407. //! \param bStart specifies if this call starts or ends a session.
  3408. //!
  3409. //! This function is used in OTG mode to start a session request or end a
  3410. //! session. If the \e bStart parameter is set to \b true, then this function
  3411. //! start a session and if it is \b false it will end a session.
  3412. //!
  3413. //! \return None.
  3414. //
  3415. //*****************************************************************************
  3416. void
  3417. USBOTGSessionRequest(unsigned long ulBase, tBoolean bStart)
  3418. {
  3419. //
  3420. // Check the arguments.
  3421. //
  3422. ASSERT(ulBase == USB0_BASE);
  3423. //
  3424. // Start or end the session as directed.
  3425. //
  3426. if(bStart)
  3427. {
  3428. HWREGB(ulBase + USB_O_DEVCTL) |= USB_DEVCTL_SESSION;
  3429. }
  3430. else
  3431. {
  3432. HWREGB(ulBase + USB_O_DEVCTL) &= ~USB_DEVCTL_SESSION;
  3433. }
  3434. }
  3435. //*****************************************************************************
  3436. //
  3437. //! Returns the absolute FIFO address for a given endpoint.
  3438. //!
  3439. //! \param ulBase specifies the USB module base address.
  3440. //! \param ulEndpoint specifies which endpoint's FIFO address to return.
  3441. //!
  3442. //! This function returns the actual physical address of the FIFO. This is
  3443. //! needed when the USB is going to be used with the uDMA controller and the
  3444. //! source or destination address needs to be set to the physical FIFO address
  3445. //! for a given endpoint.
  3446. //!
  3447. //! \return None.
  3448. //
  3449. //*****************************************************************************
  3450. unsigned long
  3451. USBFIFOAddrGet(unsigned long ulBase, unsigned long ulEndpoint)
  3452. {
  3453. //
  3454. // Return the FIFO address for this endpoint.
  3455. //
  3456. return(ulBase + USB_O_FIFO0 + (ulEndpoint >> 2));
  3457. }
  3458. //*****************************************************************************
  3459. //
  3460. //! Returns the current operating mode of the controller.
  3461. //!
  3462. //! \param ulBase specifies the USB module base address.
  3463. //!
  3464. //! This function returns the current operating mode on USB controllers with
  3465. //! OTG or Dual mode functionality.
  3466. //!
  3467. //! For OTG controllers:
  3468. //!
  3469. //! The function will return on of the following values on OTG controllers:
  3470. //! \b USB_OTG_MODE_ASIDE_HOST, \b USB_OTG_MODE_ASIDE_DEV,
  3471. //! \b USB_OTG_MODE_BSIDE_HOST, \b USB_OTG_MODE_BSIDE_DEV,
  3472. //! \b USB_OTG_MODE_NONE.
  3473. //!
  3474. //! \b USB_OTG_MODE_ASIDE_HOST indicates that the controller is in host mode
  3475. //! on the A-side of the cable.
  3476. //!
  3477. //! \b USB_OTG_MODE_ASIDE_DEV indicates that the controller is in device mode
  3478. //! on the A-side of the cable.
  3479. //!
  3480. //! \b USB_OTG_MODE_BSIDE_HOST indicates that the controller is in host mode
  3481. //! on the B-side of the cable.
  3482. //!
  3483. //! \b USB_OTG_MODE_BSIDE_DEV indicates that the controller is in device mode
  3484. //! on the B-side of the cable. If and OTG session request is started with no
  3485. //! cable in place this is the default mode for the controller.
  3486. //!
  3487. //! \b USB_OTG_MODE_NONE indicates that the controller is not attempting to
  3488. //! determine its role in the system.
  3489. //!
  3490. //! For Dual Mode controllers:
  3491. //!
  3492. //! The function will return on of the following values:
  3493. //! \b USB_DUAL_MODE_HOST, \b USB_DUAL_MODE_DEVICE, or
  3494. //! \b USB_DUAL_MODE_NONE.
  3495. //!
  3496. //! \b USB_DUAL_MODE_HOST indicates that the controller is acting as a host.
  3497. //!
  3498. //! \b USB_DUAL_MODE_DEVICE indicates that the controller acting as a device.
  3499. //!
  3500. //! \b USB_DUAL_MODE_NONE indicates that the controller is not active as
  3501. //! either a host or device.
  3502. //!
  3503. //! \return Returns \b USB_OTG_MODE_ASIDE_HOST, \b USB_OTG_MODE_ASIDE_DEV,
  3504. //! \b USB_OTG_MODE_BSIDE_HOST, \b USB_OTG_MODE_BSIDE_DEV,
  3505. //! \b USB_OTG_MODE_NONE, \b USB_DUAL_MODE_HOST, \b USB_DUAL_MODE_DEVICE, or
  3506. //! \b USB_DUAL_MODE_NONE.
  3507. //
  3508. //*****************************************************************************
  3509. unsigned long
  3510. USBModeGet(unsigned long ulBase)
  3511. {
  3512. //
  3513. // Check the arguments.
  3514. //
  3515. ASSERT(ulBase == USB0_BASE);
  3516. //
  3517. // Checks the current mode in the USB_O_DEVCTL and returns the current
  3518. // mode.
  3519. //
  3520. // USB_OTG_MODE_ASIDE_HOST: USB_DEVCTL_HOST | USB_DEVCTL_SESSION
  3521. // USB_OTG_MODE_ASIDE_DEV: USB_DEVCTL_SESSION
  3522. // USB_OTG_MODE_BSIDE_HOST: USB_DEVCTL_DEV | USB_DEVCTL_SESSION |
  3523. // USB_DEVCTL_HOST
  3524. // USB_OTG_MODE_BSIDE_DEV: USB_DEVCTL_DEV | USB_DEVCTL_SESSION
  3525. // USB_OTG_MODE_NONE: USB_DEVCTL_DEV
  3526. //
  3527. return(HWREGB(ulBase + USB_O_DEVCTL) &
  3528. (USB_DEVCTL_DEV | USB_DEVCTL_HOST | USB_DEVCTL_SESSION |
  3529. USB_DEVCTL_VBUS_M));
  3530. }
  3531. //*****************************************************************************
  3532. //
  3533. //! Sets the DMA channel to use for a given endpoint.
  3534. //!
  3535. //! \param ulBase specifies the USB module base address.
  3536. //! \param ulEndpoint specifies which endpoint's FIFO address to return.
  3537. //! \param ulChannel specifies which DMA channel to use for which endpoint.
  3538. //!
  3539. //! This function is used to configure which DMA channel to use with a given
  3540. //! endpoint. Receive DMA channels can only be used with receive endpoints
  3541. //! and transmit DMA channels can only be used with transmit endpoints. This
  3542. //! allows the 3 receive and 3 transmit DMA channels to be mapped to any
  3543. //! endpoint other than 0. The values that should be passed into the
  3544. //! \e ulChannel value are the UDMA_CHANNEL_USBEP* values defined in udma.h.
  3545. //!
  3546. //! \note This function only has an effect on microcontrollers that have the
  3547. //! ability to change the DMA channel for an endpoint. Calling this function
  3548. //! on other devices will have no effect.
  3549. //!
  3550. //! \return None.
  3551. //!
  3552. //*****************************************************************************
  3553. void
  3554. USBEndpointDMAChannel(unsigned long ulBase, unsigned long ulEndpoint,
  3555. unsigned long ulChannel)
  3556. {
  3557. unsigned long ulMask;
  3558. //
  3559. // Check the arguments.
  3560. //
  3561. ASSERT(ulBase == USB0_BASE);
  3562. ASSERT((ulEndpoint == USB_EP_1) || (ulEndpoint == USB_EP_2) ||
  3563. (ulEndpoint == USB_EP_3) || (ulEndpoint == USB_EP_4) ||
  3564. (ulEndpoint == USB_EP_5) || (ulEndpoint == USB_EP_6) ||
  3565. (ulEndpoint == USB_EP_7) || (ulEndpoint == USB_EP_8) ||
  3566. (ulEndpoint == USB_EP_9) || (ulEndpoint == USB_EP_10) ||
  3567. (ulEndpoint == USB_EP_11) || (ulEndpoint == USB_EP_12) ||
  3568. (ulEndpoint == USB_EP_13) || (ulEndpoint == USB_EP_14) ||
  3569. (ulEndpoint == USB_EP_15));
  3570. ASSERT(ulChannel <= UDMA_CHANNEL_USBEP3TX);
  3571. //
  3572. // The input select mask needs to be shifted into the correct position
  3573. // based on the channel.
  3574. //
  3575. ulMask = 0xf << (ulChannel * 4);
  3576. //
  3577. // Clear out the current selection for the channel.
  3578. //
  3579. ulMask = HWREG(ulBase + USB_O_DMASEL) & (~ulMask);
  3580. //
  3581. // The input select is now shifted into the correct position based on the
  3582. // channel.
  3583. //
  3584. ulMask |= (USB_EP_TO_INDEX(ulEndpoint)) << (ulChannel * 4);
  3585. //
  3586. // Write the value out to the register.
  3587. //
  3588. HWREG(ulBase + USB_O_DMASEL) = ulMask;
  3589. }
  3590. //*****************************************************************************
  3591. //
  3592. //! Change the mode of the USB controller to host.
  3593. //!
  3594. //! \param ulBase specifies the USB module base address.
  3595. //!
  3596. //! This function changes the mode of the USB controller to host mode.
  3597. //!
  3598. //! \return None.
  3599. //
  3600. //*****************************************************************************
  3601. void
  3602. USBHostMode(unsigned long ulBase)
  3603. {
  3604. //
  3605. // Check the arguments.
  3606. //
  3607. ASSERT(ulBase == USB0_BASE);
  3608. //
  3609. // Force mode in OTG parts that support forcing USB controller mode.
  3610. // This bit is not writable in USB controllers that do not support
  3611. // forcing the mode. Not setting the USB_GPCS_DEVMOD bit makes this a
  3612. // force of host mode.
  3613. //
  3614. HWREGB(ulBase + USB_O_GPCS) = USB_GPCS_DEVMODOTG;
  3615. }
  3616. //*****************************************************************************
  3617. //
  3618. //! Change the mode of the USB controller to device.
  3619. //!
  3620. //! \param ulBase specifies the USB module base address.
  3621. //!
  3622. //! This function changes the mode of the USB controller to device mode.
  3623. //!
  3624. //! \return None.
  3625. //
  3626. //*****************************************************************************
  3627. void
  3628. USBDevMode(unsigned long ulBase)
  3629. {
  3630. //
  3631. // Check the arguments.
  3632. //
  3633. ASSERT(ulBase == USB0_BASE);
  3634. //
  3635. // Set the USB controller mode to device.
  3636. //
  3637. HWREGB(ulBase + USB_O_GPCS) = USB_GPCS_DEVMODOTG | USB_GPCS_DEVMOD;
  3638. }
  3639. //*****************************************************************************
  3640. //
  3641. //! Change the mode of the USB controller to OTG.
  3642. //!
  3643. //! \param ulBase specifies the USB module base address.
  3644. //!
  3645. //! This function changes the mode of the USB controller to OTG mode. This
  3646. //! is only valid on microcontrollers that have the OTG capabilities.
  3647. //!
  3648. //! \return None.
  3649. //
  3650. //*****************************************************************************
  3651. void
  3652. USBOTGMode(unsigned long ulBase)
  3653. {
  3654. //
  3655. // Check the arguments.
  3656. //
  3657. ASSERT(ulBase == USB0_BASE);
  3658. //
  3659. // Disable the override of the USB controller mode when running on an OTG
  3660. // device.
  3661. //
  3662. HWREGB(ulBase + USB_O_GPCS) = 0;
  3663. }
  3664. //*****************************************************************************
  3665. //
  3666. //! Powers off the USB PHY.
  3667. //!
  3668. //! \param ulBase specifies the USB module base address.
  3669. //!
  3670. //! This function will power off the USB PHY, reducing the current consuption
  3671. //! of the device. While in the powered off state, the USB controller is
  3672. //! unable to operate.
  3673. //!
  3674. //! \return None.
  3675. //
  3676. //*****************************************************************************
  3677. void
  3678. USBPHYPowerOff(unsigned long ulBase)
  3679. {
  3680. //
  3681. // Set the PWRDNPHY bit in the PHY, putting it into its low power mode.
  3682. //
  3683. HWREGB(ulBase + USB_O_POWER) |= USB_POWER_PWRDNPHY;
  3684. }
  3685. //*****************************************************************************
  3686. //
  3687. //! Powers on the USB PHY.
  3688. //!
  3689. //! \param ulBase specifies the USB module base address.
  3690. //!
  3691. //! This function will power on the USB PHY, enabling it return to normal
  3692. //! operation. By default, the PHY is powered on, so this function only needs
  3693. //! to be called if USBPHYPowerOff() has previously been called.
  3694. //!
  3695. //! \return None.
  3696. //
  3697. //*****************************************************************************
  3698. void
  3699. USBPHYPowerOn(unsigned long ulBase)
  3700. {
  3701. //
  3702. // Clear the PWRDNPHY bit in the PHY, putting it into normal operating
  3703. // mode.
  3704. //
  3705. HWREGB(ulBase + USB_O_POWER) &= ~USB_POWER_PWRDNPHY;
  3706. }
  3707. //*****************************************************************************
  3708. //
  3709. // Close the Doxygen group.
  3710. //! @}
  3711. //
  3712. //*****************************************************************************