bmi160.c 198 KB

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