usbhostenum.c 146 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711
  1. //*****************************************************************************
  2. //
  3. // usbhostenum.c - Device enumeration code for the USB host library.
  4. //
  5. // Copyright (c) 2008-2010 Texas Instruments Incorporated. All rights reserved.
  6. // Software License Agreement
  7. //
  8. // Texas Instruments (TI) is supplying this software for use solely and
  9. // exclusively on TI's microcontroller products. The software is owned by
  10. // TI and/or its suppliers, and is protected under applicable copyright
  11. // laws. You may not combine this software with "viral" open-source
  12. // software in order to form a larger program.
  13. //
  14. // THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
  15. // NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
  16. // NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. // A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
  18. // CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
  19. // DAMAGES, FOR ANY REASON WHATSOEVER.
  20. //
  21. // This is part of AM1808 StarterWare USB Library, modified resused from revision 6288 of the
  22. // stellaris USB Library.
  23. //
  24. //*****************************************************************************
  25. #include "hw_usb.h"
  26. #include "hw_types.h"
  27. #include "debug.h"
  28. #include "interrupt.h"
  29. #include "usb.h"
  30. #include "usblib.h"
  31. #include "usblibpriv.h"
  32. #include "usbhost.h"
  33. #include "delay.h"
  34. #include "cppi41dma.h"
  35. #include <string.h>
  36. #ifdef DMA_MODE
  37. unsigned char *rxBuffer;
  38. #endif
  39. //*****************************************************************************
  40. //
  41. //! \addtogroup usblib_hcd
  42. //! @{
  43. //
  44. //*****************************************************************************
  45. //*****************************************************************************
  46. //
  47. // External prototypes.
  48. //
  49. //*****************************************************************************
  50. //extern tUSBMode g_eUSBMode;
  51. static tUSBMode g_eUSBMode = USB_MODE_HOST;
  52. //*****************************************************************************
  53. //
  54. // USB instance Object
  55. //
  56. //*****************************************************************************
  57. extern tUSBInstanceObject g_USBInstance[];
  58. //*****************************************************************************
  59. //
  60. // Internal function prototypes.
  61. //
  62. //*****************************************************************************
  63. static void USBHCDEP0StateTx(unsigned int ulIndex);
  64. static void USBHCDEnumHandler(unsigned int ulIndex);
  65. static void USBHCDClearFeature(unsigned int ulIndex,
  66. unsigned int ulDevAddress,
  67. unsigned int ulEndpoint,
  68. unsigned int ulFeature);
  69. static unsigned int USBHCDTxAbort(unsigned int ulIndex, unsigned int endPoint) ;
  70. static unsigned int USBHCDRxAbort(unsigned int ulIndex, unsigned int endPoint);
  71. static unsigned int USBHCDRetryConnect(unsigned int ulIndex);
  72. //*****************************************************************************
  73. //
  74. // Automatic power enable.
  75. //
  76. //*****************************************************************************
  77. #define USB_HOST_PWREN_AUTO 0x00000002
  78. //*****************************************************************************
  79. //
  80. // Flags used to signal between the interrupt handler and USBHCDMain().
  81. //
  82. //*****************************************************************************
  83. #define INT_EVENT_VBUS_ERR 0x01
  84. #define INT_EVENT_CONNECT 0x02
  85. #define INT_EVENT_DISCONNECT 0x04
  86. #define INT_EVENT_POWER_FAULT 0x08
  87. #define INT_EVENT_BABBLE_FAULT 0x10
  88. //volatile unsigned int g_ulUSBHIntEvents;
  89. unsigned int g_ulConnectRetry[USB_NUM_INSTANCE];
  90. //*****************************************************************************
  91. //
  92. // Flags used to indicate that a uDMA transfer is pending on a pipe.
  93. //
  94. //*****************************************************************************
  95. #define DMA_PEND_TRANSMIT_FLAG 0x10000
  96. #define DMA_PEND_RECEIVE_FLAG 0x1
  97. //volatile unsigned int g_ulDMAPending = 0;
  98. //*****************************************************************************
  99. //
  100. // This holds the current power configuration that is used when USBHCDInit()
  101. // is called.
  102. //
  103. //*****************************************************************************
  104. //static unsigned int g_ulPowerConfig = USBHCD_VBUS_AUTO_HIGH;
  105. //*****************************************************************************
  106. //
  107. // The states for endpoint 0 during enumeration.
  108. //
  109. //*****************************************************************************
  110. typedef enum
  111. {
  112. //
  113. // The USB device is waiting on a request from the host controller on
  114. // endpoint 0.
  115. //
  116. EP0_STATE_IDLE,
  117. //
  118. // Setup packet is expecting data IN.
  119. //
  120. EP0_STATE_SETUP_IN,
  121. //
  122. // Setup packet is sending data OUT.
  123. //
  124. EP0_STATE_SETUP_OUT,
  125. //
  126. // The USB device is receiving data from the device due to an SETUP IN
  127. // request.
  128. //
  129. EP0_STATE_RX,
  130. //
  131. // The USB device has completed the IN or OUT request and is now waiting
  132. // for the host to acknowledge the end of the IN/OUT transaction. This
  133. // is the status phase for a USB control transaction.
  134. //
  135. EP0_STATE_STATUS,
  136. //
  137. // This state is for when a response only has a status phase and no
  138. // data phase.
  139. //
  140. EP0_STATE_STATUS_IN,
  141. //
  142. // This endpoint has signaled a stall condition and is waiting for the
  143. // stall to be acknowledged by the host controller.
  144. //
  145. EP0_STATE_STALL,
  146. //
  147. // An error has occurred on endpoint 0.
  148. //
  149. EP0_STATE_ERROR
  150. }
  151. tEP0State;
  152. //*****************************************************************************
  153. //
  154. // This structure holds the full state for the device enumeration.
  155. //
  156. //*****************************************************************************
  157. typedef struct
  158. {
  159. //
  160. // This is the pointer to the current data being sent out or received
  161. // on endpoint 0.
  162. //
  163. unsigned char *pData;
  164. //
  165. // This is the number of bytes that remain to be sent from or received
  166. // into the g_DeviceState.pEP0Data data buffer.
  167. //
  168. volatile unsigned int ulBytesRemaining;
  169. //
  170. // The amount of data being sent/received due to a request.
  171. //
  172. unsigned int ulDataSize;
  173. //
  174. // This is the current device address in use by endpoint 0.
  175. //
  176. unsigned int ulDevAddress;
  177. //
  178. // The maximum packet size for the device responding to the setup packet.
  179. //
  180. unsigned int ulMaxPacketSize;
  181. //
  182. // The host controller's state.
  183. //
  184. tEP0State eState;
  185. }
  186. tHostState;
  187. //*****************************************************************************
  188. //
  189. // This variable holds the current state of endpoint 0.
  190. //
  191. //*****************************************************************************
  192. static volatile tHostState g_sUSBHEP0State[USB_NUM_INSTANCE] =
  193. {
  194. {
  195. 0, // pData
  196. 0, // ulBytesRemaining
  197. 0, // ulDataSize
  198. 0, // ulDevAddress
  199. 0, // ulMaxPacketSize
  200. EP0_STATE_IDLE // eState
  201. }
  202. #if (USB_NUM_INSTANCE == 2)
  203. ,{
  204. 0, // pData
  205. 0, // ulBytesRemaining
  206. 0, // ulDataSize
  207. 0, // ulDevAddress
  208. 0, // ulMaxPacketSize
  209. EP0_STATE_IDLE // eState
  210. }
  211. #endif
  212. };
  213. //*****************************************************************************
  214. //
  215. // The global tick counter
  216. //
  217. //*****************************************************************************
  218. //static volatile unsigned int g_ulCurrentTick = 0;
  219. //*****************************************************************************
  220. //
  221. // The current active driver.
  222. //
  223. //*****************************************************************************
  224. //static int g_iUSBHActiveDriver = -1;
  225. //static void *g_pvDriverInstance = 0;
  226. //*****************************************************************************
  227. //
  228. // These definitions are used to manipulate the values returned as allocated
  229. // USB pipes.
  230. //
  231. //*****************************************************************************
  232. #define EP_PIPE_USE_UDMA 0x01000000
  233. #define EP_PIPE_TYPE_ISOC 0x00800000
  234. #define EP_PIPE_TYPE_INTR 0x00400000
  235. #define EP_PIPE_TYPE_BULK 0x00200000
  236. #define EP_PIPE_TYPE_CONTROL 0x00100000
  237. #define EP_PIPE_TYPE_IN 0x00020000
  238. #define EP_PIPE_TYPE_OUT 0x00010000
  239. #define EP_PIPE_IDX_M 0x0000ffff
  240. //*****************************************************************************
  241. //
  242. // This creates a USB pipe handle from an index.
  243. //
  244. //*****************************************************************************
  245. #define OUT_PIPE_HANDLE(ulIndex, ulIdx) (g_sUSBHCD[ulIndex].USBOUTPipes[ulIdx].ulType | ulIdx)
  246. #define IN_PIPE_HANDLE(ulIndex, ulIdx) (g_sUSBHCD[ulIndex].USBINPipes[ulIdx].ulType | ulIdx)
  247. //*****************************************************************************
  248. //
  249. // Converts from an endpoint specifier to the offset of the endpoint's
  250. // control/status registers.
  251. //
  252. //*****************************************************************************
  253. #define EP_OFFSET(Endpoint) (Endpoint - 0x10)
  254. //*****************************************************************************
  255. //
  256. // The global to hold all of the state information for a given host controller.
  257. //
  258. //*****************************************************************************
  259. tUSBHCD g_sUSBHCD[USB_NUM_INSTANCE];
  260. //*****************************************************************************
  261. //
  262. // If there is an event driver this function will send out a generic connection
  263. // event USB_EVENT_CONNECTED indicating that an unknown connection event has
  264. // occurred.
  265. //
  266. //*****************************************************************************
  267. static void
  268. SendUnknownConnect(unsigned int ulIndex, unsigned int ulClass, unsigned int ulInstance)
  269. {
  270. if((g_sUSBHCD[ulIndex].iEventDriver != -1) &&
  271. (g_sUSBHCD[ulIndex].pClassDrivers[g_sUSBHCD[ulIndex]
  272. .iEventDriver]->pfnIntHandler))
  273. {
  274. //
  275. // Send the generic connected event.
  276. //
  277. g_sUSBHCD[ulIndex].EventInfo.ulEvent = USB_EVENT_CONNECTED;
  278. //
  279. // Save the class for later incase and application needs it.
  280. //
  281. g_sUSBHCD[ulIndex].ulClass = ulClass;
  282. g_sUSBHCD[ulIndex].EventInfo.ulInstance = ulInstance;
  283. g_sUSBHCD[ulIndex].pClassDrivers[g_sUSBHCD[ulIndex]
  284. .iEventDriver]->pfnIntHandler(&g_sUSBHCD[ulIndex].EventInfo);
  285. }
  286. }
  287. //*****************************************************************************
  288. //
  289. // Internal memory allocation space is two unsigned int values where each
  290. // bit represents a 64 byte block in the FIFO. This requires 64 bits for
  291. // the 4096 bytes of FIFO available.
  292. //
  293. //*****************************************************************************
  294. static unsigned int g_ulAlloc[2];
  295. //*****************************************************************************
  296. //
  297. // This function handles freeing FIFO memory that has been allocated using the
  298. // FIFOAlloc() function.
  299. //
  300. //*****************************************************************************
  301. static void
  302. FIFOFree(tUSBHCDPipe *pUSBPipe)
  303. {
  304. unsigned int ulMask;
  305. //
  306. // Calculate the mask value to use to clear off the allocated blocks used
  307. // by the USB pipe specified by pUSBPipe.
  308. //
  309. ulMask = (1 << (pUSBPipe->ucFIFOSize - 2)) - 1;
  310. ulMask = ulMask << pUSBPipe->ucFIFOBitOffset;
  311. //
  312. // Determine which 32 bit word to access based on the size.
  313. //
  314. if(pUSBPipe->ucFIFOSize > USB_FIFO_SZ_64)
  315. {
  316. //
  317. // If the FIFO size is greater than 64 then use the upper 32 bits.
  318. //
  319. g_ulAlloc[1] &= ~ulMask;
  320. }
  321. else
  322. {
  323. //
  324. // If the FIFO size is less than or equal to 64 then use the lower
  325. // 32 bits.
  326. //
  327. g_ulAlloc[0] &= ~ulMask;
  328. }
  329. }
  330. //*****************************************************************************
  331. //
  332. // This function is used to allocate FIFO memory to a given USB pipe.
  333. //
  334. // \param pUSBPipe is the USB pipe that needs FIFO memory allocated.
  335. // \param ulSize is the minimum size in bytes of the FIFO to allocate.
  336. //
  337. // This function will allocate \e ulSize bytes to the USB pipe in the
  338. // \e pUSBPipe parameter. The function will fill the pUSBPipe structure
  339. // members ucFIFOSize and ucFIFOAddr with values that can be used with the
  340. // USBFIFOConfigSet() API. This allocation uses a first fit algorithm.
  341. //
  342. // \return This function returns the size of the block allocated.
  343. //
  344. //*****************************************************************************
  345. static unsigned int
  346. FIFOAlloc(tUSBHCDPipe *pUSBPipe, unsigned int ulSize)
  347. {
  348. unsigned int ulBlocks, ulStart, ulBlockSize;
  349. unsigned short usFIFOAddr;
  350. unsigned int ulTemp, ulIndex;
  351. //
  352. // Save which 32 bit value to access, the upper is for blocks greater
  353. // than 64 and the lower is for block 64 or less.
  354. //
  355. if(ulSize > 64)
  356. {
  357. ulIndex = 1;
  358. }
  359. else
  360. {
  361. ulIndex = 0;
  362. }
  363. //
  364. // Initial FIFO address is 0.
  365. //
  366. usFIFOAddr = 0;
  367. //
  368. // Initialize the bit pattern and bit location.
  369. //
  370. ulBlocks = 1;
  371. ulStart = 0;
  372. //
  373. // The initial block size is always the minimum size of 64 bytes.
  374. //
  375. ulBlockSize = 64;
  376. //
  377. // The initial size and offset are 64 and 0.
  378. //
  379. pUSBPipe->ucFIFOBitOffset = 0;
  380. pUSBPipe->ucFIFOSize = 3;
  381. //
  382. // Scan through 32 bits looking for a memory block large enough to fill
  383. // the request.
  384. //
  385. while(usFIFOAddr <= 32)
  386. {
  387. //
  388. // If the pattern is zero then it is a possible match.
  389. //
  390. if((g_ulAlloc[ulIndex] & ulBlocks) == 0)
  391. {
  392. //
  393. // If the size is large enough then save it and break out of the
  394. // loop.
  395. //
  396. if(ulBlockSize >= ulSize)
  397. {
  398. //
  399. // Mark the memory as allocated.
  400. //
  401. g_ulAlloc[ulIndex] |= ulBlocks;
  402. break;
  403. }
  404. //
  405. // Increment the size of the FIFO block.
  406. //
  407. pUSBPipe->ucFIFOSize++;
  408. //
  409. // Add in a new bit to the size of the allocation.
  410. //
  411. ulBlocks = ulBlocks | (ulBlocks << 1) ;
  412. //
  413. // Double the current size.
  414. //
  415. ulBlockSize <<= 1;
  416. }
  417. else
  418. {
  419. //
  420. // Need to start over looking because the last allocation match
  421. // failed, so reset the bit offset to the current location and the
  422. // size to 64 bytes.
  423. //
  424. pUSBPipe->ucFIFOBitOffset = usFIFOAddr;
  425. pUSBPipe->ucFIFOSize = 3;
  426. //
  427. // Reset the block size to the minimum (64 bytes).
  428. //
  429. ulBlockSize = 64;
  430. //
  431. // Store the current starting bit location and set the block mask
  432. // to this value.
  433. //
  434. ulStart = 1 << usFIFOAddr;
  435. ulBlocks = ulStart;
  436. }
  437. //
  438. // Increase the address of the FIFO offset.
  439. //
  440. usFIFOAddr++;
  441. }
  442. //
  443. // If there was no block large enough then fail this call.
  444. //
  445. if(usFIFOAddr > 32)
  446. {
  447. ulBlockSize = 0;
  448. pUSBPipe->usFIFOAddr = 0;
  449. pUSBPipe->ucFIFOBitOffset = 0;
  450. pUSBPipe->ucFIFOSize = 0;
  451. }
  452. else
  453. {
  454. //
  455. // Calculate the offset in the FIFO.
  456. //
  457. ulTemp = pUSBPipe->ucFIFOBitOffset * 64;
  458. //
  459. // Sizes greater than 64 are allocated in the second half of the FIFO
  460. // memory space.
  461. //
  462. if(ulSize > 64)
  463. {
  464. ulTemp += 2048;
  465. }
  466. //
  467. // Convert this to the value that can be set in the USB controller.
  468. //
  469. pUSBPipe->usFIFOAddr = (unsigned short)ulTemp;
  470. }
  471. return(ulBlockSize);
  472. }
  473. //*****************************************************************************
  474. //
  475. //! This function is used to allocate a USB HCD pipe.
  476. //!
  477. //! \param ulIndex specifies which USB controller to use.
  478. //! \param ulEndpointType is the type of endpoint that this pipe will be
  479. //! communicating with.
  480. //! \param ulDevAddr is the device address to use for this endpoint.
  481. //! \param ulSize is the size of the FIFO in bytes.
  482. //! \param pfnCallback is the function that will be called when events occur on
  483. //! this USB Pipe.
  484. //!
  485. //! Since there are a limited number of USB HCD pipes that can be used in the
  486. //! host controller, this function is used to temporarily or permanently
  487. //! acquire one of the endpoints. Unlike the USBHCDPipeAlloc() function this
  488. //! function allows the caller to specify the size of the FIFO allocated to
  489. //! this endpoint in the \e ulSize parameter. This function also provides a
  490. //! method to register a callback for status changes on this endpoint. If no
  491. //! callbacks are desired then the \e pfnCallback function should be set to 0.
  492. //! The callback should be used when using the USBHCDPipeSchedule() function
  493. //! so that the caller is notified when the action is complete.
  494. //!
  495. //! \return This function returns a value indicating which pipe was reserved.
  496. //! If the value is 0 then there were no pipes currently available. This value
  497. //! should be passed to any USBHCDPipe APIs to indicate which pipe is being
  498. //! accessed.
  499. //
  500. //*****************************************************************************
  501. unsigned int
  502. USBHCDPipeAllocSize(unsigned int ulIndex, unsigned int ulEndpointType,
  503. unsigned int ulDevAddr, unsigned int ulSize,
  504. tHCDPipeCallback pfnCallback)
  505. {
  506. int iIdx;
  507. ASSERT(ulIndex == 0);
  508. //
  509. // Find a USB pipe that is free.
  510. //
  511. for(iIdx = 0; iIdx < MAX_NUM_PIPES; iIdx++)
  512. {
  513. //
  514. // Handle OUT Pipes.
  515. //
  516. if(ulEndpointType & EP_PIPE_TYPE_OUT)
  517. {
  518. //
  519. // A zero address indicates free.
  520. //
  521. if(g_sUSBHCD[ulIndex].USBOUTPipes[iIdx].ulDevAddr == 0)
  522. {
  523. //
  524. // Save the endpoint type and device address and callback
  525. // function.
  526. //
  527. g_sUSBHCD[ulIndex].USBOUTPipes[iIdx].ulType = ulEndpointType;
  528. g_sUSBHCD[ulIndex].USBOUTPipes[iIdx].ulDevAddr = ulDevAddr;
  529. g_sUSBHCD[ulIndex].USBOUTPipes[iIdx].pfnCallback = pfnCallback;
  530. g_sUSBHCD[ulIndex].USBOUTPipes[iIdx].ulEpMaxPacketSize = ulSize;
  531. //
  532. // Initialize the endpoint as idle.
  533. //
  534. g_sUSBHCD[ulIndex].USBOUTPipes[iIdx].eState = PIPE_IDLE;
  535. //
  536. // Allocate space in the FIFO for this endpoint.
  537. //
  538. if(FIFOAlloc(&g_sUSBHCD[ulIndex].USBOUTPipes[iIdx], ulSize) != 0)
  539. {
  540. //
  541. // Configure the FIFO.
  542. //
  543. USBFIFOConfigSet(g_USBInstance[ulIndex].uiBaseAddr ,
  544. INDEX_TO_USB_EP(iIdx + 1),
  545. g_sUSBHCD[ulIndex].USBOUTPipes[iIdx]
  546. .usFIFOAddr, g_sUSBHCD[ulIndex]
  547. .USBOUTPipes[iIdx].ucFIFOSize,
  548. USB_EP_HOST_OUT);
  549. }
  550. //
  551. // Set the function address for this endpoint.
  552. //
  553. USBHostAddrSet(g_USBInstance[ulIndex].uiBaseAddr,
  554. INDEX_TO_USB_EP(iIdx + 1), ulDevAddr,
  555. USB_EP_HOST_OUT);
  556. break;
  557. }
  558. }
  559. //
  560. // Handle IN Pipes.
  561. //
  562. else if(ulEndpointType & EP_PIPE_TYPE_IN)
  563. {
  564. //
  565. // A zero address indicates free.
  566. //
  567. if(g_sUSBHCD[ulIndex].USBINPipes[iIdx].ulDevAddr == 0)
  568. {
  569. //
  570. // Save the endpoint type and device address and callback
  571. // function.
  572. //
  573. g_sUSBHCD[ulIndex].USBINPipes[iIdx].ulType = ulEndpointType;
  574. g_sUSBHCD[ulIndex].USBINPipes[iIdx].ulDevAddr = ulDevAddr;
  575. g_sUSBHCD[ulIndex].USBINPipes[iIdx].pfnCallback = pfnCallback;
  576. g_sUSBHCD[ulIndex].USBINPipes[iIdx].ulEpMaxPacketSize = ulSize;
  577. //
  578. // Allocate space in the FIFO for this endpoint.
  579. //
  580. if(FIFOAlloc(&g_sUSBHCD[ulIndex].USBINPipes[iIdx], ulSize) != 0)
  581. {
  582. //
  583. // Configure the FIFO.
  584. //
  585. USBFIFOConfigSet(g_USBInstance[ulIndex].uiBaseAddr ,
  586. INDEX_TO_USB_EP(iIdx + 1),
  587. g_sUSBHCD[ulIndex].USBINPipes[iIdx]
  588. .usFIFOAddr, g_sUSBHCD[ulIndex]
  589. .USBINPipes[iIdx].ucFIFOSize,
  590. USB_EP_HOST_IN);
  591. }
  592. //
  593. // Set the function address for this endpoint.
  594. //
  595. USBHostAddrSet(g_USBInstance[ulIndex].uiBaseAddr ,
  596. INDEX_TO_USB_EP(iIdx + 1), ulDevAddr,
  597. USB_EP_HOST_IN);
  598. //
  599. // Reset the state of the pipe to idle.
  600. //
  601. g_sUSBHCD[ulIndex].USBINPipes[iIdx].eState = PIPE_IDLE;
  602. break;
  603. }
  604. }
  605. }
  606. //
  607. // Did not find a free pipe.
  608. //
  609. if(iIdx == MAX_NUM_PIPES)
  610. {
  611. return(0);
  612. }
  613. //
  614. // Return the pipe index and type that was allocated.
  615. //
  616. return(ulEndpointType | iIdx);
  617. }
  618. //*****************************************************************************
  619. //
  620. //! This function is used to allocate a USB HCD pipe.
  621. //!
  622. //! \param ulIndex specifies which USB controller to use.
  623. //! \param ulEndpointType is the type of endpoint that this pipe will be
  624. //! communicating with.
  625. //! \param ulDevAddr is the device address to use for this endpoint.
  626. //! \param pfnCallback is the function that will be called when events occur on
  627. //! this USB Pipe.
  628. //!
  629. //! Since there are a limited number of USB HCD pipes that can be used in the
  630. //! host controller, this function is used to temporarily or permanently
  631. //! acquire one of the endpoints. It also provides a method to register a
  632. //! callback for status changes on this endpoint. If no callbacks are desired
  633. //! then the \e pfnCallback function should be set to 0. The callback should
  634. //! be used when using the USBHCDPipeSchedule() function so that the caller is
  635. //! notified when the action is complete.
  636. //!
  637. //! \return This function returns a value indicating which pipe was reserved.
  638. //! If the value is 0 then there were no pipes currently available. This value
  639. //! should be passed to any USBHCDPipe APIs to indicate which pipe is being
  640. //! accessed.
  641. //
  642. //*****************************************************************************
  643. unsigned int
  644. USBHCDPipeAlloc(unsigned int ulIndex, unsigned int ulEndpointType,
  645. unsigned int ulDevAddr, tHCDPipeCallback pfnCallback)
  646. {
  647. //
  648. // The old API allocated only 64 bytes to each endpoint.
  649. //
  650. return(USBHCDPipeAllocSize(ulIndex, ulEndpointType, ulDevAddr, 64,
  651. pfnCallback));
  652. }
  653. //*****************************************************************************
  654. //
  655. //! This function is used to configure a USB HCD pipe.
  656. //!
  657. //! This should be called after allocating a USB pipe with a call to
  658. //! USBHCDPipeAlloc(). It is used to set the configuration associated with an
  659. //! endpoint like the max payload and target endpoint. The \e ulMaxPayload
  660. //! parameter is typically read directly from the devices endpoint descriptor
  661. //! and is expressed in bytes.
  662. //!
  663. //! Setting the \e ulInterval parameter depends on the type of endpoint being
  664. //! configured. For endpoints that do not need to use the \e ulInterval
  665. //! parameter \e ulInterval should be set to 0. For Bulk \e ulInterval is a
  666. //! value from 2-16 and will set the NAK timeout value as 2^(\e ulInterval-1)
  667. //! frames. For interrupt endpoints \e ulInterval is a value from 1-255 and
  668. //! is the count in frames between polling the endpoint. For isochronous
  669. //! endpoints \e ulInterval ranges from 1-16 and is the polling interval in
  670. //! frames represented as 2^(\e ulInterval-1) frames.
  671. //!
  672. //! \param ulPipe is the allocated endpoint to modify.
  673. //! \param ulMaxPayload is maximum data that can be handled per transaction.
  674. //! \param ulInterval is the polling interval for data transfers expressed in
  675. //! frames.
  676. //! \param ulTargetEndpoint is the target endpoint on the device to communicate
  677. //! with.
  678. //!
  679. //! \return If the call was successful, this function returns zero any other
  680. //! value indicates an error.
  681. //
  682. //*****************************************************************************
  683. unsigned int
  684. USBHCDPipeConfig(unsigned int devIndex, unsigned int ulPipe,
  685. unsigned int ulMaxPayload, unsigned int ulInterval,
  686. unsigned int ulTargetEndpoint)
  687. {
  688. unsigned int ulFlags;
  689. unsigned int ulIndex;
  690. //
  691. // Get the index number from the allocated pipe.
  692. //
  693. ulIndex = (ulPipe & EP_PIPE_IDX_M);
  694. //
  695. // Set the direction.
  696. //
  697. if(ulPipe & EP_PIPE_TYPE_OUT)
  698. {
  699. //
  700. // Set the mode for this endpoint.
  701. //
  702. if(g_sUSBHCD[devIndex].USBOUTPipes[ulIndex].ulType & EP_PIPE_TYPE_BULK)
  703. {
  704. ulFlags = USB_EP_MODE_BULK;
  705. }
  706. else if(g_sUSBHCD[devIndex].USBOUTPipes[ulIndex].ulType & EP_PIPE_TYPE_INTR)
  707. {
  708. ulFlags = USB_EP_MODE_INT;
  709. }
  710. else if(g_sUSBHCD[devIndex].USBOUTPipes[ulIndex].ulType & EP_PIPE_TYPE_ISOC)
  711. {
  712. ulFlags = USB_EP_MODE_ISOC;
  713. }
  714. else
  715. {
  716. ulFlags = USB_EP_MODE_CTRL;
  717. }
  718. ulFlags |= USB_EP_HOST_OUT;
  719. g_sUSBHCD[devIndex].USBOUTPipes[ulIndex].ucEPNumber =
  720. (unsigned char)ulTargetEndpoint;
  721. //
  722. // Save the interval and the next tick to trigger a scheduler event.
  723. //
  724. g_sUSBHCD[devIndex].USBOUTPipes[ulIndex].ulInterval = ulInterval;
  725. g_sUSBHCD[devIndex].USBOUTPipes[ulIndex].ulNextEventTick =
  726. ulInterval + g_sUSBHCD[devIndex].ulCurrentTick;
  727. //
  728. // Get pipe speed
  729. //
  730. g_sUSBHCD[devIndex].USBOUTPipes[ulIndex].ulPipeSpeed =
  731. g_sUSBHCD[devIndex].USBDevice[ulIndex].ulDeviceSpeed;
  732. //
  733. // Speed configuration in type0 register
  734. //
  735. switch(g_sUSBHCD[devIndex].USBDevice[ulIndex].ulDeviceSpeed)
  736. {
  737. case USB_HIGH_SPEED:
  738. ulFlags |= USB_EP_SPEED_HIGH;
  739. break;
  740. case USB_FULL_SPEED:
  741. ulFlags |= USB_EP_SPEED_FULL;
  742. break;
  743. default:
  744. ulFlags |= USB_EP_SPEED_LOW;
  745. break;
  746. }
  747. }
  748. else
  749. {
  750. //
  751. // Set the mode for this endpoint.
  752. //
  753. if(g_sUSBHCD[devIndex].USBINPipes[ulIndex].ulType & EP_PIPE_TYPE_BULK)
  754. {
  755. ulFlags = USB_EP_MODE_BULK;
  756. }
  757. else if(g_sUSBHCD[devIndex].USBINPipes[ulIndex].ulType & EP_PIPE_TYPE_INTR)
  758. {
  759. ulFlags = USB_EP_MODE_INT;
  760. }
  761. else if(g_sUSBHCD[devIndex].USBINPipes[ulIndex].ulType & EP_PIPE_TYPE_ISOC)
  762. {
  763. ulFlags = USB_EP_MODE_ISOC;
  764. }
  765. else
  766. {
  767. ulFlags = USB_EP_MODE_CTRL;
  768. }
  769. ulFlags |= USB_EP_HOST_IN;
  770. g_sUSBHCD[devIndex].USBINPipes[ulIndex].ucEPNumber =
  771. (unsigned char)ulTargetEndpoint;
  772. //
  773. // Save the interval and the next tick to trigger a scheduler event.
  774. //
  775. g_sUSBHCD[devIndex].USBINPipes[ulIndex].ulInterval = ulInterval;
  776. g_sUSBHCD[devIndex].USBINPipes[ulIndex].ulNextEventTick =
  777. ulInterval + g_sUSBHCD[devIndex].ulCurrentTick;
  778. //
  779. // Get pipe speed
  780. //
  781. g_sUSBHCD[devIndex].USBINPipes[ulIndex].ulPipeSpeed =
  782. g_sUSBHCD[devIndex].USBDevice[ulIndex].ulDeviceSpeed;
  783. //
  784. // Speed configuration in type0 register
  785. //
  786. switch(g_sUSBHCD[devIndex].USBDevice[ulIndex].ulDeviceSpeed)
  787. {
  788. case USB_HIGH_SPEED:
  789. ulFlags |= USB_EP_SPEED_HIGH;
  790. break;
  791. case USB_FULL_SPEED:
  792. ulFlags |= USB_EP_SPEED_FULL;
  793. break;
  794. default:
  795. ulFlags |= USB_EP_SPEED_LOW;
  796. break;
  797. }
  798. }
  799. //
  800. // Configure the endpoint according to the flags determined above.
  801. //
  802. USBHostEndpointConfig(g_USBInstance[devIndex].uiBaseAddr ,
  803. INDEX_TO_USB_EP((ulPipe & EP_PIPE_IDX_M) + 1),
  804. ulMaxPayload, ulInterval, ulTargetEndpoint,
  805. ulFlags);
  806. return(0);
  807. }
  808. //*****************************************************************************
  809. //
  810. //! This function is used to return the current status of a USB HCD pipe.
  811. //!
  812. //! This function will return the current status for a given USB pipe. If
  813. //! there is no status to report this call will simply return
  814. //! \b USBHCD_PIPE_NO_CHANGE.
  815. //!
  816. //! \param ulPipe is the USB pipe for this status request.
  817. //!
  818. //! \return This function returns the current status for the given endpoint.
  819. //! This will be one of the \b USBHCD_PIPE_* values.
  820. //
  821. //*****************************************************************************
  822. unsigned int
  823. USBHCDPipeStatus(unsigned int ulPipe)
  824. {
  825. return(0);
  826. }
  827. //*****************************************************************************
  828. //
  829. //! This function is used to write data to a USB HCD pipe.
  830. //!
  831. //! \param ulPipe is the USB pipe to put data into.
  832. //! \param pucData is a pointer to the data to send.
  833. //! \param ulSize is the amount of data to send.
  834. //!
  835. //! This function will block until it has sent as much data as was
  836. //! requested using the USB pipe's FIFO. The caller should have registered a
  837. //! callback with the USBHCDPipeAlloc() call in order to be informed when the
  838. //! data has been transmitted. The value returned by this function can be less
  839. //! than the \e ulSize requested if the USB pipe has less space available than
  840. //! this request is making.
  841. //!
  842. //! \return This function returns the number of bytes that were scheduled to
  843. //! be sent on the given USB pipe.
  844. //
  845. //*****************************************************************************
  846. unsigned int
  847. USBHCDPipeWrite(unsigned int ulIndex, unsigned int ulPipe,
  848. unsigned char *pucData, unsigned int ulSize)
  849. {
  850. unsigned int ulEndpoint;
  851. unsigned int ulRemainingBytes;
  852. unsigned int ulByteToSend;
  853. unsigned int ulPipeIdx;
  854. unsigned int ulEPStatus;
  855. unsigned int ulConnectRetry;
  856. #ifdef DMA_MODE
  857. unsigned int txBuffer;
  858. unsigned int nBlocks;
  859. #endif
  860. unsigned int ulTimer = 0;
  861. //
  862. // Determine which endpoint interface that this pipe is using.
  863. //
  864. ulEndpoint = INDEX_TO_USB_EP((EP_PIPE_IDX_M & ulPipe) + 1);
  865. //
  866. // Get index used for looking up pipe data
  867. //
  868. ulPipeIdx = ulPipe & EP_PIPE_IDX_M;
  869. //
  870. // Set the total number of bytes to send out.
  871. //
  872. ulRemainingBytes = ulSize;
  873. //
  874. // Send all of the requested data.
  875. //
  876. while(ulRemainingBytes != 0)
  877. {
  878. //
  879. // Start a write request.
  880. //
  881. g_sUSBHCD[ulIndex].USBOUTPipes[ulPipeIdx].eState = PIPE_WRITING;
  882. //
  883. //Calculate the Number of blocks to Transmit
  884. //
  885. if(ulRemainingBytes <=
  886. (g_sUSBHCD[ulIndex].USBOUTPipes[ulPipeIdx].ulEpMaxPacketSize))
  887. {
  888. ulByteToSend = ulRemainingBytes;
  889. }
  890. else
  891. {
  892. ulByteToSend =
  893. g_sUSBHCD[ulIndex].USBOUTPipes[ulPipeIdx].ulEpMaxPacketSize;
  894. }
  895. #ifdef DMA_MODE
  896. nBlocks = 1;
  897. //
  898. //Allocate enough buffer
  899. //
  900. txBuffer=(unsigned int)cppiDmaAllocnBuffer(nBlocks);
  901. ASSERT(NULL!=txBuffer);
  902. //
  903. // Set pending transmit DMA flag
  904. //
  905. g_sUSBHCD[ulIndex].ulDMAPending |= DMA_PEND_TRANSMIT_FLAG << ulPipeIdx;
  906. //
  907. //Copy the data to the TX buffer
  908. //
  909. memcpy((unsigned char *)txBuffer, pucData, ulByteToSend);
  910. //
  911. //Load the DMA queue with the data buffer
  912. //
  913. doDmaTxTransfer(ulIndex, (unsigned char *)txBuffer,
  914. ulByteToSend, ulEndpoint);
  915. //
  916. //Enable the DMA for TX operation
  917. //
  918. enableCoreTxDMA(ulIndex, ulEndpoint);
  919. #else
  920. //
  921. // Put the data in the buffer.
  922. //
  923. USBEndpointDataPut(g_USBInstance[ulIndex].uiBaseAddr , ulEndpoint,
  924. pucData, ulByteToSend);
  925. //
  926. // Schedule the data to be sent.
  927. //
  928. USBEndpointDataSend(g_USBInstance[ulIndex].uiBaseAddr , ulEndpoint,
  929. USB_TRANS_OUT);
  930. #endif
  931. //
  932. // Wait for a status change.
  933. //
  934. if(USB_TIMEOUT_DISABLE!=g_sUSBHCD[ulIndex].USBHTimeOut.Value.slNonEP0)
  935. {
  936. ulTimer = g_sUSBHCD[ulIndex].USBHTimeOut.Value.slNonEP0;
  937. StartTimer(ulTimer);
  938. }
  939. while(g_sUSBHCD[ulIndex].USBOUTPipes[ulPipeIdx].eState == PIPE_WRITING)
  940. {
  941. //
  942. // Exit the loop for any of the event(s): Disconnect.
  943. //
  944. if(g_sUSBHCD[ulIndex].ulUSBHIntEvents & INT_EVENT_DISCONNECT)
  945. {
  946. //
  947. // Update the Pipe status
  948. //
  949. g_sUSBHCD[ulIndex].USBOUTPipes[ulPipeIdx].eState = PIPE_ERROR;
  950. continue;
  951. }
  952. else if(g_sUSBHCD[ulIndex].ulUSBHIntEvents & INT_EVENT_BABBLE_FAULT)
  953. {
  954. //
  955. // Update the Pipe status
  956. //
  957. g_sUSBHCD[ulIndex].USBOUTPipes[ulPipeIdx].eState = PIPE_ERROR;
  958. continue;
  959. }
  960. //
  961. // Read endpoint status.
  962. //
  963. ulEPStatus = USBEndpointStatus(g_USBInstance[ulIndex].uiBaseAddr,
  964. ulEndpoint);
  965. //
  966. // Check for stall condition occurence.
  967. //
  968. if(ulEPStatus & USB_HOST_OUT_STALL)
  969. {
  970. g_sUSBHCD[ulIndex].USBOUTPipes[ulPipeIdx].eState = PIPE_STALLED;
  971. continue;
  972. }
  973. else if(ulEPStatus & USB_HOST_OUT_ERROR)
  974. {
  975. g_sUSBHCD[ulIndex].USBOUTPipes[ulPipeIdx].eState = PIPE_ERROR;
  976. continue;
  977. }
  978. if(USB_TIMEOUT_DISABLE!=g_sUSBHCD[ulIndex].USBHTimeOut.Value.slNonEP0)
  979. {
  980. if(IsTimerElapsed())
  981. {
  982. ulTimer = 0;
  983. g_sUSBHCD[ulIndex].USBOUTPipes[ulPipeIdx].eState = PIPE_ERROR;
  984. g_sUSBHCD[ulIndex].USBHTimeOut.Status.slNonEP0 = (1<<(ulPipeIdx + 1));
  985. break;
  986. }
  987. }
  988. }
  989. if(USB_TIMEOUT_DISABLE!=g_sUSBHCD[ulIndex].USBHTimeOut.Value.slNonEP0)
  990. {
  991. StopTimer();
  992. ulTimer = 0;
  993. }
  994. //
  995. // If the data was successfully sent then decrement the count and
  996. // continue.
  997. //
  998. if(g_sUSBHCD[ulIndex].USBOUTPipes[ulPipeIdx].eState == PIPE_DATA_SENT)
  999. {
  1000. #ifdef DMA_MODE
  1001. disableCoreTxDMA(g_USBInstance[ulIndex].uiUSBInstance, ulEndpoint);
  1002. //
  1003. //Free the TX buffer
  1004. //
  1005. cppiDmaFreenBuffer((unsigned int *)txBuffer);
  1006. #endif
  1007. //
  1008. // Decrement the remaining data and advance the pointer.
  1009. //
  1010. ulRemainingBytes -= ulByteToSend;
  1011. pucData += ulByteToSend;
  1012. }
  1013. else if(g_sUSBHCD[ulIndex].USBOUTPipes[ulPipeIdx].eState == PIPE_STALLED)
  1014. {
  1015. //
  1016. // Zero out the size so that the caller knows that no data was
  1017. // written.
  1018. //
  1019. ulSize = 0;
  1020. USBHCDTxAbort(ulIndex, ulEndpoint);
  1021. //
  1022. // This is the actual endpoint number.
  1023. //
  1024. USBHCDClearFeature(ulIndex, 1, ulPipe, USB_FEATURE_EP_HALT);
  1025. //
  1026. // If there was a stall, then no more data is coming so break out.
  1027. //
  1028. break;
  1029. }
  1030. else if(g_sUSBHCD[ulIndex].USBOUTPipes[ulPipeIdx].eState == PIPE_ERROR)
  1031. {
  1032. // Clean-up the EP FIFOs and CPPIDMA (if applicable)
  1033. USBHCDTxAbort(ulIndex, ulEndpoint);
  1034. if(USB_TIMEOUT_DISABLE!=g_sUSBHCD[ulIndex].USBHTimeOut.Value.slNonEP0)
  1035. {
  1036. if(!ulTimer)
  1037. {
  1038. // Try re-establising the connection with device
  1039. ulConnectRetry = g_sUSBHCD[ulIndex].ulConnectRetry;
  1040. for(;ulConnectRetry;--ulConnectRetry)
  1041. {
  1042. if(USBHCDRetryConnect(ulIndex))
  1043. {
  1044. break;
  1045. }
  1046. }
  1047. // Abort the transfer if re-establising the connection with device failed.
  1048. if(!ulConnectRetry)
  1049. {
  1050. //
  1051. // Set the size to 0 to indicate Error.
  1052. //
  1053. ulSize = 0;
  1054. break;
  1055. }
  1056. }
  1057. }
  1058. else
  1059. {
  1060. //
  1061. // Set the size to 0 to indicate Error.
  1062. //
  1063. ulSize = 0;
  1064. break;
  1065. }
  1066. }
  1067. //
  1068. // If there are less than 64 bytes to send then this is the last
  1069. // of the data to go out.
  1070. //
  1071. if(ulRemainingBytes < 64)
  1072. {
  1073. ulByteToSend = ulRemainingBytes;
  1074. }
  1075. }
  1076. //
  1077. // Go Idle once this state has been reached.
  1078. //
  1079. if(!ulRemainingBytes)
  1080. {
  1081. g_sUSBHCD[ulIndex].USBOUTPipes[ulPipeIdx].eState = PIPE_IDLE;
  1082. }
  1083. return(ulSize);
  1084. }
  1085. //*****************************************************************************
  1086. //
  1087. //! This function is used to schedule and IN transaction on a USB HCD pipe.
  1088. //!
  1089. //! \param ulPipe is the USB pipe to read data from.
  1090. //! \param pucData is a pointer to store the data that is received.
  1091. //! \param ulSize is the size in bytes of the buffer pointed to by pucData.
  1092. //!
  1093. //! This function will not block depending on the type of pipe passed in will
  1094. //! schedule either a send of data to the device or a read of data from the
  1095. //! device. In either case the amount of data will be limited to what will
  1096. //! fit in the FIFO for a given endpoint.
  1097. //!
  1098. //! \return This function returns the number of bytes that sent in the case
  1099. //! of a transfer of data or it will return 0 for a request on a USB IN pipe.
  1100. //
  1101. //*****************************************************************************
  1102. unsigned int
  1103. USBHCDPipeSchedule(unsigned int ulIndex, unsigned int ulPipe,
  1104. unsigned char *pucData, unsigned int ulSize)
  1105. {
  1106. unsigned int ulEndpoint;
  1107. //
  1108. // Determine which endpoint interface that this pipe is using.
  1109. //
  1110. ulEndpoint = INDEX_TO_USB_EP((EP_PIPE_IDX_M & ulPipe) + 1);
  1111. if(ulPipe & EP_PIPE_TYPE_OUT)
  1112. {
  1113. //
  1114. // Start a write request.
  1115. //
  1116. g_sUSBHCD[ulIndex].USBOUTPipes[EP_PIPE_IDX_M & ulPipe].eState = PIPE_WRITING;
  1117. //
  1118. // Put the data in the buffer.
  1119. //
  1120. USBEndpointDataPut(g_USBInstance[ulIndex].uiBaseAddr , ulEndpoint, pucData, ulSize);
  1121. //
  1122. // Schedule the data to be sent.
  1123. //
  1124. USBEndpointDataSend(g_USBInstance[ulIndex].uiBaseAddr , ulEndpoint, USB_TRANS_OUT);
  1125. }
  1126. else
  1127. {
  1128. //
  1129. // Start a read request.
  1130. //
  1131. g_sUSBHCD[ulIndex].USBINPipes[EP_PIPE_IDX_M & ulPipe].eState = PIPE_READING;
  1132. //
  1133. // Trigger a request for data from the device.
  1134. //
  1135. USBHostRequestIN(g_USBInstance[ulIndex].uiBaseAddr , ulEndpoint);
  1136. //
  1137. // No data was put into or read from the buffer.
  1138. //
  1139. ulSize = 0;
  1140. }
  1141. return(ulSize);
  1142. }
  1143. //*****************************************************************************
  1144. //
  1145. //! This function is used to read data from a USB HCD pipe.
  1146. //!
  1147. //! \param ulPipe is the USB pipe to read data from.
  1148. //! \param pucData is a pointer to store the data that is received.
  1149. //! \param ulSize is the size in bytes of the buffer pointed to by pucData.
  1150. //!
  1151. //! This function will not block and will only read as much data as requested
  1152. //! or as much data is currently available from the USB pipe. The caller
  1153. //! should have registered a callback with the USBHCDPipeAlloc() call in order
  1154. //! to be informed when the data has been received. The value returned by this
  1155. //! function can be less than the \e ulSize requested if the USB pipe has less
  1156. //! data available than was requested.
  1157. //!
  1158. //! \return This function returns the number of bytes that were returned in the
  1159. //! \e pucData buffer.
  1160. //
  1161. //*****************************************************************************
  1162. unsigned int
  1163. USBHCDPipeReadNonBlocking(unsigned int ulIndex, unsigned int ulPipe,
  1164. unsigned char *pucData, unsigned int ulSize)
  1165. {
  1166. unsigned int ulEndpoint;
  1167. //
  1168. // Determine which endpoint interface that this pipe is using.
  1169. //
  1170. ulEndpoint = INDEX_TO_USB_EP((EP_PIPE_IDX_M & ulPipe) + 1);
  1171. //
  1172. // Read the data out of the USB endpoint interface.
  1173. //
  1174. USBEndpointDataGet(g_USBInstance[ulIndex].uiBaseAddr , ulEndpoint, pucData, &ulSize);
  1175. //
  1176. // Acknowledge that the data was read from the endpoint.
  1177. //
  1178. USBHostEndpointDataAck(g_USBInstance[ulIndex].uiBaseAddr , ulEndpoint);
  1179. //
  1180. // Go Idle once this state has been reached.
  1181. //
  1182. g_sUSBHCD[ulIndex].USBINPipes[EP_PIPE_IDX_M & ulPipe].eState = PIPE_IDLE;
  1183. return(ulSize);
  1184. }
  1185. //*****************************************************************************
  1186. //
  1187. //! This function is used to read data from a USB HCD pipe.
  1188. //!
  1189. //! \param ulPipe is the USB pipe to read data from.
  1190. //! \param pucData is a pointer to store the data that is received.
  1191. //! \param ulSize is the size in bytes of the buffer pointed to by pucData.
  1192. //!
  1193. //! This function will block and will only return when it has read as much data
  1194. //! as requested from the USB pipe. The caller should have registered a
  1195. //! callback with the USBHCDPipeAlloc() call in order to be informed when the
  1196. //! data has been received. The value returned by this function can be less
  1197. //! than the \e ulSize requested if the USB pipe has less data available than
  1198. //! was requested.
  1199. //!
  1200. //! \return This function returns the number of bytes that were returned in the
  1201. //! \e pucData buffer.
  1202. //
  1203. //*****************************************************************************
  1204. unsigned int
  1205. USBHCDPipeRead(unsigned int ulIndex, unsigned int ulPipe,
  1206. unsigned char *pucData, unsigned int ulSize)
  1207. {
  1208. unsigned int ulEndpoint;
  1209. unsigned int ulRemainingBytes;
  1210. unsigned int ulPipeIdx;
  1211. unsigned int ulEPStatus;
  1212. unsigned int ulConnectRetry;
  1213. #ifdef DMA_MODE
  1214. unsigned int ulLength;
  1215. #else
  1216. unsigned int ulBytesRead = 0;
  1217. #endif
  1218. unsigned int ulTimer = 0;
  1219. //
  1220. // Get index used for looking up pipe data
  1221. //
  1222. ulPipeIdx = ulPipe & EP_PIPE_IDX_M;
  1223. //
  1224. // Determine which endpoint interface that this pipe is using.
  1225. //
  1226. ulEndpoint = INDEX_TO_USB_EP(ulPipeIdx + 1);
  1227. //
  1228. // Set the remaining bytes to received.
  1229. //
  1230. ulRemainingBytes = ulSize;
  1231. //
  1232. // Continue until all data requested has been received.
  1233. //
  1234. #ifdef DMA_MODE
  1235. //
  1236. //Calculate the Number of blocks requested
  1237. //
  1238. if(ulSize <=
  1239. (g_sUSBHCD[ulIndex].USBINPipes[ulPipeIdx].ulEpMaxPacketSize))
  1240. {
  1241. ulLength = ulSize;
  1242. }
  1243. else
  1244. {
  1245. ulLength =
  1246. (g_sUSBHCD[ulIndex].USBINPipes[ulPipeIdx].ulEpMaxPacketSize);
  1247. }
  1248. #endif
  1249. while(ulRemainingBytes != 0)
  1250. {
  1251. #ifdef DMA_MODE
  1252. //
  1253. // Give enough buffer to DMA
  1254. //
  1255. g_sUSBHCD[ulIndex].rxBuffer = (unsigned char *)cppiDmaAllocBuffer();
  1256. ASSERT(NULL!=g_sUSBHCD[ulIndex].rxBuffer);
  1257. doDmaRxTransfer(ulIndex, ulLength, g_sUSBHCD[ulIndex].rxBuffer,
  1258. ulEndpoint);
  1259. //
  1260. //Enable the DMA
  1261. //
  1262. enableCoreRxDMA(g_USBInstance[ulIndex].uiUSBInstance, ulEndpoint);
  1263. #endif
  1264. //
  1265. // Start a read request.
  1266. //
  1267. g_sUSBHCD[ulIndex].USBINPipes[ulPipeIdx].eState = PIPE_READING;
  1268. //
  1269. // Set pending DMA flag
  1270. //
  1271. g_sUSBHCD[ulIndex].ulDMAPending |= DMA_PEND_RECEIVE_FLAG << ulPipeIdx;
  1272. //
  1273. // Trigger a request for data from the device.
  1274. //
  1275. USBHostRequestIN(g_USBInstance[ulIndex].uiBaseAddr , ulEndpoint);
  1276. //
  1277. // Wait for a status change.
  1278. //
  1279. if(USB_TIMEOUT_DISABLE!=g_sUSBHCD[ulIndex].USBHTimeOut.Value.slNonEP0)
  1280. {
  1281. ulTimer = g_sUSBHCD[ulIndex].USBHTimeOut.Value.slNonEP0;
  1282. StartTimer(ulTimer);
  1283. }
  1284. while(g_sUSBHCD[ulIndex].USBINPipes[ulPipeIdx].eState == PIPE_READING)
  1285. {
  1286. //
  1287. // Exit the loop for any of the event(s): Disconnect.
  1288. //
  1289. if(g_sUSBHCD[ulIndex].ulUSBHIntEvents & INT_EVENT_DISCONNECT)
  1290. {
  1291. //
  1292. // Update the Pipe status
  1293. //
  1294. g_sUSBHCD[ulIndex].USBINPipes[ulPipeIdx].eState = PIPE_ERROR;
  1295. continue;
  1296. }
  1297. else if(g_sUSBHCD[ulIndex].ulUSBHIntEvents & INT_EVENT_BABBLE_FAULT)
  1298. {
  1299. //
  1300. // Update the Pipe status
  1301. //
  1302. g_sUSBHCD[ulIndex].USBINPipes[ulPipeIdx].eState = PIPE_ERROR;
  1303. continue;
  1304. }
  1305. //
  1306. // Read endpoint status.
  1307. //
  1308. ulEPStatus = USBEndpointStatus(g_USBInstance[ulIndex].uiBaseAddr,
  1309. ulEndpoint);
  1310. //
  1311. // Check for stall condition occurence.
  1312. //
  1313. if(ulEPStatus & USB_HOST_IN_STALL)
  1314. {
  1315. g_sUSBHCD[ulIndex].USBINPipes[ulPipeIdx].eState = PIPE_STALLED;
  1316. continue;
  1317. }
  1318. else if(ulEPStatus & USB_HOST_IN_ERROR)
  1319. {
  1320. g_sUSBHCD[ulIndex].USBINPipes[ulPipeIdx].eState = PIPE_ERROR;
  1321. continue;
  1322. }
  1323. if(USB_TIMEOUT_DISABLE!=g_sUSBHCD[ulIndex].USBHTimeOut.Value.slNonEP0)
  1324. {
  1325. if(IsTimerElapsed())
  1326. {
  1327. ulTimer = 0;
  1328. g_sUSBHCD[ulIndex].USBINPipes[ulPipeIdx].eState = PIPE_ERROR;
  1329. g_sUSBHCD[ulIndex].USBHTimeOut.Status.slNonEP0 = (1<<((ulPipeIdx + 1)+16));
  1330. break;
  1331. }
  1332. }
  1333. }
  1334. if(USB_TIMEOUT_DISABLE!=g_sUSBHCD[ulIndex].USBHTimeOut.Value.slNonEP0)
  1335. {
  1336. StopTimer();
  1337. ulTimer = 0;
  1338. }
  1339. //
  1340. // If data is ready then return it.
  1341. //
  1342. if(g_sUSBHCD[ulIndex].USBINPipes[ulPipeIdx].eState == PIPE_DATA_READY)
  1343. {
  1344. #ifdef DMA_MODE
  1345. disableCoreRxDMA(g_USBInstance[ulIndex].uiUSBInstance, ulEndpoint);
  1346. //
  1347. //Copy the data from DMA buffer to application buffer
  1348. //
  1349. //memset (pucData, 0, ulLength);
  1350. memcpy(pucData, g_sUSBHCD[ulIndex].rxBuffer, ulLength);
  1351. //
  1352. //Free the DMA Buffer
  1353. //
  1354. cppiDmaFreeBuffer((unsigned int * )g_sUSBHCD[ulIndex].rxBuffer);
  1355. //
  1356. //Claculate the packet length
  1357. //
  1358. if(ulRemainingBytes <=
  1359. (g_sUSBHCD[ulIndex].USBINPipes[ulPipeIdx].ulEpMaxPacketSize))
  1360. {
  1361. ulRemainingBytes = ulRemainingBytes - ulLength;
  1362. }
  1363. else
  1364. {
  1365. ulRemainingBytes = ulRemainingBytes - ulLength;
  1366. pucData += ulLength;
  1367. }
  1368. //
  1369. //Update the Pipe status
  1370. //
  1371. if(ulRemainingBytes != 0)
  1372. {
  1373. g_sUSBHCD[ulIndex].USBINPipes[ulPipeIdx].eState = PIPE_READING;
  1374. }
  1375. #else
  1376. //
  1377. // Request all of the remaining bytes.
  1378. //
  1379. ulBytesRead = ulRemainingBytes;
  1380. //
  1381. // Read the data out of the USB endpoint interface.
  1382. //
  1383. USBEndpointDataGet(g_USBInstance[ulIndex].uiBaseAddr , ulEndpoint, pucData,
  1384. &ulBytesRead);
  1385. //
  1386. // Acknowledge that the data was read from the endpoint.
  1387. //
  1388. USBHostEndpointDataAck(g_USBInstance[ulIndex].uiBaseAddr , ulEndpoint);
  1389. //
  1390. // Subtract the number of bytes read from the bytes remaining.
  1391. //
  1392. ulRemainingBytes -= ulBytesRead;
  1393. //
  1394. // If there were less than 64 bytes read, then this was a short
  1395. // packet and no more data will be returned.
  1396. //
  1397. if(ulBytesRead < g_sUSBHCD[ulIndex].USBINPipes[ulPipeIdx].ulEpMaxPacketSize)
  1398. {
  1399. //
  1400. // Subtract off the bytes that were not received and exit the
  1401. // loop.
  1402. //
  1403. ulSize = ulSize - ulRemainingBytes;
  1404. break;
  1405. }
  1406. else
  1407. {
  1408. //
  1409. // Move the buffer ahead to receive more data into the buffer.
  1410. //
  1411. pucData += g_sUSBHCD[ulIndex].USBINPipes[ulPipeIdx].ulEpMaxPacketSize;
  1412. }
  1413. #endif
  1414. }
  1415. else if(g_sUSBHCD[ulIndex].USBINPipes[ulPipeIdx].eState == PIPE_STALLED)
  1416. {
  1417. USBHCDRxAbort(ulIndex, ulEndpoint);
  1418. //
  1419. // Zero out the size so that the caller knows that no data was read.
  1420. //
  1421. ulSize = 0;
  1422. //
  1423. // This is the actual endpoint number.
  1424. //
  1425. USBHCDClearFeature(ulIndex, 1, ulPipe, USB_FEATURE_EP_HALT);
  1426. //
  1427. // If there was a stall, then no more data is coming so break out.
  1428. //
  1429. break;
  1430. }
  1431. else if(g_sUSBHCD[ulIndex].USBINPipes[ulPipeIdx].eState == PIPE_ERROR)
  1432. {
  1433. // Clean-up the EP FIFOs and CPPIDMA (if applicable)
  1434. USBHCDRxAbort(ulIndex, ulEndpoint);
  1435. if(USB_TIMEOUT_DISABLE!=g_sUSBHCD[ulIndex].USBHTimeOut.Value.slNonEP0)
  1436. {
  1437. if(!ulTimer)
  1438. {
  1439. // Try re-establising the connection with device
  1440. ulConnectRetry = g_sUSBHCD[ulIndex].ulConnectRetry;
  1441. for(;ulConnectRetry;--ulConnectRetry)
  1442. {
  1443. if(USBHCDRetryConnect(ulIndex))
  1444. {
  1445. break;
  1446. }
  1447. }
  1448. // Abort the transfer if re-establising the connection with device failed.
  1449. if(!ulConnectRetry)
  1450. {
  1451. //
  1452. // Set the size to 0 to indicate Error.
  1453. //
  1454. ulSize = 0;
  1455. break;
  1456. }
  1457. }
  1458. }
  1459. else
  1460. {
  1461. //
  1462. // Set the size to 0 to indicate Error.
  1463. //
  1464. ulSize = 0;
  1465. break;
  1466. }
  1467. }
  1468. }
  1469. //
  1470. // Go Idle once this state has been reached.
  1471. //
  1472. if(!ulRemainingBytes)
  1473. {
  1474. g_sUSBHCD[ulIndex].USBINPipes[ulPipeIdx].eState = PIPE_IDLE;
  1475. }
  1476. return(ulSize);
  1477. }
  1478. //*****************************************************************************
  1479. //
  1480. //! This function is used to release a USB pipe.
  1481. //!
  1482. //! \param ulPipe is the allocated USB pipe to release.
  1483. //!
  1484. //! This function is used to release a USB pipe that was allocated by a call to
  1485. //! USBHCDPipeAlloc() for use by some other device endpoint in the system.
  1486. //! Freeing an unallocated or invalid pipe will not generate an error and will
  1487. //! instead simply return.
  1488. //!
  1489. //! \return None.
  1490. //
  1491. //*****************************************************************************
  1492. void
  1493. USBHCDPipeFree(unsigned int ulIndex, unsigned int ulPipe)
  1494. {
  1495. int iDMAIdx;
  1496. if(ulPipe & EP_PIPE_TYPE_OUT)
  1497. {
  1498. //
  1499. // Clear the address and type for this endpoint to free it up.
  1500. //
  1501. g_sUSBHCD[ulIndex].USBOUTPipes[ulPipe & EP_PIPE_IDX_M].ulDevAddr = 0;
  1502. g_sUSBHCD[ulIndex].USBOUTPipes[ulPipe & EP_PIPE_IDX_M].ulType = 0;
  1503. g_sUSBHCD[ulIndex].USBOUTPipes[ulPipe & EP_PIPE_IDX_M].pfnCallback = 0;
  1504. //
  1505. // Get the dma channel used by this pipe.
  1506. //
  1507. iDMAIdx = g_sUSBHCD[ulIndex].USBOUTPipes[ulPipe & EP_PIPE_IDX_M].ucDMAChannel;
  1508. //
  1509. // Mark the channel as free for use.
  1510. //
  1511. g_sUSBHCD[ulIndex].ucDMAChannels[iDMAIdx] = USBHCD_DMA_UNUSED;
  1512. //
  1513. // Clear out the current channel in use by this pipe.
  1514. //
  1515. g_sUSBHCD[ulIndex].USBOUTPipes[ulPipe & EP_PIPE_IDX_M].ucDMAChannel =
  1516. USBHCD_DMA_UNUSED;
  1517. //
  1518. // Free up the FIFO memory used by this endpoint.
  1519. //
  1520. if(g_sUSBHCD[ulIndex].USBOUTPipes[ulPipe & EP_PIPE_IDX_M].ucFIFOSize)
  1521. {
  1522. FIFOFree(&g_sUSBHCD[ulIndex].USBOUTPipes[ulPipe & EP_PIPE_IDX_M]);
  1523. }
  1524. }
  1525. else if(ulPipe & EP_PIPE_TYPE_IN)
  1526. {
  1527. //
  1528. // Clear the address and type for this endpoint to free it up.
  1529. //
  1530. g_sUSBHCD[ulIndex].USBINPipes[ulPipe & EP_PIPE_IDX_M].ulDevAddr = 0;
  1531. g_sUSBHCD[ulIndex].USBINPipes[ulPipe & EP_PIPE_IDX_M].ulType = 0;
  1532. g_sUSBHCD[ulIndex].USBINPipes[ulPipe & EP_PIPE_IDX_M].pfnCallback = 0;
  1533. //
  1534. // Get the dma channel used by this pipe.
  1535. //
  1536. iDMAIdx = g_sUSBHCD[ulIndex].USBINPipes[ulPipe & EP_PIPE_IDX_M].ucDMAChannel;
  1537. //
  1538. // Mark the channel as free for use.
  1539. //
  1540. g_sUSBHCD[ulIndex].ucDMAChannels[iDMAIdx] = USBHCD_DMA_UNUSED;
  1541. //
  1542. // Clear out the current channel in use by this pipe.
  1543. //
  1544. g_sUSBHCD[ulIndex].USBINPipes[ulPipe & EP_PIPE_IDX_M].ucDMAChannel =
  1545. USBHCD_DMA_UNUSED;
  1546. //
  1547. // Free up the FIFO memory used by this endpoint.
  1548. //
  1549. if(g_sUSBHCD[ulIndex].USBINPipes[ulPipe & EP_PIPE_IDX_M].ucFIFOSize)
  1550. {
  1551. FIFOFree(&g_sUSBHCD[ulIndex].USBINPipes[ulPipe & EP_PIPE_IDX_M]);
  1552. }
  1553. }
  1554. }
  1555. //*****************************************************************************
  1556. //
  1557. // This internal function initializes the HCD code.
  1558. //
  1559. // \param ulIndex specifies which USB controller to use.
  1560. // \param pvPool is a pointer to the data to use as a memory pool for this
  1561. // controller.
  1562. // \param ulPoolSize is the size in bytes of the buffer passed in as pvPool.
  1563. //
  1564. // This function will perform all the necessary operations to allow the USB
  1565. // host controller to begin enumeration and communication with a device. This
  1566. // function should typically be called once at the start of an application
  1567. // before any other calls are made to the host controller.
  1568. //
  1569. // \return None.
  1570. //
  1571. //*****************************************************************************
  1572. static void
  1573. USBHCDInitInternal(unsigned int ulIndex, void *pvPool,
  1574. unsigned int ulPoolSize)
  1575. {
  1576. int iIdx;
  1577. ASSERT(ulIndex == 0);
  1578. //
  1579. // The first 64 Bytes are allocated to endpoint 0.
  1580. //
  1581. g_ulAlloc[0] = 1;
  1582. g_ulAlloc[1] = 0;
  1583. //
  1584. // Save the base address for this controller.
  1585. //
  1586. g_sUSBHCD[ulIndex].ulUSBBase = g_USBInstance[ulIndex].uiBaseAddr ;
  1587. //
  1588. // All Pipes are unused at start.
  1589. //
  1590. for(iIdx = 0; iIdx < MAX_NUM_PIPES; iIdx++)
  1591. {
  1592. g_sUSBHCD[ulIndex].USBINPipes[iIdx].ulDevAddr = 0;
  1593. g_sUSBHCD[ulIndex].USBINPipes[iIdx].ulType = USBHCD_PIPE_UNUSED;
  1594. g_sUSBHCD[ulIndex].USBINPipes[iIdx].ucDMAChannel = USBHCD_DMA_UNUSED;
  1595. g_sUSBHCD[ulIndex].USBOUTPipes[iIdx].ulDevAddr = 0;
  1596. g_sUSBHCD[ulIndex].USBOUTPipes[iIdx].ulType = USBHCD_PIPE_UNUSED;
  1597. g_sUSBHCD[ulIndex].USBOUTPipes[iIdx].ucDMAChannel = USBHCD_DMA_UNUSED;
  1598. }
  1599. //
  1600. // All DMA channels are unused at start.
  1601. //
  1602. for(iIdx = 0; iIdx < MAX_NUM_DMA_CHANNELS; iIdx++)
  1603. {
  1604. g_sUSBHCD[ulIndex].ucDMAChannels[iIdx] = USBHCD_DMA_UNUSED;
  1605. }
  1606. //
  1607. // Initialized the device structure.
  1608. //
  1609. g_sUSBHCD[ulIndex].eDeviceState[0] = HCD_IDLE;
  1610. g_sUSBHCD[ulIndex].USBDevice[0].pConfigDescriptor = 0;
  1611. //
  1612. // Initialize the device descriptor.
  1613. //
  1614. g_sUSBHCD[ulIndex].USBDevice[0].DeviceDescriptor.bLength = 0;
  1615. g_sUSBHCD[ulIndex].USBDevice[0].DeviceDescriptor.bMaxPacketSize0 = 0;
  1616. //
  1617. // Initialize the device address.
  1618. //
  1619. g_sUSBHCD[ulIndex].USBDevice[0].ulAddress = 0;
  1620. //
  1621. // Set the current interface to 0.
  1622. //
  1623. g_sUSBHCD[ulIndex].USBDevice[0].ulInterface = 0;
  1624. //
  1625. // Allocate the memory needed for reading descriptors.
  1626. //
  1627. g_sUSBHCD[ulIndex].pvPool = pvPool;
  1628. g_sUSBHCD[ulIndex].ulPoolSize = ulPoolSize;
  1629. //
  1630. // Initialize the device class.
  1631. //
  1632. g_sUSBHCD[ulIndex].ulClass = USB_CLASS_EVENTS;
  1633. //
  1634. // Initialize the USB tick module.
  1635. //
  1636. InternalUSBTickInit();
  1637. //
  1638. // Only do hardware update if the stack is in Host mode, do not touch the
  1639. // hardware for OTG mode operation.
  1640. //
  1641. if(g_eUSBMode == USB_MODE_HOST)
  1642. {
  1643. //
  1644. // Configure the End point 0.
  1645. //
  1646. USBHostEndpointConfig(g_USBInstance[ulIndex].uiBaseAddr , USB_EP_0, 64, 0, 0,
  1647. (USB_EP_MODE_CTRL | USB_EP_SPEED_HIGH |
  1648. USB_EP_HOST_OUT));
  1649. USBEnableOtgIntr(g_USBInstance[ulIndex].uiSubBaseAddr);
  1650. //
  1651. // Enable USB Interrupts.
  1652. //
  1653. USBIntEnableControl(g_USBInstance[ulIndex].uiBaseAddr , USB_INTCTRL_RESET |
  1654. USB_INTCTRL_DISCONNECT |
  1655. USB_INTCTRL_SOF |
  1656. USB_INTCTRL_SESSION |
  1657. USB_INTCTRL_BABBLE |
  1658. USB_INTCTRL_CONNECT |
  1659. USB_INTCTRL_RESUME |
  1660. USB_INTCTRL_SUSPEND |
  1661. USB_INTCTRL_VBUS_ERR |
  1662. USB_INTCTRL_POWER_FAULT);
  1663. USBIntEnableEndpoint(g_USBInstance[ulIndex].uiBaseAddr , USB_INTEP_ALL);
  1664. //
  1665. // Enable the USB interrupt.
  1666. //
  1667. //IntEnable(INT_USB0);
  1668. #ifdef _TMS320C6X
  1669. /* No DSP API to enable USB0 event */
  1670. #else
  1671. IntSystemEnable(g_USBInstance[ulIndex].uiInterruptNum);
  1672. #endif
  1673. //
  1674. // There is no automatic power in pure host mode.
  1675. //
  1676. USBHCDPowerConfigSet(ulIndex, g_sUSBHCD[ulIndex].ulPowerConfig & ~USB_HOST_PWREN_AUTO);
  1677. //
  1678. // This is required to get into host mode.
  1679. //
  1680. USBOTGSessionRequest(g_USBInstance[ulIndex].uiBaseAddr , true);
  1681. }
  1682. }
  1683. //*****************************************************************************
  1684. //
  1685. //! This function is used to set the power pin and power fault configuration.
  1686. //!
  1687. //! \param ulIndex specifies which USB controller to use.
  1688. //! \param ulPwrConfig is the power configuration to use for the application.
  1689. //!
  1690. //! This function must be called before HCDInit() is called so that the power
  1691. //! pin configuration can be set before power is enabled. The \e ulPwrConfig
  1692. //! flags specify the power fault level sensitivity, the power fault action,
  1693. //! and the power enable pin level and source.
  1694. //!
  1695. //! One of the following can be selected as the power fault level sensitivity:
  1696. //!
  1697. //! - \b USBHCD_FAULT_LOW - An external power fault is indicated by the pin
  1698. //! being driven low.
  1699. //! - \b USBHCD_FAULT_HIGH - An external power fault is indicated by the pin
  1700. //! being driven high.
  1701. //!
  1702. //! One of the following can be selected as the power fault action:
  1703. //!
  1704. //! - \b USBHCD_FAULT_VBUS_NONE - No automatic action when power fault
  1705. //! detected.
  1706. //! - \b USBHCD_FAULT_VBUS_TRI - Automatically Tri-state the USBnEPEN pin on a
  1707. //! power fault.
  1708. //! - \b USBHCD_FAULT_VBUS_DIS - Automatically drive the USBnEPEN pin to it's
  1709. //! inactive state on a power fault.
  1710. //!
  1711. //! One of the following can be selected as the power enable level and source:
  1712. //!
  1713. //! - \b USBHCD_VBUS_MANUAL - Power control is completely managed by the
  1714. //! application, the USB library will provide a
  1715. //! power callback to request power state changes.
  1716. //! - \b USBHCD_VBUS_AUTO_LOW - USBEPEN is driven low by the USB controller
  1717. //! automatically if USBOTGSessionRequest() has
  1718. //! enabled a session.
  1719. //! - \b USBHCD_VBUS_AUTO_HIGH - USBEPEN is driven high by the USB controller
  1720. //! automatically if USBOTGSessionRequest() has
  1721. //! enabled a session.
  1722. //!
  1723. //! If USBHCD_VBUS_MANUAL is used then the application must provide an
  1724. //! event driver to receive the USB_EVENT_POWER_ENABLE and
  1725. //! USB_EVENT_POWER_DISABLE events and enable and disable power to VBUS when
  1726. //! requested by the USB library. The application should respond to a power
  1727. //! control callback by enabling or disabling VBUS as soon as possible and
  1728. //! before returning from the callback function.
  1729. //!
  1730. //! \note The following values should no longer be used with the USB library:
  1731. //! USB_HOST_PWRFLT_LOW, USB_HOST_PWRFLT_HIGH, USB_HOST_PWRFLT_EP_NONE,
  1732. //! USB_HOST_PWRFLT_EP_TRI, USB_HOST_PWRFLT_EP_LOW, USB_HOST_PWRFLT_EP_HIGH,
  1733. //! USB_HOST_PWREN_LOW, USB_HOST_PWREN_HIGH, USB_HOST_PWREN_VBLOW, and
  1734. //! USB_HOST_PWREN_VBHIGH.
  1735. //!
  1736. //! \return None.
  1737. //
  1738. //*****************************************************************************
  1739. void
  1740. USBHCDPowerConfigInit(unsigned int ulIndex, unsigned int ulPwrConfig)
  1741. {
  1742. ASSERT(ulIndex == 0);
  1743. //
  1744. // Save the value as it will be used later.
  1745. //
  1746. g_sUSBHCD[ulIndex].ulPowerConfig = ulPwrConfig;
  1747. }
  1748. //*****************************************************************************
  1749. //
  1750. //! This function is used to get the power pin and power fault configuration.
  1751. //!
  1752. //! \param ulIndex specifies which USB controller to use.
  1753. //!
  1754. //! This function will return the current power control pin configuration as
  1755. //! set by the USBHCDPowerConfigInit() function or the defaults if not yet set.
  1756. //! See the USBHCDPowerConfigInit() documentation for the meaning of the bits
  1757. //! that are returned by this function.
  1758. //!
  1759. //! \return The configuration of the power control pins.
  1760. //!
  1761. //*****************************************************************************
  1762. unsigned int
  1763. USBHCDPowerConfigGet(unsigned int ulIndex)
  1764. {
  1765. ASSERT(ulIndex == 0);
  1766. //
  1767. // Save the value as it will be used later.
  1768. //
  1769. return(g_sUSBHCD[ulIndex].ulPowerConfig);
  1770. }
  1771. //*****************************************************************************
  1772. //
  1773. //! This function is used to set the power pin and power fault configuration.
  1774. //!
  1775. //! \param ulIndex specifies which USB controller to use.
  1776. //! \param ulConfig specifies which USB power configuration to use.
  1777. //!
  1778. //! This function will set the current power control pin configuration as
  1779. //! set by the USBHCDPowerConfigInit() function or the defaults if not yet set.
  1780. //! See the USBHCDPowerConfigInit() documentation for the meaning of the bits
  1781. //! that are set by this function.
  1782. //!
  1783. //! \return Returns zero to indicate the power setting is now active.
  1784. //!
  1785. //*****************************************************************************
  1786. unsigned int
  1787. USBHCDPowerConfigSet(unsigned int ulIndex, unsigned int ulConfig)
  1788. {
  1789. ASSERT(ulIndex == 0);
  1790. //
  1791. // Remember the current setting.
  1792. //
  1793. g_sUSBHCD[ulIndex].ulPowerConfig = ulConfig;
  1794. //
  1795. // Clear out the two flag bits.
  1796. //
  1797. ulConfig = g_sUSBHCD[ulIndex].ulPowerConfig & ~(USBHCD_VBUS_MANUAL | USBHCD_FAULT_VBUS_DIS);
  1798. //
  1799. // If there is an automatic disable power action specified then set the
  1800. // polarity of the signal to match EPEN.
  1801. //
  1802. if(g_sUSBHCD[ulIndex].ulPowerConfig & USBHCD_FAULT_VBUS_DIS)
  1803. {
  1804. //
  1805. // Insure that the assumption below is true.
  1806. //
  1807. ASSERT((USBHCD_VBUS_AUTO_HIGH & 1) == 1);
  1808. ASSERT((USBHCD_VBUS_AUTO_LOW & 1) == 0);
  1809. //
  1810. // This is taking advantage of the difference between
  1811. // USBHCD_VBUS_AUTO_LOW and USBHCD_VBUS_AUTO_HIGH being that bit
  1812. // one is set when EPEN is active high.
  1813. //
  1814. if(g_sUSBHCD[ulIndex].ulPowerConfig & 1)
  1815. {
  1816. g_sUSBHCD[ulIndex].ulPowerConfig |= USB_HOST_PWRFLT_EP_HIGH;
  1817. }
  1818. else
  1819. {
  1820. g_sUSBHCD[ulIndex].ulPowerConfig |= USB_HOST_PWRFLT_EP_LOW;
  1821. }
  1822. }
  1823. //
  1824. // Initialize the power configuration.
  1825. //
  1826. USBHostPwrConfig(g_USBInstance[ulIndex].uiBaseAddr , ulConfig);
  1827. //
  1828. // If not in manual mode then just turn on power.
  1829. //
  1830. if((g_sUSBHCD[ulIndex].ulPowerConfig & USBHCD_VBUS_MANUAL) == 0)
  1831. {
  1832. //
  1833. // Power the USB bus.
  1834. //
  1835. USBHostPwrEnable(g_USBInstance[ulIndex].uiBaseAddr );
  1836. }
  1837. //
  1838. // Return success.
  1839. //
  1840. return(0);
  1841. }
  1842. //*****************************************************************************
  1843. //
  1844. //! This function returns if the current power settings will automatically
  1845. //! handle enabling and disabling VBUS power.
  1846. //!
  1847. //! \param ulIndex specifies which USB controller to query.
  1848. //!
  1849. //! This function returns if the current power control pin configuration will
  1850. //! automatically apply power or whether it will be left to the application
  1851. //! to turn on power when it is notified.
  1852. //!
  1853. //! \return A non-zero value indicates that power is automatically applied and
  1854. //! a value of zero indicates that the application must manually apply power.
  1855. //!
  1856. //*****************************************************************************
  1857. unsigned int
  1858. USBHCDPowerAutomatic(unsigned int ulIndex)
  1859. {
  1860. //
  1861. // Check if the controller is automatically applying power or not.
  1862. //
  1863. if(g_sUSBHCD[ulIndex].ulPowerConfig & USBHCD_VBUS_MANUAL)
  1864. {
  1865. return(0);
  1866. }
  1867. return(1);
  1868. }
  1869. //*****************************************************************************
  1870. //
  1871. //! This function is used to initialize the HCD code.
  1872. //!
  1873. //! \param ulIndex specifies which USB controller to use.
  1874. //! \param pvPool is a pointer to the data to use as a memory pool for this
  1875. //! controller.
  1876. //! \param ulPoolSize is the size in bytes of the buffer passed in as pvPool.
  1877. //!
  1878. //! This function will perform all the necessary operations to allow the USB
  1879. //! host controller to begin enumeration and communication with devices. This
  1880. //! function should typically be called once at the start of an application
  1881. //! once all of the device and class drivers are ready for normal operation.
  1882. //! This call will start up the USB host controller and any connected device
  1883. //! will immediately start the enumeration sequence.
  1884. //!
  1885. //! \return None.
  1886. //
  1887. //*****************************************************************************
  1888. void
  1889. USBHCDInit(unsigned int ulIndex, void *pvPool, unsigned int ulPoolSize)
  1890. {
  1891. int iDriver;
  1892. //
  1893. // Check the arguments.
  1894. //
  1895. ASSERT(ulIndex == 0);
  1896. //
  1897. // Make sure there is at least enough to read the configuration descriptor.
  1898. //
  1899. ASSERT(ulPoolSize >= sizeof(tConfigDescriptor));
  1900. //
  1901. // Should not call this if the stack is in device mode.
  1902. //
  1903. ASSERT(g_eUSBMode != USB_MODE_DEVICE)
  1904. if(ulIndex == 0)
  1905. {
  1906. g_USBInstance[ulIndex].uiUSBInstance = ulIndex;
  1907. g_USBInstance[ulIndex].uiBaseAddr = USB0_BASE;
  1908. g_USBInstance[ulIndex].uiSubBaseAddr = USB_0_OTGBASE;
  1909. g_USBInstance[ulIndex].uiInterruptNum = SYS_INT_USB0;
  1910. g_USBInstance[ulIndex].uiSubInterruptNum = SYS_INT_USBSSINT;
  1911. g_USBInstance[ulIndex].uiPHYConfigRegAddr = CFGCHIP2_USBPHYCTRL;
  1912. }
  1913. #if (USB_NUM_INSTANCE == 2)
  1914. else if(ulIndex == 1)
  1915. {
  1916. g_USBInstance[ulIndex].uiUSBInstance = ulIndex;
  1917. g_USBInstance[ulIndex].uiBaseAddr = USB1_BASE;
  1918. g_USBInstance[ulIndex].uiSubBaseAddr = USB_1_OTGBASE;
  1919. g_USBInstance[ulIndex].uiInterruptNum = SYS_INT_USB1;
  1920. g_USBInstance[ulIndex].uiSubInterruptNum = SYS_INT_USBSSINT;
  1921. g_USBInstance[ulIndex].uiPHYConfigRegAddr = CFGCHIP2_USB1PHYCTRL;
  1922. }
  1923. #endif
  1924. g_sUSBHCD[ulIndex].USBHTimeOut.Value.slEP0 = USB_EP0_TIMEOUT_MILLISECS;
  1925. g_sUSBHCD[ulIndex].USBHTimeOut.Value.slNonEP0= USB_NONEP0_TIMEOUT_MILLISECS;
  1926. g_sUSBHCD[ulIndex].USBHTimeOut.Status.slEP0 = 0;
  1927. g_sUSBHCD[ulIndex].USBHTimeOut.Status.slNonEP0= 0;
  1928. //
  1929. // If no mode is set then make the mode become host mode.
  1930. //
  1931. if(g_eUSBMode == USB_MODE_NONE)
  1932. {
  1933. g_eUSBMode = USB_MODE_HOST;
  1934. }
  1935. //
  1936. // Only do hardware update if the stack is in Host mode, do not touch the
  1937. // hardware for OTG mode operation.
  1938. //
  1939. if(g_eUSBMode == USB_MODE_HOST)
  1940. {
  1941. //
  1942. // Enable Clocking to the USB controller.
  1943. //
  1944. USBModuleClkEnable(ulIndex, g_USBInstance[ulIndex].uiBaseAddr );
  1945. USBReset(g_USBInstance[ulIndex].uiSubBaseAddr);
  1946. //
  1947. // Turn on USB Phy clock.
  1948. //
  1949. UsbPhyOn(ulIndex);
  1950. }
  1951. //
  1952. // Call our internal function to perform the initialization.
  1953. //
  1954. USBHCDInitInternal(ulIndex, pvPool, ulPoolSize);
  1955. //
  1956. // No event driver is present by default.
  1957. //
  1958. g_sUSBHCD[ulIndex].iEventDriver = -1;
  1959. //
  1960. // Retry recovery and comunication with device on error.
  1961. //
  1962. g_sUSBHCD[ulIndex].ulConnectRetry = USBHCD_DEV_RECOVER_RETRY;
  1963. //
  1964. // Search through the Host Class driver list for the devices class.
  1965. //
  1966. for(iDriver = 0; iDriver < g_sUSBHCD[ulIndex].ulNumClassDrivers; iDriver++)
  1967. {
  1968. if(g_sUSBHCD[ulIndex].pClassDrivers[iDriver]->ulInterfaceClass ==
  1969. USB_CLASS_EVENTS)
  1970. {
  1971. //
  1972. // Event driver was found so remember it.
  1973. //
  1974. g_sUSBHCD[ulIndex].iEventDriver = iDriver;
  1975. }
  1976. }
  1977. }
  1978. //*****************************************************************************
  1979. //
  1980. //! This function is used to initialize the HCD class driver list.
  1981. //!
  1982. //! \param ulIndex specifies which USB controller to use.
  1983. //! \param ppHClassDrvs is an array of host class drivers that are
  1984. //! supported on this controller.
  1985. //! \param ulNumDrivers is the number of entries in the \e pHostClassDrivers
  1986. //! array.
  1987. //!
  1988. //! This function will set the host classes supported by the host controller
  1989. //! specified by the \e ulIndex parameter. This function should be called
  1990. //! before enabling the host controller driver with the USBHCDInit() function.
  1991. //!
  1992. //! \return None.
  1993. //
  1994. //*****************************************************************************
  1995. void
  1996. USBHCDRegisterDrivers(unsigned int ulIndex,
  1997. const tUSBHostClassDriver * const *ppHClassDrvs,
  1998. unsigned int ulNumDrivers)
  1999. {
  2000. ASSERT(ulIndex == 0);
  2001. g_sUSBHCD[ulIndex].ulIndex = ulIndex;
  2002. //
  2003. // Save the class drivers.
  2004. //
  2005. g_sUSBHCD[ulIndex].pClassDrivers = ppHClassDrvs;
  2006. //
  2007. // Save the number of class drivers.
  2008. //
  2009. g_sUSBHCD[ulIndex].ulNumClassDrivers = ulNumDrivers;
  2010. }
  2011. //*****************************************************************************
  2012. //
  2013. //! This function is used to terminate the HCD code.
  2014. //!
  2015. //! \param ulIndex specifies which USB controller to release.
  2016. //!
  2017. //! This function will clean up the USB host controller and disable it in
  2018. //! preparation for shutdown or a switch to USB device mode. Once this call is
  2019. //! made, \e USBHCDInit() may be called to reinitialize the controller and
  2020. //! prepare for host mode operation.
  2021. //!
  2022. //! \return None.
  2023. //
  2024. //*****************************************************************************
  2025. void
  2026. USBHCDTerm(unsigned int ulIndex)
  2027. {
  2028. ASSERT(ulIndex == 0);
  2029. //
  2030. // End the session.
  2031. //
  2032. USBOTGSessionRequest(g_USBInstance[ulIndex].uiBaseAddr , false);
  2033. //
  2034. // Remove power from the USB bus.
  2035. //
  2036. USBHostPwrDisable(g_USBInstance[ulIndex].uiBaseAddr );
  2037. //
  2038. // Disable USB interrupts.
  2039. //
  2040. #ifdef _TMS320C6X
  2041. /* No DSP API to disable USB0 event */
  2042. #else
  2043. IntSystemDisable(g_USBInstance[ulIndex].uiInterruptNum);
  2044. #endif
  2045. USBIntDisableControl(g_USBInstance[ulIndex].uiBaseAddr , USB_INTCTRL_ALL);
  2046. USBIntDisableEndpoint(g_USBInstance[ulIndex].uiBaseAddr , USB_INTEP_ALL);
  2047. //
  2048. // Set the host controller state back to it's initial values.
  2049. //
  2050. g_sUSBHCD[ulIndex].USBINPipes[0].ulType = USBHCD_PIPE_UNUSED;
  2051. g_sUSBHCD[ulIndex].USBINPipes[1].ulType = USBHCD_PIPE_UNUSED;
  2052. g_sUSBHCD[ulIndex].USBINPipes[2].ulType = USBHCD_PIPE_UNUSED;
  2053. g_sUSBHCD[ulIndex].USBOUTPipes[0].ulType = USBHCD_PIPE_UNUSED;
  2054. g_sUSBHCD[ulIndex].USBOUTPipes[1].ulType = USBHCD_PIPE_UNUSED;
  2055. g_sUSBHCD[ulIndex].USBOUTPipes[2].ulType = USBHCD_PIPE_UNUSED;
  2056. g_sUSBHCD[ulIndex].eDeviceState[0] = HCD_IDLE;
  2057. g_sUSBHCD[ulIndex].USBDevice[0].pConfigDescriptor = 0;
  2058. g_sUSBHCD[ulIndex].USBDevice[0].DeviceDescriptor.bLength = 0;
  2059. g_sUSBHCD[ulIndex].USBDevice[0].DeviceDescriptor.bMaxPacketSize0 = 0;
  2060. g_sUSBHCD[ulIndex].USBDevice[0].ulAddress = 0;
  2061. g_sUSBHCD[ulIndex].USBDevice[0].ulInterface = 0;
  2062. g_sUSBHCD[ulIndex].pvPool = 0;
  2063. g_sUSBHCD[ulIndex].ulPoolSize = 0;
  2064. }
  2065. //*****************************************************************************
  2066. //
  2067. //! This function generates reset signaling on the USB bus.
  2068. //!
  2069. //! \param ulIndex specifies which USB controller to use.
  2070. //!
  2071. //! This function handles sending out reset signaling on the USB bus. After
  2072. //! returning from this function, any attached device on the USB bus should
  2073. //! have returned to it's reset state.
  2074. //!
  2075. //! \return None.
  2076. //
  2077. //*****************************************************************************
  2078. void
  2079. USBHCDReset(unsigned int ulIndex)
  2080. {
  2081. ASSERT(ulIndex == 0);
  2082. //
  2083. // Start the reset signaling.
  2084. //
  2085. USBHostReset(g_USBInstance[ulIndex].uiBaseAddr , 1);
  2086. //
  2087. // Wait 20ms
  2088. //
  2089. delay(20);
  2090. //
  2091. // End reset signaling on the bus.
  2092. //
  2093. USBHostReset(g_USBInstance[ulIndex].uiBaseAddr , 0);
  2094. //
  2095. // Need to wait at least 10ms to let the device recover from
  2096. // the reset. This is the delay specified in the USB 2.0 spec.
  2097. // We will hold the reset for 20ms.
  2098. //
  2099. delay(20);
  2100. }
  2101. //*****************************************************************************
  2102. //
  2103. //! This function will generate suspend signaling on the USB bus.
  2104. //!
  2105. //! \param ulIndex specifies which USB controller to use.
  2106. //!
  2107. //! This function is used to generate suspend signaling on the USB bus. In
  2108. //! order to leave the suspended state, the application should call
  2109. //! USBHCDResume().
  2110. //!
  2111. //! \return None.
  2112. //
  2113. //*****************************************************************************
  2114. void
  2115. USBHCDSuspend(unsigned int ulIndex)
  2116. {
  2117. ASSERT(ulIndex == 0);
  2118. //
  2119. // Start the suspend signaling.
  2120. //
  2121. USBHostSuspend(g_USBInstance[ulIndex].uiBaseAddr );
  2122. }
  2123. //*****************************************************************************
  2124. //
  2125. //! This function will generate resume signaling on the USB bus.
  2126. //!
  2127. //! \param ulIndex specifies which USB controller to use.
  2128. //!
  2129. //! This function is used to generate resume signaling on the USB bus in order
  2130. //! to cause USB devices to leave their suspended state. This call should
  2131. //! not be made unless a preceding call to USBHCDSuspend() has been made.
  2132. //!
  2133. //! \return None.
  2134. //
  2135. //*****************************************************************************
  2136. void
  2137. USBHCDResume(unsigned int ulIndex)
  2138. {
  2139. ASSERT(ulIndex == 0);
  2140. //
  2141. // Start the resume signaling.
  2142. //
  2143. USBHostResume(g_USBInstance[ulIndex].uiBaseAddr , 1);
  2144. //
  2145. // Wait 100ms
  2146. //
  2147. delay(100);
  2148. //
  2149. // End reset signaling on the bus.
  2150. //
  2151. USBHostResume(g_USBInstance[ulIndex].uiBaseAddr , 0);
  2152. }
  2153. //*****************************************************************************
  2154. //
  2155. //! This function issues a request for the current configuration descriptor
  2156. //! from a device.
  2157. //!
  2158. //! \param ulIndex specifies which USB controller to use.
  2159. //! \param pDevice is a pointer to the device structure that holds the buffer
  2160. //! to store the configuration descriptor.
  2161. //!
  2162. //! This function will request the configuration descriptor from the device.
  2163. //! The \e pDevice->ConfigDescriptor member variable is used to hold the data
  2164. //! for this request. This buffer will be allocated from the pool provided by
  2165. //! the HCDInit() function. \e pDevice->DeviceDescriptor.bMaxPacketSize0
  2166. //! should be valid prior to this call in order to correctly receive the
  2167. //! configuration descriptor. If this variable is not valid then this call
  2168. //! will not return accurate data.
  2169. //!
  2170. //! \return The number of bytes returned due to the request. This value can be
  2171. //! zero if the device did not respond.
  2172. //
  2173. //*****************************************************************************
  2174. static unsigned int
  2175. USBHCDGetConfigDescriptor(unsigned int ulIndex, tUSBHostDevice *pDevice)
  2176. {
  2177. tUSBRequest SetupPacket;
  2178. unsigned int ulBytes;
  2179. ASSERT(ulIndex == 0);
  2180. ulBytes = 0;
  2181. //
  2182. // This is a Standard Device IN request.
  2183. //
  2184. SetupPacket.bmRequestType =
  2185. USB_RTYPE_DIR_IN | USB_RTYPE_STANDARD | USB_RTYPE_DEVICE;
  2186. //
  2187. // Request a Device Descriptor.
  2188. //
  2189. SetupPacket.bRequest = USBREQ_GET_DESCRIPTOR;
  2190. SetupPacket.wValue = USB_DTYPE_CONFIGURATION << 8;
  2191. //
  2192. // Index is always 0 for device configurations requests.
  2193. //
  2194. SetupPacket.wIndex = 0;
  2195. //
  2196. // Only ask for the configuration header first to see how big the
  2197. // whole thing is.
  2198. //
  2199. if(g_sUSBHCD[ulIndex].USBDevice[0].pConfigDescriptor == 0)
  2200. {
  2201. //
  2202. // Only request the space available.
  2203. //
  2204. SetupPacket.wLength = sizeof(tConfigDescriptor);
  2205. //
  2206. // Set the memory to use for the config descriptor and save the size.
  2207. //
  2208. g_sUSBHCD[ulIndex].USBDevice[0].pConfigDescriptor = g_sUSBHCD[ulIndex].pvPool;
  2209. g_sUSBHCD[ulIndex].USBDevice[0].ulConfigDescriptorSize = g_sUSBHCD[ulIndex].ulPoolSize;
  2210. //
  2211. // Put the setup packet in the buffer.
  2212. //
  2213. ulBytes =
  2214. USBHCDControlTransfer(ulIndex, &SetupPacket, pDevice->ulAddress,
  2215. (unsigned char *)pDevice->pConfigDescriptor,
  2216. sizeof(tConfigDescriptor),
  2217. pDevice->DeviceDescriptor.bMaxPacketSize0);
  2218. }
  2219. //
  2220. // If the Configuration header was successfully returned then get the
  2221. // full configuration descriptor.
  2222. //
  2223. if(ulBytes == sizeof(tConfigDescriptor))
  2224. {
  2225. //
  2226. // Save the total size and request the full configuration descriptor.
  2227. //
  2228. SetupPacket.wLength =
  2229. g_sUSBHCD[ulIndex].USBDevice[0].pConfigDescriptor->wTotalLength;
  2230. //
  2231. // Don't allow the buffer to be larger than was allocated.
  2232. //
  2233. if(SetupPacket.wLength > g_sUSBHCD[ulIndex].ulPoolSize)
  2234. {
  2235. SetupPacket.wLength = g_sUSBHCD[ulIndex].ulPoolSize;
  2236. }
  2237. //
  2238. // Put the setup packet in the buffer.
  2239. //
  2240. ulBytes =
  2241. USBHCDControlTransfer(ulIndex, &SetupPacket, pDevice->ulAddress,
  2242. (unsigned char *)pDevice->pConfigDescriptor,
  2243. SetupPacket.wLength,
  2244. pDevice->DeviceDescriptor.bMaxPacketSize0);
  2245. }
  2246. return(ulBytes);
  2247. }
  2248. //*****************************************************************************
  2249. //
  2250. //! This function issues a request for a device descriptor from a device.
  2251. //!
  2252. //! \param ulIndex specifies which USB controller to use.
  2253. //! \param pDevice is a pointer to the device structure that holds the buffer
  2254. //! to store the device descriptor into.
  2255. //!
  2256. //! This function will request the device descriptor from the device. The
  2257. //! \e pDevice->DeviceDescriptor descriptor is used to hold the data for this
  2258. //! request. \e pDevice->DeviceDescriptor.bMaxPacketSize0 should be
  2259. //! initialized to zero or to the valid maximum packet size if it is known. If
  2260. //! this variable is not set to zero, then this call will determine the maximum
  2261. //! packet size for endpoint 0 and save it in the structure member
  2262. //! bMaxPacketSize0.
  2263. //!
  2264. //! \return The number of bytes returned due to the request. This value can be
  2265. //! zero if the device did not respond.
  2266. //
  2267. //*****************************************************************************
  2268. static unsigned int
  2269. USBHCDGetDeviceDescriptor(unsigned int ulIndex, tUSBHostDevice *pDevice)
  2270. {
  2271. tUSBRequest SetupPacket;
  2272. unsigned int ulBytes;
  2273. unsigned int retStatus = 1;
  2274. ASSERT(ulIndex == 0);
  2275. //
  2276. // This is a Standard Device IN request.
  2277. //
  2278. SetupPacket.bmRequestType =
  2279. USB_RTYPE_DIR_IN | USB_RTYPE_STANDARD | USB_RTYPE_DEVICE;
  2280. //
  2281. // Request a Device Descriptor.
  2282. //
  2283. SetupPacket.bRequest = USBREQ_GET_DESCRIPTOR;
  2284. SetupPacket.wValue = USB_DTYPE_DEVICE << 8;
  2285. //
  2286. // Index is always 0 for device requests.
  2287. //
  2288. SetupPacket.wIndex = 0;
  2289. //
  2290. // All devices must have at least an 8 byte max packet size so just ask
  2291. // for 8 bytes to start with.
  2292. //
  2293. SetupPacket.wLength = 8;
  2294. ulBytes = 0;
  2295. //
  2296. // Discover the max packet size for endpoint 0.
  2297. //
  2298. if(pDevice->DeviceDescriptor.bMaxPacketSize0 == 0)
  2299. {
  2300. //
  2301. // Put the setup packet in the buffer.
  2302. //
  2303. ulBytes =
  2304. USBHCDControlTransfer(ulIndex, &SetupPacket, pDevice->ulAddress,
  2305. (unsigned char *)&(pDevice->DeviceDescriptor),
  2306. sizeof(tDeviceDescriptor),
  2307. MAX_PACKET_SIZE_EP0);
  2308. }
  2309. retStatus = ulBytes;
  2310. //
  2311. // Now get the full descriptor now that the actual maximum packet size
  2312. // is known.
  2313. //
  2314. if(retStatus && (ulBytes < sizeof(tDeviceDescriptor)))
  2315. {
  2316. SetupPacket.wLength = (unsigned short)sizeof(tDeviceDescriptor);
  2317. ulBytes =
  2318. USBHCDControlTransfer(ulIndex, &SetupPacket, pDevice->ulAddress,
  2319. (unsigned char *)&(pDevice->DeviceDescriptor),
  2320. sizeof(tDeviceDescriptor),
  2321. pDevice->DeviceDescriptor.bMaxPacketSize0);
  2322. }
  2323. return(ulBytes);
  2324. }
  2325. //*****************************************************************************
  2326. //
  2327. //! This function is used to send the set address command to a device.
  2328. //!
  2329. //! \param ulDevAddress is the new device address to use for a device.
  2330. //!
  2331. //! The USBHCDSetAddress() function is used to set the USB device address, once
  2332. //! a device has been discovered on the bus. This is typically issued
  2333. //! following a USB reset which is triggered by a call the USBHCDReset(). The
  2334. //! address passed into this function via the \e ulDevAddress parameter should
  2335. //! be used for all further communications with the device once this function
  2336. //! returns.
  2337. //!
  2338. //! \return None.
  2339. //
  2340. //*****************************************************************************
  2341. void
  2342. USBHCDSetAddress(unsigned int ulIndex, unsigned int ulDevAddress)
  2343. {
  2344. tUSBRequest SetupPacket;
  2345. //
  2346. // This is a Standard Device OUT request.
  2347. //
  2348. SetupPacket.bmRequestType =
  2349. USB_RTYPE_DIR_OUT | USB_RTYPE_STANDARD | USB_RTYPE_DEVICE;
  2350. //
  2351. // Request a Device Descriptor.
  2352. //
  2353. SetupPacket.bRequest = USBREQ_SET_ADDRESS;
  2354. SetupPacket.wValue = ulDevAddress;
  2355. //
  2356. // Index is always 0 for device requests.
  2357. //
  2358. SetupPacket.wIndex = 0;
  2359. //
  2360. // Only request the space available.
  2361. //
  2362. SetupPacket.wLength = 0;
  2363. //
  2364. // Put the setup packet in the buffer.
  2365. //
  2366. USBHCDControlTransfer(ulIndex, &SetupPacket, 0, 0, 0, MAX_PACKET_SIZE_EP0);
  2367. //
  2368. // Must delay 2ms after setting the address.
  2369. //
  2370. delay(2);
  2371. }
  2372. //*****************************************************************************
  2373. //
  2374. //! This function is used to send a Clear Feature request to a device.
  2375. //!
  2376. //! \param ulDevAddress is the USB bus address of the device that will receive
  2377. //! this request.
  2378. //! \param ulPipe is the pipe that will be used to send the request.
  2379. //! \param ulFeature is one of the USB_FEATURE_* definitions.
  2380. //!
  2381. //! This function will issue a Clear Feature request to the device indicated
  2382. //! by the \e ulDevAddress parameter. The \e ulPipe parameter is the USB pipe
  2383. //! that should be used to send this request. The \e ulFeature parameter
  2384. //! should be one of the following values:
  2385. //!
  2386. //! * \b USB_FEATURE_EP_HALT is used to end a HALT condition on a devices
  2387. //! endpoint.
  2388. //! * \b USB_FEATURE_REMOTE_WAKE is used to disable a device's remote wake
  2389. //! feature.
  2390. //! * \b USB_FEATURE_TEST_MODE is used take the USB device out of test mode.
  2391. //!
  2392. //! \return None.
  2393. //
  2394. //*****************************************************************************
  2395. void
  2396. USBHCDClearFeature(unsigned int devIndex, unsigned int ulDevAddress,
  2397. unsigned int ulPipe, unsigned int ulFeature)
  2398. {
  2399. tUSBRequest SetupPacket;
  2400. unsigned int ulIndex;
  2401. //
  2402. // Get the index number from the allocated pipe.
  2403. //
  2404. ulIndex = (ulPipe & EP_PIPE_IDX_M);
  2405. //
  2406. // This is a Standard Device OUT request.
  2407. //
  2408. SetupPacket.bmRequestType =
  2409. USB_RTYPE_DIR_OUT | USB_RTYPE_STANDARD | USB_RTYPE_ENDPOINT;
  2410. //
  2411. // Request a Device Descriptor.
  2412. //
  2413. SetupPacket.bRequest = USBREQ_CLEAR_FEATURE;
  2414. SetupPacket.wValue = ulFeature;
  2415. //
  2416. // Set the endpoint to access.
  2417. //
  2418. if(ulPipe & EP_PIPE_TYPE_IN)
  2419. {
  2420. SetupPacket.wIndex = g_sUSBHCD[devIndex].USBINPipes[ulIndex].ucEPNumber | 0x80;
  2421. }
  2422. else
  2423. {
  2424. SetupPacket.wIndex = g_sUSBHCD[devIndex].USBOUTPipes[ulIndex].ucEPNumber;
  2425. }
  2426. //
  2427. // This is always 0.
  2428. //
  2429. SetupPacket.wLength = 0;
  2430. //
  2431. // Put the setup packet in the buffer.
  2432. //
  2433. USBHCDControlTransfer(devIndex, &SetupPacket, ulDevAddress, 0, 0,
  2434. MAX_PACKET_SIZE_EP0);
  2435. //
  2436. // Set the endpoint to access.
  2437. //
  2438. if(ulPipe & EP_PIPE_TYPE_IN)
  2439. {
  2440. USBEndpointDataToggleClear(g_USBInstance[devIndex].uiBaseAddr , INDEX_TO_USB_EP(ulIndex + 1),
  2441. USB_EP_HOST_IN);
  2442. }
  2443. else
  2444. {
  2445. USBEndpointDataToggleClear(g_USBInstance[devIndex].uiBaseAddr , INDEX_TO_USB_EP(ulIndex + 1),
  2446. USB_EP_HOST_OUT);
  2447. }
  2448. //
  2449. // Must delay 2ms after clearing the feature.
  2450. //
  2451. delay(2);
  2452. }
  2453. //*****************************************************************************
  2454. //
  2455. //! This function is used to set the current configuration for a device.
  2456. //!
  2457. //! \param ulIndex specifies which USB controller to use.
  2458. //! \param ulDevice is the USB device for this function.
  2459. //! \param ulConfiguration is one of the devices valid configurations.
  2460. //!
  2461. //! This function is used to set the current device configuration for a USB
  2462. //! device. The \e ulConfiguration value must be one of the configuration
  2463. //! indexes that was returned in the configuration descriptor from the device,
  2464. //! or a value of 0. If 0 is passed in, the device will return to it's
  2465. //! addressed state and no longer be in a configured state. If the value is
  2466. //! non-zero then the device will change to the requested configuration.
  2467. //!
  2468. //! \return None.
  2469. //
  2470. //*****************************************************************************
  2471. void
  2472. USBHCDSetConfig(unsigned int ulIndex, unsigned int ulDevice,
  2473. unsigned int ulConfiguration)
  2474. {
  2475. tUSBRequest SetupPacket;
  2476. tUSBHostDevice *pDevice;
  2477. ASSERT(ulIndex == 0);
  2478. pDevice = (tUSBHostDevice *)ulDevice;
  2479. //
  2480. // This is a Standard Device OUT request.
  2481. //
  2482. SetupPacket.bmRequestType =
  2483. USB_RTYPE_DIR_OUT | USB_RTYPE_STANDARD | USB_RTYPE_DEVICE;
  2484. //
  2485. // Request a Device Descriptor.
  2486. //
  2487. SetupPacket.bRequest = USBREQ_SET_CONFIG;
  2488. SetupPacket.wValue = ulConfiguration;
  2489. //
  2490. // Index is always 0 for device requests.
  2491. //
  2492. SetupPacket.wIndex = 0;
  2493. //
  2494. // Only request the space available.
  2495. //
  2496. SetupPacket.wLength = 0;
  2497. //
  2498. // Put the setup packet in the buffer.
  2499. //
  2500. USBHCDControlTransfer(ulIndex, &SetupPacket, pDevice->ulAddress, 0, 0,
  2501. MAX_PACKET_SIZE_EP0);
  2502. }
  2503. //*****************************************************************************
  2504. //
  2505. //! This function is used to set the current interface and alternate setting
  2506. //! for an interface on a device.
  2507. //!
  2508. //! \param ulIndex specifies which USB controller to use.
  2509. //! \param ulDevice is the USB device for this function.
  2510. //! \param ulInterface is one of the valid interface numbers for a device.
  2511. //! \param ulAltSetting is one of the valid alternate interfaces for the
  2512. //! ulInterface number.
  2513. //!
  2514. //! This function is used to change the alternate setting for one of the valid
  2515. //! interfaces on a USB device. The \e ulDevice specifies the device instance
  2516. //! that was returned when the device was connected. This call will set the
  2517. //! USB device's interface based on the \e ulInterface and \e ulAltSetting.
  2518. //!
  2519. //! \b Example: Set the USB device interface 2 to alternate setting 1.
  2520. //!
  2521. //! \verbatim
  2522. //! USBHCDSetInterface(0, ulDevice, 2, 1);
  2523. //! \endverbatim
  2524. //!
  2525. //! \return None.
  2526. //
  2527. //*****************************************************************************
  2528. void
  2529. USBHCDSetInterface(unsigned int ulIndex, unsigned int ulDevice,
  2530. unsigned int ulInterface, unsigned ulAltSetting)
  2531. {
  2532. tUSBRequest SetupPacket;
  2533. tUSBHostDevice *pDevice;
  2534. ASSERT(ulIndex == 0);
  2535. pDevice = (tUSBHostDevice *)ulDevice;
  2536. //
  2537. // This is a Standard Device OUT request.
  2538. //
  2539. SetupPacket.bmRequestType =
  2540. USB_RTYPE_DIR_OUT | USB_RTYPE_STANDARD | USB_RTYPE_INTERFACE;
  2541. //
  2542. // Request a Device Descriptor.
  2543. //
  2544. SetupPacket.bRequest = USBREQ_SET_INTERFACE;
  2545. //
  2546. // Index is the interface to access.
  2547. //
  2548. SetupPacket.wIndex = ulInterface;
  2549. //
  2550. // wValue is the alternate setting.
  2551. //
  2552. SetupPacket.wValue = ulAltSetting;
  2553. //
  2554. // Only request the space available.
  2555. //
  2556. SetupPacket.wLength = 0;
  2557. //
  2558. // Put the setup packet in the buffer.
  2559. //
  2560. USBHCDControlTransfer(ulIndex, &SetupPacket, pDevice->ulAddress, 0, 0,
  2561. MAX_PACKET_SIZE_EP0);
  2562. }
  2563. //*****************************************************************************
  2564. //
  2565. // The internal function to see if a new schedule event should occur.
  2566. //
  2567. // This function is called by the main interrupt handler due to start of frame
  2568. // interrupts to determine if a new scheduler event should be sent to the USB
  2569. // pipe.
  2570. //
  2571. // \return None.
  2572. //
  2573. //*****************************************************************************
  2574. void
  2575. USBHostCheckPipes(unsigned int ulIndex)
  2576. {
  2577. int iIdx;
  2578. for(iIdx = 0; iIdx < 3; iIdx++)
  2579. {
  2580. //
  2581. // Skip unused pipes.
  2582. //
  2583. if(g_sUSBHCD[ulIndex].USBINPipes[iIdx].ulType == USBHCD_PIPE_UNUSED)
  2584. {
  2585. continue;
  2586. }
  2587. //
  2588. // If the tick has expired and it has an interval then update it.
  2589. //
  2590. if((g_sUSBHCD[ulIndex].USBINPipes[iIdx].ulInterval != 0) &&
  2591. (g_sUSBHCD[ulIndex].USBINPipes[iIdx].ulNextEventTick == g_sUSBHCD[ulIndex].ulCurrentTick))
  2592. {
  2593. //
  2594. // Schedule the next event.
  2595. //
  2596. g_sUSBHCD[ulIndex].USBINPipes[iIdx].ulNextEventTick +=
  2597. g_sUSBHCD[ulIndex].USBINPipes[iIdx].ulInterval;
  2598. //
  2599. // If the pipe is IDLE and there is a callback, let the higher
  2600. // level drivers know that a new transfer can be scheduled.
  2601. //
  2602. if((g_sUSBHCD[ulIndex].USBINPipes[iIdx].eState == PIPE_IDLE) &&
  2603. (g_sUSBHCD[ulIndex].USBINPipes[iIdx].pfnCallback))
  2604. {
  2605. g_sUSBHCD[ulIndex].USBINPipes[iIdx].pfnCallback(ulIndex, IN_PIPE_HANDLE(ulIndex, iIdx),
  2606. USB_EVENT_SCHEDULER);
  2607. }
  2608. }
  2609. }
  2610. }
  2611. //*****************************************************************************
  2612. //
  2613. // The internal USB host mode interrupt handler.
  2614. //
  2615. // \param ulIndex is the USB controller associated with this interrupt.
  2616. // \param ulStatus is the current interrupt status as read via a call to
  2617. // \e USBIntStatusControl().
  2618. //
  2619. // This the main USB interrupt handler called when operating in host mode.
  2620. // This handler will branch the interrupt off to the appropriate handlers
  2621. // depending on the current status of the USB controller.
  2622. //
  2623. // The two-tiered structure for the interrupt handler ensures that it is
  2624. // possible to use the same handler code in both host and OTG modes and
  2625. // means that device code can be excluded from applications that only require
  2626. // support for USB host mode operation.
  2627. //
  2628. // \return None.
  2629. //
  2630. //*****************************************************************************
  2631. void
  2632. USBHostIntHandlerInternal(unsigned int ulIndex, unsigned int ulStatus, unsigned int *endPStatus)
  2633. {
  2634. unsigned int ulEPStatus;
  2635. static unsigned int ulSOFDivide = 0;
  2636. unsigned int ulEvent;
  2637. unsigned int ulIdx;
  2638. unsigned int epStatus;
  2639. unsigned int epnStatus = 0;
  2640. #ifdef DMA_MODE
  2641. unsigned int pendReg = 0;
  2642. #endif
  2643. //
  2644. // Get the controller interrupt status from the wrapper registers
  2645. // Only the lower 16bits contain EP intr data
  2646. //
  2647. if(endPStatus == NULL)
  2648. {
  2649. epStatus= 0xFFFF & ulStatus;
  2650. ulStatus >>=16;
  2651. }
  2652. else
  2653. {
  2654. epStatus = *endPStatus;
  2655. }
  2656. ulStatus |= USBIntStatusControl(g_USBInstance[ulIndex].uiBaseAddr );
  2657. if(ulStatus & USB_INTCTRL_SOF)
  2658. {
  2659. g_sUSBHCD[ulIndex].ulCurrentTick++;
  2660. USBHostCheckPipes(ulIndex);
  2661. }
  2662. //
  2663. // In the event of a USB VBUS error, end the session and remove power to
  2664. // the device.
  2665. //
  2666. if(ulStatus & USB_INTCTRL_VBUS_ERR)
  2667. {
  2668. //
  2669. // Set the VBUS error event. We deliberately clear all other events
  2670. // since this one means anything else that is outstanding is
  2671. // irrelevant.
  2672. //
  2673. g_sUSBHCD[ulIndex].ulUSBHIntEvents = INT_EVENT_VBUS_ERR;
  2674. return;
  2675. }
  2676. //
  2677. // Babble Interrupt generated.
  2678. //
  2679. if(ulStatus & USB_INTCTRL_BABBLE)
  2680. {
  2681. g_sUSBHCD[ulIndex].ulUSBHIntEvents |= INT_EVENT_BABBLE_FAULT;
  2682. return;
  2683. }
  2684. //
  2685. // Suspend was signaled on the bus.
  2686. //
  2687. if(ulStatus & USB_INTCTRL_SUSPEND)
  2688. {
  2689. }
  2690. //
  2691. // Start the session.
  2692. //
  2693. if(ulStatus & USB_INTCTRL_SESSION)
  2694. {
  2695. //
  2696. // Power the USB bus.
  2697. //
  2698. USBHostPwrEnable(g_USBInstance[ulIndex].uiBaseAddr );
  2699. USBOTGSessionRequest(g_USBInstance[ulIndex].uiBaseAddr , true);
  2700. }
  2701. //
  2702. // Resume was signaled on the bus.
  2703. //
  2704. if(ulStatus & USB_INTCTRL_RESUME)
  2705. {
  2706. }
  2707. //
  2708. // Device connected so tell the main routine to issue a reset.
  2709. //
  2710. if(ulStatus & USB_INTCTRL_CONNECT)
  2711. {
  2712. //
  2713. // Set the connect flag and clear disconnect if it happens to be set.
  2714. //
  2715. g_sUSBHCD[ulIndex].ulUSBHIntEvents |= INT_EVENT_CONNECT;
  2716. g_sUSBHCD[ulIndex].ulUSBHIntEvents &= ~INT_EVENT_DISCONNECT;
  2717. //
  2718. // Power the USB bus.
  2719. //
  2720. USBHostPwrEnable(g_USBInstance[ulIndex].uiBaseAddr );
  2721. }
  2722. //
  2723. // Device was unplugged.
  2724. //
  2725. if(ulStatus & USB_INTCTRL_DISCONNECT)
  2726. {
  2727. //
  2728. // Set the disconnect flag and clear connect if it happens to be set.
  2729. //
  2730. g_sUSBHCD[ulIndex].ulUSBHIntEvents |= INT_EVENT_DISCONNECT;
  2731. g_sUSBHCD[ulIndex].ulUSBHIntEvents &= ~INT_EVENT_CONNECT;
  2732. }
  2733. //
  2734. // Start of Frame was received.
  2735. //
  2736. if(ulStatus & USB_INTCTRL_SOF)
  2737. {
  2738. //
  2739. // Increment the global Start of Frame counter.
  2740. //
  2741. g_ulUSBSOFCount++;
  2742. //
  2743. // Increment our SOF divider.
  2744. //
  2745. ulSOFDivide++;
  2746. //
  2747. // Have we counted enough SOFs to allow us to call the tick function?
  2748. //
  2749. if(ulSOFDivide == USB_SOF_TICK_DIVIDE)
  2750. {
  2751. //
  2752. // Yes - reset the divider and call the SOF tick handler.
  2753. //
  2754. ulSOFDivide = 0;
  2755. InternalUSBStartOfFrameTick(USB_SOF_TICK_DIVIDE, ulIndex);
  2756. }
  2757. }
  2758. //
  2759. // Handle end point 0 interrupts.
  2760. //
  2761. if(epStatus & USB_INTEP_0)
  2762. {
  2763. USBHCDEnumHandler(ulIndex);
  2764. }
  2765. /*
  2766. converting the epstatus(Wrapper register data) to ulStatus( MUSB register data)
  2767. */
  2768. if(endPStatus == NULL)
  2769. {
  2770. epnStatus = 0xFF & epStatus;
  2771. epnStatus = epnStatus | ((0xFF00 & epStatus)<<8);
  2772. }
  2773. else
  2774. {
  2775. epnStatus = epStatus;
  2776. }
  2777. #ifdef DMA_MODE
  2778. // Get the DMA Interrupt status
  2779. pendReg = CppiDmaGetPendStatus(ulIndex);
  2780. #endif
  2781. //
  2782. // Check to see if any uDMA transfers are pending
  2783. //
  2784. for(ulIdx = 0; ulIdx < MAX_NUM_PIPES; ulIdx++)
  2785. {
  2786. if((epnStatus == 0) && (g_sUSBHCD[ulIndex].ulDMAPending == 0))
  2787. {
  2788. break;
  2789. }
  2790. #ifdef DMA_MODE
  2791. //
  2792. // Check for any pending RX transaction
  2793. //
  2794. if((pendReg & CPDMA_RX_PENDING) && (g_sUSBHCD[ulIndex].ulDMAPending &
  2795. (DMA_PEND_RECEIVE_FLAG << ulIdx)))
  2796. {
  2797. //
  2798. //Reset the pending flag
  2799. //
  2800. g_sUSBHCD[ulIndex].ulDMAPending &= ~(DMA_PEND_RECEIVE_FLAG << ulIdx);
  2801. //
  2802. //Read the completion queue
  2803. //
  2804. g_sUSBHCD[ulIndex].rxBuffer = (unsigned char *)dmaRxCompletion(ulIndex,
  2805. INDEX_TO_USB_EP(ulIdx + 1));
  2806. //
  2807. //Send an ACk
  2808. //
  2809. USBHostEndpointDataAck(g_USBInstance[ulIndex].uiBaseAddr ,
  2810. INDEX_TO_USB_EP(ulIdx + 1));
  2811. //
  2812. //Set the pipe status
  2813. //
  2814. g_sUSBHCD[ulIndex].USBINPipes[ulIdx].eState = PIPE_DATA_READY;
  2815. //
  2816. //Set the RX event
  2817. //
  2818. ulEvent = USB_EVENT_RX_AVAILABLE;
  2819. //
  2820. // Only call a handler if one is present.
  2821. //
  2822. if(g_sUSBHCD[ulIndex].USBINPipes[ulIdx].pfnCallback)
  2823. {
  2824. g_sUSBHCD[ulIndex].USBINPipes[ulIdx].pfnCallback(ulIndex,
  2825. IN_PIPE_HANDLE(ulIndex, ulIdx), ulEvent);
  2826. }
  2827. }
  2828. //
  2829. //Check for any pending TX transaction
  2830. //
  2831. if((pendReg & CPDMA_TX_PENDING) && (g_sUSBHCD[ulIndex].ulDMAPending &
  2832. (DMA_PEND_TRANSMIT_FLAG << ulIdx)))
  2833. {
  2834. //
  2835. // Handle the case where the pipe is writing
  2836. //
  2837. if(g_sUSBHCD[ulIndex].USBOUTPipes[ulIdx].eState == PIPE_WRITING)
  2838. {
  2839. //
  2840. //Reset the the pending flag
  2841. //
  2842. g_sUSBHCD[ulIndex].ulDMAPending &= ~(DMA_PEND_TRANSMIT_FLAG << ulIdx);
  2843. //
  2844. //Read the completion queue
  2845. //
  2846. dmaTxCompletion(ulIndex,
  2847. INDEX_TO_USB_EP(ulIdx + 1));
  2848. //
  2849. // Data was transmitted successfully.
  2850. //
  2851. g_sUSBHCD[ulIndex].USBOUTPipes[ulIdx].eState = PIPE_DATA_SENT;
  2852. //
  2853. // Notify the pipe that its last transaction was completed.
  2854. //
  2855. ulEvent = USB_EVENT_TX_COMPLETE;
  2856. }
  2857. }
  2858. #endif
  2859. //
  2860. // Check the next pipe, the first time through this will clear out
  2861. // any interrupts dealing with endpoint zero since it was handled above.
  2862. //
  2863. epnStatus >>= 1;
  2864. //
  2865. // Check the status of the transmit(OUT) pipes.
  2866. //
  2867. if(epnStatus & 1)
  2868. {
  2869. //
  2870. // Read the status of the endpoint connected to this pipe.
  2871. //
  2872. ulEPStatus = USBEndpointStatus(g_USBInstance[ulIndex].uiBaseAddr ,
  2873. INDEX_TO_USB_EP(ulIdx + 1));
  2874. if(ulEPStatus & USB_HOST_OUT_ERROR)
  2875. {
  2876. //
  2877. // 3 failed attemps made to send packet. Device is non responsive.
  2878. // Clear the error condition on the endpoint.
  2879. //
  2880. USBHostEndpointStatusClear(g_USBInstance[ulIndex].uiBaseAddr ,
  2881. INDEX_TO_USB_EP(ulIdx + 1),
  2882. USB_HOST_OUT_ERROR);
  2883. //
  2884. // Data OUT failed. Flush the FIFO.
  2885. //
  2886. USBFIFOFlush(g_USBInstance[ulIndex].uiBaseAddr ,
  2887. INDEX_TO_USB_EP(ulIdx + 1),
  2888. USB_EP_HOST_OUT);
  2889. //
  2890. // Save the Pipes error state.
  2891. //
  2892. g_sUSBHCD[ulIndex].USBOUTPipes[ulIdx].eState = PIPE_ERROR;
  2893. //
  2894. // Notify the pipe that had an error.
  2895. //
  2896. ulEvent = USB_EVENT_ERROR;
  2897. }
  2898. else if(ulEPStatus & USB_HOST_OUT_STALL)
  2899. {
  2900. //
  2901. // Clear the stall condition on this endpoint pipe.
  2902. //
  2903. USBHostEndpointStatusClear(g_USBInstance[ulIndex].uiBaseAddr ,
  2904. INDEX_TO_USB_EP(ulIdx + 1),
  2905. USB_HOST_OUT_STALL);
  2906. //
  2907. // Save the STALLED state.
  2908. //
  2909. g_sUSBHCD[ulIndex].USBOUTPipes[ulIdx].eState = PIPE_STALLED;
  2910. //
  2911. // Notify the pipe that it was stalled.
  2912. //
  2913. ulEvent = USB_EVENT_STALL;
  2914. }
  2915. else
  2916. {
  2917. //
  2918. // Data was transmitted successfully.
  2919. //
  2920. g_sUSBHCD[ulIndex].USBOUTPipes[ulIdx].eState = PIPE_DATA_SENT;
  2921. //
  2922. // Notify the pipe that its last transaction was completed.
  2923. //
  2924. ulEvent = USB_EVENT_TX_COMPLETE;
  2925. }
  2926. // Clear the stall condition on this endpoint pipe.
  2927. //
  2928. USBHostEndpointStatusClear(g_USBInstance[ulIndex].uiBaseAddr,
  2929. INDEX_TO_USB_EP(ulIdx + 1),
  2930. ulEPStatus);
  2931. //
  2932. // Only call a handler if one is present.
  2933. //
  2934. if(g_sUSBHCD[ulIndex].USBOUTPipes[ulIdx].pfnCallback)
  2935. {
  2936. g_sUSBHCD[ulIndex].USBOUTPipes[ulIdx].pfnCallback(ulIndex, OUT_PIPE_HANDLE(ulIndex, ulIdx),
  2937. ulEvent);
  2938. }
  2939. }
  2940. //
  2941. // Check the status of the receive(IN) pipes.
  2942. //
  2943. if(epnStatus & 0x10000)
  2944. {
  2945. //
  2946. // Clear the status flag for the IN Pipe.
  2947. //
  2948. epnStatus &= ~0x10000;
  2949. //
  2950. // Read the status of the endpoint connected to this pipe.
  2951. //
  2952. ulEPStatus = USBEndpointStatus(g_USBInstance[ulIndex].uiBaseAddr ,
  2953. INDEX_TO_USB_EP(ulIdx + 1));
  2954. if(ulEPStatus & USB_HOST_IN_ERROR)
  2955. {
  2956. //
  2957. // 3 failed attemps made to receive packet. Device is non responsive.
  2958. // Clear the error condition on the endpoint.
  2959. //
  2960. USBHostEndpointStatusClear(g_USBInstance[ulIndex].uiBaseAddr,
  2961. INDEX_TO_USB_EP(ulIdx + 1),
  2962. USB_HOST_IN_ERROR);
  2963. //
  2964. // Data IN failed. Flush the FIFO.
  2965. //
  2966. USBFIFOFlush(g_USBInstance[ulIndex].uiBaseAddr ,
  2967. INDEX_TO_USB_EP(ulIdx + 1),
  2968. USB_EP_HOST_IN);
  2969. //
  2970. // Save the Pipes error state.
  2971. //
  2972. g_sUSBHCD[ulIndex].USBINPipes[ulIdx].eState = PIPE_ERROR;
  2973. //
  2974. // Notify the pipe that it was stalled.
  2975. //
  2976. ulEvent = USB_EVENT_ERROR;
  2977. }
  2978. else if(ulEPStatus & USB_HOST_IN_STALL)
  2979. {
  2980. //
  2981. // Clear the stall condition on this endpoint pipe.
  2982. //
  2983. USBHostEndpointStatusClear(g_USBInstance[ulIndex].uiBaseAddr ,
  2984. INDEX_TO_USB_EP(ulIdx + 1),
  2985. USB_HOST_IN_STALL);
  2986. //
  2987. // Save the STALLED state.
  2988. //
  2989. g_sUSBHCD[ulIndex].USBINPipes[ulIdx].eState = PIPE_STALLED;
  2990. //
  2991. // Notify the pipe that it was stalled.
  2992. //
  2993. ulEvent = USB_EVENT_STALL;
  2994. }
  2995. else
  2996. {
  2997. //
  2998. // Data is available.
  2999. //
  3000. g_sUSBHCD[ulIndex].USBINPipes[ulIdx].eState = PIPE_DATA_READY;
  3001. //
  3002. // Notify the pipe that its last transaction was completed.
  3003. //
  3004. ulEvent = USB_EVENT_RX_AVAILABLE;
  3005. }
  3006. //
  3007. // Only call a handler if one is present.
  3008. //
  3009. if(g_sUSBHCD[ulIndex].USBINPipes[ulIdx].pfnCallback)
  3010. {
  3011. g_sUSBHCD[ulIndex].USBINPipes[ulIdx].pfnCallback(ulIndex, IN_PIPE_HANDLE(ulIndex, ulIdx),
  3012. ulEvent);
  3013. }
  3014. }
  3015. }
  3016. //
  3017. // If there is an active driver and it has a call back then call it.
  3018. //
  3019. if((g_sUSBHCD[ulIndex].iUSBHActiveDriver >= 0) &&
  3020. (g_sUSBHCD[ulIndex].pClassDrivers[g_sUSBHCD[ulIndex].iUSBHActiveDriver]->pfnIntHandler))
  3021. {
  3022. g_sUSBHCD[ulIndex].pClassDrivers[g_sUSBHCD[ulIndex].iUSBHActiveDriver]->
  3023. pfnIntHandler(g_sUSBHCD[ulIndex].pvDriverInstance);
  3024. }
  3025. }
  3026. //*****************************************************************************
  3027. //
  3028. //! The USB host mode interrupt handler for controller index 0.
  3029. //!
  3030. //! This the main USB interrupt handler entry point. This handler will branch
  3031. //! the interrupt off to the appropriate handlers depending on the current
  3032. //! status of the USB controller. This function must be placed in the
  3033. //! interrupt table in order for the USB Library host stack to function.
  3034. //!
  3035. //! \return None.
  3036. //
  3037. //*****************************************************************************
  3038. void
  3039. USB0HostIntHandler(void)
  3040. {
  3041. unsigned int ulStatus = 0;
  3042. unsigned int ulIndex = 0;
  3043. #if defined (am335x_15x15) || defined(am335x) || defined(c6a811x)
  3044. unsigned int epStatus = 0;
  3045. ulStatus = HWREG(g_USBInstance[ulIndex].uiSubBaseAddr + USB_0_IRQ_STATUS_1);
  3046. epStatus = HWREG(g_USBInstance[ulIndex].uiSubBaseAddr + USB_0_IRQ_STATUS_0);
  3047. HWREG(g_USBInstance[ulIndex].uiSubBaseAddr + USB_0_IRQ_STATUS_1) = ulStatus;
  3048. HWREG(g_USBInstance[ulIndex].uiSubBaseAddr + USB_0_IRQ_STATUS_0) = epStatus;
  3049. #ifdef DMA_MODE
  3050. HWREG(USBSS_BASE + USBSS_IRQ_STATUS) =
  3051. HWREG(USBSS_BASE + USBSS_IRQ_STATUS);
  3052. #endif
  3053. USBHostIntHandlerInternal(ulIndex, ulStatus, &epStatus);
  3054. HWREG(g_USBInstance[ulIndex].uiSubBaseAddr + USB_0_IRQ_EOI) = 0;
  3055. #ifdef DMA_MODE
  3056. HWREG(USBSS_BASE + USBSS_IRQ_EOI) = 0;
  3057. #endif
  3058. #else
  3059. //
  3060. // Get the control interrupt status.
  3061. //
  3062. ulStatus = HWREG(g_USBInstance[ulIndex].uiSubBaseAddr + USB_0_INTR_SRC);
  3063. // Clear the Interrupts
  3064. HWREG(g_USBInstance[ulIndex].uiSubBaseAddr + USB_0_INTR_SRC_CLEAR) = ulStatus;
  3065. #ifdef _TMS320C6X
  3066. IntEventClear(g_USBInstance[ulIndex].uiInterruptNum);
  3067. #else
  3068. IntSystemStatusClear(g_USBInstance[ulIndex].uiInterruptNum);
  3069. #endif
  3070. //
  3071. // Call the internal handler to process the interrupts.
  3072. //
  3073. USBHostIntHandlerInternal(ulIndex, ulStatus, NULL);
  3074. // End of Interrupts
  3075. HWREG(g_USBInstance[ulIndex].uiSubBaseAddr + USB_0_END_OF_INTR) = 0;
  3076. #endif
  3077. }
  3078. //*****************************************************************************
  3079. //
  3080. //! The USB host mode interrupt handler for controller index 1.
  3081. //!
  3082. //! This the main USB interrupt handler entry point. This handler will branch
  3083. //! the interrupt off to the appropriate handlers depending on the current
  3084. //! status of the USB controller. This function must be placed in the
  3085. //! interrupt table in order for the USB Library host stack to function.
  3086. //!
  3087. //! \return None.
  3088. //
  3089. //*****************************************************************************
  3090. void
  3091. USB1HostIntHandler(void)
  3092. {
  3093. unsigned int ulStatus = 0;
  3094. unsigned int ulIndex = 1;
  3095. #if defined (am335x_15x15) || defined(am335x) || defined(c6a811x)
  3096. unsigned int epStatus = 0;
  3097. ulStatus = HWREG(g_USBInstance[ulIndex].uiSubBaseAddr + USB_0_IRQ_STATUS_1);
  3098. epStatus = HWREG(g_USBInstance[ulIndex].uiSubBaseAddr + USB_0_IRQ_STATUS_0);
  3099. HWREG(g_USBInstance[ulIndex].uiSubBaseAddr + USB_0_IRQ_STATUS_1) = ulStatus;
  3100. HWREG(g_USBInstance[ulIndex].uiSubBaseAddr + USB_0_IRQ_STATUS_0) = epStatus;
  3101. #ifdef DMA_MODE
  3102. HWREG(USBSS_BASE + USBSS_IRQ_STATUS) =
  3103. HWREG(USBSS_BASE + USBSS_IRQ_STATUS);
  3104. #endif
  3105. USBHostIntHandlerInternal(ulIndex, ulStatus, &epStatus);
  3106. HWREG(g_USBInstance[ulIndex].uiSubBaseAddr + USB_0_IRQ_EOI) = 0;
  3107. #ifdef DMA_MODE
  3108. HWREG(USBSS_BASE + USBSS_IRQ_EOI) = 0;
  3109. #endif
  3110. #else
  3111. //
  3112. // Get the control interrupt status.
  3113. //
  3114. ulStatus = HWREG(g_USBInstance[ulIndex].uiSubBaseAddr + USB_0_INTR_SRC);
  3115. // Clear the Interrupts
  3116. HWREG(g_USBInstance[ulIndex].uiSubBaseAddr + USB_0_INTR_SRC_CLEAR) = ulStatus;
  3117. #ifdef _TMS320C6X
  3118. IntEventClear(g_USBInstance[ulIndex].uiInterruptNum);
  3119. #else
  3120. IntSystemStatusClear(g_USBInstance[ulIndex].uiInterruptNum);
  3121. #endif
  3122. //
  3123. // Call the internal handler to process the interrupts.
  3124. //
  3125. USBHostIntHandlerInternal(ulIndex, ulStatus, NULL);
  3126. // End of Interrupts
  3127. HWREG(g_USBInstance[ulIndex].uiSubBaseAddr + USB_0_END_OF_INTR) = 0;
  3128. #endif
  3129. }
  3130. //*****************************************************************************
  3131. //
  3132. //! This function opens the class driver.
  3133. //!
  3134. //! \param ulIndex specifies which USB controller to use.
  3135. //! \param ulDeviceNum is the device number for the driver to load.
  3136. //!
  3137. //! This function opens the driver needed based on the class value found in
  3138. //! the device's interface descriptor.
  3139. //!
  3140. //! \return This function returns -1 if no driver is found, or it returns the
  3141. //! index of the driver found in the list of host class drivers.
  3142. //
  3143. //*****************************************************************************
  3144. static int
  3145. USBHCDOpenDriver(unsigned int ulIndex, unsigned int ulDeviceNum)
  3146. {
  3147. int iDriver;
  3148. unsigned int ulClass;
  3149. tInterfaceDescriptor *pInterface;
  3150. ASSERT(ulIndex == 0);
  3151. //
  3152. // Get the interface descriptor.
  3153. //
  3154. pInterface = USBDescGetInterface(g_sUSBHCD[ulIndex].USBDevice[0].pConfigDescriptor,
  3155. g_sUSBHCD[ulIndex].USBDevice[0].ulInterface,
  3156. USB_DESC_ANY);
  3157. //
  3158. // Read the interface class.
  3159. //
  3160. ulClass = pInterface->bInterfaceClass;
  3161. //
  3162. // Search through the Host Class driver list for the devices class.
  3163. //
  3164. for(iDriver = 0; iDriver < g_sUSBHCD[ulIndex].ulNumClassDrivers; iDriver++)
  3165. {
  3166. //
  3167. // If a driver was found call the open for this driver and save which
  3168. // driver is in use.
  3169. //
  3170. if(g_sUSBHCD[ulIndex].pClassDrivers[iDriver]->ulInterfaceClass == ulClass)
  3171. {
  3172. //
  3173. // Call the open function for the class driver.
  3174. //
  3175. g_sUSBHCD[ulIndex].pvDriverInstance = g_sUSBHCD[ulIndex].pClassDrivers[iDriver]->pfnOpen(
  3176. &g_sUSBHCD[ulIndex].USBDevice[0], ulDeviceNum);
  3177. //
  3178. // If the driver was successfully loaded then break out of the
  3179. // loop.
  3180. //
  3181. if(g_sUSBHCD[ulIndex].pvDriverInstance != 0)
  3182. {
  3183. break;
  3184. }
  3185. }
  3186. }
  3187. //
  3188. // If no drivers were found then return -1 to indicate an invalid
  3189. // driver instance.
  3190. //
  3191. if(iDriver == g_sUSBHCD[ulIndex].ulNumClassDrivers)
  3192. {
  3193. //
  3194. // Send an unknown connection event.
  3195. //
  3196. SendUnknownConnect(ulIndex, ulClass, ulDeviceNum);
  3197. //
  3198. // Indicate that no driver was found.
  3199. //
  3200. iDriver = -1;
  3201. }
  3202. return(iDriver);
  3203. }
  3204. //*****************************************************************************
  3205. //
  3206. // This function will send an event to a registered event driver.
  3207. //
  3208. // \param ulIndex is one of the USB_EVENT_* values.
  3209. //
  3210. // This function is only used internally to the USB library and will check
  3211. // if an event driver is registered and send on the event.
  3212. //
  3213. // Note: This function should not be called outside of the USB library.
  3214. //
  3215. // \return None.
  3216. //
  3217. //*****************************************************************************
  3218. void
  3219. InternalUSBHCDSendEvent(unsigned int ulIndex, unsigned int ulEvent)
  3220. {
  3221. //
  3222. // Make sure that an event driver has been registered.
  3223. //
  3224. if((g_sUSBHCD[ulIndex].iEventDriver != -1) &&
  3225. (g_sUSBHCD[ulIndex].pClassDrivers[g_sUSBHCD[ulIndex].iEventDriver]->pfnIntHandler))
  3226. {
  3227. //
  3228. // Send an event to the application.
  3229. //
  3230. g_sUSBHCD[ulIndex].EventInfo.ulEvent = ulEvent;
  3231. g_sUSBHCD[ulIndex].pClassDrivers[g_sUSBHCD[ulIndex].iEventDriver]->pfnIntHandler(
  3232. &g_sUSBHCD[ulIndex].EventInfo);
  3233. }
  3234. }
  3235. //*****************************************************************************
  3236. //
  3237. // This function handles the necessary clean up for device disconnect.
  3238. //
  3239. // \param ulIndex is the device number for the device that was disconnected.
  3240. //
  3241. // This function handles all of the necessary clean up after a device
  3242. // disconnect has been detected by the stack. This includes calling back the
  3243. // appropriate driver if necessary.
  3244. //
  3245. // \return None.
  3246. //
  3247. //*****************************************************************************
  3248. static void
  3249. USBHCDDeviceDisconnected(unsigned int ulIndex, unsigned int ulInstance)
  3250. {
  3251. ASSERT(ulIndex == 0);
  3252. if(g_sUSBHCD[ulIndex].USBDevice[0].pConfigDescriptor)
  3253. {
  3254. //
  3255. // Invalidate the configuration descriptor.
  3256. //
  3257. g_sUSBHCD[ulIndex].USBDevice[0].pConfigDescriptor = 0;
  3258. }
  3259. //
  3260. // Reset the max packet size so that this will be re-read from new devices.
  3261. //
  3262. g_sUSBHCD[ulIndex].USBDevice[0].DeviceDescriptor.bMaxPacketSize0 = 0;
  3263. //
  3264. // No longer have a device descriptor.
  3265. //
  3266. g_sUSBHCD[ulIndex].USBDevice[0].DeviceDescriptor.bLength = 0;
  3267. //
  3268. // No longer addressed.
  3269. //
  3270. g_sUSBHCD[ulIndex].USBDevice[0].ulAddress = 0;
  3271. //
  3272. // If this was an active driver then close it out.
  3273. //
  3274. if(g_sUSBHCD[ulIndex].iUSBHActiveDriver >= 0)
  3275. {
  3276. //
  3277. // Call the driver Close entry point.
  3278. //
  3279. g_sUSBHCD[ulIndex].pClassDrivers[g_sUSBHCD[ulIndex].iUSBHActiveDriver]->
  3280. pfnClose((void *)ulInstance);
  3281. //
  3282. // No active driver now present.
  3283. //
  3284. g_sUSBHCD[ulIndex].iUSBHActiveDriver = -1;
  3285. g_sUSBHCD[ulIndex].pvDriverInstance = 0;
  3286. }
  3287. else
  3288. {
  3289. if((g_sUSBHCD[ulIndex].iEventDriver != -1) &&
  3290. (g_sUSBHCD[ulIndex].pClassDrivers[g_sUSBHCD[ulIndex].iEventDriver]->pfnIntHandler))
  3291. {
  3292. //
  3293. // Send the generic disconnect event.
  3294. //
  3295. g_sUSBHCD[ulIndex].EventInfo.ulEvent = USB_EVENT_DISCONNECTED;
  3296. g_sUSBHCD[ulIndex].pClassDrivers[g_sUSBHCD[ulIndex].iEventDriver]->pfnIntHandler(
  3297. &g_sUSBHCD[ulIndex].EventInfo);
  3298. //
  3299. // Reset the class and the instance.
  3300. //
  3301. g_sUSBHCD[ulIndex].ulClass = USB_CLASS_EVENTS;
  3302. g_sUSBHCD[ulIndex].EventInfo.ulInstance = 0;
  3303. }
  3304. }
  3305. }
  3306. //*****************************************************************************
  3307. //
  3308. //! This function is the main routine for the Host Controller Driver.
  3309. //!
  3310. //! This function is the main routine for the host controller driver, and must
  3311. //! be called periodically by the main application outside of a callback
  3312. //! context. This allows for a simple cooperative system to access the the
  3313. //! host controller driver interface without the need for an RTOS. All time
  3314. //! critical operations are handled in interrupt context but all blocking
  3315. //! operations are run from the this function to allow them to block and wait
  3316. //! for completion without holding off other interrupts.
  3317. //!
  3318. //! \return None.
  3319. //
  3320. //*****************************************************************************
  3321. void
  3322. USBHCDMain(unsigned int ulIndex, unsigned int ulInstance)
  3323. {
  3324. unsigned int ulIntState;
  3325. tUSBHDeviceState eOldState;
  3326. //
  3327. // Save the old state to detect changes properly.
  3328. //
  3329. eOldState = g_sUSBHCD[ulIndex].eDeviceState[0];
  3330. //
  3331. // Perform this fixup with interrupts disabled to prevent race
  3332. // conditions related to g_sUSBHCD[ulIndex].ulUSBHIntEvents.
  3333. //
  3334. #ifdef _TMS320C6X
  3335. ulIntState = IntGlobalDisable();
  3336. #else
  3337. ulIntState = IntDisable();
  3338. #endif
  3339. //
  3340. // Fix up the state if any important interrupt events occurred.
  3341. //
  3342. if(g_sUSBHCD[ulIndex].ulUSBHIntEvents)
  3343. {
  3344. if(g_sUSBHCD[ulIndex].ulUSBHIntEvents & INT_EVENT_POWER_FAULT)
  3345. {
  3346. //
  3347. // A power fault has occurred so notify the application.
  3348. //
  3349. if((g_sUSBHCD[ulIndex].iEventDriver != -1) &&
  3350. (g_sUSBHCD[ulIndex].pClassDrivers[g_sUSBHCD[ulIndex]
  3351. .iEventDriver]->pfnIntHandler))
  3352. {
  3353. //
  3354. // Send the generic power fault event.
  3355. //
  3356. g_sUSBHCD[ulIndex].EventInfo.ulEvent = USB_EVENT_POWER_FAULT;
  3357. g_sUSBHCD[ulIndex].pClassDrivers[g_sUSBHCD[ulIndex]
  3358. .iEventDriver]->pfnIntHandler(&g_sUSBHCD[ulIndex].EventInfo);
  3359. }
  3360. g_sUSBHCD[ulIndex].eDeviceState[0] = HCD_POWER_FAULT;
  3361. }
  3362. else if(g_sUSBHCD[ulIndex].ulUSBHIntEvents & INT_EVENT_VBUS_ERR)
  3363. {
  3364. //
  3365. // A VBUS error has occurred. This event trumps connect and
  3366. // disconnect since it will cause a controller reset.
  3367. //
  3368. g_sUSBHCD[ulIndex].eDeviceState[0] = HCD_VBUS_ERROR;
  3369. }
  3370. else if(g_sUSBHCD[ulIndex].ulUSBHIntEvents & INT_EVENT_BABBLE_FAULT)
  3371. {
  3372. //
  3373. // A VBUS error has occurred. This event trumps connect and
  3374. // disconnect since it will cause a controller reset.
  3375. //
  3376. g_sUSBHCD[ulIndex].eDeviceState[0] = HCD_BABBLE_ERROR;
  3377. }
  3378. else
  3379. {
  3380. //
  3381. // Has a device connected?
  3382. //
  3383. if(g_sUSBHCD[ulIndex].ulUSBHIntEvents & INT_EVENT_CONNECT)
  3384. {
  3385. g_sUSBHCD[ulIndex].eDeviceState[0] = HCD_DEV_RESET;
  3386. }
  3387. else
  3388. {
  3389. //
  3390. // Has a device disconnected?
  3391. //
  3392. if(g_sUSBHCD[ulIndex].ulUSBHIntEvents & INT_EVENT_DISCONNECT)
  3393. {
  3394. g_sUSBHCD[ulIndex].eDeviceState[0] = HCD_DEV_DISCONNECTED;
  3395. }
  3396. }
  3397. }
  3398. //
  3399. // Clear the flags.
  3400. //
  3401. g_sUSBHCD[ulIndex].ulUSBHIntEvents = 0;
  3402. //
  3403. // Turn interrupts back on if they were on when we were called.
  3404. //
  3405. }
  3406. #ifdef _TMS320C6X
  3407. IntGlobalRestore(ulIntState);
  3408. #else
  3409. IntEnable(ulIntState);
  3410. #endif
  3411. switch(g_sUSBHCD[ulIndex].eDeviceState[0])
  3412. {
  3413. //
  3414. // There was a power fault condition so shut down and wait for the
  3415. // application to re-initialized the system.
  3416. //
  3417. case HCD_POWER_FAULT:
  3418. {
  3419. break;
  3420. }
  3421. //
  3422. // There was a VBUS error so handle it.
  3423. //
  3424. case HCD_VBUS_ERROR:
  3425. {
  3426. //
  3427. // Disable USB interrupts.
  3428. //
  3429. #ifdef _TMS320C6X
  3430. /* No DSP API to disable USB0 event */
  3431. #else
  3432. IntSystemDisable(g_USBInstance[ulIndex].uiInterruptNum);
  3433. #endif
  3434. //
  3435. // If there was a device in any state of connection then indicate
  3436. // that it has been disconnected.
  3437. //
  3438. if((eOldState != HCD_IDLE) && (eOldState != HCD_POWER_FAULT))
  3439. {
  3440. //
  3441. // Handle device disconnect.
  3442. //
  3443. USBHCDDeviceDisconnected(ulIndex, ulInstance);
  3444. }
  3445. //
  3446. // Reset the controller.
  3447. //
  3448. //SysCtlPeripheralReset(SYSCTL_PERIPH_USB0);
  3449. USBReset(g_USBInstance[ulIndex].uiSubBaseAddr);
  3450. //
  3451. // Wait for 100ms before trying to re-power the device.
  3452. //
  3453. delay(100);
  3454. //
  3455. // Re-initialize the HCD.
  3456. //
  3457. USBHCDInitInternal(ulIndex, g_sUSBHCD[ulIndex].pvPool,
  3458. g_sUSBHCD[ulIndex].ulPoolSize);
  3459. #ifdef _TMS320C6X
  3460. /* No DSP API to disable USB0 event */
  3461. #else
  3462. IntSystemEnable(g_USBInstance[ulIndex].uiInterruptNum);
  3463. #endif
  3464. break;
  3465. }
  3466. //
  3467. // Trigger a reset to the connected device.
  3468. //
  3469. case HCD_DEV_RESET:
  3470. {
  3471. //
  3472. // Trigger a Reset.
  3473. //
  3474. USBHCDReset(ulIndex);
  3475. //
  3476. // The state moves to connected but not configured.
  3477. //
  3478. g_sUSBHCD[ulIndex].eDeviceState[0] = HCD_DEV_CONNECTED;
  3479. break;
  3480. }
  3481. //
  3482. // Device connection has been established now start enumerating
  3483. // the device.
  3484. //
  3485. case HCD_DEV_CONNECTED:
  3486. {
  3487. //
  3488. // First get the speed of the device
  3489. //
  3490. g_sUSBHCD[ulIndex].USBDevice[0].ulDeviceSpeed =
  3491. USBHCDGetSpeed(ulIndex);
  3492. //
  3493. // First check if we have read the device descriptor at all
  3494. // before proceeding.
  3495. //
  3496. if(g_sUSBHCD[ulIndex].USBDevice[0].DeviceDescriptor.bLength == 0)
  3497. {
  3498. //
  3499. // Initialize a request for the device descriptor.
  3500. //
  3501. if(USBHCDGetDeviceDescriptor(ulIndex, &g_sUSBHCD[ulIndex]
  3502. .USBDevice[0]) == 0)
  3503. {
  3504. //
  3505. // If the device descriptor cannot be read then the device
  3506. // will be treated as unknown.
  3507. //
  3508. g_sUSBHCD[ulIndex].eDeviceState[0] = HCD_DEV_ERROR;
  3509. //
  3510. // Send an unknown connection event.
  3511. //
  3512. SendUnknownConnect(ulIndex, 0, ulInstance);
  3513. }
  3514. }
  3515. //
  3516. // If we have the device descriptor then move on to setting
  3517. // the address of the device.
  3518. //
  3519. else if(g_sUSBHCD[ulIndex].USBDevice[0].ulAddress == 0)
  3520. {
  3521. //
  3522. // Send the set address command.
  3523. //
  3524. USBHCDSetAddress(ulIndex, 1);
  3525. //
  3526. // Save the address.
  3527. //
  3528. g_sUSBHCD[ulIndex].USBDevice[0].ulAddress = 1;
  3529. //
  3530. // Move on to the addressed state.
  3531. //
  3532. g_sUSBHCD[ulIndex].eDeviceState[0] = HCD_DEV_ADDRESSED;
  3533. }
  3534. break;
  3535. }
  3536. case HCD_DEV_ADDRESSED:
  3537. {
  3538. //
  3539. // First check if we have read the configuration descriptor.
  3540. //
  3541. if (g_sUSBHCD[ulIndex].USBDevice[0].pConfigDescriptor == 0)
  3542. {
  3543. //
  3544. // Initialize a request for the device descriptor.
  3545. //
  3546. if(USBHCDGetConfigDescriptor(ulIndex, &g_sUSBHCD[ulIndex]
  3547. .USBDevice[0]) == 0)
  3548. {
  3549. //
  3550. // If the device descriptor cannot be read then the device
  3551. // will be treated as unknown.
  3552. //
  3553. g_sUSBHCD[ulIndex].eDeviceState[0] = HCD_DEV_ERROR;
  3554. //
  3555. // Send an unknown connection event.
  3556. //
  3557. SendUnknownConnect(ulIndex, 0, ulInstance);
  3558. }
  3559. }
  3560. //
  3561. // Now have addressed and received the device configuration,
  3562. // so get ready to set the device configuration.
  3563. //
  3564. else
  3565. {
  3566. //
  3567. // Use the first configuration to set the device
  3568. // configuration.
  3569. //
  3570. USBHCDSetConfig(ulIndex, (unsigned int)&g_sUSBHCD[ulIndex]
  3571. .USBDevice[0], 1);
  3572. //
  3573. // Move on to the configured state.
  3574. //
  3575. g_sUSBHCD[ulIndex].eDeviceState[0] = HCD_DEV_CONFIGURED;
  3576. //
  3577. // Open the driver for device 0.
  3578. //
  3579. g_sUSBHCD[ulIndex].iUSBHActiveDriver =
  3580. USBHCDOpenDriver(ulIndex, ulInstance);
  3581. }
  3582. break;
  3583. }
  3584. //
  3585. // The device was making a request and is now complete.
  3586. //
  3587. case HCD_DEV_REQUEST:
  3588. {
  3589. g_sUSBHCD[ulIndex].eDeviceState[0] = HCD_DEV_CONNECTED;
  3590. break;
  3591. }
  3592. //
  3593. // The strings are currently not accessed.
  3594. //
  3595. case HCD_DEV_GETSTRINGS:
  3596. {
  3597. break;
  3598. }
  3599. //
  3600. // Basically Idle at this point.
  3601. //
  3602. case HCD_DEV_DISCONNECTED:
  3603. {
  3604. //
  3605. // Handle device disconnect.
  3606. //
  3607. USBHCDDeviceDisconnected(ulIndex, ulInstance);
  3608. //
  3609. // Return to the Idle state.
  3610. //
  3611. g_sUSBHCD[ulIndex].eDeviceState[0] = HCD_IDLE;
  3612. break;
  3613. }
  3614. //
  3615. // Connection and enumeration is complete so allow this function
  3616. // to exit.
  3617. //
  3618. case HCD_DEV_CONFIGURED:
  3619. {
  3620. break;
  3621. }
  3622. case HCD_BABBLE_ERROR:
  3623. {
  3624. USBHCDTerm(ulIndex);
  3625. USBHCDDeviceDisconnected(ulIndex, ulInstance);
  3626. UsbPhyOff(ulIndex);
  3627. USBReset(g_USBInstance[ulIndex].uiSubBaseAddr);
  3628. g_sUSBHCD[ulIndex].EventInfo.ulEvent = USB_EVENT_BABBLE_ERROR;
  3629. g_sUSBHCD[ulIndex].EventInfo.ulInstance = ulIndex;
  3630. g_sUSBHCD[ulIndex].pClassDrivers[g_sUSBHCD[ulIndex].iEventDriver]->pfnIntHandler(
  3631. &g_sUSBHCD[ulIndex].EventInfo);
  3632. break;
  3633. }
  3634. //
  3635. // Poorly behaving device are in limbo in this state until removed.
  3636. //
  3637. case HCD_DEV_ERROR:
  3638. {
  3639. //
  3640. // If there was a device in any state of connection then indicate
  3641. // that it has been disconnected.
  3642. //
  3643. if((eOldState != HCD_IDLE) && (eOldState != HCD_POWER_FAULT))
  3644. {
  3645. //
  3646. // Handle device disconnect.
  3647. //
  3648. USBHCDDeviceDisconnected(ulIndex, ulInstance);
  3649. }
  3650. //
  3651. // Reset the controller.
  3652. //
  3653. USBReset(g_USBInstance[ulIndex].uiSubBaseAddr);
  3654. //
  3655. // Wait for 100ms before trying to re-power the device.
  3656. //
  3657. delay(100);
  3658. //
  3659. // Re-initialize the HCD.
  3660. //
  3661. USBHCDInitInternal(ulIndex, g_sUSBHCD[ulIndex].pvPool,
  3662. g_sUSBHCD[ulIndex].ulPoolSize);
  3663. break;
  3664. }
  3665. default:
  3666. {
  3667. break;
  3668. }
  3669. }
  3670. g_sUSBHCD[ulIndex].ulConnectRetry = g_ulConnectRetry[ulIndex];
  3671. }
  3672. //*****************************************************************************
  3673. //
  3674. //! This function completes a control transaction to a device.
  3675. //!
  3676. //! \param ulIndex is the controller index to use for this transfer.
  3677. //! \param pSetupPacket is the setup request to be sent.
  3678. //! \param ulDevAddress is the address of the device for this request.
  3679. //! \param pData is the data to send for OUT requests or the receive buffer
  3680. //! for IN requests.
  3681. //! \param ulSize is the size of the buffer in pData.
  3682. //! \param ulMaxPacketSize is the maximum packet size for the device for this
  3683. //! request.
  3684. //!
  3685. //! This function handles the state changes necessary to send a control
  3686. //! transaction to a device. This function should not be called from within
  3687. //! an interrupt callback as it is a blocking function.
  3688. //!
  3689. //! \return The number of bytes of data that were sent or received as a result
  3690. //! of this request.
  3691. //
  3692. //*****************************************************************************
  3693. unsigned int
  3694. USBHCDControlTransfer(unsigned int ulIndex, tUSBRequest *pSetupPacket,
  3695. unsigned int ulDevAddress, unsigned char *pData,
  3696. unsigned int ulSize, unsigned int ulMaxPacketSize)
  3697. {
  3698. unsigned int ulRemaining;
  3699. unsigned int ulDataSize;
  3700. unsigned int ulTimer = 0;
  3701. unsigned int retStatus = 1;
  3702. ASSERT(g_sUSBHEP0State[ulIndex].eState == EP0_STATE_IDLE);
  3703. ASSERT(ulIndex == 0);
  3704. //
  3705. // Initialize the state of the data for this request.
  3706. //
  3707. g_sUSBHEP0State[ulIndex].pData = pData;
  3708. g_sUSBHEP0State[ulIndex].ulBytesRemaining = ulSize;
  3709. g_sUSBHEP0State[ulIndex].ulDataSize = ulSize;
  3710. //
  3711. // Set the maximum packet size.
  3712. //
  3713. g_sUSBHEP0State[ulIndex].ulMaxPacketSize = ulMaxPacketSize;
  3714. //
  3715. // Save the current address.
  3716. //
  3717. g_sUSBHEP0State[ulIndex].ulDevAddress = ulDevAddress;
  3718. //
  3719. // Set the address the host will used to communicate with the device.
  3720. //
  3721. USBHostAddrSet(g_USBInstance[ulIndex].uiBaseAddr , USB_EP_0,
  3722. g_sUSBHEP0State[ulIndex].ulDevAddress, USB_EP_HOST_OUT);
  3723. //
  3724. // Put the data in the correct FIFO.
  3725. //
  3726. USBEndpointDataPut(g_USBInstance[ulIndex].uiBaseAddr , USB_EP_0,
  3727. (unsigned char *)pSetupPacket, sizeof(tUSBRequest));
  3728. //
  3729. // If this is an IN request, change to that state.
  3730. //
  3731. if(pSetupPacket->bmRequestType & USB_RTYPE_DIR_IN)
  3732. {
  3733. g_sUSBHEP0State[ulIndex].eState = EP0_STATE_SETUP_IN;
  3734. }
  3735. else
  3736. {
  3737. //
  3738. // If there is no data then this is not an OUT request.
  3739. //
  3740. if(ulSize != 0)
  3741. {
  3742. //
  3743. // Since there is data, this is an OUT request.
  3744. //
  3745. g_sUSBHEP0State[ulIndex].eState = EP0_STATE_SETUP_OUT;
  3746. }
  3747. else
  3748. {
  3749. //
  3750. // Otherwise this request has no data and just a status phase.
  3751. //
  3752. g_sUSBHEP0State[ulIndex].eState = EP0_STATE_STATUS_IN;
  3753. }
  3754. }
  3755. //
  3756. // Send the Setup packet.
  3757. //
  3758. USBEndpointDataSend(g_USBInstance[ulIndex].uiBaseAddr , USB_EP_0,
  3759. USB_TRANS_SETUP);
  3760. if(USB_TIMEOUT_DISABLE!=g_sUSBHCD[ulIndex].USBHTimeOut.Value.slEP0)
  3761. {
  3762. ulTimer = g_sUSBHCD[ulIndex].USBHTimeOut.Value.slEP0;
  3763. }
  3764. StartTimer(ulTimer);
  3765. //
  3766. // Block until endpoint 0 returns to the IDLE state.
  3767. //
  3768. while((g_sUSBHEP0State[ulIndex].eState != EP0_STATE_IDLE)&&(!IsTimerElapsed()))
  3769. {
  3770. if(g_sUSBHEP0State[ulIndex].eState == EP0_STATE_ERROR)
  3771. {
  3772. USBHCDTxAbort(ulIndex, 0);
  3773. USBHCDRxAbort(ulIndex, 0);
  3774. retStatus = 0;
  3775. break;
  3776. }
  3777. //
  3778. // If we aborted the transfer due to an error, tell the caller
  3779. // that no bytes were transferred.
  3780. //
  3781. if(g_sUSBHCD[ulIndex].ulUSBHIntEvents & (INT_EVENT_VBUS_ERR
  3782. | INT_EVENT_DISCONNECT|INT_EVENT_BABBLE_FAULT))
  3783. {
  3784. USBHCDTxAbort(ulIndex, 0);
  3785. USBHCDRxAbort(ulIndex, 0);
  3786. retStatus = 0;
  3787. break;
  3788. }
  3789. }
  3790. StopTimer();
  3791. //
  3792. // Calculate and return the number of bytes that were sent or received.
  3793. // The extra copy into local variables is required to prevent some
  3794. // compilers from warning about undefined order of volatile access.
  3795. //
  3796. if(g_sUSBHEP0State[ulIndex].eState == EP0_STATE_IDLE)
  3797. {
  3798. ulDataSize = g_sUSBHEP0State[ulIndex].ulDataSize;
  3799. ulRemaining = g_sUSBHEP0State[ulIndex].ulBytesRemaining;
  3800. retStatus = (ulDataSize - ulRemaining);
  3801. }
  3802. else
  3803. {
  3804. retStatus = 0;
  3805. g_sUSBHCD[ulIndex].USBHTimeOut.Status.slEP0 = 1;
  3806. }
  3807. return retStatus;
  3808. }
  3809. //*****************************************************************************
  3810. //
  3811. // This is the endpoint 0 interrupt handler.
  3812. //
  3813. // \return None.
  3814. //
  3815. //*****************************************************************************
  3816. static void
  3817. USBHCDEnumHandler(unsigned int ulIndex)
  3818. {
  3819. unsigned int ulEPStatus;
  3820. unsigned int ulDataSize;
  3821. //
  3822. // Get the end point 0 status.
  3823. //
  3824. ulEPStatus = USBEndpointStatus(g_USBInstance[ulIndex].uiBaseAddr , USB_EP_0);
  3825. //
  3826. // If there was an error then go to the error state.
  3827. //
  3828. if(ulEPStatus == USB_HOST_EP0_ERROR)
  3829. {
  3830. //
  3831. // Clear this status indicating that the status packet was
  3832. // received.
  3833. //
  3834. USBHostEndpointStatusClear(g_USBInstance[ulIndex].uiBaseAddr,
  3835. USB_EP_0, USB_HOST_EP0_ERROR);
  3836. USBFIFOFlush(g_USBInstance[ulIndex].uiBaseAddr , USB_EP_0, 0);
  3837. //
  3838. // Just go back to the idle state.
  3839. //
  3840. g_sUSBHEP0State[ulIndex].eState = EP0_STATE_ERROR;
  3841. return;
  3842. }
  3843. switch(g_sUSBHEP0State[ulIndex].eState)
  3844. {
  3845. //
  3846. // Handle the status state, this is a transitory state from
  3847. // USB_STATE_TX or USB_STATE_RX back to USB_STATE_IDLE.
  3848. //
  3849. case EP0_STATE_STATUS:
  3850. {
  3851. //
  3852. // Handle the case of a received status packet.
  3853. //
  3854. if(ulEPStatus & (USB_HOST_EP0_RXPKTRDY | USB_HOST_EP0_STATUS))
  3855. {
  3856. //
  3857. // Clear this status indicating that the status packet was
  3858. // received.
  3859. //
  3860. USBHostEndpointStatusClear(g_USBInstance[ulIndex].uiBaseAddr,
  3861. USB_EP_0, (USB_HOST_EP0_RXPKTRDY |
  3862. USB_HOST_EP0_STATUS));
  3863. }
  3864. //
  3865. // Just go back to the idle state.
  3866. //
  3867. g_sUSBHEP0State[ulIndex].eState = EP0_STATE_IDLE;
  3868. break;
  3869. }
  3870. //
  3871. // This state triggers a STATUS IN request from the device.
  3872. //
  3873. case EP0_STATE_STATUS_IN:
  3874. {
  3875. //
  3876. // Generate an IN request from the device.
  3877. //
  3878. USBHostRequestStatus(g_USBInstance[ulIndex].uiBaseAddr );
  3879. //
  3880. // Change to the status phase and wait for the response.
  3881. //
  3882. g_sUSBHEP0State[ulIndex].eState = EP0_STATE_STATUS;
  3883. break;
  3884. }
  3885. //
  3886. // In the IDLE state the code is waiting to receive data from the host.
  3887. //
  3888. case EP0_STATE_IDLE:
  3889. {
  3890. break;
  3891. }
  3892. //
  3893. // Data is still being sent to the host so handle this in the
  3894. // EP0StateTx() function.
  3895. //
  3896. case EP0_STATE_SETUP_OUT:
  3897. {
  3898. //
  3899. // Send remaining data if necessary.
  3900. //
  3901. USBHCDEP0StateTx(ulIndex);
  3902. break;
  3903. }
  3904. //
  3905. // Handle the receive state for commands that are receiving data on
  3906. // endpoint 0.
  3907. //
  3908. case EP0_STATE_SETUP_IN:
  3909. {
  3910. //
  3911. // Generate a new IN request to the device.
  3912. //
  3913. USBHostRequestIN(g_USBInstance[ulIndex].uiBaseAddr , USB_EP_0);
  3914. //
  3915. // Proceed to the RX state to receive the requested data.
  3916. //
  3917. g_sUSBHEP0State[ulIndex].eState = EP0_STATE_RX;
  3918. break;
  3919. }
  3920. //
  3921. // The endponit remains in this state until all requested data has
  3922. // been received.
  3923. //
  3924. case EP0_STATE_RX:
  3925. {
  3926. //
  3927. // There was a stall on endpoint 0 so go back to the idle state
  3928. // as this command has been terminated.
  3929. //
  3930. if(ulEPStatus & USB_HOST_EP0_RX_STALL)
  3931. {
  3932. g_sUSBHEP0State[ulIndex].eState = EP0_STATE_IDLE;
  3933. //
  3934. // Clear the stalled state on endpoint 0.
  3935. //
  3936. USBHostEndpointStatusClear(g_USBInstance[ulIndex].uiBaseAddr,
  3937. USB_EP_0, ulEPStatus);
  3938. break;
  3939. }
  3940. //
  3941. // Set the number of bytes to get out of this next packet.
  3942. //
  3943. if(g_sUSBHEP0State[ulIndex].ulBytesRemaining > MAX_PACKET_SIZE_EP0)
  3944. {
  3945. //
  3946. // Don't send more than EP0_MAX_PACKET_SIZE bytes.
  3947. //
  3948. ulDataSize = MAX_PACKET_SIZE_EP0;
  3949. }
  3950. else
  3951. {
  3952. //
  3953. // There was space so send the remaining bytes.
  3954. //
  3955. ulDataSize = g_sUSBHEP0State[ulIndex].ulBytesRemaining;
  3956. }
  3957. if(ulDataSize != 0)
  3958. {
  3959. //
  3960. // Get the data from the USB controller end point 0.
  3961. //
  3962. USBEndpointDataGet(g_USBInstance[ulIndex].uiBaseAddr ,
  3963. USB_EP_0, g_sUSBHEP0State[ulIndex].pData,
  3964. &ulDataSize);
  3965. }
  3966. //
  3967. // Advance the pointer.
  3968. //
  3969. g_sUSBHEP0State[ulIndex].pData += ulDataSize;
  3970. //
  3971. // Decrement the number of bytes that are being waited on.
  3972. //
  3973. g_sUSBHEP0State[ulIndex].ulBytesRemaining -= ulDataSize;
  3974. //
  3975. // Need to ack the data on end point 0 in this case
  3976. // without setting data end.
  3977. //
  3978. USBDevEndpointDataAck(g_USBInstance[ulIndex].uiBaseAddr,
  3979. USB_EP_0, false);
  3980. //
  3981. // If there was not more than the maximum packet size bytes of data
  3982. // the this was a short packet and indicates that this transfer is
  3983. // complete. If there were exactly g_sUSBHEP0State[ulIndex].ulMaxPacketSize
  3984. // remaining then there still needs to be null packet sent before
  3985. // this transfer is complete.
  3986. //
  3987. if((ulDataSize < g_sUSBHEP0State[ulIndex].ulMaxPacketSize) ||
  3988. (g_sUSBHEP0State[ulIndex].ulBytesRemaining == 0))
  3989. {
  3990. //
  3991. // Return to the idle state.
  3992. //
  3993. g_sUSBHEP0State[ulIndex].eState = EP0_STATE_STATUS;
  3994. //
  3995. // No more data.
  3996. //
  3997. g_sUSBHEP0State[ulIndex].pData = 0;
  3998. //
  3999. // Send a null packet to acknowledge that all data was received.
  4000. //
  4001. USBEndpointDataSend(g_USBInstance[ulIndex].uiBaseAddr,
  4002. USB_EP_0, USB_TRANS_STATUS);
  4003. }
  4004. else
  4005. {
  4006. //
  4007. // Request more data.
  4008. //
  4009. USBHostRequestIN(g_USBInstance[ulIndex].uiBaseAddr, USB_EP_0);
  4010. }
  4011. break;
  4012. }
  4013. //
  4014. // The device stalled endpoint zero so check if the stall needs to be
  4015. // cleared once it has been successfully sent.
  4016. //
  4017. case EP0_STATE_STALL:
  4018. {
  4019. //
  4020. // Reset the global end point 0 state to IDLE.
  4021. //
  4022. g_sUSBHEP0State[ulIndex].eState = EP0_STATE_IDLE;
  4023. break;
  4024. }
  4025. //
  4026. // Halt on an unknown state, but only in DEBUG builds.
  4027. //
  4028. default:
  4029. {
  4030. ASSERT(0);
  4031. break;
  4032. }
  4033. }
  4034. }
  4035. //*****************************************************************************
  4036. //
  4037. // This internal function handles sending data on endpoint 0.
  4038. //
  4039. // \return None.
  4040. //
  4041. //*****************************************************************************
  4042. static void
  4043. USBHCDEP0StateTx(unsigned int ulIndex)
  4044. {
  4045. unsigned int ulNumBytes;
  4046. unsigned char *pData;
  4047. //
  4048. // In the TX state on endpoint 0.
  4049. //
  4050. g_sUSBHEP0State[ulIndex].eState = EP0_STATE_SETUP_OUT;
  4051. //
  4052. // Set the number of bytes to send this iteration.
  4053. //
  4054. ulNumBytes = g_sUSBHEP0State[ulIndex].ulBytesRemaining;
  4055. //
  4056. // Limit individual transfers to 64 bytes.
  4057. //
  4058. if(ulNumBytes > 64)
  4059. {
  4060. ulNumBytes = 64;
  4061. }
  4062. //
  4063. // Save the pointer so that it can be passed to the USBEndpointDataPut()
  4064. // function.
  4065. //
  4066. pData = (unsigned char *)g_sUSBHEP0State[ulIndex].pData;
  4067. //
  4068. // Advance the data pointer and counter to the next data to be sent.
  4069. //
  4070. g_sUSBHEP0State[ulIndex].ulBytesRemaining -= ulNumBytes;
  4071. g_sUSBHEP0State[ulIndex].pData += ulNumBytes;
  4072. //
  4073. // Put the data in the correct FIFO.
  4074. //
  4075. USBEndpointDataPut(g_USBInstance[ulIndex].uiBaseAddr , USB_EP_0,
  4076. pData, ulNumBytes);
  4077. //
  4078. // If this is exactly 64 then don't set the last packet yet.
  4079. //
  4080. if(ulNumBytes == 64)
  4081. {
  4082. //
  4083. // There is more data to send or exactly 64 bytes were sent, this
  4084. // means that there is either more data coming or a null packet needs
  4085. // to be sent to complete the transaction.
  4086. //
  4087. USBEndpointDataSend(g_USBInstance[ulIndex].uiBaseAddr , USB_EP_0,
  4088. USB_TRANS_OUT);
  4089. }
  4090. else
  4091. {
  4092. //
  4093. // Send the last bit of data.
  4094. //
  4095. USBEndpointDataSend(g_USBInstance[ulIndex].uiBaseAddr , USB_EP_0,
  4096. USB_TRANS_OUT);
  4097. //
  4098. // Now go to the status state and wait for the transmit to complete.
  4099. //
  4100. g_sUSBHEP0State[ulIndex].eState = EP0_STATE_STATUS_IN;
  4101. }
  4102. }
  4103. //*****************************************************************************
  4104. //
  4105. // This internal function handles Aborts Tx by flusing the EP FIFOs and DMA(when enabled).
  4106. //
  4107. // \return None.
  4108. //
  4109. //*****************************************************************************
  4110. static unsigned int
  4111. USBHCDTxAbort(unsigned int ulIndex, unsigned int endPoint)
  4112. {
  4113. USBFIFOFlush(g_USBInstance[ulIndex].uiBaseAddr,
  4114. endPoint, USB_EP_HOST_OUT);
  4115. USBFIFOFlush(g_USBInstance[ulIndex].uiBaseAddr,
  4116. endPoint, USB_EP_HOST_OUT);
  4117. return 1;
  4118. }
  4119. //*****************************************************************************
  4120. //
  4121. // This internal function handles Aborts Rx by flusing the EP FIFOs and DMA(when enabled).
  4122. //
  4123. // \return None.
  4124. //
  4125. //*****************************************************************************
  4126. static unsigned int
  4127. USBHCDRxAbort(unsigned int ulIndex, unsigned int endPoint)
  4128. {
  4129. USBHostAutoReqClear(g_USBInstance[ulIndex].uiBaseAddr,
  4130. endPoint);
  4131. USBHostRequestINClear(g_USBInstance[ulIndex].uiBaseAddr,
  4132. endPoint);
  4133. USBFIFOFlush(g_USBInstance[ulIndex].uiBaseAddr,
  4134. endPoint, USB_EP_HOST_IN);
  4135. USBFIFOFlush(g_USBInstance[ulIndex].uiBaseAddr,
  4136. endPoint, USB_EP_HOST_IN);
  4137. USBHostAutoReqSet(g_USBInstance[ulIndex].uiBaseAddr,
  4138. endPoint);
  4139. return 1;
  4140. }
  4141. //*****************************************************************************
  4142. //
  4143. // This internal function trys to re-establish connenction with the device.
  4144. //
  4145. // \return None.
  4146. //
  4147. //*****************************************************************************
  4148. static unsigned int
  4149. USBHCDRetryConnect(unsigned int ulIndex)
  4150. {
  4151. //
  4152. // Trigger a Reset.
  4153. //
  4154. USBHCDReset(ulIndex);
  4155. if(USBHCDGetDeviceDescriptor(ulIndex, &g_sUSBHCD[ulIndex].USBDevice[0]) == 0)
  4156. {
  4157. return 0;
  4158. }
  4159. return 1;
  4160. }
  4161. //*****************************************************************************
  4162. //
  4163. // This function sets the timeout value applicable communication types.
  4164. // This API should be called after 'USBHCDInit' call.
  4165. //
  4166. // \return None.
  4167. //
  4168. //*****************************************************************************
  4169. void
  4170. USBHCDTimeOutHook(unsigned int ulIndex, tUSBHTimeOut **USBHTimeOut)
  4171. {
  4172. (*USBHTimeOut) = &(g_sUSBHCD[ulIndex].USBHTimeOut);
  4173. }
  4174. //*****************************************************************************
  4175. //
  4176. //! This function returns the speed of the device connected on the bus.
  4177. //!
  4178. //! \param ulIndex specifies which USB controller to use.
  4179. //!
  4180. //! This function calls the HCD lower layer function to return the speed
  4181. //! of the connected device. High speed devices get detected as high speed
  4182. //! only after the 2nd reset and chirp sequence. Till that time they report FS
  4183. //!
  4184. //! \return device speed as unsigned integer.
  4185. //
  4186. //*****************************************************************************
  4187. unsigned int
  4188. USBHCDGetSpeed(unsigned int ulIndex)
  4189. {
  4190. ASSERT(ulIndex == 0);
  4191. //
  4192. // Call the lower Abstraction layer speed get routine
  4193. //
  4194. return( USBHostSpeedGet( g_USBInstance[ulIndex].uiBaseAddr ) );
  4195. }
  4196. //*****************************************************************************
  4197. //
  4198. // Close the Doxygen group.
  4199. //! @}
  4200. //
  4201. //*****************************************************************************