bmi160.c 170 KB

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