bmi160.c 198 KB

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