usb.c 116 KB

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