LIS2DW12 accelerometer sensor library

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Dependents:   X_NUCLEO_IKS01A3

Revision:
2:a94816b14e3d
Parent:
1:94e908301953
Child:
3:111317ba9301
--- a/lis2dw12_reg.h	Mon Nov 19 14:35:00 2018 +0000
+++ b/lis2dw12_reg.h	Wed Nov 21 15:41:28 2018 +0000
@@ -1,8 +1,7 @@
-/**
+/*
  ******************************************************************************
  * @file    lis2dw12_reg.h
- * @author  MEMS Software Solution Team
- * @date    25-January-2018
+ * @author  Sensors Software Solution Team
  * @brief   This file contains all the functions prototypes for the 
  *          lis2dw12_reg.c driver.
  ******************************************************************************
@@ -10,34 +9,35 @@
  *
  * <h2><center>&copy; COPYRIGHT(c) 2018 STMicroelectronics</center></h2>
  *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
- *   2. Redistributions in binary form must reproduce the above copyright notice,
- *      this list of conditions and the following disclaimer in the documentation
- *      and/or other materials provided with the distribution.
- *   3. Neither the name of STMicroelectronics nor the names of its contributors
- *      may be used to endorse or promote products derived from this software
- *      without specific prior written permission.
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *   3. Neither the name of STMicroelectronics nor the names of its
+ *      contributors may be used to endorse or promote products derived from
+ *      this software without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
  *
- ******************************************************************************
  */
  
 /* Define to prevent recursive inclusion -------------------------------------*/
-#ifndef __LIS2DW12_DRIVER__H
-#define __LIS2DW12_DRIVER__H
+#ifndef LIS2DW12_REGS_H
+#define LIS2DW12_REGS_H
 
 #ifdef __cplusplus
   extern "C" {
@@ -45,51 +45,59 @@
 
 /* Includes ------------------------------------------------------------------*/
 #include <stdint.h>
+#include <math.h>
 
-/** @addtogroup lis2dw12
- * @{
- */
- 
+/** @addtogroup LIS2DW12
+  * @{
+  *
+  */
+
+/** @defgroup LIS2DW12_sensors_common_types
+  * @{
+  *
+  */
+
 #ifndef MEMS_SHARED_TYPES
 #define MEMS_SHARED_TYPES
 
-/** @defgroup ST_MEMS_common_types
+/**
+  * @defgroup axisXbitXX_t
+  * @brief    These unions are useful to represent different sensors data type.
+  *           These unions are not need by the driver.
+  *
+  *           REMOVING the unions you are compliant with:
+  *           MISRA-C 2012 [Rule 19.2] -> " Union are not allowed "
+  *
   * @{
+  *
   */
 
-/**
-  * Float typedef definition for compliance with:
-  * MISRA-C 2012 [Dir 4.6a] -> " Basic type used without typedef "
-  *
-  */
-typedef float float32_t;
-
-typedef union
-{
+typedef union{
   int16_t i16bit[3];
   uint8_t u8bit[6];
 } axis3bit16_t;
 
-typedef union
-{
+typedef union{
   int16_t i16bit;
   uint8_t u8bit[2];
 } axis1bit16_t;
 
-typedef union
-{
+typedef union{
   int32_t i32bit[3];
   uint8_t u8bit[12];
 } axis3bit32_t;
 
-typedef union
-{
+typedef union{
   int32_t i32bit;
   uint8_t u8bit[4];
 } axis1bit32_t;
 
-typedef struct
-{
+/**
+  * @}
+  *
+  */
+
+typedef struct{
   uint8_t bit0       : 1;
   uint8_t bit1       : 1;
   uint8_t bit2       : 1;
@@ -100,18 +108,23 @@
   uint8_t bit7       : 1;
 } bitwise_t;
 
-#define PROPERTY_DISABLE                (0)
-#define PROPERTY_ENABLE                 (1)
+#define PROPERTY_DISABLE                (0U)
+#define PROPERTY_ENABLE                 (1U)
 
-#endif /* MEMS_SHARED_TYPES*/
+#endif /* MEMS_SHARED_TYPES */
 
 /**
   * @}
+  *
   */
 
-/** @defgroup lis2dw12_interface
+/** @addtogroup  LIS2DW12_Interfaces_Functions
+  * @brief       This section provide a set of functions used to read and
+  *              write a generic register of the device.
+  *              MANDATORY: return 0 -> no Error.
   * @{
-  */  
+  *
+  */ 
 
 typedef int32_t (*lis2dw12_write_ptr)(void *, uint8_t, uint8_t*, uint16_t);
 typedef int32_t (*lis2dw12_read_ptr) (void *, uint8_t, uint8_t*, uint16_t);
@@ -126,26 +139,45 @@
 
 /**
   * @}
-  */ 
+  *
+  */
 
-
-/** @defgroup lis2dw12_Infos
+/** @defgroup LIS2DW12_Infos
   * @{
-  */  
-  /** I2C Device Address 8 bit format  if SA0=0 -> 31 if SA0=1 -> 33 **/
-#define LIS2DW12_I2C_ADD_L   0x31
-#define LIS2DW12_I2C_ADD_H   0x33
+  *
+  */
+  
+/** I2C Device Address 8 bit format  if SA0=0 -> 31 if SA0=1 -> 33 **/
+#define LIS2DW12_I2C_ADD_L   0x31U
+#define LIS2DW12_I2C_ADD_H   0x33U
 
 /** Device Identification (Who am I) **/
-#define LIS2DW12_ID            0x44
+#define LIS2DW12_ID            0x44U
 
 /**
   * @}
+  *
   */
 
-/** 
-  * @defgroup lis2dw12_Sensitivity
+/**
+  * @addtogroup  LIS2DW12_Sensitivity
+  * @brief       These macro are maintained for back compatibility.
+  *              in order to convert data into engineering units please
+  *              use functions:
+  *                -> _from_fs2_to_mg(int16_t lsb);
+  *                -> _from_fs4_to_mg(int16_t lsb);
+  *                -> _from_fs8_to_mg(int16_t lsb);
+  *                -> _from_fs16_to_mg(int16_t lsb);
+  *                -> _from_fs2_lp1_to_mg(int16_t lsb);
+  *                -> _from_fs4_lp1_to_mg(int16_t lsb);
+  *                -> _from_fs8_lp1_to_mg(int16_t lsb);
+  *                -> _from_fs16_lp1_to_mg(int16_t lsb);  
+  *                -> _from_lsb_to_celsius(int16_t lsb);
+  *
+  *              REMOVING the MACRO you are compliant with:
+  *              MISRA-C 2012 [Dir 4.9] -> " avoid function-like macros "
   * @{
+  *
   */
 
 #define LIS2DW12_FROM_FS_2g_TO_mg(lsb)   (float)((int16_t)lsb >> 2) * 0.244f
@@ -153,8 +185,8 @@
 #define LIS2DW12_FROM_FS_8g_TO_mg(lsb)   (float)((int16_t)lsb >> 2) * 0.976f
 #define LIS2DW12_FROM_FS_16g_TO_mg(lsb)  (float)((int16_t)lsb >> 2) * 1.952f
 
-#define LIS2DW12_FROM_FS_2g_LP1_TO_mg(lsb)  (float)((int16_t)lsb>>4)* 0.9760f
-#define LIS2DW12_FROM_FS_4g_LP1_TO_mg(lsb)  (float)((int16_t)lsb>>4)* 1.9520f
+#define LIS2DW12_FROM_FS_2g_LP1_TO_mg(lsb)  (float)((int16_t)lsb>>4)* 0.976f
+#define LIS2DW12_FROM_FS_4g_LP1_TO_mg(lsb)  (float)((int16_t)lsb>>4)* 1.952f
 #define LIS2DW12_FROM_FS_8g_LP1_TO_mg(lsb)  (float)((int16_t)lsb>>4)* 3.904f
 #define LIS2DW12_FROM_FS_16g_LP1_TO_mg(lsb) (float)((int16_t)lsb>>4)* 7.808f
 
@@ -162,19 +194,20 @@
 
 /**
   * @}
+  *
   */
 
-#define LIS2DW12_OUT_T_L                     0x0D
-#define LIS2DW12_OUT_T_H                     0x0E
-#define LIS2DW12_WHO_AM_I                    0x0F
-#define LIS2DW12_CTRL1                       0x20
+#define LIS2DW12_OUT_T_L                     0x0DU
+#define LIS2DW12_OUT_T_H                     0x0EU
+#define LIS2DW12_WHO_AM_I                    0x0FU
+#define LIS2DW12_CTRL1                       0x20U
 typedef struct {
   uint8_t lp_mode                    : 2;
   uint8_t mode                       : 2;
   uint8_t odr                        : 4;
 } lis2dw12_ctrl1_t;
 
-#define LIS2DW12_CTRL2                       0x21
+#define LIS2DW12_CTRL2                       0x21U
 typedef struct {
   uint8_t sim                        : 1;
   uint8_t i2c_disable                : 1;
@@ -186,7 +219,7 @@
   uint8_t boot                       : 1;
 } lis2dw12_ctrl2_t;
 
-#define LIS2DW12_CTRL3                       0x22
+#define LIS2DW12_CTRL3                       0x22U
 typedef struct {
   uint8_t slp_mode                   : 2;  /* slp_mode_sel + slp_mode_1 */
   uint8_t not_used_01                : 1;
@@ -196,7 +229,7 @@
   uint8_t st                         : 2;
 } lis2dw12_ctrl3_t;
 
-#define LIS2DW12_CTRL4_INT1_PAD_CTRL         0x23
+#define LIS2DW12_CTRL4_INT1_PAD_CTRL         0x23U
 typedef struct {
   uint8_t int1_drdy                  : 1;
   uint8_t int1_fth                   : 1;
@@ -208,7 +241,7 @@
   uint8_t int1_6d                    : 1;
 } lis2dw12_ctrl4_int1_pad_ctrl_t;
 
-#define LIS2DW12_CTRL5_INT2_PAD_CTRL         0x24
+#define LIS2DW12_CTRL5_INT2_PAD_CTRL         0x24U
 typedef struct {
   uint8_t int2_drdy                  : 1;
   uint8_t int2_fth                   : 1;
@@ -220,7 +253,7 @@
   uint8_t int2_sleep_state           : 1;
 } lis2dw12_ctrl5_int2_pad_ctrl_t;
 
-#define LIS2DW12_CTRL6                       0x25
+#define LIS2DW12_CTRL6                       0x25U
 typedef struct {
   uint8_t not_used_01                : 2;
   uint8_t low_noise                  : 1;
@@ -229,8 +262,8 @@
   uint8_t bw_filt                    : 2;
 } lis2dw12_ctrl6_t;
 
-#define LIS2DW12_OUT_T                       0x26
-#define LIS2DW12_STATUS                      0x27
+#define LIS2DW12_OUT_T                       0x26U
+#define LIS2DW12_STATUS                      0x27U
 typedef struct {
   uint8_t drdy                       : 1;
   uint8_t ff_ia                      : 1;
@@ -242,39 +275,39 @@
   uint8_t fifo_ths                   : 1;
 } lis2dw12_status_t;
 
-#define LIS2DW12_OUT_X_L                     0x28
-#define LIS2DW12_OUT_X_H                     0x29
-#define LIS2DW12_OUT_Y_L                     0x2A
-#define LIS2DW12_OUT_Y_H                     0x2B
-#define LIS2DW12_OUT_Z_L                     0x2C
-#define LIS2DW12_OUT_Z_H                     0x2D
-#define LIS2DW12_FIFO_CTRL                   0x2E
+#define LIS2DW12_OUT_X_L                     0x28U
+#define LIS2DW12_OUT_X_H                     0x29U
+#define LIS2DW12_OUT_Y_L                     0x2AU
+#define LIS2DW12_OUT_Y_H                     0x2BU
+#define LIS2DW12_OUT_Z_L                     0x2CU
+#define LIS2DW12_OUT_Z_H                     0x2DU
+#define LIS2DW12_FIFO_CTRL                   0x2EU
 typedef struct {
   uint8_t fth                        : 5;
   uint8_t fmode                      : 3;
 } lis2dw12_fifo_ctrl_t;
 
-#define LIS2DW12_FIFO_SAMPLES                0x2F
+#define LIS2DW12_FIFO_SAMPLES                0x2FU
 typedef struct {
   uint8_t diff                       : 6;
   uint8_t fifo_ovr                   : 1;
   uint8_t fifo_fth                   : 1;
 } lis2dw12_fifo_samples_t;
 
-#define LIS2DW12_TAP_THS_X                   0x30
+#define LIS2DW12_TAP_THS_X                   0x30U
 typedef struct {
   uint8_t tap_thsx                    : 5;
   uint8_t _6d_ths                     : 2;
   uint8_t _4d_en                      : 1;
 } lis2dw12_tap_ths_x_t;
 
-#define LIS2DW12_TAP_THS_Y                   0x31
+#define LIS2DW12_TAP_THS_Y                   0x31U
 typedef struct {
   uint8_t tap_thsy                   : 5;
   uint8_t tap_prior                  : 3;
 } lis2dw12_tap_ths_y_t;
 
-#define LIS2DW12_TAP_THS_Z                   0x32
+#define LIS2DW12_TAP_THS_Z                   0x32U
 typedef struct {
   uint8_t tap_thsz                   : 5;
   uint8_t tap_z_en                   : 1;
@@ -282,21 +315,21 @@
   uint8_t tap_x_en                   : 1;
 } lis2dw12_tap_ths_z_t;
 
-#define LIS2DW12_INT_DUR                     0x33
+#define LIS2DW12_INT_DUR                     0x33U
 typedef struct {
   uint8_t shock                      : 2;
   uint8_t quiet                      : 2;
   uint8_t latency                    : 4;
 } lis2dw12_int_dur_t;
 
-#define LIS2DW12_WAKE_UP_THS                 0x34
+#define LIS2DW12_WAKE_UP_THS                 0x34U
 typedef struct {
   uint8_t wk_ths                     : 6;
   uint8_t sleep_on                   : 1;
   uint8_t single_double_tap          : 1;
 } lis2dw12_wake_up_ths_t;
 
-#define LIS2DW12_WAKE_UP_DUR                 0x35
+#define LIS2DW12_WAKE_UP_DUR                 0x35U
 typedef struct {
   uint8_t sleep_dur                  : 4;
   uint8_t stationary                 : 1;
@@ -304,13 +337,13 @@
   uint8_t ff_dur                     : 1;
 } lis2dw12_wake_up_dur_t;
 
-#define LIS2DW12_FREE_FALL                   0x36
+#define LIS2DW12_FREE_FALL                   0x36U
 typedef struct {
   uint8_t ff_ths                     : 3;
   uint8_t ff_dur                     : 5;
 } lis2dw12_free_fall_t;
 
-#define LIS2DW12_STATUS_DUP                  0x37
+#define LIS2DW12_STATUS_DUP                  0x37U
 typedef struct {
   uint8_t drdy                       : 1;
   uint8_t ff_ia                      : 1;
@@ -322,7 +355,7 @@
   uint8_t ovr                        : 1;
 } lis2dw12_status_dup_t;
 
-#define LIS2DW12_WAKE_UP_SRC                 0x38
+#define LIS2DW12_WAKE_UP_SRC                 0x38U
 typedef struct {
   uint8_t z_wu                       : 1;
   uint8_t y_wu                       : 1;
@@ -333,7 +366,7 @@
   uint8_t not_used_01                : 2;
 } lis2dw12_wake_up_src_t;
 
-#define LIS2DW12_TAP_SRC                     0x39
+#define LIS2DW12_TAP_SRC                     0x39U
 typedef struct {
   uint8_t z_tap                      : 1;
   uint8_t y_tap                      : 1;
@@ -345,7 +378,7 @@
   uint8_t not_used_01                : 1;
 } lis2dw12_tap_src_t;
 
-#define LIS2DW12_SIXD_SRC                    0x3A
+#define LIS2DW12_SIXD_SRC                    0x3AU
 typedef struct {
   uint8_t xl                         : 1;
   uint8_t xh                         : 1;
@@ -357,7 +390,7 @@
   uint8_t not_used_01                : 1;
 } lis2dw12_sixd_src_t;
 
-#define LIS2DW12_ALL_INT_SRC                 0x3B
+#define LIS2DW12_ALL_INT_SRC                 0x3BU
 typedef struct {
   uint8_t ff_ia                      : 1;
   uint8_t wu_ia                      : 1;
@@ -368,10 +401,10 @@
   uint8_t not_used_01                : 2;
 } lis2dw12_all_int_src_t;
 
-#define LIS2DW12_X_OFS_USR                   0x3C
-#define LIS2DW12_Y_OFS_USR                   0x3D
-#define LIS2DW12_Z_OFS_USR                   0x3E
-#define LIS2DW12_CTRL_REG7                   0x3F
+#define LIS2DW12_X_OFS_USR                   0x3CU
+#define LIS2DW12_Y_OFS_USR                   0x3DU
+#define LIS2DW12_Z_OFS_USR                   0x3EU
+#define LIS2DW12_CTRL_REG7                   0x3FU
 typedef struct {
   uint8_t lpass_on6d                 : 1;
   uint8_t hp_ref_mode                : 1;
@@ -383,6 +416,18 @@
   uint8_t drdy_pulsed                : 1;
 } lis2dw12_ctrl_reg7_t;
 
+/**
+  * @defgroup LIS2DW12_Register_Union
+  * @brief    This union group all the registers that has a bitfield
+  *           description.
+  *           This union is useful but not need by the driver.
+  *
+  *           REMOVING this union you are compliant with:
+  *           MISRA-C 2012 [Rule 19.2] -> " Union are not allowed "
+  *
+  * @{
+  *
+  */
 typedef union{
   lis2dw12_ctrl1_t                   ctrl1;
   lis2dw12_ctrl2_t                   ctrl2;
@@ -410,10 +455,25 @@
   uint8_t                            byte;
 } lis2dw12_reg_t;
 
+/**
+  * @}
+  *
+  */
+
 int32_t lis2dw12_read_reg(lis2dw12_ctx_t *ctx, uint8_t reg, uint8_t* data,
                           uint16_t len);
 int32_t lis2dw12_write_reg(lis2dw12_ctx_t *ctx, uint8_t reg, uint8_t* data,
                            uint16_t len);
+                           
+extern float lis2dw12_from_fs2_to_mg(int16_t lsb);
+extern float lis2dw12_from_fs4_to_mg(int16_t lsb);
+extern float lis2dw12_from_fs8_to_mg(int16_t lsb);
+extern float lis2dw12_from_fs16_to_mg(int16_t lsb);
+extern float lis2dw12_from_fs2_lp1_to_mg(int16_t lsb);
+extern float lis2dw12_from_fs4_lp1_to_mg(int16_t lsb);
+extern float lis2dw12_from_fs8_lp1_to_mg(int16_t lsb);
+extern float lis2dw12_from_fs16_lp1_to_mg(int16_t lsb);
+extern float lis2dw12_from_lsb_to_celsius(int16_t lsb);
 
 typedef enum {
   LIS2DW12_HIGH_PERFORMANCE                    = 0x04,
@@ -747,12 +807,13 @@
 
 /**
   * @}
-  */ 
+  *
+  */
 
 #ifdef __cplusplus
 }
 #endif
 
-#endif /*__LIS2DW12_DRIVER__H */
+#endif /*LIS2DW12_REGS_H */
 
 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/