123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929 |
- /*
- * Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
- * Copyright 2016,2019 - 2020 NXP
- * All rights reserved.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- */
- #include <usb/include/usb_host_config.h>
- #if ((defined USB_HOST_CONFIG_EHCI) && (USB_HOST_CONFIG_EHCI > 0U))
- #include "usb_host.h"
- #include "usb_host_hci.h"
- #include "usb_host_devices.h"
- #if ((defined USB_HOST_CONFIG_COMPLIANCE_TEST) && (USB_HOST_CONFIG_COMPLIANCE_TEST))
- #include "usb_host_framework.h"
- #endif
- #include "fsl_device_registers.h"
- #include "usb_host_ehci.h"
- #include <usb/phy/usb_phy.h>
- #if ((defined USB_HOST_CONFIG_COMPLIANCE_TEST) && (USB_HOST_CONFIG_COMPLIANCE_TEST))
- #include "usb_host.h"
- #endif
- /*******************************************************************************
- * Definitions
- ******************************************************************************/
- #if defined(USB_STACK_USE_DEDICATED_RAM) && (USB_STACK_USE_DEDICATED_RAM > 0U)
- #error The SOC does not suppoort dedicated RAM case.
- #endif
- #define USB_HOST_EHCI_BANDWIDTH_DELAY (3500U)
- #define USB_HOST_EHCI_BANDWIDTH_HUB_LS_SETUP (333U)
- #define USB_HOST_EHCI_BANDWIDTH_FRAME_TOTOAL_TIME (900U)
- #if ((defined USB_HOST_CONFIG_COMPLIANCE_TEST) && (USB_HOST_CONFIG_COMPLIANCE_TEST))
- #define USB_HOST_EHCI_TEST_DESCRIPTOR_LENGTH (18U)
- #define USB_HOST_EHCI_PORTSC_PTC_J_STATE (0x01U)
- #define USB_HOST_EHCI_PORTSC_PTC_K_STATE (0x02U)
- #define USB_HOST_EHCI_PORTSC_PTC_SE0_NAK (0x03U)
- #define USB_HOST_EHCI_PORTSC_PTC_PACKET (0x04U)
- #define USB_HOST_EHCI_PORTSC_PTC_FORCE_ENABLE_HS (0x05U)
- #define USB_HOST_EHCI_PORTSC_PTC_FORCE_ENABLE_FS (0x06U)
- #define USB_HOST_EHCI_PORTSC_PTC_FORCE_ENABLE_LS (0x07U)
- #endif
- /*******************************************************************************
- * Prototypes
- ******************************************************************************/
- /*!
- * @brief compute data bandwidth time.
- *
- * @param speed data speed.
- * @param pipeType data type.
- * @param direction data direction.
- * @param dataLength data length.
- *
- *@return time value.
- */
- static uint32_t USB_HostBandwidthComputeTime(uint8_t speed, uint8_t pipeType, uint8_t direction, uint32_t dataLength);
- /*!
- * @brief compute current allocated bandwidth when ehci work as full-speed or low-speed host.
- *
- * @param ehciInstance ehci instance pointer.
- * @param frameIndex frame index.
- * @param frameBandwidths return frame bandwidth data.
- */
- static void USB_HostBandwidthFslsHostComputeCurrent(usb_host_ehci_instance_t *ehciInstance,
- uint16_t frameIndex,
- uint16_t *frameBandwidth);
- /*!
- * @brief compute current hub's allocated FS/LS bandwidth when ehci work as hi-speed host.
- *
- * @param ehciInstance ehci instance pointer.
- * @param hubNumber hub address.
- * @param frameIndex frame index.
- * @param frameBandwidths return frame bandwidth data.
- */
- static void USB_HostBandwidthHsHostComputeCurrentFsls(usb_host_ehci_instance_t *ehciInstance,
- uint32_t hubNumber,
- uint16_t frameIndex,
- uint16_t frameBandwidths[8]);
- /*!
- * @brief compute current allocated HS bandwidth when ehci work as hi-speed host.
- *
- * @param ehciInstance ehci instance pointer.
- * @param frameIndex frame index.
- * @param frameBandwidths return frame bandwidth data.
- */
- static void USB_HostBandwidthHsHostComputeCurrentHsAll(usb_host_ehci_instance_t *ehciInstance,
- uint16_t frameIndex,
- uint16_t frameBandwidths[8]);
- /*!
- * @brief allocate HS bandwidth when host work as high-speed host.
- *
- * @param ehciInstance ehci instance pointer.
- * @param uframeInterval micro-frame interval.
- * @param timeData time for allocating.
- * @param uframe_index_out return start uframe index.
- *
- * @return kStatus_USB_Success or error codes.
- */
- static usb_status_t USB_HostBandwidthHsHostAllocateHsCommon(usb_host_ehci_instance_t *ehciInstance,
- uint16_t uframeInterval,
- uint16_t timeData,
- uint16_t *uframeIndexOut);
- /*!
- * @brief allocate HS interrupt bandwidth when host work as high-speed host.
- *
- * @param ehciInstance ehci instance pointer.
- * @param ehciPipePointer ehci pipe pointer.
- *
- * @return kStatus_USB_Success or error codes.
- */
- static usb_status_t USB_HostBandwidthHsHostAllocateInterrupt(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_pipe_t *ehciPipePointer);
- /*!
- * @brief allocate bandwidth when host work as full-speed or low-speed host.
- *
- * @param ehciInstance ehci instance pointer.
- * @param ehciPipePointer ehci pipe pointer.
- *
- * @return kStatus_USB_Success or error codes.
- */
- static usb_status_t USB_HostBandwidthFslsHostAllocate(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_pipe_t *ehciPipePointer);
- /*!
- * @brief get the 2 power value of uint8_t.
- *
- * @param value input uint8_t value.
- */
- static uint8_t USB_HostEhciGet2PowerValue(uint8_t value);
- /*!
- * @brief memory zero.
- *
- * @param buffer buffer pointer.
- * @param length buffer length.
- */
- static void USB_HostEhciZeroMem(uint32_t *buffer, uint32_t length);
- /*!
- * @brief host ehci delay.
- *
- * @param ehciIpBase ehci ip base address.
- * @param ms millisecond.
- */
- static void USB_HostEhciDelay(USBHS_Type *ehciIpBase, uint32_t ms);
- /*!
- * @brief host ehci stop async schedule.
- *
- * @param ehciInstance ehci instance pointer.
- */
- static void USB_HostEhciStopAsync(usb_host_ehci_instance_t *ehciInstance);
- /*!
- * @brief host ehci stop periodic schedule.
- *
- * @param ehciInstance ehci instance pointer.
- */
- static void USB_HostEhciStopPeriodic(usb_host_ehci_instance_t *ehciInstance);
- /*!
- * @brief initialize the qtd for one transfer.
- *
- * @param ehciInstance ehci instance pointer.
- * @param ehciPipePointer ehci pipe pointer.
- * @param transfer transfer information.
- *
- *@return kStatus_USB_Success or error codes.
- */
- static usb_status_t USB_HostEhciQhQtdListInit(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_pipe_t *ehciPipePointer,
- usb_host_transfer_t *transfer);
- /*!
- * @brief release the qtd list.
- *
- * @param ehciInstance ehci instance pointer.
- * @param ehciQtdStart qtd list start pointer.
- * @param ehciQtdEnd qtd list end pointer.
- *
- *@return the transfer's length.
- */
- static uint32_t USB_HostEhciQtdListRelease(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_qtd_t *ehciQtdStart,
- usb_host_ehci_qtd_t *ehciQtdEnd);
- /*!
- * @brief de-initialize qh's linking qtd list.
- * 1. remove qtd from qh; 2. remove transfer from qh; 3. release qtd; 4. transfer callback.
- *
- * @param ehciInstance ehci instance pointer.
- * @param ehciPipePointer ehci pipe.
- *
- *@return kStatus_USB_Success or error codes.
- */
- static usb_status_t USB_HostEhciQhQtdListDeinit(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_pipe_t *ehciPipePointer);
- /*!
- * @brief de-initialize transfer's linking qtd list.
- * 1. stop this qh schedule; 2. remove qtd from qh; 3. remove transfer from qh; 4. release qtd; 5. transfer callback; 6.
- *start this qh schedule.
- *
- * @param ehciInstance ehci instance pointer.
- * @param ehciPipePointer ehci pipe pointer.
- * @param transfer transfer information.
- *
- *@return kStatus_USB_Success or error codes.
- */
- static usb_status_t USB_HostEhciTransferQtdListDeinit(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_pipe_t *ehciPipePointer,
- usb_host_transfer_t *transfer);
- /*!
- * @brief initialize QH when opening one control, bulk or interrupt pipe.
- *
- * @param ehciInstance ehci instance pointer.
- * @param ehciPipePointer ehci pipe pointer.
- *
- * @return kStatus_USB_Success or error codes.
- */
- static usb_status_t USB_HostEhciQhInit(usb_host_ehci_instance_t *ehciInstance, usb_host_ehci_pipe_t *ehciPipePointer);
- /*!
- * @brief de-initialize QH when closing one control, bulk or interrupt pipe.
- *
- * @param ehciInstance ehci instance pointer.
- * @param ehciPipePointer ehci pipe pointer.
- *
- * @return kStatus_USB_Success or error codes.
- */
- static usb_status_t USB_HostEhciQhDeinit(usb_host_ehci_instance_t *ehciInstance, usb_host_ehci_pipe_t *ehciPipePointer);
- /*!
- * @brief add qh to one frame entry.
- *
- * @param ehciInstance ehci instance pointer.
- * @param entryPointerValue entry pointer value.
- * @param framePos frame index.
- * @param uframeInterval micro-frame interval.
- */
- static void USB_HostEhciAddQhToFrame(usb_host_ehci_instance_t *ehciInstance,
- uint32_t entryPointerValue,
- uint16_t framePos,
- uint16_t uframeInterval);
- /*!
- * @brief remove entry from frame list.
- *
- * @param ehciInstance ehci instance pointer.
- * @param entryPointerValue entry pointer value.
- * @param framePos frame index.
- */
- static void USB_HostEhciRemoveFromFrame(usb_host_ehci_instance_t *ehciInstance,
- uint32_t entryPointerValue,
- uint16_t framePos);
- #if ((defined USB_HOST_CONFIG_EHCI_MAX_SITD) && (USB_HOST_CONFIG_EHCI_MAX_SITD))
- /*!
- * @brief add sitd array to the frame list.
- *
- * @param ehciInstance ehci instance pointer.
- * @param entryPointerValue entry pointer value.
- * @param startEntryPointer sitd entry pointer.
- */
- static void USB_HostEhciLinkSitd(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_pipe_t *ehciPipePointer,
- void *startEntryPointer);
- /*!
- * @brief initialize sitd array for one transfer.
- *
- * @param ehciInstance ehci instance pointer.
- * @param ehciPipePointer ehci pipe pointer.
- * @param transfer transfer information.
- */
- static usb_status_t USB_HostEhciSitdArrayInit(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_pipe_t *ehciPipePointer,
- usb_host_transfer_t *transfer);
- /*!
- * @brief release sitd list.
- *
- * @param ehciInstance ehci instance pointer.
- * @param startSitdPointer start sitd pointer.
- * @param endSitdPointer end sitd pointer.
- *
- * @return transfer's result length.
- */
- static uint32_t USB_HostEhciSitdArrayRelease(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_sitd_t *startSitdPointer,
- usb_host_ehci_sitd_t *endSitdPointer);
- /*!
- * @brief de-initialize sitd list.
- * 1. remove transfer; 2. remove sitd from frame list and release sitd; 3. transfer callback
- *
- * @param ehciInstance ehci instance pointer.
- * @param ehciPipePointer ehci pipe pointer.
- *
- * @return kStatus_USB_Success or error codes.
- */
- static usb_status_t USB_HostEhciSitdArrayDeinit(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_pipe_t *ehciPipePointer);
- #endif /* USB_HOST_CONFIG_EHCI_MAX_SITD */
- #if ((defined USB_HOST_CONFIG_EHCI_MAX_ITD) && (USB_HOST_CONFIG_EHCI_MAX_ITD))
- /*!
- * @brief compute the frame index when inserting itd.
- *
- * @param ehciInstance ehci instance pointer.
- * @param lastLinkUframe last inserted micro-frame.
- * @param startUframe start micro-frame.
- * @param uframeInterval micro-frame interval.
- *
- * @return frame index
- */
- static uint32_t USB_HostEhciGetItdLinkFrame(usb_host_ehci_instance_t *ehciInstance,
- uint32_t lastLinkUframe,
- uint16_t startUframe,
- uint16_t uframeInterval);
- /*!
- * @brief initialize itd list for one transfer.
- * 1. initialize itd list; 2. insert itd to frame list.
- *
- * @param ehciInstance ehci instance pointer.
- * @param ehciPipePointer ehci pipe pointer.
- * @param transfer transfer information.
- *
- * @return kStatus_USB_Success or error codes.
- */
- static usb_status_t USB_HostEhciItdArrayInit(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_pipe_t *ehciPipePointer,
- usb_host_transfer_t *transfer);
- /*!
- * @brief release itd list.
- *
- * @param ehciInstance ehci instance pointer.
- * @param startItdPointer start itd pointer.
- * @param endItdPointer end itd pointer.
- *
- * @return transfer's result length.
- */
- static uint32_t USB_HostEhciItdArrayRelease(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_itd_t *startItdPointer,
- usb_host_ehci_itd_t *endItdPointer);
- /*!
- * @brief de-initialize itd list.
- * 1. remove transfer; 2. remove itd from frame list and release itd; 3. transfer callback
- *
- * @param ehciInstance ehci instance pointer.
- * @param ehciPipePointer ehci pipe pointer.
- *
- * @return kStatus_USB_Success or error codes.
- */
- static usb_status_t USB_HostEhciItdArrayDeinit(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_pipe_t *ehciPipePointer);
- #endif /* USB_HOST_CONFIG_EHCI_MAX_ITD */
- /*!
- * @brief open control or bulk pipe.
- *
- * @param ehciInstance ehci instance pointer.
- * @param ehciPipePointer ehci pipe pointer.
- *
- * @return kStatus_USB_Success or error codes.
- */
- static usb_status_t USB_HostEhciOpenControlBulk(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_pipe_t *ehciPipePointer);
- /*!
- * @brief close control or bulk pipe.
- *
- * @param ehciInstance ehci instance pointer.
- * @param ehciPipePointer ehci pipe pointer.
- *
- * @return kStatus_USB_Success or error codes.
- */
- static usb_status_t USB_HostEhciCloseControlBulk(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_pipe_t *ehciPipePointer);
- /*!
- * @brief open interrupt pipe.
- *
- * @param ehciInstance ehci instance pointer.
- * @param ehciPipePointer ehci pipe pointer.
- *
- * @return kStatus_USB_Success or error codes.
- */
- static usb_status_t USB_HostEhciOpenInterrupt(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_pipe_t *ehciPipePointer);
- /*!
- * @brief close interrupt pipe.
- *
- * @param ehciInstance ehci instance pointer.
- * @param ehciPipePointer ehci pipe pointer.
- *
- * @return kStatus_USB_Success or error codes.
- */
- static usb_status_t USB_HostEhciCloseInterrupt(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_pipe_t *ehciPipePointer);
- #if (((defined USB_HOST_CONFIG_EHCI_MAX_ITD) && (USB_HOST_CONFIG_EHCI_MAX_ITD)) || \
- ((defined USB_HOST_CONFIG_EHCI_MAX_SITD) && (USB_HOST_CONFIG_EHCI_MAX_SITD)))
- /*!
- * @brief open iso pipe.
- *
- * @param ehciInstance ehci instance pointer.
- * @param ehciPipePointer ehci pipe pointer.
- *
- * @return kStatus_USB_Success or error codes.
- */
- static usb_status_t USB_HostEhciOpenIso(usb_host_ehci_instance_t *ehciInstance, usb_host_ehci_pipe_t *ehciPipePointer);
- /*!
- * @brief close iso pipe.
- *
- * @param ehciInstance ehci instance pointer.
- * @param ehciPipePointer ehci pipe pointer.
- *
- * @return kStatus_USB_Success or error codes.
- */
- static usb_status_t USB_HostEhciCloseIso(usb_host_ehci_instance_t *ehciInstance, usb_host_ehci_pipe_t *ehciPipePointer);
- /*!
- * @brief allocate HS iso bandwidth when host work as high-speed host.
- *
- * @param ehciInstance ehci instance pointer.
- * @param ehciPipePointer ehci pipe pointer.
- *
- * @return kStatus_USB_Success or error codes.
- */
- static usb_status_t USB_HostBandwidthHsHostAllocateIso(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_pipe_t *ehciPipePointer);
- #endif
- /*!
- * @brief reset ehci ip.
- *
- * @param ehciInstance ehci instance pointer.
- *
- * @return kStatus_USB_Success or error codes.
- */
- static usb_status_t USB_HostEhciResetIP(usb_host_ehci_instance_t *ehciInstance);
- /*!
- * @brief start ehci ip.
- *
- * @param ehciInstance ehci instance pointer.
- *
- * @return kStatus_USB_Success or error codes.
- */
- static usb_status_t USB_HostEhciStartIP(usb_host_ehci_instance_t *ehciInstance);
- /*!
- * @brief cancel pipe's transfers.
- *
- * @param ehciInstance ehci instance pointer.
- * @param ehciPipePointer ehci pipe pointer.
- * @param transfer the canceling transfer.
- *
- * @return kStatus_USB_Success or error codes.
- */
- static usb_status_t USB_HostEhciCancelPipe(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_pipe_t *ehciPipePointer,
- usb_host_transfer_t *transfer);
- /*!
- * @brief ehci transaction done process function.
- *
- * @param ehciInstance ehci instance pointer.
- */
- void USB_HostEhciTransactionDone(usb_host_ehci_instance_t *ehciInstance);
- #if ((defined(USB_HOST_CONFIG_LOW_POWER_MODE)) && (USB_HOST_CONFIG_LOW_POWER_MODE > 0U))
- /*!
- * @brief ehci timer1 interrupt process function.
- * cancel control/bulk transfer that time out.
- *
- * @param ehciInstance ehci instance pointer.
- */
- static void USB_HostEhciTimer1(usb_host_ehci_instance_t *ehciInstance);
- #endif
- #if ((defined USB_HOST_CONFIG_COMPLIANCE_TEST) && (USB_HOST_CONFIG_COMPLIANCE_TEST))
- /*!
- * @brief suspend bus.
- *
- * @param ehciInstance ehci instance pointer.
- */
- static void USB_HostEhciSuspendBus(usb_host_ehci_instance_t *ehciInstance);
- /*!
- * @brief resume bus.
- *
- * @param ehciInstance ehci instance pointer.
- */
- static void USB_HostEhciResumeBus(usb_host_ehci_instance_t *ehciInstance);
- extern usb_status_t USB_HostStandardSetGetDescriptor(usb_host_device_instance_t *deviceInstance,
- usb_host_transfer_t *transfer,
- void *param);
- #endif /* USB_HOST_CONFIG_COMPLIANCE_TEST */
- /*******************************************************************************
- * Variables
- ******************************************************************************/
- /* EHCI controller driver instances. */
- #if (USB_HOST_CONFIG_EHCI == 1U)
- USB_RAM_ADDRESS_ALIGNMENT(4096)
- USB_CONTROLLER_DATA static uint8_t s_UsbHostEhciFrameList1[USB_HOST_CONFIG_EHCI_FRAME_LIST_SIZE * 4];
- static uint8_t usbHostEhciFramListStatus[1] = {0};
- USB_RAM_ADDRESS_ALIGNMENT(64) USB_CONTROLLER_DATA static usb_host_ehci_data_t s_UsbHostEhciData1;
- #elif (USB_HOST_CONFIG_EHCI == 2U)
- USB_RAM_ADDRESS_ALIGNMENT(4096)
- USB_CONTROLLER_DATA static uint8_t s_UsbHostEhciFrameList1[USB_HOST_CONFIG_EHCI_FRAME_LIST_SIZE * 4];
- USB_RAM_ADDRESS_ALIGNMENT(4096)
- USB_CONTROLLER_DATA static uint8_t s_UsbHostEhciFrameList2[USB_HOST_CONFIG_EHCI_FRAME_LIST_SIZE * 4];
- static uint8_t usbHostEhciFramListStatus[2] = {0, 0};
- USB_RAM_ADDRESS_ALIGNMENT(64) USB_CONTROLLER_DATA static usb_host_ehci_data_t s_UsbHostEhciData1;
- USB_RAM_ADDRESS_ALIGNMENT(64) USB_CONTROLLER_DATA static usb_host_ehci_data_t s_UsbHostEhciData2;
- #else
- #error "Please increase the instance count."
- #endif
- #define USB_HOST_CONFIG_EHCI_FRAME_LIST_SIZE (1024U)
- #define USB_HOST_EHCI_MAX_MICRFRAME_VALUE ((USB_HOST_CONFIG_EHCI_FRAME_LIST_SIZE << 3U) - 1U)
- static uint8_t s_SlotMaxBandwidth[8] = {125, 125, 125, 125, 125, 125, 50, 0};
- static uint8_t s_SlotMaxBandwidthHs[8] = {100, 100, 100, 100, 100, 100, 100, 100};
- /*******************************************************************************
- * Code
- ******************************************************************************/
- /*!
- * @brief EHCI NC get USB NC bass address.
- *
- * This function is used to get USB NC bass address.
- *
- * @param[in] controllerId EHCI controller ID; See the #usb_controller_index_t.
- *
- * @retval USB NC bass address.
- */
- #if (defined(USB_HOST_CONFIG_LOW_POWER_MODE) && (USB_HOST_CONFIG_LOW_POWER_MODE > 0U))
- #if (defined(FSL_FEATURE_SOC_USBNC_COUNT) && (FSL_FEATURE_SOC_USBNC_COUNT > 0U))
- static void *USB_EhciNCGetBase(uint8_t controllerId)
- {
- void *usbNCBase = NULL;
- #if ((defined FSL_FEATURE_SOC_USBNC_COUNT) && (FSL_FEATURE_SOC_USBNC_COUNT > 0U))
- uint32_t instance;
- uint32_t newinstance = 0;
- uint32_t usbnc_base_temp[] = USBNC_BASE_ADDRS;
- uint32_t usbnc_base[] = USBNC_BASE_ADDRS;
- if (controllerId < (uint8_t)kUSB_ControllerEhci0)
- {
- return NULL;
- }
- controllerId = controllerId - (uint8_t)kUSB_ControllerEhci0;
- for (instance = 0; instance < (sizeof(usbnc_base_temp) / sizeof(usbnc_base_temp[0])); instance++)
- {
- if (usbnc_base_temp[instance] != 0U)
- {
- usbnc_base[newinstance++] = usbnc_base_temp[instance];
- }
- }
- if (controllerId > newinstance)
- {
- return NULL;
- }
- usbNCBase = (void *)(uint8_t *)usbnc_base[controllerId];
- #endif
- return usbNCBase;
- }
- #endif
- #endif
- #if ((defined USB_HOST_CONFIG_COMPLIANCE_TEST) && (USB_HOST_CONFIG_COMPLIANCE_TEST))
- usb_status_t USB_HostEhciTestSetMode(usb_host_ehci_instance_t *ehciInstance, uint32_t testMode)
- {
- uint32_t ehciPortSC;
- ehciPortSC = ehciInstance->ehciIpBase->PORTSC1;
- ehciPortSC &= ~((uint32_t)USBHS_PORTSC1_PTC_MASK); /* clear test mode bits */
- ehciPortSC |= (testMode << USBHS_PORTSC1_PTC_SHIFT); /* set test mode bits */
- ehciInstance->ehciIpBase->PORTSC1 = ehciPortSC;
- return kStatus_USB_Success;
- }
- static void USB_HostEhciTestSuspendResume(usb_host_ehci_instance_t *ehciInstance)
- {
- uint8_t timeCount;
- timeCount = 15; /* 15s */
- while (timeCount--)
- {
- USB_HostEhciDelay(ehciInstance->ehciIpBase, 1000U);
- }
- USB_HostEhciSuspendBus(ehciInstance);
- timeCount = 15; /* 15s */
- while (timeCount--)
- {
- USB_HostEhciDelay(ehciInstance->ehciIpBase, 1000U);
- }
- USB_HostEhciResumeBus(ehciInstance);
- }
- static void USB_HostEhciTestCallback(void *param, usb_host_transfer_t *transfer, usb_status_t status)
- {
- USB_HostFreeTransfer(param, transfer);
- }
- static void USB_HostEhciTestSingleStepGetDeviceDesc(usb_host_ehci_instance_t *ehciInstance,
- usb_device_handle deviceHandle)
- {
- usb_host_process_descriptor_param_t getDescriptorParam;
- usb_host_device_instance_t *deviceInstance = (usb_host_device_instance_t *)deviceHandle;
- usb_host_transfer_t *transfer;
- uint8_t timeCount;
- /* disable periodic shedule */
- USB_HostEhciStopPeriodic(ehciInstance);
- timeCount = 15; /* 15s */
- while (timeCount--)
- {
- USB_HostEhciDelay(ehciInstance->ehciIpBase, 1000U);
- }
- /* malloc one transfer */
- if (USB_HostMallocTransfer(ehciInstance->hostHandle, &transfer) != kStatus_USB_Success)
- {
- #ifdef HOST_ECHO
- usb_echo("allocate transfer error\r\n");
- #endif
- return;
- }
- getDescriptorParam.descriptorLength = sizeof(usb_descriptor_device_t);
- getDescriptorParam.descriptorLength = 18;
- getDescriptorParam.descriptorBuffer = (uint8_t *)&deviceInstance->deviceDescriptor;
- getDescriptorParam.descriptorType = USB_DESCRIPTOR_TYPE_DEVICE;
- getDescriptorParam.descriptorIndex = 0;
- getDescriptorParam.languageId = 0;
- transfer->callbackFn = USB_HostEhciTestCallback;
- transfer->callbackParam = ehciInstance->hostHandle;
- transfer->setupPacket->bmRequestType = USB_REQUEST_TYPE_DIR_IN;
- transfer->setupPacket->bRequest = USB_REQUEST_STANDARD_GET_DESCRIPTOR;
- transfer->setupPacket->wIndex = 0;
- transfer->setupPacket->wLength = 0;
- transfer->setupPacket->wValue = 0;
- USB_HostStandardSetGetDescriptor(deviceInstance, transfer, &getDescriptorParam);
- }
- static usb_status_t USB_HostEhciSingleStepQtdListInit(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_pipe_t *ehciPipePointer,
- usb_host_transfer_t *transfer,
- uint8_t setupPhase)
- {
- volatile usb_host_ehci_qh_t *vltQhPointer;
- usb_host_ehci_qtd_t *qtdPointer = NULL;
- volatile uint32_t *entryPointer;
- uint32_t qtdNumber;
- uint32_t dataLength;
- uint32_t dataAddress;
- uint8_t index;
- /* compute the qtd number */
- qtdNumber = 1;
- vltQhPointer = (volatile usb_host_ehci_qh_t *)ehciPipePointer->ehciQh;
- /* get qtd list */
- USB_HostEhciLock();
- if (qtdNumber <= ehciInstance->ehciQtdNumber)
- {
- ehciInstance->ehciQtdNumber -= qtdNumber;
- qtdPointer = NULL;
- do
- {
- if (qtdPointer != NULL)
- {
- qtdPointer->nextQtdPointer = (uint32_t)ehciInstance->ehciQtdHead;
- }
- qtdPointer = ehciInstance->ehciQtdHead;
- ehciInstance->ehciQtdHead = (usb_host_ehci_qtd_t *)qtdPointer->nextQtdPointer;
- qtdPointer->nextQtdPointer = 0;
- } while (--qtdNumber);
- }
- else
- {
- USB_HostEhciUnlock();
- return kStatus_USB_Error;
- }
- USB_HostEhciUnlock();
- /* int qTD */
- if (setupPhase == 1) /* setup transaction qtd init */
- {
- qtdPointer->alternateNextQtdPointer = EHCI_HOST_T_INVALID_VALUE;
- /* dt: need set; ioc: 0; C_Page: 0; PID Code: SETUP; Status: Active */
- qtdPointer->transferResults[0] = qtdPointer->transferResults[1] = 0;
- qtdPointer->transferResults[0] =
- ((0x00000000 << EHCI_HOST_QTD_DT_SHIFT) | (8 << EHCI_HOST_QTD_TOTAL_BYTES_SHIFT) |
- (EHCI_HOST_PID_SETUP << EHCI_HOST_QTD_PID_CODE_SHIFT) | (EHCI_HOST_QTD_STATUS_ACTIVE_MASK));
- dataAddress = (uint32_t)(transfer->setupPacket);
- qtdPointer->transferResults[1] = dataAddress; /* current offset is set too */
- /* set buffer pointer no matter data length */
- for (index = 0; index < 4; ++index)
- {
- qtdPointer->bufferPointers[index] = ((dataAddress + (index + 1) * 4 * 1024) & 0xFFFFF000);
- }
- }
- else if (setupPhase == 2) /* data transaction qtd */
- {
- dataLength = transfer->transferLength;
- if (dataLength != 0)
- {
- qtdPointer->alternateNextQtdPointer = EHCI_HOST_T_INVALID_VALUE;
- /* dt: need set; ioc: 0; C_Page: 0; PID Code: IN/OUT; Status: Active */
- qtdPointer->transferResults[0] = qtdPointer->transferResults[1] = 0;
- qtdPointer->transferResults[0] =
- ((0x00000001U << EHCI_HOST_QTD_DT_SHIFT) | (dataLength << EHCI_HOST_QTD_TOTAL_BYTES_SHIFT) |
- (EHCI_HOST_PID_IN << EHCI_HOST_QTD_PID_CODE_SHIFT) | (EHCI_HOST_QTD_STATUS_ACTIVE_MASK));
- dataAddress = (uint32_t)(transfer->transferBuffer);
- qtdPointer->transferResults[1] = dataAddress; /* current offset is set too */
- /* set buffer pointer no matter data length */
- for (index = 0; index < 4; ++index)
- {
- qtdPointer->bufferPointers[index] = ((dataAddress + (index + 1) * 4 * 1024) & 0xFFFFF000);
- }
- }
- }
- else if (setupPhase == 3)
- {
- /* status transaction qtd */
- qtdPointer->alternateNextQtdPointer = EHCI_HOST_T_INVALID_VALUE;
- /* dt: dont care; ioc: 1; C_Page: 0; PID Code: IN/OUT; Status: Active */
- qtdPointer->transferResults[0] = qtdPointer->transferResults[1] = 0;
- qtdPointer->transferResults[0] =
- ((0x00000001U << EHCI_HOST_QTD_DT_SHIFT) | (EHCI_HOST_PID_OUT << EHCI_HOST_QTD_PID_CODE_SHIFT) |
- (EHCI_HOST_QTD_IOC_MASK) | (EHCI_HOST_QTD_STATUS_ACTIVE_MASK));
- qtdPointer->nextQtdPointer |= EHCI_HOST_T_INVALID_VALUE;
- }
- qtdPointer->nextQtdPointer |= EHCI_HOST_T_INVALID_VALUE;
- qtdPointer->transferResults[0] |= EHCI_HOST_QTD_IOC_MASK; /* set IOC */
- /* save qtd to transfer */
- transfer->union1.unitHead = (uint32_t)qtdPointer;
- transfer->union2.unitTail = (uint32_t)qtdPointer;
- /* link transfer to qh */
- transfer->next = NULL;
- if (vltQhPointer->ehciTransferHead == NULL)
- {
- transfer->next = NULL;
- vltQhPointer->ehciTransferHead = vltQhPointer->ehciTransferTail = transfer;
- }
- else
- {
- transfer->next = NULL;
- vltQhPointer->ehciTransferTail->next = transfer;
- vltQhPointer->ehciTransferTail = transfer;
- }
- USB_HostEhciLock();
- /* link qtd to qh (link to end) */
- entryPointer = &(vltQhPointer->nextQtdPointer);
- dataAddress = *entryPointer; /* dataAddress variable means entry value here */
- while ((dataAddress) && (!(dataAddress & EHCI_HOST_T_INVALID_VALUE)))
- {
- entryPointer = (volatile uint32_t *)dataAddress;
- dataAddress = *entryPointer;
- }
- *entryPointer = (uint32_t)qtdPointer;
- USB_HostEhciUnlock();
- USB_HostEhciStartAsync(ehciInstance);
- return kStatus_USB_Success;
- }
- static void USB_HostEhciTestSingleStepGetDeviceDescData(usb_host_ehci_instance_t *ehciInstance,
- usb_device_handle deviceHandle)
- {
- static uint8_t buffer[USB_HOST_EHCI_TEST_DESCRIPTOR_LENGTH];
- usb_host_device_instance_t *deviceInstance = (usb_host_device_instance_t *)deviceHandle;
- usb_host_transfer_t *transfer;
- uint8_t timeCount;
- USB_HostEhciStopPeriodic(ehciInstance);
- if (USB_HostMallocTransfer(ehciInstance->hostHandle, &transfer) != kStatus_USB_Success)
- {
- return;
- }
- transfer->callbackFn = USB_HostEhciTestCallback;
- transfer->callbackParam = ehciInstance->hostHandle;
- transfer->setupPacket->bmRequestType = USB_REQUEST_TYPE_DIR_IN;
- transfer->setupPacket->bRequest = USB_REQUEST_STANDARD_GET_DESCRIPTOR;
- transfer->setupPacket->wLength = USB_SHORT_TO_LITTLE_ENDIAN(USB_HOST_EHCI_TEST_DESCRIPTOR_LENGTH);
- transfer->setupPacket->wValue = USB_SHORT_TO_LITTLE_ENDIAN((uint16_t)((uint16_t)USB_DESCRIPTOR_TYPE_DEVICE << 8));
- transfer->setupPacket->wIndex = 0;
- USB_HostEhciSingleStepQtdListInit(ehciInstance, (usb_host_ehci_pipe_t *)(deviceInstance->controlPipe), transfer, 1);
- timeCount = 15; /* 15s */
- while (timeCount--)
- {
- USB_HostEhciDelay(ehciInstance->ehciIpBase, 1000U);
- }
- if (USB_HostMallocTransfer(ehciInstance->hostHandle, &transfer) != kStatus_USB_Success)
- {
- return;
- }
- transfer->callbackFn = USB_HostEhciTestCallback;
- transfer->callbackParam = ehciInstance->hostHandle;
- transfer->transferBuffer = buffer;
- transfer->transferLength = USB_HOST_EHCI_TEST_DESCRIPTOR_LENGTH;
- USB_HostEhciSingleStepQtdListInit(ehciInstance, (usb_host_ehci_pipe_t *)(deviceInstance->controlPipe), transfer, 2);
- if (USB_HostMallocTransfer(ehciInstance->hostHandle, &transfer) != kStatus_USB_Success)
- {
- return;
- }
- transfer->callbackFn = USB_HostEhciTestCallback;
- transfer->callbackParam = ehciInstance->hostHandle;
- transfer->transferBuffer = NULL;
- transfer->transferLength = 0;
- USB_HostEhciSingleStepQtdListInit(ehciInstance, (usb_host_ehci_pipe_t *)(deviceInstance->controlPipe), transfer, 3);
- timeCount = 15; /* 15s */
- while (timeCount--)
- {
- USB_HostEhciDelay(ehciInstance->ehciIpBase, 1000U);
- }
- usb_echo("test_single_step_get_dev_desc_data finished\r\n");
- return;
- }
- static void USB_HostEhciTestModeInit(usb_device_handle deviceHandle)
- {
- uint32_t productId;
- usb_host_device_instance_t *deviceInstance = (usb_host_device_instance_t *)deviceHandle;
- usb_host_ehci_instance_t *ehciInstance =
- (usb_host_ehci_instance_t *)(((usb_host_instance_t *)(deviceInstance->hostHandle))->controllerHandle);
- USB_HostHelperGetPeripheralInformation(deviceHandle, kUSB_HostGetDevicePID, &productId);
- usb_echo("usb host ehci test mode init product id:0x%x\r\n", productId);
- switch (productId)
- {
- case 0x0101U:
- USB_HostEhciTestSetMode(ehciInstance, USB_HOST_EHCI_PORTSC_PTC_SE0_NAK);
- break;
- case 0x0102U:
- USB_HostEhciTestSetMode(ehciInstance, USB_HOST_EHCI_PORTSC_PTC_J_STATE);
- break;
- case 0x0103U:
- USB_HostEhciTestSetMode(ehciInstance, USB_HOST_EHCI_PORTSC_PTC_K_STATE);
- break;
- case 0x0104U:
- USB_HostEhciTestSetMode(ehciInstance, USB_HOST_EHCI_PORTSC_PTC_PACKET);
- break;
- case 0x0105U:
- usb_echo("set test mode FORCE_ENALBE_HS\r\n");
- USB_HostEhciTestSetMode(ehciInstance, USB_HOST_EHCI_PORTSC_PTC_FORCE_ENABLE_HS);
- break;
- case 0x0106U:
- USB_HostEhciTestSuspendResume(ehciInstance);
- break;
- case 0x0107U:
- usb_echo("start test SINGLE_STEP_GET_DEV_DESC\r\n");
- USB_HostEhciTestSingleStepGetDeviceDesc(ehciInstance, deviceHandle);
- break;
- case 0x0108U:
- usb_echo("start test SINGLE_STEP_GET_DEV_DESC_DATA\r\n");
- USB_HostEhciTestSingleStepGetDeviceDescData(ehciInstance, deviceHandle);
- break;
- default:
- /*no action */
- break;
- }
- return;
- }
- static void USB_HostEhciSuspendBus(usb_host_ehci_instance_t *ehciInstance)
- {
- uint32_t ehciPortSC;
- USB_HostEhciLock();
- ehciPortSC = ehciInstance->ehciIpBase->PORTSC1;
- if (ehciPortSC & USBHS_PORTSC1_PE_MASK)
- {
- ehciPortSC = ehciInstance->ehciIpBase->PORTSC1;
- ehciPortSC &= (uint32_t)(~EHCI_PORTSC1_W1_BITS);
- ehciInstance->ehciIpBase->PORTSC1 = (ehciPortSC | USBHS_PORTSC1_SUSP_MASK);
- }
- USB_HostEhciUnlock();
- }
- static void USB_HostEhciResumeBus(usb_host_ehci_instance_t *ehciInstance)
- {
- uint32_t ehciPortSC;
- USB_HostEhciLock();
- /* Resume port */
- ehciPortSC = ehciInstance->ehciIpBase->PORTSC1;
- if (ehciPortSC & USBHS_PORTSC1_PE_MASK)
- {
- ehciPortSC &= (uint32_t)(~EHCI_PORTSC1_W1_BITS);
- ehciInstance->ehciIpBase->PORTSC1 = (ehciPortSC | USBHS_PORTSC1_FPR_MASK);
- }
- USB_HostEhciUnlock();
- }
- #endif
- static uint32_t USB_HostBandwidthComputeTime(uint8_t speed, uint8_t pipeType, uint8_t direction, uint32_t dataLength)
- {
- uint32_t result = (3167U + ((1000U * dataLength) * 7U * 8U / 6U)) / 1000U;
- if (pipeType == USB_ENDPOINT_ISOCHRONOUS) /* iso */
- {
- if (speed == USB_SPEED_HIGH)
- {
- result = 38U * 8U * 2083U + 2083U * result + USB_HOST_EHCI_BANDWIDTH_DELAY;
- }
- else if (speed == USB_SPEED_FULL)
- {
- if (direction == USB_IN)
- {
- result = 7268000U + 83540U * result + USB_HOST_EHCI_BANDWIDTH_DELAY;
- }
- else
- {
- result = 6265000U + 83540U * result + USB_HOST_EHCI_BANDWIDTH_DELAY;
- }
- }
- else
- {
- /*no action*/
- }
- }
- else /* interrupt */
- {
- if (speed == USB_SPEED_HIGH)
- {
- result = 55U * 8U * 2083U + 2083U * result + USB_HOST_EHCI_BANDWIDTH_DELAY;
- }
- else if (speed == USB_SPEED_FULL)
- {
- result = 9107000U + 83540U * result + USB_HOST_EHCI_BANDWIDTH_DELAY;
- }
- else if (speed == USB_SPEED_LOW)
- {
- if (direction == USB_IN)
- {
- result = 64060000U + 2000U * USB_HOST_EHCI_BANDWIDTH_HUB_LS_SETUP + 676670U * result +
- USB_HOST_EHCI_BANDWIDTH_DELAY;
- }
- else
- {
- result = 6265000U + 83540U * result + USB_HOST_EHCI_BANDWIDTH_DELAY;
- }
- }
- else
- {
- /*no action*/
- }
- }
- result /= 1000000U;
- if (result == 0U)
- {
- result = 1U;
- }
- return result;
- }
- static void USB_HostBandwidthFslsHostComputeCurrent(usb_host_ehci_instance_t *ehciInstance,
- uint16_t frameIndex,
- uint16_t *frameBandwidth)
- {
- usb_host_ehci_pipe_t *ehciPipePointer;
- void *temp;
- /* clear the bandwidth */
- *frameBandwidth = 0;
- ehciPipePointer = ehciInstance->ehciRunningPipeList;
- while (ehciPipePointer != NULL)
- {
- /* only compute iso and interrupt pipe */
- if ((ehciPipePointer->pipeCommon.pipeType == USB_ENDPOINT_ISOCHRONOUS) ||
- (ehciPipePointer->pipeCommon.pipeType == USB_ENDPOINT_INTERRUPT))
- {
- /* does pipe allocate bandwidth in frameIndex frame? note: interval is power of 2. */
- if ((frameIndex >= ehciPipePointer->startFrame) &&
- (0U == ((uint32_t)((uint32_t)frameIndex - ehciPipePointer->startFrame) &
- ((uint32_t)ehciPipePointer->pipeCommon.interval - 1U))))
- {
- *frameBandwidth += ehciPipePointer->dataTime;
- }
- }
- temp = (void *)ehciPipePointer->pipeCommon.next;
- ehciPipePointer = (usb_host_ehci_pipe_t *)temp;
- }
- }
- static void USB_HostBandwidthHsHostComputeCurrentFsls(usb_host_ehci_instance_t *ehciInstance,
- uint32_t hubNumber,
- uint16_t frameIndex,
- uint16_t frameBandwidths[8])
- {
- usb_host_ehci_pipe_t *ehciPipePointer;
- uint8_t index;
- uint32_t deviceInfo;
- void *temp;
- for (index = 0; index < 8U; ++index)
- {
- frameBandwidths[index] = 0;
- }
- ehciPipePointer = ehciInstance->ehciRunningPipeList;
- while (ehciPipePointer != NULL)
- {
- /* only compute iso and interrupt pipe */
- if ((ehciPipePointer->pipeCommon.pipeType == USB_ENDPOINT_ISOCHRONOUS) ||
- (ehciPipePointer->pipeCommon.pipeType == USB_ENDPOINT_INTERRUPT))
- {
- /* compute FS/LS bandwidth that blong to same high-speed hub, because FS/LS bandwidth is allocated from
- * first parent high-speed hub */
- (void)USB_HostHelperGetPeripheralInformation(ehciPipePointer->pipeCommon.deviceHandle,
- (uint32_t)kUSB_HostGetDeviceHSHubNumber, &deviceInfo);
- if (deviceInfo != hubNumber)
- {
- temp = (void *)ehciPipePointer->pipeCommon.next;
- ehciPipePointer = (usb_host_ehci_pipe_t *)temp;
- continue;
- }
- (void)USB_HostHelperGetPeripheralInformation(ehciPipePointer->pipeCommon.deviceHandle,
- (uint32_t)kUSB_HostGetDeviceSpeed, &deviceInfo);
- if (deviceInfo == USB_SPEED_HIGH)
- {
- temp = (void *)ehciPipePointer->pipeCommon.next;
- ehciPipePointer = (usb_host_ehci_pipe_t *)temp;
- continue;
- }
- /* does pipe allocate bandwidth in frameIndex frame? note: interval is power of 2. */
- if ((frameIndex >= ehciPipePointer->startFrame) &&
- (0U == ((uint32_t)((uint32_t)frameIndex - ehciPipePointer->startFrame) &
- ((uint32_t)ehciPipePointer->pipeCommon.interval - 1U))))
- {
- if (ehciPipePointer->pipeCommon.pipeType ==
- USB_ENDPOINT_ISOCHRONOUS) /* iso bandwidth is allocated once */
- {
- frameBandwidths[ehciPipePointer->startUframe + 1U] += ehciPipePointer->dataTime;
- }
- else /* iso bandwidth is allocated three times */
- {
- frameBandwidths[ehciPipePointer->startUframe + 1U] += ehciPipePointer->dataTime;
- frameBandwidths[ehciPipePointer->startUframe + 2U] += ehciPipePointer->dataTime;
- frameBandwidths[ehciPipePointer->startUframe + 3U] += ehciPipePointer->dataTime;
- }
- }
- }
- temp = (void *)ehciPipePointer->pipeCommon.next;
- ehciPipePointer = (usb_host_ehci_pipe_t *)temp;
- }
- for (index = 0; index < 7U; ++index) /* */
- {
- if (frameBandwidths[index] > s_SlotMaxBandwidth[index])
- {
- frameBandwidths[index + 1U] += (frameBandwidths[index] - s_SlotMaxBandwidth[index]);
- frameBandwidths[index] = s_SlotMaxBandwidth[index];
- }
- }
- }
- static void USB_HostBandwidthHsHostComputeCurrentHsAll(usb_host_ehci_instance_t *ehciInstance,
- uint16_t frameIndex,
- uint16_t frameBandwidths[8])
- {
- usb_host_ehci_pipe_t *ehciPipePointer;
- uint16_t index;
- uint32_t deviceInfo;
- uint16_t frameInterval;
- void *temp;
- for (index = 0; index < 8U; ++index)
- {
- frameBandwidths[index] = 0;
- }
- ehciPipePointer = ehciInstance->ehciRunningPipeList;
- while (ehciPipePointer != NULL)
- {
- /* only compute iso and interrupt pipe */
- if ((ehciPipePointer->pipeCommon.pipeType == USB_ENDPOINT_ISOCHRONOUS) ||
- (ehciPipePointer->pipeCommon.pipeType == USB_ENDPOINT_INTERRUPT))
- {
- frameInterval = ehciPipePointer->pipeCommon.interval;
- (void)USB_HostHelperGetPeripheralInformation(ehciPipePointer->pipeCommon.deviceHandle,
- (uint32_t)kUSB_HostGetDeviceSpeed, &deviceInfo);
- if (deviceInfo == USB_SPEED_HIGH) /* high-speed data bandwidth */
- {
- /* frameInterval means micro-frame here */
- if (frameIndex >= ehciPipePointer->startFrame)
- {
- if ((frameInterval > 8U) &&
- (frameIndex * 8U - ehciPipePointer->startFrame * 8U >= ehciPipePointer->startUframe))
- {
- if (0U == ((((uint32_t)frameIndex) * 8U - ehciPipePointer->startFrame * 8U -
- ehciPipePointer->startUframe) &
- ((uint32_t)frameInterval - 1U)))
- {
- frameBandwidths[ehciPipePointer->startUframe] += ehciPipePointer->dataTime;
- }
- }
- else
- {
- for (index = ehciPipePointer->startUframe; index < 8U; index += frameInterval)
- {
- frameBandwidths[index] += ehciPipePointer->dataTime;
- }
- }
- }
- }
- else /* full-speed split bandwidth */
- {
- if ((frameIndex >= ehciPipePointer->startFrame) &&
- (0U == ((uint32_t)((uint32_t)frameIndex - ehciPipePointer->startFrame) &
- (uint32_t)((uint32_t)frameInterval - 1U))))
- {
- for (index = 0; index < 8U; ++index)
- {
- if (0U != ((uint32_t)(ehciPipePointer->uframeSmask) &
- (uint32_t)(0x01UL << index))) /* start-split micro-frames */
- {
- frameBandwidths[index] += ehciPipePointer->startSplitTime;
- }
- if (0U != ((uint32_t)(ehciPipePointer->uframeCmask) &
- (uint32_t)(0x01UL << index))) /* complete-split micro-frames */
- {
- frameBandwidths[index] += ehciPipePointer->completeSplitTime;
- }
- }
- }
- }
- }
- temp = (void *)ehciPipePointer->pipeCommon.next;
- ehciPipePointer = (usb_host_ehci_pipe_t *)temp;
- }
- #if 0
- for (index = 0; index < 7; ++index) /* */
- {
- if (frameBandwidths[index] > s_SlotMaxBandwidthHs[index])
- {
- frameBandwidths[index + 1] += (frameBandwidths[index] - s_SlotMaxBandwidthHs[index]);
- frameBandwidths[index] = s_SlotMaxBandwidthHs[index];
- }
- }
- #endif
- }
- /*!
- * @brief allocate HS bandwidth when host work as high-speed host.
- *
- * @param ehciInstance ehci instance pointer.
- * @param uframeInterval micro-frame interval.
- * @param timeData time for allocating.
- * @param uframeIndexOut return start uframe index.
- *
- * @return kStatus_USB_Success or error codes.
- */
- static usb_status_t USB_HostBandwidthHsHostAllocateHsCommon(usb_host_ehci_instance_t *ehciInstance,
- uint16_t uframeInterval,
- uint16_t timeData,
- uint16_t *uframeIndexOut)
- {
- uint16_t uframeIntervalIndex;
- uint16_t uframeIndex;
- uint16_t frameIndex;
- uint16_t frameTimes[8];
- frameIndex = 0;
- for (uint8_t i = 0; i < 8U; ++i)
- {
- frameTimes[i] = 0U;
- }
- USB_HostBandwidthHsHostComputeCurrentHsAll(
- ehciInstance, frameIndex, frameTimes); /* compute the allocated bandwidths in the frameIndex frame */
- for (uframeIntervalIndex = 0; (uframeIntervalIndex < uframeInterval); ++uframeIntervalIndex) /* start micro-frame */
- {
- /* for all the micro-frame in interval uframeInterval */
- for (uframeIndex = uframeIntervalIndex; uframeIndex < (USB_HOST_CONFIG_EHCI_FRAME_LIST_SIZE * 8U);
- uframeIndex += uframeInterval)
- {
- if (frameIndex != (uframeIndex >> 3))
- {
- frameIndex = (uframeIndex >> 3);
- USB_HostBandwidthHsHostComputeCurrentHsAll(
- ehciInstance, frameIndex,
- frameTimes); /* compute the allocated bandwidths in the new frameIndex frame */
- }
- if (frameTimes[uframeIndex & 0x0007U] + timeData >
- s_SlotMaxBandwidthHs[(uframeIndex & 0x0007U)]) /* micro-frame has enough idle bandwidth? */
- {
- break; /* fail */
- }
- }
- if (uframeIndex >= (USB_HOST_CONFIG_EHCI_FRAME_LIST_SIZE * 8U)) /* success? */
- {
- break;
- }
- }
- if (uframeIntervalIndex < uframeInterval)
- {
- *uframeIndexOut = (uframeIntervalIndex);
- return kStatus_USB_Success;
- }
- else
- {
- return kStatus_USB_Error;
- }
- }
- #if (((defined USB_HOST_CONFIG_EHCI_MAX_ITD) && (USB_HOST_CONFIG_EHCI_MAX_ITD)) || \
- ((defined USB_HOST_CONFIG_EHCI_MAX_SITD) && (USB_HOST_CONFIG_EHCI_MAX_SITD)))
- static usb_status_t USB_HostBandwidthHsHostAllocateIso(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_pipe_t *ehciPipePointer)
- {
- usb_status_t status;
- uint32_t deviceInfo = 0;
- uint32_t hubNumber = 0;
- uint16_t uframeIntervalIndex = 0;
- uint16_t frameIntervalIndex = 0;
- uint16_t frameIndex;
- uint16_t timeCompleteSplit;
- uint16_t timeStartSplit;
- uint32_t timeData;
- uint8_t SsCsNumber = 0;
- uint16_t frameInterval;
- uint16_t frameTimes[8];
- uint8_t allocateOk = 1;
- uint16_t index;
- for (uint8_t i = 0; i < 8U; ++i)
- {
- frameTimes[i] = 0U;
- }
- (void)USB_HostHelperGetPeripheralInformation(ehciPipePointer->pipeCommon.deviceHandle,
- (uint32_t)kUSB_HostGetDeviceSpeed, &deviceInfo);
- timeData = USB_HostBandwidthComputeTime(
- (uint8_t)deviceInfo, USB_ENDPOINT_ISOCHRONOUS, ehciPipePointer->pipeCommon.direction,
- (((uint32_t)ehciPipePointer->pipeCommon.maxPacketSize) * ehciPipePointer->pipeCommon.numberPerUframe));
- /* pipe is high-speed */
- if (deviceInfo == USB_SPEED_HIGH)
- {
- uframeIntervalIndex = 0;
- status = USB_HostBandwidthHsHostAllocateHsCommon(ehciInstance, ehciPipePointer->uframeInterval,
- (uint16_t)timeData, &uframeIntervalIndex);
- if (status == kStatus_USB_Success)
- {
- ehciPipePointer->startFrame = (uframeIntervalIndex / 8U);
- ehciPipePointer->startUframe = (uint8_t)(uframeIntervalIndex & 0x0007U);
- ehciPipePointer->dataTime = (uint16_t)timeData;
- return kStatus_USB_Success;
- }
- }
- else /* pipe is full-speed or low-speed */
- {
- (void)USB_HostHelperGetPeripheralInformation(ehciPipePointer->pipeCommon.deviceHandle,
- (uint32_t)kUSB_HostGetHubThinkTime,
- &deviceInfo); /* deviceInfo variable means hub think time */
- timeData += (deviceInfo * 7U / (6U * 12U));
- (void)USB_HostHelperGetPeripheralInformation(ehciPipePointer->pipeCommon.deviceHandle,
- (uint32_t)kUSB_HostGetDeviceHSHubNumber, &hubNumber);
- frameInterval = ehciPipePointer->pipeCommon.interval;
- /* compute start-split and complete-split bandwidth */
- if (ehciPipePointer->pipeCommon.direction == USB_OUT)
- {
- timeStartSplit = (uint16_t)USB_HostBandwidthComputeTime(USB_SPEED_HIGH, USB_ENDPOINT_ISOCHRONOUS, USB_OUT,
- ehciPipePointer->pipeCommon.maxPacketSize);
- timeCompleteSplit = 0;
- }
- else
- {
- timeStartSplit =
- (uint16_t)USB_HostBandwidthComputeTime(USB_SPEED_HIGH, USB_ENDPOINT_ISOCHRONOUS, USB_IN, 1);
- timeCompleteSplit = (uint16_t)USB_HostBandwidthComputeTime(USB_SPEED_HIGH, USB_ENDPOINT_ISOCHRONOUS, USB_IN,
- ehciPipePointer->pipeCommon.maxPacketSize);
- }
- /* note: bandwidth must put in one frame */
- for (uframeIntervalIndex = 0U; uframeIntervalIndex <= 5U; ++uframeIntervalIndex) /* uframe interval */
- {
- for (frameIntervalIndex = 0U; frameIntervalIndex < frameInterval; ++frameIntervalIndex) /* frame interval */
- {
- allocateOk = 1;
- for (frameIndex = frameIntervalIndex; frameIndex < USB_HOST_CONFIG_EHCI_FRAME_LIST_SIZE;
- frameIndex += frameInterval) /* check all the frames */
- {
- /* compute start-split and complete-split number */
- SsCsNumber = (uint8_t)((ehciPipePointer->pipeCommon.maxPacketSize + 187U) /
- 188U); /* ss number for iso out; cs number for iso in */
- if (ehciPipePointer->pipeCommon.direction == USB_OUT) /* ISO OUT */
- {
- if (uframeIntervalIndex + SsCsNumber > 8U)
- {
- allocateOk = 0U;
- }
- }
- else
- {
- if (uframeIntervalIndex + 2U + SsCsNumber >
- 8U) /* ISO IN: there are two micro-frame interval between start-split and complete-split */
- {
- allocateOk = 0U;
- }
- }
- if (0U != allocateOk)
- {
- /* allocate start-split and complete-split bandwidth */
- USB_HostBandwidthHsHostComputeCurrentHsAll(ehciInstance, frameIndex, frameTimes);
- if (ehciPipePointer->pipeCommon.direction == USB_OUT) /* ISO OUT */
- {
- index = uframeIntervalIndex;
- for (; index < (uframeIntervalIndex + SsCsNumber); ++index)
- {
- if (frameTimes[index] + timeStartSplit > s_SlotMaxBandwidthHs[index])
- {
- allocateOk = 0U;
- break;
- }
- }
- }
- else /* ISO IN */
- {
- index = uframeIntervalIndex;
- if (frameTimes[index] + timeStartSplit > s_SlotMaxBandwidthHs[index])
- {
- allocateOk = 0U;
- }
- if (0U != allocateOk)
- {
- index =
- uframeIntervalIndex +
- 2U; /* there are two micro-frames interval between start-split and complete-split */
- for (; index < (uframeIntervalIndex + 2U + SsCsNumber); ++index)
- {
- if (frameTimes[index] + timeCompleteSplit > s_SlotMaxBandwidthHs[index])
- {
- allocateOk = 0U;
- break;
- }
- }
- }
- }
- }
- /* allocate data bandwidth */
- if (0U != allocateOk)
- {
- USB_HostBandwidthHsHostComputeCurrentFsls(ehciInstance, hubNumber, frameIndex, frameTimes);
- index = uframeIntervalIndex + 1U; /* timeData bandwidth start position */
- /* iso must occupy all the uframe bandwidth */
- {
- deviceInfo = timeData; /* note: deviceInfo variable means bandwidth here */
- while ((index < 8U) && (deviceInfo > s_SlotMaxBandwidth[index]))
- {
- if (frameTimes[index] > 0U)
- {
- allocateOk = 0U;
- break;
- }
- else
- {
- deviceInfo -= s_SlotMaxBandwidth[index];
- }
- ++index;
- }
- }
- }
- if (0U != allocateOk)
- {
- /* data bandwidth can be put in the frame? */
- index = uframeIntervalIndex + 1U; /* timeData bandwidth start position */
- frameTimes[index] += (uint16_t)timeData;
- for (; index < 7U; ++index)
- {
- if (frameTimes[index] > s_SlotMaxBandwidth[index])
- {
- frameTimes[index + 1U] += (frameTimes[index] - s_SlotMaxBandwidth[index]);
- frameTimes[index] = s_SlotMaxBandwidth[index];
- }
- else
- {
- break;
- }
- }
- if (frameTimes[index] > s_SlotMaxBandwidth[index])
- {
- allocateOk = 0;
- }
- }
- if (0U != allocateOk)
- {
- break;
- }
- }
- if (0U != allocateOk)
- {
- break;
- }
- }
- if (0U != allocateOk)
- {
- break;
- }
- }
- if (0U != allocateOk)
- {
- ehciPipePointer->startFrame = frameIntervalIndex;
- ehciPipePointer->startUframe = (uint8_t)uframeIntervalIndex;
- ehciPipePointer->dataTime = (uint16_t)timeData;
- ehciPipePointer->startSplitTime = timeStartSplit;
- ehciPipePointer->completeSplitTime = timeCompleteSplit;
- if (ehciPipePointer->pipeCommon.direction == USB_OUT)
- {
- index = uframeIntervalIndex;
- for (; index < (uframeIntervalIndex + SsCsNumber); ++index)
- {
- ehciPipePointer->uframeSmask = ehciPipePointer->uframeSmask | (uint8_t)(0x01UL << index);
- }
- }
- else
- {
- index = uframeIntervalIndex;
- ehciPipePointer->uframeSmask = ehciPipePointer->uframeSmask | (uint8_t)(0x01UL << index);
- index = uframeIntervalIndex + 2U;
- for (; index < (uframeIntervalIndex + 2U + SsCsNumber); ++index)
- {
- ehciPipePointer->uframeCmask = ehciPipePointer->uframeCmask | (uint8_t)(0x01UL << index);
- }
- }
- return kStatus_USB_Success;
- }
- }
- return kStatus_USB_Error;
- }
- #endif
- static usb_status_t USB_HostBandwidthHsHostAllocateInterrupt(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_pipe_t *ehciPipePointer)
- {
- usb_status_t status;
- uint32_t deviceInfo = 0;
- uint32_t hubNumber = 0;
- uint16_t uframeIntervalIndex = 0;
- uint16_t frameIntervalIndex = 0;
- uint16_t frameIndex;
- uint16_t timeCompleteSplit;
- uint16_t timeStartSplit;
- uint32_t timeData;
- uint8_t SsCsNumber;
- uint16_t frameInterval;
- uint16_t frameTimes[8];
- uint8_t allocateOk = 1;
- uint8_t index;
- for (uint8_t i = 0; i < 8U; ++i)
- {
- frameTimes[i] = 0U;
- }
- (void)USB_HostHelperGetPeripheralInformation(ehciPipePointer->pipeCommon.deviceHandle,
- (uint32_t)kUSB_HostGetDeviceSpeed, &deviceInfo);
- timeData = USB_HostBandwidthComputeTime(
- (uint8_t)deviceInfo, USB_ENDPOINT_INTERRUPT, ehciPipePointer->pipeCommon.direction,
- (uint32_t)ehciPipePointer->pipeCommon.maxPacketSize * ehciPipePointer->pipeCommon.numberPerUframe);
- /* pipe is high-speed */
- if (deviceInfo == USB_SPEED_HIGH)
- {
- uframeIntervalIndex = 0;
- status = USB_HostBandwidthHsHostAllocateHsCommon(ehciInstance, ehciPipePointer->uframeInterval,
- (uint16_t)timeData, &uframeIntervalIndex);
- if (status == kStatus_USB_Success)
- {
- ehciPipePointer->startFrame = (uframeIntervalIndex / 8U);
- ehciPipePointer->startUframe = (uint8_t)(uframeIntervalIndex & 0x0007U);
- /* for HS interrupt start transaction position */
- if (ehciPipePointer->uframeInterval >= 8U)
- {
- ehciPipePointer->uframeSmask = (0x01U << ehciPipePointer->startUframe);
- }
- else
- {
- ehciPipePointer->uframeSmask = 0x00U;
- for (index = ehciPipePointer->startUframe; index < 8U;
- index += (uint8_t)ehciPipePointer->uframeInterval)
- {
- ehciPipePointer->uframeSmask |= (0x01U << index);
- }
- }
- ehciPipePointer->dataTime = (uint16_t)timeData;
- return kStatus_USB_Success;
- }
- }
- else /* pipe is full-speed or low-speed */
- {
- (void)USB_HostHelperGetPeripheralInformation(ehciPipePointer->pipeCommon.deviceHandle,
- (uint32_t)kUSB_HostGetHubThinkTime, &deviceInfo);
- timeData += (deviceInfo * 7U / (6U * 12U));
- (void)USB_HostHelperGetPeripheralInformation(ehciPipePointer->pipeCommon.deviceHandle,
- (uint32_t)kUSB_HostGetDeviceHSHubNumber, &hubNumber);
- frameInterval = ehciPipePointer->pipeCommon.interval;
- SsCsNumber = 3U; /* complete split number */
- /* compute start-split and complete-split bandwidth */
- if (ehciPipePointer->pipeCommon.direction == USB_OUT)
- {
- timeStartSplit = (uint16_t)USB_HostBandwidthComputeTime(USB_SPEED_HIGH, USB_ENDPOINT_INTERRUPT, USB_OUT,
- ehciPipePointer->pipeCommon.maxPacketSize);
- timeStartSplit +=
- (uint16_t)USB_HostBandwidthComputeTime(USB_SPEED_HIGH, USB_ENDPOINT_INTERRUPT, USB_OUT, 1U);
- timeCompleteSplit =
- (uint16_t)USB_HostBandwidthComputeTime(USB_SPEED_HIGH, USB_ENDPOINT_INTERRUPT, USB_OUT, 0U);
- }
- else
- {
- timeStartSplit = (uint16_t)USB_HostBandwidthComputeTime(USB_SPEED_HIGH, USB_ENDPOINT_INTERRUPT, USB_IN, 1U);
- timeCompleteSplit = (uint16_t)USB_HostBandwidthComputeTime(USB_SPEED_HIGH, USB_ENDPOINT_INTERRUPT, USB_IN,
- ehciPipePointer->pipeCommon.maxPacketSize);
- timeCompleteSplit +=
- (uint16_t)USB_HostBandwidthComputeTime(USB_SPEED_HIGH, USB_ENDPOINT_INTERRUPT, USB_IN, 0U);
- }
- /* note: bandwidth must put in one frame */
- for (uframeIntervalIndex = 0U; uframeIntervalIndex <= 4U; ++uframeIntervalIndex) /* uframe interval */
- {
- for (frameIntervalIndex = 0U; frameIntervalIndex < frameInterval; ++frameIntervalIndex) /* frame interval */
- {
- allocateOk = 1U;
- for (frameIndex = frameIntervalIndex; frameIndex < USB_HOST_CONFIG_EHCI_FRAME_LIST_SIZE;
- frameIndex += frameInterval) /* check all the frames */
- {
- /* allocate data bandwidth */
- USB_HostBandwidthHsHostComputeCurrentFsls(ehciInstance, hubNumber, frameIndex, frameTimes);
- index = (uint8_t)(uframeIntervalIndex + 1U);
- for (; index <= (uframeIntervalIndex + 3U); ++index) /* data bandwidth number is 3.
- uframeIntervalIndex don't exceed 4, so
- index cannot exceed 7 */
- {
- if (frameTimes[index] + timeData > s_SlotMaxBandwidth[index])
- {
- allocateOk = 0;
- break;
- }
- }
- if (0U != allocateOk)
- {
- USB_HostBandwidthHsHostComputeCurrentHsAll(ehciInstance, frameIndex, frameTimes);
- /* allocate start_split bandwidth */
- if (frameTimes[uframeIntervalIndex] + timeStartSplit >
- s_SlotMaxBandwidthHs[uframeIntervalIndex])
- {
- allocateOk = 0U;
- }
- if (0U != allocateOk)
- {
- /* allocate complete_split bandwidth */
- index = (uint8_t)uframeIntervalIndex + 2U;
- /* complete-split number is normal 3. When uframeIntervalIndex is 4, complete-split number
- * is 2. */
- for (; (index <= (uframeIntervalIndex + 1U + SsCsNumber)) && (index < 8U); ++index)
- {
- if (frameTimes[index] + timeCompleteSplit > s_SlotMaxBandwidthHs[index])
- {
- allocateOk = 0U;
- break;
- }
- }
- }
- }
- if (0U == allocateOk)
- {
- break; /* allocate fail */
- }
- }
- if (0U != allocateOk)
- {
- break;
- }
- }
- if (0U != allocateOk)
- {
- break;
- }
- }
- if (0U != allocateOk)
- {
- ehciPipePointer->startFrame = frameIntervalIndex;
- ehciPipePointer->startUframe = (uint8_t)uframeIntervalIndex;
- ehciPipePointer->uframeSmask = (0x01u << ehciPipePointer->startUframe);
- ehciPipePointer->uframeCmask = 0u;
- index = (uint8_t)uframeIntervalIndex + 2u;
- for (; (index <= (uframeIntervalIndex + 1u + SsCsNumber)) && (index < 8u); ++index)
- {
- ehciPipePointer->uframeCmask = ehciPipePointer->uframeCmask | (0x01U << index);
- }
- ehciPipePointer->dataTime = (uint16_t)timeData;
- ehciPipePointer->startSplitTime = timeStartSplit;
- ehciPipePointer->completeSplitTime = timeCompleteSplit;
- return kStatus_USB_Success;
- }
- }
- return kStatus_USB_BandwidthFail;
- }
- static usb_status_t USB_HostBandwidthFslsHostAllocate(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_pipe_t *ehciPipePointer)
- {
- uint32_t FslsTime = 0;
- uint32_t speed = 0;
- uint16_t uframeIntervalIndex;
- uint16_t frameIndex;
- uint16_t frameInterval;
- uint16_t frameTime;
- (void)USB_HostHelperGetPeripheralInformation(ehciPipePointer->pipeCommon.deviceHandle,
- (uint32_t)kUSB_HostGetHubThinkTime, &FslsTime);
- FslsTime += (FslsTime * 7U / (6U * 12U));
- (void)USB_HostHelperGetPeripheralInformation(ehciPipePointer->pipeCommon.deviceHandle,
- (uint32_t)kUSB_HostGetDeviceSpeed, &speed);
- FslsTime = FslsTime + USB_HostBandwidthComputeTime((uint8_t)speed, ehciPipePointer->pipeCommon.pipeType,
- ehciPipePointer->pipeCommon.direction,
- ehciPipePointer->pipeCommon.maxPacketSize);
- frameInterval = ehciPipePointer->pipeCommon.interval;
- for (uframeIntervalIndex = 0; uframeIntervalIndex < ehciPipePointer->uframeInterval;
- ++uframeIntervalIndex) /* uframeIntervalIndex can exceed 8 */
- {
- for (frameIndex = (uframeIntervalIndex >> 3); frameIndex < USB_HOST_CONFIG_EHCI_FRAME_LIST_SIZE;
- frameIndex += frameInterval)
- {
- USB_HostBandwidthFslsHostComputeCurrent(ehciInstance, frameIndex, &frameTime);
- if (frameTime + FslsTime > USB_HOST_EHCI_BANDWIDTH_FRAME_TOTOAL_TIME)
- {
- break;
- }
- }
- if (frameIndex >= USB_HOST_CONFIG_EHCI_FRAME_LIST_SIZE)
- {
- break;
- }
- }
- if (uframeIntervalIndex < ehciPipePointer->uframeInterval)
- {
- ehciPipePointer->startFrame = (uframeIntervalIndex >> 3);
- ehciPipePointer->startUframe = (uint8_t)(uframeIntervalIndex & 0x0007U);
- ehciPipePointer->uframeSmask = 0; /* useless */
- ehciPipePointer->uframeCmask = 0;
- ehciPipePointer->dataTime = (uint16_t)FslsTime;
- return kStatus_USB_Success;
- }
- return kStatus_USB_BandwidthFail;
- }
- static uint8_t USB_HostEhciGet2PowerValue(uint8_t value)
- {
- if ((value == 0U) || (value == 1U))
- {
- return value;
- }
- if (0U != (value & 0xf0U))
- {
- if (0U != (value & 0x80U))
- {
- return 128U;
- }
- else if (0U != (value & 0x40U))
- {
- return 64U;
- }
- else if (0U != (value & 0x20U))
- {
- return 32U;
- }
- else
- {
- return 16U;
- }
- }
- else
- {
- if (0U != (value & 0x08U))
- {
- return 8U;
- }
- else if (0U != (value & 0x04U))
- {
- return 4U;
- }
- else if (0U != (value & 0x02U))
- {
- return 2U;
- }
- else
- {
- return 1U;
- }
- }
- }
- static void USB_HostEhciZeroMem(uint32_t *buffer, uint32_t length)
- {
- /* note: the zero unit is uint32_t */
- while (0U != length)
- {
- length--;
- *buffer = 0;
- buffer++;
- }
- }
- static void USB_HostEhciDelay(USBHS_Type *ehciIpBase, uint32_t ms)
- {
- /* note: the max delay time cannot exceed half of max value (0x4000) */
- uint32_t sofStart;
- uint32_t SofEnd;
- uint32_t distance;
- sofStart = (ehciIpBase->FRINDEX & EHCI_MAX_UFRAME_VALUE);
- do
- {
- SofEnd = (ehciIpBase->FRINDEX & EHCI_MAX_UFRAME_VALUE);
- distance = (SofEnd + EHCI_MAX_UFRAME_VALUE + 1U - sofStart);
- } while ((distance & EHCI_MAX_UFRAME_VALUE) < (ms * 8U)); /* compute the distance between sofStart and SofEnd */
- }
- void USB_HostEhciStartAsync(usb_host_ehci_instance_t *ehciInstance)
- {
- uint32_t stateSync;
- if (0U == (ehciInstance->ehciIpBase->USBSTS & USBHS_USBSTS_AS_MASK))
- {
- /* the status must be same when change USBCMD->ASE */
- do
- {
- stateSync = ((ehciInstance->ehciIpBase->USBSTS & USBHS_USBSTS_AS_MASK) |
- (ehciInstance->ehciIpBase->USBCMD & USBHS_USBCMD_ASE_MASK));
- } while ((stateSync == USBHS_USBSTS_AS_MASK) || (stateSync == USBHS_USBCMD_ASE_MASK));
- ehciInstance->ehciIpBase->ASYNCLISTADDR = (uint32_t)(ehciInstance->shedFirstQh);
- ehciInstance->ehciIpBase->USBCMD |= USBHS_USBCMD_ASE_MASK;
- while (0U == (ehciInstance->ehciIpBase->USBSTS & USBHS_USBSTS_AS_MASK))
- {
- }
- }
- }
- static void USB_HostEhciStopAsync(usb_host_ehci_instance_t *ehciInstance)
- {
- uint32_t stateSync;
- /* the status must be same when change USBCMD->ASE */
- do
- {
- stateSync = ((ehciInstance->ehciIpBase->USBSTS & USBHS_USBSTS_AS_MASK) |
- (ehciInstance->ehciIpBase->USBCMD & USBHS_USBCMD_ASE_MASK));
- } while ((stateSync == USBHS_USBSTS_AS_MASK) || (stateSync == USBHS_USBCMD_ASE_MASK));
- ehciInstance->ehciIpBase->USBCMD &= (uint32_t)(~(uint32_t)USBHS_USBCMD_ASE_MASK); /* disable async schedule */
- while (0U != (ehciInstance->ehciIpBase->USBSTS & USBHS_USBSTS_AS_MASK))
- {
- }
- }
- void USB_HostEhciStartPeriodic(usb_host_ehci_instance_t *ehciInstance)
- {
- uint32_t stateSync;
- if (0U == (ehciInstance->ehciIpBase->USBSTS & USBHS_USBSTS_PS_MASK))
- {
- /* the status must be same when change USBCMD->PSE */
- do
- {
- stateSync = ((ehciInstance->ehciIpBase->USBSTS & USBHS_USBSTS_PS_MASK) |
- (ehciInstance->ehciIpBase->USBCMD & USBHS_USBCMD_PSE_MASK));
- } while ((stateSync == USBHS_USBSTS_PS_MASK) || (stateSync == USBHS_USBCMD_PSE_MASK));
- ehciInstance->ehciIpBase->PERIODICLISTBASE = (uint32_t)(ehciInstance->ehciFrameList);
- if (0U == (ehciInstance->ehciIpBase->USBCMD & USBHS_USBCMD_PSE_MASK))
- {
- ehciInstance->ehciIpBase->USBCMD |= USBHS_USBCMD_PSE_MASK; /* start periodic schedule */
- }
- while (0U == (ehciInstance->ehciIpBase->USBSTS & USBHS_USBSTS_PS_MASK))
- {
- }
- }
- return;
- }
- static void USB_HostEhciStopPeriodic(usb_host_ehci_instance_t *ehciInstance)
- {
- uint32_t stateSync;
- /* the status must be same when change USBCMD->PSE */
- do
- {
- stateSync = ((ehciInstance->ehciIpBase->USBSTS & USBHS_USBSTS_PS_MASK) |
- (ehciInstance->ehciIpBase->USBCMD & USBHS_USBCMD_PSE_MASK));
- } while ((stateSync == USBHS_USBSTS_PS_MASK) || (stateSync == USBHS_USBCMD_PSE_MASK));
- ehciInstance->ehciIpBase->USBCMD &= (~USBHS_USBCMD_PSE_MASK); /* stop periodic schedule */
- while (0U != (ehciInstance->ehciIpBase->USBSTS & USBHS_USBSTS_PS_MASK))
- {
- }
- }
- static usb_status_t USB_HostEhciQhQtdListInit(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_pipe_t *ehciPipePointer,
- usb_host_transfer_t *transfer)
- {
- volatile usb_host_ehci_qh_t *vltQhPointer;
- usb_host_ehci_qtd_t *qtdPointer = NULL;
- usb_host_ehci_qtd_t *BaseQtdPointer = NULL;
- volatile uint32_t *entryPointer;
- uint32_t qtdNumber;
- uint32_t dataLength;
- uint32_t dataAddress;
- uint32_t endAddress;
- uint8_t index;
- /* compute the qtd number */
- if (ehciPipePointer->pipeCommon.pipeType == USB_ENDPOINT_CONTROL)
- {
- /* assume setup data don't exceed one qtd data size, one qtd can transfer least 16k data */
- if (transfer->transferLength == 0U)
- {
- qtdNumber = 2U;
- }
- else
- {
- qtdNumber = 3U;
- }
- }
- else
- {
- qtdNumber = (((transfer->transferLength) & 0xFFFFC000U) >> 14U) +
- (0U != ((transfer->transferLength) & 0x00003FFFU) ? 1U : 0U);
- if (0U == qtdNumber)
- {
- qtdNumber = 1U;
- }
- }
- vltQhPointer = (volatile usb_host_ehci_qh_t *)ehciPipePointer->ehciQh;
- /* get qtd list */
- USB_HostEhciLock();
- if (qtdNumber <= ehciInstance->ehciQtdNumber)
- {
- ehciInstance->ehciQtdNumber -= (uint8_t)qtdNumber;
- BaseQtdPointer = ehciInstance->ehciQtdHead;
- qtdPointer = NULL;
- do
- {
- if (qtdPointer != NULL)
- {
- qtdPointer->nextQtdPointer = (uint32_t)ehciInstance->ehciQtdHead;
- }
- qtdPointer = ehciInstance->ehciQtdHead;
- ehciInstance->ehciQtdHead = (usb_host_ehci_qtd_t *)qtdPointer->nextQtdPointer;
- qtdPointer->nextQtdPointer = 0;
- --qtdNumber;
- } while (0U != qtdNumber);
- if (ehciInstance->ehciQtdNumber == 0U)
- {
- ehciInstance->ehciQtdTail = NULL;
- }
- }
- else
- {
- USB_HostEhciUnlock();
- return kStatus_USB_Error;
- }
- USB_HostEhciUnlock();
- /* int qTD list */
- if (ehciPipePointer->pipeCommon.pipeType == USB_ENDPOINT_CONTROL)
- {
- /* setup transaction qtd */
- qtdPointer = BaseQtdPointer;
- qtdPointer->alternateNextQtdPointer = EHCI_HOST_T_INVALID_VALUE;
- /* dt: need set; ioc: 0; C_Page: 0; PID Code: SETUP; Status: Active */
- qtdPointer->transferResults[1] = 0U;
- qtdPointer->transferResults[0] =
- ((0x00000000UL << EHCI_HOST_QTD_DT_SHIFT) | (8UL << EHCI_HOST_QTD_TOTAL_BYTES_SHIFT) |
- (EHCI_HOST_PID_SETUP << EHCI_HOST_QTD_PID_CODE_SHIFT) | (EHCI_HOST_QTD_STATUS_ACTIVE_MASK));
- dataAddress = ((uint32_t)transfer->setupPacket);
- qtdPointer->transferResults[1] = dataAddress; /* current offset is set too */
- /* set buffer pointer no matter data length */
- for (index = 0; index < 4U; ++index)
- {
- qtdPointer->bufferPointers[index] = ((dataAddress + ((uint32_t)index + 1U) * 4U * 1024U) & 0xFFFFF000U);
- }
- /* data transaction qtd */
- dataLength = transfer->transferLength;
- if (dataLength != 0U)
- {
- qtdPointer = (usb_host_ehci_qtd_t *)(qtdPointer->nextQtdPointer);
- qtdPointer->alternateNextQtdPointer = EHCI_HOST_T_INVALID_VALUE;
- /* dt: need set; ioc: 0; C_Page: 0; PID Code: IN/OUT; Status: Active */
- qtdPointer->transferResults[1] = 0U;
- if (transfer->direction == USB_OUT)
- {
- qtdPointer->transferResults[0] =
- ((0x00000001UL << EHCI_HOST_QTD_DT_SHIFT) | (dataLength << EHCI_HOST_QTD_TOTAL_BYTES_SHIFT) |
- (EHCI_HOST_PID_OUT << EHCI_HOST_QTD_PID_CODE_SHIFT) | (EHCI_HOST_QTD_STATUS_ACTIVE_MASK));
- }
- else
- {
- qtdPointer->transferResults[0] =
- ((0x00000001U << EHCI_HOST_QTD_DT_SHIFT) | (dataLength << EHCI_HOST_QTD_TOTAL_BYTES_SHIFT) |
- (EHCI_HOST_PID_IN << EHCI_HOST_QTD_PID_CODE_SHIFT) | (EHCI_HOST_QTD_STATUS_ACTIVE_MASK));
- }
- dataAddress = (uint32_t)transfer->transferBuffer;
- qtdPointer->transferResults[1] = dataAddress; /* current offset is set too */
- /* set buffer pointer no matter data length */
- for (index = 0; index < 4U; ++index)
- {
- qtdPointer->bufferPointers[index] = ((dataAddress + ((uint32_t)index + 1U) * 4U * 1024U) & 0xFFFFF000U);
- }
- }
- /* status transaction qtd */
- qtdPointer = (usb_host_ehci_qtd_t *)(qtdPointer->nextQtdPointer);
- qtdPointer->alternateNextQtdPointer = EHCI_HOST_T_INVALID_VALUE;
- /* dt: dont care; ioc: 1; C_Page: 0; PID Code: IN/OUT; Status: Active */
- qtdPointer->transferResults[1] = 0;
- if ((dataLength == 0U) || (transfer->direction == USB_OUT))
- {
- qtdPointer->transferResults[0] =
- ((0x00000001UL << EHCI_HOST_QTD_DT_SHIFT) | (EHCI_HOST_PID_IN << EHCI_HOST_QTD_PID_CODE_SHIFT) |
- (EHCI_HOST_QTD_IOC_MASK) | (EHCI_HOST_QTD_STATUS_ACTIVE_MASK));
- }
- else
- {
- qtdPointer->transferResults[0] =
- ((0x00000001U << EHCI_HOST_QTD_DT_SHIFT) | (EHCI_HOST_PID_OUT << EHCI_HOST_QTD_PID_CODE_SHIFT) |
- (EHCI_HOST_QTD_IOC_MASK) | (EHCI_HOST_QTD_STATUS_ACTIVE_MASK));
- }
- qtdPointer->nextQtdPointer |= EHCI_HOST_T_INVALID_VALUE;
- }
- else
- {
- dataAddress = (uint32_t)transfer->transferBuffer;
- qtdPointer = BaseQtdPointer;
- while (1U == 1U)
- {
- endAddress = dataAddress + (16U * 1024U);
- if (endAddress > (uint32_t)(transfer->transferBuffer + transfer->transferLength))
- {
- endAddress = (uint32_t)(transfer->transferBuffer + transfer->transferLength);
- }
- qtdPointer->alternateNextQtdPointer = EHCI_HOST_T_INVALID_VALUE;
- /* dt: set; ioc: 0; C_Page: 0; PID Code: IN/OUT; Status: Active */
- qtdPointer->transferResults[1] = 0U;
- if (transfer->direction == USB_OUT)
- {
- qtdPointer->transferResults[0] =
- (((endAddress - dataAddress) << EHCI_HOST_QTD_TOTAL_BYTES_SHIFT) |
- ((uint32_t)ehciPipePointer->pipeCommon.nextdata01 << EHCI_HOST_QTD_DT_SHIFT) |
- (EHCI_HOST_QTD_CERR_MAX_VALUE << EHCI_HOST_QTD_CERR_SHIFT) |
- (EHCI_HOST_PID_OUT << EHCI_HOST_QTD_PID_CODE_SHIFT) | (EHCI_HOST_QTD_STATUS_ACTIVE_MASK));
- }
- else
- {
- qtdPointer->transferResults[0] =
- (((endAddress - dataAddress) << EHCI_HOST_QTD_TOTAL_BYTES_SHIFT) |
- ((uint32_t)ehciPipePointer->pipeCommon.nextdata01 << EHCI_HOST_QTD_DT_SHIFT) |
- (EHCI_HOST_QTD_CERR_MAX_VALUE << EHCI_HOST_QTD_CERR_SHIFT) |
- (EHCI_HOST_PID_IN << EHCI_HOST_QTD_PID_CODE_SHIFT) | (EHCI_HOST_QTD_STATUS_ACTIVE_MASK));
- }
- qtdPointer->transferResults[1] = dataAddress; /* current offset is set too */
- /* set buffer pointer no matter data length */
- for (index = 0; index < 4U; ++index)
- {
- qtdPointer->bufferPointers[index] = ((dataAddress + ((uint32_t)index + 1U) * 4U * 1024U) & 0xFFFFF000U);
- }
- dataAddress = endAddress; /* for next qtd */
- if (qtdPointer->nextQtdPointer == 0U)
- {
- break;
- }
- qtdPointer = (usb_host_ehci_qtd_t *)(qtdPointer->nextQtdPointer);
- }
- qtdPointer->nextQtdPointer |= EHCI_HOST_T_INVALID_VALUE;
- qtdPointer->transferResults[0] |= EHCI_HOST_QTD_IOC_MASK; /* last one set IOC */
- }
- /* save qtd to transfer */
- transfer->union1.unitHead = (uint32_t)BaseQtdPointer;
- transfer->union2.unitTail = (uint32_t)qtdPointer;
- /* link transfer to qh */
- transfer->next = NULL;
- if (vltQhPointer->ehciTransferHead == NULL)
- {
- transfer->next = NULL;
- vltQhPointer->ehciTransferTail = transfer;
- vltQhPointer->ehciTransferHead = transfer;
- }
- else
- {
- transfer->next = NULL;
- vltQhPointer->ehciTransferTail->next = transfer;
- vltQhPointer->ehciTransferTail = transfer;
- }
- USB_HostEhciLock();
- /* link qtd to qh (link to end) */
- entryPointer = &(vltQhPointer->nextQtdPointer);
- dataAddress = *entryPointer; /* dataAddress variable means entry value here */
- while ((0U != dataAddress) && (0U == (dataAddress & EHCI_HOST_T_INVALID_VALUE)))
- {
- entryPointer = (volatile uint32_t *)dataAddress;
- dataAddress = *entryPointer;
- }
- *entryPointer = (uint32_t)BaseQtdPointer;
- USB_HostEhciUnlock();
- USB_HostEhciStartAsync(ehciInstance);
- return kStatus_USB_Success;
- }
- static uint32_t USB_HostEhciQtdListRelease(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_qtd_t *ehciQtdStart,
- usb_host_ehci_qtd_t *ehciQtdEnd)
- {
- uint32_t length = 0;
- usb_host_ehci_qtd_t *qtdPointer;
- ehciQtdEnd->nextQtdPointer = 0U;
- /* compute remaining length */
- qtdPointer = ehciQtdStart;
- while (qtdPointer != ehciQtdEnd)
- {
- length +=
- ((qtdPointer->transferResults[0] & EHCI_HOST_QTD_TOTAL_BYTES_MASK) >> EHCI_HOST_QTD_TOTAL_BYTES_SHIFT);
- qtdPointer = (usb_host_ehci_qtd_t *)qtdPointer->nextQtdPointer;
- }
- qtdPointer = ehciQtdEnd;
- length += ((qtdPointer->transferResults[0] & EHCI_HOST_QTD_TOTAL_BYTES_MASK) >> EHCI_HOST_QTD_TOTAL_BYTES_SHIFT);
- /* put releasing qtd to idle qtd list */
- USB_HostEhciLock();
- if (ehciInstance->ehciQtdNumber == 0U)
- {
- ehciInstance->ehciQtdHead = ehciQtdStart;
- ehciInstance->ehciQtdTail = ehciQtdEnd;
- }
- else
- {
- ehciInstance->ehciQtdTail->nextQtdPointer = (uint32_t)ehciQtdStart;
- ehciInstance->ehciQtdTail = ehciQtdEnd;
- }
- while (ehciQtdStart != ehciQtdEnd)
- {
- ehciInstance->ehciQtdNumber++;
- ehciQtdStart = (usb_host_ehci_qtd_t *)ehciQtdStart->nextQtdPointer;
- }
- ehciInstance->ehciQtdNumber++;
- USB_HostEhciUnlock();
- return length;
- }
- static usb_status_t USB_HostEhciQhQtdListDeinit(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_pipe_t *ehciPipePointer)
- {
- volatile usb_host_ehci_qh_t *vltQhPointer;
- usb_host_transfer_t *transfer;
- usb_host_transfer_t *nextTransfer;
- uint32_t currentQtdPointer;
- uint8_t needStop = 0U;
- vltQhPointer = (volatile usb_host_ehci_qh_t *)ehciPipePointer->ehciQh;
- USB_HostEhciLock(); /* this API is called from APP, the host task may occupy to access the same resource */
- /* remove qtd from qh */
- /*for misra 13.5*/
- currentQtdPointer = vltQhPointer->currentQtdPointer;
- if ((0U == ((uint32_t)vltQhPointer->nextQtdPointer & EHCI_HOST_T_INVALID_VALUE)) ||
- (0U == ((uint32_t)currentQtdPointer & EHCI_HOST_T_INVALID_VALUE)))
- {
- /* need stop async schedule */
- if ((0U == (vltQhPointer->horizontalLinkPointer & EHCI_HOST_T_INVALID_VALUE)) &&
- (ehciPipePointer->pipeCommon.pipeType != USB_ENDPOINT_INTERRUPT))
- {
- needStop = 1U;
- }
- if (0U != needStop)
- {
- USB_HostEhciStopAsync(ehciInstance);
- }
- vltQhPointer->currentQtdPointer = EHCI_HOST_T_INVALID_VALUE; /* invalid current qtd */
- vltQhPointer->nextQtdPointer = EHCI_HOST_T_INVALID_VALUE; /* invalid next qtd */
- vltQhPointer->transferOverlayResults[0] &= (~EHCI_HOST_QTD_STATUS_MASK); /* clear error status */
- if (0U != needStop)
- {
- USB_HostEhciStartAsync(ehciInstance);
- }
- }
- /* remove transfer from the QH transfer list */
- transfer = vltQhPointer->ehciTransferHead;
- vltQhPointer->ehciTransferTail = NULL;
- vltQhPointer->ehciTransferHead = NULL;
- USB_HostEhciUnlock();
- /* release qtd and transfer callback*/
- while (transfer != NULL)
- {
- nextTransfer = transfer->next; /* the transfer is released when call back */
- transfer->transferSofar =
- USB_HostEhciQtdListRelease(ehciInstance, (usb_host_ehci_qtd_t *)(transfer->union1.unitHead),
- (usb_host_ehci_qtd_t *)(transfer->union2.unitTail));
- transfer->transferSofar = (transfer->transferLength < transfer->transferSofar) ?
- 0U :
- (transfer->transferLength - transfer->transferSofar);
- /* callback function is different from the current condition */
- transfer->callbackFn(transfer->callbackParam, transfer, kStatus_USB_TransferCancel);
- transfer = nextTransfer;
- }
- return kStatus_USB_Success;
- }
- static usb_status_t USB_HostEhciTransferQtdListDeinit(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_pipe_t *ehciPipePointer,
- usb_host_transfer_t *transfer)
- {
- volatile usb_host_ehci_qh_t *vltQhPointer;
- usb_host_transfer_t *preSearchTransfer;
- uint32_t qhNextQtdValue;
- uint32_t qtdPointerEntry;
- uint32_t *searchQtdEntryPointer;
- vltQhPointer = (volatile usb_host_ehci_qh_t *)ehciPipePointer->ehciQh;
- USB_HostEhciLock(); /* this API is called from APP, the host task may occupy to access the same resource */
- /* remove qtd from qh */
- qhNextQtdValue = (uint32_t)vltQhPointer->currentQtdPointer;
- qtdPointerEntry = *((uint32_t *)qhNextQtdValue + 2); /* note: qtdPointerEntry means qtd status */
- if ((0U != (qhNextQtdValue & EHCI_HOST_T_INVALID_VALUE)) ||
- (0U == (qtdPointerEntry & EHCI_HOST_QTD_STATUS_ACTIVE_MASK)))
- {
- qhNextQtdValue = (uint32_t)vltQhPointer->nextQtdPointer;
- }
- if (0U == (qhNextQtdValue & EHCI_HOST_T_INVALID_VALUE)) /* there is pending qtd in the qh */
- {
- /* this qh don't schedule temporarily */
- if (ehciPipePointer->pipeCommon.pipeType != USB_ENDPOINT_INTERRUPT)
- {
- USB_HostEhciStopAsync(ehciInstance);
- }
- vltQhPointer->currentQtdPointer |= EHCI_HOST_T_INVALID_VALUE; /* invalid current qtd */
- vltQhPointer->nextQtdPointer |= EHCI_HOST_T_INVALID_VALUE; /* invalid next qtd */
- if (ehciPipePointer->pipeCommon.pipeType != USB_ENDPOINT_INTERRUPT)
- {
- USB_HostEhciStartAsync(ehciInstance);
- }
- /* remove qtd from qh one by one */
- qtdPointerEntry = transfer->union1.unitHead;
- while (1U == 1U)
- {
- /* search qh's qtd list for qtdPointerEntry */
- searchQtdEntryPointer = &qhNextQtdValue;
- while (0U == ((*searchQtdEntryPointer) & EHCI_HOST_T_INVALID_VALUE))
- {
- if ((*searchQtdEntryPointer) == qtdPointerEntry)
- {
- *searchQtdEntryPointer = *((uint32_t *)qtdPointerEntry); /* remove the qtd from qh */
- break;
- }
- else
- {
- searchQtdEntryPointer = (uint32_t *)(*searchQtdEntryPointer);
- }
- }
- if (qtdPointerEntry == transfer->union2.unitTail)
- {
- break;
- }
- qtdPointerEntry = *((uint32_t *)qtdPointerEntry);
- }
- }
- /* remove transfer from the QH transfer list */
- preSearchTransfer = vltQhPointer->ehciTransferHead;
- if (preSearchTransfer == transfer)
- {
- vltQhPointer->ehciTransferHead = preSearchTransfer->next;
- }
- else
- {
- while (preSearchTransfer != NULL)
- {
- if (preSearchTransfer->next == transfer)
- {
- preSearchTransfer->next = transfer->next;
- break;
- }
- else
- {
- preSearchTransfer = preSearchTransfer->next;
- }
- }
- }
- USB_HostEhciUnlock();
- /* release qtd and callback */
- transfer->transferSofar =
- USB_HostEhciQtdListRelease(ehciInstance, (usb_host_ehci_qtd_t *)(transfer->union1.unitHead),
- (usb_host_ehci_qtd_t *)(transfer->union2.unitTail));
- transfer->transferSofar = (transfer->transferLength < transfer->transferSofar) ?
- 0U :
- (transfer->transferLength - transfer->transferSofar);
- /* callback function is different from the current condition */
- transfer->callbackFn(transfer->callbackParam, transfer, kStatus_USB_TransferCancel);
- /* start this qh schedule */
- vltQhPointer->transferOverlayResults[0] &= (~EHCI_HOST_QTD_STATUS_MASK); /* clear error status */
- if ((qhNextQtdValue != 0U) && (0U == (qhNextQtdValue & EHCI_HOST_T_INVALID_VALUE)))
- {
- vltQhPointer->nextQtdPointer = qhNextQtdValue;
- }
- return kStatus_USB_Success;
- }
- static usb_status_t USB_HostEhciQhInit(usb_host_ehci_instance_t *ehciInstance, usb_host_ehci_pipe_t *ehciPipePointer)
- {
- usb_host_ehci_qh_t *qhPointer = NULL;
- uint32_t address = 0;
- uint32_t speed = 0;
- uint32_t portNumber = 0;
- uint32_t hubNumber = 0;
- ;
- uint32_t controlBits1 = 0U;
- uint32_t controlBits2 = 0U;
- /* get qh */
- USB_HostEhciLock();
- if (ehciInstance->ehciQhList != NULL)
- {
- qhPointer = (usb_host_ehci_qh_t *)ehciInstance->ehciQhList;
- ehciInstance->ehciQhList =
- (usb_host_ehci_qh_t *)(ehciInstance->ehciQhList->horizontalLinkPointer & EHCI_HOST_POINTER_ADDRESS_MASK);
- }
- USB_HostEhciUnlock();
- if (qhPointer == NULL)
- {
- #ifdef HOST_EHCO
- usb_echo("get qh error\r\n");
- #endif
- return kStatus_USB_Error;
- }
- ehciPipePointer->ehciQh = (void *)qhPointer;
- /* initialize qh */
- USB_HostEhciZeroMem((void *)qhPointer, sizeof(usb_host_ehci_qh_t) / 4U);
- qhPointer->horizontalLinkPointer = EHCI_HOST_T_INVALID_VALUE;
- qhPointer->currentQtdPointer = EHCI_HOST_T_INVALID_VALUE;
- qhPointer->nextQtdPointer = EHCI_HOST_T_INVALID_VALUE;
- qhPointer->alternateNextQtdPointer = EHCI_HOST_T_INVALID_VALUE;
- qhPointer->ehciPipePointer = ehciPipePointer;
- qhPointer->timeOutLabel = 0;
- qhPointer->timeOutValue = USB_HOST_EHCI_CONTROL_BULK_TIME_OUT_VALUE;
- (void)USB_HostHelperGetPeripheralInformation(ehciPipePointer->pipeCommon.deviceHandle,
- (uint32_t)kUSB_HostGetDeviceSpeed, &speed);
- /* initialize staticEndpointStates[0] */
- if (ehciPipePointer->pipeCommon.pipeType == USB_ENDPOINT_INTERRUPT)
- {
- /* Software should set the RL field to zero if the queue head is an interrupt endpoint. */
- controlBits1 |= ((0UL << EHCI_HOST_QH_RL_SHIFT) & EHCI_HOST_QH_RL_MASK);
- }
- else
- {
- if (ehciPipePointer->pipeCommon.nakCount >= 16U)
- {
- controlBits1 |= ((15UL << EHCI_HOST_QH_RL_SHIFT) & EHCI_HOST_QH_RL_MASK);
- }
- else
- {
- controlBits1 |=
- (((uint32_t)ehciPipePointer->pipeCommon.nakCount << EHCI_HOST_QH_RL_SHIFT) & EHCI_HOST_QH_RL_MASK);
- }
- }
- if (ehciPipePointer->pipeCommon.pipeType == USB_ENDPOINT_CONTROL)
- {
- if (speed != USB_SPEED_HIGH)
- {
- controlBits1 |= (1UL << EHCI_HOST_QH_C_SHIFT);
- }
- controlBits1 |= (1UL << EHCI_HOST_QH_DTC_SHIFT);
- }
- controlBits1 |= ((uint32_t)ehciPipePointer->pipeCommon.maxPacketSize << EHCI_HOST_QH_MAX_PACKET_LENGTH_SHIFT);
- controlBits1 |= (speed << EHCI_HOST_QH_EPS_SHIFT);
- controlBits1 |= ((uint32_t)ehciPipePointer->pipeCommon.endpointAddress << EHCI_HOST_QH_ENDPT_SHIFT);
- (void)USB_HostHelperGetPeripheralInformation(ehciPipePointer->pipeCommon.deviceHandle,
- (uint32_t)kUSB_HostGetDeviceAddress, &address);
- controlBits1 |= (address << EHCI_HOST_QH_DEVICE_ADDRESS_SHIFT);
- qhPointer->staticEndpointStates[0] = controlBits1;
- if (speed == USB_SPEED_HIGH)
- {
- controlBits2 |= ((uint32_t)ehciPipePointer->pipeCommon.numberPerUframe << EHCI_HOST_QH_MULT_SHIFT);
- }
- else
- {
- controlBits2 |= (0x00000001UL << EHCI_HOST_QH_MULT_SHIFT);
- }
- /*initialize staticEndpointStates[1] */
- if (speed != USB_SPEED_HIGH)
- {
- (void)USB_HostHelperGetPeripheralInformation(ehciPipePointer->pipeCommon.deviceHandle,
- (uint32_t)kUSB_HostGetDeviceHSHubNumber, &hubNumber);
- (void)USB_HostHelperGetPeripheralInformation(ehciPipePointer->pipeCommon.deviceHandle,
- (uint32_t)kUSB_HostGetDeviceHSHubPort, &portNumber);
- }
- else
- {
- (void)USB_HostHelperGetPeripheralInformation(ehciPipePointer->pipeCommon.deviceHandle,
- (uint32_t)kUSB_HostGetDeviceHubNumber, &hubNumber);
- (void)USB_HostHelperGetPeripheralInformation(ehciPipePointer->pipeCommon.deviceHandle,
- (uint32_t)kUSB_HostGetDevicePortNumber, &portNumber);
- }
- controlBits2 |= (portNumber << EHCI_HOST_QH_PORT_NUMBER_SHIFT);
- controlBits2 |= (hubNumber << EHCI_HOST_QH_HUB_ADDR_SHIFT);
- controlBits2 |= ((uint32_t)ehciPipePointer->uframeCmask << EHCI_HOST_QH_UFRAME_CMASK_SHIFT);
- controlBits2 |= ((uint32_t)ehciPipePointer->uframeSmask << EHCI_HOST_QH_UFRAME_SMASK_SHIFT);
- qhPointer->staticEndpointStates[1] = controlBits2;
- return kStatus_USB_Success;
- }
- static usb_status_t USB_HostEhciQhDeinit(usb_host_ehci_instance_t *ehciInstance, usb_host_ehci_pipe_t *ehciPipePointer)
- {
- usb_host_ehci_qh_t *qhPointer;
- qhPointer = (usb_host_ehci_qh_t *)ehciPipePointer->ehciQh;
- /* de-initialize qtd from qh */
- (void)USB_HostEhciQhQtdListDeinit(ehciInstance, ehciPipePointer);
- /* release QH */
- USB_HostEhciLock();
- qhPointer->horizontalLinkPointer = (uint32_t)ehciInstance->ehciQhList;
- ehciInstance->ehciQhList = qhPointer;
- USB_HostEhciUnlock();
- return kStatus_USB_Success;
- }
- static void USB_HostEhciAddQhToFrame(usb_host_ehci_instance_t *ehciInstance,
- uint32_t entryPointerValue,
- uint16_t framePos,
- uint16_t uframeInterval)
- {
- volatile uint32_t *frameEntryPointer;
- uint32_t frameEntryValue;
- void *temp;
- /* search for the inserting point by interval */
- temp = (void *)ehciInstance->ehciFrameList;
- frameEntryPointer = (volatile uint32_t *)(&((uint32_t *)temp)[framePos]);
- while (NULL != frameEntryPointer)
- {
- frameEntryValue = *frameEntryPointer;
- if (0U != (frameEntryValue & EHCI_HOST_T_INVALID_VALUE))
- {
- /* insert into the end */
- *((uint32_t *)entryPointerValue) = EHCI_HOST_T_INVALID_VALUE;
- *frameEntryPointer = (entryPointerValue | EHCI_HOST_POINTER_TYPE_QH);
- break;
- }
- if ((frameEntryValue & EHCI_HOST_POINTER_ADDRESS_MASK) == entryPointerValue)
- {
- return; /* has inserted */
- }
- if (((frameEntryValue & EHCI_HOST_POINTER_TYPE_MASK) == EHCI_HOST_POINTER_TYPE_QH) &&
- (((usb_host_ehci_qh_t *)(frameEntryValue & EHCI_HOST_POINTER_ADDRESS_MASK))
- ->ehciPipePointer->uframeInterval <= uframeInterval))
- {
- /* insert into this point */
- *((uint32_t *)entryPointerValue) = frameEntryValue;
- *frameEntryPointer = (entryPointerValue | EHCI_HOST_POINTER_TYPE_QH);
- return;
- }
- else
- {
- frameEntryPointer = (volatile uint32_t *)(frameEntryValue & EHCI_HOST_POINTER_ADDRESS_MASK);
- }
- }
- }
- static void USB_HostEhciRemoveFromFrame(usb_host_ehci_instance_t *ehciInstance,
- uint32_t entryPointerValue,
- uint16_t framePos)
- {
- volatile uint32_t *frameEntryPointer;
- uint32_t frameEntryValue;
- void *temp;
- /* search for the qh/itd/sitd entry */
- temp = (void *)ehciInstance->ehciFrameList;
- frameEntryPointer = (volatile uint32_t *)(&((uint32_t *)temp)[framePos]);
- while (NULL != frameEntryPointer)
- {
- frameEntryValue = *frameEntryPointer;
- if (0U != (frameEntryValue & EHCI_HOST_T_INVALID_VALUE))
- {
- return;
- }
- if ((frameEntryValue & EHCI_HOST_POINTER_ADDRESS_MASK) == entryPointerValue)
- {
- /* remove the entry */
- *frameEntryPointer = *((uint32_t *)entryPointerValue);
- break;
- }
- else
- {
- frameEntryPointer = (volatile uint32_t *)(frameEntryValue & EHCI_HOST_POINTER_ADDRESS_MASK);
- }
- }
- }
- #if ((defined USB_HOST_CONFIG_EHCI_MAX_SITD) && (USB_HOST_CONFIG_EHCI_MAX_SITD))
- static void USB_HostEhciLinkSitd(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_pipe_t *ehciPipePointer,
- void *startEntryPointer)
- {
- usb_host_ehci_iso_t *isoPointer = (usb_host_ehci_iso_t *)ehciPipePointer->ehciQh;
- usb_host_ehci_sitd_t *sitdPointer;
- uint32_t distance;
- uint32_t frameInterval;
- uint32_t shouldLinkFrame;
- uint32_t currentFrame;
- void *temp;
- frameInterval = ((uint32_t)ehciPipePointer->uframeInterval >> 3U);
- if (isoPointer->lastLinkFrame == 0xFFFFU) /* first link */
- {
- currentFrame = ((ehciInstance->ehciIpBase->FRINDEX & USB_HOST_EHCI_MAX_MICRFRAME_VALUE) >> 3U);
- currentFrame = ((uint32_t)(currentFrame + USB_HOST_EHCI_ISO_BOUNCE_FRAME_NUMBER) &
- (USB_HOST_EHCI_MAX_MICRFRAME_VALUE >> 3U)); /* add USB_HOST_EHCI_ISO_BOUNCE_FRAME_NUMBER */
- /* frame should align with interval */
- if (currentFrame <= ehciPipePointer->startFrame)
- {
- currentFrame = ehciPipePointer->startFrame;
- }
- else
- {
- currentFrame -= ehciPipePointer->startFrame;
- currentFrame = ((currentFrame + frameInterval - 1U) & (~(frameInterval - 1U)));
- currentFrame += ehciPipePointer->startFrame;
- }
- }
- else
- {
- shouldLinkFrame = isoPointer->lastLinkFrame + frameInterval; /* continuous next should link frame */
- if (shouldLinkFrame > USB_HOST_EHCI_MAX_MICRFRAME_VALUE >> 3U)
- {
- shouldLinkFrame = shouldLinkFrame - ((USB_HOST_EHCI_MAX_MICRFRAME_VALUE >> 3U) + 1U);
- }
- currentFrame = ((ehciInstance->ehciIpBase->FRINDEX & USB_HOST_EHCI_MAX_MICRFRAME_VALUE) >> 3U);
- distance =
- ((shouldLinkFrame + (USB_HOST_EHCI_MAX_MICRFRAME_VALUE >> 3U) + 1U - currentFrame) &
- (USB_HOST_EHCI_MAX_MICRFRAME_VALUE >> 3)); /* get the distance from shouldLinkFrame to currentFrame */
- /* shouldLinkFrame has add frameInterval, think about the align with interval, so here add (frameInterval *
- * 2) */
- if ((distance <=
- (USB_HOST_EHCI_ISO_BOUNCE_FRAME_NUMBER + frameInterval * USB_HOST_EHCI_ISO_MAX_CONTINUOUS_TRANSFER)) &&
- (distance > 0U))
- {
- currentFrame = shouldLinkFrame;
- }
- else /* re-link */
- {
- currentFrame =
- ((currentFrame + USB_HOST_EHCI_ISO_BOUNCE_FRAME_NUMBER) & (USB_HOST_EHCI_MAX_MICRFRAME_VALUE >> 3));
- /*if (currentFrame > (USB_HOST_EHCI_MAX_MICRFRAME_VALUE >> 3))
- {
- currentFrame = currentFrame - ((USB_HOST_EHCI_MAX_MICRFRAME_VALUE >> 3) + 1U);
- }*/
- /* frame should align with interval */
- if (currentFrame <= ehciPipePointer->startFrame)
- {
- currentFrame = ehciPipePointer->startFrame;
- }
- else
- {
- currentFrame -= ehciPipePointer->startFrame;
- currentFrame = ((currentFrame + frameInterval - 1U) & (~(frameInterval - 1U)));
- currentFrame += ehciPipePointer->startFrame;
- }
- }
- }
- if (currentFrame >= USB_HOST_CONFIG_EHCI_FRAME_LIST_SIZE) /* frame turn around */
- {
- shouldLinkFrame =
- (currentFrame - USB_HOST_CONFIG_EHCI_FRAME_LIST_SIZE); /* shouldLinkFrame means inserted frame position */
- }
- else
- {
- shouldLinkFrame = currentFrame; /* shouldLinkFrame means inserted frame position */
- }
- sitdPointer = (usb_host_ehci_sitd_t *)startEntryPointer;
- while (NULL != sitdPointer)
- {
- sitdPointer->frameEntryIndex = (uint16_t)shouldLinkFrame;
- /* add to frame list head */
- temp = (void *)ehciInstance->ehciFrameList;
- sitdPointer->nextLinkPointer = ((uint32_t *)temp)[shouldLinkFrame];
- ((uint32_t *)temp)[shouldLinkFrame] = ((uint32_t)sitdPointer | EHCI_HOST_POINTER_TYPE_SITD);
- if (sitdPointer->nextSitdIndex == 0xFFU) /* 0xFF is invalid value */
- {
- break;
- }
- sitdPointer = &(ehciInstance->ehciSitdIndexBase[sitdPointer->nextSitdIndex]); /* next sitd */
- shouldLinkFrame += frameInterval;
- currentFrame += frameInterval;
- if (shouldLinkFrame >= USB_HOST_CONFIG_EHCI_FRAME_LIST_SIZE)
- {
- shouldLinkFrame = (shouldLinkFrame - USB_HOST_CONFIG_EHCI_FRAME_LIST_SIZE);
- }
- }
- if (currentFrame > (USB_HOST_EHCI_MAX_MICRFRAME_VALUE >> 3))
- {
- currentFrame = currentFrame - ((USB_HOST_EHCI_MAX_MICRFRAME_VALUE >> 3) + 1U);
- }
- isoPointer->lastLinkFrame = (uint16_t)currentFrame; /* save the last link frame value */
- }
- static usb_status_t USB_HostEhciSitdArrayInit(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_pipe_t *ehciPipePointer,
- usb_host_transfer_t *transfer)
- {
- usb_host_ehci_iso_t *isoPointer;
- uint32_t sitdNumber = 0;
- usb_host_ehci_sitd_t *sitdPointer;
- uint32_t dataLength = 0;
- uint32_t sitdLength = 0;
- uint32_t dataBufferValue;
- uint32_t hubNumber = 0U;
- uint32_t portNumber = 0U;
- uint32_t address = 0U;
- uint32_t tmp;
- uint32_t *temp;
- uint32_t index;
- int32_t tempIndex;
- (void)USB_HostHelperGetPeripheralInformation(ehciPipePointer->pipeCommon.deviceHandle,
- (uint32_t)kUSB_HostGetDeviceAddress, &address);
- sitdNumber = ((transfer->transferLength - 1U + (ehciPipePointer->pipeCommon.maxPacketSize)) /
- (ehciPipePointer->pipeCommon.maxPacketSize));
- /* get sitd array */
- /* USB_HostEhciLock(); */
- if (ehciInstance->ehciSitdNumber >= sitdNumber)
- {
- sitdPointer = ehciInstance->ehciSitdList;
- transfer->union1.unitHead = (uint32_t)sitdPointer;
- for (index = 1U; index < sitdNumber; ++index)
- {
- /*misra 10.8*/
- tempIndex = (((usb_host_ehci_sitd_t *)(sitdPointer->nextLinkPointer & 0xFFFFFFFEU)) -
- ehciInstance->ehciSitdIndexBase);
- sitdPointer->nextSitdIndex = (uint8_t)tempIndex;
- sitdPointer = (usb_host_ehci_sitd_t *)(sitdPointer->nextLinkPointer & 0xFFFFFFFEU);
- }
- sitdPointer->nextSitdIndex = 0xFF;
- ehciInstance->ehciSitdList = (usb_host_ehci_sitd_t *)(sitdPointer->nextLinkPointer & 0xFFFFFFFEU);
- ehciInstance->ehciSitdNumber -= (uint8_t)sitdNumber;
- }
- else
- {
- /* USB_HostEhciUnlock(); */
- return kStatus_USB_Error;
- }
- /* USB_HostEhciUnlock(); */
- transfer->union2.unitTail = (uint32_t)sitdPointer;
- /* initialize sitd array */
- (void)USB_HostHelperGetPeripheralInformation(ehciPipePointer->pipeCommon.deviceHandle,
- (uint32_t)kUSB_HostGetDeviceHubNumber, &hubNumber);
- (void)USB_HostHelperGetPeripheralInformation(ehciPipePointer->pipeCommon.deviceHandle,
- (uint32_t)kUSB_HostGetDevicePortNumber, &portNumber);
- sitdPointer = (usb_host_ehci_sitd_t *)transfer->union1.unitHead;
- dataLength = transfer->transferLength;
- while (0U != sitdNumber)
- {
- sitdNumber--;
- USB_HostEhciZeroMem((void *)sitdPointer, 7);
- sitdLength = dataLength;
- if (sitdLength > ehciPipePointer->pipeCommon.maxPacketSize)
- {
- sitdLength = ehciPipePointer->pipeCommon.maxPacketSize;
- }
- dataBufferValue = (uint32_t)(transfer->transferBuffer + (transfer->transferLength - dataLength));
- dataLength -= sitdLength; /* update left data length */
- sitdPointer->transferResults[1] = dataBufferValue;
- sitdPointer->transferResults[2] = ((dataBufferValue + 4U * 1024U) & 0xFFFFF000U);
- sitdPointer->endpointStates[0] =
- (((uint32_t)ehciPipePointer->pipeCommon.direction << EHCI_HOST_SITD_DIRECTION_SHIFT) |
- (portNumber << EHCI_HOST_SITD_PORT_NUMBER_SHIFT) | (hubNumber << EHCI_HOST_SITD_HUB_ADDR_SHIFT) |
- ((uint32_t)ehciPipePointer->pipeCommon.endpointAddress << EHCI_HOST_SITD_ENDPT_SHIFT) |
- (address << EHCI_HOST_SITD_DEVICE_ADDRESS_SHIFT));
- sitdPointer->transferResults[0] =
- ((sitdLength << EHCI_HOST_SITD_TOTAL_BYTES_SHIFT) | (EHCI_HOST_SITD_STATUS_ACTIVE_MASK));
- if (ehciInstance->firstDeviceSpeed == USB_SPEED_HIGH)
- {
- sitdPointer->endpointStates[1] = (((uint32_t)ehciPipePointer->uframeCmask << EHCI_HOST_SITD_CMASK_SHIFT) |
- ((uint32_t)ehciPipePointer->uframeSmask << EHCI_HOST_SITD_SMASK_SHIFT));
- tmp = (sitdLength + 187U) / 188U;
- if (tmp > 1U)
- {
- sitdPointer->transferResults[2] |= (0x01U << EHCI_HOST_SITD_TP_SHIFT); /* for iso split */
- }
- else
- {
- sitdPointer->transferResults[2] |= (0x00U << EHCI_HOST_SITD_TP_SHIFT); /* for iso split */
- }
- sitdPointer->transferResults[2] |= (tmp << EHCI_HOST_SITD_TCOUNT_SHIFT); /* for iso split */
- }
- sitdPointer->backPointer = EHCI_HOST_T_INVALID_VALUE;
- sitdPointer = (ehciInstance->ehciSitdIndexBase + sitdPointer->nextSitdIndex);
- }
- sitdPointer = (usb_host_ehci_sitd_t *)transfer->union2.unitTail;
- sitdPointer->transferResults[0] |= (1UL << EHCI_HOST_SITD_IOC_SHIFT); /* last set IOC */
- /* link transfer to usb_host_ehci_iso_t transfer list */
- isoPointer = (usb_host_ehci_iso_t *)ehciPipePointer->ehciQh;
- USB_HostEhciLock();
- if (isoPointer->ehciTransferHead == NULL)
- {
- transfer->next = NULL;
- isoPointer->ehciTransferTail = transfer;
- isoPointer->ehciTransferHead = transfer;
- }
- else
- {
- transfer->next = NULL;
- isoPointer->ehciTransferTail->next = transfer;
- isoPointer->ehciTransferTail = transfer;
- }
- USB_HostEhciUnlock();
- /* link itd to frame list (note: initialize frameEntryIndex)*/
- /*misra 11.6*/
- temp = (uint32_t *)(transfer->union1.unitHead);
- USB_HostEhciLinkSitd(ehciInstance, ehciPipePointer, (void *)temp);
- return kStatus_USB_Success;
- }
- static uint32_t USB_HostEhciSitdArrayRelease(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_sitd_t *startSitdPointer,
- usb_host_ehci_sitd_t *endSitdPointer)
- {
- usb_host_ehci_sitd_t *sitdPointer = startSitdPointer;
- uint32_t leftLength = 0;
- /* remove itd from frame list */
- while (1U == 1U)
- {
- /* record the transfer's result length */
- leftLength +=
- ((sitdPointer->transferResults[0] & EHCI_HOST_SITD_TOTAL_BYTES_MASK) >> EHCI_HOST_SITD_TOTAL_BYTES_SHIFT);
- USB_HostEhciRemoveFromFrame(ehciInstance, (uint32_t)sitdPointer,
- sitdPointer->frameEntryIndex); /* remove from the inserted frame list */
- /* release itd */
- /* USB_HostEhciLock(); */
- /*set next link pointer to invalid in case hardware access invalid sitd structure in special case*/
- sitdPointer->nextLinkPointer = (((uint32_t)ehciInstance->ehciSitdList) | EHCI_HOST_T_INVALID_VALUE);
- ehciInstance->ehciSitdList = sitdPointer;
- ehciInstance->ehciSitdNumber++;
- /* USB_HostEhciUnlock(); */
- if (sitdPointer == endSitdPointer)
- {
- break;
- }
- sitdPointer = &(ehciInstance->ehciSitdIndexBase[sitdPointer->nextSitdIndex]);
- }
- return leftLength;
- }
- static usb_status_t USB_HostEhciSitdArrayDeinit(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_pipe_t *ehciPipePointer)
- {
- usb_host_ehci_iso_t *isoPointer = (usb_host_ehci_iso_t *)ehciPipePointer->ehciQh;
- usb_host_transfer_t *transfer;
- usb_host_transfer_t *nextTransfer;
- /* firstly remove the transfer (because host task may occupy to access the resource) */
- USB_HostEhciLock();
- transfer = isoPointer->ehciTransferHead;
- isoPointer->ehciTransferTail = NULL;
- isoPointer->ehciTransferHead = NULL;
- USB_HostEhciUnlock();
- while (transfer != NULL)
- {
- nextTransfer = transfer->next;
- /* remove sitd from frame list and release itd */
- transfer->transferSofar =
- transfer->transferLength - USB_HostEhciSitdArrayRelease(ehciInstance,
- (usb_host_ehci_sitd_t *)transfer->union1.unitHead,
- (usb_host_ehci_sitd_t *)transfer->union2.unitTail);
- /* callback function is different from the current condition */
- transfer->callbackFn(transfer->callbackParam, transfer, kStatus_USB_TransferCancel);
- /* next transfer */
- transfer = nextTransfer;
- }
- return kStatus_USB_Success;
- }
- #endif /* USB_HOST_CONFIG_EHCI_MAX_SITD */
- #if ((defined USB_HOST_CONFIG_EHCI_MAX_ITD) && (USB_HOST_CONFIG_EHCI_MAX_ITD))
- static uint32_t USB_HostEhciGetItdLinkFrame(usb_host_ehci_instance_t *ehciInstance,
- uint32_t lastLinkUframe,
- uint16_t startUframe,
- uint16_t uframeInterval)
- {
- uint32_t shouldLinkUframe;
- uint32_t currentUframe;
- uint32_t distance;
- if (lastLinkUframe != 0xFFFFU)
- {
- shouldLinkUframe = lastLinkUframe + uframeInterval;
- if (shouldLinkUframe > USB_HOST_EHCI_MAX_MICRFRAME_VALUE)
- {
- shouldLinkUframe = shouldLinkUframe - (USB_HOST_EHCI_MAX_MICRFRAME_VALUE + 1U);
- }
- currentUframe = (ehciInstance->ehciIpBase->FRINDEX & USB_HOST_EHCI_MAX_MICRFRAME_VALUE);
- distance = ((shouldLinkUframe + USB_HOST_EHCI_MAX_MICRFRAME_VALUE + 1U - currentUframe) &
- USB_HOST_EHCI_MAX_MICRFRAME_VALUE); /* get the distance */
- /* shouldLinkUframe has add uframeInterval, think about the align with interval, so here add (uframeInterval
- * * 2) */
- if ((distance <= ((uint32_t)USB_HOST_EHCI_ISO_BOUNCE_UFRAME_NUMBER +
- ((uint32_t)uframeInterval * USB_HOST_EHCI_ISO_MAX_CONTINUOUS_TRANSFER))) &&
- (distance > 2U))
- {
- currentUframe = shouldLinkUframe;
- }
- else /* re-link */
- {
- currentUframe =
- ((currentUframe + USB_HOST_EHCI_ISO_BOUNCE_UFRAME_NUMBER) & USB_HOST_EHCI_MAX_MICRFRAME_VALUE);
- /*if (currentUframe > USB_HOST_EHCI_MAX_MICRFRAME_VALUE)
- {
- currentUframe = currentUframe - (USB_HOST_EHCI_MAX_MICRFRAME_VALUE + 1U);
- }*/
- /* uframe should align with interval */
- if (currentUframe <= startUframe)
- {
- currentUframe = startUframe;
- }
- else
- {
- currentUframe -= startUframe;
- currentUframe = ((uint32_t)(currentUframe + uframeInterval) &
- (~((uint32_t)uframeInterval - 1U))); /* uframeInterval is power of 2 */
- currentUframe += startUframe;
- }
- }
- }
- else
- {
- currentUframe = (ehciInstance->ehciIpBase->FRINDEX & USB_HOST_EHCI_MAX_MICRFRAME_VALUE);
- currentUframe = ((currentUframe + USB_HOST_EHCI_ISO_BOUNCE_UFRAME_NUMBER) & USB_HOST_EHCI_MAX_MICRFRAME_VALUE);
- /* uframe should align with interval */
- if (currentUframe <= startUframe)
- {
- currentUframe = startUframe;
- }
- else
- {
- currentUframe -= startUframe;
- currentUframe =
- ((currentUframe + uframeInterval) & (~(uframeInterval - 1U))); /* uframeInterval is power of 2 */
- currentUframe += startUframe;
- }
- }
- return currentUframe;
- }
- static usb_status_t USB_HostEhciItdArrayInit(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_pipe_t *ehciPipePointer,
- usb_host_transfer_t *transfer)
- {
- usb_host_ehci_iso_t *isoPointer;
- usb_host_ehci_itd_t *itdPointer = NULL;
- usb_host_ehci_itd_t *itdHead = NULL;
- usb_host_ehci_itd_t *tmpItdPointer;
- uint32_t dataLength; /* the remaining data for sending */
- uint32_t transactionLength; /* the initializing transaction descriptor data length */
- uint32_t itdBufferValue;
- uint32_t itdBufferBaseValue; /* for calculating PG value */
- uint32_t address = 0U;
- uint32_t lastShouldLinkUframe;
- uint32_t linkUframe;
- uint32_t minDataPerItd =
- (uint32_t)ehciPipePointer->pipeCommon.numberPerUframe * ehciPipePointer->pipeCommon.maxPacketSize;
- uint8_t maxItdNumber;
- uint16_t index = 0;
- isoPointer = (usb_host_ehci_iso_t *)ehciPipePointer->ehciQh;
- (void)USB_HostHelperGetPeripheralInformation(ehciPipePointer->pipeCommon.deviceHandle,
- (uint32_t)kUSB_HostGetDeviceAddress, &address);
- /* max needed itd number, the actual needed number may be less because micro-frame interval may be less than 8 */
- maxItdNumber = (uint8_t)((transfer->transferLength - 1U + minDataPerItd) / minDataPerItd);
- if (ehciPipePointer->uframeInterval < 8U)
- {
- maxItdNumber = (uint8_t)((maxItdNumber * ehciPipePointer->uframeInterval + 7U) / 8U) + 1U;
- }
- if (maxItdNumber > ehciInstance->ehciItdNumber)
- {
- return kStatus_USB_Error;
- }
- /* link transfer to usb_host_ehci_iso_t transfer list */
- transfer->next = NULL;
- /* USB_HostEhciLock(); */
- if (isoPointer->ehciTransferHead == NULL)
- {
- isoPointer->ehciTransferTail = transfer;
- isoPointer->ehciTransferHead = transfer;
- }
- else
- {
- isoPointer->ehciTransferTail->next = transfer;
- isoPointer->ehciTransferTail = transfer;
- }
- /* USB_HostEhciUnlock(); */
- dataLength = transfer->transferLength;
- transfer->union1.unitHead = 0U;
- /* get the link micro-frame */
- lastShouldLinkUframe = USB_HostEhciGetItdLinkFrame(
- ehciInstance, isoPointer->lastLinkFrame,
- (uint16_t)((ehciPipePointer->startFrame << 3) + ehciPipePointer->startUframe), ehciPipePointer->uframeInterval);
- if (lastShouldLinkUframe > USB_HOST_EHCI_MAX_MICRFRAME_VALUE)
- {
- linkUframe = lastShouldLinkUframe - (USB_HOST_EHCI_MAX_MICRFRAME_VALUE + 1U);
- }
- else
- {
- linkUframe = lastShouldLinkUframe;
- }
- itdHead = ehciInstance->ehciItdList;
- while (0U != dataLength)
- {
- /* get one idle itd */
- tmpItdPointer = ehciInstance->ehciItdList;
- if (tmpItdPointer == NULL)
- {
- return kStatus_USB_Error; /* this should not reach */
- }
- ehciInstance->ehciItdList = (usb_host_ehci_itd_t *)tmpItdPointer->nextItdPointer;
- ehciInstance->ehciItdNumber -= 1U;
- tmpItdPointer->nextItdPointer = NULL;
- /* use the itd */
- if (transfer->union1.unitHead == 0U) /* first itd */
- {
- transfer->union1.unitHead = (uint32_t)tmpItdPointer;
- }
- else /* link itd list */
- {
- itdPointer->nextItdPointer = tmpItdPointer;
- }
- itdPointer = tmpItdPointer;
- /* itd has been set to all zero when releasing */
- itdBufferValue = (uint32_t)(transfer->transferBuffer + (transfer->transferLength - dataLength));
- itdBufferBaseValue = itdBufferValue;
- for (index = 0; index < 7U; ++index)
- {
- itdPointer->bufferPointers[index] = ((itdBufferBaseValue + ((uint32_t)index * 4U * 1024U)) & 0xFFFFF000U);
- }
- /* initialize iTD common fields */
- itdPointer->bufferPointers[0] |=
- (((uint32_t)ehciPipePointer->pipeCommon.endpointAddress << EHCI_HOST_ITD_ENDPT_SHIFT) |
- (address << EHCI_HOST_ITD_DEVICE_ADDRESS_SHIFT));
- itdPointer->bufferPointers[1] |=
- (((uint32_t)ehciPipePointer->pipeCommon.direction << EHCI_HOST_ITD_DIRECTION_SHIFT) |
- ((uint32_t)ehciPipePointer->pipeCommon.maxPacketSize << EHCI_HOST_ITD_MAX_PACKET_SIZE_SHIFT));
- itdPointer->bufferPointers[2] |= (ehciPipePointer->pipeCommon.numberPerUframe);
- /* initialize transaction descriptors */
- for (index = (uint8_t)(linkUframe & 0x0007U); index < 8U; index += ehciPipePointer->uframeInterval)
- {
- transactionLength = ((dataLength > minDataPerItd) ? minDataPerItd : dataLength);
- /* initialize the uframeIndex's transaction descriptor in itd */
- itdPointer->transactions[index] =
- ((EHCI_HOST_ITD_STATUS_ACTIVE_MASK) | (transactionLength << EHCI_HOST_ITD_TRANSACTION_LEN_SHIFT) |
- ((((itdBufferValue & 0xFFFFF000U) - (itdBufferBaseValue & 0xFFFFF000U)) >>
- EHCI_HOST_ITD_BUFFER_POINTER_SHIFT)
- << EHCI_HOST_ITD_PG_SHIFT) |
- (itdBufferValue & EHCI_HOST_ITD_TRANSACTION_OFFSET_MASK));
- dataLength -= transactionLength;
- itdBufferValue += transactionLength;
- if (dataLength <= 0U)
- {
- break;
- }
- }
- }
- transfer->union2.unitTail = (uint32_t)itdPointer;
- itdPointer->transactions[index] |= (1UL << EHCI_HOST_ITD_IOC_SHIFT); /* last set IOC */
- itdPointer = itdHead;
- /* link itd to frame list (note: initialize frameEntryIndex)*/
- while (NULL != itdPointer)
- {
- void *temp = (void *)ehciInstance->ehciFrameList;
- uint32_t *linkPointer = &((uint32_t *)temp)[linkUframe >> 3];
- uint32_t linkValue = *linkPointer;
- itdPointer->frameEntryIndex = linkUframe >> 3;
- while ((0U == (linkValue & EHCI_HOST_T_INVALID_VALUE)) &&
- ((linkValue & EHCI_HOST_POINTER_TYPE_MASK) == EHCI_HOST_POINTER_TYPE_ITD))
- {
- linkPointer = (uint32_t *)(linkValue & EHCI_HOST_POINTER_ADDRESS_MASK);
- linkValue = *linkPointer;
- }
- itdPointer->nextLinkPointer = *linkPointer;
- *linkPointer = ((uint32_t)itdPointer | EHCI_HOST_POINTER_TYPE_ITD);
- itdPointer = itdPointer->nextItdPointer;
- if (itdPointer == NULL)
- {
- break;
- }
- linkUframe += ehciPipePointer->uframeInterval;
- lastShouldLinkUframe += ehciPipePointer->uframeInterval;
- if (linkUframe >= (USB_HOST_CONFIG_EHCI_FRAME_LIST_SIZE << 3))
- {
- linkUframe = (linkUframe - (USB_HOST_CONFIG_EHCI_FRAME_LIST_SIZE << 3));
- }
- }
- if (lastShouldLinkUframe > USB_HOST_EHCI_MAX_MICRFRAME_VALUE)
- {
- lastShouldLinkUframe = lastShouldLinkUframe - (USB_HOST_EHCI_MAX_MICRFRAME_VALUE + 1U);
- }
- isoPointer->lastLinkFrame = (uint16_t)lastShouldLinkUframe;
- return kStatus_USB_Success;
- }
- static uint32_t USB_HostEhciItdArrayRelease(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_itd_t *startItdPointer,
- usb_host_ehci_itd_t *endItdPointer)
- {
- usb_host_ehci_itd_t *itdPointer = startItdPointer;
- uint8_t index;
- uint32_t doneLength = 0;
- /* remove itd from frame list */
- while (1U == 1U)
- {
- /* record the transfer's result length */
- for (index = 0U; index < 8U; ++index)
- {
- doneLength += ((itdPointer->transactions[index] & EHCI_HOST_ITD_TRANSACTION_LEN_MASK) >>
- EHCI_HOST_ITD_TRANSACTION_LEN_SHIFT);
- }
- USB_HostEhciRemoveFromFrame(ehciInstance, (uint32_t)itdPointer,
- (uint16_t)itdPointer->frameEntryIndex); /* remove from the inserted frame list */
- /* release itd */
- /* USB_HostEhciLock(); */
- /*set next link pointer to invalid in case hardware access invalid itd structure in special case*/
- itdPointer->nextLinkPointer = EHCI_HOST_T_INVALID_VALUE;
- itdPointer->nextItdPointer = (usb_host_ehci_itd_t *)ehciInstance->ehciItdList;
- ehciInstance->ehciItdList = itdPointer;
- ehciInstance->ehciItdNumber++;
- /* USB_HostEhciUnlock(); */
- if (itdPointer == endItdPointer)
- {
- break;
- }
- itdPointer = itdPointer->nextItdPointer;
- }
- return doneLength;
- }
- static usb_status_t USB_HostEhciItdArrayDeinit(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_pipe_t *ehciPipePointer)
- {
- usb_host_ehci_iso_t *isoPointer = (usb_host_ehci_iso_t *)ehciPipePointer->ehciQh;
- usb_host_transfer_t *transfer;
- usb_host_transfer_t *nextTransfer;
- uint32_t doneLength = 0;
- /* firstly remove the transfer (because host task may occupy to access the resource) */
- USB_HostEhciLock();
- transfer = isoPointer->ehciTransferHead;
- isoPointer->ehciTransferTail = NULL;
- isoPointer->ehciTransferHead = NULL;
- USB_HostEhciUnlock();
- while (transfer != NULL)
- {
- nextTransfer = transfer->next;
- doneLength = 0;
- /* remove itd from frame list and release itd */
- doneLength = USB_HostEhciItdArrayRelease(ehciInstance, (usb_host_ehci_itd_t *)transfer->union1.unitHead,
- (usb_host_ehci_itd_t *)transfer->union2.unitTail);
- /* transfer callback */
- if (ehciPipePointer->pipeCommon.direction == USB_OUT)
- {
- doneLength = transfer->transferLength;
- }
- transfer->transferSofar = doneLength;
- /* callback function is different from the current condition */
- transfer->callbackFn(transfer->callbackParam, transfer, kStatus_USB_TransferCancel);
- /* next transfer */
- transfer = nextTransfer;
- }
- return kStatus_USB_Success;
- }
- #endif /* USB_HOST_CONFIG_EHCI_MAX_ITD */
- static usb_status_t USB_HostEhciOpenControlBulk(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_pipe_t *ehciPipePointer)
- {
- usb_host_ehci_qh_t *qhPointer;
- if (USB_HostEhciQhInit(ehciInstance, ehciPipePointer) != kStatus_USB_Success) /* initialize control/bulk qh */
- {
- return kStatus_USB_Error;
- }
- qhPointer = (usb_host_ehci_qh_t *)ehciPipePointer->ehciQh;
- /* add qh to async */
- qhPointer->horizontalLinkPointer = ehciInstance->shedFirstQh->horizontalLinkPointer;
- ehciInstance->shedFirstQh->horizontalLinkPointer = ((uint32_t)qhPointer | EHCI_HOST_POINTER_TYPE_QH);
- return kStatus_USB_Success;
- }
- static usb_status_t USB_HostEhciCloseControlBulk(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_pipe_t *ehciPipePointer)
- {
- volatile usb_host_ehci_qh_t *vltPrevQhPointer;
- uint32_t horizontalLinkValue;
- uint32_t *temp;
- /* remove qh from async schedule */
- temp = (uint32_t *)ehciPipePointer->ehciQh;
- if ((ehciInstance->shedFirstQh->horizontalLinkPointer & EHCI_HOST_POINTER_ADDRESS_MASK) ==
- (uint32_t)temp) /* the removing qh is the first qh in the async list */
- {
- USB_HostEhciStopAsync(ehciInstance);
- ehciInstance->shedFirstQh->horizontalLinkPointer =
- ((usb_host_ehci_qh_t *)ehciPipePointer->ehciQh)->horizontalLinkPointer;
- USB_HostEhciStartAsync(ehciInstance);
- }
- else
- {
- /* search for the removing qh from the async list */
- vltPrevQhPointer = ehciInstance->shedFirstQh;
- while (vltPrevQhPointer != NULL)
- {
- horizontalLinkValue = vltPrevQhPointer->horizontalLinkPointer;
- if ((0U != (horizontalLinkValue & EHCI_HOST_T_INVALID_VALUE)) ||
- ((horizontalLinkValue & EHCI_HOST_POINTER_ADDRESS_MASK) == (uint32_t)temp) ||
- ((horizontalLinkValue & EHCI_HOST_POINTER_ADDRESS_MASK) == (uint32_t)ehciInstance->shedFirstQh))
- {
- break;
- }
- vltPrevQhPointer = (volatile usb_host_ehci_qh_t *)(horizontalLinkValue & EHCI_HOST_POINTER_ADDRESS_MASK);
- }
- /* remove the qh from async list */
- /*for misra 11.6*/
- temp = (uint32_t *)ehciPipePointer->ehciQh;
- if ((vltPrevQhPointer != NULL) && (0U == (horizontalLinkValue & EHCI_HOST_T_INVALID_VALUE)) &&
- ((horizontalLinkValue & EHCI_HOST_POINTER_ADDRESS_MASK) == (uint32_t)temp))
- {
- USB_HostEhciStopAsync(ehciInstance);
- vltPrevQhPointer->horizontalLinkPointer =
- ((usb_host_ehci_qh_t *)ehciPipePointer->ehciQh)->horizontalLinkPointer;
- USB_HostEhciStartAsync(ehciInstance);
- }
- }
- ((usb_host_ehci_qh_t *)ehciPipePointer->ehciQh)->horizontalLinkPointer =
- EHCI_HOST_T_INVALID_VALUE; /* invalid next qh link */
- return USB_HostEhciQhDeinit(ehciInstance, ehciPipePointer); /* de-initialize qh and release qh */
- }
- static usb_status_t USB_HostEhciOpenInterrupt(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_pipe_t *ehciPipePointer)
- {
- usb_status_t status = kStatus_USB_Success;
- uint32_t frameIndex;
- uint32_t *temp;
- /* allocate bandwidth */
- if (ehciInstance->firstDeviceSpeed == USB_SPEED_HIGH)
- {
- status = USB_HostBandwidthHsHostAllocateInterrupt(ehciInstance, ehciPipePointer); /* host works as high-speed */
- }
- else
- {
- status = USB_HostBandwidthFslsHostAllocate(ehciInstance,
- ehciPipePointer); /* host works as full-speed or low-speed */
- }
- if (status != kStatus_USB_Success)
- {
- return status;
- }
- if (USB_HostEhciQhInit(ehciInstance, ehciPipePointer) != kStatus_USB_Success)
- {
- return kStatus_USB_Error;
- }
- /* insert QH to frame list */
- for (frameIndex = ehciPipePointer->startFrame; frameIndex < USB_HOST_CONFIG_EHCI_FRAME_LIST_SIZE;
- frameIndex += (((uint32_t)ehciPipePointer->uframeInterval + 7U) / 8U))
- {
- temp = (uint32_t *)ehciPipePointer->ehciQh;
- USB_HostEhciAddQhToFrame(ehciInstance, (uint32_t)temp, (uint16_t)frameIndex, ehciPipePointer->uframeInterval);
- }
- return kStatus_USB_Success;
- }
- static usb_status_t USB_HostEhciCloseInterrupt(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_pipe_t *ehciPipePointer)
- {
- uint32_t frameIndex;
- uint32_t *temp;
- /* remove from frame list */
- for (frameIndex = ehciPipePointer->startFrame; frameIndex < USB_HOST_CONFIG_EHCI_FRAME_LIST_SIZE;
- frameIndex += (((uint32_t)ehciPipePointer->uframeInterval + 7U) / 8U))
- {
- temp = (uint32_t *)ehciPipePointer->ehciQh;
- USB_HostEhciRemoveFromFrame(ehciInstance, (uint32_t)temp, (uint16_t)frameIndex);
- }
- ((usb_host_ehci_qh_t *)ehciPipePointer->ehciQh)->horizontalLinkPointer |=
- EHCI_HOST_T_INVALID_VALUE; /* invalid next qh link */
- return USB_HostEhciQhDeinit(ehciInstance, ehciPipePointer); /* de-initilaze qh and release qh */
- }
- #if (((defined USB_HOST_CONFIG_EHCI_MAX_ITD) && (USB_HOST_CONFIG_EHCI_MAX_ITD)) || \
- ((defined USB_HOST_CONFIG_EHCI_MAX_SITD) && (USB_HOST_CONFIG_EHCI_MAX_SITD)))
- static usb_status_t USB_HostEhciOpenIso(usb_host_ehci_instance_t *ehciInstance, usb_host_ehci_pipe_t *ehciPipePointer)
- {
- usb_host_ehci_iso_t *isoPointer;
- usb_status_t status = kStatus_USB_Success;
- if (ehciInstance->firstDeviceSpeed == USB_SPEED_HIGH)
- {
- status = USB_HostBandwidthHsHostAllocateIso(
- ehciInstance, ehciPipePointer); /* allocate iso bandwidth when host works as high-speed */
- }
- else
- {
- status = USB_HostBandwidthFslsHostAllocate(
- ehciInstance, ehciPipePointer); /* allocate iso bandwidth when host works as full-speed or low-speed */
- }
- if (status != kStatus_USB_Success)
- {
- return status;
- }
- /* get usb_host_ehci_iso_t */
- if (ehciInstance->ehciIsoList == NULL)
- {
- return kStatus_USB_Error;
- }
- USB_HostEhciLock();
- isoPointer = ehciInstance->ehciIsoList;
- ehciInstance->ehciIsoList = ehciInstance->ehciIsoList->next;
- USB_HostEhciUnlock();
- isoPointer->lastLinkFrame = 0xFFFF;
- ehciPipePointer->ehciQh = isoPointer;
- return status;
- }
- static usb_status_t USB_HostEhciCloseIso(usb_host_ehci_instance_t *ehciInstance, usb_host_ehci_pipe_t *ehciPipePointer)
- {
- usb_host_ehci_iso_t *isoPointer;
- uint32_t speed = 0U;
- isoPointer = (usb_host_ehci_iso_t *)ehciPipePointer->ehciQh;
- if (isoPointer->ehciTransferHead != NULL)
- {
- (void)USB_HostHelperGetPeripheralInformation(ehciPipePointer->pipeCommon.deviceHandle,
- (uint32_t)kUSB_HostGetDeviceSpeed, &speed);
- if (speed == USB_SPEED_HIGH)
- {
- #if ((defined USB_HOST_CONFIG_EHCI_MAX_ITD) && (USB_HOST_CONFIG_EHCI_MAX_ITD))
- (void)USB_HostEhciItdArrayDeinit(ehciInstance, ehciPipePointer); /* de-initialize itd list and free them */
- #endif
- }
- else
- {
- #if ((defined USB_HOST_CONFIG_EHCI_MAX_SITD) && (USB_HOST_CONFIG_EHCI_MAX_SITD))
- (void)USB_HostEhciSitdArrayDeinit(ehciInstance,
- ehciPipePointer); /* de-initialize sitd list and free them */
- #endif
- }
- }
- /* release usb_host_ehci_iso_t */
- USB_HostEhciLock();
- isoPointer->next = ehciInstance->ehciIsoList;
- ehciInstance->ehciIsoList = isoPointer;
- USB_HostEhciUnlock();
- return kStatus_USB_Success;
- }
- #endif
- static usb_status_t USB_HostEhciResetIP(usb_host_ehci_instance_t *ehciInstance)
- {
- /* reset controller */
- ehciInstance->ehciIpBase->USBCMD = USBHS_USBCMD_RST_MASK;
- while (0U != (ehciInstance->ehciIpBase->USBCMD & USBHS_USBCMD_RST_MASK))
- {
- }
- /* set host mode */
- #if (ENDIANNESS == USB_LITTLE_ENDIAN)
- ehciInstance->ehciIpBase->USBMODE |= 0x03U;
- #else
- ehciInstance->ehciIpBase->USBMODE |= (0x03U | (0x01U << USBHS_USBMODE_ES_SHIFT));
- #endif
- /* check frame list size */
- if (0U == (ehciInstance->ehciIpBase->HCCPARAMS & USBHS_HCCPARAMS_PFL_MASK))
- {
- #if ((USB_HOST_CONFIG_EHCI_FRAME_LIST_SIZE < 8) || (USB_HOST_CONFIG_EHCI_FRAME_LIST_SIZE > 1024))
- return kStatus_USB_Error;
- #endif
- #if (USB_HOST_CONFIG_EHCI_FRAME_LIST_SIZE & (USB_HOST_CONFIG_EHCI_FRAME_LIST_SIZE - 1))
- return kStatus_USB_Error; /* frame size must be 1024/512/256/128/64/32/16/8 */
- #endif
- }
- return kStatus_USB_Success;
- }
- static usb_status_t USB_HostEhciStartIP(usb_host_ehci_instance_t *ehciInstance)
- {
- uint32_t tmp = 0;
- if (0U != (ehciInstance->ehciIpBase->HCSPARAMS & USBHS_HCSPARAMS_PPC_MASK)) /* Ports have power port switches */
- {
- /* only has one port */
- tmp = ehciInstance->ehciIpBase->PORTSC1;
- tmp &= (~EHCI_PORTSC1_W1_BITS);
- ehciInstance->ehciIpBase->PORTSC1 = (tmp | USBHS_PORTSC1_PP_MASK); /* turn on port power */
- }
- /* set frame list size */
- if (0U != (ehciInstance->ehciIpBase->HCCPARAMS & USBHS_HCCPARAMS_PFL_MASK))
- {
- #if (USB_HOST_CONFIG_EHCI_FRAME_LIST_SIZE <= 64)
- ehciInstance->ehciIpBase->USBCMD |= (USBHS_USBCMD_FS2_MASK);
- #if (USB_HOST_CONFIG_EHCI_FRAME_LIST_SIZE == 64)
- ehciInstance->ehciIpBase->USBCMD |= (0x00U << USBHS_USBCMD_FS_SHIFT);
- #elif (USB_HOST_CONFIG_EHCI_FRAME_LIST_SIZE == 32)
- ehciInstance->ehciIpBase->USBCMD |= (0x01U << USBHS_USBCMD_FS_SHIFT);
- #elif (USB_HOST_CONFIG_EHCI_FRAME_LIST_SIZE == 16)
- ehciInstance->ehciIpBase->USBCMD |= (0x02U << USBHS_USBCMD_FS_SHIFT);
- #elif (USB_HOST_CONFIG_EHCI_FRAME_LIST_SIZE == 8)
- ehciInstance->ehciIpBase->USBCMD |= (0x03U << USBHS_USBCMD_FS_SHIFT);
- #endif
- #else
- #if (USB_HOST_CONFIG_EHCI_FRAME_LIST_SIZE == 1024)
- ehciInstance->ehciIpBase->USBCMD |= (0x00U << USBHS_USBCMD_FS_SHIFT);
- #elif (USB_HOST_CONFIG_EHCI_FRAME_LIST_SIZE == 512)
- ehciInstance->ehciIpBase->USBCMD |= (0x01U << USBHS_USBCMD_FS_SHIFT);
- #elif (USB_HOST_CONFIG_EHCI_FRAME_LIST_SIZE == 256)
- ehciInstance->ehciIpBase->USBCMD |= (0x02U << USBHS_USBCMD_FS_SHIFT);
- #elif (USB_HOST_CONFIG_EHCI_FRAME_LIST_SIZE == 128)
- ehciInstance->ehciIpBase->USBCMD |= (0x03U << USBHS_USBCMD_FS_SHIFT);
- #endif
- #endif
- }
- /* start the controller */
- ehciInstance->ehciIpBase->USBCMD |= USBHS_USBCMD_RS_MASK;
- /* set timer0 */
- ehciInstance->ehciIpBase->GPTIMER0LD = (100U * 1000U - 1U); /* 100ms */
- /* enable interrupt (USB interrupt enable + USB error interrupt enable + port change detect enable + system error
- * enable + interrupt on async advance enable) + general purpos Timer 0 Interrupt enable */
- ehciInstance->ehciIpBase->USBINTR |= (0x1000037U);
- return kStatus_USB_Success;
- }
- static usb_status_t USB_HostEhciCancelPipe(usb_host_ehci_instance_t *ehciInstance,
- usb_host_ehci_pipe_t *ehciPipePointer,
- usb_host_transfer_t *transfer)
- {
- usb_host_ehci_qh_t *qhPointer;
- #if (((defined USB_HOST_CONFIG_EHCI_MAX_ITD) && (USB_HOST_CONFIG_EHCI_MAX_ITD)) || \
- ((defined USB_HOST_CONFIG_EHCI_MAX_SITD) && (USB_HOST_CONFIG_EHCI_MAX_SITD)))
- usb_host_ehci_iso_t *isoPointer;
- uint32_t speed = 0U;
- #endif
- uint8_t cancelPipe = 0;
- switch (ehciPipePointer->pipeCommon.pipeType)
- {
- case USB_ENDPOINT_BULK:
- case USB_ENDPOINT_CONTROL:
- case USB_ENDPOINT_INTERRUPT:
- qhPointer = (usb_host_ehci_qh_t *)ehciPipePointer->ehciQh;
- if (qhPointer->ehciTransferHead == NULL) /* there is no transfer to cancel */
- {
- return kStatus_USB_Success;
- }
- if (transfer != NULL)
- {
- if ((qhPointer->ehciTransferHead == transfer) &&
- (qhPointer->ehciTransferHead == qhPointer->ehciTransferTail)) /* only has this one transfer */
- {
- cancelPipe = 1U;
- }
- else
- {
- cancelPipe = 0U;
- }
- }
- else
- {
- cancelPipe = 1U;
- }
- if (cancelPipe == 1U) /* cancel all pipe */
- {
- (void)USB_HostEhciQhQtdListDeinit(ehciInstance, ehciPipePointer); /* release all the qtd */
- }
- else /* cancel one transfer */
- {
- (void)USB_HostEhciTransferQtdListDeinit(ehciInstance, ehciPipePointer, transfer);
- }
- break;
- #if (((defined USB_HOST_CONFIG_EHCI_MAX_ITD) && (USB_HOST_CONFIG_EHCI_MAX_ITD)) || \
- ((defined USB_HOST_CONFIG_EHCI_MAX_SITD) && (USB_HOST_CONFIG_EHCI_MAX_SITD)))
- case USB_ENDPOINT_ISOCHRONOUS:
- isoPointer = (usb_host_ehci_iso_t *)ehciPipePointer->ehciQh;
- if (isoPointer->ehciTransferHead == NULL) /* there is no transfer to cancel */
- {
- return kStatus_USB_Success;
- }
- /* cancel all pipe, don't implement canceling transfer for iso */
- (void)USB_HostHelperGetPeripheralInformation(ehciPipePointer->pipeCommon.deviceHandle,
- (uint32_t)kUSB_HostGetDeviceSpeed, &speed);
- if (speed == USB_SPEED_HIGH)
- {
- #if ((defined USB_HOST_CONFIG_EHCI_MAX_ITD) && (USB_HOST_CONFIG_EHCI_MAX_ITD))
- (void)USB_HostEhciItdArrayDeinit(ehciInstance, ehciPipePointer); /* de-initialize itd */
- #endif
- }
- else
- {
- #if ((defined USB_HOST_CONFIG_EHCI_MAX_SITD) && (USB_HOST_CONFIG_EHCI_MAX_SITD))
- (void)USB_HostEhciSitdArrayDeinit(ehciInstance, ehciPipePointer); /* de-initialize sitd */
- #endif
- }
- break;
- #endif
- default:
- /*no action*/
- break;
- }
- return kStatus_USB_Success;
- }
- usb_status_t USB_HostEhciControlBus(usb_host_ehci_instance_t *ehciInstance, uint8_t busControl)
- {
- usb_status_t status = kStatus_USB_Success;
- uint32_t portScRegister;
- usb_host_bus_control_t controlCode = (usb_host_bus_control_t)busControl;
- switch (controlCode)
- {
- case kUSB_HostBusReset:
- /* reset port */
- portScRegister = ehciInstance->ehciIpBase->PORTSC1;
- portScRegister &= (~EHCI_PORTSC1_W1_BITS);
- ehciInstance->ehciIpBase->PORTSC1 = (portScRegister | USBHS_PORTSC1_PR_MASK);
- while (0U != (ehciInstance->ehciIpBase->PORTSC1 & USBHS_PORTSC1_PR_MASK))
- {
- }
- break;
- case kUSB_HostBusRestart:
- ehciInstance->deviceAttached = (uint8_t)kEHCIDeviceDetached;
- ehciInstance->ehciIpBase->USBINTR |= (USBHS_USBINTR_PCE_MASK); /* enable ehci port change interrupt */
- break;
- case kUSB_HostBusEnableAttach: /* enable device attach */
- if (ehciInstance->deviceAttached == (uint8_t)kEHCIDeviceDetached)
- {
- ehciInstance->ehciIpBase->USBINTR |= (USBHS_USBINTR_PCE_MASK); /* enable ehci port change interrupt */
- }
- break;
- case kUSB_HostBusDisableAttach: /* disable device attach */
- ehciInstance->ehciIpBase->USBINTR &= (~USBHS_USBINTR_PCE_MASK); /* disable ehci port change interrupt */
- break;
- #if ((defined(USB_HOST_CONFIG_LOW_POWER_MODE)) && (USB_HOST_CONFIG_LOW_POWER_MODE > 0U))
- case kUSB_HostBusSuspend:
- if (0U != (ehciInstance->ehciIpBase->PORTSC1 & USBHS_PORTSC1_CCS_MASK))
- {
- /* set timer1 */
- ehciInstance->ehciIpBase->GPTIMER1LD = (1 * 1000); /* 1ms */
- ehciInstance->ehciIpBase->GPTIMER1CTL |=
- (USBHS_GPTIMER0CTL_RUN_MASK | USBHS_GPTIMER0CTL_MODE_MASK | USBHS_GPTIMER0CTL_RST_MASK);
- USB_HostEhciStopAsync(ehciInstance);
- USB_HostEhciStopPeriodic(ehciInstance);
- while (0U != (ehciInstance->ehciIpBase->USBSTS & (USBHS_USBSTS_PS_MASK | USBHS_USBSTS_AS_MASK)))
- {
- __NOP();
- }
- ehciInstance->ehciIpBase->PORTSC1 &= ~USBHS_PORTSC1_WKCN_MASK;
- ehciInstance->ehciIpBase->PORTSC1 |= USBHS_PORTSC1_WKDS_MASK;
- ehciInstance->ehciIpBase->PORTSC1 |= (USBHS_PORTSC1_SUSP_MASK); /* Suspend the device */
- ehciInstance->matchTick = 0U;
- ehciInstance->ehciIpBase->USBINTR |= (USBHS_USBINTR_TIE1_MASK);
- ehciInstance->busSuspendStatus = kBus_EhciStartSuspend;
- }
- else
- {
- status = kStatus_USB_Error;
- }
- break;
- case kUSB_HostBusResume:
- ehciInstance->ehciIpBase->PORTSC1 &= ~(USBHS_PORTSC1_SUSP_MASK); /* Clear Suspend bit */
- ehciInstance->ehciIpBase->PORTSC1 &= ~USBHS_PORTSC1_PHCD_MASK;
- if (ehciInstance->deviceAttached != (uint8_t)kEHCIDeviceDetached)
- {
- ehciInstance->busSuspendStatus = kBus_EhciStartResume;
- #if (defined(FSL_FEATURE_SOC_USBNC_COUNT) && (FSL_FEATURE_SOC_USBNC_COUNT > 0U))
- ehciInstance->registerNcBase->USB_OTGn_CTRL &= ~USBNC_USB_OTGn_CTRL_WIE_MASK;
- #else
- ehciInstance->ehciIpBase->USBGENCTRL &= ~USBHS_USBGENCTRL_WU_IE_MASK;
- #endif
- ehciInstance->ehciIpBase->USBCMD |= (USBHS_USBCMD_RS_MASK);
- ehciInstance->ehciIpBase->PORTSC1 |= (USBHS_PORTSC1_FPR_MASK); /* Resume the device */
- }
- else
- {
- status = kStatus_USB_Error;
- }
- break;
- #endif
- default:
- status = kStatus_USB_Error;
- break;
- }
- return status;
- }
- void USB_HostEhciTransactionDone(usb_host_ehci_instance_t *ehciInstance)
- {
- /* process async QH */
- usb_host_ehci_pipe_t *ehciPipePointer;
- usb_host_ehci_pipe_t *ehciClearPipePointer = NULL;
- volatile usb_host_ehci_qh_t *vltQhPointer;
- volatile usb_host_ehci_qtd_t *vltQtdPointer;
- usb_host_transfer_t *transfer;
- usb_host_transfer_t *nextTransfer;
- uint32_t qtdStatus = 0;
- #if ((defined USB_HOST_CONFIG_EHCI_MAX_ITD) && (USB_HOST_CONFIG_EHCI_MAX_ITD))
- volatile usb_host_ehci_itd_t *vltItdPointer;
- uint8_t index = 0;
- #endif
- #if ((defined USB_HOST_CONFIG_EHCI_MAX_SITD) && (USB_HOST_CONFIG_EHCI_MAX_SITD))
- volatile usb_host_ehci_sitd_t *vltSitdPointer;
- #endif
- #if (((defined USB_HOST_CONFIG_EHCI_MAX_ITD) && (USB_HOST_CONFIG_EHCI_MAX_ITD)) || \
- ((defined USB_HOST_CONFIG_EHCI_MAX_SITD) && (USB_HOST_CONFIG_EHCI_MAX_SITD)))
- usb_host_ehci_iso_t *isoPointer;
- uint32_t dataLength;
- uint32_t speed;
- #endif
- void *temp;
- uint32_t transferResults;
- uint32_t transferOverlayResults;
- ehciPipePointer = ehciInstance->ehciRunningPipeList; /* check all the running pipes */
- while (ehciPipePointer != NULL)
- {
- switch (ehciPipePointer->pipeCommon.pipeType)
- {
- case USB_ENDPOINT_BULK:
- case USB_ENDPOINT_INTERRUPT:
- case USB_ENDPOINT_CONTROL:
- vltQhPointer = (volatile usb_host_ehci_qh_t *)ehciPipePointer->ehciQh; /* pipe's qh */
- transfer = vltQhPointer->ehciTransferHead; /* qh's transfer */
- while (transfer != NULL)
- {
- nextTransfer = transfer->next;
- /* normal case */
- vltQtdPointer = (volatile usb_host_ehci_qtd_t *)transfer->union2.unitTail;
- transferResults = vltQtdPointer->transferResults[0];
- transferOverlayResults = vltQhPointer->transferOverlayResults[0];
- if ((0U != (transferResults & (EHCI_HOST_QTD_IOC_MASK))) &&
- (0U == (transferResults & EHCI_HOST_QTD_STATUS_ACTIVE_MASK))) /* transfer is done */
- {
- qtdStatus = (transferResults & EHCI_HOST_QTD_STATUS_ERROR_MASK);
- transfer->transferSofar =
- USB_HostEhciQtdListRelease(ehciInstance, (usb_host_ehci_qtd_t *)(transfer->union1.unitHead),
- (usb_host_ehci_qtd_t *)(transfer->union2.unitTail));
- transfer->transferSofar = (transfer->transferLength < transfer->transferSofar) ?
- 0U :
- (transfer->transferLength - transfer->transferSofar);
- vltQhPointer->ehciTransferHead = transfer->next;
- vltQhPointer->timeOutLabel = 0U;
- vltQhPointer->timeOutValue = USB_HOST_EHCI_CONTROL_BULK_TIME_OUT_VALUE;
- if (0U != qtdStatus) /* has errors */
- {
- if (0U == (transferOverlayResults & EHCI_HOST_QTD_STATUS_ACTIVE_MASK))
- {
- vltQhPointer->transferOverlayResults[0] &=
- (~EHCI_HOST_QTD_STATUS_MASK); /* clear error status */
- }
- if (0U != (qtdStatus & EHCI_HOST_QH_STATUS_NOSTALL_ERROR_MASK))
- {
- /* callback function is different from the current condition */
- transfer->callbackFn(transfer->callbackParam, transfer,
- kStatus_USB_TransferFailed); /* transfer fail */
- }
- else
- {
- /* callback function is different from the current condition */
- transfer->callbackFn(transfer->callbackParam, transfer, kStatus_USB_TransferStall);
- }
- }
- else
- {
- if ((ehciPipePointer->pipeCommon.pipeType == USB_ENDPOINT_CONTROL) &&
- (transfer->setupPacket->bRequest == USB_REQUEST_STANDARD_CLEAR_FEATURE) &&
- (transfer->setupPacket->bmRequestType == USB_REQUEST_TYPE_RECIPIENT_ENDPOINT) &&
- ((USB_SHORT_FROM_LITTLE_ENDIAN(transfer->setupPacket->wValue) & 0x00FFu) ==
- USB_REQUEST_STANDARD_FEATURE_SELECTOR_ENDPOINT_HALT))
- {
- ehciClearPipePointer = ehciInstance->ehciRunningPipeList;
- while (ehciClearPipePointer != NULL)
- {
- /* only compute bulk and interrupt pipe */
- if (((ehciClearPipePointer->pipeCommon.endpointAddress |
- (ehciClearPipePointer->pipeCommon.direction
- << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT)) ==
- (uint8_t)(USB_SHORT_FROM_LITTLE_ENDIAN(transfer->setupPacket->wIndex))) &&
- (ehciClearPipePointer->pipeCommon.deviceHandle ==
- ehciPipePointer->pipeCommon.deviceHandle))
- {
- break;
- }
- temp = (void *)ehciClearPipePointer->pipeCommon.next;
- ehciClearPipePointer = (usb_host_ehci_pipe_t *)temp;
- }
- if ((ehciClearPipePointer != NULL) &&
- ((ehciClearPipePointer->pipeCommon.pipeType == USB_ENDPOINT_INTERRUPT) ||
- (ehciClearPipePointer->pipeCommon.pipeType == USB_ENDPOINT_BULK)))
- {
- ((volatile usb_host_ehci_qh_t *)(ehciClearPipePointer->ehciQh))
- ->transferOverlayResults[0] &= (~EHCI_HOST_QTD_DT_MASK);
- }
- }
- /* callback function is different from the current condition */
- transfer->callbackFn(transfer->callbackParam, transfer,
- kStatus_USB_Success); /* transfer success */
- }
- }
- else if ((0U == (transferOverlayResults & EHCI_HOST_QTD_STATUS_ACTIVE_MASK)) &&
- (0U != (transferOverlayResults &
- EHCI_HOST_QH_STATUS_ERROR_MASK))) /* there is error and transfer is done */
- {
- qtdStatus = (vltQhPointer->transferOverlayResults[0] & EHCI_HOST_QH_STATUS_ERROR_MASK);
- vltQtdPointer = (volatile usb_host_ehci_qtd_t *)(vltQhPointer->currentQtdPointer);
- if ((0U != ((uint32_t)vltQtdPointer & EHCI_HOST_T_INVALID_VALUE)) ||
- (vltQtdPointer == NULL)) /* the error status is unreasonable */
- {
- vltQhPointer->transferOverlayResults[0] &=
- (~EHCI_HOST_QTD_STATUS_MASK); /* clear error status */
- }
- else
- {
- /* remove qtd from qh */
- while ((vltQtdPointer != NULL) &&
- (0U == (transferResults & EHCI_HOST_QTD_IOC_MASK))) /* find the IOC qtd */
- {
- vltQtdPointer = (volatile usb_host_ehci_qtd_t *)vltQtdPointer->nextQtdPointer;
- }
- vltQhPointer->nextQtdPointer = EHCI_HOST_T_INVALID_VALUE;
- vltQhPointer->currentQtdPointer = EHCI_HOST_T_INVALID_VALUE;
- vltQhPointer->transferOverlayResults[0] &=
- (~EHCI_HOST_QTD_STATUS_MASK); /* clear error status */
- if (vltQtdPointer != NULL)
- {
- vltQhPointer->nextQtdPointer = vltQtdPointer->nextQtdPointer;
- }
- transfer->transferSofar = USB_HostEhciQtdListRelease(
- ehciInstance, (usb_host_ehci_qtd_t *)(transfer->union1.unitHead),
- (usb_host_ehci_qtd_t *)(transfer->union2.unitTail));
- transfer->transferSofar = (transfer->transferLength < transfer->transferSofar) ?
- 0U :
- (transfer->transferLength - transfer->transferSofar);
- vltQhPointer->ehciTransferHead = transfer->next;
- vltQhPointer->timeOutLabel = 0U;
- vltQhPointer->timeOutValue = USB_HOST_EHCI_CONTROL_BULK_TIME_OUT_VALUE;
- if (0U != (qtdStatus & EHCI_HOST_QH_STATUS_NOSTALL_ERROR_MASK))
- {
- /* callback function is different from the current condition */
- transfer->callbackFn(transfer->callbackParam, transfer,
- kStatus_USB_TransferFailed); /* transfer fail */
- }
- else
- {
- /* callback function is different from the current condition */
- transfer->callbackFn(transfer->callbackParam, transfer,
- kStatus_USB_TransferStall); /* transfer stall */
- }
- }
- }
- else
- {
- break;
- }
- transfer = nextTransfer;
- }
- break;
- #if (((defined USB_HOST_CONFIG_EHCI_MAX_ITD) && (USB_HOST_CONFIG_EHCI_MAX_ITD)) || \
- ((defined USB_HOST_CONFIG_EHCI_MAX_SITD) && (USB_HOST_CONFIG_EHCI_MAX_SITD)))
- case USB_ENDPOINT_ISOCHRONOUS:
- qtdStatus = 0; /* qtdStatus means break here, because there is only one break in while for misra */
- isoPointer = (usb_host_ehci_iso_t *)ehciPipePointer->ehciQh; /* pipe's usb_host_ehci_iso_t */
- transfer = isoPointer->ehciTransferHead; /* usb_host_ehci_iso_t's transfer */
- while (transfer != NULL)
- {
- nextTransfer = transfer->next;
- (void)USB_HostHelperGetPeripheralInformation(ehciPipePointer->pipeCommon.deviceHandle,
- (uint32_t)kUSB_HostGetDeviceSpeed, &speed);
- if (speed == USB_SPEED_HIGH)
- {
- #if ((defined USB_HOST_CONFIG_EHCI_MAX_ITD) && (USB_HOST_CONFIG_EHCI_MAX_ITD))
- vltItdPointer =
- (volatile usb_host_ehci_itd_t *)(transfer->union2.unitTail); /* transfer's last itd */
- for (index = 0; index < 8U; ++index)
- {
- if (0U != (vltItdPointer->transactions[index] & EHCI_HOST_ITD_STATUS_ACTIVE_MASK))
- {
- break;
- }
- }
- if (index == 8U) /* transfer is done */
- {
- /* remove itd from frame list and release itd */
- dataLength = USB_HostEhciItdArrayRelease(ehciInstance,
- (usb_host_ehci_itd_t *)transfer->union1.unitHead,
- (usb_host_ehci_itd_t *)transfer->union2.unitTail);
- transfer->transferSofar = dataLength;
- isoPointer->ehciTransferHead = transfer->next;
- /* callback function is different from the current condition */
- transfer->callbackFn(transfer->callbackParam, transfer,
- kStatus_USB_Success); /* transfer callback success */
- /* TODO: iso callback error */
- }
- else
- {
- qtdStatus = 1U; /* break */
- }
- #endif
- }
- else
- {
- #if ((defined USB_HOST_CONFIG_EHCI_MAX_SITD) && (USB_HOST_CONFIG_EHCI_MAX_SITD))
- vltSitdPointer =
- (volatile usb_host_ehci_sitd_t *)(transfer->union2.unitTail); /* transfer's last sitd */
- if (0U == (vltSitdPointer->transferResults[0] &
- EHCI_HOST_SITD_STATUS_ACTIVE_MASK)) /* transfer is done */
- {
- /* remove sitd from frame list and release itd */
- dataLength = USB_HostEhciSitdArrayRelease(
- ehciInstance, (usb_host_ehci_sitd_t *)transfer->union1.unitHead,
- (usb_host_ehci_sitd_t *)transfer->union2.unitTail);
- transfer->transferSofar = transfer->transferLength - dataLength;
- isoPointer->ehciTransferHead = transfer->next;
- /* callback function is different from the current condition */
- transfer->callbackFn(transfer->callbackParam, transfer,
- kStatus_USB_Success); /* transfer callback success */
- /* TODO: iso callback error */
- }
- else
- {
- qtdStatus = 1U; /* break */
- }
- #endif
- }
- if (qtdStatus == 1U)
- {
- break;
- }
- transfer = nextTransfer;
- }
- break;
- #endif
- default:
- /*no action*/
- break;
- }
- temp = (void *)ehciPipePointer->pipeCommon.next;
- ehciPipePointer = (usb_host_ehci_pipe_t *)temp;
- }
- }
- void USB_HostEhciPortChange(usb_host_ehci_instance_t *ehciInstance)
- {
- /* note: only has one port */
- uint32_t portScRegister = ehciInstance->ehciIpBase->PORTSC1;
- uint32_t sofStart = 0;
- uint32_t sofCount = 0;
- uint32_t index;
- if (0U != (portScRegister & USBHS_PORTSC1_CSC_MASK)) /* connection status change */
- {
- sofStart = (ehciInstance->ehciIpBase->FRINDEX & EHCI_MAX_UFRAME_VALUE);
- /* process CSC bit */
- while (1U == 1U)
- {
- portScRegister = ehciInstance->ehciIpBase->PORTSC1;
- if (0U != (portScRegister & USBHS_PORTSC1_CSC_MASK))
- {
- /* clear csc bit */
- portScRegister = ehciInstance->ehciIpBase->PORTSC1;
- portScRegister &= (~EHCI_PORTSC1_W1_BITS);
- ehciInstance->ehciIpBase->PORTSC1 = (portScRegister | USBHS_PORTSC1_CSC_MASK);
- }
- sofCount = (ehciInstance->ehciIpBase->FRINDEX & EHCI_MAX_UFRAME_VALUE);
- if (((sofCount + EHCI_MAX_UFRAME_VALUE + 1U - sofStart) & EHCI_MAX_UFRAME_VALUE) >
- (1U * 8U)) /* delay 1ms to clear CSC */
- {
- break;
- }
- }
- }
- /* process CCS bit */
- portScRegister = ehciInstance->ehciIpBase->PORTSC1;
- if (0U != (portScRegister & USBHS_PORTSC1_CCS_MASK)) /* process attach */
- {
- if ((ehciInstance->deviceAttached == (uint8_t)kEHCIDevicePhyAttached) ||
- (ehciInstance->deviceAttached == (uint8_t)kEHCIDeviceAttached))
- {
- return;
- }
- #if ((defined(USB_HOST_CONFIG_LOW_POWER_MODE)) && (USB_HOST_CONFIG_LOW_POWER_MODE > 0U))
- ehciInstance->busSuspendStatus = kBus_EhciIdle;
- ehciInstance->ehciIpBase->USBINTR &= ~(USBHS_USBINTR_TIE1_MASK);
- #endif
- for (index = 0; index < USB_HOST_EHCI_PORT_CONNECT_DEBOUNCE_DELAY; ++index)
- {
- USB_HostEhciDelay(ehciInstance->ehciIpBase, 1);
- if (0U == (ehciInstance->ehciIpBase->PORTSC1 & USBHS_PORTSC1_CCS_MASK))
- {
- break;
- }
- }
- if (index < USB_HOST_EHCI_PORT_CONNECT_DEBOUNCE_DELAY) /* CCS is cleared */
- {
- ehciInstance->deviceAttached = (uint8_t)kEHCIDeviceDetached;
- return;
- }
- /* reset port */
- portScRegister = ehciInstance->ehciIpBase->PORTSC1;
- portScRegister &= (~EHCI_PORTSC1_W1_BITS);
- ehciInstance->ehciIpBase->PORTSC1 = (portScRegister | USBHS_PORTSC1_PR_MASK);
- while (0U != (ehciInstance->ehciIpBase->PORTSC1 & USBHS_PORTSC1_PR_MASK))
- {
- }
- ehciInstance->firstDeviceSpeed =
- (uint8_t)((ehciInstance->ehciIpBase->PORTSC1 & USBHS_PORTSC1_PSPD_MASK) >> USBHS_PORTSC1_PSPD_SHIFT);
- /* enable ehci phy disconnection */
- if (ehciInstance->firstDeviceSpeed == USB_SPEED_HIGH)
- {
- USB_EhcihostPhyDisconnectDetectCmd(ehciInstance->controllerId, 1);
- }
- /* wait for reset */
- USB_HostEhciDelay(ehciInstance->ehciIpBase, USB_HOST_EHCI_PORT_RESET_DELAY);
- /* process attach */
- (void)OSA_EventSet(ehciInstance->taskEventHandle, EHCI_TASK_EVENT_DEVICE_ATTACH);
- /* gpt timer start */
- ehciInstance->ehciIpBase->GPTIMER0CTL |=
- (USBHS_GPTIMER0CTL_RUN_MASK | USBHS_GPTIMER0CTL_MODE_MASK | USBHS_GPTIMER0CTL_RST_MASK);
- ehciInstance->deviceAttached = (uint8_t)kEHCIDevicePhyAttached;
- }
- else
- {
- if ((ehciInstance->deviceAttached == (uint8_t)kEHCIDevicePhyAttached) ||
- (ehciInstance->deviceAttached == (uint8_t)kEHCIDeviceAttached))
- {
- #if ((defined(USB_HOST_CONFIG_LOW_POWER_MODE)) && (USB_HOST_CONFIG_LOW_POWER_MODE > 0U))
- ehciInstance->busSuspendStatus = kBus_EhciIdle;
- ehciInstance->ehciIpBase->USBINTR &= ~(USBHS_USBINTR_TIE1_MASK);
- #endif
- /* disable ehci phy disconnection */
- USB_EhcihostPhyDisconnectDetectCmd(ehciInstance->controllerId, 0);
- /* disable async and periodic */
- USB_HostEhciStopAsync(ehciInstance);
- USB_HostEhciStopPeriodic(ehciInstance);
- (void)OSA_EventSet(ehciInstance->taskEventHandle, EHCI_TASK_EVENT_DEVICE_DETACH);
- }
- }
- }
- void USB_HostEhciTimer0(usb_host_ehci_instance_t *ehciInstance)
- {
- volatile usb_host_ehci_qh_t *vltQhPointer;
- usb_host_ehci_qtd_t *vltQtdPointer;
- usb_host_transfer_t *transfer;
- uint32_t backValue;
- volatile uint32_t *totalBytesAddress = NULL;
- usb_host_ehci_pipe_t *ehciPipePointer = ehciInstance->ehciRunningPipeList;
- void *temp;
- uint8_t timeoutLabel;
- while (ehciPipePointer != NULL)
- {
- switch (ehciPipePointer->pipeCommon.pipeType)
- {
- case USB_ENDPOINT_BULK:
- case USB_ENDPOINT_CONTROL:
- vltQhPointer = (volatile usb_host_ehci_qh_t *)ehciPipePointer->ehciQh; /* pipe's qh */
- transfer = vltQhPointer->ehciTransferHead; /* qh's transfer */
- if ((transfer != NULL)) /* there is transfering data */
- {
- timeoutLabel = 0U;
- if (ehciInstance->deviceAttached != (uint8_t)kEHCIDeviceAttached)
- {
- vltQtdPointer = (usb_host_ehci_qtd_t *)transfer->union2.unitTail;
- vltQhPointer->nextQtdPointer = EHCI_HOST_T_INVALID_VALUE; /* invalid next qtd */
- vltQhPointer->transferOverlayResults[0] &=
- (~EHCI_HOST_QTD_STATUS_MASK); /* clear error status */
- timeoutLabel = 1;
- }
- else
- {
- if (0U != (vltQhPointer->transferOverlayResults[0] & EHCI_HOST_QTD_STATUS_ACTIVE_MASK))
- {
- vltQtdPointer = (usb_host_ehci_qtd_t *)vltQhPointer->currentQtdPointer;
- totalBytesAddress = &(vltQhPointer->transferOverlayResults[0]);
- }
- else
- {
- vltQtdPointer = (usb_host_ehci_qtd_t *)transfer->union2.unitTail;
- totalBytesAddress = &(vltQtdPointer->transferResults[0]);
- }
- backValue =
- (((*totalBytesAddress) & EHCI_HOST_QTD_TOTAL_BYTES_MASK) >>
- EHCI_HOST_QTD_TOTAL_BYTES_SHIFT); /* backValue is used for total bytes to transfer */
- if (vltQhPointer->timeOutLabel != backValue) /* use total bytes to reflect the time out */
- {
- vltQhPointer->timeOutValue = USB_HOST_EHCI_CONTROL_BULK_TIME_OUT_VALUE;
- vltQhPointer->timeOutLabel = (uint16_t)backValue;
- }
- else
- {
- /* time out when the total bytes don't change for the duration
- * USB_HOST_EHCI_CONTROL_BULK_TIME_OUT_VALUE
- */
- (vltQhPointer->timeOutValue)--;
- if (vltQhPointer->timeOutValue == 0U)
- {
- /* stop the qh schedule */
- USB_HostEhciStopAsync(ehciInstance);
- if (backValue != (((*totalBytesAddress) & EHCI_HOST_QTD_TOTAL_BYTES_MASK) >>
- EHCI_HOST_QTD_TOTAL_BYTES_SHIFT))
- {
- USB_HostEhciStartAsync(ehciInstance);
- }
- else
- {
- vltQhPointer->nextQtdPointer = EHCI_HOST_T_INVALID_VALUE; /* invalid next qtd */
- vltQhPointer->transferOverlayResults[0] &=
- (~EHCI_HOST_QTD_STATUS_MASK); /* clear error status */
- USB_HostEhciStartAsync(ehciInstance);
- timeoutLabel = 1U;
- }
- }
- }
- }
- if (timeoutLabel == 1U)
- {
- /* remove qtd from qh */
- temp = (void *)vltQhPointer->ehciTransferTail;
- while ((vltQtdPointer != NULL) &&
- (0U == (vltQtdPointer->transferResults[0] & EHCI_HOST_QTD_IOC_MASK)) &&
- (vltQtdPointer != (usb_host_ehci_qtd_t *)temp))
- {
- vltQtdPointer = (usb_host_ehci_qtd_t *)vltQtdPointer->nextQtdPointer;
- }
- if ((vltQtdPointer != NULL) &&
- (0U == (vltQtdPointer->nextQtdPointer & EHCI_HOST_T_INVALID_VALUE)))
- {
- vltQhPointer->nextQtdPointer =
- vltQtdPointer->nextQtdPointer; /* start qh if there are other qtd that don't belong to
- the transfer */
- }
- transfer->transferSofar =
- USB_HostEhciQtdListRelease(ehciInstance, (usb_host_ehci_qtd_t *)(transfer->union1.unitHead),
- (usb_host_ehci_qtd_t *)(transfer->union2.unitTail));
- transfer->transferSofar = (transfer->transferLength < transfer->transferSofar) ?
- 0U :
- (transfer->transferLength - transfer->transferSofar);
- vltQhPointer->ehciTransferHead = transfer->next;
- vltQhPointer->timeOutValue = USB_HOST_EHCI_CONTROL_BULK_TIME_OUT_VALUE;
- /* callback function is different from the current condition */
- transfer->callbackFn(transfer->callbackParam, transfer, kStatus_USB_TransferFailed);
- }
- }
- break;
- default:
- /*no action*/
- break;
- }
- temp = (void *)ehciPipePointer->pipeCommon.next;
- ehciPipePointer = (usb_host_ehci_pipe_t *)temp;
- }
- }
- #if ((defined(USB_HOST_CONFIG_LOW_POWER_MODE)) && (USB_HOST_CONFIG_LOW_POWER_MODE > 0U))
- static void USB_HostEhciTimer1(usb_host_ehci_instance_t *ehciInstance)
- {
- if (ehciInstance->deviceAttached != (uint8_t)kEHCIDeviceDetached)
- {
- if (kBus_EhciStartSuspend == ehciInstance->busSuspendStatus)
- {
- usb_host_instance_t *hostPointer = (usb_host_instance_t *)ehciInstance->hostHandle;
- if (0U == ehciInstance->matchTick)
- {
- ehciInstance->matchTick = hostPointer->hwTick;
- }
- else
- {
- if ((hostPointer->hwTick - ehciInstance->matchTick) >= 5U)
- {
- ehciInstance->ehciIpBase->USBCMD &= ~USBHS_USBCMD_RS_MASK;
- ehciInstance->ehciIpBase->USBSTS |= USBHS_USBSTS_SRI_MASK;
- #if (defined(FSL_FEATURE_SOC_USBNC_COUNT) && (FSL_FEATURE_SOC_USBNC_COUNT > 0U))
- #if 0
- ehciInstance->registerPhyBase->CTRL |= USBPHY_CTRL_ENVBUSCHG_WKUP_MASK
- | USBPHY_CTRL_ENIDCHG_WKUP_MASK
- | USBPHY_CTRL_ENDPDMCHG_WKUP_MASK
- | USBPHY_CTRL_ENIRQRESUMEDETECT_MASK
- ;
- #endif
- #endif
- ehciInstance->ehciIpBase->PORTSC1 |= USBHS_PORTSC1_PHCD_MASK;
- ehciInstance->registerPhyBase->PWD = 0xFFFFFFFFU;
- while (0U != (ehciInstance->registerPhyBase->CTRL & (USBPHY_CTRL_UTMI_SUSPENDM_MASK)))
- {
- __NOP();
- }
- #if (defined(FSL_FEATURE_SOC_USBNC_COUNT) && (FSL_FEATURE_SOC_USBNC_COUNT > 0U))
- ehciInstance->registerNcBase->USB_OTGn_CTRL |= USBNC_USB_OTGn_CTRL_WKUP_ID_EN_MASK |
- USBNC_USB_OTGn_CTRL_WKUP_VBUS_EN_MASK |
- USBNC_USB_OTGn_CTRL_WKUP_DPDM_EN_MASK;
- ehciInstance->registerNcBase->USB_OTGn_CTRL |= USBNC_USB_OTGn_CTRL_WIE_MASK;
- #else
- ehciInstance->ehciIpBase->USBGENCTRL = USBHS_USBGENCTRL_WU_IE_MASK;
- #endif
- ehciInstance->registerPhyBase->CTRL |= USBPHY_CTRL_CLKGATE_MASK;
- (void)hostPointer->deviceCallback(hostPointer->suspendedDevice, NULL,
- kUSB_HostEventSuspended); /* call host callback function */
- ehciInstance->busSuspendStatus = kBus_EhciSuspended;
- }
- }
- }
- else if (kBus_EhciStartResume == ehciInstance->busSuspendStatus)
- {
- usb_host_instance_t *hostPointer = (usb_host_instance_t *)ehciInstance->hostHandle;
- if (0U == (ehciInstance->ehciIpBase->PORTSC1 & USBHS_PORTSC1_FPR_MASK))
- {
- ehciInstance->ehciIpBase->PORTSC1 &= ~USBHS_PORTSC1_WKDS_MASK;
- if (0U != (ehciInstance->ehciIpBase->PORTSC1 & USBHS_PORTSC1_CCS_MASK))
- {
- USB_HostEhciStartAsync(ehciInstance);
- USB_HostEhciStartPeriodic(ehciInstance);
- }
- (void)hostPointer->deviceCallback(hostPointer->suspendedDevice, NULL,
- kUSB_HostEventResumed); /* call host callback function */
- hostPointer->suspendedDevice = NULL;
- ehciInstance->busSuspendStatus = kBus_EhciIdle;
- ehciInstance->ehciIpBase->USBINTR &= ~(USBHS_USBINTR_TIE1_MASK);
- }
- }
- else
- {
- }
- }
- else
- {
- ehciInstance->busSuspendStatus = kBus_EhciIdle;
- ehciInstance->ehciIpBase->USBINTR &= ~(USBHS_USBINTR_TIE1_MASK);
- }
- }
- #endif
- usb_status_t USB_HostEhciCreate(uint8_t controllerId,
- usb_host_handle upperLayerHandle,
- usb_host_controller_handle *controllerHandle)
- {
- uint32_t index = 0;
- osa_status_t osaStatus;
- usb_host_ehci_instance_t *ehciInstance;
- uint32_t usbhsBaseAddrs[] = USBHS_BASE_ADDRS;
- usb_host_ehci_data_t *usbHostEhciData[USB_HOST_CONFIG_EHCI];
- uint32_t *framePointer;
- void *temp;
- uint8_t instanceIndex = 0U;
- if ((controllerId - (uint8_t)kUSB_ControllerEhci0) >= (sizeof(usbhsBaseAddrs) / sizeof(usbhsBaseAddrs[0])))
- {
- return kStatus_USB_ControllerNotFound;
- }
- *controllerHandle = NULL;
- ehciInstance = (usb_host_ehci_instance_t *)OSA_MemoryAllocate(
- sizeof(usb_host_ehci_instance_t)); /* malloc host ehci instance */
- if (ehciInstance == NULL)
- {
- return kStatus_USB_AllocFail;
- }
- ehciInstance->controllerId = controllerId;
- ehciInstance->hostHandle = upperLayerHandle;
- ehciInstance->deviceAttached = (uint8_t)kEHCIDeviceDetached;
- ehciInstance->ehciIpBase = (USBHS_Type *)
- usbhsBaseAddrs[controllerId - (uint8_t)kUSB_ControllerEhci0]; /* operate ehci ip through the base address */
- #if ((defined(USB_HOST_CONFIG_LOW_POWER_MODE)) && (USB_HOST_CONFIG_LOW_POWER_MODE > 0U))
- ehciInstance->busSuspendStatus = kBus_EhciIdle;
- #if (defined(USB_HOST_CONFIG_LOW_POWER_MODE) && (USB_HOST_CONFIG_LOW_POWER_MODE > 0U))
- ehciInstance->registerPhyBase = (USBPHY_Type *)USB_EhciPhyGetBase(controllerId);
- #if (defined(FSL_FEATURE_SOC_USBNC_COUNT) && (FSL_FEATURE_SOC_USBNC_COUNT > 0U))
- ehciInstance->registerNcBase = (USBNC_Type *)USB_EhciNCGetBase(controllerId);
- #endif
- #endif
- #endif
- if ((USB_HostEhciResetIP(ehciInstance) != kStatus_USB_Success) ||
- ((ehciInstance->controllerId < (uint8_t)kUSB_ControllerEhci0))) /* reset ehci ip */
- {
- OSA_MemoryFree(ehciInstance);
- return kStatus_USB_Error;
- }
- #if (USB_HOST_CONFIG_EHCI == 1U)
- if (0U == usbHostEhciFramListStatus[0])
- {
- usbHostEhciFramListStatus[0] = 1U;
- instanceIndex = 0U;
- ehciInstance->ehciFrameList = &s_UsbHostEhciFrameList1[0];
- }
- #elif (USB_HOST_CONFIG_EHCI == 2U)
- if (0U == usbHostEhciFramListStatus[0])
- {
- usbHostEhciFramListStatus[0] = 1U;
- instanceIndex = 0U;
- ehciInstance->ehciFrameList = &s_UsbHostEhciFrameList1[0];
- }
- else if (0U == usbHostEhciFramListStatus[1])
- {
- usbHostEhciFramListStatus[1] = 1U;
- instanceIndex = 1U;
- ehciInstance->ehciFrameList = &s_UsbHostEhciFrameList2[0];
- }
- else
- {
- /*no action*/
- }
- #endif
- if (ehciInstance->ehciFrameList == NULL)
- {
- OSA_MemoryFree(ehciInstance);
- return kStatus_USB_Error;
- }
- #if (USB_HOST_CONFIG_EHCI == 1U)
- usbHostEhciData[0] = &s_UsbHostEhciData1;
- #elif (USB_HOST_CONFIG_EHCI == 2U)
- usbHostEhciData[0] = &s_UsbHostEhciData1;
- usbHostEhciData[1] = &s_UsbHostEhciData2;
- #else
- #error "Please increase the instance count."
- #endif
- temp = (void *)usbHostEhciData[instanceIndex];
- ehciInstance->ehciUnitBase = (uint32_t *)(temp);
- /* initialize qh/qtd/itd/sitd/iso list */
- ehciInstance->ehciQhList = (usb_host_ehci_qh_t *)((uint32_t)(ehciInstance->ehciUnitBase));
- ehciInstance->ehciQtdHead = (usb_host_ehci_qtd_t *)((uint32_t)ehciInstance->ehciQhList +
- (sizeof(usb_host_ehci_qh_t) * USB_HOST_CONFIG_EHCI_MAX_QH));
- ehciInstance->ehciItdList = (usb_host_ehci_itd_t *)((uint32_t)ehciInstance->ehciQtdHead +
- (sizeof(usb_host_ehci_qtd_t) * USB_HOST_CONFIG_EHCI_MAX_QTD));
- #if ((defined(USB_HOST_CONFIG_EHCI_MAX_ITD)) && (USB_HOST_CONFIG_EHCI_MAX_ITD > 0U))
- /* If one ITD's first 32 bytes and next 32 bytes are in different 4K region,
- * the ITD need move 32 bytes because the ITD cannot cross over 4K boundary.
- */
- index = ((((((uint32_t)(ehciInstance->ehciItdList)) + 4095U) & 0xFFFFF000U) -
- ((uint32_t)(ehciInstance->ehciItdList))) >>
- 5U);
- if (((index / 3U) < USB_HOST_CONFIG_EHCI_MAX_ITD) && ((index % 3U) == 1U))
- {
- ehciInstance->ehciItdList = (usb_host_ehci_itd_t *)(((uint32_t)(ehciInstance->ehciItdList)) + 32U);
- }
- #endif
- ehciInstance->ehciSitdIndexBase =
- (usb_host_ehci_sitd_t *)((uint32_t)ehciInstance->ehciItdList +
- (sizeof(usb_host_ehci_itd_t) * USB_HOST_CONFIG_EHCI_MAX_ITD));
- ehciInstance->ehciSitdList = ehciInstance->ehciSitdIndexBase;
- ehciInstance->ehciIsoList = (usb_host_ehci_iso_t *)((uint32_t)ehciInstance->ehciSitdList +
- (sizeof(usb_host_ehci_sitd_t) * USB_HOST_CONFIG_EHCI_MAX_SITD));
- ehciInstance->ehciPipeIndexBase =
- (usb_host_ehci_pipe_t *)((uint32_t)ehciInstance->ehciIsoList +
- (sizeof(usb_host_ehci_iso_t) * USB_HOST_EHCI_ISO_NUMBER));
- for (index = 1U; index < USB_HOST_CONFIG_EHCI_MAX_QH; ++index)
- {
- ehciInstance->ehciQhList[index - 1U].horizontalLinkPointer = (uint32_t)(&ehciInstance->ehciQhList[index]);
- }
- ehciInstance->ehciQhList[USB_HOST_CONFIG_EHCI_MAX_QH - 1U].horizontalLinkPointer = 0U;
- for (index = 1; index < USB_HOST_CONFIG_EHCI_MAX_QTD; ++index)
- {
- ehciInstance->ehciQtdHead[index - 1U].nextQtdPointer = (uint32_t)(&ehciInstance->ehciQtdHead[index]);
- }
- ehciInstance->ehciQtdNumber = USB_HOST_CONFIG_EHCI_MAX_QTD;
- ehciInstance->ehciQtdHead[USB_HOST_CONFIG_EHCI_MAX_QTD - 1U].nextQtdPointer = 0U;
- ehciInstance->ehciQtdTail = &ehciInstance->ehciQtdHead[USB_HOST_CONFIG_EHCI_MAX_QTD - 1U];
- #if ((defined USB_HOST_CONFIG_EHCI_MAX_ITD) && (USB_HOST_CONFIG_EHCI_MAX_ITD))
- for (index = 1; index < USB_HOST_CONFIG_EHCI_MAX_ITD; ++index)
- {
- ehciInstance->ehciItdList[index - 1U].nextItdPointer =
- (usb_host_ehci_itd_t *)(&ehciInstance->ehciItdList[index]);
- }
- ehciInstance->ehciItdNumber = USB_HOST_CONFIG_EHCI_MAX_ITD;
- ehciInstance->ehciItdList[USB_HOST_CONFIG_EHCI_MAX_ITD - 1U].nextItdPointer = NULL;
- #endif /* USB_HOST_CONFIG_EHCI_MAX_ITD */
- #if ((defined USB_HOST_CONFIG_EHCI_MAX_SITD) && (USB_HOST_CONFIG_EHCI_MAX_SITD))
- for (index = 1; index < USB_HOST_CONFIG_EHCI_MAX_SITD; ++index)
- {
- ehciInstance->ehciSitdList[index - 1U].nextLinkPointer = (uint32_t)(&ehciInstance->ehciSitdList[index]);
- }
- ehciInstance->ehciSitdNumber = USB_HOST_CONFIG_EHCI_MAX_SITD;
- ehciInstance->ehciSitdList[USB_HOST_CONFIG_EHCI_MAX_SITD - 1U].nextLinkPointer = 0U;
- #endif /* USB_HOST_CONFIG_EHCI_MAX_SITD */
- #if ((defined USB_HOST_CONFIG_EHCI_MAX_ITD) && (USB_HOST_CONFIG_EHCI_MAX_ITD))
- for (index = 1; index < USB_HOST_EHCI_ISO_NUMBER; ++index)
- {
- ehciInstance->ehciIsoList[index - 1U].next = &ehciInstance->ehciIsoList[index];
- }
- ehciInstance->ehciIsoList[USB_HOST_EHCI_ISO_NUMBER - 1U].next = NULL;
- #endif
- /* initialize pipes */
- ehciInstance->ehciPipeList = ehciInstance->ehciPipeIndexBase;
- for (index = 1; index < USB_HOST_CONFIG_MAX_PIPES; ++index)
- {
- temp = (void *)&ehciInstance->ehciPipeList[index];
- ehciInstance->ehciPipeList[index - 1U].pipeCommon.next = (usb_host_pipe_t *)temp;
- }
- /* initialize mutext */
- ehciInstance->ehciMutex = (osa_mutex_handle_t)(&ehciInstance->mutexBuffer[0]);
- osaStatus = OSA_MutexCreate(ehciInstance->ehciMutex);
- if (osaStatus != KOSA_StatusSuccess)
- {
- #ifdef HOST_ECHO
- usb_echo("ehci mutex init fail\r\n");
- #endif
- OSA_MemoryFree(ehciInstance);
- return kStatus_USB_Error;
- }
- /* initialize task event */
- ehciInstance->taskEventHandle = (osa_event_handle_t)&ehciInstance->taskEventHandleBuffer[0];
- osaStatus = OSA_EventCreate(ehciInstance->taskEventHandle, 1);
- if (osaStatus != KOSA_StatusSuccess)
- {
- #ifdef HOST_ECHO
- usb_echo("ehci event init fail\r\n");
- #endif
- (void)OSA_MutexDestroy(ehciInstance->ehciMutex);
- OSA_MemoryFree(ehciInstance);
- return kStatus_USB_Error;
- }
- /* initialize first qh */
- ehciInstance->shedFirstQh = ehciInstance->ehciQhList;
- ehciInstance->ehciQhList =
- (usb_host_ehci_qh_t *)(ehciInstance->ehciQhList->horizontalLinkPointer & EHCI_HOST_POINTER_ADDRESS_MASK);
- ehciInstance->shedFirstQh->staticEndpointStates[0] |= (1UL << EHCI_HOST_QH_H_SHIFT); /* first qh */
- ehciInstance->shedFirstQh->horizontalLinkPointer = EHCI_HOST_T_INVALID_VALUE;
- ehciInstance->shedFirstQh->currentQtdPointer = EHCI_HOST_T_INVALID_VALUE;
- ehciInstance->shedFirstQh->nextQtdPointer = EHCI_HOST_T_INVALID_VALUE;
- ehciInstance->shedFirstQh->alternateNextQtdPointer = EHCI_HOST_T_INVALID_VALUE;
- ehciInstance->shedFirstQh->horizontalLinkPointer =
- (uint32_t)((uint32_t)(ehciInstance->shedFirstQh) | EHCI_HOST_POINTER_TYPE_QH);
- /* initialize periodic list */
- temp = (void *)ehciInstance->ehciFrameList;
- framePointer = (uint32_t *)temp;
- for (index = 0; index < USB_HOST_CONFIG_EHCI_FRAME_LIST_SIZE; ++index)
- {
- framePointer[index] = EHCI_HOST_T_INVALID_VALUE;
- }
- (void)USB_HostEhciStartIP(ehciInstance); /* start ehci ip */
- *controllerHandle = ehciInstance;
- return kStatus_USB_Success;
- }
- usb_status_t USB_HostEhciDestory(usb_host_controller_handle controllerHandle)
- {
- usb_host_ehci_instance_t *ehciInstance = (usb_host_ehci_instance_t *)controllerHandle;
- /* disable all interrupts */
- ehciInstance->ehciIpBase->USBINTR = 0;
- /* stop the controller */
- ehciInstance->ehciIpBase->USBCMD = 0;
- /* free memory */
- #if (USB_HOST_CONFIG_EHCI == 1U)
- if (ehciInstance->ehciFrameList == &s_UsbHostEhciFrameList1[0])
- {
- usbHostEhciFramListStatus[0] = 0;
- }
- #elif (USB_HOST_CONFIG_EHCI == 2U)
- if (ehciInstance->ehciFrameList == &s_UsbHostEhciFrameList1[0])
- {
- usbHostEhciFramListStatus[0] = 0;
- }
- else if (ehciInstance->ehciFrameList == &s_UsbHostEhciFrameList2[0])
- {
- usbHostEhciFramListStatus[1] = 0;
- }
- else
- {
- /*no action*/
- }
- #endif
- (void)OSA_MutexDestroy(ehciInstance->ehciMutex);
- (void)OSA_EventDestroy(ehciInstance->taskEventHandle);
- OSA_MemoryFree(ehciInstance);
- return kStatus_USB_Success;
- }
- usb_status_t USB_HostEhciOpenPipe(usb_host_controller_handle controllerHandle,
- usb_host_pipe_handle *pipeHandle,
- usb_host_pipe_init_t *pipeInit)
- {
- usb_host_ehci_pipe_t *ehciPipePointer = NULL;
- usb_status_t status;
- uint32_t speed = 0;
- usb_host_ehci_instance_t *ehciInstance = (usb_host_ehci_instance_t *)controllerHandle;
- uint32_t val32;
- void *temp;
- /* get one pipe */
- USB_HostEhciLock();
- if (ehciInstance->ehciPipeList != NULL)
- {
- ehciPipePointer = ehciInstance->ehciPipeList;
- temp = (void *)ehciPipePointer->pipeCommon.next;
- ehciInstance->ehciPipeList = (usb_host_ehci_pipe_t *)temp;
- }
- USB_HostEhciUnlock();
- if (ehciPipePointer == NULL)
- {
- #ifdef HOST_ECHO
- usb_echo("ehci open pipe failed\r\n");
- #endif
- return kStatus_USB_Busy;
- }
- /* initialize pipe informations */
- USB_HostEhciZeroMem((void *)ehciPipePointer, sizeof(usb_host_ehci_pipe_t) / 4U);
- ehciPipePointer->pipeCommon.deviceHandle = pipeInit->devInstance;
- ehciPipePointer->pipeCommon.endpointAddress = pipeInit->endpointAddress;
- ehciPipePointer->pipeCommon.direction = pipeInit->direction;
- ehciPipePointer->pipeCommon.interval = pipeInit->interval;
- ehciPipePointer->pipeCommon.maxPacketSize = pipeInit->maxPacketSize;
- ehciPipePointer->pipeCommon.pipeType = pipeInit->pipeType;
- ehciPipePointer->pipeCommon.numberPerUframe = pipeInit->numberPerUframe + 1U;
- if (ehciPipePointer->pipeCommon.numberPerUframe > 3U)
- {
- ehciPipePointer->pipeCommon.numberPerUframe = 3U;
- }
- ehciPipePointer->pipeCommon.nakCount = pipeInit->nakCount;
- ehciPipePointer->pipeCommon.nextdata01 = 0U;
- ehciPipePointer->ehciQh = NULL;
- (void)USB_HostHelperGetPeripheralInformation(ehciPipePointer->pipeCommon.deviceHandle,
- (uint32_t)kUSB_HostGetDeviceSpeed, &speed);
- if ((ehciPipePointer->pipeCommon.pipeType == USB_ENDPOINT_ISOCHRONOUS) ||
- (ehciPipePointer->pipeCommon.pipeType == USB_ENDPOINT_INTERRUPT))
- {
- if (ehciPipePointer->pipeCommon.pipeType == USB_ENDPOINT_ISOCHRONOUS)
- {
- ehciPipePointer->pipeCommon.interval =
- (uint16_t)(1UL << (ehciPipePointer->pipeCommon.interval - 1U)); /* iso interval is the power of 2 */
- }
- else
- {
- if (speed == USB_SPEED_HIGH)
- {
- ehciPipePointer->pipeCommon.interval = (uint16_t)(
- 1UL << (ehciPipePointer->pipeCommon.interval - 1U)); /* HS interrupt interval is the power of 2 */
- }
- else
- {
- ehciPipePointer->pipeCommon.interval = USB_HostEhciGet2PowerValue(
- (uint8_t)ehciPipePointer->pipeCommon.interval); /* FS/LS interrupt interval should be the power of
- 2, it is used for ehci bandwidth */
- }
- }
- if ((speed == USB_SPEED_HIGH) && (ehciPipePointer->pipeCommon.interval < 8U))
- {
- val32 = ehciInstance->ehciIpBase->USBCMD;
- val32 &= (~USBHS_USBCMD_ITC_MASK);
- val32 |= USBHS_USBCMD_ITC((ehciPipePointer->pipeCommon.interval));
- ehciInstance->ehciIpBase->USBCMD = val32;
- }
- }
- /* save the micro-frame interval, it is convenient for the interval process */
- if (speed == USB_SPEED_HIGH)
- {
- ehciPipePointer->uframeInterval = ehciPipePointer->pipeCommon.interval;
- }
- else
- {
- ehciPipePointer->uframeInterval = 8U * ehciPipePointer->pipeCommon.interval;
- }
- /* open pipe */
- switch (ehciPipePointer->pipeCommon.pipeType)
- {
- case USB_ENDPOINT_CONTROL:
- case USB_ENDPOINT_BULK:
- status = USB_HostEhciOpenControlBulk(ehciInstance, ehciPipePointer);
- break;
- #if (((defined USB_HOST_CONFIG_EHCI_MAX_ITD) && (USB_HOST_CONFIG_EHCI_MAX_ITD)) || \
- ((defined USB_HOST_CONFIG_EHCI_MAX_SITD) && (USB_HOST_CONFIG_EHCI_MAX_SITD)))
- case USB_ENDPOINT_ISOCHRONOUS:
- status = USB_HostEhciOpenIso(ehciInstance, ehciPipePointer);
- break;
- #endif
- case USB_ENDPOINT_INTERRUPT:
- status = USB_HostEhciOpenInterrupt(ehciInstance, ehciPipePointer);
- break;
- default:
- status = kStatus_USB_Error;
- break;
- }
- if (status != kStatus_USB_Success)
- {
- /* release pipe */
- USB_HostEhciLock();
- temp = (void *)ehciInstance->ehciPipeList;
- ehciPipePointer->pipeCommon.next = (usb_host_pipe_t *)temp;
- ehciInstance->ehciPipeList = ehciPipePointer;
- USB_HostEhciUnlock();
- return status;
- }
- /* add pipe to run pipe list */
- USB_HostEhciLock();
- temp = (void *)ehciInstance->ehciRunningPipeList;
- ehciPipePointer->pipeCommon.next = (usb_host_pipe_t *)temp;
- ehciInstance->ehciRunningPipeList = ehciPipePointer;
- USB_HostEhciUnlock();
- *pipeHandle = ehciPipePointer;
- return status;
- }
- usb_status_t USB_HostEhciClosePipe(usb_host_controller_handle controllerHandle, usb_host_pipe_handle pipeHandle)
- {
- usb_host_ehci_instance_t *ehciInstance = (usb_host_ehci_instance_t *)controllerHandle;
- usb_host_ehci_pipe_t *ehciPipePointer = (usb_host_ehci_pipe_t *)pipeHandle;
- usb_host_pipe_t *prevPointer = NULL;
- void *temp;
- void *tempCurrent;
- switch (ehciPipePointer->pipeCommon.pipeType)
- {
- case USB_ENDPOINT_BULK:
- case USB_ENDPOINT_CONTROL:
- (void)USB_HostEhciCloseControlBulk(ehciInstance, ehciPipePointer);
- break;
- case USB_ENDPOINT_INTERRUPT:
- (void)USB_HostEhciCloseInterrupt(ehciInstance, ehciPipePointer);
- break;
- #if (((defined USB_HOST_CONFIG_EHCI_MAX_ITD) && (USB_HOST_CONFIG_EHCI_MAX_ITD)) || \
- ((defined USB_HOST_CONFIG_EHCI_MAX_SITD) && (USB_HOST_CONFIG_EHCI_MAX_SITD)))
- case USB_ENDPOINT_ISOCHRONOUS:
- (void)USB_HostEhciCloseIso(ehciInstance, ehciPipePointer);
- break;
- #endif
- default:
- /*no action*/
- break;
- }
- /* delete pipe from run pipe list */
- USB_HostEhciLock();
- temp = (void *)ehciInstance->ehciRunningPipeList;
- prevPointer = (usb_host_pipe_t *)temp;
- tempCurrent = (void *)ehciPipePointer;
- if (prevPointer == (usb_host_pipe_t *)tempCurrent)
- {
- temp = (void *)prevPointer->next;
- ehciInstance->ehciRunningPipeList = (usb_host_ehci_pipe_t *)(temp);
- }
- else
- {
- while (prevPointer != NULL)
- {
- temp = (void *)ehciPipePointer;
- if (prevPointer->next == (usb_host_pipe_t *)temp)
- {
- prevPointer->next = ehciPipePointer->pipeCommon.next;
- break;
- }
- else
- {
- prevPointer = prevPointer->next;
- }
- }
- }
- USB_HostEhciUnlock();
- /* release pipe */
- USB_HostEhciLock();
- temp = (void *)ehciInstance->ehciPipeList;
- ehciPipePointer->pipeCommon.next = (usb_host_pipe_t *)temp;
- ehciInstance->ehciPipeList = ehciPipePointer;
- USB_HostEhciUnlock();
- return kStatus_USB_Success;
- }
- usb_status_t USB_HostEhciWritePipe(usb_host_controller_handle controllerHandle,
- usb_host_pipe_handle pipeHandle,
- usb_host_transfer_t *transfer)
- {
- usb_host_ehci_instance_t *ehciInstance = (usb_host_ehci_instance_t *)controllerHandle;
- usb_host_ehci_pipe_t *ehciPipePointer = (usb_host_ehci_pipe_t *)pipeHandle;
- usb_status_t status = kStatus_USB_Success;
- #if (((defined USB_HOST_CONFIG_EHCI_MAX_ITD) && (USB_HOST_CONFIG_EHCI_MAX_ITD)) || \
- ((defined USB_HOST_CONFIG_EHCI_MAX_SITD) && (USB_HOST_CONFIG_EHCI_MAX_SITD)))
- uint32_t speed = 0U;
- #endif
- switch (ehciPipePointer->pipeCommon.pipeType)
- {
- case USB_ENDPOINT_BULK:
- case USB_ENDPOINT_CONTROL:
- case USB_ENDPOINT_INTERRUPT:
- status = USB_HostEhciQhQtdListInit(ehciInstance, ehciPipePointer,
- transfer); /* initialize qtd for control/bulk transfer */
- break;
- #if (((defined USB_HOST_CONFIG_EHCI_MAX_ITD) && (USB_HOST_CONFIG_EHCI_MAX_ITD)) || \
- ((defined USB_HOST_CONFIG_EHCI_MAX_SITD) && (USB_HOST_CONFIG_EHCI_MAX_SITD)))
- case USB_ENDPOINT_ISOCHRONOUS:
- (void)USB_HostHelperGetPeripheralInformation(ehciPipePointer->pipeCommon.deviceHandle,
- (uint32_t)kUSB_HostGetDeviceSpeed, &speed);
- if (speed == USB_SPEED_HIGH)
- {
- #if ((defined USB_HOST_CONFIG_EHCI_MAX_ITD) && (USB_HOST_CONFIG_EHCI_MAX_ITD))
- status = USB_HostEhciItdArrayInit(ehciInstance, ehciPipePointer,
- transfer); /* initialize itd for iso transfer */
- #endif
- }
- else
- {
- #if ((defined USB_HOST_CONFIG_EHCI_MAX_SITD) && (USB_HOST_CONFIG_EHCI_MAX_SITD))
- status = USB_HostEhciSitdArrayInit(ehciInstance, ehciPipePointer,
- transfer); /* initialize sitd for iso transfer */
- #endif
- }
- break;
- #endif
- default:
- /*no action*/
- break;
- }
- return status;
- }
- usb_status_t USB_HostEhciReadpipe(usb_host_controller_handle controllerHandle,
- usb_host_pipe_handle pipeHandle,
- usb_host_transfer_t *transfer)
- {
- return USB_HostEhciWritePipe(controllerHandle, pipeHandle, transfer); /* same as write */
- }
- usb_status_t USB_HostEhciIoctl(usb_host_controller_handle controllerHandle, uint32_t ioctlEvent, void *ioctlParam)
- {
- usb_status_t status = kStatus_USB_Success;
- usb_host_ehci_instance_t *ehciInstance = (usb_host_ehci_instance_t *)controllerHandle;
- usb_host_cancel_param_t *param;
- usb_host_ehci_pipe_t *ehciPipePointer;
- volatile usb_host_ehci_qh_t *vltQhPointer;
- uint32_t deviceAddress = 0;
- usb_host_controller_control_t controlCode = (usb_host_controller_control_t)ioctlEvent;
- if (controllerHandle == NULL)
- {
- return kStatus_USB_InvalidHandle;
- }
- switch (controlCode)
- {
- case kUSB_HostCancelTransfer: /* cancel pipe or one transfer */
- param = (usb_host_cancel_param_t *)ioctlParam;
- status = USB_HostEhciCancelPipe(ehciInstance, (usb_host_ehci_pipe_t *)param->pipeHandle, param->transfer);
- break;
- case kUSB_HostBusControl: /* bus control */
- status = USB_HostEhciControlBus(ehciInstance, *((uint8_t *)ioctlParam));
- break;
- case kUSB_HostGetFrameNumber: /* get frame number */
- *((uint32_t *)ioctlParam) = ((ehciInstance->ehciIpBase->FRINDEX & EHCI_MAX_UFRAME_VALUE) >> 3);
- break;
- case kUSB_HostUpdateControlEndpointAddress:
- ehciPipePointer = (usb_host_ehci_pipe_t *)ioctlParam;
- vltQhPointer = (volatile usb_host_ehci_qh_t *)ehciPipePointer->ehciQh;
- /* update address */
- (void)USB_HostHelperGetPeripheralInformation(ehciPipePointer->pipeCommon.deviceHandle,
- (uint32_t)kUSB_HostGetDeviceAddress, &deviceAddress);
- vltQhPointer->staticEndpointStates[0] |= deviceAddress;
- USB_HostEhciDelay(ehciInstance->ehciIpBase, 2U);
- break;
- case kUSB_HostUpdateControlPacketSize:
- ehciPipePointer = (usb_host_ehci_pipe_t *)ioctlParam;
- vltQhPointer = (volatile usb_host_ehci_qh_t *)ehciPipePointer->ehciQh;
- USB_HostEhciLock();
- if (0U != (ehciInstance->ehciIpBase->USBSTS & USBHS_USBSTS_AS_MASK))
- {
- USB_HostEhciStopAsync(ehciInstance);
- /* update max packet size */
- vltQhPointer->staticEndpointStates[0] =
- (((vltQhPointer->staticEndpointStates[0]) & (~EHCI_HOST_QH_MAX_PACKET_LENGTH_MASK)) |
- ((uint32_t)ehciPipePointer->pipeCommon.maxPacketSize << EHCI_HOST_QH_MAX_PACKET_LENGTH_SHIFT));
- USB_HostEhciStartAsync(ehciInstance);
- }
- else
- {
- /* update max packet size */
- vltQhPointer->staticEndpointStates[0] =
- (((vltQhPointer->staticEndpointStates[0]) & (~EHCI_HOST_QH_MAX_PACKET_LENGTH_MASK)) |
- ((uint32_t)ehciPipePointer->pipeCommon.maxPacketSize << EHCI_HOST_QH_MAX_PACKET_LENGTH_SHIFT));
- }
- USB_HostEhciUnlock();
- break;
- #if ((defined USB_HOST_CONFIG_COMPLIANCE_TEST) && (USB_HOST_CONFIG_COMPLIANCE_TEST))
- case kUSB_HostTestModeInit: /* test mode control */
- USB_HostEhciTestModeInit((usb_host_device_instance_t *)ioctlParam);
- break;
- #endif
- default:
- status = kStatus_USB_NotSupported;
- break;
- }
- return status;
- }
- void USB_HostEhciTaskFunction(void *hostHandle)
- {
- usb_host_ehci_instance_t *ehciInstance;
- uint32_t bitSet;
- usb_device_handle deviceHandle;
- if (hostHandle == NULL)
- {
- return;
- }
- ehciInstance = (usb_host_ehci_instance_t *)((usb_host_instance_t *)hostHandle)->controllerHandle;
- if (OSA_EventWait(ehciInstance->taskEventHandle, 0xFF, 0, USB_OSA_WAIT_TIMEOUT, &bitSet) ==
- KOSA_StatusSuccess) /* wait all event */
- {
- if (0U != (bitSet & EHCI_TASK_EVENT_PORT_CHANGE)) /* port change */
- {
- USB_HostEhciPortChange(ehciInstance);
- }
- if (0U != (bitSet & EHCI_TASK_EVENT_TIMER0)) /* timer0 */
- {
- USB_HostEhciTimer0(ehciInstance);
- }
- #if ((defined(USB_HOST_CONFIG_LOW_POWER_MODE)) && (USB_HOST_CONFIG_LOW_POWER_MODE > 0U))
- if (0U != (bitSet & EHCI_TASK_EVENT_TIMER1)) /* timer1 */
- {
- USB_HostEhciTimer1(ehciInstance);
- }
- #endif
- if ((ehciInstance->deviceAttached == (uint8_t)kEHCIDeviceAttached))
- {
- if (0U != (bitSet & EHCI_TASK_EVENT_TRANSACTION_DONE)) /* transaction done */
- {
- USB_HostEhciTransactionDone(ehciInstance);
- }
- if (0U != (bitSet & EHCI_TASK_EVENT_DEVICE_DETACH)) /* device detach */
- {
- ehciInstance->ehciIpBase->USBINTR &=
- (~USBHS_USBINTR_PCE_MASK); /* disable attach, enable when the detach process is done */
- ehciInstance->deviceAttached = (uint8_t)kEHCIDeviceDetached;
- (void)USB_HostDetachDevice(ehciInstance->hostHandle, 0, 0);
- }
- }
- else if (ehciInstance->deviceAttached != (uint8_t)kEHCIDeviceAttached)
- {
- if (0U != (bitSet & EHCI_TASK_EVENT_DEVICE_ATTACH)) /* device is attached */
- {
- USB_HostEhciStartAsync(ehciInstance);
- USB_HostEhciStartPeriodic(ehciInstance);
- if (USB_HostAttachDevice(ehciInstance->hostHandle, ehciInstance->firstDeviceSpeed, 0, 0, 1,
- &deviceHandle) == kStatus_USB_Success)
- {
- ehciInstance->deviceAttached = (uint8_t)kEHCIDeviceAttached;
- }
- }
- }
- else
- {
- /*no action*/
- }
- }
- }
- void USB_HostEhciIsrFunction(void *hostHandle)
- {
- usb_host_ehci_instance_t *ehciInstance;
- static uint32_t interruptStatus = 0;
- if (hostHandle == NULL)
- {
- return;
- }
- ehciInstance = (usb_host_ehci_instance_t *)((usb_host_instance_t *)hostHandle)->controllerHandle;
- #if ((defined(USB_HOST_CONFIG_LOW_POWER_MODE)) && (USB_HOST_CONFIG_LOW_POWER_MODE > 0U))
- #if (defined(FSL_FEATURE_SOC_USBNC_COUNT) && (FSL_FEATURE_SOC_USBNC_COUNT > 0U))
- if (0U != (ehciInstance->registerNcBase->USB_OTGn_CTRL & USBNC_USB_OTGn_CTRL_WIE_MASK))
- {
- usb_host_instance_t *hostPointer = (usb_host_instance_t *)ehciInstance->hostHandle;
- ehciInstance->registerNcBase->USB_OTGn_CTRL &= ~USBNC_USB_OTGn_CTRL_WIE_MASK;
- (void)hostPointer->deviceCallback(hostPointer->suspendedDevice, NULL,
- kUSB_HostEventDetectResume); /* call host callback function */
- #if (defined(USBNC_USB_OTGn_PHY_CTRL_0_UTMI_CLK_VLD_MASK))
- while (0U == (ehciInstance->registerNcBase->USB_OTGn_PHY_CTRL_0 & USBNC_USB_OTGn_PHY_CTRL_0_UTMI_CLK_VLD_MASK))
- {
- }
- #endif
- if (0U != (ehciInstance->ehciIpBase->PORTSC1 & USBHS_PORTSC1_CCS_MASK))
- {
- USB_HostEhciStartAsync(ehciInstance);
- USB_HostEhciStartPeriodic(ehciInstance);
- }
- ehciInstance->ehciIpBase->USBCMD |= (USBHS_USBCMD_RS_MASK);
- if ((kBus_EhciSuspended == ehciInstance->busSuspendStatus))
- {
- /* ehciInstance->ehciIpBase->PORTSC1 |= USBHS_PORTSC1_FPR_MASK; */
- ehciInstance->busSuspendStatus = kBus_EhciStartResume;
- }
- else
- {
- /*no action*/
- }
- }
- else
- {
- /*no action*/
- }
- #else
- if (0U != (ehciInstance->ehciIpBase->USBGENCTRL & USBHS_USBGENCTRL_WU_IE_MASK))
- {
- usb_host_instance_t *hostPointer = (usb_host_instance_t *)ehciInstance->hostHandle;
- (void)hostPointer->deviceCallback(hostPointer->suspendedDevice, NULL,
- kUSB_HostEventDetectResume); /* call host callback function */
- while (0U == (USBPHY->PLL_SIC & USBPHY_PLL_SIC_PLL_LOCK_MASK))
- {
- }
- ehciInstance->ehciIpBase->USBGENCTRL |= USBHS_USBGENCTRL_WU_INT_CLR_MASK;
- ehciInstance->ehciIpBase->USBGENCTRL &= ~USBHS_USBGENCTRL_WU_IE_MASK;
- if (0U != (ehciInstance->ehciIpBase->PORTSC1 & USBHS_PORTSC1_CCS_MASK))
- {
- USB_HostEhciStartAsync(ehciInstance);
- USB_HostEhciStartPeriodic(ehciInstance);
- }
- ehciInstance->ehciIpBase->USBCMD |= (USBHS_USBCMD_RS_MASK);
- if ((kBus_EhciSuspended == ehciInstance->busSuspendStatus))
- {
- ehciInstance->busSuspendStatus = kBus_EhciStartResume;
- /*ehciInstance->ehciIpBase->PORTSC1 |= USBHS_PORTSC1_FPR_MASK; */
- }
- else
- {
- /*no action*/
- }
- }
- else
- {
- /*no action*/
- }
- #endif /* FSL_FEATURE_SOC_USBNC_COUNT */
- #endif /* USB_HOST_CONFIG_LOW_POWER_MODE */
- interruptStatus = ehciInstance->ehciIpBase->USBSTS;
- interruptStatus &= ehciInstance->ehciIpBase->USBINTR;
- while (0U != interruptStatus) /* there are usb interrupts */
- {
- ehciInstance->ehciIpBase->USBSTS = interruptStatus; /* clear interrupt */
- if (0U != (interruptStatus & USBHS_USBSTS_SRI_MASK)) /* SOF interrupt */
- {
- }
- if (0U != (interruptStatus & USBHS_USBSTS_SEI_MASK)) /* system error interrupt */
- {
- }
- if ((0U != (interruptStatus & USBHS_USBSTS_UI_MASK)) ||
- (0U != (interruptStatus & USBHS_USBSTS_UEI_MASK))) /* USB interrupt or USB error interrupt */
- {
- (void)OSA_EventSet(ehciInstance->taskEventHandle, EHCI_TASK_EVENT_TRANSACTION_DONE);
- }
- if (0U != (interruptStatus & USBHS_USBSTS_PCI_MASK)) /* port change detect interrupt */
- {
- #if ((defined(USB_HOST_CONFIG_LOW_POWER_MODE)) && (USB_HOST_CONFIG_LOW_POWER_MODE > 0U))
- usb_host_instance_t *hostPointer = (usb_host_instance_t *)ehciInstance->hostHandle;
- if (0U != (ehciInstance->ehciIpBase->PORTSC1 & USBHS_PORTSC1_FPR_MASK))
- {
- if (kBus_EhciStartSuspend == ehciInstance->busSuspendStatus)
- {
- if (0U != (ehciInstance->ehciIpBase->PORTSC1 & USBHS_PORTSC1_CCS_MASK))
- {
- USB_HostEhciStartAsync(ehciInstance);
- USB_HostEhciStartPeriodic(ehciInstance);
- }
- (void)hostPointer->deviceCallback(hostPointer->suspendedDevice, NULL,
- kUSB_HostEventNotSuspended); /* call host callback function */
- hostPointer->suspendedDevice = NULL;
- ehciInstance->busSuspendStatus = kBus_EhciIdle;
- ehciInstance->ehciIpBase->USBINTR &= ~(USBHS_USBINTR_TIE1_MASK);
- }
- else
- {
- /*no action */
- }
- }
- #endif
- (void)OSA_EventSet(ehciInstance->taskEventHandle, EHCI_TASK_EVENT_PORT_CHANGE);
- }
- if (0U != (interruptStatus & USBHS_USBSTS_TI0_MASK)) /* timer 0 interrupt */
- {
- (void)OSA_EventSet(ehciInstance->taskEventHandle, EHCI_TASK_EVENT_TIMER0);
- }
- #if ((defined(USB_HOST_CONFIG_LOW_POWER_MODE)) && (USB_HOST_CONFIG_LOW_POWER_MODE > 0U))
- if (0U != (interruptStatus & USBHS_USBSTS_TI1_MASK)) /* timer 1 interrupt */
- {
- (void)OSA_EventSet(ehciInstance->taskEventHandle, EHCI_TASK_EVENT_TIMER1);
- }
- #endif
- interruptStatus = ehciInstance->ehciIpBase->USBSTS;
- interruptStatus &= ehciInstance->ehciIpBase->USBINTR;
- }
- }
- #endif /* USB_HOST_CONFIG_EHCI */
|