am_hal_iom.c 149 KB

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