| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428 |
- /**
- * Copyright (c) 2021 Bosch Sensortec GmbH. All rights reserved.
- *
- * BSD-3-Clause
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * 3. Neither the name of the copyright holder nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
- * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- *
- * @file bmi160.c
- * @date 2021-10-05
- * @version v3.9.2
- *
- */
- #include "bmi160.h"
- /* Below look up table follows the enum bmi160_int_types.
- * Hence any change should match to the enum bmi160_int_types
- */
- const uint8_t int_mask_lookup_table[13] = {
- BMI160_INT1_SLOPE_MASK, BMI160_INT1_SLOPE_MASK, BMI160_INT2_LOW_STEP_DETECT_MASK, BMI160_INT1_DOUBLE_TAP_MASK,
- BMI160_INT1_SINGLE_TAP_MASK, BMI160_INT1_ORIENT_MASK, BMI160_INT1_FLAT_MASK, BMI160_INT1_HIGH_G_MASK,
- BMI160_INT1_LOW_G_MASK, BMI160_INT1_NO_MOTION_MASK, BMI160_INT2_DATA_READY_MASK, BMI160_INT2_FIFO_FULL_MASK,
- BMI160_INT2_FIFO_WM_MASK
- };
- /*********************************************************************/
- /* Static function declarations */
- /*!
- * @brief This API configures the pins to fire the
- * interrupt signal when it occurs
- *
- * @param[in] int_config : Structure instance of bmi160_int_settg.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error.
- */
- static int8_t set_intr_pin_config(const struct bmi160_int_settg *int_config, const struct bmi160_dev *dev);
- /*!
- * @brief This API sets the any-motion interrupt of the sensor.
- * This interrupt occurs when accel values exceeds preset threshold
- * for a certain period of time.
- *
- * @param[in] int_config : Structure instance of bmi160_int_settg.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error.
- */
- static int8_t set_accel_any_motion_int(struct bmi160_int_settg *int_config, struct bmi160_dev *dev);
- /*!
- * @brief This API sets tap interrupts.Interrupt is fired when
- * tap movements happen.
- *
- * @param[in] int_config : Structure instance of bmi160_int_settg.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error.
- */
- static int8_t set_accel_tap_int(struct bmi160_int_settg *int_config, const struct bmi160_dev *dev);
- /*!
- * @brief This API sets the data ready interrupt for both accel and gyro.
- * This interrupt occurs when new accel and gyro data come.
- *
- * @param[in] int_config : Structure instance of bmi160_int_settg.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error.
- */
- static int8_t set_accel_gyro_data_ready_int(const struct bmi160_int_settg *int_config, const struct bmi160_dev *dev);
- /*!
- * @brief This API sets the significant motion interrupt of the sensor.This
- * interrupt occurs when there is change in user location.
- *
- * @param[in] int_config : Structure instance of bmi160_int_settg.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error.
- */
- static int8_t set_accel_sig_motion_int(struct bmi160_int_settg *int_config, struct bmi160_dev *dev);
- /*!
- * @brief This API sets the no motion/slow motion interrupt of the sensor.
- * Slow motion is similar to any motion interrupt.No motion interrupt
- * occurs when slope bet. two accel values falls below preset threshold
- * for preset duration.
- *
- * @param[in] int_config : Structure instance of bmi160_int_settg.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error.
- */
- static int8_t set_accel_no_motion_int(struct bmi160_int_settg *int_config, const struct bmi160_dev *dev);
- /*!
- * @brief This API sets the step detection interrupt.This interrupt
- * occurs when the single step causes accel values to go above
- * preset threshold.
- *
- * @param[in] int_config : Structure instance of bmi160_int_settg.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error.
- */
- static int8_t set_accel_step_detect_int(struct bmi160_int_settg *int_config, const struct bmi160_dev *dev);
- /*!
- * @brief This API sets the orientation interrupt of the sensor.This
- * interrupt occurs when there is orientation change in the sensor
- * with respect to gravitational field vector g.
- *
- * @param[in] int_config : Structure instance of bmi160_int_settg.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error.
- */
- static int8_t set_accel_orientation_int(struct bmi160_int_settg *int_config, const struct bmi160_dev *dev);
- /*!
- * @brief This API sets the flat interrupt of the sensor.This interrupt
- * occurs in case of flat orientation
- *
- * @param[in] int_config : Structure instance of bmi160_int_settg.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error.
- */
- static int8_t set_accel_flat_detect_int(struct bmi160_int_settg *int_config, const struct bmi160_dev *dev);
- /*!
- * @brief This API sets the low-g interrupt of the sensor.This interrupt
- * occurs during free-fall.
- *
- * @param[in] int_config : Structure instance of bmi160_int_settg.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error.
- */
- static int8_t set_accel_low_g_int(struct bmi160_int_settg *int_config, const struct bmi160_dev *dev);
- /*!
- * @brief This API sets the high-g interrupt of the sensor.The interrupt
- * occurs if the absolute value of acceleration data of any enabled axis
- * exceeds the programmed threshold and the sign of the value does not
- * change for a preset duration.
- *
- * @param[in] int_config : Structure instance of bmi160_int_settg.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error.
- */
- static int8_t set_accel_high_g_int(struct bmi160_int_settg *int_config, const struct bmi160_dev *dev);
- /*!
- * @brief This API sets the default configuration parameters of accel & gyro.
- * Also maintain the previous state of configurations.
- *
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error.
- */
- static void default_param_settg(struct bmi160_dev *dev);
- /*!
- * @brief This API is used to validate the device structure pointer for
- * null conditions.
- *
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error.
- */
- static int8_t null_ptr_check(const struct bmi160_dev *dev);
- /*!
- * @brief This API set the accel configuration.
- *
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error.
- */
- static int8_t set_accel_conf(struct bmi160_dev *dev);
- /*!
- * @brief This API gets the accel configuration.
- *
- * @param[out] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error.
- */
- static int8_t get_accel_conf(struct bmi160_dev *dev);
- /*!
- * @brief This API check the accel configuration.
- *
- * @param[in] data : Pointer to store the updated accel config.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error.
- */
- static int8_t check_accel_config(uint8_t *data, const struct bmi160_dev *dev);
- /*!
- * @brief This API process the accel odr.
- *
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error.
- */
- static int8_t process_accel_odr(uint8_t *data, const struct bmi160_dev *dev);
- /*!
- * @brief This API process the accel bandwidth.
- *
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error.
- */
- static int8_t process_accel_bw(uint8_t *data, const struct bmi160_dev *dev);
- /*!
- * @brief This API process the accel range.
- *
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error.
- */
- static int8_t process_accel_range(uint8_t *data, const struct bmi160_dev *dev);
- /*!
- * @brief This API checks the invalid settings for ODR & Bw for Accel and Gyro.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error.
- */
- static int8_t check_invalid_settg(const struct bmi160_dev *dev);
- /*!
- * @brief This API set the gyro configuration.
- *
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error.
- */
- static int8_t set_gyro_conf(struct bmi160_dev *dev);
- /*!
- * @brief This API get the gyro configuration.
- *
- * @param[out] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error.
- */
- static int8_t get_gyro_conf(struct bmi160_dev *dev);
- /*!
- * @brief This API check the gyro configuration.
- *
- * @param[in] data : Pointer to store the updated gyro config.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error.
- */
- static int8_t check_gyro_config(uint8_t *data, const struct bmi160_dev *dev);
- /*!
- * @brief This API process the gyro odr.
- *
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error.
- */
- static int8_t process_gyro_odr(uint8_t *data, const struct bmi160_dev *dev);
- /*!
- * @brief This API process the gyro bandwidth.
- *
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error.
- */
- static int8_t process_gyro_bw(uint8_t *data, const struct bmi160_dev *dev);
- /*!
- * @brief This API process the gyro range.
- *
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error.
- */
- static int8_t process_gyro_range(uint8_t *data, const struct bmi160_dev *dev);
- /*!
- * @brief This API sets the accel power mode.
- *
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error.
- */
- static int8_t set_accel_pwr(struct bmi160_dev *dev);
- /*!
- * @brief This API process the undersampling setting of Accel.
- *
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error.
- */
- static int8_t process_under_sampling(uint8_t *data, const struct bmi160_dev *dev);
- /*!
- * @brief This API sets the gyro power mode.
- *
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error.
- */
- static int8_t set_gyro_pwr(struct bmi160_dev *dev);
- /*!
- * @brief This API reads accel data along with sensor time if time is requested
- * by user. Kindly refer the user guide(README.md) for more info.
- *
- * @param[in] len : len to read no of bytes
- * @param[out] accel : Structure pointer to store accel data
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t get_accel_data(uint8_t len, struct bmi160_sensor_data *accel, const struct bmi160_dev *dev);
- /*!
- * @brief This API reads accel data along with sensor time if time is requested
- * by user. Kindly refer the user guide(README.md) for more info.
- *
- * @param[in] len : len to read no of bytes
- * @param[out] gyro : Structure pointer to store accel data
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t get_gyro_data(uint8_t len, struct bmi160_sensor_data *gyro, const struct bmi160_dev *dev);
- /*!
- * @brief This API reads accel and gyro data along with sensor time
- * if time is requested by user.
- * Kindly refer the user guide(README.md) for more info.
- *
- * @param[in] len : len to read no of bytes
- * @param[out] accel : Structure pointer to store accel data
- * @param[out] gyro : Structure pointer to store accel data
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t get_accel_gyro_data(uint8_t len,
- struct bmi160_sensor_data *accel,
- struct bmi160_sensor_data *gyro,
- const struct bmi160_dev *dev);
- /*!
- * @brief This API enables the any-motion interrupt for accel.
- *
- * @param[in] any_motion_int_cfg : Structure instance of
- * bmi160_acc_any_mot_int_cfg.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t enable_accel_any_motion_int(const struct bmi160_acc_any_mot_int_cfg *any_motion_int_cfg,
- struct bmi160_dev *dev);
- /*!
- * @brief This API disable the sig-motion interrupt.
- *
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t disable_sig_motion_int(const struct bmi160_dev *dev);
- /*!
- * @brief This API configure the source of data(filter & pre-filter)
- * for any-motion interrupt.
- *
- * @param[in] any_motion_int_cfg : Structure instance of
- * bmi160_acc_any_mot_int_cfg.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t config_any_motion_src(const struct bmi160_acc_any_mot_int_cfg *any_motion_int_cfg,
- const struct bmi160_dev *dev);
- /*!
- * @brief This API configure the duration and threshold of
- * any-motion interrupt.
- *
- * @param[in] any_motion_int_cfg : Structure instance of
- * bmi160_acc_any_mot_int_cfg.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t config_any_dur_threshold(const struct bmi160_acc_any_mot_int_cfg *any_motion_int_cfg,
- const struct bmi160_dev *dev);
- /*!
- * @brief This API configure necessary setting of any-motion interrupt.
- *
- * @param[in] int_config : Structure instance of bmi160_int_settg.
- * @param[in] any_motion_int_cfg : Structure instance of
- * bmi160_acc_any_mot_int_cfg.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t config_any_motion_int_settg(const struct bmi160_int_settg *int_config,
- const struct bmi160_acc_any_mot_int_cfg *any_motion_int_cfg,
- const struct bmi160_dev *dev);
- /*!
- * @brief This API enable the data ready interrupt.
- *
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t enable_data_ready_int(const struct bmi160_dev *dev);
- /*!
- * @brief This API enables the no motion/slow motion interrupt.
- *
- * @param[in] no_mot_int_cfg : Structure instance of
- * bmi160_acc_no_motion_int_cfg.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t enable_no_motion_int(const struct bmi160_acc_no_motion_int_cfg *no_mot_int_cfg,
- const struct bmi160_dev *dev);
- /*!
- * @brief This API configure the interrupt PIN setting for
- * no motion/slow motion interrupt.
- *
- * @param[in] int_config : structure instance of bmi160_int_settg.
- * @param[in] no_mot_int_cfg : Structure instance of
- * bmi160_acc_no_motion_int_cfg.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t config_no_motion_int_settg(const struct bmi160_int_settg *int_config,
- const struct bmi160_acc_no_motion_int_cfg *no_mot_int_cfg,
- const struct bmi160_dev *dev);
- /*!
- * @brief This API configure the source of interrupt for no motion.
- *
- * @param[in] no_mot_int_cfg : Structure instance of
- * bmi160_acc_no_motion_int_cfg.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t config_no_motion_data_src(const struct bmi160_acc_no_motion_int_cfg *no_mot_int_cfg,
- const struct bmi160_dev *dev);
- /*!
- * @brief This API configure the duration and threshold of
- * no motion/slow motion interrupt along with selection of no/slow motion.
- *
- * @param[in] no_mot_int_cfg : Structure instance of
- * bmi160_acc_no_motion_int_cfg.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t config_no_motion_dur_thr(const struct bmi160_acc_no_motion_int_cfg *no_mot_int_cfg,
- const struct bmi160_dev *dev);
- /*!
- * @brief This API enables the sig-motion motion interrupt.
- *
- * @param[in] sig_mot_int_cfg : Structure instance of
- * bmi160_acc_sig_mot_int_cfg.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t enable_sig_motion_int(const struct bmi160_acc_sig_mot_int_cfg *sig_mot_int_cfg, struct bmi160_dev *dev);
- /*!
- * @brief This API configure the interrupt PIN setting for
- * significant motion interrupt.
- *
- * @param[in] int_config : Structure instance of bmi160_int_settg.
- * @param[in] sig_mot_int_cfg : Structure instance of
- * bmi160_acc_sig_mot_int_cfg.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t config_sig_motion_int_settg(const struct bmi160_int_settg *int_config,
- const struct bmi160_acc_sig_mot_int_cfg *sig_mot_int_cfg,
- const struct bmi160_dev *dev);
- /*!
- * @brief This API configure the source of data(filter & pre-filter)
- * for sig motion interrupt.
- *
- * @param[in] sig_mot_int_cfg : Structure instance of
- * bmi160_acc_sig_mot_int_cfg.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t config_sig_motion_data_src(const struct bmi160_acc_sig_mot_int_cfg *sig_mot_int_cfg,
- const struct bmi160_dev *dev);
- /*!
- * @brief This API configure the threshold, skip and proof time of
- * sig motion interrupt.
- *
- * @param[in] sig_mot_int_cfg : Structure instance of
- * bmi160_acc_sig_mot_int_cfg.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t config_sig_dur_threshold(const struct bmi160_acc_sig_mot_int_cfg *sig_mot_int_cfg,
- const struct bmi160_dev *dev);
- /*!
- * @brief This API enables the step detector interrupt.
- *
- * @param[in] step_detect_int_cfg : Structure instance of
- * bmi160_acc_step_detect_int_cfg.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t enable_step_detect_int(const struct bmi160_acc_step_detect_int_cfg *step_detect_int_cfg,
- const struct bmi160_dev *dev);
- /*!
- * @brief This API configure the step detector parameter.
- *
- * @param[in] step_detect_int_cfg : Structure instance of
- * bmi160_acc_step_detect_int_cfg.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t config_step_detect(const struct bmi160_acc_step_detect_int_cfg *step_detect_int_cfg,
- const struct bmi160_dev *dev);
- /*!
- * @brief This API enables the single/double tap interrupt.
- *
- * @param[in] int_config : Structure instance of bmi160_int_settg.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t enable_tap_int(const struct bmi160_int_settg *int_config,
- const struct bmi160_acc_tap_int_cfg *tap_int_cfg,
- const struct bmi160_dev *dev);
- /*!
- * @brief This API configure the interrupt PIN setting for
- * tap interrupt.
- *
- * @param[in] int_config : Structure instance of bmi160_int_settg.
- * @param[in] tap_int_cfg : Structure instance of bmi160_acc_tap_int_cfg.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t config_tap_int_settg(const struct bmi160_int_settg *int_config,
- const struct bmi160_acc_tap_int_cfg *tap_int_cfg,
- const struct bmi160_dev *dev);
- /*!
- * @brief This API configure the source of data(filter & pre-filter)
- * for tap interrupt.
- *
- * @param[in] tap_int_cfg : Structure instance of bmi160_acc_tap_int_cfg.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t config_tap_data_src(const struct bmi160_acc_tap_int_cfg *tap_int_cfg, const struct bmi160_dev *dev);
- /*!
- * @brief This API configure the parameters of tap interrupt.
- * Threshold, quite, shock, and duration.
- *
- * @param[in] int_config : Structure instance of bmi160_int_settg.
- * @param[in] tap_int_cfg : Structure instance of bmi160_acc_tap_int_cfg.
- * @param[in] dev : structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t config_tap_param(const struct bmi160_int_settg *int_config,
- const struct bmi160_acc_tap_int_cfg *tap_int_cfg,
- const struct bmi160_dev *dev);
- /*!
- * @brief This API enable the external mode configuration.
- *
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t config_sec_if(const struct bmi160_dev *dev);
- /*!
- * @brief This API configure the ODR of the auxiliary sensor.
- *
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t config_aux_odr(const struct bmi160_dev *dev);
- /*!
- * @brief This API maps the actual burst read length set by user.
- *
- * @param[in] len : Pointer to store the read length.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t map_read_len(uint16_t *len, const struct bmi160_dev *dev);
- /*!
- * @brief This API configure the settings of auxiliary sensor.
- *
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t config_aux_settg(const struct bmi160_dev *dev);
- /*!
- * @brief This API extract the read data from auxiliary sensor.
- *
- * @param[in] map_len : burst read value.
- * @param[in] reg_addr : Address of register to read.
- * @param[in] aux_data : Pointer to store the read data.
- * @param[in] len : length to read the data.
- * @param[in] dev : Structure instance of bmi160_dev.
- * @note : Refer user guide for detailed info.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t extract_aux_read(uint16_t map_len,
- uint8_t reg_addr,
- uint8_t *aux_data,
- uint16_t len,
- const struct bmi160_dev *dev);
- /*!
- * @brief This API enables the orient interrupt.
- *
- * @param[in] orient_int_cfg : Structure instance of bmi160_acc_orient_int_cfg.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t enable_orient_int(const struct bmi160_acc_orient_int_cfg *orient_int_cfg, const struct bmi160_dev *dev);
- /*!
- * @brief This API configure the necessary setting of orientation interrupt.
- *
- * @param[in] orient_int_cfg : Structure instance of bmi160_acc_orient_int_cfg.
- * @param[in] dev : structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t config_orient_int_settg(const struct bmi160_acc_orient_int_cfg *orient_int_cfg,
- const struct bmi160_dev *dev);
- /*!
- * @brief This API enables the flat interrupt.
- *
- * @param[in] flat_int : Structure instance of bmi160_acc_flat_detect_int_cfg.
- * @param[in] dev : structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t enable_flat_int(const struct bmi160_acc_flat_detect_int_cfg *flat_int, const struct bmi160_dev *dev);
- /*!
- * @brief This API configure the necessary setting of flat interrupt.
- *
- * @param[in] flat_int : Structure instance of bmi160_acc_flat_detect_int_cfg.
- * @param[in] dev : structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t config_flat_int_settg(const struct bmi160_acc_flat_detect_int_cfg *flat_int,
- const struct bmi160_dev *dev);
- /*!
- * @brief This API enables the Low-g interrupt.
- *
- * @param[in] low_g_int : Structure instance of bmi160_acc_low_g_int_cfg.
- * @param[in] dev : structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t enable_low_g_int(const struct bmi160_acc_low_g_int_cfg *low_g_int, const struct bmi160_dev *dev);
- /*!
- * @brief This API configure the source of data(filter & pre-filter) for low-g interrupt.
- *
- * @param[in] low_g_int : Structure instance of bmi160_acc_low_g_int_cfg.
- * @param[in] dev : structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t config_low_g_data_src(const struct bmi160_acc_low_g_int_cfg *low_g_int, const struct bmi160_dev *dev);
- /*!
- * @brief This API configure the necessary setting of low-g interrupt.
- *
- * @param[in] low_g_int : Structure instance of bmi160_acc_low_g_int_cfg.
- * @param[in] dev : structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t config_low_g_int_settg(const struct bmi160_acc_low_g_int_cfg *low_g_int, const struct bmi160_dev *dev);
- /*!
- * @brief This API enables the high-g interrupt.
- *
- * @param[in] high_g_int_cfg : Structure instance of bmi160_acc_high_g_int_cfg.
- * @param[in] dev : structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t enable_high_g_int(const struct bmi160_acc_high_g_int_cfg *high_g_int_cfg, const struct bmi160_dev *dev);
- /*!
- * @brief This API configure the source of data(filter & pre-filter)
- * for high-g interrupt.
- *
- * @param[in] high_g_int_cfg : Structure instance of bmi160_acc_high_g_int_cfg.
- * @param[in] dev : structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t config_high_g_data_src(const struct bmi160_acc_high_g_int_cfg *high_g_int_cfg,
- const struct bmi160_dev *dev);
- /*!
- * @brief This API configure the necessary setting of high-g interrupt.
- *
- * @param[in] high_g_int_cfg : Structure instance of bmi160_acc_high_g_int_cfg.
- * @param[in] dev : structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t config_high_g_int_settg(const struct bmi160_acc_high_g_int_cfg *high_g_int_cfg,
- const struct bmi160_dev *dev);
- /*!
- * @brief This API configure the behavioural setting of interrupt pin.
- *
- * @param[in] int_config : Structure instance of bmi160_int_settg.
- * @param[in] dev : structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t config_int_out_ctrl(const struct bmi160_int_settg *int_config, const struct bmi160_dev *dev);
- /*!
- * @brief This API configure the mode(input enable, latch or non-latch) of interrupt pin.
- *
- * @param[in] int_config : Structure instance of bmi160_int_settg.
- * @param[in] dev : structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t config_int_latch(const struct bmi160_int_settg *int_config, const struct bmi160_dev *dev);
- /*!
- * @brief This API performs the self test for accelerometer of BMI160
- *
- * @param[in] dev : structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t perform_accel_self_test(struct bmi160_dev *dev);
- /*!
- * @brief This API enables to perform the accel self test by setting proper
- * configurations to facilitate accel self test
- *
- * @param[in] dev : structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t enable_accel_self_test(struct bmi160_dev *dev);
- /*!
- * @brief This API performs accel self test with positive excitation
- *
- * @param[in] accel_pos : Structure pointer to store accel data
- * for positive excitation
- * @param[in] dev : structure instance of bmi160_dev
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t accel_self_test_positive_excitation(struct bmi160_sensor_data *accel_pos, const struct bmi160_dev *dev);
- /*!
- * @brief This API performs accel self test with negative excitation
- *
- * @param[in] accel_neg : Structure pointer to store accel data
- * for negative excitation
- * @param[in] dev : structure instance of bmi160_dev
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t accel_self_test_negative_excitation(struct bmi160_sensor_data *accel_neg, const struct bmi160_dev *dev);
- /*!
- * @brief This API validates the accel self test results
- *
- * @param[in] accel_pos : Structure pointer to store accel data
- * for positive excitation
- * @param[in] accel_neg : Structure pointer to store accel data
- * for negative excitation
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error / +ve value -> Self test fail
- */
- static int8_t validate_accel_self_test(const struct bmi160_sensor_data *accel_pos,
- const struct bmi160_sensor_data *accel_neg);
- /*!
- * @brief This API performs the self test for gyroscope of BMI160
- *
- * @param[in] dev : structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t perform_gyro_self_test(const struct bmi160_dev *dev);
- /*!
- * @brief This API enables the self test bit to trigger self test for gyro
- *
- * @param[in] dev : structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t enable_gyro_self_test(const struct bmi160_dev *dev);
- /*!
- * @brief This API validates the self test results of gyro
- *
- * @param[in] dev : structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t validate_gyro_self_test(const struct bmi160_dev *dev);
- /*!
- * @brief This API sets FIFO full interrupt of the sensor.This interrupt
- * occurs when the FIFO is full and the next full data sample would cause
- * a FIFO overflow, which may delete the old samples.
- *
- * @param[in] int_config : Structure instance of bmi160_int_settg.
- * @param[in] dev : structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t set_fifo_full_int(const struct bmi160_int_settg *int_config, const struct bmi160_dev *dev);
- /*!
- * @brief This enable the FIFO full interrupt engine.
- *
- * @param[in] int_config : Structure instance of bmi160_int_settg.
- * @param[in] dev : structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t enable_fifo_full_int(const struct bmi160_int_settg *int_config, const struct bmi160_dev *dev);
- /*!
- * @brief This API sets FIFO watermark interrupt of the sensor.The FIFO
- * watermark interrupt is fired, when the FIFO fill level is above a fifo
- * watermark.
- *
- * @param[in] int_config : Structure instance of bmi160_int_settg.
- * @param[in] dev : structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t set_fifo_watermark_int(const struct bmi160_int_settg *int_config, const struct bmi160_dev *dev);
- /*!
- * @brief This enable the FIFO watermark interrupt engine.
- *
- * @param[in] int_config : Structure instance of bmi160_int_settg.
- * @param[in] dev : structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t enable_fifo_wtm_int(const struct bmi160_int_settg *int_config, const struct bmi160_dev *dev);
- /*!
- * @brief This API is used to reset the FIFO related configurations
- * in the fifo_frame structure.
- *
- * @param[in] dev : structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static void reset_fifo_data_structure(const struct bmi160_dev *dev);
- /*!
- * @brief This API is used to read number of bytes filled
- * currently in FIFO buffer.
- *
- * @param[in] bytes_to_read : Number of bytes available in FIFO at the
- * instant which is obtained from FIFO counter.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error.
- * @retval Any non zero value -> Fail
- *
- */
- static int8_t get_fifo_byte_counter(uint16_t *bytes_to_read, struct bmi160_dev const *dev);
- /*!
- * @brief This API is used to compute the number of bytes of accel FIFO data
- * which is to be parsed in header-less mode
- *
- * @param[out] data_index : The start index for parsing data
- * @param[out] data_read_length : Number of bytes to be parsed
- * @param[in] acc_frame_count : Number of accelerometer frames to be read
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- */
- static void get_accel_len_to_parse(uint16_t *data_index,
- uint16_t *data_read_length,
- const uint8_t *acc_frame_count,
- const struct bmi160_dev *dev);
- /*!
- * @brief This API is used to parse the accelerometer data from the
- * FIFO data in both header mode and header-less mode.
- * It updates the idx value which is used to store the index of
- * the current data byte which is parsed.
- *
- * @param[in,out] acc : structure instance of sensor data
- * @param[in,out] idx : Index value of number of bytes parsed
- * @param[in,out] acc_idx : Index value of accelerometer data
- * (x,y,z axes) frames parsed
- * @param[in] frame_info : It consists of either fifo_data_enable
- * parameter in header-less mode or
- * frame header data in header mode
- * @param[in] dev : structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static void unpack_accel_frame(struct bmi160_sensor_data *acc,
- uint16_t *idx,
- uint8_t *acc_idx,
- uint8_t frame_info,
- const struct bmi160_dev *dev);
- /*!
- * @brief This API is used to parse the accelerometer data from the
- * FIFO data and store it in the instance of the structure bmi160_sensor_data.
- *
- * @param[in,out] accel_data : structure instance of sensor data
- * @param[in,out] data_start_index : Index value of number of bytes parsed
- * @param[in] dev : structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static void unpack_accel_data(struct bmi160_sensor_data *accel_data,
- uint16_t data_start_index,
- const struct bmi160_dev *dev);
- /*!
- * @brief This API is used to parse the accelerometer data from the
- * FIFO data in header mode.
- *
- * @param[in,out] accel_data : Structure instance of sensor data
- * @param[in,out] accel_length : Number of accelerometer frames
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static void extract_accel_header_mode(struct bmi160_sensor_data *accel_data,
- uint8_t *accel_length,
- const struct bmi160_dev *dev);
- /*!
- * @brief This API computes the number of bytes of gyro FIFO data
- * which is to be parsed in header-less mode
- *
- * @param[out] data_index : The start index for parsing data
- * @param[out] data_read_length : No of bytes to be parsed from FIFO buffer
- * @param[in] gyro_frame_count : Number of Gyro data frames to be read
- * @param[in] dev : Structure instance of bmi160_dev.
- */
- static void get_gyro_len_to_parse(uint16_t *data_index,
- uint16_t *data_read_length,
- const uint8_t *gyro_frame_count,
- const struct bmi160_dev *dev);
- /*!
- * @brief This API is used to parse the gyroscope's data from the
- * FIFO data in both header mode and header-less mode.
- * It updates the idx value which is used to store the index of
- * the current data byte which is parsed.
- *
- * @param[in,out] gyro : structure instance of sensor data
- * @param[in,out] idx : Index value of number of bytes parsed
- * @param[in,out] gyro_idx : Index value of gyro data
- * (x,y,z axes) frames parsed
- * @param[in] frame_info : It consists of either fifo_data_enable
- * parameter in header-less mode or
- * frame header data in header mode
- * @param[in] dev : structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static void unpack_gyro_frame(struct bmi160_sensor_data *gyro,
- uint16_t *idx,
- uint8_t *gyro_idx,
- uint8_t frame_info,
- const struct bmi160_dev *dev);
- /*!
- * @brief This API is used to parse the gyro data from the
- * FIFO data and store it in the instance of the structure bmi160_sensor_data.
- *
- * @param[in,out] gyro_data : structure instance of sensor data
- * @param[in,out] data_start_index : Index value of number of bytes parsed
- * @param[in] dev : structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static void unpack_gyro_data(struct bmi160_sensor_data *gyro_data,
- uint16_t data_start_index,
- const struct bmi160_dev *dev);
- /*!
- * @brief This API is used to parse the gyro data from the
- * FIFO data in header mode.
- *
- * @param[in,out] gyro_data : Structure instance of sensor data
- * @param[in,out] gyro_length : Number of gyro frames
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static void extract_gyro_header_mode(struct bmi160_sensor_data *gyro_data,
- uint8_t *gyro_length,
- const struct bmi160_dev *dev);
- /*!
- * @brief This API computes the number of bytes of aux FIFO data
- * which is to be parsed in header-less mode
- *
- * @param[out] data_index : The start index for parsing data
- * @param[out] data_read_length : No of bytes to be parsed from FIFO buffer
- * @param[in] aux_frame_count : Number of Aux data frames to be read
- * @param[in] dev : Structure instance of bmi160_dev.
- */
- static void get_aux_len_to_parse(uint16_t *data_index,
- uint16_t *data_read_length,
- const uint8_t *aux_frame_count,
- const struct bmi160_dev *dev);
- /*!
- * @brief This API is used to parse the aux's data from the
- * FIFO data in both header mode and header-less mode.
- * It updates the idx value which is used to store the index of
- * the current data byte which is parsed
- *
- * @param[in,out] aux_data : structure instance of sensor data
- * @param[in,out] idx : Index value of number of bytes parsed
- * @param[in,out] aux_index : Index value of gyro data
- * (x,y,z axes) frames parsed
- * @param[in] frame_info : It consists of either fifo_data_enable
- * parameter in header-less mode or
- * frame header data in header mode
- * @param[in] dev : structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static void unpack_aux_frame(struct bmi160_aux_data *aux_data,
- uint16_t *idx,
- uint8_t *aux_index,
- uint8_t frame_info,
- const struct bmi160_dev *dev);
- /*!
- * @brief This API is used to parse the aux data from the
- * FIFO data and store it in the instance of the structure bmi160_aux_data.
- *
- * @param[in,out] aux_data : structure instance of sensor data
- * @param[in,out] data_start_index : Index value of number of bytes parsed
- * @param[in] dev : structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static void unpack_aux_data(struct bmi160_aux_data *aux_data, uint16_t data_start_index, const struct bmi160_dev *dev);
- /*!
- * @brief This API is used to parse the aux data from the
- * FIFO data in header mode.
- *
- * @param[in,out] aux_data : Structure instance of sensor data
- * @param[in,out] aux_length : Number of aux frames
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static void extract_aux_header_mode(struct bmi160_aux_data *aux_data, uint8_t *aux_length,
- const struct bmi160_dev *dev);
- /*!
- * @brief This API checks the presence of non-valid frames in the read fifo data.
- *
- * @param[in,out] data_index : The index of the current data to
- * be parsed from fifo data
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static void check_frame_validity(uint16_t *data_index, const struct bmi160_dev *dev);
- /*!
- * @brief This API is used to move the data index ahead of the
- * current_frame_length parameter when unnecessary FIFO data appears while
- * extracting the user specified data.
- *
- * @param[in,out] data_index : Index of the FIFO data which
- * is to be moved ahead of the
- * current_frame_length
- * @param[in] current_frame_length : Number of bytes in a particular frame
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static void move_next_frame(uint16_t *data_index, uint8_t current_frame_length, const struct bmi160_dev *dev);
- /*!
- * @brief This API is used to parse and store the sensor time from the
- * FIFO data in the structure instance dev.
- *
- * @param[in,out] data_index : Index of the FIFO data which
- * has the sensor time.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static void unpack_sensortime_frame(uint16_t *data_index, const struct bmi160_dev *dev);
- /*!
- * @brief This API is used to parse and store the skipped_frame_count from
- * the FIFO data in the structure instance dev.
- *
- * @param[in,out] data_index : Index of the FIFO data which
- * has the skipped frame count.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static void unpack_skipped_frame(uint16_t *data_index, const struct bmi160_dev *dev);
- /*!
- * @brief This API is used to get the FOC status from the sensor
- *
- * @param[in,out] foc_status : Result of FOC status.
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t get_foc_status(uint8_t *foc_status, struct bmi160_dev const *dev);
- /*!
- * @brief This API is used to configure the offset enable bits in the sensor
- *
- * @param[in,out] foc_conf : Structure instance of bmi160_foc_conf which
- * has the FOC and offset configurations
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t configure_offset_enable(const struct bmi160_foc_conf *foc_conf, struct bmi160_dev const *dev);
- /*!
- * @brief This API is used to trigger the FOC in the sensor
- *
- * @param[in,out] offset : Structure instance of bmi160_offsets which
- * reads and stores the offset values after FOC
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t trigger_foc(struct bmi160_offsets *offset, struct bmi160_dev const *dev);
- /*!
- * @brief This API is used to map/unmap the Dataready(Accel & Gyro), FIFO full
- * and FIFO watermark interrupt
- *
- * @param[in] int_config : Structure instance of bmi160_int_settg which
- * stores the interrupt type and interrupt channel
- * configurations to map/unmap the interrupt pins
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t map_hardware_interrupt(const struct bmi160_int_settg *int_config, const struct bmi160_dev *dev);
- /*!
- * @brief This API is used to map/unmap the Any/Sig motion, Step det/Low-g,
- * Double tap, Single tap, Orientation, Flat, High-G, Nomotion interrupt pins.
- *
- * @param[in] int_config : Structure instance of bmi160_int_settg which
- * stores the interrupt type and interrupt channel
- * configurations to map/unmap the interrupt pins
- * @param[in] dev : Structure instance of bmi160_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / -ve value -> Error
- */
- static int8_t map_feature_interrupt(const struct bmi160_int_settg *int_config, const struct bmi160_dev *dev);
- /*********************** User function definitions ****************************/
- /*!
- * @brief This API reads the data from the given register address
- * of sensor.
- */
- int8_t bmi160_get_regs(uint8_t reg_addr, uint8_t *data, uint16_t len, const struct bmi160_dev *dev)
- {
- int8_t rslt = BMI160_OK;
- /* Null-pointer check */
- if ((dev == NULL) || (dev->read == NULL))
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else if (len == 0)
- {
- rslt = BMI160_E_READ_WRITE_LENGTH_INVALID;
- }
- else
- {
- /* Configuring reg_addr for SPI Interface */
- if (dev->intf == BMI160_SPI_INTF)
- {
- reg_addr = (reg_addr | BMI160_SPI_RD_MASK);
- }
- rslt = dev->read(dev->id, reg_addr, data, len);
- }
- return rslt;
- }
- /*!
- * @brief This API writes the given data to the register address
- * of sensor.
- */
- int8_t bmi160_set_regs(uint8_t reg_addr, uint8_t *data, uint16_t len, const struct bmi160_dev *dev)
- {
- int8_t rslt = BMI160_OK;
- uint8_t count = 0;
- /* Null-pointer check */
- if ((dev == NULL) || (dev->write == NULL))
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else if (len == 0)
- {
- rslt = BMI160_E_READ_WRITE_LENGTH_INVALID;
- }
- else
- {
- /* Configuring reg_addr for SPI Interface */
- if (dev->intf == BMI160_SPI_INTF)
- {
- reg_addr = (reg_addr & BMI160_SPI_WR_MASK);
- }
- if ((dev->prev_accel_cfg.power == BMI160_ACCEL_NORMAL_MODE) ||
- (dev->prev_gyro_cfg.power == BMI160_GYRO_NORMAL_MODE))
- {
- rslt = dev->write(dev->id, reg_addr, data, len);
- /* Kindly refer bmi160 data sheet section 3.2.4 */
- dev->delay_ms(1);
- }
- else
- {
- /*Burst write is not allowed in
- * suspend & low power mode */
- for (; count < len; count++)
- {
- rslt = dev->write(dev->id, reg_addr, &data[count], 1);
- reg_addr++;
- /* Kindly refer bmi160 data sheet section 3.2.4 */
- dev->delay_ms(1);
- }
- }
- if (rslt != BMI160_OK)
- {
- rslt = BMI160_E_COM_FAIL;
- }
- }
- return rslt;
- }
- /*!
- * @brief This API is the entry point for sensor.It performs
- * the selection of I2C/SPI read mechanism according to the
- * selected interface and reads the chip-id of bmi160 sensor.
- */
- int8_t bmi160_init(struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data;
- uint8_t try = 3;
- /* Null-pointer check */
- rslt = null_ptr_check(dev);
- /* Dummy read of 0x7F register to enable SPI Interface
- * if SPI is used */
- if ((rslt == BMI160_OK) && (dev->intf == BMI160_SPI_INTF))
- {
- rslt = bmi160_get_regs(BMI160_SPI_COMM_TEST_ADDR, &data, 1, dev);
- }
- if (rslt == BMI160_OK)
- {
- /* Assign chip id as zero */
- dev->chip_id = 0;
- while ((try--) && (dev->chip_id != BMI160_CHIP_ID))
- {
- /* Read chip_id */
- rslt = bmi160_get_regs(BMI160_CHIP_ID_ADDR, &dev->chip_id, 1, dev);
- }
- if ((rslt == BMI160_OK) && (dev->chip_id == BMI160_CHIP_ID))
- {
- dev->any_sig_sel = BMI160_BOTH_ANY_SIG_MOTION_DISABLED;
- /* Soft reset */
- rslt = bmi160_soft_reset(dev);
- }
- else
- {
- rslt = BMI160_E_DEV_NOT_FOUND;
- }
- }
- return rslt;
- }
- /*!
- * @brief This API resets and restarts the device.
- * All register values are overwritten with default parameters.
- */
- int8_t bmi160_soft_reset(struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data = BMI160_SOFT_RESET_CMD;
- /* Null-pointer check */
- if ((dev == NULL) || (dev->delay_ms == NULL))
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- /* Reset the device */
- rslt = bmi160_set_regs(BMI160_COMMAND_REG_ADDR, &data, 1, dev);
- dev->delay_ms(BMI160_SOFT_RESET_DELAY_MS);
- if ((rslt == BMI160_OK) && (dev->intf == BMI160_SPI_INTF))
- {
- /* Dummy read of 0x7F register to enable SPI Interface
- * if SPI is used */
- rslt = bmi160_get_regs(BMI160_SPI_COMM_TEST_ADDR, &data, 1, dev);
- }
- if (rslt == BMI160_OK)
- {
- /* Update the default parameters */
- default_param_settg(dev);
- }
- }
- return rslt;
- }
- /*!
- * @brief This API configures the power mode, range and bandwidth
- * of sensor.
- */
- int8_t bmi160_set_sens_conf(struct bmi160_dev *dev)
- {
- int8_t rslt = BMI160_OK;
- /* Null-pointer check */
- if ((dev == NULL) || (dev->delay_ms == NULL))
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- rslt = set_accel_conf(dev);
- if (rslt == BMI160_OK)
- {
- rslt = set_gyro_conf(dev);
- if (rslt == BMI160_OK)
- {
- /* write power mode for accel and gyro */
- rslt = bmi160_set_power_mode(dev);
- if (rslt == BMI160_OK)
- {
- rslt = check_invalid_settg(dev);
- }
- }
- }
- }
- return rslt;
- }
- /*!
- * @brief This API gets accel and gyro configurations.
- */
- int8_t bmi160_get_sens_conf(struct bmi160_dev *dev)
- {
- int8_t rslt = BMI160_OK;
- /* Null-pointer check */
- if ((dev == NULL) || (dev->delay_ms == NULL))
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- rslt = get_accel_conf(dev);
- if (rslt == BMI160_OK)
- {
- rslt = get_gyro_conf(dev);
- }
- }
- return rslt;
- }
- /*!
- * @brief This API sets the power mode of the sensor.
- */
- int8_t bmi160_set_power_mode(struct bmi160_dev *dev)
- {
- int8_t rslt = 0;
- /* Null-pointer check */
- if ((dev == NULL) || (dev->delay_ms == NULL))
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- rslt = set_accel_pwr(dev);
- if (rslt == BMI160_OK)
- {
- rslt = set_gyro_pwr(dev);
- }
- }
- return rslt;
- }
- /*!
- * @brief This API gets the power mode of the sensor.
- */
- int8_t bmi160_get_power_mode(struct bmi160_dev *dev)
- {
- int8_t rslt = 0;
- uint8_t power_mode = 0;
- /* Null-pointer check */
- if ((dev == NULL) || (dev->delay_ms == NULL))
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- rslt = bmi160_get_regs(BMI160_PMU_STATUS_ADDR, &power_mode, 1, dev);
- if (rslt == BMI160_OK)
- {
- /* Power mode of the accel, gyro sensor is obtained */
- dev->gyro_cfg.power = BMI160_GET_BITS(power_mode, BMI160_GYRO_POWER_MODE);
- dev->accel_cfg.power = BMI160_GET_BITS(power_mode, BMI160_ACCEL_POWER_MODE);
- }
- }
- return rslt;
- }
- /*!
- * @brief This API reads sensor data, stores it in
- * the bmi160_sensor_data structure pointer passed by the user.
- */
- int8_t bmi160_get_sensor_data(uint8_t select_sensor,
- struct bmi160_sensor_data *accel,
- struct bmi160_sensor_data *gyro,
- const struct bmi160_dev *dev)
- {
- int8_t rslt = BMI160_OK;
- uint8_t time_sel;
- uint8_t sen_sel;
- uint8_t len = 0;
- /*Extract the sensor and time select information*/
- sen_sel = select_sensor & BMI160_SEN_SEL_MASK;
- time_sel = ((sen_sel & BMI160_TIME_SEL) >> 2);
- sen_sel = sen_sel & (BMI160_ACCEL_SEL | BMI160_GYRO_SEL);
- if (time_sel == 1)
- {
- len = 3;
- }
- /* Null-pointer check */
- if (dev != NULL)
- {
- switch (sen_sel)
- {
- case BMI160_ACCEL_ONLY:
- /* Null-pointer check */
- if (accel == NULL)
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- rslt = get_accel_data(len, accel, dev);
- }
- break;
- case BMI160_GYRO_ONLY:
- /* Null-pointer check */
- if (gyro == NULL)
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- rslt = get_gyro_data(len, gyro, dev);
- }
- break;
- case BMI160_BOTH_ACCEL_AND_GYRO:
- /* Null-pointer check */
- if ((gyro == NULL) || (accel == NULL))
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- rslt = get_accel_gyro_data(len, accel, gyro, dev);
- }
- break;
- default:
- rslt = BMI160_E_INVALID_INPUT;
- break;
- }
- }
- else
- {
- rslt = BMI160_E_NULL_PTR;
- }
- return rslt;
- }
- /*!
- * @brief This API configures the necessary interrupt based on
- * the user settings in the bmi160_int_settg structure instance.
- */
- int8_t bmi160_set_int_config(struct bmi160_int_settg *int_config, struct bmi160_dev *dev)
- {
- int8_t rslt = BMI160_OK;
- switch (int_config->int_type)
- {
- case BMI160_ACC_ANY_MOTION_INT:
- /*Any-motion interrupt*/
- rslt = set_accel_any_motion_int(int_config, dev);
- break;
- case BMI160_ACC_SIG_MOTION_INT:
- /* Significant motion interrupt */
- rslt = set_accel_sig_motion_int(int_config, dev);
- break;
- case BMI160_ACC_SLOW_NO_MOTION_INT:
- /* Slow or no motion interrupt */
- rslt = set_accel_no_motion_int(int_config, dev);
- break;
- case BMI160_ACC_DOUBLE_TAP_INT:
- case BMI160_ACC_SINGLE_TAP_INT:
- /* Double tap and single tap Interrupt */
- rslt = set_accel_tap_int(int_config, dev);
- break;
- case BMI160_STEP_DETECT_INT:
- /* Step detector interrupt */
- rslt = set_accel_step_detect_int(int_config, dev);
- break;
- case BMI160_ACC_ORIENT_INT:
- /* Orientation interrupt */
- rslt = set_accel_orientation_int(int_config, dev);
- break;
- case BMI160_ACC_FLAT_INT:
- /* Flat detection interrupt */
- rslt = set_accel_flat_detect_int(int_config, dev);
- break;
- case BMI160_ACC_LOW_G_INT:
- /* Low-g interrupt */
- rslt = set_accel_low_g_int(int_config, dev);
- break;
- case BMI160_ACC_HIGH_G_INT:
- /* High-g interrupt */
- rslt = set_accel_high_g_int(int_config, dev);
- break;
- case BMI160_ACC_GYRO_DATA_RDY_INT:
- /* Data ready interrupt */
- rslt = set_accel_gyro_data_ready_int(int_config, dev);
- break;
- case BMI160_ACC_GYRO_FIFO_FULL_INT:
- /* Fifo full interrupt */
- rslt = set_fifo_full_int(int_config, dev);
- break;
- case BMI160_ACC_GYRO_FIFO_WATERMARK_INT:
- /* Fifo water-mark interrupt */
- rslt = set_fifo_watermark_int(int_config, dev);
- break;
- case BMI160_FIFO_TAG_INT_PIN:
- /* Fifo tagging feature support */
- /* Configure Interrupt pins */
- rslt = set_intr_pin_config(int_config, dev);
- break;
- default:
- break;
- }
- return rslt;
- }
- /*!
- * @brief This API enables or disable the step counter feature.
- * 1 - enable step counter (0 - disable)
- */
- int8_t bmi160_set_step_counter(uint8_t step_cnt_enable, const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data = 0;
- /* Null-pointer check */
- rslt = null_ptr_check(dev);
- if (rslt != BMI160_OK)
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- rslt = bmi160_get_regs(BMI160_INT_STEP_CONFIG_1_ADDR, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- if (step_cnt_enable == BMI160_ENABLE)
- {
- data |= (uint8_t)(step_cnt_enable << 3);
- }
- else
- {
- data &= ~BMI160_STEP_COUNT_EN_BIT_MASK;
- }
- rslt = bmi160_set_regs(BMI160_INT_STEP_CONFIG_1_ADDR, &data, 1, dev);
- }
- }
- return rslt;
- }
- /*!
- * @brief This API reads the step counter value.
- */
- int8_t bmi160_read_step_counter(uint16_t *step_val, const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data[2] = { 0, 0 };
- uint16_t msb = 0;
- uint8_t lsb = 0;
- /* Null-pointer check */
- rslt = null_ptr_check(dev);
- if (rslt != BMI160_OK)
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- rslt = bmi160_get_regs(BMI160_INT_STEP_CNT_0_ADDR, data, 2, dev);
- if (rslt == BMI160_OK)
- {
- lsb = data[0];
- msb = data[1] << 8;
- *step_val = msb | lsb;
- }
- }
- return rslt;
- }
- /*!
- * @brief This API reads the mention no of byte of data from the given
- * register address of auxiliary sensor.
- */
- int8_t bmi160_aux_read(uint8_t reg_addr, uint8_t *aux_data, uint16_t len, const struct bmi160_dev *dev)
- {
- int8_t rslt = BMI160_OK;
- uint16_t map_len = 0;
- /* Null-pointer check */
- if ((dev == NULL) || (dev->read == NULL))
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- if (dev->aux_cfg.aux_sensor_enable == BMI160_ENABLE)
- {
- rslt = map_read_len(&map_len, dev);
- if (rslt == BMI160_OK)
- {
- rslt = extract_aux_read(map_len, reg_addr, aux_data, len, dev);
- }
- }
- else
- {
- rslt = BMI160_E_INVALID_INPUT;
- }
- }
- return rslt;
- }
- /*!
- * @brief This API writes the mention no of byte of data to the given
- * register address of auxiliary sensor.
- */
- int8_t bmi160_aux_write(uint8_t reg_addr, uint8_t *aux_data, uint16_t len, const struct bmi160_dev *dev)
- {
- int8_t rslt = BMI160_OK;
- uint8_t count = 0;
- /* Null-pointer check */
- if ((dev == NULL) || (dev->write == NULL))
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- for (; count < len; count++)
- {
- /* set data to write */
- rslt = bmi160_set_regs(BMI160_AUX_IF_4_ADDR, aux_data, 1, dev);
- dev->delay_ms(BMI160_AUX_COM_DELAY);
- if (rslt == BMI160_OK)
- {
- /* set address to write */
- rslt = bmi160_set_regs(BMI160_AUX_IF_3_ADDR, ®_addr, 1, dev);
- dev->delay_ms(BMI160_AUX_COM_DELAY);
- if (rslt == BMI160_OK && (count < len - 1))
- {
- aux_data++;
- reg_addr++;
- }
- }
- }
- }
- return rslt;
- }
- /*!
- * @brief This API initialize the auxiliary sensor
- * in order to access it.
- */
- int8_t bmi160_aux_init(const struct bmi160_dev *dev)
- {
- int8_t rslt;
- /* Null-pointer check */
- rslt = null_ptr_check(dev);
- if (rslt != BMI160_OK)
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- if (dev->aux_cfg.aux_sensor_enable == BMI160_ENABLE)
- {
- /* Configures the auxiliary sensor interface settings */
- rslt = config_aux_settg(dev);
- }
- else
- {
- rslt = BMI160_E_INVALID_INPUT;
- }
- }
- return rslt;
- }
- /*!
- * @brief This API is used to setup the auxiliary sensor of bmi160 in auto mode
- * Thus enabling the auto update of 8 bytes of data from auxiliary sensor
- * to BMI160 register address 0x04 to 0x0B
- */
- int8_t bmi160_set_aux_auto_mode(uint8_t *data_addr, struct bmi160_dev *dev)
- {
- int8_t rslt;
- /* Null-pointer check */
- rslt = null_ptr_check(dev);
- if (rslt != BMI160_OK)
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- if (dev->aux_cfg.aux_sensor_enable == BMI160_ENABLE)
- {
- /* Write the aux. address to read in 0x4D of BMI160*/
- rslt = bmi160_set_regs(BMI160_AUX_IF_2_ADDR, data_addr, 1, dev);
- dev->delay_ms(BMI160_AUX_COM_DELAY);
- if (rslt == BMI160_OK)
- {
- /* Configure the polling ODR for
- * auxiliary sensor */
- rslt = config_aux_odr(dev);
- if (rslt == BMI160_OK)
- {
- /* Disable the aux. manual mode, i.e aux.
- * sensor is in auto-mode (data-mode) */
- dev->aux_cfg.manual_enable = BMI160_DISABLE;
- rslt = bmi160_config_aux_mode(dev);
- /* Auxiliary sensor data is obtained
- * in auto mode from this point */
- }
- }
- }
- else
- {
- rslt = BMI160_E_INVALID_INPUT;
- }
- }
- return rslt;
- }
- /*!
- * @brief This API configures the 0x4C register and settings like
- * Auxiliary sensor manual enable/ disable and aux burst read length.
- */
- int8_t bmi160_config_aux_mode(const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t aux_if[2] = { (uint8_t)(dev->aux_cfg.aux_i2c_addr * 2), 0 };
- rslt = bmi160_get_regs(BMI160_AUX_IF_1_ADDR, &aux_if[1], 1, dev);
- if (rslt == BMI160_OK)
- {
- /* update the Auxiliary interface to manual/auto mode */
- aux_if[1] = BMI160_SET_BITS(aux_if[1], BMI160_MANUAL_MODE_EN, dev->aux_cfg.manual_enable);
- /* update the burst read length defined by user */
- aux_if[1] = BMI160_SET_BITS_POS_0(aux_if[1], BMI160_AUX_READ_BURST, dev->aux_cfg.aux_rd_burst_len);
- /* Set the secondary interface address and manual mode
- * along with burst read length */
- rslt = bmi160_set_regs(BMI160_AUX_IF_0_ADDR, &aux_if[0], 2, dev);
- dev->delay_ms(BMI160_AUX_COM_DELAY);
- }
- return rslt;
- }
- /*!
- * @brief This API is used to read the raw uncompensated auxiliary sensor
- * data of 8 bytes from BMI160 register address 0x04 to 0x0B
- */
- int8_t bmi160_read_aux_data_auto_mode(uint8_t *aux_data, const struct bmi160_dev *dev)
- {
- int8_t rslt;
- /* Null-pointer check */
- rslt = null_ptr_check(dev);
- if (rslt != BMI160_OK)
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- if ((dev->aux_cfg.aux_sensor_enable == BMI160_ENABLE) && (dev->aux_cfg.manual_enable == BMI160_DISABLE))
- {
- /* Read the aux. sensor's raw data */
- rslt = bmi160_get_regs(BMI160_AUX_DATA_ADDR, aux_data, 8, dev);
- }
- else
- {
- rslt = BMI160_E_INVALID_INPUT;
- }
- }
- return rslt;
- }
- /*!
- * @brief This is used to perform self test of accel/gyro of the BMI160 sensor
- */
- int8_t bmi160_perform_self_test(uint8_t select_sensor, struct bmi160_dev *dev)
- {
- int8_t rslt;
- int8_t self_test_rslt = 0;
- /* Null-pointer check */
- rslt = null_ptr_check(dev);
- if (rslt != BMI160_OK)
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- /* Proceed if null check is fine */
- switch (select_sensor)
- {
- case BMI160_ACCEL_ONLY:
- rslt = perform_accel_self_test(dev);
- break;
- case BMI160_GYRO_ONLY:
- /* Set the power mode as normal mode */
- dev->gyro_cfg.power = BMI160_GYRO_NORMAL_MODE;
- rslt = bmi160_set_power_mode(dev);
- /* Perform gyro self test */
- if (rslt == BMI160_OK)
- {
- /* Perform gyro self test */
- rslt = perform_gyro_self_test(dev);
- }
- break;
- default:
- rslt = BMI160_E_INVALID_INPUT;
- break;
- }
- /* Check to ensure bus error does not occur */
- if (rslt >= BMI160_OK)
- {
- /* Store the status of self test result */
- self_test_rslt = rslt;
- /* Perform soft reset */
- rslt = bmi160_soft_reset(dev);
- }
- /* Check to ensure bus operations are success */
- if (rslt == BMI160_OK)
- {
- /* Restore self_test_rslt as return value */
- rslt = self_test_rslt;
- }
- }
- return rslt;
- }
- /*!
- * @brief This API reads the data from fifo buffer.
- */
- int8_t bmi160_get_fifo_data(struct bmi160_dev const *dev)
- {
- int8_t rslt = 0;
- uint16_t bytes_to_read = 0;
- uint16_t user_fifo_len = 0;
- /* check the bmi160 structure as NULL*/
- if ((dev == NULL) || (dev->fifo->data == NULL))
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- reset_fifo_data_structure(dev);
- /* get current FIFO fill-level*/
- rslt = get_fifo_byte_counter(&bytes_to_read, dev);
- if (rslt == BMI160_OK)
- {
- user_fifo_len = dev->fifo->length;
- if ((dev->fifo->length > bytes_to_read))
- {
- /* Handling the case where user requests
- * more data than available in FIFO */
- dev->fifo->length = bytes_to_read;
- }
- if ((dev->fifo->fifo_time_enable == BMI160_FIFO_TIME_ENABLE) &&
- (bytes_to_read + BMI160_FIFO_BYTES_OVERREAD <= user_fifo_len))
- {
- /* Handling case of sensor time availability*/
- dev->fifo->length = dev->fifo->length + BMI160_FIFO_BYTES_OVERREAD;
- }
- /* read only the filled bytes in the FIFO Buffer */
- rslt = bmi160_get_regs(BMI160_FIFO_DATA_ADDR, dev->fifo->data, dev->fifo->length, dev);
- }
- }
- return rslt;
- }
- /*!
- * @brief This API writes fifo_flush command to command register.This
- * action clears all data in the Fifo without changing fifo configuration
- * settings
- */
- int8_t bmi160_set_fifo_flush(const struct bmi160_dev *dev)
- {
- int8_t rslt = 0;
- uint8_t data = BMI160_FIFO_FLUSH_VALUE;
- uint8_t reg_addr = BMI160_COMMAND_REG_ADDR;
- /* Check the bmi160_dev structure for NULL address*/
- if (dev == NULL)
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- rslt = bmi160_set_regs(reg_addr, &data, BMI160_ONE, dev);
- }
- return rslt;
- }
- /*!
- * @brief This API sets the FIFO configuration in the sensor.
- */
- int8_t bmi160_set_fifo_config(uint8_t config, uint8_t enable, struct bmi160_dev const *dev)
- {
- int8_t rslt = 0;
- uint8_t data = 0;
- uint8_t reg_addr = BMI160_FIFO_CONFIG_1_ADDR;
- uint8_t fifo_config = config & BMI160_FIFO_CONFIG_1_MASK;
- /* Check the bmi160_dev structure for NULL address*/
- if (dev == NULL)
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- rslt = bmi160_get_regs(reg_addr, &data, BMI160_ONE, dev);
- if (rslt == BMI160_OK)
- {
- if (fifo_config > 0)
- {
- if (enable == BMI160_ENABLE)
- {
- data = data | fifo_config;
- }
- else
- {
- data = data & (~fifo_config);
- }
- }
- /* write fifo frame content configuration*/
- rslt = bmi160_set_regs(reg_addr, &data, BMI160_ONE, dev);
- if (rslt == BMI160_OK)
- {
- /* read fifo frame content configuration*/
- rslt = bmi160_get_regs(reg_addr, &data, BMI160_ONE, dev);
- if (rslt == BMI160_OK)
- {
- /* extract fifo header enabled status */
- dev->fifo->fifo_header_enable = data & BMI160_FIFO_HEAD_ENABLE;
- /* extract accel/gyr/aux. data enabled status */
- dev->fifo->fifo_data_enable = data & BMI160_FIFO_M_G_A_ENABLE;
- /* extract fifo sensor time enabled status */
- dev->fifo->fifo_time_enable = data & BMI160_FIFO_TIME_ENABLE;
- }
- }
- }
- }
- return rslt;
- }
- /*! @brief This API is used to configure the down sampling ratios of
- * the accel and gyro data for FIFO.Also, it configures filtered or
- * pre-filtered data for accel and gyro.
- *
- */
- int8_t bmi160_set_fifo_down(uint8_t fifo_down, const struct bmi160_dev *dev)
- {
- int8_t rslt = 0;
- uint8_t data = 0;
- uint8_t reg_addr = BMI160_FIFO_DOWN_ADDR;
- /* Check the bmi160_dev structure for NULL address*/
- if (dev == NULL)
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- rslt = bmi160_get_regs(reg_addr, &data, BMI160_ONE, dev);
- if (rslt == BMI160_OK)
- {
- data = data | fifo_down;
- rslt = bmi160_set_regs(reg_addr, &data, BMI160_ONE, dev);
- }
- }
- return rslt;
- }
- /*!
- * @brief This API sets the FIFO watermark level in the sensor.
- *
- */
- int8_t bmi160_set_fifo_wm(uint8_t fifo_wm, const struct bmi160_dev *dev)
- {
- int8_t rslt = 0;
- uint8_t data = fifo_wm;
- uint8_t reg_addr = BMI160_FIFO_CONFIG_0_ADDR;
- /* Check the bmi160_dev structure for NULL address*/
- if (dev == NULL)
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- rslt = bmi160_set_regs(reg_addr, &data, BMI160_ONE, dev);
- }
- return rslt;
- }
- /*!
- * @brief This API parses and extracts the accelerometer frames from
- * FIFO data read by the "bmi160_get_fifo_data" API and stores it in
- * the "accel_data" structure instance.
- */
- int8_t bmi160_extract_accel(struct bmi160_sensor_data *accel_data, uint8_t *accel_length, struct bmi160_dev const *dev)
- {
- int8_t rslt = 0;
- uint16_t data_index = 0;
- uint16_t data_read_length = 0;
- uint8_t accel_index = 0;
- uint8_t fifo_data_enable = 0;
- if (dev == NULL || dev->fifo == NULL || dev->fifo->data == NULL)
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- /* Parsing the FIFO data in header-less mode */
- if (dev->fifo->fifo_header_enable == 0)
- {
- /* Number of bytes to be parsed from FIFO */
- get_accel_len_to_parse(&data_index, &data_read_length, accel_length, dev);
- for (; data_index < data_read_length;)
- {
- /*Check for the availability of next two bytes of FIFO data */
- check_frame_validity(&data_index, dev);
- fifo_data_enable = dev->fifo->fifo_data_enable;
- unpack_accel_frame(accel_data, &data_index, &accel_index, fifo_data_enable, dev);
- }
- /* update number of accel data read*/
- *accel_length = accel_index;
- /*update the accel byte index*/
- dev->fifo->accel_byte_start_idx = data_index;
- }
- else
- {
- /* Parsing the FIFO data in header mode */
- extract_accel_header_mode(accel_data, accel_length, dev);
- }
- }
- return rslt;
- }
- /*!
- * @brief This API parses and extracts the gyro frames from
- * FIFO data read by the "bmi160_get_fifo_data" API and stores it in
- * the "gyro_data" structure instance.
- */
- int8_t bmi160_extract_gyro(struct bmi160_sensor_data *gyro_data, uint8_t *gyro_length, struct bmi160_dev const *dev)
- {
- int8_t rslt = 0;
- uint16_t data_index = 0;
- uint16_t data_read_length = 0;
- uint8_t gyro_index = 0;
- uint8_t fifo_data_enable = 0;
- if (dev == NULL || dev->fifo->data == NULL)
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- /* Parsing the FIFO data in header-less mode */
- if (dev->fifo->fifo_header_enable == 0)
- {
- /* Number of bytes to be parsed from FIFO */
- get_gyro_len_to_parse(&data_index, &data_read_length, gyro_length, dev);
- for (; data_index < data_read_length;)
- {
- /*Check for the availability of next two bytes of FIFO data */
- check_frame_validity(&data_index, dev);
- fifo_data_enable = dev->fifo->fifo_data_enable;
- unpack_gyro_frame(gyro_data, &data_index, &gyro_index, fifo_data_enable, dev);
- }
- /* update number of gyro data read */
- *gyro_length = gyro_index;
- /* update the gyro byte index */
- dev->fifo->gyro_byte_start_idx = data_index;
- }
- else
- {
- /* Parsing the FIFO data in header mode */
- extract_gyro_header_mode(gyro_data, gyro_length, dev);
- }
- }
- return rslt;
- }
- /*!
- * @brief This API parses and extracts the aux frames from
- * FIFO data read by the "bmi160_get_fifo_data" API and stores it in
- * the "aux_data" structure instance.
- */
- int8_t bmi160_extract_aux(struct bmi160_aux_data *aux_data, uint8_t *aux_len, struct bmi160_dev const *dev)
- {
- int8_t rslt = 0;
- uint16_t data_index = 0;
- uint16_t data_read_length = 0;
- uint8_t aux_index = 0;
- uint8_t fifo_data_enable = 0;
- if ((dev == NULL) || (dev->fifo->data == NULL) || (aux_data == NULL))
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- /* Parsing the FIFO data in header-less mode */
- if (dev->fifo->fifo_header_enable == 0)
- {
- /* Number of bytes to be parsed from FIFO */
- get_aux_len_to_parse(&data_index, &data_read_length, aux_len, dev);
- for (; data_index < data_read_length;)
- {
- /* Check for the availability of next two
- * bytes of FIFO data */
- check_frame_validity(&data_index, dev);
- fifo_data_enable = dev->fifo->fifo_data_enable;
- unpack_aux_frame(aux_data, &data_index, &aux_index, fifo_data_enable, dev);
- }
- /* update number of aux data read */
- *aux_len = aux_index;
- /* update the aux byte index */
- dev->fifo->aux_byte_start_idx = data_index;
- }
- else
- {
- /* Parsing the FIFO data in header mode */
- extract_aux_header_mode(aux_data, aux_len, dev);
- }
- }
- return rslt;
- }
- /*!
- * @brief This API starts the FOC of accel and gyro
- *
- * @note FOC should not be used in low-power mode of sensor
- *
- * @note Accel FOC targets values of +1g , 0g , -1g
- * Gyro FOC always targets value of 0 dps
- */
- int8_t bmi160_start_foc(const struct bmi160_foc_conf *foc_conf,
- struct bmi160_offsets *offset,
- struct bmi160_dev const *dev)
- {
- int8_t rslt;
- uint8_t data;
- /* Null-pointer check */
- rslt = null_ptr_check(dev);
- if (rslt != BMI160_OK)
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- /* Set the offset enable bits */
- rslt = configure_offset_enable(foc_conf, dev);
- if (rslt == BMI160_OK)
- {
- /* Read the FOC config from the sensor */
- rslt = bmi160_get_regs(BMI160_FOC_CONF_ADDR, &data, 1, dev);
- /* Set the FOC config for gyro */
- data = BMI160_SET_BITS(data, BMI160_GYRO_FOC_EN, foc_conf->foc_gyr_en);
- /* Set the FOC config for accel xyz axes */
- data = BMI160_SET_BITS(data, BMI160_ACCEL_FOC_X_CONF, foc_conf->foc_acc_x);
- data = BMI160_SET_BITS(data, BMI160_ACCEL_FOC_Y_CONF, foc_conf->foc_acc_y);
- data = BMI160_SET_BITS_POS_0(data, BMI160_ACCEL_FOC_Z_CONF, foc_conf->foc_acc_z);
- if (rslt == BMI160_OK)
- {
- /* Set the FOC config in the sensor */
- rslt = bmi160_set_regs(BMI160_FOC_CONF_ADDR, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- /* Procedure to trigger
- * FOC and check status */
- rslt = trigger_foc(offset, dev);
- }
- }
- }
- }
- return rslt;
- }
- /*!
- * @brief This API reads and stores the offset values of accel and gyro
- */
- int8_t bmi160_get_offsets(struct bmi160_offsets *offset, const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data[7];
- uint8_t lsb, msb;
- int16_t offset_msb, offset_lsb;
- int16_t offset_data;
- /* Null-pointer check */
- rslt = null_ptr_check(dev);
- if (rslt != BMI160_OK)
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- /* Read the FOC config from the sensor */
- rslt = bmi160_get_regs(BMI160_OFFSET_ADDR, data, 7, dev);
- /* Accel offsets */
- offset->off_acc_x = (int8_t)data[0];
- offset->off_acc_y = (int8_t)data[1];
- offset->off_acc_z = (int8_t)data[2];
- /* Gyro x-axis offset */
- lsb = data[3];
- msb = BMI160_GET_BITS_POS_0(data[6], BMI160_GYRO_OFFSET_X);
- offset_msb = (int16_t)(msb << 14);
- offset_lsb = lsb << 6;
- offset_data = offset_msb | offset_lsb;
- /* Divide by 64 to get the Right shift by 6 value */
- offset->off_gyro_x = (int16_t)(offset_data / 64);
- /* Gyro y-axis offset */
- lsb = data[4];
- msb = BMI160_GET_BITS(data[6], BMI160_GYRO_OFFSET_Y);
- offset_msb = (int16_t)(msb << 14);
- offset_lsb = lsb << 6;
- offset_data = offset_msb | offset_lsb;
- /* Divide by 64 to get the Right shift by 6 value */
- offset->off_gyro_y = (int16_t)(offset_data / 64);
- /* Gyro z-axis offset */
- lsb = data[5];
- msb = BMI160_GET_BITS(data[6], BMI160_GYRO_OFFSET_Z);
- offset_msb = (int16_t)(msb << 14);
- offset_lsb = lsb << 6;
- offset_data = offset_msb | offset_lsb;
- /* Divide by 64 to get the Right shift by 6 value */
- offset->off_gyro_z = (int16_t)(offset_data / 64);
- }
- return rslt;
- }
- /*!
- * @brief This API writes the offset values of accel and gyro to
- * the sensor but these values will be reset on POR or soft reset.
- */
- int8_t bmi160_set_offsets(const struct bmi160_foc_conf *foc_conf,
- const struct bmi160_offsets *offset,
- struct bmi160_dev const *dev)
- {
- int8_t rslt;
- uint8_t data[7];
- uint8_t x_msb, y_msb, z_msb;
- /* Null-pointer check */
- rslt = null_ptr_check(dev);
- if (rslt != BMI160_OK)
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- /* Update the accel offset */
- data[0] = (uint8_t)offset->off_acc_x;
- data[1] = (uint8_t)offset->off_acc_y;
- data[2] = (uint8_t)offset->off_acc_z;
- /* Update the LSB of gyro offset */
- data[3] = BMI160_GET_LSB(offset->off_gyro_x);
- data[4] = BMI160_GET_LSB(offset->off_gyro_y);
- data[5] = BMI160_GET_LSB(offset->off_gyro_z);
- /* Update the MSB of gyro offset */
- x_msb = BMI160_GET_BITS(offset->off_gyro_x, BMI160_GYRO_OFFSET);
- y_msb = BMI160_GET_BITS(offset->off_gyro_y, BMI160_GYRO_OFFSET);
- z_msb = BMI160_GET_BITS(offset->off_gyro_z, BMI160_GYRO_OFFSET);
- data[6] = (uint8_t)(z_msb << 4 | y_msb << 2 | x_msb);
- /* Set the offset enable/disable for gyro and accel */
- data[6] = BMI160_SET_BITS(data[6], BMI160_GYRO_OFFSET_EN, foc_conf->gyro_off_en);
- data[6] = BMI160_SET_BITS(data[6], BMI160_ACCEL_OFFSET_EN, foc_conf->acc_off_en);
- /* Set the offset config and values in the sensor */
- rslt = bmi160_set_regs(BMI160_OFFSET_ADDR, data, 7, dev);
- }
- return rslt;
- }
- /*!
- * @brief This API writes the image registers values to NVM which is
- * stored even after POR or soft reset
- */
- int8_t bmi160_update_nvm(struct bmi160_dev const *dev)
- {
- int8_t rslt;
- uint8_t data;
- uint8_t cmd = BMI160_NVM_BACKUP_EN;
- /* Read the nvm_prog_en configuration */
- rslt = bmi160_get_regs(BMI160_CONF_ADDR, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- data = BMI160_SET_BITS(data, BMI160_NVM_UPDATE, 1);
- /* Set the nvm_prog_en bit in the sensor */
- rslt = bmi160_set_regs(BMI160_CONF_ADDR, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- /* Update NVM */
- rslt = bmi160_set_regs(BMI160_COMMAND_REG_ADDR, &cmd, 1, dev);
- if (rslt == BMI160_OK)
- {
- /* Check for NVM ready status */
- rslt = bmi160_get_regs(BMI160_STATUS_ADDR, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- data = BMI160_GET_BITS(data, BMI160_NVM_STATUS);
- if (data != BMI160_ENABLE)
- {
- /* Delay to update NVM */
- dev->delay_ms(25);
- }
- }
- }
- }
- }
- return rslt;
- }
- /*!
- * @brief This API gets the interrupt status from the sensor.
- */
- int8_t bmi160_get_int_status(enum bmi160_int_status_sel int_status_sel,
- union bmi160_int_status *int_status,
- struct bmi160_dev const *dev)
- {
- int8_t rslt = 0;
- /* To get the status of all interrupts */
- if (int_status_sel == BMI160_INT_STATUS_ALL)
- {
- rslt = bmi160_get_regs(BMI160_INT_STATUS_ADDR, &int_status->data[0], 4, dev);
- }
- else
- {
- if (int_status_sel & BMI160_INT_STATUS_0)
- {
- rslt = bmi160_get_regs(BMI160_INT_STATUS_ADDR, &int_status->data[0], 1, dev);
- }
- if (int_status_sel & BMI160_INT_STATUS_1)
- {
- rslt = bmi160_get_regs(BMI160_INT_STATUS_ADDR + 1, &int_status->data[1], 1, dev);
- }
- if (int_status_sel & BMI160_INT_STATUS_2)
- {
- rslt = bmi160_get_regs(BMI160_INT_STATUS_ADDR + 2, &int_status->data[2], 1, dev);
- }
- if (int_status_sel & BMI160_INT_STATUS_3)
- {
- rslt = bmi160_get_regs(BMI160_INT_STATUS_ADDR + 3, &int_status->data[3], 1, dev);
- }
- }
- return rslt;
- }
- /*********************** Local function definitions ***************************/
- /*!
- * @brief This API sets the any-motion interrupt of the sensor.
- * This interrupt occurs when accel values exceeds preset threshold
- * for a certain period of time.
- */
- static int8_t set_accel_any_motion_int(struct bmi160_int_settg *int_config, struct bmi160_dev *dev)
- {
- int8_t rslt;
- /* Null-pointer check */
- rslt = null_ptr_check(dev);
- if ((rslt != BMI160_OK) || (int_config == NULL))
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- /* updating the interrupt structure to local structure */
- struct bmi160_acc_any_mot_int_cfg *any_motion_int_cfg = &(int_config->int_type_cfg.acc_any_motion_int);
- rslt = enable_accel_any_motion_int(any_motion_int_cfg, dev);
- if (rslt == BMI160_OK)
- {
- rslt = config_any_motion_int_settg(int_config, any_motion_int_cfg, dev);
- }
- }
- return rslt;
- }
- /*!
- * @brief This API sets tap interrupts.Interrupt is fired when
- * tap movements happen.
- */
- static int8_t set_accel_tap_int(struct bmi160_int_settg *int_config, const struct bmi160_dev *dev)
- {
- int8_t rslt;
- /* Null-pointer check */
- rslt = null_ptr_check(dev);
- if ((rslt != BMI160_OK) || (int_config == NULL))
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- /* updating the interrupt structure to local structure */
- struct bmi160_acc_tap_int_cfg *tap_int_cfg = &(int_config->int_type_cfg.acc_tap_int);
- rslt = enable_tap_int(int_config, tap_int_cfg, dev);
- if (rslt == BMI160_OK)
- {
- /* Configure Interrupt pins */
- rslt = set_intr_pin_config(int_config, dev);
- if (rslt == BMI160_OK)
- {
- rslt = config_tap_int_settg(int_config, tap_int_cfg, dev);
- }
- }
- }
- return rslt;
- }
- /*!
- * @brief This API sets the data ready interrupt for both accel and gyro.
- * This interrupt occurs when new accel and gyro data comes.
- */
- static int8_t set_accel_gyro_data_ready_int(const struct bmi160_int_settg *int_config, const struct bmi160_dev *dev)
- {
- int8_t rslt;
- /* Null-pointer check */
- rslt = null_ptr_check(dev);
- if ((rslt != BMI160_OK) || (int_config == NULL))
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- rslt = enable_data_ready_int(dev);
- if (rslt == BMI160_OK)
- {
- /* Configure Interrupt pins */
- rslt = set_intr_pin_config(int_config, dev);
- if (rslt == BMI160_OK)
- {
- rslt = map_hardware_interrupt(int_config, dev);
- }
- }
- }
- return rslt;
- }
- /*!
- * @brief This API sets the significant motion interrupt of the sensor.This
- * interrupt occurs when there is change in user location.
- */
- static int8_t set_accel_sig_motion_int(struct bmi160_int_settg *int_config, struct bmi160_dev *dev)
- {
- int8_t rslt;
- /* Null-pointer check */
- rslt = null_ptr_check(dev);
- if ((rslt != BMI160_OK) || (int_config == NULL))
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- /* updating the interrupt structure to local structure */
- struct bmi160_acc_sig_mot_int_cfg *sig_mot_int_cfg = &(int_config->int_type_cfg.acc_sig_motion_int);
- rslt = enable_sig_motion_int(sig_mot_int_cfg, dev);
- if (rslt == BMI160_OK)
- {
- rslt = config_sig_motion_int_settg(int_config, sig_mot_int_cfg, dev);
- }
- }
- return rslt;
- }
- /*!
- * @brief This API sets the no motion/slow motion interrupt of the sensor.
- * Slow motion is similar to any motion interrupt.No motion interrupt
- * occurs when slope bet. two accel values falls below preset threshold
- * for preset duration.
- */
- static int8_t set_accel_no_motion_int(struct bmi160_int_settg *int_config, const struct bmi160_dev *dev)
- {
- int8_t rslt;
- /* Null-pointer check */
- rslt = null_ptr_check(dev);
- if ((rslt != BMI160_OK) || (int_config == NULL))
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- /* updating the interrupt structure to local structure */
- struct bmi160_acc_no_motion_int_cfg *no_mot_int_cfg = &(int_config->int_type_cfg.acc_no_motion_int);
- rslt = enable_no_motion_int(no_mot_int_cfg, dev);
- if (rslt == BMI160_OK)
- {
- /* Configure the INT PIN settings*/
- rslt = config_no_motion_int_settg(int_config, no_mot_int_cfg, dev);
- }
- }
- return rslt;
- }
- /*!
- * @brief This API sets the step detection interrupt.This interrupt
- * occurs when the single step causes accel values to go above
- * preset threshold.
- */
- static int8_t set_accel_step_detect_int(struct bmi160_int_settg *int_config, const struct bmi160_dev *dev)
- {
- int8_t rslt;
- /* Null-pointer check */
- rslt = null_ptr_check(dev);
- if ((rslt != BMI160_OK) || (int_config == NULL))
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- /* updating the interrupt structure to local structure */
- struct bmi160_acc_step_detect_int_cfg *step_detect_int_cfg = &(int_config->int_type_cfg.acc_step_detect_int);
- rslt = enable_step_detect_int(step_detect_int_cfg, dev);
- if (rslt == BMI160_OK)
- {
- /* Configure Interrupt pins */
- rslt = set_intr_pin_config(int_config, dev);
- if (rslt == BMI160_OK)
- {
- rslt = map_feature_interrupt(int_config, dev);
- if (rslt == BMI160_OK)
- {
- rslt = config_step_detect(step_detect_int_cfg, dev);
- }
- }
- }
- }
- return rslt;
- }
- /*!
- * @brief This API sets the orientation interrupt of the sensor.This
- * interrupt occurs when there is orientation change in the sensor
- * with respect to gravitational field vector g.
- */
- static int8_t set_accel_orientation_int(struct bmi160_int_settg *int_config, const struct bmi160_dev *dev)
- {
- int8_t rslt;
- /* Null-pointer check */
- rslt = null_ptr_check(dev);
- if ((rslt != BMI160_OK) || (int_config == NULL))
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- /* updating the interrupt structure to local structure */
- struct bmi160_acc_orient_int_cfg *orient_int_cfg = &(int_config->int_type_cfg.acc_orient_int);
- rslt = enable_orient_int(orient_int_cfg, dev);
- if (rslt == BMI160_OK)
- {
- /* Configure Interrupt pins */
- rslt = set_intr_pin_config(int_config, dev);
- if (rslt == BMI160_OK)
- {
- /* map INT pin to orient interrupt */
- rslt = map_feature_interrupt(int_config, dev);
- if (rslt == BMI160_OK)
- {
- /* configure the
- * orientation setting*/
- rslt = config_orient_int_settg(orient_int_cfg, dev);
- }
- }
- }
- }
- return rslt;
- }
- /*!
- * @brief This API sets the flat interrupt of the sensor.This interrupt
- * occurs in case of flat orientation
- */
- static int8_t set_accel_flat_detect_int(struct bmi160_int_settg *int_config, const struct bmi160_dev *dev)
- {
- int8_t rslt;
- /* Null-pointer check */
- rslt = null_ptr_check(dev);
- if ((rslt != BMI160_OK) || (int_config == NULL))
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- /* updating the interrupt structure to local structure */
- struct bmi160_acc_flat_detect_int_cfg *flat_detect_int = &(int_config->int_type_cfg.acc_flat_int);
- /* enable the flat interrupt */
- rslt = enable_flat_int(flat_detect_int, dev);
- if (rslt == BMI160_OK)
- {
- /* Configure Interrupt pins */
- rslt = set_intr_pin_config(int_config, dev);
- if (rslt == BMI160_OK)
- {
- /* map INT pin to flat interrupt */
- rslt = map_feature_interrupt(int_config, dev);
- if (rslt == BMI160_OK)
- {
- /* configure the flat setting*/
- rslt = config_flat_int_settg(flat_detect_int, dev);
- }
- }
- }
- }
- return rslt;
- }
- /*!
- * @brief This API sets the low-g interrupt of the sensor.This interrupt
- * occurs during free-fall.
- */
- static int8_t set_accel_low_g_int(struct bmi160_int_settg *int_config, const struct bmi160_dev *dev)
- {
- int8_t rslt;
- /* Null-pointer check */
- rslt = null_ptr_check(dev);
- if ((rslt != BMI160_OK) || (int_config == NULL))
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- /* updating the interrupt structure to local structure */
- struct bmi160_acc_low_g_int_cfg *low_g_int = &(int_config->int_type_cfg.acc_low_g_int);
- /* Enable the low-g interrupt*/
- rslt = enable_low_g_int(low_g_int, dev);
- if (rslt == BMI160_OK)
- {
- /* Configure Interrupt pins */
- rslt = set_intr_pin_config(int_config, dev);
- if (rslt == BMI160_OK)
- {
- /* Map INT pin to low-g interrupt */
- rslt = map_feature_interrupt(int_config, dev);
- if (rslt == BMI160_OK)
- {
- /* configure the data source
- * for low-g interrupt*/
- rslt = config_low_g_data_src(low_g_int, dev);
- if (rslt == BMI160_OK)
- {
- rslt = config_low_g_int_settg(low_g_int, dev);
- }
- }
- }
- }
- }
- return rslt;
- }
- /*!
- * @brief This API sets the high-g interrupt of the sensor.The interrupt
- * occurs if the absolute value of acceleration data of any enabled axis
- * exceeds the programmed threshold and the sign of the value does not
- * change for a preset duration.
- */
- static int8_t set_accel_high_g_int(struct bmi160_int_settg *int_config, const struct bmi160_dev *dev)
- {
- int8_t rslt;
- /* Null-pointer check */
- rslt = null_ptr_check(dev);
- if ((rslt != BMI160_OK) || (int_config == NULL))
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- /* updating the interrupt structure to local structure */
- struct bmi160_acc_high_g_int_cfg *high_g_int_cfg = &(int_config->int_type_cfg.acc_high_g_int);
- /* Enable the high-g interrupt */
- rslt = enable_high_g_int(high_g_int_cfg, dev);
- if (rslt == BMI160_OK)
- {
- /* Configure Interrupt pins */
- rslt = set_intr_pin_config(int_config, dev);
- if (rslt == BMI160_OK)
- {
- /* Map INT pin to high-g interrupt */
- rslt = map_feature_interrupt(int_config, dev);
- if (rslt == BMI160_OK)
- {
- /* configure the data source
- * for high-g interrupt*/
- rslt = config_high_g_data_src(high_g_int_cfg, dev);
- if (rslt == BMI160_OK)
- {
- rslt = config_high_g_int_settg(high_g_int_cfg, dev);
- }
- }
- }
- }
- }
- return rslt;
- }
- /*!
- * @brief This API configures the pins to fire the
- * interrupt signal when it occurs.
- */
- static int8_t set_intr_pin_config(const struct bmi160_int_settg *int_config, const struct bmi160_dev *dev)
- {
- int8_t rslt;
- /* configure the behavioural settings of interrupt pin */
- rslt = config_int_out_ctrl(int_config, dev);
- if (rslt == BMI160_OK)
- {
- rslt = config_int_latch(int_config, dev);
- }
- return rslt;
- }
- /*!
- * @brief This internal API is used to validate the device structure pointer for
- * null conditions.
- */
- static int8_t null_ptr_check(const struct bmi160_dev *dev)
- {
- int8_t rslt;
- if ((dev == NULL) || (dev->read == NULL) || (dev->write == NULL) || (dev->delay_ms == NULL))
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- /* Device structure is fine */
- rslt = BMI160_OK;
- }
- return rslt;
- }
- /*!
- * @brief This API sets the default configuration parameters of accel & gyro.
- * Also maintain the previous state of configurations.
- */
- static void default_param_settg(struct bmi160_dev *dev)
- {
- /* Initializing accel and gyro params with
- * default values */
- dev->accel_cfg.bw = BMI160_ACCEL_BW_NORMAL_AVG4;
- dev->accel_cfg.odr = BMI160_ACCEL_ODR_100HZ;
- dev->accel_cfg.power = BMI160_ACCEL_SUSPEND_MODE;
- dev->accel_cfg.range = BMI160_ACCEL_RANGE_2G;
- dev->gyro_cfg.bw = BMI160_GYRO_BW_NORMAL_MODE;
- dev->gyro_cfg.odr = BMI160_GYRO_ODR_100HZ;
- dev->gyro_cfg.power = BMI160_GYRO_SUSPEND_MODE;
- dev->gyro_cfg.range = BMI160_GYRO_RANGE_2000_DPS;
- /* To maintain the previous state of accel configuration */
- dev->prev_accel_cfg = dev->accel_cfg;
- /* To maintain the previous state of gyro configuration */
- dev->prev_gyro_cfg = dev->gyro_cfg;
- }
- /*!
- * @brief This API set the accel configuration.
- */
- static int8_t set_accel_conf(struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data[2] = { 0 };
- rslt = check_accel_config(data, dev);
- if (rslt == BMI160_OK)
- {
- /* Write output data rate and bandwidth */
- rslt = bmi160_set_regs(BMI160_ACCEL_CONFIG_ADDR, &data[0], 1, dev);
- if (rslt == BMI160_OK)
- {
- dev->prev_accel_cfg.odr = dev->accel_cfg.odr;
- dev->prev_accel_cfg.bw = dev->accel_cfg.bw;
- /* write accel range */
- rslt = bmi160_set_regs(BMI160_ACCEL_RANGE_ADDR, &data[1], 1, dev);
- if (rslt == BMI160_OK)
- {
- dev->prev_accel_cfg.range = dev->accel_cfg.range;
- }
- }
- }
- return rslt;
- }
- /*!
- * @brief This API gets the accel configuration.
- */
- static int8_t get_accel_conf(struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data[2] = { 0 };
- /* Get accel configurations */
- rslt = bmi160_get_regs(BMI160_ACCEL_CONFIG_ADDR, data, 2, dev);
- if (rslt == BMI160_OK)
- {
- dev->accel_cfg.odr = (data[0] & BMI160_ACCEL_ODR_MASK);
- dev->accel_cfg.bw = (data[0] & BMI160_ACCEL_BW_MASK) >> BMI160_ACCEL_BW_POS;
- dev->accel_cfg.range = (data[1] & BMI160_ACCEL_RANGE_MASK);
- }
- return rslt;
- }
- /*!
- * @brief This API check the accel configuration.
- */
- static int8_t check_accel_config(uint8_t *data, const struct bmi160_dev *dev)
- {
- int8_t rslt;
- /* read accel Output data rate and bandwidth */
- rslt = bmi160_get_regs(BMI160_ACCEL_CONFIG_ADDR, data, 2, dev);
- if (rslt == BMI160_OK)
- {
- rslt = process_accel_odr(&data[0], dev);
- if (rslt == BMI160_OK)
- {
- rslt = process_accel_bw(&data[0], dev);
- if (rslt == BMI160_OK)
- {
- rslt = process_accel_range(&data[1], dev);
- }
- }
- }
- return rslt;
- }
- /*!
- * @brief This API process the accel odr.
- */
- static int8_t process_accel_odr(uint8_t *data, const struct bmi160_dev *dev)
- {
- int8_t rslt = 0;
- uint8_t temp = 0;
- uint8_t odr = 0;
- if (dev->accel_cfg.odr <= BMI160_ACCEL_ODR_1600HZ)
- {
- if (dev->accel_cfg.odr != dev->prev_accel_cfg.odr)
- {
- odr = (uint8_t)dev->accel_cfg.odr;
- temp = *data & ~BMI160_ACCEL_ODR_MASK;
- /* Adding output data rate */
- *data = temp | (odr & BMI160_ACCEL_ODR_MASK);
- }
- }
- else
- {
- rslt = BMI160_E_OUT_OF_RANGE;
- }
- return rslt;
- }
- /*!
- * @brief This API process the accel bandwidth.
- */
- static int8_t process_accel_bw(uint8_t *data, const struct bmi160_dev *dev)
- {
- int8_t rslt = 0;
- uint8_t temp = 0;
- uint8_t bw = 0;
- if (dev->accel_cfg.bw <= BMI160_ACCEL_BW_RES_AVG128)
- {
- if (dev->accel_cfg.bw != dev->prev_accel_cfg.bw)
- {
- bw = (uint8_t)dev->accel_cfg.bw;
- temp = *data & ~BMI160_ACCEL_BW_MASK;
- /* Adding bandwidth */
- *data = temp | ((bw << 4) & BMI160_ACCEL_BW_MASK);
- }
- }
- else
- {
- rslt = BMI160_E_OUT_OF_RANGE;
- }
- return rslt;
- }
- /*!
- * @brief This API process the accel range.
- */
- static int8_t process_accel_range(uint8_t *data, const struct bmi160_dev *dev)
- {
- int8_t rslt = 0;
- uint8_t temp = 0;
- uint8_t range = 0;
- if (dev->accel_cfg.range <= BMI160_ACCEL_RANGE_16G)
- {
- if (dev->accel_cfg.range != dev->prev_accel_cfg.range)
- {
- range = (uint8_t)dev->accel_cfg.range;
- temp = *data & ~BMI160_ACCEL_RANGE_MASK;
- /* Adding range */
- *data = temp | (range & BMI160_ACCEL_RANGE_MASK);
- }
- }
- else
- {
- rslt = BMI160_E_OUT_OF_RANGE;
- }
- return rslt;
- }
- /*!
- * @brief This API checks the invalid settings for ODR & Bw for
- * Accel and Gyro.
- */
- static int8_t check_invalid_settg(const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data = 0;
- /* read the error reg */
- rslt = bmi160_get_regs(BMI160_ERROR_REG_ADDR, &data, 1, dev);
- data = data >> 1;
- data = data & BMI160_ERR_REG_MASK;
- if (data == 1)
- {
- rslt = BMI160_E_ACCEL_ODR_BW_INVALID;
- }
- else if (data == 2)
- {
- rslt = BMI160_E_GYRO_ODR_BW_INVALID;
- }
- else if (data == 3)
- {
- rslt = BMI160_E_LWP_PRE_FLTR_INT_INVALID;
- }
- else if (data == 7)
- {
- rslt = BMI160_E_LWP_PRE_FLTR_INVALID;
- }
- return rslt;
- }
- static int8_t set_gyro_conf(struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data[2] = { 0 };
- rslt = check_gyro_config(data, dev);
- if (rslt == BMI160_OK)
- {
- /* Write output data rate and bandwidth */
- rslt = bmi160_set_regs(BMI160_GYRO_CONFIG_ADDR, &data[0], 1, dev);
- if (rslt == BMI160_OK)
- {
- dev->prev_gyro_cfg.odr = dev->gyro_cfg.odr;
- dev->prev_gyro_cfg.bw = dev->gyro_cfg.bw;
- /* Write gyro range */
- rslt = bmi160_set_regs(BMI160_GYRO_RANGE_ADDR, &data[1], 1, dev);
- if (rslt == BMI160_OK)
- {
- dev->prev_gyro_cfg.range = dev->gyro_cfg.range;
- }
- }
- }
- return rslt;
- }
- /*!
- * @brief This API gets the gyro configuration.
- */
- static int8_t get_gyro_conf(struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data[2] = { 0 };
- /* Get accel configurations */
- rslt = bmi160_get_regs(BMI160_GYRO_CONFIG_ADDR, data, 2, dev);
- if (rslt == BMI160_OK)
- {
- dev->gyro_cfg.odr = (data[0] & BMI160_GYRO_ODR_MASK);
- dev->gyro_cfg.bw = (data[0] & BMI160_GYRO_BW_MASK) >> BMI160_GYRO_BW_POS;
- dev->gyro_cfg.range = (data[1] & BMI160_GYRO_RANGE_MASK);
- }
- return rslt;
- }
- /*!
- * @brief This API check the gyro configuration.
- */
- static int8_t check_gyro_config(uint8_t *data, const struct bmi160_dev *dev)
- {
- int8_t rslt;
- /* read gyro Output data rate and bandwidth */
- rslt = bmi160_get_regs(BMI160_GYRO_CONFIG_ADDR, data, 2, dev);
- if (rslt == BMI160_OK)
- {
- rslt = process_gyro_odr(&data[0], dev);
- if (rslt == BMI160_OK)
- {
- rslt = process_gyro_bw(&data[0], dev);
- if (rslt == BMI160_OK)
- {
- rslt = process_gyro_range(&data[1], dev);
- }
- }
- }
- return rslt;
- }
- /*!
- * @brief This API process the gyro odr.
- */
- static int8_t process_gyro_odr(uint8_t *data, const struct bmi160_dev *dev)
- {
- int8_t rslt = 0;
- uint8_t temp = 0;
- uint8_t odr = 0;
- if (dev->gyro_cfg.odr <= BMI160_GYRO_ODR_3200HZ)
- {
- if (dev->gyro_cfg.odr != dev->prev_gyro_cfg.odr)
- {
- odr = (uint8_t)dev->gyro_cfg.odr;
- temp = (*data & ~BMI160_GYRO_ODR_MASK);
- /* Adding output data rate */
- *data = temp | (odr & BMI160_GYRO_ODR_MASK);
- }
- }
- else
- {
- rslt = BMI160_E_OUT_OF_RANGE;
- }
- return rslt;
- }
- /*!
- * @brief This API process the gyro bandwidth.
- */
- static int8_t process_gyro_bw(uint8_t *data, const struct bmi160_dev *dev)
- {
- int8_t rslt = 0;
- uint8_t temp = 0;
- uint8_t bw = 0;
- if (dev->gyro_cfg.bw <= BMI160_GYRO_BW_NORMAL_MODE)
- {
- bw = (uint8_t)dev->gyro_cfg.bw;
- temp = *data & ~BMI160_GYRO_BW_MASK;
- /* Adding bandwidth */
- *data = temp | ((bw << 4) & BMI160_GYRO_BW_MASK);
- }
- else
- {
- rslt = BMI160_E_OUT_OF_RANGE;
- }
- return rslt;
- }
- /*!
- * @brief This API process the gyro range.
- */
- static int8_t process_gyro_range(uint8_t *data, const struct bmi160_dev *dev)
- {
- int8_t rslt = 0;
- uint8_t temp = 0;
- uint8_t range = 0;
- if (dev->gyro_cfg.range <= BMI160_GYRO_RANGE_125_DPS)
- {
- if (dev->gyro_cfg.range != dev->prev_gyro_cfg.range)
- {
- range = (uint8_t)dev->gyro_cfg.range;
- temp = *data & ~BMI160_GYRO_RANGE_MASK;
- /* Adding range */
- *data = temp | (range & BMI160_GYRO_RANGE_MASK);
- }
- }
- else
- {
- rslt = BMI160_E_OUT_OF_RANGE;
- }
- return rslt;
- }
- /*!
- * @brief This API sets the accel power.
- */
- static int8_t set_accel_pwr(struct bmi160_dev *dev)
- {
- int8_t rslt = 0;
- uint8_t data = 0;
- if ((dev->accel_cfg.power >= BMI160_ACCEL_SUSPEND_MODE) && (dev->accel_cfg.power <= BMI160_ACCEL_LOWPOWER_MODE))
- {
- if (dev->accel_cfg.power != dev->prev_accel_cfg.power)
- {
- rslt = process_under_sampling(&data, dev);
- if (rslt == BMI160_OK)
- {
- /* Write accel power */
- rslt = bmi160_set_regs(BMI160_COMMAND_REG_ADDR, &dev->accel_cfg.power, 1, dev);
- /* Add delay of 3.8 ms - refer data sheet table 24*/
- if (dev->prev_accel_cfg.power == BMI160_ACCEL_SUSPEND_MODE)
- {
- dev->delay_ms(BMI160_ACCEL_DELAY_MS);
- }
- dev->prev_accel_cfg.power = dev->accel_cfg.power;
- }
- }
- }
- else
- {
- rslt = BMI160_E_INVALID_CONFIG;
- }
- return rslt;
- }
- /*!
- * @brief This API process the undersampling setting of Accel.
- */
- static int8_t process_under_sampling(uint8_t *data, const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t temp = 0;
- uint8_t pre_filter[2] = { 0 };
- rslt = bmi160_get_regs(BMI160_ACCEL_CONFIG_ADDR, data, 1, dev);
- if (rslt == BMI160_OK)
- {
- if (dev->accel_cfg.power == BMI160_ACCEL_LOWPOWER_MODE)
- {
- temp = *data & ~BMI160_ACCEL_UNDERSAMPLING_MASK;
- /* Set under-sampling parameter */
- *data = temp | ((1 << 7) & BMI160_ACCEL_UNDERSAMPLING_MASK);
- /* Write data */
- rslt = bmi160_set_regs(BMI160_ACCEL_CONFIG_ADDR, data, 1, dev);
- /* Disable the pre-filter data in low power mode */
- if (rslt == BMI160_OK)
- {
- /* Disable the Pre-filter data*/
- rslt = bmi160_set_regs(BMI160_INT_DATA_0_ADDR, pre_filter, 2, dev);
- }
- }
- else if (*data & BMI160_ACCEL_UNDERSAMPLING_MASK)
- {
- temp = *data & ~BMI160_ACCEL_UNDERSAMPLING_MASK;
- /* Disable under-sampling parameter if already enabled */
- *data = temp;
- /* Write data */
- rslt = bmi160_set_regs(BMI160_ACCEL_CONFIG_ADDR, data, 1, dev);
- }
- }
- return rslt;
- }
- /*!
- * @brief This API sets the gyro power mode.
- */
- static int8_t set_gyro_pwr(struct bmi160_dev *dev)
- {
- int8_t rslt = 0;
- if ((dev->gyro_cfg.power == BMI160_GYRO_SUSPEND_MODE) || (dev->gyro_cfg.power == BMI160_GYRO_NORMAL_MODE) ||
- (dev->gyro_cfg.power == BMI160_GYRO_FASTSTARTUP_MODE))
- {
- if (dev->gyro_cfg.power != dev->prev_gyro_cfg.power)
- {
- /* Write gyro power */
- rslt = bmi160_set_regs(BMI160_COMMAND_REG_ADDR, &dev->gyro_cfg.power, 1, dev);
- if (dev->prev_gyro_cfg.power == BMI160_GYRO_SUSPEND_MODE)
- {
- /* Delay of 80 ms - datasheet Table 24 */
- dev->delay_ms(BMI160_GYRO_DELAY_MS);
- }
- else if ((dev->prev_gyro_cfg.power == BMI160_GYRO_FASTSTARTUP_MODE) &&
- (dev->gyro_cfg.power == BMI160_GYRO_NORMAL_MODE))
- {
- /* This delay is required for transition from
- * fast-startup mode to normal mode - datasheet Table 3 */
- dev->delay_ms(10);
- }
- else
- {
- /* do nothing */
- }
- dev->prev_gyro_cfg.power = dev->gyro_cfg.power;
- }
- }
- else
- {
- rslt = BMI160_E_INVALID_CONFIG;
- }
- return rslt;
- }
- /*!
- * @brief This API reads accel data along with sensor time if time is requested
- * by user. Kindly refer the user guide(README.md) for more info.
- */
- static int8_t get_accel_data(uint8_t len, struct bmi160_sensor_data *accel, const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t idx = 0;
- uint8_t data_array[9] = { 0 };
- uint8_t time_0 = 0;
- uint16_t time_1 = 0;
- uint32_t time_2 = 0;
- uint8_t lsb;
- uint8_t msb;
- int16_t msblsb;
- /* read accel sensor data along with time if requested */
- rslt = bmi160_get_regs(BMI160_ACCEL_DATA_ADDR, data_array, 6 + len, dev);
- if (rslt == BMI160_OK)
- {
- /* Accel Data */
- lsb = data_array[idx++];
- msb = data_array[idx++];
- msblsb = (int16_t)((msb << 8) | lsb);
- accel->x = msblsb; /* Data in X axis */
- lsb = data_array[idx++];
- msb = data_array[idx++];
- msblsb = (int16_t)((msb << 8) | lsb);
- accel->y = msblsb; /* Data in Y axis */
- lsb = data_array[idx++];
- msb = data_array[idx++];
- msblsb = (int16_t)((msb << 8) | lsb);
- accel->z = msblsb; /* Data in Z axis */
- if (len == 3)
- {
- time_0 = data_array[idx++];
- time_1 = (uint16_t)(data_array[idx++] << 8);
- time_2 = (uint32_t)(data_array[idx++] << 16);
- accel->sensortime = (uint32_t)(time_2 | time_1 | time_0);
- }
- else
- {
- accel->sensortime = 0;
- }
- }
- else
- {
- rslt = BMI160_E_COM_FAIL;
- }
- return rslt;
- }
- /*!
- * @brief This API reads accel data along with sensor time if time is requested
- * by user. Kindly refer the user guide(README.md) for more info.
- */
- static int8_t get_gyro_data(uint8_t len, struct bmi160_sensor_data *gyro, const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t idx = 0;
- uint8_t data_array[15] = { 0 };
- uint8_t time_0 = 0;
- uint16_t time_1 = 0;
- uint32_t time_2 = 0;
- uint8_t lsb;
- uint8_t msb;
- int16_t msblsb;
- if (len == 0)
- {
- /* read gyro data only */
- rslt = bmi160_get_regs(BMI160_GYRO_DATA_ADDR, data_array, 6, dev);
- if (rslt == BMI160_OK)
- {
- /* Gyro Data */
- lsb = data_array[idx++];
- msb = data_array[idx++];
- msblsb = (int16_t)((msb << 8) | lsb);
- gyro->x = msblsb; /* Data in X axis */
- lsb = data_array[idx++];
- msb = data_array[idx++];
- msblsb = (int16_t)((msb << 8) | lsb);
- gyro->y = msblsb; /* Data in Y axis */
- lsb = data_array[idx++];
- msb = data_array[idx++];
- msblsb = (int16_t)((msb << 8) | lsb);
- gyro->z = msblsb; /* Data in Z axis */
- gyro->sensortime = 0;
- }
- else
- {
- rslt = BMI160_E_COM_FAIL;
- }
- }
- else
- {
- /* read gyro sensor data along with time */
- rslt = bmi160_get_regs(BMI160_GYRO_DATA_ADDR, data_array, 12 + len, dev);
- if (rslt == BMI160_OK)
- {
- /* Gyro Data */
- lsb = data_array[idx++];
- msb = data_array[idx++];
- msblsb = (int16_t)((msb << 8) | lsb);
- gyro->x = msblsb; /* gyro X axis data */
- lsb = data_array[idx++];
- msb = data_array[idx++];
- msblsb = (int16_t)((msb << 8) | lsb);
- gyro->y = msblsb; /* gyro Y axis data */
- lsb = data_array[idx++];
- msb = data_array[idx++];
- msblsb = (int16_t)((msb << 8) | lsb);
- gyro->z = msblsb; /* gyro Z axis data */
- idx = idx + 6;
- time_0 = data_array[idx++];
- time_1 = (uint16_t)(data_array[idx++] << 8);
- time_2 = (uint32_t)(data_array[idx++] << 16);
- gyro->sensortime = (uint32_t)(time_2 | time_1 | time_0);
- }
- else
- {
- rslt = BMI160_E_COM_FAIL;
- }
- }
- return rslt;
- }
- /*!
- * @brief This API reads accel and gyro data along with sensor time
- * if time is requested by user.
- * Kindly refer the user guide(README.md) for more info.
- */
- static int8_t get_accel_gyro_data(uint8_t len,
- struct bmi160_sensor_data *accel,
- struct bmi160_sensor_data *gyro,
- const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t idx = 0;
- uint8_t data_array[15] = { 0 };
- uint8_t time_0 = 0;
- uint16_t time_1 = 0;
- uint32_t time_2 = 0;
- uint8_t lsb;
- uint8_t msb;
- int16_t msblsb;
- /* read both accel and gyro sensor data
- * along with time if requested */
- rslt = bmi160_get_regs(BMI160_GYRO_DATA_ADDR, data_array, 12 + len, dev);
- if (rslt == BMI160_OK)
- {
- /* Gyro Data */
- lsb = data_array[idx++];
- msb = data_array[idx++];
- msblsb = (int16_t)((msb << 8) | lsb);
- gyro->x = msblsb; /* gyro X axis data */
- lsb = data_array[idx++];
- msb = data_array[idx++];
- msblsb = (int16_t)((msb << 8) | lsb);
- gyro->y = msblsb; /* gyro Y axis data */
- lsb = data_array[idx++];
- msb = data_array[idx++];
- msblsb = (int16_t)((msb << 8) | lsb);
- gyro->z = msblsb; /* gyro Z axis data */
- /* Accel Data */
- lsb = data_array[idx++];
- msb = data_array[idx++];
- msblsb = (int16_t)((msb << 8) | lsb);
- accel->x = (int16_t)msblsb; /* accel X axis data */
- lsb = data_array[idx++];
- msb = data_array[idx++];
- msblsb = (int16_t)((msb << 8) | lsb);
- accel->y = (int16_t)msblsb; /* accel Y axis data */
- lsb = data_array[idx++];
- msb = data_array[idx++];
- msblsb = (int16_t)((msb << 8) | lsb);
- accel->z = (int16_t)msblsb; /* accel Z axis data */
- if (len == 3)
- {
- time_0 = data_array[idx++];
- time_1 = (uint16_t)(data_array[idx++] << 8);
- time_2 = (uint32_t)(data_array[idx++] << 16);
- accel->sensortime = (uint32_t)(time_2 | time_1 | time_0);
- gyro->sensortime = (uint32_t)(time_2 | time_1 | time_0);
- }
- else
- {
- accel->sensortime = 0;
- gyro->sensortime = 0;
- }
- }
- else
- {
- rslt = BMI160_E_COM_FAIL;
- }
- return rslt;
- }
- /*!
- * @brief This API enables the any-motion interrupt for accel.
- */
- static int8_t enable_accel_any_motion_int(const struct bmi160_acc_any_mot_int_cfg *any_motion_int_cfg,
- struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data = 0;
- uint8_t temp = 0;
- /* Enable any motion x, any motion y, any motion z
- * in Int Enable 0 register */
- rslt = bmi160_get_regs(BMI160_INT_ENABLE_0_ADDR, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- if (any_motion_int_cfg->anymotion_en == BMI160_ENABLE)
- {
- temp = data & ~BMI160_ANY_MOTION_X_INT_EN_MASK;
- /* Adding Any_motion x axis */
- data = temp | (any_motion_int_cfg->anymotion_x & BMI160_ANY_MOTION_X_INT_EN_MASK);
- temp = data & ~BMI160_ANY_MOTION_Y_INT_EN_MASK;
- /* Adding Any_motion y axis */
- data = temp | ((any_motion_int_cfg->anymotion_y << 1) & BMI160_ANY_MOTION_Y_INT_EN_MASK);
- temp = data & ~BMI160_ANY_MOTION_Z_INT_EN_MASK;
- /* Adding Any_motion z axis */
- data = temp | ((any_motion_int_cfg->anymotion_z << 2) & BMI160_ANY_MOTION_Z_INT_EN_MASK);
- /* any-motion feature selected*/
- dev->any_sig_sel = BMI160_ANY_MOTION_ENABLED;
- }
- else
- {
- data = data & ~BMI160_ANY_MOTION_ALL_INT_EN_MASK;
- /* neither any-motion feature nor sig-motion selected */
- dev->any_sig_sel = BMI160_BOTH_ANY_SIG_MOTION_DISABLED;
- }
- /* write data to Int Enable 0 register */
- rslt = bmi160_set_regs(BMI160_INT_ENABLE_0_ADDR, &data, 1, dev);
- }
- return rslt;
- }
- /*!
- * @brief This API disable the sig-motion interrupt.
- */
- static int8_t disable_sig_motion_int(const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data = 0;
- uint8_t temp = 0;
- /* Disabling Significant motion interrupt if enabled */
- rslt = bmi160_get_regs(BMI160_INT_MOTION_3_ADDR, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- temp = (data & BMI160_SIG_MOTION_SEL_MASK);
- if (temp)
- {
- temp = data & ~BMI160_SIG_MOTION_SEL_MASK;
- data = temp;
- /* Write data to register */
- rslt = bmi160_set_regs(BMI160_INT_MOTION_3_ADDR, &data, 1, dev);
- }
- }
- return rslt;
- }
- /*!
- * @brief This API is used to map/unmap the Any/Sig motion, Step det/Low-g,
- * Double tap, Single tap, Orientation, Flat, High-G, Nomotion interrupt pins.
- */
- static int8_t map_feature_interrupt(const struct bmi160_int_settg *int_config, const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data[3] = { 0, 0, 0 };
- uint8_t temp[3] = { 0, 0, 0 };
- rslt = bmi160_get_regs(BMI160_INT_MAP_0_ADDR, data, 3, dev);
- if (rslt == BMI160_OK)
- {
- temp[0] = data[0] & ~int_mask_lookup_table[int_config->int_type];
- temp[2] = data[2] & ~int_mask_lookup_table[int_config->int_type];
- switch (int_config->int_channel)
- {
- case BMI160_INT_CHANNEL_NONE:
- data[0] = temp[0];
- data[2] = temp[2];
- break;
- case BMI160_INT_CHANNEL_1:
- data[0] = temp[0] | int_mask_lookup_table[int_config->int_type];
- data[2] = temp[2];
- break;
- case BMI160_INT_CHANNEL_2:
- data[2] = temp[2] | int_mask_lookup_table[int_config->int_type];
- data[0] = temp[0];
- break;
- case BMI160_INT_CHANNEL_BOTH:
- data[0] = temp[0] | int_mask_lookup_table[int_config->int_type];
- data[2] = temp[2] | int_mask_lookup_table[int_config->int_type];
- break;
- default:
- rslt = BMI160_E_OUT_OF_RANGE;
- }
- if (rslt == BMI160_OK)
- {
- rslt = bmi160_set_regs(BMI160_INT_MAP_0_ADDR, data, 3, dev);
- }
- }
- return rslt;
- }
- /*!
- * @brief This API is used to map/unmap the Dataready(Accel & Gyro), FIFO full
- * and FIFO watermark interrupt.
- */
- static int8_t map_hardware_interrupt(const struct bmi160_int_settg *int_config, const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data = 0;
- uint8_t temp = 0;
- rslt = bmi160_get_regs(BMI160_INT_MAP_1_ADDR, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- temp = data & ~int_mask_lookup_table[int_config->int_type];
- temp = temp & ~((uint8_t)(int_mask_lookup_table[int_config->int_type] << 4));
- switch (int_config->int_channel)
- {
- case BMI160_INT_CHANNEL_NONE:
- data = temp;
- break;
- case BMI160_INT_CHANNEL_1:
- data = temp | (uint8_t)((int_mask_lookup_table[int_config->int_type]) << 4);
- break;
- case BMI160_INT_CHANNEL_2:
- data = temp | int_mask_lookup_table[int_config->int_type];
- break;
- case BMI160_INT_CHANNEL_BOTH:
- data = temp | int_mask_lookup_table[int_config->int_type];
- data = data | (uint8_t)((int_mask_lookup_table[int_config->int_type]) << 4);
- break;
- default:
- rslt = BMI160_E_OUT_OF_RANGE;
- }
- if (rslt == BMI160_OK)
- {
- rslt = bmi160_set_regs(BMI160_INT_MAP_1_ADDR, &data, 1, dev);
- }
- }
- return rslt;
- }
- /*!
- * @brief This API configure the source of data(filter & pre-filter)
- * for any-motion interrupt.
- */
- static int8_t config_any_motion_src(const struct bmi160_acc_any_mot_int_cfg *any_motion_int_cfg,
- const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data = 0;
- uint8_t temp = 0;
- /* Configure Int data 1 register to add source of interrupt */
- rslt = bmi160_get_regs(BMI160_INT_DATA_1_ADDR, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- temp = data & ~BMI160_MOTION_SRC_INT_MASK;
- data = temp | ((any_motion_int_cfg->anymotion_data_src << 7) & BMI160_MOTION_SRC_INT_MASK);
- /* Write data to DATA 1 address */
- rslt = bmi160_set_regs(BMI160_INT_DATA_1_ADDR, &data, 1, dev);
- }
- return rslt;
- }
- /*!
- * @brief This API configure the duration and threshold of
- * any-motion interrupt.
- */
- static int8_t config_any_dur_threshold(const struct bmi160_acc_any_mot_int_cfg *any_motion_int_cfg,
- const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data = 0;
- uint8_t temp = 0;
- uint8_t data_array[2] = { 0 };
- uint8_t dur;
- /* Configure Int Motion 0 register */
- rslt = bmi160_get_regs(BMI160_INT_MOTION_0_ADDR, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- /* slope duration */
- dur = (uint8_t)any_motion_int_cfg->anymotion_dur;
- temp = data & ~BMI160_SLOPE_INT_DUR_MASK;
- data = temp | (dur & BMI160_MOTION_SRC_INT_MASK);
- data_array[0] = data;
- /* add slope threshold */
- data_array[1] = any_motion_int_cfg->anymotion_thr;
- /* INT MOTION 0 and INT MOTION 1 address lie consecutively,
- * hence writing data to respective registers at one go */
- /* Writing to Int_motion 0 and
- * Int_motion 1 Address simultaneously */
- rslt = bmi160_set_regs(BMI160_INT_MOTION_0_ADDR, data_array, 2, dev);
- }
- return rslt;
- }
- /*!
- * @brief This API configure necessary setting of any-motion interrupt.
- */
- static int8_t config_any_motion_int_settg(const struct bmi160_int_settg *int_config,
- const struct bmi160_acc_any_mot_int_cfg *any_motion_int_cfg,
- const struct bmi160_dev *dev)
- {
- int8_t rslt;
- /* Configure Interrupt pins */
- rslt = set_intr_pin_config(int_config, dev);
- if (rslt == BMI160_OK)
- {
- rslt = disable_sig_motion_int(dev);
- if (rslt == BMI160_OK)
- {
- rslt = map_feature_interrupt(int_config, dev);
- if (rslt == BMI160_OK)
- {
- rslt = config_any_motion_src(any_motion_int_cfg, dev);
- if (rslt == BMI160_OK)
- {
- rslt = config_any_dur_threshold(any_motion_int_cfg, dev);
- }
- }
- }
- }
- return rslt;
- }
- /*!
- * @brief This API enable the data ready interrupt.
- */
- static int8_t enable_data_ready_int(const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data = 0;
- uint8_t temp = 0;
- /* Enable data ready interrupt in Int Enable 1 register */
- rslt = bmi160_get_regs(BMI160_INT_ENABLE_1_ADDR, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- temp = data & ~BMI160_DATA_RDY_INT_EN_MASK;
- data = temp | ((1 << 4) & BMI160_DATA_RDY_INT_EN_MASK);
- /* Writing data to INT ENABLE 1 Address */
- rslt = bmi160_set_regs(BMI160_INT_ENABLE_1_ADDR, &data, 1, dev);
- }
- return rslt;
- }
- /*!
- * @brief This API enables the no motion/slow motion interrupt.
- */
- static int8_t enable_no_motion_int(const struct bmi160_acc_no_motion_int_cfg *no_mot_int_cfg,
- const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data = 0;
- uint8_t temp = 0;
- /* Enable no motion x, no motion y, no motion z
- * in Int Enable 2 register */
- rslt = bmi160_get_regs(BMI160_INT_ENABLE_2_ADDR, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- if (no_mot_int_cfg->no_motion_x == 1)
- {
- temp = data & ~BMI160_NO_MOTION_X_INT_EN_MASK;
- /* Adding No_motion x axis */
- data = temp | (1 & BMI160_NO_MOTION_X_INT_EN_MASK);
- }
- if (no_mot_int_cfg->no_motion_y == 1)
- {
- temp = data & ~BMI160_NO_MOTION_Y_INT_EN_MASK;
- /* Adding No_motion x axis */
- data = temp | ((1 << 1) & BMI160_NO_MOTION_Y_INT_EN_MASK);
- }
- if (no_mot_int_cfg->no_motion_z == 1)
- {
- temp = data & ~BMI160_NO_MOTION_Z_INT_EN_MASK;
- /* Adding No_motion x axis */
- data = temp | ((1 << 2) & BMI160_NO_MOTION_Z_INT_EN_MASK);
- }
- /* write data to Int Enable 2 register */
- rslt = bmi160_set_regs(BMI160_INT_ENABLE_2_ADDR, &data, 1, dev);
- }
- return rslt;
- }
- /*!
- * @brief This API configure the interrupt PIN setting for
- * no motion/slow motion interrupt.
- */
- static int8_t config_no_motion_int_settg(const struct bmi160_int_settg *int_config,
- const struct bmi160_acc_no_motion_int_cfg *no_mot_int_cfg,
- const struct bmi160_dev *dev)
- {
- int8_t rslt;
- /* Configure Interrupt pins */
- rslt = set_intr_pin_config(int_config, dev);
- if (rslt == BMI160_OK)
- {
- rslt = map_feature_interrupt(int_config, dev);
- if (rslt == BMI160_OK)
- {
- rslt = config_no_motion_data_src(no_mot_int_cfg, dev);
- if (rslt == BMI160_OK)
- {
- rslt = config_no_motion_dur_thr(no_mot_int_cfg, dev);
- }
- }
- }
- return rslt;
- }
- /*!
- * @brief This API configure the source of interrupt for no motion.
- */
- static int8_t config_no_motion_data_src(const struct bmi160_acc_no_motion_int_cfg *no_mot_int_cfg,
- const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data = 0;
- uint8_t temp = 0;
- /* Configure Int data 1 register to add source of interrupt */
- rslt = bmi160_get_regs(BMI160_INT_DATA_1_ADDR, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- temp = data & ~BMI160_MOTION_SRC_INT_MASK;
- data = temp | ((no_mot_int_cfg->no_motion_src << 7) & BMI160_MOTION_SRC_INT_MASK);
- /* Write data to DATA 1 address */
- rslt = bmi160_set_regs(BMI160_INT_DATA_1_ADDR, &data, 1, dev);
- }
- return rslt;
- }
- /*!
- * @brief This API configure the duration and threshold of
- * no motion/slow motion interrupt along with selection of no/slow motion.
- */
- static int8_t config_no_motion_dur_thr(const struct bmi160_acc_no_motion_int_cfg *no_mot_int_cfg,
- const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data = 0;
- uint8_t temp = 0;
- uint8_t temp_1 = 0;
- uint8_t reg_addr;
- uint8_t data_array[2] = { 0 };
- /* Configuring INT_MOTION register */
- reg_addr = BMI160_INT_MOTION_0_ADDR;
- rslt = bmi160_get_regs(reg_addr, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- temp = data & ~BMI160_NO_MOTION_INT_DUR_MASK;
- /* Adding no_motion duration */
- data = temp | ((no_mot_int_cfg->no_motion_dur << 2) & BMI160_NO_MOTION_INT_DUR_MASK);
- /* Write data to NO_MOTION 0 address */
- rslt = bmi160_set_regs(reg_addr, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- reg_addr = BMI160_INT_MOTION_3_ADDR;
- rslt = bmi160_get_regs(reg_addr, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- temp = data & ~BMI160_NO_MOTION_SEL_BIT_MASK;
- /* Adding no_motion_sel bit */
- temp_1 = (no_mot_int_cfg->no_motion_sel & BMI160_NO_MOTION_SEL_BIT_MASK);
- data = (temp | temp_1);
- data_array[1] = data;
- /* Adding no motion threshold */
- data_array[0] = no_mot_int_cfg->no_motion_thres;
- reg_addr = BMI160_INT_MOTION_2_ADDR;
- /* writing data to INT_MOTION 2 and INT_MOTION 3
- * address simultaneously */
- rslt = bmi160_set_regs(reg_addr, data_array, 2, dev);
- }
- }
- }
- return rslt;
- }
- /*!
- * @brief This API enables the sig-motion motion interrupt.
- */
- static int8_t enable_sig_motion_int(const struct bmi160_acc_sig_mot_int_cfg *sig_mot_int_cfg, struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data = 0;
- uint8_t temp = 0;
- /* For significant motion,enable any motion x,any motion y,
- * any motion z in Int Enable 0 register */
- rslt = bmi160_get_regs(BMI160_INT_ENABLE_0_ADDR, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- if (sig_mot_int_cfg->sig_en == BMI160_ENABLE)
- {
- temp = data & ~BMI160_SIG_MOTION_INT_EN_MASK;
- data = temp | (7 & BMI160_SIG_MOTION_INT_EN_MASK);
- /* sig-motion feature selected*/
- dev->any_sig_sel = BMI160_SIG_MOTION_ENABLED;
- }
- else
- {
- data = data & ~BMI160_SIG_MOTION_INT_EN_MASK;
- /* neither any-motion feature nor sig-motion selected */
- dev->any_sig_sel = BMI160_BOTH_ANY_SIG_MOTION_DISABLED;
- }
- /* write data to Int Enable 0 register */
- rslt = bmi160_set_regs(BMI160_INT_ENABLE_0_ADDR, &data, 1, dev);
- }
- return rslt;
- }
- /*!
- * @brief This API configure the interrupt PIN setting for
- * significant motion interrupt.
- */
- static int8_t config_sig_motion_int_settg(const struct bmi160_int_settg *int_config,
- const struct bmi160_acc_sig_mot_int_cfg *sig_mot_int_cfg,
- const struct bmi160_dev *dev)
- {
- int8_t rslt;
- /* Configure Interrupt pins */
- rslt = set_intr_pin_config(int_config, dev);
- if (rslt == BMI160_OK)
- {
- rslt = map_feature_interrupt(int_config, dev);
- if (rslt == BMI160_OK)
- {
- rslt = config_sig_motion_data_src(sig_mot_int_cfg, dev);
- if (rslt == BMI160_OK)
- {
- rslt = config_sig_dur_threshold(sig_mot_int_cfg, dev);
- }
- }
- }
- return rslt;
- }
- /*!
- * @brief This API configure the source of data(filter & pre-filter)
- * for sig motion interrupt.
- */
- static int8_t config_sig_motion_data_src(const struct bmi160_acc_sig_mot_int_cfg *sig_mot_int_cfg,
- const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data = 0;
- uint8_t temp = 0;
- /* Configure Int data 1 register to add source of interrupt */
- rslt = bmi160_get_regs(BMI160_INT_DATA_1_ADDR, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- temp = data & ~BMI160_MOTION_SRC_INT_MASK;
- data = temp | ((sig_mot_int_cfg->sig_data_src << 7) & BMI160_MOTION_SRC_INT_MASK);
- /* Write data to DATA 1 address */
- rslt = bmi160_set_regs(BMI160_INT_DATA_1_ADDR, &data, 1, dev);
- }
- return rslt;
- }
- /*!
- * @brief This API configure the threshold, skip and proof time of
- * sig motion interrupt.
- */
- static int8_t config_sig_dur_threshold(const struct bmi160_acc_sig_mot_int_cfg *sig_mot_int_cfg,
- const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data;
- uint8_t temp = 0;
- /* Configuring INT_MOTION registers */
- /* Write significant motion threshold.
- * This threshold is same as any motion threshold */
- data = sig_mot_int_cfg->sig_mot_thres;
- /* Write data to INT_MOTION 1 address */
- rslt = bmi160_set_regs(BMI160_INT_MOTION_1_ADDR, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- rslt = bmi160_get_regs(BMI160_INT_MOTION_3_ADDR, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- temp = data & ~BMI160_SIG_MOTION_SKIP_MASK;
- /* adding skip time of sig_motion interrupt*/
- data = temp | ((sig_mot_int_cfg->sig_mot_skip << 2) & BMI160_SIG_MOTION_SKIP_MASK);
- temp = data & ~BMI160_SIG_MOTION_PROOF_MASK;
- /* adding proof time of sig_motion interrupt */
- data = temp | ((sig_mot_int_cfg->sig_mot_proof << 4) & BMI160_SIG_MOTION_PROOF_MASK);
- /* configure the int_sig_mot_sel bit to select
- * significant motion interrupt */
- temp = data & ~BMI160_SIG_MOTION_SEL_MASK;
- data = temp | ((sig_mot_int_cfg->sig_en << 1) & BMI160_SIG_MOTION_SEL_MASK);
- rslt = bmi160_set_regs(BMI160_INT_MOTION_3_ADDR, &data, 1, dev);
- }
- }
- return rslt;
- }
- /*!
- * @brief This API enables the step detector interrupt.
- */
- static int8_t enable_step_detect_int(const struct bmi160_acc_step_detect_int_cfg *step_detect_int_cfg,
- const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data = 0;
- uint8_t temp = 0;
- /* Enable data ready interrupt in Int Enable 2 register */
- rslt = bmi160_get_regs(BMI160_INT_ENABLE_2_ADDR, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- temp = data & ~BMI160_STEP_DETECT_INT_EN_MASK;
- data = temp | ((step_detect_int_cfg->step_detector_en << 3) & BMI160_STEP_DETECT_INT_EN_MASK);
- /* Writing data to INT ENABLE 2 Address */
- rslt = bmi160_set_regs(BMI160_INT_ENABLE_2_ADDR, &data, 1, dev);
- }
- return rslt;
- }
- /*!
- * @brief This API configure the step detector parameter.
- */
- static int8_t config_step_detect(const struct bmi160_acc_step_detect_int_cfg *step_detect_int_cfg,
- const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t temp = 0;
- uint8_t data_array[2] = { 0 };
- if (step_detect_int_cfg->step_detector_mode == BMI160_STEP_DETECT_NORMAL)
- {
- /* Normal mode setting */
- data_array[0] = 0x15;
- data_array[1] = 0x03;
- }
- else if (step_detect_int_cfg->step_detector_mode == BMI160_STEP_DETECT_SENSITIVE)
- {
- /* Sensitive mode setting */
- data_array[0] = 0x2D;
- data_array[1] = 0x00;
- }
- else if (step_detect_int_cfg->step_detector_mode == BMI160_STEP_DETECT_ROBUST)
- {
- /* Robust mode setting */
- data_array[0] = 0x1D;
- data_array[1] = 0x07;
- }
- else if (step_detect_int_cfg->step_detector_mode == BMI160_STEP_DETECT_USER_DEFINE)
- {
- /* Non recommended User defined setting */
- /* Configuring STEP_CONFIG register */
- rslt = bmi160_get_regs(BMI160_INT_STEP_CONFIG_0_ADDR, &data_array[0], 2, dev);
- if (rslt == BMI160_OK)
- {
- temp = data_array[0] & ~BMI160_STEP_DETECT_MIN_THRES_MASK;
- /* Adding min_threshold */
- data_array[0] = temp | ((step_detect_int_cfg->min_threshold << 3) & BMI160_STEP_DETECT_MIN_THRES_MASK);
- temp = data_array[0] & ~BMI160_STEP_DETECT_STEPTIME_MIN_MASK;
- /* Adding steptime_min */
- data_array[0] = temp | ((step_detect_int_cfg->steptime_min) & BMI160_STEP_DETECT_STEPTIME_MIN_MASK);
- temp = data_array[1] & ~BMI160_STEP_MIN_BUF_MASK;
- /* Adding steptime_min */
- data_array[1] = temp | ((step_detect_int_cfg->step_min_buf) & BMI160_STEP_MIN_BUF_MASK);
- }
- }
- /* Write data to STEP_CONFIG register */
- rslt = bmi160_set_regs(BMI160_INT_STEP_CONFIG_0_ADDR, data_array, 2, dev);
- return rslt;
- }
- /*!
- * @brief This API enables the single/double tap interrupt.
- */
- static int8_t enable_tap_int(const struct bmi160_int_settg *int_config,
- const struct bmi160_acc_tap_int_cfg *tap_int_cfg,
- const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data = 0;
- uint8_t temp = 0;
- /* Enable single tap or double tap interrupt in Int Enable 0 register */
- rslt = bmi160_get_regs(BMI160_INT_ENABLE_0_ADDR, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- if (int_config->int_type == BMI160_ACC_SINGLE_TAP_INT)
- {
- temp = data & ~BMI160_SINGLE_TAP_INT_EN_MASK;
- data = temp | ((tap_int_cfg->tap_en << 5) & BMI160_SINGLE_TAP_INT_EN_MASK);
- }
- else
- {
- temp = data & ~BMI160_DOUBLE_TAP_INT_EN_MASK;
- data = temp | ((tap_int_cfg->tap_en << 4) & BMI160_DOUBLE_TAP_INT_EN_MASK);
- }
- /* Write to Enable 0 Address */
- rslt = bmi160_set_regs(BMI160_INT_ENABLE_0_ADDR, &data, 1, dev);
- }
- return rslt;
- }
- /*!
- * @brief This API configure the interrupt PIN setting for
- * tap interrupt.
- */
- static int8_t config_tap_int_settg(const struct bmi160_int_settg *int_config,
- const struct bmi160_acc_tap_int_cfg *tap_int_cfg,
- const struct bmi160_dev *dev)
- {
- int8_t rslt;
- /* Configure Interrupt pins */
- rslt = set_intr_pin_config(int_config, dev);
- if (rslt == BMI160_OK)
- {
- rslt = map_feature_interrupt(int_config, dev);
- if (rslt == BMI160_OK)
- {
- rslt = config_tap_data_src(tap_int_cfg, dev);
- if (rslt == BMI160_OK)
- {
- rslt = config_tap_param(int_config, tap_int_cfg, dev);
- }
- }
- }
- return rslt;
- }
- /*!
- * @brief This API configure the source of data(filter & pre-filter)
- * for tap interrupt.
- */
- static int8_t config_tap_data_src(const struct bmi160_acc_tap_int_cfg *tap_int_cfg, const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data = 0;
- uint8_t temp = 0;
- /* Configure Int data 0 register to add source of interrupt */
- rslt = bmi160_get_regs(BMI160_INT_DATA_0_ADDR, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- temp = data & ~BMI160_TAP_SRC_INT_MASK;
- data = temp | ((tap_int_cfg->tap_data_src << 3) & BMI160_TAP_SRC_INT_MASK);
- /* Write data to Data 0 address */
- rslt = bmi160_set_regs(BMI160_INT_DATA_0_ADDR, &data, 1, dev);
- }
- return rslt;
- }
- /*!
- * @brief This API configure the parameters of tap interrupt.
- * Threshold, quite, shock, and duration.
- */
- static int8_t config_tap_param(const struct bmi160_int_settg *int_config,
- const struct bmi160_acc_tap_int_cfg *tap_int_cfg,
- const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t temp = 0;
- uint8_t data = 0;
- uint8_t data_array[2] = { 0 };
- uint8_t count = 0;
- uint8_t dur, shock, quiet, thres;
- /* Configure tap 0 register for tap shock,tap quiet duration
- * in case of single tap interrupt */
- rslt = bmi160_get_regs(BMI160_INT_TAP_0_ADDR, data_array, 2, dev);
- if (rslt == BMI160_OK)
- {
- data = data_array[count];
- if (int_config->int_type == BMI160_ACC_DOUBLE_TAP_INT)
- {
- dur = (uint8_t)tap_int_cfg->tap_dur;
- temp = (data & ~BMI160_TAP_DUR_MASK);
- /* Add tap duration data in case of
- * double tap interrupt */
- data = temp | (dur & BMI160_TAP_DUR_MASK);
- }
- shock = (uint8_t)tap_int_cfg->tap_shock;
- temp = data & ~BMI160_TAP_SHOCK_DUR_MASK;
- data = temp | ((shock << 6) & BMI160_TAP_SHOCK_DUR_MASK);
- quiet = (uint8_t)tap_int_cfg->tap_quiet;
- temp = data & ~BMI160_TAP_QUIET_DUR_MASK;
- data = temp | ((quiet << 7) & BMI160_TAP_QUIET_DUR_MASK);
- data_array[count++] = data;
- data = data_array[count];
- thres = (uint8_t)tap_int_cfg->tap_thr;
- temp = data & ~BMI160_TAP_THRES_MASK;
- data = temp | (thres & BMI160_TAP_THRES_MASK);
- data_array[count++] = data;
- /* TAP 0 and TAP 1 address lie consecutively,
- * hence writing data to respective registers at one go */
- /* Writing to Tap 0 and Tap 1 Address simultaneously */
- rslt = bmi160_set_regs(BMI160_INT_TAP_0_ADDR, data_array, count, dev);
- }
- return rslt;
- }
- /*!
- * @brief This API configure the secondary interface.
- */
- static int8_t config_sec_if(const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t if_conf = 0;
- uint8_t cmd = BMI160_AUX_NORMAL_MODE;
- /* set the aux power mode to normal*/
- rslt = bmi160_set_regs(BMI160_COMMAND_REG_ADDR, &cmd, 1, dev);
- if (rslt == BMI160_OK)
- {
- /* 0.5ms delay - refer datasheet table 24*/
- dev->delay_ms(1);
- rslt = bmi160_get_regs(BMI160_IF_CONF_ADDR, &if_conf, 1, dev);
- if_conf |= (uint8_t)(1 << 5);
- if (rslt == BMI160_OK)
- {
- /*enable the secondary interface also*/
- rslt = bmi160_set_regs(BMI160_IF_CONF_ADDR, &if_conf, 1, dev);
- }
- }
- return rslt;
- }
- /*!
- * @brief This API configure the ODR of the auxiliary sensor.
- */
- static int8_t config_aux_odr(const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t aux_odr;
- rslt = bmi160_get_regs(BMI160_AUX_ODR_ADDR, &aux_odr, 1, dev);
- if (rslt == BMI160_OK)
- {
- aux_odr = (uint8_t)(dev->aux_cfg.aux_odr);
- /* Set the secondary interface ODR
- * i.e polling rate of secondary sensor */
- rslt = bmi160_set_regs(BMI160_AUX_ODR_ADDR, &aux_odr, 1, dev);
- dev->delay_ms(BMI160_AUX_COM_DELAY);
- }
- return rslt;
- }
- /*!
- * @brief This API maps the actual burst read length set by user.
- */
- static int8_t map_read_len(uint16_t *len, const struct bmi160_dev *dev)
- {
- int8_t rslt = BMI160_OK;
- switch (dev->aux_cfg.aux_rd_burst_len)
- {
- case BMI160_AUX_READ_LEN_0:
- *len = 1;
- break;
- case BMI160_AUX_READ_LEN_1:
- *len = 2;
- break;
- case BMI160_AUX_READ_LEN_2:
- *len = 6;
- break;
- case BMI160_AUX_READ_LEN_3:
- *len = 8;
- break;
- default:
- rslt = BMI160_E_INVALID_INPUT;
- break;
- }
- return rslt;
- }
- /*!
- * @brief This API configure the settings of auxiliary sensor.
- */
- static int8_t config_aux_settg(const struct bmi160_dev *dev)
- {
- int8_t rslt;
- rslt = config_sec_if(dev);
- if (rslt == BMI160_OK)
- {
- /* Configures the auxiliary interface settings */
- rslt = bmi160_config_aux_mode(dev);
- }
- return rslt;
- }
- /*!
- * @brief This API extract the read data from auxiliary sensor.
- */
- static int8_t extract_aux_read(uint16_t map_len,
- uint8_t reg_addr,
- uint8_t *aux_data,
- uint16_t len,
- const struct bmi160_dev *dev)
- {
- int8_t rslt = BMI160_OK;
- uint8_t data[8] = { 0, };
- uint8_t read_addr = BMI160_AUX_DATA_ADDR;
- uint8_t count = 0;
- uint8_t read_count;
- uint8_t read_len = (uint8_t)map_len;
- for (; count < len;)
- {
- /* set address to read */
- rslt = bmi160_set_regs(BMI160_AUX_IF_2_ADDR, ®_addr, 1, dev);
- dev->delay_ms(BMI160_AUX_COM_DELAY);
- if (rslt == BMI160_OK)
- {
- rslt = bmi160_get_regs(read_addr, data, map_len, dev);
- if (rslt == BMI160_OK)
- {
- read_count = 0;
- /* if read len is less the burst read len
- * mention by user*/
- if (len < map_len)
- {
- read_len = (uint8_t)len;
- }
- else if ((len - count) < map_len)
- {
- read_len = (uint8_t)(len - count);
- }
- for (; read_count < read_len; read_count++)
- {
- aux_data[count + read_count] = data[read_count];
- }
- reg_addr += (uint8_t)map_len;
- count += (uint8_t)map_len;
- }
- else
- {
- rslt = BMI160_E_COM_FAIL;
- break;
- }
- }
- }
- return rslt;
- }
- /*!
- * @brief This API enables the orient interrupt.
- */
- static int8_t enable_orient_int(const struct bmi160_acc_orient_int_cfg *orient_int_cfg, const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data = 0;
- uint8_t temp = 0;
- /* Enable data ready interrupt in Int Enable 0 register */
- rslt = bmi160_get_regs(BMI160_INT_ENABLE_0_ADDR, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- temp = data & ~BMI160_ORIENT_INT_EN_MASK;
- data = temp | ((orient_int_cfg->orient_en << 6) & BMI160_ORIENT_INT_EN_MASK);
- /* write data to Int Enable 0 register */
- rslt = bmi160_set_regs(BMI160_INT_ENABLE_0_ADDR, &data, 1, dev);
- }
- return rslt;
- }
- /*!
- * @brief This API configure the necessary setting of orientation interrupt.
- */
- static int8_t config_orient_int_settg(const struct bmi160_acc_orient_int_cfg *orient_int_cfg,
- const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data = 0;
- uint8_t temp = 0;
- uint8_t data_array[2] = { 0, 0 };
- /* Configuring INT_ORIENT registers */
- rslt = bmi160_get_regs(BMI160_INT_ORIENT_0_ADDR, data_array, 2, dev);
- if (rslt == BMI160_OK)
- {
- data = data_array[0];
- temp = data & ~BMI160_ORIENT_MODE_MASK;
- /* Adding Orientation mode */
- data = temp | ((orient_int_cfg->orient_mode) & BMI160_ORIENT_MODE_MASK);
- temp = data & ~BMI160_ORIENT_BLOCK_MASK;
- /* Adding Orientation blocking */
- data = temp | ((orient_int_cfg->orient_blocking << 2) & BMI160_ORIENT_BLOCK_MASK);
- temp = data & ~BMI160_ORIENT_HYST_MASK;
- /* Adding Orientation hysteresis */
- data = temp | ((orient_int_cfg->orient_hyst << 4) & BMI160_ORIENT_HYST_MASK);
- data_array[0] = data;
- data = data_array[1];
- temp = data & ~BMI160_ORIENT_THETA_MASK;
- /* Adding Orientation threshold */
- data = temp | ((orient_int_cfg->orient_theta) & BMI160_ORIENT_THETA_MASK);
- temp = data & ~BMI160_ORIENT_UD_ENABLE;
- /* Adding Orient_ud_en */
- data = temp | ((orient_int_cfg->orient_ud_en << 6) & BMI160_ORIENT_UD_ENABLE);
- temp = data & ~BMI160_AXES_EN_MASK;
- /* Adding axes_en */
- data = temp | ((orient_int_cfg->axes_ex << 7) & BMI160_AXES_EN_MASK);
- data_array[1] = data;
- /* Writing data to INT_ORIENT 0 and INT_ORIENT 1
- * registers simultaneously */
- rslt = bmi160_set_regs(BMI160_INT_ORIENT_0_ADDR, data_array, 2, dev);
- }
- return rslt;
- }
- /*!
- * @brief This API enables the flat interrupt.
- */
- static int8_t enable_flat_int(const struct bmi160_acc_flat_detect_int_cfg *flat_int, const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data = 0;
- uint8_t temp = 0;
- /* Enable flat interrupt in Int Enable 0 register */
- rslt = bmi160_get_regs(BMI160_INT_ENABLE_0_ADDR, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- temp = data & ~BMI160_FLAT_INT_EN_MASK;
- data = temp | ((flat_int->flat_en << 7) & BMI160_FLAT_INT_EN_MASK);
- /* write data to Int Enable 0 register */
- rslt = bmi160_set_regs(BMI160_INT_ENABLE_0_ADDR, &data, 1, dev);
- }
- return rslt;
- }
- /*!
- * @brief This API configure the necessary setting of flat interrupt.
- */
- static int8_t config_flat_int_settg(const struct bmi160_acc_flat_detect_int_cfg *flat_int, const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data = 0;
- uint8_t temp = 0;
- uint8_t data_array[2] = { 0, 0 };
- /* Configuring INT_FLAT register */
- rslt = bmi160_get_regs(BMI160_INT_FLAT_0_ADDR, data_array, 2, dev);
- if (rslt == BMI160_OK)
- {
- data = data_array[0];
- temp = data & ~BMI160_FLAT_THRES_MASK;
- /* Adding flat theta */
- data = temp | ((flat_int->flat_theta) & BMI160_FLAT_THRES_MASK);
- data_array[0] = data;
- data = data_array[1];
- temp = data & ~BMI160_FLAT_HOLD_TIME_MASK;
- /* Adding flat hold time */
- data = temp | ((flat_int->flat_hold_time << 4) & BMI160_FLAT_HOLD_TIME_MASK);
- temp = data & ~BMI160_FLAT_HYST_MASK;
- /* Adding flat hysteresis */
- data = temp | ((flat_int->flat_hy) & BMI160_FLAT_HYST_MASK);
- data_array[1] = data;
- /* Writing data to INT_FLAT 0 and INT_FLAT 1
- * registers simultaneously */
- rslt = bmi160_set_regs(BMI160_INT_FLAT_0_ADDR, data_array, 2, dev);
- }
- return rslt;
- }
- /*!
- * @brief This API enables the Low-g interrupt.
- */
- static int8_t enable_low_g_int(const struct bmi160_acc_low_g_int_cfg *low_g_int, const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data = 0;
- uint8_t temp = 0;
- /* Enable low-g interrupt in Int Enable 1 register */
- rslt = bmi160_get_regs(BMI160_INT_ENABLE_1_ADDR, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- temp = data & ~BMI160_LOW_G_INT_EN_MASK;
- data = temp | ((low_g_int->low_en << 3) & BMI160_LOW_G_INT_EN_MASK);
- /* write data to Int Enable 0 register */
- rslt = bmi160_set_regs(BMI160_INT_ENABLE_1_ADDR, &data, 1, dev);
- }
- return rslt;
- }
- /*!
- * @brief This API configure the source of data(filter & pre-filter)
- * for low-g interrupt.
- */
- static int8_t config_low_g_data_src(const struct bmi160_acc_low_g_int_cfg *low_g_int, const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data = 0;
- uint8_t temp = 0;
- /* Configure Int data 0 register to add source of interrupt */
- rslt = bmi160_get_regs(BMI160_INT_DATA_0_ADDR, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- temp = data & ~BMI160_LOW_HIGH_SRC_INT_MASK;
- data = temp | ((low_g_int->low_data_src << 7) & BMI160_LOW_HIGH_SRC_INT_MASK);
- /* Write data to Data 0 address */
- rslt = bmi160_set_regs(BMI160_INT_DATA_0_ADDR, &data, 1, dev);
- }
- return rslt;
- }
- /*!
- * @brief This API configure the necessary setting of low-g interrupt.
- */
- static int8_t config_low_g_int_settg(const struct bmi160_acc_low_g_int_cfg *low_g_int, const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t temp = 0;
- uint8_t data_array[3] = { 0, 0, 0 };
- /* Configuring INT_LOWHIGH register for low-g interrupt */
- rslt = bmi160_get_regs(BMI160_INT_LOWHIGH_2_ADDR, &data_array[2], 1, dev);
- if (rslt == BMI160_OK)
- {
- temp = data_array[2] & ~BMI160_LOW_G_HYST_MASK;
- /* Adding low-g hysteresis */
- data_array[2] = temp | (low_g_int->low_hyst & BMI160_LOW_G_HYST_MASK);
- temp = data_array[2] & ~BMI160_LOW_G_LOW_MODE_MASK;
- /* Adding low-mode */
- data_array[2] = temp | ((low_g_int->low_mode << 2) & BMI160_LOW_G_LOW_MODE_MASK);
- /* Adding low-g threshold */
- data_array[1] = low_g_int->low_thres;
- /* Adding low-g interrupt delay */
- data_array[0] = low_g_int->low_dur;
- /* Writing data to INT_LOWHIGH 0,1,2 registers simultaneously*/
- rslt = bmi160_set_regs(BMI160_INT_LOWHIGH_0_ADDR, data_array, 3, dev);
- }
- return rslt;
- }
- /*!
- * @brief This API enables the high-g interrupt.
- */
- static int8_t enable_high_g_int(const struct bmi160_acc_high_g_int_cfg *high_g_int_cfg, const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data = 0;
- uint8_t temp = 0;
- /* Enable low-g interrupt in Int Enable 1 register */
- rslt = bmi160_get_regs(BMI160_INT_ENABLE_1_ADDR, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- /* Adding high-g X-axis */
- temp = data & ~BMI160_HIGH_G_X_INT_EN_MASK;
- data = temp | (high_g_int_cfg->high_g_x & BMI160_HIGH_G_X_INT_EN_MASK);
- /* Adding high-g Y-axis */
- temp = data & ~BMI160_HIGH_G_Y_INT_EN_MASK;
- data = temp | ((high_g_int_cfg->high_g_y << 1) & BMI160_HIGH_G_Y_INT_EN_MASK);
- /* Adding high-g Z-axis */
- temp = data & ~BMI160_HIGH_G_Z_INT_EN_MASK;
- data = temp | ((high_g_int_cfg->high_g_z << 2) & BMI160_HIGH_G_Z_INT_EN_MASK);
- /* write data to Int Enable 0 register */
- rslt = bmi160_set_regs(BMI160_INT_ENABLE_1_ADDR, &data, 1, dev);
- }
- return rslt;
- }
- /*!
- * @brief This API configure the source of data(filter & pre-filter)
- * for high-g interrupt.
- */
- static int8_t config_high_g_data_src(const struct bmi160_acc_high_g_int_cfg *high_g_int_cfg,
- const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data = 0;
- uint8_t temp = 0;
- /* Configure Int data 0 register to add source of interrupt */
- rslt = bmi160_get_regs(BMI160_INT_DATA_0_ADDR, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- temp = data & ~BMI160_LOW_HIGH_SRC_INT_MASK;
- data = temp | ((high_g_int_cfg->high_data_src << 7) & BMI160_LOW_HIGH_SRC_INT_MASK);
- /* Write data to Data 0 address */
- rslt = bmi160_set_regs(BMI160_INT_DATA_0_ADDR, &data, 1, dev);
- }
- return rslt;
- }
- /*!
- * @brief This API configure the necessary setting of high-g interrupt.
- */
- static int8_t config_high_g_int_settg(const struct bmi160_acc_high_g_int_cfg *high_g_int_cfg,
- const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t temp = 0;
- uint8_t data_array[3] = { 0, 0, 0 };
- rslt = bmi160_get_regs(BMI160_INT_LOWHIGH_2_ADDR, &data_array[0], 1, dev);
- if (rslt == BMI160_OK)
- {
- temp = data_array[0] & ~BMI160_HIGH_G_HYST_MASK;
- /* Adding high-g hysteresis */
- data_array[0] = temp | ((high_g_int_cfg->high_hy << 6) & BMI160_HIGH_G_HYST_MASK);
- /* Adding high-g duration */
- data_array[1] = high_g_int_cfg->high_dur;
- /* Adding high-g threshold */
- data_array[2] = high_g_int_cfg->high_thres;
- rslt = bmi160_set_regs(BMI160_INT_LOWHIGH_2_ADDR, data_array, 3, dev);
- }
- return rslt;
- }
- /*!
- * @brief This API configure the behavioural setting of interrupt pin.
- */
- static int8_t config_int_out_ctrl(const struct bmi160_int_settg *int_config, const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t temp = 0;
- uint8_t data = 0;
- /* Configuration of output interrupt signals on pins INT1 and INT2 are
- * done in BMI160_INT_OUT_CTRL_ADDR register*/
- rslt = bmi160_get_regs(BMI160_INT_OUT_CTRL_ADDR, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- /* updating the interrupt pin structure to local structure */
- const struct bmi160_int_pin_settg *intr_pin_sett = &(int_config->int_pin_settg);
- /* Configuring channel 1 */
- if (int_config->int_channel == BMI160_INT_CHANNEL_1)
- {
- /* Output enable */
- temp = data & ~BMI160_INT1_OUTPUT_EN_MASK;
- data = temp | ((intr_pin_sett->output_en << 3) & BMI160_INT1_OUTPUT_EN_MASK);
- /* Output mode */
- temp = data & ~BMI160_INT1_OUTPUT_MODE_MASK;
- data = temp | ((intr_pin_sett->output_mode << 2) & BMI160_INT1_OUTPUT_MODE_MASK);
- /* Output type */
- temp = data & ~BMI160_INT1_OUTPUT_TYPE_MASK;
- data = temp | ((intr_pin_sett->output_type << 1) & BMI160_INT1_OUTPUT_TYPE_MASK);
- /* edge control */
- temp = data & ~BMI160_INT1_EDGE_CTRL_MASK;
- data = temp | ((intr_pin_sett->edge_ctrl) & BMI160_INT1_EDGE_CTRL_MASK);
- }
- else
- {
- /* Configuring channel 2 */
- /* Output enable */
- temp = data & ~BMI160_INT2_OUTPUT_EN_MASK;
- data = temp | ((intr_pin_sett->output_en << 7) & BMI160_INT2_OUTPUT_EN_MASK);
- /* Output mode */
- temp = data & ~BMI160_INT2_OUTPUT_MODE_MASK;
- data = temp | ((intr_pin_sett->output_mode << 6) & BMI160_INT2_OUTPUT_MODE_MASK);
- /* Output type */
- temp = data & ~BMI160_INT2_OUTPUT_TYPE_MASK;
- data = temp | ((intr_pin_sett->output_type << 5) & BMI160_INT2_OUTPUT_TYPE_MASK);
- /* edge control */
- temp = data & ~BMI160_INT2_EDGE_CTRL_MASK;
- data = temp | ((intr_pin_sett->edge_ctrl << 4) & BMI160_INT2_EDGE_CTRL_MASK);
- }
- rslt = bmi160_set_regs(BMI160_INT_OUT_CTRL_ADDR, &data, 1, dev);
- }
- return rslt;
- }
- /*!
- * @brief This API configure the mode(input enable, latch or non-latch) of interrupt pin.
- */
- static int8_t config_int_latch(const struct bmi160_int_settg *int_config, const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t temp = 0;
- uint8_t data = 0;
- /* Configuration of latch on pins INT1 and INT2 are done in
- * BMI160_INT_LATCH_ADDR register*/
- rslt = bmi160_get_regs(BMI160_INT_LATCH_ADDR, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- /* updating the interrupt pin structure to local structure */
- const struct bmi160_int_pin_settg *intr_pin_sett = &(int_config->int_pin_settg);
- if (int_config->int_channel == BMI160_INT_CHANNEL_1)
- {
- /* Configuring channel 1 */
- /* Input enable */
- temp = data & ~BMI160_INT1_INPUT_EN_MASK;
- data = temp | ((intr_pin_sett->input_en << 4) & BMI160_INT1_INPUT_EN_MASK);
- }
- else
- {
- /* Configuring channel 2 */
- /* Input enable */
- temp = data & ~BMI160_INT2_INPUT_EN_MASK;
- data = temp | ((intr_pin_sett->input_en << 5) & BMI160_INT2_INPUT_EN_MASK);
- }
- /* In case of latch interrupt,update the latch duration */
- /* Latching holds the interrupt for the amount of latch
- * duration time */
- temp = data & ~BMI160_INT_LATCH_MASK;
- data = temp | (intr_pin_sett->latch_dur & BMI160_INT_LATCH_MASK);
- /* OUT_CTRL_INT and LATCH_INT address lie consecutively,
- * hence writing data to respective registers at one go */
- rslt = bmi160_set_regs(BMI160_INT_LATCH_ADDR, &data, 1, dev);
- }
- return rslt;
- }
- /*!
- * @brief This API performs the self test for accelerometer of BMI160
- */
- static int8_t perform_accel_self_test(struct bmi160_dev *dev)
- {
- int8_t rslt;
- struct bmi160_sensor_data accel_pos, accel_neg;
- /* Enable Gyro self test bit */
- rslt = enable_accel_self_test(dev);
- if (rslt == BMI160_OK)
- {
- /* Perform accel self test with positive excitation */
- rslt = accel_self_test_positive_excitation(&accel_pos, dev);
- if (rslt == BMI160_OK)
- {
- /* Perform accel self test with negative excitation */
- rslt = accel_self_test_negative_excitation(&accel_neg, dev);
- if (rslt == BMI160_OK)
- {
- /* Validate the self test result */
- rslt = validate_accel_self_test(&accel_pos, &accel_neg);
- }
- }
- }
- return rslt;
- }
- /*!
- * @brief This API enables to perform the accel self test by setting proper
- * configurations to facilitate accel self test
- */
- static int8_t enable_accel_self_test(struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t reg_data;
- /* Set the Accel power mode as normal mode */
- dev->accel_cfg.power = BMI160_ACCEL_NORMAL_MODE;
- /* Set the sensor range configuration as 8G */
- dev->accel_cfg.range = BMI160_ACCEL_RANGE_8G;
- rslt = bmi160_set_sens_conf(dev);
- if (rslt == BMI160_OK)
- {
- /* Accel configurations are set to facilitate self test
- * acc_odr - 1600Hz ; acc_bwp = 2 ; acc_us = 0 */
- reg_data = BMI160_ACCEL_SELF_TEST_CONFIG;
- rslt = bmi160_set_regs(BMI160_ACCEL_CONFIG_ADDR, ®_data, 1, dev);
- }
- return rslt;
- }
- /*!
- * @brief This API performs accel self test with positive excitation
- */
- static int8_t accel_self_test_positive_excitation(struct bmi160_sensor_data *accel_pos, const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t reg_data;
- /* Enable accel self test with positive self-test excitation
- * and with amplitude of deflection set as high */
- reg_data = BMI160_ACCEL_SELF_TEST_POSITIVE_EN;
- rslt = bmi160_set_regs(BMI160_SELF_TEST_ADDR, ®_data, 1, dev);
- if (rslt == BMI160_OK)
- {
- /* Read the data after a delay of 50ms - refer datasheet 2.8.1 accel self test*/
- dev->delay_ms(BMI160_ACCEL_SELF_TEST_DELAY);
- rslt = bmi160_get_sensor_data(BMI160_ACCEL_ONLY, accel_pos, NULL, dev);
- }
- return rslt;
- }
- /*!
- * @brief This API performs accel self test with negative excitation
- */
- static int8_t accel_self_test_negative_excitation(struct bmi160_sensor_data *accel_neg, const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t reg_data;
- /* Enable accel self test with negative self-test excitation
- * and with amplitude of deflection set as high */
- reg_data = BMI160_ACCEL_SELF_TEST_NEGATIVE_EN;
- rslt = bmi160_set_regs(BMI160_SELF_TEST_ADDR, ®_data, 1, dev);
- if (rslt == BMI160_OK)
- {
- /* Read the data after a delay of 50ms */
- dev->delay_ms(BMI160_ACCEL_SELF_TEST_DELAY);
- rslt = bmi160_get_sensor_data(BMI160_ACCEL_ONLY, accel_neg, NULL, dev);
- }
- return rslt;
- }
- /*!
- * @brief This API validates the accel self test results
- */
- static int8_t validate_accel_self_test(const struct bmi160_sensor_data *accel_pos,
- const struct bmi160_sensor_data *accel_neg)
- {
- int8_t rslt;
- /* Validate the results of self test */
- if (((accel_neg->x - accel_pos->x) > BMI160_ACCEL_SELF_TEST_LIMIT) &&
- ((accel_neg->y - accel_pos->y) > BMI160_ACCEL_SELF_TEST_LIMIT) &&
- ((accel_neg->z - accel_pos->z) > BMI160_ACCEL_SELF_TEST_LIMIT))
- {
- /* Self test pass condition */
- rslt = BMI160_OK;
- }
- else
- {
- rslt = BMI160_W_ACCEl_SELF_TEST_FAIL;
- }
- return rslt;
- }
- /*!
- * @brief This API performs the self test for gyroscope of BMI160
- */
- static int8_t perform_gyro_self_test(const struct bmi160_dev *dev)
- {
- int8_t rslt;
- /* Enable Gyro self test bit */
- rslt = enable_gyro_self_test(dev);
- if (rslt == BMI160_OK)
- {
- /* Validate the gyro self test a delay of 50ms */
- dev->delay_ms(50);
- /* Validate the gyro self test results */
- rslt = validate_gyro_self_test(dev);
- }
- return rslt;
- }
- /*!
- * @brief This API enables the self test bit to trigger self test for Gyro
- */
- static int8_t enable_gyro_self_test(const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t reg_data;
- /* Enable the Gyro self test bit to trigger the self test */
- rslt = bmi160_get_regs(BMI160_SELF_TEST_ADDR, ®_data, 1, dev);
- if (rslt == BMI160_OK)
- {
- reg_data = BMI160_SET_BITS(reg_data, BMI160_GYRO_SELF_TEST, 1);
- rslt = bmi160_set_regs(BMI160_SELF_TEST_ADDR, ®_data, 1, dev);
- if (rslt == BMI160_OK)
- {
- /* Delay to enable gyro self test */
- dev->delay_ms(15);
- }
- }
- return rslt;
- }
- /*!
- * @brief This API validates the self test results of Gyro
- */
- static int8_t validate_gyro_self_test(const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t reg_data;
- /* Validate the Gyro self test result */
- rslt = bmi160_get_regs(BMI160_STATUS_ADDR, ®_data, 1, dev);
- if (rslt == BMI160_OK)
- {
- reg_data = BMI160_GET_BITS(reg_data, BMI160_GYRO_SELF_TEST_STATUS);
- if (reg_data == BMI160_ENABLE)
- {
- /* Gyro self test success case */
- rslt = BMI160_OK;
- }
- else
- {
- rslt = BMI160_W_GYRO_SELF_TEST_FAIL;
- }
- }
- return rslt;
- }
- /*!
- * @brief This API sets FIFO full interrupt of the sensor.This interrupt
- * occurs when the FIFO is full and the next full data sample would cause
- * a FIFO overflow, which may delete the old samples.
- */
- static int8_t set_fifo_full_int(const struct bmi160_int_settg *int_config, const struct bmi160_dev *dev)
- {
- int8_t rslt = BMI160_OK;
- /* Null-pointer check */
- if ((dev == NULL) || (dev->delay_ms == NULL))
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- /*enable the fifo full interrupt */
- rslt = enable_fifo_full_int(int_config, dev);
- if (rslt == BMI160_OK)
- {
- /* Configure Interrupt pins */
- rslt = set_intr_pin_config(int_config, dev);
- if (rslt == BMI160_OK)
- {
- rslt = map_hardware_interrupt(int_config, dev);
- }
- }
- }
- return rslt;
- }
- /*!
- * @brief This enable the FIFO full interrupt engine.
- */
- static int8_t enable_fifo_full_int(const struct bmi160_int_settg *int_config, const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data = 0;
- rslt = bmi160_get_regs(BMI160_INT_ENABLE_1_ADDR, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- data = BMI160_SET_BITS(data, BMI160_FIFO_FULL_INT, int_config->fifo_full_int_en);
- /* Writing data to INT ENABLE 1 Address */
- rslt = bmi160_set_regs(BMI160_INT_ENABLE_1_ADDR, &data, 1, dev);
- }
- return rslt;
- }
- /*!
- * @brief This API sets FIFO watermark interrupt of the sensor.The FIFO
- * watermark interrupt is fired, when the FIFO fill level is above a fifo
- * watermark.
- */
- static int8_t set_fifo_watermark_int(const struct bmi160_int_settg *int_config, const struct bmi160_dev *dev)
- {
- int8_t rslt = BMI160_OK;
- if ((dev == NULL) || (dev->delay_ms == NULL))
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- /* Enable fifo-watermark interrupt in Int Enable 1 register */
- rslt = enable_fifo_wtm_int(int_config, dev);
- if (rslt == BMI160_OK)
- {
- /* Configure Interrupt pins */
- rslt = set_intr_pin_config(int_config, dev);
- if (rslt == BMI160_OK)
- {
- rslt = map_hardware_interrupt(int_config, dev);
- }
- }
- }
- return rslt;
- }
- /*!
- * @brief This enable the FIFO watermark interrupt engine.
- */
- static int8_t enable_fifo_wtm_int(const struct bmi160_int_settg *int_config, const struct bmi160_dev *dev)
- {
- int8_t rslt;
- uint8_t data = 0;
- rslt = bmi160_get_regs(BMI160_INT_ENABLE_1_ADDR, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- data = BMI160_SET_BITS(data, BMI160_FIFO_WTM_INT, int_config->fifo_wtm_int_en);
- /* Writing data to INT ENABLE 1 Address */
- rslt = bmi160_set_regs(BMI160_INT_ENABLE_1_ADDR, &data, 1, dev);
- }
- return rslt;
- }
- /*!
- * @brief This API is used to reset the FIFO related configurations
- * in the fifo_frame structure.
- */
- static void reset_fifo_data_structure(const struct bmi160_dev *dev)
- {
- /*Prepare for next FIFO read by resetting FIFO's
- * internal data structures*/
- dev->fifo->accel_byte_start_idx = 0;
- dev->fifo->gyro_byte_start_idx = 0;
- dev->fifo->aux_byte_start_idx = 0;
- dev->fifo->sensor_time = 0;
- dev->fifo->skipped_frame_count = 0;
- }
- /*!
- * @brief This API is used to read fifo_byte_counter value (i.e)
- * current fill-level in Fifo buffer.
- */
- static int8_t get_fifo_byte_counter(uint16_t *bytes_to_read, struct bmi160_dev const *dev)
- {
- int8_t rslt = 0;
- uint8_t data[2];
- uint8_t addr = BMI160_FIFO_LENGTH_ADDR;
- rslt |= bmi160_get_regs(addr, data, 2, dev);
- data[1] = data[1] & BMI160_FIFO_BYTE_COUNTER_MASK;
- /* Available data in FIFO is stored in bytes_to_read*/
- *bytes_to_read = (((uint16_t)data[1] << 8) | ((uint16_t)data[0]));
- return rslt;
- }
- /*!
- * @brief This API is used to compute the number of bytes of accel FIFO data
- * which is to be parsed in header-less mode
- */
- static void get_accel_len_to_parse(uint16_t *data_index,
- uint16_t *data_read_length,
- const uint8_t *acc_frame_count,
- const struct bmi160_dev *dev)
- {
- /* Data start index */
- *data_index = dev->fifo->accel_byte_start_idx;
- if (dev->fifo->fifo_data_enable == BMI160_FIFO_A_ENABLE)
- {
- *data_read_length = (*acc_frame_count) * BMI160_FIFO_A_LENGTH;
- }
- else if (dev->fifo->fifo_data_enable == BMI160_FIFO_G_A_ENABLE)
- {
- *data_read_length = (*acc_frame_count) * BMI160_FIFO_GA_LENGTH;
- }
- else if (dev->fifo->fifo_data_enable == BMI160_FIFO_M_A_ENABLE)
- {
- *data_read_length = (*acc_frame_count) * BMI160_FIFO_MA_LENGTH;
- }
- else if (dev->fifo->fifo_data_enable == BMI160_FIFO_M_G_A_ENABLE)
- {
- *data_read_length = (*acc_frame_count) * BMI160_FIFO_MGA_LENGTH;
- }
- else
- {
- /* When accel is not enabled ,there will be no accel data.
- * so we update the data index as complete */
- *data_index = dev->fifo->length;
- }
- if (*data_read_length > dev->fifo->length)
- {
- /* Handling the case where more data is requested
- * than that is available*/
- *data_read_length = dev->fifo->length;
- }
- }
- /*!
- * @brief This API is used to parse the accelerometer data from the
- * FIFO data in both header mode and header-less mode.
- * It updates the idx value which is used to store the index of
- * the current data byte which is parsed.
- */
- static void unpack_accel_frame(struct bmi160_sensor_data *acc,
- uint16_t *idx,
- uint8_t *acc_idx,
- uint8_t frame_info,
- const struct bmi160_dev *dev)
- {
- switch (frame_info)
- {
- case BMI160_FIFO_HEAD_A:
- case BMI160_FIFO_A_ENABLE:
- /*Partial read, then skip the data*/
- if ((*idx + BMI160_FIFO_A_LENGTH) > dev->fifo->length)
- {
- /*Update the data index as complete*/
- *idx = dev->fifo->length;
- break;
- }
- /*Unpack the data array into the structure instance "acc" */
- unpack_accel_data(&acc[*acc_idx], *idx, dev);
- /*Move the data index*/
- *idx = *idx + BMI160_FIFO_A_LENGTH;
- (*acc_idx)++;
- break;
- case BMI160_FIFO_HEAD_G_A:
- case BMI160_FIFO_G_A_ENABLE:
- /*Partial read, then skip the data*/
- if ((*idx + BMI160_FIFO_GA_LENGTH) > dev->fifo->length)
- {
- /*Update the data index as complete*/
- *idx = dev->fifo->length;
- break;
- }
- /*Unpack the data array into structure instance "acc"*/
- unpack_accel_data(&acc[*acc_idx], *idx + BMI160_FIFO_G_LENGTH, dev);
- /*Move the data index*/
- *idx = *idx + BMI160_FIFO_GA_LENGTH;
- (*acc_idx)++;
- break;
- case BMI160_FIFO_HEAD_M_A:
- case BMI160_FIFO_M_A_ENABLE:
- /*Partial read, then skip the data*/
- if ((*idx + BMI160_FIFO_MA_LENGTH) > dev->fifo->length)
- {
- /*Update the data index as complete*/
- *idx = dev->fifo->length;
- break;
- }
- /*Unpack the data array into structure instance "acc"*/
- unpack_accel_data(&acc[*acc_idx], *idx + BMI160_FIFO_M_LENGTH, dev);
- /*Move the data index*/
- *idx = *idx + BMI160_FIFO_MA_LENGTH;
- (*acc_idx)++;
- break;
- case BMI160_FIFO_HEAD_M_G_A:
- case BMI160_FIFO_M_G_A_ENABLE:
- /*Partial read, then skip the data*/
- if ((*idx + BMI160_FIFO_MGA_LENGTH) > dev->fifo->length)
- {
- /*Update the data index as complete*/
- *idx = dev->fifo->length;
- break;
- }
- /*Unpack the data array into structure instance "acc"*/
- unpack_accel_data(&acc[*acc_idx], *idx + BMI160_FIFO_MG_LENGTH, dev);
- /*Move the data index*/
- *idx = *idx + BMI160_FIFO_MGA_LENGTH;
- (*acc_idx)++;
- break;
- case BMI160_FIFO_HEAD_M:
- case BMI160_FIFO_M_ENABLE:
- (*idx) = (*idx) + BMI160_FIFO_M_LENGTH;
- break;
- case BMI160_FIFO_HEAD_G:
- case BMI160_FIFO_G_ENABLE:
- (*idx) = (*idx) + BMI160_FIFO_G_LENGTH;
- break;
- case BMI160_FIFO_HEAD_M_G:
- case BMI160_FIFO_M_G_ENABLE:
- (*idx) = (*idx) + BMI160_FIFO_MG_LENGTH;
- break;
- default:
- break;
- }
- }
- /*!
- * @brief This API is used to parse the accelerometer data from the
- * FIFO data and store it in the instance of the structure bmi160_sensor_data.
- */
- static void unpack_accel_data(struct bmi160_sensor_data *accel_data,
- uint16_t data_start_index,
- const struct bmi160_dev *dev)
- {
- uint16_t data_lsb;
- uint16_t data_msb;
- /* Accel raw x data */
- data_lsb = dev->fifo->data[data_start_index++];
- data_msb = dev->fifo->data[data_start_index++];
- accel_data->x = (int16_t)((data_msb << 8) | data_lsb);
- /* Accel raw y data */
- data_lsb = dev->fifo->data[data_start_index++];
- data_msb = dev->fifo->data[data_start_index++];
- accel_data->y = (int16_t)((data_msb << 8) | data_lsb);
- /* Accel raw z data */
- data_lsb = dev->fifo->data[data_start_index++];
- data_msb = dev->fifo->data[data_start_index++];
- accel_data->z = (int16_t)((data_msb << 8) | data_lsb);
- }
- /*!
- * @brief This API is used to parse the accelerometer data from the
- * FIFO data in header mode.
- */
- static void extract_accel_header_mode(struct bmi160_sensor_data *accel_data,
- uint8_t *accel_length,
- const struct bmi160_dev *dev)
- {
- uint8_t frame_header = 0;
- uint16_t data_index;
- uint8_t accel_index = 0;
- for (data_index = dev->fifo->accel_byte_start_idx; data_index < dev->fifo->length;)
- {
- /* extracting Frame header */
- frame_header = (dev->fifo->data[data_index] & BMI160_FIFO_TAG_INTR_MASK);
- /*Index is moved to next byte where the data is starting*/
- data_index++;
- switch (frame_header)
- {
- /* Accel frame */
- case BMI160_FIFO_HEAD_A:
- case BMI160_FIFO_HEAD_M_A:
- case BMI160_FIFO_HEAD_G_A:
- case BMI160_FIFO_HEAD_M_G_A:
- unpack_accel_frame(accel_data, &data_index, &accel_index, frame_header, dev);
- break;
- case BMI160_FIFO_HEAD_M:
- move_next_frame(&data_index, BMI160_FIFO_M_LENGTH, dev);
- break;
- case BMI160_FIFO_HEAD_G:
- move_next_frame(&data_index, BMI160_FIFO_G_LENGTH, dev);
- break;
- case BMI160_FIFO_HEAD_M_G:
- move_next_frame(&data_index, BMI160_FIFO_MG_LENGTH, dev);
- break;
- /* Sensor time frame */
- case BMI160_FIFO_HEAD_SENSOR_TIME:
- unpack_sensortime_frame(&data_index, dev);
- break;
- /* Skip frame */
- case BMI160_FIFO_HEAD_SKIP_FRAME:
- unpack_skipped_frame(&data_index, dev);
- break;
- /* Input config frame */
- case BMI160_FIFO_HEAD_INPUT_CONFIG:
- move_next_frame(&data_index, 1, dev);
- break;
- case BMI160_FIFO_HEAD_OVER_READ:
- /* Update the data index as complete in case of Over read */
- data_index = dev->fifo->length;
- break;
- default:
- break;
- }
- if (*accel_length == accel_index)
- {
- /* Number of frames to read completed */
- break;
- }
- }
- /*Update number of accel data read*/
- *accel_length = accel_index;
- /*Update the accel frame index*/
- dev->fifo->accel_byte_start_idx = data_index;
- }
- /*!
- * @brief This API computes the number of bytes of gyro FIFO data
- * which is to be parsed in header-less mode
- */
- static void get_gyro_len_to_parse(uint16_t *data_index,
- uint16_t *data_read_length,
- const uint8_t *gyro_frame_count,
- const struct bmi160_dev *dev)
- {
- /* Data start index */
- *data_index = dev->fifo->gyro_byte_start_idx;
- if (dev->fifo->fifo_data_enable == BMI160_FIFO_G_ENABLE)
- {
- *data_read_length = (*gyro_frame_count) * BMI160_FIFO_G_LENGTH;
- }
- else if (dev->fifo->fifo_data_enable == BMI160_FIFO_G_A_ENABLE)
- {
- *data_read_length = (*gyro_frame_count) * BMI160_FIFO_GA_LENGTH;
- }
- else if (dev->fifo->fifo_data_enable == BMI160_FIFO_M_G_ENABLE)
- {
- *data_read_length = (*gyro_frame_count) * BMI160_FIFO_MG_LENGTH;
- }
- else if (dev->fifo->fifo_data_enable == BMI160_FIFO_M_G_A_ENABLE)
- {
- *data_read_length = (*gyro_frame_count) * BMI160_FIFO_MGA_LENGTH;
- }
- else
- {
- /* When gyro is not enabled ,there will be no gyro data.
- * so we update the data index as complete */
- *data_index = dev->fifo->length;
- }
- if (*data_read_length > dev->fifo->length)
- {
- /* Handling the case where more data is requested
- * than that is available*/
- *data_read_length = dev->fifo->length;
- }
- }
- /*!
- * @brief This API is used to parse the gyroscope's data from the
- * FIFO data in both header mode and header-less mode.
- * It updates the idx value which is used to store the index of
- * the current data byte which is parsed.
- */
- static void unpack_gyro_frame(struct bmi160_sensor_data *gyro,
- uint16_t *idx,
- uint8_t *gyro_idx,
- uint8_t frame_info,
- const struct bmi160_dev *dev)
- {
- switch (frame_info)
- {
- case BMI160_FIFO_HEAD_G:
- case BMI160_FIFO_G_ENABLE:
- /*Partial read, then skip the data*/
- if ((*idx + BMI160_FIFO_G_LENGTH) > dev->fifo->length)
- {
- /*Update the data index as complete*/
- *idx = dev->fifo->length;
- break;
- }
- /*Unpack the data array into structure instance "gyro"*/
- unpack_gyro_data(&gyro[*gyro_idx], *idx, dev);
- /*Move the data index*/
- (*idx) = (*idx) + BMI160_FIFO_G_LENGTH;
- (*gyro_idx)++;
- break;
- case BMI160_FIFO_HEAD_G_A:
- case BMI160_FIFO_G_A_ENABLE:
- /*Partial read, then skip the data*/
- if ((*idx + BMI160_FIFO_GA_LENGTH) > dev->fifo->length)
- {
- /*Update the data index as complete*/
- *idx = dev->fifo->length;
- break;
- }
- /* Unpack the data array into structure instance "gyro" */
- unpack_gyro_data(&gyro[*gyro_idx], *idx, dev);
- /* Move the data index */
- *idx = *idx + BMI160_FIFO_GA_LENGTH;
- (*gyro_idx)++;
- break;
- case BMI160_FIFO_HEAD_M_G_A:
- case BMI160_FIFO_M_G_A_ENABLE:
- /*Partial read, then skip the data*/
- if ((*idx + BMI160_FIFO_MGA_LENGTH) > dev->fifo->length)
- {
- /*Update the data index as complete*/
- *idx = dev->fifo->length;
- break;
- }
- /*Unpack the data array into structure instance "gyro"*/
- unpack_gyro_data(&gyro[*gyro_idx], *idx + BMI160_FIFO_M_LENGTH, dev);
- /*Move the data index*/
- *idx = *idx + BMI160_FIFO_MGA_LENGTH;
- (*gyro_idx)++;
- break;
- case BMI160_FIFO_HEAD_M_A:
- case BMI160_FIFO_M_A_ENABLE:
- /* Move the data index */
- *idx = *idx + BMI160_FIFO_MA_LENGTH;
- break;
- case BMI160_FIFO_HEAD_M:
- case BMI160_FIFO_M_ENABLE:
- (*idx) = (*idx) + BMI160_FIFO_M_LENGTH;
- break;
- case BMI160_FIFO_HEAD_M_G:
- case BMI160_FIFO_M_G_ENABLE:
- /*Partial read, then skip the data*/
- if ((*idx + BMI160_FIFO_MG_LENGTH) > dev->fifo->length)
- {
- /*Update the data index as complete*/
- *idx = dev->fifo->length;
- break;
- }
- /*Unpack the data array into structure instance "gyro"*/
- unpack_gyro_data(&gyro[*gyro_idx], *idx + BMI160_FIFO_M_LENGTH, dev);
- /*Move the data index*/
- (*idx) = (*idx) + BMI160_FIFO_MG_LENGTH;
- (*gyro_idx)++;
- break;
- case BMI160_FIFO_HEAD_A:
- case BMI160_FIFO_A_ENABLE:
- /*Move the data index*/
- *idx = *idx + BMI160_FIFO_A_LENGTH;
- break;
- default:
- break;
- }
- }
- /*!
- * @brief This API is used to parse the gyro data from the
- * FIFO data and store it in the instance of the structure bmi160_sensor_data.
- */
- static void unpack_gyro_data(struct bmi160_sensor_data *gyro_data,
- uint16_t data_start_index,
- const struct bmi160_dev *dev)
- {
- uint16_t data_lsb;
- uint16_t data_msb;
- /* Gyro raw x data */
- data_lsb = dev->fifo->data[data_start_index++];
- data_msb = dev->fifo->data[data_start_index++];
- gyro_data->x = (int16_t)((data_msb << 8) | data_lsb);
- /* Gyro raw y data */
- data_lsb = dev->fifo->data[data_start_index++];
- data_msb = dev->fifo->data[data_start_index++];
- gyro_data->y = (int16_t)((data_msb << 8) | data_lsb);
- /* Gyro raw z data */
- data_lsb = dev->fifo->data[data_start_index++];
- data_msb = dev->fifo->data[data_start_index++];
- gyro_data->z = (int16_t)((data_msb << 8) | data_lsb);
- }
- /*!
- * @brief This API is used to parse the gyro data from the
- * FIFO data in header mode.
- */
- static void extract_gyro_header_mode(struct bmi160_sensor_data *gyro_data,
- uint8_t *gyro_length,
- const struct bmi160_dev *dev)
- {
- uint8_t frame_header = 0;
- uint16_t data_index;
- uint8_t gyro_index = 0;
- for (data_index = dev->fifo->gyro_byte_start_idx; data_index < dev->fifo->length;)
- {
- /* extracting Frame header */
- frame_header = (dev->fifo->data[data_index] & BMI160_FIFO_TAG_INTR_MASK);
- /*Index is moved to next byte where the data is starting*/
- data_index++;
- switch (frame_header)
- {
- /* GYRO frame */
- case BMI160_FIFO_HEAD_G:
- case BMI160_FIFO_HEAD_G_A:
- case BMI160_FIFO_HEAD_M_G:
- case BMI160_FIFO_HEAD_M_G_A:
- unpack_gyro_frame(gyro_data, &data_index, &gyro_index, frame_header, dev);
- break;
- case BMI160_FIFO_HEAD_A:
- move_next_frame(&data_index, BMI160_FIFO_A_LENGTH, dev);
- break;
- case BMI160_FIFO_HEAD_M:
- move_next_frame(&data_index, BMI160_FIFO_M_LENGTH, dev);
- break;
- case BMI160_FIFO_HEAD_M_A:
- move_next_frame(&data_index, BMI160_FIFO_M_LENGTH, dev);
- break;
- /* Sensor time frame */
- case BMI160_FIFO_HEAD_SENSOR_TIME:
- unpack_sensortime_frame(&data_index, dev);
- break;
- /* Skip frame */
- case BMI160_FIFO_HEAD_SKIP_FRAME:
- unpack_skipped_frame(&data_index, dev);
- break;
- /* Input config frame */
- case BMI160_FIFO_HEAD_INPUT_CONFIG:
- move_next_frame(&data_index, 1, dev);
- break;
- case BMI160_FIFO_HEAD_OVER_READ:
- /* Update the data index as complete in case of over read */
- data_index = dev->fifo->length;
- break;
- default:
- break;
- }
- if (*gyro_length == gyro_index)
- {
- /*Number of frames to read completed*/
- break;
- }
- }
- /*Update number of gyro data read*/
- *gyro_length = gyro_index;
- /*Update the gyro frame index*/
- dev->fifo->gyro_byte_start_idx = data_index;
- }
- /*!
- * @brief This API computes the number of bytes of aux FIFO data
- * which is to be parsed in header-less mode
- */
- static void get_aux_len_to_parse(uint16_t *data_index,
- uint16_t *data_read_length,
- const uint8_t *aux_frame_count,
- const struct bmi160_dev *dev)
- {
- /* Data start index */
- *data_index = dev->fifo->gyro_byte_start_idx;
- if (dev->fifo->fifo_data_enable == BMI160_FIFO_M_ENABLE)
- {
- *data_read_length = (*aux_frame_count) * BMI160_FIFO_M_LENGTH;
- }
- else if (dev->fifo->fifo_data_enable == BMI160_FIFO_M_A_ENABLE)
- {
- *data_read_length = (*aux_frame_count) * BMI160_FIFO_MA_LENGTH;
- }
- else if (dev->fifo->fifo_data_enable == BMI160_FIFO_M_G_ENABLE)
- {
- *data_read_length = (*aux_frame_count) * BMI160_FIFO_MG_LENGTH;
- }
- else if (dev->fifo->fifo_data_enable == BMI160_FIFO_M_G_A_ENABLE)
- {
- *data_read_length = (*aux_frame_count) * BMI160_FIFO_MGA_LENGTH;
- }
- else
- {
- /* When aux is not enabled ,there will be no aux data.
- * so we update the data index as complete */
- *data_index = dev->fifo->length;
- }
- if (*data_read_length > dev->fifo->length)
- {
- /* Handling the case where more data is requested
- * than that is available */
- *data_read_length = dev->fifo->length;
- }
- }
- /*!
- * @brief This API is used to parse the aux's data from the
- * FIFO data in both header mode and header-less mode.
- * It updates the idx value which is used to store the index of
- * the current data byte which is parsed
- */
- static void unpack_aux_frame(struct bmi160_aux_data *aux_data,
- uint16_t *idx,
- uint8_t *aux_index,
- uint8_t frame_info,
- const struct bmi160_dev *dev)
- {
- switch (frame_info)
- {
- case BMI160_FIFO_HEAD_M:
- case BMI160_FIFO_M_ENABLE:
- /* Partial read, then skip the data */
- if ((*idx + BMI160_FIFO_M_LENGTH) > dev->fifo->length)
- {
- /* Update the data index as complete */
- *idx = dev->fifo->length;
- break;
- }
- /* Unpack the data array into structure instance */
- unpack_aux_data(&aux_data[*aux_index], *idx, dev);
- /* Move the data index */
- *idx = *idx + BMI160_FIFO_M_LENGTH;
- (*aux_index)++;
- break;
- case BMI160_FIFO_HEAD_M_A:
- case BMI160_FIFO_M_A_ENABLE:
- /* Partial read, then skip the data */
- if ((*idx + BMI160_FIFO_MA_LENGTH) > dev->fifo->length)
- {
- /* Update the data index as complete */
- *idx = dev->fifo->length;
- break;
- }
- /* Unpack the data array into structure instance */
- unpack_aux_data(&aux_data[*aux_index], *idx, dev);
- /* Move the data index */
- *idx = *idx + BMI160_FIFO_MA_LENGTH;
- (*aux_index)++;
- break;
- case BMI160_FIFO_HEAD_M_G:
- case BMI160_FIFO_M_G_ENABLE:
- /* Partial read, then skip the data */
- if ((*idx + BMI160_FIFO_MG_LENGTH) > dev->fifo->length)
- {
- /* Update the data index as complete */
- *idx = dev->fifo->length;
- break;
- }
- /* Unpack the data array into structure instance */
- unpack_aux_data(&aux_data[*aux_index], *idx, dev);
- /* Move the data index */
- (*idx) = (*idx) + BMI160_FIFO_MG_LENGTH;
- (*aux_index)++;
- break;
- case BMI160_FIFO_HEAD_M_G_A:
- case BMI160_FIFO_M_G_A_ENABLE:
- /*Partial read, then skip the data*/
- if ((*idx + BMI160_FIFO_MGA_LENGTH) > dev->fifo->length)
- {
- /* Update the data index as complete */
- *idx = dev->fifo->length;
- break;
- }
- /* Unpack the data array into structure instance */
- unpack_aux_data(&aux_data[*aux_index], *idx, dev);
- /*Move the data index*/
- *idx = *idx + BMI160_FIFO_MGA_LENGTH;
- (*aux_index)++;
- break;
- case BMI160_FIFO_HEAD_G:
- case BMI160_FIFO_G_ENABLE:
- /* Move the data index */
- (*idx) = (*idx) + BMI160_FIFO_G_LENGTH;
- break;
- case BMI160_FIFO_HEAD_G_A:
- case BMI160_FIFO_G_A_ENABLE:
- /* Move the data index */
- *idx = *idx + BMI160_FIFO_GA_LENGTH;
- break;
- case BMI160_FIFO_HEAD_A:
- case BMI160_FIFO_A_ENABLE:
- /* Move the data index */
- *idx = *idx + BMI160_FIFO_A_LENGTH;
- break;
- default:
- break;
- }
- }
- /*!
- * @brief This API is used to parse the aux data from the
- * FIFO data and store it in the instance of the structure bmi160_aux_data.
- */
- static void unpack_aux_data(struct bmi160_aux_data *aux_data, uint16_t data_start_index, const struct bmi160_dev *dev)
- {
- /* Aux data bytes */
- aux_data->data[0] = dev->fifo->data[data_start_index++];
- aux_data->data[1] = dev->fifo->data[data_start_index++];
- aux_data->data[2] = dev->fifo->data[data_start_index++];
- aux_data->data[3] = dev->fifo->data[data_start_index++];
- aux_data->data[4] = dev->fifo->data[data_start_index++];
- aux_data->data[5] = dev->fifo->data[data_start_index++];
- aux_data->data[6] = dev->fifo->data[data_start_index++];
- aux_data->data[7] = dev->fifo->data[data_start_index++];
- }
- /*!
- * @brief This API is used to parse the aux data from the
- * FIFO data in header mode.
- */
- static void extract_aux_header_mode(struct bmi160_aux_data *aux_data, uint8_t *aux_length, const struct bmi160_dev *dev)
- {
- uint8_t frame_header = 0;
- uint16_t data_index;
- uint8_t aux_index = 0;
- for (data_index = dev->fifo->aux_byte_start_idx; data_index < dev->fifo->length;)
- {
- /* extracting Frame header */
- frame_header = (dev->fifo->data[data_index] & BMI160_FIFO_TAG_INTR_MASK);
- /*Index is moved to next byte where the data is starting*/
- data_index++;
- switch (frame_header)
- {
- /* Aux frame */
- case BMI160_FIFO_HEAD_M:
- case BMI160_FIFO_HEAD_M_A:
- case BMI160_FIFO_HEAD_M_G:
- case BMI160_FIFO_HEAD_M_G_A:
- unpack_aux_frame(aux_data, &data_index, &aux_index, frame_header, dev);
- break;
- case BMI160_FIFO_HEAD_G:
- move_next_frame(&data_index, BMI160_FIFO_G_LENGTH, dev);
- break;
- case BMI160_FIFO_HEAD_G_A:
- move_next_frame(&data_index, BMI160_FIFO_GA_LENGTH, dev);
- break;
- case BMI160_FIFO_HEAD_A:
- move_next_frame(&data_index, BMI160_FIFO_A_LENGTH, dev);
- break;
- /* Sensor time frame */
- case BMI160_FIFO_HEAD_SENSOR_TIME:
- unpack_sensortime_frame(&data_index, dev);
- break;
- /* Skip frame */
- case BMI160_FIFO_HEAD_SKIP_FRAME:
- unpack_skipped_frame(&data_index, dev);
- break;
- /* Input config frame */
- case BMI160_FIFO_HEAD_INPUT_CONFIG:
- move_next_frame(&data_index, 1, dev);
- break;
- case BMI160_FIFO_HEAD_OVER_READ:
- /* Update the data index as complete in case
- * of over read */
- data_index = dev->fifo->length;
- break;
- default:
- /* Update the data index as complete in case of
- * getting other headers like 0x00 */
- data_index = dev->fifo->length;
- break;
- }
- if (*aux_length == aux_index)
- {
- /*Number of frames to read completed*/
- break;
- }
- }
- /* Update number of aux data read */
- *aux_length = aux_index;
- /* Update the aux frame index */
- dev->fifo->aux_byte_start_idx = data_index;
- }
- /*!
- * @brief This API checks the presence of non-valid frames in the read fifo data.
- */
- static void check_frame_validity(uint16_t *data_index, const struct bmi160_dev *dev)
- {
- if ((*data_index + 2) < dev->fifo->length)
- {
- /* Check if FIFO is empty */
- if ((dev->fifo->data[*data_index] == FIFO_CONFIG_MSB_CHECK) &&
- (dev->fifo->data[*data_index + 1] == FIFO_CONFIG_LSB_CHECK))
- {
- /*Update the data index as complete*/
- *data_index = dev->fifo->length;
- }
- }
- }
- /*!
- * @brief This API is used to move the data index ahead of the
- * current_frame_length parameter when unnecessary FIFO data appears while
- * extracting the user specified data.
- */
- static void move_next_frame(uint16_t *data_index, uint8_t current_frame_length, const struct bmi160_dev *dev)
- {
- /*Partial read, then move the data index to last data*/
- if ((*data_index + current_frame_length) > dev->fifo->length)
- {
- /*Update the data index as complete*/
- *data_index = dev->fifo->length;
- }
- else
- {
- /*Move the data index to next frame*/
- *data_index = *data_index + current_frame_length;
- }
- }
- /*!
- * @brief This API is used to parse and store the sensor time from the
- * FIFO data in the structure instance dev.
- */
- static void unpack_sensortime_frame(uint16_t *data_index, const struct bmi160_dev *dev)
- {
- uint32_t sensor_time_byte3 = 0;
- uint16_t sensor_time_byte2 = 0;
- uint8_t sensor_time_byte1 = 0;
- /*Partial read, then move the data index to last data*/
- if ((*data_index + BMI160_SENSOR_TIME_LENGTH) > dev->fifo->length)
- {
- /*Update the data index as complete*/
- *data_index = dev->fifo->length;
- }
- else
- {
- sensor_time_byte3 = dev->fifo->data[(*data_index) + BMI160_SENSOR_TIME_MSB_BYTE] << 16;
- sensor_time_byte2 = dev->fifo->data[(*data_index) + BMI160_SENSOR_TIME_XLSB_BYTE] << 8;
- sensor_time_byte1 = dev->fifo->data[(*data_index)];
- /* Sensor time */
- dev->fifo->sensor_time = (uint32_t)(sensor_time_byte3 | sensor_time_byte2 | sensor_time_byte1);
- *data_index = (*data_index) + BMI160_SENSOR_TIME_LENGTH;
- }
- }
- /*!
- * @brief This API is used to parse and store the skipped_frame_count from
- * the FIFO data in the structure instance dev.
- */
- static void unpack_skipped_frame(uint16_t *data_index, const struct bmi160_dev *dev)
- {
- /*Partial read, then move the data index to last data*/
- if (*data_index >= dev->fifo->length)
- {
- /*Update the data index as complete*/
- *data_index = dev->fifo->length;
- }
- else
- {
- dev->fifo->skipped_frame_count = dev->fifo->data[*data_index];
- /*Move the data index*/
- *data_index = (*data_index) + 1;
- }
- }
- /*!
- * @brief This API is used to get the FOC status from the sensor
- */
- static int8_t get_foc_status(uint8_t *foc_status, struct bmi160_dev const *dev)
- {
- int8_t rslt;
- uint8_t data;
- /* Read the FOC status from sensor */
- rslt = bmi160_get_regs(BMI160_STATUS_ADDR, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- /* Get the foc_status bit */
- *foc_status = BMI160_GET_BITS(data, BMI160_FOC_STATUS);
- }
- return rslt;
- }
- /*!
- * @brief This API is used to configure the offset enable bits in the sensor
- */
- static int8_t configure_offset_enable(const struct bmi160_foc_conf *foc_conf, struct bmi160_dev const *dev)
- {
- int8_t rslt;
- uint8_t data;
- /* Null-pointer check */
- rslt = null_ptr_check(dev);
- if (rslt != BMI160_OK)
- {
- rslt = BMI160_E_NULL_PTR;
- }
- else
- {
- /* Read the FOC config from the sensor */
- rslt = bmi160_get_regs(BMI160_OFFSET_CONF_ADDR, &data, 1, dev);
- if (rslt == BMI160_OK)
- {
- /* Set the offset enable/disable for gyro */
- data = BMI160_SET_BITS(data, BMI160_GYRO_OFFSET_EN, foc_conf->gyro_off_en);
- /* Set the offset enable/disable for accel */
- data = BMI160_SET_BITS(data, BMI160_ACCEL_OFFSET_EN, foc_conf->acc_off_en);
- /* Set the offset config in the sensor */
- rslt = bmi160_set_regs(BMI160_OFFSET_CONF_ADDR, &data, 1, dev);
- }
- }
- return rslt;
- }
- static int8_t trigger_foc(struct bmi160_offsets *offset, struct bmi160_dev const *dev)
- {
- int8_t rslt;
- uint8_t foc_status = BMI160_ENABLE;
- uint8_t cmd = BMI160_START_FOC_CMD;
- uint8_t timeout = 0;
- uint8_t data_array[20];
- /* Start the FOC process */
- rslt = bmi160_set_regs(BMI160_COMMAND_REG_ADDR, &cmd, 1, dev);
- if (rslt == BMI160_OK)
- {
- /* Check the FOC status*/
- rslt = get_foc_status(&foc_status, dev);
- if ((rslt != BMI160_OK) || (foc_status != BMI160_ENABLE))
- {
- while ((foc_status != BMI160_ENABLE) && (timeout < 11))
- {
- /* Maximum time of 250ms is given in 10
- * steps of 25ms each - 250ms refer datasheet 2.9.1 */
- dev->delay_ms(25);
- /* Check the FOC status*/
- rslt = get_foc_status(&foc_status, dev);
- timeout++;
- }
- if ((rslt == BMI160_OK) && (foc_status == BMI160_ENABLE))
- {
- /* Get offset values from sensor */
- rslt = bmi160_get_offsets(offset, dev);
- }
- else
- {
- /* FOC failure case */
- rslt = BMI160_E_FOC_FAILURE;
- }
- }
- if (rslt == BMI160_OK)
- {
- /* Read registers 0x04-0x17 */
- rslt = bmi160_get_regs(BMI160_GYRO_DATA_ADDR, data_array, 20, dev);
- }
- }
- return rslt;
- }
|