lsm6dsl.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144
  1. /**
  2. ******************************************************************************
  3. * @file lsm6dsl.c
  4. * @author MEMS Software Solutions Team
  5. * @brief LSM6DSL driver file
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; COPYRIGHT(c) 2018 STMicroelectronics</center></h2>
  10. *
  11. * Redistribution and use in source and binary forms, with or without modification,
  12. * are permitted provided that the following conditions are met:
  13. * 1. Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  28. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSIBILITY OF SUCH DAMAGE.
  32. *
  33. ******************************************************************************
  34. */
  35. /* Includes ------------------------------------------------------------------*/
  36. #include "lsm6dsl.h"
  37. /** @addtogroup BSP BSP
  38. * @{
  39. */
  40. /** @addtogroup Component Component
  41. * @{
  42. */
  43. /** @defgroup LSM6DSL LSM6DSL
  44. * @{
  45. */
  46. /** @defgroup LSM6DSL_Exported_Variables LSM6DSL Exported Variables
  47. * @{
  48. */
  49. LSM6DSL_CommonDrv_t LSM6DSL_COMMON_Driver =
  50. {
  51. LSM6DSL_Init,
  52. LSM6DSL_DeInit,
  53. LSM6DSL_ReadID,
  54. LSM6DSL_GetCapabilities,
  55. };
  56. LSM6DSL_ACC_Drv_t LSM6DSL_ACC_Driver =
  57. {
  58. LSM6DSL_ACC_Enable,
  59. LSM6DSL_ACC_Disable,
  60. LSM6DSL_ACC_GetSensitivity,
  61. LSM6DSL_ACC_GetOutputDataRate,
  62. LSM6DSL_ACC_SetOutputDataRate,
  63. LSM6DSL_ACC_GetFullScale,
  64. LSM6DSL_ACC_SetFullScale,
  65. LSM6DSL_ACC_GetAxes,
  66. LSM6DSL_ACC_GetAxesRaw,
  67. };
  68. LSM6DSL_GYRO_Drv_t LSM6DSL_GYRO_Driver =
  69. {
  70. LSM6DSL_GYRO_Enable,
  71. LSM6DSL_GYRO_Disable,
  72. LSM6DSL_GYRO_GetSensitivity,
  73. LSM6DSL_GYRO_GetOutputDataRate,
  74. LSM6DSL_GYRO_SetOutputDataRate,
  75. LSM6DSL_GYRO_GetFullScale,
  76. LSM6DSL_GYRO_SetFullScale,
  77. LSM6DSL_GYRO_GetAxes,
  78. LSM6DSL_GYRO_GetAxesRaw,
  79. };
  80. /**
  81. * @}
  82. */
  83. /** @defgroup LSM6DSL_Private_Function_Prototypes LSM6DSL Private Function Prototypes
  84. * @{
  85. */
  86. static int32_t ReadRegWrap(void *Handle, uint8_t Reg, uint8_t *pData, uint16_t Length);
  87. static int32_t WriteRegWrap(void *Handle, uint8_t Reg, uint8_t *pData, uint16_t Length);
  88. static int32_t LSM6DSL_ACC_SetOutputDataRate_When_Enabled(LSM6DSL_Object_t *pObj, float Odr);
  89. static int32_t LSM6DSL_ACC_SetOutputDataRate_When_Disabled(LSM6DSL_Object_t *pObj, float Odr);
  90. static int32_t LSM6DSL_GYRO_SetOutputDataRate_When_Enabled(LSM6DSL_Object_t *pObj, float Odr);
  91. static int32_t LSM6DSL_GYRO_SetOutputDataRate_When_Disabled(LSM6DSL_Object_t *pObj, float Odr);
  92. /**
  93. * @}
  94. */
  95. /** @defgroup LSM6DSL_Exported_Functions LSM6DSL Exported Functions
  96. * @{
  97. */
  98. /**
  99. * @brief Register Component Bus IO operations
  100. * @param pObj the device pObj
  101. * @retval 0 in case of success, an error code otherwise
  102. */
  103. int32_t LSM6DSL_RegisterBusIO(LSM6DSL_Object_t *pObj, LSM6DSL_IO_t *pIO)
  104. {
  105. int32_t ret = LSM6DSL_OK;
  106. if (pObj == NULL)
  107. {
  108. ret = LSM6DSL_ERROR;
  109. }
  110. else
  111. {
  112. pObj->IO.Init = pIO->Init;
  113. pObj->IO.DeInit = pIO->DeInit;
  114. pObj->IO.BusType = pIO->BusType;
  115. pObj->IO.Address = pIO->Address;
  116. pObj->IO.WriteReg = pIO->WriteReg;
  117. pObj->IO.ReadReg = pIO->ReadReg;
  118. pObj->IO.GetTick = pIO->GetTick;
  119. pObj->Ctx.read_reg = ReadRegWrap;
  120. pObj->Ctx.write_reg = WriteRegWrap;
  121. pObj->Ctx.handle = pObj;
  122. if (pObj->IO.Init == NULL)
  123. {
  124. ret = LSM6DSL_ERROR;
  125. }
  126. else if (pObj->IO.Init() != LSM6DSL_OK)
  127. {
  128. ret = LSM6DSL_ERROR;
  129. }
  130. else
  131. {
  132. if (pObj->IO.BusType == LSM6DSL_SPI_3WIRES_BUS) /* SPI 3-Wires */
  133. {
  134. /* Enable the SPI 3-Wires support only the first time */
  135. if (pObj->is_initialized == 0U)
  136. {
  137. /* Enable SPI 3-Wires on the component */
  138. uint8_t data = 0x0C;
  139. if (LSM6DSL_Write_Reg(pObj, LSM6DSL_CTRL3_C, data) != LSM6DSL_OK)
  140. {
  141. ret = LSM6DSL_ERROR;
  142. }
  143. }
  144. }
  145. }
  146. }
  147. return ret;
  148. }
  149. /**
  150. * @brief Initialize the LSM6DSL sensor
  151. * @param pObj the device pObj
  152. * @retval 0 in case of success, an error code otherwise
  153. */
  154. int32_t LSM6DSL_Init(LSM6DSL_Object_t *pObj)
  155. {
  156. /* Enable register address automatically incremented during a multiple byte
  157. access with a serial interface. */
  158. if (lsm6dsl_auto_increment_set(&(pObj->Ctx), PROPERTY_ENABLE) != LSM6DSL_OK)
  159. {
  160. return LSM6DSL_ERROR;
  161. }
  162. /* Enable BDU */
  163. if (lsm6dsl_block_data_update_set(&(pObj->Ctx), PROPERTY_ENABLE) != LSM6DSL_OK)
  164. {
  165. return LSM6DSL_ERROR;
  166. }
  167. /* FIFO mode selection */
  168. if (lsm6dsl_fifo_mode_set(&(pObj->Ctx), LSM6DSL_BYPASS_MODE) != LSM6DSL_OK)
  169. {
  170. return LSM6DSL_ERROR;
  171. }
  172. /* Select default output data rate. */
  173. pObj->acc_odr = LSM6DSL_XL_ODR_104Hz;
  174. /* Output data rate selection - power down. */
  175. if (lsm6dsl_xl_data_rate_set(&(pObj->Ctx), LSM6DSL_XL_ODR_OFF) != LSM6DSL_OK)
  176. {
  177. return LSM6DSL_ERROR;
  178. }
  179. /* Full scale selection. */
  180. if (lsm6dsl_xl_full_scale_set(&(pObj->Ctx), LSM6DSL_2g) != LSM6DSL_OK)
  181. {
  182. return LSM6DSL_ERROR;
  183. }
  184. /* Select default output data rate. */
  185. pObj->gyro_odr = LSM6DSL_GY_ODR_104Hz;
  186. /* Output data rate selection - power down. */
  187. if (lsm6dsl_gy_data_rate_set(&(pObj->Ctx), LSM6DSL_GY_ODR_OFF) != LSM6DSL_OK)
  188. {
  189. return LSM6DSL_ERROR;
  190. }
  191. /* Full scale selection. */
  192. if (lsm6dsl_gy_full_scale_set(&(pObj->Ctx), LSM6DSL_2000dps) != LSM6DSL_OK)
  193. {
  194. return LSM6DSL_ERROR;
  195. }
  196. pObj->is_initialized = 1;
  197. return LSM6DSL_OK;
  198. }
  199. /**
  200. * @brief Deinitialize the LSM6DSL sensor
  201. * @param pObj the device pObj
  202. * @retval 0 in case of success, an error code otherwise
  203. */
  204. int32_t LSM6DSL_DeInit(LSM6DSL_Object_t *pObj)
  205. {
  206. /* Disable the component */
  207. if (LSM6DSL_ACC_Disable(pObj) != LSM6DSL_OK)
  208. {
  209. return LSM6DSL_ERROR;
  210. }
  211. if (LSM6DSL_GYRO_Disable(pObj) != LSM6DSL_OK)
  212. {
  213. return LSM6DSL_ERROR;
  214. }
  215. /* Reset output data rate. */
  216. pObj->acc_odr = LSM6DSL_XL_ODR_OFF;
  217. pObj->gyro_odr = LSM6DSL_GY_ODR_OFF;
  218. pObj->is_initialized = 0;
  219. return LSM6DSL_OK;
  220. }
  221. /**
  222. * @brief Read component ID
  223. * @param pObj the device pObj
  224. * @param Id the WHO_AM_I value
  225. * @retval 0 in case of success, an error code otherwise
  226. */
  227. int32_t LSM6DSL_ReadID(LSM6DSL_Object_t *pObj, uint8_t *Id)
  228. {
  229. if (lsm6dsl_device_id_get(&(pObj->Ctx), Id) != LSM6DSL_OK)
  230. {
  231. return LSM6DSL_ERROR;
  232. }
  233. return LSM6DSL_OK;
  234. }
  235. /**
  236. * @brief Get LSM6DSL sensor capabilities
  237. * @param pObj Component object pointer
  238. * @param Capabilities pointer to LSM6DSL sensor capabilities
  239. * @retval 0 in case of success, an error code otherwise
  240. */
  241. int32_t LSM6DSL_GetCapabilities(LSM6DSL_Object_t *pObj, LSM6DSL_Capabilities_t *Capabilities)
  242. {
  243. /* Prevent unused argument(s) compilation warning */
  244. (void)(pObj);
  245. Capabilities->Acc = 1;
  246. Capabilities->Gyro = 1;
  247. Capabilities->Magneto = 0;
  248. Capabilities->LowPower = 0;
  249. Capabilities->GyroMaxFS = 2000;
  250. Capabilities->AccMaxFS = 16;
  251. Capabilities->MagMaxFS = 0;
  252. Capabilities->GyroMaxOdr = 6660.0f;
  253. Capabilities->AccMaxOdr = 6660.0f;
  254. Capabilities->MagMaxOdr = 0.0f;
  255. return LSM6DSL_OK;
  256. }
  257. /**
  258. * @brief Enable the LSM6DSL accelerometer sensor
  259. * @param pObj the device pObj
  260. * @retval 0 in case of success, an error code otherwise
  261. */
  262. int32_t LSM6DSL_ACC_Enable(LSM6DSL_Object_t *pObj)
  263. {
  264. /* Check if the component is already enabled */
  265. if (pObj->acc_is_enabled == 1U)
  266. {
  267. return LSM6DSL_OK;
  268. }
  269. /* Output data rate selection. */
  270. if (lsm6dsl_xl_data_rate_set(&(pObj->Ctx), pObj->acc_odr) != LSM6DSL_OK)
  271. {
  272. return LSM6DSL_ERROR;
  273. }
  274. pObj->acc_is_enabled = 1;
  275. return LSM6DSL_OK;
  276. }
  277. /**
  278. * @brief Disable the LSM6DSL accelerometer sensor
  279. * @param pObj the device pObj
  280. * @retval 0 in case of success, an error code otherwise
  281. */
  282. int32_t LSM6DSL_ACC_Disable(LSM6DSL_Object_t *pObj)
  283. {
  284. /* Check if the component is already disabled */
  285. if (pObj->acc_is_enabled == 0U)
  286. {
  287. return LSM6DSL_OK;
  288. }
  289. /* Get current output data rate. */
  290. if (lsm6dsl_xl_data_rate_get(&(pObj->Ctx), &pObj->acc_odr) != LSM6DSL_OK)
  291. {
  292. return LSM6DSL_ERROR;
  293. }
  294. /* Output data rate selection - power down. */
  295. if (lsm6dsl_xl_data_rate_set(&(pObj->Ctx), LSM6DSL_XL_ODR_OFF) != LSM6DSL_OK)
  296. {
  297. return LSM6DSL_ERROR;
  298. }
  299. pObj->acc_is_enabled = 0;
  300. return LSM6DSL_OK;
  301. }
  302. /**
  303. * @brief Get the LSM6DSL accelerometer sensor sensitivity
  304. * @param pObj the device pObj
  305. * @param Sensitivity pointer
  306. * @retval 0 in case of success, an error code otherwise
  307. */
  308. int32_t LSM6DSL_ACC_GetSensitivity(LSM6DSL_Object_t *pObj, float *Sensitivity)
  309. {
  310. int32_t ret = LSM6DSL_OK;
  311. lsm6dsl_fs_xl_t full_scale;
  312. /* Read actual full scale selection from sensor. */
  313. if (lsm6dsl_xl_full_scale_get(&(pObj->Ctx), &full_scale) != LSM6DSL_OK)
  314. {
  315. return LSM6DSL_ERROR;
  316. }
  317. /* Store the Sensitivity based on actual full scale. */
  318. switch (full_scale)
  319. {
  320. case LSM6DSL_2g:
  321. *Sensitivity = LSM6DSL_ACC_SENSITIVITY_FS_2G;
  322. break;
  323. case LSM6DSL_4g:
  324. *Sensitivity = LSM6DSL_ACC_SENSITIVITY_FS_4G;
  325. break;
  326. case LSM6DSL_8g:
  327. *Sensitivity = LSM6DSL_ACC_SENSITIVITY_FS_8G;
  328. break;
  329. case LSM6DSL_16g:
  330. *Sensitivity = LSM6DSL_ACC_SENSITIVITY_FS_16G;
  331. break;
  332. default:
  333. ret = LSM6DSL_ERROR;
  334. break;
  335. }
  336. return ret;
  337. }
  338. /**
  339. * @brief Get the LSM6DSL accelerometer sensor output data rate
  340. * @param pObj the device pObj
  341. * @param Odr pointer where the output data rate is written
  342. * @retval 0 in case of success, an error code otherwise
  343. */
  344. int32_t LSM6DSL_ACC_GetOutputDataRate(LSM6DSL_Object_t *pObj, float *Odr)
  345. {
  346. int32_t ret = LSM6DSL_OK;
  347. lsm6dsl_odr_xl_t odr_low_level;
  348. /* Get current output data rate. */
  349. if (lsm6dsl_xl_data_rate_get(&(pObj->Ctx), &odr_low_level) != LSM6DSL_OK)
  350. {
  351. return LSM6DSL_ERROR;
  352. }
  353. switch (odr_low_level)
  354. {
  355. case LSM6DSL_XL_ODR_OFF:
  356. *Odr = 0.0f;
  357. break;
  358. case LSM6DSL_XL_ODR_1Hz6:
  359. *Odr = 1.6f;
  360. break;
  361. case LSM6DSL_XL_ODR_12Hz5:
  362. *Odr = 12.5f;
  363. break;
  364. case LSM6DSL_XL_ODR_26Hz:
  365. *Odr = 26.0f;
  366. break;
  367. case LSM6DSL_XL_ODR_52Hz:
  368. *Odr = 52.0f;
  369. break;
  370. case LSM6DSL_XL_ODR_104Hz:
  371. *Odr = 104.0f;
  372. break;
  373. case LSM6DSL_XL_ODR_208Hz:
  374. *Odr = 208.0f;
  375. break;
  376. case LSM6DSL_XL_ODR_416Hz:
  377. *Odr = 416.0f;
  378. break;
  379. case LSM6DSL_XL_ODR_833Hz:
  380. *Odr = 833.0f;
  381. break;
  382. case LSM6DSL_XL_ODR_1k66Hz:
  383. *Odr = 1660.0f;
  384. break;
  385. case LSM6DSL_XL_ODR_3k33Hz:
  386. *Odr = 3330.0f;
  387. break;
  388. case LSM6DSL_XL_ODR_6k66Hz:
  389. *Odr = 6660.0f;
  390. break;
  391. default:
  392. ret = LSM6DSL_ERROR;
  393. break;
  394. }
  395. return ret;
  396. }
  397. /**
  398. * @brief Set the LSM6DSL accelerometer sensor output data rate
  399. * @param pObj the device pObj
  400. * @param Odr the output data rate value to be set
  401. * @retval 0 in case of success, an error code otherwise
  402. */
  403. int32_t LSM6DSL_ACC_SetOutputDataRate(LSM6DSL_Object_t *pObj, float Odr)
  404. {
  405. /* Check if the component is enabled */
  406. if (pObj->acc_is_enabled == 1U)
  407. {
  408. return LSM6DSL_ACC_SetOutputDataRate_When_Enabled(pObj, Odr);
  409. }
  410. else
  411. {
  412. return LSM6DSL_ACC_SetOutputDataRate_When_Disabled(pObj, Odr);
  413. }
  414. }
  415. /**
  416. * @brief Get the LSM6DSL accelerometer sensor full scale
  417. * @param pObj the device pObj
  418. * @param FullScale pointer where the full scale is written
  419. * @retval 0 in case of success, an error code otherwise
  420. */
  421. int32_t LSM6DSL_ACC_GetFullScale(LSM6DSL_Object_t *pObj, int32_t *FullScale)
  422. {
  423. int32_t ret = LSM6DSL_OK;
  424. lsm6dsl_fs_xl_t fs_low_level;
  425. /* Read actual full scale selection from sensor. */
  426. if (lsm6dsl_xl_full_scale_get(&(pObj->Ctx), &fs_low_level) != LSM6DSL_OK)
  427. {
  428. return LSM6DSL_ERROR;
  429. }
  430. switch (fs_low_level)
  431. {
  432. case LSM6DSL_2g:
  433. *FullScale = 2;
  434. break;
  435. case LSM6DSL_4g:
  436. *FullScale = 4;
  437. break;
  438. case LSM6DSL_8g:
  439. *FullScale = 8;
  440. break;
  441. case LSM6DSL_16g:
  442. *FullScale = 16;
  443. break;
  444. default:
  445. ret = LSM6DSL_ERROR;
  446. break;
  447. }
  448. return ret;
  449. }
  450. /**
  451. * @brief Set the LSM6DSL accelerometer sensor full scale
  452. * @param pObj the device pObj
  453. * @param FullScale the functional full scale to be set
  454. * @retval 0 in case of success, an error code otherwise
  455. */
  456. int32_t LSM6DSL_ACC_SetFullScale(LSM6DSL_Object_t *pObj, int32_t FullScale)
  457. {
  458. lsm6dsl_fs_xl_t new_fs;
  459. /* Seems like MISRA C-2012 rule 14.3a violation but only from single file statical analysis point of view because
  460. the parameter passed to the function is not known at the moment of analysis */
  461. new_fs = (FullScale <= 2) ? LSM6DSL_2g
  462. : (FullScale <= 4) ? LSM6DSL_4g
  463. : (FullScale <= 8) ? LSM6DSL_8g
  464. : LSM6DSL_16g;
  465. if (lsm6dsl_xl_full_scale_set(&(pObj->Ctx), new_fs) != LSM6DSL_OK)
  466. {
  467. return LSM6DSL_ERROR;
  468. }
  469. return LSM6DSL_OK;
  470. }
  471. /**
  472. * @brief Get the LSM6DSL accelerometer sensor raw axes
  473. * @param pObj the device pObj
  474. * @param Value pointer where the raw values of the axes are written
  475. * @retval 0 in case of success, an error code otherwise
  476. */
  477. int32_t LSM6DSL_ACC_GetAxesRaw(LSM6DSL_Object_t *pObj, LSM6DSL_AxesRaw_t *Value)
  478. {
  479. axis3bit16_t data_raw;
  480. /* Read raw data values. */
  481. if (lsm6dsl_acceleration_raw_get(&(pObj->Ctx), data_raw.u8bit) != LSM6DSL_OK)
  482. {
  483. return LSM6DSL_ERROR;
  484. }
  485. /* Format the data. */
  486. Value->x = data_raw.i16bit[0];
  487. Value->y = data_raw.i16bit[1];
  488. Value->z = data_raw.i16bit[2];
  489. return LSM6DSL_OK;
  490. }
  491. /**
  492. * @brief Get the LSM6DSL accelerometer sensor axes
  493. * @param pObj the device pObj
  494. * @param Acceleration pointer where the values of the axes are written
  495. * @retval 0 in case of success, an error code otherwise
  496. */
  497. int32_t LSM6DSL_ACC_GetAxes(LSM6DSL_Object_t *pObj, LSM6DSL_Axes_t *Acceleration)
  498. {
  499. axis3bit16_t data_raw;
  500. float sensitivity = 0.0f;
  501. /* Read raw data values. */
  502. if (lsm6dsl_acceleration_raw_get(&(pObj->Ctx), data_raw.u8bit) != LSM6DSL_OK)
  503. {
  504. return LSM6DSL_ERROR;
  505. }
  506. /* Get LSM6DSL actual sensitivity. */
  507. if (LSM6DSL_ACC_GetSensitivity(pObj, &sensitivity) != LSM6DSL_OK)
  508. {
  509. return LSM6DSL_ERROR;
  510. }
  511. /* Calculate the data. */
  512. Acceleration->x = (int32_t)((float)((float)data_raw.i16bit[0] * sensitivity));
  513. Acceleration->y = (int32_t)((float)((float)data_raw.i16bit[1] * sensitivity));
  514. Acceleration->z = (int32_t)((float)((float)data_raw.i16bit[2] * sensitivity));
  515. return LSM6DSL_OK;
  516. }
  517. /**
  518. * @brief Enable the LSM6DSL gyroscope sensor
  519. * @param pObj the device pObj
  520. * @retval 0 in case of success, an error code otherwise
  521. */
  522. int32_t LSM6DSL_GYRO_Enable(LSM6DSL_Object_t *pObj)
  523. {
  524. /* Check if the component is already enabled */
  525. if (pObj->gyro_is_enabled == 1U)
  526. {
  527. return LSM6DSL_OK;
  528. }
  529. /* Output data rate selection. */
  530. if (lsm6dsl_gy_data_rate_set(&(pObj->Ctx), pObj->gyro_odr) != LSM6DSL_OK)
  531. {
  532. return LSM6DSL_ERROR;
  533. }
  534. pObj->gyro_is_enabled = 1;
  535. return LSM6DSL_OK;
  536. }
  537. /**
  538. * @brief Disable the LSM6DSL gyroscope sensor
  539. * @param pObj the device pObj
  540. * @retval 0 in case of success, an error code otherwise
  541. */
  542. int32_t LSM6DSL_GYRO_Disable(LSM6DSL_Object_t *pObj)
  543. {
  544. /* Check if the component is already disabled */
  545. if (pObj->gyro_is_enabled == 0U)
  546. {
  547. return LSM6DSL_OK;
  548. }
  549. /* Get current output data rate. */
  550. if (lsm6dsl_gy_data_rate_get(&(pObj->Ctx), &pObj->gyro_odr) != LSM6DSL_OK)
  551. {
  552. return LSM6DSL_ERROR;
  553. }
  554. /* Output data rate selection - power down. */
  555. if (lsm6dsl_gy_data_rate_set(&(pObj->Ctx), LSM6DSL_GY_ODR_OFF) != LSM6DSL_OK)
  556. {
  557. return LSM6DSL_ERROR;
  558. }
  559. pObj->gyro_is_enabled = 0;
  560. return LSM6DSL_OK;
  561. }
  562. /**
  563. * @brief Get the LSM6DSL gyroscope sensor sensitivity
  564. * @param pObj the device pObj
  565. * @param Sensitivity pointer
  566. * @retval 0 in case of success, an error code otherwise
  567. */
  568. int32_t LSM6DSL_GYRO_GetSensitivity(LSM6DSL_Object_t *pObj, float *Sensitivity)
  569. {
  570. int32_t ret = LSM6DSL_OK;
  571. lsm6dsl_fs_g_t full_scale;
  572. /* Read actual full scale selection from sensor. */
  573. if (lsm6dsl_gy_full_scale_get(&(pObj->Ctx), &full_scale) != LSM6DSL_OK)
  574. {
  575. return LSM6DSL_ERROR;
  576. }
  577. /* Store the sensitivity based on actual full scale. */
  578. switch (full_scale)
  579. {
  580. case LSM6DSL_125dps:
  581. *Sensitivity = LSM6DSL_GYRO_SENSITIVITY_FS_125DPS;
  582. break;
  583. case LSM6DSL_250dps:
  584. *Sensitivity = LSM6DSL_GYRO_SENSITIVITY_FS_250DPS;
  585. break;
  586. case LSM6DSL_500dps:
  587. *Sensitivity = LSM6DSL_GYRO_SENSITIVITY_FS_500DPS;
  588. break;
  589. case LSM6DSL_1000dps:
  590. *Sensitivity = LSM6DSL_GYRO_SENSITIVITY_FS_1000DPS;
  591. break;
  592. case LSM6DSL_2000dps:
  593. *Sensitivity = LSM6DSL_GYRO_SENSITIVITY_FS_2000DPS;
  594. break;
  595. default:
  596. ret = LSM6DSL_ERROR;
  597. break;
  598. }
  599. return ret;
  600. }
  601. /**
  602. * @brief Get the LSM6DSL gyroscope sensor output data rate
  603. * @param pObj the device pObj
  604. * @param Odr pointer where the output data rate is written
  605. * @retval 0 in case of success, an error code otherwise
  606. */
  607. int32_t LSM6DSL_GYRO_GetOutputDataRate(LSM6DSL_Object_t *pObj, float *Odr)
  608. {
  609. int32_t ret = LSM6DSL_OK;
  610. lsm6dsl_odr_g_t odr_low_level;
  611. /* Get current output data rate. */
  612. if (lsm6dsl_gy_data_rate_get(&(pObj->Ctx), &odr_low_level) != LSM6DSL_OK)
  613. {
  614. return LSM6DSL_ERROR;
  615. }
  616. switch (odr_low_level)
  617. {
  618. case LSM6DSL_GY_ODR_OFF:
  619. *Odr = 0.0f;
  620. break;
  621. case LSM6DSL_GY_ODR_12Hz5:
  622. *Odr = 12.5f;
  623. break;
  624. case LSM6DSL_GY_ODR_26Hz:
  625. *Odr = 26.0f;
  626. break;
  627. case LSM6DSL_GY_ODR_52Hz:
  628. *Odr = 52.0f;
  629. break;
  630. case LSM6DSL_GY_ODR_104Hz:
  631. *Odr = 104.0f;
  632. break;
  633. case LSM6DSL_GY_ODR_208Hz:
  634. *Odr = 208.0f;
  635. break;
  636. case LSM6DSL_GY_ODR_416Hz:
  637. *Odr = 416.0f;
  638. break;
  639. case LSM6DSL_GY_ODR_833Hz:
  640. *Odr = 833.0f;
  641. break;
  642. case LSM6DSL_GY_ODR_1k66Hz:
  643. *Odr = 1660.0f;
  644. break;
  645. case LSM6DSL_GY_ODR_3k33Hz:
  646. *Odr = 3330.0f;
  647. break;
  648. case LSM6DSL_GY_ODR_6k66Hz:
  649. *Odr = 6660.0f;
  650. break;
  651. default:
  652. ret = LSM6DSL_ERROR;
  653. break;
  654. }
  655. return ret;
  656. }
  657. /**
  658. * @brief Set the LSM6DSL gyroscope sensor output data rate
  659. * @param pObj the device pObj
  660. * @param Odr the output data rate value to be set
  661. * @retval 0 in case of success, an error code otherwise
  662. */
  663. int32_t LSM6DSL_GYRO_SetOutputDataRate(LSM6DSL_Object_t *pObj, float Odr)
  664. {
  665. /* Check if the component is enabled */
  666. if (pObj->gyro_is_enabled == 1U)
  667. {
  668. return LSM6DSL_GYRO_SetOutputDataRate_When_Enabled(pObj, Odr);
  669. }
  670. else
  671. {
  672. return LSM6DSL_GYRO_SetOutputDataRate_When_Disabled(pObj, Odr);
  673. }
  674. }
  675. /**
  676. * @brief Get the LSM6DSL gyroscope sensor full scale
  677. * @param pObj the device pObj
  678. * @param FullScale pointer where the full scale is written
  679. * @retval 0 in case of success, an error code otherwise
  680. */
  681. int32_t LSM6DSL_GYRO_GetFullScale(LSM6DSL_Object_t *pObj, int32_t *FullScale)
  682. {
  683. int32_t ret = LSM6DSL_OK;
  684. lsm6dsl_fs_g_t fs_low_level;
  685. /* Read actual full scale selection from sensor. */
  686. if (lsm6dsl_gy_full_scale_get(&(pObj->Ctx), &fs_low_level) != LSM6DSL_OK)
  687. {
  688. return LSM6DSL_ERROR;
  689. }
  690. switch (fs_low_level)
  691. {
  692. case LSM6DSL_125dps:
  693. *FullScale = 125;
  694. break;
  695. case LSM6DSL_250dps:
  696. *FullScale = 250;
  697. break;
  698. case LSM6DSL_500dps:
  699. *FullScale = 500;
  700. break;
  701. case LSM6DSL_1000dps:
  702. *FullScale = 1000;
  703. break;
  704. case LSM6DSL_2000dps:
  705. *FullScale = 2000;
  706. break;
  707. default:
  708. ret = LSM6DSL_ERROR;
  709. break;
  710. }
  711. return ret;
  712. }
  713. /**
  714. * @brief Set the LSM6DSL gyroscope sensor full scale
  715. * @param pObj the device pObj
  716. * @param FullScale the functional full scale to be set
  717. * @retval 0 in case of success, an error code otherwise
  718. */
  719. int32_t LSM6DSL_GYRO_SetFullScale(LSM6DSL_Object_t *pObj, int32_t FullScale)
  720. {
  721. lsm6dsl_fs_g_t new_fs;
  722. new_fs = (FullScale <= 125) ? LSM6DSL_125dps
  723. : (FullScale <= 250) ? LSM6DSL_250dps
  724. : (FullScale <= 500) ? LSM6DSL_500dps
  725. : (FullScale <= 1000) ? LSM6DSL_1000dps
  726. : LSM6DSL_2000dps;
  727. if (lsm6dsl_gy_full_scale_set(&(pObj->Ctx), new_fs) != LSM6DSL_OK)
  728. {
  729. return LSM6DSL_ERROR;
  730. }
  731. return LSM6DSL_OK;
  732. }
  733. /**
  734. * @brief Get the LSM6DSL gyroscope sensor raw axes
  735. * @param pObj the device pObj
  736. * @param Value pointer where the raw values of the axes are written
  737. * @retval 0 in case of success, an error code otherwise
  738. */
  739. int32_t LSM6DSL_GYRO_GetAxesRaw(LSM6DSL_Object_t *pObj, LSM6DSL_AxesRaw_t *Value)
  740. {
  741. axis3bit16_t data_raw;
  742. /* Read raw data values. */
  743. if (lsm6dsl_angular_rate_raw_get(&(pObj->Ctx), data_raw.u8bit) != LSM6DSL_OK)
  744. {
  745. return LSM6DSL_ERROR;
  746. }
  747. /* Format the data. */
  748. Value->x = data_raw.i16bit[0];
  749. Value->y = data_raw.i16bit[1];
  750. Value->z = data_raw.i16bit[2];
  751. return LSM6DSL_OK;
  752. }
  753. /**
  754. * @brief Get the LSM6DSL gyroscope sensor axes
  755. * @param pObj the device pObj
  756. * @param AngularRate pointer where the values of the axes are written
  757. * @retval 0 in case of success, an error code otherwise
  758. */
  759. int32_t LSM6DSL_GYRO_GetAxes(LSM6DSL_Object_t *pObj, LSM6DSL_Axes_t *AngularRate)
  760. {
  761. axis3bit16_t data_raw;
  762. float sensitivity;
  763. /* Read raw data values. */
  764. if (lsm6dsl_angular_rate_raw_get(&(pObj->Ctx), data_raw.u8bit) != LSM6DSL_OK)
  765. {
  766. return LSM6DSL_ERROR;
  767. }
  768. /* Get LSM6DSL actual sensitivity. */
  769. if (LSM6DSL_GYRO_GetSensitivity(pObj, &sensitivity) != LSM6DSL_OK)
  770. {
  771. return LSM6DSL_ERROR;
  772. }
  773. /* Calculate the data. */
  774. AngularRate->x = (int32_t)((float)((float)data_raw.i16bit[0] * sensitivity));
  775. AngularRate->y = (int32_t)((float)((float)data_raw.i16bit[1] * sensitivity));
  776. AngularRate->z = (int32_t)((float)((float)data_raw.i16bit[2] * sensitivity));
  777. return LSM6DSL_OK;
  778. }
  779. /**
  780. * @brief Get the LSM6DSL register value
  781. * @param pObj the device pObj
  782. * @param Reg address to be read
  783. * @param Data pointer where the value is written
  784. * @retval 0 in case of success, an error code otherwise
  785. */
  786. int32_t LSM6DSL_Read_Reg(LSM6DSL_Object_t *pObj, uint8_t Reg, uint8_t *Data)
  787. {
  788. if (lsm6dsl_read_reg(&(pObj->Ctx), Reg, Data, 1) != LSM6DSL_OK)
  789. {
  790. return LSM6DSL_ERROR;
  791. }
  792. return LSM6DSL_OK;
  793. }
  794. /**
  795. * @brief Set the LSM6DSL register value
  796. * @param pObj the device pObj
  797. * @param Reg address to be written
  798. * @param Data value to be written
  799. * @retval 0 in case of success, an error code otherwise
  800. */
  801. int32_t LSM6DSL_Write_Reg(LSM6DSL_Object_t *pObj, uint8_t Reg, uint8_t Data)
  802. {
  803. if (lsm6dsl_write_reg(&(pObj->Ctx), Reg, &Data, 1) != LSM6DSL_OK)
  804. {
  805. return LSM6DSL_ERROR;
  806. }
  807. return LSM6DSL_OK;
  808. }
  809. /**
  810. * @brief Set the interrupt latch
  811. * @param pObj the device pObj
  812. * @param Status value to be written
  813. * @retval 0 in case of success, an error code otherwise
  814. */
  815. int32_t LSM6DSL_Set_Interrupt_Latch(LSM6DSL_Object_t *pObj, uint8_t Status)
  816. {
  817. if (Status > 1U)
  818. {
  819. return LSM6DSL_ERROR;
  820. }
  821. if (lsm6dsl_int_notification_set(&(pObj->Ctx), (lsm6dsl_lir_t)Status) != LSM6DSL_OK)
  822. {
  823. return LSM6DSL_ERROR;
  824. }
  825. return LSM6DSL_OK;
  826. }
  827. /**
  828. * @brief Enable free fall detection
  829. * @param pObj the device pObj
  830. * @param IntPin interrupt pin line to be used
  831. * @retval 0 in case of success, an error code otherwise
  832. */
  833. int32_t LSM6DSL_ACC_Enable_Free_Fall_Detection(LSM6DSL_Object_t *pObj, LSM6DSL_SensorIntPin_t IntPin)
  834. {
  835. int32_t ret = LSM6DSL_OK;
  836. lsm6dsl_int1_route_t val1;
  837. lsm6dsl_int2_route_t val2;
  838. /* Output Data Rate selection */
  839. if (LSM6DSL_ACC_SetOutputDataRate(pObj, 416.0f) != LSM6DSL_OK)
  840. {
  841. return LSM6DSL_ERROR;
  842. }
  843. /* Full scale selection */
  844. if (LSM6DSL_ACC_SetFullScale(pObj, 2) != LSM6DSL_OK)
  845. {
  846. return LSM6DSL_ERROR;
  847. }
  848. /* FF_DUR setting */
  849. if (lsm6dsl_ff_dur_set(&(pObj->Ctx), 0x06) != LSM6DSL_OK)
  850. {
  851. return LSM6DSL_ERROR;
  852. }
  853. /* WAKE_DUR setting */
  854. if (lsm6dsl_wkup_dur_set(&(pObj->Ctx), 0x00) != LSM6DSL_OK)
  855. {
  856. return LSM6DSL_ERROR;
  857. }
  858. /* TIMER_HR setting */
  859. if (lsm6dsl_timestamp_res_set(&(pObj->Ctx), LSM6DSL_LSB_6ms4) != LSM6DSL_OK)
  860. {
  861. return LSM6DSL_ERROR;
  862. }
  863. /* SLEEP_DUR setting */
  864. if (lsm6dsl_act_sleep_dur_set(&(pObj->Ctx), 0x00) != LSM6DSL_OK)
  865. {
  866. return LSM6DSL_ERROR;
  867. }
  868. /* FF_THS setting */
  869. if (lsm6dsl_ff_threshold_set(&(pObj->Ctx), LSM6DSL_FF_TSH_312mg) != LSM6DSL_OK)
  870. {
  871. return LSM6DSL_ERROR;
  872. }
  873. /* Enable free fall event on either INT1 or INT2 pin */
  874. switch (IntPin)
  875. {
  876. case LSM6DSL_INT1_PIN:
  877. if (lsm6dsl_pin_int1_route_get(&(pObj->Ctx), &val1) != LSM6DSL_OK)
  878. {
  879. return LSM6DSL_ERROR;
  880. }
  881. val1.int1_ff = PROPERTY_ENABLE;
  882. if (lsm6dsl_pin_int1_route_set(&(pObj->Ctx), val1) != LSM6DSL_OK)
  883. {
  884. return LSM6DSL_ERROR;
  885. }
  886. break;
  887. case LSM6DSL_INT2_PIN:
  888. if (lsm6dsl_pin_int2_route_get(&(pObj->Ctx), &val2) != LSM6DSL_OK)
  889. {
  890. return LSM6DSL_ERROR;
  891. }
  892. val2.int2_ff = PROPERTY_ENABLE;
  893. if (lsm6dsl_pin_int2_route_set(&(pObj->Ctx), val2) != LSM6DSL_OK)
  894. {
  895. return LSM6DSL_ERROR;
  896. }
  897. break;
  898. default:
  899. ret = LSM6DSL_ERROR;
  900. break;
  901. }
  902. return ret;
  903. }
  904. /**
  905. * @brief Disable free fall detection
  906. * @param pObj the device pObj
  907. * @retval 0 in case of success, an error code otherwise
  908. */
  909. int32_t LSM6DSL_ACC_Disable_Free_Fall_Detection(LSM6DSL_Object_t *pObj)
  910. {
  911. lsm6dsl_int1_route_t val1;
  912. lsm6dsl_int2_route_t val2;
  913. /* Disable free fall event on both INT1 and INT2 pins */
  914. if (lsm6dsl_pin_int1_route_get(&(pObj->Ctx), &val1) != LSM6DSL_OK)
  915. {
  916. return LSM6DSL_ERROR;
  917. }
  918. val1.int1_ff = PROPERTY_DISABLE;
  919. if (lsm6dsl_pin_int1_route_set(&(pObj->Ctx), val1) != LSM6DSL_OK)
  920. {
  921. return LSM6DSL_ERROR;
  922. }
  923. if (lsm6dsl_pin_int2_route_get(&(pObj->Ctx), &val2) != LSM6DSL_OK)
  924. {
  925. return LSM6DSL_ERROR;
  926. }
  927. val2.int2_ff = PROPERTY_DISABLE;
  928. if (lsm6dsl_pin_int2_route_set(&(pObj->Ctx), val2) != LSM6DSL_OK)
  929. {
  930. return LSM6DSL_ERROR;
  931. }
  932. /* FF_DUR setting */
  933. if (lsm6dsl_ff_dur_set(&(pObj->Ctx), 0x00) != LSM6DSL_OK)
  934. {
  935. return LSM6DSL_ERROR;
  936. }
  937. /* FF_THS setting */
  938. if (lsm6dsl_ff_threshold_set(&(pObj->Ctx), LSM6DSL_FF_TSH_156mg) != LSM6DSL_OK)
  939. {
  940. return LSM6DSL_ERROR;
  941. }
  942. return LSM6DSL_OK;
  943. }
  944. /**
  945. * @brief Set free fall threshold
  946. * @param pObj the device pObj
  947. * @param Threshold free fall detection threshold
  948. * @retval 0 in case of success, an error code otherwise
  949. */
  950. int32_t LSM6DSL_ACC_Set_Free_Fall_Threshold(LSM6DSL_Object_t *pObj, uint8_t Threshold)
  951. {
  952. if (lsm6dsl_ff_threshold_set(&(pObj->Ctx), (lsm6dsl_ff_ths_t)Threshold) != LSM6DSL_OK)
  953. {
  954. return LSM6DSL_ERROR;
  955. }
  956. return LSM6DSL_OK;
  957. }
  958. /**
  959. * @brief Set free fall duration
  960. * @param pObj the device pObj
  961. * @param Duration free fall detection duration
  962. * @retval 0 in case of success, an error code otherwise
  963. */
  964. int32_t LSM6DSL_ACC_Set_Free_Fall_Duration(LSM6DSL_Object_t *pObj, uint8_t Duration)
  965. {
  966. if (lsm6dsl_ff_dur_set(&(pObj->Ctx), Duration) != LSM6DSL_OK)
  967. {
  968. return LSM6DSL_ERROR;
  969. }
  970. return LSM6DSL_OK;
  971. }
  972. /**
  973. * @brief Enable pedometer
  974. * @param pObj the device pObj
  975. * @retval 0 in case of success, an error code otherwise
  976. */
  977. int32_t LSM6DSL_ACC_Enable_Pedometer(LSM6DSL_Object_t *pObj)
  978. {
  979. lsm6dsl_int1_route_t val;
  980. /* Output Data Rate selection */
  981. if (LSM6DSL_ACC_SetOutputDataRate(pObj, 26.0f) != LSM6DSL_OK)
  982. {
  983. return LSM6DSL_ERROR;
  984. }
  985. /* Full scale selection */
  986. if (LSM6DSL_ACC_SetFullScale(pObj, 2) != LSM6DSL_OK)
  987. {
  988. return LSM6DSL_ERROR;
  989. }
  990. /* Set pedometer threshold. */
  991. if (lsm6dsl_pedo_threshold_set(&(pObj->Ctx), 0x17) != LSM6DSL_OK)
  992. {
  993. return LSM6DSL_ERROR;
  994. }
  995. /* Enable pedometer algorithm. */
  996. if (lsm6dsl_pedo_sens_set(&(pObj->Ctx), PROPERTY_ENABLE) != LSM6DSL_OK)
  997. {
  998. return LSM6DSL_ERROR;
  999. }
  1000. /* Enable step detector on INT1 pin */
  1001. if (lsm6dsl_pin_int1_route_get(&(pObj->Ctx), &val) != LSM6DSL_OK)
  1002. {
  1003. return LSM6DSL_ERROR;
  1004. }
  1005. val.int1_step_detector = PROPERTY_ENABLE;
  1006. if (lsm6dsl_pin_int1_route_set(&(pObj->Ctx), val) != LSM6DSL_OK)
  1007. {
  1008. return LSM6DSL_ERROR;
  1009. }
  1010. return LSM6DSL_OK;
  1011. }
  1012. /**
  1013. * @brief Disable pedometer
  1014. * @param pObj the device pObj
  1015. * @retval 0 in case of success, an error code otherwise
  1016. */
  1017. int32_t LSM6DSL_ACC_Disable_Pedometer(LSM6DSL_Object_t *pObj)
  1018. {
  1019. lsm6dsl_int1_route_t val1;
  1020. /* Disable step detector on INT1 pin */
  1021. if (lsm6dsl_pin_int1_route_get(&(pObj->Ctx), &val1) != LSM6DSL_OK)
  1022. {
  1023. return LSM6DSL_ERROR;
  1024. }
  1025. val1.int1_step_detector = PROPERTY_DISABLE;
  1026. if (lsm6dsl_pin_int1_route_set(&(pObj->Ctx), val1) != LSM6DSL_OK)
  1027. {
  1028. return LSM6DSL_ERROR;
  1029. }
  1030. /* Disable pedometer algorithm. */
  1031. if (lsm6dsl_pedo_sens_set(&(pObj->Ctx), PROPERTY_DISABLE) != LSM6DSL_OK)
  1032. {
  1033. return LSM6DSL_ERROR;
  1034. }
  1035. /* Reset pedometer threshold. */
  1036. if (lsm6dsl_pedo_threshold_set(&(pObj->Ctx), 0x0) != LSM6DSL_OK)
  1037. {
  1038. return LSM6DSL_ERROR;
  1039. }
  1040. return LSM6DSL_OK;
  1041. }
  1042. /**
  1043. * @brief Get step count
  1044. * @param pObj the device pObj
  1045. * @param StepCount step counter
  1046. * @retval 0 in case of success, an error code otherwise
  1047. */
  1048. int32_t LSM6DSL_ACC_Get_Step_Count(LSM6DSL_Object_t *pObj, uint16_t *StepCount)
  1049. {
  1050. if (lsm6dsl_read_reg(&(pObj->Ctx), LSM6DSL_STEP_COUNTER_L, (uint8_t *)StepCount, 2) != LSM6DSL_OK)
  1051. {
  1052. return LSM6DSL_ERROR;
  1053. }
  1054. return LSM6DSL_OK;
  1055. }
  1056. /**
  1057. * @brief Enable step counter reset
  1058. * @param pObj the device pObj
  1059. * @retval 0 in case of success, an error code otherwise
  1060. */
  1061. int32_t LSM6DSL_ACC_Enable_Step_Counter_Reset(LSM6DSL_Object_t *pObj)
  1062. {
  1063. if (lsm6dsl_pedo_step_reset_set(&(pObj->Ctx), PROPERTY_ENABLE) != LSM6DSL_OK)
  1064. {
  1065. return LSM6DSL_ERROR;
  1066. }
  1067. return LSM6DSL_OK;
  1068. }
  1069. /**
  1070. * @brief Disable step counter reset
  1071. * @param pObj the device pObj
  1072. * @retval 0 in case of success, an error code otherwise
  1073. */
  1074. int32_t LSM6DSL_ACC_Disable_Step_Counter_Reset(LSM6DSL_Object_t *pObj)
  1075. {
  1076. if (lsm6dsl_pedo_step_reset_set(&(pObj->Ctx), PROPERTY_DISABLE) != LSM6DSL_OK)
  1077. {
  1078. return LSM6DSL_ERROR;
  1079. }
  1080. return LSM6DSL_OK;
  1081. }
  1082. /**
  1083. * @brief Set pedometer threshold
  1084. * @param pObj the device pObj
  1085. * @param Threshold pedometer threshold
  1086. * @retval 0 in case of success, an error code otherwise
  1087. */
  1088. int32_t LSM6DSL_ACC_Set_Pedometer_Threshold(LSM6DSL_Object_t *pObj, uint8_t Threshold)
  1089. {
  1090. if (lsm6dsl_pedo_threshold_set(&(pObj->Ctx), Threshold) != LSM6DSL_OK)
  1091. {
  1092. return LSM6DSL_ERROR;
  1093. }
  1094. return LSM6DSL_OK;
  1095. }
  1096. /**
  1097. * @brief Enable tilt detection
  1098. * @param pObj the device pObj
  1099. * @param IntPin interrupt pin line to be used
  1100. * @retval 0 in case of success, an error code otherwise
  1101. */
  1102. int32_t LSM6DSL_ACC_Enable_Tilt_Detection(LSM6DSL_Object_t *pObj, LSM6DSL_SensorIntPin_t IntPin)
  1103. {
  1104. int32_t ret = LSM6DSL_OK;
  1105. lsm6dsl_int1_route_t val1;
  1106. lsm6dsl_int2_route_t val2;
  1107. /* Output Data Rate selection */
  1108. if (LSM6DSL_ACC_SetOutputDataRate(pObj, 26.0f) != LSM6DSL_OK)
  1109. {
  1110. return LSM6DSL_ERROR;
  1111. }
  1112. /* Full scale selection */
  1113. if (LSM6DSL_ACC_SetFullScale(pObj, 2) != LSM6DSL_OK)
  1114. {
  1115. return LSM6DSL_ERROR;
  1116. }
  1117. /* Enable tilt calculation. */
  1118. if (lsm6dsl_tilt_sens_set(&(pObj->Ctx), PROPERTY_ENABLE) != LSM6DSL_OK)
  1119. {
  1120. return LSM6DSL_ERROR;
  1121. }
  1122. /* Enable tilt event on either INT1 or INT2 pin */
  1123. switch (IntPin)
  1124. {
  1125. case LSM6DSL_INT1_PIN:
  1126. if (lsm6dsl_pin_int1_route_get(&(pObj->Ctx), &val1) != LSM6DSL_OK)
  1127. {
  1128. return LSM6DSL_ERROR;
  1129. }
  1130. val1.int1_tilt = PROPERTY_ENABLE;
  1131. if (lsm6dsl_pin_int1_route_set(&(pObj->Ctx), val1) != LSM6DSL_OK)
  1132. {
  1133. return LSM6DSL_ERROR;
  1134. }
  1135. break;
  1136. case LSM6DSL_INT2_PIN:
  1137. if (lsm6dsl_pin_int2_route_get(&(pObj->Ctx), &val2) != LSM6DSL_OK)
  1138. {
  1139. return LSM6DSL_ERROR;
  1140. }
  1141. val2.int2_tilt = PROPERTY_ENABLE;
  1142. if (lsm6dsl_pin_int2_route_set(&(pObj->Ctx), val2) != LSM6DSL_OK)
  1143. {
  1144. return LSM6DSL_ERROR;
  1145. }
  1146. break;
  1147. default:
  1148. ret = LSM6DSL_ERROR;
  1149. break;
  1150. }
  1151. return ret;
  1152. }
  1153. /**
  1154. * @brief Disable tilt detection
  1155. * @param pObj the device pObj
  1156. * @retval 0 in case of success, an error code otherwise
  1157. */
  1158. int32_t LSM6DSL_ACC_Disable_Tilt_Detection(LSM6DSL_Object_t *pObj)
  1159. {
  1160. lsm6dsl_int1_route_t val1;
  1161. lsm6dsl_int2_route_t val2;
  1162. /* Disable tilt event on both INT1 and INT2 pins */
  1163. if (lsm6dsl_pin_int1_route_get(&(pObj->Ctx), &val1) != LSM6DSL_OK)
  1164. {
  1165. return LSM6DSL_ERROR;
  1166. }
  1167. val1.int1_tilt = PROPERTY_DISABLE;
  1168. if (lsm6dsl_pin_int1_route_set(&(pObj->Ctx), val1) != LSM6DSL_OK)
  1169. {
  1170. return LSM6DSL_ERROR;
  1171. }
  1172. if (lsm6dsl_pin_int2_route_get(&(pObj->Ctx), &val2) != LSM6DSL_OK)
  1173. {
  1174. return LSM6DSL_ERROR;
  1175. }
  1176. val2.int2_tilt = PROPERTY_DISABLE;
  1177. if (lsm6dsl_pin_int2_route_set(&(pObj->Ctx), val2) != LSM6DSL_OK)
  1178. {
  1179. return LSM6DSL_ERROR;
  1180. }
  1181. /* Disable tilt calculation. */
  1182. if (lsm6dsl_tilt_sens_set(&(pObj->Ctx), PROPERTY_DISABLE) != LSM6DSL_OK)
  1183. {
  1184. return LSM6DSL_ERROR;
  1185. }
  1186. return LSM6DSL_OK;
  1187. }
  1188. /**
  1189. * @brief Enable wake up detection
  1190. * @param pObj the device pObj
  1191. * @param IntPin interrupt pin line to be used
  1192. * @retval 0 in case of success, an error code otherwise
  1193. */
  1194. int32_t LSM6DSL_ACC_Enable_Wake_Up_Detection(LSM6DSL_Object_t *pObj, LSM6DSL_SensorIntPin_t IntPin)
  1195. {
  1196. int32_t ret = LSM6DSL_OK;
  1197. lsm6dsl_int1_route_t val1;
  1198. lsm6dsl_int2_route_t val2;
  1199. /* Output Data Rate selection */
  1200. if (LSM6DSL_ACC_SetOutputDataRate(pObj, 416.0f) != LSM6DSL_OK)
  1201. {
  1202. return LSM6DSL_ERROR;
  1203. }
  1204. /* Full scale selection */
  1205. if (LSM6DSL_ACC_SetFullScale(pObj, 2) != LSM6DSL_OK)
  1206. {
  1207. return LSM6DSL_ERROR;
  1208. }
  1209. /* WAKE_DUR setting */
  1210. if (lsm6dsl_wkup_dur_set(&(pObj->Ctx), 0x00) != LSM6DSL_OK)
  1211. {
  1212. return LSM6DSL_ERROR;
  1213. }
  1214. /* Set wake up threshold. */
  1215. if (lsm6dsl_wkup_threshold_set(&(pObj->Ctx), 0x02) != LSM6DSL_OK)
  1216. {
  1217. return LSM6DSL_ERROR;
  1218. }
  1219. /* Enable wake up event on either INT1 or INT2 pin */
  1220. switch (IntPin)
  1221. {
  1222. case LSM6DSL_INT1_PIN:
  1223. if (lsm6dsl_pin_int1_route_get(&(pObj->Ctx), &val1) != LSM6DSL_OK)
  1224. {
  1225. return LSM6DSL_ERROR;
  1226. }
  1227. val1.int1_wu = PROPERTY_ENABLE;
  1228. if (lsm6dsl_pin_int1_route_set(&(pObj->Ctx), val1) != LSM6DSL_OK)
  1229. {
  1230. return LSM6DSL_ERROR;
  1231. }
  1232. break;
  1233. case LSM6DSL_INT2_PIN:
  1234. if (lsm6dsl_pin_int2_route_get(&(pObj->Ctx), &val2) != LSM6DSL_OK)
  1235. {
  1236. return LSM6DSL_ERROR;
  1237. }
  1238. val2.int2_wu = PROPERTY_ENABLE;
  1239. if (lsm6dsl_pin_int2_route_set(&(pObj->Ctx), val2) != LSM6DSL_OK)
  1240. {
  1241. return LSM6DSL_ERROR;
  1242. }
  1243. break;
  1244. default:
  1245. ret = LSM6DSL_ERROR;
  1246. break;
  1247. }
  1248. return ret;
  1249. }
  1250. /**
  1251. * @brief Disable wake up detection
  1252. * @param pObj the device pObj
  1253. * @retval 0 in case of success, an error code otherwise
  1254. */
  1255. int32_t LSM6DSL_ACC_Disable_Wake_Up_Detection(LSM6DSL_Object_t *pObj)
  1256. {
  1257. lsm6dsl_int1_route_t val1;
  1258. lsm6dsl_int2_route_t val2;
  1259. /* Disable wake up event on both INT1 and INT2 pins */
  1260. if (lsm6dsl_pin_int1_route_get(&(pObj->Ctx), &val1) != LSM6DSL_OK)
  1261. {
  1262. return LSM6DSL_ERROR;
  1263. }
  1264. val1.int1_wu = PROPERTY_DISABLE;
  1265. if (lsm6dsl_pin_int1_route_set(&(pObj->Ctx), val1) != LSM6DSL_OK)
  1266. {
  1267. return LSM6DSL_ERROR;
  1268. }
  1269. if (lsm6dsl_pin_int2_route_get(&(pObj->Ctx), &val2) != LSM6DSL_OK)
  1270. {
  1271. return LSM6DSL_ERROR;
  1272. }
  1273. val2.int2_wu = PROPERTY_DISABLE;
  1274. if (lsm6dsl_pin_int2_route_set(&(pObj->Ctx), val2) != LSM6DSL_OK)
  1275. {
  1276. return LSM6DSL_ERROR;
  1277. }
  1278. /* Reset wake up threshold. */
  1279. if (lsm6dsl_wkup_threshold_set(&(pObj->Ctx), 0x00) != LSM6DSL_OK)
  1280. {
  1281. return LSM6DSL_ERROR;
  1282. }
  1283. /* WAKE_DUR setting */
  1284. if (lsm6dsl_wkup_dur_set(&(pObj->Ctx), 0x00) != LSM6DSL_OK)
  1285. {
  1286. return LSM6DSL_ERROR;
  1287. }
  1288. return LSM6DSL_OK;
  1289. }
  1290. /**
  1291. * @brief Set wake up threshold
  1292. * @param pObj the device pObj
  1293. * @param Threshold wake up detection threshold
  1294. * @retval 0 in case of success, an error code otherwise
  1295. */
  1296. int32_t LSM6DSL_ACC_Set_Wake_Up_Threshold(LSM6DSL_Object_t *pObj, uint8_t Threshold)
  1297. {
  1298. /* Set wake up threshold. */
  1299. if (lsm6dsl_wkup_threshold_set(&(pObj->Ctx), Threshold) != LSM6DSL_OK)
  1300. {
  1301. return LSM6DSL_ERROR;
  1302. }
  1303. return LSM6DSL_OK;
  1304. }
  1305. /**
  1306. * @brief Set wake up duration
  1307. * @param pObj the device pObj
  1308. * @param Duration wake up detection duration
  1309. * @retval 0 in case of success, an error code otherwise
  1310. */
  1311. int32_t LSM6DSL_ACC_Set_Wake_Up_Duration(LSM6DSL_Object_t *pObj, uint8_t Duration)
  1312. {
  1313. /* Set wake up duration. */
  1314. if (lsm6dsl_wkup_dur_set(&(pObj->Ctx), Duration) != LSM6DSL_OK)
  1315. {
  1316. return LSM6DSL_ERROR;
  1317. }
  1318. return LSM6DSL_OK;
  1319. }
  1320. /**
  1321. * @brief Enable inactivity detection
  1322. * @param pObj the device pObj
  1323. * @param IntPin interrupt pin line to be used
  1324. * @retval 0 in case of success, an error code otherwise
  1325. */
  1326. int32_t LSM6DSL_ACC_Enable_Inactivity_Detection(LSM6DSL_Object_t *pObj, LSM6DSL_SensorIntPin_t IntPin)
  1327. {
  1328. int32_t ret = LSM6DSL_OK;
  1329. lsm6dsl_int1_route_t val1;
  1330. lsm6dsl_int2_route_t val2;
  1331. /* Output Data Rate and Full scale must be selected externally */
  1332. /* SLEEP_DUR setting */
  1333. if (lsm6dsl_act_sleep_dur_set(&(pObj->Ctx), 0x01) != LSM6DSL_OK)
  1334. {
  1335. return LSM6DSL_ERROR;
  1336. }
  1337. /* Set wake up threshold. */
  1338. if (lsm6dsl_wkup_threshold_set(&(pObj->Ctx), 0x02) != LSM6DSL_OK)
  1339. {
  1340. return LSM6DSL_ERROR;
  1341. }
  1342. /* Enable inactivity detection. Gyroscope is left configured as it is */
  1343. if (lsm6dsl_act_mode_set(&(pObj->Ctx), LSM6DSL_XL_12Hz5_GY_NOT_AFFECTED) != LSM6DSL_OK)
  1344. {
  1345. return LSM6DSL_ERROR;
  1346. }
  1347. /* Enable activity/inactivity event on either INT1 or INT2 pin */
  1348. switch (IntPin)
  1349. {
  1350. case LSM6DSL_INT1_PIN:
  1351. if (lsm6dsl_pin_int1_route_get(&(pObj->Ctx), &val1) != LSM6DSL_OK)
  1352. {
  1353. return LSM6DSL_ERROR;
  1354. }
  1355. val1.int1_inact_state = PROPERTY_ENABLE;
  1356. if (lsm6dsl_pin_int1_route_set(&(pObj->Ctx), val1) != LSM6DSL_OK)
  1357. {
  1358. return LSM6DSL_ERROR;
  1359. }
  1360. break;
  1361. case LSM6DSL_INT2_PIN:
  1362. if (lsm6dsl_pin_int2_route_get(&(pObj->Ctx), &val2) != LSM6DSL_OK)
  1363. {
  1364. return LSM6DSL_ERROR;
  1365. }
  1366. val2.int2_inact_state = PROPERTY_ENABLE;
  1367. if (lsm6dsl_pin_int2_route_set(&(pObj->Ctx), val2) != LSM6DSL_OK)
  1368. {
  1369. return LSM6DSL_ERROR;
  1370. }
  1371. break;
  1372. default:
  1373. ret = LSM6DSL_ERROR;
  1374. break;
  1375. }
  1376. return ret;
  1377. }
  1378. /**
  1379. * @brief Disable inactivity detection
  1380. * @param pObj the device pObj
  1381. * @retval 0 in case of success, an error code otherwise
  1382. */
  1383. int32_t LSM6DSL_ACC_Disable_Inactivity_Detection(LSM6DSL_Object_t *pObj)
  1384. {
  1385. lsm6dsl_int1_route_t val1;
  1386. lsm6dsl_int2_route_t val2;
  1387. /* Disable activity/inactivity event on both INT1 and INT2 pins */
  1388. if (lsm6dsl_pin_int1_route_get(&(pObj->Ctx), &val1) != LSM6DSL_OK)
  1389. {
  1390. return LSM6DSL_ERROR;
  1391. }
  1392. val1.int1_inact_state = PROPERTY_DISABLE;
  1393. if (lsm6dsl_pin_int1_route_set(&(pObj->Ctx), val1) != LSM6DSL_OK)
  1394. {
  1395. return LSM6DSL_ERROR;
  1396. }
  1397. if (lsm6dsl_pin_int2_route_get(&(pObj->Ctx), &val2) != LSM6DSL_OK)
  1398. {
  1399. return LSM6DSL_ERROR;
  1400. }
  1401. val2.int2_inact_state = PROPERTY_DISABLE;
  1402. if (lsm6dsl_pin_int2_route_set(&(pObj->Ctx), val2) != LSM6DSL_OK)
  1403. {
  1404. return LSM6DSL_ERROR;
  1405. }
  1406. /* Disable inactivity detection. */
  1407. if (lsm6dsl_act_mode_set(&(pObj->Ctx), LSM6DSL_PROPERTY_DISABLE) != LSM6DSL_OK)
  1408. {
  1409. return LSM6DSL_ERROR;
  1410. }
  1411. /* Reset wake up threshold. */
  1412. if (lsm6dsl_wkup_threshold_set(&(pObj->Ctx), 0x00) != LSM6DSL_OK)
  1413. {
  1414. return LSM6DSL_ERROR;
  1415. }
  1416. /* SLEEP_DUR setting */
  1417. if (lsm6dsl_act_sleep_dur_set(&(pObj->Ctx), 0x00) != LSM6DSL_OK)
  1418. {
  1419. return LSM6DSL_ERROR;
  1420. }
  1421. return LSM6DSL_OK;
  1422. }
  1423. /**
  1424. * @brief Set sleep duration
  1425. * @param pObj the device pObj
  1426. * @param Duration wake up detection duration
  1427. * @retval 0 in case of success, an error code otherwise
  1428. */
  1429. int32_t LSM6DSL_ACC_Set_Sleep_Duration(LSM6DSL_Object_t *pObj, uint8_t Duration)
  1430. {
  1431. /* Set sleep duration. */
  1432. if (lsm6dsl_act_sleep_dur_set(&(pObj->Ctx), Duration) != LSM6DSL_OK)
  1433. {
  1434. return LSM6DSL_ERROR;
  1435. }
  1436. return LSM6DSL_OK;
  1437. }
  1438. /**
  1439. * @brief Enable single tap detection
  1440. * @param pObj the device pObj
  1441. * @param IntPin interrupt pin line to be used
  1442. * @retval 0 in case of success, an error code otherwise
  1443. */
  1444. int32_t LSM6DSL_ACC_Enable_Single_Tap_Detection(LSM6DSL_Object_t *pObj, LSM6DSL_SensorIntPin_t IntPin)
  1445. {
  1446. int32_t ret = LSM6DSL_OK;
  1447. lsm6dsl_int1_route_t val1;
  1448. lsm6dsl_int2_route_t val2;
  1449. /* Output Data Rate selection */
  1450. if (LSM6DSL_ACC_SetOutputDataRate(pObj, 416.0f) != LSM6DSL_OK)
  1451. {
  1452. return LSM6DSL_ERROR;
  1453. }
  1454. /* Full scale selection */
  1455. if (LSM6DSL_ACC_SetFullScale(pObj, 2) != LSM6DSL_OK)
  1456. {
  1457. return LSM6DSL_ERROR;
  1458. }
  1459. /* Enable X direction in tap recognition. */
  1460. if (lsm6dsl_tap_detection_on_x_set(&(pObj->Ctx), PROPERTY_ENABLE) != LSM6DSL_OK)
  1461. {
  1462. return LSM6DSL_ERROR;
  1463. }
  1464. /* Enable Y direction in tap recognition. */
  1465. if (lsm6dsl_tap_detection_on_y_set(&(pObj->Ctx), PROPERTY_ENABLE) != LSM6DSL_OK)
  1466. {
  1467. return LSM6DSL_ERROR;
  1468. }
  1469. /* Enable Z direction in tap recognition. */
  1470. if (lsm6dsl_tap_detection_on_z_set(&(pObj->Ctx), PROPERTY_ENABLE) != LSM6DSL_OK)
  1471. {
  1472. return LSM6DSL_ERROR;
  1473. }
  1474. /* Set tap threshold. */
  1475. if (lsm6dsl_tap_threshold_x_set(&(pObj->Ctx), 0x08) != LSM6DSL_OK)
  1476. {
  1477. return LSM6DSL_ERROR;
  1478. }
  1479. /* Set tap shock time window. */
  1480. if (lsm6dsl_tap_shock_set(&(pObj->Ctx), 0x02) != LSM6DSL_OK)
  1481. {
  1482. return LSM6DSL_ERROR;
  1483. }
  1484. /* Set tap quiet time window. */
  1485. if (lsm6dsl_tap_quiet_set(&(pObj->Ctx), 0x01) != LSM6DSL_OK)
  1486. {
  1487. return LSM6DSL_ERROR;
  1488. }
  1489. /* _NOTE_: Tap duration time window - don't care for single tap. */
  1490. /* _NOTE_: Single/Double Tap event - don't care of this flag for single tap. */
  1491. /* Enable single tap event on either INT1 or INT2 pin */
  1492. switch (IntPin)
  1493. {
  1494. case LSM6DSL_INT1_PIN:
  1495. if (lsm6dsl_pin_int1_route_get(&(pObj->Ctx), &val1) != LSM6DSL_OK)
  1496. {
  1497. return LSM6DSL_ERROR;
  1498. }
  1499. val1.int1_single_tap = PROPERTY_ENABLE;
  1500. if (lsm6dsl_pin_int1_route_set(&(pObj->Ctx), val1) != LSM6DSL_OK)
  1501. {
  1502. return LSM6DSL_ERROR;
  1503. }
  1504. break;
  1505. case LSM6DSL_INT2_PIN:
  1506. if (lsm6dsl_pin_int2_route_get(&(pObj->Ctx), &val2) != LSM6DSL_OK)
  1507. {
  1508. return LSM6DSL_ERROR;
  1509. }
  1510. val2.int2_single_tap = PROPERTY_ENABLE;
  1511. if (lsm6dsl_pin_int2_route_set(&(pObj->Ctx), val2) != LSM6DSL_OK)
  1512. {
  1513. return LSM6DSL_ERROR;
  1514. }
  1515. break;
  1516. default:
  1517. ret = LSM6DSL_ERROR;
  1518. break;
  1519. }
  1520. return ret;
  1521. }
  1522. /**
  1523. * @brief Disable single tap detection
  1524. * @param pObj the device pObj
  1525. * @retval 0 in case of success, an error code otherwise
  1526. */
  1527. int32_t LSM6DSL_ACC_Disable_Single_Tap_Detection(LSM6DSL_Object_t *pObj)
  1528. {
  1529. lsm6dsl_int1_route_t val1;
  1530. lsm6dsl_int2_route_t val2;
  1531. /* Disable single tap event on both INT1 and INT2 pins */
  1532. if (lsm6dsl_pin_int1_route_get(&(pObj->Ctx), &val1) != LSM6DSL_OK)
  1533. {
  1534. return LSM6DSL_ERROR;
  1535. }
  1536. val1.int1_single_tap = PROPERTY_DISABLE;
  1537. if (lsm6dsl_pin_int1_route_set(&(pObj->Ctx), val1) != LSM6DSL_OK)
  1538. {
  1539. return LSM6DSL_ERROR;
  1540. }
  1541. if (lsm6dsl_pin_int2_route_get(&(pObj->Ctx), &val2) != LSM6DSL_OK)
  1542. {
  1543. return LSM6DSL_ERROR;
  1544. }
  1545. val2.int2_single_tap = PROPERTY_DISABLE;
  1546. if (lsm6dsl_pin_int2_route_set(&(pObj->Ctx), val2) != LSM6DSL_OK)
  1547. {
  1548. return LSM6DSL_ERROR;
  1549. }
  1550. /* Reset tap quiet time window. */
  1551. if (lsm6dsl_tap_quiet_set(&(pObj->Ctx), 0x00) != LSM6DSL_OK)
  1552. {
  1553. return LSM6DSL_ERROR;
  1554. }
  1555. /* Reset tap shock time window. */
  1556. if (lsm6dsl_tap_shock_set(&(pObj->Ctx), 0x00) != LSM6DSL_OK)
  1557. {
  1558. return LSM6DSL_ERROR;
  1559. }
  1560. /* Reset tap threshold. */
  1561. if (lsm6dsl_tap_threshold_x_set(&(pObj->Ctx), 0x00) != LSM6DSL_OK)
  1562. {
  1563. return LSM6DSL_ERROR;
  1564. }
  1565. /* Disable Z direction in tap recognition. */
  1566. if (lsm6dsl_tap_detection_on_z_set(&(pObj->Ctx), PROPERTY_DISABLE) != LSM6DSL_OK)
  1567. {
  1568. return LSM6DSL_ERROR;
  1569. }
  1570. /* Disable Y direction in tap recognition. */
  1571. if (lsm6dsl_tap_detection_on_y_set(&(pObj->Ctx), PROPERTY_DISABLE) != LSM6DSL_OK)
  1572. {
  1573. return LSM6DSL_ERROR;
  1574. }
  1575. /* Disable X direction in tap recognition. */
  1576. if (lsm6dsl_tap_detection_on_x_set(&(pObj->Ctx), PROPERTY_DISABLE) != LSM6DSL_OK)
  1577. {
  1578. return LSM6DSL_ERROR;
  1579. }
  1580. return LSM6DSL_OK;
  1581. }
  1582. /**
  1583. * @brief Enable double tap detection
  1584. * @param pObj the device pObj
  1585. * @param IntPin interrupt pin line to be used
  1586. * @retval 0 in case of success, an error code otherwise
  1587. */
  1588. int32_t LSM6DSL_ACC_Enable_Double_Tap_Detection(LSM6DSL_Object_t *pObj, LSM6DSL_SensorIntPin_t IntPin)
  1589. {
  1590. int32_t ret = LSM6DSL_OK;
  1591. lsm6dsl_int1_route_t val1;
  1592. lsm6dsl_int2_route_t val2;
  1593. /* Output Data Rate selection */
  1594. if (LSM6DSL_ACC_SetOutputDataRate(pObj, 416.0f) != LSM6DSL_OK)
  1595. {
  1596. return LSM6DSL_ERROR;
  1597. }
  1598. /* Full scale selection */
  1599. if (LSM6DSL_ACC_SetFullScale(pObj, 2) != LSM6DSL_OK)
  1600. {
  1601. return LSM6DSL_ERROR;
  1602. }
  1603. /* Enable X direction in tap recognition. */
  1604. if (lsm6dsl_tap_detection_on_x_set(&(pObj->Ctx), PROPERTY_ENABLE) != LSM6DSL_OK)
  1605. {
  1606. return LSM6DSL_ERROR;
  1607. }
  1608. /* Enable Y direction in tap recognition. */
  1609. if (lsm6dsl_tap_detection_on_y_set(&(pObj->Ctx), PROPERTY_ENABLE) != LSM6DSL_OK)
  1610. {
  1611. return LSM6DSL_ERROR;
  1612. }
  1613. /* Enable Z direction in tap recognition. */
  1614. if (lsm6dsl_tap_detection_on_z_set(&(pObj->Ctx), PROPERTY_ENABLE) != LSM6DSL_OK)
  1615. {
  1616. return LSM6DSL_ERROR;
  1617. }
  1618. /* Set tap threshold. */
  1619. if (lsm6dsl_tap_threshold_x_set(&(pObj->Ctx), 0x08) != LSM6DSL_OK)
  1620. {
  1621. return LSM6DSL_ERROR;
  1622. }
  1623. /* Set tap shock time window. */
  1624. if (lsm6dsl_tap_shock_set(&(pObj->Ctx), 0x03) != LSM6DSL_OK)
  1625. {
  1626. return LSM6DSL_ERROR;
  1627. }
  1628. /* Set tap quiet time window. */
  1629. if (lsm6dsl_tap_quiet_set(&(pObj->Ctx), 0x03) != LSM6DSL_OK)
  1630. {
  1631. return LSM6DSL_ERROR;
  1632. }
  1633. /* Set tap duration time window. */
  1634. if (lsm6dsl_tap_dur_set(&(pObj->Ctx), 0x08) != LSM6DSL_OK)
  1635. {
  1636. return LSM6DSL_ERROR;
  1637. }
  1638. /* Single and double tap enabled. */
  1639. if (lsm6dsl_tap_mode_set(&(pObj->Ctx), LSM6DSL_BOTH_SINGLE_DOUBLE) != LSM6DSL_OK)
  1640. {
  1641. return LSM6DSL_ERROR;
  1642. }
  1643. /* Enable double tap event on either INT1 or INT2 pin */
  1644. switch (IntPin)
  1645. {
  1646. case LSM6DSL_INT1_PIN:
  1647. if (lsm6dsl_pin_int1_route_get(&(pObj->Ctx), &val1) != LSM6DSL_OK)
  1648. {
  1649. return LSM6DSL_ERROR;
  1650. }
  1651. val1.int1_double_tap = PROPERTY_ENABLE;
  1652. if (lsm6dsl_pin_int1_route_set(&(pObj->Ctx), val1) != LSM6DSL_OK)
  1653. {
  1654. return LSM6DSL_ERROR;
  1655. }
  1656. break;
  1657. case LSM6DSL_INT2_PIN:
  1658. if (lsm6dsl_pin_int2_route_get(&(pObj->Ctx), &val2) != LSM6DSL_OK)
  1659. {
  1660. return LSM6DSL_ERROR;
  1661. }
  1662. val2.int2_double_tap = PROPERTY_ENABLE;
  1663. if (lsm6dsl_pin_int2_route_set(&(pObj->Ctx), val2) != LSM6DSL_OK)
  1664. {
  1665. return LSM6DSL_ERROR;
  1666. }
  1667. break;
  1668. default:
  1669. ret = LSM6DSL_ERROR;
  1670. break;
  1671. }
  1672. return ret;
  1673. }
  1674. /**
  1675. * @brief Disable double tap detection
  1676. * @param pObj the device pObj
  1677. * @retval 0 in case of success, an error code otherwise
  1678. */
  1679. int32_t LSM6DSL_ACC_Disable_Double_Tap_Detection(LSM6DSL_Object_t *pObj)
  1680. {
  1681. lsm6dsl_int1_route_t val1;
  1682. lsm6dsl_int2_route_t val2;
  1683. /* Disable double tap event on both INT1 and INT2 pins */
  1684. if (lsm6dsl_pin_int1_route_get(&(pObj->Ctx), &val1) != LSM6DSL_OK)
  1685. {
  1686. return LSM6DSL_ERROR;
  1687. }
  1688. val1.int1_double_tap = PROPERTY_DISABLE;
  1689. if (lsm6dsl_pin_int1_route_set(&(pObj->Ctx), val1) != LSM6DSL_OK)
  1690. {
  1691. return LSM6DSL_ERROR;
  1692. }
  1693. if (lsm6dsl_pin_int2_route_get(&(pObj->Ctx), &val2) != LSM6DSL_OK)
  1694. {
  1695. return LSM6DSL_ERROR;
  1696. }
  1697. val2.int2_double_tap = PROPERTY_DISABLE;
  1698. if (lsm6dsl_pin_int2_route_set(&(pObj->Ctx), val2) != LSM6DSL_OK)
  1699. {
  1700. return LSM6DSL_ERROR;
  1701. }
  1702. /* Only single tap enabled. */
  1703. if (lsm6dsl_tap_mode_set(&(pObj->Ctx), LSM6DSL_ONLY_SINGLE) != LSM6DSL_OK)
  1704. {
  1705. return LSM6DSL_ERROR;
  1706. }
  1707. /* Reset tap duration time window. */
  1708. if (lsm6dsl_tap_dur_set(&(pObj->Ctx), 0x00) != LSM6DSL_OK)
  1709. {
  1710. return LSM6DSL_ERROR;
  1711. }
  1712. /* Reset tap quiet time window. */
  1713. if (lsm6dsl_tap_quiet_set(&(pObj->Ctx), 0x00) != LSM6DSL_OK)
  1714. {
  1715. return LSM6DSL_ERROR;
  1716. }
  1717. /* Reset tap shock time window. */
  1718. if (lsm6dsl_tap_shock_set(&(pObj->Ctx), 0x00) != LSM6DSL_OK)
  1719. {
  1720. return LSM6DSL_ERROR;
  1721. }
  1722. /* Reset tap threshold. */
  1723. if (lsm6dsl_tap_threshold_x_set(&(pObj->Ctx), 0x00) != LSM6DSL_OK)
  1724. {
  1725. return LSM6DSL_ERROR;
  1726. }
  1727. /* Disable Z direction in tap recognition. */
  1728. if (lsm6dsl_tap_detection_on_z_set(&(pObj->Ctx), PROPERTY_DISABLE) != LSM6DSL_OK)
  1729. {
  1730. return LSM6DSL_ERROR;
  1731. }
  1732. /* Disable Y direction in tap recognition. */
  1733. if (lsm6dsl_tap_detection_on_y_set(&(pObj->Ctx), PROPERTY_DISABLE) != LSM6DSL_OK)
  1734. {
  1735. return LSM6DSL_ERROR;
  1736. }
  1737. /* Disable X direction in tap recognition. */
  1738. if (lsm6dsl_tap_detection_on_x_set(&(pObj->Ctx), PROPERTY_DISABLE) != LSM6DSL_OK)
  1739. {
  1740. return LSM6DSL_ERROR;
  1741. }
  1742. return LSM6DSL_OK;
  1743. }
  1744. /**
  1745. * @brief Set tap threshold
  1746. * @param pObj the device pObj
  1747. * @param Threshold tap threshold
  1748. * @retval 0 in case of success, an error code otherwise
  1749. */
  1750. int32_t LSM6DSL_ACC_Set_Tap_Threshold(LSM6DSL_Object_t *pObj, uint8_t Threshold)
  1751. {
  1752. /* Set tap threshold. */
  1753. if (lsm6dsl_tap_threshold_x_set(&(pObj->Ctx), Threshold) != LSM6DSL_OK)
  1754. {
  1755. return LSM6DSL_ERROR;
  1756. }
  1757. return LSM6DSL_OK;
  1758. }
  1759. /**
  1760. * @brief Set tap shock time
  1761. * @param pObj the device pObj
  1762. * @param Time tap shock time
  1763. * @retval 0 in case of success, an error code otherwise
  1764. */
  1765. int32_t LSM6DSL_ACC_Set_Tap_Shock_Time(LSM6DSL_Object_t *pObj, uint8_t Time)
  1766. {
  1767. /* Set tap shock time window. */
  1768. if (lsm6dsl_tap_shock_set(&(pObj->Ctx), Time) != LSM6DSL_OK)
  1769. {
  1770. return LSM6DSL_ERROR;
  1771. }
  1772. return LSM6DSL_OK;
  1773. }
  1774. /**
  1775. * @brief Set tap quiet time
  1776. * @param pObj the device pObj
  1777. * @param Time tap quiet time
  1778. * @retval 0 in case of success, an error code otherwise
  1779. */
  1780. int32_t LSM6DSL_ACC_Set_Tap_Quiet_Time(LSM6DSL_Object_t *pObj, uint8_t Time)
  1781. {
  1782. /* Set tap quiet time window. */
  1783. if (lsm6dsl_tap_quiet_set(&(pObj->Ctx), Time) != LSM6DSL_OK)
  1784. {
  1785. return LSM6DSL_ERROR;
  1786. }
  1787. return LSM6DSL_OK;
  1788. }
  1789. /**
  1790. * @brief Set tap duration time
  1791. * @param pObj the device pObj
  1792. * @param Time tap duration time
  1793. * @retval 0 in case of success, an error code otherwise
  1794. */
  1795. int32_t LSM6DSL_ACC_Set_Tap_Duration_Time(LSM6DSL_Object_t *pObj, uint8_t Time)
  1796. {
  1797. /* Set tap duration time window. */
  1798. if (lsm6dsl_tap_dur_set(&(pObj->Ctx), Time) != LSM6DSL_OK)
  1799. {
  1800. return LSM6DSL_ERROR;
  1801. }
  1802. return LSM6DSL_OK;
  1803. }
  1804. /**
  1805. * @brief Enable 6D orientation detection
  1806. * @param pObj the device pObj
  1807. * @param IntPin interrupt pin line to be used
  1808. * @retval 0 in case of success, an error code otherwise
  1809. */
  1810. int32_t LSM6DSL_ACC_Enable_6D_Orientation(LSM6DSL_Object_t *pObj, LSM6DSL_SensorIntPin_t IntPin)
  1811. {
  1812. int32_t ret = LSM6DSL_OK;
  1813. lsm6dsl_int1_route_t val1;
  1814. lsm6dsl_int2_route_t val2;
  1815. /* Output Data Rate selection */
  1816. if (LSM6DSL_ACC_SetOutputDataRate(pObj, 416.0f) != LSM6DSL_OK)
  1817. {
  1818. return LSM6DSL_ERROR;
  1819. }
  1820. /* Full scale selection */
  1821. if (LSM6DSL_ACC_SetFullScale(pObj, 2) != LSM6DSL_OK)
  1822. {
  1823. return LSM6DSL_ERROR;
  1824. }
  1825. /* 6D orientation enabled. */
  1826. if (lsm6dsl_6d_threshold_set(&(pObj->Ctx), LSM6DSL_DEG_60) != LSM6DSL_OK)
  1827. {
  1828. return LSM6DSL_ERROR;
  1829. }
  1830. /* Enable 6D orientation event on either INT1 or INT2 pin */
  1831. switch (IntPin)
  1832. {
  1833. case LSM6DSL_INT1_PIN:
  1834. if (lsm6dsl_pin_int1_route_get(&(pObj->Ctx), &val1) != LSM6DSL_OK)
  1835. {
  1836. return LSM6DSL_ERROR;
  1837. }
  1838. val1.int1_6d = PROPERTY_ENABLE;
  1839. if (lsm6dsl_pin_int1_route_set(&(pObj->Ctx), val1) != LSM6DSL_OK)
  1840. {
  1841. return LSM6DSL_ERROR;
  1842. }
  1843. break;
  1844. case LSM6DSL_INT2_PIN:
  1845. if (lsm6dsl_pin_int2_route_get(&(pObj->Ctx), &val2) != LSM6DSL_OK)
  1846. {
  1847. return LSM6DSL_ERROR;
  1848. }
  1849. val2.int2_6d = PROPERTY_ENABLE;
  1850. if (lsm6dsl_pin_int2_route_set(&(pObj->Ctx), val2) != LSM6DSL_OK)
  1851. {
  1852. return LSM6DSL_ERROR;
  1853. }
  1854. break;
  1855. default:
  1856. ret = LSM6DSL_ERROR;
  1857. break;
  1858. }
  1859. return ret;
  1860. }
  1861. /**
  1862. * @brief Disable 6D orientation detection
  1863. * @param pObj the device pObj
  1864. * @retval 0 in case of success, an error code otherwise
  1865. */
  1866. int32_t LSM6DSL_ACC_Disable_6D_Orientation(LSM6DSL_Object_t *pObj)
  1867. {
  1868. lsm6dsl_int1_route_t val1;
  1869. lsm6dsl_int2_route_t val2;
  1870. /* Disable 6D orientation event on both INT1 and INT2 pins */
  1871. if (lsm6dsl_pin_int1_route_get(&(pObj->Ctx), &val1) != LSM6DSL_OK)
  1872. {
  1873. return LSM6DSL_ERROR;
  1874. }
  1875. val1.int1_6d = PROPERTY_DISABLE;
  1876. if (lsm6dsl_pin_int1_route_set(&(pObj->Ctx), val1) != LSM6DSL_OK)
  1877. {
  1878. return LSM6DSL_ERROR;
  1879. }
  1880. if (lsm6dsl_pin_int2_route_get(&(pObj->Ctx), &val2) != LSM6DSL_OK)
  1881. {
  1882. return LSM6DSL_ERROR;
  1883. }
  1884. val2.int2_6d = PROPERTY_DISABLE;
  1885. if (lsm6dsl_pin_int2_route_set(&(pObj->Ctx), val2) != LSM6DSL_OK)
  1886. {
  1887. return LSM6DSL_ERROR;
  1888. }
  1889. /* Reset 6D orientation. */
  1890. if (lsm6dsl_6d_threshold_set(&(pObj->Ctx), LSM6DSL_DEG_80) != LSM6DSL_OK)
  1891. {
  1892. return LSM6DSL_ERROR;
  1893. }
  1894. return LSM6DSL_OK;
  1895. }
  1896. /**
  1897. * @brief Set 6D orientation threshold
  1898. * @param pObj the device pObj
  1899. * @param Threshold free fall detection threshold
  1900. * @retval 0 in case of success, an error code otherwise
  1901. */
  1902. int32_t LSM6DSL_ACC_Set_6D_Orientation_Threshold(LSM6DSL_Object_t *pObj, uint8_t Threshold)
  1903. {
  1904. if (lsm6dsl_6d_threshold_set(&(pObj->Ctx), (lsm6dsl_sixd_ths_t)Threshold) != LSM6DSL_OK)
  1905. {
  1906. return LSM6DSL_ERROR;
  1907. }
  1908. return LSM6DSL_OK;
  1909. }
  1910. /**
  1911. * @brief Get the status of XLow orientation
  1912. * @param pObj the device pObj
  1913. * @param XLow the status of XLow orientation
  1914. * @retval 0 in case of success, an error code otherwise
  1915. */
  1916. int32_t LSM6DSL_ACC_Get_6D_Orientation_XL(LSM6DSL_Object_t *pObj, uint8_t *XLow)
  1917. {
  1918. lsm6dsl_d6d_src_t data;
  1919. if (lsm6dsl_read_reg(&(pObj->Ctx), LSM6DSL_D6D_SRC, (uint8_t *)&data, 1) != LSM6DSL_OK)
  1920. {
  1921. return LSM6DSL_ERROR;
  1922. }
  1923. *XLow = data.xl;
  1924. return LSM6DSL_OK;
  1925. }
  1926. /**
  1927. * @brief Get the status of XHigh orientation
  1928. * @param pObj the device pObj
  1929. * @param XHigh the status of XHigh orientation
  1930. * @retval 0 in case of success, an error code otherwise
  1931. */
  1932. int32_t LSM6DSL_ACC_Get_6D_Orientation_XH(LSM6DSL_Object_t *pObj, uint8_t *XHigh)
  1933. {
  1934. lsm6dsl_d6d_src_t data;
  1935. if (lsm6dsl_read_reg(&(pObj->Ctx), LSM6DSL_D6D_SRC, (uint8_t *)&data, 1) != LSM6DSL_OK)
  1936. {
  1937. return LSM6DSL_ERROR;
  1938. }
  1939. *XHigh = data.xh;
  1940. return LSM6DSL_OK;
  1941. }
  1942. /**
  1943. * @brief Get the status of YLow orientation
  1944. * @param pObj the device pObj
  1945. * @param YLow the status of YLow orientation
  1946. * @retval 0 in case of success, an error code otherwise
  1947. */
  1948. int32_t LSM6DSL_ACC_Get_6D_Orientation_YL(LSM6DSL_Object_t *pObj, uint8_t *YLow)
  1949. {
  1950. lsm6dsl_d6d_src_t data;
  1951. if (lsm6dsl_read_reg(&(pObj->Ctx), LSM6DSL_D6D_SRC, (uint8_t *)&data, 1) != LSM6DSL_OK)
  1952. {
  1953. return LSM6DSL_ERROR;
  1954. }
  1955. *YLow = data.yl;
  1956. return LSM6DSL_OK;
  1957. }
  1958. /**
  1959. * @brief Get the status of YHigh orientation
  1960. * @param pObj the device pObj
  1961. * @param YHigh the status of YHigh orientation
  1962. * @retval 0 in case of success, an error code otherwise
  1963. */
  1964. int32_t LSM6DSL_ACC_Get_6D_Orientation_YH(LSM6DSL_Object_t *pObj, uint8_t *YHigh)
  1965. {
  1966. lsm6dsl_d6d_src_t data;
  1967. if (lsm6dsl_read_reg(&(pObj->Ctx), LSM6DSL_D6D_SRC, (uint8_t *)&data, 1) != LSM6DSL_OK)
  1968. {
  1969. return LSM6DSL_ERROR;
  1970. }
  1971. *YHigh = data.yh;
  1972. return LSM6DSL_OK;
  1973. }
  1974. /**
  1975. * @brief Get the status of ZLow orientation
  1976. * @param pObj the device pObj
  1977. * @param ZLow the status of ZLow orientation
  1978. * @retval 0 in case of success, an error code otherwise
  1979. */
  1980. int32_t LSM6DSL_ACC_Get_6D_Orientation_ZL(LSM6DSL_Object_t *pObj, uint8_t *ZLow)
  1981. {
  1982. lsm6dsl_d6d_src_t data;
  1983. if (lsm6dsl_read_reg(&(pObj->Ctx), LSM6DSL_D6D_SRC, (uint8_t *)&data, 1) != LSM6DSL_OK)
  1984. {
  1985. return LSM6DSL_ERROR;
  1986. }
  1987. *ZLow = data.zl;
  1988. return LSM6DSL_OK;
  1989. }
  1990. /**
  1991. * @brief Get the status of ZHigh orientation
  1992. * @param pObj the device pObj
  1993. * @param ZHigh the status of ZHigh orientation
  1994. * @retval 0 in case of success, an error code otherwise
  1995. */
  1996. int32_t LSM6DSL_ACC_Get_6D_Orientation_ZH(LSM6DSL_Object_t *pObj, uint8_t *ZHigh)
  1997. {
  1998. lsm6dsl_d6d_src_t data;
  1999. if (lsm6dsl_read_reg(&(pObj->Ctx), LSM6DSL_D6D_SRC, (uint8_t *)&data, 1) != LSM6DSL_OK)
  2000. {
  2001. return LSM6DSL_ERROR;
  2002. }
  2003. *ZHigh = data.zh;
  2004. return LSM6DSL_OK;
  2005. }
  2006. /**
  2007. * @brief Get the status of all hardware events
  2008. * @param pObj the device pObj
  2009. * @param Status the status of all hardware events
  2010. * @retval 0 in case of success, an error code otherwise
  2011. */
  2012. int32_t LSM6DSL_ACC_Get_Event_Status(LSM6DSL_Object_t *pObj, LSM6DSL_Event_Status_t *Status)
  2013. {
  2014. lsm6dsl_wake_up_src_t wake_up_src;
  2015. lsm6dsl_tap_src_t tap_src;
  2016. lsm6dsl_d6d_src_t d6d_src;
  2017. lsm6dsl_func_src1_t func_src;
  2018. lsm6dsl_md1_cfg_t md1_cfg;
  2019. lsm6dsl_md2_cfg_t md2_cfg;
  2020. lsm6dsl_int1_ctrl_t int1_ctrl;
  2021. (void)memset((void *)Status, 0x0, sizeof(LSM6DSL_Event_Status_t));
  2022. if (lsm6dsl_read_reg(&(pObj->Ctx), LSM6DSL_WAKE_UP_SRC, (uint8_t *)&wake_up_src, 1) != LSM6DSL_OK)
  2023. {
  2024. return LSM6DSL_ERROR;
  2025. }
  2026. if (lsm6dsl_read_reg(&(pObj->Ctx), LSM6DSL_TAP_SRC, (uint8_t *)&tap_src, 1) != LSM6DSL_OK)
  2027. {
  2028. return LSM6DSL_ERROR;
  2029. }
  2030. if (lsm6dsl_read_reg(&(pObj->Ctx), LSM6DSL_D6D_SRC, (uint8_t *)&d6d_src, 1) != LSM6DSL_OK)
  2031. {
  2032. return LSM6DSL_ERROR;
  2033. }
  2034. if (lsm6dsl_read_reg(&(pObj->Ctx), LSM6DSL_FUNC_SRC1, (uint8_t *)&func_src, 1) != LSM6DSL_OK)
  2035. {
  2036. return LSM6DSL_ERROR;
  2037. }
  2038. if (lsm6dsl_read_reg(&(pObj->Ctx), LSM6DSL_MD1_CFG, (uint8_t *)&md1_cfg, 1) != LSM6DSL_OK)
  2039. {
  2040. return LSM6DSL_ERROR;
  2041. }
  2042. if (lsm6dsl_read_reg(&(pObj->Ctx), LSM6DSL_MD2_CFG, (uint8_t *)&md2_cfg, 1) != LSM6DSL_OK)
  2043. {
  2044. return LSM6DSL_ERROR;
  2045. }
  2046. if (lsm6dsl_read_reg(&(pObj->Ctx), LSM6DSL_INT1_CTRL, (uint8_t *)&int1_ctrl, 1) != LSM6DSL_OK)
  2047. {
  2048. return LSM6DSL_ERROR;
  2049. }
  2050. if ((md1_cfg.int1_ff == 1U) || (md2_cfg.int2_ff == 1U))
  2051. {
  2052. if (wake_up_src.ff_ia == 1U)
  2053. {
  2054. Status->FreeFallStatus = 1;
  2055. }
  2056. }
  2057. if ((md1_cfg.int1_wu == 1U) || (md2_cfg.int2_wu == 1U))
  2058. {
  2059. if (wake_up_src.wu_ia == 1U)
  2060. {
  2061. Status->WakeUpStatus = 1;
  2062. }
  2063. }
  2064. if ((md1_cfg.int1_inact_state == 1U) || (md2_cfg.int2_inact_state == 1U))
  2065. {
  2066. if (wake_up_src.sleep_state_ia == 1U)
  2067. {
  2068. Status->SleepStatus = 1;
  2069. }
  2070. }
  2071. if ((md1_cfg.int1_single_tap == 1U) || (md2_cfg.int2_single_tap == 1U))
  2072. {
  2073. if (tap_src.single_tap == 1U)
  2074. {
  2075. Status->TapStatus = 1;
  2076. }
  2077. }
  2078. if ((md1_cfg.int1_double_tap == 1U) || (md2_cfg.int2_double_tap == 1U))
  2079. {
  2080. if (tap_src.double_tap == 1U)
  2081. {
  2082. Status->DoubleTapStatus = 1;
  2083. }
  2084. }
  2085. if ((md1_cfg.int1_6d == 1U) || (md2_cfg.int2_6d == 1U))
  2086. {
  2087. if (d6d_src.d6d_ia == 1U)
  2088. {
  2089. Status->D6DOrientationStatus = 1;
  2090. }
  2091. }
  2092. if (int1_ctrl.int1_step_detector == 1U)
  2093. {
  2094. if (func_src.step_detected == 1U)
  2095. {
  2096. Status->StepStatus = 1;
  2097. }
  2098. }
  2099. if ((md1_cfg.int1_tilt == 1U) || (md2_cfg.int2_tilt == 1U))
  2100. {
  2101. if (func_src.tilt_ia == 1U)
  2102. {
  2103. Status->TiltStatus = 1;
  2104. }
  2105. }
  2106. return LSM6DSL_OK;
  2107. }
  2108. /**
  2109. * @brief Set self test
  2110. * @param pObj the device pObj
  2111. * @param val the value of st_xl in reg CTRL5_C
  2112. * @retval 0 in case of success, an error code otherwise
  2113. */
  2114. int32_t LSM6DSL_ACC_Set_SelfTest(LSM6DSL_Object_t *pObj, uint8_t val)
  2115. {
  2116. lsm6dsl_st_xl_t reg;
  2117. reg = (val == 0U) ? LSM6DSL_XL_ST_DISABLE
  2118. : (val == 1U) ? LSM6DSL_XL_ST_POSITIVE
  2119. : (val == 2U) ? LSM6DSL_XL_ST_NEGATIVE
  2120. : LSM6DSL_XL_ST_ND;
  2121. if (lsm6dsl_xl_self_test_set(&(pObj->Ctx), reg) != LSM6DSL_OK)
  2122. {
  2123. return LSM6DSL_ERROR;
  2124. }
  2125. return LSM6DSL_OK;
  2126. }
  2127. /**
  2128. * @brief Get the LSM6DSL ACC data ready bit value
  2129. * @param pObj the device pObj
  2130. * @param Status the status of data ready bit
  2131. * @retval 0 in case of success, an error code otherwise
  2132. */
  2133. int32_t LSM6DSL_ACC_Get_DRDY_Status(LSM6DSL_Object_t *pObj, uint8_t *Status)
  2134. {
  2135. if (lsm6dsl_xl_flag_data_ready_get(&(pObj->Ctx), Status) != LSM6DSL_OK)
  2136. {
  2137. return LSM6DSL_ERROR;
  2138. }
  2139. return LSM6DSL_OK;
  2140. }
  2141. /**
  2142. * @brief Get the LSM6DSL ACC initialization status
  2143. * @param pObj the device pObj
  2144. * @param Status 1 if initialized, 0 otherwise
  2145. * @retval 0 in case of success, an error code otherwise
  2146. */
  2147. int32_t LSM6DSL_ACC_Get_Init_Status(LSM6DSL_Object_t *pObj, uint8_t *Status)
  2148. {
  2149. if (pObj == NULL)
  2150. {
  2151. return LSM6DSL_ERROR;
  2152. }
  2153. *Status = pObj->is_initialized;
  2154. return LSM6DSL_OK;
  2155. }
  2156. /**
  2157. * @brief Set self test
  2158. * @param pObj the device pObj
  2159. * @param val the value of st_xl in reg CTRL5_C
  2160. * @retval 0 in case of success, an error code otherwise
  2161. */
  2162. int32_t LSM6DSL_GYRO_Set_SelfTest(LSM6DSL_Object_t *pObj, uint8_t val)
  2163. {
  2164. lsm6dsl_st_g_t reg;
  2165. reg = (val == 0U) ? LSM6DSL_GY_ST_DISABLE
  2166. : (val == 1U) ? LSM6DSL_GY_ST_POSITIVE
  2167. : (val == 2U) ? LSM6DSL_GY_ST_NEGATIVE
  2168. : LSM6DSL_GY_ST_ND;
  2169. if (lsm6dsl_gy_self_test_set(&(pObj->Ctx), reg) != LSM6DSL_OK)
  2170. {
  2171. return LSM6DSL_ERROR;
  2172. }
  2173. return LSM6DSL_OK;
  2174. }
  2175. /**
  2176. * @brief Get the LSM6DSL GYRO data ready bit value
  2177. * @param pObj the device pObj
  2178. * @param Status the status of data ready bit
  2179. * @retval 0 in case of success, an error code otherwise
  2180. */
  2181. int32_t LSM6DSL_GYRO_Get_DRDY_Status(LSM6DSL_Object_t *pObj, uint8_t *Status)
  2182. {
  2183. if (lsm6dsl_gy_flag_data_ready_get(&(pObj->Ctx), Status) != LSM6DSL_OK)
  2184. {
  2185. return LSM6DSL_ERROR;
  2186. }
  2187. return LSM6DSL_OK;
  2188. }
  2189. /**
  2190. * @brief Get the LSM6DSL GYRO initialization status
  2191. * @param pObj the device pObj
  2192. * @param Status 1 if initialized, 0 otherwise
  2193. * @retval 0 in case of success, an error code otherwise
  2194. */
  2195. int32_t LSM6DSL_GYRO_Get_Init_Status(LSM6DSL_Object_t *pObj, uint8_t *Status)
  2196. {
  2197. if (pObj == NULL)
  2198. {
  2199. return LSM6DSL_ERROR;
  2200. }
  2201. *Status = pObj->is_initialized;
  2202. return LSM6DSL_OK;
  2203. }
  2204. /**
  2205. * @brief Get the LSM6DSL FIFO number of samples
  2206. * @param pObj the device pObj
  2207. * @param NumSamples number of samples
  2208. * @retval 0 in case of success, an error code otherwise
  2209. */
  2210. int32_t LSM6DSL_FIFO_Get_Num_Samples(LSM6DSL_Object_t *pObj, uint16_t *NumSamples)
  2211. {
  2212. if (lsm6dsl_fifo_data_level_get(&(pObj->Ctx), NumSamples) != LSM6DSL_OK)
  2213. {
  2214. return LSM6DSL_ERROR;
  2215. }
  2216. return LSM6DSL_OK;
  2217. }
  2218. /**
  2219. * @brief Get the LSM6DSL FIFO full status
  2220. * @param pObj the device pObj
  2221. * @param Status FIFO full status
  2222. * @retval 0 in case of success, an error code otherwise
  2223. */
  2224. int32_t LSM6DSL_FIFO_Get_Full_Status(LSM6DSL_Object_t *pObj, uint8_t *Status)
  2225. {
  2226. lsm6dsl_reg_t reg;
  2227. if (lsm6dsl_read_reg(&(pObj->Ctx), LSM6DSL_FIFO_STATUS2, &reg.byte, 1) != LSM6DSL_OK)
  2228. {
  2229. return LSM6DSL_ERROR;
  2230. }
  2231. *Status = reg.fifo_status2.fifo_full_smart;
  2232. return LSM6DSL_OK;
  2233. }
  2234. /**
  2235. * @brief Set the LSM6DSL FIFO ODR value
  2236. * @param pObj the device pObj
  2237. * @param Odr FIFO ODR value
  2238. * @retval 0 in case of success, an error code otherwise
  2239. */
  2240. int32_t LSM6DSL_FIFO_Set_ODR_Value(LSM6DSL_Object_t *pObj, float Odr)
  2241. {
  2242. lsm6dsl_odr_fifo_t new_odr;
  2243. new_odr = (Odr <= 12.5f) ? LSM6DSL_FIFO_12Hz5
  2244. : (Odr <= 26.0f) ? LSM6DSL_FIFO_26Hz
  2245. : (Odr <= 52.0f) ? LSM6DSL_FIFO_52Hz
  2246. : (Odr <= 104.0f) ? LSM6DSL_FIFO_104Hz
  2247. : (Odr <= 208.0f) ? LSM6DSL_FIFO_208Hz
  2248. : (Odr <= 416.0f) ? LSM6DSL_FIFO_416Hz
  2249. : (Odr <= 833.0f) ? LSM6DSL_FIFO_833Hz
  2250. : (Odr <= 1660.0f) ? LSM6DSL_FIFO_1k66Hz
  2251. : (Odr <= 3330.0f) ? LSM6DSL_FIFO_3k33Hz
  2252. : LSM6DSL_FIFO_6k66Hz;
  2253. if (lsm6dsl_fifo_data_rate_set(&(pObj->Ctx), new_odr) != LSM6DSL_OK)
  2254. {
  2255. return LSM6DSL_ERROR;
  2256. }
  2257. return LSM6DSL_OK;
  2258. }
  2259. /**
  2260. * @brief Set the LSM6DSL FIFO full interrupt on INT1 pin
  2261. * @param pObj the device pObj
  2262. * @param Status FIFO full interrupt on INT1 pin status
  2263. * @retval 0 in case of success, an error code otherwise
  2264. */
  2265. int32_t LSM6DSL_FIFO_Set_INT1_FIFO_Full(LSM6DSL_Object_t *pObj, uint8_t Status)
  2266. {
  2267. lsm6dsl_reg_t reg;
  2268. if (lsm6dsl_read_reg(&(pObj->Ctx), LSM6DSL_INT1_CTRL, &reg.byte, 1) != LSM6DSL_OK)
  2269. {
  2270. return LSM6DSL_ERROR;
  2271. }
  2272. reg.int1_ctrl.int1_full_flag = Status;
  2273. if (lsm6dsl_write_reg(&(pObj->Ctx), LSM6DSL_INT1_CTRL, &reg.byte, 1) != LSM6DSL_OK)
  2274. {
  2275. return LSM6DSL_ERROR;
  2276. }
  2277. return LSM6DSL_OK;
  2278. }
  2279. /**
  2280. * @brief Set the LSM6DSL FIFO watermark level
  2281. * @param pObj the device pObj
  2282. * @param Watermark FIFO watermark level
  2283. * @retval 0 in case of success, an error code otherwise
  2284. */
  2285. int32_t LSM6DSL_FIFO_Set_Watermark_Level(LSM6DSL_Object_t *pObj, uint16_t Watermark)
  2286. {
  2287. if (lsm6dsl_fifo_watermark_set(&(pObj->Ctx), Watermark) != LSM6DSL_OK)
  2288. {
  2289. return LSM6DSL_ERROR;
  2290. }
  2291. return LSM6DSL_OK;
  2292. }
  2293. /**
  2294. * @brief Set the LSM6DSL FIFO stop on watermark
  2295. * @param pObj the device pObj
  2296. * @param Status FIFO stop on watermark status
  2297. * @retval 0 in case of success, an error code otherwise
  2298. */
  2299. int32_t LSM6DSL_FIFO_Set_Stop_On_Fth(LSM6DSL_Object_t *pObj, uint8_t Status)
  2300. {
  2301. if (lsm6dsl_fifo_stop_on_wtm_set(&(pObj->Ctx), Status) != LSM6DSL_OK)
  2302. {
  2303. return LSM6DSL_ERROR;
  2304. }
  2305. return LSM6DSL_OK;
  2306. }
  2307. /**
  2308. * @brief Set the LSM6DSL FIFO mode
  2309. * @param pObj the device pObj
  2310. * @param Mode FIFO mode
  2311. * @retval 0 in case of success, an error code otherwise
  2312. */
  2313. int32_t LSM6DSL_FIFO_Set_Mode(LSM6DSL_Object_t *pObj, uint8_t Mode)
  2314. {
  2315. int32_t ret = LSM6DSL_OK;
  2316. /* Verify that the passed parameter contains one of the valid values. */
  2317. switch ((lsm6dsl_fifo_mode_t)Mode)
  2318. {
  2319. case LSM6DSL_BYPASS_MODE:
  2320. case LSM6DSL_FIFO_MODE:
  2321. case LSM6DSL_STREAM_TO_FIFO_MODE:
  2322. case LSM6DSL_BYPASS_TO_STREAM_MODE:
  2323. case LSM6DSL_STREAM_MODE:
  2324. break;
  2325. default:
  2326. ret = LSM6DSL_ERROR;
  2327. break;
  2328. }
  2329. if (ret == LSM6DSL_ERROR)
  2330. {
  2331. return ret;
  2332. }
  2333. if (lsm6dsl_fifo_mode_set(&(pObj->Ctx), (lsm6dsl_fifo_mode_t)Mode) != LSM6DSL_OK)
  2334. {
  2335. return LSM6DSL_ERROR;
  2336. }
  2337. return ret;
  2338. }
  2339. /**
  2340. * @brief Get the LSM6DSL FIFO pattern
  2341. * @param pObj the device pObj
  2342. * @param Pattern FIFO pattern
  2343. * @retval 0 in case of success, an error code otherwise
  2344. */
  2345. int32_t LSM6DSL_FIFO_Get_Pattern(LSM6DSL_Object_t *pObj, uint16_t *Pattern)
  2346. {
  2347. if (lsm6dsl_fifo_pattern_get(&(pObj->Ctx), Pattern) != LSM6DSL_OK)
  2348. {
  2349. return LSM6DSL_ERROR;
  2350. }
  2351. return LSM6DSL_OK;
  2352. }
  2353. /**
  2354. * @brief Get the LSM6DSL FIFO raw data
  2355. * @param pObj the device pObj
  2356. * @param Data FIFO raw data array [2]
  2357. * @retval 0 in case of success, an error code otherwise
  2358. */
  2359. int32_t LSM6DSL_FIFO_Get_Data(LSM6DSL_Object_t *pObj, uint8_t *Data)
  2360. {
  2361. if (lsm6dsl_read_reg(&(pObj->Ctx), LSM6DSL_FIFO_DATA_OUT_L, Data, 2) != LSM6DSL_OK)
  2362. {
  2363. return LSM6DSL_ERROR;
  2364. }
  2365. return LSM6DSL_OK;
  2366. }
  2367. /**
  2368. * @brief Set the LSM6DSL FIFO accelero decimation
  2369. * @param pObj the device pObj
  2370. * @param Decimation FIFO accelero decimation
  2371. * @retval 0 in case of success, an error code otherwise
  2372. */
  2373. int32_t LSM6DSL_FIFO_ACC_Set_Decimation(LSM6DSL_Object_t *pObj, uint8_t Decimation)
  2374. {
  2375. int32_t ret = LSM6DSL_OK;
  2376. /* Verify that the passed parameter contains one of the valid values. */
  2377. switch ((lsm6dsl_dec_fifo_xl_t)Decimation)
  2378. {
  2379. case LSM6DSL_FIFO_XL_DISABLE:
  2380. case LSM6DSL_FIFO_XL_NO_DEC:
  2381. case LSM6DSL_FIFO_XL_DEC_2:
  2382. case LSM6DSL_FIFO_XL_DEC_3:
  2383. case LSM6DSL_FIFO_XL_DEC_4:
  2384. case LSM6DSL_FIFO_XL_DEC_8:
  2385. case LSM6DSL_FIFO_XL_DEC_16:
  2386. case LSM6DSL_FIFO_XL_DEC_32:
  2387. break;
  2388. default:
  2389. ret = LSM6DSL_ERROR;
  2390. break;
  2391. }
  2392. if (ret == LSM6DSL_ERROR)
  2393. {
  2394. return ret;
  2395. }
  2396. if (lsm6dsl_fifo_xl_batch_set(&(pObj->Ctx), (lsm6dsl_dec_fifo_xl_t)Decimation) != LSM6DSL_OK)
  2397. {
  2398. return LSM6DSL_ERROR;
  2399. }
  2400. return ret;
  2401. }
  2402. /**
  2403. * @brief Get the LSM6DSL FIFO accelero single sample (16-bit data) and calculate acceleration [mg]
  2404. * @param pObj the device pObj
  2405. * @param Acceleration FIFO single accelero axis [mg]
  2406. * @retval 0 in case of success, an error code otherwise
  2407. */
  2408. int32_t LSM6DSL_FIFO_ACC_Get_Axis(LSM6DSL_Object_t *pObj, int32_t *Acceleration)
  2409. {
  2410. uint8_t data[2];
  2411. int16_t data_raw;
  2412. float sensitivity = 0.0f;
  2413. float acceleration_float;
  2414. if (LSM6DSL_FIFO_Get_Data(pObj, data) != LSM6DSL_OK)
  2415. {
  2416. return LSM6DSL_ERROR;
  2417. }
  2418. data_raw = ((int16_t)data[1] << 8) | data[0];
  2419. if (LSM6DSL_ACC_GetSensitivity(pObj, &sensitivity) != LSM6DSL_OK)
  2420. {
  2421. return LSM6DSL_ERROR;
  2422. }
  2423. acceleration_float = (float)data_raw * sensitivity;
  2424. *Acceleration = (int32_t)acceleration_float;
  2425. return LSM6DSL_OK;
  2426. }
  2427. /**
  2428. * @brief Set the LSM6DSL FIFO gyro decimation
  2429. * @param pObj the device pObj
  2430. * @param Decimation FIFO gyro decimation
  2431. * @retval 0 in case of success, an error code otherwise
  2432. */
  2433. int32_t LSM6DSL_FIFO_GYRO_Set_Decimation(LSM6DSL_Object_t *pObj, uint8_t Decimation)
  2434. {
  2435. int32_t ret = LSM6DSL_OK;
  2436. /* Verify that the passed parameter contains one of the valid values. */
  2437. switch ((lsm6dsl_dec_fifo_gyro_t)Decimation)
  2438. {
  2439. case LSM6DSL_FIFO_GY_DISABLE:
  2440. case LSM6DSL_FIFO_GY_NO_DEC:
  2441. case LSM6DSL_FIFO_GY_DEC_2:
  2442. case LSM6DSL_FIFO_GY_DEC_3:
  2443. case LSM6DSL_FIFO_GY_DEC_4:
  2444. case LSM6DSL_FIFO_GY_DEC_8:
  2445. case LSM6DSL_FIFO_GY_DEC_16:
  2446. case LSM6DSL_FIFO_GY_DEC_32:
  2447. break;
  2448. default:
  2449. ret = LSM6DSL_ERROR;
  2450. break;
  2451. }
  2452. if (ret == LSM6DSL_ERROR)
  2453. {
  2454. return ret;
  2455. }
  2456. if (lsm6dsl_fifo_gy_batch_set(&(pObj->Ctx), (lsm6dsl_dec_fifo_gyro_t)Decimation) != LSM6DSL_OK)
  2457. {
  2458. return LSM6DSL_ERROR;
  2459. }
  2460. return ret;
  2461. }
  2462. /**
  2463. * @brief Get the LSM6DSL FIFO gyro single sample (16-bit data) and calculate angular velocity [mDPS]
  2464. * @param pObj the device pObj
  2465. * @param AngularVelocity FIFO single gyro axis [mDPS]
  2466. * @retval 0 in case of success, an error code otherwise
  2467. */
  2468. int32_t LSM6DSL_FIFO_GYRO_Get_Axis(LSM6DSL_Object_t *pObj, int32_t *AngularVelocity)
  2469. {
  2470. uint8_t data[2];
  2471. int16_t data_raw;
  2472. float sensitivity = 0.0f;
  2473. float angular_velocity_float;
  2474. if (LSM6DSL_FIFO_Get_Data(pObj, data) != LSM6DSL_OK)
  2475. {
  2476. return LSM6DSL_ERROR;
  2477. }
  2478. data_raw = ((int16_t)data[1] << 8) | data[0];
  2479. if (LSM6DSL_GYRO_GetSensitivity(pObj, &sensitivity) != LSM6DSL_OK)
  2480. {
  2481. return LSM6DSL_ERROR;
  2482. }
  2483. angular_velocity_float = (float)data_raw * sensitivity;
  2484. *AngularVelocity = (int32_t)angular_velocity_float;
  2485. return LSM6DSL_OK;
  2486. }
  2487. /**
  2488. * @}
  2489. */
  2490. /** @defgroup LSM6DSL_Private_Functions LSM6DSL Private Functions
  2491. * @{
  2492. */
  2493. /**
  2494. * @brief Set the LSM6DSL accelerometer sensor output data rate when enabled
  2495. * @param pObj the device pObj
  2496. * @param Odr the functional output data rate to be set
  2497. * @retval 0 in case of success, an error code otherwise
  2498. */
  2499. static int32_t LSM6DSL_ACC_SetOutputDataRate_When_Enabled(LSM6DSL_Object_t *pObj, float Odr)
  2500. {
  2501. lsm6dsl_odr_xl_t new_odr;
  2502. new_odr = (Odr <= 12.5f) ? LSM6DSL_XL_ODR_12Hz5
  2503. : (Odr <= 26.0f) ? LSM6DSL_XL_ODR_26Hz
  2504. : (Odr <= 52.0f) ? LSM6DSL_XL_ODR_52Hz
  2505. : (Odr <= 104.0f) ? LSM6DSL_XL_ODR_104Hz
  2506. : (Odr <= 208.0f) ? LSM6DSL_XL_ODR_208Hz
  2507. : (Odr <= 416.0f) ? LSM6DSL_XL_ODR_416Hz
  2508. : (Odr <= 833.0f) ? LSM6DSL_XL_ODR_833Hz
  2509. : (Odr <= 1660.0f) ? LSM6DSL_XL_ODR_1k66Hz
  2510. : (Odr <= 3330.0f) ? LSM6DSL_XL_ODR_3k33Hz
  2511. : LSM6DSL_XL_ODR_6k66Hz;
  2512. /* Output data rate selection. */
  2513. if (lsm6dsl_xl_data_rate_set(&(pObj->Ctx), new_odr) != LSM6DSL_OK)
  2514. {
  2515. return LSM6DSL_ERROR;
  2516. }
  2517. return LSM6DSL_OK;
  2518. }
  2519. /**
  2520. * @brief Set the LSM6DSL accelerometer sensor output data rate when disabled
  2521. * @param pObj the device pObj
  2522. * @param Odr the functional output data rate to be set
  2523. * @retval 0 in case of success, an error code otherwise
  2524. */
  2525. static int32_t LSM6DSL_ACC_SetOutputDataRate_When_Disabled(LSM6DSL_Object_t *pObj, float Odr)
  2526. {
  2527. pObj->acc_odr = (Odr <= 12.5f) ? LSM6DSL_XL_ODR_12Hz5
  2528. : (Odr <= 26.0f) ? LSM6DSL_XL_ODR_26Hz
  2529. : (Odr <= 52.0f) ? LSM6DSL_XL_ODR_52Hz
  2530. : (Odr <= 104.0f) ? LSM6DSL_XL_ODR_104Hz
  2531. : (Odr <= 208.0f) ? LSM6DSL_XL_ODR_208Hz
  2532. : (Odr <= 416.0f) ? LSM6DSL_XL_ODR_416Hz
  2533. : (Odr <= 833.0f) ? LSM6DSL_XL_ODR_833Hz
  2534. : (Odr <= 1660.0f) ? LSM6DSL_XL_ODR_1k66Hz
  2535. : (Odr <= 3330.0f) ? LSM6DSL_XL_ODR_3k33Hz
  2536. : LSM6DSL_XL_ODR_6k66Hz;
  2537. return LSM6DSL_OK;
  2538. }
  2539. /**
  2540. * @brief Set the LSM6DSL gyroscope sensor output data rate when enabled
  2541. * @param pObj the device pObj
  2542. * @param Odr the functional output data rate to be set
  2543. * @retval 0 in case of success, an error code otherwise
  2544. */
  2545. static int32_t LSM6DSL_GYRO_SetOutputDataRate_When_Enabled(LSM6DSL_Object_t *pObj, float Odr)
  2546. {
  2547. lsm6dsl_odr_g_t new_odr;
  2548. new_odr = (Odr <= 12.5f) ? LSM6DSL_GY_ODR_12Hz5
  2549. : (Odr <= 26.0f) ? LSM6DSL_GY_ODR_26Hz
  2550. : (Odr <= 52.0f) ? LSM6DSL_GY_ODR_52Hz
  2551. : (Odr <= 104.0f) ? LSM6DSL_GY_ODR_104Hz
  2552. : (Odr <= 208.0f) ? LSM6DSL_GY_ODR_208Hz
  2553. : (Odr <= 416.0f) ? LSM6DSL_GY_ODR_416Hz
  2554. : (Odr <= 833.0f) ? LSM6DSL_GY_ODR_833Hz
  2555. : (Odr <= 1660.0f) ? LSM6DSL_GY_ODR_1k66Hz
  2556. : (Odr <= 3330.0f) ? LSM6DSL_GY_ODR_3k33Hz
  2557. : LSM6DSL_GY_ODR_6k66Hz;
  2558. /* Output data rate selection. */
  2559. if (lsm6dsl_gy_data_rate_set(&(pObj->Ctx), new_odr) != LSM6DSL_OK)
  2560. {
  2561. return LSM6DSL_ERROR;
  2562. }
  2563. return LSM6DSL_OK;
  2564. }
  2565. /**
  2566. * @brief Set the LSM6DSL gyroscope sensor output data rate when disabled
  2567. * @param pObj the device pObj
  2568. * @param Odr the functional output data rate to be set
  2569. * @retval 0 in case of success, an error code otherwise
  2570. */
  2571. static int32_t LSM6DSL_GYRO_SetOutputDataRate_When_Disabled(LSM6DSL_Object_t *pObj, float Odr)
  2572. {
  2573. pObj->gyro_odr = (Odr <= 12.5f) ? LSM6DSL_GY_ODR_12Hz5
  2574. : (Odr <= 26.0f) ? LSM6DSL_GY_ODR_26Hz
  2575. : (Odr <= 52.0f) ? LSM6DSL_GY_ODR_52Hz
  2576. : (Odr <= 104.0f) ? LSM6DSL_GY_ODR_104Hz
  2577. : (Odr <= 208.0f) ? LSM6DSL_GY_ODR_208Hz
  2578. : (Odr <= 416.0f) ? LSM6DSL_GY_ODR_416Hz
  2579. : (Odr <= 833.0f) ? LSM6DSL_GY_ODR_833Hz
  2580. : (Odr <= 1660.0f) ? LSM6DSL_GY_ODR_1k66Hz
  2581. : (Odr <= 3330.0f) ? LSM6DSL_GY_ODR_3k33Hz
  2582. : LSM6DSL_GY_ODR_6k66Hz;
  2583. return LSM6DSL_OK;
  2584. }
  2585. /**
  2586. * @brief Wrap Read register component function to Bus IO function
  2587. * @param Handle the device handler
  2588. * @param Reg the register address
  2589. * @param pData the stored data pointer
  2590. * @param Length the length
  2591. * @retval 0 in case of success, an error code otherwise
  2592. */
  2593. static int32_t ReadRegWrap(void *Handle, uint8_t Reg, uint8_t *pData, uint16_t Length)
  2594. {
  2595. LSM6DSL_Object_t *pObj = (LSM6DSL_Object_t *)Handle;
  2596. return pObj->IO.ReadReg(pObj->IO.Address, Reg, pData, Length);
  2597. }
  2598. /**
  2599. * @brief Wrap Write register component function to Bus IO function
  2600. * @param Handle the device handler
  2601. * @param Reg the register address
  2602. * @param pData the stored data pointer
  2603. * @param Length the length
  2604. * @retval 0 in case of success, an error code otherwise
  2605. */
  2606. static int32_t WriteRegWrap(void *Handle, uint8_t Reg, uint8_t *pData, uint16_t Length)
  2607. {
  2608. LSM6DSL_Object_t *pObj = (LSM6DSL_Object_t *)Handle;
  2609. return pObj->IO.WriteReg(pObj->IO.Address, Reg, pData, Length);
  2610. }
  2611. /**
  2612. * @}
  2613. */
  2614. /**
  2615. * @}
  2616. */
  2617. /**
  2618. * @}
  2619. */
  2620. /**
  2621. * @}
  2622. */
  2623. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/