bmi160.c 206 KB

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