bmi160.c 170 KB

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