bmi160.c 168 KB

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