1 /**
2   ******************************************************************************
3   * @file    iis3dwb_reg.c
4   * @author  Sensors Software Solution Team
5   * @brief   IIS3DWB driver file
6   ******************************************************************************
7   * @attention
8   *
9   * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
10   * All rights reserved.</center></h2>
11   *
12   * This software component is licensed by ST under BSD 3-Clause license,
13   * the "License"; You may not use this file except in compliance with the
14   * License. You may obtain a copy of the License at:
15   *                        opensource.org/licenses/BSD-3-Clause
16   *
17   ******************************************************************************
18   */
19 
20 #include "iis3dwb_reg.h"
21 #include <string.h>
22 
23 /**
24   * @defgroup    IIS3DWB
25   * @brief       This file provides a set of functions needed to drive the
26   *              iis3dwb enhanced inertial module.
27   * @{
28   *
29   */
30 
31 /**
32   * @defgroup    IIS3DWB_Interfaces_Functions
33   * @brief       This section provide a set of functions used to read and
34   *              write a generic register of the device.
35   *              MANDATORY: return 0 -> no Error.
36   * @{
37   *
38   */
39 
40 /**
41   * @brief  Read generic device register
42   *
43   * @param  ctx   read / write interface definitions(ptr)
44   * @param  reg   register to read
45   * @param  data  pointer to buffer that store the data read(ptr)
46   * @param  len   number of consecutive register to read
47   * @retval       interface status (MANDATORY: return 0 -> no Error)
48   *
49   */
iis3dwb_read_reg(const stmdev_ctx_t * ctx,uint8_t reg,uint8_t * data,uint16_t len)50 int32_t __weak iis3dwb_read_reg(const stmdev_ctx_t *ctx, uint8_t reg,
51                                 uint8_t *data,
52                                 uint16_t len)
53 {
54   if (ctx == NULL) return -1;
55 
56   return ctx->read_reg(ctx->handle, reg, data, len);
57 }
58 
59 /**
60   * @brief  Write generic device register
61   *
62   * @param  ctx   read / write interface definitions(ptr)
63   * @param  reg   register to write
64   * @param  data  pointer to data to write in register reg(ptr)
65   * @param  len   number of consecutive register to write
66   * @retval       interface status (MANDATORY: return 0 -> no Error)
67   *
68   */
iis3dwb_write_reg(const stmdev_ctx_t * ctx,uint8_t reg,uint8_t * data,uint16_t len)69 int32_t __weak iis3dwb_write_reg(const stmdev_ctx_t *ctx, uint8_t reg,
70                                  uint8_t *data,
71                                  uint16_t len)
72 {
73   if (ctx == NULL) return -1;
74 
75   return ctx->write_reg(ctx->handle, reg, data, len);
76 }
77 
78 /**
79   * @}
80   *
81   */
82 
83 /**
84   * @defgroup  Private functions
85   * @brief     Section collect all the utility functions needed by APIs.
86   * @{
87   *
88   */
89 
bytecpy(uint8_t * target,const uint8_t * source)90 static void bytecpy(uint8_t *target, const uint8_t *source)
91 {
92   if ((target != NULL) && (source != NULL))
93   {
94     *target = *source;
95   }
96 }
97 
98 /**
99   * @}
100   *
101   */
102 
103 /**
104   * @defgroup    IIS3DWB_Sensitivity
105   * @brief       These functions convert raw-data into engineering units.
106   * @{
107   *
108   */
109 
iis3dwb_from_fs2g_to_mg(int16_t lsb)110 float_t iis3dwb_from_fs2g_to_mg(int16_t lsb)
111 {
112   return ((float_t)lsb * 0.061f);
113 }
114 
iis3dwb_from_fs4g_to_mg(int16_t lsb)115 float_t iis3dwb_from_fs4g_to_mg(int16_t lsb)
116 {
117   return ((float_t)lsb * 0.122f);
118 }
119 
iis3dwb_from_fs8g_to_mg(int16_t lsb)120 float_t iis3dwb_from_fs8g_to_mg(int16_t lsb)
121 {
122   return ((float_t)lsb * 0.244f);
123 }
124 
iis3dwb_from_fs16g_to_mg(int16_t lsb)125 float_t iis3dwb_from_fs16g_to_mg(int16_t lsb)
126 {
127   return ((float_t)lsb * 0.488f);
128 }
129 
iis3dwb_from_lsb_to_celsius(int16_t lsb)130 float_t iis3dwb_from_lsb_to_celsius(int16_t lsb)
131 {
132   return (((float_t)lsb / 256.0f) + 25.0f);
133 }
134 
iis3dwb_from_lsb_to_nsec(int32_t lsb)135 float_t iis3dwb_from_lsb_to_nsec(int32_t lsb)
136 {
137   return ((float_t)lsb * 25000.0f);
138 }
139 
140 /**
141   * @}
142   *
143   */
144 
145 /**
146   * @defgroup   LSM9DS1_Data_generation
147   * @brief      This section groups all the functions concerning data
148   *             generation
149   * @{
150   *
151   */
152 
153 /**
154   * @brief  Accelerometer full-scale selection[set]
155   *
156   * @param  ctx    Read / write interface definitions.(ptr)
157   * @param  val    Change the values of fs_xl in reg CTRL1_XL
158   * @retval        Interface status (MANDATORY: return 0 -> no Error).
159   *
160   */
iis3dwb_xl_full_scale_set(const stmdev_ctx_t * ctx,iis3dwb_fs_xl_t val)161 int32_t iis3dwb_xl_full_scale_set(const stmdev_ctx_t *ctx,
162                                   iis3dwb_fs_xl_t val)
163 {
164   iis3dwb_ctrl1_xl_t ctrl1_xl;
165 
166   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL1_XL, (uint8_t *)&ctrl1_xl, 1);
167 
168   if (ret == 0)
169   {
170     ctrl1_xl.fs_xl = (uint8_t)val;
171     ret = iis3dwb_write_reg(ctx, IIS3DWB_CTRL1_XL,
172                             (uint8_t *)&ctrl1_xl, 1);
173   }
174 
175   return ret;
176 }
177 
178 /**
179   * @brief  Accelerometer full-scale selection.[get]
180   *
181   * @param  ctx    Read / write interface definitions.(ptr)
182   * @param  val    Get the values of fs_xl in reg CTRL1_XL
183   * @retval        Interface status (MANDATORY: return 0 -> no Error).
184   *
185   */
iis3dwb_xl_full_scale_get(const stmdev_ctx_t * ctx,iis3dwb_fs_xl_t * val)186 int32_t iis3dwb_xl_full_scale_get(const stmdev_ctx_t *ctx,
187                                   iis3dwb_fs_xl_t *val)
188 {
189   iis3dwb_ctrl1_xl_t ctrl1_xl;
190 
191   const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL1_XL, (uint8_t *)&ctrl1_xl, 1);
192 
193   switch (ctrl1_xl.fs_xl)
194   {
195     case IIS3DWB_2g:
196       *val = IIS3DWB_2g;
197       break;
198 
199     case IIS3DWB_16g:
200       *val = IIS3DWB_16g;
201       break;
202 
203     case IIS3DWB_4g:
204       *val = IIS3DWB_4g;
205       break;
206 
207     case IIS3DWB_8g:
208       *val = IIS3DWB_8g;
209       break;
210 
211     default:
212       *val = IIS3DWB_2g;
213       break;
214   }
215 
216   return ret;
217 }
218 
219 /**
220   * @brief  Accelerometer UI data rate selection.[set]
221   *
222   * @param  ctx    Read / write interface definitions.(ptr)
223   * @param  val    Change the values of xl_en in reg CTRL1_XL
224   * @retval        Interface status (MANDATORY: return 0 -> no Error).
225   *
226   */
iis3dwb_xl_data_rate_set(const stmdev_ctx_t * ctx,iis3dwb_odr_xl_t val)227 int32_t iis3dwb_xl_data_rate_set(const stmdev_ctx_t *ctx,
228                                  iis3dwb_odr_xl_t val)
229 {
230   iis3dwb_ctrl1_xl_t ctrl1_xl;
231 
232   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL1_XL, (uint8_t *)&ctrl1_xl, 1);
233 
234   if (ret == 0)
235   {
236     ctrl1_xl.xl_en = (uint8_t)val;
237     ret = iis3dwb_write_reg(ctx, IIS3DWB_CTRL1_XL,
238                             (uint8_t *)&ctrl1_xl, 1);
239   }
240 
241   return ret;
242 }
243 
244 /**
245   * @brief  Accelerometer UI data rate selection.[get]
246   *
247   * @param  ctx    Read / write interface definitions.(ptr)
248   * @param  val    Get the values of odr_xl in reg CTRL1_XL
249   * @retval        Interface status (MANDATORY: return 0 -> no Error).
250   *
251   */
iis3dwb_xl_data_rate_get(const stmdev_ctx_t * ctx,iis3dwb_odr_xl_t * val)252 int32_t iis3dwb_xl_data_rate_get(const stmdev_ctx_t *ctx,
253                                  iis3dwb_odr_xl_t *val)
254 {
255   iis3dwb_ctrl1_xl_t ctrl1_xl;
256 
257   const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL1_XL, (uint8_t *)&ctrl1_xl, 1);
258 
259   switch (ctrl1_xl.xl_en)
260   {
261     case IIS3DWB_XL_ODR_OFF:
262       *val = IIS3DWB_XL_ODR_OFF;
263       break;
264 
265     case IIS3DWB_XL_ODR_26k7Hz:
266       *val = IIS3DWB_XL_ODR_26k7Hz;
267       break;
268 
269     default:
270       *val = IIS3DWB_XL_ODR_OFF;
271       break;
272   }
273 
274   return ret;
275 }
276 
277 /**
278   * @brief  Block data update.[set]
279   *
280   * @param  ctx    Read / write interface definitions.(ptr)
281   * @param  val    Change the values of bdu in reg CTRL3_C
282   * @retval        Interface status (MANDATORY: return 0 -> no Error).
283   *
284   */
iis3dwb_block_data_update_set(const stmdev_ctx_t * ctx,uint8_t val)285 int32_t iis3dwb_block_data_update_set(const stmdev_ctx_t *ctx, uint8_t val)
286 {
287   iis3dwb_ctrl3_c_t ctrl3_c;
288 
289   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL3_C, (uint8_t *)&ctrl3_c, 1);
290 
291   if (ret == 0)
292   {
293     ctrl3_c.bdu = (uint8_t)val;
294     ret = iis3dwb_write_reg(ctx, IIS3DWB_CTRL3_C, (uint8_t *)&ctrl3_c, 1);
295   }
296 
297   return ret;
298 }
299 
300 /**
301   * @brief  Block data update.[get]
302   *
303   * @param  ctx    Read / write interface definitions.(ptr)
304   * @param  val    Change the values of bdu in reg CTRL3_C
305   * @retval        Interface status (MANDATORY: return 0 -> no Error).
306   *
307   */
iis3dwb_block_data_update_get(const stmdev_ctx_t * ctx,uint8_t * val)308 int32_t iis3dwb_block_data_update_get(const stmdev_ctx_t *ctx, uint8_t *val)
309 {
310   iis3dwb_ctrl3_c_t ctrl3_c;
311 
312   const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL3_C, (uint8_t *)&ctrl3_c, 1);
313   *val = ctrl3_c.bdu;
314 
315   return ret;
316 }
317 
318 /**
319   * @brief  Weight of XL user offset bits of registers X_OFS_USR (73h),
320   *         Y_OFS_USR (74h), Z_OFS_USR (75h).[set]
321   *
322   * @param  ctx    Read / write interface definitions.(ptr)
323   * @param  val    Change the values of usr_off_w in reg CTRL6_C
324   * @retval        Interface status (MANDATORY: return 0 -> no Error).
325   *
326   */
iis3dwb_xl_offset_weight_set(const stmdev_ctx_t * ctx,iis3dwb_usr_off_w_t val)327 int32_t iis3dwb_xl_offset_weight_set(const stmdev_ctx_t *ctx,
328                                      iis3dwb_usr_off_w_t val)
329 {
330   iis3dwb_ctrl6_c_t ctrl6_c;
331 
332   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL6_C, (uint8_t *)&ctrl6_c, 1);
333 
334   if (ret == 0)
335   {
336     ctrl6_c.usr_off_w = (uint8_t)val;
337     ret = iis3dwb_write_reg(ctx, IIS3DWB_CTRL6_C, (uint8_t *)&ctrl6_c, 1);
338   }
339 
340   return ret;
341 }
342 
343 /**
344   * @brief  Weight of XL user offset bits of registers X_OFS_USR (73h),
345   *         Y_OFS_USR (74h), Z_OFS_USR (75h).[get]
346   *
347   * @param  ctx    Read / write interface definitions.(ptr)
348   * @param  val    Get the values of usr_off_w in reg CTRL6_C
349   * @retval        Interface status (MANDATORY: return 0 -> no Error).
350   *
351   */
iis3dwb_xl_offset_weight_get(const stmdev_ctx_t * ctx,iis3dwb_usr_off_w_t * val)352 int32_t iis3dwb_xl_offset_weight_get(const stmdev_ctx_t *ctx,
353                                      iis3dwb_usr_off_w_t *val)
354 {
355   iis3dwb_ctrl6_c_t ctrl6_c;
356 
357   const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL6_C, (uint8_t *)&ctrl6_c, 1);
358 
359   switch (ctrl6_c.usr_off_w)
360   {
361     case IIS3DWB_LSb_1mg:
362       *val = IIS3DWB_LSb_1mg;
363       break;
364 
365     case IIS3DWB_LSb_16mg:
366       *val = IIS3DWB_LSb_16mg;
367       break;
368 
369     default:
370       *val = IIS3DWB_LSb_1mg;
371       break;
372   }
373 
374   return ret;
375 }
376 
377 /**
378   * @brief  select accelerometer axis.[set]
379   *
380   * @param  ctx    Read / write interface definitions.(ptr)
381   * @param  val    Change the values of xl_axis_sel in reg CTRL6_C and
382   *                the values of _1ax_to_3regout in reg CTRL4_C
383   * @retval        Interface status (MANDATORY: return 0 -> no Error).
384   *
385   */
iis3dwb_xl_axis_selection_set(const stmdev_ctx_t * ctx,iis3dwb_xl_axis_sel_t val)386 int32_t iis3dwb_xl_axis_selection_set(const stmdev_ctx_t *ctx,
387                                       iis3dwb_xl_axis_sel_t val)
388 {
389   iis3dwb_ctrl4_c_t ctrl4_c;
390   iis3dwb_ctrl6_c_t ctrl6_c;
391 
392   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL4_C, (uint8_t *)&ctrl4_c, 1);
393 
394   if (ret == 0)
395   {
396     ctrl4_c._1ax_to_3regout = ((uint8_t)val & 0x10U) >> 4;
397     ret = iis3dwb_write_reg(ctx, IIS3DWB_CTRL4_C, (uint8_t *)&ctrl4_c, 1);
398   }
399 
400   if (ret == 0)
401   {
402     ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL6_C, (uint8_t *)&ctrl6_c, 1);
403   }
404 
405   if (ret == 0)
406   {
407     ctrl6_c.xl_axis_sel = (uint8_t)val;
408     ret = iis3dwb_write_reg(ctx, IIS3DWB_CTRL6_C, (uint8_t *)&ctrl6_c, 1);
409   }
410 
411   return ret;
412 }
413 
414 /**
415   * @brief  select accelerometer axis.[get]
416   *
417   * @param  ctx    Read / write interface definitions.(ptr)
418   * @param  val    Get the values of xl_axis_sel in reg CTRL6_C and
419   *                the values of _1ax_to_3regout in reg CTRL4_C.(ptr)
420   * @retval        Interface status (MANDATORY: return 0 -> no Error).
421   *
422   */
iis3dwb_xl_axis_selection_get(const stmdev_ctx_t * ctx,iis3dwb_xl_axis_sel_t * val)423 int32_t iis3dwb_xl_axis_selection_get(const stmdev_ctx_t *ctx,
424                                       iis3dwb_xl_axis_sel_t *val)
425 {
426   iis3dwb_ctrl4_c_t ctrl4_c;
427   iis3dwb_ctrl6_c_t ctrl6_c;
428 
429   *val = IIS3DWB_ENABLE_ALL;
430 
431   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL4_C, (uint8_t *)&ctrl4_c, 1);
432   if (ret != 0) { return ret; }
433 
434   ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL6_C, (uint8_t *)&ctrl6_c, 1);
435   if (ret != 0) { return ret; }
436 
437   switch ((ctrl4_c._1ax_to_3regout << 4) + ctrl6_c.xl_axis_sel)
438   {
439     case IIS3DWB_ENABLE_ALL:
440       *val = IIS3DWB_ENABLE_ALL;
441       break;
442 
443     case IIS3DWB_ONLY_X_ON_ONE_OUT_REG:
444       *val = IIS3DWB_ONLY_X_ON_ONE_OUT_REG;
445       break;
446 
447     case IIS3DWB_ONLY_Y_ON_ONE_OUT_REG:
448       *val = IIS3DWB_ONLY_Y_ON_ONE_OUT_REG;
449       break;
450 
451     case IIS3DWB_ONLY_Z_ON_ONE_OUT_REG:
452       *val = IIS3DWB_ONLY_Z_ON_ONE_OUT_REG;
453       break;
454 
455     case IIS3DWB_ONLY_X_ON_ALL_OUT_REG:
456       *val = IIS3DWB_ONLY_X_ON_ALL_OUT_REG;
457       break;
458 
459     case IIS3DWB_ONLY_Y_ON_ALL_OUT_REG:
460       *val = IIS3DWB_ONLY_Y_ON_ALL_OUT_REG;
461       break;
462 
463     case IIS3DWB_ONLY_Z_ON_ALL_OUT_REG:
464       *val = IIS3DWB_ONLY_Z_ON_ALL_OUT_REG;
465       break;
466 
467     default:
468       *val = IIS3DWB_ENABLE_ALL;
469       break;
470   }
471 
472   return ret;
473 }
474 
475 /**
476   * @brief  Read all the interrupt flag of the device.[get]
477   * @param  ctx    Read / write interface definitions.(ptr)
478   * @param  val    Get registers ALL_INT_SRC; WAKE_UP_SRC;
479   *                              TAP_SRC; D6D_SRC; STATUS_REG;
480   *                              EMB_FUNC_STATUS; FSM_STATUS_A/B
481   * @retval        Interface status (MANDATORY: return 0 -> no Error).
482   *
483   */
iis3dwb_all_sources_get(const stmdev_ctx_t * ctx,iis3dwb_all_sources_t * val)484 int32_t iis3dwb_all_sources_get(const stmdev_ctx_t *ctx,
485                                 iis3dwb_all_sources_t *val)
486 {
487   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_ALL_INT_SRC,
488                          (uint8_t *)&val->all_int_src, 1);
489 
490   if (ret == 0)
491   {
492     ret = iis3dwb_read_reg(ctx, IIS3DWB_WAKE_UP_SRC,
493                            (uint8_t *)&val->wake_up_src, 1);
494   }
495 
496   if (ret == 0)
497   {
498     ret = iis3dwb_read_reg(ctx, IIS3DWB_STATUS_REG,
499                            (uint8_t *)&val->status_reg, 1);
500   }
501 
502   return ret;
503 }
504 
505 /**
506   * @brief  The STATUS_REG register is read by the primary interface.[get]
507   *
508   * @param  ctx    Read / write interface definitions.(ptr)
509   * @param  val    Get register STATUS_REG
510   * @retval        Interface status (MANDATORY: return 0 -> no Error).
511   *
512   */
iis3dwb_status_reg_get(const stmdev_ctx_t * ctx,iis3dwb_status_reg_t * val)513 int32_t iis3dwb_status_reg_get(const stmdev_ctx_t *ctx,
514                                iis3dwb_status_reg_t *val)
515 {
516   const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_STATUS_REG, (uint8_t *) val, 1);
517 
518   return ret;
519 }
520 
521 /**
522   * @brief  Accelerometer new data available.[get]
523   *
524   * @param  ctx    Read / write interface definitions.(ptr)
525   * @param  val    Change the values of xlda in reg STATUS_REG
526   * @retval        Interface status (MANDATORY: return 0 -> no Error).
527   *
528   */
iis3dwb_xl_flag_data_ready_get(const stmdev_ctx_t * ctx,uint8_t * val)529 int32_t iis3dwb_xl_flag_data_ready_get(const stmdev_ctx_t *ctx,
530                                        uint8_t *val)
531 {
532   iis3dwb_status_reg_t status_reg;
533 
534   const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_STATUS_REG,
535                          (uint8_t *)&status_reg, 1);
536   *val = status_reg.xlda;
537 
538   return ret;
539 }
540 
541 /**
542   * @brief  Temperature new data available.[get]
543   *
544   * @param  ctx    Read / write interface definitions.(ptr)
545   * @param  val    Change the values of tda in reg STATUS_REG
546   * @retval        Interface status (MANDATORY: return 0 -> no Error).
547   *
548   */
iis3dwb_temp_flag_data_ready_get(const stmdev_ctx_t * ctx,uint8_t * val)549 int32_t iis3dwb_temp_flag_data_ready_get(const stmdev_ctx_t *ctx,
550                                          uint8_t *val)
551 {
552   iis3dwb_status_reg_t status_reg;
553 
554   const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_STATUS_REG,
555                          (uint8_t *)&status_reg, 1);
556   *val = status_reg.tda;
557 
558   return ret;
559 }
560 
561 /**
562   * @brief  Enables the accelerometer user offset correction block, can be enabled
563   * by setting the USR_OFF_ON_OUT bit of the CTRL7_C register.[set]
564   *
565   * @param  ctx    Read / write interface definitions.(ptr)
566   * @param  val    Change the values of USR_OFF_ON_OUT in reg CTRL7_C
567   * @retval        Interface status (MANDATORY: return 0 -> no Error).
568   *
569   */
iis3dwb_usr_offset_block_set(const stmdev_ctx_t * ctx,uint8_t val)570 int32_t iis3dwb_usr_offset_block_set(const stmdev_ctx_t *ctx, uint8_t val)
571 {
572   iis3dwb_ctrl7_c_t ctrl7_c;
573 
574   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL7_C, (uint8_t *)&ctrl7_c, 1);
575 
576   if (ret == 0)
577   {
578     ctrl7_c.usr_off_on_out = val;
579     ret = iis3dwb_write_reg(ctx, IIS3DWB_CTRL7_C, (uint8_t *)&ctrl7_c, 1);
580   }
581 
582   return ret;
583 }
584 
585 /**
586   * @brief  Enables the accelerometer user offset correction block, can be enabled
587   * by setting the USR_OFF_ON_OUT bit of the CTRL7_C register.[get]
588   *
589   * @param  ctx    Read / write interface definitions.(ptr)
590   * @param  val    Change the values of USR_OFF_ON_OUT in reg CTRL7_C
591   * @retval        Interface status (MANDATORY: return 0 -> no Error).
592   *
593   */
iis3dwb_usr_offset_block_get(const stmdev_ctx_t * ctx,uint8_t * val)594 int32_t iis3dwb_usr_offset_block_get(const stmdev_ctx_t *ctx, uint8_t *val)
595 {
596   iis3dwb_ctrl7_c_t ctrl7_c;
597 
598   const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL7_C, (uint8_t *)&ctrl7_c, 1);
599   *val = ctrl7_c.usr_off_on_out;
600 
601   return ret;
602 }
603 
604 /**
605   * @brief  Accelerometer X-axis user offset correction expressed in two’s
606   *         complement, weight depends on USR_OFF_W in CTRL6_C (15h).
607   *         The value must be in the range [-127 127].[set]
608   *
609   * @param  ctx    Read / write interface definitions.(ptr)
610   * @param  buff   Buffer that contains data to write
611   * @retval        Interface status (MANDATORY: return 0 -> no Error).
612   *
613   */
iis3dwb_xl_usr_offset_x_set(const stmdev_ctx_t * ctx,uint8_t * buff)614 int32_t iis3dwb_xl_usr_offset_x_set(const stmdev_ctx_t *ctx, uint8_t *buff)
615 {
616   const int32_t ret = iis3dwb_write_reg(ctx, IIS3DWB_X_OFS_USR, buff, 1);
617 
618   return ret;
619 }
620 
621 /**
622   * @brief  Accelerometer X-axis user offset correction expressed in two’s
623   *         complement, weight depends on USR_OFF_W in CTRL6_C (15h).
624   *         The value must be in the range [-127 127].[get]
625   *
626   * @param  ctx    Read / write interface definitions.(ptr)
627   * @param  buff   Buffer that stores data read
628   * @retval        Interface status (MANDATORY: return 0 -> no Error).
629   *
630   */
iis3dwb_xl_usr_offset_x_get(const stmdev_ctx_t * ctx,uint8_t * buff)631 int32_t iis3dwb_xl_usr_offset_x_get(const stmdev_ctx_t *ctx, uint8_t *buff)
632 {
633   const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_X_OFS_USR, buff, 1);
634 
635   return ret;
636 }
637 
638 /**
639   * @brief  Accelerometer Y-axis user offset correction expressed in two’s
640   *         complement, weight depends on USR_OFF_W in CTRL6_C (15h).
641   *         The value must be in the range [-127 127].[set]
642   *
643   * @param  ctx    Read / write interface definitions.(ptr)
644   * @param  buff   Buffer that contains data to write
645   * @retval        Interface status (MANDATORY: return 0 -> no Error).
646   *
647   */
iis3dwb_xl_usr_offset_y_set(const stmdev_ctx_t * ctx,uint8_t * buff)648 int32_t iis3dwb_xl_usr_offset_y_set(const stmdev_ctx_t *ctx, uint8_t *buff)
649 {
650   const int32_t ret = iis3dwb_write_reg(ctx, IIS3DWB_Y_OFS_USR, buff, 1);
651 
652   return ret;
653 }
654 
655 /**
656   * @brief  Accelerometer Y-axis user offset correction expressed in two’s
657   *         complement, weight depends on USR_OFF_W in CTRL6_C (15h).
658   *         The value must be in the range [-127 127].[get]
659   *
660   * @param  ctx    Read / write interface definitions.(ptr)
661   * @param  buff   Buffer that stores data read
662   * @retval        Interface status (MANDATORY: return 0 -> no Error).
663   *
664   */
iis3dwb_xl_usr_offset_y_get(const stmdev_ctx_t * ctx,uint8_t * buff)665 int32_t iis3dwb_xl_usr_offset_y_get(const stmdev_ctx_t *ctx, uint8_t *buff)
666 {
667   const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_Y_OFS_USR, buff, 1);
668 
669   return ret;
670 }
671 
672 /**
673   * @brief  Accelerometer Z-axis user offset correction expressed in two’s
674   *         complement, weight depends on USR_OFF_W in CTRL6_C (15h).
675   *         The value must be in the range [-127 127].[set]
676   *
677   * @param  ctx    Read / write interface definitions.(ptr)
678   * @param  buff   Buffer that contains data to write
679   * @retval        Interface status (MANDATORY: return 0 -> no Error).
680   *
681   */
iis3dwb_xl_usr_offset_z_set(const stmdev_ctx_t * ctx,uint8_t * buff)682 int32_t iis3dwb_xl_usr_offset_z_set(const stmdev_ctx_t *ctx, uint8_t *buff)
683 {
684   const int32_t ret = iis3dwb_write_reg(ctx, IIS3DWB_Z_OFS_USR, buff, 1);
685 
686   return ret;
687 }
688 
689 /**
690   * @brief  Accelerometer X-axis user offset correction expressed in two’s
691   *         complement, weight depends on USR_OFF_W in CTRL6_C (15h).
692   *         The value must be in the range [-127 127].[get]
693   *
694   * @param  ctx    Read / write interface definitions.(ptr)
695   * @param  buff   Buffer that stores data read
696   * @retval        Interface status (MANDATORY: return 0 -> no Error).
697   *
698   */
iis3dwb_xl_usr_offset_z_get(const stmdev_ctx_t * ctx,uint8_t * buff)699 int32_t iis3dwb_xl_usr_offset_z_get(const stmdev_ctx_t *ctx, uint8_t *buff)
700 {
701   const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_Z_OFS_USR, buff, 1);
702 
703   return ret;
704 }
705 
706 /**
707   * @}
708   *
709   */
710 
711 /**
712   * @defgroup   IIS3DWB_Timestamp
713   * @brief      This section groups all the functions that manage the
714   *             timestamp generation.
715   * @{
716   *
717   */
718 
719 /**
720   * @brief  Reset timestamp counter.[set]
721   *
722   * @param  ctx    Read / write interface definitions.(ptr)
723   * @retval        Interface status (MANDATORY: return 0 -> no Error).
724   *
725   */
iis3dwb_timestamp_rst(const stmdev_ctx_t * ctx)726 int32_t iis3dwb_timestamp_rst(const stmdev_ctx_t *ctx)
727 {
728   uint8_t rst_val = 0xAA;
729   return iis3dwb_write_reg(ctx, IIS3DWB_TIMESTAMP2, &rst_val, 1);
730 }
731 
732 /**
733   * @brief  Enables timestamp counter.[set]
734   *
735   * @param  ctx    Read / write interface definitions.(ptr)
736   * @param  val    Change the values of timestamp_en in reg CTRL10_C
737   * @retval        Interface status (MANDATORY: return 0 -> no Error).
738   *
739   */
iis3dwb_timestamp_set(const stmdev_ctx_t * ctx,uint8_t val)740 int32_t iis3dwb_timestamp_set(const stmdev_ctx_t *ctx, uint8_t val)
741 {
742   iis3dwb_ctrl10_c_t ctrl10_c;
743 
744   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL10_C, (uint8_t *)&ctrl10_c, 1);
745 
746   if (ret == 0)
747   {
748     ctrl10_c.timestamp_en = (uint8_t)val;
749     ret = iis3dwb_write_reg(ctx, IIS3DWB_CTRL10_C,
750                             (uint8_t *)&ctrl10_c, 1);
751   }
752 
753   return ret;
754 }
755 
756 /**
757   * @brief  Enables timestamp counter.[get]
758   *
759   * @param  ctx    Read / write interface definitions.(ptr)
760   * @param  val    Change the values of timestamp_en in reg CTRL10_C
761   * @retval        Interface status (MANDATORY: return 0 -> no Error).
762   *
763   */
iis3dwb_timestamp_get(const stmdev_ctx_t * ctx,uint8_t * val)764 int32_t iis3dwb_timestamp_get(const stmdev_ctx_t *ctx, uint8_t *val)
765 {
766   iis3dwb_ctrl10_c_t ctrl10_c;
767 
768   const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL10_C, (uint8_t *)&ctrl10_c, 1);
769   *val = ctrl10_c.timestamp_en;
770 
771   return ret;
772 }
773 
774 /**
775   * @brief  Timestamp first data output register (r).
776   *         The value is expressed as a 32-bit word and the bit resolution
777   *         is 25 μs.[get]
778   *
779   * @param  ctx    Read / write interface definitions.(ptr)
780   * @param  val    Buffer that stores data read
781   * @retval        Interface status (MANDATORY: return 0 -> no Error).
782   *
783   */
iis3dwb_timestamp_raw_get(const stmdev_ctx_t * ctx,uint32_t * val)784 int32_t iis3dwb_timestamp_raw_get(const stmdev_ctx_t *ctx, uint32_t *val)
785 {
786   uint8_t buff[4];
787 
788   const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_TIMESTAMP0, buff, 4);
789   *val = buff[3];
790   *val = (*val * 256U) +  buff[2];
791   *val = (*val * 256U) +  buff[1];
792   *val = (*val * 256U) +  buff[0];
793 
794   return ret;
795 }
796 
797 /**
798   * @}
799   *
800   */
801 
802 /**
803   * @defgroup   IIS3DWB_Data output
804   * @brief      This section groups all the data output functions.
805   * @{
806   *
807   */
808 
809 /**
810   * @brief  Circular burst-mode (rounding) read of the output registers.[set]
811   *
812   * @param  ctx    Read / write interface definitions.(ptr)
813   * @param  val    Change the values of rounding in reg CTRL5_C
814   * @retval        Interface status (MANDATORY: return 0 -> no Error).
815   *
816   */
iis3dwb_rounding_mode_set(const stmdev_ctx_t * ctx,iis3dwb_rounding_t val)817 int32_t iis3dwb_rounding_mode_set(const stmdev_ctx_t *ctx,
818                                   iis3dwb_rounding_t val)
819 {
820   iis3dwb_ctrl5_c_t ctrl5_c;
821 
822   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL5_C, (uint8_t *)&ctrl5_c, 1);
823 
824   if (ret == 0)
825   {
826     ctrl5_c.rounding = (uint8_t)val;
827     ret = iis3dwb_write_reg(ctx, IIS3DWB_CTRL5_C, (uint8_t *)&ctrl5_c, 1);
828   }
829 
830   return ret;
831 }
832 
833 /**
834   * @brief  Gyroscope UI chain full-scale selection.[get]
835   *
836   * @param  ctx    Read / write interface definitions.(ptr)
837   * @param  val    Get the values of rounding in reg CTRL5_C
838   * @retval        Interface status (MANDATORY: return 0 -> no Error).
839   *
840   */
iis3dwb_rounding_mode_get(const stmdev_ctx_t * ctx,iis3dwb_rounding_t * val)841 int32_t iis3dwb_rounding_mode_get(const stmdev_ctx_t *ctx,
842                                   iis3dwb_rounding_t *val)
843 {
844   iis3dwb_ctrl5_c_t ctrl5_c;
845 
846   const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL5_C, (uint8_t *)&ctrl5_c, 1);
847 
848   switch (ctrl5_c.rounding)
849   {
850     case IIS3DWB_NO_ROUND:
851       *val = IIS3DWB_NO_ROUND;
852       break;
853 
854     case IIS3DWB_ROUND:
855       *val = IIS3DWB_ROUND;
856       break;
857 
858     default:
859       *val = IIS3DWB_NO_ROUND;
860       break;
861   }
862 
863   return ret;
864 }
865 
866 /**
867   * @brief  Temperature data output register (r).
868   *         L and H registers together express a 16-bit word in two’s
869   *         complement.[get]
870   *
871   * @param  ctx    Read / write interface definitions.(ptr)
872   * @param  val    Buffer that stores data read
873   * @retval        Interface status (MANDATORY: return 0 -> no Error).
874   *
875   */
iis3dwb_temperature_raw_get(const stmdev_ctx_t * ctx,int16_t * val)876 int32_t iis3dwb_temperature_raw_get(const stmdev_ctx_t *ctx, int16_t *val)
877 {
878   uint8_t buff[2];
879 
880   const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_OUT_TEMP_L, buff, 2);
881   *val = (int16_t)buff[1];
882   *val = (*val * 256) + (int16_t)buff[0];
883 
884   return ret;
885 }
886 
887 /**
888   * @brief  Linear acceleration output register. The value is expressed as a
889   *         16-bit word in two’s complement.[get]
890   *
891   * @param  ctx    Read / write interface definitions.(ptr)
892   * @param  val    Buffer that stores data read
893   * @retval        Interface status (MANDATORY: return 0 -> no Error).
894   *
895   */
iis3dwb_acceleration_raw_get(const stmdev_ctx_t * ctx,int16_t * val)896 int32_t iis3dwb_acceleration_raw_get(const stmdev_ctx_t *ctx, int16_t *val)
897 {
898   uint8_t buff[6];
899 
900   const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_OUTX_L_A, buff, 6);
901   val[0] = (int16_t)buff[1];
902   val[0] = (val[0] * 256) + (int16_t)buff[0];
903   val[1] = (int16_t)buff[3];
904   val[1] = (val[1] * 256) + (int16_t)buff[2];
905   val[2] = (int16_t)buff[5];
906   val[2] = (val[2] * 256) + (int16_t)buff[4];
907 
908   return ret;
909 }
910 
911 /**
912   * @brief  FIFO data output.[get]
913   *
914   * @param  ctx    Read / write interface definitions.(ptr)
915   * @param  val    Buffer that stores data read
916   * @retval        Interface status (MANDATORY: return 0 -> no Error).
917   *
918   */
iis3dwb_fifo_out_raw_get(const stmdev_ctx_t * ctx,iis3dwb_fifo_out_raw_t * val)919 int32_t iis3dwb_fifo_out_raw_get(const stmdev_ctx_t *ctx, iis3dwb_fifo_out_raw_t *val)
920 {
921   const int32_t ret = iis3dwb_fifo_out_multi_raw_get(ctx, val, 1);
922 
923   return ret;
924 }
925 
926 /**
927   * @brief  FIFO data multi output.[get]
928   *
929   * @param  ctx    Read / write interface definitions.(ptr)
930   * @param  fdata  Buffer that stores data read
931   * @param  num    Number of FIFO entries to be read
932   * @retval        Interface status (MANDATORY: return 0 -> no Error).
933   *
934   */
iis3dwb_fifo_out_multi_raw_get(const stmdev_ctx_t * ctx,iis3dwb_fifo_out_raw_t * fdata,uint16_t num)935 int32_t iis3dwb_fifo_out_multi_raw_get(const stmdev_ctx_t *ctx,
936                                        iis3dwb_fifo_out_raw_t *fdata,
937                                        uint16_t num)
938 {
939   /* read out all FIFO entries in a single read */
940   const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_FIFO_DATA_OUT_TAG,
941                          (uint8_t *)fdata,
942                          sizeof(iis3dwb_fifo_out_raw_t) * num);
943 
944   return ret;
945 }
946 
947 /**
948   * @brief  Identifies the sensor in FIFO_DATA_OUT.[get]
949   *
950   * @param  ctx    Read / write interface definitions.(ptr)
951   * @param  val    Change the values of tag_sensor in reg FIFO_DATA_OUT_TAG
952   * @retval        Interface status (MANDATORY: return 0 -> no Error).
953   *
954   */
iis3dwb_fifo_sensor_tag_get(const stmdev_ctx_t * ctx,iis3dwb_fifo_tag_t * val)955 int32_t iis3dwb_fifo_sensor_tag_get(const stmdev_ctx_t *ctx,
956                                     iis3dwb_fifo_tag_t *val)
957 {
958   iis3dwb_fifo_data_out_tag_t fifo_data_out_tag;
959 
960   const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_FIFO_DATA_OUT_TAG,
961                          (uint8_t *)&fifo_data_out_tag, 1);
962 
963   switch (fifo_data_out_tag.tag_sensor)
964   {
965     case IIS3DWB_XL_TAG:
966       *val = IIS3DWB_XL_TAG;
967       break;
968 
969     case IIS3DWB_TEMPERATURE_TAG:
970       *val = IIS3DWB_TEMPERATURE_TAG;
971       break;
972 
973     case IIS3DWB_TIMESTAMP_TAG:
974       *val = IIS3DWB_TIMESTAMP_TAG;
975       break;
976 
977     default:
978       *val = IIS3DWB_XL_TAG;
979       break;
980   }
981 
982   return ret;
983 }
984 
985 /**
986   * @}
987   *
988   */
989 
990 /**
991   * @defgroup   IIS3DWB_common
992   * @brief      This section groups common useful functions.
993   * @{
994   *
995   */
996 
997 /**
998   * @brief  Difference in percentage of the effective ODR (and timestamp rate)
999   *         with respect to the typical.[set]
1000   *         Step:  0.15%. 8-bit format, 2's complement.
1001   *
1002   * @param  ctx    Read / write interface definitions.(ptr)
1003   * @param  val    Change the values of freq_fine in reg INTERNAL_FREQ_FINE
1004   * @retval        Interface status (MANDATORY: return 0 -> no Error).
1005   *
1006   */
iis3dwb_odr_cal_reg_set(const stmdev_ctx_t * ctx,uint8_t val)1007 int32_t iis3dwb_odr_cal_reg_set(const stmdev_ctx_t *ctx, uint8_t val)
1008 {
1009   iis3dwb_internal_freq_fine_t internal_freq_fine;
1010 
1011   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_INTERNAL_FREQ_FINE,
1012                          (uint8_t *)&internal_freq_fine, 1);
1013 
1014   if (ret == 0)
1015   {
1016     internal_freq_fine.freq_fine = (uint8_t)val;
1017     ret = iis3dwb_write_reg(ctx, IIS3DWB_INTERNAL_FREQ_FINE,
1018                             (uint8_t *)&internal_freq_fine, 1);
1019   }
1020 
1021   return ret;
1022 }
1023 
1024 /**
1025   * @brief  Difference in percentage of the effective ODR (and timestamp rate)
1026   *         with respect to the typical.[get]
1027   *         Step:  0.15%. 8-bit format, 2's complement.
1028   *
1029   * @param  ctx    Read / write interface definitions.(ptr)
1030   * @param  val    Change the values of freq_fine in reg INTERNAL_FREQ_FINE
1031   * @retval        Interface status (MANDATORY: return 0 -> no Error).
1032   *
1033   */
iis3dwb_odr_cal_reg_get(const stmdev_ctx_t * ctx,uint8_t * val)1034 int32_t iis3dwb_odr_cal_reg_get(const stmdev_ctx_t *ctx, uint8_t *val)
1035 {
1036   iis3dwb_internal_freq_fine_t internal_freq_fine;
1037 
1038   const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_INTERNAL_FREQ_FINE,
1039                          (uint8_t *)&internal_freq_fine, 1);
1040   *val = internal_freq_fine.freq_fine;
1041 
1042   return ret;
1043 }
1044 
1045 /**
1046   * @brief  Data-ready pulsed / letched mode.[set]
1047   *
1048   * @param  ctx    Read / write interface definitions.(ptr)
1049   * @param  val    Change the values of dataready_pulsed in
1050   *                reg COUNTER_BDR_REG1
1051   * @retval        Interface status (MANDATORY: return 0 -> no Error).
1052   *
1053   */
iis3dwb_data_ready_mode_set(const stmdev_ctx_t * ctx,iis3dwb_dataready_pulsed_t val)1054 int32_t iis3dwb_data_ready_mode_set(const stmdev_ctx_t *ctx,
1055                                     iis3dwb_dataready_pulsed_t val)
1056 {
1057   iis3dwb_counter_bdr_reg1_t counter_bdr_reg1;
1058 
1059   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_COUNTER_BDR_REG1,
1060                          (uint8_t *)&counter_bdr_reg1, 1);
1061 
1062   if (ret == 0)
1063   {
1064     counter_bdr_reg1.dataready_pulsed = (uint8_t)val;
1065     ret = iis3dwb_write_reg(ctx, IIS3DWB_COUNTER_BDR_REG1,
1066                             (uint8_t *)&counter_bdr_reg1, 1);
1067   }
1068 
1069   return ret;
1070 }
1071 
1072 /**
1073   * @brief  Data-ready pulsed / letched mode.[get]
1074   *
1075   * @param  ctx    Read / write interface definitions.(ptr)
1076   * @param  val    Get the values of dataready_pulsed in
1077   *                reg COUNTER_BDR_REG1
1078   * @retval        Interface status (MANDATORY: return 0 -> no Error).
1079   *
1080   */
iis3dwb_data_ready_mode_get(const stmdev_ctx_t * ctx,iis3dwb_dataready_pulsed_t * val)1081 int32_t iis3dwb_data_ready_mode_get(const stmdev_ctx_t *ctx,
1082                                     iis3dwb_dataready_pulsed_t *val)
1083 {
1084   iis3dwb_counter_bdr_reg1_t counter_bdr_reg1;
1085 
1086   const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_COUNTER_BDR_REG1,
1087                          (uint8_t *)&counter_bdr_reg1, 1);
1088 
1089   switch (counter_bdr_reg1.dataready_pulsed)
1090   {
1091     case IIS3DWB_DRDY_LATCHED:
1092       *val = IIS3DWB_DRDY_LATCHED;
1093       break;
1094 
1095     case IIS3DWB_DRDY_PULSED:
1096       *val = IIS3DWB_DRDY_PULSED;
1097       break;
1098 
1099     default:
1100       *val = IIS3DWB_DRDY_LATCHED;
1101       break;
1102   }
1103 
1104   return ret;
1105 }
1106 
1107 /**
1108   * @brief  Device Who am I.[get]
1109   *
1110   * @param  ctx    Read / write interface definitions.(ptr)
1111   * @param  buff   Buffer that stores data read
1112   * @retval        Interface status (MANDATORY: return 0 -> no Error).
1113   *
1114   */
iis3dwb_device_id_get(const stmdev_ctx_t * ctx,uint8_t * buff)1115 int32_t iis3dwb_device_id_get(const stmdev_ctx_t *ctx, uint8_t *buff)
1116 {
1117   const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_WHO_AM_I, buff, 1);
1118 
1119   return ret;
1120 }
1121 
1122 /**
1123   * @brief  Software reset.[set]
1124   *
1125   * @param  ctx    Read / write interface definitions.(ptr)
1126   * @param  val    Value of sw_reset in reg CTRL3_C
1127   * @retval        Interface status (MANDATORY: return 0 -> no Error).
1128   *
1129   */
iis3dwb_reset_set(const stmdev_ctx_t * ctx,uint8_t val)1130 int32_t iis3dwb_reset_set(const stmdev_ctx_t *ctx, uint8_t val)
1131 {
1132   iis3dwb_ctrl3_c_t ctrl3_c;
1133 
1134   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL3_C, (uint8_t *)&ctrl3_c, 1);
1135 
1136   if (ret == 0)
1137   {
1138     ctrl3_c.sw_reset = (uint8_t)val;
1139     ret = iis3dwb_write_reg(ctx, IIS3DWB_CTRL3_C, (uint8_t *)&ctrl3_c, 1);
1140   }
1141 
1142   return ret;
1143 }
1144 
1145 /**
1146   * @brief  Software reset.[get]
1147   *
1148   * @param  ctx    Read / write interface definitions.(ptr)
1149   * @param  val    Value of sw_reset in reg CTRL3_C
1150   * @retval        Interface status (MANDATORY: return 0 -> no Error).
1151   *
1152   */
iis3dwb_reset_get(const stmdev_ctx_t * ctx,uint8_t * val)1153 int32_t iis3dwb_reset_get(const stmdev_ctx_t *ctx, uint8_t *val)
1154 {
1155   iis3dwb_ctrl3_c_t ctrl3_c;
1156 
1157   const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL3_C, (uint8_t *)&ctrl3_c, 1);
1158   *val = ctrl3_c.sw_reset;
1159 
1160   return ret;
1161 }
1162 
1163 /**
1164   * @brief  Register address automatically incremented during a multiple byte
1165   *         access with a serial interface.[set]
1166   *
1167   * @param  ctx    Read / write interface definitions.(ptr)
1168   * @param  val    Change the values of if_inc in reg CTRL3_C
1169   * @retval        Interface status (MANDATORY: return 0 -> no Error).
1170   *
1171   */
iis3dwb_auto_increment_set(const stmdev_ctx_t * ctx,uint8_t val)1172 int32_t iis3dwb_auto_increment_set(const stmdev_ctx_t *ctx, uint8_t val)
1173 {
1174   iis3dwb_ctrl3_c_t ctrl3_c;
1175 
1176   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL3_C, (uint8_t *)&ctrl3_c, 1);
1177 
1178   if (ret == 0)
1179   {
1180     ctrl3_c.if_inc = (uint8_t)val;
1181     ret = iis3dwb_write_reg(ctx, IIS3DWB_CTRL3_C, (uint8_t *)&ctrl3_c, 1);
1182   }
1183 
1184   return ret;
1185 }
1186 
1187 /**
1188   * @brief  Register address automatically incremented during a multiple byte
1189   *         access with a serial interface.[get]
1190   *
1191   * @param  ctx    Read / write interface definitions.(ptr)
1192   * @param  val    Change the values of if_inc in reg CTRL3_C
1193   * @retval        Interface status (MANDATORY: return 0 -> no Error).
1194   *
1195   */
iis3dwb_auto_increment_get(const stmdev_ctx_t * ctx,uint8_t * val)1196 int32_t iis3dwb_auto_increment_get(const stmdev_ctx_t *ctx, uint8_t *val)
1197 {
1198   iis3dwb_ctrl3_c_t ctrl3_c;
1199 
1200   const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL3_C, (uint8_t *)&ctrl3_c, 1);
1201   *val = ctrl3_c.if_inc;
1202 
1203   return ret;
1204 }
1205 
1206 /**
1207   * @brief  Reboot memory content.[set]
1208   *
1209   * @param  ctx    Read / write interface definitions.(ptr)
1210   * @param  val    Value of boot in reg CTRL3_C
1211   * @retval        Interface status (MANDATORY: return 0 -> no Error).
1212   *
1213   */
iis3dwb_boot_set(const stmdev_ctx_t * ctx,uint8_t val)1214 int32_t iis3dwb_boot_set(const stmdev_ctx_t *ctx, uint8_t val)
1215 {
1216   iis3dwb_ctrl3_c_t ctrl3_c;
1217 
1218   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL3_C, (uint8_t *)&ctrl3_c, 1);
1219 
1220   if (ret == 0)
1221   {
1222     ctrl3_c.boot = (uint8_t)val;
1223     ret = iis3dwb_write_reg(ctx, IIS3DWB_CTRL3_C, (uint8_t *)&ctrl3_c, 1);
1224   }
1225 
1226   return ret;
1227 }
1228 
1229 /**
1230   * @brief  Reboot memory content.[get]
1231   *
1232   * @param  ctx    Read / write interface definitions.(ptr)
1233   * @param  val    Value of boot in reg CTRL3_C
1234   * @retval        Interface status (MANDATORY: return 0 -> no Error).
1235   *
1236   */
iis3dwb_boot_get(const stmdev_ctx_t * ctx,uint8_t * val)1237 int32_t iis3dwb_boot_get(const stmdev_ctx_t *ctx, uint8_t *val)
1238 {
1239   iis3dwb_ctrl3_c_t ctrl3_c;
1240 
1241   const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL3_C, (uint8_t *)&ctrl3_c, 1);
1242   *val = ctrl3_c.boot;
1243 
1244   return ret;
1245 }
1246 
1247 
1248 
1249 /**
1250   * @brief  Linear acceleration sensor self-test enable.[set]
1251   *
1252   * @param  ctx    Read / write interface definitions.(ptr)
1253   * @param  val    Change the values of st_xl in reg CTRL5_C
1254   * @retval        Interface status (MANDATORY: return 0 -> no Error).
1255   *
1256   */
iis3dwb_xl_self_test_set(const stmdev_ctx_t * ctx,iis3dwb_st_xl_t val)1257 int32_t iis3dwb_xl_self_test_set(const stmdev_ctx_t *ctx,
1258                                  iis3dwb_st_xl_t val)
1259 {
1260   iis3dwb_ctrl5_c_t ctrl5_c;
1261 
1262   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL5_C, (uint8_t *)&ctrl5_c, 1);
1263 
1264   if (ret == 0)
1265   {
1266     ctrl5_c.st_xl = (uint8_t)val;
1267     ret = iis3dwb_write_reg(ctx, IIS3DWB_CTRL5_C, (uint8_t *)&ctrl5_c, 1);
1268   }
1269 
1270   return ret;
1271 }
1272 
1273 /**
1274   * @brief  Linear acceleration sensor self-test enable.[get]
1275   *
1276   * @param  ctx    Read / write interface definitions.(ptr)
1277   * @param  val    Get the values of st_xl in reg CTRL5_C
1278   * @retval        Interface status (MANDATORY: return 0 -> no Error).
1279   *
1280   */
iis3dwb_xl_self_test_get(const stmdev_ctx_t * ctx,iis3dwb_st_xl_t * val)1281 int32_t iis3dwb_xl_self_test_get(const stmdev_ctx_t *ctx,
1282                                  iis3dwb_st_xl_t *val)
1283 {
1284   iis3dwb_ctrl5_c_t ctrl5_c;
1285 
1286   const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL5_C, (uint8_t *)&ctrl5_c, 1);
1287 
1288   switch (ctrl5_c.st_xl)
1289   {
1290     case IIS3DWB_XL_ST_DISABLE:
1291       *val = IIS3DWB_XL_ST_DISABLE;
1292       break;
1293 
1294     case IIS3DWB_XL_ST_POSITIVE:
1295       *val = IIS3DWB_XL_ST_POSITIVE;
1296       break;
1297 
1298     case IIS3DWB_XL_ST_NEGATIVE:
1299       *val = IIS3DWB_XL_ST_NEGATIVE;
1300       break;
1301 
1302     default:
1303       *val = IIS3DWB_XL_ST_DISABLE;
1304       break;
1305   }
1306 
1307   return ret;
1308 }
1309 
1310 
1311 
1312 /**
1313   * @}
1314   *
1315   */
1316 
1317 /**
1318   * @defgroup   IIS3DWB_filters
1319   * @brief      This section group all the functions concerning the
1320   *             filters configuration
1321   * @{
1322   *
1323   */
1324 
1325 /**
1326   * @brief  Mask DRDY on pin (both XL & Gyro) until filter settling ends
1327   *         (XL and Gyro independently masked).[set]
1328   *
1329   * @param  ctx    Read / write interface definitions.(ptr)
1330   * @param  val    Change the values of drdy_mask in reg CTRL4_C
1331   * @retval        Interface status (MANDATORY: return 0 -> no Error).
1332   *
1333   */
iis3dwb_filter_settling_mask_set(const stmdev_ctx_t * ctx,uint8_t val)1334 int32_t iis3dwb_filter_settling_mask_set(const stmdev_ctx_t *ctx,
1335                                          uint8_t val)
1336 {
1337   iis3dwb_ctrl4_c_t ctrl4_c;
1338 
1339   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL4_C, (uint8_t *)&ctrl4_c, 1);
1340 
1341   if (ret == 0)
1342   {
1343     ctrl4_c.drdy_mask = (uint8_t)val;
1344     ret = iis3dwb_write_reg(ctx, IIS3DWB_CTRL4_C, (uint8_t *)&ctrl4_c, 1);
1345   }
1346 
1347   return ret;
1348 }
1349 
1350 /**
1351   * @brief  Mask DRDY on pin (both XL & Gyro) until filter settling ends
1352   *         (XL and Gyro independently masked).[get]
1353   *
1354   * @param  ctx    Read / write interface definitions.(ptr)
1355   * @param  val    Change the values of drdy_mask in reg CTRL4_C
1356   * @retval        Interface status (MANDATORY: return 0 -> no Error).
1357   *
1358   */
iis3dwb_filter_settling_mask_get(const stmdev_ctx_t * ctx,uint8_t * val)1359 int32_t iis3dwb_filter_settling_mask_get(const stmdev_ctx_t *ctx,
1360                                          uint8_t *val)
1361 {
1362   iis3dwb_ctrl4_c_t ctrl4_c;
1363   const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL4_C, (uint8_t *)&ctrl4_c, 1);
1364   *val = ctrl4_c.drdy_mask;
1365 
1366   return ret;
1367 }
1368 
1369 /**
1370   * @brief  Accelerometer filter selection on output.[set]
1371   *
1372   * @param  ctx    Read / write interface definitions.(ptr)
1373   * @param  val    Change filter selection on output.
1374   * @retval        Interface status (MANDATORY: return 0 -> no Error).
1375   *
1376   */
iis3dwb_xl_filt_path_on_out_set(const stmdev_ctx_t * ctx,iis3dwb_filt_xl_en_t val)1377 int32_t iis3dwb_xl_filt_path_on_out_set(const stmdev_ctx_t *ctx,
1378                                         iis3dwb_filt_xl_en_t val)
1379 {
1380   iis3dwb_ctrl1_xl_t ctrl1_xl;
1381   iis3dwb_ctrl8_xl_t ctrl8_xl;
1382 
1383   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL1_XL, (uint8_t *)&ctrl1_xl, 1);
1384 
1385   if (ret == 0)
1386   {
1387     ctrl1_xl.lpf2_xl_en = ((uint8_t)val & 0x80U) >> 7;
1388     ret = iis3dwb_write_reg(ctx, IIS3DWB_CTRL1_XL, (uint8_t *)&ctrl1_xl, 1);
1389   }
1390 
1391   if (ret == 0)
1392   {
1393     ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL8_XL, (uint8_t *)&ctrl8_xl, 1);
1394   }
1395 
1396   if (ret == 0)
1397   {
1398     ctrl8_xl.fds = ((uint8_t)val & 0x10U) >> 4;
1399     ctrl8_xl.hp_ref_mode_xl = ((uint8_t)val & 0x20U) >> 5;
1400     ctrl8_xl.hpcf_xl = (uint8_t)val & 0x07U;
1401     ret = iis3dwb_write_reg(ctx, IIS3DWB_CTRL8_XL, (uint8_t *)&ctrl8_xl, 1);
1402   }
1403 
1404   return ret;
1405 }
1406 
1407 /**
1408   * @brief Accelerometer filter selection on output.[get]
1409   *
1410   * @param  ctx    Read / write interface definitions.(ptr)
1411   * @param  val    Get filter selection on output.
1412   * @retval        Interface status (MANDATORY: return 0 -> no Error).
1413   *
1414   */
iis3dwb_xl_filt_path_on_out_get(const stmdev_ctx_t * ctx,iis3dwb_filt_xl_en_t * val)1415 int32_t iis3dwb_xl_filt_path_on_out_get(const stmdev_ctx_t *ctx,
1416                                         iis3dwb_filt_xl_en_t *val)
1417 {
1418   iis3dwb_ctrl1_xl_t ctrl1_xl;
1419   iis3dwb_ctrl8_xl_t ctrl8_xl;
1420 
1421   *val = IIS3DWB_HP_REF_MODE;
1422 
1423   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL1_XL, (uint8_t *)&ctrl1_xl, 1);
1424   if (ret != 0) { return ret; }
1425 
1426   ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL8_XL, (uint8_t *)&ctrl8_xl, 1);
1427   if (ret != 0) { return ret; }
1428 
1429   switch ((ctrl1_xl.lpf2_xl_en << 7) + (ctrl8_xl.hp_ref_mode_xl << 5) +
1430           (ctrl8_xl.fds << 4) + ctrl8_xl.hpcf_xl)
1431   {
1432     case IIS3DWB_HP_REF_MODE:
1433       *val = IIS3DWB_HP_REF_MODE;
1434       break;
1435 
1436     case IIS3DWB_HP_ODR_DIV_10:
1437       *val = IIS3DWB_HP_ODR_DIV_10;
1438       break;
1439 
1440     case IIS3DWB_HP_ODR_DIV_20:
1441       *val = IIS3DWB_HP_ODR_DIV_20;
1442       break;
1443 
1444     case IIS3DWB_HP_ODR_DIV_45:
1445       *val = IIS3DWB_HP_ODR_DIV_45;
1446       break;
1447 
1448     case IIS3DWB_HP_ODR_DIV_100:
1449       *val = IIS3DWB_HP_ODR_DIV_100;
1450       break;
1451 
1452     case IIS3DWB_HP_ODR_DIV_200:
1453       *val = IIS3DWB_HP_ODR_DIV_200;
1454       break;
1455 
1456     case IIS3DWB_HP_ODR_DIV_400:
1457       *val = IIS3DWB_HP_ODR_DIV_400;
1458       break;
1459 
1460     case IIS3DWB_HP_ODR_DIV_800:
1461       *val = IIS3DWB_HP_ODR_DIV_800;
1462       break;
1463 
1464     case IIS3DWB_LP_ODR_DIV_4:
1465       *val = IIS3DWB_LP_ODR_DIV_4;
1466       break;
1467 
1468     case IIS3DWB_LP_6k3Hz:
1469       *val = IIS3DWB_LP_6k3Hz;
1470       break;
1471 
1472     case IIS3DWB_LP_ODR_DIV_10:
1473       *val = IIS3DWB_LP_ODR_DIV_10;
1474       break;
1475 
1476     case IIS3DWB_LP_ODR_DIV_20:
1477       *val = IIS3DWB_LP_ODR_DIV_20;
1478       break;
1479 
1480     case IIS3DWB_LP_ODR_DIV_45:
1481       *val = IIS3DWB_LP_ODR_DIV_45;
1482       break;
1483 
1484     case IIS3DWB_LP_ODR_DIV_100:
1485       *val = IIS3DWB_LP_ODR_DIV_100;
1486       break;
1487 
1488     case IIS3DWB_LP_ODR_DIV_200:
1489       *val = IIS3DWB_LP_ODR_DIV_200;
1490       break;
1491 
1492     case IIS3DWB_LP_ODR_DIV_400:
1493       *val = IIS3DWB_LP_ODR_DIV_400;
1494       break;
1495 
1496     case IIS3DWB_LP_ODR_DIV_800:
1497       *val = IIS3DWB_LP_ODR_DIV_800;
1498       break;
1499 
1500     default:
1501       *val = IIS3DWB_HP_REF_MODE;
1502       break;
1503   }
1504 
1505   return ret;
1506 }
1507 
1508 /**
1509   * @brief  Enables accelerometer LPF2 and HPF fast-settling mode.
1510   *         The filter sets the second samples after writing this bit.
1511   *         Active only during device exit from powerdown mode.[set]
1512   *
1513   * @param  ctx    Read / write interface definitions.(ptr)
1514   * @param  val    Change the values of fastsettl_mode_xl in reg CTRL8_XL
1515   * @retval        Interface status (MANDATORY: return 0 -> no Error).
1516   *
1517   */
iis3dwb_xl_fast_settling_set(const stmdev_ctx_t * ctx,uint8_t val)1518 int32_t iis3dwb_xl_fast_settling_set(const stmdev_ctx_t *ctx, uint8_t val)
1519 {
1520   iis3dwb_ctrl8_xl_t ctrl8_xl;
1521 
1522   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL8_XL, (uint8_t *)&ctrl8_xl, 1);
1523 
1524   if (ret == 0)
1525   {
1526     ctrl8_xl.fastsettl_mode_xl = (uint8_t)val;
1527     ret = iis3dwb_write_reg(ctx, IIS3DWB_CTRL8_XL,
1528                             (uint8_t *)&ctrl8_xl, 1);
1529   }
1530 
1531   return ret;
1532 }
1533 
1534 /**
1535   * @brief  Enables accelerometer LPF2 and HPF fast-settling mode.
1536   *         The filter sets the second samples after writing
1537   *         this bit. Active only during device exit from powerdown mode.[get]
1538   *
1539   * @param  ctx    Read / write interface definitions.(ptr)
1540   * @param  val    Change the values of fastsettl_mode_xl in reg CTRL8_XL
1541   * @retval        Interface status (MANDATORY: return 0 -> no Error).
1542   *
1543   */
iis3dwb_xl_fast_settling_get(const stmdev_ctx_t * ctx,uint8_t * val)1544 int32_t iis3dwb_xl_fast_settling_get(const stmdev_ctx_t *ctx, uint8_t *val)
1545 {
1546   iis3dwb_ctrl8_xl_t ctrl8_xl;
1547 
1548   const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL8_XL, (uint8_t *)&ctrl8_xl, 1);
1549   *val = ctrl8_xl.fastsettl_mode_xl;
1550 
1551   return ret;
1552 }
1553 
1554 /**
1555   * @brief  HPF or SLOPE filter selection on wake-up and Activity/Inactivity
1556   *         functions.[set]
1557   *
1558   * @param  ctx    Read / write interface definitions.(ptr)
1559   * @param  val    Change the values of slope_fds in reg TAP_CFG0
1560   * @retval        Interface status (MANDATORY: return 0 -> no Error).
1561   *
1562   */
iis3dwb_xl_hp_path_internal_set(const stmdev_ctx_t * ctx,iis3dwb_slope_fds_t val)1563 int32_t iis3dwb_xl_hp_path_internal_set(const stmdev_ctx_t *ctx,
1564                                         iis3dwb_slope_fds_t val)
1565 {
1566   iis3dwb_slope_en_t int_cfg0;
1567 
1568   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_SLOPE_EN, (uint8_t *)&int_cfg0, 1);
1569 
1570   if (ret == 0)
1571   {
1572     int_cfg0.slope_fds = (uint8_t)val;
1573     ret = iis3dwb_write_reg(ctx, IIS3DWB_SLOPE_EN,
1574                             (uint8_t *)&int_cfg0, 1);
1575   }
1576 
1577   return ret;
1578 }
1579 
1580 /**
1581   * @brief  HPF or SLOPE filter selection on wake-up and Activity/Inactivity
1582   *         functions.[get]
1583   *
1584   * @param  ctx    Read / write interface definitions.(ptr)
1585   * @param  val    Get the values of slope_fds in reg TAP_CFG0
1586   * @retval        Interface status (MANDATORY: return 0 -> no Error).
1587   *
1588   */
iis3dwb_xl_hp_path_internal_get(const stmdev_ctx_t * ctx,iis3dwb_slope_fds_t * val)1589 int32_t iis3dwb_xl_hp_path_internal_get(const stmdev_ctx_t *ctx,
1590                                         iis3dwb_slope_fds_t *val)
1591 {
1592   iis3dwb_slope_en_t int_cfg0;
1593 
1594   const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_SLOPE_EN, (uint8_t *)&int_cfg0, 1);
1595 
1596   switch (int_cfg0.slope_fds)
1597   {
1598     case IIS3DWB_USE_SLOPE:
1599       *val = IIS3DWB_USE_SLOPE;
1600       break;
1601 
1602     case IIS3DWB_USE_HPF:
1603       *val = IIS3DWB_USE_HPF;
1604       break;
1605 
1606     default:
1607       *val = IIS3DWB_USE_SLOPE;
1608       break;
1609   }
1610 
1611   return ret;
1612 }
1613 
1614 /**
1615   * @}
1616   *
1617   */
1618 
1619 /**
1620   * @defgroup   IIS3DWB_ main_serial_interface
1621   * @brief      This section groups all the functions concerning main
1622   *             serial interface management (not auxiliary)
1623   * @{
1624   *
1625   */
1626 
1627 /**
1628   * @brief  Connect/Disconnect SDO/SA0 internal pull-up.[set]
1629   *
1630   * @param  ctx    Read / write interface definitions.(ptr)
1631   * @param  val    Change the values of sdo_pu_en in reg PIN_CTRL
1632   * @retval        Interface status (MANDATORY: return 0 -> no Error).
1633   *
1634   */
iis3dwb_sdo_sa0_mode_set(const stmdev_ctx_t * ctx,iis3dwb_sdo_pu_en_t val)1635 int32_t iis3dwb_sdo_sa0_mode_set(const stmdev_ctx_t *ctx,
1636                                  iis3dwb_sdo_pu_en_t val)
1637 {
1638   iis3dwb_pin_ctrl_t pin_ctrl;
1639 
1640   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_PIN_CTRL, (uint8_t *)&pin_ctrl, 1);
1641 
1642   if (ret == 0)
1643   {
1644     pin_ctrl.sdo_pu_en = (uint8_t)val;
1645     ret = iis3dwb_write_reg(ctx, IIS3DWB_PIN_CTRL, (uint8_t *)&pin_ctrl, 1);
1646   }
1647 
1648   return ret;
1649 }
1650 
1651 /**
1652   * @brief  Connect/Disconnect SDO/SA0 internal pull-up.[get]
1653   *
1654   * @param  ctx    Read / write interface definitions.(ptr)
1655   * @param  val    Get the values of sdo_pu_en in reg PIN_CTRL
1656   * @retval        Interface status (MANDATORY: return 0 -> no Error).
1657   *
1658   */
iis3dwb_sdo_sa0_mode_get(const stmdev_ctx_t * ctx,iis3dwb_sdo_pu_en_t * val)1659 int32_t iis3dwb_sdo_sa0_mode_get(const stmdev_ctx_t *ctx,
1660                                  iis3dwb_sdo_pu_en_t *val)
1661 {
1662   iis3dwb_pin_ctrl_t pin_ctrl;
1663 
1664   const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_PIN_CTRL, (uint8_t *)&pin_ctrl, 1);
1665 
1666   switch (pin_ctrl.sdo_pu_en)
1667   {
1668     case IIS3DWB_PULL_UP_DISC:
1669       *val = IIS3DWB_PULL_UP_DISC;
1670       break;
1671 
1672     case IIS3DWB_PULL_UP_CONNECT:
1673       *val = IIS3DWB_PULL_UP_CONNECT;
1674       break;
1675 
1676     default:
1677       *val = IIS3DWB_PULL_UP_DISC;
1678       break;
1679   }
1680 
1681   return ret;
1682 }
1683 
1684 /**
1685   * @brief  SPI Serial Interface Mode selection.[set]
1686   *
1687   * @param  ctx    Read / write interface definitions.(ptr)
1688   * @param  val    Change the values of sim in reg CTRL3_C
1689   * @retval        Interface status (MANDATORY: return 0 -> no Error).
1690   *
1691   */
iis3dwb_spi_mode_set(const stmdev_ctx_t * ctx,iis3dwb_sim_t val)1692 int32_t iis3dwb_spi_mode_set(const stmdev_ctx_t *ctx, iis3dwb_sim_t val)
1693 {
1694   iis3dwb_ctrl3_c_t ctrl3_c;
1695 
1696   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL3_C, (uint8_t *)&ctrl3_c, 1);
1697 
1698   if (ret == 0)
1699   {
1700     ctrl3_c.sim = (uint8_t)val;
1701     ret = iis3dwb_write_reg(ctx, IIS3DWB_CTRL3_C, (uint8_t *)&ctrl3_c, 1);
1702   }
1703 
1704   return ret;
1705 }
1706 
1707 /**
1708   * @brief  SPI Serial Interface Mode selection.[get]
1709   *
1710   * @param  ctx    Read / write interface definitions.(ptr)
1711   * @param  val    Get the values of sim in reg CTRL3_C
1712   * @retval        Interface status (MANDATORY: return 0 -> no Error).
1713   *
1714   */
iis3dwb_spi_mode_get(const stmdev_ctx_t * ctx,iis3dwb_sim_t * val)1715 int32_t iis3dwb_spi_mode_get(const stmdev_ctx_t *ctx, iis3dwb_sim_t *val)
1716 {
1717   iis3dwb_ctrl3_c_t ctrl3_c;
1718 
1719   const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL3_C, (uint8_t *)&ctrl3_c, 1);
1720 
1721   switch (ctrl3_c.sim)
1722   {
1723     case IIS3DWB_SPI_4_WIRE:
1724       *val = IIS3DWB_SPI_4_WIRE;
1725       break;
1726 
1727     case IIS3DWB_SPI_3_WIRE:
1728       *val = IIS3DWB_SPI_3_WIRE;
1729       break;
1730 
1731     default:
1732       *val = IIS3DWB_SPI_4_WIRE;
1733       break;
1734   }
1735 
1736   return ret;
1737 }
1738 
1739 /**
1740   * @brief  Disable / Enable I2C interface.[set]
1741   *
1742   * @param  ctx    Read / write interface definitions.(ptr)
1743   * @param  val    Change the values of i2c_disable in reg CTRL4_C
1744   * @retval        Interface status (MANDATORY: return 0 -> no Error).
1745   *
1746   */
iis3dwb_i2c_interface_set(const stmdev_ctx_t * ctx,iis3dwb_i2c_disable_t val)1747 int32_t iis3dwb_i2c_interface_set(const stmdev_ctx_t *ctx,
1748                                   iis3dwb_i2c_disable_t val)
1749 {
1750   iis3dwb_ctrl4_c_t ctrl4_c;
1751 
1752   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL4_C, (uint8_t *)&ctrl4_c, 1);
1753 
1754   if (ret == 0)
1755   {
1756     ctrl4_c.i2c_disable = (uint8_t)val;
1757     ret = iis3dwb_write_reg(ctx, IIS3DWB_CTRL4_C, (uint8_t *)&ctrl4_c, 1);
1758   }
1759 
1760   return ret;
1761 }
1762 
1763 /**
1764   * @brief  Disable / Enable I2C interface.[get]
1765   *
1766   * @param  ctx    Read / write interface definitions.(ptr)
1767   * @param  val    Get the values of i2c reg CTRL4_C
1768   * @retval        Interface status (MANDATORY: return 0 -> no Error).
1769   *
1770   */
iis3dwb_i2c_interface_get(const stmdev_ctx_t * ctx,iis3dwb_i2c_disable_t * val)1771 int32_t iis3dwb_i2c_interface_get(const stmdev_ctx_t *ctx,
1772                                   iis3dwb_i2c_disable_t *val)
1773 {
1774   iis3dwb_ctrl4_c_t ctrl4_c;
1775 
1776   const int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL4_C, (uint8_t *)&ctrl4_c, 1);
1777 
1778   switch (ctrl4_c.i2c_disable)
1779   {
1780     case IIS3DWB_I2C_ENABLE:
1781       *val = IIS3DWB_I2C_ENABLE;
1782       break;
1783 
1784     case IIS3DWB_I2C_DISABLE:
1785       *val = IIS3DWB_I2C_DISABLE;
1786       break;
1787 
1788     default:
1789       *val = IIS3DWB_I2C_ENABLE;
1790       break;
1791   }
1792 
1793   return ret;
1794 }
1795 
1796 /**
1797   * @}
1798   *
1799   */
1800 
1801 /**
1802   * @defgroup   IIS3DWB_interrupt_pins
1803   * @brief      This section groups all the functions that manage
1804   *             interrupt pins
1805   * @{
1806   *
1807   */
1808 
1809 /**
1810   * @brief   Select the signal that need to route on int1 pad[set]
1811   *
1812   * @param  ctx    Read / write interface definitions.(ptr)
1813   * @param  val    the signals to route on int1 pin.
1814   * @retval        Interface status (MANDATORY: return 0 -> no Error).
1815   *
1816   */
iis3dwb_pin_int1_route_set(const stmdev_ctx_t * ctx,iis3dwb_pin_int1_route_t * val)1817 int32_t iis3dwb_pin_int1_route_set(const stmdev_ctx_t *ctx,
1818                                    iis3dwb_pin_int1_route_t *val)
1819 {
1820   iis3dwb_int1_ctrl_t          int1_ctrl;
1821   iis3dwb_slope_en_t           slope_en;
1822   iis3dwb_md1_cfg_t            md1_cfg;
1823 
1824   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_INT1_CTRL, (uint8_t *)&int1_ctrl, 1);
1825   if (ret != 0) { return ret; }
1826 
1827   ret = iis3dwb_read_reg(ctx, IIS3DWB_MD1_CFG, (uint8_t *)&md1_cfg, 1);
1828   if (ret != 0) { return ret; }
1829 
1830   ret = iis3dwb_read_reg(ctx, IIS3DWB_SLOPE_EN, (uint8_t *)&slope_en, 1);
1831   if (ret != 0) { return ret; }
1832 
1833   int1_ctrl.int1_drdy_xl       = val->drdy_xl;
1834   int1_ctrl.int1_boot          = val->boot;
1835   int1_ctrl.int1_fifo_th       = val->fifo_th;
1836   int1_ctrl.int1_fifo_ovr      = val->fifo_ovr;
1837   int1_ctrl.int1_fifo_full     = val->fifo_full;
1838   int1_ctrl.int1_cnt_bdr       = val->fifo_bdr;
1839   md1_cfg.int1_wu              = val->wake_up;
1840   md1_cfg.int1_sleep_change    = val->sleep_change | val->sleep_status;
1841   slope_en.sleep_status_on_int = val->sleep_status;
1842 
1843   ret = iis3dwb_write_reg(ctx, IIS3DWB_INT1_CTRL, (uint8_t *)&int1_ctrl, 1);
1844   if (ret != 0) { return ret; }
1845 
1846   ret = iis3dwb_write_reg(ctx, IIS3DWB_MD1_CFG, (uint8_t *)&md1_cfg, 1);
1847   if (ret != 0) { return ret; }
1848 
1849   ret = iis3dwb_write_reg(ctx, IIS3DWB_SLOPE_EN, (uint8_t *)&slope_en, 1);
1850 
1851   return ret;
1852 }
1853 
1854 /**
1855   * @brief  Select the signal that need to route on int1 pad.[get]
1856   *
1857   * @param  ctx    Read / write interface definitions.(ptr)
1858   * @param  val    the signals that are routed on int1 pin.(ptr)
1859   * @retval        Interface status (MANDATORY: return 0 -> no Error).
1860   *
1861   */
iis3dwb_pin_int1_route_get(const stmdev_ctx_t * ctx,iis3dwb_pin_int1_route_t * val)1862 int32_t iis3dwb_pin_int1_route_get(const stmdev_ctx_t *ctx,
1863                                    iis3dwb_pin_int1_route_t *val)
1864 {
1865   iis3dwb_int1_ctrl_t          int1_ctrl;
1866   iis3dwb_slope_en_t           slope_en;
1867   iis3dwb_md1_cfg_t            md1_cfg;
1868 
1869   memset(val, 0, sizeof(iis3dwb_pin_int1_route_t));
1870 
1871   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_INT1_CTRL, (uint8_t *)&int1_ctrl, 1);
1872   if (ret != 0) { return ret; }
1873 
1874   ret = iis3dwb_read_reg(ctx, IIS3DWB_MD1_CFG, (uint8_t *)&md1_cfg, 1);
1875   if (ret != 0) { return ret; }
1876 
1877   ret = iis3dwb_read_reg(ctx, IIS3DWB_SLOPE_EN, (uint8_t *)&slope_en, 1);
1878   if (ret != 0) { return ret; }
1879 
1880   val->drdy_xl   = int1_ctrl.int1_drdy_xl;
1881   val->boot      = int1_ctrl.int1_boot;
1882   val->fifo_th   = int1_ctrl.int1_fifo_th;
1883   val->fifo_ovr  = int1_ctrl.int1_fifo_ovr;
1884   val->fifo_full = int1_ctrl.int1_fifo_full;
1885   val->fifo_bdr  = int1_ctrl.int1_cnt_bdr;
1886   val->wake_up      = md1_cfg.int1_wu;
1887 
1888   if (slope_en.sleep_status_on_int == PROPERTY_ENABLE)
1889   {
1890     val->sleep_status = PROPERTY_ENABLE;
1891     val->sleep_change = PROPERTY_DISABLE;
1892   }
1893 
1894   else
1895   {
1896     val->sleep_change = md1_cfg.int1_sleep_change;
1897   }
1898 
1899   return ret;
1900 }
1901 
1902 /**
1903   * @brief  Select the signal that need to route on int2 pad[set]
1904   *
1905   * @param  ctx    Read / write interface definitions.(ptr)
1906   * @param  val    the signals to route on int2 pin.
1907   * @retval        Interface status (MANDATORY: return 0 -> no Error).
1908   *
1909   */
iis3dwb_pin_int2_route_set(const stmdev_ctx_t * ctx,iis3dwb_pin_int2_route_t * val)1910 int32_t iis3dwb_pin_int2_route_set(const stmdev_ctx_t *ctx,
1911                                    iis3dwb_pin_int2_route_t *val)
1912 {
1913   iis3dwb_int2_ctrl_t          int2_ctrl;
1914   iis3dwb_slope_en_t           slope_en;
1915   iis3dwb_md2_cfg_t            md2_cfg;
1916 
1917   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_INT2_CTRL, (uint8_t *)&int2_ctrl, 1);
1918   if (ret != 0) { return ret; }
1919 
1920   ret = iis3dwb_read_reg(ctx, IIS3DWB_MD2_CFG, (uint8_t *)&md2_cfg, 1);
1921   if (ret != 0) { return ret; }
1922 
1923   ret = iis3dwb_read_reg(ctx, IIS3DWB_SLOPE_EN, (uint8_t *)&slope_en, 1);
1924   if (ret != 0) { return ret; }
1925 
1926   int2_ctrl.int2_drdy_xl       = val->drdy_xl;
1927   int2_ctrl.int2_drdy_temp     = val->drdy_temp;
1928   int2_ctrl.int2_fifo_th       = val->fifo_th;
1929   int2_ctrl.int2_fifo_ovr      = val->fifo_ovr;
1930   int2_ctrl.int2_fifo_full     = val->fifo_full;
1931   int2_ctrl.int2_cnt_bdr       = val->fifo_bdr;
1932   md2_cfg.int2_timestamp       = val->timestamp;
1933   md2_cfg.int2_wu              = val->wake_up;
1934   md2_cfg.int2_sleep_change    = val->sleep_change | val->sleep_status;
1935   slope_en.sleep_status_on_int = val->sleep_status;
1936 
1937   ret = iis3dwb_write_reg(ctx, IIS3DWB_INT2_CTRL, (uint8_t *)&int2_ctrl, 1);
1938   if (ret != 0) { return ret; }
1939 
1940   ret = iis3dwb_write_reg(ctx, IIS3DWB_MD2_CFG, (uint8_t *)&md2_cfg, 1);
1941   if (ret != 0) { return ret; }
1942 
1943   ret = iis3dwb_write_reg(ctx, IIS3DWB_SLOPE_EN, (uint8_t *)&slope_en, 1);
1944 
1945   return ret;
1946 }
1947 
1948 /**
1949   * @brief  Select the signal that need to route on int2 pad.[get]
1950   *
1951   * @param  ctx    Read / write interface definitions.(ptr)
1952   * @param  val    the signals that are routed on int2 pin.(ptr)
1953   * @retval        Interface status (MANDATORY: return 0 -> no Error).
1954   *
1955   */
iis3dwb_pin_int2_route_get(const stmdev_ctx_t * ctx,iis3dwb_pin_int2_route_t * val)1956 int32_t iis3dwb_pin_int2_route_get(const stmdev_ctx_t *ctx,
1957                                    iis3dwb_pin_int2_route_t *val)
1958 {
1959   iis3dwb_int2_ctrl_t          int2_ctrl;
1960   iis3dwb_slope_en_t           slope_en;
1961   iis3dwb_md2_cfg_t            md2_cfg;
1962 
1963   memset(val, 0, sizeof(iis3dwb_pin_int2_route_t));
1964 
1965   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_INT2_CTRL, (uint8_t *)&int2_ctrl, 1);
1966   if (ret != 0) { return ret; }
1967 
1968   ret = iis3dwb_read_reg(ctx, IIS3DWB_MD2_CFG, (uint8_t *)&md2_cfg, 1);
1969   if (ret != 0) { return ret; }
1970 
1971   ret = iis3dwb_read_reg(ctx, IIS3DWB_SLOPE_EN, (uint8_t *)&slope_en, 1);
1972   if (ret != 0) { return ret; }
1973 
1974   val->drdy_xl   = int2_ctrl.int2_drdy_xl;
1975   val->drdy_temp = int2_ctrl.int2_drdy_temp;
1976   val->fifo_th   = int2_ctrl.int2_fifo_th;
1977   val->fifo_ovr  = int2_ctrl.int2_fifo_ovr;
1978   val->fifo_full = int2_ctrl.int2_fifo_full;
1979   val->fifo_bdr  = int2_ctrl.int2_cnt_bdr;
1980   val->timestamp  = md2_cfg.int2_timestamp;
1981   val->wake_up   = md2_cfg.int2_wu;
1982 
1983   if (slope_en.sleep_status_on_int == PROPERTY_ENABLE)
1984   {
1985     val->sleep_status = PROPERTY_ENABLE;
1986     val->sleep_change = PROPERTY_DISABLE;
1987   }
1988 
1989   else
1990   {
1991     val->sleep_change = md2_cfg.int2_sleep_change;
1992   }
1993 
1994   return ret;
1995 }
1996 
1997 /**
1998   * @brief  Push-pull/open drain selection on interrupt pads.[set]
1999   *
2000   * @param  ctx    Read / write interface definitions.(ptr)
2001   * @param  val    Change the values of pp_od in reg CTRL3_C
2002   * @retval        Interface status (MANDATORY: return 0 -> no Error).
2003   *
2004   */
iis3dwb_pin_mode_set(const stmdev_ctx_t * ctx,iis3dwb_pp_od_t val)2005 int32_t iis3dwb_pin_mode_set(const stmdev_ctx_t *ctx, iis3dwb_pp_od_t val)
2006 {
2007   iis3dwb_ctrl3_c_t ctrl3_c;
2008 
2009   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL3_C, (uint8_t *)&ctrl3_c, 1);
2010 
2011   if (ret == 0)
2012   {
2013     ctrl3_c.pp_od = (uint8_t)val;
2014     ret = iis3dwb_write_reg(ctx, IIS3DWB_CTRL3_C, (uint8_t *)&ctrl3_c, 1);
2015   }
2016 
2017   return ret;
2018 }
2019 
2020 /**
2021   * @brief  Push-pull/open drain selection on interrupt pads.[get]
2022   *
2023   * @param  ctx    Read / write interface definitions.(ptr)
2024   * @param  val    Get the values of pp_od in reg CTRL3_C
2025   * @retval        Interface status (MANDATORY: return 0 -> no Error).
2026   *
2027   */
iis3dwb_pin_mode_get(const stmdev_ctx_t * ctx,iis3dwb_pp_od_t * val)2028 int32_t iis3dwb_pin_mode_get(const stmdev_ctx_t *ctx, iis3dwb_pp_od_t *val)
2029 {
2030   iis3dwb_ctrl3_c_t ctrl3_c;
2031 
2032   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL3_C, (uint8_t *)&ctrl3_c, 1);
2033 
2034   switch (ctrl3_c.pp_od)
2035   {
2036     case IIS3DWB_PUSH_PULL:
2037       *val = IIS3DWB_PUSH_PULL;
2038       break;
2039 
2040     case IIS3DWB_OPEN_DRAIN:
2041       *val = IIS3DWB_OPEN_DRAIN;
2042       break;
2043 
2044     default:
2045       *val = IIS3DWB_PUSH_PULL;
2046       break;
2047   }
2048 
2049   return ret;
2050 }
2051 
2052 /**
2053   * @brief  Interrupt active-high/low.[set]
2054   *
2055   * @param  ctx    Read / write interface definitions.(ptr)
2056   * @param  val    Change the values of h_lactive in reg CTRL3_C
2057   * @retval        Interface status (MANDATORY: return 0 -> no Error).
2058   *
2059   */
iis3dwb_pin_polarity_set(const stmdev_ctx_t * ctx,iis3dwb_h_lactive_t val)2060 int32_t iis3dwb_pin_polarity_set(const stmdev_ctx_t *ctx,
2061                                  iis3dwb_h_lactive_t val)
2062 {
2063   iis3dwb_ctrl3_c_t ctrl3_c;
2064 
2065   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL3_C, (uint8_t *)&ctrl3_c, 1);
2066 
2067   if (ret == 0)
2068   {
2069     ctrl3_c.h_lactive = (uint8_t)val;
2070     ret = iis3dwb_write_reg(ctx, IIS3DWB_CTRL3_C, (uint8_t *)&ctrl3_c, 1);
2071   }
2072 
2073   return ret;
2074 }
2075 
2076 /**
2077   * @brief  Interrupt active-high/low.[get]
2078   *
2079   * @param  ctx    Read / write interface definitions.(ptr)
2080   * @param  val    Get the values of h_lactive in reg CTRL3_C
2081   * @retval        Interface status (MANDATORY: return 0 -> no Error).
2082   *
2083   */
iis3dwb_pin_polarity_get(const stmdev_ctx_t * ctx,iis3dwb_h_lactive_t * val)2084 int32_t iis3dwb_pin_polarity_get(const stmdev_ctx_t *ctx,
2085                                  iis3dwb_h_lactive_t *val)
2086 {
2087   iis3dwb_ctrl3_c_t ctrl3_c;
2088 
2089   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL3_C, (uint8_t *)&ctrl3_c, 1);
2090 
2091   switch (ctrl3_c.h_lactive)
2092   {
2093     case IIS3DWB_ACTIVE_HIGH:
2094       *val = IIS3DWB_ACTIVE_HIGH;
2095       break;
2096 
2097     case IIS3DWB_ACTIVE_LOW:
2098       *val = IIS3DWB_ACTIVE_LOW;
2099       break;
2100 
2101     default:
2102       *val = IIS3DWB_ACTIVE_HIGH;
2103       break;
2104   }
2105 
2106   return ret;
2107 }
2108 
2109 /**
2110   * @brief  All interrupt signals become available on INT1 pin.[set]
2111   *
2112   * @param  ctx    Read / write interface definitions.(ptr)
2113   * @param  val    Change the values of int2_on_int1 in reg CTRL4_C
2114   * @retval        Interface status (MANDATORY: return 0 -> no Error).
2115   *
2116   */
iis3dwb_all_on_int1_set(const stmdev_ctx_t * ctx,uint8_t val)2117 int32_t iis3dwb_all_on_int1_set(const stmdev_ctx_t *ctx, uint8_t val)
2118 {
2119   iis3dwb_ctrl4_c_t ctrl4_c;
2120 
2121   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL4_C, (uint8_t *)&ctrl4_c, 1);
2122 
2123   if (ret == 0)
2124   {
2125     ctrl4_c.int2_on_int1 = (uint8_t)val;
2126     ret = iis3dwb_write_reg(ctx, IIS3DWB_CTRL4_C, (uint8_t *)&ctrl4_c, 1);
2127   }
2128 
2129   return ret;
2130 }
2131 
2132 /**
2133   * @brief  All interrupt signals become available on INT1 pin.[get]
2134   *
2135   * @param  ctx    Read / write interface definitions.(ptr)
2136   * @param  val    Change the values of int2_on_int1 in reg CTRL4_C
2137   * @retval        Interface status (MANDATORY: return 0 -> no Error).
2138   *
2139   */
iis3dwb_all_on_int1_get(const stmdev_ctx_t * ctx,uint8_t * val)2140 int32_t iis3dwb_all_on_int1_get(const stmdev_ctx_t *ctx, uint8_t *val)
2141 {
2142   iis3dwb_ctrl4_c_t ctrl4_c;
2143 
2144   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_CTRL4_C, (uint8_t *)&ctrl4_c, 1);
2145   *val = ctrl4_c.int2_on_int1;
2146 
2147   return ret;
2148 }
2149 
2150 /**
2151   * @brief  All interrupt signals notification mode.[set]
2152   *
2153   * @param  ctx    Read / write interface definitions.(ptr)
2154   * @param  val    Change the values of lir in reg SLOPE_EN
2155   * @retval        Interface status (MANDATORY: return 0 -> no Error).
2156   *
2157   */
iis3dwb_int_notification_set(const stmdev_ctx_t * ctx,iis3dwb_lir_t val)2158 int32_t iis3dwb_int_notification_set(const stmdev_ctx_t *ctx,
2159                                      iis3dwb_lir_t val)
2160 {
2161   iis3dwb_slope_en_t slope_en;
2162 
2163   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_SLOPE_EN, (uint8_t *)&slope_en, 1);
2164 
2165   if (ret == 0)
2166   {
2167     slope_en.lir = (uint8_t)val;
2168     ret = iis3dwb_write_reg(ctx, IIS3DWB_SLOPE_EN, (uint8_t *)&slope_en, 1);
2169   }
2170 
2171   return ret;
2172 }
2173 
2174 /**
2175   * @brief  All interrupt signals notification mode.[get]
2176   *
2177   * @param  ctx    Read / write interface definitions.(ptr)
2178   * @param  val    Get the values of lir in reg SLOPE_EN
2179   * @retval        Interface status (MANDATORY: return 0 -> no Error).
2180   *
2181   */
iis3dwb_int_notification_get(const stmdev_ctx_t * ctx,iis3dwb_lir_t * val)2182 int32_t iis3dwb_int_notification_get(const stmdev_ctx_t *ctx,
2183                                      iis3dwb_lir_t *val)
2184 {
2185   iis3dwb_slope_en_t slope_en;
2186 
2187   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_SLOPE_EN, (uint8_t *)&slope_en, 1);
2188 
2189   switch (slope_en.lir)
2190   {
2191     case IIS3DWB_INT_PULSED:
2192       *val = IIS3DWB_INT_PULSED;
2193       break;
2194 
2195     case IIS3DWB_INT_LATCHED:
2196       *val = IIS3DWB_INT_LATCHED;
2197       break;
2198 
2199     default:
2200       *val = IIS3DWB_INT_PULSED;
2201       break;
2202   }
2203 
2204   return ret;
2205 }
2206 
2207 /**
2208   * @}
2209   *
2210   */
2211 
2212 /**
2213   * @defgroup   IIS3DWB_Wake_Up_event
2214   * @brief      This section groups all the functions that manage the
2215   *             Wake Up event generation.
2216   * @{
2217   *
2218   */
2219 
2220 /**
2221   * @brief  Weight of 1 LSB of wakeup threshold.[set]
2222   *         0: 1 LSB =FS_XL  /  64
2223   *         1: 1 LSB = FS_XL / 256
2224   *
2225   * @param  ctx    Read / write interface definitions.(ptr)
2226   * @param  val    Change the values of wake_ths_w in reg WAKE_UP_DUR
2227   * @retval        Interface status (MANDATORY: return 0 -> no Error).
2228   *
2229   */
iis3dwb_wkup_ths_weight_set(const stmdev_ctx_t * ctx,iis3dwb_wake_ths_w_t val)2230 int32_t iis3dwb_wkup_ths_weight_set(const stmdev_ctx_t *ctx,
2231                                     iis3dwb_wake_ths_w_t val)
2232 {
2233   iis3dwb_wake_up_dur_t wake_up_dur;
2234 
2235   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_WAKE_UP_DUR,
2236                          (uint8_t *)&wake_up_dur, 1);
2237 
2238   if (ret == 0)
2239   {
2240     wake_up_dur.wake_ths_w = (uint8_t)val;
2241     ret = iis3dwb_write_reg(ctx, IIS3DWB_WAKE_UP_DUR,
2242                             (uint8_t *)&wake_up_dur, 1);
2243   }
2244 
2245   return ret;
2246 }
2247 
2248 /**
2249   * @brief  Weight of 1 LSB of wakeup threshold.[get]
2250   *         0: 1 LSB =FS_XL  /  64
2251   *         1: 1 LSB = FS_XL / 256
2252   *
2253   * @param  ctx    Read / write interface definitions.(ptr)
2254   * @param  val    Get the values of wake_ths_w in reg WAKE_UP_DUR
2255   * @retval        Interface status (MANDATORY: return 0 -> no Error).
2256   *
2257   */
iis3dwb_wkup_ths_weight_get(const stmdev_ctx_t * ctx,iis3dwb_wake_ths_w_t * val)2258 int32_t iis3dwb_wkup_ths_weight_get(const stmdev_ctx_t *ctx,
2259                                     iis3dwb_wake_ths_w_t *val)
2260 {
2261   iis3dwb_wake_up_dur_t wake_up_dur;
2262 
2263   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_WAKE_UP_DUR,
2264                          (uint8_t *)&wake_up_dur, 1);
2265 
2266   switch (wake_up_dur.wake_ths_w)
2267   {
2268     case IIS3DWB_LSb_FS_DIV_64:
2269       *val = IIS3DWB_LSb_FS_DIV_64;
2270       break;
2271 
2272     case IIS3DWB_LSb_FS_DIV_256:
2273       *val = IIS3DWB_LSb_FS_DIV_256;
2274       break;
2275 
2276     default:
2277       *val = IIS3DWB_LSb_FS_DIV_64;
2278       break;
2279   }
2280 
2281   return ret;
2282 }
2283 
2284 /**
2285   * @brief  Threshold for wakeup: 1 LSB weight depends on WAKE_THS_W in
2286   *         WAKE_UP_DUR. This function is mandatory for activate the
2287   *         wake up (and activity/inactivity) logic.[set]
2288   *
2289   * @param  ctx    Read / write interface definitions.(ptr)
2290   * @param  val    Change the values of wk_ths in reg WAKE_UP_THS
2291   * @retval        Interface status (MANDATORY: return 0 -> no Error).
2292   *
2293   */
iis3dwb_wkup_threshold_set(const stmdev_ctx_t * ctx,uint8_t val)2294 int32_t iis3dwb_wkup_threshold_set(const stmdev_ctx_t *ctx, uint8_t val)
2295 {
2296   iis3dwb_interrupts_en_t interrupts_en;
2297   iis3dwb_wake_up_ths_t wake_up_ths;
2298 
2299   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_WAKE_UP_THS,
2300                          (uint8_t *)&wake_up_ths, 1);
2301 
2302   if (ret == 0)
2303   {
2304     wake_up_ths.wk_ths = (uint8_t)val;
2305     ret = iis3dwb_write_reg(ctx, IIS3DWB_WAKE_UP_THS,
2306                             (uint8_t *)&wake_up_ths, 1);
2307   }
2308 
2309   if (ret == 0)
2310   {
2311     ret = iis3dwb_read_reg(ctx, IIS3DWB_INTERRUPTS_EN,
2312                            (uint8_t *)&interrupts_en, 1);
2313   }
2314 
2315   if (ret == 0)
2316   {
2317     interrupts_en.interrupts_enable = PROPERTY_ENABLE;
2318     ret = iis3dwb_write_reg(ctx, IIS3DWB_INTERRUPTS_EN,
2319                             (uint8_t *)&interrupts_en, 1);
2320   }
2321 
2322   return ret;
2323 }
2324 
2325 /**
2326   * @brief  Threshold for wakeup: 1 LSB weight depends on WAKE_THS_W in
2327   *         WAKE_UP_DUR.[get]
2328   *
2329   * @param  ctx    Read / write interface definitions.(ptr)
2330   * @param  val    Change the values of wk_ths in reg WAKE_UP_THS
2331   * @retval        Interface status (MANDATORY: return 0 -> no Error).
2332   *
2333   */
iis3dwb_wkup_threshold_get(const stmdev_ctx_t * ctx,uint8_t * val)2334 int32_t iis3dwb_wkup_threshold_get(const stmdev_ctx_t *ctx, uint8_t *val)
2335 {
2336   iis3dwb_wake_up_ths_t wake_up_ths;
2337 
2338   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_WAKE_UP_THS,
2339                          (uint8_t *)&wake_up_ths, 1);
2340   *val = wake_up_ths.wk_ths;
2341 
2342   return ret;
2343 }
2344 
2345 /**
2346   * @brief  Wake up duration event( 1LSb = 1 / ODR ).[set]
2347   *
2348   * @param  ctx    Read / write interface definitions.(ptr)
2349   * @param  val    Change the values of usr_off_on_wu in reg WAKE_UP_THS
2350   * @retval        Interface status (MANDATORY: return 0 -> no Error).
2351   *
2352   */
iis3dwb_xl_usr_offset_on_wkup_set(const stmdev_ctx_t * ctx,uint8_t val)2353 int32_t iis3dwb_xl_usr_offset_on_wkup_set(const stmdev_ctx_t *ctx,
2354                                           uint8_t val)
2355 {
2356   iis3dwb_wake_up_ths_t wake_up_ths;
2357 
2358   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_WAKE_UP_THS,
2359                          (uint8_t *)&wake_up_ths, 1);
2360 
2361   if (ret == 0)
2362   {
2363     wake_up_ths.usr_off_on_wu = (uint8_t)val;
2364     ret = iis3dwb_write_reg(ctx, IIS3DWB_WAKE_UP_THS,
2365                             (uint8_t *)&wake_up_ths, 1);
2366   }
2367 
2368   return ret;
2369 }
2370 
2371 /**
2372   * @brief  Wake up duration event( 1LSb = 1 / ODR ).[get]
2373   *
2374   * @param  ctx    Read / write interface definitions.(ptr)
2375   * @param  val    Change the values of usr_off_on_wu in reg WAKE_UP_THS
2376   * @retval        Interface status (MANDATORY: return 0 -> no Error).
2377   *
2378   */
iis3dwb_xl_usr_offset_on_wkup_get(const stmdev_ctx_t * ctx,uint8_t * val)2379 int32_t iis3dwb_xl_usr_offset_on_wkup_get(const stmdev_ctx_t *ctx,
2380                                           uint8_t *val)
2381 {
2382   iis3dwb_wake_up_ths_t wake_up_ths;
2383 
2384   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_WAKE_UP_THS,
2385                          (uint8_t *)&wake_up_ths, 1);
2386   *val = wake_up_ths.usr_off_on_wu;
2387 
2388   return ret;
2389 }
2390 
2391 /**
2392   * @brief  Wake up duration event(1LSb = 1 / ODR).[set]
2393   *
2394   * @param  ctx    Read / write interface definitions.(ptr)
2395   * @param  val    Change the values of wake_dur in reg WAKE_UP_DUR
2396   * @retval        Interface status (MANDATORY: return 0 -> no Error).
2397   *
2398   */
iis3dwb_wkup_dur_set(const stmdev_ctx_t * ctx,uint8_t val)2399 int32_t iis3dwb_wkup_dur_set(const stmdev_ctx_t *ctx, uint8_t val)
2400 {
2401   iis3dwb_wake_up_dur_t wake_up_dur;
2402 
2403   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_WAKE_UP_DUR,
2404                          (uint8_t *)&wake_up_dur, 1);
2405 
2406   if (ret == 0)
2407   {
2408     wake_up_dur.wake_dur = (uint8_t)val;
2409     ret = iis3dwb_write_reg(ctx, IIS3DWB_WAKE_UP_DUR,
2410                             (uint8_t *)&wake_up_dur, 1);
2411   }
2412 
2413   return ret;
2414 }
2415 
2416 /**
2417   * @brief  Wake up duration event(1LSb = 1 / ODR).[get]
2418   *
2419   * @param  ctx    Read / write interface definitions.(ptr)
2420   * @param  val    Change the values of wake_dur in reg WAKE_UP_DUR
2421   * @retval        Interface status (MANDATORY: return 0 -> no Error).
2422   *
2423   */
iis3dwb_wkup_dur_get(const stmdev_ctx_t * ctx,uint8_t * val)2424 int32_t iis3dwb_wkup_dur_get(const stmdev_ctx_t *ctx, uint8_t *val)
2425 {
2426   iis3dwb_wake_up_dur_t wake_up_dur;
2427 
2428   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_WAKE_UP_DUR,
2429                          (uint8_t *)&wake_up_dur, 1);
2430   *val = wake_up_dur.wake_dur;
2431 
2432   return ret;
2433 }
2434 
2435 /**
2436   * @}
2437   *
2438   */
2439 
2440 /**
2441   * @defgroup   IIS3DWB_ Activity/Inactivity_detection
2442   * @brief      This section groups all the functions concerning
2443   *             activity/inactivity detection.
2444   * @{
2445   *
2446   */
2447 
2448 /**
2449   * @brief  Duration to go in sleep mode (1 LSb = 512 / ODR).[set]
2450   *
2451   * @param  ctx    Read / write interface definitions.(ptr)
2452   * @param  val    Change the values of sleep_dur in reg WAKE_UP_DUR
2453   * @retval        Interface status (MANDATORY: return 0 -> no Error).
2454   *
2455   */
iis3dwb_act_sleep_dur_set(const stmdev_ctx_t * ctx,uint8_t val)2456 int32_t iis3dwb_act_sleep_dur_set(const stmdev_ctx_t *ctx, uint8_t val)
2457 {
2458   iis3dwb_wake_up_dur_t wake_up_dur;
2459 
2460   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_WAKE_UP_DUR,
2461                          (uint8_t *)&wake_up_dur, 1);
2462 
2463   if (ret == 0)
2464   {
2465     wake_up_dur.sleep_dur = (uint8_t)val;
2466     ret = iis3dwb_write_reg(ctx, IIS3DWB_WAKE_UP_DUR,
2467                             (uint8_t *)&wake_up_dur, 1);
2468   }
2469 
2470   return ret;
2471 }
2472 
2473 /**
2474   * @brief  Duration to go in sleep mode.(1 LSb = 512 / ODR).[get]
2475   *
2476   * @param  ctx    Read / write interface definitions.(ptr)
2477   * @param  val    Change the values of sleep_dur in reg WAKE_UP_DUR
2478   * @retval        Interface status (MANDATORY: return 0 -> no Error).
2479   *
2480   */
iis3dwb_act_sleep_dur_get(const stmdev_ctx_t * ctx,uint8_t * val)2481 int32_t iis3dwb_act_sleep_dur_get(const stmdev_ctx_t *ctx, uint8_t *val)
2482 {
2483   iis3dwb_wake_up_dur_t wake_up_dur;
2484 
2485   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_WAKE_UP_DUR,
2486                          (uint8_t *)&wake_up_dur, 1);
2487   *val = wake_up_dur.sleep_dur;
2488 
2489   return ret;
2490 }
2491 
2492 /**
2493   * @}
2494   *
2495   */
2496 
2497 /**
2498   * @defgroup   IIS3DWB_fifo
2499   * @brief      This section group all the functions concerning
2500   *             the fifo usage
2501   * @{
2502   *
2503   */
2504 
2505 /**
2506   * @brief  FIFO watermark level selection.[set]
2507   *
2508   * @param  ctx    Read / write interface definitions.(ptr)
2509   * @param  val    Change the values of wtm in reg FIFO_CTRL1
2510   * @retval        Interface status (MANDATORY: return 0 -> no Error).
2511   *
2512   */
iis3dwb_fifo_watermark_set(const stmdev_ctx_t * ctx,uint16_t val)2513 int32_t iis3dwb_fifo_watermark_set(const stmdev_ctx_t *ctx, uint16_t val)
2514 {
2515   iis3dwb_fifo_ctrl1_t fifo_ctrl1;
2516   iis3dwb_fifo_ctrl2_t fifo_ctrl2;
2517 
2518   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_FIFO_CTRL2,
2519                          (uint8_t *)&fifo_ctrl2, 1);
2520 
2521   if (ret == 0)
2522   {
2523     fifo_ctrl1.wtm = (uint8_t)(0x00FFU & val);
2524     ret = iis3dwb_write_reg(ctx, IIS3DWB_FIFO_CTRL1,
2525                             (uint8_t *)&fifo_ctrl1, 1);
2526   }
2527 
2528   if (ret == 0)
2529   {
2530     fifo_ctrl2.wtm = (uint8_t)((0x0100U & val) >> 8);
2531     ret = iis3dwb_write_reg(ctx, IIS3DWB_FIFO_CTRL2,
2532                             (uint8_t *)&fifo_ctrl2, 1);
2533   }
2534 
2535   return ret;
2536 }
2537 
2538 /**
2539   * @brief  FIFO watermark level selection.[get]
2540   *
2541   * @param  ctx    Read / write interface definitions.(ptr)
2542   * @param  val    Change the values of wtm in reg FIFO_CTRL1
2543   * @retval        Interface status (MANDATORY: return 0 -> no Error).
2544   *
2545   */
iis3dwb_fifo_watermark_get(const stmdev_ctx_t * ctx,uint16_t * val)2546 int32_t iis3dwb_fifo_watermark_get(const stmdev_ctx_t *ctx, uint16_t *val)
2547 {
2548   iis3dwb_fifo_ctrl1_t fifo_ctrl1;
2549   iis3dwb_fifo_ctrl2_t fifo_ctrl2;
2550 
2551   *val = 0;
2552 
2553   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_FIFO_CTRL2,
2554                          (uint8_t *)&fifo_ctrl2, 1);
2555   if (ret != 0) { return ret; }
2556 
2557   ret = iis3dwb_read_reg(ctx, IIS3DWB_FIFO_CTRL1,
2558                            (uint8_t *)&fifo_ctrl1, 1);
2559 
2560   *val = fifo_ctrl2.wtm;
2561   *val = *val << 8;
2562   *val += fifo_ctrl1.wtm;
2563 
2564   return ret;
2565 }
2566 
2567 /**
2568   * @brief  Sensing chain FIFO stop values memorization at threshold
2569   *         level.[set]
2570   *
2571   * @param  ctx    Read / write interface definitions.(ptr)
2572   * @param  val    Change the values of stop_on_wtm in reg FIFO_CTRL2
2573   * @retval        Interface status (MANDATORY: return 0 -> no Error).
2574   *
2575   */
iis3dwb_fifo_stop_on_wtm_set(const stmdev_ctx_t * ctx,uint8_t val)2576 int32_t iis3dwb_fifo_stop_on_wtm_set(const stmdev_ctx_t *ctx, uint8_t val)
2577 {
2578   iis3dwb_fifo_ctrl2_t fifo_ctrl2;
2579 
2580   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_FIFO_CTRL2,
2581                          (uint8_t *)&fifo_ctrl2, 1);
2582 
2583   if (ret == 0)
2584   {
2585     fifo_ctrl2.stop_on_wtm = (uint8_t)val;
2586     ret = iis3dwb_write_reg(ctx, IIS3DWB_FIFO_CTRL2,
2587                             (uint8_t *)&fifo_ctrl2, 1);
2588   }
2589 
2590   return ret;
2591 }
2592 
2593 /**
2594   * @brief  Sensing chain FIFO stop values memorization at threshold
2595   *         level.[get]
2596   *
2597   * @param  ctx    Read / write interface definitions.(ptr)
2598   * @param  val    Change the values of stop_on_wtm in reg FIFO_CTRL2
2599   * @retval        Interface status (MANDATORY: return 0 -> no Error).
2600   *
2601   */
iis3dwb_fifo_stop_on_wtm_get(const stmdev_ctx_t * ctx,uint8_t * val)2602 int32_t iis3dwb_fifo_stop_on_wtm_get(const stmdev_ctx_t *ctx, uint8_t *val)
2603 {
2604   iis3dwb_fifo_ctrl2_t fifo_ctrl2;
2605 
2606   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_FIFO_CTRL2,
2607                          (uint8_t *)&fifo_ctrl2, 1);
2608   *val = fifo_ctrl2.stop_on_wtm;
2609 
2610   return ret;
2611 }
2612 
2613 /**
2614   * @brief  Selects Batching Data Rate (writing frequency in FIFO)
2615   *         for accelerometer data.[set]
2616   *
2617   * @param  ctx    Read / write interface definitions.(ptr)
2618   * @param  val    Change the values of bdr_xl in reg FIFO_CTRL3
2619   * @retval        Interface status (MANDATORY: return 0 -> no Error).
2620   *
2621   */
iis3dwb_fifo_xl_batch_set(const stmdev_ctx_t * ctx,iis3dwb_bdr_xl_t val)2622 int32_t iis3dwb_fifo_xl_batch_set(const stmdev_ctx_t *ctx,
2623                                   iis3dwb_bdr_xl_t val)
2624 {
2625   iis3dwb_fifo_ctrl3_t fifo_ctrl3;
2626 
2627   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_FIFO_CTRL3,
2628                          (uint8_t *)&fifo_ctrl3, 1);
2629 
2630   if (ret == 0)
2631   {
2632     fifo_ctrl3.bdr_xl = (uint8_t)val;
2633     ret = iis3dwb_write_reg(ctx, IIS3DWB_FIFO_CTRL3,
2634                             (uint8_t *)&fifo_ctrl3, 1);
2635   }
2636 
2637   return ret;
2638 }
2639 
2640 /**
2641   * @brief  Selects Batching Data Rate (writing frequency in FIFO)
2642   *         for accelerometer data.[get]
2643   *
2644   * @param  ctx    Read / write interface definitions.(ptr)
2645   * @param  val    Get the values of bdr_xl in reg FIFO_CTRL3
2646   * @retval        Interface status (MANDATORY: return 0 -> no Error).
2647   *
2648   */
iis3dwb_fifo_xl_batch_get(const stmdev_ctx_t * ctx,iis3dwb_bdr_xl_t * val)2649 int32_t iis3dwb_fifo_xl_batch_get(const stmdev_ctx_t *ctx,
2650                                   iis3dwb_bdr_xl_t *val)
2651 {
2652   iis3dwb_fifo_ctrl3_t fifo_ctrl3;
2653 
2654   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_FIFO_CTRL3,
2655                          (uint8_t *)&fifo_ctrl3, 1);
2656 
2657   switch (fifo_ctrl3.bdr_xl)
2658   {
2659     case IIS3DWB_XL_NOT_BATCHED:
2660       *val = IIS3DWB_XL_NOT_BATCHED;
2661       break;
2662 
2663     case IIS3DWB_XL_BATCHED_AT_26k7Hz:
2664       *val = IIS3DWB_XL_BATCHED_AT_26k7Hz;
2665       break;
2666 
2667     default:
2668       *val = IIS3DWB_XL_NOT_BATCHED;
2669       break;
2670   }
2671 
2672   return ret;
2673 }
2674 
2675 /**
2676   * @brief  FIFO mode selection.[set]
2677   *
2678   * @param  ctx    Read / write interface definitions.(ptr)
2679   * @param  val    Change the values of fifo_mode in reg FIFO_CTRL4
2680   * @retval        Interface status (MANDATORY: return 0 -> no Error).
2681   *
2682   */
iis3dwb_fifo_mode_set(const stmdev_ctx_t * ctx,iis3dwb_fifo_mode_t val)2683 int32_t iis3dwb_fifo_mode_set(const stmdev_ctx_t *ctx,
2684                               iis3dwb_fifo_mode_t val)
2685 {
2686   iis3dwb_fifo_ctrl4_t fifo_ctrl4;
2687 
2688   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_FIFO_CTRL4,
2689                          (uint8_t *)&fifo_ctrl4, 1);
2690 
2691   if (ret == 0)
2692   {
2693     fifo_ctrl4.fifo_mode = (uint8_t)val;
2694     ret = iis3dwb_write_reg(ctx, IIS3DWB_FIFO_CTRL4,
2695                             (uint8_t *)&fifo_ctrl4, 1);
2696   }
2697 
2698   return ret;
2699 }
2700 
2701 /**
2702   * @brief  FIFO mode selection.[get]
2703   *
2704   * @param  ctx    Read / write interface definitions.(ptr)
2705   * @param  val    Get the values of fifo_mode in reg FIFO_CTRL4
2706   * @retval        Interface status (MANDATORY: return 0 -> no Error).
2707   *
2708   */
iis3dwb_fifo_mode_get(const stmdev_ctx_t * ctx,iis3dwb_fifo_mode_t * val)2709 int32_t iis3dwb_fifo_mode_get(const stmdev_ctx_t *ctx,
2710                               iis3dwb_fifo_mode_t *val)
2711 {
2712   iis3dwb_fifo_ctrl4_t fifo_ctrl4;
2713 
2714   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_FIFO_CTRL4,
2715                          (uint8_t *)&fifo_ctrl4, 1);
2716 
2717   switch (fifo_ctrl4.fifo_mode)
2718   {
2719     case IIS3DWB_BYPASS_MODE:
2720       *val = IIS3DWB_BYPASS_MODE;
2721       break;
2722 
2723     case IIS3DWB_FIFO_MODE:
2724       *val = IIS3DWB_FIFO_MODE;
2725       break;
2726 
2727     case IIS3DWB_STREAM_TO_FIFO_MODE:
2728       *val = IIS3DWB_STREAM_TO_FIFO_MODE;
2729       break;
2730 
2731     case IIS3DWB_BYPASS_TO_STREAM_MODE:
2732       *val = IIS3DWB_BYPASS_TO_STREAM_MODE;
2733       break;
2734 
2735     case IIS3DWB_STREAM_MODE:
2736       *val = IIS3DWB_STREAM_MODE;
2737       break;
2738 
2739     case IIS3DWB_BYPASS_TO_FIFO_MODE:
2740       *val = IIS3DWB_BYPASS_TO_FIFO_MODE;
2741       break;
2742 
2743     default:
2744       *val = IIS3DWB_BYPASS_MODE;
2745       break;
2746   }
2747 
2748   return ret;
2749 }
2750 
2751 /**
2752   * @brief  Selects Batching Data Rate (writing frequency in FIFO)
2753   *         for temperature data.[set]
2754   *
2755   * @param  ctx    Read / write interface definitions.(ptr)
2756   * @param  val    Change the values of odr_t_batch in reg FIFO_CTRL4
2757   * @retval        Interface status (MANDATORY: return 0 -> no Error).
2758   *
2759   */
iis3dwb_fifo_temp_batch_set(const stmdev_ctx_t * ctx,iis3dwb_odr_t_batch_t val)2760 int32_t iis3dwb_fifo_temp_batch_set(const stmdev_ctx_t *ctx,
2761                                     iis3dwb_odr_t_batch_t val)
2762 {
2763   iis3dwb_fifo_ctrl4_t fifo_ctrl4;
2764 
2765   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_FIFO_CTRL4,
2766                          (uint8_t *)&fifo_ctrl4, 1);
2767 
2768   if (ret == 0)
2769   {
2770     fifo_ctrl4.odr_t_batch = (uint8_t)val;
2771     ret = iis3dwb_write_reg(ctx, IIS3DWB_FIFO_CTRL4,
2772                             (uint8_t *)&fifo_ctrl4, 1);
2773   }
2774 
2775   return ret;
2776 }
2777 
2778 /**
2779   * @brief  Selects Batching Data Rate (writing frequency in FIFO)
2780   *         for temperature data.[get]
2781   *
2782   * @param  ctx    Read / write interface definitions.(ptr)
2783   * @param  val    Get the values of odr_t_batch in reg FIFO_CTRL4
2784   * @retval        Interface status (MANDATORY: return 0 -> no Error).
2785   *
2786   */
iis3dwb_fifo_temp_batch_get(const stmdev_ctx_t * ctx,iis3dwb_odr_t_batch_t * val)2787 int32_t iis3dwb_fifo_temp_batch_get(const stmdev_ctx_t *ctx,
2788                                     iis3dwb_odr_t_batch_t *val)
2789 {
2790   iis3dwb_fifo_ctrl4_t fifo_ctrl4;
2791 
2792   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_FIFO_CTRL4,
2793                          (uint8_t *)&fifo_ctrl4, 1);
2794 
2795   switch (fifo_ctrl4.odr_t_batch)
2796   {
2797     case IIS3DWB_TEMP_NOT_BATCHED:
2798       *val = IIS3DWB_TEMP_NOT_BATCHED;
2799       break;
2800 
2801     case IIS3DWB_TEMP_BATCHED_AT_104Hz:
2802       *val = IIS3DWB_TEMP_BATCHED_AT_104Hz;
2803       break;
2804 
2805     default:
2806       *val = IIS3DWB_TEMP_NOT_BATCHED;
2807       break;
2808   }
2809 
2810   return ret;
2811 }
2812 
2813 /**
2814   * @brief  Selects decimation for timestamp batching in FIFO.
2815   *         Writing rate will be the maximum rate between XL and
2816   *         GYRO BDR divided by decimation decoder.[set]
2817   *
2818   * @param  ctx    Read / write interface definitions.(ptr)
2819   * @param  val    Change the values of odr_ts_batch in reg FIFO_CTRL4
2820   * @retval        Interface status (MANDATORY: return 0 -> no Error).
2821   *
2822   */
iis3dwb_fifo_timestamp_batch_set(const stmdev_ctx_t * ctx,iis3dwb_fifo_timestamp_batch_t val)2823 int32_t iis3dwb_fifo_timestamp_batch_set(const stmdev_ctx_t *ctx,
2824                                          iis3dwb_fifo_timestamp_batch_t val)
2825 {
2826   iis3dwb_fifo_ctrl4_t fifo_ctrl4;
2827 
2828   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_FIFO_CTRL4,
2829                          (uint8_t *)&fifo_ctrl4, 1);
2830 
2831   if (ret == 0)
2832   {
2833     fifo_ctrl4.odr_ts_batch = (uint8_t)val;
2834     ret = iis3dwb_write_reg(ctx, IIS3DWB_FIFO_CTRL4,
2835                             (uint8_t *)&fifo_ctrl4, 1);
2836   }
2837 
2838   return ret;
2839 }
2840 
2841 /**
2842   * @brief  Selects decimation for timestamp batching in FIFO.
2843   *         Writing rate will be the maximum rate between XL and
2844   *         GYRO BDR divided by decimation decoder.[get]
2845   *
2846   * @param  ctx    Read / write interface definitions.(ptr)
2847   * @param  val    Get the values of odr_ts_batch in reg
2848   *                                 FIFO_CTRL4
2849   * @retval        Interface status (MANDATORY: return 0 -> no Error).
2850   *
2851   */
iis3dwb_fifo_timestamp_batch_get(const stmdev_ctx_t * ctx,iis3dwb_fifo_timestamp_batch_t * val)2852 int32_t iis3dwb_fifo_timestamp_batch_get(const stmdev_ctx_t *ctx,
2853                                          iis3dwb_fifo_timestamp_batch_t *val)
2854 {
2855   iis3dwb_fifo_ctrl4_t fifo_ctrl4;
2856 
2857   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_FIFO_CTRL4,
2858                          (uint8_t *)&fifo_ctrl4, 1);
2859 
2860   switch (fifo_ctrl4.odr_ts_batch)
2861   {
2862     case IIS3DWB_NO_DECIMATION:
2863       *val = IIS3DWB_NO_DECIMATION;
2864       break;
2865 
2866     case IIS3DWB_DEC_1:
2867       *val = IIS3DWB_DEC_1;
2868       break;
2869 
2870     case IIS3DWB_DEC_8:
2871       *val = IIS3DWB_DEC_8;
2872       break;
2873 
2874     case IIS3DWB_DEC_32:
2875       *val = IIS3DWB_DEC_32;
2876       break;
2877 
2878     default:
2879       *val = IIS3DWB_NO_DECIMATION;
2880       break;
2881   }
2882 
2883   return ret;
2884 }
2885 
2886 /**
2887   * @brief  Resets the internal counter of batching events for a single sensor.
2888   *         This bit is automatically reset to zero if it was set to ‘1’.[set]
2889   *
2890   * @param  ctx    Read / write interface definitions.(ptr)
2891   * @param  val    Change the values of rst_counter_bdr in reg COUNTER_BDR_REG1
2892   * @retval        Interface status (MANDATORY: return 0 -> no Error).
2893   *
2894   */
iis3dwb_rst_batch_counter_set(const stmdev_ctx_t * ctx,uint8_t val)2895 int32_t iis3dwb_rst_batch_counter_set(const stmdev_ctx_t *ctx, uint8_t val)
2896 {
2897   iis3dwb_counter_bdr_reg1_t counter_bdr_reg1;
2898 
2899   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_COUNTER_BDR_REG1,
2900                          (uint8_t *)&counter_bdr_reg1, 1);
2901 
2902   if (ret == 0)
2903   {
2904     counter_bdr_reg1.rst_counter_bdr = (uint8_t)val;
2905     ret = iis3dwb_write_reg(ctx, IIS3DWB_COUNTER_BDR_REG1,
2906                             (uint8_t *)&counter_bdr_reg1, 1);
2907   }
2908 
2909   return ret;
2910 }
2911 
2912 /**
2913   * @brief  Resets the internal counter of batching events for a single sensor.
2914   *         This bit is automatically reset to zero if it was set to ‘1’.[get]
2915   *
2916   * @param  ctx    Read / write interface definitions.(ptr)
2917   * @param  val    Change the values of rst_counter_bdr in reg COUNTER_BDR_REG1
2918   * @retval        Interface status (MANDATORY: return 0 -> no Error).
2919   *
2920   */
iis3dwb_rst_batch_counter_get(const stmdev_ctx_t * ctx,uint8_t * val)2921 int32_t iis3dwb_rst_batch_counter_get(const stmdev_ctx_t *ctx, uint8_t *val)
2922 {
2923   iis3dwb_counter_bdr_reg1_t counter_bdr_reg1;
2924 
2925   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_COUNTER_BDR_REG1,
2926                          (uint8_t *)&counter_bdr_reg1, 1);
2927   *val = counter_bdr_reg1.rst_counter_bdr;
2928 
2929   return ret;
2930 }
2931 
2932 /**
2933   * @brief  Batch data rate counter.[set]
2934   *
2935   * @param  ctx    Read / write interface definitions.(ptr)
2936   * @param  val    Change the values of cnt_bdr_th in reg COUNTER_BDR_REG2
2937   *                and COUNTER_BDR_REG1.
2938   * @retval        Interface status (MANDATORY: return 0 -> no Error).
2939   *
2940   */
iis3dwb_batch_counter_threshold_set(const stmdev_ctx_t * ctx,uint16_t val)2941 int32_t iis3dwb_batch_counter_threshold_set(const stmdev_ctx_t *ctx,
2942                                             uint16_t val)
2943 {
2944   iis3dwb_counter_bdr_reg2_t counter_bdr_reg1;
2945   iis3dwb_counter_bdr_reg2_t counter_bdr_reg2;
2946 
2947   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_COUNTER_BDR_REG1,
2948                          (uint8_t *)&counter_bdr_reg1, 1);
2949 
2950   if (ret == 0)
2951   {
2952     counter_bdr_reg1.cnt_bdr_th = (uint8_t)((0x0700U & val) >> 8);
2953     ret = iis3dwb_write_reg(ctx, IIS3DWB_COUNTER_BDR_REG1,
2954                             (uint8_t *)&counter_bdr_reg1, 1);
2955   }
2956 
2957   if (ret == 0)
2958   {
2959     counter_bdr_reg2.cnt_bdr_th = (uint8_t)(0x00FFU & val);
2960     ret = iis3dwb_write_reg(ctx, IIS3DWB_COUNTER_BDR_REG2,
2961                             (uint8_t *)&counter_bdr_reg2, 1);
2962   }
2963 
2964   return ret;
2965 }
2966 
2967 /**
2968   * @brief  Batch data rate counter.[get]
2969   *
2970   * @param  ctx    Read / write interface definitions.(ptr)
2971   * @param  val    Change the values of cnt_bdr_th in reg COUNTER_BDR_REG2
2972   *                and COUNTER_BDR_REG1.
2973   * @retval        Interface status (MANDATORY: return 0 -> no Error).
2974   *
2975   */
iis3dwb_batch_counter_threshold_get(const stmdev_ctx_t * ctx,uint16_t * val)2976 int32_t iis3dwb_batch_counter_threshold_get(const stmdev_ctx_t *ctx,
2977                                             uint16_t *val)
2978 {
2979   iis3dwb_counter_bdr_reg1_t counter_bdr_reg1;
2980   iis3dwb_counter_bdr_reg2_t counter_bdr_reg2;
2981 
2982   *val = 0;
2983 
2984   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_COUNTER_BDR_REG1,
2985                          (uint8_t *)&counter_bdr_reg1, 1);
2986   if (ret != 0) { return ret; }
2987 
2988   ret = iis3dwb_read_reg(ctx, IIS3DWB_COUNTER_BDR_REG2,
2989                            (uint8_t *)&counter_bdr_reg2, 1);
2990   if (ret != 0) { return ret; }
2991 
2992   *val = counter_bdr_reg1.cnt_bdr_th;
2993   *val = *val << 8;
2994   *val += counter_bdr_reg2.cnt_bdr_th;
2995 
2996   return ret;
2997 }
2998 
2999 /**
3000   * @brief  Number of unread sensor data (TAG + 6 bytes) stored in FIFO.[get]
3001   *
3002   * @param  ctx    Read / write interface definitions.(ptr)
3003   * @param  val    Change the values of diff_fifo in reg FIFO_STATUS1
3004   * @retval        Interface status (MANDATORY: return 0 -> no Error).
3005   *
3006   */
iis3dwb_fifo_data_level_get(const stmdev_ctx_t * ctx,uint16_t * val)3007 int32_t iis3dwb_fifo_data_level_get(const stmdev_ctx_t *ctx, uint16_t *val)
3008 {
3009   iis3dwb_fifo_status1_t fifo_status1;
3010   iis3dwb_fifo_status2_t fifo_status2;
3011 
3012   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_FIFO_STATUS1,
3013                          (uint8_t *)&fifo_status1, 1);
3014 
3015   if (ret == 0)
3016   {
3017     ret = iis3dwb_read_reg(ctx, IIS3DWB_FIFO_STATUS2,
3018                            (uint8_t *)&fifo_status2, 1);
3019     *val = fifo_status2.diff_fifo;
3020     *val = *val << 8;
3021     *val += fifo_status1.diff_fifo;
3022   }
3023 
3024   return ret;
3025 }
3026 
3027 /**
3028   * @brief  Smart FIFO status.[get]
3029   *
3030   * @param  ctx    Read / write interface definitions.(ptr)
3031   * @param  val    Registers FIFO_STATUS1 and FIFO_STATUS2
3032   * @retval        Interface status (MANDATORY: return 0 -> no Error).
3033   *
3034   */
iis3dwb_fifo_status_get(const stmdev_ctx_t * ctx,iis3dwb_fifo_status_t * val)3035 int32_t iis3dwb_fifo_status_get(const stmdev_ctx_t *ctx,
3036                                 iis3dwb_fifo_status_t *val)
3037 {
3038   uint8_t buff[2];
3039   iis3dwb_fifo_status2_t status;
3040 
3041   int32_t ret = iis3dwb_read_reg(ctx, IIS3DWB_FIFO_STATUS1, (uint8_t *)&buff[0], 2);
3042   bytecpy((uint8_t *)&status, &buff[1]);
3043 
3044   val->fifo_bdr = status.counter_bdr_ia;
3045   val->fifo_ovr = status.fifo_ovr_ia | status.fifo_ovr_latched;
3046   val->fifo_full = status.fifo_full_ia;
3047   val->fifo_th = status.fifo_wtm_ia;
3048 
3049   val->fifo_level = (uint16_t)buff[1] & 0x03U;
3050   val->fifo_level = (val->fifo_level * 256U) + buff[0];
3051 
3052   return ret;
3053 }
3054 
3055 /**
3056   * @}
3057   *
3058   */
3059 
3060 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
3061