bmi160.c 206 KB

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