am_hal_iom.c 150 KB

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