am_hal_iom.c 176 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253
  1. //*****************************************************************************
  2. //
  3. // am_hal_iom.c
  4. //! @file
  5. //!
  6. //! @brief Functions for interfacing with the IO Master module
  7. //!
  8. //! @addtogroup iom2 IO Master (SPI/I2C)
  9. //! @ingroup apollo2hal
  10. //! @{
  11. //
  12. //*****************************************************************************
  13. //*****************************************************************************
  14. //
  15. // Copyright (c) 2017, Ambiq Micro
  16. // All rights reserved.
  17. //
  18. // Redistribution and use in source and binary forms, with or without
  19. // modification, are permitted provided that the following conditions are met:
  20. //
  21. // 1. Redistributions of source code must retain the above copyright notice,
  22. // this list of conditions and the following disclaimer.
  23. //
  24. // 2. Redistributions in binary form must reproduce the above copyright
  25. // notice, this list of conditions and the following disclaimer in the
  26. // documentation and/or other materials provided with the distribution.
  27. //
  28. // 3. Neither the name of the copyright holder nor the names of its
  29. // contributors may be used to endorse or promote products derived from this
  30. // software without specific prior written permission.
  31. //
  32. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  33. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  34. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  36. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  37. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  38. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  39. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  40. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  42. // POSSIBILITY OF SUCH DAMAGE.
  43. //
  44. // This is part of revision 1.2.11 of the AmbiqSuite Development Package.
  45. //
  46. //*****************************************************************************
  47. #include <stdint.h>
  48. #include <stdbool.h>
  49. #include "am_mcu_apollo.h"
  50. #ifdef __IAR_SYSTEMS_ICC__
  51. #define AM_INSTR_CLZ(n) __CLZ(n)
  52. #else
  53. #define AM_INSTR_CLZ(n) __builtin_clz(n)
  54. #endif
  55. //! ASSERT(1) or Correct(0) invalid IOM R/W Thresholds.
  56. #ifndef AM_ASSERT_INVALID_THRESHOLD
  57. #define AM_ASSERT_INVALID_THRESHOLD (1)
  58. #endif
  59. //*****************************************************************************
  60. //
  61. // Forcing optimizations
  62. //
  63. // These pragmas must be enabled if we intend to use the IOM4 workaround with a
  64. // delay higher than 18-bits in the first word.
  65. //
  66. //*****************************************************************************
  67. //#ifdef __IAR_SYSTEMS_ICC__
  68. //#pragma optimize=3 s
  69. //#endif
  70. //
  71. //#ifdef __ARMCC_VERSION
  72. //#pragma O3
  73. //#endif
  74. //
  75. //#ifdef __GNUC__
  76. //#pragma GCC optimize ("O3")
  77. //#endif
  78. //*****************************************************************************
  79. //
  80. // Forward declarations.
  81. //
  82. //*****************************************************************************
  83. static void iom_workaround_loop(uint32_t ui32PadRegVal,
  84. volatile uint32_t *pui32PadReg,
  85. bool bRising);
  86. static uint32_t
  87. internal_am_hal_iom_spi_cmd_construct(uint32_t ui32Operation,
  88. uint32_t ui32ChipSelect,
  89. uint32_t ui32NumBytes,
  90. uint32_t ui32Options);
  91. static am_hal_iom_status_e
  92. internal_iom_wait_i2c_scl_hi(uint32_t ui32Module);
  93. //*****************************************************************************
  94. //
  95. // IOM Buffer states.
  96. //
  97. //*****************************************************************************
  98. #define BUFFER_IDLE 0x0
  99. #define BUFFER_SENDING 0x1
  100. #define BUFFER_RECEIVING 0x2
  101. //*****************************************************************************
  102. //
  103. // Global state variables
  104. //
  105. //*****************************************************************************
  106. //
  107. // Define a structure to map CE for IOM4 only.
  108. //
  109. typedef struct
  110. {
  111. uint8_t channel; // CE channel for SPI
  112. uint8_t pad; // GPIO Pad
  113. uint8_t funcsel; // FNCSEL value
  114. } IOMPad_t;
  115. // Define the mapping between SPI CEn, Pads, and FNCSEL values for all IOMs.
  116. const IOMPad_t g_IOMPads[] =
  117. {
  118. {0, 29, 6}, {0, 34, 6}, {1, 18, 4}, {1, 37, 5}, {2, 41, 6},
  119. {3, 17, 4}, {3, 45, 4}, {4, 10, 6}, {4, 46, 6}, {5, 9, 4},
  120. {5, 47, 6}, {6, 35, 4}, {7, 38, 6}
  121. };
  122. typedef struct
  123. {
  124. uint8_t module; // IOM module
  125. uint8_t pad; // GPIO Pad
  126. uint8_t funcsel; // FNCSEL value
  127. } I2CPad_t;
  128. // Define the mapping between I2C SCL Pads, and FNCSEL values for all IOMs.
  129. const I2CPad_t g_I2CPads[] =
  130. {
  131. {0, 5, 0},
  132. {1, 8, 0},
  133. {2, 0, 7},
  134. {3, 42, 4},
  135. {4, 39, 4},
  136. {5, 48, 4},
  137. {2, 27, 4},
  138. };
  139. // Defines for IOM 4 Workaround
  140. #define WORKAROUND_IOM 4
  141. #define WORKAROUND_IOM_MOSI_PIN 44
  142. #define WORKAROUND_IOM_MOSI_CFG AM_HAL_PIN_44_M4MOSI
  143. #define MAX_IOM_BITS 9
  144. #define IOM_OVERHEAD_FACTOR 2
  145. //*****************************************************************************
  146. //
  147. // Non-blocking buffer and buffer-management variables.
  148. //
  149. //*****************************************************************************
  150. typedef struct
  151. {
  152. uint32_t ui32State;
  153. uint32_t *pui32Data;
  154. uint32_t ui32BytesLeft;
  155. uint32_t ui32Options;
  156. void (*pfnCallback)(void);
  157. }
  158. am_hal_iom_nb_buffer;
  159. //
  160. // Global State to keep track if there is an ongoing transaction
  161. //
  162. volatile bool g_bIomBusy[AM_REG_IOMSTR_NUM_MODULES + 1] = {0};
  163. am_hal_iom_nb_buffer g_psIOMBuffers[AM_REG_IOMSTR_NUM_MODULES];
  164. //
  165. // Save error status from non-blocking calls
  166. //
  167. static am_hal_iom_status_e g_iom_error_status[AM_REG_IOMSTR_NUM_MODULES + 1];
  168. //*****************************************************************************
  169. //
  170. // Computed timeout.
  171. //
  172. // The IOM may not always respond to events (e.g., CMDCMP). This is a
  173. // timeout value in cycles to be used when waiting on status changes.
  174. //*****************************************************************************
  175. uint32_t ui32StatusTimeout[AM_REG_IOMSTR_NUM_MODULES];
  176. //*****************************************************************************
  177. //
  178. // Queue management variables.
  179. //
  180. //*****************************************************************************
  181. am_hal_queue_t g_psIOMQueue[AM_REG_IOMSTR_NUM_MODULES];
  182. //*****************************************************************************
  183. //
  184. // Default queue flush function
  185. //
  186. //*****************************************************************************
  187. am_hal_iom_queue_flush_t am_hal_iom_queue_flush = am_hal_iom_sleeping_queue_flush;
  188. //*****************************************************************************
  189. //
  190. // Power management structure.
  191. //
  192. //*****************************************************************************
  193. am_hal_iom_pwrsave_t am_hal_iom_pwrsave[AM_REG_IOMSTR_NUM_MODULES];
  194. //*****************************************************************************
  195. //
  196. // I2C BitBang to IOM error mapping
  197. //
  198. //*****************************************************************************
  199. const am_hal_iom_status_e i2c_bb_errmap[AM_HAL_I2C_BIT_BANG_STATUS_MAX] =
  200. {
  201. AM_HAL_IOM_SUCCESS, // AM_HAL_I2C_BIT_BANG_SUCCESS,
  202. AM_HAL_IOM_ERR_I2C_NAK, // AM_HAL_I2C_BIT_BANG_ADDRESS_NAKED,
  203. AM_HAL_IOM_ERR_I2C_NAK, // AM_HAL_I2C_BIT_BANG_DATA_NAKED,
  204. AM_HAL_IOM_ERR_TIMEOUT, // AM_HAL_I2C_BIT_BANG_CLOCK_TIMEOUT,
  205. AM_HAL_IOM_ERR_TIMEOUT, // AM_HAL_I2C_BIT_BANG_DATA_TIMEOUT,
  206. };
  207. //*****************************************************************************
  208. //
  209. // Static helper functions
  210. //
  211. //*****************************************************************************
  212. //*****************************************************************************
  213. //
  214. // Get the error interrupt staus
  215. //
  216. //*****************************************************************************
  217. //
  218. //! @brief Returns the error status based on interrupt bits
  219. //!
  220. //! @param ui32Module - IOM module
  221. //! ui32Status - Currently accumulated error status
  222. //!
  223. //! The function looks at the supplied interrupt error status bits and the
  224. //! the current INTSTAT and maps the same to the enum am_hal_iom_status_e.
  225. //!
  226. //! @return Returns the appropriate error status in the form of am_hal_iom_status_e
  227. //!
  228. //*****************************************************************************
  229. static am_hal_iom_status_e
  230. internal_iom_get_int_err(uint32_t ui32Module, uint32_t ui32IntStatus)
  231. {
  232. am_hal_iom_status_e ui32Status = AM_HAL_IOM_SUCCESS;
  233. //
  234. // Let's accumulate the errors
  235. //
  236. ui32IntStatus |= am_hal_iom_int_status_get(ui32Module, false);
  237. if (ui32IntStatus & AM_HAL_IOM_INT_SWERR)
  238. {
  239. // Error in hardware command issued or illegal access by SW
  240. ui32Status = AM_HAL_IOM_ERR_INVALID_OPER;
  241. }
  242. else if (ui32IntStatus & AM_HAL_IOM_INT_I2CARBERR)
  243. {
  244. // Loss of I2C multi-master arbitration
  245. ui32Status = AM_HAL_IOM_ERR_I2C_ARB;
  246. }
  247. else if (ui32IntStatus & AM_HAL_IOM_INT_NAK)
  248. {
  249. // I2C NAK
  250. ui32Status = AM_HAL_IOM_ERR_I2C_NAK;
  251. }
  252. return ui32Status;
  253. }
  254. //*****************************************************************************
  255. // onebit()
  256. //*****************************************************************************
  257. //
  258. // A power of 2?
  259. // Return true if ui32Value has exactly 1 bit set, otherwise false.
  260. //
  261. static bool onebit(uint32_t ui32Value)
  262. {
  263. return ui32Value && !(ui32Value & (ui32Value - 1));
  264. }
  265. //*****************************************************************************
  266. // compute_freq()
  267. //*****************************************************************************
  268. //
  269. // Compute the interface frequency based on the given parameters
  270. //
  271. static uint32_t compute_freq(uint32_t ui32HFRCfreqHz,
  272. uint32_t ui32Fsel, uint32_t ui32Div3,
  273. uint32_t ui32DivEn, uint32_t ui32TotPer)
  274. {
  275. uint32_t ui32Denomfinal, ui32ClkFreq;
  276. ui32Denomfinal = ((1 << (ui32Fsel - 1)) * (1 + ui32Div3 * 2) * (1 + ui32DivEn * (ui32TotPer)));
  277. ui32ClkFreq = (ui32HFRCfreqHz) / ui32Denomfinal; // Compute the set frequency value
  278. ui32ClkFreq += (((ui32HFRCfreqHz) % ui32Denomfinal) > (ui32Denomfinal / 2)) ? 1 : 0;
  279. return ui32ClkFreq;
  280. }
  281. //*****************************************************************************
  282. // iom_calc_gpio()
  283. //
  284. // Calculate the IOM4 GPIO to assert.
  285. //
  286. //*****************************************************************************
  287. static uint32_t iom_calc_gpio(uint32_t ui32ChipSelect)
  288. {
  289. uint32_t index;
  290. uint8_t ui8PadRegVal, ui8FncSelVal;
  291. //
  292. // Figure out which GPIO we are using for the IOM
  293. //
  294. for ( index = 0; index < (sizeof(g_IOMPads) / sizeof(IOMPad_t)); index++ )
  295. {
  296. //
  297. // Is this one of the CEn that we are using?
  298. //
  299. if ( g_IOMPads[index].channel == ui32ChipSelect )
  300. {
  301. //
  302. // Get the PAD register value
  303. //
  304. ui8PadRegVal = ((AM_REGVAL(AM_HAL_GPIO_PADREG(g_IOMPads[index].pad))) &
  305. AM_HAL_GPIO_PADREG_M(g_IOMPads[index].pad)) >>
  306. AM_HAL_GPIO_PADREG_S(g_IOMPads[index].pad);
  307. //
  308. // Get the FNCSEL field value
  309. //
  310. ui8FncSelVal = (ui8PadRegVal & 0x38) >> 3;
  311. //
  312. // Is the FNCSEL filed for this pad set to the expected value?
  313. //
  314. if ( ui8FncSelVal == g_IOMPads[index].funcsel )
  315. {
  316. // This is the GPIO we need to use.
  317. return g_IOMPads[index].pad;
  318. }
  319. }
  320. }
  321. return 0xDEADBEEF;
  322. }
  323. //*****************************************************************************
  324. //
  325. // Checks to see if this processor is a Rev B0 device.
  326. //
  327. // This is needed for the B0 IOM workaround.
  328. //
  329. //*****************************************************************************
  330. bool
  331. isRevB0(void)
  332. {
  333. //
  334. // Check to make sure the major rev is B and the minor rev is zero.
  335. //
  336. if ( (AM_REG(MCUCTRL, CHIPREV) & 0xFF) == AM_REG_MCUCTRL_CHIPREV_REVMAJ_B )
  337. {
  338. return true;
  339. }
  340. else
  341. {
  342. return false;
  343. }
  344. }
  345. //*****************************************************************************
  346. //
  347. // Checks to see if this processor is a Rev B2 device.
  348. //
  349. // This is needed for the B2 I2C workaround.
  350. //
  351. //*****************************************************************************
  352. static bool
  353. isRevB2(void)
  354. {
  355. //
  356. // Check to make sure the major rev is B and the minor rev is zero.
  357. //
  358. if ( (AM_REG(MCUCTRL, CHIPREV) & 0xFF) ==
  359. (AM_REG_MCUCTRL_CHIPREV_REVMAJ_B | AM_REG_MCUCTRL_CHIPREV_REVMIN_REV2) )
  360. {
  361. return true;
  362. }
  363. else
  364. {
  365. return false;
  366. }
  367. }
  368. //*****************************************************************************
  369. //
  370. // Checks and waits for the SCL line to be high
  371. // This is to ensure clock hi time specs are not violated in case slave did
  372. // clock stretching in previous transaction
  373. //
  374. //*****************************************************************************
  375. static am_hal_iom_status_e
  376. internal_iom_wait_i2c_scl_hi(uint32_t ui32Module)
  377. {
  378. uint32_t ui32IOMGPIO = 0xDEADBEEF;
  379. volatile uint32_t *pui32SCLPadreg;
  380. uint32_t ui32SCLPadregVal;
  381. uint32_t index;
  382. uint8_t ui8PadRegVal;
  383. uint8_t ui8FncSelVal;
  384. uint32_t waitStatus;
  385. // Need to change the SCL pin as a GPIO and poll till it is set to hi
  386. // For all the IOM's except for IOM2, there is a single designated pin for SCL
  387. // IOM2 has two choices and we need to determine which one
  388. //
  389. // Figure out which GPIO we are using for the SCL
  390. //
  391. for ( index = 0; index < (sizeof(g_I2CPads) / sizeof(I2CPad_t)); index++ )
  392. {
  393. //
  394. // Is this for the IOM that we are using?
  395. //
  396. if ( g_I2CPads[index].module == ui32Module )
  397. {
  398. //
  399. // Get the PAD register value
  400. //
  401. ui8PadRegVal = ((AM_REGVAL(AM_HAL_GPIO_PADREG(g_I2CPads[index].pad))) &
  402. AM_HAL_GPIO_PADREG_M(g_I2CPads[index].pad)) >>
  403. AM_HAL_GPIO_PADREG_S(g_I2CPads[index].pad);
  404. //
  405. // Get the FNCSEL field value
  406. //
  407. ui8FncSelVal = (ui8PadRegVal & 0x38) >> 3;
  408. //
  409. // Is the FNCSEL filed for this pad set to the expected value?
  410. //
  411. if ( ui8FncSelVal == g_I2CPads[index].funcsel )
  412. {
  413. // This is the GPIO we need to use.
  414. ui32IOMGPIO = g_I2CPads[index].pad;
  415. break;
  416. }
  417. }
  418. }
  419. if (0xDEADBEEF == ui32IOMGPIO)
  420. {
  421. // SCL has not been configured
  422. return AM_HAL_IOM_ERR_INVALID_CFG;
  423. }
  424. //
  425. // Save the locations and values of the SCL pin configuration
  426. // information.
  427. //
  428. pui32SCLPadreg = (volatile uint32_t *)AM_HAL_GPIO_PADREG(ui32IOMGPIO);
  429. ui32SCLPadregVal = *pui32SCLPadreg;
  430. //
  431. // Temporarily configure the override pin as an input.
  432. //
  433. am_hal_gpio_pin_config(ui32IOMGPIO, AM_HAL_PIN_INPUT);
  434. //
  435. // Make sure SCL is high within standard timeout
  436. //
  437. waitStatus = am_hal_flash_delay_status_change(ui32StatusTimeout[ui32Module],
  438. AM_HAL_GPIO_RD_REG(ui32IOMGPIO), AM_HAL_GPIO_RD_M(ui32IOMGPIO),
  439. AM_HAL_GPIO_RD_M(ui32IOMGPIO));
  440. //
  441. // Write the GPIO PADKEY register
  442. //
  443. AM_REGn(GPIO, 0, PADKEY) = AM_REG_GPIO_PADKEY_KEYVAL;
  444. // Revert back the original settings
  445. *pui32SCLPadreg = ui32SCLPadregVal;
  446. //
  447. // Re-lock the GPIO PADKEY register
  448. //
  449. AM_REGn(GPIO, 0, PADKEY) = 0;
  450. if (waitStatus != 1)
  451. {
  452. return AM_HAL_IOM_ERR_TIMEOUT;
  453. }
  454. return AM_HAL_IOM_SUCCESS;
  455. }
  456. //*****************************************************************************
  457. //
  458. //! @brief Returns the proper settings for the CLKCFG register.
  459. //!
  460. //! @param ui32FreqHz - The desired interface frequency in Hz.
  461. //! ui32Phase - SPI phase (0 or 1). Can affect duty cycle.
  462. //!
  463. //! Given a desired serial interface clock frequency, this function computes
  464. //! the appropriate settings for the various fields in the CLKCFG register
  465. //! and returns the 32-bit value that should be written to that register.
  466. //! The actual interface frequency may be slightly lower than the specified
  467. //! frequency, but the actual frequency is also returned.
  468. //!
  469. //! @note A couple of criteria that this algorithm follow are:
  470. //! 1. For power savings, choose the highest FSEL possible.
  471. //! 2. For best duty cycle, use DIV3 when possible rather than DIVEN.
  472. //!
  473. //! An example of #1 is that both of the following CLKCFGs would result
  474. //! in a frequency of 428,571 Hz: 0x0E071400 and 0x1C0E1300.
  475. //! The former is chosen by the algorithm because it results in FSEL=4
  476. //! while the latter is FSEL=3.
  477. //!
  478. //! An example of #2 is that both of the following CLKCFGs would result
  479. //! in a frequency of 2,000,000 Hz: 0x02011400 and 0x00000C00.
  480. //! The latter is chosen by the algorithm because it results in use of DIV3
  481. //! rather than DIVEN.
  482. //!
  483. //! @return An unsigned 64-bit value.
  484. //! The lower 32-bits represent the value to use to set CLKCFG.
  485. //! The upper 32-bits represent the actual frequency (in Hz) that will result
  486. //! from setting CLKCFG with the lower 32-bits.
  487. //!
  488. //! 0 (64 bits) = error. Note that the caller must check the entire 64 bits.
  489. //! It is not an error if only the low 32-bits are 0 (this is a valid value).
  490. //! But the entire 64 bits returning 0 is an error.
  491. //!
  492. //*****************************************************************************
  493. static
  494. uint64_t iom_get_interface_clock_cfg(uint32_t ui32FreqHz, uint32_t ui32Phase )
  495. {
  496. uint32_t ui32Fsel, ui32Div3, ui32DivEn, ui32TotPer, ui32LowPer;
  497. uint32_t ui32Denom, ui32v1, ui32Denomfinal, ui32ClkFreq, ui32ClkCfg;
  498. uint32_t ui32HFRCfreqHz;
  499. int32_t i32Div, i32N;
  500. if ( ui32FreqHz == 0 )
  501. {
  502. return 0;
  503. }
  504. //
  505. // Set the HFRC clock frequency.
  506. //
  507. ui32HFRCfreqHz = AM_HAL_CLKGEN_FREQ_MAX_HZ;
  508. //
  509. // Compute various parameters used for computing the optimal CLKCFG setting.
  510. //
  511. i32Div = (ui32HFRCfreqHz / ui32FreqHz) + ((ui32HFRCfreqHz % ui32FreqHz) ? 1 : 0); // Round up (ceiling)
  512. //
  513. // Compute N (count the number of LS zeros of Div) = ctz(Div) = log2(Div & (-Div))
  514. //
  515. i32N = 31 - AM_INSTR_CLZ((i32Div & (-i32Div)));
  516. if ( i32N > 6 )
  517. {
  518. i32N = 6;
  519. }
  520. ui32Div3 = ( (ui32FreqHz < (ui32HFRCfreqHz / 16384)) ||
  521. ( ((ui32FreqHz >= (ui32HFRCfreqHz / 3)) &&
  522. (ui32FreqHz <= ((ui32HFRCfreqHz / 2) - 1)) ) ) ) ? 1 : 0;
  523. ui32Denom = ( 1 << i32N ) * ( 1 + (ui32Div3 * 2) );
  524. ui32TotPer = i32Div / ui32Denom;
  525. ui32TotPer += (i32Div % ui32Denom) ? 1 : 0;
  526. ui32v1 = 31 - AM_INSTR_CLZ(ui32TotPer); // v1 = log2(TotPer)
  527. ui32Fsel = (ui32v1 > 7) ? ui32v1 + i32N - 7 : i32N;
  528. ui32Fsel++;
  529. if ( ui32Fsel > 7 )
  530. {
  531. //
  532. // This is an error, can't go that low.
  533. //
  534. return 0;
  535. }
  536. if ( ui32v1 > 7 )
  537. {
  538. ui32DivEn = ui32TotPer; // Save TotPer for the round up calculation
  539. ui32TotPer = ui32TotPer>>(ui32v1-7);
  540. ui32TotPer += ((ui32DivEn) % (1 << (ui32v1 - 7))) ? 1 : 0;
  541. }
  542. ui32DivEn = ( (ui32FreqHz >= (ui32HFRCfreqHz / 4)) ||
  543. ((1 << (ui32Fsel - 1)) == i32Div) ) ? 0 : 1;
  544. if (ui32Phase == 1)
  545. {
  546. ui32LowPer = (ui32TotPer - 2) / 2; // Longer high phase
  547. }
  548. else
  549. {
  550. ui32LowPer = (ui32TotPer - 1) / 2; // Longer low phase
  551. }
  552. ui32ClkCfg = AM_REG_IOMSTR_CLKCFG_FSEL(ui32Fsel) |
  553. AM_REG_IOMSTR_CLKCFG_DIV3(ui32Div3) |
  554. AM_REG_IOMSTR_CLKCFG_DIVEN(ui32DivEn) |
  555. AM_REG_IOMSTR_CLKCFG_LOWPER(ui32LowPer) |
  556. AM_REG_IOMSTR_CLKCFG_TOTPER(ui32TotPer - 1);
  557. //
  558. // Now, compute the actual frequency, which will be returned.
  559. //
  560. ui32ClkFreq = compute_freq(ui32HFRCfreqHz, ui32Fsel, ui32Div3, ui32DivEn, ui32TotPer - 1);
  561. //
  562. // Determine if the actual frequency is a power of 2 (MHz).
  563. //
  564. if ( (ui32ClkFreq % 250000) == 0 )
  565. {
  566. //
  567. // If the actual clock frequency is a power of 2 ranging from 250KHz up,
  568. // we can simplify the CLKCFG value using DIV3 (which also results in a
  569. // better duty cycle).
  570. //
  571. ui32Denomfinal = ui32ClkFreq / (uint32_t)250000;
  572. if ( onebit(ui32Denomfinal) )
  573. {
  574. //
  575. // These configurations can be simplified by using DIV3. Configs
  576. // using DIV3 have a 50% duty cycle, while those from DIVEN will
  577. // have a 66/33 duty cycle.
  578. //
  579. ui32TotPer = ui32LowPer = ui32DivEn = 0;
  580. ui32Div3 = 1;
  581. //
  582. // Now, compute the return values.
  583. //
  584. ui32ClkFreq = compute_freq(ui32HFRCfreqHz, ui32Fsel, ui32Div3, ui32DivEn, ui32TotPer);
  585. ui32ClkCfg = AM_REG_IOMSTR_CLKCFG_FSEL(ui32Fsel) |
  586. AM_REG_IOMSTR_CLKCFG_DIV3(1) |
  587. AM_REG_IOMSTR_CLKCFG_DIVEN(0) |
  588. AM_REG_IOMSTR_CLKCFG_LOWPER(0) |
  589. AM_REG_IOMSTR_CLKCFG_TOTPER(0);
  590. }
  591. }
  592. return ( ((uint64_t)ui32ClkFreq) << 32) | (uint64_t)ui32ClkCfg;
  593. } //iom_get_interface_clock_cfg()
  594. //*****************************************************************************
  595. //
  596. //! @brief Clock setting for the I2C Clock Stretch Workaround
  597. //!
  598. //! This restricts the frequencies that can be used for I2C devices on
  599. //!
  600. //! @param ui32FreqHz - The desired interface frequency in Hz.
  601. //!
  602. //! @return None.
  603. //
  604. //*****************************************************************************
  605. static
  606. uint64_t iom_get_i2c_workaround_clock_cfg(uint32_t ui32FreqHz)
  607. {
  608. uint32_t ui32Fsel;
  609. // Only allow certain SCL frequencies for clock stretching devices.
  610. if (ui32FreqHz == AM_HAL_IOM_800KHZ)
  611. {
  612. ui32Fsel = 2;
  613. }
  614. else if (ui32FreqHz == AM_HAL_IOM_400KHZ)
  615. {
  616. ui32Fsel = 3;
  617. }
  618. else if (ui32FreqHz == AM_HAL_IOM_200KHZ)
  619. {
  620. ui32Fsel = 4;
  621. }
  622. else if (ui32FreqHz == AM_HAL_IOM_100KHZ)
  623. {
  624. ui32Fsel = 5;
  625. }
  626. else // Default is 100KHz.
  627. {
  628. ui32Fsel = 5;
  629. }
  630. // Return the resulting CLKCFG register settings.
  631. return (AM_REG_IOMSTR_CLKCFG_FSEL(ui32Fsel) |
  632. AM_REG_IOMSTR_CLKCFG_DIV3(0) |
  633. AM_REG_IOMSTR_CLKCFG_DIVEN(1) |
  634. AM_REG_IOMSTR_CLKCFG_LOWPER(14) |
  635. AM_REG_IOMSTR_CLKCFG_TOTPER(29));
  636. } // iom_get_i2c_workaround_clock_cfg
  637. //*****************************************************************************
  638. //
  639. //! @brief Enable the IOM in the power control block.
  640. //!
  641. //! This function enables the desigated IOM module in the power control block.
  642. //!
  643. //! @return None.
  644. //
  645. //*****************************************************************************
  646. void
  647. am_hal_iom_pwrctrl_enable(uint32_t ui32Module)
  648. {
  649. am_hal_debug_assert_msg(ui32Module < AM_REG_IOMSTR_NUM_MODULES,
  650. "Trying to enable an IOM module that doesn't exist.");
  651. am_hal_pwrctrl_periph_enable(AM_HAL_PWRCTRL_IOM0 << ui32Module);
  652. }
  653. //*****************************************************************************
  654. //
  655. //! @brief Disable the IOM in the power control block.
  656. //!
  657. //! This function disables the desigated IOM module in the power control block.
  658. //!
  659. //! @return None.
  660. //
  661. //*****************************************************************************
  662. void
  663. am_hal_iom_pwrctrl_disable(uint32_t ui32Module)
  664. {
  665. am_hal_debug_assert_msg(ui32Module < AM_REG_IOMSTR_NUM_MODULES,
  666. "Trying to disable an IOM module that doesn't exist.");
  667. am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_IOM0 << ui32Module);
  668. }
  669. //*****************************************************************************
  670. //
  671. //! @brief Enables the IOM module
  672. //!
  673. //! @param ui32Module - The number of the IOM module to be enabled.
  674. //!
  675. //! This function enables the IOM module using the IFCEN bitfield in the
  676. //! IOMSTR_CFG register.
  677. //!
  678. //! @return None.
  679. //
  680. //*****************************************************************************
  681. void
  682. am_hal_iom_enable(uint32_t ui32Module)
  683. {
  684. if ( ui32Module < AM_REG_IOMSTR_NUM_MODULES )
  685. {
  686. AM_REGn(IOMSTR, ui32Module, CFG) |= AM_REG_IOMSTR_CFG_IFCEN(1);
  687. g_bIomBusy[ui32Module] = false;
  688. }
  689. }
  690. //*****************************************************************************
  691. //
  692. //! @brief Disables the IOM module.
  693. //!
  694. //! @param ui32Module - The number of the IOM module to be disabled.
  695. //!
  696. //! This function disables the IOM module using the IFCEN bitfield in the
  697. //! IOMSTR_CFG register.
  698. //!
  699. //! @return None.
  700. //
  701. //*****************************************************************************
  702. void
  703. am_hal_iom_disable(uint32_t ui32Module)
  704. {
  705. if ( ui32Module < AM_REG_IOMSTR_NUM_MODULES )
  706. {
  707. //
  708. // Wait until the bus is idle.
  709. //
  710. am_hal_iom_poll_complete(ui32Module);
  711. //
  712. // Disable the interface.
  713. //
  714. AM_REGn(IOMSTR, ui32Module, CFG) &= ~(AM_REG_IOMSTR_CFG_IFCEN(1));
  715. }
  716. }
  717. //*****************************************************************************
  718. //
  719. //! @brief Enable power to the selected IOM module.
  720. //!
  721. //! @param ui32Module - Module number for the IOM to be turned on.
  722. //!
  723. //! This function enables the power gate to the selected IOM module. It is
  724. //! intended to be used along with am_hal_iom_power_off_save(). Used together,
  725. //! these functions allow the caller to power IOM modules off to save
  726. //! additional power without losing important configuration information.
  727. //!
  728. //! The am_hal_iom_power_off_save() function will save IOM configuration
  729. //! register information to SRAM before powering off the selected IOM module.
  730. //! This function will re-enable the IOM module, and restore those
  731. //! configuration settings from SRAM.
  732. //!
  733. //! @return None.
  734. //
  735. //*****************************************************************************
  736. void
  737. am_hal_iom_power_on_restore(uint32_t ui32Module)
  738. {
  739. am_hal_debug_assert_msg(ui32Module < AM_REG_IOMSTR_NUM_MODULES,
  740. "Trying to enable an IOM module that doesn't exist.");
  741. //
  742. // Make sure this restore is a companion to a previous save call.
  743. //
  744. if ( am_hal_iom_pwrsave[ui32Module].bValid == 0 )
  745. {
  746. return;
  747. }
  748. //
  749. // Enable power to the selected IOM.
  750. //
  751. am_hal_pwrctrl_periph_enable(AM_HAL_PWRCTRL_IOM0 << ui32Module);
  752. //
  753. // Restore the IOM configuration registers from the structure in SRAM.
  754. //
  755. AM_REGn(IOMSTR, ui32Module, FIFOTHR) = am_hal_iom_pwrsave[ui32Module].FIFOTHR;
  756. AM_REGn(IOMSTR, ui32Module, CLKCFG) = am_hal_iom_pwrsave[ui32Module].CLKCFG;
  757. AM_REGn(IOMSTR, ui32Module, CFG) = am_hal_iom_pwrsave[ui32Module].CFG;
  758. AM_REGn(IOMSTR, ui32Module, INTEN) = am_hal_iom_pwrsave[ui32Module].INTEN;
  759. //
  760. // Indicates we have restored the configuration.
  761. //
  762. am_hal_iom_pwrsave[ui32Module].bValid = 0;
  763. }
  764. //*****************************************************************************
  765. //
  766. //! @brief Disable power to the selected IOM module.
  767. //!
  768. //! @param ui32Module - Module number for the IOM to be turned off.
  769. //!
  770. //! This function disables the power gate to the selected IOM module. It is
  771. //! intended to be used along with am_hal_iom_power_on_restore(). Used together,
  772. //! these functions allow the caller to power IOM modules off to save
  773. //! additional power without losing important configuration information.
  774. //!
  775. //! The am_hal_iom_power_off_save() function will save IOM configuration
  776. //! register information to SRAM before powering off the selected IOM module.
  777. //! The am_hal_iom_power_on_restore() function will re-enable the IOM module
  778. //! and restore those configuration settings from SRAM.
  779. //!
  780. //! @return None.
  781. //
  782. //*****************************************************************************
  783. void
  784. am_hal_iom_power_off_save(uint32_t ui32Module)
  785. {
  786. am_hal_debug_assert_msg(ui32Module < AM_REG_IOMSTR_NUM_MODULES,
  787. "Trying to disable an IOM module that doesn't exist.");
  788. //
  789. // Save the IOM configuration registers to the structure in SRAM.
  790. //
  791. am_hal_iom_pwrsave[ui32Module].FIFOTHR = AM_REGn(IOMSTR, ui32Module, FIFOTHR);
  792. am_hal_iom_pwrsave[ui32Module].CLKCFG = AM_REGn(IOMSTR, ui32Module, CLKCFG);
  793. am_hal_iom_pwrsave[ui32Module].CFG = AM_REGn(IOMSTR, ui32Module, CFG);
  794. am_hal_iom_pwrsave[ui32Module].INTEN = AM_REGn(IOMSTR, ui32Module, INTEN);
  795. //
  796. // Indicates we have a valid saved configuration.
  797. //
  798. am_hal_iom_pwrsave[ui32Module].bValid = 1;
  799. //
  800. // Disable power to the selected IOM.
  801. //
  802. am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_IOM0 << ui32Module);
  803. }
  804. //
  805. //! Check and correct the IOM FIFO threshold.
  806. //
  807. #define MAX_RW_THRESHOLD (AM_HAL_IOM_MAX_FIFO_SIZE - 4)
  808. #define MIN_RW_THRESHOLD (4)
  809. #if (AM_ASSERT_INVALID_THRESHOLD == 0)
  810. static uint8_t check_iom_threshold(const uint8_t iom_threshold)
  811. {
  812. uint8_t corrected_threshold = iom_threshold;
  813. if ( corrected_threshold < MIN_RW_THRESHOLD )
  814. {
  815. corrected_threshold = MIN_RW_THRESHOLD;
  816. }
  817. if ( corrected_threshold > MAX_RW_THRESHOLD )
  818. {
  819. corrected_threshold = MAX_RW_THRESHOLD;
  820. }
  821. return corrected_threshold;
  822. }
  823. #endif
  824. //*****************************************************************************
  825. //
  826. //! @brief Sets module-wide configuration options for the IOM module.
  827. //!
  828. //! @param ui32Module - The instance number for the module to be configured
  829. //! (zero or one)
  830. //!
  831. //! @param psConfig - Pointer to an IOM configuration structure.
  832. //!
  833. //! This function is used to set the interface mode (SPI or I2C), clock
  834. //! frequency, SPI format (when relevant), and FIFO read/write interrupt
  835. //! thresholds for the IO master. For more information on specific
  836. //! configuration options, please see the documentation for the configuration
  837. //! structure.
  838. //!
  839. //! @note The IOM module should be disabled before configuring or
  840. //! re-configuring. This function will not re-enable the module when it
  841. //! completes. Call the am_hal_iom_enable function when the module is
  842. //! configured and ready to use.
  843. //!
  844. //! @return None.
  845. //
  846. //*****************************************************************************
  847. void
  848. am_hal_iom_config(uint32_t ui32Module, const am_hal_iom_config_t *psConfig)
  849. {
  850. uint32_t ui32Config, ui32ClkCfg;
  851. //
  852. // Start by checking the interface mode (I2C or SPI), and writing it to the
  853. // configuration word.
  854. //
  855. ui32Config = psConfig->ui32InterfaceMode;
  856. if ( ui32Module >= AM_REG_IOMSTR_NUM_MODULES )
  857. {
  858. return;
  859. }
  860. //
  861. // Check the SPI format, and OR in the bits for SPHA (clock phase) and SPOL
  862. // (polarity). These shouldn't have any effect in I2C mode, so it should be
  863. // ok to write them without checking exactly which mode we're in.
  864. //
  865. if ( psConfig->bSPHA )
  866. {
  867. ui32Config |= AM_REG_IOMSTR_CFG_SPHA(1);
  868. }
  869. if ( psConfig->bSPOL )
  870. {
  871. ui32Config |= AM_REG_IOMSTR_CFG_SPOL(1);
  872. }
  873. // Set the STARTRD based on the interface speed
  874. // For all I2C frequencies and SPI frequencies below 16 MHz, the STARTRD
  875. // field should be set to 0 to minimize the potential of the IO transfer
  876. // holding off a bus access to the FIFO. For SPI frequencies of 16 MHz
  877. // or 24 MHz, the STARTRD field must be set to a value of 2 to insure
  878. // enough time for the IO preread.
  879. if ( psConfig->ui32ClockFrequency >= 16000000UL)
  880. {
  881. ui32Config |= AM_REG_IOMSTR_CFG_STARTRD(2);
  882. }
  883. //
  884. // Write the resulting configuration word to the IO master CFG register for
  885. // the module number we were provided.
  886. //
  887. AM_REGn(IOMSTR, ui32Module, CFG) = ui32Config;
  888. //
  889. // Write the FIFO write and read thresholds to the appropriate registers.
  890. //
  891. #if (AM_ASSERT_INVALID_THRESHOLD == 1)
  892. am_hal_debug_assert_msg(
  893. (psConfig->ui8WriteThreshold <= MAX_RW_THRESHOLD), "IOM write threshold too big.");
  894. am_hal_debug_assert_msg(
  895. (psConfig->ui8ReadThreshold <= MAX_RW_THRESHOLD), "IOM read threshold too big.");
  896. am_hal_debug_assert_msg(
  897. (psConfig->ui8WriteThreshold >= MIN_RW_THRESHOLD), "IOM write threshold too small.");
  898. am_hal_debug_assert_msg(
  899. (psConfig->ui8ReadThreshold >= MIN_RW_THRESHOLD), "IOM read threshold too small.");
  900. AM_REGn(IOMSTR, ui32Module, FIFOTHR) =
  901. (AM_REG_IOMSTR_FIFOTHR_FIFOWTHR(psConfig->ui8WriteThreshold) |
  902. AM_REG_IOMSTR_FIFOTHR_FIFORTHR(psConfig->ui8ReadThreshold));
  903. #elif (AM_ASSERT_INVALID_THRESHOLD == 0)
  904. AM_REGn(IOMSTR, ui32Module, FIFOTHR) =
  905. (AM_REG_IOMSTR_FIFOTHR_FIFOWTHR(check_iom_threshold(psConfig->ui8WriteThreshold)) |
  906. AM_REG_IOMSTR_FIFOTHR_FIFORTHR(check_iom_threshold(psConfig->ui8ReadThreshold)));
  907. #else
  908. #error AM_ASSERT_INVALID_THRESHOLD must be 0 or 1.
  909. #endif
  910. // Apply I2C clock stretching workaround if B2 silicon and IOM 1,2,3, or 5
  911. // Note: Only I2C clock speeds of AM_HAL_IOM_800KHZ, AM_HAL_IOM_400KHZ,
  912. // AM_HAL_IOM_200KHZ, and AM_HAL_IOM_100KHZ are allowed. Other values will
  913. // result in the default of AM_HAL_IOM_100KHZ.
  914. if ((0 != ui32Module) && (4 != ui32Module) && (6 != ui32Module) &&
  915. isRevB2() && (AM_HAL_IOM_I2CMODE == psConfig->ui32InterfaceMode))
  916. {
  917. // Set SPHA field to 1 on B2 silicon to enable the feature;
  918. AM_REGn(IOMSTR, ui32Module, CFG) |= AM_REG_IOMSTR_CFG_SPHA_M;
  919. ui32ClkCfg = iom_get_i2c_workaround_clock_cfg(psConfig->ui32ClockFrequency);
  920. }
  921. else
  922. {
  923. //
  924. // An exception occurs in the LOWPER computation when setting an interface
  925. // frequency (such as a divide by 5 frequency) which results in a 60/40
  926. // duty cycle. The 60% cycle must occur in the appropriate half-period,
  927. // as only one of the half-periods is active, depending on which phase
  928. // is being selected.
  929. // If SPHA=0 the low period must be 60%. If SPHA=1 high period must be 60%.
  930. // Note that the predetermined frequency parameters use the formula
  931. // lowper = (totper-1)/2, which results in a 60% low period.
  932. //
  933. ui32ClkCfg = iom_get_interface_clock_cfg(psConfig->ui32ClockFrequency,
  934. psConfig->bSPHA );
  935. }
  936. if ( ui32ClkCfg )
  937. {
  938. AM_REGn(IOMSTR, ui32Module, CLKCFG) = (uint32_t)ui32ClkCfg;
  939. }
  940. //
  941. // Compute the status timeout value.
  942. //
  943. ui32StatusTimeout[ui32Module] = MAX_IOM_BITS * AM_HAL_IOM_MAX_FIFO_SIZE *
  944. IOM_OVERHEAD_FACTOR * (am_hal_clkgen_sysclk_get() / psConfig->ui32ClockFrequency);
  945. }
  946. //*****************************************************************************
  947. //
  948. //! @brief Returns the actual currently configured interface frequency in Hz.
  949. //
  950. //*****************************************************************************
  951. uint32_t
  952. am_hal_iom_frequency_get(uint32_t ui32ClkCfg)
  953. {
  954. uint32_t ui32Freq;
  955. ui32Freq = compute_freq(AM_HAL_CLKGEN_FREQ_MAX_HZ,
  956. (ui32ClkCfg & AM_REG_IOMSTR_CLKCFG_FSEL_M) >> AM_REG_IOMSTR_CLKCFG_FSEL_S,
  957. (ui32ClkCfg & AM_REG_IOMSTR_CLKCFG_DIV3_M) >> AM_REG_IOMSTR_CLKCFG_DIV3_S,
  958. (ui32ClkCfg & AM_REG_IOMSTR_CLKCFG_DIVEN_M) >> AM_REG_IOMSTR_CLKCFG_DIVEN_S,
  959. (ui32ClkCfg & AM_REG_IOMSTR_CLKCFG_TOTPER_M)>> AM_REG_IOMSTR_CLKCFG_TOTPER_S);
  960. return ui32Freq;
  961. }
  962. //*****************************************************************************
  963. //
  964. // Helper function for the B0 workaround.
  965. //
  966. //*****************************************************************************
  967. static uint32_t
  968. iom_get_workaround_fsel(uint32_t maxFreq)
  969. {
  970. uint32_t ui32Freq, ui32Fsel;
  971. uint32_t ui32ClkCfg = AM_REGn(IOMSTR, 4, CLKCFG);
  972. //
  973. // Starting with the current clock configuration parameters, find a value
  974. // of FSEL that will bring our total frequency down to or below maxFreq.
  975. //
  976. for ( ui32Fsel = 1; ui32Fsel < 8; ui32Fsel++ )
  977. {
  978. ui32Freq = compute_freq(AM_HAL_CLKGEN_FREQ_MAX_HZ, ui32Fsel,
  979. AM_BFX(IOMSTR, CLKCFG, DIV3, ui32ClkCfg),
  980. AM_BFX(IOMSTR, CLKCFG, DIVEN, ui32ClkCfg),
  981. AM_BFX(IOMSTR, CLKCFG, TOTPER, ui32ClkCfg));
  982. if ( ui32Freq <= maxFreq && ui32Freq != 0 )
  983. {
  984. //
  985. // Return the new FSEL
  986. //
  987. return ui32Fsel;
  988. }
  989. }
  990. //
  991. // Couldn't find an appropriate frequency. This should be impossible
  992. // because there should always be a value of FSEL that brings the final IOM
  993. // frequency below 500 KHz.
  994. //
  995. am_hal_debug_assert_msg(false, "Could find a valid frequency. Should never get here.");
  996. return maxFreq;
  997. }
  998. // Separating this piece of code in separate function to keep the impact of
  999. // rest of the code to mimimal because of stack usage
  1000. static void
  1001. internal_iom_workaround_critical(uint32_t ui32Command,
  1002. volatile uint32_t *pui32CSPadreg,
  1003. uint32_t ui32CSPadregVal,
  1004. uint32_t ui32DelayTime,
  1005. uint32_t ui32ClkCfg,
  1006. uint32_t ui32LowClkCfg,
  1007. bool bRising)
  1008. {
  1009. uint32_t ui32Critical = 0;
  1010. //
  1011. // Start a critical section.
  1012. //
  1013. ui32Critical = am_hal_interrupt_master_disable();
  1014. //
  1015. // Start the write on the bus.
  1016. //
  1017. AM_REGn(IOMSTR, WORKAROUND_IOM, CMD) = ui32Command;
  1018. //
  1019. // Slow down the clock, and run the workaround loop. The workaround
  1020. // loop runs an edge-detector on MOSI, and triggers a falling edge on
  1021. // chip-enable on the first bit of our real data.
  1022. //
  1023. ((void (*)(uint32_t)) 0x0800009d)(ui32DelayTime);
  1024. // Switch to Low Freq
  1025. AM_REGn(IOMSTR, WORKAROUND_IOM, CLKCFG) = ui32LowClkCfg;
  1026. iom_workaround_loop(ui32CSPadregVal, pui32CSPadreg, bRising);
  1027. //
  1028. // Restore the clock frequency and the normal MOSI pin function.
  1029. //
  1030. AM_REGn(IOMSTR, WORKAROUND_IOM, CLKCFG) = ui32ClkCfg;
  1031. am_hal_gpio_pin_config(WORKAROUND_IOM_MOSI_PIN, WORKAROUND_IOM_MOSI_CFG);
  1032. //
  1033. // End the critical section.
  1034. //
  1035. am_hal_interrupt_master_set(ui32Critical);
  1036. }
  1037. //*****************************************************************************
  1038. //
  1039. //! @brief Workaround for an Apollo2 Rev B0 issue.
  1040. //!
  1041. //! @param ui32ChipSelect - Chip-select number for this transaction.
  1042. //! @param pui32Data - Pointer to the bytes that will be sent.
  1043. //! @param ui32NumBytes - Number of bytes to send.
  1044. //! @param ui32Options - Additional SPI transfer options.
  1045. //!
  1046. //! Some Apollo2 Rev B0 devices have an issue where the first byte of a SPI
  1047. //! write transaction can have some of its bits changed from ones to zeroes. In
  1048. //! order to get around this issue, we artificially pad the SPI write data with
  1049. //! additional bytes, and manually control the CS pin for the beginning of the
  1050. //! SPI frame so that the receiving device will ignore the bytes of padding
  1051. //! that we added.
  1052. //!
  1053. //! This function acts as a helper function to higher-level spi APIs. It
  1054. //! performs the functions of am_hal_iom_fifo_write() and
  1055. //! am_hal_iom_spi_cmd_run() to get a SPI write started on the bus, including
  1056. //! all of the necessary workaround behavior.
  1057. //!
  1058. //! @return None.
  1059. //
  1060. //*****************************************************************************
  1061. void
  1062. am_hal_iom_workaround_word_write(uint32_t ui32ChipSelect,
  1063. uint32_t *pui32Data, uint32_t ui32NumBytes,
  1064. uint32_t ui32Options)
  1065. {
  1066. uint32_t ui32TransferSize;
  1067. uint32_t ui32IOMGPIO = 0xDEADBEEF;
  1068. volatile uint32_t *pui32CSPadreg = 0;
  1069. uint32_t ui32CSPadregVal = 0;
  1070. uint32_t ui32ClkCfg = 0;
  1071. uint32_t ui32HiClkCfg, ui32LowClkCfg;
  1072. bool bRising = 0;
  1073. uint32_t ui32HiFreq = 0, ui32NormalFreq = 0;
  1074. uint32_t ui32DelayTime = 0;
  1075. uint32_t ui32LowFsel = 0;
  1076. uint32_t ui32HiFsel = 0;
  1077. uint32_t ui32FirstWord = 0;
  1078. uint32_t ui32MaxFifoSize = ((0 == AM_BFRn(IOMSTR, WORKAROUND_IOM, CFG, FULLDUP)) ?
  1079. AM_HAL_IOM_MAX_FIFO_SIZE : AM_HAL_IOM_MAX_FIFO_SIZE / 2);
  1080. uint32_t ui32Command;
  1081. //
  1082. // Make sure the transfer isn't too long for the hardware to support.
  1083. //
  1084. // Note: This is a little shorter than usual, since the workaround
  1085. // consumes an extra byte at the beginning of the transfer.
  1086. //
  1087. am_hal_debug_assert_msg(ui32NumBytes <= 4091, "SPI transfer too big.");
  1088. //
  1089. // Create a "dummy" word to add on to the beginning of the transfer
  1090. // that will guarantee a transition between the first word and the
  1091. // second on the bus.
  1092. //
  1093. // For raw transactions, this is straightforward. For transactions
  1094. // preceded by an offset, we'll add the offset in to the "dummy" word
  1095. // to preserve data alignment later.
  1096. //
  1097. // The workaround uses a critical section for precision
  1098. // To minimize the time in critical section, we raise the SPI frequency
  1099. // to the max possible for the initial preamble to be clocked out
  1100. // then we switch to a 'reasonably' slow frequency to be able to reliably
  1101. // catch the rising or falling edge by polling. Then we switch back to
  1102. // configured frequency
  1103. //
  1104. // We want to slow down the clock to help us count edges more
  1105. // accurately. Save it first, then slow it down. Also, we will
  1106. // pre-calculate a delay for when we need to restore the SPI settings.
  1107. //
  1108. ui32ClkCfg = AM_REGn(IOMSTR, WORKAROUND_IOM, CLKCFG);
  1109. // Get the largest speed we can configure within our rated speed of 16MHz
  1110. ui32HiFsel = iom_get_workaround_fsel(16000000);
  1111. ui32HiClkCfg = ((ui32ClkCfg & (~AM_REG_IOMSTR_CLKCFG_FSEL_M)) |
  1112. AM_BFV(IOMSTR, CLKCFG, FSEL, ui32HiFsel));
  1113. // Switch to Hi Freq
  1114. // Need to make sure we wait long enough for the hi clock to be effective
  1115. // Delay 2 cycles based on previous frequency
  1116. ui32NormalFreq = am_hal_iom_frequency_get(ui32ClkCfg);
  1117. AM_REGn(IOMSTR, WORKAROUND_IOM, CLKCFG) = ui32HiClkCfg;
  1118. ui32DelayTime = ((2 * AM_HAL_CLKGEN_FREQ_MAX_HZ) / (ui32NormalFreq * 3));
  1119. ((void (*)(uint32_t)) 0x0800009d)(ui32DelayTime);
  1120. //
  1121. // Remember what frequency we'll be running at.during Hi Phase
  1122. //
  1123. ui32HiFreq = am_hal_iom_frequency_get(ui32HiClkCfg);
  1124. //
  1125. // Validate return value to prevent DIVBY0 errors.
  1126. //
  1127. am_hal_debug_assert_msg(ui32HiFreq > 0, "Invalid Hi Frequency for IOM.");
  1128. // Get a reasonably slow speed (~1MHz) we can safely poll for the transition
  1129. ui32LowFsel = iom_get_workaround_fsel(1000000);
  1130. ui32LowClkCfg = ((ui32ClkCfg & (~AM_REG_IOMSTR_CLKCFG_FSEL_M)) |
  1131. AM_BFV(IOMSTR, CLKCFG, FSEL, ui32LowFsel));
  1132. if ( ui32Options & AM_HAL_IOM_RAW )
  1133. {
  1134. //
  1135. // The transition we care for is on 33rd bit.
  1136. // Prepare to delay 27 bits past the start of the transaction
  1137. // before getting into polling - to leave some
  1138. // margin for compiler related variations
  1139. //
  1140. ui32DelayTime = ((27 * AM_HAL_CLKGEN_FREQ_MAX_HZ) / (ui32HiFreq * 3));
  1141. if ( pui32Data[0] & 0x80 )
  1142. {
  1143. ui32FirstWord = 0x00000000;
  1144. bRising = true;
  1145. }
  1146. else
  1147. {
  1148. ui32FirstWord = 0xFFFFFF00;
  1149. bRising = false;
  1150. }
  1151. }
  1152. else
  1153. {
  1154. //
  1155. // The transition we care for is on 25th bit.
  1156. // Prepare to delay 19 bits past the start of the transaction
  1157. // before getting into polling - to leave some
  1158. // margin for compiler related variations
  1159. //
  1160. ui32DelayTime = ((19 * AM_HAL_CLKGEN_FREQ_MAX_HZ) / (ui32HiFreq * 3));
  1161. ui32FirstWord = ((ui32Options & 0xFF00) << 16);
  1162. if ( ui32FirstWord & 0x80000000 )
  1163. {
  1164. bRising = true;
  1165. }
  1166. else
  1167. {
  1168. ui32FirstWord |= 0x00FFFF00;
  1169. bRising = false;
  1170. }
  1171. }
  1172. //
  1173. // Now that weve taken care of the offset byte, we can run the
  1174. // transaction in RAW mode.
  1175. //
  1176. ui32Options |= AM_HAL_IOM_RAW;
  1177. ui32NumBytes += 4;
  1178. //
  1179. // Figure out how many bytes we can write to the FIFO immediately.
  1180. //
  1181. ui32TransferSize = (ui32NumBytes <= ui32MaxFifoSize ? ui32NumBytes :
  1182. ui32MaxFifoSize);
  1183. am_hal_iom_fifo_write(WORKAROUND_IOM, &ui32FirstWord, 4);
  1184. am_hal_iom_fifo_write(WORKAROUND_IOM, pui32Data, ui32TransferSize - 4);
  1185. //
  1186. // Calculate the GPIO to be controlled until the initial shift is
  1187. // complete. Make sure we get a valid value.
  1188. //
  1189. ui32IOMGPIO = iom_calc_gpio(ui32ChipSelect);
  1190. am_hal_debug_assert(0xDEADBEEF != ui32IOMGPIO);
  1191. //
  1192. // Save the locations and values of the CS pin configuration
  1193. // information.
  1194. //
  1195. pui32CSPadreg = (volatile uint32_t *)AM_HAL_GPIO_PADREG(ui32IOMGPIO);
  1196. ui32CSPadregVal = *pui32CSPadreg;
  1197. //
  1198. // Switch CS to a GPIO.
  1199. //
  1200. am_hal_gpio_out_bit_set(ui32IOMGPIO);
  1201. am_hal_gpio_pin_config(ui32IOMGPIO, AM_HAL_GPIO_OUTPUT);
  1202. //
  1203. // Enable the input buffer on MOSI.
  1204. //
  1205. am_hal_gpio_pin_config(WORKAROUND_IOM_MOSI_PIN, WORKAROUND_IOM_MOSI_CFG | AM_HAL_PIN_DIR_INPUT);
  1206. //
  1207. // Write the GPIO PADKEY register to allow the workaround loop to
  1208. // reconfigure chip enable.
  1209. //
  1210. AM_REGn(GPIO, 0, PADKEY) = AM_REG_GPIO_PADKEY_KEYVAL;
  1211. // Preconstruct the command - to save on calculations inside critical section
  1212. ui32Command = internal_am_hal_iom_spi_cmd_construct(AM_HAL_IOM_WRITE,
  1213. ui32ChipSelect, ui32NumBytes, ui32Options);
  1214. internal_iom_workaround_critical(ui32Command,
  1215. pui32CSPadreg, ui32CSPadregVal,
  1216. ui32DelayTime, ui32ClkCfg,
  1217. ui32LowClkCfg, bRising);
  1218. //
  1219. // Re-lock the GPIO PADKEY register
  1220. //
  1221. AM_REGn(GPIO, 0, PADKEY) = 0;
  1222. //
  1223. // Update the pointer and data counter.
  1224. //
  1225. ui32NumBytes -= ui32TransferSize;
  1226. pui32Data += (ui32TransferSize - 4) >> 2;
  1227. }
  1228. //*****************************************************************************
  1229. //
  1230. //! @brief Implement an iterative spin loop.
  1231. //!
  1232. //! @param ui32Iterations - Number of iterations to delay.
  1233. //!
  1234. //! Use this function to implement a CPU busy waiting spin. For Apollo, this
  1235. //! delay can be used for timing purposes since for Apollo, each iteration will
  1236. //! take 3 cycles.
  1237. //!
  1238. //! @return None.
  1239. //
  1240. //*****************************************************************************
  1241. #if defined(__GNUC__)
  1242. static void __attribute__((naked))
  1243. iom_workaround_loop(uint32_t ui32PadRegVal, volatile uint32_t *pui32PadReg,
  1244. bool bRising)
  1245. {
  1246. //
  1247. // Check to see if this is a "rising edge" or "falling edge" detector.
  1248. //
  1249. __asm(" cbz r2, falling_edge");
  1250. //
  1251. // Read GPIO pin 44, and loop until it's HIGH.
  1252. //
  1253. __asm("rising_edge:");
  1254. __asm(" ldr r2, =0x40010084");
  1255. __asm("rising_check_mosi:");
  1256. __asm(" ldr r3, [r2]");
  1257. __asm(" ands r3, r3, #0x1000");
  1258. __asm(" beq rising_check_mosi");
  1259. //
  1260. // Write the PADREG Value to the PADREG register.
  1261. //
  1262. __asm(" str r0, [r1]");
  1263. __asm(" bx lr");
  1264. //
  1265. // Read GPIO pin 44, and loop until it's LOW.
  1266. //
  1267. __asm("falling_edge:");
  1268. __asm(" ldr r2, =0x40010084");
  1269. __asm("falling_check_mosi:");
  1270. __asm(" ldr r3, [r2]");
  1271. __asm(" ands r3, r3, #0x1000");
  1272. __asm(" bne falling_check_mosi");
  1273. //
  1274. // Write the PADREG Value to the PADREG register.
  1275. //
  1276. __asm(" str r0, [r1]");
  1277. __asm(" bx lr");
  1278. }
  1279. #elif defined(__CC_ARM)
  1280. __asm static void
  1281. iom_workaround_loop(uint32_t ui32PadRegVal, volatile uint32_t *pui32PadReg,
  1282. bool bRising)
  1283. {
  1284. //
  1285. // Check to see if this is a "rising edge" or "falling edge" detector.
  1286. //
  1287. cbz r2, falling_edge
  1288. //
  1289. // Read GPIO pin 44, and loop until it's HIGH.
  1290. //
  1291. rising_edge
  1292. ldr r2, =0x40010084
  1293. rising_check_mosi
  1294. ldr r3, [r2]
  1295. ands r3, r3, #0x1000
  1296. beq rising_check_mosi
  1297. //
  1298. // Write the PADREG Value to the PADREG register.
  1299. //
  1300. str r0, [r1]
  1301. bx lr
  1302. //
  1303. // Read GPIO pin 44, and loop until it's LOW.
  1304. //
  1305. falling_edge
  1306. ldr r2, =0x40010084
  1307. falling_check_mosi
  1308. ldr r3, [r2]
  1309. ands r3, r3, #0x1000
  1310. bne falling_check_mosi
  1311. //
  1312. // Write the PADREG Value to the PADREG register.
  1313. //
  1314. str r0, [r1]
  1315. bx lr
  1316. nop
  1317. }
  1318. #elif defined(__ICCARM__)
  1319. static void
  1320. iom_workaround_loop(uint32_t ui32PadRegVal, volatile uint32_t *pui32PadReg,
  1321. bool bRising)
  1322. {
  1323. //
  1324. // Check to see if this is a "rising edge" or "falling edge" detector.
  1325. //
  1326. asm(
  1327. " cbz r2, falling_edge\n"
  1328. //
  1329. // Read GPIO pin 44, and loop until it's HIGH.
  1330. //
  1331. "rising_edge:\n"
  1332. " mov32 r2, #0x40010084\n"
  1333. "rising_check_mosi:\n"
  1334. " ldr r3, [r2]\n"
  1335. " ands r3, r3, #0x1000\n"
  1336. " beq rising_check_mosi\n"
  1337. //
  1338. // Write the PADREG Value to the PADREG register.
  1339. //
  1340. " str r0, [r1]\n"
  1341. " bx lr\n"
  1342. //
  1343. // Read GPIO pin 44, and loop until it's LOW.
  1344. //
  1345. "falling_edge:\n"
  1346. " mov32 r2, #0x40010084\n"
  1347. "falling_check_mosi:\n"
  1348. " ldr r3, [r2]\n"
  1349. " ands r3, r3, #0x1000\n"
  1350. " bne falling_check_mosi\n"
  1351. //
  1352. // Write the PADREG Value to the PADREG register.
  1353. //
  1354. " str r0, [r1]\n"
  1355. " bx lr"
  1356. );
  1357. }
  1358. #endif
  1359. //*****************************************************************************
  1360. //
  1361. //! @brief Perform a simple write to the SPI interface.
  1362. //!
  1363. //! @param ui32Module - Module number for the IOM
  1364. //! @param ui32ChipSelect - Chip-select number for this transaction.
  1365. //! @param pui32Data - Pointer to the bytes that will be sent.
  1366. //! @param ui32NumBytes - Number of bytes to send.
  1367. //! @param ui32Options - Additional SPI transfer options.
  1368. //!
  1369. //! This function performs SPI writes to a selected SPI device.
  1370. //!
  1371. //! @note The actual SPI and I2C interfaces operate in BYTES, not 32-bit words.
  1372. //! This means that you will need to byte-pack the \e pui32Data array with the
  1373. //! data you intend to send over the interface. One easy way to do this is to
  1374. //! declare the array as a 32-bit integer array, but use an 8-bit pointer to
  1375. //! put your actual data into the array. If there are not enough bytes in your
  1376. //! desired message to completely fill the last 32-bit word, you may pad that
  1377. //! last word with bytes of any value. The IOM hardware will only read the
  1378. //! first \e ui32NumBytes in the \e pui8Data array.
  1379. //!
  1380. //! @return returns AM_HAL_IOM_SUCCESS on successful execution.
  1381. //
  1382. //*****************************************************************************
  1383. am_hal_iom_status_e
  1384. am_hal_iom_spi_write(uint32_t ui32Module, uint32_t ui32ChipSelect,
  1385. uint32_t *pui32Data, uint32_t ui32NumBytes,
  1386. uint32_t ui32Options)
  1387. {
  1388. am_hal_iom_status_e ui32Status;
  1389. //
  1390. // Validate parameters
  1391. //
  1392. if ( ui32Module >= AM_REG_IOMSTR_NUM_MODULES )
  1393. {
  1394. return AM_HAL_IOM_ERR_INVALID_MODULE;
  1395. }
  1396. // Reset the error status
  1397. ui32Status = g_iom_error_status[ui32Module] = AM_HAL_IOM_SUCCESS;
  1398. if (ui32NumBytes == 0)
  1399. {
  1400. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_INVALID_PARAM;
  1401. return ui32Status;
  1402. }
  1403. //
  1404. // Check to see if queues have been enabled. If they are, we'll actually
  1405. // switch to the queued interface.
  1406. //
  1407. if ( g_psIOMQueue[ui32Module].pui8Data != NULL )
  1408. {
  1409. //
  1410. // If the queue is on, go ahead and add this transaction to the queue.
  1411. //
  1412. ui32Status = am_hal_iom_queue_spi_write(ui32Module, ui32ChipSelect, pui32Data,
  1413. ui32NumBytes, ui32Options, 0);
  1414. if (ui32Status == AM_HAL_IOM_SUCCESS)
  1415. {
  1416. //
  1417. // Wait until the transaction actually clears.
  1418. //
  1419. am_hal_iom_queue_flush(ui32Module);
  1420. // g_iom_error_status gets set in the isr handling
  1421. ui32Status = g_iom_error_status[ui32Module];
  1422. }
  1423. //
  1424. // At this point, we've completed the transaction, and we can return.
  1425. //
  1426. }
  1427. else
  1428. {
  1429. //
  1430. // Otherwise, we'll just do a polled transaction.
  1431. //
  1432. ui32Status = am_hal_iom_spi_write_nq(ui32Module, ui32ChipSelect, pui32Data,
  1433. ui32NumBytes, ui32Options);
  1434. }
  1435. return ui32Status;
  1436. }
  1437. //*****************************************************************************
  1438. //
  1439. //! @brief Perform simple SPI read operations.
  1440. //!
  1441. //! @param ui32Module - Module number for the IOM
  1442. //! @param ui32ChipSelect - Chip-select number for this transaction.
  1443. //! @param pui32Data - Pointer to the array where received bytes should go.
  1444. //! @param ui32NumBytes - Number of bytes to read.
  1445. //! @param ui32Options - Additional SPI transfer options.
  1446. //!
  1447. //! This function performs simple SPI read operations. The caller is
  1448. //! responsible for ensuring that the receive buffer is large enough to hold
  1449. //! the requested amount of data.
  1450. //!
  1451. //! @note The actual SPI and I2C interfaces operate in BYTES, not 32-bit words.
  1452. //! This function will pack the individual bytes from the physical interface
  1453. //! into 32-bit words, which are then placed into the \e pui32Data array. Only
  1454. //! the first \e ui32NumBytes bytes in this array will contain valid data.
  1455. //!
  1456. //! @return returns AM_HAL_IOM_SUCCESS on successful execution.
  1457. //
  1458. //*****************************************************************************
  1459. am_hal_iom_status_e
  1460. am_hal_iom_spi_read(uint32_t ui32Module, uint32_t ui32ChipSelect,
  1461. uint32_t *pui32Data, uint32_t ui32NumBytes,
  1462. uint32_t ui32Options)
  1463. {
  1464. am_hal_iom_status_e ui32Status;
  1465. //
  1466. // Validate parameters
  1467. //
  1468. if ( ui32Module >= AM_REG_IOMSTR_NUM_MODULES )
  1469. {
  1470. return AM_HAL_IOM_ERR_INVALID_MODULE;
  1471. }
  1472. // Reset the error status
  1473. AM_REGn(IOMSTR, ui32Module, INTCLR) = AM_HAL_IOM_INT_ALL;
  1474. ui32Status = g_iom_error_status[ui32Module] = AM_HAL_IOM_SUCCESS;
  1475. if (ui32NumBytes == 0)
  1476. {
  1477. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_INVALID_PARAM;
  1478. return ui32Status;
  1479. }
  1480. //
  1481. // Make sure the transfer isn't too long for the hardware to support.
  1482. //
  1483. if (ui32NumBytes >= 4096)
  1484. {
  1485. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_INVALID_PARAM;
  1486. return ui32Status;
  1487. }
  1488. //
  1489. // Check to see if queues have been enabled. If they are, we'll actually
  1490. // switch to the queued interface.
  1491. //
  1492. if ( g_psIOMQueue[ui32Module].pui8Data != NULL )
  1493. {
  1494. //
  1495. // If the queue is on, go ahead and add this transaction to the queue.
  1496. //
  1497. ui32Status = am_hal_iom_queue_spi_read(ui32Module, ui32ChipSelect, pui32Data,
  1498. ui32NumBytes, ui32Options, 0);
  1499. if (ui32Status == AM_HAL_IOM_SUCCESS)
  1500. {
  1501. //
  1502. // Wait until the transaction actually clears.
  1503. //
  1504. am_hal_iom_queue_flush(ui32Module);
  1505. // g_iom_error_status gets set in the isr handling
  1506. ui32Status = g_iom_error_status[ui32Module];
  1507. }
  1508. //
  1509. // At this point, we've completed the transaction, and we can return.
  1510. //
  1511. }
  1512. else
  1513. {
  1514. //
  1515. // Otherwise, just perform a polled transaction.
  1516. //
  1517. ui32Status = am_hal_iom_spi_read_nq(ui32Module, ui32ChipSelect, pui32Data,
  1518. ui32NumBytes, ui32Options);
  1519. }
  1520. return ui32Status;
  1521. }
  1522. //*****************************************************************************
  1523. //
  1524. //! @brief Perform a simple full-duplex transaction to the SPI interface.
  1525. //!
  1526. //! @param ui32Module - Module number for the IOM
  1527. //! @param ui32ChipSelect - Chip-select number for this transaction.
  1528. //! @param pui32TxData - Pointer to the bytes that will be sent.
  1529. //! @param pui32RxData - Pointer to the bytes that will be received.
  1530. //! @param ui32NumBytes - Number of bytes to send.
  1531. //! @param ui32Options - Additional SPI transfer options.
  1532. //!
  1533. //! This function performs SPI full-duplex operation to a selected SPI device.
  1534. //!
  1535. //! @note The actual SPI and I2C interfaces operate in BYTES, not 32-bit words.
  1536. //! This means that you will need to byte-pack the \e pui32TxData array with the
  1537. //! data you intend to send over the interface. One easy way to do this is to
  1538. //! declare the array as a 32-bit integer array, but use an 8-bit pointer to
  1539. //! put your actual data into the array. If there are not enough bytes in your
  1540. //! desired message to completely fill the last 32-bit word, you may pad that
  1541. //! last word with bytes of any value. The IOM hardware will only read the
  1542. //! first \e ui32NumBytes in the \e pui32TxData array.
  1543. //!
  1544. //! @return returns AM_HAL_IOM_SUCCESS on successful execution.
  1545. //
  1546. //*****************************************************************************
  1547. am_hal_iom_status_e
  1548. am_hal_iom_spi_fullduplex(uint32_t ui32Module, uint32_t ui32ChipSelect,
  1549. uint32_t *pui32TxData, uint32_t *pui32RxData,
  1550. uint32_t ui32NumBytes, uint32_t ui32Options)
  1551. {
  1552. am_hal_iom_status_e ui32Status;
  1553. //
  1554. // Validate parameters
  1555. //
  1556. if ( ui32Module >= AM_REG_IOMSTR_NUM_MODULES )
  1557. {
  1558. return AM_HAL_IOM_ERR_INVALID_MODULE;
  1559. }
  1560. // Reset the error status
  1561. ui32Status = g_iom_error_status[ui32Module] = AM_HAL_IOM_SUCCESS;
  1562. if (ui32NumBytes == 0)
  1563. {
  1564. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_INVALID_PARAM;
  1565. return ui32Status;
  1566. }
  1567. //
  1568. // Full-Duplex operation is only supported for Apollo2 B2 Silicon.
  1569. //
  1570. if (!isRevB2())
  1571. {
  1572. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_INVALID_OPER;
  1573. return ui32Status;
  1574. }
  1575. //
  1576. // Check to see if queues have been enabled. If they are, we'll actually
  1577. // switch to the queued interface.
  1578. //
  1579. #if 0
  1580. // To be implemented!!!
  1581. if ( g_psIOMQueue[ui32Module].pui8Data != NULL )
  1582. {
  1583. //
  1584. // If the queue is on, go ahead and add this transaction to the queue.
  1585. //
  1586. ui32Status = am_hal_iom_queue_spi_write(ui32Module, ui32ChipSelect, pui32Data,
  1587. ui32NumBytes, ui32Options, 0);
  1588. if (ui32Status == AM_HAL_IOM_SUCCESS)
  1589. {
  1590. //
  1591. // Wait until the transaction actually clears.
  1592. //
  1593. am_hal_iom_queue_flush(ui32Module);
  1594. // g_iom_error_status gets set in the isr handling
  1595. ui32Status = g_iom_error_status[ui32Module];
  1596. }
  1597. //
  1598. // At this point, we've completed the transaction, and we can return.
  1599. //
  1600. }
  1601. else
  1602. {
  1603. #endif
  1604. //
  1605. // Otherwise, we'll just do a polled transaction.
  1606. //
  1607. ui32Status = am_hal_iom_spi_fullduplex_nq(ui32Module, ui32ChipSelect,
  1608. pui32TxData, pui32RxData,
  1609. ui32NumBytes, ui32Options);
  1610. #if 0
  1611. }
  1612. #endif
  1613. return ui32Status;
  1614. }
  1615. //*****************************************************************************
  1616. //
  1617. //! @brief Perform a simple write to the SPI interface (without queuing)
  1618. //!
  1619. //! @param ui32Module - Module number for the IOM
  1620. //! @param ui32ChipSelect - Chip-select number for this transaction.
  1621. //! @param pui32Data - Pointer to the bytes that will be sent.
  1622. //! @param ui32NumBytes - Number of bytes to send.
  1623. //! @param ui32Options - Additional SPI transfer options.
  1624. //!
  1625. //! This function performs SPI writes to a selected SPI device.
  1626. //!
  1627. //! @note The actual SPI and I2C interfaces operate in BYTES, not 32-bit words.
  1628. //! This means that you will need to byte-pack the \e pui32Data array with the
  1629. //! data you intend to send over the interface. One easy way to do this is to
  1630. //! declare the array as a 32-bit integer array, but use an 8-bit pointer to
  1631. //! put your actual data into the array. If there are not enough bytes in your
  1632. //! desired message to completely fill the last 32-bit word, you may pad that
  1633. //! last word with bytes of any value. The IOM hardware will only read the
  1634. //! first \e ui32NumBytes in the \e pui8Data array.
  1635. //!
  1636. //! @return returns AM_HAL_IOM_SUCCESS on successful execution.
  1637. //
  1638. //*****************************************************************************
  1639. am_hal_iom_status_e
  1640. am_hal_iom_spi_write_nq(uint32_t ui32Module, uint32_t ui32ChipSelect,
  1641. uint32_t *pui32Data, uint32_t ui32NumBytes,
  1642. uint32_t ui32Options)
  1643. {
  1644. uint32_t ui32TransferSize;
  1645. uint32_t ui32SpaceInFifo;
  1646. uint32_t ui32IntConfig;
  1647. uint32_t ui32MaxFifoSize;
  1648. am_hal_iom_status_e ui32Status;
  1649. uint32_t waitStatus;
  1650. //
  1651. // Validate parameters
  1652. //
  1653. if ( ui32Module >= AM_REG_IOMSTR_NUM_MODULES )
  1654. {
  1655. return AM_HAL_IOM_ERR_INVALID_MODULE;
  1656. }
  1657. //
  1658. // Wait until the bus is idle
  1659. //
  1660. am_hal_iom_poll_complete(ui32Module);
  1661. // Reset the error status
  1662. ui32Status = g_iom_error_status[ui32Module] = AM_HAL_IOM_SUCCESS;
  1663. if (ui32NumBytes == 0)
  1664. {
  1665. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_INVALID_PARAM;
  1666. return ui32Status;
  1667. }
  1668. //
  1669. // Make sure the transfer isn't too long for the hardware to support.
  1670. //
  1671. if (ui32NumBytes >= 4096)
  1672. {
  1673. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_INVALID_PARAM;
  1674. return ui32Status;
  1675. }
  1676. ui32MaxFifoSize = ((0 == AM_BFRn(IOMSTR, ui32Module, CFG, FULLDUP)) ?
  1677. AM_HAL_IOM_MAX_FIFO_SIZE : AM_HAL_IOM_MAX_FIFO_SIZE / 2);
  1678. //
  1679. // Disable interrupts so that we don't get any undesired interrupts.
  1680. //
  1681. ui32IntConfig = AM_REGn(IOMSTR, ui32Module, INTEN);
  1682. AM_REGn(IOMSTR, ui32Module, INTEN) = 0;
  1683. // Clear interrupts
  1684. AM_REGn(IOMSTR, ui32Module, INTCLR) = AM_HAL_IOM_INT_ALL;
  1685. //
  1686. // If we're on a B0 part, and we're using IOM4, our first byte coule be
  1687. // corrupted, so we need to send a dummy word with chip-select held high to
  1688. // get that first byte out of the way.
  1689. //
  1690. // That operation is tricky and detailed, so we'll call a function to do it
  1691. // for us.
  1692. //
  1693. if ( WORKAROUND_IOM == ui32Module && isRevB0() )
  1694. {
  1695. am_hal_iom_workaround_word_write(ui32ChipSelect, pui32Data,
  1696. ui32NumBytes, ui32Options);
  1697. //
  1698. // The workaround function is going to a partial transfer for us, but
  1699. // we have to keep our own data-tracking variables updated. Here, we're
  1700. // subtracting 4 bytes from the effective transfer size to account for
  1701. // the 4 bytes of "dummy" word that we sent instead of the actual data.
  1702. //
  1703. ui32TransferSize = (ui32NumBytes <= (ui32MaxFifoSize - 4) ? ui32NumBytes :
  1704. (ui32MaxFifoSize - 4));
  1705. }
  1706. else
  1707. {
  1708. //
  1709. // Figure out how many bytes we can write to the FIFO immediately.
  1710. //
  1711. ui32TransferSize = (ui32NumBytes <= ui32MaxFifoSize ? ui32NumBytes :
  1712. ui32MaxFifoSize);
  1713. //
  1714. // write our first word to the fifo.
  1715. //
  1716. am_hal_iom_fifo_write(ui32Module, pui32Data, ui32TransferSize);
  1717. //
  1718. // Start the write on the bus.
  1719. //
  1720. am_hal_iom_spi_cmd_run(AM_HAL_IOM_WRITE, ui32Module, ui32ChipSelect,
  1721. ui32NumBytes, ui32Options);
  1722. }
  1723. //
  1724. // Update the pointer and data counter.
  1725. //
  1726. ui32NumBytes -= ui32TransferSize;
  1727. pui32Data += ui32TransferSize >> 2;
  1728. //
  1729. // Keep looping until we're out of bytes to send or command complete (error).
  1730. //
  1731. while ( ui32NumBytes && !AM_BFRn(IOMSTR, ui32Module, INTSTAT, CMDCMP) )
  1732. {
  1733. //
  1734. // This will always return a multiple of four.
  1735. //
  1736. ui32SpaceInFifo = am_hal_iom_fifo_empty_slots(ui32Module);
  1737. if ( ui32NumBytes <= ui32SpaceInFifo )
  1738. {
  1739. //
  1740. // If the entire message will fit in the fifo, prepare to copy
  1741. // everything.
  1742. //
  1743. ui32TransferSize = ui32NumBytes;
  1744. }
  1745. else
  1746. {
  1747. //
  1748. // If only a portion of the message will fit in the fifo, prepare
  1749. // to copy the largest number of 4-byte blocks possible.
  1750. //
  1751. ui32TransferSize = ui32SpaceInFifo & ~(0x3);
  1752. }
  1753. //
  1754. // Write this chunk to the fifo.
  1755. //
  1756. am_hal_iom_fifo_write(ui32Module, pui32Data, ui32TransferSize);
  1757. //
  1758. // Update the data pointer and bytes-left count.
  1759. //
  1760. ui32NumBytes -= ui32TransferSize;
  1761. pui32Data += ui32TransferSize >> 2;
  1762. }
  1763. //
  1764. // Make sure CMDCMP was raised with standard timeout
  1765. //
  1766. waitStatus = am_hal_flash_delay_status_change(ui32StatusTimeout[ui32Module],
  1767. AM_REG_IOMSTRn(ui32Module) + AM_REG_IOMSTR_INTSTAT_O,
  1768. AM_REG_IOMSTR_INTEN_CMDCMP_M, AM_REG_IOMSTR_INTEN_CMDCMP_M);
  1769. if (waitStatus != 1)
  1770. {
  1771. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_TIMEOUT;
  1772. }
  1773. else
  1774. {
  1775. g_iom_error_status[ui32Module] = ui32Status = internal_iom_get_int_err(ui32Module, 0);
  1776. }
  1777. //
  1778. // Re-enable IOM interrupts.
  1779. //
  1780. // Clear interrupts
  1781. AM_REGn(IOMSTR, ui32Module, INTCLR) = AM_HAL_IOM_INT_ALL;
  1782. AM_REGn(IOMSTR, ui32Module, INTEN) = ui32IntConfig;
  1783. return ui32Status;
  1784. }
  1785. //*****************************************************************************
  1786. //
  1787. //! @brief Perform simple SPI read operations (without queuing).
  1788. //!
  1789. //! @param ui32Module - Module number for the IOM
  1790. //! @param ui32ChipSelect - Chip-select number for this transaction.
  1791. //! @param pui32Data - Pointer to the array where received bytes should go.
  1792. //! @param ui32NumBytes - Number of bytes to read.
  1793. //! @param ui32Options - Additional SPI transfer options.
  1794. //!
  1795. //! This function performs simple SPI read operations. The caller is
  1796. //! responsible for ensuring that the receive buffer is large enough to hold
  1797. //! the requested amount of data.
  1798. //!
  1799. //! @note The actual SPI and I2C interfaces operate in BYTES, not 32-bit words.
  1800. //! This function will pack the individual bytes from the physical interface
  1801. //! into 32-bit words, which are then placed into the \e pui32Data array. Only
  1802. //! the first \e ui32NumBytes bytes in this array will contain valid data.
  1803. //!
  1804. //! @return returns AM_HAL_IOM_SUCCESS on successful execution.
  1805. //
  1806. //*****************************************************************************
  1807. am_hal_iom_status_e
  1808. am_hal_iom_spi_read_nq(uint32_t ui32Module, uint32_t ui32ChipSelect,
  1809. uint32_t *pui32Data, uint32_t ui32NumBytes,
  1810. uint32_t ui32Options)
  1811. {
  1812. uint32_t ui32BytesInFifo;
  1813. uint32_t ui32IntConfig;
  1814. uint32_t bCmdCmp = false;
  1815. am_hal_iom_status_e ui32Status;
  1816. uint32_t waitStatus;
  1817. //
  1818. // Validate parameters
  1819. //
  1820. if ( ui32Module >= AM_REG_IOMSTR_NUM_MODULES )
  1821. {
  1822. return AM_HAL_IOM_ERR_INVALID_MODULE;
  1823. }
  1824. //
  1825. // Wait until the bus is idle
  1826. //
  1827. am_hal_iom_poll_complete(ui32Module);
  1828. // Reset the error status
  1829. ui32Status = g_iom_error_status[ui32Module] = AM_HAL_IOM_SUCCESS;
  1830. if (ui32NumBytes == 0)
  1831. {
  1832. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_INVALID_PARAM;
  1833. return ui32Status;
  1834. }
  1835. //
  1836. // Make sure the transfer isn't too long for the hardware to support.
  1837. //
  1838. if (ui32NumBytes >= 4096)
  1839. {
  1840. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_INVALID_PARAM;
  1841. return ui32Status;
  1842. }
  1843. //
  1844. // Disable interrupts so that we don't get any undesired interrupts.
  1845. //
  1846. ui32IntConfig = AM_REGn(IOMSTR, ui32Module, INTEN);
  1847. //
  1848. // Disable IOM interrupts as we'll be polling
  1849. //
  1850. AM_REGn(IOMSTR, ui32Module, INTEN) = 0;
  1851. // Clear interrupts
  1852. AM_REGn(IOMSTR, ui32Module, INTCLR) = AM_HAL_IOM_INT_ALL;
  1853. //
  1854. // If we're on a B0 part, and we're using IOM4, our first byte coule be
  1855. // corrupted, so we need to send a dummy word with chip-select held high to
  1856. // get that first byte out of the way. This is only true for spi reads with
  1857. // OFFSET values.
  1858. //
  1859. // That operation is tricky and detailed, so we'll call a function to do it
  1860. // for us.
  1861. //
  1862. if ( (WORKAROUND_IOM == ui32Module) && !(ui32Options & AM_HAL_IOM_RAW) &&
  1863. isRevB0() )
  1864. {
  1865. am_hal_iom_workaround_word_write(ui32ChipSelect, pui32Data, 0,
  1866. ui32Options | AM_HAL_IOM_CS_LOW);
  1867. //
  1868. // The workaround will send our offset for us, so we can run a RAW
  1869. // command after.
  1870. //
  1871. ui32Options |= AM_HAL_IOM_RAW;
  1872. //
  1873. // Wait for the dummy word to go out over the bus.
  1874. //
  1875. // Make sure the command complete has also been raised
  1876. waitStatus = am_hal_flash_delay_status_change(ui32StatusTimeout[ui32Module],
  1877. AM_REG_IOMSTRn(ui32Module) + AM_REG_IOMSTR_INTSTAT_O,
  1878. AM_REG_IOMSTR_INTEN_CMDCMP_M, AM_REG_IOMSTR_INTEN_CMDCMP_M);
  1879. if (waitStatus != 1)
  1880. {
  1881. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_TIMEOUT;
  1882. //
  1883. // Re-enable IOM interrupts.
  1884. //
  1885. // Clear interrupts
  1886. AM_REGn(IOMSTR, ui32Module, INTCLR) = AM_HAL_IOM_INT_ALL;
  1887. AM_REGn(IOMSTR, ui32Module, INTEN) = ui32IntConfig;
  1888. return ui32Status;
  1889. }
  1890. // Clear CMDCMP status
  1891. AM_BFWn(IOMSTR, ui32Module, INTCLR, CMDCMP, 1);
  1892. }
  1893. am_hal_iom_spi_cmd_run(AM_HAL_IOM_READ, ui32Module, ui32ChipSelect,
  1894. ui32NumBytes, ui32Options);
  1895. //
  1896. // Start a loop to catch the Rx data.
  1897. //
  1898. while ( ui32NumBytes )
  1899. {
  1900. ui32BytesInFifo = am_hal_iom_fifo_full_slots(ui32Module);
  1901. if ( ui32BytesInFifo >= ui32NumBytes )
  1902. {
  1903. //
  1904. // If the fifo contains our entire message, just copy the whole
  1905. // thing out.
  1906. //
  1907. am_hal_iom_fifo_read(ui32Module, pui32Data, ui32NumBytes);
  1908. ui32NumBytes = 0;
  1909. }
  1910. else if ( ui32BytesInFifo >= 4 )
  1911. {
  1912. //
  1913. // If the fifo has at least one 32-bit word in it, copy whole
  1914. // words out.
  1915. //
  1916. am_hal_iom_fifo_read(ui32Module, pui32Data, ui32BytesInFifo & ~0x3);
  1917. ui32NumBytes -= ui32BytesInFifo & ~0x3;
  1918. pui32Data += ui32BytesInFifo >> 2;
  1919. }
  1920. if ( bCmdCmp == true )
  1921. {
  1922. //
  1923. // No more data expected. Get out of the loop
  1924. //
  1925. break;
  1926. }
  1927. bCmdCmp = AM_BFRn(IOMSTR, ui32Module, INTSTAT, CMDCMP);
  1928. }
  1929. //
  1930. // Make sure CMDCMP was raised,
  1931. //
  1932. waitStatus = am_hal_flash_delay_status_change(ui32StatusTimeout[ui32Module],
  1933. AM_REG_IOMSTRn(ui32Module) + AM_REG_IOMSTR_INTSTAT_O,
  1934. AM_REG_IOMSTR_INTEN_CMDCMP_M, AM_REG_IOMSTR_INTEN_CMDCMP_M);
  1935. if (waitStatus != 1)
  1936. {
  1937. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_TIMEOUT;
  1938. }
  1939. else
  1940. {
  1941. g_iom_error_status[ui32Module] = ui32Status = internal_iom_get_int_err(ui32Module, 0);
  1942. }
  1943. //
  1944. // Re-enable IOM interrupts. Make sure CMDCMP is cleared
  1945. //
  1946. // Clear interrupts
  1947. AM_REGn(IOMSTR, ui32Module, INTCLR) = AM_HAL_IOM_INT_ALL;
  1948. AM_REGn(IOMSTR, ui32Module, INTEN) = ui32IntConfig;
  1949. return ui32Status;
  1950. }
  1951. //*****************************************************************************
  1952. //
  1953. //! @brief Perform a simple full-duplex operation to the SPI interface (without queuing)
  1954. //!
  1955. //! @param ui32Module - Module number for the IOM
  1956. //! @param ui32ChipSelect - Chip-select number for this transaction.
  1957. //! @param pui32Data - Pointer to the bytes that will be sent.
  1958. //! @param pui32Data - Pointer to the bytes that will be received.
  1959. //! @param ui32NumBytes - Number of bytes to send.
  1960. //! @param ui32Options - Additional SPI transfer options.
  1961. //!
  1962. //! This function performs SPI full-duplex operation to a selected SPI device.
  1963. //!
  1964. //! @note The actual SPI and I2C interfaces operate in BYTES, not 32-bit words.
  1965. //! This means that you will need to byte-pack the \e pui32TxData array with the
  1966. //! data you intend to send over the interface. One easy way to do this is to
  1967. //! declare the array as a 32-bit integer array, but use an 8-bit pointer to
  1968. //! put your actual data into the array. If there are not enough bytes in your
  1969. //! desired message to completely fill the last 32-bit word, you may pad that
  1970. //! last word with bytes of any value. The IOM hardware will only read the
  1971. //! first \e ui32NumBytes in the \e pui32TxData array.
  1972. //!
  1973. //! @return returns AM_HAL_IOM_SUCCESS on successful execution.
  1974. //
  1975. //*****************************************************************************
  1976. am_hal_iom_status_e
  1977. am_hal_iom_spi_fullduplex_nq(uint32_t ui32Module, uint32_t ui32ChipSelect,
  1978. uint32_t *pui32TxData, uint32_t *pui32RxData,
  1979. uint32_t ui32NumBytes, uint32_t ui32Options)
  1980. {
  1981. uint32_t ui32TransferSize;
  1982. uint32_t ui32SpaceInFifo;
  1983. uint32_t bCmdCmp = false;
  1984. uint32_t ui32IntConfig;
  1985. uint32_t ui32MaxFifoSize;
  1986. am_hal_iom_status_e ui32Status;
  1987. uint32_t waitStatus;
  1988. //
  1989. // Validate parameters
  1990. //
  1991. if ( ui32Module >= AM_REG_IOMSTR_NUM_MODULES )
  1992. {
  1993. return AM_HAL_IOM_ERR_INVALID_MODULE;
  1994. }
  1995. //
  1996. // Wait until the bus is idle
  1997. //
  1998. am_hal_iom_poll_complete(ui32Module);
  1999. // Reset the error status
  2000. ui32Status = g_iom_error_status[ui32Module] = AM_HAL_IOM_SUCCESS;
  2001. if (ui32NumBytes == 0)
  2002. {
  2003. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_INVALID_PARAM;
  2004. return ui32Status;
  2005. }
  2006. //
  2007. // Make sure the transfer isn't too long for the hardware to support.
  2008. //
  2009. if (ui32NumBytes >= 4096)
  2010. {
  2011. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_INVALID_PARAM;
  2012. return ui32Status;
  2013. }
  2014. //
  2015. // Full-Duplex operation is only supported for Apollo2 B2 Silicon.
  2016. //
  2017. if (!isRevB2())
  2018. {
  2019. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_INVALID_OPER;
  2020. return ui32Status;
  2021. }
  2022. //
  2023. // Put the IOM into Full-Duplex mode.
  2024. //
  2025. AM_REGn(IOMSTR, ui32Module, CFG) |= AM_REG_IOMSTR_CFG_FULLDUP_FULLDUP;
  2026. ui32MaxFifoSize = ((0 == AM_BFRn(IOMSTR, ui32Module, CFG, FULLDUP)) ?
  2027. AM_HAL_IOM_MAX_FIFO_SIZE : AM_HAL_IOM_MAX_FIFO_SIZE / 2);
  2028. //
  2029. // Disable interrupts so that we don't get any undesired interrupts.
  2030. //
  2031. ui32IntConfig = AM_REGn(IOMSTR, ui32Module, INTEN);
  2032. AM_REGn(IOMSTR, ui32Module, INTEN) = 0;
  2033. // Clear interrupts
  2034. AM_REGn(IOMSTR, ui32Module, INTCLR) = AM_HAL_IOM_INT_ALL;
  2035. //
  2036. // Figure out how many bytes we can write to the FIFO immediately.
  2037. //
  2038. ui32TransferSize = (ui32NumBytes <= ui32MaxFifoSize ? ui32NumBytes :
  2039. ui32MaxFifoSize);
  2040. //
  2041. // write up to the maximum FIFO size into the fifo.
  2042. //
  2043. am_hal_iom_fifo_write(ui32Module, pui32TxData, ui32TransferSize);
  2044. //
  2045. // Start the write on the bus.
  2046. //
  2047. am_hal_iom_spi_cmd_run(AM_HAL_IOM_WRITE, ui32Module, ui32ChipSelect,
  2048. ui32NumBytes, ui32Options);
  2049. //
  2050. // Update the pointer and data counter.
  2051. //
  2052. pui32TxData += ui32TransferSize >> 2;
  2053. //
  2054. // Keep looping until we're out of bytes to receive or command complete (error).
  2055. //
  2056. while ( ui32NumBytes )
  2057. {
  2058. //
  2059. // Wait for FIFO to empty.
  2060. //
  2061. while (am_hal_iom_fifo_full_slots(ui32Module) > 0);
  2062. am_hal_iom_fifo_read(ui32Module, pui32RxData, ui32TransferSize);
  2063. ui32NumBytes -= ui32TransferSize;
  2064. pui32RxData += ui32TransferSize >> 2;
  2065. //
  2066. // Get the number of bytes that can be written (always a multiple of 4).
  2067. //
  2068. ui32SpaceInFifo = am_hal_iom_fifo_empty_slots(ui32Module);
  2069. if ( ui32NumBytes <= ui32SpaceInFifo )
  2070. {
  2071. //
  2072. // If the entire message will fit in the fifo, prepare to copy
  2073. // everything.
  2074. //
  2075. ui32TransferSize = ui32NumBytes;
  2076. }
  2077. else
  2078. {
  2079. //
  2080. // If only a portion of the message will fit in the fifo, prepare
  2081. // to copy the largest number of 4-byte blocks possible.
  2082. //
  2083. ui32TransferSize = ui32SpaceInFifo & ~(0x3);
  2084. }
  2085. //
  2086. // Write this chunk to the fifo.
  2087. //
  2088. am_hal_iom_fifo_write(ui32Module, pui32TxData, ui32TransferSize);
  2089. //
  2090. // Update the data pointer and bytes-left count.
  2091. //
  2092. pui32TxData += ui32TransferSize >> 2;
  2093. //
  2094. // Check for Command Complete condition.
  2095. if ( bCmdCmp == true )
  2096. {
  2097. //
  2098. // No more data expected. Get out of the loop
  2099. //
  2100. break;
  2101. }
  2102. bCmdCmp = AM_BFRn(IOMSTR, ui32Module, INTSTAT, CMDCMP);
  2103. }
  2104. //
  2105. // Make sure CMDCMP was raised with standard timeout
  2106. //
  2107. waitStatus = am_hal_flash_delay_status_change(ui32StatusTimeout[ui32Module],
  2108. AM_REG_IOMSTRn(ui32Module) + AM_REG_IOMSTR_INTSTAT_O,
  2109. AM_REG_IOMSTR_INTEN_CMDCMP_M, AM_REG_IOMSTR_INTEN_CMDCMP_M);
  2110. if (waitStatus != 1)
  2111. {
  2112. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_TIMEOUT;
  2113. }
  2114. else
  2115. {
  2116. g_iom_error_status[ui32Module] = ui32Status = internal_iom_get_int_err(ui32Module, 0);
  2117. }
  2118. //
  2119. // Re-enable IOM interrupts.
  2120. //
  2121. // Clear interrupts
  2122. AM_REGn(IOMSTR, ui32Module, INTCLR) = AM_HAL_IOM_INT_ALL;
  2123. AM_REGn(IOMSTR, ui32Module, INTEN) = ui32IntConfig;
  2124. //
  2125. // Take the IOM out of Full-Duplex mode.
  2126. //
  2127. AM_REGn(IOMSTR, ui32Module, CFG) &= ~AM_REG_IOMSTR_CFG_FULLDUP_FULLDUP;
  2128. return ui32Status;
  2129. }
  2130. //*****************************************************************************
  2131. //
  2132. //! @brief Perform a non-blocking write to the SPI interface.
  2133. //!
  2134. //! @param ui32Module - Module number for the IOM
  2135. //! @param ui32ChipSelect - Chip-select number for this transaction.
  2136. //! @param pui32Data - Pointer to the bytes that will be sent.
  2137. //! @param ui32NumBytes - Number of bytes to send.
  2138. //! @param ui32Options - Additional SPI transfer options.
  2139. //! @param pfnCallback - Function to call when the transaction completes.
  2140. //!
  2141. //! This function performs SPI writes to the selected SPI device.
  2142. //!
  2143. //! This function call is a non-blocking implementation. It will write as much
  2144. //! data to the FIFO as possible immediately, store a pointer to the remaining
  2145. //! data, start the transfer on the bus, and then immediately return. The
  2146. //! caller will need to make sure that \e am_hal_iom_int_service() is called
  2147. //! for IOM FIFO interrupt events and "command complete" interrupt events. The
  2148. //! \e am_hal_iom_int_service() function will refill the FIFO as necessary and
  2149. //! call the \e pfnCallback function when the transaction is finished.
  2150. //!
  2151. //! @note The actual SPI and I2C interfaces operate in BYTES, not 32-bit words.
  2152. //! This means that you will need to byte-pack the \e pui32Data array with the
  2153. //! data you intend to send over the interface. One easy way to do this is to
  2154. //! declare the array as a 32-bit integer array, but use an 8-bit pointer to
  2155. //! put your actual data into the array. If there are not enough bytes in your
  2156. //! desired message to completely fill the last 32-bit word, you may pad that
  2157. //! last word with bytes of any value. The IOM hardware will only read the
  2158. //! first \e ui32NumBytes in the \e pui8Data array.
  2159. //!
  2160. //! @return returns AM_HAL_IOM_SUCCESS on successful execution. Note that
  2161. //! successful execution for non-blocking call only means the transaction was
  2162. //! successfully initiated. The status of the transaction is not known till the
  2163. //! callback is called on completion
  2164. //
  2165. //*****************************************************************************
  2166. am_hal_iom_status_e
  2167. am_hal_iom_spi_write_nb(uint32_t ui32Module, uint32_t ui32ChipSelect,
  2168. uint32_t *pui32Data, uint32_t ui32NumBytes,
  2169. uint32_t ui32Options,
  2170. am_hal_iom_callback_t pfnCallback)
  2171. {
  2172. am_hal_iom_status_e ui32Status;
  2173. uint32_t ui32TransferSize;
  2174. uint32_t ui32MaxFifoSize;
  2175. //
  2176. // Validate parameters
  2177. //
  2178. if ( ui32Module >= AM_REG_IOMSTR_NUM_MODULES )
  2179. {
  2180. return AM_HAL_IOM_ERR_INVALID_MODULE;
  2181. }
  2182. //
  2183. // Wait until the bus is idle
  2184. //
  2185. am_hal_iom_poll_complete(ui32Module);
  2186. // Reset the error status for non-blocking transfer
  2187. ui32Status = g_iom_error_status[ui32Module] = AM_HAL_IOM_SUCCESS;
  2188. if (ui32NumBytes == 0)
  2189. {
  2190. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_INVALID_PARAM;
  2191. return ui32Status;
  2192. }
  2193. //
  2194. // Make sure the transfer isn't too long for the hardware to support.
  2195. //
  2196. if (ui32NumBytes >= 4096)
  2197. {
  2198. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_INVALID_PARAM;
  2199. return ui32Status;
  2200. }
  2201. ui32MaxFifoSize = ((0 == AM_BFRn(IOMSTR, ui32Module, CFG, FULLDUP)) ?
  2202. AM_HAL_IOM_MAX_FIFO_SIZE : AM_HAL_IOM_MAX_FIFO_SIZE / 2);
  2203. //
  2204. // Need to mark IOM busy to avoid another transaction to be scheduled.
  2205. // This is to take care of a race condition in Queue mode, where the IDLE
  2206. // set is not a guarantee that the CMDCMP has been received
  2207. //
  2208. g_bIomBusy[ui32Module] = true;
  2209. // Clear interrupts
  2210. AM_REGn(IOMSTR, ui32Module, INTCLR) = AM_HAL_IOM_INT_ALL;
  2211. //
  2212. // Check to see if we need to do the workaround.
  2213. //
  2214. if ( WORKAROUND_IOM == ui32Module && isRevB0() )
  2215. {
  2216. //
  2217. // Figure out how many bytes we can write to the FIFO immediately,
  2218. // accounting for the extra word from the workaround.
  2219. //
  2220. ui32TransferSize = (ui32NumBytes <= (ui32MaxFifoSize - 4) ? ui32NumBytes :
  2221. (ui32MaxFifoSize - 4));
  2222. //
  2223. // Prepare the global IOM buffer structure.
  2224. //
  2225. g_psIOMBuffers[ui32Module].ui32State = BUFFER_SENDING;
  2226. g_psIOMBuffers[ui32Module].pui32Data = pui32Data + (ui32TransferSize / 4);
  2227. g_psIOMBuffers[ui32Module].ui32BytesLeft = ui32NumBytes - ui32TransferSize;
  2228. g_psIOMBuffers[ui32Module].pfnCallback = pfnCallback;
  2229. g_psIOMBuffers[ui32Module].ui32Options = ui32Options;
  2230. //
  2231. // Start the write on the bus using the workaround. This includes both
  2232. // the command write and the first fifo write, so we won't need to do
  2233. // either of those things manually.
  2234. //
  2235. am_hal_iom_workaround_word_write(ui32ChipSelect, pui32Data,
  2236. ui32NumBytes, ui32Options);
  2237. }
  2238. else
  2239. {
  2240. //
  2241. // Figure out how many bytes we can write to the FIFO immediately.
  2242. //
  2243. ui32TransferSize = (ui32NumBytes <= ui32MaxFifoSize ? ui32NumBytes :
  2244. ui32MaxFifoSize);
  2245. if ( am_hal_iom_fifo_write(ui32Module, pui32Data, ui32TransferSize) > 0 )
  2246. {
  2247. //
  2248. // Prepare the global IOM buffer structure.
  2249. //
  2250. g_psIOMBuffers[ui32Module].ui32State = BUFFER_SENDING;
  2251. g_psIOMBuffers[ui32Module].pui32Data = pui32Data;
  2252. g_psIOMBuffers[ui32Module].ui32BytesLeft = ui32NumBytes;
  2253. g_psIOMBuffers[ui32Module].pfnCallback = pfnCallback;
  2254. g_psIOMBuffers[ui32Module].ui32Options = ui32Options;
  2255. //
  2256. // Update the pointer and the byte counter based on the portion of
  2257. // the transfer we just sent to the fifo.
  2258. //
  2259. g_psIOMBuffers[ui32Module].ui32BytesLeft -= ui32TransferSize;
  2260. g_psIOMBuffers[ui32Module].pui32Data += (ui32TransferSize / 4);
  2261. //
  2262. // Start the write on the bus.
  2263. //
  2264. am_hal_iom_spi_cmd_run(AM_HAL_IOM_WRITE, ui32Module, ui32ChipSelect,
  2265. ui32NumBytes, ui32Options);
  2266. }
  2267. }
  2268. return ui32Status;
  2269. }
  2270. //*****************************************************************************
  2271. //
  2272. //! @brief Perform a non-blocking SPI read.
  2273. //!
  2274. //! @param ui32Module - Module number for the IOM.
  2275. //! @param ui32ChipSelect - Chip select number of the target device.
  2276. //! @param pui32Data - Pointer to the array where received bytes should go.
  2277. //! @param ui32NumBytes - Number of bytes to read.
  2278. //! @param ui32Options - Additional SPI transfer options.
  2279. //! @param pfnCallback - Function to call when the transaction completes.
  2280. //!
  2281. //! This function performs SPI reads to a selected SPI device.
  2282. //!
  2283. //! This function call is a non-blocking implementation. It will start the SPI
  2284. //! transaction on the bus and store a pointer for the destination for the read
  2285. //! data, but it will not wait for the SPI transaction to finish. The caller
  2286. //! will need to make sure that \e am_hal_iom_int_service() is called for IOM
  2287. //! FIFO interrupt events and "command complete" interrupt events. The \e
  2288. //! am_hal_iom_int_service() function will empty the FIFO as necessary,
  2289. //! transfer the data to the \e pui32Data buffer, and call the \e pfnCallback
  2290. //! function when the transaction is finished.
  2291. //!
  2292. //! @note The actual SPI and I2C interfaces operate in BYTES, not 32-bit words.
  2293. //! This function will pack the individual bytes from the physical interface
  2294. //! into 32-bit words, which are then placed into the \e pui32Data array. Only
  2295. //! the first \e ui32NumBytes bytes in this array will contain valid data.
  2296. //!
  2297. //! @return returns AM_HAL_IOM_SUCCESS on successful execution. Note that
  2298. //! successful execution for non-blocking call only means the transaction was
  2299. //! successfully initiated. The status of the transaction is not known till the
  2300. //! callback is called on completion
  2301. //
  2302. //*****************************************************************************
  2303. am_hal_iom_status_e
  2304. am_hal_iom_spi_read_nb(uint32_t ui32Module, uint32_t ui32ChipSelect,
  2305. uint32_t *pui32Data, uint32_t ui32NumBytes,
  2306. uint32_t ui32Options,
  2307. am_hal_iom_callback_t pfnCallback)
  2308. {
  2309. am_hal_iom_status_e ui32Status;
  2310. uint32_t ui32IntConfig;
  2311. uint32_t waitStatus;
  2312. //
  2313. // Validate parameters
  2314. //
  2315. if ( ui32Module >= AM_REG_IOMSTR_NUM_MODULES )
  2316. {
  2317. return AM_HAL_IOM_ERR_INVALID_MODULE;
  2318. }
  2319. //
  2320. // Wait until the bus is idle
  2321. //
  2322. am_hal_iom_poll_complete(ui32Module);
  2323. // Reset the error status
  2324. ui32Status = g_iom_error_status[ui32Module] = AM_HAL_IOM_SUCCESS;
  2325. if (ui32NumBytes == 0)
  2326. {
  2327. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_INVALID_PARAM;
  2328. return ui32Status;
  2329. }
  2330. //
  2331. // Make sure the transfer isn't too long for the hardware to support.
  2332. //
  2333. if (ui32NumBytes >= 4096)
  2334. {
  2335. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_INVALID_PARAM;
  2336. return ui32Status;
  2337. }
  2338. //
  2339. // Need to mark IOM busy to avoid another transaction to be scheduled.
  2340. // This is to take care of a race condition in Queue mode, where the IDLE
  2341. // set is not a guarantee that the CMDCMP has been received
  2342. //
  2343. g_bIomBusy[ui32Module] = true;
  2344. // Clear interrupts
  2345. AM_REGn(IOMSTR, ui32Module, INTCLR) = AM_HAL_IOM_INT_ALL;
  2346. //
  2347. // If we're on a B0 part, and we're using IOM4, our first byte could be
  2348. // corrupted, so we need to send a dummy word with chip-select held high to
  2349. // get that first byte out of the way. This is only true for spi reads with
  2350. // OFFSET values.
  2351. //
  2352. // That operation is tricky and detailed, so we'll call a function to do it
  2353. // for us.
  2354. //
  2355. if ( (WORKAROUND_IOM == ui32Module) && !(ui32Options & AM_HAL_IOM_RAW) &&
  2356. isRevB0() )
  2357. {
  2358. //
  2359. // We might mess up the interrupt handler behavior if we allow this
  2360. // polled transaction to complete with interrupts enabled. We'll
  2361. // briefly turn them off here.
  2362. //
  2363. ui32IntConfig = AM_REGn(IOMSTR, 4, INTEN);
  2364. AM_REGn(IOMSTR, 4, INTEN) = 0;
  2365. am_hal_iom_workaround_word_write(ui32ChipSelect, pui32Data,
  2366. 0, ui32Options | AM_HAL_IOM_CS_LOW);
  2367. //
  2368. // The workaround will send our offset for us, so we can run a RAW
  2369. // command after.
  2370. //
  2371. ui32Options |= AM_HAL_IOM_RAW;
  2372. //
  2373. // Wait for the dummy word to go out over the bus.
  2374. //
  2375. // Make sure the command complete has also been raised
  2376. waitStatus = am_hal_flash_delay_status_change(ui32StatusTimeout[ui32Module],
  2377. AM_REG_IOMSTRn(ui32Module) + AM_REG_IOMSTR_INTSTAT_O,
  2378. AM_REG_IOMSTR_INTEN_CMDCMP_M, AM_REG_IOMSTR_INTEN_CMDCMP_M);
  2379. if (waitStatus != 1)
  2380. {
  2381. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_TIMEOUT;
  2382. return ui32Status;
  2383. }
  2384. //
  2385. // Re-mark IOM as busy
  2386. //
  2387. g_bIomBusy[ui32Module] = true;
  2388. //
  2389. // Re-enable IOM interrupts. Make sure CMDCMP is cleared
  2390. //
  2391. AM_REGn(IOMSTR, ui32Module, INTCLR) = AM_HAL_IOM_INT_ALL;
  2392. AM_REGn(IOMSTR, 4, INTEN) = ui32IntConfig;
  2393. }
  2394. //
  2395. // Prepare the global IOM buffer structure.
  2396. //
  2397. g_psIOMBuffers[ui32Module].ui32State = BUFFER_RECEIVING;
  2398. g_psIOMBuffers[ui32Module].pui32Data = pui32Data;
  2399. g_psIOMBuffers[ui32Module].ui32BytesLeft = ui32NumBytes;
  2400. g_psIOMBuffers[ui32Module].pfnCallback = pfnCallback;
  2401. g_psIOMBuffers[ui32Module].ui32Options = ui32Options;
  2402. //
  2403. // Start the read transaction on the bus.
  2404. //
  2405. am_hal_iom_spi_cmd_run(AM_HAL_IOM_READ, ui32Module, ui32ChipSelect,
  2406. ui32NumBytes, ui32Options);
  2407. return ui32Status;
  2408. }
  2409. static uint32_t
  2410. internal_am_hal_iom_spi_cmd_construct(uint32_t ui32Operation,
  2411. uint32_t ui32ChipSelect,
  2412. uint32_t ui32NumBytes,
  2413. uint32_t ui32Options)
  2414. {
  2415. uint32_t ui32Command;
  2416. //
  2417. // Start building the command from the operation parameter.
  2418. //
  2419. ui32Command = ui32Operation;
  2420. //
  2421. // Set the transfer length (the length field is split, so this requires
  2422. // some swizzling).
  2423. //
  2424. ui32Command |= ((ui32NumBytes & 0xF00) << 15);
  2425. ui32Command |= (ui32NumBytes & 0xFF);
  2426. //
  2427. // Set the chip select number.
  2428. //
  2429. ui32Command |= ((ui32ChipSelect << 16) & 0x00070000);
  2430. //
  2431. // Finally, OR in the rest of the options. This mask should make sure that
  2432. // erroneous option values won't interfere with the other transfer
  2433. // parameters.
  2434. //
  2435. ui32Command |= ui32Options & 0x5C00FF00;
  2436. return ui32Command;
  2437. }
  2438. //*****************************************************************************
  2439. //
  2440. //! @brief Runs a SPI "command" through the IO master.
  2441. //!
  2442. //! @param ui32Operation - SPI action to be performed.
  2443. //!
  2444. //! @param psDevice - Structure containing information about the slave device.
  2445. //!
  2446. //! @param ui32NumBytes - Number of bytes to move (transmit or receive) with
  2447. //! this command.
  2448. //!
  2449. //! @param ui32Options - Additional SPI options to apply to this command.
  2450. //!
  2451. //! @return None.
  2452. //
  2453. //*****************************************************************************
  2454. void
  2455. am_hal_iom_spi_cmd_run(uint32_t ui32Operation, uint32_t ui32Module,
  2456. uint32_t ui32ChipSelect, uint32_t ui32NumBytes,
  2457. uint32_t ui32Options)
  2458. {
  2459. uint32_t ui32Command;
  2460. am_hal_debug_assert_msg(ui32NumBytes > 0,
  2461. "Trying to do a 0 byte transaction");
  2462. ui32Command = internal_am_hal_iom_spi_cmd_construct(ui32Operation,
  2463. ui32ChipSelect, ui32NumBytes, ui32Options);
  2464. //
  2465. // Write the complete command word to the IOM command register.
  2466. //
  2467. AM_REGn(IOMSTR, ui32Module, CMD) = ui32Command;
  2468. }
  2469. //*****************************************************************************
  2470. //
  2471. //! @brief Perform a simple write to the I2C interface (without queuing)
  2472. //!
  2473. //! @param ui32Module - Module number for the IOM.
  2474. //! @param ui32BusAddress - I2C address of the target device.
  2475. //! @param pui32Data - Pointer to the bytes that will be sent.
  2476. //! @param ui32NumBytes - Number of bytes to send.
  2477. //! @param ui32Options - Additional I2C transfer options.
  2478. //!
  2479. //! This function performs I2C writes to a selected I2C device.
  2480. //!
  2481. //! This function call is a blocking implementation. It will write as much
  2482. //! data to the FIFO as possible immediately, and then refill the FIFO as data
  2483. //! is transmiitted.
  2484. //!
  2485. //! @note The actual SPI and I2C interfaces operate in BYTES, not 32-bit words
  2486. //! This means that you will need to byte-pack the \e pui32Data array with the
  2487. //! data you intend to send over the interface. One easy way to do this is to
  2488. //! declare the array as a 32-bit integer array, but use an 8-bit pointer to
  2489. //! put your actual data into the array. If there are not enough bytes in your
  2490. //! desired message to completely fill the last 32-bit word, you may pad that
  2491. //! last word with bytes of any value. The IOM hardware will only read the
  2492. //! first \e ui32NumBytes in the \e pui32Data array.
  2493. //!
  2494. //! @return returns AM_HAL_IOM_SUCCESS on successful execution.
  2495. //
  2496. //*****************************************************************************
  2497. am_hal_iom_status_e
  2498. am_hal_iom_i2c_write_nq(uint32_t ui32Module, uint32_t ui32BusAddress,
  2499. uint32_t *pui32Data, uint32_t ui32NumBytes,
  2500. uint32_t ui32Options)
  2501. {
  2502. uint32_t ui32TransferSize;
  2503. uint32_t ui32SpaceInFifo;
  2504. uint32_t ui32IntConfig;
  2505. uint32_t ui32MaxFifoSize;
  2506. am_hal_iom_status_e ui32Status;
  2507. uint32_t waitStatus;
  2508. am_hal_i2c_bit_bang_enum_e i2cBBStatus;
  2509. //
  2510. // Validate parameters
  2511. //
  2512. if ( ui32Module > AM_REG_IOMSTR_NUM_MODULES )
  2513. {
  2514. return AM_HAL_IOM_ERR_INVALID_MODULE;
  2515. }
  2516. //
  2517. // Wait until any earlier transactions have completed.
  2518. //
  2519. am_hal_iom_poll_complete(ui32Module);
  2520. // Reset the error status
  2521. ui32Status = g_iom_error_status[ui32Module] = AM_HAL_IOM_SUCCESS;
  2522. if (ui32NumBytes == 0)
  2523. {
  2524. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_INVALID_PARAM;
  2525. return ui32Status;
  2526. }
  2527. //
  2528. // Redirect to the bit-bang interface if the module number matches the
  2529. // software I2C module.
  2530. //
  2531. if ( ui32Module == AM_HAL_IOM_I2CBB_MODULE )
  2532. {
  2533. if ( ui32Options & AM_HAL_IOM_RAW )
  2534. {
  2535. i2cBBStatus = am_hal_i2c_bit_bang_send(ui32BusAddress << 1, ui32NumBytes,
  2536. (uint8_t *)pui32Data, 0, false,
  2537. (ui32Options & AM_HAL_IOM_NO_STOP));
  2538. }
  2539. else
  2540. {
  2541. i2cBBStatus = am_hal_i2c_bit_bang_send(ui32BusAddress << 1, ui32NumBytes,
  2542. (uint8_t *)pui32Data,
  2543. ((ui32Options & 0xFF00) >> 8),
  2544. true,
  2545. (ui32Options & AM_HAL_IOM_NO_STOP));
  2546. }
  2547. //
  2548. // Return. convert BB retCode to proper retCode here
  2549. //
  2550. g_iom_error_status[ui32Module] = ui32Status = i2c_bb_errmap[i2cBBStatus];
  2551. return ui32Status;
  2552. }
  2553. //
  2554. // Make sure the transfer isn't too long for the hardware to support.
  2555. //
  2556. if (ui32NumBytes >= 256)
  2557. {
  2558. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_INVALID_PARAM;
  2559. return ui32Status;
  2560. }
  2561. ui32MaxFifoSize = ((0 == AM_BFRn(IOMSTR, ui32Module, CFG, FULLDUP)) ?
  2562. AM_HAL_IOM_MAX_FIFO_SIZE : AM_HAL_IOM_MAX_FIFO_SIZE / 2);
  2563. //
  2564. // Disable interrupts so that we don't get any undesired interrupts.
  2565. //
  2566. ui32IntConfig = AM_REGn(IOMSTR, ui32Module, INTEN);
  2567. AM_REGn(IOMSTR, ui32Module, INTEN) = 0;
  2568. // Clear interrupts
  2569. AM_REGn(IOMSTR, ui32Module, INTCLR) = AM_HAL_IOM_INT_ALL;
  2570. //
  2571. // Figure out how many bytes we can write to the FIFO immediately.
  2572. //
  2573. ui32TransferSize = (ui32NumBytes <= ui32MaxFifoSize ? ui32NumBytes :
  2574. ui32MaxFifoSize);
  2575. am_hal_iom_fifo_write(ui32Module, pui32Data, ui32TransferSize);
  2576. //
  2577. // Start the write on the bus.
  2578. //
  2579. ui32Status = am_hal_iom_i2c_cmd_run(AM_HAL_IOM_WRITE, ui32Module, ui32BusAddress,
  2580. ui32NumBytes, ui32Options);
  2581. if (ui32Status != AM_HAL_IOM_SUCCESS)
  2582. {
  2583. g_iom_error_status[ui32Module] = ui32Status = ui32Status;
  2584. //
  2585. // Re-enable IOM interrupts.
  2586. //
  2587. // Clear interrupts
  2588. AM_REGn(IOMSTR, ui32Module, INTCLR) = AM_HAL_IOM_INT_ALL;
  2589. AM_REGn(IOMSTR, ui32Module, INTEN) = ui32IntConfig;
  2590. return ui32Status;
  2591. }
  2592. //
  2593. // Update the pointer and data counter.
  2594. //
  2595. ui32NumBytes -= ui32TransferSize;
  2596. pui32Data += ui32TransferSize >> 2;
  2597. //
  2598. // Keep looping until we're out of bytes to send or command complete (error).
  2599. //
  2600. while ( ui32NumBytes && !AM_BFRn(IOMSTR, ui32Module, INTSTAT, CMDCMP) )
  2601. {
  2602. //
  2603. // This will always return a multiple of four.
  2604. //
  2605. ui32SpaceInFifo = am_hal_iom_fifo_empty_slots(ui32Module);
  2606. if ( ui32NumBytes <= ui32SpaceInFifo )
  2607. {
  2608. //
  2609. // If the entire message will fit in the fifo, prepare to copy
  2610. // everything.
  2611. //
  2612. ui32TransferSize = ui32NumBytes;
  2613. }
  2614. else
  2615. {
  2616. //
  2617. // If only a portion of the message will fit in the fifo, prepare
  2618. // to copy the largest number of 4-byte blocks possible.
  2619. //
  2620. ui32TransferSize = ui32SpaceInFifo;
  2621. }
  2622. //
  2623. // Write this chunk to the fifo.
  2624. //
  2625. am_hal_iom_fifo_write(ui32Module, pui32Data, ui32TransferSize);
  2626. //
  2627. // Update the data pointer and bytes-left count.
  2628. //
  2629. ui32NumBytes -= ui32TransferSize;
  2630. pui32Data += ui32TransferSize >> 2;
  2631. }
  2632. //
  2633. // Make sure CMDCMP was raised,
  2634. //
  2635. waitStatus = am_hal_flash_delay_status_change(ui32StatusTimeout[ui32Module],
  2636. AM_REG_IOMSTRn(ui32Module) + AM_REG_IOMSTR_INTSTAT_O,
  2637. AM_REG_IOMSTR_INTEN_CMDCMP_M, AM_REG_IOMSTR_INTEN_CMDCMP_M);
  2638. if (waitStatus != 1)
  2639. {
  2640. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_TIMEOUT;
  2641. }
  2642. else
  2643. {
  2644. g_iom_error_status[ui32Module] = ui32Status = internal_iom_get_int_err(ui32Module, 0);
  2645. }
  2646. //
  2647. // Re-enable IOM interrupts.
  2648. //
  2649. // Clear interrupts
  2650. AM_REGn(IOMSTR, ui32Module, INTCLR) = AM_HAL_IOM_INT_ALL;
  2651. AM_REGn(IOMSTR, ui32Module, INTEN) = ui32IntConfig;
  2652. return ui32Status;
  2653. }
  2654. //*****************************************************************************
  2655. //
  2656. //! @brief Perform simple I2C read operations (without queuing).
  2657. //!
  2658. //! @param ui32Module - Module number for the IOM.
  2659. //! @param ui32BusAddress - I2C address of the target device.
  2660. //! @param pui32Data - Pointer to the array where received bytes should go.
  2661. //! @param ui32NumBytes - Number of bytes to read.
  2662. //! @param ui32Options - Additional I2C transfer options.
  2663. //!
  2664. //! This function performs an I2C read to a selected I2C device.
  2665. //!
  2666. //! This function call is a blocking implementation. It will read as much
  2667. //! data from the FIFO as possible immediately, and then re-read the FIFO as more
  2668. //! data is available.
  2669. //!
  2670. //! @note The actual SPI and I2C interfaces operate in BYTES, not 32-bit words.
  2671. //! This function will pack the individual bytes from the physical interface
  2672. //! into 32-bit words, which are then placed into the \e pui32Data array. Only
  2673. //! the first \e ui32NumBytes bytes in this array will contain valid data.
  2674. //!
  2675. //! @return returns AM_HAL_IOM_SUCCESS on successful execution.
  2676. //
  2677. //*****************************************************************************
  2678. am_hal_iom_status_e
  2679. am_hal_iom_i2c_read_nq(uint32_t ui32Module, uint32_t ui32BusAddress,
  2680. uint32_t *pui32Data, uint32_t ui32NumBytes,
  2681. uint32_t ui32Options)
  2682. {
  2683. uint32_t ui32BytesInFifo;
  2684. uint32_t ui32IntConfig;
  2685. uint32_t bCmdCmp = false;
  2686. am_hal_iom_status_e ui32Status;
  2687. uint32_t waitStatus;
  2688. am_hal_i2c_bit_bang_enum_e i2cBBStatus;
  2689. //
  2690. // Validate parameters
  2691. //
  2692. if ( ui32Module > AM_REG_IOMSTR_NUM_MODULES )
  2693. {
  2694. return AM_HAL_IOM_ERR_INVALID_MODULE;
  2695. }
  2696. //
  2697. // Wait until the bus is idle
  2698. //
  2699. am_hal_iom_poll_complete(ui32Module);
  2700. // Reset the error status
  2701. ui32Status = g_iom_error_status[ui32Module] = AM_HAL_IOM_SUCCESS;
  2702. if (ui32NumBytes == 0)
  2703. {
  2704. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_INVALID_PARAM;
  2705. return ui32Status;
  2706. }
  2707. //
  2708. // Redirect to the bit-bang interface if the module number matches the
  2709. // software I2C module.
  2710. //
  2711. if ( ui32Module == AM_HAL_IOM_I2CBB_MODULE )
  2712. {
  2713. if ( ui32Options & AM_HAL_IOM_RAW )
  2714. {
  2715. i2cBBStatus = am_hal_i2c_bit_bang_receive((ui32BusAddress << 1) | 1, ui32NumBytes,
  2716. (uint8_t *)pui32Data, 0, false,
  2717. (ui32Options & AM_HAL_IOM_NO_STOP));
  2718. }
  2719. else
  2720. {
  2721. i2cBBStatus = am_hal_i2c_bit_bang_receive((ui32BusAddress << 1) | 1, ui32NumBytes,
  2722. (uint8_t *)pui32Data,
  2723. ((ui32Options & 0xFF00) >> 8),
  2724. true,
  2725. (ui32Options & AM_HAL_IOM_NO_STOP));
  2726. }
  2727. //
  2728. // Return. convert i2c bb retCode
  2729. //
  2730. g_iom_error_status[ui32Module] = ui32Status = i2c_bb_errmap[i2cBBStatus];
  2731. return ui32Status;
  2732. }
  2733. //
  2734. // Make sure the transfer isn't too long for the hardware to support.
  2735. //
  2736. if (ui32NumBytes >= 256)
  2737. {
  2738. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_INVALID_PARAM;
  2739. return ui32Status;
  2740. }
  2741. //
  2742. // Disable interrupts so that we don't get any undesired interrupts.
  2743. //
  2744. ui32IntConfig = AM_REGn(IOMSTR, ui32Module, INTEN);
  2745. AM_REGn(IOMSTR, ui32Module, INTEN) = 0;
  2746. // Clear interrupts
  2747. AM_REGn(IOMSTR, ui32Module, INTCLR) = AM_HAL_IOM_INT_ALL;
  2748. ui32Status = am_hal_iom_i2c_cmd_run(AM_HAL_IOM_READ, ui32Module, ui32BusAddress,
  2749. ui32NumBytes, ui32Options);
  2750. if (ui32Status != AM_HAL_IOM_SUCCESS)
  2751. {
  2752. g_iom_error_status[ui32Module] = ui32Status = ui32Status;
  2753. //
  2754. // Re-enable IOM interrupts.
  2755. //
  2756. // Clear interrupts
  2757. AM_REGn(IOMSTR, ui32Module, INTCLR) = AM_HAL_IOM_INT_ALL;
  2758. AM_REGn(IOMSTR, ui32Module, INTEN) = ui32IntConfig;
  2759. return ui32Status;
  2760. }
  2761. //
  2762. // Start a loop to catch the Rx data.
  2763. //
  2764. while ( ui32NumBytes )
  2765. {
  2766. ui32BytesInFifo = am_hal_iom_fifo_full_slots(ui32Module);
  2767. if ( ui32BytesInFifo >= ui32NumBytes )
  2768. {
  2769. //
  2770. // If the fifo contains our entire message, just copy the whole
  2771. // thing out.
  2772. //
  2773. am_hal_iom_fifo_read(ui32Module, pui32Data, ui32NumBytes);
  2774. ui32NumBytes = 0;
  2775. }
  2776. else if ( ui32BytesInFifo >= 4 )
  2777. {
  2778. //
  2779. // If the fifo has at least one 32-bit word in it, copy whole
  2780. // words out.
  2781. //
  2782. am_hal_iom_fifo_read(ui32Module, pui32Data, ui32BytesInFifo & ~0x3);
  2783. ui32NumBytes -= ui32BytesInFifo & ~0x3;
  2784. pui32Data += ui32BytesInFifo >> 2;
  2785. }
  2786. if ( bCmdCmp == true )
  2787. {
  2788. // No more data expected - exit out of loop
  2789. break;
  2790. }
  2791. bCmdCmp = AM_BFRn(IOMSTR, ui32Module, INTSTAT, CMDCMP);
  2792. }
  2793. //
  2794. // Make sure CMDCMP was raised,
  2795. //
  2796. waitStatus = am_hal_flash_delay_status_change(ui32StatusTimeout[ui32Module],
  2797. AM_REG_IOMSTRn(ui32Module) + AM_REG_IOMSTR_INTSTAT_O,
  2798. AM_REG_IOMSTR_INTEN_CMDCMP_M, AM_REG_IOMSTR_INTEN_CMDCMP_M);
  2799. if (waitStatus != 1)
  2800. {
  2801. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_TIMEOUT;
  2802. }
  2803. else
  2804. {
  2805. g_iom_error_status[ui32Module] = ui32Status = internal_iom_get_int_err(ui32Module, 0);
  2806. }
  2807. //
  2808. // Re-enable IOM interrupts.
  2809. //
  2810. // Clear interrupts
  2811. AM_REGn(IOMSTR, ui32Module, INTCLR) = AM_HAL_IOM_INT_ALL;
  2812. AM_REGn(IOMSTR, ui32Module, INTEN) = ui32IntConfig;
  2813. return ui32Status;
  2814. }
  2815. //*****************************************************************************
  2816. //
  2817. //! @brief Perform a simple write to the I2C interface.
  2818. //!
  2819. //! @param ui32Module - Module number for the IOM
  2820. //! @param ui32BusAddress - I2C bus address for this transaction.
  2821. //! @param pui32Data - Pointer to the bytes that will be sent.
  2822. //! @param ui32NumBytes - Number of bytes to send.
  2823. //! @param ui32Options - Additional options
  2824. //!
  2825. //! Performs a write to the I2C interface using the provided parameters.
  2826. //!
  2827. //! See the "Command Options" section for parameters that may be ORed together
  2828. //! and used in the \b ui32Options parameter.
  2829. //!
  2830. //! @return returns AM_HAL_IOM_SUCCESS on successful execution.
  2831. //
  2832. //*****************************************************************************
  2833. am_hal_iom_status_e
  2834. am_hal_iom_i2c_write(uint32_t ui32Module, uint32_t ui32BusAddress,
  2835. uint32_t *pui32Data, uint32_t ui32NumBytes,
  2836. uint32_t ui32Options)
  2837. {
  2838. am_hal_iom_status_e ui32Status;
  2839. am_hal_i2c_bit_bang_enum_e i2cBBStatus;
  2840. //
  2841. // Validate parameters
  2842. //
  2843. if ( ui32Module > AM_REG_IOMSTR_NUM_MODULES )
  2844. {
  2845. return AM_HAL_IOM_ERR_INVALID_MODULE;
  2846. }
  2847. //
  2848. // Wait until the bus is idle
  2849. //
  2850. am_hal_iom_poll_complete(ui32Module);
  2851. // Reset the error status
  2852. ui32Status = g_iom_error_status[ui32Module] = AM_HAL_IOM_SUCCESS;
  2853. if (ui32NumBytes == 0)
  2854. {
  2855. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_INVALID_PARAM;
  2856. return ui32Status;
  2857. }
  2858. //
  2859. // Redirect to the bit-bang interface if the module number matches the
  2860. // software I2C module.
  2861. //
  2862. if ( ui32Module == AM_HAL_IOM_I2CBB_MODULE )
  2863. {
  2864. if ( ui32Options & AM_HAL_IOM_RAW )
  2865. {
  2866. i2cBBStatus = am_hal_i2c_bit_bang_send(ui32BusAddress << 1, ui32NumBytes,
  2867. (uint8_t *)pui32Data, 0, false,
  2868. (ui32Options & AM_HAL_IOM_NO_STOP));
  2869. }
  2870. else
  2871. {
  2872. i2cBBStatus = am_hal_i2c_bit_bang_send(ui32BusAddress << 1, ui32NumBytes,
  2873. (uint8_t *)pui32Data,
  2874. ((ui32Options & 0xFF00) >> 8),
  2875. true,
  2876. (ui32Options & AM_HAL_IOM_NO_STOP));
  2877. }
  2878. //
  2879. // Return. convert i2c bb retCode
  2880. //
  2881. g_iom_error_status[ui32Module] = ui32Status = i2c_bb_errmap[i2cBBStatus];
  2882. return ui32Status;
  2883. }
  2884. //
  2885. // Make sure the transfer isn't too long for the hardware to support.
  2886. //
  2887. if (ui32NumBytes >= 256)
  2888. {
  2889. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_INVALID_PARAM;
  2890. return ui32Status;
  2891. }
  2892. //
  2893. // Check to see if queues have been enabled. If they are, we'll actually
  2894. // switch to the queued interface.
  2895. //
  2896. if ( g_psIOMQueue[ui32Module].pui8Data != NULL )
  2897. {
  2898. //
  2899. // If the queue is on, go ahead and add this transaction to the queue.
  2900. //
  2901. ui32Status = am_hal_iom_queue_i2c_write(ui32Module, ui32BusAddress, pui32Data,
  2902. ui32NumBytes, ui32Options, 0);
  2903. if (ui32Status == AM_HAL_IOM_SUCCESS)
  2904. {
  2905. //
  2906. // Wait until the transaction actually clears.
  2907. //
  2908. am_hal_iom_queue_flush(ui32Module);
  2909. // g_iom_error_status gets set in the isr handling
  2910. ui32Status = g_iom_error_status[ui32Module];
  2911. }
  2912. //
  2913. // At this point, we've completed the transaction, and we can return.
  2914. //
  2915. }
  2916. else
  2917. {
  2918. //
  2919. // Otherwise, we'll just do a polled transaction.
  2920. //
  2921. ui32Status = am_hal_iom_i2c_write_nq(ui32Module, ui32BusAddress, pui32Data,
  2922. ui32NumBytes, ui32Options);
  2923. }
  2924. return ui32Status;
  2925. }
  2926. //*****************************************************************************
  2927. //
  2928. //! @brief Perform simple I2C read operations.
  2929. //!
  2930. //! @param ui32Module - Module number for the IOM
  2931. //! @param ui32BusAddress - I2C bus address for this transaction.
  2932. //! @param pui32Data - Pointer to the array where received bytes should go.
  2933. //! @param ui32NumBytes - Number of bytes to read.
  2934. //! @param ui32Options - Additional I2C transfer options.
  2935. //!
  2936. //! This function performs simple I2C read operations. The caller is
  2937. //! responsible for ensuring that the receive buffer is large enough to hold
  2938. //! the requested amount of data. If \e bPolled is true, this function will
  2939. //! block until all of the requested data has been received and placed in the
  2940. //! user-supplied buffer. Otherwise, the function will execute the I2C read
  2941. //! command and return immediately. The user-supplied buffer will be filled
  2942. //! with the received I2C data as it comes in over the physical interface, and
  2943. //! the "command complete" interrupt bit will become active once the entire
  2944. //! message is available.
  2945. //!
  2946. //! See the "Command Options" section for parameters that may be ORed together
  2947. //! and used in the \b ui32Options parameter.
  2948. //!
  2949. //! @note The actual SPI and I2C interfaces operate in BYTES, not 32-bit words.
  2950. //! This function will pack the individual bytes from the physical interface
  2951. //! into 32-bit words, which are then placed into the \e pui32Data array. Only
  2952. //! the first \e ui32NumBytes bytes in this array will contain valid data.
  2953. //!
  2954. //! @return returns AM_HAL_IOM_SUCCESS on successful execution.
  2955. //
  2956. //*****************************************************************************
  2957. am_hal_iom_status_e
  2958. am_hal_iom_i2c_read(uint32_t ui32Module, uint32_t ui32BusAddress,
  2959. uint32_t *pui32Data, uint32_t ui32NumBytes,
  2960. uint32_t ui32Options)
  2961. {
  2962. am_hal_iom_status_e ui32Status;
  2963. am_hal_i2c_bit_bang_enum_e i2cBBStatus;
  2964. //
  2965. // Validate parameters
  2966. //
  2967. if ( ui32Module > AM_REG_IOMSTR_NUM_MODULES )
  2968. {
  2969. return AM_HAL_IOM_ERR_INVALID_MODULE;
  2970. }
  2971. //
  2972. // Wait until the bus is idle
  2973. //
  2974. am_hal_iom_poll_complete(ui32Module);
  2975. // Reset the error status
  2976. ui32Status = g_iom_error_status[ui32Module] = AM_HAL_IOM_SUCCESS;
  2977. if (ui32NumBytes == 0)
  2978. {
  2979. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_INVALID_PARAM;
  2980. return ui32Status;
  2981. }
  2982. //
  2983. // Redirect to the bit-bang interface if the module number matches the
  2984. // software I2C module.
  2985. //
  2986. if ( ui32Module == AM_HAL_IOM_I2CBB_MODULE )
  2987. {
  2988. if ( ui32Options & AM_HAL_IOM_RAW )
  2989. {
  2990. i2cBBStatus = am_hal_i2c_bit_bang_receive((ui32BusAddress << 1) | 1, ui32NumBytes,
  2991. (uint8_t *)pui32Data, 0, false,
  2992. (ui32Options & AM_HAL_IOM_NO_STOP));
  2993. }
  2994. else
  2995. {
  2996. i2cBBStatus = am_hal_i2c_bit_bang_receive((ui32BusAddress << 1) | 1, ui32NumBytes,
  2997. (uint8_t *)pui32Data,
  2998. ((ui32Options & 0xFF00) >> 8),
  2999. true,
  3000. (ui32Options & AM_HAL_IOM_NO_STOP));
  3001. }
  3002. //
  3003. // Return. convert i2c bb retCode
  3004. //
  3005. g_iom_error_status[ui32Module] = ui32Status = i2c_bb_errmap[i2cBBStatus];
  3006. return ui32Status;
  3007. }
  3008. //
  3009. // Make sure the transfer isn't too long for the hardware to support.
  3010. //
  3011. if (ui32NumBytes >= 256)
  3012. {
  3013. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_INVALID_PARAM;
  3014. return ui32Status;
  3015. }
  3016. //
  3017. // Check to see if queues have been enabled. If they are, we'll actually
  3018. // switch to the queued interface.
  3019. //
  3020. if ( g_psIOMQueue[ui32Module].pui8Data != NULL )
  3021. {
  3022. //
  3023. // If the queue is on, go ahead and add this transaction to the queue.
  3024. //
  3025. ui32Status = am_hal_iom_queue_i2c_read(ui32Module, ui32BusAddress, pui32Data,
  3026. ui32NumBytes, ui32Options, 0);
  3027. if (ui32Status == AM_HAL_IOM_SUCCESS)
  3028. {
  3029. //
  3030. // Wait until the transaction actually clears.
  3031. //
  3032. am_hal_iom_queue_flush(ui32Module);
  3033. // g_iom_error_status gets set in the isr handling
  3034. ui32Status = g_iom_error_status[ui32Module];
  3035. }
  3036. //
  3037. // At this point, we've completed the transaction, and we can return.
  3038. //
  3039. }
  3040. else
  3041. {
  3042. //
  3043. // Otherwise, just perform a polled transaction.
  3044. //
  3045. ui32Status = am_hal_iom_i2c_read_nq(ui32Module, ui32BusAddress, pui32Data,
  3046. ui32NumBytes, ui32Options);
  3047. }
  3048. return ui32Status;
  3049. }
  3050. //*****************************************************************************
  3051. //
  3052. //! @brief Perform a non-blocking write to the I2C interface.
  3053. //!
  3054. //! @param ui32Module - Module number for the IOM.
  3055. //! @param ui32BusAddress - I2C address of the target device.
  3056. //! @param pui32Data - Pointer to the bytes that will be sent.
  3057. //! @param ui32NumBytes - Number of bytes to send.
  3058. //! @param ui32Options - Additional I2C transfer options.
  3059. //! @param pfnCallback - Function to call when the transaction completes.
  3060. //!
  3061. //! This function performs I2C writes to a selected I2C device.
  3062. //!
  3063. //! This function call is a non-blocking implementation. It will write as much
  3064. //! data to the FIFO as possible immediately, store a pointer to the remaining
  3065. //! data, start the transfer on the bus, and then immediately return. The
  3066. //! caller will need to make sure that \e am_hal_iom_int_service() is called
  3067. //! for IOM FIFO interrupt events and "command complete" interrupt events. The
  3068. //! \e am_hal_iom_int_service() function will refill the FIFO as necessary and
  3069. //! call the \e pfnCallback function when the transaction is finished.
  3070. //!
  3071. //! @note The actual SPI and I2C interfaces operate in BYTES, not 32-bit words.
  3072. //! This means that you will need to byte-pack the \e pui32Data array with the
  3073. //! data you intend to send over the interface. One easy way to do this is to
  3074. //! declare the array as a 32-bit integer array, but use an 8-bit pointer to
  3075. //! put your actual data into the array. If there are not enough bytes in your
  3076. //! desired message to completely fill the last 32-bit word, you may pad that
  3077. //! last word with bytes of any value. The IOM hardware will only read the
  3078. //! first \e ui32NumBytes in the \e pui32Data array.
  3079. //!
  3080. //! @return returns AM_HAL_IOM_SUCCESS on successful execution. Note that
  3081. //! successful execution for non-blocking call only means the transaction was
  3082. //! successfully initiated. The status of the transaction is not known till the
  3083. //! callback is called on completion
  3084. //
  3085. //*****************************************************************************
  3086. am_hal_iom_status_e
  3087. am_hal_iom_i2c_write_nb(uint32_t ui32Module, uint32_t ui32BusAddress,
  3088. uint32_t *pui32Data, uint32_t ui32NumBytes,
  3089. uint32_t ui32Options,
  3090. am_hal_iom_callback_t pfnCallback)
  3091. {
  3092. am_hal_iom_status_e ui32Status;
  3093. uint32_t ui32TransferSize;
  3094. uint32_t ui32MaxFifoSize;
  3095. am_hal_i2c_bit_bang_enum_e i2cBBStatus;
  3096. //
  3097. // Validate parameters
  3098. //
  3099. if ( ui32Module > AM_REG_IOMSTR_NUM_MODULES )
  3100. {
  3101. return AM_HAL_IOM_ERR_INVALID_MODULE;
  3102. }
  3103. //
  3104. // Wait until the bus is idle
  3105. //
  3106. am_hal_iom_poll_complete(ui32Module);
  3107. // Reset the error status
  3108. ui32Status = g_iom_error_status[ui32Module] = AM_HAL_IOM_SUCCESS;
  3109. if (ui32NumBytes == 0)
  3110. {
  3111. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_INVALID_PARAM;
  3112. return ui32Status;
  3113. }
  3114. //
  3115. // Redirect to the bit-bang interface if the module number matches the
  3116. // software I2C module.
  3117. //
  3118. if ( ui32Module == AM_HAL_IOM_I2CBB_MODULE )
  3119. {
  3120. // Reset the error status for non-blocking transfer
  3121. g_iom_error_status[ui32Module] = AM_HAL_IOM_SUCCESS;
  3122. if ( ui32Options & AM_HAL_IOM_RAW )
  3123. {
  3124. i2cBBStatus = am_hal_i2c_bit_bang_send(ui32BusAddress << 1, ui32NumBytes,
  3125. (uint8_t *)pui32Data, 0, false,
  3126. (ui32Options & AM_HAL_IOM_NO_STOP));
  3127. }
  3128. else
  3129. {
  3130. i2cBBStatus = am_hal_i2c_bit_bang_send(ui32BusAddress << 1, ui32NumBytes,
  3131. (uint8_t *)pui32Data,
  3132. ((ui32Options & 0xFF00) >> 8),
  3133. true,
  3134. (ui32Options & AM_HAL_IOM_NO_STOP));
  3135. }
  3136. //
  3137. // Return. convert i2c bb retCode
  3138. //
  3139. g_iom_error_status[ui32Module] = ui32Status = i2c_bb_errmap[i2cBBStatus];
  3140. //
  3141. // The I2C bit-bang interface is actually a blocking transfer, and it
  3142. // doesn't trigger the interrupt handler, so we have to call the
  3143. // callback function manually.
  3144. //
  3145. if ( pfnCallback )
  3146. {
  3147. pfnCallback();
  3148. }
  3149. return ui32Status;
  3150. }
  3151. //
  3152. // Make sure the transfer isn't too long for the hardware to support.
  3153. //
  3154. if (ui32NumBytes >= 256)
  3155. {
  3156. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_INVALID_PARAM;
  3157. return ui32Status;
  3158. }
  3159. ui32MaxFifoSize = ((0 == AM_BFRn(IOMSTR, ui32Module, CFG, FULLDUP)) ?
  3160. AM_HAL_IOM_MAX_FIFO_SIZE : AM_HAL_IOM_MAX_FIFO_SIZE / 2);
  3161. //
  3162. // Figure out how many bytes we can write to the FIFO immediately.
  3163. //
  3164. ui32TransferSize = (ui32NumBytes <= ui32MaxFifoSize ? ui32NumBytes :
  3165. ui32MaxFifoSize);
  3166. // Need to mark IOM busy to avoid another transaction to be scheduled.
  3167. // This is to take care of a race condition in Queue mode, where the IDLE
  3168. // set is not a guarantee that the CMDCMP has been received
  3169. g_bIomBusy[ui32Module] = true;
  3170. // Clear interrupts
  3171. AM_REGn(IOMSTR, ui32Module, INTCLR) = AM_HAL_IOM_INT_ALL;
  3172. if ( am_hal_iom_fifo_write(ui32Module, pui32Data, ui32TransferSize) > 0 )
  3173. {
  3174. //
  3175. // Prepare the global IOM buffer structure.
  3176. //
  3177. g_psIOMBuffers[ui32Module].ui32State = BUFFER_SENDING;
  3178. g_psIOMBuffers[ui32Module].pui32Data = pui32Data;
  3179. g_psIOMBuffers[ui32Module].ui32BytesLeft = ui32NumBytes;
  3180. g_psIOMBuffers[ui32Module].pfnCallback = pfnCallback;
  3181. //
  3182. // Update the pointer and the byte counter based on the portion of the
  3183. // transfer we just sent to the fifo.
  3184. //
  3185. g_psIOMBuffers[ui32Module].ui32BytesLeft -= ui32TransferSize;
  3186. g_psIOMBuffers[ui32Module].pui32Data += (ui32TransferSize / 4);
  3187. //
  3188. // Start the write on the bus.
  3189. //
  3190. ui32Status = am_hal_iom_i2c_cmd_run(AM_HAL_IOM_WRITE, ui32Module, ui32BusAddress,
  3191. ui32NumBytes, ui32Options);
  3192. if (ui32Status != AM_HAL_IOM_SUCCESS)
  3193. {
  3194. g_iom_error_status[ui32Module] = ui32Status;
  3195. }
  3196. }
  3197. return ui32Status;
  3198. }
  3199. //*****************************************************************************
  3200. //
  3201. //! @brief Perform a non-blocking I2C read.
  3202. //!
  3203. //! @param ui32Module - Module number for the IOM.
  3204. //! @param ui32ChipSelect - I2C address of the target device.
  3205. //! @param pui32Data - Pointer to the array where received bytes should go.
  3206. //! @param ui32NumBytes - Number of bytes to read.
  3207. //! @param ui32Options - Additional I2C transfer options.
  3208. //! @param pfnCallback - Function to call when the transaction completes.
  3209. //!
  3210. //! This function performs an I2C read to a selected I2C device.
  3211. //!
  3212. //! This function call is a non-blocking implementation. It will start the I2C
  3213. //! transaction on the bus and store a pointer for the destination for the read
  3214. //! data, but it will not wait for the I2C transaction to finish. The caller
  3215. //! will need to make sure that \e am_hal_iom_int_service() is called for IOM
  3216. //! FIFO interrupt events and "command complete" interrupt events. The \e
  3217. //! am_hal_iom_int_service() function will empty the FIFO as necessary,
  3218. //! transfer the data to the \e pui32Data buffer, and call the \e pfnCallback
  3219. //! function when the transaction is finished.
  3220. //!
  3221. //! @note The actual SPI and I2C interfaces operate in BYTES, not 32-bit words.
  3222. //! This function will pack the individual bytes from the physical interface
  3223. //! into 32-bit words, which are then placed into the \e pui32Data array. Only
  3224. //! the first \e ui32NumBytes bytes in this array will contain valid data.
  3225. //!
  3226. //! @return returns AM_HAL_IOM_SUCCESS on successful execution. Note that
  3227. //! successful execution for non-blocking call only means the transaction was
  3228. //! successfully initiated. The status of the transaction is not known till the
  3229. //! callback is called on completion
  3230. //
  3231. //*****************************************************************************
  3232. am_hal_iom_status_e
  3233. am_hal_iom_i2c_read_nb(uint32_t ui32Module, uint32_t ui32BusAddress,
  3234. uint32_t *pui32Data, uint32_t ui32NumBytes,
  3235. uint32_t ui32Options,
  3236. am_hal_iom_callback_t pfnCallback)
  3237. {
  3238. am_hal_iom_status_e ui32Status;
  3239. am_hal_i2c_bit_bang_enum_e i2cBBStatus;
  3240. //
  3241. // Validate parameters
  3242. //
  3243. if ( ui32Module > AM_REG_IOMSTR_NUM_MODULES )
  3244. {
  3245. return AM_HAL_IOM_ERR_INVALID_MODULE;
  3246. }
  3247. //
  3248. // Wait until the bus is idle
  3249. //
  3250. am_hal_iom_poll_complete(ui32Module);
  3251. // Reset the error status
  3252. ui32Status = g_iom_error_status[ui32Module] = AM_HAL_IOM_SUCCESS;
  3253. if (ui32NumBytes == 0)
  3254. {
  3255. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_INVALID_PARAM;
  3256. return ui32Status;
  3257. }
  3258. //
  3259. // Redirect to the bit-bang interface if the module number matches the
  3260. // software I2C module.
  3261. //
  3262. if ( ui32Module == AM_HAL_IOM_I2CBB_MODULE )
  3263. {
  3264. // Reset the error status for non-blocking transfer
  3265. g_iom_error_status[ui32Module] = AM_HAL_IOM_SUCCESS;
  3266. if ( ui32Options & AM_HAL_IOM_RAW )
  3267. {
  3268. i2cBBStatus = am_hal_i2c_bit_bang_receive((ui32BusAddress << 1) | 1, ui32NumBytes,
  3269. (uint8_t *)pui32Data, 0, false,
  3270. (ui32Options & AM_HAL_IOM_NO_STOP));
  3271. }
  3272. else
  3273. {
  3274. i2cBBStatus = am_hal_i2c_bit_bang_receive((ui32BusAddress << 1) | 1, ui32NumBytes,
  3275. (uint8_t *)pui32Data,
  3276. ((ui32Options & 0xFF00) >> 8),
  3277. true,
  3278. (ui32Options & AM_HAL_IOM_NO_STOP));
  3279. }
  3280. //
  3281. // Return. convert i2c bb retCode
  3282. //
  3283. g_iom_error_status[ui32Module] = ui32Status = i2c_bb_errmap[i2cBBStatus];
  3284. //
  3285. // The I2C bit-bang interface is actually a blocking transfer, and it
  3286. // doesn't trigger the interrupt handler, so we have to call the
  3287. // callback function manually.
  3288. //
  3289. if ( pfnCallback )
  3290. {
  3291. pfnCallback();
  3292. }
  3293. return ui32Status;
  3294. }
  3295. //
  3296. // Make sure the transfer isn't too long for the hardware to support.
  3297. //
  3298. if (ui32NumBytes >= 256)
  3299. {
  3300. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_INVALID_PARAM;
  3301. return ui32Status;
  3302. }
  3303. // Need to mark IOM busy to avoid another transaction to be scheduled.
  3304. // This is to take care of a race condition in Queue mode, where the IDLE
  3305. // set is not a guarantee that the CMDCMP has been received
  3306. //
  3307. g_bIomBusy[ui32Module] = true;
  3308. // Clear interrupts
  3309. AM_REGn(IOMSTR, ui32Module, INTCLR) = AM_HAL_IOM_INT_ALL;
  3310. //
  3311. // Prepare the global IOM buffer structure.
  3312. //
  3313. g_psIOMBuffers[ui32Module].ui32State = BUFFER_RECEIVING;
  3314. g_psIOMBuffers[ui32Module].pui32Data = pui32Data;
  3315. g_psIOMBuffers[ui32Module].ui32BytesLeft = ui32NumBytes;
  3316. g_psIOMBuffers[ui32Module].pfnCallback = pfnCallback;
  3317. //
  3318. // Start the read transaction on the bus.
  3319. //
  3320. ui32Status = am_hal_iom_i2c_cmd_run(AM_HAL_IOM_READ, ui32Module, ui32BusAddress,
  3321. ui32NumBytes, ui32Options);
  3322. if (ui32Status != AM_HAL_IOM_SUCCESS)
  3323. {
  3324. g_iom_error_status[ui32Module] = ui32Status;
  3325. }
  3326. return ui32Status;
  3327. }
  3328. //*****************************************************************************
  3329. //
  3330. //! @brief Runs a I2C "command" through the IO master.
  3331. //!
  3332. //! @param ui32Operation - I2C action to be performed. This should either be
  3333. //! AM_HAL_IOM_WRITE or AM_HAL_IOM_READ.
  3334. //! @param psDevice - Structure containing information about the slave device.
  3335. //! @param ui32NumBytes - Number of bytes to move (transmit or receive) with
  3336. //! this command.
  3337. //! @param ui32Options - Additional I2C options to apply to this command.
  3338. //!
  3339. //! This function may be used along with am_hal_iom_fifo_write and
  3340. //! am_hal_iom_fifo_read to perform more complex I2C reads and writes.
  3341. //! This function has additional logic to make sure SCL is high before a new
  3342. //! transaction is initiated.
  3343. //!
  3344. //! @return 0 on success
  3345. //
  3346. //*****************************************************************************
  3347. am_hal_iom_status_e
  3348. am_hal_iom_i2c_cmd_run(uint32_t ui32Operation, uint32_t ui32Module,
  3349. uint32_t ui32BusAddress, uint32_t ui32NumBytes,
  3350. uint32_t ui32Options)
  3351. {
  3352. uint32_t ui32Command;
  3353. am_hal_iom_status_e ui32Status = AM_HAL_IOM_SUCCESS;
  3354. //
  3355. // Validate parameters
  3356. //
  3357. if ( ui32Module >= AM_REG_IOMSTR_NUM_MODULES )
  3358. {
  3359. return AM_HAL_IOM_ERR_INVALID_MODULE;
  3360. }
  3361. if (ui32NumBytes == 0)
  3362. {
  3363. return AM_HAL_IOM_ERR_INVALID_PARAM;
  3364. }
  3365. //
  3366. // Start building the command from the operation parameter.
  3367. //
  3368. ui32Command = ui32Operation;
  3369. //
  3370. // Set the transfer length.
  3371. //
  3372. ui32Command |= (ui32NumBytes & 0xFF);
  3373. //
  3374. // Set the chip select number.
  3375. //
  3376. ui32Command |= ((ui32BusAddress << 16) & 0x03FF0000);
  3377. //
  3378. // Finally, OR in the rest of the options. This mask should make sure that
  3379. // erroneous option values won't interfere with the other transfer
  3380. // parameters.
  3381. //
  3382. ui32Command |= (ui32Options & 0x5C00FF00);
  3383. //
  3384. // Wait for SCL to be high before initiating a new transaction
  3385. // This is to ensure clock hi time specs are not violated in case slave did
  3386. // clock stretching in previous transaction
  3387. //
  3388. ui32Status = internal_iom_wait_i2c_scl_hi(ui32Module);
  3389. if (ui32Status == AM_HAL_IOM_SUCCESS)
  3390. {
  3391. //
  3392. // Write the complete command word to the IOM command register.
  3393. //
  3394. AM_REGn(IOMSTR, ui32Module, CMD) = ui32Command;
  3395. }
  3396. return ui32Status;
  3397. }
  3398. //*****************************************************************************
  3399. //
  3400. //! @brief Sets the repeat count for the next IOM command.
  3401. //!
  3402. //! @param ui32Module is the IOM module number.
  3403. //! @param ui32CmdCount is the number of times the next command should be
  3404. //! executed.
  3405. //!
  3406. //! @note This function is not compatible with the am_hal_iom_spi_read/write()
  3407. //! or am_hal_iom_i2c_read/write() functions. Instead, you will need to use the
  3408. //! am_hal_iom_fifo_read/write() functions and the am_hal_iom_spi/i2c_cmd_run()
  3409. //! functions.
  3410. //!
  3411. //! Example usage:
  3412. //! @code
  3413. //!
  3414. //! //
  3415. //! // Create a buffer and add 3 bytes of data to it.
  3416. //! //
  3417. //! am_hal_iom_buffer(3) psBuffer;
  3418. //! psBuffer.bytes[0] = 's';
  3419. //! psBuffer.bytes[1] = 'p';
  3420. //! psBuffer.bytes[2] = 'i';
  3421. //!
  3422. //! //
  3423. //! // Send three different bytes to the same SPI register on a remote device.
  3424. //! //
  3425. //! am_hal_iom_fifo_write(ui32Module, psBuffer.words, 3);
  3426. //!
  3427. //! am_hal_command_repeat_set(ui32Module, 3);
  3428. //!
  3429. //! am_hal_iom_spi_cmd_run(AM_HAL_IOM_WRITE, psDevice, 1,
  3430. //! AM_HAL_IOM_OFFSET(0x5));
  3431. //!
  3432. //! //
  3433. //! // The sequence "0x5, 's', 0x5, 'p', 0x5, 'i'" should be written to the SPI
  3434. //! // bus.
  3435. //! //
  3436. //!
  3437. //! @endcode
  3438. //!
  3439. //! @return None.
  3440. //
  3441. //*****************************************************************************
  3442. void
  3443. am_hal_iom_command_repeat_set(uint32_t ui32Module, uint32_t ui32CmdCount)
  3444. {
  3445. //
  3446. // Validate parameters
  3447. //
  3448. if ( ui32Module >= AM_REG_IOMSTR_NUM_MODULES )
  3449. {
  3450. return;
  3451. }
  3452. AM_REGn(IOMSTR, ui32Module, CMDRPT) = ui32CmdCount;
  3453. }
  3454. //*****************************************************************************
  3455. //
  3456. //! @brief Writes data to the IOM FIFO.
  3457. //!
  3458. //! @param ui32Module - Selects the IOM module to use (zero or one).
  3459. //! @param pui32Data - Pointer to an array of the data to be written.
  3460. //! @param ui32NumBytes - Number of BYTES to copy into the FIFO.
  3461. //!
  3462. //! This function copies data from the array \e pui32Data into the IOM FIFO.
  3463. //! This prepares the data to eventually be sent as SPI or I2C data by an IOM
  3464. //! "command".
  3465. //!
  3466. //! @note The actual SPI and I2C interfaces operate in BYTES, not 32-bit words.
  3467. //! This means that you will need to byte-pack the \e pui32Data array with the
  3468. //! data you intend to send over the interface. One easy way to do this is to
  3469. //! declare the array as a 32-bit integer array, but use an 8-bit pointer to
  3470. //! put your actual data into the array. If there are not enough bytes in your
  3471. //! desired message to completely fill the last 32-bit word, you may pad that
  3472. //! last word with bytes of any value. The IOM hardware will only read the
  3473. //! first \e ui32NumBytes in the \e pui8Data array.
  3474. //!
  3475. //! @note This function may be used to write partial or complete SPI or I2C
  3476. //! messages into the IOM FIFO. When writing partial messages to the FIFO, make
  3477. //! sure that the number of bytes written is a multiple of four. Only the last
  3478. //! 'part' of a message may consist of a number of bytes that is not a multiple
  3479. //! of four. If this rule is not followed, the IOM will not be able to send
  3480. //! these bytes correctly.
  3481. //!
  3482. //! @return Number of bytes actually written to the FIFO.
  3483. //
  3484. //*****************************************************************************
  3485. uint32_t
  3486. am_hal_iom_fifo_write(uint32_t ui32Module, uint32_t *pui32Data,
  3487. uint32_t ui32NumBytes)
  3488. {
  3489. uint32_t ui32Index;
  3490. //
  3491. // Validate parameters
  3492. //
  3493. if ( ui32Module >= AM_REG_IOMSTR_NUM_MODULES )
  3494. {
  3495. return 0;
  3496. }
  3497. //
  3498. // Make sure we check the number of bytes we're writing to the FIFO.
  3499. //
  3500. am_hal_debug_assert_msg((am_hal_iom_fifo_empty_slots(ui32Module) >= ui32NumBytes),
  3501. "The fifo couldn't fit the requested number of bytes");
  3502. //
  3503. // Loop over the words in the array until we have the correct number of
  3504. // bytes.
  3505. //
  3506. for ( ui32Index = 0; (4 * ui32Index) < ui32NumBytes; ui32Index++ )
  3507. {
  3508. //
  3509. // Write the word to the FIFO.
  3510. //
  3511. AM_REGn(IOMSTR, ui32Module, FIFO) = pui32Data[ui32Index];
  3512. }
  3513. return ui32NumBytes;
  3514. }
  3515. //*****************************************************************************
  3516. //
  3517. //! @brief Reads data from the IOM FIFO.
  3518. //!
  3519. //! @param ui32Module - Selects the IOM module to use (zero or one).
  3520. //! @param pui32Data - Pointer to an array where the FIFO data will be copied.
  3521. //! @param ui32NumBytes - Number of bytes to copy into array.
  3522. //!
  3523. //! This function copies data from the IOM FIFO into the array \e pui32Data.
  3524. //! This is how input data from SPI or I2C transactions may be retrieved.
  3525. //!
  3526. //! @note The actual SPI and I2C interfaces operate in BYTES, not 32-bit words.
  3527. //! This function will pack the individual bytes from the physical interface
  3528. //! into 32-bit words, which are then placed into the \e pui32Data array. Only
  3529. //! the first \e ui32NumBytes bytes in this array will contain valid data.
  3530. //!
  3531. //! @return Number of bytes read from the fifo.
  3532. //
  3533. //*****************************************************************************
  3534. uint32_t
  3535. am_hal_iom_fifo_read(uint32_t ui32Module, uint32_t *pui32Data,
  3536. uint32_t ui32NumBytes)
  3537. {
  3538. am_hal_iom_buffer(4) sTempBuffer;
  3539. uint32_t i, j, ui32NumWords, ui32Leftovers;
  3540. uint8_t *pui8Data;
  3541. //
  3542. // Validate parameters
  3543. //
  3544. if ( ui32Module >= AM_REG_IOMSTR_NUM_MODULES )
  3545. {
  3546. return 0;
  3547. }
  3548. //
  3549. // Make sure we check the number of bytes we're reading from the FIFO.
  3550. // This doesn't make sense for Full-Duplex operations.
  3551. //
  3552. if ( AM_REG_IOMSTR_CFG_FULLDUP_NORMAL == (AM_REGn(IOMSTR, ui32Module, CFG) & AM_REG_IOMSTR_CFG_FULLDUP_M) )
  3553. {
  3554. am_hal_debug_assert_msg((am_hal_iom_fifo_full_slots(ui32Module) >= ui32NumBytes),
  3555. "The fifo doesn't contain the requested number of bytes.");
  3556. }
  3557. //
  3558. // Figure out how many whole words we're reading from the fifo, and how
  3559. // many bytes will be left over when we're done.
  3560. //
  3561. ui32NumWords = ui32NumBytes / 4;
  3562. ui32Leftovers = ui32NumBytes - (ui32NumWords * 4);
  3563. //
  3564. // Copy out as many full words as we can.
  3565. //
  3566. for ( i = 0; i < ui32NumWords; i++ )
  3567. {
  3568. //
  3569. // Copy data out of the FIFO, one word at a time.
  3570. //
  3571. pui32Data[i] = AM_REGn(IOMSTR, ui32Module, FIFO);
  3572. }
  3573. //
  3574. // If there were leftovers, we'll copy them carefully. Pull the last word
  3575. // from the fifo (there should only be one) into a temporary buffer. Also,
  3576. // create an 8-bit pointer to help us copy the remaining bytes one at a
  3577. // time.
  3578. //
  3579. // Note: If the data buffer we were given was truly a word pointer like the
  3580. // definition requests, we wouldn't need to do this. It's possible to call
  3581. // this function with a re-cast or packed pointer instead though. If that
  3582. // happens, we want to be careful not to overwrite any data that might be
  3583. // sitting just past the end of the destination array.
  3584. //
  3585. if ( ui32Leftovers )
  3586. {
  3587. sTempBuffer.words[0] = AM_REGn(IOMSTR, ui32Module, FIFO);
  3588. pui8Data = (uint8_t *) (&pui32Data[i]);
  3589. //
  3590. // If we had leftover bytes, copy them out one byte at a time.
  3591. //
  3592. for ( j = 0; j < ui32Leftovers; j++ )
  3593. {
  3594. pui8Data[j] = sTempBuffer.bytes[j];
  3595. }
  3596. }
  3597. return ui32NumBytes;
  3598. }
  3599. //*****************************************************************************
  3600. //
  3601. //! @brief Check amount of empty space in the IOM fifo.
  3602. //!
  3603. //! @param ui32Module - Module number of the IOM whose fifo should be checked.
  3604. //!
  3605. //! Returns the number of bytes that could be written to the IOM fifo without
  3606. //! causing an overflow.
  3607. //!
  3608. //! @return Amount of space available in the fifo (in bytes).
  3609. //
  3610. //*****************************************************************************
  3611. uint8_t
  3612. am_hal_iom_fifo_empty_slots(uint32_t ui32Module)
  3613. {
  3614. uint32_t ui32MaxFifoSize;
  3615. //
  3616. // Validate parameters
  3617. //
  3618. if ( ui32Module >= AM_REG_IOMSTR_NUM_MODULES )
  3619. {
  3620. return 0;
  3621. }
  3622. ui32MaxFifoSize = ((0 == AM_BFRn(IOMSTR, ui32Module, CFG, FULLDUP)) ? AM_HAL_IOM_MAX_FIFO_SIZE : AM_HAL_IOM_MAX_FIFO_SIZE / 2);
  3623. //
  3624. // Calculate the FIFO Remaining from the FIFO size. This will be different
  3625. // depending on whether the IOM is configured for half-duplex or
  3626. // full-duplex.
  3627. //
  3628. return (ui32MaxFifoSize - AM_BFRn(IOMSTR, ui32Module, FIFOPTR, FIFOSIZ)) & (~0x3);
  3629. }
  3630. //*****************************************************************************
  3631. //
  3632. //! @brief Check to see how much data is in the IOM fifo.
  3633. //!
  3634. //! @param ui32Module - Module number of the IOM whose fifo should be checked.
  3635. //!
  3636. //! Returns the number of bytes of data that are currently in the IOM fifo.
  3637. //!
  3638. //! @return Number of bytes in the fifo.
  3639. //
  3640. //*****************************************************************************
  3641. uint8_t
  3642. am_hal_iom_fifo_full_slots(uint32_t ui32Module)
  3643. {
  3644. //
  3645. // Validate parameters
  3646. //
  3647. if ( ui32Module >= AM_REG_IOMSTR_NUM_MODULES )
  3648. {
  3649. return 0;
  3650. }
  3651. return AM_BFRn(IOMSTR, ui32Module, FIFOPTR, FIFOSIZ);
  3652. }
  3653. //*****************************************************************************
  3654. //
  3655. //! @brief Wait for the current IOM command to complete.
  3656. //!
  3657. //! @param ui32Module - The module number of the IOM to use.
  3658. //!
  3659. //! This function polls until the IOM bus becomes idle.
  3660. //!
  3661. //! @return None.
  3662. //
  3663. //*****************************************************************************
  3664. void
  3665. am_hal_iom_poll_complete(uint32_t ui32Module)
  3666. {
  3667. //
  3668. // Validate parameters
  3669. //
  3670. if ( ui32Module >= AM_REG_IOMSTR_NUM_MODULES )
  3671. {
  3672. return;
  3673. }
  3674. //
  3675. // Poll on the IDLE bit in the status register.
  3676. //
  3677. while ( g_bIomBusy[ui32Module] );
  3678. }
  3679. //*****************************************************************************
  3680. //
  3681. //! @brief Returns the contents of the IOM status register.
  3682. //!
  3683. //! @param ui32Module IOM instance to check the status of.
  3684. //!
  3685. //! This function is just a wrapper around the IOM status register.
  3686. //!
  3687. //! @return 32-bit contents of IOM status register.
  3688. //
  3689. //*****************************************************************************
  3690. uint32_t
  3691. am_hal_iom_status_get(uint32_t ui32Module)
  3692. {
  3693. //
  3694. // Validate parameters
  3695. //
  3696. if ( ui32Module >= AM_REG_IOMSTR_NUM_MODULES )
  3697. {
  3698. return 0;
  3699. }
  3700. return AM_REGn(IOMSTR, ui32Module, STATUS);
  3701. }
  3702. //*****************************************************************************
  3703. //
  3704. //! @brief Returns current error state of the IOM.
  3705. //!
  3706. //! @param ui32Module IOM instance to check the status of.
  3707. //!
  3708. //! This function returns status indicating whether the IOM has incurred any
  3709. //! errors or not for previous operation.
  3710. //! This function can be called when the callback is invoked to determine the
  3711. //! status of the transaction just completed.
  3712. //! This function can also be called after a blocking call, though it would
  3713. //! return the same status as returned from the call itself
  3714. //! This function should not be called for an ongoing transaction, and the
  3715. //! result of such operation is indeterministic
  3716. //!
  3717. //! @return AM_HAL_IOM_SUCCESS if all is well.
  3718. //
  3719. //*****************************************************************************
  3720. am_hal_iom_status_e
  3721. am_hal_iom_error_status_get(uint32_t ui32Module)
  3722. {
  3723. //
  3724. // Validate parameters
  3725. //
  3726. if ( ui32Module >= AM_REG_IOMSTR_NUM_MODULES )
  3727. {
  3728. //
  3729. // AM_HAL_IOM_ERR_INVALID_MODULE is defined as an unused interrupt bit.
  3730. //
  3731. return AM_HAL_IOM_ERR_INVALID_MODULE;
  3732. }
  3733. return (g_iom_error_status[ui32Module]);
  3734. }
  3735. //*****************************************************************************
  3736. //
  3737. //! @brief Service interrupts from the IOM.
  3738. //!
  3739. //! @param ui32Status is the IOM interrupt status as returned from
  3740. //! am_hal_iom_int_status_get()
  3741. //!
  3742. //! This function performs the necessary operations to facilitate non-blocking
  3743. //! IOM writes and reads.
  3744. //!
  3745. //! @return None.
  3746. //
  3747. //*****************************************************************************
  3748. void
  3749. am_hal_iom_int_service(uint32_t ui32Module, uint32_t ui32Status)
  3750. {
  3751. am_hal_iom_nb_buffer *psBuffer;
  3752. uint32_t ui32NumBytes;
  3753. uint32_t ui32SpaceInFifo;
  3754. uint32_t thresh;
  3755. //
  3756. // Validate parameters
  3757. //
  3758. if ( ui32Module >= AM_REG_IOMSTR_NUM_MODULES )
  3759. {
  3760. return;
  3761. }
  3762. //
  3763. // Find the buffer information for the chosen IOM module.
  3764. //
  3765. psBuffer = &g_psIOMBuffers[ui32Module];
  3766. // Keep accumulating any error indications
  3767. // This is to account for the case if the error indication comes before CMDCMP
  3768. g_iom_error_status[ui32Module] |= ui32Status;
  3769. //
  3770. // Figure out what type of interrupt this was.
  3771. //
  3772. if ( ui32Status & AM_HAL_IOM_INT_CMDCMP )
  3773. {
  3774. //
  3775. // Need to mark IOM Free
  3776. //
  3777. g_bIomBusy[ui32Module] = false;
  3778. //
  3779. // If we're not in the middle of a non-blocking call right now, there's
  3780. // nothing for this routine to do.
  3781. //
  3782. if ( psBuffer->ui32State == BUFFER_IDLE )
  3783. {
  3784. return;
  3785. }
  3786. //
  3787. // If a command just completed, we need to transfer all available data.
  3788. //
  3789. if ( psBuffer->ui32State == BUFFER_RECEIVING )
  3790. {
  3791. //
  3792. // If we were receiving, we need to copy any remaining data out of
  3793. // the IOM FIFO before calling the callback.
  3794. //
  3795. ui32NumBytes = am_hal_iom_fifo_full_slots(ui32Module);
  3796. am_hal_iom_fifo_read(ui32Module, psBuffer->pui32Data, ui32NumBytes);
  3797. }
  3798. //
  3799. // A command complete event also means that we've already transferred
  3800. // all of the data we need, so we can mark the data buffer as IDLE.
  3801. //
  3802. psBuffer->ui32State = BUFFER_IDLE;
  3803. g_iom_error_status[ui32Module] = internal_iom_get_int_err(ui32Module, g_iom_error_status[ui32Module]);
  3804. //
  3805. // If we have a callback, call it now.
  3806. //
  3807. if ( psBuffer->pfnCallback )
  3808. {
  3809. psBuffer->pfnCallback();
  3810. }
  3811. }
  3812. else if ( ui32Status & AM_HAL_IOM_INT_THR )
  3813. {
  3814. //
  3815. // If we're not in the middle of a non-blocking call right now, there's
  3816. // nothing for this routine to do.
  3817. //
  3818. if ( psBuffer->ui32State == BUFFER_IDLE )
  3819. {
  3820. return;
  3821. }
  3822. //
  3823. // If we received a threshold event in the middle of a command, we need
  3824. // to transfer data.
  3825. //
  3826. if ( psBuffer->ui32State == BUFFER_SENDING )
  3827. {
  3828. thresh = AM_BFRn(IOMSTR, ui32Module, FIFOTHR, FIFOWTHR);
  3829. do
  3830. {
  3831. ui32SpaceInFifo = am_hal_iom_fifo_empty_slots(ui32Module);
  3832. //
  3833. // Figure out how much data we can send.
  3834. //
  3835. if ( psBuffer->ui32BytesLeft <= ui32SpaceInFifo )
  3836. {
  3837. //
  3838. // If the whole transfer will fit in the fifo, send it all.
  3839. //
  3840. ui32NumBytes = psBuffer->ui32BytesLeft;
  3841. }
  3842. else
  3843. {
  3844. //
  3845. // If the transfer won't fit in the fifo completely, send as
  3846. // much as we can (rounded down to a multiple of four bytes).
  3847. //
  3848. ui32NumBytes = ui32SpaceInFifo;
  3849. }
  3850. //
  3851. // Perform the transfer.
  3852. //
  3853. am_hal_iom_fifo_write(ui32Module, psBuffer->pui32Data, ui32NumBytes);
  3854. // Clear any spurious THR interrupt that might have got raised
  3855. // while we were adding data to FIFO
  3856. AM_BFWn(IOMSTR, ui32Module, INTCLR, THR, 1);
  3857. //
  3858. // Update the pointer and the byte counter.
  3859. //
  3860. psBuffer->ui32BytesLeft -= ui32NumBytes;
  3861. psBuffer->pui32Data += (ui32NumBytes / 4);
  3862. if ( 0 == psBuffer->ui32BytesLeft )
  3863. {
  3864. //
  3865. // Done with this transaction
  3866. //
  3867. break;
  3868. }
  3869. } while ( am_hal_iom_fifo_full_slots(ui32Module) <= thresh );
  3870. }
  3871. else
  3872. {
  3873. thresh = AM_BFRn(IOMSTR, ui32Module, FIFOTHR, FIFORTHR);
  3874. while ( (ui32NumBytes = am_hal_iom_fifo_full_slots(ui32Module)) >= thresh )
  3875. {
  3876. //
  3877. // If we get here, we're in the middle of a read. Transfer as much
  3878. // data as possible out of the FIFO and into our buffer.
  3879. //
  3880. if ( ui32NumBytes == psBuffer->ui32BytesLeft )
  3881. {
  3882. //
  3883. // If the fifo contains our entire message, just copy the whole
  3884. // thing out.
  3885. //
  3886. am_hal_iom_fifo_read(ui32Module, psBuffer->pui32Data,
  3887. psBuffer->ui32BytesLeft);
  3888. break;
  3889. }
  3890. else if ( ui32NumBytes >= 4 )
  3891. {
  3892. //
  3893. // If the fifo has at least one 32-bit word in it, copy out the
  3894. // biggest block we can.
  3895. //
  3896. ui32NumBytes = (ui32NumBytes & (~0x3));
  3897. am_hal_iom_fifo_read(ui32Module, psBuffer->pui32Data, ui32NumBytes);
  3898. //
  3899. // Update the pointer and the byte counter.
  3900. //
  3901. psBuffer->ui32BytesLeft -= ui32NumBytes;
  3902. psBuffer->pui32Data += (ui32NumBytes / 4);
  3903. // Clear any spurious THR interrupt that might have got raised
  3904. // while we were reading the data from FIFO
  3905. AM_BFWn(IOMSTR, ui32Module, INTCLR, THR, 1);
  3906. }
  3907. }
  3908. }
  3909. }
  3910. }
  3911. //*****************************************************************************
  3912. //
  3913. //! @brief Initialize the IOM queue system.
  3914. //!
  3915. //! @param ui32Module - IOM module to be initialized for queue transfers.
  3916. //! @param psQueueMemory - Memory to be used for queueing IOM transfers.
  3917. //! @param ui32QueueMemSize - Size of the queue memory.
  3918. //!
  3919. //! This function prepares the selected IOM interface for use with the IOM
  3920. //! queue system. The IOM queue system allows the caller to start multiple IOM
  3921. //! transfers in a non-blocking way. In order to do this, the HAL requires some
  3922. //! amount of memory dedicated to keeping track of IOM transactions before they
  3923. //! can be sent to the hardware registers. This function tells the HAL what
  3924. //! memory it should use for this purpose. For more information on the IOM
  3925. //! queue interface, please see the documentation for
  3926. //! am_hal_iom_queue_spi_write().
  3927. //!
  3928. //! @note This function only needs to be called once (per module), but it must
  3929. //! be called before any other am_hal_iom_queue function.
  3930. //!
  3931. //! @note Each IOM module will need its own working space. If you intend to use
  3932. //! the queueing mechanism with more than one IOM module, you will need to
  3933. //! provide separate queue memory for each module.
  3934. //!
  3935. //! Example usage:
  3936. //!
  3937. //! @code
  3938. //!
  3939. //! //
  3940. //! // Declare an array to be used for IOM queue transactions. This array will
  3941. //! // be big enough to handle 32 IOM transactions.
  3942. //! //
  3943. //! am_hal_iom_queue_entry_t g_psQueueMemory[32];
  3944. //!
  3945. //! //
  3946. //! // Attach the IOM0 queue system to the memory we just allocated.
  3947. //! //
  3948. //! am_hal_iom_queue_init(0, g_psQueueMemory, sizeof(g_psQueueMemory));
  3949. //!
  3950. //! @endcode
  3951. //
  3952. //*****************************************************************************
  3953. void
  3954. am_hal_iom_queue_init(uint32_t ui32Module, am_hal_iom_queue_entry_t *psQueueMemory,
  3955. uint32_t ui32QueueMemSize)
  3956. {
  3957. //
  3958. // Validate parameters
  3959. //
  3960. if ( ui32Module >= AM_REG_IOMSTR_NUM_MODULES )
  3961. {
  3962. return;
  3963. }
  3964. am_hal_queue_init(&g_psIOMQueue[ui32Module], psQueueMemory,
  3965. sizeof(am_hal_iom_queue_entry_t), ui32QueueMemSize);
  3966. }
  3967. //*****************************************************************************
  3968. //
  3969. //! @brief Check to see how many transactions are in the queue.
  3970. //!
  3971. //! @param ui32Module Module number for the queue to check
  3972. //!
  3973. //! This function will check to see how many transactions are in the IOM queue
  3974. //! for the selected IOM module.
  3975. //!
  3976. //! @return Number of transactions in the queue.
  3977. //
  3978. //*****************************************************************************
  3979. uint32_t
  3980. am_hal_iom_queue_length_get(uint32_t ui32Module)
  3981. {
  3982. //
  3983. // Validate parameters
  3984. //
  3985. if ( ui32Module >= AM_REG_IOMSTR_NUM_MODULES )
  3986. {
  3987. return 0;
  3988. }
  3989. return am_hal_queue_data_left(&g_psIOMQueue[ui32Module]);
  3990. }
  3991. //*****************************************************************************
  3992. //
  3993. //! @brief Executes the next operation in the IOM queue.
  3994. //!
  3995. //! @param ui32ModuleNum - Module number for the IOM to use.
  3996. //!
  3997. //! This function checks the IOM queue to see if there are any remaining
  3998. //! transactions. If so, it will start the next available transaction in a
  3999. //! non-blocking way.
  4000. //!
  4001. //! @note This function is called automatically by am_hal_iom_queue_service().
  4002. //! You should not call this function standalone in a normal application.
  4003. //
  4004. //*****************************************************************************
  4005. void
  4006. am_hal_iom_queue_start_next_msg(uint32_t ui32Module)
  4007. {
  4008. am_hal_iom_queue_entry_t sIOMTransaction = {0};
  4009. uint32_t ui32ChipSelect;
  4010. uint32_t *pui32Data;
  4011. uint32_t ui32NumBytes;
  4012. uint32_t ui32Options;
  4013. am_hal_iom_callback_t pfnCallback;
  4014. am_hal_iom_status_e ui32Status = AM_HAL_IOM_SUCCESS;
  4015. uint32_t ui32Critical;
  4016. //
  4017. // Validate parameters
  4018. //
  4019. if ( ui32Module >= AM_REG_IOMSTR_NUM_MODULES )
  4020. {
  4021. return;
  4022. }
  4023. //
  4024. // Start a critical section.
  4025. //
  4026. ui32Critical = am_hal_interrupt_master_disable();
  4027. //
  4028. // Try to get the next IOM operation from the queue.
  4029. //
  4030. if ( am_hal_queue_item_get(&g_psIOMQueue[ui32Module], &sIOMTransaction, 1) )
  4031. {
  4032. //
  4033. // Read the operation parameters
  4034. //
  4035. ui32ChipSelect = sIOMTransaction.ui32ChipSelect;
  4036. pui32Data = sIOMTransaction.pui32Data;
  4037. ui32NumBytes = sIOMTransaction.ui32NumBytes;
  4038. ui32Options = sIOMTransaction.ui32Options;
  4039. pfnCallback = sIOMTransaction.pfnCallback;
  4040. //
  4041. // Figure out if this was a SPI or I2C write or read, and call the
  4042. // appropriate non-blocking function.
  4043. //
  4044. switch ( sIOMTransaction.ui32Operation )
  4045. {
  4046. case AM_HAL_IOM_QUEUE_SPI_WRITE:
  4047. ui32Status = am_hal_iom_spi_write_nb(ui32Module, ui32ChipSelect, pui32Data,
  4048. ui32NumBytes, ui32Options, pfnCallback);
  4049. break;
  4050. case AM_HAL_IOM_QUEUE_SPI_READ:
  4051. ui32Status = am_hal_iom_spi_read_nb(ui32Module, ui32ChipSelect, pui32Data,
  4052. ui32NumBytes, ui32Options, pfnCallback);
  4053. break;
  4054. case AM_HAL_IOM_QUEUE_I2C_WRITE:
  4055. ui32Status = am_hal_iom_i2c_write_nb(ui32Module, ui32ChipSelect, pui32Data,
  4056. ui32NumBytes, ui32Options, pfnCallback);
  4057. break;
  4058. case AM_HAL_IOM_QUEUE_I2C_READ:
  4059. ui32Status = am_hal_iom_i2c_read_nb(ui32Module, ui32ChipSelect, pui32Data,
  4060. ui32NumBytes, ui32Options, pfnCallback);
  4061. break;
  4062. }
  4063. }
  4064. //
  4065. // Exit the critical section.
  4066. //
  4067. am_hal_interrupt_master_set(ui32Critical);
  4068. if (ui32Status != AM_HAL_IOM_SUCCESS)
  4069. {
  4070. // Preserve the error
  4071. g_iom_error_status[ui32Module] = ui32Status;
  4072. // Call the respective callback
  4073. pfnCallback();
  4074. }
  4075. }
  4076. //*****************************************************************************
  4077. //
  4078. //! @brief Send a SPI frame using the IOM queue.
  4079. //!
  4080. //! @param ui32Module - Module number for the IOM
  4081. //! @param ui32ChipSelect - Chip-select number for this transaction.
  4082. //! @param pui32Data - Pointer to the bytes that will be sent.
  4083. //! @param ui32NumBytes - Number of bytes to send.
  4084. //! @param ui32Options - Additional SPI transfer options.
  4085. //!
  4086. //! This function performs SPI writes to a selected SPI device.
  4087. //!
  4088. //! This function call is a queued implementation. It will write as much
  4089. //! data to the FIFO as possible immediately, store a pointer to the remaining
  4090. //! data, start the transfer on the bus, and then immediately return. If the
  4091. //! FIFO is already in use, this function will save its arguments to the IOM
  4092. //! queue and execute the transaction when the FIFO becomes available.
  4093. //!
  4094. //! The caller will need to make sure that \e am_hal_iom_queue_service() is
  4095. //! called for IOM FIFO interrupt events and "command complete" interrupt
  4096. //! events. The \e am_hal_iom_queue_service() function will refill the FIFO as
  4097. //! necessary and call the \e pfnCallback function when the transaction is
  4098. //! finished.
  4099. //!
  4100. //! @note The actual SPI and I2C interfaces operate in BYTES, not 32-bit words.
  4101. //! This means that you will need to byte-pack the \e pui32Data array with the
  4102. //! data you intend to send over the interface. One easy way to do this is to
  4103. //! declare the array as a 32-bit integer array, but use an 8-bit pointer to
  4104. //! put your actual data into the array. If there are not enough bytes in your
  4105. //! desired message to completely fill the last 32-bit word, you may pad that
  4106. //! last word with bytes of any value. The IOM hardware will only read the
  4107. //! first \e ui32NumBytes in the \e pui8Data array.
  4108. //
  4109. //*****************************************************************************
  4110. am_hal_iom_status_e
  4111. am_hal_iom_queue_spi_write(uint32_t ui32Module, uint32_t ui32ChipSelect,
  4112. uint32_t *pui32Data, uint32_t ui32NumBytes,
  4113. uint32_t ui32Options, am_hal_iom_callback_t pfnCallback)
  4114. {
  4115. uint32_t ui32Critical;
  4116. am_hal_iom_status_e ui32Status;
  4117. //
  4118. // Validate parameters
  4119. //
  4120. if ( ui32Module >= AM_REG_IOMSTR_NUM_MODULES )
  4121. {
  4122. return AM_HAL_IOM_ERR_INVALID_MODULE;
  4123. }
  4124. // Reset the error status
  4125. ui32Status = g_iom_error_status[ui32Module] = AM_HAL_IOM_SUCCESS;
  4126. if ( ui32NumBytes == 0 )
  4127. {
  4128. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_INVALID_PARAM;
  4129. return ui32Status;
  4130. }
  4131. //
  4132. // Start a critical section.
  4133. //
  4134. ui32Critical = am_hal_interrupt_master_disable();
  4135. //
  4136. // Check to see if we need to use the queue. If the IOM is idle, and
  4137. // there's nothing in the queue already, we can go ahead and start the
  4138. // transaction in the physical IOM. Need to check for the g_bIomBusy to
  4139. // avoid a race condition where IDLE is set - but the command complete
  4140. // for previous transaction has not been processed yet
  4141. //
  4142. if ( (g_bIomBusy[ui32Module] == false) &&
  4143. am_hal_queue_empty(&g_psIOMQueue[ui32Module]) )
  4144. {
  4145. //
  4146. // Send the packet.
  4147. //
  4148. ui32Status = am_hal_iom_spi_write_nb(ui32Module, ui32ChipSelect, pui32Data,
  4149. ui32NumBytes, ui32Options, pfnCallback);
  4150. }
  4151. else
  4152. {
  4153. //
  4154. // Otherwise, we'll build a transaction structure and add it to the queue.
  4155. //
  4156. am_hal_iom_queue_entry_t sIOMTransaction;
  4157. sIOMTransaction.ui32Operation = AM_HAL_IOM_QUEUE_SPI_WRITE;
  4158. sIOMTransaction.ui32Module = ui32Module;
  4159. sIOMTransaction.ui32ChipSelect = ui32ChipSelect;
  4160. sIOMTransaction.pui32Data = pui32Data;
  4161. sIOMTransaction.ui32NumBytes = ui32NumBytes;
  4162. sIOMTransaction.ui32Options = ui32Options;
  4163. sIOMTransaction.pfnCallback = pfnCallback;
  4164. //
  4165. // Make sure the item actually makes it into the queue
  4166. //
  4167. if ( am_hal_queue_item_add(&g_psIOMQueue[ui32Module], &sIOMTransaction, 1) == false )
  4168. {
  4169. //
  4170. // Didn't have enough memory.
  4171. //
  4172. ui32Status = AM_HAL_IOM_ERR_RESOURCE_ERR;
  4173. }
  4174. }
  4175. if (ui32Status != AM_HAL_IOM_SUCCESS)
  4176. {
  4177. g_iom_error_status[ui32Module] = ui32Status;
  4178. }
  4179. //
  4180. // Exit the critical section.
  4181. //
  4182. am_hal_interrupt_master_set(ui32Critical);
  4183. return ui32Status;
  4184. }
  4185. //*****************************************************************************
  4186. //
  4187. //! @brief Read a SPI frame using the IOM queue.
  4188. //!
  4189. //! @param ui32Module - Module number for the IOM
  4190. //! @param ui32ChipSelect - Chip select number for this transaction.
  4191. //! @param pui32Data - Pointer to the array where received bytes should go.
  4192. //! @param ui32NumBytes - Number of bytes to read.
  4193. //! @param ui32Options - Additional SPI transfer options.
  4194. //!
  4195. //! This function performs SPI reads to a selected SPI device.
  4196. //!
  4197. //! This function call is a queued implementation. It will write as much
  4198. //! data to the FIFO as possible immediately, store a pointer to the remaining
  4199. //! data, start the transfer on the bus, and then immediately return. If the
  4200. //! FIFO is already in use, this function will save its arguments to the IOM
  4201. //! queue and execute the transaction when the FIFO becomes available.
  4202. //!
  4203. //! The caller will need to make sure that \e am_hal_iom_queue_service() is
  4204. //! called for IOM FIFO interrupt events and "command complete" interrupt
  4205. //! events. The \e am_hal_iom_queue_service() function will empty the FIFO as
  4206. //! necessary and call the \e pfnCallback function when the transaction is
  4207. //! finished.
  4208. //!
  4209. //! @note The actual SPI and I2C interfaces operate in BYTES, not 32-bit words.
  4210. //! This means that you will need to byte-pack the \e pui32Data array with the
  4211. //! data you intend to send over the interface. One easy way to do this is to
  4212. //! declare the array as a 32-bit integer array, but use an 8-bit pointer to
  4213. //! put your actual data into the array. If there are not enough bytes in your
  4214. //! desired message to completely fill the last 32-bit word, you may pad that
  4215. //! last word with bytes of any value. The IOM hardware will only read the
  4216. //! first \e ui32NumBytes in the \e pui8Data array.
  4217. //
  4218. //*****************************************************************************
  4219. am_hal_iom_status_e
  4220. am_hal_iom_queue_spi_read(uint32_t ui32Module, uint32_t ui32ChipSelect,
  4221. uint32_t *pui32Data, uint32_t ui32NumBytes,
  4222. uint32_t ui32Options, am_hal_iom_callback_t pfnCallback)
  4223. {
  4224. uint32_t ui32Critical;
  4225. am_hal_iom_status_e ui32Status;
  4226. //
  4227. // Validate parameters
  4228. //
  4229. if ( ui32Module >= AM_REG_IOMSTR_NUM_MODULES )
  4230. {
  4231. return AM_HAL_IOM_ERR_INVALID_MODULE;
  4232. }
  4233. // Reset the error status
  4234. ui32Status = g_iom_error_status[ui32Module] = AM_HAL_IOM_SUCCESS;
  4235. if ( ui32NumBytes == 0 )
  4236. {
  4237. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_INVALID_PARAM;
  4238. return ui32Status;
  4239. }
  4240. // Start a critical section.
  4241. //
  4242. ui32Critical = am_hal_interrupt_master_disable();
  4243. //
  4244. // Check to see if we need to use the queue. If the IOM is idle, and
  4245. // there's nothing in the queue already, we can go ahead and start the
  4246. // transaction in the physical IOM. Need to check for the g_bIomBusy to
  4247. // avoid a race condition where IDLE is set - but the command complete
  4248. // for previous transaction has not been processed yet
  4249. //
  4250. if ( (g_bIomBusy[ui32Module] == false) &&
  4251. am_hal_queue_empty(&g_psIOMQueue[ui32Module]) )
  4252. {
  4253. //
  4254. // Send the packet.
  4255. //
  4256. ui32Status = am_hal_iom_spi_read_nb(ui32Module, ui32ChipSelect, pui32Data,
  4257. ui32NumBytes, ui32Options, pfnCallback);
  4258. }
  4259. else
  4260. {
  4261. //
  4262. // Otherwise, we'll build a transaction structure and add it to the queue.
  4263. //
  4264. am_hal_iom_queue_entry_t sIOMTransaction;
  4265. sIOMTransaction.ui32Operation = AM_HAL_IOM_QUEUE_SPI_READ;
  4266. sIOMTransaction.ui32Module = ui32Module;
  4267. sIOMTransaction.ui32ChipSelect = ui32ChipSelect;
  4268. sIOMTransaction.pui32Data = pui32Data;
  4269. sIOMTransaction.ui32NumBytes = ui32NumBytes;
  4270. sIOMTransaction.ui32Options = ui32Options;
  4271. sIOMTransaction.pfnCallback = pfnCallback;
  4272. //
  4273. // Make sure the item actually makes it into the queue
  4274. //
  4275. if ( am_hal_queue_item_add(&g_psIOMQueue[ui32Module], &sIOMTransaction, 1) == false )
  4276. {
  4277. //
  4278. // Didn't have enough memory.
  4279. //
  4280. ui32Status = AM_HAL_IOM_ERR_RESOURCE_ERR;
  4281. }
  4282. }
  4283. if (ui32Status != AM_HAL_IOM_SUCCESS)
  4284. {
  4285. g_iom_error_status[ui32Module] = ui32Status;
  4286. }
  4287. //
  4288. // Exit the critical section.
  4289. //
  4290. am_hal_interrupt_master_set(ui32Critical);
  4291. return ui32Status;
  4292. }
  4293. //*****************************************************************************
  4294. //
  4295. //! @brief Send an I2C frame using the IOM queue.
  4296. //!
  4297. //! @param ui32Module - Module number for the IOM
  4298. //! @param ui32BusAddress - I2C address of the target device.
  4299. //! @param pui32Data - Pointer to the bytes that will be sent.
  4300. //! @param ui32NumBytes - Number of bytes to send.
  4301. //! @param ui32Options - Additional I2C transfer options.
  4302. //!
  4303. //! This function performs I2C writes to a selected I2C device.
  4304. //!
  4305. //! This function call is a queued implementation. It will write as much
  4306. //! data to the FIFO as possible immediately, store a pointer to the remaining
  4307. //! data, start the transfer on the bus, and then immediately return. If the
  4308. //! FIFO is already in use, this function will save its arguments to the IOM
  4309. //! queue and execute the transaction when the FIFO becomes available.
  4310. //!
  4311. //! The caller will need to make sure that \e am_hal_iom_queue_service() is
  4312. //! called for IOM FIFO interrupt events and "command complete" interrupt
  4313. //! events. The \e am_hal_iom_queue_service() function will refill the FIFO as
  4314. //! necessary and call the \e pfnCallback function when the transaction is
  4315. //! finished.
  4316. //!
  4317. //! @note The actual SPI and I2C interfaces operate in BYTES, not 32-bit words.
  4318. //! This means that you will need to byte-pack the \e pui32Data array with the
  4319. //! data you intend to send over the interface. One easy way to do this is to
  4320. //! declare the array as a 32-bit integer array, but use an 8-bit pointer to
  4321. //! put your actual data into the array. If there are not enough bytes in your
  4322. //! desired message to completely fill the last 32-bit word, you may pad that
  4323. //! last word with bytes of any value. The IOM hardware will only read the
  4324. //! first \e ui32NumBytes in the \e pui8Data array.
  4325. //
  4326. //*****************************************************************************
  4327. am_hal_iom_status_e
  4328. am_hal_iom_queue_i2c_write(uint32_t ui32Module, uint32_t ui32BusAddress,
  4329. uint32_t *pui32Data, uint32_t ui32NumBytes,
  4330. uint32_t ui32Options, am_hal_iom_callback_t pfnCallback)
  4331. {
  4332. am_hal_iom_status_e ui32Status;
  4333. uint32_t ui32Critical;
  4334. //
  4335. // Validate parameters
  4336. //
  4337. if ( ui32Module >= AM_REG_IOMSTR_NUM_MODULES )
  4338. {
  4339. return AM_HAL_IOM_ERR_INVALID_MODULE;
  4340. }
  4341. // Reset the error status
  4342. ui32Status = g_iom_error_status[ui32Module] = AM_HAL_IOM_SUCCESS;
  4343. if ( ui32NumBytes == 0 )
  4344. {
  4345. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_INVALID_PARAM;
  4346. return ui32Status;
  4347. }
  4348. //
  4349. // Start a critical section.
  4350. //
  4351. ui32Critical = am_hal_interrupt_master_disable();
  4352. //
  4353. // Check to see if we need to use the queue. If the IOM is idle, and
  4354. // there's nothing in the queue already, we can go ahead and start the
  4355. // transaction in the physical IOM. Need to check for the g_bIomBusy to
  4356. // avoid a race condition where IDLE is set - but the command complete
  4357. // for previous transaction has not been processed yet
  4358. //
  4359. if ( (g_bIomBusy[ui32Module] == false) &&
  4360. am_hal_queue_empty(&g_psIOMQueue[ui32Module]) )
  4361. {
  4362. //
  4363. // Send the packet.
  4364. //
  4365. ui32Status = am_hal_iom_i2c_write_nb(ui32Module, ui32BusAddress, pui32Data,
  4366. ui32NumBytes, ui32Options, pfnCallback);
  4367. }
  4368. else
  4369. {
  4370. //
  4371. // Otherwise, we'll build a transaction structure and add it to the queue.
  4372. //
  4373. am_hal_iom_queue_entry_t sIOMTransaction;
  4374. sIOMTransaction.ui32Operation = AM_HAL_IOM_QUEUE_I2C_WRITE;
  4375. sIOMTransaction.ui32Module = ui32Module;
  4376. sIOMTransaction.ui32ChipSelect = ui32BusAddress;
  4377. sIOMTransaction.pui32Data = pui32Data;
  4378. sIOMTransaction.ui32NumBytes = ui32NumBytes;
  4379. sIOMTransaction.ui32Options = ui32Options;
  4380. sIOMTransaction.pfnCallback = pfnCallback;
  4381. //
  4382. // Make sure the item actually makes it into the queue
  4383. //
  4384. if ( am_hal_queue_item_add(&g_psIOMQueue[ui32Module], &sIOMTransaction, 1) == false )
  4385. {
  4386. //
  4387. // Didn't have enough memory.
  4388. //
  4389. ui32Status = AM_HAL_IOM_ERR_RESOURCE_ERR;
  4390. }
  4391. }
  4392. if (ui32Status != AM_HAL_IOM_SUCCESS)
  4393. {
  4394. g_iom_error_status[ui32Module] = ui32Status;
  4395. }
  4396. //
  4397. // Exit the critical section.
  4398. //
  4399. am_hal_interrupt_master_set(ui32Critical);
  4400. return ui32Status;
  4401. }
  4402. //*****************************************************************************
  4403. //
  4404. //! @brief Read a I2C frame using the IOM queue.
  4405. //!
  4406. //! @param ui32Module - Module number for the IOM
  4407. //! @param ui32BusAddress - I2C address of the target device.
  4408. //! @param pui32Data - Pointer to the array where received bytes should go.
  4409. //! @param ui32NumBytes - Number of bytes to read.
  4410. //! @param ui32Options - Additional I2C transfer options.
  4411. //!
  4412. //! This function performs I2C reads to a selected I2C device.
  4413. //!
  4414. //! This function call is a queued implementation. It will write as much
  4415. //! data to the FIFO as possible immediately, store a pointer to the remaining
  4416. //! data, start the transfer on the bus, and then immediately return. If the
  4417. //! FIFO is already in use, this function will save its arguments to the IOM
  4418. //! queue and execute the transaction when the FIFO becomes available.
  4419. //!
  4420. //! The caller will need to make sure that \e am_hal_iom_queue_service() is
  4421. //! called for IOM FIFO interrupt events and "command complete" interrupt
  4422. //! events. The \e am_hal_iom_queue_service() function will empty the FIFO as
  4423. //! necessary and call the \e pfnCallback function when the transaction is
  4424. //! finished.
  4425. //!
  4426. //! @note The actual SPI and I2C interfaces operate in BYTES, not 32-bit words.
  4427. //! This means that you will need to byte-pack the \e pui32Data array with the
  4428. //! data you intend to send over the interface. One easy way to do this is to
  4429. //! declare the array as a 32-bit integer array, but use an 8-bit pointer to
  4430. //! put your actual data into the array. If there are not enough bytes in your
  4431. //! desired message to completely fill the last 32-bit word, you may pad that
  4432. //! last word with bytes of any value. The IOM hardware will only read the
  4433. //! first \e ui32NumBytes in the \e pui8Data array.
  4434. //
  4435. //*****************************************************************************
  4436. am_hal_iom_status_e
  4437. am_hal_iom_queue_i2c_read(uint32_t ui32Module, uint32_t ui32BusAddress,
  4438. uint32_t *pui32Data, uint32_t ui32NumBytes,
  4439. uint32_t ui32Options, am_hal_iom_callback_t pfnCallback)
  4440. {
  4441. uint32_t ui32Critical;
  4442. am_hal_iom_status_e ui32Status;
  4443. //
  4444. // Validate parameters
  4445. //
  4446. if ( ui32Module >= AM_REG_IOMSTR_NUM_MODULES )
  4447. {
  4448. return AM_HAL_IOM_ERR_INVALID_MODULE;
  4449. }
  4450. // Reset the error status
  4451. ui32Status = g_iom_error_status[ui32Module] = AM_HAL_IOM_SUCCESS;
  4452. if ( ui32NumBytes == 0 )
  4453. {
  4454. g_iom_error_status[ui32Module] = ui32Status = AM_HAL_IOM_ERR_INVALID_PARAM;
  4455. return ui32Status;
  4456. }
  4457. //
  4458. // Start a critical section.
  4459. //
  4460. ui32Critical = am_hal_interrupt_master_disable();
  4461. //
  4462. // Check to see if we need to use the queue. If the IOM is idle, and
  4463. // there's nothing in the queue already, we can go ahead and start the
  4464. // transaction in the physical IOM. Need to check for the g_bIomBusy to
  4465. // avoid a race condition where IDLE is set - but the command complete
  4466. // for previous transaction has not been processed yet
  4467. //
  4468. if ( (g_bIomBusy[ui32Module] == false) &&
  4469. am_hal_queue_empty(&g_psIOMQueue[ui32Module]) )
  4470. {
  4471. //
  4472. // Send the packet.
  4473. //
  4474. ui32Status = am_hal_iom_i2c_read_nb(ui32Module, ui32BusAddress, pui32Data,
  4475. ui32NumBytes, ui32Options, pfnCallback);
  4476. }
  4477. else
  4478. {
  4479. //
  4480. // Otherwise, we'll build a transaction structure and add it to the queue.
  4481. //
  4482. am_hal_iom_queue_entry_t sIOMTransaction;
  4483. sIOMTransaction.ui32Operation = AM_HAL_IOM_QUEUE_I2C_READ;
  4484. sIOMTransaction.ui32Module = ui32Module;
  4485. sIOMTransaction.ui32ChipSelect = ui32BusAddress;
  4486. sIOMTransaction.pui32Data = pui32Data;
  4487. sIOMTransaction.ui32NumBytes = ui32NumBytes;
  4488. sIOMTransaction.ui32Options = ui32Options;
  4489. sIOMTransaction.pfnCallback = pfnCallback;
  4490. //
  4491. // Make sure the item actually makes it into the queue
  4492. //
  4493. if ( am_hal_queue_item_add(&g_psIOMQueue[ui32Module], &sIOMTransaction, 1) == false )
  4494. {
  4495. //
  4496. // Didn't have enough memory.
  4497. //
  4498. ui32Status = AM_HAL_IOM_ERR_RESOURCE_ERR;
  4499. }
  4500. }
  4501. if (ui32Status != AM_HAL_IOM_SUCCESS)
  4502. {
  4503. g_iom_error_status[ui32Module] = ui32Status;
  4504. }
  4505. //
  4506. // Exit the critical section.
  4507. //
  4508. am_hal_interrupt_master_set(ui32Critical);
  4509. return ui32Status;
  4510. }
  4511. //*****************************************************************************
  4512. //
  4513. //! @brief "Block" until the queue of IOM transactions is over.
  4514. //!
  4515. //! @param ui32Module - Module number for the IOM.
  4516. //!
  4517. //! This function will sleep the core block until the queue for the selected
  4518. //! IOM is empty. This is mainly useful for non-RTOS applications where the
  4519. //! caller needs to know that a certain IOM transaction is complete before
  4520. //! continuing with the main program flow.
  4521. //!
  4522. //! @note This function will put the core to sleep while it waits for the
  4523. //! queued IOM transactions to complete. This will save power, in most
  4524. //! situations, but it may not be the best option in all cases. \e Do \e not
  4525. //! call this function from interrupt context (the core may not wake up again).
  4526. //! \e Be \e careful using this function from an RTOS task (many RTOS
  4527. //! implementations use hardware interrupts to switch contexts, and most RTOS
  4528. //! implementations expect to control sleep behavior).
  4529. //
  4530. //*****************************************************************************
  4531. void
  4532. am_hal_iom_sleeping_queue_flush(uint32_t ui32Module)
  4533. {
  4534. bool bWaiting = true;
  4535. uint32_t ui32Critical;
  4536. //
  4537. // Validate parameters
  4538. //
  4539. if ( ui32Module >= AM_REG_IOMSTR_NUM_MODULES )
  4540. {
  4541. return;
  4542. }
  4543. //
  4544. // Loop forever waiting for the IOM to be idle and the queue to be empty.
  4545. //
  4546. while ( bWaiting )
  4547. {
  4548. //
  4549. // Start a critical section.
  4550. //
  4551. ui32Critical = am_hal_interrupt_master_disable();
  4552. //
  4553. // Check the queue and the IOM itself.
  4554. //
  4555. if ( (g_bIomBusy[ui32Module] == false) &&
  4556. am_hal_queue_empty(&g_psIOMQueue[ui32Module]) )
  4557. {
  4558. //
  4559. // If the queue is empty and the IOM is idle, we can go ahead and
  4560. // return.
  4561. //
  4562. bWaiting = false;
  4563. }
  4564. else
  4565. {
  4566. //
  4567. // Otherwise, we should sleep until the interface is actually free.
  4568. //
  4569. am_hal_sysctrl_sleep(AM_HAL_SYSCTRL_SLEEP_NORMAL);
  4570. }
  4571. //
  4572. // End the critical section.
  4573. //
  4574. am_hal_interrupt_master_set(ui32Critical);
  4575. }
  4576. }
  4577. //*****************************************************************************
  4578. //
  4579. //! @brief Service IOM transaction queue.
  4580. //!
  4581. //! @param ui32Module - Module number for the IOM to be used.
  4582. //! @param ui32Status - Interrupt status bits for the IOM module being used.
  4583. //!
  4584. //! This function handles the operation of FIFOs and the IOM queue during
  4585. //! queued IOM transactions. If you are using \e am_hal_iom_queue_spi_write()
  4586. //! or similar functions, you will need to call this function in your interrupt
  4587. //! handler.
  4588. //!
  4589. //! @note This interrupt service routine relies on the user to enable the IOM
  4590. //! interrupts for FIFO threshold and CMD complete.
  4591. //!
  4592. //! Example:
  4593. //!
  4594. //! @code
  4595. //! void
  4596. //! am_iomaster0_isr(void)
  4597. //! {
  4598. //! uint32_t ui32Status;
  4599. //!
  4600. //! //
  4601. //! // Check to see which interrupt caused us to enter the ISR.
  4602. //! //
  4603. //! ui32Status = am_hal_iom_int_status(0, true);
  4604. //!
  4605. //! //
  4606. //! // Clear the interrupts. This should be done before calling service routine
  4607. //! // as otherwise we may lose re-triggered interrupts
  4608. //! //
  4609. //! am_hal_iom_int_clear(ui32Status);
  4610. //!
  4611. //! //
  4612. //! // Fill or empty the FIFO, and either continue the current operation or
  4613. //! // start the next one in the queue. If there was a callback, it will be
  4614. //! // called here.
  4615. //! //
  4616. //! am_hal_iom_queue_service(0, ui32Status);
  4617. //! }
  4618. //! @endcode
  4619. //!
  4620. //! @return
  4621. //
  4622. //*****************************************************************************
  4623. void
  4624. am_hal_iom_queue_service(uint32_t ui32Module, uint32_t ui32Status)
  4625. {
  4626. //
  4627. // Validate parameters
  4628. //
  4629. if ( ui32Module >= AM_REG_IOMSTR_NUM_MODULES )
  4630. {
  4631. return;
  4632. }
  4633. //
  4634. // Service the FIFOs in case this was a threshold interrupt.
  4635. //
  4636. am_hal_iom_int_service(ui32Module, ui32Status);
  4637. //
  4638. // If the last interrupt was a "command complete", then the IOM should be
  4639. // idle already or very soon. Make absolutely sure that the IOM is not in
  4640. // use, and then start the next transaction in the queue.
  4641. //
  4642. if ( ui32Status & AM_HAL_IOM_INT_CMDCMP )
  4643. {
  4644. if ( g_psIOMQueue[ui32Module].pui8Data != NULL )
  4645. {
  4646. am_hal_iom_queue_start_next_msg(ui32Module);
  4647. }
  4648. }
  4649. }
  4650. //*****************************************************************************
  4651. //
  4652. //! @brief Enable selected IOM Interrupts.
  4653. //!
  4654. //! @param ui32Module - Module number.
  4655. //! @param ui32Interrupt - Use the macro bit fields provided in am_hal_iom.h
  4656. //!
  4657. //! Use this function to enable the IOM interrupts.
  4658. //!
  4659. //! @return None
  4660. //
  4661. //*****************************************************************************
  4662. void
  4663. am_hal_iom_int_enable(uint32_t ui32Module, uint32_t ui32Interrupt)
  4664. {
  4665. //
  4666. // Validate parameters
  4667. //
  4668. if ( ui32Module >= AM_REG_IOMSTR_NUM_MODULES )
  4669. {
  4670. return;
  4671. }
  4672. AM_REGn(IOMSTR, ui32Module, INTEN) |= ui32Interrupt;
  4673. }
  4674. //*****************************************************************************
  4675. //
  4676. //! @brief Return the enabled IOM Interrupts.
  4677. //!
  4678. //! @param ui32Module - Module number.
  4679. //!
  4680. //! Use this function to return all enabled IOM interrupts.
  4681. //!
  4682. //! @return all enabled IOM interrupts.
  4683. //
  4684. //*****************************************************************************
  4685. uint32_t
  4686. am_hal_iom_int_enable_get(uint32_t ui32Module)
  4687. {
  4688. //
  4689. // Validate parameters
  4690. //
  4691. if ( ui32Module >= AM_REG_IOMSTR_NUM_MODULES )
  4692. {
  4693. return 0;
  4694. }
  4695. return AM_REGn(IOMSTR, ui32Module, INTEN);
  4696. }
  4697. //*****************************************************************************
  4698. //
  4699. //! @brief Disable selected IOM Interrupts.
  4700. //!
  4701. //! @param ui32Module - Module number.
  4702. //! @param ui32Interrupt - Use the macro bit fields provided in am_hal_iom.h
  4703. //!
  4704. //! Use this function to disable the IOM interrupts.
  4705. //!
  4706. //! @return None
  4707. //
  4708. //*****************************************************************************
  4709. void
  4710. am_hal_iom_int_disable(uint32_t ui32Module, uint32_t ui32Interrupt)
  4711. {
  4712. //
  4713. // Validate parameters
  4714. //
  4715. if ( ui32Module >= AM_REG_IOMSTR_NUM_MODULES )
  4716. {
  4717. return;
  4718. }
  4719. AM_REGn(IOMSTR, ui32Module, INTEN) &= ~ui32Interrupt;
  4720. }
  4721. //*****************************************************************************
  4722. //
  4723. //! @brief Clear selected IOM Interrupts.
  4724. //!
  4725. //! @param ui32Module - Module number.
  4726. //! @param ui32Interrupt - Use the macro bit fields provided in am_hal_iom.h
  4727. //!
  4728. //! Use this function to clear the IOM interrupts.
  4729. //!
  4730. //! @return None
  4731. //
  4732. //*****************************************************************************
  4733. void
  4734. am_hal_iom_int_clear(uint32_t ui32Module, uint32_t ui32Interrupt)
  4735. {
  4736. //
  4737. // Validate parameters
  4738. //
  4739. if ( ui32Module >= AM_REG_IOMSTR_NUM_MODULES )
  4740. {
  4741. return;
  4742. }
  4743. AM_REGn(IOMSTR, ui32Module, INTCLR) = ui32Interrupt;
  4744. }
  4745. //*****************************************************************************
  4746. //
  4747. //! @brief Set selected IOM Interrupts.
  4748. //!
  4749. //! @param ui32Module - Module number.
  4750. //! @param ui32Interrupt - Use the macro bit fields provided in am_hal_iom.h
  4751. //!
  4752. //! Use this function to set the IOM interrupts.
  4753. //!
  4754. //! @return None
  4755. //
  4756. //*****************************************************************************
  4757. void
  4758. am_hal_iom_int_set(uint32_t ui32Module, uint32_t ui32Interrupt)
  4759. {
  4760. //
  4761. // Validate parameters
  4762. //
  4763. if ( ui32Module >= AM_REG_IOMSTR_NUM_MODULES )
  4764. {
  4765. return;
  4766. }
  4767. AM_REGn(IOMSTR, ui32Module, INTSET) = ui32Interrupt;
  4768. }
  4769. //*****************************************************************************
  4770. //
  4771. //! @brief Return the IOM Interrupt status.
  4772. //!
  4773. //! @param ui32Module - Module number.
  4774. //! @param bEnabledOnly - return only the enabled interrupts.
  4775. //!
  4776. //! Use this function to get the IOM interrupt status.
  4777. //!
  4778. //! @return interrupt status
  4779. //
  4780. //*****************************************************************************
  4781. uint32_t
  4782. am_hal_iom_int_status_get(uint32_t ui32Module, bool bEnabledOnly)
  4783. {
  4784. //
  4785. // Validate parameters
  4786. //
  4787. if ( ui32Module >= AM_REG_IOMSTR_NUM_MODULES )
  4788. {
  4789. return 0;
  4790. }
  4791. if ( bEnabledOnly )
  4792. {
  4793. uint32_t u32RetVal = AM_REGn(IOMSTR, ui32Module, INTSTAT);
  4794. return u32RetVal & AM_REGn(IOMSTR, ui32Module, INTEN);
  4795. }
  4796. else
  4797. {
  4798. return AM_REGn(IOMSTR, ui32Module, INTSTAT);
  4799. }
  4800. }
  4801. //*****************************************************************************
  4802. //
  4803. // End Doxygen group.
  4804. //! @}
  4805. //
  4806. //*****************************************************************************