bmi160.c 191 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988
  1. /**
  2. * Copyright (c) 2021 Bosch Sensortec GmbH. All rights reserved.
  3. *
  4. * BSD-3-Clause
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * 3. Neither the name of the copyright holder nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  26. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  29. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  30. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. * POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. * @file bmi160.c
  34. * @date 2021-10-05
  35. * @version v3.9.2
  36. *
  37. */
  38. #include "bmi160.h"
  39. /* Below look up table follows the enum bmi160_int_types.
  40. * Hence any change should match to the enum bmi160_int_types
  41. */
  42. const uint8_t int_mask_lookup_table[13] = {
  43. BMI160_INT1_SLOPE_MASK,
  44. BMI160_INT1_SLOPE_MASK,
  45. BMI160_INT2_LOW_STEP_DETECT_MASK,
  46. BMI160_INT1_DOUBLE_TAP_MASK,
  47. BMI160_INT1_SINGLE_TAP_MASK,
  48. BMI160_INT1_ORIENT_MASK,
  49. BMI160_INT1_FLAT_MASK,
  50. BMI160_INT1_HIGH_G_MASK,
  51. BMI160_INT1_LOW_G_MASK,
  52. BMI160_INT1_NO_MOTION_MASK,
  53. BMI160_INT2_DATA_READY_MASK,
  54. BMI160_INT2_FIFO_FULL_MASK,
  55. BMI160_INT2_FIFO_WM_MASK};
  56. /*********************************************************************/
  57. /* Static function declarations */
  58. /*!
  59. * @brief This API configures the pins to fire the
  60. * interrupt signal when it occurs
  61. *
  62. * @param[in] int_config : Structure instance of bmi160_int_settg.
  63. * @param[in] dev : Structure instance of bmi160_dev.
  64. *
  65. * @return Result of API execution status
  66. * @retval zero -> Success / -ve value -> Error.
  67. */
  68. static int8_t
  69. set_intr_pin_config(const struct bmi160_int_settg* int_config, const struct bmi160_dev* dev);
  70. /*!
  71. * @brief This API sets the any-motion interrupt of the sensor.
  72. * This interrupt occurs when accel values exceeds preset threshold
  73. * for a certain period of time.
  74. *
  75. * @param[in] int_config : Structure instance of bmi160_int_settg.
  76. * @param[in] dev : Structure instance of bmi160_dev.
  77. *
  78. * @return Result of API execution status
  79. * @retval zero -> Success / -ve value -> Error.
  80. */
  81. static int8_t
  82. set_accel_any_motion_int(struct bmi160_int_settg* int_config, struct bmi160_dev* dev);
  83. /*!
  84. * @brief This API sets tap interrupts.Interrupt is fired when
  85. * tap movements happen.
  86. *
  87. * @param[in] int_config : Structure instance of bmi160_int_settg.
  88. * @param[in] dev : Structure instance of bmi160_dev.
  89. *
  90. * @return Result of API execution status
  91. * @retval zero -> Success / -ve value -> Error.
  92. */
  93. static int8_t set_accel_tap_int(struct bmi160_int_settg* int_config, const struct bmi160_dev* dev);
  94. /*!
  95. * @brief This API sets the data ready interrupt for both accel and gyro.
  96. * This interrupt occurs when new accel and gyro data come.
  97. *
  98. * @param[in] int_config : Structure instance of bmi160_int_settg.
  99. * @param[in] dev : Structure instance of bmi160_dev.
  100. *
  101. * @return Result of API execution status
  102. * @retval zero -> Success / -ve value -> Error.
  103. */
  104. static int8_t set_accel_gyro_data_ready_int(
  105. const struct bmi160_int_settg* int_config,
  106. const struct bmi160_dev* dev);
  107. /*!
  108. * @brief This API sets the significant motion interrupt of the sensor.This
  109. * interrupt occurs when there is change in user location.
  110. *
  111. * @param[in] int_config : Structure instance of bmi160_int_settg.
  112. * @param[in] dev : Structure instance of bmi160_dev.
  113. *
  114. *
  115. * @return Result of API execution status
  116. * @retval zero -> Success / -ve value -> Error.
  117. */
  118. static int8_t
  119. set_accel_sig_motion_int(struct bmi160_int_settg* int_config, struct bmi160_dev* dev);
  120. /*!
  121. * @brief This API sets the no motion/slow motion interrupt of the sensor.
  122. * Slow motion is similar to any motion interrupt.No motion interrupt
  123. * occurs when slope bet. two accel values falls below preset threshold
  124. * for preset duration.
  125. *
  126. * @param[in] int_config : Structure instance of bmi160_int_settg.
  127. * @param[in] dev : Structure instance of bmi160_dev.
  128. *
  129. * @return Result of API execution status
  130. * @retval zero -> Success / -ve value -> Error.
  131. */
  132. static int8_t
  133. set_accel_no_motion_int(struct bmi160_int_settg* int_config, const struct bmi160_dev* dev);
  134. /*!
  135. * @brief This API sets the step detection interrupt.This interrupt
  136. * occurs when the single step causes accel values to go above
  137. * preset threshold.
  138. *
  139. * @param[in] int_config : Structure instance of bmi160_int_settg.
  140. * @param[in] dev : Structure instance of bmi160_dev.
  141. *
  142. * @return Result of API execution status
  143. * @retval zero -> Success / -ve value -> Error.
  144. */
  145. static int8_t
  146. set_accel_step_detect_int(struct bmi160_int_settg* int_config, const struct bmi160_dev* dev);
  147. /*!
  148. * @brief This API sets the orientation interrupt of the sensor.This
  149. * interrupt occurs when there is orientation change in the sensor
  150. * with respect to gravitational field vector g.
  151. *
  152. * @param[in] int_config : Structure instance of bmi160_int_settg.
  153. * @param[in] dev : Structure instance of bmi160_dev.
  154. *
  155. * @return Result of API execution status
  156. * @retval zero -> Success / -ve value -> Error.
  157. */
  158. static int8_t
  159. set_accel_orientation_int(struct bmi160_int_settg* int_config, const struct bmi160_dev* dev);
  160. /*!
  161. * @brief This API sets the flat interrupt of the sensor.This interrupt
  162. * occurs in case of flat orientation
  163. *
  164. * @param[in] int_config : Structure instance of bmi160_int_settg.
  165. * @param[in] dev : Structure instance of bmi160_dev.
  166. *
  167. * @return Result of API execution status
  168. * @retval zero -> Success / -ve value -> Error.
  169. */
  170. static int8_t
  171. set_accel_flat_detect_int(struct bmi160_int_settg* int_config, const struct bmi160_dev* dev);
  172. /*!
  173. * @brief This API sets the low-g interrupt of the sensor.This interrupt
  174. * occurs during free-fall.
  175. *
  176. * @param[in] int_config : Structure instance of bmi160_int_settg.
  177. * @param[in] dev : Structure instance of bmi160_dev.
  178. *
  179. * @return Result of API execution status
  180. * @retval zero -> Success / -ve value -> Error.
  181. */
  182. static int8_t
  183. set_accel_low_g_int(struct bmi160_int_settg* int_config, const struct bmi160_dev* dev);
  184. /*!
  185. * @brief This API sets the high-g interrupt of the sensor.The interrupt
  186. * occurs if the absolute value of acceleration data of any enabled axis
  187. * exceeds the programmed threshold and the sign of the value does not
  188. * change for a preset duration.
  189. *
  190. * @param[in] int_config : Structure instance of bmi160_int_settg.
  191. * @param[in] dev : Structure instance of bmi160_dev.
  192. *
  193. * @return Result of API execution status
  194. * @retval zero -> Success / -ve value -> Error.
  195. */
  196. static int8_t
  197. set_accel_high_g_int(struct bmi160_int_settg* int_config, const struct bmi160_dev* dev);
  198. /*!
  199. * @brief This API sets the default configuration parameters of accel & gyro.
  200. * Also maintain the previous state of configurations.
  201. *
  202. * @param[in] dev : Structure instance of bmi160_dev.
  203. *
  204. * @return Result of API execution status
  205. * @retval zero -> Success / -ve value -> Error.
  206. */
  207. static void default_param_settg(struct bmi160_dev* dev);
  208. /*!
  209. * @brief This API is used to validate the device structure pointer for
  210. * null conditions.
  211. *
  212. * @param[in] dev : Structure instance of bmi160_dev.
  213. *
  214. * @return Result of API execution status
  215. * @retval zero -> Success / -ve value -> Error.
  216. */
  217. static int8_t null_ptr_check(const struct bmi160_dev* dev);
  218. /*!
  219. * @brief This API set the accel configuration.
  220. *
  221. * @param[in] dev : Structure instance of bmi160_dev.
  222. *
  223. * @return Result of API execution status
  224. * @retval zero -> Success / -ve value -> Error.
  225. */
  226. static int8_t set_accel_conf(struct bmi160_dev* dev);
  227. /*!
  228. * @brief This API gets the accel configuration.
  229. *
  230. * @param[out] dev : Structure instance of bmi160_dev.
  231. *
  232. * @return Result of API execution status
  233. * @retval zero -> Success / -ve value -> Error.
  234. */
  235. static int8_t get_accel_conf(struct bmi160_dev* dev);
  236. /*!
  237. * @brief This API check the accel configuration.
  238. *
  239. * @param[in] data : Pointer to store the updated accel config.
  240. * @param[in] dev : Structure instance of bmi160_dev.
  241. *
  242. * @return Result of API execution status
  243. * @retval zero -> Success / -ve value -> Error.
  244. */
  245. static int8_t check_accel_config(uint8_t* data, const struct bmi160_dev* dev);
  246. /*!
  247. * @brief This API process the accel odr.
  248. *
  249. * @param[in] dev : Structure instance of bmi160_dev.
  250. *
  251. * @return Result of API execution status
  252. * @retval zero -> Success / -ve value -> Error.
  253. */
  254. static int8_t process_accel_odr(uint8_t* data, const struct bmi160_dev* dev);
  255. /*!
  256. * @brief This API process the accel bandwidth.
  257. *
  258. * @param[in] dev : Structure instance of bmi160_dev.
  259. *
  260. * @return Result of API execution status
  261. * @retval zero -> Success / -ve value -> Error.
  262. */
  263. static int8_t process_accel_bw(uint8_t* data, const struct bmi160_dev* dev);
  264. /*!
  265. * @brief This API process the accel range.
  266. *
  267. * @param[in] dev : Structure instance of bmi160_dev.
  268. *
  269. * @return Result of API execution status
  270. * @retval zero -> Success / -ve value -> Error.
  271. */
  272. static int8_t process_accel_range(uint8_t* data, const struct bmi160_dev* dev);
  273. /*!
  274. * @brief This API checks the invalid settings for ODR & Bw for Accel and Gyro.
  275. * @param[in] dev : Structure instance of bmi160_dev.
  276. *
  277. * @return Result of API execution status
  278. * @retval zero -> Success / -ve value -> Error.
  279. */
  280. static int8_t check_invalid_settg(const struct bmi160_dev* dev);
  281. /*!
  282. * @brief This API set the gyro configuration.
  283. *
  284. * @param[in] dev : Structure instance of bmi160_dev.
  285. *
  286. * @return Result of API execution status
  287. * @retval zero -> Success / -ve value -> Error.
  288. */
  289. static int8_t set_gyro_conf(struct bmi160_dev* dev);
  290. /*!
  291. * @brief This API get the gyro configuration.
  292. *
  293. * @param[out] dev : Structure instance of bmi160_dev.
  294. *
  295. * @return Result of API execution status
  296. * @retval zero -> Success / -ve value -> Error.
  297. */
  298. static int8_t get_gyro_conf(struct bmi160_dev* dev);
  299. /*!
  300. * @brief This API check the gyro configuration.
  301. *
  302. * @param[in] data : Pointer to store the updated gyro config.
  303. * @param[in] dev : Structure instance of bmi160_dev.
  304. *
  305. * @return Result of API execution status
  306. * @retval zero -> Success / -ve value -> Error.
  307. */
  308. static int8_t check_gyro_config(uint8_t* data, const struct bmi160_dev* dev);
  309. /*!
  310. * @brief This API process the gyro odr.
  311. *
  312. * @param[in] dev : Structure instance of bmi160_dev.
  313. *
  314. * @return Result of API execution status
  315. * @retval zero -> Success / -ve value -> Error.
  316. */
  317. static int8_t process_gyro_odr(uint8_t* data, const struct bmi160_dev* dev);
  318. /*!
  319. * @brief This API process the gyro bandwidth.
  320. *
  321. * @param[in] dev : Structure instance of bmi160_dev.
  322. *
  323. * @return Result of API execution status
  324. * @retval zero -> Success / -ve value -> Error.
  325. */
  326. static int8_t process_gyro_bw(uint8_t* data, const struct bmi160_dev* dev);
  327. /*!
  328. * @brief This API process the gyro range.
  329. *
  330. * @param[in] dev : Structure instance of bmi160_dev.
  331. *
  332. * @return Result of API execution status
  333. * @retval zero -> Success / -ve value -> Error.
  334. */
  335. static int8_t process_gyro_range(uint8_t* data, const struct bmi160_dev* dev);
  336. /*!
  337. * @brief This API sets the accel power mode.
  338. *
  339. * @param[in] dev : Structure instance of bmi160_dev.
  340. *
  341. * @return Result of API execution status
  342. * @retval zero -> Success / -ve value -> Error.
  343. */
  344. static int8_t set_accel_pwr(struct bmi160_dev* dev);
  345. /*!
  346. * @brief This API process the undersampling setting of Accel.
  347. *
  348. * @param[in] dev : Structure instance of bmi160_dev.
  349. *
  350. * @return Result of API execution status
  351. * @retval zero -> Success / -ve value -> Error.
  352. */
  353. static int8_t process_under_sampling(uint8_t* data, const struct bmi160_dev* dev);
  354. /*!
  355. * @brief This API sets the gyro power mode.
  356. *
  357. * @param[in] dev : Structure instance of bmi160_dev.
  358. *
  359. * @return Result of API execution status
  360. * @retval zero -> Success / -ve value -> Error.
  361. */
  362. static int8_t set_gyro_pwr(struct bmi160_dev* dev);
  363. /*!
  364. * @brief This API reads accel data along with sensor time if time is requested
  365. * by user. Kindly refer the user guide(README.md) for more info.
  366. *
  367. * @param[in] len : len to read no of bytes
  368. * @param[out] accel : Structure pointer to store accel data
  369. * @param[in] dev : Structure instance of bmi160_dev.
  370. *
  371. * @return Result of API execution status
  372. * @retval zero -> Success / -ve value -> Error
  373. */
  374. static int8_t
  375. get_accel_data(uint8_t len, struct bmi160_sensor_data* accel, const struct bmi160_dev* dev);
  376. /*!
  377. * @brief This API reads accel data along with sensor time if time is requested
  378. * by user. Kindly refer the user guide(README.md) for more info.
  379. *
  380. * @param[in] len : len to read no of bytes
  381. * @param[out] gyro : Structure pointer to store accel data
  382. * @param[in] dev : Structure instance of bmi160_dev.
  383. *
  384. * @return Result of API execution status
  385. * @retval zero -> Success / -ve value -> Error
  386. */
  387. static int8_t
  388. get_gyro_data(uint8_t len, struct bmi160_sensor_data* gyro, const struct bmi160_dev* dev);
  389. /*!
  390. * @brief This API reads accel and gyro data along with sensor time
  391. * if time is requested by user.
  392. * Kindly refer the user guide(README.md) for more info.
  393. *
  394. * @param[in] len : len to read no of bytes
  395. * @param[out] accel : Structure pointer to store accel data
  396. * @param[out] gyro : Structure pointer to store accel data
  397. * @param[in] dev : Structure instance of bmi160_dev.
  398. *
  399. * @return Result of API execution status
  400. * @retval zero -> Success / -ve value -> Error
  401. */
  402. static int8_t get_accel_gyro_data(
  403. uint8_t len,
  404. struct bmi160_sensor_data* accel,
  405. struct bmi160_sensor_data* gyro,
  406. const struct bmi160_dev* dev);
  407. /*!
  408. * @brief This API enables the any-motion interrupt for accel.
  409. *
  410. * @param[in] any_motion_int_cfg : Structure instance of
  411. * bmi160_acc_any_mot_int_cfg.
  412. * @param[in] dev : Structure instance of bmi160_dev.
  413. *
  414. * @return Result of API execution status
  415. * @retval zero -> Success / -ve value -> Error
  416. */
  417. static int8_t enable_accel_any_motion_int(
  418. const struct bmi160_acc_any_mot_int_cfg* any_motion_int_cfg,
  419. struct bmi160_dev* dev);
  420. /*!
  421. * @brief This API disable the sig-motion interrupt.
  422. *
  423. * @param[in] dev : Structure instance of bmi160_dev.
  424. *
  425. * @return Result of API execution status
  426. * @retval zero -> Success / -ve value -> Error
  427. */
  428. static int8_t disable_sig_motion_int(const struct bmi160_dev* dev);
  429. /*!
  430. * @brief This API configure the source of data(filter & pre-filter)
  431. * for any-motion interrupt.
  432. *
  433. * @param[in] any_motion_int_cfg : Structure instance of
  434. * bmi160_acc_any_mot_int_cfg.
  435. * @param[in] dev : Structure instance of bmi160_dev.
  436. *
  437. * @return Result of API execution status
  438. * @retval zero -> Success / -ve value -> Error
  439. */
  440. static int8_t config_any_motion_src(
  441. const struct bmi160_acc_any_mot_int_cfg* any_motion_int_cfg,
  442. const struct bmi160_dev* dev);
  443. /*!
  444. * @brief This API configure the duration and threshold of
  445. * any-motion interrupt.
  446. *
  447. * @param[in] any_motion_int_cfg : Structure instance of
  448. * bmi160_acc_any_mot_int_cfg.
  449. * @param[in] dev : Structure instance of bmi160_dev.
  450. *
  451. * @return Result of API execution status
  452. * @retval zero -> Success / -ve value -> Error
  453. */
  454. static int8_t config_any_dur_threshold(
  455. const struct bmi160_acc_any_mot_int_cfg* any_motion_int_cfg,
  456. const struct bmi160_dev* dev);
  457. /*!
  458. * @brief This API configure necessary setting of any-motion interrupt.
  459. *
  460. * @param[in] int_config : Structure instance of bmi160_int_settg.
  461. * @param[in] any_motion_int_cfg : Structure instance of
  462. * bmi160_acc_any_mot_int_cfg.
  463. * @param[in] dev : Structure instance of bmi160_dev.
  464. *
  465. * @return Result of API execution status
  466. * @retval zero -> Success / -ve value -> Error
  467. */
  468. static int8_t config_any_motion_int_settg(
  469. const struct bmi160_int_settg* int_config,
  470. const struct bmi160_acc_any_mot_int_cfg* any_motion_int_cfg,
  471. const struct bmi160_dev* dev);
  472. /*!
  473. * @brief This API enable the data ready interrupt.
  474. *
  475. * @param[in] dev : Structure instance of bmi160_dev.
  476. *
  477. * @return Result of API execution status
  478. * @retval zero -> Success / -ve value -> Error
  479. */
  480. static int8_t enable_data_ready_int(const struct bmi160_dev* dev);
  481. /*!
  482. * @brief This API enables the no motion/slow motion interrupt.
  483. *
  484. * @param[in] no_mot_int_cfg : Structure instance of
  485. * bmi160_acc_no_motion_int_cfg.
  486. * @param[in] dev : Structure instance of bmi160_dev.
  487. *
  488. * @return Result of API execution status
  489. * @retval zero -> Success / -ve value -> Error
  490. */
  491. static int8_t enable_no_motion_int(
  492. const struct bmi160_acc_no_motion_int_cfg* no_mot_int_cfg,
  493. const struct bmi160_dev* dev);
  494. /*!
  495. * @brief This API configure the interrupt PIN setting for
  496. * no motion/slow motion interrupt.
  497. *
  498. * @param[in] int_config : structure instance of bmi160_int_settg.
  499. * @param[in] no_mot_int_cfg : Structure instance of
  500. * bmi160_acc_no_motion_int_cfg.
  501. * @param[in] dev : Structure instance of bmi160_dev.
  502. *
  503. * @return Result of API execution status
  504. * @retval zero -> Success / -ve value -> Error
  505. */
  506. static int8_t config_no_motion_int_settg(
  507. const struct bmi160_int_settg* int_config,
  508. const struct bmi160_acc_no_motion_int_cfg* no_mot_int_cfg,
  509. const struct bmi160_dev* dev);
  510. /*!
  511. * @brief This API configure the source of interrupt for no motion.
  512. *
  513. * @param[in] no_mot_int_cfg : Structure instance of
  514. * bmi160_acc_no_motion_int_cfg.
  515. * @param[in] dev : Structure instance of bmi160_dev.
  516. *
  517. * @return Result of API execution status
  518. * @retval zero -> Success / -ve value -> Error
  519. */
  520. static int8_t config_no_motion_data_src(
  521. const struct bmi160_acc_no_motion_int_cfg* no_mot_int_cfg,
  522. const struct bmi160_dev* dev);
  523. /*!
  524. * @brief This API configure the duration and threshold of
  525. * no motion/slow motion interrupt along with selection of no/slow motion.
  526. *
  527. * @param[in] no_mot_int_cfg : Structure instance of
  528. * bmi160_acc_no_motion_int_cfg.
  529. * @param[in] dev : Structure instance of bmi160_dev.
  530. *
  531. * @return Result of API execution status
  532. * @retval zero -> Success / -ve value -> Error
  533. */
  534. static int8_t config_no_motion_dur_thr(
  535. const struct bmi160_acc_no_motion_int_cfg* no_mot_int_cfg,
  536. const struct bmi160_dev* dev);
  537. /*!
  538. * @brief This API enables the sig-motion motion interrupt.
  539. *
  540. * @param[in] sig_mot_int_cfg : Structure instance of
  541. * bmi160_acc_sig_mot_int_cfg.
  542. * @param[in] dev : Structure instance of bmi160_dev.
  543. *
  544. * @return Result of API execution status
  545. * @retval zero -> Success / -ve value -> Error
  546. */
  547. static int8_t enable_sig_motion_int(
  548. const struct bmi160_acc_sig_mot_int_cfg* sig_mot_int_cfg,
  549. struct bmi160_dev* dev);
  550. /*!
  551. * @brief This API configure the interrupt PIN setting for
  552. * significant motion interrupt.
  553. *
  554. * @param[in] int_config : Structure instance of bmi160_int_settg.
  555. * @param[in] sig_mot_int_cfg : Structure instance of
  556. * bmi160_acc_sig_mot_int_cfg.
  557. * @param[in] dev : Structure instance of bmi160_dev.
  558. *
  559. * @return Result of API execution status
  560. * @retval zero -> Success / -ve value -> Error
  561. */
  562. static int8_t config_sig_motion_int_settg(
  563. const struct bmi160_int_settg* int_config,
  564. const struct bmi160_acc_sig_mot_int_cfg* sig_mot_int_cfg,
  565. const struct bmi160_dev* dev);
  566. /*!
  567. * @brief This API configure the source of data(filter & pre-filter)
  568. * for sig motion interrupt.
  569. *
  570. * @param[in] sig_mot_int_cfg : Structure instance of
  571. * bmi160_acc_sig_mot_int_cfg.
  572. * @param[in] dev : Structure instance of bmi160_dev.
  573. *
  574. * @return Result of API execution status
  575. * @retval zero -> Success / -ve value -> Error
  576. */
  577. static int8_t config_sig_motion_data_src(
  578. const struct bmi160_acc_sig_mot_int_cfg* sig_mot_int_cfg,
  579. const struct bmi160_dev* dev);
  580. /*!
  581. * @brief This API configure the threshold, skip and proof time of
  582. * sig motion interrupt.
  583. *
  584. * @param[in] sig_mot_int_cfg : Structure instance of
  585. * bmi160_acc_sig_mot_int_cfg.
  586. * @param[in] dev : Structure instance of bmi160_dev.
  587. *
  588. * @return Result of API execution status
  589. * @retval zero -> Success / -ve value -> Error
  590. */
  591. static int8_t config_sig_dur_threshold(
  592. const struct bmi160_acc_sig_mot_int_cfg* sig_mot_int_cfg,
  593. const struct bmi160_dev* dev);
  594. /*!
  595. * @brief This API enables the step detector interrupt.
  596. *
  597. * @param[in] step_detect_int_cfg : Structure instance of
  598. * bmi160_acc_step_detect_int_cfg.
  599. * @param[in] dev : Structure instance of bmi160_dev.
  600. *
  601. * @return Result of API execution status
  602. * @retval zero -> Success / -ve value -> Error
  603. */
  604. static int8_t enable_step_detect_int(
  605. const struct bmi160_acc_step_detect_int_cfg* step_detect_int_cfg,
  606. const struct bmi160_dev* dev);
  607. /*!
  608. * @brief This API configure the step detector parameter.
  609. *
  610. * @param[in] step_detect_int_cfg : Structure instance of
  611. * bmi160_acc_step_detect_int_cfg.
  612. * @param[in] dev : Structure instance of bmi160_dev.
  613. *
  614. * @return Result of API execution status
  615. * @retval zero -> Success / -ve value -> Error
  616. */
  617. static int8_t config_step_detect(
  618. const struct bmi160_acc_step_detect_int_cfg* step_detect_int_cfg,
  619. const struct bmi160_dev* dev);
  620. /*!
  621. * @brief This API enables the single/double tap interrupt.
  622. *
  623. * @param[in] int_config : Structure instance of bmi160_int_settg.
  624. * @param[in] dev : Structure instance of bmi160_dev.
  625. *
  626. * @return Result of API execution status
  627. * @retval zero -> Success / -ve value -> Error
  628. */
  629. static int8_t enable_tap_int(
  630. const struct bmi160_int_settg* int_config,
  631. const struct bmi160_acc_tap_int_cfg* tap_int_cfg,
  632. const struct bmi160_dev* dev);
  633. /*!
  634. * @brief This API configure the interrupt PIN setting for
  635. * tap interrupt.
  636. *
  637. * @param[in] int_config : Structure instance of bmi160_int_settg.
  638. * @param[in] tap_int_cfg : Structure instance of bmi160_acc_tap_int_cfg.
  639. * @param[in] dev : Structure instance of bmi160_dev.
  640. *
  641. * @return Result of API execution status
  642. * @retval zero -> Success / -ve value -> Error
  643. */
  644. static int8_t config_tap_int_settg(
  645. const struct bmi160_int_settg* int_config,
  646. const struct bmi160_acc_tap_int_cfg* tap_int_cfg,
  647. const struct bmi160_dev* dev);
  648. /*!
  649. * @brief This API configure the source of data(filter & pre-filter)
  650. * for tap interrupt.
  651. *
  652. * @param[in] tap_int_cfg : Structure instance of bmi160_acc_tap_int_cfg.
  653. * @param[in] dev : Structure instance of bmi160_dev.
  654. *
  655. * @return Result of API execution status
  656. * @retval zero -> Success / -ve value -> Error
  657. */
  658. static int8_t config_tap_data_src(
  659. const struct bmi160_acc_tap_int_cfg* tap_int_cfg,
  660. const struct bmi160_dev* dev);
  661. /*!
  662. * @brief This API configure the parameters of tap interrupt.
  663. * Threshold, quite, shock, and duration.
  664. *
  665. * @param[in] int_config : Structure instance of bmi160_int_settg.
  666. * @param[in] tap_int_cfg : Structure instance of bmi160_acc_tap_int_cfg.
  667. * @param[in] dev : structure instance of bmi160_dev.
  668. *
  669. * @return Result of API execution status
  670. * @retval zero -> Success / -ve value -> Error
  671. */
  672. static int8_t config_tap_param(
  673. const struct bmi160_int_settg* int_config,
  674. const struct bmi160_acc_tap_int_cfg* tap_int_cfg,
  675. const struct bmi160_dev* dev);
  676. /*!
  677. * @brief This API enable the external mode configuration.
  678. *
  679. * @param[in] dev : Structure instance of bmi160_dev.
  680. *
  681. * @return Result of API execution status
  682. * @retval zero -> Success / -ve value -> Error
  683. */
  684. static int8_t config_sec_if(const struct bmi160_dev* dev);
  685. /*!
  686. * @brief This API configure the ODR of the auxiliary sensor.
  687. *
  688. * @param[in] dev : Structure instance of bmi160_dev.
  689. *
  690. * @return Result of API execution status
  691. * @retval zero -> Success / -ve value -> Error
  692. */
  693. static int8_t config_aux_odr(const struct bmi160_dev* dev);
  694. /*!
  695. * @brief This API maps the actual burst read length set by user.
  696. *
  697. * @param[in] len : Pointer to store the read length.
  698. * @param[in] dev : Structure instance of bmi160_dev.
  699. *
  700. * @return Result of API execution status
  701. * @retval zero -> Success / -ve value -> Error
  702. */
  703. static int8_t map_read_len(uint16_t* len, const struct bmi160_dev* dev);
  704. /*!
  705. * @brief This API configure the settings of auxiliary sensor.
  706. *
  707. * @param[in] dev : Structure instance of bmi160_dev.
  708. *
  709. * @return Result of API execution status
  710. * @retval zero -> Success / -ve value -> Error
  711. */
  712. static int8_t config_aux_settg(const struct bmi160_dev* dev);
  713. /*!
  714. * @brief This API extract the read data from auxiliary sensor.
  715. *
  716. * @param[in] map_len : burst read value.
  717. * @param[in] reg_addr : Address of register to read.
  718. * @param[in] aux_data : Pointer to store the read data.
  719. * @param[in] len : length to read the data.
  720. * @param[in] dev : Structure instance of bmi160_dev.
  721. * @note : Refer user guide for detailed info.
  722. *
  723. * @return Result of API execution status
  724. * @retval zero -> Success / -ve value -> Error
  725. */
  726. static int8_t extract_aux_read(
  727. uint16_t map_len,
  728. uint8_t reg_addr,
  729. uint8_t* aux_data,
  730. uint16_t len,
  731. const struct bmi160_dev* dev);
  732. /*!
  733. * @brief This API enables the orient interrupt.
  734. *
  735. * @param[in] orient_int_cfg : Structure instance of bmi160_acc_orient_int_cfg.
  736. * @param[in] dev : Structure instance of bmi160_dev.
  737. *
  738. * @return Result of API execution status
  739. * @retval zero -> Success / -ve value -> Error
  740. */
  741. static int8_t enable_orient_int(
  742. const struct bmi160_acc_orient_int_cfg* orient_int_cfg,
  743. const struct bmi160_dev* dev);
  744. /*!
  745. * @brief This API configure the necessary setting of orientation interrupt.
  746. *
  747. * @param[in] orient_int_cfg : Structure instance of bmi160_acc_orient_int_cfg.
  748. * @param[in] dev : structure instance of bmi160_dev.
  749. *
  750. * @return Result of API execution status
  751. * @retval zero -> Success / -ve value -> Error
  752. */
  753. static int8_t config_orient_int_settg(
  754. const struct bmi160_acc_orient_int_cfg* orient_int_cfg,
  755. const struct bmi160_dev* dev);
  756. /*!
  757. * @brief This API enables the flat interrupt.
  758. *
  759. * @param[in] flat_int : Structure instance of bmi160_acc_flat_detect_int_cfg.
  760. * @param[in] dev : structure instance of bmi160_dev.
  761. *
  762. * @return Result of API execution status
  763. * @retval zero -> Success / -ve value -> Error
  764. */
  765. static int8_t enable_flat_int(
  766. const struct bmi160_acc_flat_detect_int_cfg* flat_int,
  767. const struct bmi160_dev* dev);
  768. /*!
  769. * @brief This API configure the necessary setting of flat interrupt.
  770. *
  771. * @param[in] flat_int : Structure instance of bmi160_acc_flat_detect_int_cfg.
  772. * @param[in] dev : structure instance of bmi160_dev.
  773. *
  774. * @return Result of API execution status
  775. * @retval zero -> Success / -ve value -> Error
  776. */
  777. static int8_t config_flat_int_settg(
  778. const struct bmi160_acc_flat_detect_int_cfg* flat_int,
  779. const struct bmi160_dev* dev);
  780. /*!
  781. * @brief This API enables the Low-g interrupt.
  782. *
  783. * @param[in] low_g_int : Structure instance of bmi160_acc_low_g_int_cfg.
  784. * @param[in] dev : structure instance of bmi160_dev.
  785. *
  786. * @return Result of API execution status
  787. * @retval zero -> Success / -ve value -> Error
  788. */
  789. static int8_t enable_low_g_int(
  790. const struct bmi160_acc_low_g_int_cfg* low_g_int,
  791. const struct bmi160_dev* dev);
  792. /*!
  793. * @brief This API configure the source of data(filter & pre-filter) for low-g interrupt.
  794. *
  795. * @param[in] low_g_int : Structure instance of bmi160_acc_low_g_int_cfg.
  796. * @param[in] dev : structure instance of bmi160_dev.
  797. *
  798. * @return Result of API execution status
  799. * @retval zero -> Success / -ve value -> Error
  800. */
  801. static int8_t config_low_g_data_src(
  802. const struct bmi160_acc_low_g_int_cfg* low_g_int,
  803. const struct bmi160_dev* dev);
  804. /*!
  805. * @brief This API configure the necessary setting of low-g interrupt.
  806. *
  807. * @param[in] low_g_int : Structure instance of bmi160_acc_low_g_int_cfg.
  808. * @param[in] dev : structure instance of bmi160_dev.
  809. *
  810. * @return Result of API execution status
  811. * @retval zero -> Success / -ve value -> Error
  812. */
  813. static int8_t config_low_g_int_settg(
  814. const struct bmi160_acc_low_g_int_cfg* low_g_int,
  815. const struct bmi160_dev* dev);
  816. /*!
  817. * @brief This API enables the high-g interrupt.
  818. *
  819. * @param[in] high_g_int_cfg : Structure instance of bmi160_acc_high_g_int_cfg.
  820. * @param[in] dev : structure instance of bmi160_dev.
  821. *
  822. * @return Result of API execution status
  823. * @retval zero -> Success / -ve value -> Error
  824. */
  825. static int8_t enable_high_g_int(
  826. const struct bmi160_acc_high_g_int_cfg* high_g_int_cfg,
  827. const struct bmi160_dev* dev);
  828. /*!
  829. * @brief This API configure the source of data(filter & pre-filter)
  830. * for high-g interrupt.
  831. *
  832. * @param[in] high_g_int_cfg : Structure instance of bmi160_acc_high_g_int_cfg.
  833. * @param[in] dev : structure instance of bmi160_dev.
  834. *
  835. * @return Result of API execution status
  836. * @retval zero -> Success / -ve value -> Error
  837. */
  838. static int8_t config_high_g_data_src(
  839. const struct bmi160_acc_high_g_int_cfg* high_g_int_cfg,
  840. const struct bmi160_dev* dev);
  841. /*!
  842. * @brief This API configure the necessary setting of high-g interrupt.
  843. *
  844. * @param[in] high_g_int_cfg : Structure instance of bmi160_acc_high_g_int_cfg.
  845. * @param[in] dev : structure instance of bmi160_dev.
  846. *
  847. * @return Result of API execution status
  848. * @retval zero -> Success / -ve value -> Error
  849. */
  850. static int8_t config_high_g_int_settg(
  851. const struct bmi160_acc_high_g_int_cfg* high_g_int_cfg,
  852. const struct bmi160_dev* dev);
  853. /*!
  854. * @brief This API configure the behavioural setting of interrupt pin.
  855. *
  856. * @param[in] int_config : Structure instance of bmi160_int_settg.
  857. * @param[in] dev : structure instance of bmi160_dev.
  858. *
  859. * @return Result of API execution status
  860. * @retval zero -> Success / -ve value -> Error
  861. */
  862. static int8_t
  863. config_int_out_ctrl(const struct bmi160_int_settg* int_config, const struct bmi160_dev* dev);
  864. /*!
  865. * @brief This API configure the mode(input enable, latch or non-latch) of interrupt pin.
  866. *
  867. * @param[in] int_config : Structure instance of bmi160_int_settg.
  868. * @param[in] dev : structure instance of bmi160_dev.
  869. *
  870. * @return Result of API execution status
  871. * @retval zero -> Success / -ve value -> Error
  872. */
  873. static int8_t
  874. config_int_latch(const struct bmi160_int_settg* int_config, const struct bmi160_dev* dev);
  875. /*!
  876. * @brief This API performs the self test for accelerometer of BMI160
  877. *
  878. * @param[in] dev : structure instance of bmi160_dev.
  879. *
  880. * @return Result of API execution status
  881. * @retval zero -> Success / -ve value -> Error
  882. */
  883. static int8_t perform_accel_self_test(struct bmi160_dev* dev);
  884. /*!
  885. * @brief This API enables to perform the accel self test by setting proper
  886. * configurations to facilitate accel self test
  887. *
  888. * @param[in] dev : structure instance of bmi160_dev.
  889. *
  890. * @return Result of API execution status
  891. * @retval zero -> Success / -ve value -> Error
  892. */
  893. static int8_t enable_accel_self_test(struct bmi160_dev* dev);
  894. /*!
  895. * @brief This API performs accel self test with positive excitation
  896. *
  897. * @param[in] accel_pos : Structure pointer to store accel data
  898. * for positive excitation
  899. * @param[in] dev : structure instance of bmi160_dev
  900. *
  901. * @return Result of API execution status
  902. * @retval zero -> Success / -ve value -> Error
  903. */
  904. static int8_t accel_self_test_positive_excitation(
  905. struct bmi160_sensor_data* accel_pos,
  906. const struct bmi160_dev* dev);
  907. /*!
  908. * @brief This API performs accel self test with negative excitation
  909. *
  910. * @param[in] accel_neg : Structure pointer to store accel data
  911. * for negative excitation
  912. * @param[in] dev : structure instance of bmi160_dev
  913. *
  914. * @return Result of API execution status
  915. * @retval zero -> Success / -ve value -> Error
  916. */
  917. static int8_t accel_self_test_negative_excitation(
  918. struct bmi160_sensor_data* accel_neg,
  919. const struct bmi160_dev* dev);
  920. /*!
  921. * @brief This API validates the accel self test results
  922. *
  923. * @param[in] accel_pos : Structure pointer to store accel data
  924. * for positive excitation
  925. * @param[in] accel_neg : Structure pointer to store accel data
  926. * for negative excitation
  927. *
  928. * @return Result of API execution status
  929. * @retval zero -> Success / -ve value -> Error / +ve value -> Self test fail
  930. */
  931. static int8_t validate_accel_self_test(
  932. const struct bmi160_sensor_data* accel_pos,
  933. const struct bmi160_sensor_data* accel_neg);
  934. /*!
  935. * @brief This API performs the self test for gyroscope of BMI160
  936. *
  937. * @param[in] dev : structure instance of bmi160_dev.
  938. *
  939. * @return Result of API execution status
  940. * @retval zero -> Success / -ve value -> Error
  941. */
  942. static int8_t perform_gyro_self_test(const struct bmi160_dev* dev);
  943. /*!
  944. * @brief This API enables the self test bit to trigger self test for gyro
  945. *
  946. * @param[in] dev : structure instance of bmi160_dev.
  947. *
  948. * @return Result of API execution status
  949. * @retval zero -> Success / -ve value -> Error
  950. */
  951. static int8_t enable_gyro_self_test(const struct bmi160_dev* dev);
  952. /*!
  953. * @brief This API validates the self test results of gyro
  954. *
  955. * @param[in] dev : structure instance of bmi160_dev.
  956. *
  957. * @return Result of API execution status
  958. * @retval zero -> Success / -ve value -> Error
  959. */
  960. static int8_t validate_gyro_self_test(const struct bmi160_dev* dev);
  961. /*!
  962. * @brief This API sets FIFO full interrupt of the sensor.This interrupt
  963. * occurs when the FIFO is full and the next full data sample would cause
  964. * a FIFO overflow, which may delete the old samples.
  965. *
  966. * @param[in] int_config : Structure instance of bmi160_int_settg.
  967. * @param[in] dev : structure instance of bmi160_dev.
  968. *
  969. * @return Result of API execution status
  970. * @retval zero -> Success / -ve value -> Error
  971. */
  972. static int8_t
  973. set_fifo_full_int(const struct bmi160_int_settg* int_config, const struct bmi160_dev* dev);
  974. /*!
  975. * @brief This enable the FIFO full interrupt engine.
  976. *
  977. * @param[in] int_config : Structure instance of bmi160_int_settg.
  978. * @param[in] dev : structure instance of bmi160_dev.
  979. *
  980. * @return Result of API execution status
  981. * @retval zero -> Success / -ve value -> Error
  982. */
  983. static int8_t
  984. enable_fifo_full_int(const struct bmi160_int_settg* int_config, const struct bmi160_dev* dev);
  985. /*!
  986. * @brief This API sets FIFO watermark interrupt of the sensor.The FIFO
  987. * watermark interrupt is fired, when the FIFO fill level is above a fifo
  988. * watermark.
  989. *
  990. * @param[in] int_config : Structure instance of bmi160_int_settg.
  991. * @param[in] dev : structure instance of bmi160_dev.
  992. *
  993. * @return Result of API execution status
  994. * @retval zero -> Success / -ve value -> Error
  995. */
  996. static int8_t
  997. set_fifo_watermark_int(const struct bmi160_int_settg* int_config, const struct bmi160_dev* dev);
  998. /*!
  999. * @brief This enable the FIFO watermark interrupt engine.
  1000. *
  1001. * @param[in] int_config : Structure instance of bmi160_int_settg.
  1002. * @param[in] dev : structure instance of bmi160_dev.
  1003. *
  1004. * @return Result of API execution status
  1005. * @retval zero -> Success / -ve value -> Error
  1006. */
  1007. static int8_t
  1008. enable_fifo_wtm_int(const struct bmi160_int_settg* int_config, const struct bmi160_dev* dev);
  1009. /*!
  1010. * @brief This API is used to reset the FIFO related configurations
  1011. * in the fifo_frame structure.
  1012. *
  1013. * @param[in] dev : structure instance of bmi160_dev.
  1014. *
  1015. * @return Result of API execution status
  1016. * @retval zero -> Success / -ve value -> Error
  1017. */
  1018. static void reset_fifo_data_structure(const struct bmi160_dev* dev);
  1019. /*!
  1020. * @brief This API is used to read number of bytes filled
  1021. * currently in FIFO buffer.
  1022. *
  1023. * @param[in] bytes_to_read : Number of bytes available in FIFO at the
  1024. * instant which is obtained from FIFO counter.
  1025. * @param[in] dev : Structure instance of bmi160_dev.
  1026. *
  1027. * @return Result of API execution status
  1028. * @retval zero -> Success / -ve value -> Error.
  1029. * @retval Any non zero value -> Fail
  1030. *
  1031. */
  1032. static int8_t get_fifo_byte_counter(uint16_t* bytes_to_read, struct bmi160_dev const* dev);
  1033. /*!
  1034. * @brief This API is used to compute the number of bytes of accel FIFO data
  1035. * which is to be parsed in header-less mode
  1036. *
  1037. * @param[out] data_index : The start index for parsing data
  1038. * @param[out] data_read_length : Number of bytes to be parsed
  1039. * @param[in] acc_frame_count : Number of accelerometer frames to be read
  1040. * @param[in] dev : Structure instance of bmi160_dev.
  1041. *
  1042. */
  1043. static void get_accel_len_to_parse(
  1044. uint16_t* data_index,
  1045. uint16_t* data_read_length,
  1046. const uint8_t* acc_frame_count,
  1047. const struct bmi160_dev* dev);
  1048. /*!
  1049. * @brief This API is used to parse the accelerometer data from the
  1050. * FIFO data in both header mode and header-less mode.
  1051. * It updates the idx value which is used to store the index of
  1052. * the current data byte which is parsed.
  1053. *
  1054. * @param[in,out] acc : structure instance of sensor data
  1055. * @param[in,out] idx : Index value of number of bytes parsed
  1056. * @param[in,out] acc_idx : Index value of accelerometer data
  1057. * (x,y,z axes) frames parsed
  1058. * @param[in] frame_info : It consists of either fifo_data_enable
  1059. * parameter in header-less mode or
  1060. * frame header data in header mode
  1061. * @param[in] dev : structure instance of bmi160_dev.
  1062. *
  1063. * @return Result of API execution status
  1064. * @retval zero -> Success / -ve value -> Error
  1065. */
  1066. static void unpack_accel_frame(
  1067. struct bmi160_sensor_data* acc,
  1068. uint16_t* idx,
  1069. uint8_t* acc_idx,
  1070. uint8_t frame_info,
  1071. const struct bmi160_dev* dev);
  1072. /*!
  1073. * @brief This API is used to parse the accelerometer data from the
  1074. * FIFO data and store it in the instance of the structure bmi160_sensor_data.
  1075. *
  1076. * @param[in,out] accel_data : structure instance of sensor data
  1077. * @param[in,out] data_start_index : Index value of number of bytes parsed
  1078. * @param[in] dev : structure instance of bmi160_dev.
  1079. *
  1080. * @return Result of API execution status
  1081. * @retval zero -> Success / -ve value -> Error
  1082. */
  1083. static void unpack_accel_data(
  1084. struct bmi160_sensor_data* accel_data,
  1085. uint16_t data_start_index,
  1086. const struct bmi160_dev* dev);
  1087. /*!
  1088. * @brief This API is used to parse the accelerometer data from the
  1089. * FIFO data in header mode.
  1090. *
  1091. * @param[in,out] accel_data : Structure instance of sensor data
  1092. * @param[in,out] accel_length : Number of accelerometer frames
  1093. * @param[in] dev : Structure instance of bmi160_dev.
  1094. *
  1095. * @return Result of API execution status
  1096. * @retval zero -> Success / -ve value -> Error
  1097. */
  1098. static void extract_accel_header_mode(
  1099. struct bmi160_sensor_data* accel_data,
  1100. uint8_t* accel_length,
  1101. const struct bmi160_dev* dev);
  1102. /*!
  1103. * @brief This API computes the number of bytes of gyro FIFO data
  1104. * which is to be parsed in header-less mode
  1105. *
  1106. * @param[out] data_index : The start index for parsing data
  1107. * @param[out] data_read_length : No of bytes to be parsed from FIFO buffer
  1108. * @param[in] gyro_frame_count : Number of Gyro data frames to be read
  1109. * @param[in] dev : Structure instance of bmi160_dev.
  1110. */
  1111. static void get_gyro_len_to_parse(
  1112. uint16_t* data_index,
  1113. uint16_t* data_read_length,
  1114. const uint8_t* gyro_frame_count,
  1115. const struct bmi160_dev* dev);
  1116. /*!
  1117. * @brief This API is used to parse the gyroscope's data from the
  1118. * FIFO data in both header mode and header-less mode.
  1119. * It updates the idx value which is used to store the index of
  1120. * the current data byte which is parsed.
  1121. *
  1122. * @param[in,out] gyro : structure instance of sensor data
  1123. * @param[in,out] idx : Index value of number of bytes parsed
  1124. * @param[in,out] gyro_idx : Index value of gyro data
  1125. * (x,y,z axes) frames parsed
  1126. * @param[in] frame_info : It consists of either fifo_data_enable
  1127. * parameter in header-less mode or
  1128. * frame header data in header mode
  1129. * @param[in] dev : structure instance of bmi160_dev.
  1130. *
  1131. * @return Result of API execution status
  1132. * @retval zero -> Success / -ve value -> Error
  1133. */
  1134. static void unpack_gyro_frame(
  1135. struct bmi160_sensor_data* gyro,
  1136. uint16_t* idx,
  1137. uint8_t* gyro_idx,
  1138. uint8_t frame_info,
  1139. const struct bmi160_dev* dev);
  1140. /*!
  1141. * @brief This API is used to parse the gyro data from the
  1142. * FIFO data and store it in the instance of the structure bmi160_sensor_data.
  1143. *
  1144. * @param[in,out] gyro_data : structure instance of sensor data
  1145. * @param[in,out] data_start_index : Index value of number of bytes parsed
  1146. * @param[in] dev : structure instance of bmi160_dev.
  1147. *
  1148. * @return Result of API execution status
  1149. * @retval zero -> Success / -ve value -> Error
  1150. */
  1151. static void unpack_gyro_data(
  1152. struct bmi160_sensor_data* gyro_data,
  1153. uint16_t data_start_index,
  1154. const struct bmi160_dev* dev);
  1155. /*!
  1156. * @brief This API is used to parse the gyro data from the
  1157. * FIFO data in header mode.
  1158. *
  1159. * @param[in,out] gyro_data : Structure instance of sensor data
  1160. * @param[in,out] gyro_length : Number of gyro frames
  1161. * @param[in] dev : Structure instance of bmi160_dev.
  1162. *
  1163. * @return Result of API execution status
  1164. * @retval zero -> Success / -ve value -> Error
  1165. */
  1166. static void extract_gyro_header_mode(
  1167. struct bmi160_sensor_data* gyro_data,
  1168. uint8_t* gyro_length,
  1169. const struct bmi160_dev* dev);
  1170. /*!
  1171. * @brief This API computes the number of bytes of aux FIFO data
  1172. * which is to be parsed in header-less mode
  1173. *
  1174. * @param[out] data_index : The start index for parsing data
  1175. * @param[out] data_read_length : No of bytes to be parsed from FIFO buffer
  1176. * @param[in] aux_frame_count : Number of Aux data frames to be read
  1177. * @param[in] dev : Structure instance of bmi160_dev.
  1178. */
  1179. static void get_aux_len_to_parse(
  1180. uint16_t* data_index,
  1181. uint16_t* data_read_length,
  1182. const uint8_t* aux_frame_count,
  1183. const struct bmi160_dev* dev);
  1184. /*!
  1185. * @brief This API is used to parse the aux's data from the
  1186. * FIFO data in both header mode and header-less mode.
  1187. * It updates the idx value which is used to store the index of
  1188. * the current data byte which is parsed
  1189. *
  1190. * @param[in,out] aux_data : structure instance of sensor data
  1191. * @param[in,out] idx : Index value of number of bytes parsed
  1192. * @param[in,out] aux_index : Index value of gyro data
  1193. * (x,y,z axes) frames parsed
  1194. * @param[in] frame_info : It consists of either fifo_data_enable
  1195. * parameter in header-less mode or
  1196. * frame header data in header mode
  1197. * @param[in] dev : structure instance of bmi160_dev.
  1198. *
  1199. * @return Result of API execution status
  1200. * @retval zero -> Success / -ve value -> Error
  1201. */
  1202. static void unpack_aux_frame(
  1203. struct bmi160_aux_data* aux_data,
  1204. uint16_t* idx,
  1205. uint8_t* aux_index,
  1206. uint8_t frame_info,
  1207. const struct bmi160_dev* dev);
  1208. /*!
  1209. * @brief This API is used to parse the aux data from the
  1210. * FIFO data and store it in the instance of the structure bmi160_aux_data.
  1211. *
  1212. * @param[in,out] aux_data : structure instance of sensor data
  1213. * @param[in,out] data_start_index : Index value of number of bytes parsed
  1214. * @param[in] dev : structure instance of bmi160_dev.
  1215. *
  1216. * @return Result of API execution status
  1217. * @retval zero -> Success / -ve value -> Error
  1218. */
  1219. static void unpack_aux_data(
  1220. struct bmi160_aux_data* aux_data,
  1221. uint16_t data_start_index,
  1222. const struct bmi160_dev* dev);
  1223. /*!
  1224. * @brief This API is used to parse the aux data from the
  1225. * FIFO data in header mode.
  1226. *
  1227. * @param[in,out] aux_data : Structure instance of sensor data
  1228. * @param[in,out] aux_length : Number of aux frames
  1229. * @param[in] dev : Structure instance of bmi160_dev.
  1230. *
  1231. * @return Result of API execution status
  1232. * @retval zero -> Success / -ve value -> Error
  1233. */
  1234. static void extract_aux_header_mode(
  1235. struct bmi160_aux_data* aux_data,
  1236. uint8_t* aux_length,
  1237. const struct bmi160_dev* dev);
  1238. /*!
  1239. * @brief This API checks the presence of non-valid frames in the read fifo data.
  1240. *
  1241. * @param[in,out] data_index : The index of the current data to
  1242. * be parsed from fifo data
  1243. * @param[in] dev : Structure instance of bmi160_dev.
  1244. *
  1245. * @return Result of API execution status
  1246. * @retval zero -> Success / -ve value -> Error
  1247. */
  1248. static void check_frame_validity(uint16_t* data_index, const struct bmi160_dev* dev);
  1249. /*!
  1250. * @brief This API is used to move the data index ahead of the
  1251. * current_frame_length parameter when unnecessary FIFO data appears while
  1252. * extracting the user specified data.
  1253. *
  1254. * @param[in,out] data_index : Index of the FIFO data which
  1255. * is to be moved ahead of the
  1256. * current_frame_length
  1257. * @param[in] current_frame_length : Number of bytes in a particular frame
  1258. * @param[in] dev : Structure instance of bmi160_dev.
  1259. *
  1260. * @return Result of API execution status
  1261. * @retval zero -> Success / -ve value -> Error
  1262. */
  1263. static void move_next_frame(
  1264. uint16_t* data_index,
  1265. uint8_t current_frame_length,
  1266. const struct bmi160_dev* dev);
  1267. /*!
  1268. * @brief This API is used to parse and store the sensor time from the
  1269. * FIFO data in the structure instance dev.
  1270. *
  1271. * @param[in,out] data_index : Index of the FIFO data which
  1272. * has the sensor time.
  1273. * @param[in] dev : Structure instance of bmi160_dev.
  1274. *
  1275. * @return Result of API execution status
  1276. * @retval zero -> Success / -ve value -> Error
  1277. */
  1278. static void unpack_sensortime_frame(uint16_t* data_index, const struct bmi160_dev* dev);
  1279. /*!
  1280. * @brief This API is used to parse and store the skipped_frame_count from
  1281. * the FIFO data in the structure instance dev.
  1282. *
  1283. * @param[in,out] data_index : Index of the FIFO data which
  1284. * has the skipped frame count.
  1285. * @param[in] dev : Structure instance of bmi160_dev.
  1286. *
  1287. * @return Result of API execution status
  1288. * @retval zero -> Success / -ve value -> Error
  1289. */
  1290. static void unpack_skipped_frame(uint16_t* data_index, const struct bmi160_dev* dev);
  1291. /*!
  1292. * @brief This API is used to get the FOC status from the sensor
  1293. *
  1294. * @param[in,out] foc_status : Result of FOC status.
  1295. * @param[in] dev : Structure instance of bmi160_dev.
  1296. *
  1297. * @return Result of API execution status
  1298. * @retval zero -> Success / -ve value -> Error
  1299. */
  1300. static int8_t get_foc_status(uint8_t* foc_status, struct bmi160_dev const* dev);
  1301. /*!
  1302. * @brief This API is used to configure the offset enable bits in the sensor
  1303. *
  1304. * @param[in,out] foc_conf : Structure instance of bmi160_foc_conf which
  1305. * has the FOC and offset configurations
  1306. * @param[in] dev : Structure instance of bmi160_dev.
  1307. *
  1308. * @return Result of API execution status
  1309. * @retval zero -> Success / -ve value -> Error
  1310. */
  1311. static int8_t
  1312. configure_offset_enable(const struct bmi160_foc_conf* foc_conf, struct bmi160_dev const* dev);
  1313. /*!
  1314. * @brief This API is used to trigger the FOC in the sensor
  1315. *
  1316. * @param[in,out] offset : Structure instance of bmi160_offsets which
  1317. * reads and stores the offset values after FOC
  1318. * @param[in] dev : Structure instance of bmi160_dev.
  1319. *
  1320. * @return Result of API execution status
  1321. * @retval zero -> Success / -ve value -> Error
  1322. */
  1323. static int8_t trigger_foc(struct bmi160_offsets* offset, struct bmi160_dev const* dev);
  1324. /*!
  1325. * @brief This API is used to map/unmap the Dataready(Accel & Gyro), FIFO full
  1326. * and FIFO watermark interrupt
  1327. *
  1328. * @param[in] int_config : Structure instance of bmi160_int_settg which
  1329. * stores the interrupt type and interrupt channel
  1330. * configurations to map/unmap the interrupt pins
  1331. * @param[in] dev : Structure instance of bmi160_dev.
  1332. *
  1333. * @return Result of API execution status
  1334. * @retval zero -> Success / -ve value -> Error
  1335. */
  1336. static int8_t
  1337. map_hardware_interrupt(const struct bmi160_int_settg* int_config, const struct bmi160_dev* dev);
  1338. /*!
  1339. * @brief This API is used to map/unmap the Any/Sig motion, Step det/Low-g,
  1340. * Double tap, Single tap, Orientation, Flat, High-G, Nomotion interrupt pins.
  1341. *
  1342. * @param[in] int_config : Structure instance of bmi160_int_settg which
  1343. * stores the interrupt type and interrupt channel
  1344. * configurations to map/unmap the interrupt pins
  1345. * @param[in] dev : Structure instance of bmi160_dev.
  1346. *
  1347. * @return Result of API execution status
  1348. * @retval zero -> Success / -ve value -> Error
  1349. */
  1350. static int8_t
  1351. map_feature_interrupt(const struct bmi160_int_settg* int_config, const struct bmi160_dev* dev);
  1352. /*********************** User function definitions ****************************/
  1353. /*!
  1354. * @brief This API reads the data from the given register address
  1355. * of sensor.
  1356. */
  1357. int8_t
  1358. bmi160_get_regs(uint8_t reg_addr, uint8_t* data, uint16_t len, const struct bmi160_dev* dev) {
  1359. int8_t rslt = BMI160_OK;
  1360. /* Null-pointer check */
  1361. if((dev == NULL) || (dev->read == NULL)) {
  1362. rslt = BMI160_E_NULL_PTR;
  1363. } else if(len == 0) {
  1364. rslt = BMI160_E_READ_WRITE_LENGTH_INVALID;
  1365. } else {
  1366. /* Configuring reg_addr for SPI Interface */
  1367. if(dev->intf == BMI160_SPI_INTF) {
  1368. reg_addr = (reg_addr | BMI160_SPI_RD_MASK);
  1369. }
  1370. rslt = dev->read(dev->id, reg_addr, data, len);
  1371. }
  1372. return rslt;
  1373. }
  1374. /*!
  1375. * @brief This API writes the given data to the register address
  1376. * of sensor.
  1377. */
  1378. int8_t
  1379. bmi160_set_regs(uint8_t reg_addr, uint8_t* data, uint16_t len, const struct bmi160_dev* dev) {
  1380. int8_t rslt = BMI160_OK;
  1381. uint8_t count = 0;
  1382. /* Null-pointer check */
  1383. if((dev == NULL) || (dev->write == NULL)) {
  1384. rslt = BMI160_E_NULL_PTR;
  1385. } else if(len == 0) {
  1386. rslt = BMI160_E_READ_WRITE_LENGTH_INVALID;
  1387. } else {
  1388. /* Configuring reg_addr for SPI Interface */
  1389. if(dev->intf == BMI160_SPI_INTF) {
  1390. reg_addr = (reg_addr & BMI160_SPI_WR_MASK);
  1391. }
  1392. if((dev->prev_accel_cfg.power == BMI160_ACCEL_NORMAL_MODE) ||
  1393. (dev->prev_gyro_cfg.power == BMI160_GYRO_NORMAL_MODE)) {
  1394. rslt = dev->write(dev->id, reg_addr, data, len);
  1395. /* Kindly refer bmi160 data sheet section 3.2.4 */
  1396. dev->delay_ms(1);
  1397. } else {
  1398. /*Burst write is not allowed in
  1399. * suspend & low power mode */
  1400. for(; count < len; count++) {
  1401. rslt = dev->write(dev->id, reg_addr, &data[count], 1);
  1402. reg_addr++;
  1403. /* Kindly refer bmi160 data sheet section 3.2.4 */
  1404. dev->delay_ms(1);
  1405. }
  1406. }
  1407. if(rslt != BMI160_OK) {
  1408. rslt = BMI160_E_COM_FAIL;
  1409. }
  1410. }
  1411. return rslt;
  1412. }
  1413. /*!
  1414. * @brief This API is the entry point for sensor.It performs
  1415. * the selection of I2C/SPI read mechanism according to the
  1416. * selected interface and reads the chip-id of bmi160 sensor.
  1417. */
  1418. int8_t bmi160_init(struct bmi160_dev* dev) {
  1419. int8_t rslt;
  1420. uint8_t data;
  1421. uint8_t try = 3;
  1422. /* Null-pointer check */
  1423. rslt = null_ptr_check(dev);
  1424. /* Dummy read of 0x7F register to enable SPI Interface
  1425. * if SPI is used */
  1426. if((rslt == BMI160_OK) && (dev->intf == BMI160_SPI_INTF)) {
  1427. rslt = bmi160_get_regs(BMI160_SPI_COMM_TEST_ADDR, &data, 1, dev);
  1428. }
  1429. if(rslt == BMI160_OK) {
  1430. /* Assign chip id as zero */
  1431. dev->chip_id = 0;
  1432. while((try--) && (dev->chip_id != BMI160_CHIP_ID)) {
  1433. /* Read chip_id */
  1434. rslt = bmi160_get_regs(BMI160_CHIP_ID_ADDR, &dev->chip_id, 1, dev);
  1435. }
  1436. if((rslt == BMI160_OK) && (dev->chip_id == BMI160_CHIP_ID)) {
  1437. dev->any_sig_sel = BMI160_BOTH_ANY_SIG_MOTION_DISABLED;
  1438. /* Soft reset */
  1439. rslt = bmi160_soft_reset(dev);
  1440. } else {
  1441. rslt = BMI160_E_DEV_NOT_FOUND;
  1442. }
  1443. }
  1444. return rslt;
  1445. }
  1446. /*!
  1447. * @brief This API resets and restarts the device.
  1448. * All register values are overwritten with default parameters.
  1449. */
  1450. int8_t bmi160_soft_reset(struct bmi160_dev* dev) {
  1451. int8_t rslt;
  1452. uint8_t data = BMI160_SOFT_RESET_CMD;
  1453. /* Null-pointer check */
  1454. if((dev == NULL) || (dev->delay_ms == NULL)) {
  1455. rslt = BMI160_E_NULL_PTR;
  1456. } else {
  1457. /* Reset the device */
  1458. rslt = bmi160_set_regs(BMI160_COMMAND_REG_ADDR, &data, 1, dev);
  1459. dev->delay_ms(BMI160_SOFT_RESET_DELAY_MS);
  1460. if((rslt == BMI160_OK) && (dev->intf == BMI160_SPI_INTF)) {
  1461. /* Dummy read of 0x7F register to enable SPI Interface
  1462. * if SPI is used */
  1463. rslt = bmi160_get_regs(BMI160_SPI_COMM_TEST_ADDR, &data, 1, dev);
  1464. }
  1465. if(rslt == BMI160_OK) {
  1466. /* Update the default parameters */
  1467. default_param_settg(dev);
  1468. }
  1469. }
  1470. return rslt;
  1471. }
  1472. /*!
  1473. * @brief This API configures the power mode, range and bandwidth
  1474. * of sensor.
  1475. */
  1476. int8_t bmi160_set_sens_conf(struct bmi160_dev* dev) {
  1477. int8_t rslt = BMI160_OK;
  1478. /* Null-pointer check */
  1479. if((dev == NULL) || (dev->delay_ms == NULL)) {
  1480. rslt = BMI160_E_NULL_PTR;
  1481. } else {
  1482. rslt = set_accel_conf(dev);
  1483. if(rslt == BMI160_OK) {
  1484. rslt = set_gyro_conf(dev);
  1485. if(rslt == BMI160_OK) {
  1486. /* write power mode for accel and gyro */
  1487. rslt = bmi160_set_power_mode(dev);
  1488. if(rslt == BMI160_OK) {
  1489. rslt = check_invalid_settg(dev);
  1490. }
  1491. }
  1492. }
  1493. }
  1494. return rslt;
  1495. }
  1496. /*!
  1497. * @brief This API gets accel and gyro configurations.
  1498. */
  1499. int8_t bmi160_get_sens_conf(struct bmi160_dev* dev) {
  1500. int8_t rslt = BMI160_OK;
  1501. /* Null-pointer check */
  1502. if((dev == NULL) || (dev->delay_ms == NULL)) {
  1503. rslt = BMI160_E_NULL_PTR;
  1504. } else {
  1505. rslt = get_accel_conf(dev);
  1506. if(rslt == BMI160_OK) {
  1507. rslt = get_gyro_conf(dev);
  1508. }
  1509. }
  1510. return rslt;
  1511. }
  1512. /*!
  1513. * @brief This API sets the power mode of the sensor.
  1514. */
  1515. int8_t bmi160_set_power_mode(struct bmi160_dev* dev) {
  1516. int8_t rslt = 0;
  1517. /* Null-pointer check */
  1518. if((dev == NULL) || (dev->delay_ms == NULL)) {
  1519. rslt = BMI160_E_NULL_PTR;
  1520. } else {
  1521. rslt = set_accel_pwr(dev);
  1522. if(rslt == BMI160_OK) {
  1523. rslt = set_gyro_pwr(dev);
  1524. }
  1525. }
  1526. return rslt;
  1527. }
  1528. /*!
  1529. * @brief This API gets the power mode of the sensor.
  1530. */
  1531. int8_t bmi160_get_power_mode(struct bmi160_dev* dev) {
  1532. int8_t rslt = 0;
  1533. uint8_t power_mode = 0;
  1534. /* Null-pointer check */
  1535. if((dev == NULL) || (dev->delay_ms == NULL)) {
  1536. rslt = BMI160_E_NULL_PTR;
  1537. } else {
  1538. rslt = bmi160_get_regs(BMI160_PMU_STATUS_ADDR, &power_mode, 1, dev);
  1539. if(rslt == BMI160_OK) {
  1540. /* Power mode of the accel, gyro sensor is obtained */
  1541. dev->gyro_cfg.power = BMI160_GET_BITS(power_mode, BMI160_GYRO_POWER_MODE);
  1542. dev->accel_cfg.power = BMI160_GET_BITS(power_mode, BMI160_ACCEL_POWER_MODE);
  1543. }
  1544. }
  1545. return rslt;
  1546. }
  1547. /*!
  1548. * @brief This API reads sensor data, stores it in
  1549. * the bmi160_sensor_data structure pointer passed by the user.
  1550. */
  1551. int8_t bmi160_get_sensor_data(
  1552. uint8_t select_sensor,
  1553. struct bmi160_sensor_data* accel,
  1554. struct bmi160_sensor_data* gyro,
  1555. const struct bmi160_dev* dev) {
  1556. int8_t rslt = BMI160_OK;
  1557. uint8_t time_sel;
  1558. uint8_t sen_sel;
  1559. uint8_t len = 0;
  1560. /*Extract the sensor and time select information*/
  1561. sen_sel = select_sensor & BMI160_SEN_SEL_MASK;
  1562. time_sel = ((sen_sel & BMI160_TIME_SEL) >> 2);
  1563. sen_sel = sen_sel & (BMI160_ACCEL_SEL | BMI160_GYRO_SEL);
  1564. if(time_sel == 1) {
  1565. len = 3;
  1566. }
  1567. /* Null-pointer check */
  1568. if(dev != NULL) {
  1569. switch(sen_sel) {
  1570. case BMI160_ACCEL_ONLY:
  1571. /* Null-pointer check */
  1572. if(accel == NULL) {
  1573. rslt = BMI160_E_NULL_PTR;
  1574. } else {
  1575. rslt = get_accel_data(len, accel, dev);
  1576. }
  1577. break;
  1578. case BMI160_GYRO_ONLY:
  1579. /* Null-pointer check */
  1580. if(gyro == NULL) {
  1581. rslt = BMI160_E_NULL_PTR;
  1582. } else {
  1583. rslt = get_gyro_data(len, gyro, dev);
  1584. }
  1585. break;
  1586. case BMI160_BOTH_ACCEL_AND_GYRO:
  1587. /* Null-pointer check */
  1588. if((gyro == NULL) || (accel == NULL)) {
  1589. rslt = BMI160_E_NULL_PTR;
  1590. } else {
  1591. rslt = get_accel_gyro_data(len, accel, gyro, dev);
  1592. }
  1593. break;
  1594. default:
  1595. rslt = BMI160_E_INVALID_INPUT;
  1596. break;
  1597. }
  1598. } else {
  1599. rslt = BMI160_E_NULL_PTR;
  1600. }
  1601. return rslt;
  1602. }
  1603. /*!
  1604. * @brief This API configures the necessary interrupt based on
  1605. * the user settings in the bmi160_int_settg structure instance.
  1606. */
  1607. int8_t bmi160_set_int_config(struct bmi160_int_settg* int_config, struct bmi160_dev* dev) {
  1608. int8_t rslt = BMI160_OK;
  1609. switch(int_config->int_type) {
  1610. case BMI160_ACC_ANY_MOTION_INT:
  1611. /*Any-motion interrupt*/
  1612. rslt = set_accel_any_motion_int(int_config, dev);
  1613. break;
  1614. case BMI160_ACC_SIG_MOTION_INT:
  1615. /* Significant motion interrupt */
  1616. rslt = set_accel_sig_motion_int(int_config, dev);
  1617. break;
  1618. case BMI160_ACC_SLOW_NO_MOTION_INT:
  1619. /* Slow or no motion interrupt */
  1620. rslt = set_accel_no_motion_int(int_config, dev);
  1621. break;
  1622. case BMI160_ACC_DOUBLE_TAP_INT:
  1623. case BMI160_ACC_SINGLE_TAP_INT:
  1624. /* Double tap and single tap Interrupt */
  1625. rslt = set_accel_tap_int(int_config, dev);
  1626. break;
  1627. case BMI160_STEP_DETECT_INT:
  1628. /* Step detector interrupt */
  1629. rslt = set_accel_step_detect_int(int_config, dev);
  1630. break;
  1631. case BMI160_ACC_ORIENT_INT:
  1632. /* Orientation interrupt */
  1633. rslt = set_accel_orientation_int(int_config, dev);
  1634. break;
  1635. case BMI160_ACC_FLAT_INT:
  1636. /* Flat detection interrupt */
  1637. rslt = set_accel_flat_detect_int(int_config, dev);
  1638. break;
  1639. case BMI160_ACC_LOW_G_INT:
  1640. /* Low-g interrupt */
  1641. rslt = set_accel_low_g_int(int_config, dev);
  1642. break;
  1643. case BMI160_ACC_HIGH_G_INT:
  1644. /* High-g interrupt */
  1645. rslt = set_accel_high_g_int(int_config, dev);
  1646. break;
  1647. case BMI160_ACC_GYRO_DATA_RDY_INT:
  1648. /* Data ready interrupt */
  1649. rslt = set_accel_gyro_data_ready_int(int_config, dev);
  1650. break;
  1651. case BMI160_ACC_GYRO_FIFO_FULL_INT:
  1652. /* Fifo full interrupt */
  1653. rslt = set_fifo_full_int(int_config, dev);
  1654. break;
  1655. case BMI160_ACC_GYRO_FIFO_WATERMARK_INT:
  1656. /* Fifo water-mark interrupt */
  1657. rslt = set_fifo_watermark_int(int_config, dev);
  1658. break;
  1659. case BMI160_FIFO_TAG_INT_PIN:
  1660. /* Fifo tagging feature support */
  1661. /* Configure Interrupt pins */
  1662. rslt = set_intr_pin_config(int_config, dev);
  1663. break;
  1664. default:
  1665. break;
  1666. }
  1667. return rslt;
  1668. }
  1669. /*!
  1670. * @brief This API enables or disable the step counter feature.
  1671. * 1 - enable step counter (0 - disable)
  1672. */
  1673. int8_t bmi160_set_step_counter(uint8_t step_cnt_enable, const struct bmi160_dev* dev) {
  1674. int8_t rslt;
  1675. uint8_t data = 0;
  1676. /* Null-pointer check */
  1677. rslt = null_ptr_check(dev);
  1678. if(rslt != BMI160_OK) {
  1679. rslt = BMI160_E_NULL_PTR;
  1680. } else {
  1681. rslt = bmi160_get_regs(BMI160_INT_STEP_CONFIG_1_ADDR, &data, 1, dev);
  1682. if(rslt == BMI160_OK) {
  1683. if(step_cnt_enable == BMI160_ENABLE) {
  1684. data |= (uint8_t)(step_cnt_enable << 3);
  1685. } else {
  1686. data &= ~BMI160_STEP_COUNT_EN_BIT_MASK;
  1687. }
  1688. rslt = bmi160_set_regs(BMI160_INT_STEP_CONFIG_1_ADDR, &data, 1, dev);
  1689. }
  1690. }
  1691. return rslt;
  1692. }
  1693. /*!
  1694. * @brief This API reads the step counter value.
  1695. */
  1696. int8_t bmi160_read_step_counter(uint16_t* step_val, const struct bmi160_dev* dev) {
  1697. int8_t rslt;
  1698. uint8_t data[2] = {0, 0};
  1699. uint16_t msb = 0;
  1700. uint8_t lsb = 0;
  1701. /* Null-pointer check */
  1702. rslt = null_ptr_check(dev);
  1703. if(rslt != BMI160_OK) {
  1704. rslt = BMI160_E_NULL_PTR;
  1705. } else {
  1706. rslt = bmi160_get_regs(BMI160_INT_STEP_CNT_0_ADDR, data, 2, dev);
  1707. if(rslt == BMI160_OK) {
  1708. lsb = data[0];
  1709. msb = data[1] << 8;
  1710. *step_val = msb | lsb;
  1711. }
  1712. }
  1713. return rslt;
  1714. }
  1715. /*!
  1716. * @brief This API reads the mention no of byte of data from the given
  1717. * register address of auxiliary sensor.
  1718. */
  1719. int8_t bmi160_aux_read(
  1720. uint8_t reg_addr,
  1721. uint8_t* aux_data,
  1722. uint16_t len,
  1723. const struct bmi160_dev* dev) {
  1724. int8_t rslt = BMI160_OK;
  1725. uint16_t map_len = 0;
  1726. /* Null-pointer check */
  1727. if((dev == NULL) || (dev->read == NULL)) {
  1728. rslt = BMI160_E_NULL_PTR;
  1729. } else {
  1730. if(dev->aux_cfg.aux_sensor_enable == BMI160_ENABLE) {
  1731. rslt = map_read_len(&map_len, dev);
  1732. if(rslt == BMI160_OK) {
  1733. rslt = extract_aux_read(map_len, reg_addr, aux_data, len, dev);
  1734. }
  1735. } else {
  1736. rslt = BMI160_E_INVALID_INPUT;
  1737. }
  1738. }
  1739. return rslt;
  1740. }
  1741. /*!
  1742. * @brief This API writes the mention no of byte of data to the given
  1743. * register address of auxiliary sensor.
  1744. */
  1745. int8_t bmi160_aux_write(
  1746. uint8_t reg_addr,
  1747. uint8_t* aux_data,
  1748. uint16_t len,
  1749. const struct bmi160_dev* dev) {
  1750. int8_t rslt = BMI160_OK;
  1751. uint8_t count = 0;
  1752. /* Null-pointer check */
  1753. if((dev == NULL) || (dev->write == NULL)) {
  1754. rslt = BMI160_E_NULL_PTR;
  1755. } else {
  1756. for(; count < len; count++) {
  1757. /* set data to write */
  1758. rslt = bmi160_set_regs(BMI160_AUX_IF_4_ADDR, aux_data, 1, dev);
  1759. dev->delay_ms(BMI160_AUX_COM_DELAY);
  1760. if(rslt == BMI160_OK) {
  1761. /* set address to write */
  1762. rslt = bmi160_set_regs(BMI160_AUX_IF_3_ADDR, &reg_addr, 1, dev);
  1763. dev->delay_ms(BMI160_AUX_COM_DELAY);
  1764. if(rslt == BMI160_OK && (count < len - 1)) {
  1765. aux_data++;
  1766. reg_addr++;
  1767. }
  1768. }
  1769. }
  1770. }
  1771. return rslt;
  1772. }
  1773. /*!
  1774. * @brief This API initialize the auxiliary sensor
  1775. * in order to access it.
  1776. */
  1777. int8_t bmi160_aux_init(const struct bmi160_dev* dev) {
  1778. int8_t rslt;
  1779. /* Null-pointer check */
  1780. rslt = null_ptr_check(dev);
  1781. if(rslt != BMI160_OK) {
  1782. rslt = BMI160_E_NULL_PTR;
  1783. } else {
  1784. if(dev->aux_cfg.aux_sensor_enable == BMI160_ENABLE) {
  1785. /* Configures the auxiliary sensor interface settings */
  1786. rslt = config_aux_settg(dev);
  1787. } else {
  1788. rslt = BMI160_E_INVALID_INPUT;
  1789. }
  1790. }
  1791. return rslt;
  1792. }
  1793. /*!
  1794. * @brief This API is used to setup the auxiliary sensor of bmi160 in auto mode
  1795. * Thus enabling the auto update of 8 bytes of data from auxiliary sensor
  1796. * to BMI160 register address 0x04 to 0x0B
  1797. */
  1798. int8_t bmi160_set_aux_auto_mode(uint8_t* data_addr, struct bmi160_dev* dev) {
  1799. int8_t rslt;
  1800. /* Null-pointer check */
  1801. rslt = null_ptr_check(dev);
  1802. if(rslt != BMI160_OK) {
  1803. rslt = BMI160_E_NULL_PTR;
  1804. } else {
  1805. if(dev->aux_cfg.aux_sensor_enable == BMI160_ENABLE) {
  1806. /* Write the aux. address to read in 0x4D of BMI160*/
  1807. rslt = bmi160_set_regs(BMI160_AUX_IF_2_ADDR, data_addr, 1, dev);
  1808. dev->delay_ms(BMI160_AUX_COM_DELAY);
  1809. if(rslt == BMI160_OK) {
  1810. /* Configure the polling ODR for
  1811. * auxiliary sensor */
  1812. rslt = config_aux_odr(dev);
  1813. if(rslt == BMI160_OK) {
  1814. /* Disable the aux. manual mode, i.e aux.
  1815. * sensor is in auto-mode (data-mode) */
  1816. dev->aux_cfg.manual_enable = BMI160_DISABLE;
  1817. rslt = bmi160_config_aux_mode(dev);
  1818. /* Auxiliary sensor data is obtained
  1819. * in auto mode from this point */
  1820. }
  1821. }
  1822. } else {
  1823. rslt = BMI160_E_INVALID_INPUT;
  1824. }
  1825. }
  1826. return rslt;
  1827. }
  1828. /*!
  1829. * @brief This API configures the 0x4C register and settings like
  1830. * Auxiliary sensor manual enable/ disable and aux burst read length.
  1831. */
  1832. int8_t bmi160_config_aux_mode(const struct bmi160_dev* dev) {
  1833. int8_t rslt;
  1834. uint8_t aux_if[2] = {(uint8_t)(dev->aux_cfg.aux_i2c_addr * 2), 0};
  1835. rslt = bmi160_get_regs(BMI160_AUX_IF_1_ADDR, &aux_if[1], 1, dev);
  1836. if(rslt == BMI160_OK) {
  1837. /* update the Auxiliary interface to manual/auto mode */
  1838. aux_if[1] = BMI160_SET_BITS(aux_if[1], BMI160_MANUAL_MODE_EN, dev->aux_cfg.manual_enable);
  1839. /* update the burst read length defined by user */
  1840. aux_if[1] =
  1841. BMI160_SET_BITS_POS_0(aux_if[1], BMI160_AUX_READ_BURST, dev->aux_cfg.aux_rd_burst_len);
  1842. /* Set the secondary interface address and manual mode
  1843. * along with burst read length */
  1844. rslt = bmi160_set_regs(BMI160_AUX_IF_0_ADDR, &aux_if[0], 2, dev);
  1845. dev->delay_ms(BMI160_AUX_COM_DELAY);
  1846. }
  1847. return rslt;
  1848. }
  1849. /*!
  1850. * @brief This API is used to read the raw uncompensated auxiliary sensor
  1851. * data of 8 bytes from BMI160 register address 0x04 to 0x0B
  1852. */
  1853. int8_t bmi160_read_aux_data_auto_mode(uint8_t* aux_data, const struct bmi160_dev* dev) {
  1854. int8_t rslt;
  1855. /* Null-pointer check */
  1856. rslt = null_ptr_check(dev);
  1857. if(rslt != BMI160_OK) {
  1858. rslt = BMI160_E_NULL_PTR;
  1859. } else {
  1860. if((dev->aux_cfg.aux_sensor_enable == BMI160_ENABLE) &&
  1861. (dev->aux_cfg.manual_enable == BMI160_DISABLE)) {
  1862. /* Read the aux. sensor's raw data */
  1863. rslt = bmi160_get_regs(BMI160_AUX_DATA_ADDR, aux_data, 8, dev);
  1864. } else {
  1865. rslt = BMI160_E_INVALID_INPUT;
  1866. }
  1867. }
  1868. return rslt;
  1869. }
  1870. /*!
  1871. * @brief This is used to perform self test of accel/gyro of the BMI160 sensor
  1872. */
  1873. int8_t bmi160_perform_self_test(uint8_t select_sensor, struct bmi160_dev* dev) {
  1874. int8_t rslt;
  1875. int8_t self_test_rslt = 0;
  1876. /* Null-pointer check */
  1877. rslt = null_ptr_check(dev);
  1878. if(rslt != BMI160_OK) {
  1879. rslt = BMI160_E_NULL_PTR;
  1880. } else {
  1881. /* Proceed if null check is fine */
  1882. switch(select_sensor) {
  1883. case BMI160_ACCEL_ONLY:
  1884. rslt = perform_accel_self_test(dev);
  1885. break;
  1886. case BMI160_GYRO_ONLY:
  1887. /* Set the power mode as normal mode */
  1888. dev->gyro_cfg.power = BMI160_GYRO_NORMAL_MODE;
  1889. rslt = bmi160_set_power_mode(dev);
  1890. /* Perform gyro self test */
  1891. if(rslt == BMI160_OK) {
  1892. /* Perform gyro self test */
  1893. rslt = perform_gyro_self_test(dev);
  1894. }
  1895. break;
  1896. default:
  1897. rslt = BMI160_E_INVALID_INPUT;
  1898. break;
  1899. }
  1900. /* Check to ensure bus error does not occur */
  1901. if(rslt >= BMI160_OK) {
  1902. /* Store the status of self test result */
  1903. self_test_rslt = rslt;
  1904. /* Perform soft reset */
  1905. rslt = bmi160_soft_reset(dev);
  1906. }
  1907. /* Check to ensure bus operations are success */
  1908. if(rslt == BMI160_OK) {
  1909. /* Restore self_test_rslt as return value */
  1910. rslt = self_test_rslt;
  1911. }
  1912. }
  1913. return rslt;
  1914. }
  1915. /*!
  1916. * @brief This API reads the data from fifo buffer.
  1917. */
  1918. int8_t bmi160_get_fifo_data(struct bmi160_dev const* dev) {
  1919. int8_t rslt = 0;
  1920. uint16_t bytes_to_read = 0;
  1921. uint16_t user_fifo_len = 0;
  1922. /* check the bmi160 structure as NULL*/
  1923. if((dev == NULL) || (dev->fifo->data == NULL)) {
  1924. rslt = BMI160_E_NULL_PTR;
  1925. } else {
  1926. reset_fifo_data_structure(dev);
  1927. /* get current FIFO fill-level*/
  1928. rslt = get_fifo_byte_counter(&bytes_to_read, dev);
  1929. if(rslt == BMI160_OK) {
  1930. user_fifo_len = dev->fifo->length;
  1931. if((dev->fifo->length > bytes_to_read)) {
  1932. /* Handling the case where user requests
  1933. * more data than available in FIFO */
  1934. dev->fifo->length = bytes_to_read;
  1935. }
  1936. if((dev->fifo->fifo_time_enable == BMI160_FIFO_TIME_ENABLE) &&
  1937. (bytes_to_read + BMI160_FIFO_BYTES_OVERREAD <= user_fifo_len)) {
  1938. /* Handling case of sensor time availability*/
  1939. dev->fifo->length = dev->fifo->length + BMI160_FIFO_BYTES_OVERREAD;
  1940. }
  1941. /* read only the filled bytes in the FIFO Buffer */
  1942. rslt = bmi160_get_regs(BMI160_FIFO_DATA_ADDR, dev->fifo->data, dev->fifo->length, dev);
  1943. }
  1944. }
  1945. return rslt;
  1946. }
  1947. /*!
  1948. * @brief This API writes fifo_flush command to command register.This
  1949. * action clears all data in the Fifo without changing fifo configuration
  1950. * settings
  1951. */
  1952. int8_t bmi160_set_fifo_flush(const struct bmi160_dev* dev) {
  1953. int8_t rslt = 0;
  1954. uint8_t data = BMI160_FIFO_FLUSH_VALUE;
  1955. uint8_t reg_addr = BMI160_COMMAND_REG_ADDR;
  1956. /* Check the bmi160_dev structure for NULL address*/
  1957. if(dev == NULL) {
  1958. rslt = BMI160_E_NULL_PTR;
  1959. } else {
  1960. rslt = bmi160_set_regs(reg_addr, &data, BMI160_ONE, dev);
  1961. }
  1962. return rslt;
  1963. }
  1964. /*!
  1965. * @brief This API sets the FIFO configuration in the sensor.
  1966. */
  1967. int8_t bmi160_set_fifo_config(uint8_t config, uint8_t enable, struct bmi160_dev const* dev) {
  1968. int8_t rslt = 0;
  1969. uint8_t data = 0;
  1970. uint8_t reg_addr = BMI160_FIFO_CONFIG_1_ADDR;
  1971. uint8_t fifo_config = config & BMI160_FIFO_CONFIG_1_MASK;
  1972. /* Check the bmi160_dev structure for NULL address*/
  1973. if(dev == NULL) {
  1974. rslt = BMI160_E_NULL_PTR;
  1975. } else {
  1976. rslt = bmi160_get_regs(reg_addr, &data, BMI160_ONE, dev);
  1977. if(rslt == BMI160_OK) {
  1978. if(fifo_config > 0) {
  1979. if(enable == BMI160_ENABLE) {
  1980. data = data | fifo_config;
  1981. } else {
  1982. data = data & (~fifo_config);
  1983. }
  1984. }
  1985. /* write fifo frame content configuration*/
  1986. rslt = bmi160_set_regs(reg_addr, &data, BMI160_ONE, dev);
  1987. if(rslt == BMI160_OK) {
  1988. /* read fifo frame content configuration*/
  1989. rslt = bmi160_get_regs(reg_addr, &data, BMI160_ONE, dev);
  1990. if(rslt == BMI160_OK) {
  1991. /* extract fifo header enabled status */
  1992. dev->fifo->fifo_header_enable = data & BMI160_FIFO_HEAD_ENABLE;
  1993. /* extract accel/gyr/aux. data enabled status */
  1994. dev->fifo->fifo_data_enable = data & BMI160_FIFO_M_G_A_ENABLE;
  1995. /* extract fifo sensor time enabled status */
  1996. dev->fifo->fifo_time_enable = data & BMI160_FIFO_TIME_ENABLE;
  1997. }
  1998. }
  1999. }
  2000. }
  2001. return rslt;
  2002. }
  2003. /*! @brief This API is used to configure the down sampling ratios of
  2004. * the accel and gyro data for FIFO.Also, it configures filtered or
  2005. * pre-filtered data for accel and gyro.
  2006. *
  2007. */
  2008. int8_t bmi160_set_fifo_down(uint8_t fifo_down, const struct bmi160_dev* dev) {
  2009. int8_t rslt = 0;
  2010. uint8_t data = 0;
  2011. uint8_t reg_addr = BMI160_FIFO_DOWN_ADDR;
  2012. /* Check the bmi160_dev structure for NULL address*/
  2013. if(dev == NULL) {
  2014. rslt = BMI160_E_NULL_PTR;
  2015. } else {
  2016. rslt = bmi160_get_regs(reg_addr, &data, BMI160_ONE, dev);
  2017. if(rslt == BMI160_OK) {
  2018. data = data | fifo_down;
  2019. rslt = bmi160_set_regs(reg_addr, &data, BMI160_ONE, dev);
  2020. }
  2021. }
  2022. return rslt;
  2023. }
  2024. /*!
  2025. * @brief This API sets the FIFO watermark level in the sensor.
  2026. *
  2027. */
  2028. int8_t bmi160_set_fifo_wm(uint8_t fifo_wm, const struct bmi160_dev* dev) {
  2029. int8_t rslt = 0;
  2030. uint8_t data = fifo_wm;
  2031. uint8_t reg_addr = BMI160_FIFO_CONFIG_0_ADDR;
  2032. /* Check the bmi160_dev structure for NULL address*/
  2033. if(dev == NULL) {
  2034. rslt = BMI160_E_NULL_PTR;
  2035. } else {
  2036. rslt = bmi160_set_regs(reg_addr, &data, BMI160_ONE, dev);
  2037. }
  2038. return rslt;
  2039. }
  2040. /*!
  2041. * @brief This API parses and extracts the accelerometer frames from
  2042. * FIFO data read by the "bmi160_get_fifo_data" API and stores it in
  2043. * the "accel_data" structure instance.
  2044. */
  2045. int8_t bmi160_extract_accel(
  2046. struct bmi160_sensor_data* accel_data,
  2047. uint8_t* accel_length,
  2048. struct bmi160_dev const* dev) {
  2049. int8_t rslt = 0;
  2050. uint16_t data_index = 0;
  2051. uint16_t data_read_length = 0;
  2052. uint8_t accel_index = 0;
  2053. uint8_t fifo_data_enable = 0;
  2054. if(dev == NULL || dev->fifo == NULL || dev->fifo->data == NULL) {
  2055. rslt = BMI160_E_NULL_PTR;
  2056. } else {
  2057. /* Parsing the FIFO data in header-less mode */
  2058. if(dev->fifo->fifo_header_enable == 0) {
  2059. /* Number of bytes to be parsed from FIFO */
  2060. get_accel_len_to_parse(&data_index, &data_read_length, accel_length, dev);
  2061. for(; data_index < data_read_length;) {
  2062. /*Check for the availability of next two bytes of FIFO data */
  2063. check_frame_validity(&data_index, dev);
  2064. fifo_data_enable = dev->fifo->fifo_data_enable;
  2065. unpack_accel_frame(accel_data, &data_index, &accel_index, fifo_data_enable, dev);
  2066. }
  2067. /* update number of accel data read*/
  2068. *accel_length = accel_index;
  2069. /*update the accel byte index*/
  2070. dev->fifo->accel_byte_start_idx = data_index;
  2071. } else {
  2072. /* Parsing the FIFO data in header mode */
  2073. extract_accel_header_mode(accel_data, accel_length, dev);
  2074. }
  2075. }
  2076. return rslt;
  2077. }
  2078. /*!
  2079. * @brief This API parses and extracts the gyro frames from
  2080. * FIFO data read by the "bmi160_get_fifo_data" API and stores it in
  2081. * the "gyro_data" structure instance.
  2082. */
  2083. int8_t bmi160_extract_gyro(
  2084. struct bmi160_sensor_data* gyro_data,
  2085. uint8_t* gyro_length,
  2086. struct bmi160_dev const* dev) {
  2087. int8_t rslt = 0;
  2088. uint16_t data_index = 0;
  2089. uint16_t data_read_length = 0;
  2090. uint8_t gyro_index = 0;
  2091. uint8_t fifo_data_enable = 0;
  2092. if(dev == NULL || dev->fifo->data == NULL) {
  2093. rslt = BMI160_E_NULL_PTR;
  2094. } else {
  2095. /* Parsing the FIFO data in header-less mode */
  2096. if(dev->fifo->fifo_header_enable == 0) {
  2097. /* Number of bytes to be parsed from FIFO */
  2098. get_gyro_len_to_parse(&data_index, &data_read_length, gyro_length, dev);
  2099. for(; data_index < data_read_length;) {
  2100. /*Check for the availability of next two bytes of FIFO data */
  2101. check_frame_validity(&data_index, dev);
  2102. fifo_data_enable = dev->fifo->fifo_data_enable;
  2103. unpack_gyro_frame(gyro_data, &data_index, &gyro_index, fifo_data_enable, dev);
  2104. }
  2105. /* update number of gyro data read */
  2106. *gyro_length = gyro_index;
  2107. /* update the gyro byte index */
  2108. dev->fifo->gyro_byte_start_idx = data_index;
  2109. } else {
  2110. /* Parsing the FIFO data in header mode */
  2111. extract_gyro_header_mode(gyro_data, gyro_length, dev);
  2112. }
  2113. }
  2114. return rslt;
  2115. }
  2116. /*!
  2117. * @brief This API parses and extracts the aux frames from
  2118. * FIFO data read by the "bmi160_get_fifo_data" API and stores it in
  2119. * the "aux_data" structure instance.
  2120. */
  2121. int8_t bmi160_extract_aux(
  2122. struct bmi160_aux_data* aux_data,
  2123. uint8_t* aux_len,
  2124. struct bmi160_dev const* dev) {
  2125. int8_t rslt = 0;
  2126. uint16_t data_index = 0;
  2127. uint16_t data_read_length = 0;
  2128. uint8_t aux_index = 0;
  2129. uint8_t fifo_data_enable = 0;
  2130. if((dev == NULL) || (dev->fifo->data == NULL) || (aux_data == NULL)) {
  2131. rslt = BMI160_E_NULL_PTR;
  2132. } else {
  2133. /* Parsing the FIFO data in header-less mode */
  2134. if(dev->fifo->fifo_header_enable == 0) {
  2135. /* Number of bytes to be parsed from FIFO */
  2136. get_aux_len_to_parse(&data_index, &data_read_length, aux_len, dev);
  2137. for(; data_index < data_read_length;) {
  2138. /* Check for the availability of next two
  2139. * bytes of FIFO data */
  2140. check_frame_validity(&data_index, dev);
  2141. fifo_data_enable = dev->fifo->fifo_data_enable;
  2142. unpack_aux_frame(aux_data, &data_index, &aux_index, fifo_data_enable, dev);
  2143. }
  2144. /* update number of aux data read */
  2145. *aux_len = aux_index;
  2146. /* update the aux byte index */
  2147. dev->fifo->aux_byte_start_idx = data_index;
  2148. } else {
  2149. /* Parsing the FIFO data in header mode */
  2150. extract_aux_header_mode(aux_data, aux_len, dev);
  2151. }
  2152. }
  2153. return rslt;
  2154. }
  2155. /*!
  2156. * @brief This API starts the FOC of accel and gyro
  2157. *
  2158. * @note FOC should not be used in low-power mode of sensor
  2159. *
  2160. * @note Accel FOC targets values of +1g , 0g , -1g
  2161. * Gyro FOC always targets value of 0 dps
  2162. */
  2163. int8_t bmi160_start_foc(
  2164. const struct bmi160_foc_conf* foc_conf,
  2165. struct bmi160_offsets* offset,
  2166. struct bmi160_dev const* dev) {
  2167. int8_t rslt;
  2168. uint8_t data;
  2169. /* Null-pointer check */
  2170. rslt = null_ptr_check(dev);
  2171. if(rslt != BMI160_OK) {
  2172. rslt = BMI160_E_NULL_PTR;
  2173. } else {
  2174. /* Set the offset enable bits */
  2175. rslt = configure_offset_enable(foc_conf, dev);
  2176. if(rslt == BMI160_OK) {
  2177. /* Read the FOC config from the sensor */
  2178. rslt = bmi160_get_regs(BMI160_FOC_CONF_ADDR, &data, 1, dev);
  2179. /* Set the FOC config for gyro */
  2180. data = BMI160_SET_BITS(data, BMI160_GYRO_FOC_EN, foc_conf->foc_gyr_en);
  2181. /* Set the FOC config for accel xyz axes */
  2182. data = BMI160_SET_BITS(data, BMI160_ACCEL_FOC_X_CONF, foc_conf->foc_acc_x);
  2183. data = BMI160_SET_BITS(data, BMI160_ACCEL_FOC_Y_CONF, foc_conf->foc_acc_y);
  2184. data = BMI160_SET_BITS_POS_0(data, BMI160_ACCEL_FOC_Z_CONF, foc_conf->foc_acc_z);
  2185. if(rslt == BMI160_OK) {
  2186. /* Set the FOC config in the sensor */
  2187. rslt = bmi160_set_regs(BMI160_FOC_CONF_ADDR, &data, 1, dev);
  2188. if(rslt == BMI160_OK) {
  2189. /* Procedure to trigger
  2190. * FOC and check status */
  2191. rslt = trigger_foc(offset, dev);
  2192. }
  2193. }
  2194. }
  2195. }
  2196. return rslt;
  2197. }
  2198. /*!
  2199. * @brief This API reads and stores the offset values of accel and gyro
  2200. */
  2201. int8_t bmi160_get_offsets(struct bmi160_offsets* offset, const struct bmi160_dev* dev) {
  2202. int8_t rslt;
  2203. uint8_t data[7];
  2204. uint8_t lsb, msb;
  2205. int16_t offset_msb, offset_lsb;
  2206. int16_t offset_data;
  2207. /* Null-pointer check */
  2208. rslt = null_ptr_check(dev);
  2209. if(rslt != BMI160_OK) {
  2210. rslt = BMI160_E_NULL_PTR;
  2211. } else {
  2212. /* Read the FOC config from the sensor */
  2213. rslt = bmi160_get_regs(BMI160_OFFSET_ADDR, data, 7, dev);
  2214. /* Accel offsets */
  2215. offset->off_acc_x = (int8_t)data[0];
  2216. offset->off_acc_y = (int8_t)data[1];
  2217. offset->off_acc_z = (int8_t)data[2];
  2218. /* Gyro x-axis offset */
  2219. lsb = data[3];
  2220. msb = BMI160_GET_BITS_POS_0(data[6], BMI160_GYRO_OFFSET_X);
  2221. offset_msb = (int16_t)(msb << 14);
  2222. offset_lsb = lsb << 6;
  2223. offset_data = offset_msb | offset_lsb;
  2224. /* Divide by 64 to get the Right shift by 6 value */
  2225. offset->off_gyro_x = (int16_t)(offset_data / 64);
  2226. /* Gyro y-axis offset */
  2227. lsb = data[4];
  2228. msb = BMI160_GET_BITS(data[6], BMI160_GYRO_OFFSET_Y);
  2229. offset_msb = (int16_t)(msb << 14);
  2230. offset_lsb = lsb << 6;
  2231. offset_data = offset_msb | offset_lsb;
  2232. /* Divide by 64 to get the Right shift by 6 value */
  2233. offset->off_gyro_y = (int16_t)(offset_data / 64);
  2234. /* Gyro z-axis offset */
  2235. lsb = data[5];
  2236. msb = BMI160_GET_BITS(data[6], BMI160_GYRO_OFFSET_Z);
  2237. offset_msb = (int16_t)(msb << 14);
  2238. offset_lsb = lsb << 6;
  2239. offset_data = offset_msb | offset_lsb;
  2240. /* Divide by 64 to get the Right shift by 6 value */
  2241. offset->off_gyro_z = (int16_t)(offset_data / 64);
  2242. }
  2243. return rslt;
  2244. }
  2245. /*!
  2246. * @brief This API writes the offset values of accel and gyro to
  2247. * the sensor but these values will be reset on POR or soft reset.
  2248. */
  2249. int8_t bmi160_set_offsets(
  2250. const struct bmi160_foc_conf* foc_conf,
  2251. const struct bmi160_offsets* offset,
  2252. struct bmi160_dev const* dev) {
  2253. int8_t rslt;
  2254. uint8_t data[7];
  2255. uint8_t x_msb, y_msb, z_msb;
  2256. /* Null-pointer check */
  2257. rslt = null_ptr_check(dev);
  2258. if(rslt != BMI160_OK) {
  2259. rslt = BMI160_E_NULL_PTR;
  2260. } else {
  2261. /* Update the accel offset */
  2262. data[0] = (uint8_t)offset->off_acc_x;
  2263. data[1] = (uint8_t)offset->off_acc_y;
  2264. data[2] = (uint8_t)offset->off_acc_z;
  2265. /* Update the LSB of gyro offset */
  2266. data[3] = BMI160_GET_LSB(offset->off_gyro_x);
  2267. data[4] = BMI160_GET_LSB(offset->off_gyro_y);
  2268. data[5] = BMI160_GET_LSB(offset->off_gyro_z);
  2269. /* Update the MSB of gyro offset */
  2270. x_msb = BMI160_GET_BITS(offset->off_gyro_x, BMI160_GYRO_OFFSET);
  2271. y_msb = BMI160_GET_BITS(offset->off_gyro_y, BMI160_GYRO_OFFSET);
  2272. z_msb = BMI160_GET_BITS(offset->off_gyro_z, BMI160_GYRO_OFFSET);
  2273. data[6] = (uint8_t)(z_msb << 4 | y_msb << 2 | x_msb);
  2274. /* Set the offset enable/disable for gyro and accel */
  2275. data[6] = BMI160_SET_BITS(data[6], BMI160_GYRO_OFFSET_EN, foc_conf->gyro_off_en);
  2276. data[6] = BMI160_SET_BITS(data[6], BMI160_ACCEL_OFFSET_EN, foc_conf->acc_off_en);
  2277. /* Set the offset config and values in the sensor */
  2278. rslt = bmi160_set_regs(BMI160_OFFSET_ADDR, data, 7, dev);
  2279. }
  2280. return rslt;
  2281. }
  2282. /*!
  2283. * @brief This API writes the image registers values to NVM which is
  2284. * stored even after POR or soft reset
  2285. */
  2286. int8_t bmi160_update_nvm(struct bmi160_dev const* dev) {
  2287. int8_t rslt;
  2288. uint8_t data;
  2289. uint8_t cmd = BMI160_NVM_BACKUP_EN;
  2290. /* Read the nvm_prog_en configuration */
  2291. rslt = bmi160_get_regs(BMI160_CONF_ADDR, &data, 1, dev);
  2292. if(rslt == BMI160_OK) {
  2293. data = BMI160_SET_BITS(data, BMI160_NVM_UPDATE, 1);
  2294. /* Set the nvm_prog_en bit in the sensor */
  2295. rslt = bmi160_set_regs(BMI160_CONF_ADDR, &data, 1, dev);
  2296. if(rslt == BMI160_OK) {
  2297. /* Update NVM */
  2298. rslt = bmi160_set_regs(BMI160_COMMAND_REG_ADDR, &cmd, 1, dev);
  2299. if(rslt == BMI160_OK) {
  2300. /* Check for NVM ready status */
  2301. rslt = bmi160_get_regs(BMI160_STATUS_ADDR, &data, 1, dev);
  2302. if(rslt == BMI160_OK) {
  2303. data = BMI160_GET_BITS(data, BMI160_NVM_STATUS);
  2304. if(data != BMI160_ENABLE) {
  2305. /* Delay to update NVM */
  2306. dev->delay_ms(25);
  2307. }
  2308. }
  2309. }
  2310. }
  2311. }
  2312. return rslt;
  2313. }
  2314. /*!
  2315. * @brief This API gets the interrupt status from the sensor.
  2316. */
  2317. int8_t bmi160_get_int_status(
  2318. enum bmi160_int_status_sel int_status_sel,
  2319. union bmi160_int_status* int_status,
  2320. struct bmi160_dev const* dev) {
  2321. int8_t rslt = 0;
  2322. /* To get the status of all interrupts */
  2323. if(int_status_sel == BMI160_INT_STATUS_ALL) {
  2324. rslt = bmi160_get_regs(BMI160_INT_STATUS_ADDR, &int_status->data[0], 4, dev);
  2325. } else {
  2326. if(int_status_sel & BMI160_INT_STATUS_0) {
  2327. rslt = bmi160_get_regs(BMI160_INT_STATUS_ADDR, &int_status->data[0], 1, dev);
  2328. }
  2329. if(int_status_sel & BMI160_INT_STATUS_1) {
  2330. rslt = bmi160_get_regs(BMI160_INT_STATUS_ADDR + 1, &int_status->data[1], 1, dev);
  2331. }
  2332. if(int_status_sel & BMI160_INT_STATUS_2) {
  2333. rslt = bmi160_get_regs(BMI160_INT_STATUS_ADDR + 2, &int_status->data[2], 1, dev);
  2334. }
  2335. if(int_status_sel & BMI160_INT_STATUS_3) {
  2336. rslt = bmi160_get_regs(BMI160_INT_STATUS_ADDR + 3, &int_status->data[3], 1, dev);
  2337. }
  2338. }
  2339. return rslt;
  2340. }
  2341. /*********************** Local function definitions ***************************/
  2342. /*!
  2343. * @brief This API sets the any-motion interrupt of the sensor.
  2344. * This interrupt occurs when accel values exceeds preset threshold
  2345. * for a certain period of time.
  2346. */
  2347. static int8_t
  2348. set_accel_any_motion_int(struct bmi160_int_settg* int_config, struct bmi160_dev* dev) {
  2349. int8_t rslt;
  2350. /* Null-pointer check */
  2351. rslt = null_ptr_check(dev);
  2352. if((rslt != BMI160_OK) || (int_config == NULL)) {
  2353. rslt = BMI160_E_NULL_PTR;
  2354. } else {
  2355. /* updating the interrupt structure to local structure */
  2356. struct bmi160_acc_any_mot_int_cfg* any_motion_int_cfg =
  2357. &(int_config->int_type_cfg.acc_any_motion_int);
  2358. rslt = enable_accel_any_motion_int(any_motion_int_cfg, dev);
  2359. if(rslt == BMI160_OK) {
  2360. rslt = config_any_motion_int_settg(int_config, any_motion_int_cfg, dev);
  2361. }
  2362. }
  2363. return rslt;
  2364. }
  2365. /*!
  2366. * @brief This API sets tap interrupts.Interrupt is fired when
  2367. * tap movements happen.
  2368. */
  2369. static int8_t
  2370. set_accel_tap_int(struct bmi160_int_settg* int_config, const struct bmi160_dev* dev) {
  2371. int8_t rslt;
  2372. /* Null-pointer check */
  2373. rslt = null_ptr_check(dev);
  2374. if((rslt != BMI160_OK) || (int_config == NULL)) {
  2375. rslt = BMI160_E_NULL_PTR;
  2376. } else {
  2377. /* updating the interrupt structure to local structure */
  2378. struct bmi160_acc_tap_int_cfg* tap_int_cfg = &(int_config->int_type_cfg.acc_tap_int);
  2379. rslt = enable_tap_int(int_config, tap_int_cfg, dev);
  2380. if(rslt == BMI160_OK) {
  2381. /* Configure Interrupt pins */
  2382. rslt = set_intr_pin_config(int_config, dev);
  2383. if(rslt == BMI160_OK) {
  2384. rslt = config_tap_int_settg(int_config, tap_int_cfg, dev);
  2385. }
  2386. }
  2387. }
  2388. return rslt;
  2389. }
  2390. /*!
  2391. * @brief This API sets the data ready interrupt for both accel and gyro.
  2392. * This interrupt occurs when new accel and gyro data comes.
  2393. */
  2394. static int8_t set_accel_gyro_data_ready_int(
  2395. const struct bmi160_int_settg* int_config,
  2396. const struct bmi160_dev* dev) {
  2397. int8_t rslt;
  2398. /* Null-pointer check */
  2399. rslt = null_ptr_check(dev);
  2400. if((rslt != BMI160_OK) || (int_config == NULL)) {
  2401. rslt = BMI160_E_NULL_PTR;
  2402. } else {
  2403. rslt = enable_data_ready_int(dev);
  2404. if(rslt == BMI160_OK) {
  2405. /* Configure Interrupt pins */
  2406. rslt = set_intr_pin_config(int_config, dev);
  2407. if(rslt == BMI160_OK) {
  2408. rslt = map_hardware_interrupt(int_config, dev);
  2409. }
  2410. }
  2411. }
  2412. return rslt;
  2413. }
  2414. /*!
  2415. * @brief This API sets the significant motion interrupt of the sensor.This
  2416. * interrupt occurs when there is change in user location.
  2417. */
  2418. static int8_t
  2419. set_accel_sig_motion_int(struct bmi160_int_settg* int_config, struct bmi160_dev* dev) {
  2420. int8_t rslt;
  2421. /* Null-pointer check */
  2422. rslt = null_ptr_check(dev);
  2423. if((rslt != BMI160_OK) || (int_config == NULL)) {
  2424. rslt = BMI160_E_NULL_PTR;
  2425. } else {
  2426. /* updating the interrupt structure to local structure */
  2427. struct bmi160_acc_sig_mot_int_cfg* sig_mot_int_cfg =
  2428. &(int_config->int_type_cfg.acc_sig_motion_int);
  2429. rslt = enable_sig_motion_int(sig_mot_int_cfg, dev);
  2430. if(rslt == BMI160_OK) {
  2431. rslt = config_sig_motion_int_settg(int_config, sig_mot_int_cfg, dev);
  2432. }
  2433. }
  2434. return rslt;
  2435. }
  2436. /*!
  2437. * @brief This API sets the no motion/slow motion interrupt of the sensor.
  2438. * Slow motion is similar to any motion interrupt.No motion interrupt
  2439. * occurs when slope bet. two accel values falls below preset threshold
  2440. * for preset duration.
  2441. */
  2442. static int8_t
  2443. set_accel_no_motion_int(struct bmi160_int_settg* int_config, const struct bmi160_dev* dev) {
  2444. int8_t rslt;
  2445. /* Null-pointer check */
  2446. rslt = null_ptr_check(dev);
  2447. if((rslt != BMI160_OK) || (int_config == NULL)) {
  2448. rslt = BMI160_E_NULL_PTR;
  2449. } else {
  2450. /* updating the interrupt structure to local structure */
  2451. struct bmi160_acc_no_motion_int_cfg* no_mot_int_cfg =
  2452. &(int_config->int_type_cfg.acc_no_motion_int);
  2453. rslt = enable_no_motion_int(no_mot_int_cfg, dev);
  2454. if(rslt == BMI160_OK) {
  2455. /* Configure the INT PIN settings*/
  2456. rslt = config_no_motion_int_settg(int_config, no_mot_int_cfg, dev);
  2457. }
  2458. }
  2459. return rslt;
  2460. }
  2461. /*!
  2462. * @brief This API sets the step detection interrupt.This interrupt
  2463. * occurs when the single step causes accel values to go above
  2464. * preset threshold.
  2465. */
  2466. static int8_t
  2467. set_accel_step_detect_int(struct bmi160_int_settg* int_config, const struct bmi160_dev* dev) {
  2468. int8_t rslt;
  2469. /* Null-pointer check */
  2470. rslt = null_ptr_check(dev);
  2471. if((rslt != BMI160_OK) || (int_config == NULL)) {
  2472. rslt = BMI160_E_NULL_PTR;
  2473. } else {
  2474. /* updating the interrupt structure to local structure */
  2475. struct bmi160_acc_step_detect_int_cfg* step_detect_int_cfg =
  2476. &(int_config->int_type_cfg.acc_step_detect_int);
  2477. rslt = enable_step_detect_int(step_detect_int_cfg, dev);
  2478. if(rslt == BMI160_OK) {
  2479. /* Configure Interrupt pins */
  2480. rslt = set_intr_pin_config(int_config, dev);
  2481. if(rslt == BMI160_OK) {
  2482. rslt = map_feature_interrupt(int_config, dev);
  2483. if(rslt == BMI160_OK) {
  2484. rslt = config_step_detect(step_detect_int_cfg, dev);
  2485. }
  2486. }
  2487. }
  2488. }
  2489. return rslt;
  2490. }
  2491. /*!
  2492. * @brief This API sets the orientation interrupt of the sensor.This
  2493. * interrupt occurs when there is orientation change in the sensor
  2494. * with respect to gravitational field vector g.
  2495. */
  2496. static int8_t
  2497. set_accel_orientation_int(struct bmi160_int_settg* int_config, const struct bmi160_dev* dev) {
  2498. int8_t rslt;
  2499. /* Null-pointer check */
  2500. rslt = null_ptr_check(dev);
  2501. if((rslt != BMI160_OK) || (int_config == NULL)) {
  2502. rslt = BMI160_E_NULL_PTR;
  2503. } else {
  2504. /* updating the interrupt structure to local structure */
  2505. struct bmi160_acc_orient_int_cfg* orient_int_cfg =
  2506. &(int_config->int_type_cfg.acc_orient_int);
  2507. rslt = enable_orient_int(orient_int_cfg, dev);
  2508. if(rslt == BMI160_OK) {
  2509. /* Configure Interrupt pins */
  2510. rslt = set_intr_pin_config(int_config, dev);
  2511. if(rslt == BMI160_OK) {
  2512. /* map INT pin to orient interrupt */
  2513. rslt = map_feature_interrupt(int_config, dev);
  2514. if(rslt == BMI160_OK) {
  2515. /* configure the
  2516. * orientation setting*/
  2517. rslt = config_orient_int_settg(orient_int_cfg, dev);
  2518. }
  2519. }
  2520. }
  2521. }
  2522. return rslt;
  2523. }
  2524. /*!
  2525. * @brief This API sets the flat interrupt of the sensor.This interrupt
  2526. * occurs in case of flat orientation
  2527. */
  2528. static int8_t
  2529. set_accel_flat_detect_int(struct bmi160_int_settg* int_config, const struct bmi160_dev* dev) {
  2530. int8_t rslt;
  2531. /* Null-pointer check */
  2532. rslt = null_ptr_check(dev);
  2533. if((rslt != BMI160_OK) || (int_config == NULL)) {
  2534. rslt = BMI160_E_NULL_PTR;
  2535. } else {
  2536. /* updating the interrupt structure to local structure */
  2537. struct bmi160_acc_flat_detect_int_cfg* flat_detect_int =
  2538. &(int_config->int_type_cfg.acc_flat_int);
  2539. /* enable the flat interrupt */
  2540. rslt = enable_flat_int(flat_detect_int, dev);
  2541. if(rslt == BMI160_OK) {
  2542. /* Configure Interrupt pins */
  2543. rslt = set_intr_pin_config(int_config, dev);
  2544. if(rslt == BMI160_OK) {
  2545. /* map INT pin to flat interrupt */
  2546. rslt = map_feature_interrupt(int_config, dev);
  2547. if(rslt == BMI160_OK) {
  2548. /* configure the flat setting*/
  2549. rslt = config_flat_int_settg(flat_detect_int, dev);
  2550. }
  2551. }
  2552. }
  2553. }
  2554. return rslt;
  2555. }
  2556. /*!
  2557. * @brief This API sets the low-g interrupt of the sensor.This interrupt
  2558. * occurs during free-fall.
  2559. */
  2560. static int8_t
  2561. set_accel_low_g_int(struct bmi160_int_settg* int_config, const struct bmi160_dev* dev) {
  2562. int8_t rslt;
  2563. /* Null-pointer check */
  2564. rslt = null_ptr_check(dev);
  2565. if((rslt != BMI160_OK) || (int_config == NULL)) {
  2566. rslt = BMI160_E_NULL_PTR;
  2567. } else {
  2568. /* updating the interrupt structure to local structure */
  2569. struct bmi160_acc_low_g_int_cfg* low_g_int = &(int_config->int_type_cfg.acc_low_g_int);
  2570. /* Enable the low-g interrupt*/
  2571. rslt = enable_low_g_int(low_g_int, dev);
  2572. if(rslt == BMI160_OK) {
  2573. /* Configure Interrupt pins */
  2574. rslt = set_intr_pin_config(int_config, dev);
  2575. if(rslt == BMI160_OK) {
  2576. /* Map INT pin to low-g interrupt */
  2577. rslt = map_feature_interrupt(int_config, dev);
  2578. if(rslt == BMI160_OK) {
  2579. /* configure the data source
  2580. * for low-g interrupt*/
  2581. rslt = config_low_g_data_src(low_g_int, dev);
  2582. if(rslt == BMI160_OK) {
  2583. rslt = config_low_g_int_settg(low_g_int, dev);
  2584. }
  2585. }
  2586. }
  2587. }
  2588. }
  2589. return rslt;
  2590. }
  2591. /*!
  2592. * @brief This API sets the high-g interrupt of the sensor.The interrupt
  2593. * occurs if the absolute value of acceleration data of any enabled axis
  2594. * exceeds the programmed threshold and the sign of the value does not
  2595. * change for a preset duration.
  2596. */
  2597. static int8_t
  2598. set_accel_high_g_int(struct bmi160_int_settg* int_config, const struct bmi160_dev* dev) {
  2599. int8_t rslt;
  2600. /* Null-pointer check */
  2601. rslt = null_ptr_check(dev);
  2602. if((rslt != BMI160_OK) || (int_config == NULL)) {
  2603. rslt = BMI160_E_NULL_PTR;
  2604. } else {
  2605. /* updating the interrupt structure to local structure */
  2606. struct bmi160_acc_high_g_int_cfg* high_g_int_cfg =
  2607. &(int_config->int_type_cfg.acc_high_g_int);
  2608. /* Enable the high-g interrupt */
  2609. rslt = enable_high_g_int(high_g_int_cfg, dev);
  2610. if(rslt == BMI160_OK) {
  2611. /* Configure Interrupt pins */
  2612. rslt = set_intr_pin_config(int_config, dev);
  2613. if(rslt == BMI160_OK) {
  2614. /* Map INT pin to high-g interrupt */
  2615. rslt = map_feature_interrupt(int_config, dev);
  2616. if(rslt == BMI160_OK) {
  2617. /* configure the data source
  2618. * for high-g interrupt*/
  2619. rslt = config_high_g_data_src(high_g_int_cfg, dev);
  2620. if(rslt == BMI160_OK) {
  2621. rslt = config_high_g_int_settg(high_g_int_cfg, dev);
  2622. }
  2623. }
  2624. }
  2625. }
  2626. }
  2627. return rslt;
  2628. }
  2629. /*!
  2630. * @brief This API configures the pins to fire the
  2631. * interrupt signal when it occurs.
  2632. */
  2633. static int8_t
  2634. set_intr_pin_config(const struct bmi160_int_settg* int_config, const struct bmi160_dev* dev) {
  2635. int8_t rslt;
  2636. /* configure the behavioural settings of interrupt pin */
  2637. rslt = config_int_out_ctrl(int_config, dev);
  2638. if(rslt == BMI160_OK) {
  2639. rslt = config_int_latch(int_config, dev);
  2640. }
  2641. return rslt;
  2642. }
  2643. /*!
  2644. * @brief This internal API is used to validate the device structure pointer for
  2645. * null conditions.
  2646. */
  2647. static int8_t null_ptr_check(const struct bmi160_dev* dev) {
  2648. int8_t rslt;
  2649. if((dev == NULL) || (dev->read == NULL) || (dev->write == NULL) || (dev->delay_ms == NULL)) {
  2650. rslt = BMI160_E_NULL_PTR;
  2651. } else {
  2652. /* Device structure is fine */
  2653. rslt = BMI160_OK;
  2654. }
  2655. return rslt;
  2656. }
  2657. /*!
  2658. * @brief This API sets the default configuration parameters of accel & gyro.
  2659. * Also maintain the previous state of configurations.
  2660. */
  2661. static void default_param_settg(struct bmi160_dev* dev) {
  2662. /* Initializing accel and gyro params with
  2663. * default values */
  2664. dev->accel_cfg.bw = BMI160_ACCEL_BW_NORMAL_AVG4;
  2665. dev->accel_cfg.odr = BMI160_ACCEL_ODR_100HZ;
  2666. dev->accel_cfg.power = BMI160_ACCEL_SUSPEND_MODE;
  2667. dev->accel_cfg.range = BMI160_ACCEL_RANGE_2G;
  2668. dev->gyro_cfg.bw = BMI160_GYRO_BW_NORMAL_MODE;
  2669. dev->gyro_cfg.odr = BMI160_GYRO_ODR_100HZ;
  2670. dev->gyro_cfg.power = BMI160_GYRO_SUSPEND_MODE;
  2671. dev->gyro_cfg.range = BMI160_GYRO_RANGE_2000_DPS;
  2672. /* To maintain the previous state of accel configuration */
  2673. dev->prev_accel_cfg = dev->accel_cfg;
  2674. /* To maintain the previous state of gyro configuration */
  2675. dev->prev_gyro_cfg = dev->gyro_cfg;
  2676. }
  2677. /*!
  2678. * @brief This API set the accel configuration.
  2679. */
  2680. static int8_t set_accel_conf(struct bmi160_dev* dev) {
  2681. int8_t rslt;
  2682. uint8_t data[2] = {0};
  2683. rslt = check_accel_config(data, dev);
  2684. if(rslt == BMI160_OK) {
  2685. /* Write output data rate and bandwidth */
  2686. rslt = bmi160_set_regs(BMI160_ACCEL_CONFIG_ADDR, &data[0], 1, dev);
  2687. if(rslt == BMI160_OK) {
  2688. dev->prev_accel_cfg.odr = dev->accel_cfg.odr;
  2689. dev->prev_accel_cfg.bw = dev->accel_cfg.bw;
  2690. /* write accel range */
  2691. rslt = bmi160_set_regs(BMI160_ACCEL_RANGE_ADDR, &data[1], 1, dev);
  2692. if(rslt == BMI160_OK) {
  2693. dev->prev_accel_cfg.range = dev->accel_cfg.range;
  2694. }
  2695. }
  2696. }
  2697. return rslt;
  2698. }
  2699. /*!
  2700. * @brief This API gets the accel configuration.
  2701. */
  2702. static int8_t get_accel_conf(struct bmi160_dev* dev) {
  2703. int8_t rslt;
  2704. uint8_t data[2] = {0};
  2705. /* Get accel configurations */
  2706. rslt = bmi160_get_regs(BMI160_ACCEL_CONFIG_ADDR, data, 2, dev);
  2707. if(rslt == BMI160_OK) {
  2708. dev->accel_cfg.odr = (data[0] & BMI160_ACCEL_ODR_MASK);
  2709. dev->accel_cfg.bw = (data[0] & BMI160_ACCEL_BW_MASK) >> BMI160_ACCEL_BW_POS;
  2710. dev->accel_cfg.range = (data[1] & BMI160_ACCEL_RANGE_MASK);
  2711. }
  2712. return rslt;
  2713. }
  2714. /*!
  2715. * @brief This API check the accel configuration.
  2716. */
  2717. static int8_t check_accel_config(uint8_t* data, const struct bmi160_dev* dev) {
  2718. int8_t rslt;
  2719. /* read accel Output data rate and bandwidth */
  2720. rslt = bmi160_get_regs(BMI160_ACCEL_CONFIG_ADDR, data, 2, dev);
  2721. if(rslt == BMI160_OK) {
  2722. rslt = process_accel_odr(&data[0], dev);
  2723. if(rslt == BMI160_OK) {
  2724. rslt = process_accel_bw(&data[0], dev);
  2725. if(rslt == BMI160_OK) {
  2726. rslt = process_accel_range(&data[1], dev);
  2727. }
  2728. }
  2729. }
  2730. return rslt;
  2731. }
  2732. /*!
  2733. * @brief This API process the accel odr.
  2734. */
  2735. static int8_t process_accel_odr(uint8_t* data, const struct bmi160_dev* dev) {
  2736. int8_t rslt = 0;
  2737. uint8_t temp = 0;
  2738. uint8_t odr = 0;
  2739. if(dev->accel_cfg.odr <= BMI160_ACCEL_ODR_1600HZ) {
  2740. if(dev->accel_cfg.odr != dev->prev_accel_cfg.odr) {
  2741. odr = (uint8_t)dev->accel_cfg.odr;
  2742. temp = *data & ~BMI160_ACCEL_ODR_MASK;
  2743. /* Adding output data rate */
  2744. *data = temp | (odr & BMI160_ACCEL_ODR_MASK);
  2745. }
  2746. } else {
  2747. rslt = BMI160_E_OUT_OF_RANGE;
  2748. }
  2749. return rslt;
  2750. }
  2751. /*!
  2752. * @brief This API process the accel bandwidth.
  2753. */
  2754. static int8_t process_accel_bw(uint8_t* data, const struct bmi160_dev* dev) {
  2755. int8_t rslt = 0;
  2756. uint8_t temp = 0;
  2757. uint8_t bw = 0;
  2758. if(dev->accel_cfg.bw <= BMI160_ACCEL_BW_RES_AVG128) {
  2759. if(dev->accel_cfg.bw != dev->prev_accel_cfg.bw) {
  2760. bw = (uint8_t)dev->accel_cfg.bw;
  2761. temp = *data & ~BMI160_ACCEL_BW_MASK;
  2762. /* Adding bandwidth */
  2763. *data = temp | ((bw << 4) & BMI160_ACCEL_BW_MASK);
  2764. }
  2765. } else {
  2766. rslt = BMI160_E_OUT_OF_RANGE;
  2767. }
  2768. return rslt;
  2769. }
  2770. /*!
  2771. * @brief This API process the accel range.
  2772. */
  2773. static int8_t process_accel_range(uint8_t* data, const struct bmi160_dev* dev) {
  2774. int8_t rslt = 0;
  2775. uint8_t temp = 0;
  2776. uint8_t range = 0;
  2777. if(dev->accel_cfg.range <= BMI160_ACCEL_RANGE_16G) {
  2778. if(dev->accel_cfg.range != dev->prev_accel_cfg.range) {
  2779. range = (uint8_t)dev->accel_cfg.range;
  2780. temp = *data & ~BMI160_ACCEL_RANGE_MASK;
  2781. /* Adding range */
  2782. *data = temp | (range & BMI160_ACCEL_RANGE_MASK);
  2783. }
  2784. } else {
  2785. rslt = BMI160_E_OUT_OF_RANGE;
  2786. }
  2787. return rslt;
  2788. }
  2789. /*!
  2790. * @brief This API checks the invalid settings for ODR & Bw for
  2791. * Accel and Gyro.
  2792. */
  2793. static int8_t check_invalid_settg(const struct bmi160_dev* dev) {
  2794. int8_t rslt;
  2795. uint8_t data = 0;
  2796. /* read the error reg */
  2797. rslt = bmi160_get_regs(BMI160_ERROR_REG_ADDR, &data, 1, dev);
  2798. data = data >> 1;
  2799. data = data & BMI160_ERR_REG_MASK;
  2800. if(data == 1) {
  2801. rslt = BMI160_E_ACCEL_ODR_BW_INVALID;
  2802. } else if(data == 2) {
  2803. rslt = BMI160_E_GYRO_ODR_BW_INVALID;
  2804. } else if(data == 3) {
  2805. rslt = BMI160_E_LWP_PRE_FLTR_INT_INVALID;
  2806. } else if(data == 7) {
  2807. rslt = BMI160_E_LWP_PRE_FLTR_INVALID;
  2808. }
  2809. return rslt;
  2810. }
  2811. static int8_t set_gyro_conf(struct bmi160_dev* dev) {
  2812. int8_t rslt;
  2813. uint8_t data[2] = {0};
  2814. rslt = check_gyro_config(data, dev);
  2815. if(rslt == BMI160_OK) {
  2816. /* Write output data rate and bandwidth */
  2817. rslt = bmi160_set_regs(BMI160_GYRO_CONFIG_ADDR, &data[0], 1, dev);
  2818. if(rslt == BMI160_OK) {
  2819. dev->prev_gyro_cfg.odr = dev->gyro_cfg.odr;
  2820. dev->prev_gyro_cfg.bw = dev->gyro_cfg.bw;
  2821. /* Write gyro range */
  2822. rslt = bmi160_set_regs(BMI160_GYRO_RANGE_ADDR, &data[1], 1, dev);
  2823. if(rslt == BMI160_OK) {
  2824. dev->prev_gyro_cfg.range = dev->gyro_cfg.range;
  2825. }
  2826. }
  2827. }
  2828. return rslt;
  2829. }
  2830. /*!
  2831. * @brief This API gets the gyro configuration.
  2832. */
  2833. static int8_t get_gyro_conf(struct bmi160_dev* dev) {
  2834. int8_t rslt;
  2835. uint8_t data[2] = {0};
  2836. /* Get accel configurations */
  2837. rslt = bmi160_get_regs(BMI160_GYRO_CONFIG_ADDR, data, 2, dev);
  2838. if(rslt == BMI160_OK) {
  2839. dev->gyro_cfg.odr = (data[0] & BMI160_GYRO_ODR_MASK);
  2840. dev->gyro_cfg.bw = (data[0] & BMI160_GYRO_BW_MASK) >> BMI160_GYRO_BW_POS;
  2841. dev->gyro_cfg.range = (data[1] & BMI160_GYRO_RANGE_MASK);
  2842. }
  2843. return rslt;
  2844. }
  2845. /*!
  2846. * @brief This API check the gyro configuration.
  2847. */
  2848. static int8_t check_gyro_config(uint8_t* data, const struct bmi160_dev* dev) {
  2849. int8_t rslt;
  2850. /* read gyro Output data rate and bandwidth */
  2851. rslt = bmi160_get_regs(BMI160_GYRO_CONFIG_ADDR, data, 2, dev);
  2852. if(rslt == BMI160_OK) {
  2853. rslt = process_gyro_odr(&data[0], dev);
  2854. if(rslt == BMI160_OK) {
  2855. rslt = process_gyro_bw(&data[0], dev);
  2856. if(rslt == BMI160_OK) {
  2857. rslt = process_gyro_range(&data[1], dev);
  2858. }
  2859. }
  2860. }
  2861. return rslt;
  2862. }
  2863. /*!
  2864. * @brief This API process the gyro odr.
  2865. */
  2866. static int8_t process_gyro_odr(uint8_t* data, const struct bmi160_dev* dev) {
  2867. int8_t rslt = 0;
  2868. uint8_t temp = 0;
  2869. uint8_t odr = 0;
  2870. if(dev->gyro_cfg.odr <= BMI160_GYRO_ODR_3200HZ) {
  2871. if(dev->gyro_cfg.odr != dev->prev_gyro_cfg.odr) {
  2872. odr = (uint8_t)dev->gyro_cfg.odr;
  2873. temp = (*data & ~BMI160_GYRO_ODR_MASK);
  2874. /* Adding output data rate */
  2875. *data = temp | (odr & BMI160_GYRO_ODR_MASK);
  2876. }
  2877. } else {
  2878. rslt = BMI160_E_OUT_OF_RANGE;
  2879. }
  2880. return rslt;
  2881. }
  2882. /*!
  2883. * @brief This API process the gyro bandwidth.
  2884. */
  2885. static int8_t process_gyro_bw(uint8_t* data, const struct bmi160_dev* dev) {
  2886. int8_t rslt = 0;
  2887. uint8_t temp = 0;
  2888. uint8_t bw = 0;
  2889. if(dev->gyro_cfg.bw <= BMI160_GYRO_BW_NORMAL_MODE) {
  2890. bw = (uint8_t)dev->gyro_cfg.bw;
  2891. temp = *data & ~BMI160_GYRO_BW_MASK;
  2892. /* Adding bandwidth */
  2893. *data = temp | ((bw << 4) & BMI160_GYRO_BW_MASK);
  2894. } else {
  2895. rslt = BMI160_E_OUT_OF_RANGE;
  2896. }
  2897. return rslt;
  2898. }
  2899. /*!
  2900. * @brief This API process the gyro range.
  2901. */
  2902. static int8_t process_gyro_range(uint8_t* data, const struct bmi160_dev* dev) {
  2903. int8_t rslt = 0;
  2904. uint8_t temp = 0;
  2905. uint8_t range = 0;
  2906. if(dev->gyro_cfg.range <= BMI160_GYRO_RANGE_125_DPS) {
  2907. if(dev->gyro_cfg.range != dev->prev_gyro_cfg.range) {
  2908. range = (uint8_t)dev->gyro_cfg.range;
  2909. temp = *data & ~BMI160_GYRO_RANGE_MASK;
  2910. /* Adding range */
  2911. *data = temp | (range & BMI160_GYRO_RANGE_MASK);
  2912. }
  2913. } else {
  2914. rslt = BMI160_E_OUT_OF_RANGE;
  2915. }
  2916. return rslt;
  2917. }
  2918. /*!
  2919. * @brief This API sets the accel power.
  2920. */
  2921. static int8_t set_accel_pwr(struct bmi160_dev* dev) {
  2922. int8_t rslt = 0;
  2923. uint8_t data = 0;
  2924. if((dev->accel_cfg.power >= BMI160_ACCEL_SUSPEND_MODE) &&
  2925. (dev->accel_cfg.power <= BMI160_ACCEL_LOWPOWER_MODE)) {
  2926. if(dev->accel_cfg.power != dev->prev_accel_cfg.power) {
  2927. rslt = process_under_sampling(&data, dev);
  2928. if(rslt == BMI160_OK) {
  2929. /* Write accel power */
  2930. rslt = bmi160_set_regs(BMI160_COMMAND_REG_ADDR, &dev->accel_cfg.power, 1, dev);
  2931. /* Add delay of 3.8 ms - refer data sheet table 24*/
  2932. if(dev->prev_accel_cfg.power == BMI160_ACCEL_SUSPEND_MODE) {
  2933. dev->delay_ms(BMI160_ACCEL_DELAY_MS);
  2934. }
  2935. dev->prev_accel_cfg.power = dev->accel_cfg.power;
  2936. }
  2937. }
  2938. } else {
  2939. rslt = BMI160_E_INVALID_CONFIG;
  2940. }
  2941. return rslt;
  2942. }
  2943. /*!
  2944. * @brief This API process the undersampling setting of Accel.
  2945. */
  2946. static int8_t process_under_sampling(uint8_t* data, const struct bmi160_dev* dev) {
  2947. int8_t rslt;
  2948. uint8_t temp = 0;
  2949. uint8_t pre_filter[2] = {0};
  2950. rslt = bmi160_get_regs(BMI160_ACCEL_CONFIG_ADDR, data, 1, dev);
  2951. if(rslt == BMI160_OK) {
  2952. if(dev->accel_cfg.power == BMI160_ACCEL_LOWPOWER_MODE) {
  2953. temp = *data & ~BMI160_ACCEL_UNDERSAMPLING_MASK;
  2954. /* Set under-sampling parameter */
  2955. *data = temp | ((1 << 7) & BMI160_ACCEL_UNDERSAMPLING_MASK);
  2956. /* Write data */
  2957. rslt = bmi160_set_regs(BMI160_ACCEL_CONFIG_ADDR, data, 1, dev);
  2958. /* Disable the pre-filter data in low power mode */
  2959. if(rslt == BMI160_OK) {
  2960. /* Disable the Pre-filter data*/
  2961. rslt = bmi160_set_regs(BMI160_INT_DATA_0_ADDR, pre_filter, 2, dev);
  2962. }
  2963. } else if(*data & BMI160_ACCEL_UNDERSAMPLING_MASK) {
  2964. temp = *data & ~BMI160_ACCEL_UNDERSAMPLING_MASK;
  2965. /* Disable under-sampling parameter if already enabled */
  2966. *data = temp;
  2967. /* Write data */
  2968. rslt = bmi160_set_regs(BMI160_ACCEL_CONFIG_ADDR, data, 1, dev);
  2969. }
  2970. }
  2971. return rslt;
  2972. }
  2973. /*!
  2974. * @brief This API sets the gyro power mode.
  2975. */
  2976. static int8_t set_gyro_pwr(struct bmi160_dev* dev) {
  2977. int8_t rslt = 0;
  2978. if((dev->gyro_cfg.power == BMI160_GYRO_SUSPEND_MODE) ||
  2979. (dev->gyro_cfg.power == BMI160_GYRO_NORMAL_MODE) ||
  2980. (dev->gyro_cfg.power == BMI160_GYRO_FASTSTARTUP_MODE)) {
  2981. if(dev->gyro_cfg.power != dev->prev_gyro_cfg.power) {
  2982. /* Write gyro power */
  2983. rslt = bmi160_set_regs(BMI160_COMMAND_REG_ADDR, &dev->gyro_cfg.power, 1, dev);
  2984. if(dev->prev_gyro_cfg.power == BMI160_GYRO_SUSPEND_MODE) {
  2985. /* Delay of 80 ms - datasheet Table 24 */
  2986. dev->delay_ms(BMI160_GYRO_DELAY_MS);
  2987. } else if(
  2988. (dev->prev_gyro_cfg.power == BMI160_GYRO_FASTSTARTUP_MODE) &&
  2989. (dev->gyro_cfg.power == BMI160_GYRO_NORMAL_MODE)) {
  2990. /* This delay is required for transition from
  2991. * fast-startup mode to normal mode - datasheet Table 3 */
  2992. dev->delay_ms(10);
  2993. } else {
  2994. /* do nothing */
  2995. }
  2996. dev->prev_gyro_cfg.power = dev->gyro_cfg.power;
  2997. }
  2998. } else {
  2999. rslt = BMI160_E_INVALID_CONFIG;
  3000. }
  3001. return rslt;
  3002. }
  3003. /*!
  3004. * @brief This API reads accel data along with sensor time if time is requested
  3005. * by user. Kindly refer the user guide(README.md) for more info.
  3006. */
  3007. static int8_t
  3008. get_accel_data(uint8_t len, struct bmi160_sensor_data* accel, const struct bmi160_dev* dev) {
  3009. int8_t rslt;
  3010. uint8_t idx = 0;
  3011. uint8_t data_array[9] = {0};
  3012. uint8_t time_0 = 0;
  3013. uint16_t time_1 = 0;
  3014. uint32_t time_2 = 0;
  3015. uint8_t lsb;
  3016. uint8_t msb;
  3017. int16_t msblsb;
  3018. /* read accel sensor data along with time if requested */
  3019. rslt = bmi160_get_regs(BMI160_ACCEL_DATA_ADDR, data_array, 6 + len, dev);
  3020. if(rslt == BMI160_OK) {
  3021. /* Accel Data */
  3022. lsb = data_array[idx++];
  3023. msb = data_array[idx++];
  3024. msblsb = (int16_t)((msb << 8) | lsb);
  3025. accel->x = msblsb; /* Data in X axis */
  3026. lsb = data_array[idx++];
  3027. msb = data_array[idx++];
  3028. msblsb = (int16_t)((msb << 8) | lsb);
  3029. accel->y = msblsb; /* Data in Y axis */
  3030. lsb = data_array[idx++];
  3031. msb = data_array[idx++];
  3032. msblsb = (int16_t)((msb << 8) | lsb);
  3033. accel->z = msblsb; /* Data in Z axis */
  3034. if(len == 3) {
  3035. time_0 = data_array[idx++];
  3036. time_1 = (uint16_t)(data_array[idx++] << 8);
  3037. time_2 = (uint32_t)(data_array[idx++] << 16);
  3038. accel->sensortime = (uint32_t)(time_2 | time_1 | time_0);
  3039. } else {
  3040. accel->sensortime = 0;
  3041. }
  3042. } else {
  3043. rslt = BMI160_E_COM_FAIL;
  3044. }
  3045. return rslt;
  3046. }
  3047. /*!
  3048. * @brief This API reads accel data along with sensor time if time is requested
  3049. * by user. Kindly refer the user guide(README.md) for more info.
  3050. */
  3051. static int8_t
  3052. get_gyro_data(uint8_t len, struct bmi160_sensor_data* gyro, const struct bmi160_dev* dev) {
  3053. int8_t rslt;
  3054. uint8_t idx = 0;
  3055. uint8_t data_array[15] = {0};
  3056. uint8_t time_0 = 0;
  3057. uint16_t time_1 = 0;
  3058. uint32_t time_2 = 0;
  3059. uint8_t lsb;
  3060. uint8_t msb;
  3061. int16_t msblsb;
  3062. if(len == 0) {
  3063. /* read gyro data only */
  3064. rslt = bmi160_get_regs(BMI160_GYRO_DATA_ADDR, data_array, 6, dev);
  3065. if(rslt == BMI160_OK) {
  3066. /* Gyro Data */
  3067. lsb = data_array[idx++];
  3068. msb = data_array[idx++];
  3069. msblsb = (int16_t)((msb << 8) | lsb);
  3070. gyro->x = msblsb; /* Data in X axis */
  3071. lsb = data_array[idx++];
  3072. msb = data_array[idx++];
  3073. msblsb = (int16_t)((msb << 8) | lsb);
  3074. gyro->y = msblsb; /* Data in Y axis */
  3075. lsb = data_array[idx++];
  3076. msb = data_array[idx++];
  3077. msblsb = (int16_t)((msb << 8) | lsb);
  3078. gyro->z = msblsb; /* Data in Z axis */
  3079. gyro->sensortime = 0;
  3080. } else {
  3081. rslt = BMI160_E_COM_FAIL;
  3082. }
  3083. } else {
  3084. /* read gyro sensor data along with time */
  3085. rslt = bmi160_get_regs(BMI160_GYRO_DATA_ADDR, data_array, 12 + len, dev);
  3086. if(rslt == BMI160_OK) {
  3087. /* Gyro Data */
  3088. lsb = data_array[idx++];
  3089. msb = data_array[idx++];
  3090. msblsb = (int16_t)((msb << 8) | lsb);
  3091. gyro->x = msblsb; /* gyro X axis data */
  3092. lsb = data_array[idx++];
  3093. msb = data_array[idx++];
  3094. msblsb = (int16_t)((msb << 8) | lsb);
  3095. gyro->y = msblsb; /* gyro Y axis data */
  3096. lsb = data_array[idx++];
  3097. msb = data_array[idx++];
  3098. msblsb = (int16_t)((msb << 8) | lsb);
  3099. gyro->z = msblsb; /* gyro Z axis data */
  3100. idx = idx + 6;
  3101. time_0 = data_array[idx++];
  3102. time_1 = (uint16_t)(data_array[idx++] << 8);
  3103. time_2 = (uint32_t)(data_array[idx++] << 16);
  3104. gyro->sensortime = (uint32_t)(time_2 | time_1 | time_0);
  3105. } else {
  3106. rslt = BMI160_E_COM_FAIL;
  3107. }
  3108. }
  3109. return rslt;
  3110. }
  3111. /*!
  3112. * @brief This API reads accel and gyro data along with sensor time
  3113. * if time is requested by user.
  3114. * Kindly refer the user guide(README.md) for more info.
  3115. */
  3116. static int8_t get_accel_gyro_data(
  3117. uint8_t len,
  3118. struct bmi160_sensor_data* accel,
  3119. struct bmi160_sensor_data* gyro,
  3120. const struct bmi160_dev* dev) {
  3121. int8_t rslt;
  3122. uint8_t idx = 0;
  3123. uint8_t data_array[15] = {0};
  3124. uint8_t time_0 = 0;
  3125. uint16_t time_1 = 0;
  3126. uint32_t time_2 = 0;
  3127. uint8_t lsb;
  3128. uint8_t msb;
  3129. int16_t msblsb;
  3130. /* read both accel and gyro sensor data
  3131. * along with time if requested */
  3132. rslt = bmi160_get_regs(BMI160_GYRO_DATA_ADDR, data_array, 12 + len, dev);
  3133. if(rslt == BMI160_OK) {
  3134. /* Gyro Data */
  3135. lsb = data_array[idx++];
  3136. msb = data_array[idx++];
  3137. msblsb = (int16_t)((msb << 8) | lsb);
  3138. gyro->x = msblsb; /* gyro X axis data */
  3139. lsb = data_array[idx++];
  3140. msb = data_array[idx++];
  3141. msblsb = (int16_t)((msb << 8) | lsb);
  3142. gyro->y = msblsb; /* gyro Y axis data */
  3143. lsb = data_array[idx++];
  3144. msb = data_array[idx++];
  3145. msblsb = (int16_t)((msb << 8) | lsb);
  3146. gyro->z = msblsb; /* gyro Z axis data */
  3147. /* Accel Data */
  3148. lsb = data_array[idx++];
  3149. msb = data_array[idx++];
  3150. msblsb = (int16_t)((msb << 8) | lsb);
  3151. accel->x = (int16_t)msblsb; /* accel X axis data */
  3152. lsb = data_array[idx++];
  3153. msb = data_array[idx++];
  3154. msblsb = (int16_t)((msb << 8) | lsb);
  3155. accel->y = (int16_t)msblsb; /* accel Y axis data */
  3156. lsb = data_array[idx++];
  3157. msb = data_array[idx++];
  3158. msblsb = (int16_t)((msb << 8) | lsb);
  3159. accel->z = (int16_t)msblsb; /* accel Z axis data */
  3160. if(len == 3) {
  3161. time_0 = data_array[idx++];
  3162. time_1 = (uint16_t)(data_array[idx++] << 8);
  3163. time_2 = (uint32_t)(data_array[idx++] << 16);
  3164. accel->sensortime = (uint32_t)(time_2 | time_1 | time_0);
  3165. gyro->sensortime = (uint32_t)(time_2 | time_1 | time_0);
  3166. } else {
  3167. accel->sensortime = 0;
  3168. gyro->sensortime = 0;
  3169. }
  3170. } else {
  3171. rslt = BMI160_E_COM_FAIL;
  3172. }
  3173. return rslt;
  3174. }
  3175. /*!
  3176. * @brief This API enables the any-motion interrupt for accel.
  3177. */
  3178. static int8_t enable_accel_any_motion_int(
  3179. const struct bmi160_acc_any_mot_int_cfg* any_motion_int_cfg,
  3180. struct bmi160_dev* dev) {
  3181. int8_t rslt;
  3182. uint8_t data = 0;
  3183. uint8_t temp = 0;
  3184. /* Enable any motion x, any motion y, any motion z
  3185. * in Int Enable 0 register */
  3186. rslt = bmi160_get_regs(BMI160_INT_ENABLE_0_ADDR, &data, 1, dev);
  3187. if(rslt == BMI160_OK) {
  3188. if(any_motion_int_cfg->anymotion_en == BMI160_ENABLE) {
  3189. temp = data & ~BMI160_ANY_MOTION_X_INT_EN_MASK;
  3190. /* Adding Any_motion x axis */
  3191. data = temp | (any_motion_int_cfg->anymotion_x & BMI160_ANY_MOTION_X_INT_EN_MASK);
  3192. temp = data & ~BMI160_ANY_MOTION_Y_INT_EN_MASK;
  3193. /* Adding Any_motion y axis */
  3194. data = temp |
  3195. ((any_motion_int_cfg->anymotion_y << 1) & BMI160_ANY_MOTION_Y_INT_EN_MASK);
  3196. temp = data & ~BMI160_ANY_MOTION_Z_INT_EN_MASK;
  3197. /* Adding Any_motion z axis */
  3198. data = temp |
  3199. ((any_motion_int_cfg->anymotion_z << 2) & BMI160_ANY_MOTION_Z_INT_EN_MASK);
  3200. /* any-motion feature selected*/
  3201. dev->any_sig_sel = BMI160_ANY_MOTION_ENABLED;
  3202. } else {
  3203. data = data & ~BMI160_ANY_MOTION_ALL_INT_EN_MASK;
  3204. /* neither any-motion feature nor sig-motion selected */
  3205. dev->any_sig_sel = BMI160_BOTH_ANY_SIG_MOTION_DISABLED;
  3206. }
  3207. /* write data to Int Enable 0 register */
  3208. rslt = bmi160_set_regs(BMI160_INT_ENABLE_0_ADDR, &data, 1, dev);
  3209. }
  3210. return rslt;
  3211. }
  3212. /*!
  3213. * @brief This API disable the sig-motion interrupt.
  3214. */
  3215. static int8_t disable_sig_motion_int(const struct bmi160_dev* dev) {
  3216. int8_t rslt;
  3217. uint8_t data = 0;
  3218. uint8_t temp = 0;
  3219. /* Disabling Significant motion interrupt if enabled */
  3220. rslt = bmi160_get_regs(BMI160_INT_MOTION_3_ADDR, &data, 1, dev);
  3221. if(rslt == BMI160_OK) {
  3222. temp = (data & BMI160_SIG_MOTION_SEL_MASK);
  3223. if(temp) {
  3224. temp = data & ~BMI160_SIG_MOTION_SEL_MASK;
  3225. data = temp;
  3226. /* Write data to register */
  3227. rslt = bmi160_set_regs(BMI160_INT_MOTION_3_ADDR, &data, 1, dev);
  3228. }
  3229. }
  3230. return rslt;
  3231. }
  3232. /*!
  3233. * @brief This API is used to map/unmap the Any/Sig motion, Step det/Low-g,
  3234. * Double tap, Single tap, Orientation, Flat, High-G, Nomotion interrupt pins.
  3235. */
  3236. static int8_t
  3237. map_feature_interrupt(const struct bmi160_int_settg* int_config, const struct bmi160_dev* dev) {
  3238. int8_t rslt;
  3239. uint8_t data[3] = {0, 0, 0};
  3240. uint8_t temp[3] = {0, 0, 0};
  3241. rslt = bmi160_get_regs(BMI160_INT_MAP_0_ADDR, data, 3, dev);
  3242. if(rslt == BMI160_OK) {
  3243. temp[0] = data[0] & ~int_mask_lookup_table[int_config->int_type];
  3244. temp[2] = data[2] & ~int_mask_lookup_table[int_config->int_type];
  3245. switch(int_config->int_channel) {
  3246. case BMI160_INT_CHANNEL_NONE:
  3247. data[0] = temp[0];
  3248. data[2] = temp[2];
  3249. break;
  3250. case BMI160_INT_CHANNEL_1:
  3251. data[0] = temp[0] | int_mask_lookup_table[int_config->int_type];
  3252. data[2] = temp[2];
  3253. break;
  3254. case BMI160_INT_CHANNEL_2:
  3255. data[2] = temp[2] | int_mask_lookup_table[int_config->int_type];
  3256. data[0] = temp[0];
  3257. break;
  3258. case BMI160_INT_CHANNEL_BOTH:
  3259. data[0] = temp[0] | int_mask_lookup_table[int_config->int_type];
  3260. data[2] = temp[2] | int_mask_lookup_table[int_config->int_type];
  3261. break;
  3262. default:
  3263. rslt = BMI160_E_OUT_OF_RANGE;
  3264. }
  3265. if(rslt == BMI160_OK) {
  3266. rslt = bmi160_set_regs(BMI160_INT_MAP_0_ADDR, data, 3, dev);
  3267. }
  3268. }
  3269. return rslt;
  3270. }
  3271. /*!
  3272. * @brief This API is used to map/unmap the Dataready(Accel & Gyro), FIFO full
  3273. * and FIFO watermark interrupt.
  3274. */
  3275. static int8_t map_hardware_interrupt(
  3276. const struct bmi160_int_settg* int_config,
  3277. const struct bmi160_dev* dev) {
  3278. int8_t rslt;
  3279. uint8_t data = 0;
  3280. uint8_t temp = 0;
  3281. rslt = bmi160_get_regs(BMI160_INT_MAP_1_ADDR, &data, 1, dev);
  3282. if(rslt == BMI160_OK) {
  3283. temp = data & ~int_mask_lookup_table[int_config->int_type];
  3284. temp = temp & ~((uint8_t)(int_mask_lookup_table[int_config->int_type] << 4));
  3285. switch(int_config->int_channel) {
  3286. case BMI160_INT_CHANNEL_NONE:
  3287. data = temp;
  3288. break;
  3289. case BMI160_INT_CHANNEL_1:
  3290. data = temp | (uint8_t)((int_mask_lookup_table[int_config->int_type]) << 4);
  3291. break;
  3292. case BMI160_INT_CHANNEL_2:
  3293. data = temp | int_mask_lookup_table[int_config->int_type];
  3294. break;
  3295. case BMI160_INT_CHANNEL_BOTH:
  3296. data = temp | int_mask_lookup_table[int_config->int_type];
  3297. data = data | (uint8_t)((int_mask_lookup_table[int_config->int_type]) << 4);
  3298. break;
  3299. default:
  3300. rslt = BMI160_E_OUT_OF_RANGE;
  3301. }
  3302. if(rslt == BMI160_OK) {
  3303. rslt = bmi160_set_regs(BMI160_INT_MAP_1_ADDR, &data, 1, dev);
  3304. }
  3305. }
  3306. return rslt;
  3307. }
  3308. /*!
  3309. * @brief This API configure the source of data(filter & pre-filter)
  3310. * for any-motion interrupt.
  3311. */
  3312. static int8_t config_any_motion_src(
  3313. const struct bmi160_acc_any_mot_int_cfg* any_motion_int_cfg,
  3314. const struct bmi160_dev* dev) {
  3315. int8_t rslt;
  3316. uint8_t data = 0;
  3317. uint8_t temp = 0;
  3318. /* Configure Int data 1 register to add source of interrupt */
  3319. rslt = bmi160_get_regs(BMI160_INT_DATA_1_ADDR, &data, 1, dev);
  3320. if(rslt == BMI160_OK) {
  3321. temp = data & ~BMI160_MOTION_SRC_INT_MASK;
  3322. data = temp | ((any_motion_int_cfg->anymotion_data_src << 7) & BMI160_MOTION_SRC_INT_MASK);
  3323. /* Write data to DATA 1 address */
  3324. rslt = bmi160_set_regs(BMI160_INT_DATA_1_ADDR, &data, 1, dev);
  3325. }
  3326. return rslt;
  3327. }
  3328. /*!
  3329. * @brief This API configure the duration and threshold of
  3330. * any-motion interrupt.
  3331. */
  3332. static int8_t config_any_dur_threshold(
  3333. const struct bmi160_acc_any_mot_int_cfg* any_motion_int_cfg,
  3334. const struct bmi160_dev* dev) {
  3335. int8_t rslt;
  3336. uint8_t data = 0;
  3337. uint8_t temp = 0;
  3338. uint8_t data_array[2] = {0};
  3339. uint8_t dur;
  3340. /* Configure Int Motion 0 register */
  3341. rslt = bmi160_get_regs(BMI160_INT_MOTION_0_ADDR, &data, 1, dev);
  3342. if(rslt == BMI160_OK) {
  3343. /* slope duration */
  3344. dur = (uint8_t)any_motion_int_cfg->anymotion_dur;
  3345. temp = data & ~BMI160_SLOPE_INT_DUR_MASK;
  3346. data = temp | (dur & BMI160_MOTION_SRC_INT_MASK);
  3347. data_array[0] = data;
  3348. /* add slope threshold */
  3349. data_array[1] = any_motion_int_cfg->anymotion_thr;
  3350. /* INT MOTION 0 and INT MOTION 1 address lie consecutively,
  3351. * hence writing data to respective registers at one go */
  3352. /* Writing to Int_motion 0 and
  3353. * Int_motion 1 Address simultaneously */
  3354. rslt = bmi160_set_regs(BMI160_INT_MOTION_0_ADDR, data_array, 2, dev);
  3355. }
  3356. return rslt;
  3357. }
  3358. /*!
  3359. * @brief This API configure necessary setting of any-motion interrupt.
  3360. */
  3361. static int8_t config_any_motion_int_settg(
  3362. const struct bmi160_int_settg* int_config,
  3363. const struct bmi160_acc_any_mot_int_cfg* any_motion_int_cfg,
  3364. const struct bmi160_dev* dev) {
  3365. int8_t rslt;
  3366. /* Configure Interrupt pins */
  3367. rslt = set_intr_pin_config(int_config, dev);
  3368. if(rslt == BMI160_OK) {
  3369. rslt = disable_sig_motion_int(dev);
  3370. if(rslt == BMI160_OK) {
  3371. rslt = map_feature_interrupt(int_config, dev);
  3372. if(rslt == BMI160_OK) {
  3373. rslt = config_any_motion_src(any_motion_int_cfg, dev);
  3374. if(rslt == BMI160_OK) {
  3375. rslt = config_any_dur_threshold(any_motion_int_cfg, dev);
  3376. }
  3377. }
  3378. }
  3379. }
  3380. return rslt;
  3381. }
  3382. /*!
  3383. * @brief This API enable the data ready interrupt.
  3384. */
  3385. static int8_t enable_data_ready_int(const struct bmi160_dev* dev) {
  3386. int8_t rslt;
  3387. uint8_t data = 0;
  3388. uint8_t temp = 0;
  3389. /* Enable data ready interrupt in Int Enable 1 register */
  3390. rslt = bmi160_get_regs(BMI160_INT_ENABLE_1_ADDR, &data, 1, dev);
  3391. if(rslt == BMI160_OK) {
  3392. temp = data & ~BMI160_DATA_RDY_INT_EN_MASK;
  3393. data = temp | ((1 << 4) & BMI160_DATA_RDY_INT_EN_MASK);
  3394. /* Writing data to INT ENABLE 1 Address */
  3395. rslt = bmi160_set_regs(BMI160_INT_ENABLE_1_ADDR, &data, 1, dev);
  3396. }
  3397. return rslt;
  3398. }
  3399. /*!
  3400. * @brief This API enables the no motion/slow motion interrupt.
  3401. */
  3402. static int8_t enable_no_motion_int(
  3403. const struct bmi160_acc_no_motion_int_cfg* no_mot_int_cfg,
  3404. const struct bmi160_dev* dev) {
  3405. int8_t rslt;
  3406. uint8_t data = 0;
  3407. uint8_t temp = 0;
  3408. /* Enable no motion x, no motion y, no motion z
  3409. * in Int Enable 2 register */
  3410. rslt = bmi160_get_regs(BMI160_INT_ENABLE_2_ADDR, &data, 1, dev);
  3411. if(rslt == BMI160_OK) {
  3412. if(no_mot_int_cfg->no_motion_x == 1) {
  3413. temp = data & ~BMI160_NO_MOTION_X_INT_EN_MASK;
  3414. /* Adding No_motion x axis */
  3415. data = temp | (1 & BMI160_NO_MOTION_X_INT_EN_MASK);
  3416. }
  3417. if(no_mot_int_cfg->no_motion_y == 1) {
  3418. temp = data & ~BMI160_NO_MOTION_Y_INT_EN_MASK;
  3419. /* Adding No_motion x axis */
  3420. data = temp | ((1 << 1) & BMI160_NO_MOTION_Y_INT_EN_MASK);
  3421. }
  3422. if(no_mot_int_cfg->no_motion_z == 1) {
  3423. temp = data & ~BMI160_NO_MOTION_Z_INT_EN_MASK;
  3424. /* Adding No_motion x axis */
  3425. data = temp | ((1 << 2) & BMI160_NO_MOTION_Z_INT_EN_MASK);
  3426. }
  3427. /* write data to Int Enable 2 register */
  3428. rslt = bmi160_set_regs(BMI160_INT_ENABLE_2_ADDR, &data, 1, dev);
  3429. }
  3430. return rslt;
  3431. }
  3432. /*!
  3433. * @brief This API configure the interrupt PIN setting for
  3434. * no motion/slow motion interrupt.
  3435. */
  3436. static int8_t config_no_motion_int_settg(
  3437. const struct bmi160_int_settg* int_config,
  3438. const struct bmi160_acc_no_motion_int_cfg* no_mot_int_cfg,
  3439. const struct bmi160_dev* dev) {
  3440. int8_t rslt;
  3441. /* Configure Interrupt pins */
  3442. rslt = set_intr_pin_config(int_config, dev);
  3443. if(rslt == BMI160_OK) {
  3444. rslt = map_feature_interrupt(int_config, dev);
  3445. if(rslt == BMI160_OK) {
  3446. rslt = config_no_motion_data_src(no_mot_int_cfg, dev);
  3447. if(rslt == BMI160_OK) {
  3448. rslt = config_no_motion_dur_thr(no_mot_int_cfg, dev);
  3449. }
  3450. }
  3451. }
  3452. return rslt;
  3453. }
  3454. /*!
  3455. * @brief This API configure the source of interrupt for no motion.
  3456. */
  3457. static int8_t config_no_motion_data_src(
  3458. const struct bmi160_acc_no_motion_int_cfg* no_mot_int_cfg,
  3459. const struct bmi160_dev* dev) {
  3460. int8_t rslt;
  3461. uint8_t data = 0;
  3462. uint8_t temp = 0;
  3463. /* Configure Int data 1 register to add source of interrupt */
  3464. rslt = bmi160_get_regs(BMI160_INT_DATA_1_ADDR, &data, 1, dev);
  3465. if(rslt == BMI160_OK) {
  3466. temp = data & ~BMI160_MOTION_SRC_INT_MASK;
  3467. data = temp | ((no_mot_int_cfg->no_motion_src << 7) & BMI160_MOTION_SRC_INT_MASK);
  3468. /* Write data to DATA 1 address */
  3469. rslt = bmi160_set_regs(BMI160_INT_DATA_1_ADDR, &data, 1, dev);
  3470. }
  3471. return rslt;
  3472. }
  3473. /*!
  3474. * @brief This API configure the duration and threshold of
  3475. * no motion/slow motion interrupt along with selection of no/slow motion.
  3476. */
  3477. static int8_t config_no_motion_dur_thr(
  3478. const struct bmi160_acc_no_motion_int_cfg* no_mot_int_cfg,
  3479. const struct bmi160_dev* dev) {
  3480. int8_t rslt;
  3481. uint8_t data = 0;
  3482. uint8_t temp = 0;
  3483. uint8_t temp_1 = 0;
  3484. uint8_t reg_addr;
  3485. uint8_t data_array[2] = {0};
  3486. /* Configuring INT_MOTION register */
  3487. reg_addr = BMI160_INT_MOTION_0_ADDR;
  3488. rslt = bmi160_get_regs(reg_addr, &data, 1, dev);
  3489. if(rslt == BMI160_OK) {
  3490. temp = data & ~BMI160_NO_MOTION_INT_DUR_MASK;
  3491. /* Adding no_motion duration */
  3492. data = temp | ((no_mot_int_cfg->no_motion_dur << 2) & BMI160_NO_MOTION_INT_DUR_MASK);
  3493. /* Write data to NO_MOTION 0 address */
  3494. rslt = bmi160_set_regs(reg_addr, &data, 1, dev);
  3495. if(rslt == BMI160_OK) {
  3496. reg_addr = BMI160_INT_MOTION_3_ADDR;
  3497. rslt = bmi160_get_regs(reg_addr, &data, 1, dev);
  3498. if(rslt == BMI160_OK) {
  3499. temp = data & ~BMI160_NO_MOTION_SEL_BIT_MASK;
  3500. /* Adding no_motion_sel bit */
  3501. temp_1 = (no_mot_int_cfg->no_motion_sel & BMI160_NO_MOTION_SEL_BIT_MASK);
  3502. data = (temp | temp_1);
  3503. data_array[1] = data;
  3504. /* Adding no motion threshold */
  3505. data_array[0] = no_mot_int_cfg->no_motion_thres;
  3506. reg_addr = BMI160_INT_MOTION_2_ADDR;
  3507. /* writing data to INT_MOTION 2 and INT_MOTION 3
  3508. * address simultaneously */
  3509. rslt = bmi160_set_regs(reg_addr, data_array, 2, dev);
  3510. }
  3511. }
  3512. }
  3513. return rslt;
  3514. }
  3515. /*!
  3516. * @brief This API enables the sig-motion motion interrupt.
  3517. */
  3518. static int8_t enable_sig_motion_int(
  3519. const struct bmi160_acc_sig_mot_int_cfg* sig_mot_int_cfg,
  3520. struct bmi160_dev* dev) {
  3521. int8_t rslt;
  3522. uint8_t data = 0;
  3523. uint8_t temp = 0;
  3524. /* For significant motion,enable any motion x,any motion y,
  3525. * any motion z in Int Enable 0 register */
  3526. rslt = bmi160_get_regs(BMI160_INT_ENABLE_0_ADDR, &data, 1, dev);
  3527. if(rslt == BMI160_OK) {
  3528. if(sig_mot_int_cfg->sig_en == BMI160_ENABLE) {
  3529. temp = data & ~BMI160_SIG_MOTION_INT_EN_MASK;
  3530. data = temp | (7 & BMI160_SIG_MOTION_INT_EN_MASK);
  3531. /* sig-motion feature selected*/
  3532. dev->any_sig_sel = BMI160_SIG_MOTION_ENABLED;
  3533. } else {
  3534. data = data & ~BMI160_SIG_MOTION_INT_EN_MASK;
  3535. /* neither any-motion feature nor sig-motion selected */
  3536. dev->any_sig_sel = BMI160_BOTH_ANY_SIG_MOTION_DISABLED;
  3537. }
  3538. /* write data to Int Enable 0 register */
  3539. rslt = bmi160_set_regs(BMI160_INT_ENABLE_0_ADDR, &data, 1, dev);
  3540. }
  3541. return rslt;
  3542. }
  3543. /*!
  3544. * @brief This API configure the interrupt PIN setting for
  3545. * significant motion interrupt.
  3546. */
  3547. static int8_t config_sig_motion_int_settg(
  3548. const struct bmi160_int_settg* int_config,
  3549. const struct bmi160_acc_sig_mot_int_cfg* sig_mot_int_cfg,
  3550. const struct bmi160_dev* dev) {
  3551. int8_t rslt;
  3552. /* Configure Interrupt pins */
  3553. rslt = set_intr_pin_config(int_config, dev);
  3554. if(rslt == BMI160_OK) {
  3555. rslt = map_feature_interrupt(int_config, dev);
  3556. if(rslt == BMI160_OK) {
  3557. rslt = config_sig_motion_data_src(sig_mot_int_cfg, dev);
  3558. if(rslt == BMI160_OK) {
  3559. rslt = config_sig_dur_threshold(sig_mot_int_cfg, dev);
  3560. }
  3561. }
  3562. }
  3563. return rslt;
  3564. }
  3565. /*!
  3566. * @brief This API configure the source of data(filter & pre-filter)
  3567. * for sig motion interrupt.
  3568. */
  3569. static int8_t config_sig_motion_data_src(
  3570. const struct bmi160_acc_sig_mot_int_cfg* sig_mot_int_cfg,
  3571. const struct bmi160_dev* dev) {
  3572. int8_t rslt;
  3573. uint8_t data = 0;
  3574. uint8_t temp = 0;
  3575. /* Configure Int data 1 register to add source of interrupt */
  3576. rslt = bmi160_get_regs(BMI160_INT_DATA_1_ADDR, &data, 1, dev);
  3577. if(rslt == BMI160_OK) {
  3578. temp = data & ~BMI160_MOTION_SRC_INT_MASK;
  3579. data = temp | ((sig_mot_int_cfg->sig_data_src << 7) & BMI160_MOTION_SRC_INT_MASK);
  3580. /* Write data to DATA 1 address */
  3581. rslt = bmi160_set_regs(BMI160_INT_DATA_1_ADDR, &data, 1, dev);
  3582. }
  3583. return rslt;
  3584. }
  3585. /*!
  3586. * @brief This API configure the threshold, skip and proof time of
  3587. * sig motion interrupt.
  3588. */
  3589. static int8_t config_sig_dur_threshold(
  3590. const struct bmi160_acc_sig_mot_int_cfg* sig_mot_int_cfg,
  3591. const struct bmi160_dev* dev) {
  3592. int8_t rslt;
  3593. uint8_t data;
  3594. uint8_t temp = 0;
  3595. /* Configuring INT_MOTION registers */
  3596. /* Write significant motion threshold.
  3597. * This threshold is same as any motion threshold */
  3598. data = sig_mot_int_cfg->sig_mot_thres;
  3599. /* Write data to INT_MOTION 1 address */
  3600. rslt = bmi160_set_regs(BMI160_INT_MOTION_1_ADDR, &data, 1, dev);
  3601. if(rslt == BMI160_OK) {
  3602. rslt = bmi160_get_regs(BMI160_INT_MOTION_3_ADDR, &data, 1, dev);
  3603. if(rslt == BMI160_OK) {
  3604. temp = data & ~BMI160_SIG_MOTION_SKIP_MASK;
  3605. /* adding skip time of sig_motion interrupt*/
  3606. data = temp | ((sig_mot_int_cfg->sig_mot_skip << 2) & BMI160_SIG_MOTION_SKIP_MASK);
  3607. temp = data & ~BMI160_SIG_MOTION_PROOF_MASK;
  3608. /* adding proof time of sig_motion interrupt */
  3609. data = temp | ((sig_mot_int_cfg->sig_mot_proof << 4) & BMI160_SIG_MOTION_PROOF_MASK);
  3610. /* configure the int_sig_mot_sel bit to select
  3611. * significant motion interrupt */
  3612. temp = data & ~BMI160_SIG_MOTION_SEL_MASK;
  3613. data = temp | ((sig_mot_int_cfg->sig_en << 1) & BMI160_SIG_MOTION_SEL_MASK);
  3614. rslt = bmi160_set_regs(BMI160_INT_MOTION_3_ADDR, &data, 1, dev);
  3615. }
  3616. }
  3617. return rslt;
  3618. }
  3619. /*!
  3620. * @brief This API enables the step detector interrupt.
  3621. */
  3622. static int8_t enable_step_detect_int(
  3623. const struct bmi160_acc_step_detect_int_cfg* step_detect_int_cfg,
  3624. const struct bmi160_dev* dev) {
  3625. int8_t rslt;
  3626. uint8_t data = 0;
  3627. uint8_t temp = 0;
  3628. /* Enable data ready interrupt in Int Enable 2 register */
  3629. rslt = bmi160_get_regs(BMI160_INT_ENABLE_2_ADDR, &data, 1, dev);
  3630. if(rslt == BMI160_OK) {
  3631. temp = data & ~BMI160_STEP_DETECT_INT_EN_MASK;
  3632. data = temp |
  3633. ((step_detect_int_cfg->step_detector_en << 3) & BMI160_STEP_DETECT_INT_EN_MASK);
  3634. /* Writing data to INT ENABLE 2 Address */
  3635. rslt = bmi160_set_regs(BMI160_INT_ENABLE_2_ADDR, &data, 1, dev);
  3636. }
  3637. return rslt;
  3638. }
  3639. /*!
  3640. * @brief This API configure the step detector parameter.
  3641. */
  3642. static int8_t config_step_detect(
  3643. const struct bmi160_acc_step_detect_int_cfg* step_detect_int_cfg,
  3644. const struct bmi160_dev* dev) {
  3645. int8_t rslt;
  3646. uint8_t temp = 0;
  3647. uint8_t data_array[2] = {0};
  3648. if(step_detect_int_cfg->step_detector_mode == BMI160_STEP_DETECT_NORMAL) {
  3649. /* Normal mode setting */
  3650. data_array[0] = 0x15;
  3651. data_array[1] = 0x03;
  3652. } else if(step_detect_int_cfg->step_detector_mode == BMI160_STEP_DETECT_SENSITIVE) {
  3653. /* Sensitive mode setting */
  3654. data_array[0] = 0x2D;
  3655. data_array[1] = 0x00;
  3656. } else if(step_detect_int_cfg->step_detector_mode == BMI160_STEP_DETECT_ROBUST) {
  3657. /* Robust mode setting */
  3658. data_array[0] = 0x1D;
  3659. data_array[1] = 0x07;
  3660. } else if(step_detect_int_cfg->step_detector_mode == BMI160_STEP_DETECT_USER_DEFINE) {
  3661. /* Non recommended User defined setting */
  3662. /* Configuring STEP_CONFIG register */
  3663. rslt = bmi160_get_regs(BMI160_INT_STEP_CONFIG_0_ADDR, &data_array[0], 2, dev);
  3664. if(rslt == BMI160_OK) {
  3665. temp = data_array[0] & ~BMI160_STEP_DETECT_MIN_THRES_MASK;
  3666. /* Adding min_threshold */
  3667. data_array[0] = temp | ((step_detect_int_cfg->min_threshold << 3) &
  3668. BMI160_STEP_DETECT_MIN_THRES_MASK);
  3669. temp = data_array[0] & ~BMI160_STEP_DETECT_STEPTIME_MIN_MASK;
  3670. /* Adding steptime_min */
  3671. data_array[0] = temp | ((step_detect_int_cfg->steptime_min) &
  3672. BMI160_STEP_DETECT_STEPTIME_MIN_MASK);
  3673. temp = data_array[1] & ~BMI160_STEP_MIN_BUF_MASK;
  3674. /* Adding steptime_min */
  3675. data_array[1] = temp |
  3676. ((step_detect_int_cfg->step_min_buf) & BMI160_STEP_MIN_BUF_MASK);
  3677. }
  3678. }
  3679. /* Write data to STEP_CONFIG register */
  3680. rslt = bmi160_set_regs(BMI160_INT_STEP_CONFIG_0_ADDR, data_array, 2, dev);
  3681. return rslt;
  3682. }
  3683. /*!
  3684. * @brief This API enables the single/double tap interrupt.
  3685. */
  3686. static int8_t enable_tap_int(
  3687. const struct bmi160_int_settg* int_config,
  3688. const struct bmi160_acc_tap_int_cfg* tap_int_cfg,
  3689. const struct bmi160_dev* dev) {
  3690. int8_t rslt;
  3691. uint8_t data = 0;
  3692. uint8_t temp = 0;
  3693. /* Enable single tap or double tap interrupt in Int Enable 0 register */
  3694. rslt = bmi160_get_regs(BMI160_INT_ENABLE_0_ADDR, &data, 1, dev);
  3695. if(rslt == BMI160_OK) {
  3696. if(int_config->int_type == BMI160_ACC_SINGLE_TAP_INT) {
  3697. temp = data & ~BMI160_SINGLE_TAP_INT_EN_MASK;
  3698. data = temp | ((tap_int_cfg->tap_en << 5) & BMI160_SINGLE_TAP_INT_EN_MASK);
  3699. } else {
  3700. temp = data & ~BMI160_DOUBLE_TAP_INT_EN_MASK;
  3701. data = temp | ((tap_int_cfg->tap_en << 4) & BMI160_DOUBLE_TAP_INT_EN_MASK);
  3702. }
  3703. /* Write to Enable 0 Address */
  3704. rslt = bmi160_set_regs(BMI160_INT_ENABLE_0_ADDR, &data, 1, dev);
  3705. }
  3706. return rslt;
  3707. }
  3708. /*!
  3709. * @brief This API configure the interrupt PIN setting for
  3710. * tap interrupt.
  3711. */
  3712. static int8_t config_tap_int_settg(
  3713. const struct bmi160_int_settg* int_config,
  3714. const struct bmi160_acc_tap_int_cfg* tap_int_cfg,
  3715. const struct bmi160_dev* dev) {
  3716. int8_t rslt;
  3717. /* Configure Interrupt pins */
  3718. rslt = set_intr_pin_config(int_config, dev);
  3719. if(rslt == BMI160_OK) {
  3720. rslt = map_feature_interrupt(int_config, dev);
  3721. if(rslt == BMI160_OK) {
  3722. rslt = config_tap_data_src(tap_int_cfg, dev);
  3723. if(rslt == BMI160_OK) {
  3724. rslt = config_tap_param(int_config, tap_int_cfg, dev);
  3725. }
  3726. }
  3727. }
  3728. return rslt;
  3729. }
  3730. /*!
  3731. * @brief This API configure the source of data(filter & pre-filter)
  3732. * for tap interrupt.
  3733. */
  3734. static int8_t config_tap_data_src(
  3735. const struct bmi160_acc_tap_int_cfg* tap_int_cfg,
  3736. const struct bmi160_dev* dev) {
  3737. int8_t rslt;
  3738. uint8_t data = 0;
  3739. uint8_t temp = 0;
  3740. /* Configure Int data 0 register to add source of interrupt */
  3741. rslt = bmi160_get_regs(BMI160_INT_DATA_0_ADDR, &data, 1, dev);
  3742. if(rslt == BMI160_OK) {
  3743. temp = data & ~BMI160_TAP_SRC_INT_MASK;
  3744. data = temp | ((tap_int_cfg->tap_data_src << 3) & BMI160_TAP_SRC_INT_MASK);
  3745. /* Write data to Data 0 address */
  3746. rslt = bmi160_set_regs(BMI160_INT_DATA_0_ADDR, &data, 1, dev);
  3747. }
  3748. return rslt;
  3749. }
  3750. /*!
  3751. * @brief This API configure the parameters of tap interrupt.
  3752. * Threshold, quite, shock, and duration.
  3753. */
  3754. static int8_t config_tap_param(
  3755. const struct bmi160_int_settg* int_config,
  3756. const struct bmi160_acc_tap_int_cfg* tap_int_cfg,
  3757. const struct bmi160_dev* dev) {
  3758. int8_t rslt;
  3759. uint8_t temp = 0;
  3760. uint8_t data = 0;
  3761. uint8_t data_array[2] = {0};
  3762. uint8_t count = 0;
  3763. uint8_t dur, shock, quiet, thres;
  3764. /* Configure tap 0 register for tap shock,tap quiet duration
  3765. * in case of single tap interrupt */
  3766. rslt = bmi160_get_regs(BMI160_INT_TAP_0_ADDR, data_array, 2, dev);
  3767. if(rslt == BMI160_OK) {
  3768. data = data_array[count];
  3769. if(int_config->int_type == BMI160_ACC_DOUBLE_TAP_INT) {
  3770. dur = (uint8_t)tap_int_cfg->tap_dur;
  3771. temp = (data & ~BMI160_TAP_DUR_MASK);
  3772. /* Add tap duration data in case of
  3773. * double tap interrupt */
  3774. data = temp | (dur & BMI160_TAP_DUR_MASK);
  3775. }
  3776. shock = (uint8_t)tap_int_cfg->tap_shock;
  3777. temp = data & ~BMI160_TAP_SHOCK_DUR_MASK;
  3778. data = temp | ((shock << 6) & BMI160_TAP_SHOCK_DUR_MASK);
  3779. quiet = (uint8_t)tap_int_cfg->tap_quiet;
  3780. temp = data & ~BMI160_TAP_QUIET_DUR_MASK;
  3781. data = temp | ((quiet << 7) & BMI160_TAP_QUIET_DUR_MASK);
  3782. data_array[count++] = data;
  3783. data = data_array[count];
  3784. thres = (uint8_t)tap_int_cfg->tap_thr;
  3785. temp = data & ~BMI160_TAP_THRES_MASK;
  3786. data = temp | (thres & BMI160_TAP_THRES_MASK);
  3787. data_array[count++] = data;
  3788. /* TAP 0 and TAP 1 address lie consecutively,
  3789. * hence writing data to respective registers at one go */
  3790. /* Writing to Tap 0 and Tap 1 Address simultaneously */
  3791. rslt = bmi160_set_regs(BMI160_INT_TAP_0_ADDR, data_array, count, dev);
  3792. }
  3793. return rslt;
  3794. }
  3795. /*!
  3796. * @brief This API configure the secondary interface.
  3797. */
  3798. static int8_t config_sec_if(const struct bmi160_dev* dev) {
  3799. int8_t rslt;
  3800. uint8_t if_conf = 0;
  3801. uint8_t cmd = BMI160_AUX_NORMAL_MODE;
  3802. /* set the aux power mode to normal*/
  3803. rslt = bmi160_set_regs(BMI160_COMMAND_REG_ADDR, &cmd, 1, dev);
  3804. if(rslt == BMI160_OK) {
  3805. /* 0.5ms delay - refer datasheet table 24*/
  3806. dev->delay_ms(1);
  3807. rslt = bmi160_get_regs(BMI160_IF_CONF_ADDR, &if_conf, 1, dev);
  3808. if_conf |= (uint8_t)(1 << 5);
  3809. if(rslt == BMI160_OK) {
  3810. /*enable the secondary interface also*/
  3811. rslt = bmi160_set_regs(BMI160_IF_CONF_ADDR, &if_conf, 1, dev);
  3812. }
  3813. }
  3814. return rslt;
  3815. }
  3816. /*!
  3817. * @brief This API configure the ODR of the auxiliary sensor.
  3818. */
  3819. static int8_t config_aux_odr(const struct bmi160_dev* dev) {
  3820. int8_t rslt;
  3821. uint8_t aux_odr;
  3822. rslt = bmi160_get_regs(BMI160_AUX_ODR_ADDR, &aux_odr, 1, dev);
  3823. if(rslt == BMI160_OK) {
  3824. aux_odr = (uint8_t)(dev->aux_cfg.aux_odr);
  3825. /* Set the secondary interface ODR
  3826. * i.e polling rate of secondary sensor */
  3827. rslt = bmi160_set_regs(BMI160_AUX_ODR_ADDR, &aux_odr, 1, dev);
  3828. dev->delay_ms(BMI160_AUX_COM_DELAY);
  3829. }
  3830. return rslt;
  3831. }
  3832. /*!
  3833. * @brief This API maps the actual burst read length set by user.
  3834. */
  3835. static int8_t map_read_len(uint16_t* len, const struct bmi160_dev* dev) {
  3836. int8_t rslt = BMI160_OK;
  3837. switch(dev->aux_cfg.aux_rd_burst_len) {
  3838. case BMI160_AUX_READ_LEN_0:
  3839. *len = 1;
  3840. break;
  3841. case BMI160_AUX_READ_LEN_1:
  3842. *len = 2;
  3843. break;
  3844. case BMI160_AUX_READ_LEN_2:
  3845. *len = 6;
  3846. break;
  3847. case BMI160_AUX_READ_LEN_3:
  3848. *len = 8;
  3849. break;
  3850. default:
  3851. rslt = BMI160_E_INVALID_INPUT;
  3852. break;
  3853. }
  3854. return rslt;
  3855. }
  3856. /*!
  3857. * @brief This API configure the settings of auxiliary sensor.
  3858. */
  3859. static int8_t config_aux_settg(const struct bmi160_dev* dev) {
  3860. int8_t rslt;
  3861. rslt = config_sec_if(dev);
  3862. if(rslt == BMI160_OK) {
  3863. /* Configures the auxiliary interface settings */
  3864. rslt = bmi160_config_aux_mode(dev);
  3865. }
  3866. return rslt;
  3867. }
  3868. /*!
  3869. * @brief This API extract the read data from auxiliary sensor.
  3870. */
  3871. static int8_t extract_aux_read(
  3872. uint16_t map_len,
  3873. uint8_t reg_addr,
  3874. uint8_t* aux_data,
  3875. uint16_t len,
  3876. const struct bmi160_dev* dev) {
  3877. int8_t rslt = BMI160_OK;
  3878. uint8_t data[8] = {
  3879. 0,
  3880. };
  3881. uint8_t read_addr = BMI160_AUX_DATA_ADDR;
  3882. uint8_t count = 0;
  3883. uint8_t read_count;
  3884. uint8_t read_len = (uint8_t)map_len;
  3885. for(; count < len;) {
  3886. /* set address to read */
  3887. rslt = bmi160_set_regs(BMI160_AUX_IF_2_ADDR, &reg_addr, 1, dev);
  3888. dev->delay_ms(BMI160_AUX_COM_DELAY);
  3889. if(rslt == BMI160_OK) {
  3890. rslt = bmi160_get_regs(read_addr, data, map_len, dev);
  3891. if(rslt == BMI160_OK) {
  3892. read_count = 0;
  3893. /* if read len is less the burst read len
  3894. * mention by user*/
  3895. if(len < map_len) {
  3896. read_len = (uint8_t)len;
  3897. } else if((len - count) < map_len) {
  3898. read_len = (uint8_t)(len - count);
  3899. }
  3900. for(; read_count < read_len; read_count++) {
  3901. aux_data[count + read_count] = data[read_count];
  3902. }
  3903. reg_addr += (uint8_t)map_len;
  3904. count += (uint8_t)map_len;
  3905. } else {
  3906. rslt = BMI160_E_COM_FAIL;
  3907. break;
  3908. }
  3909. }
  3910. }
  3911. return rslt;
  3912. }
  3913. /*!
  3914. * @brief This API enables the orient interrupt.
  3915. */
  3916. static int8_t enable_orient_int(
  3917. const struct bmi160_acc_orient_int_cfg* orient_int_cfg,
  3918. const struct bmi160_dev* dev) {
  3919. int8_t rslt;
  3920. uint8_t data = 0;
  3921. uint8_t temp = 0;
  3922. /* Enable data ready interrupt in Int Enable 0 register */
  3923. rslt = bmi160_get_regs(BMI160_INT_ENABLE_0_ADDR, &data, 1, dev);
  3924. if(rslt == BMI160_OK) {
  3925. temp = data & ~BMI160_ORIENT_INT_EN_MASK;
  3926. data = temp | ((orient_int_cfg->orient_en << 6) & BMI160_ORIENT_INT_EN_MASK);
  3927. /* write data to Int Enable 0 register */
  3928. rslt = bmi160_set_regs(BMI160_INT_ENABLE_0_ADDR, &data, 1, dev);
  3929. }
  3930. return rslt;
  3931. }
  3932. /*!
  3933. * @brief This API configure the necessary setting of orientation interrupt.
  3934. */
  3935. static int8_t config_orient_int_settg(
  3936. const struct bmi160_acc_orient_int_cfg* orient_int_cfg,
  3937. const struct bmi160_dev* dev) {
  3938. int8_t rslt;
  3939. uint8_t data = 0;
  3940. uint8_t temp = 0;
  3941. uint8_t data_array[2] = {0, 0};
  3942. /* Configuring INT_ORIENT registers */
  3943. rslt = bmi160_get_regs(BMI160_INT_ORIENT_0_ADDR, data_array, 2, dev);
  3944. if(rslt == BMI160_OK) {
  3945. data = data_array[0];
  3946. temp = data & ~BMI160_ORIENT_MODE_MASK;
  3947. /* Adding Orientation mode */
  3948. data = temp | ((orient_int_cfg->orient_mode) & BMI160_ORIENT_MODE_MASK);
  3949. temp = data & ~BMI160_ORIENT_BLOCK_MASK;
  3950. /* Adding Orientation blocking */
  3951. data = temp | ((orient_int_cfg->orient_blocking << 2) & BMI160_ORIENT_BLOCK_MASK);
  3952. temp = data & ~BMI160_ORIENT_HYST_MASK;
  3953. /* Adding Orientation hysteresis */
  3954. data = temp | ((orient_int_cfg->orient_hyst << 4) & BMI160_ORIENT_HYST_MASK);
  3955. data_array[0] = data;
  3956. data = data_array[1];
  3957. temp = data & ~BMI160_ORIENT_THETA_MASK;
  3958. /* Adding Orientation threshold */
  3959. data = temp | ((orient_int_cfg->orient_theta) & BMI160_ORIENT_THETA_MASK);
  3960. temp = data & ~BMI160_ORIENT_UD_ENABLE;
  3961. /* Adding Orient_ud_en */
  3962. data = temp | ((orient_int_cfg->orient_ud_en << 6) & BMI160_ORIENT_UD_ENABLE);
  3963. temp = data & ~BMI160_AXES_EN_MASK;
  3964. /* Adding axes_en */
  3965. data = temp | ((orient_int_cfg->axes_ex << 7) & BMI160_AXES_EN_MASK);
  3966. data_array[1] = data;
  3967. /* Writing data to INT_ORIENT 0 and INT_ORIENT 1
  3968. * registers simultaneously */
  3969. rslt = bmi160_set_regs(BMI160_INT_ORIENT_0_ADDR, data_array, 2, dev);
  3970. }
  3971. return rslt;
  3972. }
  3973. /*!
  3974. * @brief This API enables the flat interrupt.
  3975. */
  3976. static int8_t enable_flat_int(
  3977. const struct bmi160_acc_flat_detect_int_cfg* flat_int,
  3978. const struct bmi160_dev* dev) {
  3979. int8_t rslt;
  3980. uint8_t data = 0;
  3981. uint8_t temp = 0;
  3982. /* Enable flat interrupt in Int Enable 0 register */
  3983. rslt = bmi160_get_regs(BMI160_INT_ENABLE_0_ADDR, &data, 1, dev);
  3984. if(rslt == BMI160_OK) {
  3985. temp = data & ~BMI160_FLAT_INT_EN_MASK;
  3986. data = temp | ((flat_int->flat_en << 7) & BMI160_FLAT_INT_EN_MASK);
  3987. /* write data to Int Enable 0 register */
  3988. rslt = bmi160_set_regs(BMI160_INT_ENABLE_0_ADDR, &data, 1, dev);
  3989. }
  3990. return rslt;
  3991. }
  3992. /*!
  3993. * @brief This API configure the necessary setting of flat interrupt.
  3994. */
  3995. static int8_t config_flat_int_settg(
  3996. const struct bmi160_acc_flat_detect_int_cfg* flat_int,
  3997. const struct bmi160_dev* dev) {
  3998. int8_t rslt;
  3999. uint8_t data = 0;
  4000. uint8_t temp = 0;
  4001. uint8_t data_array[2] = {0, 0};
  4002. /* Configuring INT_FLAT register */
  4003. rslt = bmi160_get_regs(BMI160_INT_FLAT_0_ADDR, data_array, 2, dev);
  4004. if(rslt == BMI160_OK) {
  4005. data = data_array[0];
  4006. temp = data & ~BMI160_FLAT_THRES_MASK;
  4007. /* Adding flat theta */
  4008. data = temp | ((flat_int->flat_theta) & BMI160_FLAT_THRES_MASK);
  4009. data_array[0] = data;
  4010. data = data_array[1];
  4011. temp = data & ~BMI160_FLAT_HOLD_TIME_MASK;
  4012. /* Adding flat hold time */
  4013. data = temp | ((flat_int->flat_hold_time << 4) & BMI160_FLAT_HOLD_TIME_MASK);
  4014. temp = data & ~BMI160_FLAT_HYST_MASK;
  4015. /* Adding flat hysteresis */
  4016. data = temp | ((flat_int->flat_hy) & BMI160_FLAT_HYST_MASK);
  4017. data_array[1] = data;
  4018. /* Writing data to INT_FLAT 0 and INT_FLAT 1
  4019. * registers simultaneously */
  4020. rslt = bmi160_set_regs(BMI160_INT_FLAT_0_ADDR, data_array, 2, dev);
  4021. }
  4022. return rslt;
  4023. }
  4024. /*!
  4025. * @brief This API enables the Low-g interrupt.
  4026. */
  4027. static int8_t enable_low_g_int(
  4028. const struct bmi160_acc_low_g_int_cfg* low_g_int,
  4029. const struct bmi160_dev* dev) {
  4030. int8_t rslt;
  4031. uint8_t data = 0;
  4032. uint8_t temp = 0;
  4033. /* Enable low-g interrupt in Int Enable 1 register */
  4034. rslt = bmi160_get_regs(BMI160_INT_ENABLE_1_ADDR, &data, 1, dev);
  4035. if(rslt == BMI160_OK) {
  4036. temp = data & ~BMI160_LOW_G_INT_EN_MASK;
  4037. data = temp | ((low_g_int->low_en << 3) & BMI160_LOW_G_INT_EN_MASK);
  4038. /* write data to Int Enable 0 register */
  4039. rslt = bmi160_set_regs(BMI160_INT_ENABLE_1_ADDR, &data, 1, dev);
  4040. }
  4041. return rslt;
  4042. }
  4043. /*!
  4044. * @brief This API configure the source of data(filter & pre-filter)
  4045. * for low-g interrupt.
  4046. */
  4047. static int8_t config_low_g_data_src(
  4048. const struct bmi160_acc_low_g_int_cfg* low_g_int,
  4049. const struct bmi160_dev* dev) {
  4050. int8_t rslt;
  4051. uint8_t data = 0;
  4052. uint8_t temp = 0;
  4053. /* Configure Int data 0 register to add source of interrupt */
  4054. rslt = bmi160_get_regs(BMI160_INT_DATA_0_ADDR, &data, 1, dev);
  4055. if(rslt == BMI160_OK) {
  4056. temp = data & ~BMI160_LOW_HIGH_SRC_INT_MASK;
  4057. data = temp | ((low_g_int->low_data_src << 7) & BMI160_LOW_HIGH_SRC_INT_MASK);
  4058. /* Write data to Data 0 address */
  4059. rslt = bmi160_set_regs(BMI160_INT_DATA_0_ADDR, &data, 1, dev);
  4060. }
  4061. return rslt;
  4062. }
  4063. /*!
  4064. * @brief This API configure the necessary setting of low-g interrupt.
  4065. */
  4066. static int8_t config_low_g_int_settg(
  4067. const struct bmi160_acc_low_g_int_cfg* low_g_int,
  4068. const struct bmi160_dev* dev) {
  4069. int8_t rslt;
  4070. uint8_t temp = 0;
  4071. uint8_t data_array[3] = {0, 0, 0};
  4072. /* Configuring INT_LOWHIGH register for low-g interrupt */
  4073. rslt = bmi160_get_regs(BMI160_INT_LOWHIGH_2_ADDR, &data_array[2], 1, dev);
  4074. if(rslt == BMI160_OK) {
  4075. temp = data_array[2] & ~BMI160_LOW_G_HYST_MASK;
  4076. /* Adding low-g hysteresis */
  4077. data_array[2] = temp | (low_g_int->low_hyst & BMI160_LOW_G_HYST_MASK);
  4078. temp = data_array[2] & ~BMI160_LOW_G_LOW_MODE_MASK;
  4079. /* Adding low-mode */
  4080. data_array[2] = temp | ((low_g_int->low_mode << 2) & BMI160_LOW_G_LOW_MODE_MASK);
  4081. /* Adding low-g threshold */
  4082. data_array[1] = low_g_int->low_thres;
  4083. /* Adding low-g interrupt delay */
  4084. data_array[0] = low_g_int->low_dur;
  4085. /* Writing data to INT_LOWHIGH 0,1,2 registers simultaneously*/
  4086. rslt = bmi160_set_regs(BMI160_INT_LOWHIGH_0_ADDR, data_array, 3, dev);
  4087. }
  4088. return rslt;
  4089. }
  4090. /*!
  4091. * @brief This API enables the high-g interrupt.
  4092. */
  4093. static int8_t enable_high_g_int(
  4094. const struct bmi160_acc_high_g_int_cfg* high_g_int_cfg,
  4095. const struct bmi160_dev* dev) {
  4096. int8_t rslt;
  4097. uint8_t data = 0;
  4098. uint8_t temp = 0;
  4099. /* Enable low-g interrupt in Int Enable 1 register */
  4100. rslt = bmi160_get_regs(BMI160_INT_ENABLE_1_ADDR, &data, 1, dev);
  4101. if(rslt == BMI160_OK) {
  4102. /* Adding high-g X-axis */
  4103. temp = data & ~BMI160_HIGH_G_X_INT_EN_MASK;
  4104. data = temp | (high_g_int_cfg->high_g_x & BMI160_HIGH_G_X_INT_EN_MASK);
  4105. /* Adding high-g Y-axis */
  4106. temp = data & ~BMI160_HIGH_G_Y_INT_EN_MASK;
  4107. data = temp | ((high_g_int_cfg->high_g_y << 1) & BMI160_HIGH_G_Y_INT_EN_MASK);
  4108. /* Adding high-g Z-axis */
  4109. temp = data & ~BMI160_HIGH_G_Z_INT_EN_MASK;
  4110. data = temp | ((high_g_int_cfg->high_g_z << 2) & BMI160_HIGH_G_Z_INT_EN_MASK);
  4111. /* write data to Int Enable 0 register */
  4112. rslt = bmi160_set_regs(BMI160_INT_ENABLE_1_ADDR, &data, 1, dev);
  4113. }
  4114. return rslt;
  4115. }
  4116. /*!
  4117. * @brief This API configure the source of data(filter & pre-filter)
  4118. * for high-g interrupt.
  4119. */
  4120. static int8_t config_high_g_data_src(
  4121. const struct bmi160_acc_high_g_int_cfg* high_g_int_cfg,
  4122. const struct bmi160_dev* dev) {
  4123. int8_t rslt;
  4124. uint8_t data = 0;
  4125. uint8_t temp = 0;
  4126. /* Configure Int data 0 register to add source of interrupt */
  4127. rslt = bmi160_get_regs(BMI160_INT_DATA_0_ADDR, &data, 1, dev);
  4128. if(rslt == BMI160_OK) {
  4129. temp = data & ~BMI160_LOW_HIGH_SRC_INT_MASK;
  4130. data = temp | ((high_g_int_cfg->high_data_src << 7) & BMI160_LOW_HIGH_SRC_INT_MASK);
  4131. /* Write data to Data 0 address */
  4132. rslt = bmi160_set_regs(BMI160_INT_DATA_0_ADDR, &data, 1, dev);
  4133. }
  4134. return rslt;
  4135. }
  4136. /*!
  4137. * @brief This API configure the necessary setting of high-g interrupt.
  4138. */
  4139. static int8_t config_high_g_int_settg(
  4140. const struct bmi160_acc_high_g_int_cfg* high_g_int_cfg,
  4141. const struct bmi160_dev* dev) {
  4142. int8_t rslt;
  4143. uint8_t temp = 0;
  4144. uint8_t data_array[3] = {0, 0, 0};
  4145. rslt = bmi160_get_regs(BMI160_INT_LOWHIGH_2_ADDR, &data_array[0], 1, dev);
  4146. if(rslt == BMI160_OK) {
  4147. temp = data_array[0] & ~BMI160_HIGH_G_HYST_MASK;
  4148. /* Adding high-g hysteresis */
  4149. data_array[0] = temp | ((high_g_int_cfg->high_hy << 6) & BMI160_HIGH_G_HYST_MASK);
  4150. /* Adding high-g duration */
  4151. data_array[1] = high_g_int_cfg->high_dur;
  4152. /* Adding high-g threshold */
  4153. data_array[2] = high_g_int_cfg->high_thres;
  4154. rslt = bmi160_set_regs(BMI160_INT_LOWHIGH_2_ADDR, data_array, 3, dev);
  4155. }
  4156. return rslt;
  4157. }
  4158. /*!
  4159. * @brief This API configure the behavioural setting of interrupt pin.
  4160. */
  4161. static int8_t
  4162. config_int_out_ctrl(const struct bmi160_int_settg* int_config, const struct bmi160_dev* dev) {
  4163. int8_t rslt;
  4164. uint8_t temp = 0;
  4165. uint8_t data = 0;
  4166. /* Configuration of output interrupt signals on pins INT1 and INT2 are
  4167. * done in BMI160_INT_OUT_CTRL_ADDR register*/
  4168. rslt = bmi160_get_regs(BMI160_INT_OUT_CTRL_ADDR, &data, 1, dev);
  4169. if(rslt == BMI160_OK) {
  4170. /* updating the interrupt pin structure to local structure */
  4171. const struct bmi160_int_pin_settg* intr_pin_sett = &(int_config->int_pin_settg);
  4172. /* Configuring channel 1 */
  4173. if(int_config->int_channel == BMI160_INT_CHANNEL_1) {
  4174. /* Output enable */
  4175. temp = data & ~BMI160_INT1_OUTPUT_EN_MASK;
  4176. data = temp | ((intr_pin_sett->output_en << 3) & BMI160_INT1_OUTPUT_EN_MASK);
  4177. /* Output mode */
  4178. temp = data & ~BMI160_INT1_OUTPUT_MODE_MASK;
  4179. data = temp | ((intr_pin_sett->output_mode << 2) & BMI160_INT1_OUTPUT_MODE_MASK);
  4180. /* Output type */
  4181. temp = data & ~BMI160_INT1_OUTPUT_TYPE_MASK;
  4182. data = temp | ((intr_pin_sett->output_type << 1) & BMI160_INT1_OUTPUT_TYPE_MASK);
  4183. /* edge control */
  4184. temp = data & ~BMI160_INT1_EDGE_CTRL_MASK;
  4185. data = temp | ((intr_pin_sett->edge_ctrl) & BMI160_INT1_EDGE_CTRL_MASK);
  4186. } else {
  4187. /* Configuring channel 2 */
  4188. /* Output enable */
  4189. temp = data & ~BMI160_INT2_OUTPUT_EN_MASK;
  4190. data = temp | ((intr_pin_sett->output_en << 7) & BMI160_INT2_OUTPUT_EN_MASK);
  4191. /* Output mode */
  4192. temp = data & ~BMI160_INT2_OUTPUT_MODE_MASK;
  4193. data = temp | ((intr_pin_sett->output_mode << 6) & BMI160_INT2_OUTPUT_MODE_MASK);
  4194. /* Output type */
  4195. temp = data & ~BMI160_INT2_OUTPUT_TYPE_MASK;
  4196. data = temp | ((intr_pin_sett->output_type << 5) & BMI160_INT2_OUTPUT_TYPE_MASK);
  4197. /* edge control */
  4198. temp = data & ~BMI160_INT2_EDGE_CTRL_MASK;
  4199. data = temp | ((intr_pin_sett->edge_ctrl << 4) & BMI160_INT2_EDGE_CTRL_MASK);
  4200. }
  4201. rslt = bmi160_set_regs(BMI160_INT_OUT_CTRL_ADDR, &data, 1, dev);
  4202. }
  4203. return rslt;
  4204. }
  4205. /*!
  4206. * @brief This API configure the mode(input enable, latch or non-latch) of interrupt pin.
  4207. */
  4208. static int8_t
  4209. config_int_latch(const struct bmi160_int_settg* int_config, const struct bmi160_dev* dev) {
  4210. int8_t rslt;
  4211. uint8_t temp = 0;
  4212. uint8_t data = 0;
  4213. /* Configuration of latch on pins INT1 and INT2 are done in
  4214. * BMI160_INT_LATCH_ADDR register*/
  4215. rslt = bmi160_get_regs(BMI160_INT_LATCH_ADDR, &data, 1, dev);
  4216. if(rslt == BMI160_OK) {
  4217. /* updating the interrupt pin structure to local structure */
  4218. const struct bmi160_int_pin_settg* intr_pin_sett = &(int_config->int_pin_settg);
  4219. if(int_config->int_channel == BMI160_INT_CHANNEL_1) {
  4220. /* Configuring channel 1 */
  4221. /* Input enable */
  4222. temp = data & ~BMI160_INT1_INPUT_EN_MASK;
  4223. data = temp | ((intr_pin_sett->input_en << 4) & BMI160_INT1_INPUT_EN_MASK);
  4224. } else {
  4225. /* Configuring channel 2 */
  4226. /* Input enable */
  4227. temp = data & ~BMI160_INT2_INPUT_EN_MASK;
  4228. data = temp | ((intr_pin_sett->input_en << 5) & BMI160_INT2_INPUT_EN_MASK);
  4229. }
  4230. /* In case of latch interrupt,update the latch duration */
  4231. /* Latching holds the interrupt for the amount of latch
  4232. * duration time */
  4233. temp = data & ~BMI160_INT_LATCH_MASK;
  4234. data = temp | (intr_pin_sett->latch_dur & BMI160_INT_LATCH_MASK);
  4235. /* OUT_CTRL_INT and LATCH_INT address lie consecutively,
  4236. * hence writing data to respective registers at one go */
  4237. rslt = bmi160_set_regs(BMI160_INT_LATCH_ADDR, &data, 1, dev);
  4238. }
  4239. return rslt;
  4240. }
  4241. /*!
  4242. * @brief This API performs the self test for accelerometer of BMI160
  4243. */
  4244. static int8_t perform_accel_self_test(struct bmi160_dev* dev) {
  4245. int8_t rslt;
  4246. struct bmi160_sensor_data accel_pos, accel_neg;
  4247. /* Enable Gyro self test bit */
  4248. rslt = enable_accel_self_test(dev);
  4249. if(rslt == BMI160_OK) {
  4250. /* Perform accel self test with positive excitation */
  4251. rslt = accel_self_test_positive_excitation(&accel_pos, dev);
  4252. if(rslt == BMI160_OK) {
  4253. /* Perform accel self test with negative excitation */
  4254. rslt = accel_self_test_negative_excitation(&accel_neg, dev);
  4255. if(rslt == BMI160_OK) {
  4256. /* Validate the self test result */
  4257. rslt = validate_accel_self_test(&accel_pos, &accel_neg);
  4258. }
  4259. }
  4260. }
  4261. return rslt;
  4262. }
  4263. /*!
  4264. * @brief This API enables to perform the accel self test by setting proper
  4265. * configurations to facilitate accel self test
  4266. */
  4267. static int8_t enable_accel_self_test(struct bmi160_dev* dev) {
  4268. int8_t rslt;
  4269. uint8_t reg_data;
  4270. /* Set the Accel power mode as normal mode */
  4271. dev->accel_cfg.power = BMI160_ACCEL_NORMAL_MODE;
  4272. /* Set the sensor range configuration as 8G */
  4273. dev->accel_cfg.range = BMI160_ACCEL_RANGE_8G;
  4274. rslt = bmi160_set_sens_conf(dev);
  4275. if(rslt == BMI160_OK) {
  4276. /* Accel configurations are set to facilitate self test
  4277. * acc_odr - 1600Hz ; acc_bwp = 2 ; acc_us = 0 */
  4278. reg_data = BMI160_ACCEL_SELF_TEST_CONFIG;
  4279. rslt = bmi160_set_regs(BMI160_ACCEL_CONFIG_ADDR, &reg_data, 1, dev);
  4280. }
  4281. return rslt;
  4282. }
  4283. /*!
  4284. * @brief This API performs accel self test with positive excitation
  4285. */
  4286. static int8_t accel_self_test_positive_excitation(
  4287. struct bmi160_sensor_data* accel_pos,
  4288. const struct bmi160_dev* dev) {
  4289. int8_t rslt;
  4290. uint8_t reg_data;
  4291. /* Enable accel self test with positive self-test excitation
  4292. * and with amplitude of deflection set as high */
  4293. reg_data = BMI160_ACCEL_SELF_TEST_POSITIVE_EN;
  4294. rslt = bmi160_set_regs(BMI160_SELF_TEST_ADDR, &reg_data, 1, dev);
  4295. if(rslt == BMI160_OK) {
  4296. /* Read the data after a delay of 50ms - refer datasheet 2.8.1 accel self test*/
  4297. dev->delay_ms(BMI160_ACCEL_SELF_TEST_DELAY);
  4298. rslt = bmi160_get_sensor_data(BMI160_ACCEL_ONLY, accel_pos, NULL, dev);
  4299. }
  4300. return rslt;
  4301. }
  4302. /*!
  4303. * @brief This API performs accel self test with negative excitation
  4304. */
  4305. static int8_t accel_self_test_negative_excitation(
  4306. struct bmi160_sensor_data* accel_neg,
  4307. const struct bmi160_dev* dev) {
  4308. int8_t rslt;
  4309. uint8_t reg_data;
  4310. /* Enable accel self test with negative self-test excitation
  4311. * and with amplitude of deflection set as high */
  4312. reg_data = BMI160_ACCEL_SELF_TEST_NEGATIVE_EN;
  4313. rslt = bmi160_set_regs(BMI160_SELF_TEST_ADDR, &reg_data, 1, dev);
  4314. if(rslt == BMI160_OK) {
  4315. /* Read the data after a delay of 50ms */
  4316. dev->delay_ms(BMI160_ACCEL_SELF_TEST_DELAY);
  4317. rslt = bmi160_get_sensor_data(BMI160_ACCEL_ONLY, accel_neg, NULL, dev);
  4318. }
  4319. return rslt;
  4320. }
  4321. /*!
  4322. * @brief This API validates the accel self test results
  4323. */
  4324. static int8_t validate_accel_self_test(
  4325. const struct bmi160_sensor_data* accel_pos,
  4326. const struct bmi160_sensor_data* accel_neg) {
  4327. int8_t rslt;
  4328. /* Validate the results of self test */
  4329. if(((accel_neg->x - accel_pos->x) > BMI160_ACCEL_SELF_TEST_LIMIT) &&
  4330. ((accel_neg->y - accel_pos->y) > BMI160_ACCEL_SELF_TEST_LIMIT) &&
  4331. ((accel_neg->z - accel_pos->z) > BMI160_ACCEL_SELF_TEST_LIMIT)) {
  4332. /* Self test pass condition */
  4333. rslt = BMI160_OK;
  4334. } else {
  4335. rslt = BMI160_W_ACCEl_SELF_TEST_FAIL;
  4336. }
  4337. return rslt;
  4338. }
  4339. /*!
  4340. * @brief This API performs the self test for gyroscope of BMI160
  4341. */
  4342. static int8_t perform_gyro_self_test(const struct bmi160_dev* dev) {
  4343. int8_t rslt;
  4344. /* Enable Gyro self test bit */
  4345. rslt = enable_gyro_self_test(dev);
  4346. if(rslt == BMI160_OK) {
  4347. /* Validate the gyro self test a delay of 50ms */
  4348. dev->delay_ms(50);
  4349. /* Validate the gyro self test results */
  4350. rslt = validate_gyro_self_test(dev);
  4351. }
  4352. return rslt;
  4353. }
  4354. /*!
  4355. * @brief This API enables the self test bit to trigger self test for Gyro
  4356. */
  4357. static int8_t enable_gyro_self_test(const struct bmi160_dev* dev) {
  4358. int8_t rslt;
  4359. uint8_t reg_data;
  4360. /* Enable the Gyro self test bit to trigger the self test */
  4361. rslt = bmi160_get_regs(BMI160_SELF_TEST_ADDR, &reg_data, 1, dev);
  4362. if(rslt == BMI160_OK) {
  4363. reg_data = BMI160_SET_BITS(reg_data, BMI160_GYRO_SELF_TEST, 1);
  4364. rslt = bmi160_set_regs(BMI160_SELF_TEST_ADDR, &reg_data, 1, dev);
  4365. if(rslt == BMI160_OK) {
  4366. /* Delay to enable gyro self test */
  4367. dev->delay_ms(15);
  4368. }
  4369. }
  4370. return rslt;
  4371. }
  4372. /*!
  4373. * @brief This API validates the self test results of Gyro
  4374. */
  4375. static int8_t validate_gyro_self_test(const struct bmi160_dev* dev) {
  4376. int8_t rslt;
  4377. uint8_t reg_data;
  4378. /* Validate the Gyro self test result */
  4379. rslt = bmi160_get_regs(BMI160_STATUS_ADDR, &reg_data, 1, dev);
  4380. if(rslt == BMI160_OK) {
  4381. reg_data = BMI160_GET_BITS(reg_data, BMI160_GYRO_SELF_TEST_STATUS);
  4382. if(reg_data == BMI160_ENABLE) {
  4383. /* Gyro self test success case */
  4384. rslt = BMI160_OK;
  4385. } else {
  4386. rslt = BMI160_W_GYRO_SELF_TEST_FAIL;
  4387. }
  4388. }
  4389. return rslt;
  4390. }
  4391. /*!
  4392. * @brief This API sets FIFO full interrupt of the sensor.This interrupt
  4393. * occurs when the FIFO is full and the next full data sample would cause
  4394. * a FIFO overflow, which may delete the old samples.
  4395. */
  4396. static int8_t
  4397. set_fifo_full_int(const struct bmi160_int_settg* int_config, const struct bmi160_dev* dev) {
  4398. int8_t rslt = BMI160_OK;
  4399. /* Null-pointer check */
  4400. if((dev == NULL) || (dev->delay_ms == NULL)) {
  4401. rslt = BMI160_E_NULL_PTR;
  4402. } else {
  4403. /*enable the fifo full interrupt */
  4404. rslt = enable_fifo_full_int(int_config, dev);
  4405. if(rslt == BMI160_OK) {
  4406. /* Configure Interrupt pins */
  4407. rslt = set_intr_pin_config(int_config, dev);
  4408. if(rslt == BMI160_OK) {
  4409. rslt = map_hardware_interrupt(int_config, dev);
  4410. }
  4411. }
  4412. }
  4413. return rslt;
  4414. }
  4415. /*!
  4416. * @brief This enable the FIFO full interrupt engine.
  4417. */
  4418. static int8_t
  4419. enable_fifo_full_int(const struct bmi160_int_settg* int_config, const struct bmi160_dev* dev) {
  4420. int8_t rslt;
  4421. uint8_t data = 0;
  4422. rslt = bmi160_get_regs(BMI160_INT_ENABLE_1_ADDR, &data, 1, dev);
  4423. if(rslt == BMI160_OK) {
  4424. data = BMI160_SET_BITS(data, BMI160_FIFO_FULL_INT, int_config->fifo_full_int_en);
  4425. /* Writing data to INT ENABLE 1 Address */
  4426. rslt = bmi160_set_regs(BMI160_INT_ENABLE_1_ADDR, &data, 1, dev);
  4427. }
  4428. return rslt;
  4429. }
  4430. /*!
  4431. * @brief This API sets FIFO watermark interrupt of the sensor.The FIFO
  4432. * watermark interrupt is fired, when the FIFO fill level is above a fifo
  4433. * watermark.
  4434. */
  4435. static int8_t set_fifo_watermark_int(
  4436. const struct bmi160_int_settg* int_config,
  4437. const struct bmi160_dev* dev) {
  4438. int8_t rslt = BMI160_OK;
  4439. if((dev == NULL) || (dev->delay_ms == NULL)) {
  4440. rslt = BMI160_E_NULL_PTR;
  4441. } else {
  4442. /* Enable fifo-watermark interrupt in Int Enable 1 register */
  4443. rslt = enable_fifo_wtm_int(int_config, dev);
  4444. if(rslt == BMI160_OK) {
  4445. /* Configure Interrupt pins */
  4446. rslt = set_intr_pin_config(int_config, dev);
  4447. if(rslt == BMI160_OK) {
  4448. rslt = map_hardware_interrupt(int_config, dev);
  4449. }
  4450. }
  4451. }
  4452. return rslt;
  4453. }
  4454. /*!
  4455. * @brief This enable the FIFO watermark interrupt engine.
  4456. */
  4457. static int8_t
  4458. enable_fifo_wtm_int(const struct bmi160_int_settg* int_config, const struct bmi160_dev* dev) {
  4459. int8_t rslt;
  4460. uint8_t data = 0;
  4461. rslt = bmi160_get_regs(BMI160_INT_ENABLE_1_ADDR, &data, 1, dev);
  4462. if(rslt == BMI160_OK) {
  4463. data = BMI160_SET_BITS(data, BMI160_FIFO_WTM_INT, int_config->fifo_wtm_int_en);
  4464. /* Writing data to INT ENABLE 1 Address */
  4465. rslt = bmi160_set_regs(BMI160_INT_ENABLE_1_ADDR, &data, 1, dev);
  4466. }
  4467. return rslt;
  4468. }
  4469. /*!
  4470. * @brief This API is used to reset the FIFO related configurations
  4471. * in the fifo_frame structure.
  4472. */
  4473. static void reset_fifo_data_structure(const struct bmi160_dev* dev) {
  4474. /*Prepare for next FIFO read by resetting FIFO's
  4475. * internal data structures*/
  4476. dev->fifo->accel_byte_start_idx = 0;
  4477. dev->fifo->gyro_byte_start_idx = 0;
  4478. dev->fifo->aux_byte_start_idx = 0;
  4479. dev->fifo->sensor_time = 0;
  4480. dev->fifo->skipped_frame_count = 0;
  4481. }
  4482. /*!
  4483. * @brief This API is used to read fifo_byte_counter value (i.e)
  4484. * current fill-level in Fifo buffer.
  4485. */
  4486. static int8_t get_fifo_byte_counter(uint16_t* bytes_to_read, struct bmi160_dev const* dev) {
  4487. int8_t rslt = 0;
  4488. uint8_t data[2];
  4489. uint8_t addr = BMI160_FIFO_LENGTH_ADDR;
  4490. rslt |= bmi160_get_regs(addr, data, 2, dev);
  4491. data[1] = data[1] & BMI160_FIFO_BYTE_COUNTER_MASK;
  4492. /* Available data in FIFO is stored in bytes_to_read*/
  4493. *bytes_to_read = (((uint16_t)data[1] << 8) | ((uint16_t)data[0]));
  4494. return rslt;
  4495. }
  4496. /*!
  4497. * @brief This API is used to compute the number of bytes of accel FIFO data
  4498. * which is to be parsed in header-less mode
  4499. */
  4500. static void get_accel_len_to_parse(
  4501. uint16_t* data_index,
  4502. uint16_t* data_read_length,
  4503. const uint8_t* acc_frame_count,
  4504. const struct bmi160_dev* dev) {
  4505. /* Data start index */
  4506. *data_index = dev->fifo->accel_byte_start_idx;
  4507. if(dev->fifo->fifo_data_enable == BMI160_FIFO_A_ENABLE) {
  4508. *data_read_length = (*acc_frame_count) * BMI160_FIFO_A_LENGTH;
  4509. } else if(dev->fifo->fifo_data_enable == BMI160_FIFO_G_A_ENABLE) {
  4510. *data_read_length = (*acc_frame_count) * BMI160_FIFO_GA_LENGTH;
  4511. } else if(dev->fifo->fifo_data_enable == BMI160_FIFO_M_A_ENABLE) {
  4512. *data_read_length = (*acc_frame_count) * BMI160_FIFO_MA_LENGTH;
  4513. } else if(dev->fifo->fifo_data_enable == BMI160_FIFO_M_G_A_ENABLE) {
  4514. *data_read_length = (*acc_frame_count) * BMI160_FIFO_MGA_LENGTH;
  4515. } else {
  4516. /* When accel is not enabled ,there will be no accel data.
  4517. * so we update the data index as complete */
  4518. *data_index = dev->fifo->length;
  4519. }
  4520. if(*data_read_length > dev->fifo->length) {
  4521. /* Handling the case where more data is requested
  4522. * than that is available*/
  4523. *data_read_length = dev->fifo->length;
  4524. }
  4525. }
  4526. /*!
  4527. * @brief This API is used to parse the accelerometer data from the
  4528. * FIFO data in both header mode and header-less mode.
  4529. * It updates the idx value which is used to store the index of
  4530. * the current data byte which is parsed.
  4531. */
  4532. static void unpack_accel_frame(
  4533. struct bmi160_sensor_data* acc,
  4534. uint16_t* idx,
  4535. uint8_t* acc_idx,
  4536. uint8_t frame_info,
  4537. const struct bmi160_dev* dev) {
  4538. switch(frame_info) {
  4539. case BMI160_FIFO_HEAD_A:
  4540. case BMI160_FIFO_A_ENABLE:
  4541. /*Partial read, then skip the data*/
  4542. if((*idx + BMI160_FIFO_A_LENGTH) > dev->fifo->length) {
  4543. /*Update the data index as complete*/
  4544. *idx = dev->fifo->length;
  4545. break;
  4546. }
  4547. /*Unpack the data array into the structure instance "acc" */
  4548. unpack_accel_data(&acc[*acc_idx], *idx, dev);
  4549. /*Move the data index*/
  4550. *idx = *idx + BMI160_FIFO_A_LENGTH;
  4551. (*acc_idx)++;
  4552. break;
  4553. case BMI160_FIFO_HEAD_G_A:
  4554. case BMI160_FIFO_G_A_ENABLE:
  4555. /*Partial read, then skip the data*/
  4556. if((*idx + BMI160_FIFO_GA_LENGTH) > dev->fifo->length) {
  4557. /*Update the data index as complete*/
  4558. *idx = dev->fifo->length;
  4559. break;
  4560. }
  4561. /*Unpack the data array into structure instance "acc"*/
  4562. unpack_accel_data(&acc[*acc_idx], *idx + BMI160_FIFO_G_LENGTH, dev);
  4563. /*Move the data index*/
  4564. *idx = *idx + BMI160_FIFO_GA_LENGTH;
  4565. (*acc_idx)++;
  4566. break;
  4567. case BMI160_FIFO_HEAD_M_A:
  4568. case BMI160_FIFO_M_A_ENABLE:
  4569. /*Partial read, then skip the data*/
  4570. if((*idx + BMI160_FIFO_MA_LENGTH) > dev->fifo->length) {
  4571. /*Update the data index as complete*/
  4572. *idx = dev->fifo->length;
  4573. break;
  4574. }
  4575. /*Unpack the data array into structure instance "acc"*/
  4576. unpack_accel_data(&acc[*acc_idx], *idx + BMI160_FIFO_M_LENGTH, dev);
  4577. /*Move the data index*/
  4578. *idx = *idx + BMI160_FIFO_MA_LENGTH;
  4579. (*acc_idx)++;
  4580. break;
  4581. case BMI160_FIFO_HEAD_M_G_A:
  4582. case BMI160_FIFO_M_G_A_ENABLE:
  4583. /*Partial read, then skip the data*/
  4584. if((*idx + BMI160_FIFO_MGA_LENGTH) > dev->fifo->length) {
  4585. /*Update the data index as complete*/
  4586. *idx = dev->fifo->length;
  4587. break;
  4588. }
  4589. /*Unpack the data array into structure instance "acc"*/
  4590. unpack_accel_data(&acc[*acc_idx], *idx + BMI160_FIFO_MG_LENGTH, dev);
  4591. /*Move the data index*/
  4592. *idx = *idx + BMI160_FIFO_MGA_LENGTH;
  4593. (*acc_idx)++;
  4594. break;
  4595. case BMI160_FIFO_HEAD_M:
  4596. case BMI160_FIFO_M_ENABLE:
  4597. (*idx) = (*idx) + BMI160_FIFO_M_LENGTH;
  4598. break;
  4599. case BMI160_FIFO_HEAD_G:
  4600. case BMI160_FIFO_G_ENABLE:
  4601. (*idx) = (*idx) + BMI160_FIFO_G_LENGTH;
  4602. break;
  4603. case BMI160_FIFO_HEAD_M_G:
  4604. case BMI160_FIFO_M_G_ENABLE:
  4605. (*idx) = (*idx) + BMI160_FIFO_MG_LENGTH;
  4606. break;
  4607. default:
  4608. break;
  4609. }
  4610. }
  4611. /*!
  4612. * @brief This API is used to parse the accelerometer data from the
  4613. * FIFO data and store it in the instance of the structure bmi160_sensor_data.
  4614. */
  4615. static void unpack_accel_data(
  4616. struct bmi160_sensor_data* accel_data,
  4617. uint16_t data_start_index,
  4618. const struct bmi160_dev* dev) {
  4619. uint16_t data_lsb;
  4620. uint16_t data_msb;
  4621. /* Accel raw x data */
  4622. data_lsb = dev->fifo->data[data_start_index++];
  4623. data_msb = dev->fifo->data[data_start_index++];
  4624. accel_data->x = (int16_t)((data_msb << 8) | data_lsb);
  4625. /* Accel raw y data */
  4626. data_lsb = dev->fifo->data[data_start_index++];
  4627. data_msb = dev->fifo->data[data_start_index++];
  4628. accel_data->y = (int16_t)((data_msb << 8) | data_lsb);
  4629. /* Accel raw z data */
  4630. data_lsb = dev->fifo->data[data_start_index++];
  4631. data_msb = dev->fifo->data[data_start_index++];
  4632. accel_data->z = (int16_t)((data_msb << 8) | data_lsb);
  4633. }
  4634. /*!
  4635. * @brief This API is used to parse the accelerometer data from the
  4636. * FIFO data in header mode.
  4637. */
  4638. static void extract_accel_header_mode(
  4639. struct bmi160_sensor_data* accel_data,
  4640. uint8_t* accel_length,
  4641. const struct bmi160_dev* dev) {
  4642. uint8_t frame_header = 0;
  4643. uint16_t data_index;
  4644. uint8_t accel_index = 0;
  4645. for(data_index = dev->fifo->accel_byte_start_idx; data_index < dev->fifo->length;) {
  4646. /* extracting Frame header */
  4647. frame_header = (dev->fifo->data[data_index] & BMI160_FIFO_TAG_INTR_MASK);
  4648. /*Index is moved to next byte where the data is starting*/
  4649. data_index++;
  4650. switch(frame_header) {
  4651. /* Accel frame */
  4652. case BMI160_FIFO_HEAD_A:
  4653. case BMI160_FIFO_HEAD_M_A:
  4654. case BMI160_FIFO_HEAD_G_A:
  4655. case BMI160_FIFO_HEAD_M_G_A:
  4656. unpack_accel_frame(accel_data, &data_index, &accel_index, frame_header, dev);
  4657. break;
  4658. case BMI160_FIFO_HEAD_M:
  4659. move_next_frame(&data_index, BMI160_FIFO_M_LENGTH, dev);
  4660. break;
  4661. case BMI160_FIFO_HEAD_G:
  4662. move_next_frame(&data_index, BMI160_FIFO_G_LENGTH, dev);
  4663. break;
  4664. case BMI160_FIFO_HEAD_M_G:
  4665. move_next_frame(&data_index, BMI160_FIFO_MG_LENGTH, dev);
  4666. break;
  4667. /* Sensor time frame */
  4668. case BMI160_FIFO_HEAD_SENSOR_TIME:
  4669. unpack_sensortime_frame(&data_index, dev);
  4670. break;
  4671. /* Skip frame */
  4672. case BMI160_FIFO_HEAD_SKIP_FRAME:
  4673. unpack_skipped_frame(&data_index, dev);
  4674. break;
  4675. /* Input config frame */
  4676. case BMI160_FIFO_HEAD_INPUT_CONFIG:
  4677. move_next_frame(&data_index, 1, dev);
  4678. break;
  4679. case BMI160_FIFO_HEAD_OVER_READ:
  4680. /* Update the data index as complete in case of Over read */
  4681. data_index = dev->fifo->length;
  4682. break;
  4683. default:
  4684. break;
  4685. }
  4686. if(*accel_length == accel_index) {
  4687. /* Number of frames to read completed */
  4688. break;
  4689. }
  4690. }
  4691. /*Update number of accel data read*/
  4692. *accel_length = accel_index;
  4693. /*Update the accel frame index*/
  4694. dev->fifo->accel_byte_start_idx = data_index;
  4695. }
  4696. /*!
  4697. * @brief This API computes the number of bytes of gyro FIFO data
  4698. * which is to be parsed in header-less mode
  4699. */
  4700. static void get_gyro_len_to_parse(
  4701. uint16_t* data_index,
  4702. uint16_t* data_read_length,
  4703. const uint8_t* gyro_frame_count,
  4704. const struct bmi160_dev* dev) {
  4705. /* Data start index */
  4706. *data_index = dev->fifo->gyro_byte_start_idx;
  4707. if(dev->fifo->fifo_data_enable == BMI160_FIFO_G_ENABLE) {
  4708. *data_read_length = (*gyro_frame_count) * BMI160_FIFO_G_LENGTH;
  4709. } else if(dev->fifo->fifo_data_enable == BMI160_FIFO_G_A_ENABLE) {
  4710. *data_read_length = (*gyro_frame_count) * BMI160_FIFO_GA_LENGTH;
  4711. } else if(dev->fifo->fifo_data_enable == BMI160_FIFO_M_G_ENABLE) {
  4712. *data_read_length = (*gyro_frame_count) * BMI160_FIFO_MG_LENGTH;
  4713. } else if(dev->fifo->fifo_data_enable == BMI160_FIFO_M_G_A_ENABLE) {
  4714. *data_read_length = (*gyro_frame_count) * BMI160_FIFO_MGA_LENGTH;
  4715. } else {
  4716. /* When gyro is not enabled ,there will be no gyro data.
  4717. * so we update the data index as complete */
  4718. *data_index = dev->fifo->length;
  4719. }
  4720. if(*data_read_length > dev->fifo->length) {
  4721. /* Handling the case where more data is requested
  4722. * than that is available*/
  4723. *data_read_length = dev->fifo->length;
  4724. }
  4725. }
  4726. /*!
  4727. * @brief This API is used to parse the gyroscope's data from the
  4728. * FIFO data in both header mode and header-less mode.
  4729. * It updates the idx value which is used to store the index of
  4730. * the current data byte which is parsed.
  4731. */
  4732. static void unpack_gyro_frame(
  4733. struct bmi160_sensor_data* gyro,
  4734. uint16_t* idx,
  4735. uint8_t* gyro_idx,
  4736. uint8_t frame_info,
  4737. const struct bmi160_dev* dev) {
  4738. switch(frame_info) {
  4739. case BMI160_FIFO_HEAD_G:
  4740. case BMI160_FIFO_G_ENABLE:
  4741. /*Partial read, then skip the data*/
  4742. if((*idx + BMI160_FIFO_G_LENGTH) > dev->fifo->length) {
  4743. /*Update the data index as complete*/
  4744. *idx = dev->fifo->length;
  4745. break;
  4746. }
  4747. /*Unpack the data array into structure instance "gyro"*/
  4748. unpack_gyro_data(&gyro[*gyro_idx], *idx, dev);
  4749. /*Move the data index*/
  4750. (*idx) = (*idx) + BMI160_FIFO_G_LENGTH;
  4751. (*gyro_idx)++;
  4752. break;
  4753. case BMI160_FIFO_HEAD_G_A:
  4754. case BMI160_FIFO_G_A_ENABLE:
  4755. /*Partial read, then skip the data*/
  4756. if((*idx + BMI160_FIFO_GA_LENGTH) > dev->fifo->length) {
  4757. /*Update the data index as complete*/
  4758. *idx = dev->fifo->length;
  4759. break;
  4760. }
  4761. /* Unpack the data array into structure instance "gyro" */
  4762. unpack_gyro_data(&gyro[*gyro_idx], *idx, dev);
  4763. /* Move the data index */
  4764. *idx = *idx + BMI160_FIFO_GA_LENGTH;
  4765. (*gyro_idx)++;
  4766. break;
  4767. case BMI160_FIFO_HEAD_M_G_A:
  4768. case BMI160_FIFO_M_G_A_ENABLE:
  4769. /*Partial read, then skip the data*/
  4770. if((*idx + BMI160_FIFO_MGA_LENGTH) > dev->fifo->length) {
  4771. /*Update the data index as complete*/
  4772. *idx = dev->fifo->length;
  4773. break;
  4774. }
  4775. /*Unpack the data array into structure instance "gyro"*/
  4776. unpack_gyro_data(&gyro[*gyro_idx], *idx + BMI160_FIFO_M_LENGTH, dev);
  4777. /*Move the data index*/
  4778. *idx = *idx + BMI160_FIFO_MGA_LENGTH;
  4779. (*gyro_idx)++;
  4780. break;
  4781. case BMI160_FIFO_HEAD_M_A:
  4782. case BMI160_FIFO_M_A_ENABLE:
  4783. /* Move the data index */
  4784. *idx = *idx + BMI160_FIFO_MA_LENGTH;
  4785. break;
  4786. case BMI160_FIFO_HEAD_M:
  4787. case BMI160_FIFO_M_ENABLE:
  4788. (*idx) = (*idx) + BMI160_FIFO_M_LENGTH;
  4789. break;
  4790. case BMI160_FIFO_HEAD_M_G:
  4791. case BMI160_FIFO_M_G_ENABLE:
  4792. /*Partial read, then skip the data*/
  4793. if((*idx + BMI160_FIFO_MG_LENGTH) > dev->fifo->length) {
  4794. /*Update the data index as complete*/
  4795. *idx = dev->fifo->length;
  4796. break;
  4797. }
  4798. /*Unpack the data array into structure instance "gyro"*/
  4799. unpack_gyro_data(&gyro[*gyro_idx], *idx + BMI160_FIFO_M_LENGTH, dev);
  4800. /*Move the data index*/
  4801. (*idx) = (*idx) + BMI160_FIFO_MG_LENGTH;
  4802. (*gyro_idx)++;
  4803. break;
  4804. case BMI160_FIFO_HEAD_A:
  4805. case BMI160_FIFO_A_ENABLE:
  4806. /*Move the data index*/
  4807. *idx = *idx + BMI160_FIFO_A_LENGTH;
  4808. break;
  4809. default:
  4810. break;
  4811. }
  4812. }
  4813. /*!
  4814. * @brief This API is used to parse the gyro data from the
  4815. * FIFO data and store it in the instance of the structure bmi160_sensor_data.
  4816. */
  4817. static void unpack_gyro_data(
  4818. struct bmi160_sensor_data* gyro_data,
  4819. uint16_t data_start_index,
  4820. const struct bmi160_dev* dev) {
  4821. uint16_t data_lsb;
  4822. uint16_t data_msb;
  4823. /* Gyro raw x data */
  4824. data_lsb = dev->fifo->data[data_start_index++];
  4825. data_msb = dev->fifo->data[data_start_index++];
  4826. gyro_data->x = (int16_t)((data_msb << 8) | data_lsb);
  4827. /* Gyro raw y data */
  4828. data_lsb = dev->fifo->data[data_start_index++];
  4829. data_msb = dev->fifo->data[data_start_index++];
  4830. gyro_data->y = (int16_t)((data_msb << 8) | data_lsb);
  4831. /* Gyro raw z data */
  4832. data_lsb = dev->fifo->data[data_start_index++];
  4833. data_msb = dev->fifo->data[data_start_index++];
  4834. gyro_data->z = (int16_t)((data_msb << 8) | data_lsb);
  4835. }
  4836. /*!
  4837. * @brief This API is used to parse the gyro data from the
  4838. * FIFO data in header mode.
  4839. */
  4840. static void extract_gyro_header_mode(
  4841. struct bmi160_sensor_data* gyro_data,
  4842. uint8_t* gyro_length,
  4843. const struct bmi160_dev* dev) {
  4844. uint8_t frame_header = 0;
  4845. uint16_t data_index;
  4846. uint8_t gyro_index = 0;
  4847. for(data_index = dev->fifo->gyro_byte_start_idx; data_index < dev->fifo->length;) {
  4848. /* extracting Frame header */
  4849. frame_header = (dev->fifo->data[data_index] & BMI160_FIFO_TAG_INTR_MASK);
  4850. /*Index is moved to next byte where the data is starting*/
  4851. data_index++;
  4852. switch(frame_header) {
  4853. /* GYRO frame */
  4854. case BMI160_FIFO_HEAD_G:
  4855. case BMI160_FIFO_HEAD_G_A:
  4856. case BMI160_FIFO_HEAD_M_G:
  4857. case BMI160_FIFO_HEAD_M_G_A:
  4858. unpack_gyro_frame(gyro_data, &data_index, &gyro_index, frame_header, dev);
  4859. break;
  4860. case BMI160_FIFO_HEAD_A:
  4861. move_next_frame(&data_index, BMI160_FIFO_A_LENGTH, dev);
  4862. break;
  4863. case BMI160_FIFO_HEAD_M:
  4864. move_next_frame(&data_index, BMI160_FIFO_M_LENGTH, dev);
  4865. break;
  4866. case BMI160_FIFO_HEAD_M_A:
  4867. move_next_frame(&data_index, BMI160_FIFO_M_LENGTH, dev);
  4868. break;
  4869. /* Sensor time frame */
  4870. case BMI160_FIFO_HEAD_SENSOR_TIME:
  4871. unpack_sensortime_frame(&data_index, dev);
  4872. break;
  4873. /* Skip frame */
  4874. case BMI160_FIFO_HEAD_SKIP_FRAME:
  4875. unpack_skipped_frame(&data_index, dev);
  4876. break;
  4877. /* Input config frame */
  4878. case BMI160_FIFO_HEAD_INPUT_CONFIG:
  4879. move_next_frame(&data_index, 1, dev);
  4880. break;
  4881. case BMI160_FIFO_HEAD_OVER_READ:
  4882. /* Update the data index as complete in case of over read */
  4883. data_index = dev->fifo->length;
  4884. break;
  4885. default:
  4886. break;
  4887. }
  4888. if(*gyro_length == gyro_index) {
  4889. /*Number of frames to read completed*/
  4890. break;
  4891. }
  4892. }
  4893. /*Update number of gyro data read*/
  4894. *gyro_length = gyro_index;
  4895. /*Update the gyro frame index*/
  4896. dev->fifo->gyro_byte_start_idx = data_index;
  4897. }
  4898. /*!
  4899. * @brief This API computes the number of bytes of aux FIFO data
  4900. * which is to be parsed in header-less mode
  4901. */
  4902. static void get_aux_len_to_parse(
  4903. uint16_t* data_index,
  4904. uint16_t* data_read_length,
  4905. const uint8_t* aux_frame_count,
  4906. const struct bmi160_dev* dev) {
  4907. /* Data start index */
  4908. *data_index = dev->fifo->gyro_byte_start_idx;
  4909. if(dev->fifo->fifo_data_enable == BMI160_FIFO_M_ENABLE) {
  4910. *data_read_length = (*aux_frame_count) * BMI160_FIFO_M_LENGTH;
  4911. } else if(dev->fifo->fifo_data_enable == BMI160_FIFO_M_A_ENABLE) {
  4912. *data_read_length = (*aux_frame_count) * BMI160_FIFO_MA_LENGTH;
  4913. } else if(dev->fifo->fifo_data_enable == BMI160_FIFO_M_G_ENABLE) {
  4914. *data_read_length = (*aux_frame_count) * BMI160_FIFO_MG_LENGTH;
  4915. } else if(dev->fifo->fifo_data_enable == BMI160_FIFO_M_G_A_ENABLE) {
  4916. *data_read_length = (*aux_frame_count) * BMI160_FIFO_MGA_LENGTH;
  4917. } else {
  4918. /* When aux is not enabled ,there will be no aux data.
  4919. * so we update the data index as complete */
  4920. *data_index = dev->fifo->length;
  4921. }
  4922. if(*data_read_length > dev->fifo->length) {
  4923. /* Handling the case where more data is requested
  4924. * than that is available */
  4925. *data_read_length = dev->fifo->length;
  4926. }
  4927. }
  4928. /*!
  4929. * @brief This API is used to parse the aux's data from the
  4930. * FIFO data in both header mode and header-less mode.
  4931. * It updates the idx value which is used to store the index of
  4932. * the current data byte which is parsed
  4933. */
  4934. static void unpack_aux_frame(
  4935. struct bmi160_aux_data* aux_data,
  4936. uint16_t* idx,
  4937. uint8_t* aux_index,
  4938. uint8_t frame_info,
  4939. const struct bmi160_dev* dev) {
  4940. switch(frame_info) {
  4941. case BMI160_FIFO_HEAD_M:
  4942. case BMI160_FIFO_M_ENABLE:
  4943. /* Partial read, then skip the data */
  4944. if((*idx + BMI160_FIFO_M_LENGTH) > dev->fifo->length) {
  4945. /* Update the data index as complete */
  4946. *idx = dev->fifo->length;
  4947. break;
  4948. }
  4949. /* Unpack the data array into structure instance */
  4950. unpack_aux_data(&aux_data[*aux_index], *idx, dev);
  4951. /* Move the data index */
  4952. *idx = *idx + BMI160_FIFO_M_LENGTH;
  4953. (*aux_index)++;
  4954. break;
  4955. case BMI160_FIFO_HEAD_M_A:
  4956. case BMI160_FIFO_M_A_ENABLE:
  4957. /* Partial read, then skip the data */
  4958. if((*idx + BMI160_FIFO_MA_LENGTH) > dev->fifo->length) {
  4959. /* Update the data index as complete */
  4960. *idx = dev->fifo->length;
  4961. break;
  4962. }
  4963. /* Unpack the data array into structure instance */
  4964. unpack_aux_data(&aux_data[*aux_index], *idx, dev);
  4965. /* Move the data index */
  4966. *idx = *idx + BMI160_FIFO_MA_LENGTH;
  4967. (*aux_index)++;
  4968. break;
  4969. case BMI160_FIFO_HEAD_M_G:
  4970. case BMI160_FIFO_M_G_ENABLE:
  4971. /* Partial read, then skip the data */
  4972. if((*idx + BMI160_FIFO_MG_LENGTH) > dev->fifo->length) {
  4973. /* Update the data index as complete */
  4974. *idx = dev->fifo->length;
  4975. break;
  4976. }
  4977. /* Unpack the data array into structure instance */
  4978. unpack_aux_data(&aux_data[*aux_index], *idx, dev);
  4979. /* Move the data index */
  4980. (*idx) = (*idx) + BMI160_FIFO_MG_LENGTH;
  4981. (*aux_index)++;
  4982. break;
  4983. case BMI160_FIFO_HEAD_M_G_A:
  4984. case BMI160_FIFO_M_G_A_ENABLE:
  4985. /*Partial read, then skip the data*/
  4986. if((*idx + BMI160_FIFO_MGA_LENGTH) > dev->fifo->length) {
  4987. /* Update the data index as complete */
  4988. *idx = dev->fifo->length;
  4989. break;
  4990. }
  4991. /* Unpack the data array into structure instance */
  4992. unpack_aux_data(&aux_data[*aux_index], *idx, dev);
  4993. /*Move the data index*/
  4994. *idx = *idx + BMI160_FIFO_MGA_LENGTH;
  4995. (*aux_index)++;
  4996. break;
  4997. case BMI160_FIFO_HEAD_G:
  4998. case BMI160_FIFO_G_ENABLE:
  4999. /* Move the data index */
  5000. (*idx) = (*idx) + BMI160_FIFO_G_LENGTH;
  5001. break;
  5002. case BMI160_FIFO_HEAD_G_A:
  5003. case BMI160_FIFO_G_A_ENABLE:
  5004. /* Move the data index */
  5005. *idx = *idx + BMI160_FIFO_GA_LENGTH;
  5006. break;
  5007. case BMI160_FIFO_HEAD_A:
  5008. case BMI160_FIFO_A_ENABLE:
  5009. /* Move the data index */
  5010. *idx = *idx + BMI160_FIFO_A_LENGTH;
  5011. break;
  5012. default:
  5013. break;
  5014. }
  5015. }
  5016. /*!
  5017. * @brief This API is used to parse the aux data from the
  5018. * FIFO data and store it in the instance of the structure bmi160_aux_data.
  5019. */
  5020. static void unpack_aux_data(
  5021. struct bmi160_aux_data* aux_data,
  5022. uint16_t data_start_index,
  5023. const struct bmi160_dev* dev) {
  5024. /* Aux data bytes */
  5025. aux_data->data[0] = dev->fifo->data[data_start_index++];
  5026. aux_data->data[1] = dev->fifo->data[data_start_index++];
  5027. aux_data->data[2] = dev->fifo->data[data_start_index++];
  5028. aux_data->data[3] = dev->fifo->data[data_start_index++];
  5029. aux_data->data[4] = dev->fifo->data[data_start_index++];
  5030. aux_data->data[5] = dev->fifo->data[data_start_index++];
  5031. aux_data->data[6] = dev->fifo->data[data_start_index++];
  5032. aux_data->data[7] = dev->fifo->data[data_start_index++];
  5033. }
  5034. /*!
  5035. * @brief This API is used to parse the aux data from the
  5036. * FIFO data in header mode.
  5037. */
  5038. static void extract_aux_header_mode(
  5039. struct bmi160_aux_data* aux_data,
  5040. uint8_t* aux_length,
  5041. const struct bmi160_dev* dev) {
  5042. uint8_t frame_header = 0;
  5043. uint16_t data_index;
  5044. uint8_t aux_index = 0;
  5045. for(data_index = dev->fifo->aux_byte_start_idx; data_index < dev->fifo->length;) {
  5046. /* extracting Frame header */
  5047. frame_header = (dev->fifo->data[data_index] & BMI160_FIFO_TAG_INTR_MASK);
  5048. /*Index is moved to next byte where the data is starting*/
  5049. data_index++;
  5050. switch(frame_header) {
  5051. /* Aux frame */
  5052. case BMI160_FIFO_HEAD_M:
  5053. case BMI160_FIFO_HEAD_M_A:
  5054. case BMI160_FIFO_HEAD_M_G:
  5055. case BMI160_FIFO_HEAD_M_G_A:
  5056. unpack_aux_frame(aux_data, &data_index, &aux_index, frame_header, dev);
  5057. break;
  5058. case BMI160_FIFO_HEAD_G:
  5059. move_next_frame(&data_index, BMI160_FIFO_G_LENGTH, dev);
  5060. break;
  5061. case BMI160_FIFO_HEAD_G_A:
  5062. move_next_frame(&data_index, BMI160_FIFO_GA_LENGTH, dev);
  5063. break;
  5064. case BMI160_FIFO_HEAD_A:
  5065. move_next_frame(&data_index, BMI160_FIFO_A_LENGTH, dev);
  5066. break;
  5067. /* Sensor time frame */
  5068. case BMI160_FIFO_HEAD_SENSOR_TIME:
  5069. unpack_sensortime_frame(&data_index, dev);
  5070. break;
  5071. /* Skip frame */
  5072. case BMI160_FIFO_HEAD_SKIP_FRAME:
  5073. unpack_skipped_frame(&data_index, dev);
  5074. break;
  5075. /* Input config frame */
  5076. case BMI160_FIFO_HEAD_INPUT_CONFIG:
  5077. move_next_frame(&data_index, 1, dev);
  5078. break;
  5079. case BMI160_FIFO_HEAD_OVER_READ:
  5080. /* Update the data index as complete in case
  5081. * of over read */
  5082. data_index = dev->fifo->length;
  5083. break;
  5084. default:
  5085. /* Update the data index as complete in case of
  5086. * getting other headers like 0x00 */
  5087. data_index = dev->fifo->length;
  5088. break;
  5089. }
  5090. if(*aux_length == aux_index) {
  5091. /*Number of frames to read completed*/
  5092. break;
  5093. }
  5094. }
  5095. /* Update number of aux data read */
  5096. *aux_length = aux_index;
  5097. /* Update the aux frame index */
  5098. dev->fifo->aux_byte_start_idx = data_index;
  5099. }
  5100. /*!
  5101. * @brief This API checks the presence of non-valid frames in the read fifo data.
  5102. */
  5103. static void check_frame_validity(uint16_t* data_index, const struct bmi160_dev* dev) {
  5104. if((*data_index + 2) < dev->fifo->length) {
  5105. /* Check if FIFO is empty */
  5106. if((dev->fifo->data[*data_index] == FIFO_CONFIG_MSB_CHECK) &&
  5107. (dev->fifo->data[*data_index + 1] == FIFO_CONFIG_LSB_CHECK)) {
  5108. /*Update the data index as complete*/
  5109. *data_index = dev->fifo->length;
  5110. }
  5111. }
  5112. }
  5113. /*!
  5114. * @brief This API is used to move the data index ahead of the
  5115. * current_frame_length parameter when unnecessary FIFO data appears while
  5116. * extracting the user specified data.
  5117. */
  5118. static void move_next_frame(
  5119. uint16_t* data_index,
  5120. uint8_t current_frame_length,
  5121. const struct bmi160_dev* dev) {
  5122. /*Partial read, then move the data index to last data*/
  5123. if((*data_index + current_frame_length) > dev->fifo->length) {
  5124. /*Update the data index as complete*/
  5125. *data_index = dev->fifo->length;
  5126. } else {
  5127. /*Move the data index to next frame*/
  5128. *data_index = *data_index + current_frame_length;
  5129. }
  5130. }
  5131. /*!
  5132. * @brief This API is used to parse and store the sensor time from the
  5133. * FIFO data in the structure instance dev.
  5134. */
  5135. static void unpack_sensortime_frame(uint16_t* data_index, const struct bmi160_dev* dev) {
  5136. uint32_t sensor_time_byte3 = 0;
  5137. uint16_t sensor_time_byte2 = 0;
  5138. uint8_t sensor_time_byte1 = 0;
  5139. /*Partial read, then move the data index to last data*/
  5140. if((*data_index + BMI160_SENSOR_TIME_LENGTH) > dev->fifo->length) {
  5141. /*Update the data index as complete*/
  5142. *data_index = dev->fifo->length;
  5143. } else {
  5144. sensor_time_byte3 = dev->fifo->data[(*data_index) + BMI160_SENSOR_TIME_MSB_BYTE] << 16;
  5145. sensor_time_byte2 = dev->fifo->data[(*data_index) + BMI160_SENSOR_TIME_XLSB_BYTE] << 8;
  5146. sensor_time_byte1 = dev->fifo->data[(*data_index)];
  5147. /* Sensor time */
  5148. dev->fifo->sensor_time =
  5149. (uint32_t)(sensor_time_byte3 | sensor_time_byte2 | sensor_time_byte1);
  5150. *data_index = (*data_index) + BMI160_SENSOR_TIME_LENGTH;
  5151. }
  5152. }
  5153. /*!
  5154. * @brief This API is used to parse and store the skipped_frame_count from
  5155. * the FIFO data in the structure instance dev.
  5156. */
  5157. static void unpack_skipped_frame(uint16_t* data_index, const struct bmi160_dev* dev) {
  5158. /*Partial read, then move the data index to last data*/
  5159. if(*data_index >= dev->fifo->length) {
  5160. /*Update the data index as complete*/
  5161. *data_index = dev->fifo->length;
  5162. } else {
  5163. dev->fifo->skipped_frame_count = dev->fifo->data[*data_index];
  5164. /*Move the data index*/
  5165. *data_index = (*data_index) + 1;
  5166. }
  5167. }
  5168. /*!
  5169. * @brief This API is used to get the FOC status from the sensor
  5170. */
  5171. static int8_t get_foc_status(uint8_t* foc_status, struct bmi160_dev const* dev) {
  5172. int8_t rslt;
  5173. uint8_t data;
  5174. /* Read the FOC status from sensor */
  5175. rslt = bmi160_get_regs(BMI160_STATUS_ADDR, &data, 1, dev);
  5176. if(rslt == BMI160_OK) {
  5177. /* Get the foc_status bit */
  5178. *foc_status = BMI160_GET_BITS(data, BMI160_FOC_STATUS);
  5179. }
  5180. return rslt;
  5181. }
  5182. /*!
  5183. * @brief This API is used to configure the offset enable bits in the sensor
  5184. */
  5185. static int8_t
  5186. configure_offset_enable(const struct bmi160_foc_conf* foc_conf, struct bmi160_dev const* dev) {
  5187. int8_t rslt;
  5188. uint8_t data;
  5189. /* Null-pointer check */
  5190. rslt = null_ptr_check(dev);
  5191. if(rslt != BMI160_OK) {
  5192. rslt = BMI160_E_NULL_PTR;
  5193. } else {
  5194. /* Read the FOC config from the sensor */
  5195. rslt = bmi160_get_regs(BMI160_OFFSET_CONF_ADDR, &data, 1, dev);
  5196. if(rslt == BMI160_OK) {
  5197. /* Set the offset enable/disable for gyro */
  5198. data = BMI160_SET_BITS(data, BMI160_GYRO_OFFSET_EN, foc_conf->gyro_off_en);
  5199. /* Set the offset enable/disable for accel */
  5200. data = BMI160_SET_BITS(data, BMI160_ACCEL_OFFSET_EN, foc_conf->acc_off_en);
  5201. /* Set the offset config in the sensor */
  5202. rslt = bmi160_set_regs(BMI160_OFFSET_CONF_ADDR, &data, 1, dev);
  5203. }
  5204. }
  5205. return rslt;
  5206. }
  5207. static int8_t trigger_foc(struct bmi160_offsets* offset, struct bmi160_dev const* dev) {
  5208. int8_t rslt;
  5209. uint8_t foc_status = BMI160_ENABLE;
  5210. uint8_t cmd = BMI160_START_FOC_CMD;
  5211. uint8_t timeout = 0;
  5212. uint8_t data_array[20];
  5213. /* Start the FOC process */
  5214. rslt = bmi160_set_regs(BMI160_COMMAND_REG_ADDR, &cmd, 1, dev);
  5215. if(rslt == BMI160_OK) {
  5216. /* Check the FOC status*/
  5217. rslt = get_foc_status(&foc_status, dev);
  5218. if((rslt != BMI160_OK) || (foc_status != BMI160_ENABLE)) {
  5219. while((foc_status != BMI160_ENABLE) && (timeout < 11)) {
  5220. /* Maximum time of 250ms is given in 10
  5221. * steps of 25ms each - 250ms refer datasheet 2.9.1 */
  5222. dev->delay_ms(25);
  5223. /* Check the FOC status*/
  5224. rslt = get_foc_status(&foc_status, dev);
  5225. timeout++;
  5226. }
  5227. if((rslt == BMI160_OK) && (foc_status == BMI160_ENABLE)) {
  5228. /* Get offset values from sensor */
  5229. rslt = bmi160_get_offsets(offset, dev);
  5230. } else {
  5231. /* FOC failure case */
  5232. rslt = BMI160_E_FOC_FAILURE;
  5233. }
  5234. }
  5235. if(rslt == BMI160_OK) {
  5236. /* Read registers 0x04-0x17 */
  5237. rslt = bmi160_get_regs(BMI160_GYRO_DATA_ADDR, data_array, 20, dev);
  5238. }
  5239. }
  5240. return rslt;
  5241. }