lsm6dso.c 93 KB

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