bmi160.c 158 KB

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