bmi160.c 170 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675
  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 24 Nov 2017
  44. * @version 3.7.4
  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 try = 3;
  1312. /* Assign chip id as zero */
  1313. dev->chip_id = 0;
  1314. /* Null-pointer check */
  1315. rslt = null_ptr_check(dev);
  1316. /* Dummy read of 0x7F register to enable SPI Interface
  1317. if SPI is used */
  1318. if ((rslt == BMI160_OK) && (dev->interface == BMI160_SPI_INTF))
  1319. rslt = bmi160_get_regs(BMI160_SPI_COMM_TEST_ADDR, &data, 1, dev);
  1320. if (rslt == BMI160_OK) {
  1321. while ((try--) && (dev->chip_id != BMI160_CHIP_ID)) {
  1322. /* Read chip_id */
  1323. rslt = bmi160_get_regs(BMI160_CHIP_ID_ADDR, &dev->chip_id, 1, dev);
  1324. }
  1325. if ((rslt == BMI160_OK) && (dev->chip_id == BMI160_CHIP_ID)) {
  1326. dev->any_sig_sel = BMI160_BOTH_ANY_SIG_MOTION_DISABLED;
  1327. /* Soft reset */
  1328. rslt = bmi160_soft_reset(dev);
  1329. } else {
  1330. rslt = BMI160_E_DEV_NOT_FOUND;
  1331. }
  1332. }
  1333. return rslt;
  1334. }
  1335. /*!
  1336. * @brief This API resets and restarts the device.
  1337. * All register values are overwritten with default parameters.
  1338. */
  1339. int8_t bmi160_soft_reset(struct bmi160_dev *dev)
  1340. {
  1341. int8_t rslt;
  1342. uint8_t data = BMI160_SOFT_RESET_CMD;
  1343. /* Null-pointer check */
  1344. if ((dev == NULL) || (dev->delay_ms == NULL)) {
  1345. rslt = BMI160_E_NULL_PTR;
  1346. } else {
  1347. /* Reset the device */
  1348. rslt = bmi160_set_regs(BMI160_COMMAND_REG_ADDR, &data, 1, dev);
  1349. dev->delay_ms(BMI160_SOFT_RESET_DELAY_MS);
  1350. if ((rslt == BMI160_OK) && (dev->interface == BMI160_SPI_INTF)) {
  1351. /* Dummy read of 0x7F register to enable SPI Interface
  1352. if SPI is used */
  1353. rslt = bmi160_get_regs(BMI160_SPI_COMM_TEST_ADDR, &data, 1, dev);
  1354. }
  1355. if (rslt == BMI160_OK) {
  1356. /* Update the default parameters */
  1357. default_param_settg(dev);
  1358. }
  1359. }
  1360. return rslt;
  1361. }
  1362. /*!
  1363. * @brief This API configures the power mode, range and bandwidth
  1364. * of sensor.
  1365. */
  1366. int8_t bmi160_set_sens_conf(struct bmi160_dev *dev)
  1367. {
  1368. int8_t rslt = BMI160_OK;
  1369. /* Null-pointer check */
  1370. if ((dev == NULL) || (dev->delay_ms == NULL)) {
  1371. rslt = BMI160_E_NULL_PTR;
  1372. } else {
  1373. rslt = set_accel_conf(dev);
  1374. if (rslt == BMI160_OK) {
  1375. rslt = set_gyro_conf(dev);
  1376. if (rslt == BMI160_OK) {
  1377. /* write power mode for accel and gyro */
  1378. rslt = bmi160_set_power_mode(dev);
  1379. if (rslt == BMI160_OK)
  1380. rslt = check_invalid_settg(dev);
  1381. }
  1382. }
  1383. }
  1384. return rslt;
  1385. }
  1386. /*!
  1387. * @brief This API sets the power mode of the sensor.
  1388. */
  1389. int8_t bmi160_set_power_mode(struct bmi160_dev *dev)
  1390. {
  1391. int8_t rslt = 0;
  1392. /* Null-pointer check */
  1393. if ((dev == NULL) || (dev->delay_ms == NULL)) {
  1394. rslt = BMI160_E_NULL_PTR;
  1395. } else {
  1396. rslt = set_accel_pwr(dev);
  1397. if (rslt == BMI160_OK)
  1398. rslt = set_gyro_pwr(dev);
  1399. }
  1400. return rslt;
  1401. }
  1402. /*!
  1403. * @brief This API reads sensor data, stores it in
  1404. * the bmi160_sensor_data structure pointer passed by the user.
  1405. */
  1406. int8_t bmi160_get_sensor_data(uint8_t select_sensor, struct bmi160_sensor_data *accel, struct bmi160_sensor_data *gyro,
  1407. const struct bmi160_dev *dev)
  1408. {
  1409. int8_t rslt = BMI160_OK;
  1410. uint8_t time_sel;
  1411. uint8_t sen_sel;
  1412. uint8_t len = 0;
  1413. /*Extract the sensor and time select information*/
  1414. sen_sel = select_sensor & BMI160_SEN_SEL_MASK;
  1415. time_sel = ((sen_sel & BMI160_TIME_SEL) >> 2);
  1416. sen_sel = sen_sel & (BMI160_ACCEL_SEL | BMI160_GYRO_SEL);
  1417. if (time_sel == 1)
  1418. len = 3;
  1419. /* Null-pointer check */
  1420. if (dev != NULL) {
  1421. switch (sen_sel) {
  1422. case BMI160_ACCEL_ONLY:
  1423. /* Null-pointer check */
  1424. if (accel == NULL)
  1425. rslt = BMI160_E_NULL_PTR;
  1426. else
  1427. rslt = get_accel_data(len, accel, dev);
  1428. break;
  1429. case BMI160_GYRO_ONLY:
  1430. /* Null-pointer check */
  1431. if (gyro == NULL)
  1432. rslt = BMI160_E_NULL_PTR;
  1433. else
  1434. rslt = get_gyro_data(len, gyro, dev);
  1435. break;
  1436. case BMI160_BOTH_ACCEL_AND_GYRO:
  1437. /* Null-pointer check */
  1438. if ((gyro == NULL) || (accel == NULL))
  1439. rslt = BMI160_E_NULL_PTR;
  1440. else
  1441. rslt = get_accel_gyro_data(len, accel, gyro, dev);
  1442. break;
  1443. default:
  1444. rslt = BMI160_E_INVALID_INPUT;
  1445. break;
  1446. }
  1447. } else {
  1448. rslt = BMI160_E_NULL_PTR;
  1449. }
  1450. return rslt;
  1451. }
  1452. /*!
  1453. * @brief This API configures the necessary interrupt based on
  1454. * the user settings in the bmi160_int_settg structure instance.
  1455. */
  1456. int8_t bmi160_set_int_config(struct bmi160_int_settg *int_config, struct bmi160_dev *dev)
  1457. {
  1458. int8_t rslt = BMI160_OK;
  1459. switch (int_config->int_type) {
  1460. case BMI160_ACC_ANY_MOTION_INT:
  1461. /*Any-motion interrupt*/
  1462. rslt = set_accel_any_motion_int(int_config, dev);
  1463. break;
  1464. case BMI160_ACC_SIG_MOTION_INT:
  1465. /* Significant motion interrupt */
  1466. rslt = set_accel_sig_motion_int(int_config, dev);
  1467. break;
  1468. case BMI160_ACC_SLOW_NO_MOTION_INT:
  1469. /* Slow or no motion interrupt */
  1470. rslt = set_accel_no_motion_int(int_config, dev);
  1471. break;
  1472. case BMI160_ACC_DOUBLE_TAP_INT:
  1473. case BMI160_ACC_SINGLE_TAP_INT:
  1474. /* Double tap and single tap Interrupt */
  1475. rslt = set_accel_tap_int(int_config, dev);
  1476. break;
  1477. case BMI160_STEP_DETECT_INT:
  1478. /* Step detector interrupt */
  1479. rslt = set_accel_step_detect_int(int_config, dev);
  1480. break;
  1481. case BMI160_ACC_ORIENT_INT:
  1482. /* Orientation interrupt */
  1483. rslt = set_accel_orientation_int(int_config, dev);
  1484. break;
  1485. case BMI160_ACC_FLAT_INT:
  1486. /* Flat detection interrupt */
  1487. rslt = set_accel_flat_detect_int(int_config, dev);
  1488. break;
  1489. case BMI160_ACC_LOW_G_INT:
  1490. /* Low-g interrupt */
  1491. rslt = set_accel_low_g_int(int_config, dev);
  1492. break;
  1493. case BMI160_ACC_HIGH_G_INT:
  1494. /* High-g interrupt */
  1495. rslt = set_accel_high_g_int(int_config, dev);
  1496. break;
  1497. case BMI160_ACC_GYRO_DATA_RDY_INT:
  1498. /* Data ready interrupt */
  1499. rslt = set_accel_gyro_data_ready_int(int_config, dev);
  1500. break;
  1501. case BMI160_ACC_GYRO_FIFO_FULL_INT:
  1502. /* Fifo full interrupt */
  1503. rslt = set_fifo_full_int(int_config, dev);
  1504. break;
  1505. case BMI160_ACC_GYRO_FIFO_WATERMARK_INT:
  1506. /* Fifo water-mark interrupt */
  1507. rslt = set_fifo_watermark_int(int_config, dev);
  1508. break;
  1509. case BMI160_FIFO_TAG_INT_PIN:
  1510. /* Fifo tagging feature support */
  1511. /* Configure Interrupt pins */
  1512. rslt = set_intr_pin_config(int_config, dev);
  1513. break;
  1514. default:
  1515. break;
  1516. }
  1517. return rslt;
  1518. }
  1519. /*!
  1520. * @brief This API enables or disable the step counter feature.
  1521. * 1 - enable step counter (0 - disable)
  1522. */
  1523. int8_t bmi160_set_step_counter(uint8_t step_cnt_enable, const struct bmi160_dev *dev)
  1524. {
  1525. int8_t rslt;
  1526. uint8_t data = 0;
  1527. /* Null-pointer check */
  1528. rslt = null_ptr_check(dev);
  1529. if (rslt != BMI160_OK) {
  1530. rslt = BMI160_E_NULL_PTR;
  1531. } else {
  1532. rslt = bmi160_get_regs(BMI160_INT_STEP_CONFIG_1_ADDR, &data, 1, dev);
  1533. if (rslt == BMI160_OK) {
  1534. if (step_cnt_enable == BMI160_ENABLE)
  1535. data |= (uint8_t)(step_cnt_enable << 3);
  1536. else
  1537. data &= ~BMI160_STEP_COUNT_EN_BIT_MASK;
  1538. rslt = bmi160_set_regs(BMI160_INT_STEP_CONFIG_1_ADDR, &data, 1, dev);
  1539. }
  1540. }
  1541. return rslt;
  1542. }
  1543. /*!
  1544. * @brief This API reads the step counter value.
  1545. */
  1546. int8_t bmi160_read_step_counter(uint16_t *step_val, const struct bmi160_dev *dev)
  1547. {
  1548. int8_t rslt;
  1549. uint8_t data[2] = {0, 0};
  1550. uint16_t msb = 0;
  1551. uint8_t lsb = 0;
  1552. /* Null-pointer check */
  1553. rslt = null_ptr_check(dev);
  1554. if (rslt != BMI160_OK) {
  1555. rslt = BMI160_E_NULL_PTR;
  1556. } else {
  1557. rslt = bmi160_get_regs(BMI160_INT_STEP_CNT_0_ADDR, data, 2, dev);
  1558. if (rslt == BMI160_OK) {
  1559. lsb = data[0];
  1560. msb = data[1] << 8;
  1561. *step_val = msb | lsb;
  1562. }
  1563. }
  1564. return rslt;
  1565. }
  1566. /*!
  1567. * @brief This API reads the mention no of byte of data from the given
  1568. * register address of auxiliary sensor.
  1569. */
  1570. int8_t bmi160_aux_read(uint8_t reg_addr, uint8_t *aux_data, uint16_t len, const struct bmi160_dev *dev)
  1571. {
  1572. int8_t rslt = BMI160_OK;
  1573. uint16_t map_len = 0;
  1574. /* Null-pointer check */
  1575. if ((dev == NULL) || (dev->read == NULL)) {
  1576. rslt = BMI160_E_NULL_PTR;
  1577. } else {
  1578. if (dev->aux_cfg.aux_sensor_enable == BMI160_ENABLE) {
  1579. rslt = map_read_len(&map_len, dev);
  1580. if (rslt == BMI160_OK)
  1581. rslt = extract_aux_read(map_len, reg_addr, aux_data, len, dev);
  1582. } else {
  1583. rslt = BMI160_E_INVALID_INPUT;
  1584. }
  1585. }
  1586. return rslt;
  1587. }
  1588. /*!
  1589. * @brief This API writes the mention no of byte of data to the given
  1590. * register address of auxiliary sensor.
  1591. */
  1592. int8_t bmi160_aux_write(uint8_t reg_addr, uint8_t *aux_data, uint16_t len, const struct bmi160_dev *dev)
  1593. {
  1594. int8_t rslt = BMI160_OK;
  1595. uint8_t count = 0;
  1596. /* Null-pointer check */
  1597. if ((dev == NULL) || (dev->write == NULL)) {
  1598. rslt = BMI160_E_NULL_PTR;
  1599. } else {
  1600. for (; count < len; count++) {
  1601. /* set data to write */
  1602. rslt = bmi160_set_regs(BMI160_AUX_IF_4_ADDR, aux_data, 1, dev);
  1603. dev->delay_ms(BMI160_AUX_COM_DELAY);
  1604. if (rslt == BMI160_OK) {
  1605. /* set address to write */
  1606. rslt = bmi160_set_regs(BMI160_AUX_IF_3_ADDR, &reg_addr, 1, dev);
  1607. dev->delay_ms(BMI160_AUX_COM_DELAY);
  1608. if (rslt == BMI160_OK && (count < len - 1)) {
  1609. aux_data++;
  1610. reg_addr++;
  1611. }
  1612. }
  1613. }
  1614. }
  1615. return rslt;
  1616. }
  1617. /*!
  1618. * @brief This API initialize the auxiliary sensor
  1619. * in order to access it.
  1620. */
  1621. int8_t bmi160_aux_init(const struct bmi160_dev *dev)
  1622. {
  1623. int8_t rslt;
  1624. /* Null-pointer check */
  1625. rslt = null_ptr_check(dev);
  1626. if (rslt != BMI160_OK) {
  1627. rslt = BMI160_E_NULL_PTR;
  1628. } else {
  1629. if (dev->aux_cfg.aux_sensor_enable == BMI160_ENABLE) {
  1630. /* Configures the auxiliary sensor interface settings */
  1631. rslt = config_aux_settg(dev);
  1632. } else {
  1633. rslt = BMI160_E_INVALID_INPUT;
  1634. }
  1635. }
  1636. return rslt;
  1637. }
  1638. /*!
  1639. * @brief This API is used to setup the auxiliary sensor of bmi160 in auto mode
  1640. * Thus enabling the auto update of 8 bytes of data from auxiliary sensor
  1641. * to BMI160 register address 0x04 to 0x0B
  1642. */
  1643. int8_t bmi160_set_aux_auto_mode(uint8_t *data_addr, struct bmi160_dev *dev)
  1644. {
  1645. int8_t rslt;
  1646. /* Null-pointer check */
  1647. rslt = null_ptr_check(dev);
  1648. if (rslt != BMI160_OK) {
  1649. rslt = BMI160_E_NULL_PTR;
  1650. } else {
  1651. if (dev->aux_cfg.aux_sensor_enable == BMI160_ENABLE) {
  1652. /* Write the aux. address to read in 0x4D of BMI160*/
  1653. rslt = bmi160_set_regs(BMI160_AUX_IF_2_ADDR, data_addr, 1, dev);
  1654. dev->delay_ms(BMI160_AUX_COM_DELAY);
  1655. if (rslt == BMI160_OK) {
  1656. /* Configure the polling ODR for
  1657. auxiliary sensor */
  1658. rslt = config_aux_odr(dev);
  1659. if (rslt == BMI160_OK) {
  1660. /* Disable the aux. manual mode, i.e aux.
  1661. * sensor is in auto-mode (data-mode) */
  1662. dev->aux_cfg.manual_enable = BMI160_DISABLE;
  1663. rslt = bmi160_config_aux_mode(dev);
  1664. /* Auxiliary sensor data is obtained
  1665. * in auto mode from this point */
  1666. }
  1667. }
  1668. } else {
  1669. rslt = BMI160_E_INVALID_INPUT;
  1670. }
  1671. }
  1672. return rslt;
  1673. }
  1674. /*!
  1675. * @brief This API configures the 0x4C register and settings like
  1676. * Auxiliary sensor manual enable/ disable and aux burst read length.
  1677. */
  1678. int8_t bmi160_config_aux_mode(const struct bmi160_dev *dev)
  1679. {
  1680. int8_t rslt;
  1681. uint8_t aux_if[2] = {(uint8_t)(dev->aux_cfg.aux_i2c_addr * 2), 0};
  1682. rslt = bmi160_get_regs(BMI160_AUX_IF_1_ADDR, &aux_if[1], 1, dev);
  1683. if (rslt == BMI160_OK) {
  1684. /* update the Auxiliary interface to manual/auto mode */
  1685. aux_if[1] = BMI160_SET_BITS(aux_if[1], BMI160_MANUAL_MODE_EN, dev->aux_cfg.manual_enable);
  1686. /* update the burst read length defined by user */
  1687. aux_if[1] = BMI160_SET_BITS_POS_0(aux_if[1], BMI160_AUX_READ_BURST, dev->aux_cfg.aux_rd_burst_len);
  1688. /* Set the secondary interface address and manual mode
  1689. * along with burst read length */
  1690. rslt = bmi160_set_regs(BMI160_AUX_IF_0_ADDR, &aux_if[0], 2, dev);
  1691. dev->delay_ms(BMI160_AUX_COM_DELAY);
  1692. }
  1693. return rslt;
  1694. }
  1695. /*!
  1696. * @brief This API is used to read the raw uncompensated auxiliary sensor
  1697. * data of 8 bytes from BMI160 register address 0x04 to 0x0B
  1698. */
  1699. int8_t bmi160_read_aux_data_auto_mode(uint8_t *aux_data, const struct bmi160_dev *dev)
  1700. {
  1701. int8_t rslt;
  1702. /* Null-pointer check */
  1703. rslt = null_ptr_check(dev);
  1704. if (rslt != BMI160_OK) {
  1705. rslt = BMI160_E_NULL_PTR;
  1706. } else {
  1707. if ((dev->aux_cfg.aux_sensor_enable == BMI160_ENABLE) &&
  1708. (dev->aux_cfg.manual_enable == BMI160_DISABLE)) {
  1709. /* Read the aux. sensor's raw data */
  1710. rslt = bmi160_get_regs(BMI160_AUX_DATA_ADDR, aux_data, 8, dev);
  1711. } else {
  1712. rslt = BMI160_E_INVALID_INPUT;
  1713. }
  1714. }
  1715. return rslt;
  1716. }
  1717. /*!
  1718. * @brief This is used to perform self test of accel/gyro of the BMI160 sensor
  1719. */
  1720. int8_t bmi160_perform_self_test(uint8_t select_sensor, struct bmi160_dev *dev)
  1721. {
  1722. int8_t rslt;
  1723. int8_t self_test_rslt = 0;
  1724. /* Null-pointer check */
  1725. rslt = null_ptr_check(dev);
  1726. if (rslt != BMI160_OK) {
  1727. rslt = BMI160_E_NULL_PTR;
  1728. } else {
  1729. /* Proceed if null check is fine */
  1730. switch (select_sensor) {
  1731. case BMI160_ACCEL_ONLY:
  1732. rslt = perform_accel_self_test(dev);
  1733. break;
  1734. case BMI160_GYRO_ONLY:
  1735. /* Set the power mode as normal mode */
  1736. dev->gyro_cfg.power = BMI160_GYRO_NORMAL_MODE;
  1737. rslt = bmi160_set_power_mode(dev);
  1738. /* Perform gyro self test */
  1739. if (rslt == BMI160_OK) {
  1740. /* Perform gyro self test */
  1741. rslt = perform_gyro_self_test(dev);
  1742. }
  1743. break;
  1744. default:
  1745. rslt = BMI160_E_INVALID_INPUT;
  1746. break;
  1747. }
  1748. /* Check to ensure bus error does not occur */
  1749. if (rslt >= BMI160_OK) {
  1750. /* Store the status of self test result */
  1751. self_test_rslt = rslt;
  1752. /* Perform soft reset */
  1753. rslt = bmi160_soft_reset(dev);
  1754. }
  1755. /* Check to ensure bus operations are success */
  1756. if (rslt == BMI160_OK) {
  1757. /* Restore self_test_rslt as return value */
  1758. rslt = self_test_rslt;
  1759. }
  1760. }
  1761. return rslt;
  1762. }
  1763. /*!
  1764. * @brief This API reads the data from fifo buffer.
  1765. */
  1766. int8_t bmi160_get_fifo_data(struct bmi160_dev const *dev)
  1767. {
  1768. int8_t rslt = 0;
  1769. uint16_t bytes_to_read = 0;
  1770. uint16_t user_fifo_len = 0;
  1771. uint8_t addr = BMI160_FIFO_DATA_ADDR;
  1772. /* check the bmi160 structure as NULL*/
  1773. if ((dev == NULL) || (dev->fifo->data == NULL)) {
  1774. rslt = BMI160_E_NULL_PTR;
  1775. } else {
  1776. reset_fifo_data_structure(dev);
  1777. /* get current FIFO fill-level*/
  1778. rslt = get_fifo_byte_counter(&bytes_to_read, dev);
  1779. if (rslt == BMI160_OK) {
  1780. user_fifo_len = dev->fifo->length;
  1781. if (dev->fifo->length > bytes_to_read) {
  1782. /* Handling the case where user requests
  1783. more data than available in FIFO */
  1784. dev->fifo->length = bytes_to_read;
  1785. }
  1786. if ((dev->fifo->fifo_time_enable == BMI160_FIFO_TIME_ENABLE)
  1787. && (bytes_to_read + BMI160_FIFO_BYTES_OVERREAD <= user_fifo_len)) {
  1788. /* Handling case of sensor time availability*/
  1789. dev->fifo->length = dev->fifo->length + BMI160_FIFO_BYTES_OVERREAD;
  1790. }
  1791. if (dev->interface == BMI160_SPI_INTF) {
  1792. /* SPI read mask */
  1793. addr = addr | BMI160_SPI_RD_MASK;
  1794. }
  1795. /* read only the filled bytes in the FIFO Buffer */
  1796. rslt = dev->read(dev->id, addr, dev->fifo->data, dev->fifo->length);
  1797. }
  1798. }
  1799. return rslt;
  1800. }
  1801. /*!
  1802. * @brief This API writes fifo_flush command to command register.This
  1803. * action clears all data in the Fifo without changing fifo configuration
  1804. * settings
  1805. */
  1806. int8_t bmi160_set_fifo_flush(const struct bmi160_dev *dev)
  1807. {
  1808. int8_t rslt = 0;
  1809. uint8_t data = BMI160_FIFO_FLUSH_VALUE;
  1810. uint8_t reg_addr = BMI160_COMMAND_REG_ADDR;
  1811. /* Check the bmi160_dev structure for NULL address*/
  1812. if (dev == NULL)
  1813. rslt = BMI160_E_NULL_PTR;
  1814. else
  1815. rslt = bmi160_set_regs(reg_addr, &data, BMI160_ONE, dev);
  1816. return rslt;
  1817. }
  1818. /*!
  1819. * @brief This API sets the FIFO configuration in the sensor.
  1820. */
  1821. int8_t bmi160_set_fifo_config(uint8_t config, uint8_t enable, struct bmi160_dev const *dev)
  1822. {
  1823. int8_t rslt = 0;
  1824. uint8_t data = 0;
  1825. uint8_t reg_addr = BMI160_FIFO_CONFIG_1_ADDR;
  1826. uint8_t fifo_config = config & BMI160_FIFO_CONFIG_1_MASK;
  1827. /* Check the bmi160_dev structure for NULL address*/
  1828. if (dev == NULL) {
  1829. rslt = BMI160_E_NULL_PTR;
  1830. } else {
  1831. rslt = bmi160_get_regs(reg_addr, &data, BMI160_ONE, dev);
  1832. if (rslt == BMI160_OK) {
  1833. if (fifo_config > 0) {
  1834. if (enable == BMI160_ENABLE)
  1835. data = data | fifo_config;
  1836. else
  1837. data = data & (~fifo_config);
  1838. }
  1839. /* write fifo frame content configuration*/
  1840. rslt = bmi160_set_regs(reg_addr, &data, BMI160_ONE, dev);
  1841. if (rslt == BMI160_OK) {
  1842. /* read fifo frame content configuration*/
  1843. rslt = bmi160_get_regs(reg_addr, &data, BMI160_ONE, dev);
  1844. if (rslt == BMI160_OK) {
  1845. /* extract fifo header enabled status */
  1846. dev->fifo->fifo_header_enable = data & BMI160_FIFO_HEAD_ENABLE;
  1847. /* extract accel/gyr/aux. data enabled status */
  1848. dev->fifo->fifo_data_enable = data & BMI160_FIFO_M_G_A_ENABLE;
  1849. /* extract fifo sensor time enabled status */
  1850. dev->fifo->fifo_time_enable = data & BMI160_FIFO_TIME_ENABLE;
  1851. }
  1852. }
  1853. }
  1854. }
  1855. return rslt;
  1856. }
  1857. /*! @brief This API is used to configure the down sampling ratios of
  1858. * the accel and gyro data for FIFO.Also, it configures filtered or
  1859. * pre-filtered data for accel and gyro.
  1860. *
  1861. */
  1862. int8_t bmi160_set_fifo_down(uint8_t fifo_down, const struct bmi160_dev *dev)
  1863. {
  1864. int8_t rslt = 0;
  1865. uint8_t data = 0;
  1866. uint8_t reg_addr = BMI160_FIFO_DOWN_ADDR;
  1867. /* Check the bmi160_dev structure for NULL address*/
  1868. if (dev == NULL) {
  1869. rslt = BMI160_E_NULL_PTR;
  1870. } else {
  1871. rslt = bmi160_get_regs(reg_addr, &data, BMI160_ONE, dev);
  1872. if (rslt == BMI160_OK) {
  1873. data = data | fifo_down;
  1874. rslt = bmi160_set_regs(reg_addr, &data, BMI160_ONE, dev);
  1875. }
  1876. }
  1877. return rslt;
  1878. }
  1879. /*!
  1880. * @brief This API sets the FIFO watermark level in the sensor.
  1881. *
  1882. */
  1883. int8_t bmi160_set_fifo_wm(uint8_t fifo_wm, const struct bmi160_dev *dev)
  1884. {
  1885. int8_t rslt = 0;
  1886. uint8_t data = fifo_wm;
  1887. uint8_t reg_addr = BMI160_FIFO_CONFIG_0_ADDR;
  1888. /* Check the bmi160_dev structure for NULL address*/
  1889. if (dev == NULL)
  1890. rslt = BMI160_E_NULL_PTR;
  1891. else
  1892. rslt = bmi160_set_regs(reg_addr, &data, BMI160_ONE, dev);
  1893. return rslt;
  1894. }
  1895. /*!
  1896. * @brief This API parses and extracts the accelerometer frames from
  1897. * FIFO data read by the "bmi160_get_fifo_data" API and stores it in
  1898. * the "accel_data" structure instance.
  1899. */
  1900. int8_t bmi160_extract_accel(struct bmi160_sensor_data *accel_data, uint8_t *accel_length, struct bmi160_dev const *dev)
  1901. {
  1902. int8_t rslt = 0;
  1903. uint16_t data_index = 0;
  1904. uint16_t data_read_length = 0;
  1905. uint8_t accel_index = 0;
  1906. uint8_t fifo_data_enable = 0;
  1907. if (dev == NULL || dev->fifo == NULL || dev->fifo->data == NULL) {
  1908. rslt = BMI160_E_NULL_PTR;
  1909. } else {
  1910. /* Parsing the FIFO data in header-less mode */
  1911. if (dev->fifo->fifo_header_enable == 0) {
  1912. /* Number of bytes to be parsed from FIFO */
  1913. get_accel_len_to_parse(&data_index, &data_read_length, accel_length, dev);
  1914. for (; data_index < data_read_length; ) {
  1915. /*Check for the availability of next two bytes of FIFO data */
  1916. check_frame_validity(&data_index, dev);
  1917. fifo_data_enable = dev->fifo->fifo_data_enable;
  1918. unpack_accel_frame(accel_data, &data_index, &accel_index, fifo_data_enable, dev);
  1919. }
  1920. /* update number of accel data read*/
  1921. *accel_length = accel_index;
  1922. /*update the accel byte index*/
  1923. dev->fifo->accel_byte_start_idx = data_index;
  1924. } else {
  1925. /* Parsing the FIFO data in header mode */
  1926. extract_accel_header_mode(accel_data, accel_length, dev);
  1927. }
  1928. }
  1929. return rslt;
  1930. }
  1931. /*!
  1932. * @brief This API parses and extracts the gyro frames from
  1933. * FIFO data read by the "bmi160_get_fifo_data" API and stores it in
  1934. * the "gyro_data" structure instance.
  1935. */
  1936. int8_t bmi160_extract_gyro(struct bmi160_sensor_data *gyro_data, uint8_t *gyro_length, struct bmi160_dev const *dev)
  1937. {
  1938. int8_t rslt = 0;
  1939. uint16_t data_index = 0;
  1940. uint16_t data_read_length = 0;
  1941. uint8_t gyro_index = 0;
  1942. uint8_t fifo_data_enable = 0;
  1943. if (dev == NULL || dev->fifo->data == NULL) {
  1944. rslt = BMI160_E_NULL_PTR;
  1945. } else {
  1946. /* Parsing the FIFO data in header-less mode */
  1947. if (dev->fifo->fifo_header_enable == 0) {
  1948. /* Number of bytes to be parsed from FIFO */
  1949. get_gyro_len_to_parse(&data_index, &data_read_length, gyro_length, dev);
  1950. for (; data_index < data_read_length ;) {
  1951. /*Check for the availability of next two bytes of FIFO data */
  1952. check_frame_validity(&data_index, dev);
  1953. fifo_data_enable = dev->fifo->fifo_data_enable;
  1954. unpack_gyro_frame(gyro_data, &data_index, &gyro_index, fifo_data_enable, dev);
  1955. }
  1956. /* update number of gyro data read */
  1957. *gyro_length = gyro_index;
  1958. /* update the gyro byte index */
  1959. dev->fifo->gyro_byte_start_idx = data_index;
  1960. } else {
  1961. /* Parsing the FIFO data in header mode */
  1962. extract_gyro_header_mode(gyro_data, gyro_length, dev);
  1963. }
  1964. }
  1965. return rslt;
  1966. }
  1967. /*!
  1968. * @brief This API parses and extracts the aux frames from
  1969. * FIFO data read by the "bmi160_get_fifo_data" API and stores it in
  1970. * the "aux_data" structure instance.
  1971. */
  1972. int8_t bmi160_extract_aux(struct bmi160_aux_data *aux_data, uint8_t *aux_len, struct bmi160_dev const *dev)
  1973. {
  1974. int8_t rslt = 0;
  1975. uint16_t data_index = 0;
  1976. uint16_t data_read_length = 0;
  1977. uint8_t aux_index = 0;
  1978. uint8_t fifo_data_enable = 0;
  1979. if ((dev == NULL) || (dev->fifo->data == NULL) || (aux_data == NULL)) {
  1980. rslt = BMI160_E_NULL_PTR;
  1981. } else {
  1982. /* Parsing the FIFO data in header-less mode */
  1983. if (dev->fifo->fifo_header_enable == 0) {
  1984. /* Number of bytes to be parsed from FIFO */
  1985. get_aux_len_to_parse(&data_index, &data_read_length, aux_len, dev);
  1986. for (; data_index < data_read_length ;) {
  1987. /* Check for the availability of next two
  1988. * bytes of FIFO data */
  1989. check_frame_validity(&data_index, dev);
  1990. fifo_data_enable = dev->fifo->fifo_data_enable;
  1991. unpack_aux_frame(aux_data, &data_index, &aux_index, fifo_data_enable, dev);
  1992. }
  1993. /* update number of aux data read */
  1994. *aux_len = aux_index;
  1995. /* update the aux byte index */
  1996. dev->fifo->aux_byte_start_idx = data_index;
  1997. } else {
  1998. /* Parsing the FIFO data in header mode */
  1999. extract_aux_header_mode(aux_data, aux_len, dev);
  2000. }
  2001. }
  2002. return rslt;
  2003. }
  2004. /*!
  2005. * @brief This API starts the FOC of accel and gyro
  2006. *
  2007. * @note FOC should not be used in low-power mode of sensor
  2008. *
  2009. * @note Accel FOC targets values of +1g , 0g , -1g
  2010. * Gyro FOC always targets value of 0 dps
  2011. */
  2012. int8_t bmi160_start_foc(const struct bmi160_foc_conf *foc_conf, struct bmi160_offsets *offset,
  2013. struct bmi160_dev const *dev)
  2014. {
  2015. int8_t rslt;
  2016. uint8_t data;
  2017. /* Null-pointer check */
  2018. rslt = null_ptr_check(dev);
  2019. if (rslt != BMI160_OK) {
  2020. rslt = BMI160_E_NULL_PTR;
  2021. } else {
  2022. /* Set the offset enable bits */
  2023. rslt = configure_offset_enable(foc_conf, dev);
  2024. if (rslt == BMI160_OK) {
  2025. /* Read the FOC config from the sensor */
  2026. rslt = bmi160_get_regs(BMI160_FOC_CONF_ADDR, &data, 1, dev);
  2027. /* Set the FOC config for gyro */
  2028. data = BMI160_SET_BITS(data, BMI160_GYRO_FOC_EN, foc_conf->foc_gyr_en);
  2029. /* Set the FOC config for accel xyz axes */
  2030. data = BMI160_SET_BITS(data, BMI160_ACCEL_FOC_X_CONF, foc_conf->foc_acc_x);
  2031. data = BMI160_SET_BITS(data, BMI160_ACCEL_FOC_Y_CONF, foc_conf->foc_acc_y);
  2032. data = BMI160_SET_BITS_POS_0(data, BMI160_ACCEL_FOC_Z_CONF, foc_conf->foc_acc_z);
  2033. if (rslt == BMI160_OK) {
  2034. /* Set the FOC config in the sensor */
  2035. rslt = bmi160_set_regs(BMI160_FOC_CONF_ADDR, &data, 1, dev);
  2036. if (rslt == BMI160_OK) {
  2037. /* Procedure to trigger
  2038. * FOC and check status */
  2039. rslt = trigger_foc(offset, dev);
  2040. }
  2041. }
  2042. }
  2043. }
  2044. return rslt;
  2045. }
  2046. /*!
  2047. * @brief This API reads and stores the offset values of accel and gyro
  2048. */
  2049. int8_t bmi160_get_offsets(struct bmi160_offsets *offset, const struct bmi160_dev *dev)
  2050. {
  2051. int8_t rslt;
  2052. uint8_t data[7];
  2053. uint8_t lsb, msb;
  2054. int16_t offset_msb, offset_lsb;
  2055. int16_t offset_data;
  2056. /* Null-pointer check */
  2057. rslt = null_ptr_check(dev);
  2058. if (rslt != BMI160_OK) {
  2059. rslt = BMI160_E_NULL_PTR;
  2060. } else {
  2061. /* Read the FOC config from the sensor */
  2062. rslt = bmi160_get_regs(BMI160_OFFSET_ADDR, data, 7, dev);
  2063. /* Accel offsets */
  2064. offset->off_acc_x = (int8_t)data[0];
  2065. offset->off_acc_y = (int8_t)data[1];
  2066. offset->off_acc_z = (int8_t)data[2];
  2067. /* Gyro x-axis offset */
  2068. lsb = data[3];
  2069. msb = BMI160_GET_BITS_POS_0(data[6], BMI160_GYRO_OFFSET_X);
  2070. offset_msb = (int16_t)(msb << 14);
  2071. offset_lsb = lsb << 6;
  2072. offset_data = offset_msb | offset_lsb;
  2073. /* Divide by 64 to get the Right shift by 6 value */
  2074. offset->off_gyro_x = (int16_t)(offset_data / 64);
  2075. /* Gyro y-axis offset */
  2076. lsb = data[4];
  2077. msb = BMI160_GET_BITS(data[6], BMI160_GYRO_OFFSET_Y);
  2078. offset_msb = (int16_t)(msb << 14);
  2079. offset_lsb = lsb << 6;
  2080. offset_data = offset_msb | offset_lsb;
  2081. /* Divide by 64 to get the Right shift by 6 value */
  2082. offset->off_gyro_y = (int16_t)(offset_data / 64);
  2083. /* Gyro z-axis offset */
  2084. lsb = data[5];
  2085. msb = BMI160_GET_BITS(data[6], BMI160_GYRO_OFFSET_Z);
  2086. offset_msb = (int16_t)(msb << 14);
  2087. offset_lsb = lsb << 6;
  2088. offset_data = offset_msb | offset_lsb;
  2089. /* Divide by 64 to get the Right shift by 6 value */
  2090. offset->off_gyro_z = (int16_t)(offset_data / 64);
  2091. }
  2092. return rslt;
  2093. }
  2094. /*!
  2095. * @brief This API writes the offset values of accel and gyro to
  2096. * the sensor but these values will be reset on POR or soft reset.
  2097. */
  2098. int8_t bmi160_set_offsets(const struct bmi160_foc_conf *foc_conf, const struct bmi160_offsets *offset,
  2099. struct bmi160_dev const *dev)
  2100. {
  2101. int8_t rslt;
  2102. uint8_t data[7];
  2103. uint8_t x_msb, y_msb, z_msb;
  2104. /* Null-pointer check */
  2105. rslt = null_ptr_check(dev);
  2106. if (rslt != BMI160_OK) {
  2107. rslt = BMI160_E_NULL_PTR;
  2108. } else {
  2109. /* Update the accel offset */
  2110. data[0] = (uint8_t)offset->off_acc_x;
  2111. data[1] = (uint8_t)offset->off_acc_y;
  2112. data[2] = (uint8_t)offset->off_acc_z;
  2113. /* Update the LSB of gyro offset */
  2114. data[3] = BMI160_GET_LSB(offset->off_gyro_x);
  2115. data[4] = BMI160_GET_LSB(offset->off_gyro_y);
  2116. data[5] = BMI160_GET_LSB(offset->off_gyro_z);
  2117. /* Update the MSB of gyro offset */
  2118. x_msb = BMI160_GET_BITS(offset->off_gyro_x, BMI160_GYRO_OFFSET);
  2119. y_msb = BMI160_GET_BITS(offset->off_gyro_y, BMI160_GYRO_OFFSET);
  2120. z_msb = BMI160_GET_BITS(offset->off_gyro_z, BMI160_GYRO_OFFSET);
  2121. data[6] = (uint8_t)(z_msb << 4 | y_msb << 2 | x_msb);
  2122. /* Set the offset enable/disable for gyro and accel */
  2123. data[6] = BMI160_SET_BITS(data[6], BMI160_GYRO_OFFSET_EN, foc_conf->gyro_off_en);
  2124. data[6] = BMI160_SET_BITS(data[6], BMI160_ACCEL_OFFSET_EN, foc_conf->acc_off_en);
  2125. /* Set the offset config and values in the sensor */
  2126. rslt = bmi160_set_regs(BMI160_OFFSET_ADDR, data, 7, dev);
  2127. }
  2128. return rslt;
  2129. }
  2130. /*!
  2131. * @brief This API writes the image registers values to NVM which is
  2132. * stored even after POR or soft reset
  2133. */
  2134. int8_t bmi160_update_nvm(struct bmi160_dev const *dev)
  2135. {
  2136. int8_t rslt;
  2137. uint8_t data;
  2138. uint8_t cmd = BMI160_NVM_BACKUP_EN;
  2139. /* Read the nvm_prog_en configuration */
  2140. rslt = bmi160_get_regs(BMI160_CONF_ADDR, &data, 1, dev);
  2141. if (rslt == BMI160_OK) {
  2142. data = BMI160_SET_BITS(data, BMI160_NVM_UPDATE, 1);
  2143. /* Set the nvm_prog_en bit in the sensor */
  2144. rslt = bmi160_set_regs(BMI160_CONF_ADDR, &data, 1, dev);
  2145. if (rslt == BMI160_OK) {
  2146. /* Update NVM */
  2147. rslt = bmi160_set_regs(BMI160_COMMAND_REG_ADDR, &cmd, 1, dev);
  2148. if (rslt == BMI160_OK) {
  2149. /* Check for NVM ready status */
  2150. rslt = bmi160_get_regs(BMI160_STATUS_ADDR, &data, 1, dev);
  2151. if (rslt == BMI160_OK) {
  2152. data = BMI160_GET_BITS(data, BMI160_NVM_STATUS);
  2153. if (data != BMI160_ENABLE) {
  2154. /* Delay to update NVM */
  2155. dev->delay_ms(25);
  2156. }
  2157. }
  2158. }
  2159. }
  2160. }
  2161. return rslt;
  2162. }
  2163. /*!
  2164. * @brief This API gets the interrupt status from the sensor.
  2165. */
  2166. int8_t bmi160_get_int_status(enum bmi160_int_status_sel int_status_sel,
  2167. union bmi160_int_status *int_status, struct bmi160_dev const *dev)
  2168. {
  2169. int8_t rslt = 0;
  2170. /* To get the status of all interrupts */
  2171. if (int_status_sel == BMI160_INT_STATUS_ALL) {
  2172. rslt = bmi160_get_regs(BMI160_INT_STATUS_ADDR, &int_status->data[0], 4, dev);
  2173. } else {
  2174. if (int_status_sel & BMI160_INT_STATUS_0)
  2175. rslt = bmi160_get_regs(BMI160_INT_STATUS_ADDR, &int_status->data[0], 1, dev);
  2176. if (int_status_sel & BMI160_INT_STATUS_1)
  2177. rslt = bmi160_get_regs(BMI160_INT_STATUS_ADDR + 1, &int_status->data[1], 1, dev);
  2178. if (int_status_sel & BMI160_INT_STATUS_2)
  2179. rslt = bmi160_get_regs(BMI160_INT_STATUS_ADDR + 2, &int_status->data[2], 1, dev);
  2180. if (int_status_sel & BMI160_INT_STATUS_3)
  2181. rslt = bmi160_get_regs(BMI160_INT_STATUS_ADDR + 3, &int_status->data[3], 1, dev);
  2182. }
  2183. return rslt;
  2184. }
  2185. /*********************** Local function definitions ***************************/
  2186. /*!
  2187. * @brief This API sets the any-motion interrupt of the sensor.
  2188. * This interrupt occurs when accel values exceeds preset threshold
  2189. * for a certain period of time.
  2190. */
  2191. static int8_t set_accel_any_motion_int(struct bmi160_int_settg *int_config, struct bmi160_dev *dev)
  2192. {
  2193. int8_t rslt;
  2194. /* Null-pointer check */
  2195. rslt = null_ptr_check(dev);
  2196. if ((rslt != BMI160_OK) || (int_config == NULL)) {
  2197. rslt = BMI160_E_NULL_PTR;
  2198. } else {
  2199. /* updating the interrupt structure to local structure */
  2200. struct bmi160_acc_any_mot_int_cfg *any_motion_int_cfg = &(int_config->int_type_cfg.acc_any_motion_int);
  2201. rslt = enable_accel_any_motion_int(any_motion_int_cfg, dev);
  2202. if (rslt == BMI160_OK)
  2203. rslt = config_any_motion_int_settg(int_config, any_motion_int_cfg, dev);
  2204. }
  2205. return rslt;
  2206. }
  2207. /*!
  2208. * @brief This API sets tap interrupts.Interrupt is fired when
  2209. * tap movements happen.
  2210. */
  2211. static int8_t set_accel_tap_int(struct bmi160_int_settg *int_config, const struct bmi160_dev *dev)
  2212. {
  2213. int8_t rslt;
  2214. /* Null-pointer check */
  2215. rslt = null_ptr_check(dev);
  2216. if ((rslt != BMI160_OK) || (int_config == NULL)) {
  2217. rslt = BMI160_E_NULL_PTR;
  2218. } else {
  2219. /* updating the interrupt structure to local structure */
  2220. struct bmi160_acc_tap_int_cfg *tap_int_cfg = &(int_config->int_type_cfg.acc_tap_int);
  2221. rslt = enable_tap_int(int_config, tap_int_cfg, dev);
  2222. if (rslt == BMI160_OK) {
  2223. /* Configure Interrupt pins */
  2224. rslt = set_intr_pin_config(int_config, dev);
  2225. if (rslt == BMI160_OK)
  2226. rslt = config_tap_int_settg(int_config, tap_int_cfg, dev);
  2227. }
  2228. }
  2229. return rslt;
  2230. }
  2231. /*!
  2232. * @brief This API sets the data ready interrupt for both accel and gyro.
  2233. * This interrupt occurs when new accel and gyro data comes.
  2234. */
  2235. static int8_t set_accel_gyro_data_ready_int(const struct bmi160_int_settg *int_config, const struct bmi160_dev *dev)
  2236. {
  2237. int8_t rslt;
  2238. /* Null-pointer check */
  2239. rslt = null_ptr_check(dev);
  2240. if ((rslt != BMI160_OK) || (int_config == NULL)) {
  2241. rslt = BMI160_E_NULL_PTR;
  2242. } else {
  2243. rslt = enable_data_ready_int(dev);
  2244. if (rslt == BMI160_OK) {
  2245. /* Configure Interrupt pins */
  2246. rslt = set_intr_pin_config(int_config, dev);
  2247. if (rslt == BMI160_OK)
  2248. rslt = map_hardware_interrupt(int_config, dev);
  2249. }
  2250. }
  2251. return rslt;
  2252. }
  2253. /*!
  2254. * @brief This API sets the significant motion interrupt of the sensor.This
  2255. * interrupt occurs when there is change in user location.
  2256. */
  2257. static int8_t set_accel_sig_motion_int(struct bmi160_int_settg *int_config, struct bmi160_dev *dev)
  2258. {
  2259. int8_t rslt;
  2260. /* Null-pointer check */
  2261. rslt = null_ptr_check(dev);
  2262. if ((rslt != BMI160_OK) || (int_config == NULL)) {
  2263. rslt = BMI160_E_NULL_PTR;
  2264. } else {
  2265. /* updating the interrupt structure to local structure */
  2266. struct bmi160_acc_sig_mot_int_cfg *sig_mot_int_cfg = &(int_config->int_type_cfg.acc_sig_motion_int);
  2267. rslt = enable_sig_motion_int(sig_mot_int_cfg, dev);
  2268. if (rslt == BMI160_OK)
  2269. rslt = config_sig_motion_int_settg(int_config, sig_mot_int_cfg, dev);
  2270. }
  2271. return rslt;
  2272. }
  2273. /*!
  2274. * @brief This API sets the no motion/slow motion interrupt of the sensor.
  2275. * Slow motion is similar to any motion interrupt.No motion interrupt
  2276. * occurs when slope bet. two accel values falls below preset threshold
  2277. * for preset duration.
  2278. */
  2279. static int8_t set_accel_no_motion_int(struct bmi160_int_settg *int_config, const struct bmi160_dev *dev)
  2280. {
  2281. int8_t rslt;
  2282. /* Null-pointer check */
  2283. rslt = null_ptr_check(dev);
  2284. if ((rslt != BMI160_OK) || (int_config == NULL)) {
  2285. rslt = BMI160_E_NULL_PTR;
  2286. } else {
  2287. /* updating the interrupt structure to local structure */
  2288. struct bmi160_acc_no_motion_int_cfg *no_mot_int_cfg = &(int_config->int_type_cfg.acc_no_motion_int);
  2289. rslt = enable_no_motion_int(no_mot_int_cfg, dev);
  2290. if (rslt == BMI160_OK)
  2291. /* Configure the INT PIN settings*/
  2292. rslt = config_no_motion_int_settg(int_config, no_mot_int_cfg, dev);
  2293. }
  2294. return rslt;
  2295. }
  2296. /*!
  2297. * @brief This API sets the step detection interrupt.This interrupt
  2298. * occurs when the single step causes accel values to go above
  2299. * preset threshold.
  2300. */
  2301. static int8_t set_accel_step_detect_int(struct bmi160_int_settg *int_config, const struct bmi160_dev *dev)
  2302. {
  2303. int8_t rslt;
  2304. /* Null-pointer check */
  2305. rslt = null_ptr_check(dev);
  2306. if ((rslt != BMI160_OK) || (int_config == NULL)) {
  2307. rslt = BMI160_E_NULL_PTR;
  2308. } else {
  2309. /* updating the interrupt structure to local structure */
  2310. struct bmi160_acc_step_detect_int_cfg *step_detect_int_cfg =
  2311. &(int_config->int_type_cfg.acc_step_detect_int);
  2312. rslt = enable_step_detect_int(step_detect_int_cfg, dev);
  2313. if (rslt == BMI160_OK) {
  2314. /* Configure Interrupt pins */
  2315. rslt = set_intr_pin_config(int_config, dev);
  2316. if (rslt == BMI160_OK) {
  2317. rslt = map_feature_interrupt(int_config, dev);
  2318. if (rslt == BMI160_OK)
  2319. rslt = config_step_detect(step_detect_int_cfg, dev);
  2320. }
  2321. }
  2322. }
  2323. return rslt;
  2324. }
  2325. /*!
  2326. * @brief This API sets the orientation interrupt of the sensor.This
  2327. * interrupt occurs when there is orientation change in the sensor
  2328. * with respect to gravitational field vector g.
  2329. */
  2330. static int8_t set_accel_orientation_int(struct bmi160_int_settg *int_config, const struct bmi160_dev *dev)
  2331. {
  2332. int8_t rslt;
  2333. /* Null-pointer check */
  2334. rslt = null_ptr_check(dev);
  2335. if ((rslt != BMI160_OK) || (int_config == NULL)) {
  2336. rslt = BMI160_E_NULL_PTR;
  2337. } else {
  2338. /* updating the interrupt structure to local structure */
  2339. struct bmi160_acc_orient_int_cfg *orient_int_cfg = &(int_config->int_type_cfg.acc_orient_int);
  2340. rslt = enable_orient_int(orient_int_cfg, dev);
  2341. if (rslt == BMI160_OK) {
  2342. /* Configure Interrupt pins */
  2343. rslt = set_intr_pin_config(int_config, dev);
  2344. if (rslt == BMI160_OK) {
  2345. /* map INT pin to orient interrupt */
  2346. rslt = map_feature_interrupt(int_config, dev);
  2347. if (rslt == BMI160_OK)
  2348. /* configure the
  2349. * orientation setting*/
  2350. rslt = config_orient_int_settg(orient_int_cfg, dev);
  2351. }
  2352. }
  2353. }
  2354. return rslt;
  2355. }
  2356. /*!
  2357. * @brief This API sets the flat interrupt of the sensor.This interrupt
  2358. * occurs in case of flat orientation
  2359. */
  2360. static int8_t set_accel_flat_detect_int(struct bmi160_int_settg *int_config, const struct bmi160_dev *dev)
  2361. {
  2362. int8_t rslt;
  2363. /* Null-pointer check */
  2364. rslt = null_ptr_check(dev);
  2365. if ((rslt != BMI160_OK) || (int_config == NULL)) {
  2366. rslt = BMI160_E_NULL_PTR;
  2367. } else {
  2368. /* updating the interrupt structure to local structure */
  2369. struct bmi160_acc_flat_detect_int_cfg *flat_detect_int = &(int_config->int_type_cfg.acc_flat_int);
  2370. /* enable the flat interrupt */
  2371. rslt = enable_flat_int(flat_detect_int, dev);
  2372. if (rslt == BMI160_OK) {
  2373. /* Configure Interrupt pins */
  2374. rslt = set_intr_pin_config(int_config, dev);
  2375. if (rslt == BMI160_OK) {
  2376. /* map INT pin to flat interrupt */
  2377. rslt = map_feature_interrupt(int_config, dev);
  2378. if (rslt == BMI160_OK)
  2379. /* configure the flat setting*/
  2380. rslt = config_flat_int_settg(flat_detect_int, dev);
  2381. }
  2382. }
  2383. }
  2384. return rslt;
  2385. }
  2386. /*!
  2387. * @brief This API sets the low-g interrupt of the sensor.This interrupt
  2388. * occurs during free-fall.
  2389. */
  2390. static int8_t set_accel_low_g_int(struct bmi160_int_settg *int_config, const struct bmi160_dev *dev)
  2391. {
  2392. int8_t rslt;
  2393. /* Null-pointer check */
  2394. rslt = null_ptr_check(dev);
  2395. if ((rslt != BMI160_OK) || (int_config == NULL)) {
  2396. rslt = BMI160_E_NULL_PTR;
  2397. } else {
  2398. /* updating the interrupt structure to local structure */
  2399. struct bmi160_acc_low_g_int_cfg *low_g_int = &(int_config->int_type_cfg.acc_low_g_int);
  2400. /* Enable the low-g interrupt*/
  2401. rslt = enable_low_g_int (low_g_int, dev);
  2402. if (rslt == BMI160_OK) {
  2403. /* Configure Interrupt pins */
  2404. rslt = set_intr_pin_config(int_config, dev);
  2405. if (rslt == BMI160_OK) {
  2406. /* Map INT pin to low-g interrupt */
  2407. rslt = map_feature_interrupt(int_config, dev);
  2408. if (rslt == BMI160_OK) {
  2409. /* configure the data source
  2410. * for low-g interrupt*/
  2411. rslt = config_low_g_data_src(low_g_int, dev);
  2412. if (rslt == BMI160_OK)
  2413. rslt = config_low_g_int_settg(low_g_int, dev);
  2414. }
  2415. }
  2416. }
  2417. }
  2418. return rslt;
  2419. }
  2420. /*!
  2421. * @brief This API sets the high-g interrupt of the sensor.The interrupt
  2422. * occurs if the absolute value of acceleration data of any enabled axis
  2423. * exceeds the programmed threshold and the sign of the value does not
  2424. * change for a preset duration.
  2425. */
  2426. static int8_t set_accel_high_g_int(struct bmi160_int_settg *int_config, const struct bmi160_dev *dev)
  2427. {
  2428. int8_t rslt;
  2429. /* Null-pointer check */
  2430. rslt = null_ptr_check(dev);
  2431. if ((rslt != BMI160_OK) || (int_config == NULL)) {
  2432. rslt = BMI160_E_NULL_PTR;
  2433. } else {
  2434. /* updating the interrupt structure to local structure */
  2435. struct bmi160_acc_high_g_int_cfg *high_g_int_cfg = &(int_config->int_type_cfg.acc_high_g_int);
  2436. /* Enable the high-g interrupt */
  2437. rslt = enable_high_g_int(high_g_int_cfg, dev);
  2438. if (rslt == BMI160_OK) {
  2439. /* Configure Interrupt pins */
  2440. rslt = set_intr_pin_config(int_config, dev);
  2441. if (rslt == BMI160_OK) {
  2442. /* Map INT pin to high-g interrupt */
  2443. rslt = map_feature_interrupt(int_config, dev);
  2444. if (rslt == BMI160_OK) {
  2445. /* configure the data source
  2446. * for high-g interrupt*/
  2447. rslt = config_high_g_data_src(high_g_int_cfg, dev);
  2448. if (rslt == BMI160_OK)
  2449. rslt = config_high_g_int_settg(high_g_int_cfg, dev);
  2450. }
  2451. }
  2452. }
  2453. }
  2454. return rslt;
  2455. }
  2456. /*!
  2457. * @brief This API configures the pins to fire the
  2458. * interrupt signal when it occurs.
  2459. */
  2460. static int8_t set_intr_pin_config(const struct bmi160_int_settg *int_config, const struct bmi160_dev *dev)
  2461. {
  2462. int8_t rslt;
  2463. /* configure the behavioural settings of interrupt pin */
  2464. rslt = config_int_out_ctrl(int_config, dev);
  2465. if (rslt == BMI160_OK)
  2466. rslt = config_int_latch(int_config, dev);
  2467. return rslt;
  2468. }
  2469. /*!
  2470. * @brief This internal API is used to validate the device structure pointer for
  2471. * null conditions.
  2472. */
  2473. static int8_t null_ptr_check(const struct bmi160_dev *dev)
  2474. {
  2475. int8_t rslt;
  2476. if ((dev == NULL) || (dev->read == NULL) || (dev->write == NULL) || (dev->delay_ms == NULL)) {
  2477. rslt = BMI160_E_NULL_PTR;
  2478. } else {
  2479. /* Device structure is fine */
  2480. rslt = BMI160_OK;
  2481. }
  2482. return rslt;
  2483. }
  2484. /*!
  2485. * @brief This API sets the default configuration parameters of accel & gyro.
  2486. * Also maintain the previous state of configurations.
  2487. */
  2488. static void default_param_settg(struct bmi160_dev *dev)
  2489. {
  2490. /* Initializing accel and gyro params with
  2491. * default values */
  2492. dev->accel_cfg.bw = BMI160_ACCEL_BW_NORMAL_AVG4;
  2493. dev->accel_cfg.odr = BMI160_ACCEL_ODR_100HZ;
  2494. dev->accel_cfg.power = BMI160_ACCEL_SUSPEND_MODE;
  2495. dev->accel_cfg.range = BMI160_ACCEL_RANGE_2G;
  2496. dev->gyro_cfg.bw = BMI160_GYRO_BW_NORMAL_MODE;
  2497. dev->gyro_cfg.odr = BMI160_GYRO_ODR_100HZ;
  2498. dev->gyro_cfg.power = BMI160_GYRO_SUSPEND_MODE;
  2499. dev->gyro_cfg.range = BMI160_GYRO_RANGE_2000_DPS;
  2500. /* To maintain the previous state of accel configuration */
  2501. dev->prev_accel_cfg = dev->accel_cfg;
  2502. /* To maintain the previous state of gyro configuration */
  2503. dev->prev_gyro_cfg = dev->gyro_cfg;
  2504. }
  2505. /*!
  2506. * @brief This API set the accel configuration.
  2507. */
  2508. static int8_t set_accel_conf(struct bmi160_dev *dev)
  2509. {
  2510. int8_t rslt;
  2511. uint8_t data[2] = {0};
  2512. rslt = check_accel_config(data, dev);
  2513. if (rslt == BMI160_OK) {
  2514. /* Write output data rate and bandwidth */
  2515. rslt = bmi160_set_regs(BMI160_ACCEL_CONFIG_ADDR, &data[0], 1, dev);
  2516. if (rslt == BMI160_OK) {
  2517. dev->prev_accel_cfg.odr = dev->accel_cfg.odr;
  2518. dev->prev_accel_cfg.bw = dev->accel_cfg.bw;
  2519. dev->delay_ms(BMI160_ONE_MS_DELAY);
  2520. /* write accel range */
  2521. rslt = bmi160_set_regs(BMI160_ACCEL_RANGE_ADDR, &data[1], 1, dev);
  2522. if (rslt == BMI160_OK)
  2523. dev->prev_accel_cfg.range = dev->accel_cfg.range;
  2524. }
  2525. }
  2526. return rslt;
  2527. }
  2528. /*!
  2529. * @brief This API check the accel configuration.
  2530. */
  2531. static int8_t check_accel_config(uint8_t *data, const struct bmi160_dev *dev)
  2532. {
  2533. int8_t rslt;
  2534. /* read accel Output data rate and bandwidth */
  2535. rslt = bmi160_get_regs(BMI160_ACCEL_CONFIG_ADDR, data, 2, dev);
  2536. if (rslt == BMI160_OK) {
  2537. rslt = process_accel_odr(&data[0], dev);
  2538. if (rslt == BMI160_OK) {
  2539. rslt = process_accel_bw(&data[0], dev);
  2540. if (rslt == BMI160_OK)
  2541. rslt = process_accel_range(&data[1], dev);
  2542. }
  2543. }
  2544. return rslt;
  2545. }
  2546. /*!
  2547. * @brief This API process the accel odr.
  2548. */
  2549. static int8_t process_accel_odr(uint8_t *data, const struct bmi160_dev *dev)
  2550. {
  2551. int8_t rslt = 0;
  2552. uint8_t temp = 0;
  2553. uint8_t odr = 0;
  2554. if (dev->accel_cfg.odr <= BMI160_ACCEL_ODR_MAX) {
  2555. if (dev->accel_cfg.odr != dev->prev_accel_cfg.odr) {
  2556. odr = (uint8_t)dev->accel_cfg.odr;
  2557. temp = *data & ~BMI160_ACCEL_ODR_MASK;
  2558. /* Adding output data rate */
  2559. *data = temp | (odr & BMI160_ACCEL_ODR_MASK);
  2560. }
  2561. } else {
  2562. rslt = BMI160_E_OUT_OF_RANGE;
  2563. }
  2564. return rslt;
  2565. }
  2566. /*!
  2567. * @brief This API process the accel bandwidth.
  2568. */
  2569. static int8_t process_accel_bw(uint8_t *data, const struct bmi160_dev *dev)
  2570. {
  2571. int8_t rslt = 0;
  2572. uint8_t temp = 0;
  2573. uint8_t bw = 0;
  2574. if (dev->accel_cfg.bw <= BMI160_ACCEL_BW_MAX) {
  2575. if (dev->accel_cfg.bw != dev->prev_accel_cfg.bw) {
  2576. bw = (uint8_t)dev->accel_cfg.bw;
  2577. temp = *data & ~BMI160_ACCEL_BW_MASK;
  2578. /* Adding bandwidth */
  2579. *data = temp | ((bw << 4) & BMI160_ACCEL_ODR_MASK);
  2580. }
  2581. } else {
  2582. rslt = BMI160_E_OUT_OF_RANGE;
  2583. }
  2584. return rslt;
  2585. }
  2586. /*!
  2587. * @brief This API process the accel range.
  2588. */
  2589. static int8_t process_accel_range(uint8_t *data, const struct bmi160_dev *dev)
  2590. {
  2591. int8_t rslt = 0;
  2592. uint8_t temp = 0;
  2593. uint8_t range = 0;
  2594. if (dev->accel_cfg.range <= BMI160_ACCEL_RANGE_MAX) {
  2595. if (dev->accel_cfg.range != dev->prev_accel_cfg.range) {
  2596. range = (uint8_t)dev->accel_cfg.range;
  2597. temp = *data & ~BMI160_ACCEL_RANGE_MASK;
  2598. /* Adding range */
  2599. *data = temp | (range & BMI160_ACCEL_RANGE_MASK);
  2600. }
  2601. } else {
  2602. rslt = BMI160_E_OUT_OF_RANGE;
  2603. }
  2604. return rslt;
  2605. }
  2606. /*!
  2607. * @brief This API checks the invalid settings for ODR & Bw for
  2608. * Accel and Gyro.
  2609. */
  2610. static int8_t check_invalid_settg(const struct bmi160_dev *dev)
  2611. {
  2612. int8_t rslt;
  2613. uint8_t data = 0;
  2614. /* read the error reg */
  2615. rslt = bmi160_get_regs(BMI160_ERROR_REG_ADDR, &data, 1, dev);
  2616. data = data >> 1;
  2617. data = data & BMI160_ERR_REG_MASK;
  2618. if (data == 1)
  2619. rslt = BMI160_E_ACCEL_ODR_BW_INVALID;
  2620. else if (data == 2)
  2621. rslt = BMI160_E_GYRO_ODR_BW_INVALID;
  2622. else if (data == 3)
  2623. rslt = BMI160_E_LWP_PRE_FLTR_INT_INVALID;
  2624. else if (data == 7)
  2625. rslt = BMI160_E_LWP_PRE_FLTR_INVALID;
  2626. return rslt;
  2627. }
  2628. static int8_t set_gyro_conf(struct bmi160_dev *dev)
  2629. {
  2630. int8_t rslt;
  2631. uint8_t data[2] = {0};
  2632. rslt = check_gyro_config(data, dev);
  2633. if (rslt == BMI160_OK) {
  2634. /* Write output data rate and bandwidth */
  2635. rslt = bmi160_set_regs(BMI160_GYRO_CONFIG_ADDR, &data[0], 1, dev);
  2636. if (rslt == BMI160_OK) {
  2637. dev->prev_gyro_cfg.odr = dev->gyro_cfg.odr;
  2638. dev->prev_gyro_cfg.bw = dev->gyro_cfg.bw;
  2639. dev->delay_ms(BMI160_ONE_MS_DELAY);
  2640. /* Write gyro range */
  2641. rslt = bmi160_set_regs(BMI160_GYRO_RANGE_ADDR, &data[1], 1, dev);
  2642. if (rslt == BMI160_OK)
  2643. dev->prev_gyro_cfg.range = dev->gyro_cfg.range;
  2644. }
  2645. }
  2646. return rslt;
  2647. }
  2648. /*!
  2649. * @brief This API check the gyro configuration.
  2650. */
  2651. static int8_t check_gyro_config(uint8_t *data, const struct bmi160_dev *dev)
  2652. {
  2653. int8_t rslt;
  2654. /* read gyro Output data rate and bandwidth */
  2655. rslt = bmi160_get_regs(BMI160_GYRO_CONFIG_ADDR, data, 2, dev);
  2656. if (rslt == BMI160_OK) {
  2657. rslt = process_gyro_odr(&data[0], dev);
  2658. if (rslt == BMI160_OK) {
  2659. rslt = process_gyro_bw(&data[0], dev);
  2660. if (rslt == BMI160_OK)
  2661. rslt = process_gyro_range(&data[1], dev);
  2662. }
  2663. }
  2664. return rslt;
  2665. }
  2666. /*!
  2667. * @brief This API process the gyro odr.
  2668. */
  2669. static int8_t process_gyro_odr(uint8_t *data, const struct bmi160_dev *dev)
  2670. {
  2671. int8_t rslt = 0;
  2672. uint8_t temp = 0;
  2673. uint8_t odr = 0;
  2674. if (dev->gyro_cfg.odr <= BMI160_GYRO_ODR_MAX) {
  2675. if (dev->gyro_cfg.odr != dev->prev_gyro_cfg.odr) {
  2676. odr = (uint8_t)dev->gyro_cfg.odr;
  2677. temp = (*data & ~BMI160_GYRO_ODR_MASK);
  2678. /* Adding output data rate */
  2679. *data = temp | (odr & BMI160_GYRO_ODR_MASK);
  2680. }
  2681. } else {
  2682. rslt = BMI160_E_OUT_OF_RANGE;
  2683. }
  2684. return rslt;
  2685. }
  2686. /*!
  2687. * @brief This API process the gyro bandwidth.
  2688. */
  2689. static int8_t process_gyro_bw(uint8_t *data, const struct bmi160_dev *dev)
  2690. {
  2691. int8_t rslt = 0;
  2692. uint8_t temp = 0;
  2693. uint8_t bw = 0;
  2694. if (dev->gyro_cfg.bw <= BMI160_GYRO_BW_MAX) {
  2695. bw = (uint8_t)dev->gyro_cfg.bw;
  2696. temp = *data & ~BMI160_GYRO_BW_MASK;
  2697. /* Adding bandwidth */
  2698. *data = temp | ((bw << 4) & BMI160_GYRO_BW_MASK);
  2699. } else {
  2700. rslt = BMI160_E_OUT_OF_RANGE;
  2701. }
  2702. return rslt;
  2703. }
  2704. /*!
  2705. * @brief This API process the gyro range.
  2706. */
  2707. static int8_t process_gyro_range(uint8_t *data, const struct bmi160_dev *dev)
  2708. {
  2709. int8_t rslt = 0;
  2710. uint8_t temp = 0;
  2711. uint8_t range = 0;
  2712. if (dev->gyro_cfg.range <= BMI160_GYRO_RANGE_MAX) {
  2713. if (dev->gyro_cfg.range != dev->prev_gyro_cfg.range) {
  2714. range = (uint8_t)dev->gyro_cfg.range;
  2715. temp = *data & ~BMI160_GYRO_RANGE_MSK;
  2716. /* Adding range */
  2717. *data = temp | (range & BMI160_GYRO_RANGE_MSK);
  2718. }
  2719. } else {
  2720. rslt = BMI160_E_OUT_OF_RANGE;
  2721. }
  2722. return rslt;
  2723. }
  2724. /*!
  2725. * @brief This API sets the accel power.
  2726. */
  2727. static int8_t set_accel_pwr(struct bmi160_dev *dev)
  2728. {
  2729. int8_t rslt = 0;
  2730. uint8_t data = 0;
  2731. if ((dev->accel_cfg.power >= BMI160_ACCEL_SUSPEND_MODE) &&
  2732. (dev->accel_cfg.power <= BMI160_ACCEL_LOWPOWER_MODE)) {
  2733. if (dev->accel_cfg.power != dev->prev_accel_cfg.power) {
  2734. rslt = process_under_sampling(&data, dev);
  2735. if (rslt == BMI160_OK) {
  2736. /* Write accel power */
  2737. rslt = bmi160_set_regs(BMI160_COMMAND_REG_ADDR, &dev->accel_cfg.power, 1, dev);
  2738. /* Add delay of 5 ms */
  2739. if (dev->prev_accel_cfg.power == BMI160_ACCEL_SUSPEND_MODE)
  2740. dev->delay_ms(BMI160_ACCEL_DELAY_MS);
  2741. dev->prev_accel_cfg.power = dev->accel_cfg.power;
  2742. }
  2743. }
  2744. } else {
  2745. rslt = BMI160_E_OUT_OF_RANGE;
  2746. }
  2747. return rslt;
  2748. }
  2749. /*!
  2750. * @brief This API process the undersampling setting of Accel.
  2751. */
  2752. static int8_t process_under_sampling(uint8_t *data, const struct bmi160_dev *dev)
  2753. {
  2754. int8_t rslt;
  2755. uint8_t temp = 0;
  2756. uint8_t pre_filter = 0;
  2757. rslt = bmi160_get_regs(BMI160_ACCEL_CONFIG_ADDR, data, 1, dev);
  2758. if (rslt == BMI160_OK) {
  2759. if (dev->accel_cfg.power == BMI160_ACCEL_LOWPOWER_MODE) {
  2760. temp = *data & ~BMI160_ACCEL_UNDERSAMPLING_MASK;
  2761. /* Set under-sampling parameter */
  2762. *data = temp | ((1 << 7) & BMI160_ACCEL_UNDERSAMPLING_MASK);
  2763. /* Write data */
  2764. rslt = bmi160_set_regs(BMI160_ACCEL_CONFIG_ADDR, data, 1, dev);
  2765. /* disable the pre-filter data in
  2766. * low power mode */
  2767. if (rslt == BMI160_OK)
  2768. /* Disable the Pre-filter data*/
  2769. rslt = bmi160_set_regs(BMI160_INT_DATA_0_ADDR, &pre_filter, 2, dev);
  2770. } else {
  2771. if (*data & BMI160_ACCEL_UNDERSAMPLING_MASK) {
  2772. temp = *data & ~BMI160_ACCEL_UNDERSAMPLING_MASK;
  2773. /* disable under-sampling parameter
  2774. if already enabled */
  2775. *data = temp;
  2776. /* Write data */
  2777. rslt = bmi160_set_regs(BMI160_ACCEL_CONFIG_ADDR, data, 1, dev);
  2778. }
  2779. }
  2780. }
  2781. return rslt;
  2782. }
  2783. /*!
  2784. * @brief This API sets the gyro power mode.
  2785. */
  2786. static int8_t set_gyro_pwr(struct bmi160_dev *dev)
  2787. {
  2788. int8_t rslt = 0;
  2789. if ((dev->gyro_cfg.power == BMI160_GYRO_SUSPEND_MODE) || (dev->gyro_cfg.power == BMI160_GYRO_NORMAL_MODE)
  2790. || (dev->gyro_cfg.power == BMI160_GYRO_FASTSTARTUP_MODE)) {
  2791. if (dev->gyro_cfg.power != dev->prev_gyro_cfg.power) {
  2792. /* Write gyro power */
  2793. rslt = bmi160_set_regs(BMI160_COMMAND_REG_ADDR, &dev->gyro_cfg.power, 1, dev);
  2794. if (dev->prev_gyro_cfg.power ==
  2795. BMI160_GYRO_SUSPEND_MODE) {
  2796. /* Delay of 81 ms */
  2797. dev->delay_ms(BMI160_GYRO_DELAY_MS);
  2798. } else if ((dev->prev_gyro_cfg.power == BMI160_GYRO_FASTSTARTUP_MODE)
  2799. && (dev->gyro_cfg.power == BMI160_GYRO_NORMAL_MODE)) {
  2800. /* This delay is required for transition from
  2801. fast-startup mode to normal mode */
  2802. dev->delay_ms(10);
  2803. } else {
  2804. /* do nothing */
  2805. }
  2806. dev->prev_gyro_cfg.power = dev->gyro_cfg.power;
  2807. }
  2808. } else {
  2809. rslt = BMI160_E_OUT_OF_RANGE;
  2810. }
  2811. return rslt;
  2812. }
  2813. /*!
  2814. * @brief This API reads accel data along with sensor time if time is requested
  2815. * by user. Kindly refer the user guide(README.md) for more info.
  2816. */
  2817. static int8_t get_accel_data(uint8_t len, struct bmi160_sensor_data *accel, const struct bmi160_dev *dev)
  2818. {
  2819. int8_t rslt;
  2820. uint8_t idx = 0;
  2821. uint8_t data_array[9] = {0};
  2822. uint8_t time_0 = 0;
  2823. uint16_t time_1 = 0;
  2824. uint32_t time_2 = 0;
  2825. uint8_t lsb;
  2826. uint8_t msb;
  2827. int16_t msblsb;
  2828. /* read accel sensor data along with time if requested */
  2829. rslt = bmi160_get_regs(BMI160_ACCEL_DATA_ADDR, data_array, 6 + len, dev);
  2830. if (rslt == BMI160_OK) {
  2831. /* Accel Data */
  2832. lsb = data_array[idx++];
  2833. msb = data_array[idx++];
  2834. msblsb = (int16_t)((msb << 8) | lsb);
  2835. accel->x = msblsb; /* Data in X axis */
  2836. lsb = data_array[idx++];
  2837. msb = data_array[idx++];
  2838. msblsb = (int16_t)((msb << 8) | lsb);
  2839. accel->y = msblsb; /* Data in Y axis */
  2840. lsb = data_array[idx++];
  2841. msb = data_array[idx++];
  2842. msblsb = (int16_t)((msb << 8) | lsb);
  2843. accel->z = msblsb; /* Data in Z axis */
  2844. if (len == 3) {
  2845. time_0 = data_array[idx++];
  2846. time_1 = (uint16_t)(data_array[idx++] << 8);
  2847. time_2 = (uint32_t)(data_array[idx++] << 16);
  2848. accel->sensortime = (uint32_t)(time_2 | time_1 | time_0);
  2849. } else {
  2850. accel->sensortime = 0;
  2851. }
  2852. } else {
  2853. rslt = BMI160_E_COM_FAIL;
  2854. }
  2855. return rslt;
  2856. }
  2857. /*!
  2858. * @brief This API reads accel data along with sensor time if time is requested
  2859. * by user. Kindly refer the user guide(README.md) for more info.
  2860. */
  2861. static int8_t get_gyro_data(uint8_t len, struct bmi160_sensor_data *gyro, const struct bmi160_dev *dev)
  2862. {
  2863. int8_t rslt;
  2864. uint8_t idx = 0;
  2865. uint8_t data_array[15] = {0};
  2866. uint8_t time_0 = 0;
  2867. uint16_t time_1 = 0;
  2868. uint32_t time_2 = 0;
  2869. uint8_t lsb;
  2870. uint8_t msb;
  2871. int16_t msblsb;
  2872. if (len == 0) {
  2873. /* read gyro data only */
  2874. rslt = bmi160_get_regs(BMI160_GYRO_DATA_ADDR, data_array, 6, dev);
  2875. if (rslt == BMI160_OK) {
  2876. /* Gyro Data */
  2877. lsb = data_array[idx++];
  2878. msb = data_array[idx++];
  2879. msblsb = (int16_t)((msb << 8) | lsb);
  2880. gyro->x = msblsb; /* Data in X axis */
  2881. lsb = data_array[idx++];
  2882. msb = data_array[idx++];
  2883. msblsb = (int16_t)((msb << 8) | lsb);
  2884. gyro->y = msblsb; /* Data in Y axis */
  2885. lsb = data_array[idx++];
  2886. msb = data_array[idx++];
  2887. msblsb = (int16_t)((msb << 8) | lsb);
  2888. gyro->z = msblsb; /* Data in Z axis */
  2889. gyro->sensortime = 0;
  2890. } else {
  2891. rslt = BMI160_E_COM_FAIL;
  2892. }
  2893. } else {
  2894. /* read gyro sensor data along with time */
  2895. rslt = bmi160_get_regs(BMI160_GYRO_DATA_ADDR, data_array, 12 + len, dev);
  2896. if (rslt == BMI160_OK) {
  2897. /* Gyro Data */
  2898. lsb = data_array[idx++];
  2899. msb = data_array[idx++];
  2900. msblsb = (int16_t)((msb << 8) | lsb);
  2901. gyro->x = msblsb; /* gyro X axis data */
  2902. lsb = data_array[idx++];
  2903. msb = data_array[idx++];
  2904. msblsb = (int16_t)((msb << 8) | lsb);
  2905. gyro->y = msblsb; /* gyro Y axis data */
  2906. lsb = data_array[idx++];
  2907. msb = data_array[idx++];
  2908. msblsb = (int16_t)((msb << 8) | lsb);
  2909. gyro->z = msblsb; /* gyro Z axis data */
  2910. idx = idx + 6;
  2911. time_0 = data_array[idx++];
  2912. time_1 = (uint16_t)(data_array[idx++] << 8);
  2913. time_2 = (uint32_t)(data_array[idx++] << 16);
  2914. gyro->sensortime = (uint32_t)(time_2 | time_1 | time_0);
  2915. } else {
  2916. rslt = BMI160_E_COM_FAIL;
  2917. }
  2918. }
  2919. return rslt;
  2920. }
  2921. /*!
  2922. * @brief This API reads accel and gyro data along with sensor time
  2923. * if time is requested by user.
  2924. * Kindly refer the user guide(README.md) for more info.
  2925. */
  2926. static int8_t get_accel_gyro_data(uint8_t len, struct bmi160_sensor_data *accel, struct bmi160_sensor_data *gyro,
  2927. const struct bmi160_dev *dev)
  2928. {
  2929. int8_t rslt;
  2930. uint8_t idx = 0;
  2931. uint8_t data_array[15] = {0};
  2932. uint8_t time_0 = 0;
  2933. uint16_t time_1 = 0;
  2934. uint32_t time_2 = 0;
  2935. uint8_t lsb;
  2936. uint8_t msb;
  2937. int16_t msblsb;
  2938. /* read both accel and gyro sensor data
  2939. * along with time if requested */
  2940. rslt = bmi160_get_regs(BMI160_GYRO_DATA_ADDR, data_array, 12 + len, dev);
  2941. if (rslt == BMI160_OK) {
  2942. /* Gyro Data */
  2943. lsb = data_array[idx++];
  2944. msb = data_array[idx++];
  2945. msblsb = (int16_t)((msb << 8) | lsb);
  2946. gyro->x = msblsb; /* gyro X axis data */
  2947. lsb = data_array[idx++];
  2948. msb = data_array[idx++];
  2949. msblsb = (int16_t)((msb << 8) | lsb);
  2950. gyro->y = msblsb; /* gyro Y axis data */
  2951. lsb = data_array[idx++];
  2952. msb = data_array[idx++];
  2953. msblsb = (int16_t)((msb << 8) | lsb);
  2954. gyro->z = msblsb; /* gyro Z axis data */
  2955. /* Accel Data */
  2956. lsb = data_array[idx++];
  2957. msb = data_array[idx++];
  2958. msblsb = (int16_t)((msb << 8) | lsb);
  2959. accel->x = (int16_t)msblsb; /* accel X axis data */
  2960. lsb = data_array[idx++];
  2961. msb = data_array[idx++];
  2962. msblsb = (int16_t)((msb << 8) | lsb);
  2963. accel->y = (int16_t)msblsb; /* accel Y axis data */
  2964. lsb = data_array[idx++];
  2965. msb = data_array[idx++];
  2966. msblsb = (int16_t)((msb << 8) | lsb);
  2967. accel->z = (int16_t)msblsb; /* accel Z axis data */
  2968. if (len == 3) {
  2969. time_0 = data_array[idx++];
  2970. time_1 = (uint16_t)(data_array[idx++] << 8);
  2971. time_2 = (uint32_t)(data_array[idx++] << 16);
  2972. accel->sensortime = (uint32_t)(time_2 | time_1 | time_0);
  2973. gyro->sensortime = (uint32_t)(time_2 | time_1 | time_0);
  2974. } else {
  2975. accel->sensortime = 0;
  2976. gyro->sensortime = 0;
  2977. }
  2978. } else {
  2979. rslt = BMI160_E_COM_FAIL;
  2980. }
  2981. return rslt;
  2982. }
  2983. /*!
  2984. * @brief This API enables the any-motion interrupt for accel.
  2985. */
  2986. static int8_t enable_accel_any_motion_int(const struct bmi160_acc_any_mot_int_cfg *any_motion_int_cfg,
  2987. struct bmi160_dev *dev)
  2988. {
  2989. int8_t rslt;
  2990. uint8_t data = 0;
  2991. uint8_t temp = 0;
  2992. /* Enable any motion x, any motion y, any motion z
  2993. in Int Enable 0 register */
  2994. rslt = bmi160_get_regs(BMI160_INT_ENABLE_0_ADDR, &data, 1, dev);
  2995. if (rslt == BMI160_OK) {
  2996. if (any_motion_int_cfg->anymotion_en == BMI160_ENABLE) {
  2997. temp = data & ~BMI160_ANY_MOTION_X_INT_EN_MASK;
  2998. /* Adding Any_motion x axis */
  2999. data = temp | (any_motion_int_cfg->anymotion_x & BMI160_ANY_MOTION_X_INT_EN_MASK);
  3000. temp = data & ~BMI160_ANY_MOTION_Y_INT_EN_MASK;
  3001. /* Adding Any_motion y axis */
  3002. data = temp | ((any_motion_int_cfg->anymotion_y << 1) & BMI160_ANY_MOTION_Y_INT_EN_MASK);
  3003. temp = data & ~BMI160_ANY_MOTION_Z_INT_EN_MASK;
  3004. /* Adding Any_motion z axis */
  3005. data = temp | ((any_motion_int_cfg->anymotion_z << 2) & BMI160_ANY_MOTION_Z_INT_EN_MASK);
  3006. /* any-motion feature selected*/
  3007. dev->any_sig_sel = BMI160_ANY_MOTION_ENABLED;
  3008. } else {
  3009. data = data & ~BMI160_ANY_MOTION_ALL_INT_EN_MASK;
  3010. /* neither any-motion feature nor sig-motion selected */
  3011. dev->any_sig_sel = BMI160_BOTH_ANY_SIG_MOTION_DISABLED;
  3012. }
  3013. /* write data to Int Enable 0 register */
  3014. rslt = bmi160_set_regs(BMI160_INT_ENABLE_0_ADDR, &data, 1, dev);
  3015. }
  3016. return rslt;
  3017. }
  3018. /*!
  3019. * @brief This API disable the sig-motion interrupt.
  3020. */
  3021. static int8_t disable_sig_motion_int(const struct bmi160_dev *dev)
  3022. {
  3023. int8_t rslt;
  3024. uint8_t data = 0;
  3025. uint8_t temp = 0;
  3026. /* Disabling Significant motion interrupt if enabled */
  3027. rslt = bmi160_get_regs(BMI160_INT_MOTION_3_ADDR, &data, 1, dev);
  3028. if (rslt == BMI160_OK) {
  3029. temp = (data & BMI160_SIG_MOTION_SEL_MASK);
  3030. if (temp) {
  3031. temp = data & ~BMI160_SIG_MOTION_SEL_MASK;
  3032. data = temp;
  3033. /* Write data to register */
  3034. rslt = bmi160_set_regs(BMI160_INT_MOTION_3_ADDR, &data, 1, dev);
  3035. }
  3036. }
  3037. return rslt;
  3038. }
  3039. /*!
  3040. * @brief This API is used to map/unmap the Any/Sig motion, Step det/Low-g,
  3041. * Double tap, Single tap, Orientation, Flat, High-G, Nomotion interrupt pins.
  3042. */
  3043. static int8_t map_feature_interrupt(const struct bmi160_int_settg *int_config, const struct bmi160_dev *dev)
  3044. {
  3045. int8_t rslt;
  3046. uint8_t data[3] = {0, 0, 0};
  3047. uint8_t temp[3] = {0, 0, 0};
  3048. rslt = bmi160_get_regs(BMI160_INT_MAP_0_ADDR, data, 3, dev);
  3049. if (rslt == BMI160_OK) {
  3050. temp[0] = data[0] & ~int_mask_lookup_table[int_config->int_type];
  3051. temp[2] = data[2] & ~int_mask_lookup_table[int_config->int_type];
  3052. switch (int_config->int_channel) {
  3053. case BMI160_INT_CHANNEL_NONE:
  3054. data[0] = temp[0];
  3055. data[2] = temp[2];
  3056. break;
  3057. case BMI160_INT_CHANNEL_1:
  3058. data[0] = temp[0] | int_mask_lookup_table[int_config->int_type];
  3059. data[2] = temp[2];
  3060. break;
  3061. case BMI160_INT_CHANNEL_2:
  3062. data[2] = temp[2] | int_mask_lookup_table[int_config->int_type];
  3063. data[0] = temp[0];
  3064. break;
  3065. case BMI160_INT_CHANNEL_BOTH:
  3066. data[0] = temp[0] | int_mask_lookup_table[int_config->int_type];
  3067. data[2] = temp[2] | int_mask_lookup_table[int_config->int_type];
  3068. break;
  3069. default:
  3070. rslt = BMI160_E_OUT_OF_RANGE;
  3071. }
  3072. if (rslt == BMI160_OK)
  3073. rslt = bmi160_set_regs(BMI160_INT_MAP_0_ADDR, data, 3, dev);
  3074. }
  3075. return rslt;
  3076. }
  3077. /*!
  3078. * @brief This API is used to map/unmap the Dataready(Accel & Gyro), FIFO full
  3079. * and FIFO watermark interrupt.
  3080. */
  3081. static int8_t map_hardware_interrupt(const struct bmi160_int_settg *int_config, const struct bmi160_dev *dev)
  3082. {
  3083. int8_t rslt;
  3084. uint8_t data = 0;
  3085. uint8_t temp = 0;
  3086. rslt = bmi160_get_regs(BMI160_INT_MAP_1_ADDR, &data, 1, dev);
  3087. if (rslt == BMI160_OK) {
  3088. temp = data & ~int_mask_lookup_table[int_config->int_type];
  3089. temp = temp & ~((uint8_t)(int_mask_lookup_table[int_config->int_type] << 4));
  3090. switch (int_config->int_channel) {
  3091. case BMI160_INT_CHANNEL_NONE:
  3092. data = temp;
  3093. break;
  3094. case BMI160_INT_CHANNEL_1:
  3095. data = temp | (uint8_t)((int_mask_lookup_table[int_config->int_type]) << 4);
  3096. break;
  3097. case BMI160_INT_CHANNEL_2:
  3098. data = temp | int_mask_lookup_table[int_config->int_type];
  3099. break;
  3100. case BMI160_INT_CHANNEL_BOTH:
  3101. data = temp | int_mask_lookup_table[int_config->int_type];
  3102. data = data | (uint8_t)((int_mask_lookup_table[int_config->int_type]) << 4);
  3103. break;
  3104. default:
  3105. rslt = BMI160_E_OUT_OF_RANGE;
  3106. }
  3107. if (rslt == BMI160_OK)
  3108. rslt = bmi160_set_regs(BMI160_INT_MAP_1_ADDR, &data, 1, dev);
  3109. }
  3110. return rslt;
  3111. }
  3112. /*!
  3113. * @brief This API configure the source of data(filter & pre-filter)
  3114. * for any-motion interrupt.
  3115. */
  3116. static int8_t config_any_motion_src(const struct bmi160_acc_any_mot_int_cfg *any_motion_int_cfg,
  3117. const struct bmi160_dev *dev)
  3118. {
  3119. int8_t rslt;
  3120. uint8_t data = 0;
  3121. uint8_t temp = 0;
  3122. /* Configure Int data 1 register to add source of interrupt */
  3123. rslt = bmi160_get_regs(BMI160_INT_DATA_1_ADDR, &data, 1, dev);
  3124. if (rslt == BMI160_OK) {
  3125. temp = data & ~BMI160_MOTION_SRC_INT_MASK;
  3126. data = temp | ((any_motion_int_cfg->anymotion_data_src << 7) & BMI160_MOTION_SRC_INT_MASK);
  3127. /* Write data to DATA 1 address */
  3128. rslt = bmi160_set_regs(BMI160_INT_DATA_1_ADDR, &data, 1, dev);
  3129. }
  3130. return rslt;
  3131. }
  3132. /*!
  3133. * @brief This API configure the duration and threshold of
  3134. * any-motion interrupt.
  3135. */
  3136. static int8_t config_any_dur_threshold(const struct bmi160_acc_any_mot_int_cfg *any_motion_int_cfg,
  3137. const struct bmi160_dev *dev)
  3138. {
  3139. int8_t rslt;
  3140. uint8_t data = 0;
  3141. uint8_t temp = 0;
  3142. uint8_t data_array[2] = {0};
  3143. uint8_t dur;
  3144. /* Configure Int Motion 0 register */
  3145. rslt = bmi160_get_regs(BMI160_INT_MOTION_0_ADDR, &data, 1, dev);
  3146. if (rslt == BMI160_OK) {
  3147. /* slope duration */
  3148. dur = (uint8_t)any_motion_int_cfg->anymotion_dur;
  3149. temp = data & ~BMI160_SLOPE_INT_DUR_MASK;
  3150. data = temp | (dur & BMI160_MOTION_SRC_INT_MASK);
  3151. data_array[0] = data;
  3152. /* add slope threshold */
  3153. data_array[1] = any_motion_int_cfg->anymotion_thr;
  3154. /* INT MOTION 0 and INT MOTION 1 address lie consecutively,
  3155. hence writing data to respective registers at one go */
  3156. /* Writing to Int_motion 0 and
  3157. Int_motion 1 Address simultaneously */
  3158. rslt = bmi160_set_regs(BMI160_INT_MOTION_0_ADDR, data_array, 2, dev);
  3159. }
  3160. return rslt;
  3161. }
  3162. /*!
  3163. * @brief This API configure necessary setting of any-motion interrupt.
  3164. */
  3165. static int8_t config_any_motion_int_settg(const struct bmi160_int_settg *int_config,
  3166. const struct bmi160_acc_any_mot_int_cfg *any_motion_int_cfg,
  3167. const struct bmi160_dev *dev)
  3168. {
  3169. int8_t rslt;
  3170. /* Configure Interrupt pins */
  3171. rslt = set_intr_pin_config(int_config, dev);
  3172. if (rslt == BMI160_OK) {
  3173. rslt = disable_sig_motion_int(dev);
  3174. if (rslt == BMI160_OK) {
  3175. rslt = map_feature_interrupt(int_config, dev);
  3176. if (rslt == BMI160_OK) {
  3177. rslt = config_any_motion_src(any_motion_int_cfg, dev);
  3178. if (rslt == BMI160_OK)
  3179. rslt = config_any_dur_threshold(any_motion_int_cfg, dev);
  3180. }
  3181. }
  3182. }
  3183. return rslt;
  3184. }
  3185. /*!
  3186. * @brief This API enable the data ready interrupt.
  3187. */
  3188. static int8_t enable_data_ready_int(const struct bmi160_dev *dev)
  3189. {
  3190. int8_t rslt;
  3191. uint8_t data = 0;
  3192. uint8_t temp = 0;
  3193. /* Enable data ready interrupt in Int Enable 1 register */
  3194. rslt = bmi160_get_regs(BMI160_INT_ENABLE_1_ADDR, &data, 1, dev);
  3195. if (rslt == BMI160_OK) {
  3196. temp = data & ~BMI160_DATA_RDY_INT_EN_MASK;
  3197. data = temp | ((1 << 4) & BMI160_DATA_RDY_INT_EN_MASK);
  3198. /* Writing data to INT ENABLE 1 Address */
  3199. rslt = bmi160_set_regs(BMI160_INT_ENABLE_1_ADDR, &data, 1, dev);
  3200. }
  3201. return rslt;
  3202. }
  3203. /*!
  3204. * @brief This API enables the no motion/slow motion interrupt.
  3205. */
  3206. static int8_t enable_no_motion_int(const struct bmi160_acc_no_motion_int_cfg *no_mot_int_cfg,
  3207. const struct bmi160_dev *dev)
  3208. {
  3209. int8_t rslt;
  3210. uint8_t data = 0;
  3211. uint8_t temp = 0;
  3212. /* Enable no motion x, no motion y, no motion z
  3213. in Int Enable 2 register */
  3214. rslt = bmi160_get_regs(BMI160_INT_ENABLE_2_ADDR, &data, 1, dev);
  3215. if (rslt == BMI160_OK) {
  3216. if (no_mot_int_cfg->no_motion_x == 1) {
  3217. temp = data & ~BMI160_NO_MOTION_X_INT_EN_MASK;
  3218. /* Adding No_motion x axis */
  3219. data = temp | (1 & BMI160_NO_MOTION_X_INT_EN_MASK);
  3220. }
  3221. if (no_mot_int_cfg->no_motion_y == 1) {
  3222. temp = data & ~BMI160_NO_MOTION_Y_INT_EN_MASK;
  3223. /* Adding No_motion x axis */
  3224. data = temp | ((1 << 1) & BMI160_NO_MOTION_Y_INT_EN_MASK);
  3225. }
  3226. if (no_mot_int_cfg->no_motion_z == 1) {
  3227. temp = data & ~BMI160_NO_MOTION_Z_INT_EN_MASK;
  3228. /* Adding No_motion x axis */
  3229. data = temp | ((1 << 2) & BMI160_NO_MOTION_Z_INT_EN_MASK);
  3230. }
  3231. /* write data to Int Enable 2 register */
  3232. rslt = bmi160_set_regs(BMI160_INT_ENABLE_2_ADDR, &data, 1, dev);
  3233. }
  3234. return rslt;
  3235. }
  3236. /*!
  3237. * @brief This API configure the interrupt PIN setting for
  3238. * no motion/slow motion interrupt.
  3239. */
  3240. static int8_t config_no_motion_int_settg(const struct bmi160_int_settg *int_config,
  3241. const struct bmi160_acc_no_motion_int_cfg *no_mot_int_cfg,
  3242. const struct bmi160_dev *dev)
  3243. {
  3244. int8_t rslt;
  3245. /* Configure Interrupt pins */
  3246. rslt = set_intr_pin_config(int_config, dev);
  3247. if (rslt == BMI160_OK) {
  3248. rslt = map_feature_interrupt(int_config, dev);
  3249. if (rslt == BMI160_OK) {
  3250. rslt = config_no_motion_data_src(no_mot_int_cfg, dev);
  3251. if (rslt == BMI160_OK)
  3252. rslt = config_no_motion_dur_thr(no_mot_int_cfg, dev);
  3253. }
  3254. }
  3255. return rslt;
  3256. }
  3257. /*!
  3258. * @brief This API configure the source of interrupt for no motion.
  3259. */
  3260. static int8_t config_no_motion_data_src(const struct bmi160_acc_no_motion_int_cfg *no_mot_int_cfg,
  3261. const struct bmi160_dev *dev)
  3262. {
  3263. int8_t rslt;
  3264. uint8_t data = 0;
  3265. uint8_t temp = 0;
  3266. /* Configure Int data 1 register to add source of interrupt */
  3267. rslt = bmi160_get_regs(BMI160_INT_DATA_1_ADDR, &data, 1, dev);
  3268. if (rslt == BMI160_OK) {
  3269. temp = data & ~BMI160_MOTION_SRC_INT_MASK;
  3270. data = temp | ((no_mot_int_cfg->no_motion_src << 7) & BMI160_MOTION_SRC_INT_MASK);
  3271. /* Write data to DATA 1 address */
  3272. rslt = bmi160_set_regs(BMI160_INT_DATA_1_ADDR, &data, 1, dev);
  3273. }
  3274. return rslt;
  3275. }
  3276. /*!
  3277. * @brief This API configure the duration and threshold of
  3278. * no motion/slow motion interrupt along with selection of no/slow motion.
  3279. */
  3280. static int8_t config_no_motion_dur_thr(const struct bmi160_acc_no_motion_int_cfg *no_mot_int_cfg,
  3281. const struct bmi160_dev *dev)
  3282. {
  3283. int8_t rslt;
  3284. uint8_t data = 0;
  3285. uint8_t temp = 0;
  3286. uint8_t temp_1 = 0;
  3287. uint8_t reg_addr;
  3288. uint8_t data_array[2] = {0};
  3289. /* Configuring INT_MOTION register */
  3290. reg_addr = BMI160_INT_MOTION_0_ADDR;
  3291. rslt = bmi160_get_regs(reg_addr, &data, 1, dev);
  3292. if (rslt == BMI160_OK) {
  3293. temp = data & ~BMI160_NO_MOTION_INT_DUR_MASK;
  3294. /* Adding no_motion duration */
  3295. data = temp | ((no_mot_int_cfg->no_motion_dur << 2) & BMI160_NO_MOTION_INT_DUR_MASK);
  3296. /* Write data to NO_MOTION 0 address */
  3297. rslt = bmi160_set_regs(reg_addr, &data, 1, dev);
  3298. if (rslt == BMI160_OK) {
  3299. reg_addr = BMI160_INT_MOTION_3_ADDR;
  3300. rslt = bmi160_get_regs(reg_addr, &data, 1, dev);
  3301. if (rslt == BMI160_OK) {
  3302. temp = data & ~BMI160_NO_MOTION_SEL_BIT_MASK;
  3303. /* Adding no_motion_sel bit */
  3304. temp_1 = (no_mot_int_cfg->no_motion_sel & BMI160_NO_MOTION_SEL_BIT_MASK);
  3305. data = (temp | temp_1);
  3306. data_array[1] = data;
  3307. /* Adding no motion threshold */
  3308. data_array[0] = no_mot_int_cfg->no_motion_thres;
  3309. reg_addr = BMI160_INT_MOTION_2_ADDR;
  3310. /* writing data to INT_MOTION 2 and INT_MOTION 3
  3311. * address simultaneously */
  3312. rslt = bmi160_set_regs(reg_addr, data_array, 2, dev);
  3313. }
  3314. }
  3315. }
  3316. return rslt;
  3317. }
  3318. /*!
  3319. * @brief This API enables the sig-motion motion interrupt.
  3320. */
  3321. static int8_t enable_sig_motion_int(const struct bmi160_acc_sig_mot_int_cfg *sig_mot_int_cfg, struct bmi160_dev *dev)
  3322. {
  3323. int8_t rslt;
  3324. uint8_t data = 0;
  3325. uint8_t temp = 0;
  3326. /* For significant motion,enable any motion x,any motion y,
  3327. * any motion z in Int Enable 0 register */
  3328. rslt = bmi160_get_regs(BMI160_INT_ENABLE_0_ADDR, &data, 1, dev);
  3329. if (rslt == BMI160_OK) {
  3330. if (sig_mot_int_cfg->sig_en == BMI160_ENABLE) {
  3331. temp = data & ~BMI160_SIG_MOTION_INT_EN_MASK;
  3332. data = temp | (7 & BMI160_SIG_MOTION_INT_EN_MASK);
  3333. /* sig-motion feature selected*/
  3334. dev->any_sig_sel = BMI160_SIG_MOTION_ENABLED;
  3335. } else {
  3336. data = data & ~BMI160_SIG_MOTION_INT_EN_MASK;
  3337. /* neither any-motion feature nor sig-motion selected */
  3338. dev->any_sig_sel = BMI160_BOTH_ANY_SIG_MOTION_DISABLED;
  3339. }
  3340. /* write data to Int Enable 0 register */
  3341. rslt = bmi160_set_regs(BMI160_INT_ENABLE_0_ADDR, &data, 1, dev);
  3342. }
  3343. return rslt;
  3344. }
  3345. /*!
  3346. * @brief This API configure the interrupt PIN setting for
  3347. * significant motion interrupt.
  3348. */
  3349. static int8_t config_sig_motion_int_settg(const struct bmi160_int_settg *int_config,
  3350. const struct bmi160_acc_sig_mot_int_cfg *sig_mot_int_cfg,
  3351. const struct bmi160_dev *dev)
  3352. {
  3353. int8_t rslt;
  3354. /* Configure Interrupt pins */
  3355. rslt = set_intr_pin_config(int_config, dev);
  3356. if (rslt == BMI160_OK) {
  3357. rslt = map_feature_interrupt(int_config, dev);
  3358. if (rslt == BMI160_OK) {
  3359. rslt = config_sig_motion_data_src(sig_mot_int_cfg, dev);
  3360. if (rslt == BMI160_OK)
  3361. rslt = config_sig_dur_threshold(sig_mot_int_cfg, dev);
  3362. }
  3363. }
  3364. return rslt;
  3365. }
  3366. /*!
  3367. * @brief This API configure the source of data(filter & pre-filter)
  3368. * for sig motion interrupt.
  3369. */
  3370. static int8_t config_sig_motion_data_src(const struct bmi160_acc_sig_mot_int_cfg *sig_mot_int_cfg,
  3371. const struct bmi160_dev *dev)
  3372. {
  3373. int8_t rslt;
  3374. uint8_t data = 0;
  3375. uint8_t temp = 0;
  3376. /* Configure Int data 1 register to add source of interrupt */
  3377. rslt = bmi160_get_regs(BMI160_INT_DATA_1_ADDR, &data, 1, dev);
  3378. if (rslt == BMI160_OK) {
  3379. temp = data & ~BMI160_MOTION_SRC_INT_MASK;
  3380. data = temp | ((sig_mot_int_cfg->sig_data_src << 7) & BMI160_MOTION_SRC_INT_MASK);
  3381. /* Write data to DATA 1 address */
  3382. rslt = bmi160_set_regs(BMI160_INT_DATA_1_ADDR, &data, 1, dev);
  3383. }
  3384. return rslt;
  3385. }
  3386. /*!
  3387. * @brief This API configure the threshold, skip and proof time of
  3388. * sig motion interrupt.
  3389. */
  3390. static int8_t config_sig_dur_threshold(const struct bmi160_acc_sig_mot_int_cfg *sig_mot_int_cfg,
  3391. const struct bmi160_dev *dev)
  3392. {
  3393. int8_t rslt;
  3394. uint8_t data;
  3395. uint8_t temp = 0;
  3396. /* Configuring INT_MOTION registers */
  3397. /* Write significant motion threshold.
  3398. * This threshold is same as any motion threshold */
  3399. data = sig_mot_int_cfg->sig_mot_thres;
  3400. /* Write data to INT_MOTION 1 address */
  3401. rslt = bmi160_set_regs(BMI160_INT_MOTION_1_ADDR, &data, 1, dev);
  3402. if (rslt == BMI160_OK) {
  3403. rslt = bmi160_get_regs(BMI160_INT_MOTION_3_ADDR, &data, 1, dev);
  3404. if (rslt == BMI160_OK) {
  3405. temp = data & ~BMI160_SIG_MOTION_SKIP_MASK;
  3406. /* adding skip time of sig_motion interrupt*/
  3407. data = temp | ((sig_mot_int_cfg->sig_mot_skip << 2) & BMI160_SIG_MOTION_SKIP_MASK);
  3408. temp = data & ~BMI160_SIG_MOTION_PROOF_MASK;
  3409. /* adding proof time of sig_motion interrupt */
  3410. data = temp | ((sig_mot_int_cfg->sig_mot_proof << 4) & BMI160_SIG_MOTION_PROOF_MASK);
  3411. /* configure the int_sig_mot_sel bit to select
  3412. * significant motion interrupt */
  3413. temp = data & ~BMI160_SIG_MOTION_SEL_MASK;
  3414. data = temp | ((sig_mot_int_cfg->sig_en << 1) & BMI160_SIG_MOTION_SEL_MASK);
  3415. rslt = bmi160_set_regs(BMI160_INT_MOTION_3_ADDR, &data, 1, dev);
  3416. }
  3417. }
  3418. return rslt;
  3419. }
  3420. /*!
  3421. * @brief This API enables the step detector interrupt.
  3422. */
  3423. static int8_t enable_step_detect_int(const struct bmi160_acc_step_detect_int_cfg *step_detect_int_cfg,
  3424. const struct bmi160_dev *dev)
  3425. {
  3426. int8_t rslt;
  3427. uint8_t data = 0;
  3428. uint8_t temp = 0;
  3429. /* Enable data ready interrupt in Int Enable 2 register */
  3430. rslt = bmi160_get_regs(BMI160_INT_ENABLE_2_ADDR, &data, 1, dev);
  3431. if (rslt == BMI160_OK) {
  3432. temp = data & ~BMI160_STEP_DETECT_INT_EN_MASK;
  3433. data = temp | ((step_detect_int_cfg->step_detector_en << 3) & BMI160_STEP_DETECT_INT_EN_MASK);
  3434. /* Writing data to INT ENABLE 2 Address */
  3435. rslt = bmi160_set_regs(BMI160_INT_ENABLE_2_ADDR, &data, 1, dev);
  3436. }
  3437. return rslt;
  3438. }
  3439. /*!
  3440. * @brief This API configure the step detector parameter.
  3441. */
  3442. static int8_t config_step_detect(const struct bmi160_acc_step_detect_int_cfg *step_detect_int_cfg,
  3443. const struct bmi160_dev *dev)
  3444. {
  3445. int8_t rslt;
  3446. uint8_t temp = 0;
  3447. uint8_t data_array[2] = {0};
  3448. if (step_detect_int_cfg->step_detector_mode == BMI160_STEP_DETECT_NORMAL) {
  3449. /* Normal mode setting */
  3450. data_array[0] = 0x15;
  3451. data_array[1] = 0x03;
  3452. } else if (step_detect_int_cfg->step_detector_mode == BMI160_STEP_DETECT_SENSITIVE) {
  3453. /* Sensitive mode setting */
  3454. data_array[0] = 0x2D;
  3455. data_array[1] = 0x00;
  3456. } else if (step_detect_int_cfg->step_detector_mode == BMI160_STEP_DETECT_ROBUST) {
  3457. /* Robust mode setting */
  3458. data_array[0] = 0x1D;
  3459. data_array[1] = 0x07;
  3460. } else if (step_detect_int_cfg->step_detector_mode == BMI160_STEP_DETECT_USER_DEFINE) {
  3461. /* Non recommended User defined setting */
  3462. /* Configuring STEP_CONFIG register */
  3463. rslt = bmi160_get_regs(BMI160_INT_STEP_CONFIG_0_ADDR, &data_array[0], 2, dev);
  3464. if (rslt == BMI160_OK) {
  3465. temp = data_array[0] & ~BMI160_STEP_DETECT_MIN_THRES_MASK;
  3466. /* Adding min_threshold */
  3467. data_array[0] = temp | ((step_detect_int_cfg->min_threshold << 3)
  3468. & BMI160_STEP_DETECT_MIN_THRES_MASK);
  3469. temp = data_array[0] & ~BMI160_STEP_DETECT_STEPTIME_MIN_MASK;
  3470. /* Adding steptime_min */
  3471. data_array[0] = temp | ((step_detect_int_cfg->steptime_min)
  3472. & BMI160_STEP_DETECT_STEPTIME_MIN_MASK);
  3473. temp = data_array[1] & ~BMI160_STEP_MIN_BUF_MASK;
  3474. /* Adding steptime_min */
  3475. data_array[1] = temp | ((step_detect_int_cfg->step_min_buf) & BMI160_STEP_MIN_BUF_MASK);
  3476. }
  3477. }
  3478. /* Write data to STEP_CONFIG register */
  3479. rslt = bmi160_set_regs(BMI160_INT_STEP_CONFIG_0_ADDR, data_array, 2, dev);
  3480. return rslt;
  3481. }
  3482. /*!
  3483. * @brief This API enables the single/double tap interrupt.
  3484. */
  3485. static int8_t enable_tap_int(const struct bmi160_int_settg *int_config,
  3486. const struct bmi160_acc_tap_int_cfg *tap_int_cfg,
  3487. const struct bmi160_dev *dev)
  3488. {
  3489. int8_t rslt;
  3490. uint8_t data = 0;
  3491. uint8_t temp = 0;
  3492. /* Enable single tap or double tap interrupt in Int Enable 0 register */
  3493. rslt = bmi160_get_regs(BMI160_INT_ENABLE_0_ADDR, &data, 1, dev);
  3494. if (rslt == BMI160_OK) {
  3495. if (int_config->int_type == BMI160_ACC_SINGLE_TAP_INT) {
  3496. temp = data & ~BMI160_SINGLE_TAP_INT_EN_MASK;
  3497. data = temp | ((tap_int_cfg->tap_en << 5) & BMI160_SINGLE_TAP_INT_EN_MASK);
  3498. } else {
  3499. temp = data & ~BMI160_DOUBLE_TAP_INT_EN_MASK;
  3500. data = temp | ((tap_int_cfg->tap_en << 4) & BMI160_DOUBLE_TAP_INT_EN_MASK);
  3501. }
  3502. /* Write to Enable 0 Address */
  3503. rslt = bmi160_set_regs(BMI160_INT_ENABLE_0_ADDR, &data, 1, dev);
  3504. }
  3505. return rslt;
  3506. }
  3507. /*!
  3508. * @brief This API configure the interrupt PIN setting for
  3509. * tap interrupt.
  3510. */
  3511. static int8_t config_tap_int_settg(const struct bmi160_int_settg *int_config,
  3512. const struct bmi160_acc_tap_int_cfg *tap_int_cfg,
  3513. const struct bmi160_dev *dev)
  3514. {
  3515. int8_t rslt;
  3516. /* Configure Interrupt pins */
  3517. rslt = set_intr_pin_config(int_config, dev);
  3518. if (rslt == BMI160_OK) {
  3519. rslt = map_feature_interrupt(int_config, dev);
  3520. if (rslt == BMI160_OK) {
  3521. rslt = config_tap_data_src(tap_int_cfg, dev);
  3522. if (rslt == BMI160_OK)
  3523. rslt = config_tap_param(int_config, tap_int_cfg, dev);
  3524. }
  3525. }
  3526. return rslt;
  3527. }
  3528. /*!
  3529. * @brief This API configure the source of data(filter & pre-filter)
  3530. * for tap interrupt.
  3531. */
  3532. static int8_t config_tap_data_src(const struct bmi160_acc_tap_int_cfg *tap_int_cfg, const struct bmi160_dev *dev)
  3533. {
  3534. int8_t rslt;
  3535. uint8_t data = 0;
  3536. uint8_t temp = 0;
  3537. /* Configure Int data 0 register to add source of interrupt */
  3538. rslt = bmi160_get_regs(BMI160_INT_DATA_0_ADDR, &data, 1, dev);
  3539. if (rslt == BMI160_OK) {
  3540. temp = data & ~BMI160_TAP_SRC_INT_MASK;
  3541. data = temp | ((tap_int_cfg->tap_data_src << 3) & BMI160_TAP_SRC_INT_MASK);
  3542. /* Write data to Data 0 address */
  3543. rslt = bmi160_set_regs(BMI160_INT_DATA_0_ADDR, &data, 1, dev);
  3544. }
  3545. return rslt;
  3546. }
  3547. /*!
  3548. * @brief This API configure the parameters of tap interrupt.
  3549. * Threshold, quite, shock, and duration.
  3550. */
  3551. static int8_t config_tap_param(const struct bmi160_int_settg *int_config,
  3552. const struct bmi160_acc_tap_int_cfg *tap_int_cfg,
  3553. const struct bmi160_dev *dev)
  3554. {
  3555. int8_t rslt;
  3556. uint8_t temp = 0;
  3557. uint8_t data = 0;
  3558. uint8_t data_array[2] = {0};
  3559. uint8_t count = 0;
  3560. uint8_t dur, shock, quiet, thres;
  3561. /* Configure tap 0 register for tap shock,tap quiet duration
  3562. * in case of single tap interrupt */
  3563. rslt = bmi160_get_regs(BMI160_INT_TAP_0_ADDR, data_array, 2, dev);
  3564. if (rslt == BMI160_OK) {
  3565. data = data_array[count];
  3566. if (int_config->int_type == BMI160_ACC_DOUBLE_TAP_INT) {
  3567. dur = (uint8_t)tap_int_cfg->tap_dur;
  3568. temp = (data & ~BMI160_TAP_DUR_MASK);
  3569. /* Add tap duration data in case of
  3570. * double tap interrupt */
  3571. data = temp | (dur & BMI160_TAP_DUR_MASK);
  3572. }
  3573. shock = (uint8_t)tap_int_cfg->tap_shock;
  3574. temp = data & ~BMI160_TAP_SHOCK_DUR_MASK;
  3575. data = temp | ((shock << 6) & BMI160_TAP_SHOCK_DUR_MASK);
  3576. quiet = (uint8_t)tap_int_cfg->tap_quiet;
  3577. temp = data & ~BMI160_TAP_QUIET_DUR_MASK;
  3578. data = temp | ((quiet << 7) & BMI160_TAP_QUIET_DUR_MASK);
  3579. data_array[count++] = data;
  3580. data = data_array[count];
  3581. thres = (uint8_t)tap_int_cfg->tap_thr;
  3582. temp = data & ~BMI160_TAP_THRES_MASK;
  3583. data = temp | (thres & BMI160_TAP_THRES_MASK);
  3584. data_array[count++] = data;
  3585. /* TAP 0 and TAP 1 address lie consecutively,
  3586. hence writing data to respective registers at one go */
  3587. /* Writing to Tap 0 and Tap 1 Address simultaneously */
  3588. rslt = bmi160_set_regs(BMI160_INT_TAP_0_ADDR, data_array, count, dev);
  3589. }
  3590. return rslt;
  3591. }
  3592. /*!
  3593. * @brief This API configure the secondary interface.
  3594. */
  3595. static int8_t config_sec_if(const struct bmi160_dev *dev)
  3596. {
  3597. int8_t rslt;
  3598. uint8_t if_conf = 0;
  3599. uint8_t cmd = BMI160_AUX_NORMAL_MODE;
  3600. /* set the aux power mode to normal*/
  3601. rslt = bmi160_set_regs(BMI160_COMMAND_REG_ADDR, &cmd, 1, dev);
  3602. if (rslt == BMI160_OK) {
  3603. dev->delay_ms(60);
  3604. rslt = bmi160_get_regs(BMI160_IF_CONF_ADDR, &if_conf, 1, dev);
  3605. if_conf |= (uint8_t)(1 << 5);
  3606. if (rslt == BMI160_OK)
  3607. /*enable the secondary interface also*/
  3608. rslt = bmi160_set_regs(BMI160_IF_CONF_ADDR, &if_conf, 1, dev);
  3609. }
  3610. return rslt;
  3611. }
  3612. /*!
  3613. * @brief This API configure the ODR of the auxiliary sensor.
  3614. */
  3615. static int8_t config_aux_odr(const struct bmi160_dev *dev)
  3616. {
  3617. int8_t rslt;
  3618. uint8_t aux_odr;
  3619. rslt = bmi160_get_regs(BMI160_AUX_ODR_ADDR, &aux_odr, 1, dev);
  3620. if (rslt == BMI160_OK) {
  3621. aux_odr = (uint8_t)(dev->aux_cfg.aux_odr);
  3622. /* Set the secondary interface ODR
  3623. i.e polling rate of secondary sensor */
  3624. rslt = bmi160_set_regs(BMI160_AUX_ODR_ADDR, &aux_odr, 1, dev);
  3625. dev->delay_ms(BMI160_AUX_COM_DELAY);
  3626. }
  3627. return rslt;
  3628. }
  3629. /*!
  3630. * @brief This API maps the actual burst read length set by user.
  3631. */
  3632. static int8_t map_read_len(uint16_t *len, const struct bmi160_dev *dev)
  3633. {
  3634. int8_t rslt = BMI160_OK;
  3635. switch (dev->aux_cfg.aux_rd_burst_len) {
  3636. case BMI160_AUX_READ_LEN_0:
  3637. *len = 1;
  3638. break;
  3639. case BMI160_AUX_READ_LEN_1:
  3640. *len = 2;
  3641. break;
  3642. case BMI160_AUX_READ_LEN_2:
  3643. *len = 6;
  3644. break;
  3645. case BMI160_AUX_READ_LEN_3:
  3646. *len = 8;
  3647. break;
  3648. default:
  3649. rslt = BMI160_E_INVALID_INPUT;
  3650. break;
  3651. }
  3652. return rslt;
  3653. }
  3654. /*!
  3655. * @brief This API configure the settings of auxiliary sensor.
  3656. */
  3657. static int8_t config_aux_settg(const struct bmi160_dev *dev)
  3658. {
  3659. int8_t rslt;
  3660. rslt = config_sec_if(dev);
  3661. if (rslt == BMI160_OK) {
  3662. /* Configures the auxiliary interface settings */
  3663. rslt = bmi160_config_aux_mode(dev);
  3664. }
  3665. return rslt;
  3666. }
  3667. /*!
  3668. * @brief This API extract the read data from auxiliary sensor.
  3669. */
  3670. static int8_t extract_aux_read(uint16_t map_len, uint8_t reg_addr, uint8_t *aux_data, uint16_t len,
  3671. const struct bmi160_dev *dev)
  3672. {
  3673. int8_t rslt = BMI160_OK;
  3674. uint8_t data[8] = {0,};
  3675. uint8_t read_addr = BMI160_AUX_DATA_ADDR;
  3676. uint8_t count = 0;
  3677. uint8_t read_count;
  3678. uint8_t read_len = (uint8_t)map_len;
  3679. for (; count < len;) {
  3680. /* set address to read */
  3681. rslt = bmi160_set_regs(BMI160_AUX_IF_2_ADDR, &reg_addr, 1, dev);
  3682. dev->delay_ms(BMI160_AUX_COM_DELAY);
  3683. if (rslt == BMI160_OK) {
  3684. rslt = bmi160_get_regs(read_addr, data, map_len, dev);
  3685. if (rslt == BMI160_OK) {
  3686. read_count = 0;
  3687. /* if read len is less the burst read len
  3688. * mention by user*/
  3689. if (len < map_len) {
  3690. read_len = (uint8_t)len;
  3691. } else {
  3692. if ((len - count) < map_len)
  3693. read_len = (uint8_t)(len - count);
  3694. }
  3695. for (; read_count < read_len; read_count++)
  3696. aux_data[count + read_count] = data[read_count];
  3697. reg_addr += (uint8_t)map_len;
  3698. count += (uint8_t)map_len;
  3699. } else {
  3700. rslt = BMI160_E_COM_FAIL;
  3701. break;
  3702. }
  3703. }
  3704. }
  3705. return rslt;
  3706. }
  3707. /*!
  3708. * @brief This API enables the orient interrupt.
  3709. */
  3710. static int8_t enable_orient_int(const struct bmi160_acc_orient_int_cfg *orient_int_cfg, const struct bmi160_dev *dev)
  3711. {
  3712. int8_t rslt;
  3713. uint8_t data = 0;
  3714. uint8_t temp = 0;
  3715. /* Enable data ready interrupt in Int Enable 0 register */
  3716. rslt = bmi160_get_regs(BMI160_INT_ENABLE_0_ADDR, &data, 1, dev);
  3717. if (rslt == BMI160_OK) {
  3718. temp = data & ~BMI160_ORIENT_INT_EN_MASK;
  3719. data = temp | ((orient_int_cfg->orient_en << 6) & BMI160_ORIENT_INT_EN_MASK);
  3720. /* write data to Int Enable 0 register */
  3721. rslt = bmi160_set_regs(BMI160_INT_ENABLE_0_ADDR, &data, 1, dev);
  3722. }
  3723. return rslt;
  3724. }
  3725. /*!
  3726. * @brief This API configure the necessary setting of orientation interrupt.
  3727. */
  3728. static int8_t config_orient_int_settg(const struct bmi160_acc_orient_int_cfg *orient_int_cfg,
  3729. const struct bmi160_dev *dev)
  3730. {
  3731. int8_t rslt;
  3732. uint8_t data = 0;
  3733. uint8_t temp = 0;
  3734. uint8_t data_array[2] = {0, 0};
  3735. /* Configuring INT_ORIENT registers */
  3736. rslt = bmi160_get_regs(BMI160_INT_ORIENT_0_ADDR, data_array, 2, dev);
  3737. if (rslt == BMI160_OK) {
  3738. data = data_array[0];
  3739. temp = data & ~BMI160_ORIENT_MODE_MASK;
  3740. /* Adding Orientation mode */
  3741. data = temp | ((orient_int_cfg->orient_mode) & BMI160_ORIENT_MODE_MASK);
  3742. temp = data & ~BMI160_ORIENT_BLOCK_MASK;
  3743. /* Adding Orientation blocking */
  3744. data = temp | ((orient_int_cfg->orient_blocking << 2) & BMI160_ORIENT_BLOCK_MASK);
  3745. temp = data & ~BMI160_ORIENT_HYST_MASK;
  3746. /* Adding Orientation hysteresis */
  3747. data = temp | ((orient_int_cfg->orient_hyst << 4) & BMI160_ORIENT_HYST_MASK);
  3748. data_array[0] = data;
  3749. data = data_array[1];
  3750. temp = data & ~BMI160_ORIENT_THETA_MASK;
  3751. /* Adding Orientation threshold */
  3752. data = temp | ((orient_int_cfg->orient_theta) & BMI160_ORIENT_THETA_MASK);
  3753. temp = data & ~BMI160_ORIENT_UD_ENABLE;
  3754. /* Adding Orient_ud_en */
  3755. data = temp | ((orient_int_cfg->orient_ud_en << 6) & BMI160_ORIENT_UD_ENABLE);
  3756. temp = data & ~BMI160_AXES_EN_MASK;
  3757. /* Adding axes_en */
  3758. data = temp | ((orient_int_cfg->axes_ex << 7) & BMI160_AXES_EN_MASK);
  3759. data_array[1] = data;
  3760. /* Writing data to INT_ORIENT 0 and INT_ORIENT 1
  3761. * registers simultaneously */
  3762. rslt = bmi160_set_regs(BMI160_INT_ORIENT_0_ADDR, data_array, 2, dev);
  3763. }
  3764. return rslt;
  3765. }
  3766. /*!
  3767. * @brief This API enables the flat interrupt.
  3768. */
  3769. static int8_t enable_flat_int(const struct bmi160_acc_flat_detect_int_cfg *flat_int, const struct bmi160_dev *dev)
  3770. {
  3771. int8_t rslt;
  3772. uint8_t data = 0;
  3773. uint8_t temp = 0;
  3774. /* Enable flat interrupt in Int Enable 0 register */
  3775. rslt = bmi160_get_regs(BMI160_INT_ENABLE_0_ADDR, &data, 1, dev);
  3776. if (rslt == BMI160_OK) {
  3777. temp = data & ~BMI160_FLAT_INT_EN_MASK;
  3778. data = temp | ((flat_int->flat_en << 7) & BMI160_FLAT_INT_EN_MASK);
  3779. /* write data to Int Enable 0 register */
  3780. rslt = bmi160_set_regs(BMI160_INT_ENABLE_0_ADDR, &data, 1, dev);
  3781. }
  3782. return rslt;
  3783. }
  3784. /*!
  3785. * @brief This API configure the necessary setting of flat interrupt.
  3786. */
  3787. static int8_t config_flat_int_settg(const struct bmi160_acc_flat_detect_int_cfg *flat_int, const struct bmi160_dev *dev)
  3788. {
  3789. int8_t rslt;
  3790. uint8_t data = 0;
  3791. uint8_t temp = 0;
  3792. uint8_t data_array[2] = {0, 0};
  3793. /* Configuring INT_FLAT register */
  3794. rslt = bmi160_get_regs(BMI160_INT_FLAT_0_ADDR, data_array, 2, dev);
  3795. if (rslt == BMI160_OK) {
  3796. data = data_array[0];
  3797. temp = data & ~BMI160_FLAT_THRES_MASK;
  3798. /* Adding flat theta */
  3799. data = temp | ((flat_int->flat_theta) & BMI160_FLAT_THRES_MASK);
  3800. data_array[0] = data;
  3801. data = data_array[1];
  3802. temp = data & ~BMI160_FLAT_HOLD_TIME_MASK;
  3803. /* Adding flat hold time */
  3804. data = temp | ((flat_int->flat_hold_time << 4) & BMI160_FLAT_HOLD_TIME_MASK);
  3805. temp = data & ~BMI160_FLAT_HYST_MASK;
  3806. /* Adding flat hysteresis */
  3807. data = temp | ((flat_int->flat_hy) & BMI160_FLAT_HYST_MASK);
  3808. data_array[1] = data;
  3809. /* Writing data to INT_FLAT 0 and INT_FLAT 1
  3810. * registers simultaneously */
  3811. rslt = bmi160_set_regs(BMI160_INT_FLAT_0_ADDR, data_array, 2, dev);
  3812. }
  3813. return rslt;
  3814. }
  3815. /*!
  3816. * @brief This API enables the Low-g interrupt.
  3817. */
  3818. static int8_t enable_low_g_int(const struct bmi160_acc_low_g_int_cfg *low_g_int, const struct bmi160_dev *dev)
  3819. {
  3820. int8_t rslt;
  3821. uint8_t data = 0;
  3822. uint8_t temp = 0;
  3823. /* Enable low-g interrupt in Int Enable 1 register */
  3824. rslt = bmi160_get_regs(BMI160_INT_ENABLE_1_ADDR, &data, 1, dev);
  3825. if (rslt == BMI160_OK) {
  3826. temp = data & ~BMI160_LOW_G_INT_EN_MASK;
  3827. data = temp | ((low_g_int->low_en << 3) & BMI160_LOW_G_INT_EN_MASK);
  3828. /* write data to Int Enable 0 register */
  3829. rslt = bmi160_set_regs(BMI160_INT_ENABLE_1_ADDR, &data, 1, dev);
  3830. }
  3831. return rslt;
  3832. }
  3833. /*!
  3834. * @brief This API configure the source of data(filter & pre-filter)
  3835. * for low-g interrupt.
  3836. */
  3837. static int8_t config_low_g_data_src(const struct bmi160_acc_low_g_int_cfg *low_g_int, const struct bmi160_dev *dev)
  3838. {
  3839. int8_t rslt;
  3840. uint8_t data = 0;
  3841. uint8_t temp = 0;
  3842. /* Configure Int data 0 register to add source of interrupt */
  3843. rslt = bmi160_get_regs(BMI160_INT_DATA_0_ADDR, &data, 1, dev);
  3844. if (rslt == BMI160_OK) {
  3845. temp = data & ~BMI160_LOW_HIGH_SRC_INT_MASK;
  3846. data = temp | ((low_g_int->low_data_src << 7) & BMI160_LOW_HIGH_SRC_INT_MASK);
  3847. /* Write data to Data 0 address */
  3848. rslt = bmi160_set_regs(BMI160_INT_DATA_0_ADDR, &data, 1, dev);
  3849. }
  3850. return rslt;
  3851. }
  3852. /*!
  3853. * @brief This API configure the necessary setting of low-g interrupt.
  3854. */
  3855. static int8_t config_low_g_int_settg(const struct bmi160_acc_low_g_int_cfg *low_g_int, const struct bmi160_dev *dev)
  3856. {
  3857. int8_t rslt;
  3858. uint8_t temp = 0;
  3859. uint8_t data_array[3] = {0, 0, 0};
  3860. /* Configuring INT_LOWHIGH register for low-g interrupt */
  3861. rslt = bmi160_get_regs(BMI160_INT_LOWHIGH_2_ADDR, &data_array[2], 1, dev);
  3862. if (rslt == BMI160_OK) {
  3863. temp = data_array[2] & ~BMI160_LOW_G_HYST_MASK;
  3864. /* Adding low-g hysteresis */
  3865. data_array[2] = temp | (low_g_int->low_hyst & BMI160_LOW_G_HYST_MASK);
  3866. temp = data_array[2] & ~BMI160_LOW_G_LOW_MODE_MASK;
  3867. /* Adding low-mode */
  3868. data_array[2] = temp | ((low_g_int->low_mode << 2) & BMI160_LOW_G_LOW_MODE_MASK);
  3869. /* Adding low-g threshold */
  3870. data_array[1] = low_g_int->low_thres;
  3871. /* Adding low-g interrupt delay */
  3872. data_array[0] = low_g_int->low_dur;
  3873. /* Writing data to INT_LOWHIGH 0,1,2 registers simultaneously*/
  3874. rslt = bmi160_set_regs(BMI160_INT_LOWHIGH_0_ADDR, data_array, 3, dev);
  3875. }
  3876. return rslt;
  3877. }
  3878. /*!
  3879. * @brief This API enables the high-g interrupt.
  3880. */
  3881. static int8_t enable_high_g_int(const struct bmi160_acc_high_g_int_cfg *high_g_int_cfg, const struct bmi160_dev *dev)
  3882. {
  3883. int8_t rslt;
  3884. uint8_t data = 0;
  3885. uint8_t temp = 0;
  3886. /* Enable low-g interrupt in Int Enable 1 register */
  3887. rslt = bmi160_get_regs(BMI160_INT_ENABLE_1_ADDR, &data, 1, dev);
  3888. if (rslt == BMI160_OK) {
  3889. /* Adding high-g X-axis */
  3890. temp = data & ~BMI160_HIGH_G_X_INT_EN_MASK;
  3891. data = temp | (high_g_int_cfg->high_g_x & BMI160_HIGH_G_X_INT_EN_MASK);
  3892. /* Adding high-g Y-axis */
  3893. temp = data & ~BMI160_HIGH_G_Y_INT_EN_MASK;
  3894. data = temp | ((high_g_int_cfg->high_g_y << 1) & BMI160_HIGH_G_Y_INT_EN_MASK);
  3895. /* Adding high-g Z-axis */
  3896. temp = data & ~BMI160_HIGH_G_Z_INT_EN_MASK;
  3897. data = temp | ((high_g_int_cfg->high_g_z << 2) & BMI160_HIGH_G_Z_INT_EN_MASK);
  3898. /* write data to Int Enable 0 register */
  3899. rslt = bmi160_set_regs(BMI160_INT_ENABLE_1_ADDR, &data, 1, dev);
  3900. }
  3901. return rslt;
  3902. }
  3903. /*!
  3904. * @brief This API configure the source of data(filter & pre-filter)
  3905. * for high-g interrupt.
  3906. */
  3907. static int8_t config_high_g_data_src(const struct bmi160_acc_high_g_int_cfg *high_g_int_cfg,
  3908. const struct bmi160_dev *dev)
  3909. {
  3910. int8_t rslt;
  3911. uint8_t data = 0;
  3912. uint8_t temp = 0;
  3913. /* Configure Int data 0 register to add source of interrupt */
  3914. rslt = bmi160_get_regs(BMI160_INT_DATA_0_ADDR, &data, 1, dev);
  3915. if (rslt == BMI160_OK) {
  3916. temp = data & ~BMI160_LOW_HIGH_SRC_INT_MASK;
  3917. data = temp | ((high_g_int_cfg->high_data_src << 7) & BMI160_LOW_HIGH_SRC_INT_MASK);
  3918. /* Write data to Data 0 address */
  3919. rslt = bmi160_set_regs(BMI160_INT_DATA_0_ADDR, &data, 1, dev);
  3920. }
  3921. return rslt;
  3922. }
  3923. /*!
  3924. * @brief This API configure the necessary setting of high-g interrupt.
  3925. */
  3926. static int8_t config_high_g_int_settg(const struct bmi160_acc_high_g_int_cfg *high_g_int_cfg,
  3927. const struct bmi160_dev *dev)
  3928. {
  3929. int8_t rslt;
  3930. uint8_t temp = 0;
  3931. uint8_t data_array[3] = {0, 0, 0};
  3932. rslt = bmi160_get_regs(BMI160_INT_LOWHIGH_2_ADDR, &data_array[0], 1, dev);
  3933. if (rslt == BMI160_OK) {
  3934. temp = data_array[0] & ~BMI160_HIGH_G_HYST_MASK;
  3935. /* Adding high-g hysteresis */
  3936. data_array[0] = temp | ((high_g_int_cfg->high_hy << 6) & BMI160_HIGH_G_HYST_MASK);
  3937. /* Adding high-g duration */
  3938. data_array[1] = high_g_int_cfg->high_dur;
  3939. /* Adding high-g threshold */
  3940. data_array[2] = high_g_int_cfg->high_thres;
  3941. rslt = bmi160_set_regs(BMI160_INT_LOWHIGH_2_ADDR, data_array, 3, dev);
  3942. }
  3943. return rslt;
  3944. }
  3945. /*!
  3946. * @brief This API configure the behavioural setting of interrupt pin.
  3947. */
  3948. static int8_t config_int_out_ctrl(const struct bmi160_int_settg *int_config, const struct bmi160_dev *dev)
  3949. {
  3950. int8_t rslt;
  3951. uint8_t temp = 0;
  3952. uint8_t data = 0;
  3953. /* Configuration of output interrupt signals on pins INT1 and INT2 are
  3954. * done in BMI160_INT_OUT_CTRL_ADDR register*/
  3955. rslt = bmi160_get_regs(BMI160_INT_OUT_CTRL_ADDR, &data, 1, dev);
  3956. if (rslt == BMI160_OK) {
  3957. /* updating the interrupt pin structure to local structure */
  3958. const struct bmi160_int_pin_settg *intr_pin_sett = &(int_config->int_pin_settg);
  3959. /* Configuring channel 1 */
  3960. if (int_config->int_channel == BMI160_INT_CHANNEL_1) {
  3961. /* Output enable */
  3962. temp = data & ~BMI160_INT1_OUTPUT_EN_MASK;
  3963. data = temp | ((intr_pin_sett->output_en << 3) & BMI160_INT1_OUTPUT_EN_MASK);
  3964. /* Output mode */
  3965. temp = data & ~BMI160_INT1_OUTPUT_MODE_MASK;
  3966. data = temp | ((intr_pin_sett->output_mode << 2) & BMI160_INT1_OUTPUT_MODE_MASK);
  3967. /* Output type */
  3968. temp = data & ~BMI160_INT1_OUTPUT_TYPE_MASK;
  3969. data = temp | ((intr_pin_sett->output_type << 1) & BMI160_INT1_OUTPUT_TYPE_MASK);
  3970. /* edge control */
  3971. temp = data & ~BMI160_INT1_EDGE_CTRL_MASK;
  3972. data = temp | ((intr_pin_sett->edge_ctrl) & BMI160_INT1_EDGE_CTRL_MASK);
  3973. } else {
  3974. /* Configuring channel 2 */
  3975. /* Output enable */
  3976. temp = data & ~BMI160_INT2_OUTPUT_EN_MASK;
  3977. data = temp | ((intr_pin_sett->output_en << 7) & BMI160_INT2_OUTPUT_EN_MASK);
  3978. /* Output mode */
  3979. temp = data & ~BMI160_INT2_OUTPUT_MODE_MASK;
  3980. data = temp | ((intr_pin_sett->output_mode << 6) & BMI160_INT2_OUTPUT_MODE_MASK);
  3981. /* Output type */
  3982. temp = data & ~BMI160_INT2_OUTPUT_TYPE_MASK;
  3983. data = temp | ((intr_pin_sett->output_type << 5) & BMI160_INT2_OUTPUT_TYPE_MASK);
  3984. /* edge control */
  3985. temp = data & ~BMI160_INT2_EDGE_CTRL_MASK;
  3986. data = temp | ((intr_pin_sett->edge_ctrl << 4) & BMI160_INT2_EDGE_CTRL_MASK);
  3987. }
  3988. rslt = bmi160_set_regs(BMI160_INT_OUT_CTRL_ADDR, &data, 1, dev);
  3989. }
  3990. return rslt;
  3991. }
  3992. /*!
  3993. * @brief This API configure the mode(input enable, latch or non-latch) of interrupt pin.
  3994. */
  3995. static int8_t config_int_latch(const struct bmi160_int_settg *int_config, const struct bmi160_dev *dev)
  3996. {
  3997. int8_t rslt;
  3998. uint8_t temp = 0;
  3999. uint8_t data = 0;
  4000. /* Configuration of latch on pins INT1 and INT2 are done in
  4001. * BMI160_INT_LATCH_ADDR register*/
  4002. rslt = bmi160_get_regs(BMI160_INT_LATCH_ADDR, &data, 1, dev);
  4003. if (rslt == BMI160_OK) {
  4004. /* updating the interrupt pin structure to local structure */
  4005. const struct bmi160_int_pin_settg *intr_pin_sett = &(int_config->int_pin_settg);
  4006. if (int_config->int_channel == BMI160_INT_CHANNEL_1) {
  4007. /* Configuring channel 1 */
  4008. /* Input enable */
  4009. temp = data & ~BMI160_INT1_INPUT_EN_MASK;
  4010. data = temp | ((intr_pin_sett->input_en << 4) & BMI160_INT1_INPUT_EN_MASK);
  4011. } else {
  4012. /* Configuring channel 2 */
  4013. /* Input enable */
  4014. temp = data & ~BMI160_INT2_INPUT_EN_MASK;
  4015. data = temp | ((intr_pin_sett->input_en << 5) & BMI160_INT2_INPUT_EN_MASK);
  4016. }
  4017. /* In case of latch interrupt,update the latch duration */
  4018. /* Latching holds the interrupt for the amount of latch
  4019. * duration time */
  4020. temp = data & ~BMI160_INT_LATCH_MASK;
  4021. data = temp | (intr_pin_sett->latch_dur & BMI160_INT_LATCH_MASK);
  4022. /* OUT_CTRL_INT and LATCH_INT address lie consecutively,
  4023. * hence writing data to respective registers at one go */
  4024. rslt = bmi160_set_regs(BMI160_INT_LATCH_ADDR, &data, 1, dev);
  4025. }
  4026. return rslt;
  4027. }
  4028. /*!
  4029. * @brief This API performs the self test for accelerometer of BMI160
  4030. */
  4031. static int8_t perform_accel_self_test(struct bmi160_dev *dev)
  4032. {
  4033. int8_t rslt;
  4034. struct bmi160_sensor_data accel_pos, accel_neg;
  4035. /* Enable Gyro self test bit */
  4036. rslt = enable_accel_self_test(dev);
  4037. if (rslt == BMI160_OK) {
  4038. /* Perform accel self test with positive excitation */
  4039. rslt = accel_self_test_positive_excitation(&accel_pos, dev);
  4040. if (rslt == BMI160_OK) {
  4041. /* Perform accel self test with negative excitation */
  4042. rslt = accel_self_test_negative_excitation(&accel_neg, dev);
  4043. if (rslt == BMI160_OK) {
  4044. /* Validate the self test result */
  4045. rslt = validate_accel_self_test(&accel_pos, &accel_neg);
  4046. }
  4047. }
  4048. }
  4049. return rslt;
  4050. }
  4051. /*!
  4052. * @brief This API enables to perform the accel self test by setting proper
  4053. * configurations to facilitate accel self test
  4054. */
  4055. static int8_t enable_accel_self_test(struct bmi160_dev *dev)
  4056. {
  4057. int8_t rslt;
  4058. uint8_t reg_data;
  4059. /* Set the Accel power mode as normal mode */
  4060. dev->accel_cfg.power = BMI160_ACCEL_NORMAL_MODE;
  4061. /* Set the sensor range configuration as 8G */
  4062. dev->accel_cfg.range = BMI160_ACCEL_RANGE_8G;
  4063. rslt = bmi160_set_sens_conf(dev);
  4064. if (rslt == BMI160_OK) {
  4065. /* Accel configurations are set to facilitate self test
  4066. * acc_odr - 1600Hz ; acc_bwp = 2 ; acc_us = 0 */
  4067. reg_data = BMI160_ACCEL_SELF_TEST_CONFIG;
  4068. rslt = bmi160_set_regs(BMI160_ACCEL_CONFIG_ADDR, &reg_data, 1, dev);
  4069. }
  4070. return rslt;
  4071. }
  4072. /*!
  4073. * @brief This API performs accel self test with positive excitation
  4074. */
  4075. static int8_t accel_self_test_positive_excitation(struct bmi160_sensor_data *accel_pos, const struct bmi160_dev *dev)
  4076. {
  4077. int8_t rslt;
  4078. uint8_t reg_data;
  4079. /* Enable accel self test with positive self-test excitation
  4080. and with amplitude of deflection set as high */
  4081. reg_data = BMI160_ACCEL_SELF_TEST_POSITIVE_EN;
  4082. rslt = bmi160_set_regs(BMI160_SELF_TEST_ADDR, &reg_data, 1, dev);
  4083. if (rslt == BMI160_OK) {
  4084. /* Read the data after a delay of 50ms */
  4085. dev->delay_ms(BMI160_ACCEL_SELF_TEST_DELAY);
  4086. rslt = bmi160_get_sensor_data(BMI160_ACCEL_ONLY, accel_pos, NULL, dev);
  4087. }
  4088. return rslt;
  4089. }
  4090. /*!
  4091. * @brief This API performs accel self test with negative excitation
  4092. */
  4093. static int8_t accel_self_test_negative_excitation(struct bmi160_sensor_data *accel_neg, const struct bmi160_dev *dev)
  4094. {
  4095. int8_t rslt;
  4096. uint8_t reg_data;
  4097. /* Enable accel self test with negative self-test excitation
  4098. and with amplitude of deflection set as high */
  4099. reg_data = BMI160_ACCEL_SELF_TEST_NEGATIVE_EN;
  4100. rslt = bmi160_set_regs(BMI160_SELF_TEST_ADDR, &reg_data, 1, dev);
  4101. if (rslt == BMI160_OK) {
  4102. /* Read the data after a delay of 50ms */
  4103. dev->delay_ms(BMI160_ACCEL_SELF_TEST_DELAY);
  4104. rslt = bmi160_get_sensor_data(BMI160_ACCEL_ONLY, accel_neg, NULL, dev);
  4105. }
  4106. return rslt;
  4107. }
  4108. /*!
  4109. * @brief This API validates the accel self test results
  4110. */
  4111. static int8_t validate_accel_self_test(const struct bmi160_sensor_data *accel_pos,
  4112. const struct bmi160_sensor_data *accel_neg)
  4113. {
  4114. int8_t rslt;
  4115. /* Validate the results of self test */
  4116. if (((accel_neg->x - accel_pos->x) > BMI160_ACCEL_SELF_TEST_LIMIT)
  4117. && ((accel_neg->y - accel_pos->y) > BMI160_ACCEL_SELF_TEST_LIMIT)
  4118. && ((accel_neg->z - accel_pos->z) > BMI160_ACCEL_SELF_TEST_LIMIT)) {
  4119. /* Self test pass condition */
  4120. rslt = BMI160_OK;
  4121. } else {
  4122. rslt = BMI160_W_ACCEl_SELF_TEST_FAIL;
  4123. }
  4124. return rslt;
  4125. }
  4126. /*!
  4127. * @brief This API performs the self test for gyroscope of BMI160
  4128. */
  4129. static int8_t perform_gyro_self_test(const struct bmi160_dev *dev)
  4130. {
  4131. int8_t rslt;
  4132. /* Enable Gyro self test bit */
  4133. rslt = enable_gyro_self_test(dev);
  4134. if (rslt == BMI160_OK) {
  4135. /* Validate the gyro self test results */
  4136. rslt = validate_gyro_self_test(dev);
  4137. }
  4138. return rslt;
  4139. }
  4140. /*!
  4141. * @brief This API enables the self test bit to trigger self test for Gyro
  4142. */
  4143. static int8_t enable_gyro_self_test(const struct bmi160_dev *dev)
  4144. {
  4145. int8_t rslt;
  4146. uint8_t reg_data;
  4147. /* Enable the Gyro self test bit to trigger the self test */
  4148. rslt = bmi160_get_regs(BMI160_SELF_TEST_ADDR, &reg_data, 1, dev);
  4149. if (rslt == BMI160_OK) {
  4150. reg_data = BMI160_SET_BITS(reg_data, BMI160_GYRO_SELF_TEST, 1);
  4151. rslt = bmi160_set_regs(BMI160_SELF_TEST_ADDR, &reg_data, 1, dev);
  4152. if (rslt == BMI160_OK) {
  4153. /* Delay to enable gyro self test */
  4154. dev->delay_ms(BMI160_GYRO_SELF_TEST_DELAY);
  4155. }
  4156. }
  4157. return rslt;
  4158. }
  4159. /*!
  4160. * @brief This API validates the self test results of Gyro
  4161. */
  4162. static int8_t validate_gyro_self_test(const struct bmi160_dev *dev)
  4163. {
  4164. int8_t rslt;
  4165. uint8_t reg_data;
  4166. /* Validate the Gyro self test result */
  4167. rslt = bmi160_get_regs(BMI160_STATUS_ADDR, &reg_data, 1, dev);
  4168. if (rslt == BMI160_OK) {
  4169. reg_data = BMI160_GET_BITS(reg_data, BMI160_GYRO_SELF_TEST_STATUS);
  4170. if (reg_data == BMI160_ENABLE) {
  4171. /* Gyro self test success case */
  4172. rslt = BMI160_OK;
  4173. } else {
  4174. rslt = BMI160_W_GYRO_SELF_TEST_FAIL;
  4175. }
  4176. }
  4177. return rslt;
  4178. }
  4179. /*!
  4180. * @brief This API sets FIFO full interrupt of the sensor.This interrupt
  4181. * occurs when the FIFO is full and the next full data sample would cause
  4182. * a FIFO overflow, which may delete the old samples.
  4183. */
  4184. static int8_t set_fifo_full_int(const struct bmi160_int_settg *int_config, const struct bmi160_dev *dev)
  4185. {
  4186. int8_t rslt = BMI160_OK;
  4187. /* Null-pointer check */
  4188. if ((dev == NULL) || (dev->delay_ms == NULL)) {
  4189. rslt = BMI160_E_NULL_PTR;
  4190. } else {
  4191. /*enable the fifo full interrupt */
  4192. rslt = enable_fifo_full_int(int_config, dev);
  4193. if (rslt == BMI160_OK) {
  4194. /* Configure Interrupt pins */
  4195. rslt = set_intr_pin_config(int_config, dev);
  4196. if (rslt == BMI160_OK)
  4197. rslt = map_hardware_interrupt(int_config, dev);
  4198. }
  4199. }
  4200. return rslt;
  4201. }
  4202. /*!
  4203. * @brief This enable the FIFO full interrupt engine.
  4204. */
  4205. static int8_t enable_fifo_full_int(const struct bmi160_int_settg *int_config, const struct bmi160_dev *dev)
  4206. {
  4207. int8_t rslt;
  4208. uint8_t data = 0;
  4209. rslt = bmi160_get_regs(BMI160_INT_ENABLE_1_ADDR, &data, 1, dev);
  4210. if (rslt == BMI160_OK) {
  4211. data = BMI160_SET_BITS(data, BMI160_FIFO_FULL_INT, int_config->fifo_full_int_en);
  4212. /* Writing data to INT ENABLE 1 Address */
  4213. rslt = bmi160_set_regs(BMI160_INT_ENABLE_1_ADDR, &data, 1, dev);
  4214. }
  4215. return rslt;
  4216. }
  4217. /*!
  4218. * @brief This API sets FIFO watermark interrupt of the sensor.The FIFO
  4219. * watermark interrupt is fired, when the FIFO fill level is above a fifo
  4220. * watermark.
  4221. */
  4222. static int8_t set_fifo_watermark_int(const struct bmi160_int_settg *int_config, const struct bmi160_dev *dev)
  4223. {
  4224. int8_t rslt = BMI160_OK;
  4225. if ((dev == NULL) || (dev->delay_ms == NULL)) {
  4226. rslt = BMI160_E_NULL_PTR;
  4227. } else {
  4228. /* Enable fifo-watermark interrupt in Int Enable 1 register */
  4229. rslt = enable_fifo_wtm_int(int_config, dev);
  4230. if (rslt == BMI160_OK) {
  4231. /* Configure Interrupt pins */
  4232. rslt = set_intr_pin_config(int_config, dev);
  4233. if (rslt == BMI160_OK)
  4234. rslt = map_hardware_interrupt(int_config, dev);
  4235. }
  4236. }
  4237. return rslt;
  4238. }
  4239. /*!
  4240. * @brief This enable the FIFO watermark interrupt engine.
  4241. */
  4242. static int8_t enable_fifo_wtm_int(const struct bmi160_int_settg *int_config, const struct bmi160_dev *dev)
  4243. {
  4244. int8_t rslt;
  4245. uint8_t data = 0;
  4246. rslt = bmi160_get_regs(BMI160_INT_ENABLE_1_ADDR, &data, 1, dev);
  4247. if (rslt == BMI160_OK) {
  4248. data = BMI160_SET_BITS(data, BMI160_FIFO_WTM_INT, int_config->fifo_WTM_int_en);
  4249. /* Writing data to INT ENABLE 1 Address */
  4250. rslt = bmi160_set_regs(BMI160_INT_ENABLE_1_ADDR, &data, 1, dev);
  4251. }
  4252. return rslt;
  4253. }
  4254. /*!
  4255. * @brief This API is used to reset the FIFO related configurations
  4256. * in the fifo_frame structure.
  4257. */
  4258. static void reset_fifo_data_structure(const struct bmi160_dev *dev)
  4259. {
  4260. /*Prepare for next FIFO read by resetting FIFO's
  4261. internal data structures*/
  4262. dev->fifo->accel_byte_start_idx = 0;
  4263. dev->fifo->gyro_byte_start_idx = 0;
  4264. dev->fifo->aux_byte_start_idx = 0;
  4265. dev->fifo->sensor_time = 0;
  4266. dev->fifo->skipped_frame_count = 0;
  4267. }
  4268. /*!
  4269. * @brief This API is used to read fifo_byte_counter value (i.e)
  4270. * current fill-level in Fifo buffer.
  4271. */
  4272. static int8_t get_fifo_byte_counter(uint16_t *bytes_to_read, struct bmi160_dev const *dev)
  4273. {
  4274. int8_t rslt = 0;
  4275. uint8_t data[2];
  4276. uint8_t addr = BMI160_FIFO_LENGTH_ADDR;
  4277. rslt |= bmi160_get_regs(addr, data, 2, dev);
  4278. data[1] = data[1] & BMI160_FIFO_BYTE_COUNTER_MASK;
  4279. /* Available data in FIFO is stored in bytes_to_read*/
  4280. *bytes_to_read = (((uint16_t)data[1] << 8) | ((uint16_t)data[0]));
  4281. return rslt;
  4282. }
  4283. /*!
  4284. * @brief This API is used to compute the number of bytes of accel FIFO data
  4285. * which is to be parsed in header-less mode
  4286. */
  4287. static void get_accel_len_to_parse(uint16_t *data_index, uint16_t *data_read_length, const uint8_t *acc_frame_count,
  4288. const struct bmi160_dev *dev)
  4289. {
  4290. /* Data start index */
  4291. *data_index = dev->fifo->accel_byte_start_idx;
  4292. if (dev->fifo->fifo_data_enable == BMI160_FIFO_A_ENABLE) {
  4293. *data_read_length = (*acc_frame_count) * BMI160_FIFO_A_LENGTH;
  4294. } else if (dev->fifo->fifo_data_enable == BMI160_FIFO_G_A_ENABLE) {
  4295. *data_read_length = (*acc_frame_count) * BMI160_FIFO_GA_LENGTH;
  4296. } else if (dev->fifo->fifo_data_enable == BMI160_FIFO_M_A_ENABLE) {
  4297. *data_read_length = (*acc_frame_count) * BMI160_FIFO_MA_LENGTH;
  4298. } else if (dev->fifo->fifo_data_enable == BMI160_FIFO_M_G_A_ENABLE) {
  4299. *data_read_length = (*acc_frame_count) * BMI160_FIFO_MGA_LENGTH;
  4300. } else {
  4301. /* When accel is not enabled ,there will be no accel data.
  4302. so we update the data index as complete */
  4303. *data_index = dev->fifo->length;
  4304. }
  4305. if (*data_read_length > dev->fifo->length) {
  4306. /* Handling the case where more data is requested
  4307. than that is available*/
  4308. *data_read_length = dev->fifo->length;
  4309. }
  4310. }
  4311. /*!
  4312. * @brief This API is used to parse the accelerometer data from the
  4313. * FIFO data in both header mode and header-less mode.
  4314. * It updates the idx value which is used to store the index of
  4315. * the current data byte which is parsed.
  4316. */
  4317. static void unpack_accel_frame(struct bmi160_sensor_data *acc, uint16_t *idx, uint8_t *acc_idx, uint8_t frame_info,
  4318. const struct bmi160_dev *dev)
  4319. {
  4320. switch (frame_info) {
  4321. case BMI160_FIFO_HEAD_A:
  4322. case BMI160_FIFO_A_ENABLE:
  4323. /*Partial read, then skip the data*/
  4324. if ((*idx + BMI160_FIFO_A_LENGTH) > dev->fifo->length) {
  4325. /*Update the data index as complete*/
  4326. *idx = dev->fifo->length;
  4327. break;
  4328. }
  4329. /*Unpack the data array into the structure instance "acc" */
  4330. unpack_accel_data(&acc[*acc_idx], *idx, dev);
  4331. /*Move the data index*/
  4332. *idx = *idx + BMI160_FIFO_A_LENGTH;
  4333. (*acc_idx)++;
  4334. break;
  4335. case BMI160_FIFO_HEAD_G_A:
  4336. case BMI160_FIFO_G_A_ENABLE:
  4337. /*Partial read, then skip the data*/
  4338. if ((*idx + BMI160_FIFO_GA_LENGTH) > dev->fifo->length) {
  4339. /*Update the data index as complete*/
  4340. *idx = dev->fifo->length;
  4341. break;
  4342. }
  4343. /*Unpack the data array into structure instance "acc"*/
  4344. unpack_accel_data(&acc[*acc_idx], *idx + BMI160_FIFO_G_LENGTH, dev);
  4345. /*Move the data index*/
  4346. *idx = *idx + BMI160_FIFO_GA_LENGTH;
  4347. (*acc_idx)++;
  4348. break;
  4349. case BMI160_FIFO_HEAD_M_A:
  4350. case BMI160_FIFO_M_A_ENABLE:
  4351. /*Partial read, then skip the data*/
  4352. if ((*idx + BMI160_FIFO_MA_LENGTH) > dev->fifo->length) {
  4353. /*Update the data index as complete*/
  4354. *idx = dev->fifo->length;
  4355. break;
  4356. }
  4357. /*Unpack the data array into structure instance "acc"*/
  4358. unpack_accel_data(&acc[*acc_idx], *idx + BMI160_FIFO_M_LENGTH, dev);
  4359. /*Move the data index*/
  4360. *idx = *idx + BMI160_FIFO_MA_LENGTH;
  4361. (*acc_idx)++;
  4362. break;
  4363. case BMI160_FIFO_HEAD_M_G_A:
  4364. case BMI160_FIFO_M_G_A_ENABLE:
  4365. /*Partial read, then skip the data*/
  4366. if ((*idx + BMI160_FIFO_MGA_LENGTH) > dev->fifo->length) {
  4367. /*Update the data index as complete*/
  4368. *idx = dev->fifo->length;
  4369. break;
  4370. }
  4371. /*Unpack the data array into structure instance "acc"*/
  4372. unpack_accel_data(&acc[*acc_idx], *idx + BMI160_FIFO_MG_LENGTH, dev);
  4373. /*Move the data index*/
  4374. *idx = *idx + BMI160_FIFO_MGA_LENGTH;
  4375. (*acc_idx)++;
  4376. break;
  4377. case BMI160_FIFO_HEAD_M:
  4378. case BMI160_FIFO_M_ENABLE:
  4379. (*idx) = (*idx) + BMI160_FIFO_M_LENGTH;
  4380. break;
  4381. case BMI160_FIFO_HEAD_G:
  4382. case BMI160_FIFO_G_ENABLE:
  4383. (*idx) = (*idx) + BMI160_FIFO_G_LENGTH;
  4384. break;
  4385. case BMI160_FIFO_HEAD_M_G:
  4386. case BMI160_FIFO_M_G_ENABLE:
  4387. (*idx) = (*idx) + BMI160_FIFO_MG_LENGTH;
  4388. break;
  4389. default:
  4390. break;
  4391. }
  4392. }
  4393. /*!
  4394. * @brief This API is used to parse the accelerometer data from the
  4395. * FIFO data and store it in the instance of the structure bmi160_sensor_data.
  4396. */
  4397. static void unpack_accel_data(struct bmi160_sensor_data *accel_data, uint16_t data_start_index,
  4398. const struct bmi160_dev *dev)
  4399. {
  4400. uint16_t data_lsb;
  4401. uint16_t data_msb;
  4402. /* Accel raw x data */
  4403. data_lsb = dev->fifo->data[data_start_index++];
  4404. data_msb = dev->fifo->data[data_start_index++];
  4405. accel_data->x = (int16_t)((data_msb << 8) | data_lsb);
  4406. /* Accel raw y data */
  4407. data_lsb = dev->fifo->data[data_start_index++];
  4408. data_msb = dev->fifo->data[data_start_index++];
  4409. accel_data->y = (int16_t)((data_msb << 8) | data_lsb);
  4410. /* Accel raw z data */
  4411. data_lsb = dev->fifo->data[data_start_index++];
  4412. data_msb = dev->fifo->data[data_start_index++];
  4413. accel_data->z = (int16_t)((data_msb << 8) | data_lsb);
  4414. }
  4415. /*!
  4416. * @brief This API is used to parse the accelerometer data from the
  4417. * FIFO data in header mode.
  4418. */
  4419. static void extract_accel_header_mode(struct bmi160_sensor_data *accel_data, uint8_t *accel_length,
  4420. const struct bmi160_dev *dev)
  4421. {
  4422. uint8_t frame_header = 0;
  4423. uint16_t data_index;
  4424. uint8_t accel_index = 0;
  4425. for (data_index = dev->fifo->accel_byte_start_idx; data_index < dev->fifo->length;) {
  4426. /* extracting Frame header */
  4427. frame_header = (dev->fifo->data[data_index] & BMI160_FIFO_TAG_INTR_MASK);
  4428. /*Index is moved to next byte where the data is starting*/
  4429. data_index++;
  4430. switch (frame_header) {
  4431. /* Accel frame */
  4432. case BMI160_FIFO_HEAD_A:
  4433. case BMI160_FIFO_HEAD_M_A:
  4434. case BMI160_FIFO_HEAD_G_A:
  4435. case BMI160_FIFO_HEAD_M_G_A:
  4436. unpack_accel_frame(accel_data, &data_index, &accel_index, frame_header, dev);
  4437. break;
  4438. case BMI160_FIFO_HEAD_M:
  4439. move_next_frame(&data_index, BMI160_FIFO_M_LENGTH, dev);
  4440. break;
  4441. case BMI160_FIFO_HEAD_G:
  4442. move_next_frame(&data_index, BMI160_FIFO_G_LENGTH, dev);
  4443. break;
  4444. case BMI160_FIFO_HEAD_M_G:
  4445. move_next_frame(&data_index, BMI160_FIFO_MG_LENGTH, dev);
  4446. break;
  4447. /* Sensor time frame */
  4448. case BMI160_FIFO_HEAD_SENSOR_TIME:
  4449. unpack_sensortime_frame(&data_index, dev);
  4450. break;
  4451. /* Skip frame */
  4452. case BMI160_FIFO_HEAD_SKIP_FRAME:
  4453. unpack_skipped_frame(&data_index, dev);
  4454. break;
  4455. /* Input config frame */
  4456. case BMI160_FIFO_HEAD_INPUT_CONFIG:
  4457. move_next_frame(&data_index, 1, dev);
  4458. break;
  4459. case BMI160_FIFO_HEAD_OVER_READ:
  4460. /* Update the data index as complete in case of Over read */
  4461. data_index = dev->fifo->length;
  4462. break;
  4463. default:
  4464. break;
  4465. }
  4466. if (*accel_length == accel_index) {
  4467. /* Number of frames to read completed */
  4468. break;
  4469. }
  4470. }
  4471. /*Update number of accel data read*/
  4472. *accel_length = accel_index;
  4473. /*Update the accel frame index*/
  4474. dev->fifo->accel_byte_start_idx = data_index;
  4475. }
  4476. /*!
  4477. * @brief This API computes the number of bytes of gyro FIFO data
  4478. * which is to be parsed in header-less mode
  4479. */
  4480. static void get_gyro_len_to_parse(uint16_t *data_index, uint16_t *data_read_length, const uint8_t *gyro_frame_count,
  4481. const struct bmi160_dev *dev)
  4482. {
  4483. /* Data start index */
  4484. *data_index = dev->fifo->gyro_byte_start_idx;
  4485. if (dev->fifo->fifo_data_enable == BMI160_FIFO_G_ENABLE) {
  4486. *data_read_length = (*gyro_frame_count) * BMI160_FIFO_G_LENGTH;
  4487. } else if (dev->fifo->fifo_data_enable == BMI160_FIFO_G_A_ENABLE) {
  4488. *data_read_length = (*gyro_frame_count) * BMI160_FIFO_GA_LENGTH;
  4489. } else if (dev->fifo->fifo_data_enable == BMI160_FIFO_M_G_ENABLE) {
  4490. *data_read_length = (*gyro_frame_count) * BMI160_FIFO_MG_LENGTH;
  4491. } else if (dev->fifo->fifo_data_enable == BMI160_FIFO_M_G_A_ENABLE) {
  4492. *data_read_length = (*gyro_frame_count) * BMI160_FIFO_MGA_LENGTH;
  4493. } else {
  4494. /* When gyro is not enabled ,there will be no gyro data.
  4495. so we update the data index as complete */
  4496. *data_index = dev->fifo->length;
  4497. }
  4498. if (*data_read_length > dev->fifo->length) {
  4499. /* Handling the case where more data is requested
  4500. than that is available*/
  4501. *data_read_length = dev->fifo->length;
  4502. }
  4503. }
  4504. /*!
  4505. * @brief This API is used to parse the gyroscope's data from the
  4506. * FIFO data in both header mode and header-less mode.
  4507. * It updates the idx value which is used to store the index of
  4508. * the current data byte which is parsed.
  4509. */
  4510. static void unpack_gyro_frame(struct bmi160_sensor_data *gyro, uint16_t *idx, uint8_t *gyro_idx, uint8_t frame_info,
  4511. const struct bmi160_dev *dev)
  4512. {
  4513. switch (frame_info) {
  4514. case BMI160_FIFO_HEAD_G:
  4515. case BMI160_FIFO_G_ENABLE:
  4516. /*Partial read, then skip the data*/
  4517. if ((*idx + BMI160_FIFO_G_LENGTH) > dev->fifo->length) {
  4518. /*Update the data index as complete*/
  4519. *idx = dev->fifo->length;
  4520. break;
  4521. }
  4522. /*Unpack the data array into structure instance "gyro"*/
  4523. unpack_gyro_data(&gyro[*gyro_idx], *idx, dev);
  4524. /*Move the data index*/
  4525. (*idx) = (*idx) + BMI160_FIFO_G_LENGTH;
  4526. (*gyro_idx)++;
  4527. break;
  4528. case BMI160_FIFO_HEAD_G_A:
  4529. case BMI160_FIFO_G_A_ENABLE:
  4530. /*Partial read, then skip the data*/
  4531. if ((*idx + BMI160_FIFO_GA_LENGTH) > dev->fifo->length) {
  4532. /*Update the data index as complete*/
  4533. *idx = dev->fifo->length;
  4534. break;
  4535. }
  4536. /* Unpack the data array into structure instance "gyro" */
  4537. unpack_gyro_data(&gyro[*gyro_idx], *idx, dev);
  4538. /* Move the data index */
  4539. *idx = *idx + BMI160_FIFO_GA_LENGTH;
  4540. (*gyro_idx)++;
  4541. break;
  4542. case BMI160_FIFO_HEAD_M_G_A:
  4543. case BMI160_FIFO_M_G_A_ENABLE:
  4544. /*Partial read, then skip the data*/
  4545. if ((*idx + BMI160_FIFO_MGA_LENGTH) > dev->fifo->length) {
  4546. /*Update the data index as complete*/
  4547. *idx = dev->fifo->length;
  4548. break;
  4549. }
  4550. /*Unpack the data array into structure instance "gyro"*/
  4551. unpack_gyro_data(&gyro[*gyro_idx], *idx + BMI160_FIFO_M_LENGTH, dev);
  4552. /*Move the data index*/
  4553. *idx = *idx + BMI160_FIFO_MGA_LENGTH;
  4554. (*gyro_idx)++;
  4555. break;
  4556. case BMI160_FIFO_HEAD_M_A:
  4557. case BMI160_FIFO_M_A_ENABLE:
  4558. /* Move the data index */
  4559. *idx = *idx + BMI160_FIFO_MA_LENGTH;
  4560. break;
  4561. case BMI160_FIFO_HEAD_M:
  4562. case BMI160_FIFO_M_ENABLE:
  4563. (*idx) = (*idx) + BMI160_FIFO_M_LENGTH;
  4564. break;
  4565. case BMI160_FIFO_HEAD_M_G:
  4566. case BMI160_FIFO_M_G_ENABLE:
  4567. /*Partial read, then skip the data*/
  4568. if ((*idx + BMI160_FIFO_MG_LENGTH) > dev->fifo->length) {
  4569. /*Update the data index as complete*/
  4570. *idx = dev->fifo->length;
  4571. break;
  4572. }
  4573. /*Unpack the data array into structure instance "gyro"*/
  4574. unpack_gyro_data(&gyro[*gyro_idx], *idx + BMI160_FIFO_M_LENGTH, dev);
  4575. /*Move the data index*/
  4576. (*idx) = (*idx) + BMI160_FIFO_MG_LENGTH;
  4577. (*gyro_idx)++;
  4578. break;
  4579. case BMI160_FIFO_HEAD_A:
  4580. case BMI160_FIFO_A_ENABLE:
  4581. /*Move the data index*/
  4582. *idx = *idx + BMI160_FIFO_A_LENGTH;
  4583. break;
  4584. default:
  4585. break;
  4586. }
  4587. }
  4588. /*!
  4589. * @brief This API is used to parse the gyro data from the
  4590. * FIFO data and store it in the instance of the structure bmi160_sensor_data.
  4591. */
  4592. static void unpack_gyro_data(struct bmi160_sensor_data *gyro_data, uint16_t data_start_index,
  4593. const struct bmi160_dev *dev)
  4594. {
  4595. uint16_t data_lsb;
  4596. uint16_t data_msb;
  4597. /* Gyro raw x data */
  4598. data_lsb = dev->fifo->data[data_start_index++];
  4599. data_msb = dev->fifo->data[data_start_index++];
  4600. gyro_data->x = (int16_t)((data_msb << 8) | data_lsb);
  4601. /* Gyro raw y data */
  4602. data_lsb = dev->fifo->data[data_start_index++];
  4603. data_msb = dev->fifo->data[data_start_index++];
  4604. gyro_data->y = (int16_t)((data_msb << 8) | data_lsb);
  4605. /* Gyro raw z data */
  4606. data_lsb = dev->fifo->data[data_start_index++];
  4607. data_msb = dev->fifo->data[data_start_index++];
  4608. gyro_data->z = (int16_t)((data_msb << 8) | data_lsb);
  4609. }
  4610. /*!
  4611. * @brief This API is used to parse the gyro data from the
  4612. * FIFO data in header mode.
  4613. */
  4614. static void extract_gyro_header_mode(struct bmi160_sensor_data *gyro_data, uint8_t *gyro_length,
  4615. const struct bmi160_dev *dev)
  4616. {
  4617. uint8_t frame_header = 0;
  4618. uint16_t data_index;
  4619. uint8_t gyro_index = 0;
  4620. for (data_index = dev->fifo->gyro_byte_start_idx; data_index < dev->fifo->length;) {
  4621. /* extracting Frame header */
  4622. frame_header = (dev->fifo->data[data_index] & BMI160_FIFO_TAG_INTR_MASK);
  4623. /*Index is moved to next byte where the data is starting*/
  4624. data_index++;
  4625. switch (frame_header) {
  4626. /* GYRO frame */
  4627. case BMI160_FIFO_HEAD_G:
  4628. case BMI160_FIFO_HEAD_G_A:
  4629. case BMI160_FIFO_HEAD_M_G:
  4630. case BMI160_FIFO_HEAD_M_G_A:
  4631. unpack_gyro_frame(gyro_data, &data_index, &gyro_index, frame_header, dev);
  4632. break;
  4633. case BMI160_FIFO_HEAD_A:
  4634. move_next_frame(&data_index, BMI160_FIFO_A_LENGTH, dev);
  4635. break;
  4636. case BMI160_FIFO_HEAD_M:
  4637. move_next_frame(&data_index, BMI160_FIFO_M_LENGTH, dev);
  4638. break;
  4639. case BMI160_FIFO_HEAD_M_A:
  4640. move_next_frame(&data_index, BMI160_FIFO_M_LENGTH, dev);
  4641. break;
  4642. /* Sensor time frame */
  4643. case BMI160_FIFO_HEAD_SENSOR_TIME:
  4644. unpack_sensortime_frame(&data_index, dev);
  4645. break;
  4646. /* Skip frame */
  4647. case BMI160_FIFO_HEAD_SKIP_FRAME:
  4648. unpack_skipped_frame(&data_index, dev);
  4649. break;
  4650. /* Input config frame */
  4651. case BMI160_FIFO_HEAD_INPUT_CONFIG:
  4652. move_next_frame(&data_index, 1, dev);
  4653. break;
  4654. case BMI160_FIFO_HEAD_OVER_READ:
  4655. /* Update the data index as complete in case of over read */
  4656. data_index = dev->fifo->length;
  4657. break;
  4658. default:
  4659. break;
  4660. }
  4661. if (*gyro_length == gyro_index) {
  4662. /*Number of frames to read completed*/
  4663. break;
  4664. }
  4665. }
  4666. /*Update number of gyro data read*/
  4667. *gyro_length = gyro_index;
  4668. /*Update the gyro frame index*/
  4669. dev->fifo->gyro_byte_start_idx = data_index;
  4670. }
  4671. /*!
  4672. * @brief This API computes the number of bytes of aux FIFO data
  4673. * which is to be parsed in header-less mode
  4674. */
  4675. static void get_aux_len_to_parse(uint16_t *data_index, uint16_t *data_read_length, const uint8_t *aux_frame_count,
  4676. const struct bmi160_dev *dev)
  4677. {
  4678. /* Data start index */
  4679. *data_index = dev->fifo->gyro_byte_start_idx;
  4680. if (dev->fifo->fifo_data_enable == BMI160_FIFO_M_ENABLE) {
  4681. *data_read_length = (*aux_frame_count) * BMI160_FIFO_M_LENGTH;
  4682. } else if (dev->fifo->fifo_data_enable == BMI160_FIFO_M_A_ENABLE) {
  4683. *data_read_length = (*aux_frame_count) * BMI160_FIFO_MA_LENGTH;
  4684. } else if (dev->fifo->fifo_data_enable == BMI160_FIFO_M_G_ENABLE) {
  4685. *data_read_length = (*aux_frame_count) * BMI160_FIFO_MG_LENGTH;
  4686. } else if (dev->fifo->fifo_data_enable == BMI160_FIFO_M_G_A_ENABLE) {
  4687. *data_read_length = (*aux_frame_count) * BMI160_FIFO_MGA_LENGTH;
  4688. } else {
  4689. /* When aux is not enabled ,there will be no aux data.
  4690. * so we update the data index as complete */
  4691. *data_index = dev->fifo->length;
  4692. }
  4693. if (*data_read_length > dev->fifo->length) {
  4694. /* Handling the case where more data is requested
  4695. * than that is available */
  4696. *data_read_length = dev->fifo->length;
  4697. }
  4698. }
  4699. /*!
  4700. * @brief This API is used to parse the aux's data from the
  4701. * FIFO data in both header mode and header-less mode.
  4702. * It updates the idx value which is used to store the index of
  4703. * the current data byte which is parsed
  4704. */
  4705. static void unpack_aux_frame(struct bmi160_aux_data *aux_data, uint16_t *idx, uint8_t *aux_index, uint8_t frame_info,
  4706. const struct bmi160_dev *dev)
  4707. {
  4708. switch (frame_info) {
  4709. case BMI160_FIFO_HEAD_M:
  4710. case BMI160_FIFO_M_ENABLE:
  4711. /* Partial read, then skip the data */
  4712. if ((*idx + BMI160_FIFO_M_LENGTH) > dev->fifo->length) {
  4713. /* Update the data index as complete */
  4714. *idx = dev->fifo->length;
  4715. break;
  4716. }
  4717. /* Unpack the data array into structure instance */
  4718. unpack_aux_data(&aux_data[*aux_index], *idx, dev);
  4719. /* Move the data index */
  4720. *idx = *idx + BMI160_FIFO_M_LENGTH;
  4721. (*aux_index)++;
  4722. break;
  4723. case BMI160_FIFO_HEAD_M_A:
  4724. case BMI160_FIFO_M_A_ENABLE:
  4725. /* Partial read, then skip the data */
  4726. if ((*idx + BMI160_FIFO_MA_LENGTH) > dev->fifo->length) {
  4727. /* Update the data index as complete */
  4728. *idx = dev->fifo->length;
  4729. break;
  4730. }
  4731. /* Unpack the data array into structure instance */
  4732. unpack_aux_data(&aux_data[*aux_index], *idx, dev);
  4733. /* Move the data index */
  4734. *idx = *idx + BMI160_FIFO_MA_LENGTH;
  4735. (*aux_index)++;
  4736. break;
  4737. case BMI160_FIFO_HEAD_M_G:
  4738. case BMI160_FIFO_M_G_ENABLE:
  4739. /* Partial read, then skip the data */
  4740. if ((*idx + BMI160_FIFO_MG_LENGTH) > dev->fifo->length) {
  4741. /* Update the data index as complete */
  4742. *idx = dev->fifo->length;
  4743. break;
  4744. }
  4745. /* Unpack the data array into structure instance */
  4746. unpack_aux_data(&aux_data[*aux_index], *idx, dev);
  4747. /* Move the data index */
  4748. (*idx) = (*idx) + BMI160_FIFO_MG_LENGTH;
  4749. (*aux_index)++;
  4750. break;
  4751. case BMI160_FIFO_HEAD_M_G_A:
  4752. case BMI160_FIFO_M_G_A_ENABLE:
  4753. /*Partial read, then skip the data*/
  4754. if ((*idx + BMI160_FIFO_MGA_LENGTH) > dev->fifo->length) {
  4755. /* Update the data index as complete */
  4756. *idx = dev->fifo->length;
  4757. break;
  4758. }
  4759. /* Unpack the data array into structure instance */
  4760. unpack_aux_data(&aux_data[*aux_index], *idx, dev);
  4761. /*Move the data index*/
  4762. *idx = *idx + BMI160_FIFO_MGA_LENGTH;
  4763. (*aux_index)++;
  4764. break;
  4765. case BMI160_FIFO_HEAD_G:
  4766. case BMI160_FIFO_G_ENABLE:
  4767. /* Move the data index */
  4768. (*idx) = (*idx) + BMI160_FIFO_G_LENGTH;
  4769. break;
  4770. case BMI160_FIFO_HEAD_G_A:
  4771. case BMI160_FIFO_G_A_ENABLE:
  4772. /* Move the data index */
  4773. *idx = *idx + BMI160_FIFO_GA_LENGTH;
  4774. break;
  4775. case BMI160_FIFO_HEAD_A:
  4776. case BMI160_FIFO_A_ENABLE:
  4777. /* Move the data index */
  4778. *idx = *idx + BMI160_FIFO_A_LENGTH;
  4779. break;
  4780. default:
  4781. break;
  4782. }
  4783. }
  4784. /*!
  4785. * @brief This API is used to parse the aux data from the
  4786. * FIFO data and store it in the instance of the structure bmi160_aux_data.
  4787. */
  4788. static void unpack_aux_data(struct bmi160_aux_data *aux_data, uint16_t data_start_index,
  4789. const struct bmi160_dev *dev)
  4790. {
  4791. /* Aux data bytes */
  4792. aux_data->data[0] = dev->fifo->data[data_start_index++];
  4793. aux_data->data[1] = dev->fifo->data[data_start_index++];
  4794. aux_data->data[2] = dev->fifo->data[data_start_index++];
  4795. aux_data->data[3] = dev->fifo->data[data_start_index++];
  4796. aux_data->data[4] = dev->fifo->data[data_start_index++];
  4797. aux_data->data[5] = dev->fifo->data[data_start_index++];
  4798. aux_data->data[6] = dev->fifo->data[data_start_index++];
  4799. aux_data->data[7] = dev->fifo->data[data_start_index++];
  4800. }
  4801. /*!
  4802. * @brief This API is used to parse the aux data from the
  4803. * FIFO data in header mode.
  4804. */
  4805. static void extract_aux_header_mode(struct bmi160_aux_data *aux_data, uint8_t *aux_length,
  4806. const struct bmi160_dev *dev)
  4807. {
  4808. uint8_t frame_header = 0;
  4809. uint16_t data_index;
  4810. uint8_t aux_index = 0;
  4811. for (data_index = dev->fifo->aux_byte_start_idx; data_index < dev->fifo->length;) {
  4812. /* extracting Frame header */
  4813. frame_header = (dev->fifo->data[data_index] & BMI160_FIFO_TAG_INTR_MASK);
  4814. /*Index is moved to next byte where the data is starting*/
  4815. data_index++;
  4816. switch (frame_header) {
  4817. /* Aux frame */
  4818. case BMI160_FIFO_HEAD_M:
  4819. case BMI160_FIFO_HEAD_M_A:
  4820. case BMI160_FIFO_HEAD_M_G:
  4821. case BMI160_FIFO_HEAD_M_G_A:
  4822. unpack_aux_frame(aux_data, &data_index, &aux_index, frame_header, dev);
  4823. break;
  4824. case BMI160_FIFO_HEAD_G:
  4825. move_next_frame(&data_index, BMI160_FIFO_G_LENGTH, dev);
  4826. break;
  4827. case BMI160_FIFO_HEAD_G_A:
  4828. move_next_frame(&data_index, BMI160_FIFO_GA_LENGTH, dev);
  4829. break;
  4830. case BMI160_FIFO_HEAD_A:
  4831. move_next_frame(&data_index, BMI160_FIFO_A_LENGTH, dev);
  4832. break;
  4833. /* Sensor time frame */
  4834. case BMI160_FIFO_HEAD_SENSOR_TIME:
  4835. unpack_sensortime_frame(&data_index, dev);
  4836. break;
  4837. /* Skip frame */
  4838. case BMI160_FIFO_HEAD_SKIP_FRAME:
  4839. unpack_skipped_frame(&data_index, dev);
  4840. break;
  4841. /* Input config frame */
  4842. case BMI160_FIFO_HEAD_INPUT_CONFIG:
  4843. move_next_frame(&data_index, 1, dev);
  4844. break;
  4845. case BMI160_FIFO_HEAD_OVER_READ:
  4846. /* Update the data index as complete in case
  4847. * of over read */
  4848. data_index = dev->fifo->length;
  4849. break;
  4850. default:
  4851. /* Update the data index as complete in case of
  4852. * getting other headers like 0x00 */
  4853. data_index = dev->fifo->length;
  4854. break;
  4855. }
  4856. if (*aux_length == aux_index) {
  4857. /*Number of frames to read completed*/
  4858. break;
  4859. }
  4860. }
  4861. /* Update number of aux data read */
  4862. *aux_length = aux_index;
  4863. /* Update the aux frame index */
  4864. dev->fifo->aux_byte_start_idx = data_index;
  4865. }
  4866. /*!
  4867. * @brief This API checks the presence of non-valid frames in the read fifo data.
  4868. */
  4869. static void check_frame_validity(uint16_t *data_index, const struct bmi160_dev *dev)
  4870. {
  4871. if ((*data_index + 2) < dev->fifo->length) {
  4872. /* Check if FIFO is empty */
  4873. if ((dev->fifo->data[*data_index] == FIFO_CONFIG_MSB_CHECK)
  4874. && (dev->fifo->data[*data_index + 1] == FIFO_CONFIG_LSB_CHECK)) {
  4875. /*Update the data index as complete*/
  4876. *data_index = dev->fifo->length;
  4877. }
  4878. }
  4879. }
  4880. /*!
  4881. * @brief This API is used to move the data index ahead of the
  4882. * current_frame_length parameter when unnecessary FIFO data appears while
  4883. * extracting the user specified data.
  4884. */
  4885. static void move_next_frame(uint16_t *data_index, uint8_t current_frame_length, const struct bmi160_dev *dev)
  4886. {
  4887. /*Partial read, then move the data index to last data*/
  4888. if ((*data_index + current_frame_length) > dev->fifo->length) {
  4889. /*Update the data index as complete*/
  4890. *data_index = dev->fifo->length;
  4891. } else {
  4892. /*Move the data index to next frame*/
  4893. *data_index = *data_index + current_frame_length;
  4894. }
  4895. }
  4896. /*!
  4897. * @brief This API is used to parse and store the sensor time from the
  4898. * FIFO data in the structure instance dev.
  4899. */
  4900. static void unpack_sensortime_frame(uint16_t *data_index, const struct bmi160_dev *dev)
  4901. {
  4902. uint32_t sensor_time_byte3 = 0;
  4903. uint16_t sensor_time_byte2 = 0;
  4904. uint8_t sensor_time_byte1 = 0;
  4905. /*Partial read, then move the data index to last data*/
  4906. if ((*data_index + BMI160_SENSOR_TIME_LENGTH) > dev->fifo->length) {
  4907. /*Update the data index as complete*/
  4908. *data_index = dev->fifo->length;
  4909. } else {
  4910. sensor_time_byte3 = dev->fifo->data[(*data_index) + BMI160_SENSOR_TIME_MSB_BYTE] << 16;
  4911. sensor_time_byte2 = dev->fifo->data[(*data_index) + BMI160_SENSOR_TIME_XLSB_BYTE] << 8;
  4912. sensor_time_byte1 = dev->fifo->data[(*data_index)];
  4913. /* Sensor time */
  4914. dev->fifo->sensor_time = (uint32_t)(sensor_time_byte3 | sensor_time_byte2 | sensor_time_byte1);
  4915. *data_index = (*data_index) + BMI160_SENSOR_TIME_LENGTH;
  4916. }
  4917. }
  4918. /*!
  4919. * @brief This API is used to parse and store the skipped_frame_count from
  4920. * the FIFO data in the structure instance dev.
  4921. */
  4922. static void unpack_skipped_frame(uint16_t *data_index, const struct bmi160_dev *dev)
  4923. {
  4924. /*Partial read, then move the data index to last data*/
  4925. if (*data_index >= dev->fifo->length) {
  4926. /*Update the data index as complete*/
  4927. *data_index = dev->fifo->length;
  4928. } else {
  4929. dev->fifo->skipped_frame_count = dev->fifo->data[*data_index];
  4930. /*Move the data index*/
  4931. *data_index = (*data_index) + 1;
  4932. }
  4933. }
  4934. /*!
  4935. * @brief This API is used to get the FOC status from the sensor
  4936. */
  4937. static int8_t get_foc_status(uint8_t *foc_status, struct bmi160_dev const *dev)
  4938. {
  4939. int8_t rslt;
  4940. uint8_t data;
  4941. /* Read the FOC status from sensor */
  4942. rslt = bmi160_get_regs(BMI160_STATUS_ADDR, &data, 1, dev);
  4943. if (rslt == BMI160_OK) {
  4944. /* Get the foc_status bit */
  4945. *foc_status = BMI160_GET_BITS(data, BMI160_FOC_STATUS);
  4946. }
  4947. return rslt;
  4948. }
  4949. /*!
  4950. * @brief This API is used to configure the offset enable bits in the sensor
  4951. */
  4952. static int8_t configure_offset_enable(const struct bmi160_foc_conf *foc_conf, struct bmi160_dev const *dev)
  4953. {
  4954. int8_t rslt;
  4955. uint8_t data;
  4956. /* Null-pointer check */
  4957. rslt = null_ptr_check(dev);
  4958. if (rslt != BMI160_OK) {
  4959. rslt = BMI160_E_NULL_PTR;
  4960. } else {
  4961. /* Read the FOC config from the sensor */
  4962. rslt = bmi160_get_regs(BMI160_OFFSET_CONF_ADDR, &data, 1, dev);
  4963. if (rslt == BMI160_OK) {
  4964. /* Set the offset enable/disable for gyro */
  4965. data = BMI160_SET_BITS(data, BMI160_GYRO_OFFSET_EN, foc_conf->gyro_off_en);
  4966. /* Set the offset enable/disable for accel */
  4967. data = BMI160_SET_BITS(data, BMI160_ACCEL_OFFSET_EN, foc_conf->acc_off_en);
  4968. /* Set the offset config in the sensor */
  4969. rslt = bmi160_set_regs(BMI160_OFFSET_CONF_ADDR, &data, 1, dev);
  4970. }
  4971. }
  4972. return rslt;
  4973. }
  4974. static int8_t trigger_foc(struct bmi160_offsets *offset, struct bmi160_dev const *dev)
  4975. {
  4976. int8_t rslt;
  4977. uint8_t foc_status;
  4978. uint8_t cmd = BMI160_START_FOC_CMD;
  4979. uint8_t timeout = 0;
  4980. uint8_t data_array[20];
  4981. /* Start the FOC process */
  4982. rslt = bmi160_set_regs(BMI160_COMMAND_REG_ADDR, &cmd, 1, dev);
  4983. if (rslt == BMI160_OK) {
  4984. /* Check the FOC status*/
  4985. rslt = get_foc_status(&foc_status, dev);
  4986. if ((rslt != BMI160_OK) || (foc_status != BMI160_ENABLE)) {
  4987. while ((foc_status != BMI160_ENABLE) && (timeout < 11)) {
  4988. /* Maximum time of 250ms is given in 10
  4989. * steps of 25ms each */
  4990. dev->delay_ms(25);
  4991. /* Check the FOC status*/
  4992. rslt = get_foc_status(&foc_status, dev);
  4993. timeout++;
  4994. }
  4995. if ((rslt == BMI160_OK) && (foc_status == BMI160_ENABLE)) {
  4996. /* Get offset values from sensor */
  4997. rslt = bmi160_get_offsets(offset, dev);
  4998. } else {
  4999. /* FOC failure case */
  5000. rslt = BMI160_FOC_FAILURE;
  5001. }
  5002. }
  5003. if (rslt == BMI160_OK) {
  5004. /* Read registers 0x04-0x17 */
  5005. rslt = bmi160_get_regs(BMI160_GYRO_DATA_ADDR,
  5006. data_array, 20, dev);
  5007. }
  5008. }
  5009. return rslt;
  5010. }
  5011. /** @}*/