lsm6dso.c 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832
  1. /**
  2. ******************************************************************************
  3. * @file lsm6dso.c
  4. * @author MEMS Software Solutions Team
  5. * @brief LSM6DSO driver file
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "lsm6dso.h"
  21. /** @addtogroup BSP BSP
  22. * @{
  23. */
  24. /** @addtogroup Component Component
  25. * @{
  26. */
  27. /** @defgroup LSM6DSO LSM6DSO
  28. * @{
  29. */
  30. /** @defgroup LSM6DSO_Exported_Variables LSM6DSO Exported Variables
  31. * @{
  32. */
  33. LSM6DSO_CommonDrv_t LSM6DSO_COMMON_Driver =
  34. {
  35. LSM6DSO_Init,
  36. LSM6DSO_DeInit,
  37. LSM6DSO_ReadID,
  38. LSM6DSO_GetCapabilities,
  39. };
  40. LSM6DSO_ACC_Drv_t LSM6DSO_ACC_Driver =
  41. {
  42. LSM6DSO_ACC_Enable,
  43. LSM6DSO_ACC_Disable,
  44. LSM6DSO_ACC_GetSensitivity,
  45. LSM6DSO_ACC_GetOutputDataRate,
  46. LSM6DSO_ACC_SetOutputDataRate,
  47. LSM6DSO_ACC_GetFullScale,
  48. LSM6DSO_ACC_SetFullScale,
  49. LSM6DSO_ACC_GetAxes,
  50. LSM6DSO_ACC_GetAxesRaw,
  51. };
  52. LSM6DSO_GYRO_Drv_t LSM6DSO_GYRO_Driver =
  53. {
  54. LSM6DSO_GYRO_Enable,
  55. LSM6DSO_GYRO_Disable,
  56. LSM6DSO_GYRO_GetSensitivity,
  57. LSM6DSO_GYRO_GetOutputDataRate,
  58. LSM6DSO_GYRO_SetOutputDataRate,
  59. LSM6DSO_GYRO_GetFullScale,
  60. LSM6DSO_GYRO_SetFullScale,
  61. LSM6DSO_GYRO_GetAxes,
  62. LSM6DSO_GYRO_GetAxesRaw,
  63. };
  64. /**
  65. * @}
  66. */
  67. /** @defgroup LSM6DSO_Private_Function_Prototypes LSM6DSO Private Function Prototypes
  68. * @{
  69. */
  70. static int32_t LSM6DSO_ACC_SetOutputDataRate_When_Enabled(LSM6DSO_Object_t *pObj, float_t Odr);
  71. static int32_t LSM6DSO_ACC_SetOutputDataRate_When_Disabled(LSM6DSO_Object_t *pObj, float_t Odr);
  72. static int32_t LSM6DSO_GYRO_SetOutputDataRate_When_Enabled(LSM6DSO_Object_t *pObj, float_t Odr);
  73. static int32_t LSM6DSO_GYRO_SetOutputDataRate_When_Disabled(LSM6DSO_Object_t *pObj, float_t Odr);
  74. static int32_t ReadRegWrap(void *Handle, uint8_t Reg, uint8_t *pData, uint16_t Length);
  75. static int32_t WriteRegWrap(void *Handle, uint8_t Reg, uint8_t *pData, uint16_t Length);
  76. /**
  77. * @}
  78. */
  79. /** @defgroup LSM6DSO_Exported_Functions LSM6DSO Exported Functions
  80. * @{
  81. */
  82. /**
  83. * @brief Register Component Bus IO operations
  84. * @param pObj the device pObj
  85. * @retval 0 in case of success, an error code otherwise
  86. */
  87. int32_t LSM6DSO_RegisterBusIO(LSM6DSO_Object_t *pObj, LSM6DSO_IO_t *pIO)
  88. {
  89. int32_t ret = LSM6DSO_OK;
  90. if (pObj == NULL)
  91. {
  92. ret = LSM6DSO_ERROR;
  93. }
  94. else
  95. {
  96. pObj->IO.Init = pIO->Init;
  97. pObj->IO.DeInit = pIO->DeInit;
  98. pObj->IO.BusType = pIO->BusType;
  99. pObj->IO.Address = pIO->Address;
  100. pObj->IO.WriteReg = pIO->WriteReg;
  101. pObj->IO.ReadReg = pIO->ReadReg;
  102. pObj->IO.GetTick = pIO->GetTick;
  103. pObj->Ctx.read_reg = ReadRegWrap;
  104. pObj->Ctx.write_reg = WriteRegWrap;
  105. pObj->Ctx.handle = pObj;
  106. if (pObj->IO.Init == NULL)
  107. {
  108. ret = LSM6DSO_ERROR;
  109. }
  110. else if (pObj->IO.Init() != LSM6DSO_OK)
  111. {
  112. ret = LSM6DSO_ERROR;
  113. }
  114. else
  115. {
  116. if (pObj->IO.BusType == LSM6DSO_SPI_3WIRES_BUS) /* SPI 3-Wires */
  117. {
  118. /* Enable the SPI 3-Wires support only the first time */
  119. if (pObj->is_initialized == 0U)
  120. {
  121. /* Enable SPI 3-Wires on the component */
  122. uint8_t data = 0x0C;
  123. if (LSM6DSO_Write_Reg(pObj, LSM6DSO_CTRL3_C, data) != LSM6DSO_OK)
  124. {
  125. ret = LSM6DSO_ERROR;
  126. }
  127. }
  128. }
  129. }
  130. }
  131. return ret;
  132. }
  133. /**
  134. * @brief Initialize the LSM6DSO sensor
  135. * @param pObj the device pObj
  136. * @retval 0 in case of success, an error code otherwise
  137. */
  138. int32_t LSM6DSO_Init(LSM6DSO_Object_t *pObj)
  139. {
  140. /* Disable I3C */
  141. if (lsm6dso_i3c_disable_set(&(pObj->Ctx), LSM6DSO_I3C_DISABLE) != LSM6DSO_OK)
  142. {
  143. return LSM6DSO_ERROR;
  144. }
  145. /* Enable register address automatically incremented during a multiple byte
  146. access with a serial interface. */
  147. if (lsm6dso_auto_increment_set(&(pObj->Ctx), PROPERTY_ENABLE) != LSM6DSO_OK)
  148. {
  149. return LSM6DSO_ERROR;
  150. }
  151. /* Enable BDU */
  152. if (lsm6dso_block_data_update_set(&(pObj->Ctx), PROPERTY_ENABLE) != LSM6DSO_OK)
  153. {
  154. return LSM6DSO_ERROR;
  155. }
  156. /* FIFO mode selection */
  157. if (lsm6dso_fifo_mode_set(&(pObj->Ctx), LSM6DSO_BYPASS_MODE) != LSM6DSO_OK)
  158. {
  159. return LSM6DSO_ERROR;
  160. }
  161. /* Select default output data rate. */
  162. pObj->acc_odr = LSM6DSO_XL_ODR_104Hz;
  163. /* Output data rate selection - power down. */
  164. if (lsm6dso_xl_data_rate_set(&(pObj->Ctx), LSM6DSO_XL_ODR_OFF) != LSM6DSO_OK)
  165. {
  166. return LSM6DSO_ERROR;
  167. }
  168. /* Full scale selection. */
  169. if (lsm6dso_xl_full_scale_set(&(pObj->Ctx), LSM6DSO_2g) != LSM6DSO_OK)
  170. {
  171. return LSM6DSO_ERROR;
  172. }
  173. /* Select default output data rate. */
  174. pObj->gyro_odr = LSM6DSO_GY_ODR_104Hz;
  175. /* Output data rate selection - power down. */
  176. if (lsm6dso_gy_data_rate_set(&(pObj->Ctx), LSM6DSO_GY_ODR_OFF) != LSM6DSO_OK)
  177. {
  178. return LSM6DSO_ERROR;
  179. }
  180. /* Full scale selection. */
  181. if (lsm6dso_gy_full_scale_set(&(pObj->Ctx), LSM6DSO_2000dps) != LSM6DSO_OK)
  182. {
  183. return LSM6DSO_ERROR;
  184. }
  185. pObj->is_initialized = 1;
  186. return LSM6DSO_OK;
  187. }
  188. /**
  189. * @brief Deinitialize the LSM6DSO sensor
  190. * @param pObj the device pObj
  191. * @retval 0 in case of success, an error code otherwise
  192. */
  193. int32_t LSM6DSO_DeInit(LSM6DSO_Object_t *pObj)
  194. {
  195. /* Disable the component */
  196. if (LSM6DSO_ACC_Disable(pObj) != LSM6DSO_OK)
  197. {
  198. return LSM6DSO_ERROR;
  199. }
  200. if (LSM6DSO_GYRO_Disable(pObj) != LSM6DSO_OK)
  201. {
  202. return LSM6DSO_ERROR;
  203. }
  204. /* Reset output data rate. */
  205. pObj->acc_odr = LSM6DSO_XL_ODR_OFF;
  206. pObj->gyro_odr = LSM6DSO_GY_ODR_OFF;
  207. pObj->is_initialized = 0;
  208. return LSM6DSO_OK;
  209. }
  210. /**
  211. * @brief Read component ID
  212. * @param pObj the device pObj
  213. * @param Id the WHO_AM_I value
  214. * @retval 0 in case of success, an error code otherwise
  215. */
  216. int32_t LSM6DSO_ReadID(LSM6DSO_Object_t *pObj, uint8_t *Id)
  217. {
  218. if (lsm6dso_device_id_get(&(pObj->Ctx), Id) != LSM6DSO_OK)
  219. {
  220. return LSM6DSO_ERROR;
  221. }
  222. return LSM6DSO_OK;
  223. }
  224. /**
  225. * @brief Get LSM6DSO sensor capabilities
  226. * @param pObj Component object pointer
  227. * @param Capabilities pointer to LSM6DSO sensor capabilities
  228. * @retval 0 in case of success, an error code otherwise
  229. */
  230. int32_t LSM6DSO_GetCapabilities(LSM6DSO_Object_t *pObj, LSM6DSO_Capabilities_t *Capabilities)
  231. {
  232. /* Prevent unused argument(s) compilation warning */
  233. (void)(pObj);
  234. Capabilities->Acc = 1;
  235. Capabilities->Gyro = 1;
  236. Capabilities->Magneto = 0;
  237. Capabilities->LowPower = 0;
  238. Capabilities->GyroMaxFS = 2000;
  239. Capabilities->AccMaxFS = 16;
  240. Capabilities->MagMaxFS = 0;
  241. Capabilities->GyroMaxOdr = 6660.0f;
  242. Capabilities->AccMaxOdr = 6660.0f;
  243. Capabilities->MagMaxOdr = 0.0f;
  244. return LSM6DSO_OK;
  245. }
  246. /**
  247. * @brief Enable the LSM6DSO accelerometer sensor
  248. * @param pObj the device pObj
  249. * @retval 0 in case of success, an error code otherwise
  250. */
  251. int32_t LSM6DSO_ACC_Enable(LSM6DSO_Object_t *pObj)
  252. {
  253. /* Check if the component is already enabled */
  254. if (pObj->acc_is_enabled == 1U)
  255. {
  256. return LSM6DSO_OK;
  257. }
  258. /* Output data rate selection. */
  259. if (lsm6dso_xl_data_rate_set(&(pObj->Ctx), pObj->acc_odr) != LSM6DSO_OK)
  260. {
  261. return LSM6DSO_ERROR;
  262. }
  263. pObj->acc_is_enabled = 1;
  264. return LSM6DSO_OK;
  265. }
  266. /**
  267. * @brief Disable the LSM6DSO accelerometer sensor
  268. * @param pObj the device pObj
  269. * @retval 0 in case of success, an error code otherwise
  270. */
  271. int32_t LSM6DSO_ACC_Disable(LSM6DSO_Object_t *pObj)
  272. {
  273. /* Check if the component is already disabled */
  274. if (pObj->acc_is_enabled == 0U)
  275. {
  276. return LSM6DSO_OK;
  277. }
  278. /* Get current output data rate. */
  279. if (lsm6dso_xl_data_rate_get(&(pObj->Ctx), &pObj->acc_odr) != LSM6DSO_OK)
  280. {
  281. return LSM6DSO_ERROR;
  282. }
  283. /* Output data rate selection - power down. */
  284. if (lsm6dso_xl_data_rate_set(&(pObj->Ctx), LSM6DSO_XL_ODR_OFF) != LSM6DSO_OK)
  285. {
  286. return LSM6DSO_ERROR;
  287. }
  288. pObj->acc_is_enabled = 0;
  289. return LSM6DSO_OK;
  290. }
  291. /**
  292. * @brief Get the LSM6DSO accelerometer sensor sensitivity
  293. * @param pObj the device pObj
  294. * @param Sensitivity pointer
  295. * @retval 0 in case of success, an error code otherwise
  296. */
  297. int32_t LSM6DSO_ACC_GetSensitivity(LSM6DSO_Object_t *pObj, float_t *Sensitivity)
  298. {
  299. int32_t ret = LSM6DSO_OK;
  300. lsm6dso_fs_xl_t full_scale;
  301. /* Read actual full scale selection from sensor. */
  302. if (lsm6dso_xl_full_scale_get(&(pObj->Ctx), &full_scale) != LSM6DSO_OK)
  303. {
  304. return LSM6DSO_ERROR;
  305. }
  306. /* Store the Sensitivity based on actual full scale. */
  307. switch (full_scale)
  308. {
  309. case LSM6DSO_2g:
  310. *Sensitivity = LSM6DSO_ACC_SENSITIVITY_FS_2G;
  311. break;
  312. case LSM6DSO_4g:
  313. *Sensitivity = LSM6DSO_ACC_SENSITIVITY_FS_4G;
  314. break;
  315. case LSM6DSO_8g:
  316. *Sensitivity = LSM6DSO_ACC_SENSITIVITY_FS_8G;
  317. break;
  318. case LSM6DSO_16g:
  319. *Sensitivity = LSM6DSO_ACC_SENSITIVITY_FS_16G;
  320. break;
  321. default:
  322. ret = LSM6DSO_ERROR;
  323. break;
  324. }
  325. return ret;
  326. }
  327. /**
  328. * @brief Get the LSM6DSO accelerometer sensor output data rate
  329. * @param pObj the device pObj
  330. * @param Odr pointer where the output data rate is written
  331. * @retval 0 in case of success, an error code otherwise
  332. */
  333. int32_t LSM6DSO_ACC_GetOutputDataRate(LSM6DSO_Object_t *pObj, float_t *Odr)
  334. {
  335. int32_t ret = LSM6DSO_OK;
  336. lsm6dso_odr_xl_t odr_low_level;
  337. /* Get current output data rate. */
  338. if (lsm6dso_xl_data_rate_get(&(pObj->Ctx), &odr_low_level) != LSM6DSO_OK)
  339. {
  340. return LSM6DSO_ERROR;
  341. }
  342. switch (odr_low_level)
  343. {
  344. case LSM6DSO_XL_ODR_OFF:
  345. *Odr = 0.0f;
  346. break;
  347. case LSM6DSO_XL_ODR_1Hz6:
  348. *Odr = 1.6f;
  349. break;
  350. case LSM6DSO_XL_ODR_12Hz5:
  351. *Odr = 12.5f;
  352. break;
  353. case LSM6DSO_XL_ODR_26Hz:
  354. *Odr = 26.0f;
  355. break;
  356. case LSM6DSO_XL_ODR_52Hz:
  357. *Odr = 52.0f;
  358. break;
  359. case LSM6DSO_XL_ODR_104Hz:
  360. *Odr = 104.0f;
  361. break;
  362. case LSM6DSO_XL_ODR_208Hz:
  363. *Odr = 208.0f;
  364. break;
  365. case LSM6DSO_XL_ODR_417Hz:
  366. *Odr = 417.0f;
  367. break;
  368. case LSM6DSO_XL_ODR_833Hz:
  369. *Odr = 833.0f;
  370. break;
  371. case LSM6DSO_XL_ODR_1667Hz:
  372. *Odr = 1667.0f;
  373. break;
  374. case LSM6DSO_XL_ODR_3333Hz:
  375. *Odr = 3333.0f;
  376. break;
  377. case LSM6DSO_XL_ODR_6667Hz:
  378. *Odr = 6667.0f;
  379. break;
  380. default:
  381. ret = LSM6DSO_ERROR;
  382. break;
  383. }
  384. return ret;
  385. }
  386. /**
  387. * @brief Set the LSM6DSO accelerometer sensor output data rate
  388. * @param pObj the device pObj
  389. * @param Odr the output data rate value to be set
  390. * @retval 0 in case of success, an error code otherwise
  391. */
  392. int32_t LSM6DSO_ACC_SetOutputDataRate(LSM6DSO_Object_t *pObj, float_t Odr)
  393. {
  394. return LSM6DSO_ACC_SetOutputDataRate_With_Mode(pObj, Odr, LSM6DSO_ACC_HIGH_PERFORMANCE_MODE);
  395. }
  396. /**
  397. * @brief Set the LSM6DSO accelerometer sensor output data rate with operating mode
  398. * @param pObj the device pObj
  399. * @param Odr the output data rate value to be set
  400. * @param Mode the accelerometer operating mode
  401. * @note This function switches off the gyroscope if Ultra Low Power Mode is set
  402. * @retval 0 in case of success, an error code otherwise
  403. */
  404. int32_t LSM6DSO_ACC_SetOutputDataRate_With_Mode(LSM6DSO_Object_t *pObj, float_t Odr, LSM6DSO_ACC_Operating_Mode_t Mode)
  405. {
  406. int32_t ret = LSM6DSO_OK;
  407. float_t newOdr = Odr;
  408. switch (Mode)
  409. {
  410. case LSM6DSO_ACC_HIGH_PERFORMANCE_MODE:
  411. {
  412. /* We must uncheck Low Power and Ultra Low Power bits if they are enabled */
  413. lsm6dso_ctrl5_c_t val1;
  414. lsm6dso_ctrl6_c_t val2;
  415. if (lsm6dso_read_reg(&(pObj->Ctx), LSM6DSO_CTRL5_C, (uint8_t *)&val1, 1) != LSM6DSO_OK)
  416. {
  417. return LSM6DSO_ERROR;
  418. }
  419. if (val1.xl_ulp_en != 0U)
  420. {
  421. /* Power off the accelerometer */
  422. if (pObj->acc_is_enabled == 1U)
  423. {
  424. if (lsm6dso_xl_data_rate_set(&(pObj->Ctx), LSM6DSO_XL_ODR_OFF) != LSM6DSO_OK)
  425. {
  426. return LSM6DSO_ERROR;
  427. }
  428. }
  429. val1.xl_ulp_en = 0;
  430. if (lsm6dso_write_reg(&(pObj->Ctx), LSM6DSO_CTRL5_C, (uint8_t *)&val1, 1) != LSM6DSO_OK)
  431. {
  432. return LSM6DSO_ERROR;
  433. }
  434. }
  435. if (lsm6dso_read_reg(&(pObj->Ctx), LSM6DSO_CTRL6_C, (uint8_t *)&val2, 1) != LSM6DSO_OK)
  436. {
  437. return LSM6DSO_ERROR;
  438. }
  439. if (val2.xl_hm_mode != 0U)
  440. {
  441. val2.xl_hm_mode = 0U;
  442. if (lsm6dso_write_reg(&(pObj->Ctx), LSM6DSO_CTRL6_C, (uint8_t *)&val2, 1) != LSM6DSO_OK)
  443. {
  444. return LSM6DSO_ERROR;
  445. }
  446. }
  447. /* ODR should be at least 12.5Hz */
  448. if (newOdr < 12.5f)
  449. {
  450. newOdr = 12.5f;
  451. }
  452. break;
  453. }
  454. case LSM6DSO_ACC_LOW_POWER_NORMAL_MODE:
  455. {
  456. /* We must uncheck Ultra Low Power bit if it is enabled */
  457. /* and check the Low Power bit if it is unchecked */
  458. lsm6dso_ctrl5_c_t val1;
  459. lsm6dso_ctrl6_c_t val2;
  460. if (lsm6dso_read_reg(&(pObj->Ctx), LSM6DSO_CTRL5_C, (uint8_t *)&val1, 1) != LSM6DSO_OK)
  461. {
  462. return LSM6DSO_ERROR;
  463. }
  464. if (val1.xl_ulp_en != 0U)
  465. {
  466. /* Power off the accelerometer */
  467. if (pObj->acc_is_enabled == 1U)
  468. {
  469. if (lsm6dso_xl_data_rate_set(&(pObj->Ctx), LSM6DSO_XL_ODR_OFF) != LSM6DSO_OK)
  470. {
  471. return LSM6DSO_ERROR;
  472. }
  473. }
  474. val1.xl_ulp_en = 0;
  475. if (lsm6dso_write_reg(&(pObj->Ctx), LSM6DSO_CTRL5_C, (uint8_t *)&val1, 1) != LSM6DSO_OK)
  476. {
  477. return LSM6DSO_ERROR;
  478. }
  479. }
  480. if (lsm6dso_read_reg(&(pObj->Ctx), LSM6DSO_CTRL6_C, (uint8_t *)&val2, 1) != LSM6DSO_OK)
  481. {
  482. return LSM6DSO_ERROR;
  483. }
  484. if (val2.xl_hm_mode == 0U)
  485. {
  486. val2.xl_hm_mode = 1U;
  487. if (lsm6dso_write_reg(&(pObj->Ctx), LSM6DSO_CTRL6_C, (uint8_t *)&val2, 1) != LSM6DSO_OK)
  488. {
  489. return LSM6DSO_ERROR;
  490. }
  491. }
  492. /* Now we need to limit the ODR to 208 Hz if it is higher */
  493. if (newOdr > 208.0f)
  494. {
  495. newOdr = 208.0f;
  496. }
  497. break;
  498. }
  499. case LSM6DSO_ACC_ULTRA_LOW_POWER_MODE:
  500. {
  501. /* We must uncheck Low Power bit if it is enabled */
  502. /* and check the Ultra Low Power bit if it is unchecked */
  503. /* We must switch off gyro otherwise Ultra Low Power does not work */
  504. lsm6dso_ctrl5_c_t val1;
  505. lsm6dso_ctrl6_c_t val2;
  506. if (lsm6dso_read_reg(&(pObj->Ctx), LSM6DSO_CTRL6_C, (uint8_t *)&val2, 1) != LSM6DSO_OK)
  507. {
  508. return LSM6DSO_ERROR;
  509. }
  510. if (val2.xl_hm_mode != 0U)
  511. {
  512. val2.xl_hm_mode = 0U;
  513. if (lsm6dso_write_reg(&(pObj->Ctx), LSM6DSO_CTRL6_C, (uint8_t *)&val2, 1) != LSM6DSO_OK)
  514. {
  515. return LSM6DSO_ERROR;
  516. }
  517. }
  518. /* Disable Gyro */
  519. if (pObj->gyro_is_enabled == 1U)
  520. {
  521. if (LSM6DSO_GYRO_Disable(pObj) != LSM6DSO_OK)
  522. {
  523. return LSM6DSO_ERROR;
  524. }
  525. }
  526. if (lsm6dso_read_reg(&(pObj->Ctx), LSM6DSO_CTRL5_C, (uint8_t *)&val1, 1) != LSM6DSO_OK)
  527. {
  528. return LSM6DSO_ERROR;
  529. }
  530. if (val1.xl_ulp_en == 0U)
  531. {
  532. /* Power off the accelerometer */
  533. if (pObj->acc_is_enabled == 1U)
  534. {
  535. if (lsm6dso_xl_data_rate_set(&(pObj->Ctx), LSM6DSO_XL_ODR_OFF) != LSM6DSO_OK)
  536. {
  537. return LSM6DSO_ERROR;
  538. }
  539. }
  540. val1.xl_ulp_en = 1U;
  541. if (lsm6dso_write_reg(&(pObj->Ctx), LSM6DSO_CTRL5_C, (uint8_t *)&val1, 1) != LSM6DSO_OK)
  542. {
  543. return LSM6DSO_ERROR;
  544. }
  545. }
  546. /* Now we need to limit the ODR to 208 Hz if it is higher */
  547. if (newOdr > 208.0f)
  548. {
  549. newOdr = 208.0f;
  550. }
  551. break;
  552. }
  553. default:
  554. ret = LSM6DSO_ERROR;
  555. break;
  556. }
  557. if(ret == LSM6DSO_ERROR)
  558. {
  559. return LSM6DSO_ERROR;
  560. }
  561. if (pObj->acc_is_enabled == 1U)
  562. {
  563. ret = LSM6DSO_ACC_SetOutputDataRate_When_Enabled(pObj, newOdr);
  564. }
  565. else
  566. {
  567. ret = LSM6DSO_ACC_SetOutputDataRate_When_Disabled(pObj, newOdr);
  568. }
  569. return ret;
  570. }
  571. /**
  572. * @brief Get the LSM6DSO accelerometer sensor full scale
  573. * @param pObj the device pObj
  574. * @param FullScale pointer where the full scale is written
  575. * @retval 0 in case of success, an error code otherwise
  576. */
  577. int32_t LSM6DSO_ACC_GetFullScale(LSM6DSO_Object_t *pObj, int32_t *FullScale)
  578. {
  579. int32_t ret = LSM6DSO_OK;
  580. lsm6dso_fs_xl_t fs_low_level;
  581. /* Read actual full scale selection from sensor. */
  582. if (lsm6dso_xl_full_scale_get(&(pObj->Ctx), &fs_low_level) != LSM6DSO_OK)
  583. {
  584. return LSM6DSO_ERROR;
  585. }
  586. switch (fs_low_level)
  587. {
  588. case LSM6DSO_2g:
  589. *FullScale = 2;
  590. break;
  591. case LSM6DSO_4g:
  592. *FullScale = 4;
  593. break;
  594. case LSM6DSO_8g:
  595. *FullScale = 8;
  596. break;
  597. case LSM6DSO_16g:
  598. *FullScale = 16;
  599. break;
  600. default:
  601. ret = LSM6DSO_ERROR;
  602. break;
  603. }
  604. return ret;
  605. }
  606. /**
  607. * @brief Set the LSM6DSO accelerometer sensor full scale
  608. * @param pObj the device pObj
  609. * @param FullScale the functional full scale to be set
  610. * @retval 0 in case of success, an error code otherwise
  611. */
  612. int32_t LSM6DSO_ACC_SetFullScale(LSM6DSO_Object_t *pObj, int32_t FullScale)
  613. {
  614. lsm6dso_fs_xl_t new_fs;
  615. /* Seems like MISRA C-2012 rule 14.3a violation but only from single file statical analysis point of view because
  616. the parameter passed to the function is not known at the moment of analysis */
  617. new_fs = (FullScale <= 2) ? LSM6DSO_2g
  618. : (FullScale <= 4) ? LSM6DSO_4g
  619. : (FullScale <= 8) ? LSM6DSO_8g
  620. : LSM6DSO_16g;
  621. if (lsm6dso_xl_full_scale_set(&(pObj->Ctx), new_fs) != LSM6DSO_OK)
  622. {
  623. return LSM6DSO_ERROR;
  624. }
  625. return LSM6DSO_OK;
  626. }
  627. /**
  628. * @brief Get the LSM6DSO accelerometer sensor raw axes
  629. * @param pObj the device pObj
  630. * @param Value pointer where the raw values of the axes are written
  631. * @retval 0 in case of success, an error code otherwise
  632. */
  633. int32_t LSM6DSO_ACC_GetAxesRaw(LSM6DSO_Object_t *pObj, LSM6DSO_AxesRaw_t *Value)
  634. {
  635. lsm6dso_axis3bit16_t data_raw;
  636. /* Read raw data values. */
  637. if (lsm6dso_acceleration_raw_get(&(pObj->Ctx), data_raw.u8bit) != LSM6DSO_OK)
  638. {
  639. return LSM6DSO_ERROR;
  640. }
  641. /* Format the data. */
  642. Value->x = data_raw.i16bit[0];
  643. Value->y = data_raw.i16bit[1];
  644. Value->z = data_raw.i16bit[2];
  645. return LSM6DSO_OK;
  646. }
  647. /**
  648. * @brief Get the LSM6DSO accelerometer sensor axes
  649. * @param pObj the device pObj
  650. * @param Acceleration pointer where the values of the axes are written
  651. * @retval 0 in case of success, an error code otherwise
  652. */
  653. int32_t LSM6DSO_ACC_GetAxes(LSM6DSO_Object_t *pObj, LSM6DSO_Axes_t *Acceleration)
  654. {
  655. lsm6dso_axis3bit16_t data_raw;
  656. float_t sensitivity = 0.0f;
  657. /* Read raw data values. */
  658. if (lsm6dso_acceleration_raw_get(&(pObj->Ctx), data_raw.u8bit) != LSM6DSO_OK)
  659. {
  660. return LSM6DSO_ERROR;
  661. }
  662. /* Get LSM6DSO actual sensitivity. */
  663. if (LSM6DSO_ACC_GetSensitivity(pObj, &sensitivity) != LSM6DSO_OK)
  664. {
  665. return LSM6DSO_ERROR;
  666. }
  667. /* Calculate the data. */
  668. Acceleration->x = (int32_t)((float_t)((float_t)data_raw.i16bit[0] * sensitivity));
  669. Acceleration->y = (int32_t)((float_t)((float_t)data_raw.i16bit[1] * sensitivity));
  670. Acceleration->z = (int32_t)((float_t)((float_t)data_raw.i16bit[2] * sensitivity));
  671. return LSM6DSO_OK;
  672. }
  673. /**
  674. * @brief Enable the LSM6DSO gyroscope sensor
  675. * @param pObj the device pObj
  676. * @retval 0 in case of success, an error code otherwise
  677. */
  678. int32_t LSM6DSO_GYRO_Enable(LSM6DSO_Object_t *pObj)
  679. {
  680. /* Check if the component is already enabled */
  681. if (pObj->gyro_is_enabled == 1U)
  682. {
  683. return LSM6DSO_OK;
  684. }
  685. /* Output data rate selection. */
  686. if (lsm6dso_gy_data_rate_set(&(pObj->Ctx), pObj->gyro_odr) != LSM6DSO_OK)
  687. {
  688. return LSM6DSO_ERROR;
  689. }
  690. pObj->gyro_is_enabled = 1;
  691. return LSM6DSO_OK;
  692. }
  693. /**
  694. * @brief Disable the LSM6DSO gyroscope sensor
  695. * @param pObj the device pObj
  696. * @retval 0 in case of success, an error code otherwise
  697. */
  698. int32_t LSM6DSO_GYRO_Disable(LSM6DSO_Object_t *pObj)
  699. {
  700. /* Check if the component is already disabled */
  701. if (pObj->gyro_is_enabled == 0U)
  702. {
  703. return LSM6DSO_OK;
  704. }
  705. /* Get current output data rate. */
  706. if (lsm6dso_gy_data_rate_get(&(pObj->Ctx), &pObj->gyro_odr) != LSM6DSO_OK)
  707. {
  708. return LSM6DSO_ERROR;
  709. }
  710. /* Output data rate selection - power down. */
  711. if (lsm6dso_gy_data_rate_set(&(pObj->Ctx), LSM6DSO_GY_ODR_OFF) != LSM6DSO_OK)
  712. {
  713. return LSM6DSO_ERROR;
  714. }
  715. pObj->gyro_is_enabled = 0;
  716. return LSM6DSO_OK;
  717. }
  718. /**
  719. * @brief Get the LSM6DSO gyroscope sensor sensitivity
  720. * @param pObj the device pObj
  721. * @param Sensitivity pointer
  722. * @retval 0 in case of success, an error code otherwise
  723. */
  724. int32_t LSM6DSO_GYRO_GetSensitivity(LSM6DSO_Object_t *pObj, float_t *Sensitivity)
  725. {
  726. int32_t ret = LSM6DSO_OK;
  727. lsm6dso_fs_g_t full_scale;
  728. /* Read actual full scale selection from sensor. */
  729. if (lsm6dso_gy_full_scale_get(&(pObj->Ctx), &full_scale) != LSM6DSO_OK)
  730. {
  731. return LSM6DSO_ERROR;
  732. }
  733. /* Store the sensitivity based on actual full scale. */
  734. switch (full_scale)
  735. {
  736. case LSM6DSO_125dps:
  737. *Sensitivity = LSM6DSO_GYRO_SENSITIVITY_FS_125DPS;
  738. break;
  739. case LSM6DSO_250dps:
  740. *Sensitivity = LSM6DSO_GYRO_SENSITIVITY_FS_250DPS;
  741. break;
  742. case LSM6DSO_500dps:
  743. *Sensitivity = LSM6DSO_GYRO_SENSITIVITY_FS_500DPS;
  744. break;
  745. case LSM6DSO_1000dps:
  746. *Sensitivity = LSM6DSO_GYRO_SENSITIVITY_FS_1000DPS;
  747. break;
  748. case LSM6DSO_2000dps:
  749. *Sensitivity = LSM6DSO_GYRO_SENSITIVITY_FS_2000DPS;
  750. break;
  751. default:
  752. ret = LSM6DSO_ERROR;
  753. break;
  754. }
  755. return ret;
  756. }
  757. /**
  758. * @brief Get the LSM6DSO gyroscope sensor output data rate
  759. * @param pObj the device pObj
  760. * @param Odr pointer where the output data rate is written
  761. * @retval 0 in case of success, an error code otherwise
  762. */
  763. int32_t LSM6DSO_GYRO_GetOutputDataRate(LSM6DSO_Object_t *pObj, float_t *Odr)
  764. {
  765. int32_t ret = LSM6DSO_OK;
  766. lsm6dso_odr_g_t odr_low_level;
  767. /* Get current output data rate. */
  768. if (lsm6dso_gy_data_rate_get(&(pObj->Ctx), &odr_low_level) != LSM6DSO_OK)
  769. {
  770. return LSM6DSO_ERROR;
  771. }
  772. switch (odr_low_level)
  773. {
  774. case LSM6DSO_GY_ODR_OFF:
  775. *Odr = 0.0f;
  776. break;
  777. case LSM6DSO_GY_ODR_12Hz5:
  778. *Odr = 12.5f;
  779. break;
  780. case LSM6DSO_GY_ODR_26Hz:
  781. *Odr = 26.0f;
  782. break;
  783. case LSM6DSO_GY_ODR_52Hz:
  784. *Odr = 52.0f;
  785. break;
  786. case LSM6DSO_GY_ODR_104Hz:
  787. *Odr = 104.0f;
  788. break;
  789. case LSM6DSO_GY_ODR_208Hz:
  790. *Odr = 208.0f;
  791. break;
  792. case LSM6DSO_GY_ODR_417Hz:
  793. *Odr = 417.0f;
  794. break;
  795. case LSM6DSO_GY_ODR_833Hz:
  796. *Odr = 833.0f;
  797. break;
  798. case LSM6DSO_GY_ODR_1667Hz:
  799. *Odr = 1667.0f;
  800. break;
  801. case LSM6DSO_GY_ODR_3333Hz:
  802. *Odr = 3333.0f;
  803. break;
  804. case LSM6DSO_GY_ODR_6667Hz:
  805. *Odr = 6667.0f;
  806. break;
  807. default:
  808. ret = LSM6DSO_ERROR;
  809. break;
  810. }
  811. return ret;
  812. }
  813. /**
  814. * @brief Set the LSM6DSO gyroscope sensor output data rate
  815. * @param pObj the device pObj
  816. * @param Odr the output data rate value to be set
  817. * @retval 0 in case of success, an error code otherwise
  818. */
  819. int32_t LSM6DSO_GYRO_SetOutputDataRate(LSM6DSO_Object_t *pObj, float_t Odr)
  820. {
  821. return LSM6DSO_GYRO_SetOutputDataRate_With_Mode(pObj, Odr, LSM6DSO_GYRO_HIGH_PERFORMANCE_MODE);
  822. }
  823. /**
  824. * @brief Set the LSM6DSO gyroscope sensor output data rate with operating mode
  825. * @param pObj the device pObj
  826. * @param Odr the output data rate value to be set
  827. * @param Mode the gyroscope operating mode
  828. * @retval 0 in case of success, an error code otherwise
  829. */
  830. int32_t LSM6DSO_GYRO_SetOutputDataRate_With_Mode(LSM6DSO_Object_t *pObj, float_t Odr, LSM6DSO_GYRO_Operating_Mode_t Mode)
  831. {
  832. int32_t ret = LSM6DSO_OK;
  833. float_t newOdr = Odr;
  834. switch (Mode)
  835. {
  836. case LSM6DSO_GYRO_HIGH_PERFORMANCE_MODE:
  837. {
  838. /* We must uncheck Low Power bit if it is enabled */
  839. lsm6dso_ctrl7_g_t val1;
  840. if (lsm6dso_read_reg(&(pObj->Ctx), LSM6DSO_CTRL7_G, (uint8_t *)&val1, 1) != LSM6DSO_OK)
  841. {
  842. return LSM6DSO_ERROR;
  843. }
  844. if (val1.g_hm_mode != 0U)
  845. {
  846. val1.g_hm_mode = 0U;
  847. if (lsm6dso_write_reg(&(pObj->Ctx), LSM6DSO_CTRL7_G, (uint8_t *)&val1, 1) != LSM6DSO_OK)
  848. {
  849. return LSM6DSO_ERROR;
  850. }
  851. }
  852. break;
  853. }
  854. case LSM6DSO_GYRO_LOW_POWER_NORMAL_MODE:
  855. {
  856. /* We must check the Low Power bit if it is unchecked */
  857. lsm6dso_ctrl7_g_t val1;
  858. if (lsm6dso_read_reg(&(pObj->Ctx), LSM6DSO_CTRL7_G, (uint8_t *)&val1, 1) != LSM6DSO_OK)
  859. {
  860. return LSM6DSO_ERROR;
  861. }
  862. if (val1.g_hm_mode == 0U)
  863. {
  864. val1.g_hm_mode = 1U;
  865. if (lsm6dso_write_reg(&(pObj->Ctx), LSM6DSO_CTRL7_G, (uint8_t *)&val1, 1) != LSM6DSO_OK)
  866. {
  867. return LSM6DSO_ERROR;
  868. }
  869. }
  870. /* Now we need to limit the ODR to 208 Hz if it is higher */
  871. if (newOdr > 208.0f)
  872. {
  873. newOdr = 208.0f;
  874. }
  875. break;
  876. }
  877. default:
  878. ret = LSM6DSO_ERROR;
  879. break;
  880. }
  881. if (ret == LSM6DSO_ERROR)
  882. {
  883. return LSM6DSO_ERROR;
  884. }
  885. if (pObj->gyro_is_enabled == 1U)
  886. {
  887. ret = LSM6DSO_GYRO_SetOutputDataRate_When_Enabled(pObj, newOdr);
  888. }
  889. else
  890. {
  891. ret = LSM6DSO_GYRO_SetOutputDataRate_When_Disabled(pObj, newOdr);
  892. }
  893. return ret;
  894. }
  895. /**
  896. * @brief Get the LSM6DSO gyroscope sensor full scale
  897. * @param pObj the device pObj
  898. * @param FullScale pointer where the full scale is written
  899. * @retval 0 in case of success, an error code otherwise
  900. */
  901. int32_t LSM6DSO_GYRO_GetFullScale(LSM6DSO_Object_t *pObj, int32_t *FullScale)
  902. {
  903. int32_t ret = LSM6DSO_OK;
  904. lsm6dso_fs_g_t fs_low_level;
  905. /* Read actual full scale selection from sensor. */
  906. if (lsm6dso_gy_full_scale_get(&(pObj->Ctx), &fs_low_level) != LSM6DSO_OK)
  907. {
  908. return LSM6DSO_ERROR;
  909. }
  910. switch (fs_low_level)
  911. {
  912. case LSM6DSO_125dps:
  913. *FullScale = 125;
  914. break;
  915. case LSM6DSO_250dps:
  916. *FullScale = 250;
  917. break;
  918. case LSM6DSO_500dps:
  919. *FullScale = 500;
  920. break;
  921. case LSM6DSO_1000dps:
  922. *FullScale = 1000;
  923. break;
  924. case LSM6DSO_2000dps:
  925. *FullScale = 2000;
  926. break;
  927. default:
  928. ret = LSM6DSO_ERROR;
  929. break;
  930. }
  931. return ret;
  932. }
  933. /**
  934. * @brief Set the LSM6DSO gyroscope sensor full scale
  935. * @param pObj the device pObj
  936. * @param FullScale the functional full scale to be set
  937. * @retval 0 in case of success, an error code otherwise
  938. */
  939. int32_t LSM6DSO_GYRO_SetFullScale(LSM6DSO_Object_t *pObj, int32_t FullScale)
  940. {
  941. lsm6dso_fs_g_t new_fs;
  942. new_fs = (FullScale <= 125) ? LSM6DSO_125dps
  943. : (FullScale <= 250) ? LSM6DSO_250dps
  944. : (FullScale <= 500) ? LSM6DSO_500dps
  945. : (FullScale <= 1000) ? LSM6DSO_1000dps
  946. : LSM6DSO_2000dps;
  947. if (lsm6dso_gy_full_scale_set(&(pObj->Ctx), new_fs) != LSM6DSO_OK)
  948. {
  949. return LSM6DSO_ERROR;
  950. }
  951. return LSM6DSO_OK;
  952. }
  953. /**
  954. * @brief Get the LSM6DSO gyroscope sensor raw axes
  955. * @param pObj the device pObj
  956. * @param Value pointer where the raw values of the axes are written
  957. * @retval 0 in case of success, an error code otherwise
  958. */
  959. int32_t LSM6DSO_GYRO_GetAxesRaw(LSM6DSO_Object_t *pObj, LSM6DSO_AxesRaw_t *Value)
  960. {
  961. lsm6dso_axis3bit16_t data_raw;
  962. /* Read raw data values. */
  963. if (lsm6dso_angular_rate_raw_get(&(pObj->Ctx), data_raw.u8bit) != LSM6DSO_OK)
  964. {
  965. return LSM6DSO_ERROR;
  966. }
  967. /* Format the data. */
  968. Value->x = data_raw.i16bit[0];
  969. Value->y = data_raw.i16bit[1];
  970. Value->z = data_raw.i16bit[2];
  971. return LSM6DSO_OK;
  972. }
  973. /**
  974. * @brief Get the LSM6DSO gyroscope sensor axes
  975. * @param pObj the device pObj
  976. * @param AngularRate pointer where the values of the axes are written
  977. * @retval 0 in case of success, an error code otherwise
  978. */
  979. int32_t LSM6DSO_GYRO_GetAxes(LSM6DSO_Object_t *pObj, LSM6DSO_Axes_t *AngularRate)
  980. {
  981. lsm6dso_axis3bit16_t data_raw;
  982. float_t sensitivity;
  983. /* Read raw data values. */
  984. if (lsm6dso_angular_rate_raw_get(&(pObj->Ctx), data_raw.u8bit) != LSM6DSO_OK)
  985. {
  986. return LSM6DSO_ERROR;
  987. }
  988. /* Get LSM6DSO actual sensitivity. */
  989. if (LSM6DSO_GYRO_GetSensitivity(pObj, &sensitivity) != LSM6DSO_OK)
  990. {
  991. return LSM6DSO_ERROR;
  992. }
  993. /* Calculate the data. */
  994. AngularRate->x = (int32_t)((float_t)((float_t)data_raw.i16bit[0] * sensitivity));
  995. AngularRate->y = (int32_t)((float_t)((float_t)data_raw.i16bit[1] * sensitivity));
  996. AngularRate->z = (int32_t)((float_t)((float_t)data_raw.i16bit[2] * sensitivity));
  997. return LSM6DSO_OK;
  998. }
  999. /**
  1000. * @brief Get the LSM6DSO register value
  1001. * @param pObj the device pObj
  1002. * @param Reg address to be read
  1003. * @param Data pointer where the value is written
  1004. * @retval 0 in case of success, an error code otherwise
  1005. */
  1006. int32_t LSM6DSO_Read_Reg(LSM6DSO_Object_t *pObj, uint8_t Reg, uint8_t *Data)
  1007. {
  1008. if (lsm6dso_read_reg(&(pObj->Ctx), Reg, Data, 1) != LSM6DSO_OK)
  1009. {
  1010. return LSM6DSO_ERROR;
  1011. }
  1012. return LSM6DSO_OK;
  1013. }
  1014. /**
  1015. * @brief Set the LSM6DSO register value
  1016. * @param pObj the device pObj
  1017. * @param Reg address to be written
  1018. * @param Data value to be written
  1019. * @retval 0 in case of success, an error code otherwise
  1020. */
  1021. int32_t LSM6DSO_Write_Reg(LSM6DSO_Object_t *pObj, uint8_t Reg, uint8_t Data)
  1022. {
  1023. if (lsm6dso_write_reg(&(pObj->Ctx), Reg, &Data, 1) != LSM6DSO_OK)
  1024. {
  1025. return LSM6DSO_ERROR;
  1026. }
  1027. return LSM6DSO_OK;
  1028. }
  1029. /**
  1030. * @brief Set the interrupt latch
  1031. * @param pObj the device pObj
  1032. * @param Status value to be written
  1033. * @retval 0 in case of success, an error code otherwise
  1034. */
  1035. int32_t LSM6DSO_Set_Interrupt_Latch(LSM6DSO_Object_t *pObj, uint8_t Status)
  1036. {
  1037. int32_t ret = LSM6DSO_OK;
  1038. lsm6dso_lir_t newStatus = LSM6DSO_ALL_INT_PULSED;
  1039. switch (Status)
  1040. {
  1041. case 0:
  1042. newStatus = LSM6DSO_ALL_INT_PULSED;
  1043. break;
  1044. case 1:
  1045. newStatus = LSM6DSO_BASE_LATCHED_EMB_PULSED;
  1046. break;
  1047. case 2:
  1048. newStatus = LSM6DSO_BASE_PULSED_EMB_LATCHED;
  1049. break;
  1050. case 3:
  1051. newStatus = LSM6DSO_ALL_INT_LATCHED;
  1052. break;
  1053. default:
  1054. ret = LSM6DSO_ERROR;
  1055. break;
  1056. }
  1057. if (ret == LSM6DSO_ERROR)
  1058. {
  1059. return LSM6DSO_ERROR;
  1060. }
  1061. if (Status > 1U)
  1062. {
  1063. return LSM6DSO_ERROR;
  1064. }
  1065. if (lsm6dso_int_notification_set(&(pObj->Ctx), newStatus) != LSM6DSO_OK)
  1066. {
  1067. return LSM6DSO_ERROR;
  1068. }
  1069. return LSM6DSO_OK;
  1070. }
  1071. /**
  1072. * @brief Enable free fall detection
  1073. * @param pObj the device pObj
  1074. * @param IntPin interrupt pin line to be used
  1075. * @retval 0 in case of success, an error code otherwise
  1076. */
  1077. int32_t LSM6DSO_ACC_Enable_Free_Fall_Detection(LSM6DSO_Object_t *pObj, LSM6DSO_SensorIntPin_t IntPin)
  1078. {
  1079. int32_t ret = LSM6DSO_OK;
  1080. lsm6dso_pin_int1_route_t val1;
  1081. lsm6dso_pin_int2_route_t val2;
  1082. /* Output Data Rate selection */
  1083. if (LSM6DSO_ACC_SetOutputDataRate(pObj, 417.0f) != LSM6DSO_OK)
  1084. {
  1085. return LSM6DSO_ERROR;
  1086. }
  1087. /* Full scale selection */
  1088. if (LSM6DSO_ACC_SetFullScale(pObj, 2) != LSM6DSO_OK)
  1089. {
  1090. return LSM6DSO_ERROR;
  1091. }
  1092. /* FF_DUR setting */
  1093. if (lsm6dso_ff_dur_set(&(pObj->Ctx), 0x06) != LSM6DSO_OK)
  1094. {
  1095. return LSM6DSO_ERROR;
  1096. }
  1097. /* WAKE_DUR setting */
  1098. if (lsm6dso_wkup_dur_set(&(pObj->Ctx), 0x00) != LSM6DSO_OK)
  1099. {
  1100. return LSM6DSO_ERROR;
  1101. }
  1102. /* SLEEP_DUR setting */
  1103. if (lsm6dso_act_sleep_dur_set(&(pObj->Ctx), 0x00) != LSM6DSO_OK)
  1104. {
  1105. return LSM6DSO_ERROR;
  1106. }
  1107. /* FF_THS setting */
  1108. if (lsm6dso_ff_threshold_set(&(pObj->Ctx), LSM6DSO_FF_TSH_312mg) != LSM6DSO_OK)
  1109. {
  1110. return LSM6DSO_ERROR;
  1111. }
  1112. /* Enable free fall event on either INT1 or INT2 pin */
  1113. switch (IntPin)
  1114. {
  1115. case LSM6DSO_INT1_PIN:
  1116. if (lsm6dso_pin_int1_route_get(&(pObj->Ctx), &val1) != LSM6DSO_OK)
  1117. {
  1118. return LSM6DSO_ERROR;
  1119. }
  1120. val1.md1_cfg.int1_ff = PROPERTY_ENABLE;
  1121. if (lsm6dso_pin_int1_route_set(&(pObj->Ctx), &val1) != LSM6DSO_OK)
  1122. {
  1123. return LSM6DSO_ERROR;
  1124. }
  1125. break;
  1126. case LSM6DSO_INT2_PIN:
  1127. if (lsm6dso_pin_int2_route_get(&(pObj->Ctx), &val2) != LSM6DSO_OK)
  1128. {
  1129. return LSM6DSO_ERROR;
  1130. }
  1131. val2.md2_cfg.int2_ff = PROPERTY_ENABLE;
  1132. if (lsm6dso_pin_int2_route_set(&(pObj->Ctx), &val2) != LSM6DSO_OK)
  1133. {
  1134. return LSM6DSO_ERROR;
  1135. }
  1136. break;
  1137. default:
  1138. ret = LSM6DSO_ERROR;
  1139. break;
  1140. }
  1141. return ret;
  1142. }
  1143. /**
  1144. * @brief Disable free fall detection
  1145. * @param pObj the device pObj
  1146. * @retval 0 in case of success, an error code otherwise
  1147. */
  1148. int32_t LSM6DSO_ACC_Disable_Free_Fall_Detection(LSM6DSO_Object_t *pObj)
  1149. {
  1150. lsm6dso_pin_int1_route_t val1;
  1151. lsm6dso_pin_int2_route_t val2;
  1152. /* Disable free fall event on both INT1 and INT2 pins */
  1153. if (lsm6dso_pin_int1_route_get(&(pObj->Ctx), &val1) != LSM6DSO_OK)
  1154. {
  1155. return LSM6DSO_ERROR;
  1156. }
  1157. val1.md1_cfg.int1_ff = PROPERTY_DISABLE;
  1158. if (lsm6dso_pin_int1_route_set(&(pObj->Ctx), &val1) != LSM6DSO_OK)
  1159. {
  1160. return LSM6DSO_ERROR;
  1161. }
  1162. if (lsm6dso_pin_int2_route_get(&(pObj->Ctx), &val2) != LSM6DSO_OK)
  1163. {
  1164. return LSM6DSO_ERROR;
  1165. }
  1166. val2.md2_cfg.int2_ff = PROPERTY_DISABLE;
  1167. if (lsm6dso_pin_int2_route_set(&(pObj->Ctx), &val2) != LSM6DSO_OK)
  1168. {
  1169. return LSM6DSO_ERROR;
  1170. }
  1171. /* FF_DUR setting */
  1172. if (lsm6dso_ff_dur_set(&(pObj->Ctx), 0x00) != LSM6DSO_OK)
  1173. {
  1174. return LSM6DSO_ERROR;
  1175. }
  1176. /* FF_THS setting */
  1177. if (lsm6dso_ff_threshold_set(&(pObj->Ctx), LSM6DSO_FF_TSH_156mg) != LSM6DSO_OK)
  1178. {
  1179. return LSM6DSO_ERROR;
  1180. }
  1181. return LSM6DSO_OK;
  1182. }
  1183. /**
  1184. * @brief Set free fall threshold
  1185. * @param pObj the device pObj
  1186. * @param Threshold free fall detection threshold
  1187. * @retval 0 in case of success, an error code otherwise
  1188. */
  1189. int32_t LSM6DSO_ACC_Set_Free_Fall_Threshold(LSM6DSO_Object_t *pObj, uint8_t Threshold)
  1190. {
  1191. int32_t ret = LSM6DSO_OK;
  1192. lsm6dso_ff_ths_t newThreshold = LSM6DSO_FF_TSH_156mg;
  1193. switch (Threshold)
  1194. {
  1195. case 0:
  1196. newThreshold = LSM6DSO_FF_TSH_156mg;
  1197. break;
  1198. case 1:
  1199. newThreshold = LSM6DSO_FF_TSH_219mg;
  1200. break;
  1201. case 2:
  1202. newThreshold = LSM6DSO_FF_TSH_250mg;
  1203. break;
  1204. case 3:
  1205. newThreshold = LSM6DSO_FF_TSH_312mg;
  1206. break;
  1207. case 4:
  1208. newThreshold = LSM6DSO_FF_TSH_344mg;
  1209. break;
  1210. case 5:
  1211. newThreshold = LSM6DSO_FF_TSH_406mg;
  1212. break;
  1213. case 6:
  1214. newThreshold = LSM6DSO_FF_TSH_469mg;
  1215. break;
  1216. case 7:
  1217. newThreshold = LSM6DSO_FF_TSH_500mg;
  1218. break;
  1219. default:
  1220. ret = LSM6DSO_ERROR;
  1221. break;
  1222. }
  1223. if (ret == LSM6DSO_ERROR)
  1224. {
  1225. return LSM6DSO_ERROR;
  1226. }
  1227. if (lsm6dso_ff_threshold_set(&(pObj->Ctx), newThreshold) != LSM6DSO_OK)
  1228. {
  1229. return LSM6DSO_ERROR;
  1230. }
  1231. return ret;
  1232. }
  1233. /**
  1234. * @brief Set free fall duration
  1235. * @param pObj the device pObj
  1236. * @param Duration free fall detection duration
  1237. * @retval 0 in case of success, an error code otherwise
  1238. */
  1239. int32_t LSM6DSO_ACC_Set_Free_Fall_Duration(LSM6DSO_Object_t *pObj, uint8_t Duration)
  1240. {
  1241. if (lsm6dso_ff_dur_set(&(pObj->Ctx), Duration) != LSM6DSO_OK)
  1242. {
  1243. return LSM6DSO_ERROR;
  1244. }
  1245. return LSM6DSO_OK;
  1246. }
  1247. /**
  1248. * @brief Enable pedometer
  1249. * @param pObj the device pObj
  1250. * @retval 0 in case of success, an error code otherwise
  1251. */
  1252. int32_t LSM6DSO_ACC_Enable_Pedometer(LSM6DSO_Object_t *pObj)
  1253. {
  1254. lsm6dso_pin_int1_route_t val;
  1255. /* Output Data Rate selection */
  1256. if (LSM6DSO_ACC_SetOutputDataRate(pObj, 26.0f) != LSM6DSO_OK)
  1257. {
  1258. return LSM6DSO_ERROR;
  1259. }
  1260. /* Full scale selection */
  1261. if (LSM6DSO_ACC_SetFullScale(pObj, 2) != LSM6DSO_OK)
  1262. {
  1263. return LSM6DSO_ERROR;
  1264. }
  1265. /* Enable pedometer algorithm. */
  1266. if (lsm6dso_pedo_sens_set(&(pObj->Ctx), LSM6DSO_PEDO_BASE_MODE) != LSM6DSO_OK)
  1267. {
  1268. return LSM6DSO_ERROR;
  1269. }
  1270. /* Enable step detector on INT1 pin */
  1271. if (lsm6dso_pin_int1_route_get(&(pObj->Ctx), &val) != LSM6DSO_OK)
  1272. {
  1273. return LSM6DSO_ERROR;
  1274. }
  1275. val.emb_func_int1.int1_step_detector = PROPERTY_ENABLE;
  1276. if (lsm6dso_pin_int1_route_set(&(pObj->Ctx), &val) != LSM6DSO_OK)
  1277. {
  1278. return LSM6DSO_ERROR;
  1279. }
  1280. return LSM6DSO_OK;
  1281. }
  1282. /**
  1283. * @brief Disable pedometer
  1284. * @param pObj the device pObj
  1285. * @retval 0 in case of success, an error code otherwise
  1286. */
  1287. int32_t LSM6DSO_ACC_Disable_Pedometer(LSM6DSO_Object_t *pObj)
  1288. {
  1289. lsm6dso_pin_int1_route_t val1;
  1290. /* Disable step detector on INT1 pin */
  1291. if (lsm6dso_pin_int1_route_get(&(pObj->Ctx), &val1) != LSM6DSO_OK)
  1292. {
  1293. return LSM6DSO_ERROR;
  1294. }
  1295. val1.emb_func_int1.int1_step_detector = PROPERTY_DISABLE;
  1296. if (lsm6dso_pin_int1_route_set(&(pObj->Ctx), &val1) != LSM6DSO_OK)
  1297. {
  1298. return LSM6DSO_ERROR;
  1299. }
  1300. /* Disable pedometer algorithm. */
  1301. if (lsm6dso_pedo_sens_set(&(pObj->Ctx), LSM6DSO_PEDO_DISABLE) != LSM6DSO_OK)
  1302. {
  1303. return LSM6DSO_ERROR;
  1304. }
  1305. return LSM6DSO_OK;
  1306. }
  1307. /**
  1308. * @brief Get step count
  1309. * @param pObj the device pObj
  1310. * @param StepCount step counter
  1311. * @retval 0 in case of success, an error code otherwise
  1312. */
  1313. int32_t LSM6DSO_ACC_Get_Step_Count(LSM6DSO_Object_t *pObj, uint16_t *StepCount)
  1314. {
  1315. if (lsm6dso_number_of_steps_get(&(pObj->Ctx), (uint8_t *)StepCount) != LSM6DSO_OK)
  1316. {
  1317. return LSM6DSO_ERROR;
  1318. }
  1319. return LSM6DSO_OK;
  1320. }
  1321. /**
  1322. * @brief Enable step counter reset
  1323. * @param pObj the device pObj
  1324. * @retval 0 in case of success, an error code otherwise
  1325. */
  1326. int32_t LSM6DSO_ACC_Step_Counter_Reset(LSM6DSO_Object_t *pObj)
  1327. {
  1328. if (lsm6dso_steps_reset(&(pObj->Ctx)) != LSM6DSO_OK)
  1329. {
  1330. return LSM6DSO_ERROR;
  1331. }
  1332. return LSM6DSO_OK;
  1333. }
  1334. /**
  1335. * @brief Enable tilt detection
  1336. * @param pObj the device pObj
  1337. * @param IntPin interrupt pin line to be used
  1338. * @retval 0 in case of success, an error code otherwise
  1339. */
  1340. int32_t LSM6DSO_ACC_Enable_Tilt_Detection(LSM6DSO_Object_t *pObj, LSM6DSO_SensorIntPin_t IntPin)
  1341. {
  1342. int32_t ret = LSM6DSO_OK;
  1343. lsm6dso_pin_int1_route_t val1;
  1344. lsm6dso_pin_int2_route_t val2;
  1345. /* Output Data Rate selection */
  1346. if (LSM6DSO_ACC_SetOutputDataRate(pObj, 26.0f) != LSM6DSO_OK)
  1347. {
  1348. return LSM6DSO_ERROR;
  1349. }
  1350. /* Full scale selection */
  1351. if (LSM6DSO_ACC_SetFullScale(pObj, 2) != LSM6DSO_OK)
  1352. {
  1353. return LSM6DSO_ERROR;
  1354. }
  1355. /* Enable tilt calculation. */
  1356. if (lsm6dso_tilt_sens_set(&(pObj->Ctx), PROPERTY_ENABLE) != LSM6DSO_OK)
  1357. {
  1358. return LSM6DSO_ERROR;
  1359. }
  1360. /* Enable tilt event on either INT1 or INT2 pin */
  1361. switch (IntPin)
  1362. {
  1363. case LSM6DSO_INT1_PIN:
  1364. if (lsm6dso_pin_int1_route_get(&(pObj->Ctx), &val1) != LSM6DSO_OK)
  1365. {
  1366. return LSM6DSO_ERROR;
  1367. }
  1368. val1.emb_func_int1.int1_tilt = PROPERTY_ENABLE;
  1369. if (lsm6dso_pin_int1_route_set(&(pObj->Ctx), &val1) != LSM6DSO_OK)
  1370. {
  1371. return LSM6DSO_ERROR;
  1372. }
  1373. break;
  1374. case LSM6DSO_INT2_PIN:
  1375. if (lsm6dso_pin_int2_route_get(&(pObj->Ctx), &val2) != LSM6DSO_OK)
  1376. {
  1377. return LSM6DSO_ERROR;
  1378. }
  1379. val2.emb_func_int2.int2_tilt = PROPERTY_ENABLE;
  1380. if (lsm6dso_pin_int2_route_set(&(pObj->Ctx), &val2) != LSM6DSO_OK)
  1381. {
  1382. return LSM6DSO_ERROR;
  1383. }
  1384. break;
  1385. default:
  1386. ret = LSM6DSO_ERROR;
  1387. break;
  1388. }
  1389. return ret;
  1390. }
  1391. /**
  1392. * @brief Disable tilt detection
  1393. * @param pObj the device pObj
  1394. * @retval 0 in case of success, an error code otherwise
  1395. */
  1396. int32_t LSM6DSO_ACC_Disable_Tilt_Detection(LSM6DSO_Object_t *pObj)
  1397. {
  1398. lsm6dso_pin_int1_route_t val1;
  1399. lsm6dso_pin_int2_route_t val2;
  1400. /* Disable tilt event on both INT1 and INT2 pins */
  1401. if (lsm6dso_pin_int1_route_get(&(pObj->Ctx), &val1) != LSM6DSO_OK)
  1402. {
  1403. return LSM6DSO_ERROR;
  1404. }
  1405. val1.emb_func_int1.int1_tilt = PROPERTY_DISABLE;
  1406. if (lsm6dso_pin_int1_route_set(&(pObj->Ctx), &val1) != LSM6DSO_OK)
  1407. {
  1408. return LSM6DSO_ERROR;
  1409. }
  1410. if (lsm6dso_pin_int2_route_get(&(pObj->Ctx), &val2) != LSM6DSO_OK)
  1411. {
  1412. return LSM6DSO_ERROR;
  1413. }
  1414. val2.emb_func_int2.int2_tilt = PROPERTY_DISABLE;
  1415. if (lsm6dso_pin_int2_route_set(&(pObj->Ctx), &val2) != LSM6DSO_OK)
  1416. {
  1417. return LSM6DSO_ERROR;
  1418. }
  1419. /* Disable tilt calculation. */
  1420. if (lsm6dso_tilt_sens_set(&(pObj->Ctx), PROPERTY_DISABLE) != LSM6DSO_OK)
  1421. {
  1422. return LSM6DSO_ERROR;
  1423. }
  1424. return LSM6DSO_OK;
  1425. }
  1426. /**
  1427. * @brief Enable wake up detection
  1428. * @param pObj the device pObj
  1429. * @param IntPin interrupt pin line to be used
  1430. * @retval 0 in case of success, an error code otherwise
  1431. */
  1432. int32_t LSM6DSO_ACC_Enable_Wake_Up_Detection(LSM6DSO_Object_t *pObj, LSM6DSO_SensorIntPin_t IntPin)
  1433. {
  1434. int32_t ret = LSM6DSO_OK;
  1435. lsm6dso_pin_int1_route_t val1;
  1436. lsm6dso_pin_int2_route_t val2;
  1437. /* Output Data Rate selection */
  1438. if (LSM6DSO_ACC_SetOutputDataRate(pObj, 417.0f) != LSM6DSO_OK)
  1439. {
  1440. return LSM6DSO_ERROR;
  1441. }
  1442. /* Full scale selection */
  1443. if (LSM6DSO_ACC_SetFullScale(pObj, 2) != LSM6DSO_OK)
  1444. {
  1445. return LSM6DSO_ERROR;
  1446. }
  1447. /* WAKE_DUR setting */
  1448. if (lsm6dso_wkup_dur_set(&(pObj->Ctx), 0x00) != LSM6DSO_OK)
  1449. {
  1450. return LSM6DSO_ERROR;
  1451. }
  1452. /* Set wake up threshold. */
  1453. if (lsm6dso_wkup_threshold_set(&(pObj->Ctx), 0x02) != LSM6DSO_OK)
  1454. {
  1455. return LSM6DSO_ERROR;
  1456. }
  1457. /* Enable wake up event on either INT1 or INT2 pin */
  1458. switch (IntPin)
  1459. {
  1460. case LSM6DSO_INT1_PIN:
  1461. if (lsm6dso_pin_int1_route_get(&(pObj->Ctx), &val1) != LSM6DSO_OK)
  1462. {
  1463. return LSM6DSO_ERROR;
  1464. }
  1465. val1.md1_cfg.int1_wu = PROPERTY_ENABLE;
  1466. if (lsm6dso_pin_int1_route_set(&(pObj->Ctx), &val1) != LSM6DSO_OK)
  1467. {
  1468. return LSM6DSO_ERROR;
  1469. }
  1470. break;
  1471. case LSM6DSO_INT2_PIN:
  1472. if (lsm6dso_pin_int2_route_get(&(pObj->Ctx), &val2) != LSM6DSO_OK)
  1473. {
  1474. return LSM6DSO_ERROR;
  1475. }
  1476. val2.md2_cfg.int2_wu = PROPERTY_ENABLE;
  1477. if (lsm6dso_pin_int2_route_set(&(pObj->Ctx), &val2) != LSM6DSO_OK)
  1478. {
  1479. return LSM6DSO_ERROR;
  1480. }
  1481. break;
  1482. default:
  1483. ret = LSM6DSO_ERROR;
  1484. break;
  1485. }
  1486. return ret;
  1487. }
  1488. /**
  1489. * @brief Disable wake up detection
  1490. * @param pObj the device pObj
  1491. * @retval 0 in case of success, an error code otherwise
  1492. */
  1493. int32_t LSM6DSO_ACC_Disable_Wake_Up_Detection(LSM6DSO_Object_t *pObj)
  1494. {
  1495. lsm6dso_pin_int1_route_t val1;
  1496. lsm6dso_pin_int2_route_t val2;
  1497. /* Disable wake up event on both INT1 and INT2 pins */
  1498. if (lsm6dso_pin_int1_route_get(&(pObj->Ctx), &val1) != LSM6DSO_OK)
  1499. {
  1500. return LSM6DSO_ERROR;
  1501. }
  1502. val1.md1_cfg.int1_wu = PROPERTY_DISABLE;
  1503. if (lsm6dso_pin_int1_route_set(&(pObj->Ctx), &val1) != LSM6DSO_OK)
  1504. {
  1505. return LSM6DSO_ERROR;
  1506. }
  1507. if (lsm6dso_pin_int2_route_get(&(pObj->Ctx), &val2) != LSM6DSO_OK)
  1508. {
  1509. return LSM6DSO_ERROR;
  1510. }
  1511. val2.md2_cfg.int2_wu = PROPERTY_DISABLE;
  1512. if (lsm6dso_pin_int2_route_set(&(pObj->Ctx), &val2) != LSM6DSO_OK)
  1513. {
  1514. return LSM6DSO_ERROR;
  1515. }
  1516. /* Reset wake up threshold. */
  1517. if (lsm6dso_wkup_threshold_set(&(pObj->Ctx), 0x00) != LSM6DSO_OK)
  1518. {
  1519. return LSM6DSO_ERROR;
  1520. }
  1521. /* WAKE_DUR setting */
  1522. if (lsm6dso_wkup_dur_set(&(pObj->Ctx), 0x00) != LSM6DSO_OK)
  1523. {
  1524. return LSM6DSO_ERROR;
  1525. }
  1526. return LSM6DSO_OK;
  1527. }
  1528. /**
  1529. * @brief Set wake up threshold
  1530. * @param pObj the device pObj
  1531. * @param Threshold wake up detection threshold
  1532. * @retval 0 in case of success, an error code otherwise
  1533. */
  1534. int32_t LSM6DSO_ACC_Set_Wake_Up_Threshold(LSM6DSO_Object_t *pObj, uint8_t Threshold)
  1535. {
  1536. /* Set wake up threshold. */
  1537. if (lsm6dso_wkup_threshold_set(&(pObj->Ctx), Threshold) != LSM6DSO_OK)
  1538. {
  1539. return LSM6DSO_ERROR;
  1540. }
  1541. return LSM6DSO_OK;
  1542. }
  1543. /**
  1544. * @brief Set wake up duration
  1545. * @param pObj the device pObj
  1546. * @param Duration wake up detection duration
  1547. * @retval 0 in case of success, an error code otherwise
  1548. */
  1549. int32_t LSM6DSO_ACC_Set_Wake_Up_Duration(LSM6DSO_Object_t *pObj, uint8_t Duration)
  1550. {
  1551. /* Set wake up duration. */
  1552. if (lsm6dso_wkup_dur_set(&(pObj->Ctx), Duration) != LSM6DSO_OK)
  1553. {
  1554. return LSM6DSO_ERROR;
  1555. }
  1556. return LSM6DSO_OK;
  1557. }
  1558. /**
  1559. * @brief Enable single tap detection
  1560. * @param pObj the device pObj
  1561. * @param IntPin interrupt pin line to be used
  1562. * @retval 0 in case of success, an error code otherwise
  1563. */
  1564. int32_t LSM6DSO_ACC_Enable_Single_Tap_Detection(LSM6DSO_Object_t *pObj, LSM6DSO_SensorIntPin_t IntPin)
  1565. {
  1566. int32_t ret = LSM6DSO_OK;
  1567. lsm6dso_pin_int1_route_t val1;
  1568. lsm6dso_pin_int2_route_t val2;
  1569. /* Output Data Rate selection */
  1570. if (LSM6DSO_ACC_SetOutputDataRate(pObj, 417.0f) != LSM6DSO_OK)
  1571. {
  1572. return LSM6DSO_ERROR;
  1573. }
  1574. /* Full scale selection */
  1575. if (LSM6DSO_ACC_SetFullScale(pObj, 2) != LSM6DSO_OK)
  1576. {
  1577. return LSM6DSO_ERROR;
  1578. }
  1579. /* Enable X direction in tap recognition. */
  1580. if (lsm6dso_tap_detection_on_x_set(&(pObj->Ctx), PROPERTY_ENABLE) != LSM6DSO_OK)
  1581. {
  1582. return LSM6DSO_ERROR;
  1583. }
  1584. /* Enable Y direction in tap recognition. */
  1585. if (lsm6dso_tap_detection_on_y_set(&(pObj->Ctx), PROPERTY_ENABLE) != LSM6DSO_OK)
  1586. {
  1587. return LSM6DSO_ERROR;
  1588. }
  1589. /* Enable Z direction in tap recognition. */
  1590. if (lsm6dso_tap_detection_on_z_set(&(pObj->Ctx), PROPERTY_ENABLE) != LSM6DSO_OK)
  1591. {
  1592. return LSM6DSO_ERROR;
  1593. }
  1594. /* Set tap threshold. */
  1595. if (lsm6dso_tap_threshold_x_set(&(pObj->Ctx), 0x08) != LSM6DSO_OK)
  1596. {
  1597. return LSM6DSO_ERROR;
  1598. }
  1599. /* Set tap shock time window. */
  1600. if (lsm6dso_tap_shock_set(&(pObj->Ctx), 0x02) != LSM6DSO_OK)
  1601. {
  1602. return LSM6DSO_ERROR;
  1603. }
  1604. /* Set tap quiet time window. */
  1605. if (lsm6dso_tap_quiet_set(&(pObj->Ctx), 0x01) != LSM6DSO_OK)
  1606. {
  1607. return LSM6DSO_ERROR;
  1608. }
  1609. /* _NOTE_: Tap duration time window - don't care for single tap. */
  1610. /* _NOTE_: Single/Double Tap event - don't care of this flag for single tap. */
  1611. /* Enable single tap event on either INT1 or INT2 pin */
  1612. switch (IntPin)
  1613. {
  1614. case LSM6DSO_INT1_PIN:
  1615. if (lsm6dso_pin_int1_route_get(&(pObj->Ctx), &val1) != LSM6DSO_OK)
  1616. {
  1617. return LSM6DSO_ERROR;
  1618. }
  1619. val1.md1_cfg.int1_single_tap = PROPERTY_ENABLE;
  1620. if (lsm6dso_pin_int1_route_set(&(pObj->Ctx), &val1) != LSM6DSO_OK)
  1621. {
  1622. return LSM6DSO_ERROR;
  1623. }
  1624. break;
  1625. case LSM6DSO_INT2_PIN:
  1626. if (lsm6dso_pin_int2_route_get(&(pObj->Ctx), &val2) != LSM6DSO_OK)
  1627. {
  1628. return LSM6DSO_ERROR;
  1629. }
  1630. val2.md2_cfg.int2_single_tap = PROPERTY_ENABLE;
  1631. if (lsm6dso_pin_int2_route_set(&(pObj->Ctx), &val2) != LSM6DSO_OK)
  1632. {
  1633. return LSM6DSO_ERROR;
  1634. }
  1635. break;
  1636. default:
  1637. ret = LSM6DSO_ERROR;
  1638. break;
  1639. }
  1640. return ret;
  1641. }
  1642. /**
  1643. * @brief Disable single tap detection
  1644. * @param pObj the device pObj
  1645. * @retval 0 in case of success, an error code otherwise
  1646. */
  1647. int32_t LSM6DSO_ACC_Disable_Single_Tap_Detection(LSM6DSO_Object_t *pObj)
  1648. {
  1649. lsm6dso_pin_int1_route_t val1;
  1650. lsm6dso_pin_int2_route_t val2;
  1651. /* Disable single tap event on both INT1 and INT2 pins */
  1652. if (lsm6dso_pin_int1_route_get(&(pObj->Ctx), &val1) != LSM6DSO_OK)
  1653. {
  1654. return LSM6DSO_ERROR;
  1655. }
  1656. val1.md1_cfg.int1_single_tap = PROPERTY_DISABLE;
  1657. if (lsm6dso_pin_int1_route_set(&(pObj->Ctx), &val1) != LSM6DSO_OK)
  1658. {
  1659. return LSM6DSO_ERROR;
  1660. }
  1661. if (lsm6dso_pin_int2_route_get(&(pObj->Ctx), &val2) != LSM6DSO_OK)
  1662. {
  1663. return LSM6DSO_ERROR;
  1664. }
  1665. val2.md2_cfg.int2_single_tap = PROPERTY_DISABLE;
  1666. if (lsm6dso_pin_int2_route_set(&(pObj->Ctx), &val2) != LSM6DSO_OK)
  1667. {
  1668. return LSM6DSO_ERROR;
  1669. }
  1670. /* Reset tap quiet time window. */
  1671. if (lsm6dso_tap_quiet_set(&(pObj->Ctx), 0x00) != LSM6DSO_OK)
  1672. {
  1673. return LSM6DSO_ERROR;
  1674. }
  1675. /* Reset tap shock time window. */
  1676. if (lsm6dso_tap_shock_set(&(pObj->Ctx), 0x00) != LSM6DSO_OK)
  1677. {
  1678. return LSM6DSO_ERROR;
  1679. }
  1680. /* Reset tap threshold. */
  1681. if (lsm6dso_tap_threshold_x_set(&(pObj->Ctx), 0x00) != LSM6DSO_OK)
  1682. {
  1683. return LSM6DSO_ERROR;
  1684. }
  1685. /* Disable Z direction in tap recognition. */
  1686. if (lsm6dso_tap_detection_on_z_set(&(pObj->Ctx), PROPERTY_DISABLE) != LSM6DSO_OK)
  1687. {
  1688. return LSM6DSO_ERROR;
  1689. }
  1690. /* Disable Y direction in tap recognition. */
  1691. if (lsm6dso_tap_detection_on_y_set(&(pObj->Ctx), PROPERTY_DISABLE) != LSM6DSO_OK)
  1692. {
  1693. return LSM6DSO_ERROR;
  1694. }
  1695. /* Disable X direction in tap recognition. */
  1696. if (lsm6dso_tap_detection_on_x_set(&(pObj->Ctx), PROPERTY_DISABLE) != LSM6DSO_OK)
  1697. {
  1698. return LSM6DSO_ERROR;
  1699. }
  1700. return LSM6DSO_OK;
  1701. }
  1702. /**
  1703. * @brief Enable double tap detection
  1704. * @param pObj the device pObj
  1705. * @param IntPin interrupt pin line to be used
  1706. * @retval 0 in case of success, an error code otherwise
  1707. */
  1708. int32_t LSM6DSO_ACC_Enable_Double_Tap_Detection(LSM6DSO_Object_t *pObj, LSM6DSO_SensorIntPin_t IntPin)
  1709. {
  1710. int32_t ret = LSM6DSO_OK;
  1711. lsm6dso_pin_int1_route_t val1;
  1712. lsm6dso_pin_int2_route_t val2;
  1713. /* Output Data Rate selection */
  1714. if (LSM6DSO_ACC_SetOutputDataRate(pObj, 417.0f) != LSM6DSO_OK)
  1715. {
  1716. return LSM6DSO_ERROR;
  1717. }
  1718. /* Full scale selection */
  1719. if (LSM6DSO_ACC_SetFullScale(pObj, 2) != LSM6DSO_OK)
  1720. {
  1721. return LSM6DSO_ERROR;
  1722. }
  1723. /* Enable X direction in tap recognition. */
  1724. if (lsm6dso_tap_detection_on_x_set(&(pObj->Ctx), PROPERTY_ENABLE) != LSM6DSO_OK)
  1725. {
  1726. return LSM6DSO_ERROR;
  1727. }
  1728. /* Enable Y direction in tap recognition. */
  1729. if (lsm6dso_tap_detection_on_y_set(&(pObj->Ctx), PROPERTY_ENABLE) != LSM6DSO_OK)
  1730. {
  1731. return LSM6DSO_ERROR;
  1732. }
  1733. /* Enable Z direction in tap recognition. */
  1734. if (lsm6dso_tap_detection_on_z_set(&(pObj->Ctx), PROPERTY_ENABLE) != LSM6DSO_OK)
  1735. {
  1736. return LSM6DSO_ERROR;
  1737. }
  1738. /* Set tap threshold. */
  1739. if (lsm6dso_tap_threshold_x_set(&(pObj->Ctx), 0x08) != LSM6DSO_OK)
  1740. {
  1741. return LSM6DSO_ERROR;
  1742. }
  1743. /* Set tap shock time window. */
  1744. if (lsm6dso_tap_shock_set(&(pObj->Ctx), 0x03) != LSM6DSO_OK)
  1745. {
  1746. return LSM6DSO_ERROR;
  1747. }
  1748. /* Set tap quiet time window. */
  1749. if (lsm6dso_tap_quiet_set(&(pObj->Ctx), 0x03) != LSM6DSO_OK)
  1750. {
  1751. return LSM6DSO_ERROR;
  1752. }
  1753. /* Set tap duration time window. */
  1754. if (lsm6dso_tap_dur_set(&(pObj->Ctx), 0x08) != LSM6DSO_OK)
  1755. {
  1756. return LSM6DSO_ERROR;
  1757. }
  1758. /* Single and double tap enabled. */
  1759. if (lsm6dso_tap_mode_set(&(pObj->Ctx), LSM6DSO_BOTH_SINGLE_DOUBLE) != LSM6DSO_OK)
  1760. {
  1761. return LSM6DSO_ERROR;
  1762. }
  1763. /* Enable double tap event on either INT1 or INT2 pin */
  1764. switch (IntPin)
  1765. {
  1766. case LSM6DSO_INT1_PIN:
  1767. if (lsm6dso_pin_int1_route_get(&(pObj->Ctx), &val1) != LSM6DSO_OK)
  1768. {
  1769. return LSM6DSO_ERROR;
  1770. }
  1771. val1.md1_cfg.int1_double_tap = PROPERTY_ENABLE;
  1772. if (lsm6dso_pin_int1_route_set(&(pObj->Ctx), &val1) != LSM6DSO_OK)
  1773. {
  1774. return LSM6DSO_ERROR;
  1775. }
  1776. break;
  1777. case LSM6DSO_INT2_PIN:
  1778. if (lsm6dso_pin_int2_route_get(&(pObj->Ctx), &val2) != LSM6DSO_OK)
  1779. {
  1780. return LSM6DSO_ERROR;
  1781. }
  1782. val2.md2_cfg.int2_double_tap = PROPERTY_ENABLE;
  1783. if (lsm6dso_pin_int2_route_set(&(pObj->Ctx), &val2) != LSM6DSO_OK)
  1784. {
  1785. return LSM6DSO_ERROR;
  1786. }
  1787. break;
  1788. default:
  1789. ret = LSM6DSO_ERROR;
  1790. break;
  1791. }
  1792. return ret;
  1793. }
  1794. /**
  1795. * @brief Disable double tap detection
  1796. * @param pObj the device pObj
  1797. * @retval 0 in case of success, an error code otherwise
  1798. */
  1799. int32_t LSM6DSO_ACC_Disable_Double_Tap_Detection(LSM6DSO_Object_t *pObj)
  1800. {
  1801. lsm6dso_pin_int1_route_t val1;
  1802. lsm6dso_pin_int2_route_t val2;
  1803. /* Disable double tap event on both INT1 and INT2 pins */
  1804. if (lsm6dso_pin_int1_route_get(&(pObj->Ctx), &val1) != LSM6DSO_OK)
  1805. {
  1806. return LSM6DSO_ERROR;
  1807. }
  1808. val1.md1_cfg.int1_double_tap = PROPERTY_DISABLE;
  1809. if (lsm6dso_pin_int1_route_set(&(pObj->Ctx), &val1) != LSM6DSO_OK)
  1810. {
  1811. return LSM6DSO_ERROR;
  1812. }
  1813. if (lsm6dso_pin_int2_route_get(&(pObj->Ctx), &val2) != LSM6DSO_OK)
  1814. {
  1815. return LSM6DSO_ERROR;
  1816. }
  1817. val2.md2_cfg.int2_double_tap = PROPERTY_DISABLE;
  1818. if (lsm6dso_pin_int2_route_set(&(pObj->Ctx), &val2) != LSM6DSO_OK)
  1819. {
  1820. return LSM6DSO_ERROR;
  1821. }
  1822. /* Only single tap enabled. */
  1823. if (lsm6dso_tap_mode_set(&(pObj->Ctx), LSM6DSO_ONLY_SINGLE) != LSM6DSO_OK)
  1824. {
  1825. return LSM6DSO_ERROR;
  1826. }
  1827. /* Reset tap duration time window. */
  1828. if (lsm6dso_tap_dur_set(&(pObj->Ctx), 0x00) != LSM6DSO_OK)
  1829. {
  1830. return LSM6DSO_ERROR;
  1831. }
  1832. /* Reset tap quiet time window. */
  1833. if (lsm6dso_tap_quiet_set(&(pObj->Ctx), 0x00) != LSM6DSO_OK)
  1834. {
  1835. return LSM6DSO_ERROR;
  1836. }
  1837. /* Reset tap shock time window. */
  1838. if (lsm6dso_tap_shock_set(&(pObj->Ctx), 0x00) != LSM6DSO_OK)
  1839. {
  1840. return LSM6DSO_ERROR;
  1841. }
  1842. /* Reset tap threshold. */
  1843. if (lsm6dso_tap_threshold_x_set(&(pObj->Ctx), 0x00) != LSM6DSO_OK)
  1844. {
  1845. return LSM6DSO_ERROR;
  1846. }
  1847. /* Disable Z direction in tap recognition. */
  1848. if (lsm6dso_tap_detection_on_z_set(&(pObj->Ctx), PROPERTY_DISABLE) != LSM6DSO_OK)
  1849. {
  1850. return LSM6DSO_ERROR;
  1851. }
  1852. /* Disable Y direction in tap recognition. */
  1853. if (lsm6dso_tap_detection_on_y_set(&(pObj->Ctx), PROPERTY_DISABLE) != LSM6DSO_OK)
  1854. {
  1855. return LSM6DSO_ERROR;
  1856. }
  1857. /* Disable X direction in tap recognition. */
  1858. if (lsm6dso_tap_detection_on_x_set(&(pObj->Ctx), PROPERTY_DISABLE) != LSM6DSO_OK)
  1859. {
  1860. return LSM6DSO_ERROR;
  1861. }
  1862. return LSM6DSO_OK;
  1863. }
  1864. /**
  1865. * @brief Set tap threshold
  1866. * @param pObj the device pObj
  1867. * @param Threshold tap threshold
  1868. * @retval 0 in case of success, an error code otherwise
  1869. */
  1870. int32_t LSM6DSO_ACC_Set_Tap_Threshold(LSM6DSO_Object_t *pObj, uint8_t Threshold)
  1871. {
  1872. /* Set tap threshold. */
  1873. if (lsm6dso_tap_threshold_x_set(&(pObj->Ctx), Threshold) != LSM6DSO_OK)
  1874. {
  1875. return LSM6DSO_ERROR;
  1876. }
  1877. return LSM6DSO_OK;
  1878. }
  1879. /**
  1880. * @brief Set tap shock time
  1881. * @param pObj the device pObj
  1882. * @param Time tap shock time
  1883. * @retval 0 in case of success, an error code otherwise
  1884. */
  1885. int32_t LSM6DSO_ACC_Set_Tap_Shock_Time(LSM6DSO_Object_t *pObj, uint8_t Time)
  1886. {
  1887. /* Set tap shock time window. */
  1888. if (lsm6dso_tap_shock_set(&(pObj->Ctx), Time) != LSM6DSO_OK)
  1889. {
  1890. return LSM6DSO_ERROR;
  1891. }
  1892. return LSM6DSO_OK;
  1893. }
  1894. /**
  1895. * @brief Set tap quiet time
  1896. * @param pObj the device pObj
  1897. * @param Time tap quiet time
  1898. * @retval 0 in case of success, an error code otherwise
  1899. */
  1900. int32_t LSM6DSO_ACC_Set_Tap_Quiet_Time(LSM6DSO_Object_t *pObj, uint8_t Time)
  1901. {
  1902. /* Set tap quiet time window. */
  1903. if (lsm6dso_tap_quiet_set(&(pObj->Ctx), Time) != LSM6DSO_OK)
  1904. {
  1905. return LSM6DSO_ERROR;
  1906. }
  1907. return LSM6DSO_OK;
  1908. }
  1909. /**
  1910. * @brief Set tap duration time
  1911. * @param pObj the device pObj
  1912. * @param Time tap duration time
  1913. * @retval 0 in case of success, an error code otherwise
  1914. */
  1915. int32_t LSM6DSO_ACC_Set_Tap_Duration_Time(LSM6DSO_Object_t *pObj, uint8_t Time)
  1916. {
  1917. /* Set tap duration time window. */
  1918. if (lsm6dso_tap_dur_set(&(pObj->Ctx), Time) != LSM6DSO_OK)
  1919. {
  1920. return LSM6DSO_ERROR;
  1921. }
  1922. return LSM6DSO_OK;
  1923. }
  1924. /**
  1925. * @brief Enable 6D orientation detection
  1926. * @param pObj the device pObj
  1927. * @param IntPin interrupt pin line to be used
  1928. * @retval 0 in case of success, an error code otherwise
  1929. */
  1930. int32_t LSM6DSO_ACC_Enable_6D_Orientation(LSM6DSO_Object_t *pObj, LSM6DSO_SensorIntPin_t IntPin)
  1931. {
  1932. int32_t ret = LSM6DSO_OK;
  1933. lsm6dso_pin_int1_route_t val1;
  1934. lsm6dso_pin_int2_route_t val2;
  1935. /* Output Data Rate selection */
  1936. if (LSM6DSO_ACC_SetOutputDataRate(pObj, 417.0f) != LSM6DSO_OK)
  1937. {
  1938. return LSM6DSO_ERROR;
  1939. }
  1940. /* Full scale selection */
  1941. if (LSM6DSO_ACC_SetFullScale(pObj, 2) != LSM6DSO_OK)
  1942. {
  1943. return LSM6DSO_ERROR;
  1944. }
  1945. /* 6D orientation enabled. */
  1946. if (lsm6dso_6d_threshold_set(&(pObj->Ctx), LSM6DSO_DEG_60) != LSM6DSO_OK)
  1947. {
  1948. return LSM6DSO_ERROR;
  1949. }
  1950. /* Enable 6D orientation event on either INT1 or INT2 pin */
  1951. switch (IntPin)
  1952. {
  1953. case LSM6DSO_INT1_PIN:
  1954. if (lsm6dso_pin_int1_route_get(&(pObj->Ctx), &val1) != LSM6DSO_OK)
  1955. {
  1956. return LSM6DSO_ERROR;
  1957. }
  1958. val1.md1_cfg.int1_6d = PROPERTY_ENABLE;
  1959. if (lsm6dso_pin_int1_route_set(&(pObj->Ctx), &val1) != LSM6DSO_OK)
  1960. {
  1961. return LSM6DSO_ERROR;
  1962. }
  1963. break;
  1964. case LSM6DSO_INT2_PIN:
  1965. if (lsm6dso_pin_int2_route_get(&(pObj->Ctx), &val2) != LSM6DSO_OK)
  1966. {
  1967. return LSM6DSO_ERROR;
  1968. }
  1969. val2.md2_cfg.int2_6d = PROPERTY_ENABLE;
  1970. if (lsm6dso_pin_int2_route_set(&(pObj->Ctx), &val2) != LSM6DSO_OK)
  1971. {
  1972. return LSM6DSO_ERROR;
  1973. }
  1974. break;
  1975. default:
  1976. ret = LSM6DSO_ERROR;
  1977. break;
  1978. }
  1979. return ret;
  1980. }
  1981. /**
  1982. * @brief Disable 6D orientation detection
  1983. * @param pObj the device pObj
  1984. * @retval 0 in case of success, an error code otherwise
  1985. */
  1986. int32_t LSM6DSO_ACC_Disable_6D_Orientation(LSM6DSO_Object_t *pObj)
  1987. {
  1988. lsm6dso_pin_int1_route_t val1;
  1989. lsm6dso_pin_int2_route_t val2;
  1990. /* Disable 6D orientation event on both INT1 and INT2 pins */
  1991. if (lsm6dso_pin_int1_route_get(&(pObj->Ctx), &val1) != LSM6DSO_OK)
  1992. {
  1993. return LSM6DSO_ERROR;
  1994. }
  1995. val1.md1_cfg.int1_6d = PROPERTY_DISABLE;
  1996. if (lsm6dso_pin_int1_route_set(&(pObj->Ctx), &val1) != LSM6DSO_OK)
  1997. {
  1998. return LSM6DSO_ERROR;
  1999. }
  2000. if (lsm6dso_pin_int2_route_get(&(pObj->Ctx), &val2) != LSM6DSO_OK)
  2001. {
  2002. return LSM6DSO_ERROR;
  2003. }
  2004. val2.md2_cfg.int2_6d = PROPERTY_DISABLE;
  2005. if (lsm6dso_pin_int2_route_set(&(pObj->Ctx), &val2) != LSM6DSO_OK)
  2006. {
  2007. return LSM6DSO_ERROR;
  2008. }
  2009. /* Reset 6D orientation. */
  2010. if (lsm6dso_6d_threshold_set(&(pObj->Ctx), LSM6DSO_DEG_80) != LSM6DSO_OK)
  2011. {
  2012. return LSM6DSO_ERROR;
  2013. }
  2014. return LSM6DSO_OK;
  2015. }
  2016. /**
  2017. * @brief Set 6D orientation threshold
  2018. * @param pObj the device pObj
  2019. * @param Threshold 6D Orientation detection threshold
  2020. * @retval 0 in case of success, an error code otherwise
  2021. */
  2022. int32_t LSM6DSO_ACC_Set_6D_Orientation_Threshold(LSM6DSO_Object_t *pObj, uint8_t Threshold)
  2023. {
  2024. int32_t ret = LSM6DSO_OK;
  2025. lsm6dso_sixd_ths_t newThreshold = LSM6DSO_DEG_80;
  2026. switch (Threshold)
  2027. {
  2028. case 0:
  2029. newThreshold = LSM6DSO_DEG_80;
  2030. break;
  2031. case 1:
  2032. newThreshold = LSM6DSO_DEG_70;
  2033. break;
  2034. case 2:
  2035. newThreshold = LSM6DSO_DEG_60;
  2036. break;
  2037. case 3:
  2038. newThreshold = LSM6DSO_DEG_50;
  2039. break;
  2040. default:
  2041. ret = LSM6DSO_ERROR;
  2042. break;
  2043. }
  2044. if(ret == LSM6DSO_ERROR)
  2045. {
  2046. return LSM6DSO_ERROR;
  2047. }
  2048. if (lsm6dso_6d_threshold_set(&(pObj->Ctx), newThreshold) != LSM6DSO_OK)
  2049. {
  2050. return LSM6DSO_ERROR;
  2051. }
  2052. return LSM6DSO_OK;
  2053. }
  2054. /**
  2055. * @brief Get the status of XLow orientation
  2056. * @param pObj the device pObj
  2057. * @param XLow the status of XLow orientation
  2058. * @retval 0 in case of success, an error code otherwise
  2059. */
  2060. int32_t LSM6DSO_ACC_Get_6D_Orientation_XL(LSM6DSO_Object_t *pObj, uint8_t *XLow)
  2061. {
  2062. lsm6dso_d6d_src_t data;
  2063. if (lsm6dso_read_reg(&(pObj->Ctx), LSM6DSO_D6D_SRC, (uint8_t *)&data, 1) != LSM6DSO_OK)
  2064. {
  2065. return LSM6DSO_ERROR;
  2066. }
  2067. *XLow = data.xl;
  2068. return LSM6DSO_OK;
  2069. }
  2070. /**
  2071. * @brief Get the status of XHigh orientation
  2072. * @param pObj the device pObj
  2073. * @param XHigh the status of XHigh orientation
  2074. * @retval 0 in case of success, an error code otherwise
  2075. */
  2076. int32_t LSM6DSO_ACC_Get_6D_Orientation_XH(LSM6DSO_Object_t *pObj, uint8_t *XHigh)
  2077. {
  2078. lsm6dso_d6d_src_t data;
  2079. if (lsm6dso_read_reg(&(pObj->Ctx), LSM6DSO_D6D_SRC, (uint8_t *)&data, 1) != LSM6DSO_OK)
  2080. {
  2081. return LSM6DSO_ERROR;
  2082. }
  2083. *XHigh = data.xh;
  2084. return LSM6DSO_OK;
  2085. }
  2086. /**
  2087. * @brief Get the status of YLow orientation
  2088. * @param pObj the device pObj
  2089. * @param YLow the status of YLow orientation
  2090. * @retval 0 in case of success, an error code otherwise
  2091. */
  2092. int32_t LSM6DSO_ACC_Get_6D_Orientation_YL(LSM6DSO_Object_t *pObj, uint8_t *YLow)
  2093. {
  2094. lsm6dso_d6d_src_t data;
  2095. if (lsm6dso_read_reg(&(pObj->Ctx), LSM6DSO_D6D_SRC, (uint8_t *)&data, 1) != LSM6DSO_OK)
  2096. {
  2097. return LSM6DSO_ERROR;
  2098. }
  2099. *YLow = data.yl;
  2100. return LSM6DSO_OK;
  2101. }
  2102. /**
  2103. * @brief Get the status of YHigh orientation
  2104. * @param pObj the device pObj
  2105. * @param YHigh the status of YHigh orientation
  2106. * @retval 0 in case of success, an error code otherwise
  2107. */
  2108. int32_t LSM6DSO_ACC_Get_6D_Orientation_YH(LSM6DSO_Object_t *pObj, uint8_t *YHigh)
  2109. {
  2110. lsm6dso_d6d_src_t data;
  2111. if (lsm6dso_read_reg(&(pObj->Ctx), LSM6DSO_D6D_SRC, (uint8_t *)&data, 1) != LSM6DSO_OK)
  2112. {
  2113. return LSM6DSO_ERROR;
  2114. }
  2115. *YHigh = data.yh;
  2116. return LSM6DSO_OK;
  2117. }
  2118. /**
  2119. * @brief Get the status of ZLow orientation
  2120. * @param pObj the device pObj
  2121. * @param ZLow the status of ZLow orientation
  2122. * @retval 0 in case of success, an error code otherwise
  2123. */
  2124. int32_t LSM6DSO_ACC_Get_6D_Orientation_ZL(LSM6DSO_Object_t *pObj, uint8_t *ZLow)
  2125. {
  2126. lsm6dso_d6d_src_t data;
  2127. if (lsm6dso_read_reg(&(pObj->Ctx), LSM6DSO_D6D_SRC, (uint8_t *)&data, 1) != LSM6DSO_OK)
  2128. {
  2129. return LSM6DSO_ERROR;
  2130. }
  2131. *ZLow = data.zl;
  2132. return LSM6DSO_OK;
  2133. }
  2134. /**
  2135. * @brief Get the status of ZHigh orientation
  2136. * @param pObj the device pObj
  2137. * @param ZHigh the status of ZHigh orientation
  2138. * @retval 0 in case of success, an error code otherwise
  2139. */
  2140. int32_t LSM6DSO_ACC_Get_6D_Orientation_ZH(LSM6DSO_Object_t *pObj, uint8_t *ZHigh)
  2141. {
  2142. lsm6dso_d6d_src_t data;
  2143. if (lsm6dso_read_reg(&(pObj->Ctx), LSM6DSO_D6D_SRC, (uint8_t *)&data, 1) != LSM6DSO_OK)
  2144. {
  2145. return LSM6DSO_ERROR;
  2146. }
  2147. *ZHigh = data.zh;
  2148. return LSM6DSO_OK;
  2149. }
  2150. /**
  2151. * @brief Get the LSM6DSO ACC data ready bit value
  2152. * @param pObj the device pObj
  2153. * @param Status the status of data ready bit
  2154. * @retval 0 in case of success, an error code otherwise
  2155. */
  2156. int32_t LSM6DSO_ACC_Get_DRDY_Status(LSM6DSO_Object_t *pObj, uint8_t *Status)
  2157. {
  2158. if (lsm6dso_xl_flag_data_ready_get(&(pObj->Ctx), Status) != LSM6DSO_OK)
  2159. {
  2160. return LSM6DSO_ERROR;
  2161. }
  2162. return LSM6DSO_OK;
  2163. }
  2164. /**
  2165. * @brief Get the status of all hardware events
  2166. * @param pObj the device pObj
  2167. * @param Status the status of all hardware events
  2168. * @retval 0 in case of success, an error code otherwise
  2169. */
  2170. int32_t LSM6DSO_ACC_Get_Event_Status(LSM6DSO_Object_t *pObj, LSM6DSO_Event_Status_t *Status)
  2171. {
  2172. uint8_t tilt_ia;
  2173. lsm6dso_wake_up_src_t wake_up_src;
  2174. lsm6dso_tap_src_t tap_src;
  2175. lsm6dso_d6d_src_t d6d_src;
  2176. lsm6dso_emb_func_src_t func_src;
  2177. lsm6dso_md1_cfg_t md1_cfg;
  2178. lsm6dso_md2_cfg_t md2_cfg;
  2179. lsm6dso_emb_func_int1_t int1_ctrl;
  2180. lsm6dso_emb_func_int2_t int2_ctrl;
  2181. (void)memset((void *)Status, 0x0, sizeof(LSM6DSO_Event_Status_t));
  2182. if (lsm6dso_read_reg(&(pObj->Ctx), LSM6DSO_WAKE_UP_SRC, (uint8_t *)&wake_up_src, 1) != LSM6DSO_OK)
  2183. {
  2184. return LSM6DSO_ERROR;
  2185. }
  2186. if (lsm6dso_read_reg(&(pObj->Ctx), LSM6DSO_TAP_SRC, (uint8_t *)&tap_src, 1) != LSM6DSO_OK)
  2187. {
  2188. return LSM6DSO_ERROR;
  2189. }
  2190. if (lsm6dso_read_reg(&(pObj->Ctx), LSM6DSO_D6D_SRC, (uint8_t *)&d6d_src, 1) != LSM6DSO_OK)
  2191. {
  2192. return LSM6DSO_ERROR;
  2193. }
  2194. if (lsm6dso_mem_bank_set(&(pObj->Ctx), LSM6DSO_EMBEDDED_FUNC_BANK) != LSM6DSO_OK)
  2195. {
  2196. return LSM6DSO_ERROR;
  2197. }
  2198. if (lsm6dso_read_reg(&(pObj->Ctx), LSM6DSO_EMB_FUNC_SRC, (uint8_t *)&func_src, 1) != LSM6DSO_OK)
  2199. {
  2200. return LSM6DSO_ERROR;
  2201. }
  2202. if (lsm6dso_read_reg(&(pObj->Ctx), LSM6DSO_EMB_FUNC_INT1, (uint8_t *)&int1_ctrl, 1) != LSM6DSO_OK)
  2203. {
  2204. return LSM6DSO_ERROR;
  2205. }
  2206. if (lsm6dso_read_reg(&(pObj->Ctx), LSM6DSO_EMB_FUNC_INT2, (uint8_t *)&int2_ctrl, 1) != LSM6DSO_OK)
  2207. {
  2208. return LSM6DSO_ERROR;
  2209. }
  2210. if (lsm6dso_mem_bank_set(&(pObj->Ctx), LSM6DSO_USER_BANK) != 0)
  2211. {
  2212. return LSM6DSO_ERROR;
  2213. }
  2214. if (lsm6dso_read_reg(&(pObj->Ctx), LSM6DSO_MD1_CFG, (uint8_t *)&md1_cfg, 1) != LSM6DSO_OK)
  2215. {
  2216. return LSM6DSO_ERROR;
  2217. }
  2218. if (lsm6dso_read_reg(&(pObj->Ctx), LSM6DSO_MD2_CFG, (uint8_t *)&md2_cfg, 1) != LSM6DSO_OK)
  2219. {
  2220. return LSM6DSO_ERROR;
  2221. }
  2222. if (lsm6dso_tilt_flag_data_ready_get(&(pObj->Ctx), &tilt_ia) != LSM6DSO_OK)
  2223. {
  2224. return LSM6DSO_ERROR;
  2225. }
  2226. if ((md1_cfg.int1_ff == 1U) || (md2_cfg.int2_ff == 1U))
  2227. {
  2228. if (wake_up_src.ff_ia == 1U)
  2229. {
  2230. Status->FreeFallStatus = 1;
  2231. }
  2232. }
  2233. if ((md1_cfg.int1_wu == 1U) || (md2_cfg.int2_wu == 1U))
  2234. {
  2235. if (wake_up_src.wu_ia == 1U)
  2236. {
  2237. Status->WakeUpStatus = 1;
  2238. }
  2239. }
  2240. if ((md1_cfg.int1_single_tap == 1U) || (md2_cfg.int2_single_tap == 1U))
  2241. {
  2242. if (tap_src.single_tap == 1U)
  2243. {
  2244. Status->TapStatus = 1;
  2245. }
  2246. }
  2247. if ((md1_cfg.int1_double_tap == 1U) || (md2_cfg.int2_double_tap == 1U))
  2248. {
  2249. if (tap_src.double_tap == 1U)
  2250. {
  2251. Status->DoubleTapStatus = 1;
  2252. }
  2253. }
  2254. if ((md1_cfg.int1_6d == 1U) || (md2_cfg.int2_6d == 1U))
  2255. {
  2256. if (d6d_src.d6d_ia == 1U)
  2257. {
  2258. Status->D6DOrientationStatus = 1;
  2259. }
  2260. }
  2261. if (int1_ctrl.int1_step_detector == 1U)
  2262. {
  2263. if (func_src.step_detected == 1U)
  2264. {
  2265. Status->StepStatus = 1;
  2266. }
  2267. }
  2268. if ((int1_ctrl.int1_tilt == 1U) || (int2_ctrl.int2_tilt == 1U))
  2269. {
  2270. if (tilt_ia == 1U)
  2271. {
  2272. Status->TiltStatus = 1;
  2273. }
  2274. }
  2275. return LSM6DSO_OK;
  2276. }
  2277. /**
  2278. * @brief Set self test
  2279. * @param pObj the device pObj
  2280. * @param val the value of st_xl in reg CTRL5_C
  2281. * @retval 0 in case of success, an error code otherwise
  2282. */
  2283. int32_t LSM6DSO_ACC_Set_SelfTest(LSM6DSO_Object_t *pObj, uint8_t val)
  2284. {
  2285. lsm6dso_st_xl_t reg;
  2286. reg = (val == 0U) ? LSM6DSO_XL_ST_DISABLE
  2287. : (val == 1U) ? LSM6DSO_XL_ST_POSITIVE
  2288. : (val == 2U) ? LSM6DSO_XL_ST_NEGATIVE
  2289. : LSM6DSO_XL_ST_DISABLE;
  2290. if (lsm6dso_xl_self_test_set(&(pObj->Ctx), reg) != LSM6DSO_OK)
  2291. {
  2292. return LSM6DSO_ERROR;
  2293. }
  2294. return LSM6DSO_OK;
  2295. }
  2296. /**
  2297. * @brief Get the LSM6DSO GYRO data ready bit value
  2298. * @param pObj the device pObj
  2299. * @param Status the status of data ready bit
  2300. * @retval 0 in case of success, an error code otherwise
  2301. */
  2302. int32_t LSM6DSO_GYRO_Get_DRDY_Status(LSM6DSO_Object_t *pObj, uint8_t *Status)
  2303. {
  2304. if (lsm6dso_gy_flag_data_ready_get(&(pObj->Ctx), Status) != LSM6DSO_OK)
  2305. {
  2306. return LSM6DSO_ERROR;
  2307. }
  2308. return LSM6DSO_OK;
  2309. }
  2310. /**
  2311. * @brief Set self test
  2312. * @param pObj the device pObj
  2313. * @param val the value of st_xl in reg CTRL5_C
  2314. * @retval 0 in case of success, an error code otherwise
  2315. */
  2316. int32_t LSM6DSO_GYRO_Set_SelfTest(LSM6DSO_Object_t *pObj, uint8_t val)
  2317. {
  2318. lsm6dso_st_g_t reg;
  2319. reg = (val == 0U) ? LSM6DSO_GY_ST_DISABLE
  2320. : (val == 1U) ? LSM6DSO_GY_ST_POSITIVE
  2321. : (val == 2U) ? LSM6DSO_GY_ST_NEGATIVE
  2322. : LSM6DSO_GY_ST_DISABLE;
  2323. if (lsm6dso_gy_self_test_set(&(pObj->Ctx), reg) != LSM6DSO_OK)
  2324. {
  2325. return LSM6DSO_ERROR;
  2326. }
  2327. return LSM6DSO_OK;
  2328. }
  2329. /**
  2330. * @brief Get the LSM6DSO FIFO number of samples
  2331. * @param pObj the device pObj
  2332. * @param NumSamples number of samples
  2333. * @retval 0 in case of success, an error code otherwise
  2334. */
  2335. int32_t LSM6DSO_FIFO_Get_Num_Samples(LSM6DSO_Object_t *pObj, uint16_t *NumSamples)
  2336. {
  2337. if (lsm6dso_fifo_data_level_get(&(pObj->Ctx), NumSamples) != LSM6DSO_OK)
  2338. {
  2339. return LSM6DSO_ERROR;
  2340. }
  2341. return LSM6DSO_OK;
  2342. }
  2343. /**
  2344. * @brief Get the LSM6DSO FIFO full status
  2345. * @param pObj the device pObj
  2346. * @param Status FIFO full status
  2347. * @retval 0 in case of success, an error code otherwise
  2348. */
  2349. int32_t LSM6DSO_FIFO_Get_Full_Status(LSM6DSO_Object_t *pObj, uint8_t *Status)
  2350. {
  2351. lsm6dso_reg_t reg;
  2352. if (lsm6dso_read_reg(&(pObj->Ctx), LSM6DSO_FIFO_STATUS2, &reg.byte, 1) != LSM6DSO_OK)
  2353. {
  2354. return LSM6DSO_ERROR;
  2355. }
  2356. *Status = reg.fifo_status2.fifo_full_ia;
  2357. return LSM6DSO_OK;
  2358. }
  2359. /**
  2360. * @brief Set the LSM6DSO FIFO full interrupt on INT1 pin
  2361. * @param pObj the device pObj
  2362. * @param Status FIFO full interrupt on INT1 pin status
  2363. * @retval 0 in case of success, an error code otherwise
  2364. */
  2365. int32_t LSM6DSO_FIFO_Set_INT1_FIFO_Full(LSM6DSO_Object_t *pObj, uint8_t Status)
  2366. {
  2367. lsm6dso_reg_t reg;
  2368. if (lsm6dso_read_reg(&(pObj->Ctx), LSM6DSO_INT1_CTRL, &reg.byte, 1) != LSM6DSO_OK)
  2369. {
  2370. return LSM6DSO_ERROR;
  2371. }
  2372. reg.int1_ctrl.int1_fifo_full = Status;
  2373. if (lsm6dso_write_reg(&(pObj->Ctx), LSM6DSO_INT1_CTRL, &reg.byte, 1) != LSM6DSO_OK)
  2374. {
  2375. return LSM6DSO_ERROR;
  2376. }
  2377. return LSM6DSO_OK;
  2378. }
  2379. /**
  2380. * @brief Set the LSM6DSO FIFO watermark level
  2381. * @param pObj the device pObj
  2382. * @param Watermark FIFO watermark level
  2383. * @retval 0 in case of success, an error code otherwise
  2384. */
  2385. int32_t LSM6DSO_FIFO_Set_Watermark_Level(LSM6DSO_Object_t *pObj, uint16_t Watermark)
  2386. {
  2387. if (lsm6dso_fifo_watermark_set(&(pObj->Ctx), Watermark) != LSM6DSO_OK)
  2388. {
  2389. return LSM6DSO_ERROR;
  2390. }
  2391. return LSM6DSO_OK;
  2392. }
  2393. /**
  2394. * @brief Set the LSM6DSO FIFO stop on watermark
  2395. * @param pObj the device pObj
  2396. * @param Status FIFO stop on watermark status
  2397. * @retval 0 in case of success, an error code otherwise
  2398. */
  2399. int32_t LSM6DSO_FIFO_Set_Stop_On_Fth(LSM6DSO_Object_t *pObj, uint8_t Status)
  2400. {
  2401. if (lsm6dso_fifo_stop_on_wtm_set(&(pObj->Ctx), Status) != LSM6DSO_OK)
  2402. {
  2403. return LSM6DSO_ERROR;
  2404. }
  2405. return LSM6DSO_OK;
  2406. }
  2407. /**
  2408. * @brief Set the LSM6DSO FIFO mode
  2409. * @param pObj the device pObj
  2410. * @param Mode FIFO mode
  2411. * @retval 0 in case of success, an error code otherwise
  2412. */
  2413. int32_t LSM6DSO_FIFO_Set_Mode(LSM6DSO_Object_t *pObj, uint8_t Mode)
  2414. {
  2415. int32_t ret = LSM6DSO_OK;
  2416. lsm6dso_fifo_mode_t newMode = LSM6DSO_BYPASS_MODE;
  2417. switch (Mode)
  2418. {
  2419. case 0:
  2420. newMode = LSM6DSO_BYPASS_MODE;
  2421. break;
  2422. case 1:
  2423. newMode = LSM6DSO_FIFO_MODE;
  2424. break;
  2425. case 3:
  2426. newMode = LSM6DSO_STREAM_TO_FIFO_MODE;
  2427. break;
  2428. case 4:
  2429. newMode = LSM6DSO_BYPASS_TO_STREAM_MODE;
  2430. break;
  2431. case 6:
  2432. newMode = LSM6DSO_STREAM_MODE;
  2433. break;
  2434. case 7:
  2435. newMode = LSM6DSO_BYPASS_TO_FIFO_MODE;
  2436. break;
  2437. default:
  2438. ret = LSM6DSO_ERROR;
  2439. break;
  2440. }
  2441. if (ret == LSM6DSO_ERROR)
  2442. {
  2443. return LSM6DSO_ERROR;
  2444. }
  2445. if (lsm6dso_fifo_mode_set(&(pObj->Ctx), newMode) != LSM6DSO_OK)
  2446. {
  2447. return LSM6DSO_ERROR;
  2448. }
  2449. return ret;
  2450. }
  2451. /**
  2452. * @brief Get the LSM6DSO FIFO tag
  2453. * @param pObj the device pObj
  2454. * @param Tag FIFO tag
  2455. * @retval 0 in case of success, an error code otherwise
  2456. */
  2457. int32_t LSM6DSO_FIFO_Get_Tag(LSM6DSO_Object_t *pObj, uint8_t *Tag)
  2458. {
  2459. lsm6dso_fifo_tag_t tag_local;
  2460. if (lsm6dso_fifo_sensor_tag_get(&(pObj->Ctx), &tag_local) != LSM6DSO_OK)
  2461. {
  2462. return LSM6DSO_ERROR;
  2463. }
  2464. *Tag = (uint8_t)tag_local;
  2465. return LSM6DSO_OK;
  2466. }
  2467. /**
  2468. * @brief Get the LSM6DSO FIFO raw data
  2469. * @param pObj the device pObj
  2470. * @param Data FIFO raw data array [6]
  2471. * @retval 0 in case of success, an error code otherwise
  2472. */
  2473. int32_t LSM6DSO_FIFO_Get_Data(LSM6DSO_Object_t *pObj, uint8_t *Data)
  2474. {
  2475. if (lsm6dso_read_reg(&(pObj->Ctx), LSM6DSO_FIFO_DATA_OUT_X_L, Data, 6) != LSM6DSO_OK)
  2476. {
  2477. return LSM6DSO_ERROR;
  2478. }
  2479. return LSM6DSO_OK;
  2480. }
  2481. /**
  2482. * @brief Get the LSM6DSO FIFO accelero single sample (16-bit data per 3 axes) and calculate acceleration [mg]
  2483. * @param pObj the device pObj
  2484. * @param Acceleration FIFO accelero axes [mg]
  2485. * @retval 0 in case of success, an error code otherwise
  2486. */
  2487. int32_t LSM6DSO_FIFO_ACC_Get_Axes(LSM6DSO_Object_t *pObj, LSM6DSO_Axes_t *Acceleration)
  2488. {
  2489. lsm6dso_axis3bit16_t data_raw;
  2490. float_t sensitivity = 0.0f;
  2491. float_t acceleration_float_t[3];
  2492. if (LSM6DSO_FIFO_Get_Data(pObj, data_raw.u8bit) != LSM6DSO_OK)
  2493. {
  2494. return LSM6DSO_ERROR;
  2495. }
  2496. if (LSM6DSO_ACC_GetSensitivity(pObj, &sensitivity) != LSM6DSO_OK)
  2497. {
  2498. return LSM6DSO_ERROR;
  2499. }
  2500. acceleration_float_t[0] = (float_t)data_raw.i16bit[0] * sensitivity;
  2501. acceleration_float_t[1] = (float_t)data_raw.i16bit[1] * sensitivity;
  2502. acceleration_float_t[2] = (float_t)data_raw.i16bit[2] * sensitivity;
  2503. Acceleration->x = (int32_t)acceleration_float_t[0];
  2504. Acceleration->y = (int32_t)acceleration_float_t[1];
  2505. Acceleration->z = (int32_t)acceleration_float_t[2];
  2506. return LSM6DSO_OK;
  2507. }
  2508. /**
  2509. * @brief Set the LSM6DSO FIFO accelero BDR value
  2510. * @param pObj the device pObj
  2511. * @param Bdr FIFO accelero BDR value
  2512. * @retval 0 in case of success, an error code otherwise
  2513. */
  2514. int32_t LSM6DSO_FIFO_ACC_Set_BDR(LSM6DSO_Object_t *pObj, float_t Bdr)
  2515. {
  2516. lsm6dso_bdr_xl_t new_bdr;
  2517. new_bdr = (Bdr <= 0.0f) ? LSM6DSO_XL_NOT_BATCHED
  2518. : (Bdr <= 12.5f) ? LSM6DSO_XL_BATCHED_AT_12Hz5
  2519. : (Bdr <= 26.0f) ? LSM6DSO_XL_BATCHED_AT_26Hz
  2520. : (Bdr <= 52.0f) ? LSM6DSO_XL_BATCHED_AT_52Hz
  2521. : (Bdr <= 104.0f) ? LSM6DSO_XL_BATCHED_AT_104Hz
  2522. : (Bdr <= 208.0f) ? LSM6DSO_XL_BATCHED_AT_208Hz
  2523. : (Bdr <= 417.0f) ? LSM6DSO_XL_BATCHED_AT_417Hz
  2524. : (Bdr <= 833.0f) ? LSM6DSO_XL_BATCHED_AT_833Hz
  2525. : (Bdr <= 1667.0f) ? LSM6DSO_XL_BATCHED_AT_1667Hz
  2526. : (Bdr <= 3333.0f) ? LSM6DSO_XL_BATCHED_AT_3333Hz
  2527. : LSM6DSO_XL_BATCHED_AT_6667Hz;
  2528. if (lsm6dso_fifo_xl_batch_set(&(pObj->Ctx), new_bdr) != LSM6DSO_OK)
  2529. {
  2530. return LSM6DSO_ERROR;
  2531. }
  2532. return LSM6DSO_OK;
  2533. }
  2534. /**
  2535. * @brief Get the LSM6DSO FIFO gyro single sample (16-bit data per 3 axes) and calculate angular velocity [mDPS]
  2536. * @param pObj the device pObj
  2537. * @param AngularVelocity FIFO gyro axes [mDPS]
  2538. * @retval 0 in case of success, an error code otherwise
  2539. */
  2540. int32_t LSM6DSO_FIFO_GYRO_Get_Axes(LSM6DSO_Object_t *pObj, LSM6DSO_Axes_t *AngularVelocity)
  2541. {
  2542. lsm6dso_axis3bit16_t data_raw;
  2543. float_t sensitivity = 0.0f;
  2544. float_t angular_velocity_float_t[3];
  2545. if (LSM6DSO_FIFO_Get_Data(pObj, data_raw.u8bit) != LSM6DSO_OK)
  2546. {
  2547. return LSM6DSO_ERROR;
  2548. }
  2549. if (LSM6DSO_GYRO_GetSensitivity(pObj, &sensitivity) != LSM6DSO_OK)
  2550. {
  2551. return LSM6DSO_ERROR;
  2552. }
  2553. angular_velocity_float_t[0] = (float_t)data_raw.i16bit[0] * sensitivity;
  2554. angular_velocity_float_t[1] = (float_t)data_raw.i16bit[1] * sensitivity;
  2555. angular_velocity_float_t[2] = (float_t)data_raw.i16bit[2] * sensitivity;
  2556. AngularVelocity->x = (int32_t)angular_velocity_float_t[0];
  2557. AngularVelocity->y = (int32_t)angular_velocity_float_t[1];
  2558. AngularVelocity->z = (int32_t)angular_velocity_float_t[2];
  2559. return LSM6DSO_OK;
  2560. }
  2561. /**
  2562. * @brief Set the LSM6DSO FIFO gyro BDR value
  2563. * @param pObj the device pObj
  2564. * @param Bdr FIFO gyro BDR value
  2565. * @retval 0 in case of success, an error code otherwise
  2566. */
  2567. int32_t LSM6DSO_FIFO_GYRO_Set_BDR(LSM6DSO_Object_t *pObj, float_t Bdr)
  2568. {
  2569. lsm6dso_bdr_gy_t new_bdr;
  2570. new_bdr = (Bdr <= 0.0f) ? LSM6DSO_GY_NOT_BATCHED
  2571. : (Bdr <= 12.5f) ? LSM6DSO_GY_BATCHED_AT_12Hz5
  2572. : (Bdr <= 26.0f) ? LSM6DSO_GY_BATCHED_AT_26Hz
  2573. : (Bdr <= 52.0f) ? LSM6DSO_GY_BATCHED_AT_52Hz
  2574. : (Bdr <= 104.0f) ? LSM6DSO_GY_BATCHED_AT_104Hz
  2575. : (Bdr <= 208.0f) ? LSM6DSO_GY_BATCHED_AT_208Hz
  2576. : (Bdr <= 417.0f) ? LSM6DSO_GY_BATCHED_AT_417Hz
  2577. : (Bdr <= 833.0f) ? LSM6DSO_GY_BATCHED_AT_833Hz
  2578. : (Bdr <= 1667.0f) ? LSM6DSO_GY_BATCHED_AT_1667Hz
  2579. : (Bdr <= 3333.0f) ? LSM6DSO_GY_BATCHED_AT_3333Hz
  2580. : LSM6DSO_GY_BATCHED_AT_6667Hz;
  2581. if (lsm6dso_fifo_gy_batch_set(&(pObj->Ctx), new_bdr) != LSM6DSO_OK)
  2582. {
  2583. return LSM6DSO_ERROR;
  2584. }
  2585. return LSM6DSO_OK;
  2586. }
  2587. /**
  2588. * @brief Enable LSM6DSO accelerometer DRDY interrupt on INT1
  2589. * @param pObj the device pObj
  2590. * @retval 0 in case of success, an error code otherwise
  2591. */
  2592. int32_t LSM6DSO_ACC_Enable_DRDY_On_INT1(LSM6DSO_Object_t *pObj)
  2593. {
  2594. lsm6dso_pin_int1_route_t pin_int1_route;
  2595. /* Enable accelerometer DRDY Interrupt on INT1 */
  2596. if (lsm6dso_pin_int1_route_get(&(pObj->Ctx), &pin_int1_route) != LSM6DSO_OK)
  2597. {
  2598. return LSM6DSO_ERROR;
  2599. }
  2600. pin_int1_route.int1_ctrl.int1_drdy_xl = 1;
  2601. pin_int1_route.int1_ctrl.int1_drdy_g = 0;
  2602. if (lsm6dso_pin_int1_route_set(&(pObj->Ctx), &pin_int1_route) != LSM6DSO_OK)
  2603. {
  2604. return LSM6DSO_ERROR;
  2605. }
  2606. return LSM6DSO_OK;
  2607. }
  2608. /**
  2609. * @brief Disable LSM6DSO accelerometer DRDY interrupt on INT1
  2610. * @param pObj the device pObj
  2611. * @retval 0 in case of success, an error code otherwise
  2612. */
  2613. int32_t LSM6DSO_ACC_Disable_DRDY_On_INT1(LSM6DSO_Object_t *pObj)
  2614. {
  2615. lsm6dso_pin_int1_route_t pin_int1_route;
  2616. /* Disable accelerometer DRDY Interrupt on INT1 */
  2617. if (lsm6dso_pin_int1_route_get(&(pObj->Ctx), &pin_int1_route) != LSM6DSO_OK)
  2618. {
  2619. return LSM6DSO_ERROR;
  2620. }
  2621. pin_int1_route.int1_ctrl.int1_drdy_xl = 0;
  2622. if (lsm6dso_pin_int1_route_set(&(pObj->Ctx), &pin_int1_route) != LSM6DSO_OK)
  2623. {
  2624. return LSM6DSO_ERROR;
  2625. }
  2626. return LSM6DSO_OK;
  2627. }
  2628. /**
  2629. * @brief Set the LSM6DSO accelerometer power mode
  2630. * @param pObj the device pObj
  2631. * @param PowerMode Value of the powerMode
  2632. * @retval 0 in case of success, an error code otherwise
  2633. */
  2634. int32_t LSM6DSO_ACC_Set_Power_Mode(LSM6DSO_Object_t *pObj, uint8_t PowerMode)
  2635. {
  2636. int32_t ret = LSM6DSO_OK;
  2637. lsm6dso_xl_hm_mode_t newPowerMode = LSM6DSO_HIGH_PERFORMANCE_MD;
  2638. switch (PowerMode)
  2639. {
  2640. case 0:
  2641. newPowerMode = LSM6DSO_HIGH_PERFORMANCE_MD;
  2642. break;
  2643. case 1:
  2644. newPowerMode = LSM6DSO_LOW_NORMAL_POWER_MD;
  2645. break;
  2646. case 2:
  2647. newPowerMode = LSM6DSO_ULTRA_LOW_POWER_MD;
  2648. break;
  2649. default:
  2650. ret = LSM6DSO_ERROR;
  2651. break;
  2652. }
  2653. if (ret == LSM6DSO_ERROR)
  2654. {
  2655. return LSM6DSO_ERROR;
  2656. }
  2657. if (lsm6dso_xl_power_mode_set(&(pObj->Ctx), newPowerMode) != LSM6DSO_OK)
  2658. {
  2659. return LSM6DSO_ERROR;
  2660. }
  2661. return LSM6DSO_OK;
  2662. }
  2663. /**
  2664. * @brief Set the LSM6DSO accelerometer filter mode
  2665. * @param pObj the device pObj
  2666. * @param LowHighPassFlag 0/1 for setting low-pass/high-pass filter mode
  2667. * @param FilterMode Value of the filter Mode
  2668. * @retval 0 in case of success, an error code otherwise
  2669. */
  2670. int32_t LSM6DSO_ACC_Set_Filter_Mode(LSM6DSO_Object_t *pObj, uint8_t LowHighPassFlag, uint8_t FilterMode)
  2671. {
  2672. int32_t ret = LSM6DSO_OK;
  2673. lsm6dso_hp_slope_xl_en_t newFilterMode = LSM6DSO_HP_PATH_DISABLE_ON_OUT;
  2674. switch (FilterMode)
  2675. {
  2676. case 0x00:
  2677. newFilterMode = LSM6DSO_HP_PATH_DISABLE_ON_OUT;
  2678. break;
  2679. case 0x01:
  2680. newFilterMode = LSM6DSO_LP_ODR_DIV_10;
  2681. break;
  2682. case 0x02:
  2683. newFilterMode = LSM6DSO_LP_ODR_DIV_20;
  2684. break;
  2685. case 0x03:
  2686. newFilterMode = LSM6DSO_LP_ODR_DIV_45;
  2687. break;
  2688. case 0x04:
  2689. newFilterMode = LSM6DSO_LP_ODR_DIV_100;
  2690. break;
  2691. case 0x05:
  2692. newFilterMode = LSM6DSO_LP_ODR_DIV_200;
  2693. break;
  2694. case 0x06:
  2695. newFilterMode = LSM6DSO_LP_ODR_DIV_400;
  2696. break;
  2697. case 0x07:
  2698. newFilterMode = LSM6DSO_LP_ODR_DIV_800;
  2699. break;
  2700. case 0x10:
  2701. newFilterMode = LSM6DSO_SLOPE_ODR_DIV_4;
  2702. break;
  2703. case 0x11:
  2704. newFilterMode = LSM6DSO_HP_ODR_DIV_10;
  2705. break;
  2706. case 0x12:
  2707. newFilterMode = LSM6DSO_HP_ODR_DIV_20;
  2708. break;
  2709. case 0x13:
  2710. newFilterMode = LSM6DSO_HP_ODR_DIV_45;
  2711. break;
  2712. case 0x14:
  2713. newFilterMode = LSM6DSO_HP_ODR_DIV_100;
  2714. break;
  2715. case 0x15:
  2716. newFilterMode = LSM6DSO_HP_ODR_DIV_200;
  2717. break;
  2718. case 0x16:
  2719. newFilterMode = LSM6DSO_HP_ODR_DIV_400;
  2720. break;
  2721. case 0x17:
  2722. newFilterMode = LSM6DSO_HP_ODR_DIV_800;
  2723. break;
  2724. case 0x31:
  2725. newFilterMode = LSM6DSO_HP_REF_MD_ODR_DIV_10;
  2726. break;
  2727. case 0x32:
  2728. newFilterMode = LSM6DSO_HP_REF_MD_ODR_DIV_20;
  2729. break;
  2730. case 0x33:
  2731. newFilterMode = LSM6DSO_HP_REF_MD_ODR_DIV_45;
  2732. break;
  2733. case 0x34:
  2734. newFilterMode = LSM6DSO_HP_REF_MD_ODR_DIV_100;
  2735. break;
  2736. case 0x35:
  2737. newFilterMode = LSM6DSO_HP_REF_MD_ODR_DIV_200;
  2738. break;
  2739. case 0x36:
  2740. newFilterMode = LSM6DSO_HP_REF_MD_ODR_DIV_400;
  2741. break;
  2742. case 0x37:
  2743. newFilterMode = LSM6DSO_HP_REF_MD_ODR_DIV_800;
  2744. break;
  2745. default:
  2746. ret = LSM6DSO_ERROR;
  2747. break;
  2748. }
  2749. if (ret == LSM6DSO_ERROR)
  2750. {
  2751. return LSM6DSO_ERROR;
  2752. }
  2753. if (LowHighPassFlag == 0U)
  2754. {
  2755. /*Set accelerometer low_pass filter-mode*/
  2756. /*Set to 1 LPF2 bit (CTRL8_XL)*/
  2757. if (lsm6dso_xl_filter_lp2_set(&(pObj->Ctx), 1) != LSM6DSO_OK)
  2758. {
  2759. return LSM6DSO_ERROR;
  2760. }
  2761. if (lsm6dso_xl_hp_path_on_out_set(&(pObj->Ctx), newFilterMode) != LSM6DSO_OK)
  2762. {
  2763. return LSM6DSO_ERROR;
  2764. }
  2765. }
  2766. else
  2767. {
  2768. /*Set accelerometer high_pass filter-mode*/
  2769. if (lsm6dso_xl_hp_path_on_out_set(&(pObj->Ctx), newFilterMode) != LSM6DSO_OK)
  2770. {
  2771. return LSM6DSO_ERROR;
  2772. }
  2773. }
  2774. return LSM6DSO_OK;
  2775. }
  2776. /**
  2777. * @brief Enable LSM6DSO accelerometer inactivity detection
  2778. * @param pObj the device pObj
  2779. * @param InactMode inactivity detection mode
  2780. * @param IntPin interrupt pin line to be used
  2781. * @retval 0 in case of success, an error code otherwise
  2782. */
  2783. int32_t LSM6DSO_ACC_Enable_Inactivity_Detection(LSM6DSO_Object_t *pObj, lsm6dso_inact_en_t InactMode, LSM6DSO_SensorIntPin_t IntPin)
  2784. {
  2785. lsm6dso_pin_int1_route_t val1;
  2786. lsm6dso_pin_int2_route_t val2;
  2787. int32_t ret = LSM6DSO_OK;
  2788. /* Full scale selection */
  2789. if (LSM6DSO_ACC_SetFullScale(pObj, 2) != LSM6DSO_OK)
  2790. {
  2791. return LSM6DSO_ERROR;
  2792. }
  2793. if (LSM6DSO_GYRO_SetFullScale(pObj, 250) != LSM6DSO_OK)
  2794. {
  2795. return LSM6DSO_ERROR;
  2796. }
  2797. /* SLEEP_DUR setting */
  2798. if (lsm6dso_act_sleep_dur_set(&(pObj->Ctx), 0x01) != LSM6DSO_OK)
  2799. {
  2800. return LSM6DSO_ERROR;
  2801. }
  2802. /* Enable inactivity detection. */
  2803. switch(InactMode)
  2804. {
  2805. case LSM6DSO_XL_AND_GY_NOT_AFFECTED:
  2806. {
  2807. if (lsm6dso_act_mode_set(&(pObj->Ctx), LSM6DSO_XL_AND_GY_NOT_AFFECTED) != LSM6DSO_OK)
  2808. {
  2809. return LSM6DSO_ERROR;
  2810. }
  2811. break;
  2812. }
  2813. case LSM6DSO_XL_12Hz5_GY_NOT_AFFECTED:
  2814. {
  2815. if (lsm6dso_act_mode_set(&(pObj->Ctx), LSM6DSO_XL_12Hz5_GY_NOT_AFFECTED) != LSM6DSO_OK)
  2816. {
  2817. return LSM6DSO_ERROR;
  2818. }
  2819. break;
  2820. }
  2821. case LSM6DSO_XL_12Hz5_GY_SLEEP:
  2822. {
  2823. if (lsm6dso_act_mode_set(&(pObj->Ctx), LSM6DSO_XL_12Hz5_GY_SLEEP) != LSM6DSO_OK)
  2824. {
  2825. return LSM6DSO_ERROR;
  2826. }
  2827. break;
  2828. }
  2829. case LSM6DSO_XL_12Hz5_GY_PD:
  2830. {
  2831. if (lsm6dso_act_mode_set(&(pObj->Ctx), LSM6DSO_XL_12Hz5_GY_PD) != LSM6DSO_OK)
  2832. {
  2833. return LSM6DSO_ERROR;
  2834. }
  2835. break;
  2836. }
  2837. default:
  2838. ret = LSM6DSO_ERROR;
  2839. break;
  2840. }
  2841. if (ret == LSM6DSO_ERROR)
  2842. {
  2843. return LSM6DSO_ERROR;
  2844. }
  2845. /* Enable Inactivity event on either INT1 or INT2 pin */
  2846. switch (IntPin)
  2847. {
  2848. case LSM6DSO_INT1_PIN:
  2849. {
  2850. if (lsm6dso_pin_int1_route_get(&(pObj->Ctx), &val1) != LSM6DSO_OK)
  2851. {
  2852. return LSM6DSO_ERROR;
  2853. }
  2854. val1.md1_cfg.int1_sleep_change = PROPERTY_ENABLE;
  2855. if (lsm6dso_pin_int1_route_set(&(pObj->Ctx), &val1) != LSM6DSO_OK)
  2856. {
  2857. return LSM6DSO_ERROR;
  2858. }
  2859. break;
  2860. }
  2861. case LSM6DSO_INT2_PIN:
  2862. {
  2863. if (lsm6dso_pin_int2_route_get(&(pObj->Ctx), &val2) != LSM6DSO_OK)
  2864. {
  2865. return LSM6DSO_ERROR;
  2866. }
  2867. val2.md2_cfg.int2_sleep_change = PROPERTY_ENABLE;
  2868. if (lsm6dso_pin_int2_route_set(&(pObj->Ctx), &val2) != LSM6DSO_OK)
  2869. {
  2870. return LSM6DSO_ERROR;
  2871. }
  2872. break;
  2873. }
  2874. default:
  2875. ret = LSM6DSO_ERROR;
  2876. break;
  2877. }
  2878. return ret;
  2879. }
  2880. /**
  2881. * @brief Disable LSM6DSO accelerometer inactivity detection
  2882. * @param pObj the device pObj
  2883. * @retval 0 in case of success, an error code otherwise
  2884. */
  2885. int32_t LSM6DSO_ACC_Disable_Inactivity_Detection(LSM6DSO_Object_t *pObj)
  2886. {
  2887. lsm6dso_pin_int1_route_t val1;
  2888. lsm6dso_pin_int2_route_t val2;
  2889. /* Disable inactivity event on both INT1 and INT2 pins */
  2890. if (lsm6dso_pin_int1_route_get(&(pObj->Ctx), &val1) != LSM6DSO_OK)
  2891. {
  2892. return LSM6DSO_ERROR;
  2893. }
  2894. val1.md1_cfg.int1_sleep_change = PROPERTY_DISABLE;
  2895. if (lsm6dso_pin_int1_route_set(&(pObj->Ctx), &val1) != LSM6DSO_OK)
  2896. {
  2897. return LSM6DSO_ERROR;
  2898. }
  2899. if (lsm6dso_pin_int2_route_get(&(pObj->Ctx), &val2) != LSM6DSO_OK)
  2900. {
  2901. return LSM6DSO_ERROR;
  2902. }
  2903. val2.md2_cfg.int2_sleep_change = PROPERTY_DISABLE;
  2904. if (lsm6dso_pin_int2_route_set(&(pObj->Ctx), &val2) != LSM6DSO_OK)
  2905. {
  2906. return LSM6DSO_ERROR;
  2907. }
  2908. /* Disable inactivity detection. */
  2909. if (lsm6dso_act_mode_set(&(pObj->Ctx), LSM6DSO_XL_AND_GY_NOT_AFFECTED) != LSM6DSO_OK)
  2910. {
  2911. return LSM6DSO_ERROR;
  2912. }
  2913. /* SLEEP_DUR reset */
  2914. if (lsm6dso_act_sleep_dur_set(&(pObj->Ctx), 0x00) != LSM6DSO_OK)
  2915. {
  2916. return LSM6DSO_ERROR;
  2917. }
  2918. return LSM6DSO_OK;
  2919. }
  2920. /**
  2921. * @brief Set LSM6DSO accelerometer sleep duration
  2922. * @param pObj the device pObj
  2923. * @param Duration wake up detection duration
  2924. * @retval 0 in case of success, an error code otherwise
  2925. */
  2926. int32_t LSM6DSO_ACC_Set_Sleep_Duration(LSM6DSO_Object_t *pObj, uint8_t Duration)
  2927. {
  2928. /* Set wake up duration. */
  2929. if (lsm6dso_act_sleep_dur_set(&(pObj->Ctx), Duration) != LSM6DSO_OK)
  2930. {
  2931. return LSM6DSO_ERROR;
  2932. }
  2933. return LSM6DSO_OK;
  2934. }
  2935. /**
  2936. * @brief Enable LSM6DSO gyroscope DRDY interrupt on INT2
  2937. * @param pObj the device pObj
  2938. * @retval 0 in case of success, an error code otherwise
  2939. */
  2940. int32_t LSM6DSO_GYRO_Enable_DRDY_On_INT2(LSM6DSO_Object_t *pObj)
  2941. {
  2942. lsm6dso_pin_int2_route_t pin_int2_route;
  2943. /* Enable gyroscope DRDY Interrupts on INT2 */
  2944. if (lsm6dso_pin_int2_route_get(&(pObj->Ctx), &pin_int2_route) != LSM6DSO_OK)
  2945. {
  2946. return LSM6DSO_ERROR;
  2947. }
  2948. pin_int2_route.int2_ctrl.int2_drdy_xl = 0;
  2949. pin_int2_route.int2_ctrl.int2_drdy_g = 1;
  2950. if (lsm6dso_pin_int2_route_set(&(pObj->Ctx), &pin_int2_route) != LSM6DSO_OK)
  2951. {
  2952. return LSM6DSO_ERROR;
  2953. }
  2954. return LSM6DSO_OK;
  2955. }
  2956. /**
  2957. * @brief Set the LSM6DSO gyroscope power mode
  2958. * @param pObj the device pObj
  2959. * @param PowerMode Value of the powerMode
  2960. * @retval 0 in case of success, an error code otherwise
  2961. */
  2962. int32_t LSM6DSO_GYRO_Set_Power_Mode(LSM6DSO_Object_t *pObj, uint8_t PowerMode)
  2963. {
  2964. int32_t ret = LSM6DSO_OK;
  2965. lsm6dso_g_hm_mode_t newPowerMode = LSM6DSO_GY_HIGH_PERFORMANCE;
  2966. switch (PowerMode)
  2967. {
  2968. case 0:
  2969. newPowerMode = LSM6DSO_GY_HIGH_PERFORMANCE;
  2970. break;
  2971. case 1:
  2972. newPowerMode = LSM6DSO_GY_NORMAL;
  2973. break;
  2974. default:
  2975. ret = LSM6DSO_ERROR;
  2976. break;
  2977. }
  2978. if (ret == LSM6DSO_ERROR)
  2979. {
  2980. return LSM6DSO_ERROR;
  2981. }
  2982. if (lsm6dso_gy_power_mode_set(&(pObj->Ctx), newPowerMode) != LSM6DSO_OK)
  2983. {
  2984. return LSM6DSO_ERROR;
  2985. }
  2986. return LSM6DSO_OK;
  2987. }
  2988. /**
  2989. * @brief Set the LSM6DSO gyroscope filter mode
  2990. * @param pObj the device pObj
  2991. * @param LowHighPassFlag 0/1 for setting low-pass/high-pass filter mode
  2992. * @param FilterMode Value of the filter Mode
  2993. * @retval 0 in case of success, an error code otherwise
  2994. */
  2995. int32_t LSM6DSO_GYRO_Set_Filter_Mode(LSM6DSO_Object_t *pObj, uint8_t LowHighPassFlag, uint8_t FilterMode)
  2996. {
  2997. int32_t ret = LSM6DSO_OK;
  2998. if (LowHighPassFlag == 0U)
  2999. {
  3000. lsm6dso_ftype_t newFilterMode = LSM6DSO_ULTRA_LIGHT;
  3001. switch (FilterMode)
  3002. {
  3003. case 0:
  3004. newFilterMode = LSM6DSO_ULTRA_LIGHT;
  3005. break;
  3006. case 1:
  3007. newFilterMode = LSM6DSO_VERY_LIGHT;
  3008. break;
  3009. case 2:
  3010. newFilterMode = LSM6DSO_LIGHT;
  3011. break;
  3012. case 3:
  3013. newFilterMode = LSM6DSO_MEDIUM;
  3014. break;
  3015. case 4:
  3016. newFilterMode = LSM6DSO_STRONG;
  3017. break;
  3018. case 5:
  3019. newFilterMode = LSM6DSO_VERY_STRONG;
  3020. break;
  3021. case 6:
  3022. newFilterMode = LSM6DSO_AGGRESSIVE;
  3023. break;
  3024. case 7:
  3025. newFilterMode = LSM6DSO_XTREME;
  3026. break;
  3027. default:
  3028. ret = LSM6DSO_ERROR;
  3029. break;
  3030. }
  3031. if (ret == LSM6DSO_ERROR)
  3032. {
  3033. return LSM6DSO_ERROR;
  3034. }
  3035. /*Set gyroscope low_pass 1 filter-mode*/
  3036. /* Enable low-pass filter */
  3037. if (lsm6dso_gy_filter_lp1_set(&(pObj->Ctx), 1) != LSM6DSO_OK)
  3038. {
  3039. return LSM6DSO_ERROR;
  3040. }
  3041. if (lsm6dso_gy_lp1_bandwidth_set(&(pObj->Ctx), newFilterMode) != LSM6DSO_OK)
  3042. {
  3043. return LSM6DSO_ERROR;
  3044. }
  3045. }
  3046. else
  3047. {
  3048. lsm6dso_hpm_g_t newFilterMode = LSM6DSO_HP_FILTER_NONE;
  3049. switch (FilterMode)
  3050. {
  3051. case 0x00:
  3052. newFilterMode = LSM6DSO_HP_FILTER_NONE;
  3053. break;
  3054. case 0x80:
  3055. newFilterMode = LSM6DSO_HP_FILTER_16mHz;
  3056. break;
  3057. case 0x81:
  3058. newFilterMode = LSM6DSO_HP_FILTER_65mHz;
  3059. break;
  3060. case 0x82:
  3061. newFilterMode = LSM6DSO_HP_FILTER_260mHz;
  3062. break;
  3063. case 0x83:
  3064. newFilterMode = LSM6DSO_HP_FILTER_1Hz04;
  3065. break;
  3066. default:
  3067. ret = LSM6DSO_ERROR;
  3068. break;
  3069. }
  3070. if (ret == LSM6DSO_ERROR)
  3071. {
  3072. return LSM6DSO_ERROR;
  3073. }
  3074. /*Set gyroscope high_pass filter-mode*/
  3075. /* Enable high-pass filter */
  3076. if (lsm6dso_gy_hp_path_internal_set(&(pObj->Ctx), newFilterMode) != LSM6DSO_OK)
  3077. {
  3078. return LSM6DSO_ERROR;
  3079. }
  3080. }
  3081. return LSM6DSO_OK;
  3082. }
  3083. /**
  3084. * @brief Set LSM6DSO DRDY mode
  3085. * @param pObj the device pObj
  3086. * @param Mode DRDY mode
  3087. * @retval 0 in case of success, an error code otherwise
  3088. */
  3089. int32_t LSM6DSO_DRDY_Set_Mode(LSM6DSO_Object_t *pObj, uint8_t Mode)
  3090. {
  3091. int32_t ret = LSM6DSO_OK;
  3092. lsm6dso_dataready_pulsed_t newMode = LSM6DSO_DRDY_LATCHED;
  3093. switch (Mode)
  3094. {
  3095. case 0:
  3096. newMode = LSM6DSO_DRDY_LATCHED;
  3097. break;
  3098. case 1:
  3099. newMode = LSM6DSO_DRDY_PULSED;
  3100. break;
  3101. default:
  3102. ret = LSM6DSO_ERROR;
  3103. break;
  3104. }
  3105. if (ret == LSM6DSO_ERROR)
  3106. {
  3107. return LSM6DSO_ERROR;
  3108. }
  3109. /* Set DRDY mode */
  3110. if (lsm6dso_data_ready_mode_set(&(pObj->Ctx), newMode) != LSM6DSO_OK)
  3111. {
  3112. return LSM6DSO_ERROR;
  3113. }
  3114. return LSM6DSO_OK;
  3115. }
  3116. /**
  3117. * @}
  3118. */
  3119. /** @defgroup LSM6DSO_Private_Functions LSM6DSO Private Functions
  3120. * @{
  3121. */
  3122. /**
  3123. * @brief Set the LSM6DSO accelerometer sensor output data rate when enabled
  3124. * @param pObj the device pObj
  3125. * @param Odr the functional output data rate to be set
  3126. * @retval 0 in case of success, an error code otherwise
  3127. */
  3128. static int32_t LSM6DSO_ACC_SetOutputDataRate_When_Enabled(LSM6DSO_Object_t *pObj, float_t Odr)
  3129. {
  3130. lsm6dso_odr_xl_t new_odr;
  3131. new_odr = (Odr <= 1.6f) ? LSM6DSO_XL_ODR_1Hz6
  3132. : (Odr <= 12.5f) ? LSM6DSO_XL_ODR_12Hz5
  3133. : (Odr <= 26.0f) ? LSM6DSO_XL_ODR_26Hz
  3134. : (Odr <= 52.0f) ? LSM6DSO_XL_ODR_52Hz
  3135. : (Odr <= 104.0f) ? LSM6DSO_XL_ODR_104Hz
  3136. : (Odr <= 208.0f) ? LSM6DSO_XL_ODR_208Hz
  3137. : (Odr <= 417.0f) ? LSM6DSO_XL_ODR_417Hz
  3138. : (Odr <= 833.0f) ? LSM6DSO_XL_ODR_833Hz
  3139. : (Odr <= 1667.0f) ? LSM6DSO_XL_ODR_1667Hz
  3140. : (Odr <= 3333.0f) ? LSM6DSO_XL_ODR_3333Hz
  3141. : LSM6DSO_XL_ODR_6667Hz;
  3142. /* Output data rate selection. */
  3143. if (lsm6dso_xl_data_rate_set(&(pObj->Ctx), new_odr) != LSM6DSO_OK)
  3144. {
  3145. return LSM6DSO_ERROR;
  3146. }
  3147. return LSM6DSO_OK;
  3148. }
  3149. /**
  3150. * @brief Set the LSM6DSO accelerometer sensor output data rate when disabled
  3151. * @param pObj the device pObj
  3152. * @param Odr the functional output data rate to be set
  3153. * @retval 0 in case of success, an error code otherwise
  3154. */
  3155. static int32_t LSM6DSO_ACC_SetOutputDataRate_When_Disabled(LSM6DSO_Object_t *pObj, float_t Odr)
  3156. {
  3157. pObj->acc_odr = (Odr <= 1.6f) ? LSM6DSO_XL_ODR_1Hz6
  3158. : (Odr <= 12.5f) ? LSM6DSO_XL_ODR_12Hz5
  3159. : (Odr <= 26.0f) ? LSM6DSO_XL_ODR_26Hz
  3160. : (Odr <= 52.0f) ? LSM6DSO_XL_ODR_52Hz
  3161. : (Odr <= 104.0f) ? LSM6DSO_XL_ODR_104Hz
  3162. : (Odr <= 208.0f) ? LSM6DSO_XL_ODR_208Hz
  3163. : (Odr <= 417.0f) ? LSM6DSO_XL_ODR_417Hz
  3164. : (Odr <= 833.0f) ? LSM6DSO_XL_ODR_833Hz
  3165. : (Odr <= 1667.0f) ? LSM6DSO_XL_ODR_1667Hz
  3166. : (Odr <= 3333.0f) ? LSM6DSO_XL_ODR_3333Hz
  3167. : LSM6DSO_XL_ODR_6667Hz;
  3168. return LSM6DSO_OK;
  3169. }
  3170. /**
  3171. * @brief Set the LSM6DSO gyroscope sensor output data rate when enabled
  3172. * @param pObj the device pObj
  3173. * @param Odr the functional output data rate to be set
  3174. * @retval 0 in case of success, an error code otherwise
  3175. */
  3176. static int32_t LSM6DSO_GYRO_SetOutputDataRate_When_Enabled(LSM6DSO_Object_t *pObj, float_t Odr)
  3177. {
  3178. lsm6dso_odr_g_t new_odr;
  3179. new_odr = (Odr <= 12.5f) ? LSM6DSO_GY_ODR_12Hz5
  3180. : (Odr <= 26.0f) ? LSM6DSO_GY_ODR_26Hz
  3181. : (Odr <= 52.0f) ? LSM6DSO_GY_ODR_52Hz
  3182. : (Odr <= 104.0f) ? LSM6DSO_GY_ODR_104Hz
  3183. : (Odr <= 208.0f) ? LSM6DSO_GY_ODR_208Hz
  3184. : (Odr <= 417.0f) ? LSM6DSO_GY_ODR_417Hz
  3185. : (Odr <= 833.0f) ? LSM6DSO_GY_ODR_833Hz
  3186. : (Odr <= 1667.0f) ? LSM6DSO_GY_ODR_1667Hz
  3187. : (Odr <= 3333.0f) ? LSM6DSO_GY_ODR_3333Hz
  3188. : LSM6DSO_GY_ODR_6667Hz;
  3189. /* Output data rate selection. */
  3190. if (lsm6dso_gy_data_rate_set(&(pObj->Ctx), new_odr) != LSM6DSO_OK)
  3191. {
  3192. return LSM6DSO_ERROR;
  3193. }
  3194. return LSM6DSO_OK;
  3195. }
  3196. /**
  3197. * @brief Set the LSM6DSO gyroscope sensor output data rate when disabled
  3198. * @param pObj the device pObj
  3199. * @param Odr the functional output data rate to be set
  3200. * @retval 0 in case of success, an error code otherwise
  3201. */
  3202. static int32_t LSM6DSO_GYRO_SetOutputDataRate_When_Disabled(LSM6DSO_Object_t *pObj, float_t Odr)
  3203. {
  3204. pObj->gyro_odr = (Odr <= 12.5f) ? LSM6DSO_GY_ODR_12Hz5
  3205. : (Odr <= 26.0f) ? LSM6DSO_GY_ODR_26Hz
  3206. : (Odr <= 52.0f) ? LSM6DSO_GY_ODR_52Hz
  3207. : (Odr <= 104.0f) ? LSM6DSO_GY_ODR_104Hz
  3208. : (Odr <= 208.0f) ? LSM6DSO_GY_ODR_208Hz
  3209. : (Odr <= 417.0f) ? LSM6DSO_GY_ODR_417Hz
  3210. : (Odr <= 833.0f) ? LSM6DSO_GY_ODR_833Hz
  3211. : (Odr <= 1667.0f) ? LSM6DSO_GY_ODR_1667Hz
  3212. : (Odr <= 3333.0f) ? LSM6DSO_GY_ODR_3333Hz
  3213. : LSM6DSO_GY_ODR_6667Hz;
  3214. return LSM6DSO_OK;
  3215. }
  3216. /**
  3217. * @brief Wrap Read register component function to Bus IO function
  3218. * @param Handle the device handler
  3219. * @param Reg the register address
  3220. * @param pData the stored data pointer
  3221. * @param Length the length
  3222. * @retval 0 in case of success, an error code otherwise
  3223. */
  3224. static int32_t ReadRegWrap(void *Handle, uint8_t Reg, uint8_t *pData, uint16_t Length)
  3225. {
  3226. LSM6DSO_Object_t *pObj = (LSM6DSO_Object_t *)Handle;
  3227. return pObj->IO.ReadReg(pObj->IO.Address, Reg, pData, Length);
  3228. }
  3229. /**
  3230. * @brief Wrap Write register component function to Bus IO function
  3231. * @param Handle the device handler
  3232. * @param Reg the register address
  3233. * @param pData the stored data pointer
  3234. * @param Length the length
  3235. * @retval 0 in case of success, an error code otherwise
  3236. */
  3237. static int32_t WriteRegWrap(void *Handle, uint8_t Reg, uint8_t *pData, uint16_t Length)
  3238. {
  3239. LSM6DSO_Object_t *pObj = (LSM6DSO_Object_t *)Handle;
  3240. return pObj->IO.WriteReg(pObj->IO.Address, Reg, pData, Length);
  3241. }
  3242. /**
  3243. * @}
  3244. */
  3245. /**
  3246. * @}
  3247. */
  3248. /**
  3249. * @}
  3250. */
  3251. /**
  3252. * @}
  3253. */
  3254. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/