bmi160.c 167 KB

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