bmi160.c 171 KB

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