usb.c 130 KB

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