3-axis MEMS ultra low power magnetometer

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Dependents:   X_NUCLEO_IKS01A3 X_NUCLEO_IKS01A3

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lis2mdl_reg.h Source File

lis2mdl_reg.h

00001 /*
00002  ******************************************************************************
00003  * @file    lis2mdl_reg.h
00004  * @author  Sensors Software Solution Team
00005  * @brief   This file contains all the functions prototypes for the
00006  *          lis2mdl_reg.c driver.
00007  ******************************************************************************
00008  * @attention
00009  *
00010  * <h2><center>&copy; COPYRIGHT(c) 2018 STMicroelectronics</center></h2>
00011  *
00012  * Redistribution and use in source and binary forms, with or without
00013  * modification, are permitted provided that the following conditions
00014  * are met:
00015  *   1. Redistributions of source code must retain the above copyright notice,
00016  *      this list of conditions and the following disclaimer.
00017  *   2. Redistributions in binary form must reproduce the above copyright
00018  *      notice, this list of conditions and the following disclaimer in the
00019  *      documentation and/or other materials provided with the distribution.
00020  *   3. Neither the name of STMicroelectronics nor the names of its
00021  *      contributors may be used to endorse or promote products derived from
00022  *      this software without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00027  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00028  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00029  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00030  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00031  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00032  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00033  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034  * POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  */
00037 /* Define to prevent recursive inclusion -------------------------------------*/
00038 #ifndef LIS2MDL_REGS_H
00039 #define LIS2MDL_REGS_H
00040 
00041 #ifdef __cplusplus
00042 extern "C" {
00043 #endif
00044 
00045 /* Includes ------------------------------------------------------------------*/
00046 #include <stdint.h>
00047 #include <math.h>
00048 
00049 
00050 #ifndef float_t
00051 #define float_t float
00052 #endif
00053 
00054 /** @addtogroup LIS2MDL
00055   * @{
00056   *
00057   */
00058 
00059 /** @defgroup LIS2MDL_sensors_common_types
00060   * @{
00061   *
00062   */
00063 
00064 #ifndef MEMS_SHARED_TYPES
00065 #define MEMS_SHARED_TYPES
00066 
00067 /**
00068   * @defgroup axisXbitXX_t
00069   * @brief    These unions are useful to represent different sensors data type.
00070   *           These unions are not need by the driver.
00071   *
00072   *           REMOVING the unions you are compliant with:
00073   *           MISRA-C 2012 [Rule 19.2] -> " Union are not allowed "
00074   *
00075   * @{
00076   *
00077   */
00078 
00079 typedef union {
00080     int16_t i16bit[3];
00081     uint8_t u8bit[6];
00082 } axis3bit16_t;
00083 
00084 typedef union {
00085     int16_t i16bit;
00086     uint8_t u8bit[2];
00087 } axis1bit16_t;
00088 
00089 typedef union {
00090     int32_t i32bit[3];
00091     uint8_t u8bit[12];
00092 } axis3bit32_t;
00093 
00094 typedef union {
00095     int32_t i32bit;
00096     uint8_t u8bit[4];
00097 } axis1bit32_t;
00098 
00099 /**
00100   * @}
00101   *
00102   */
00103 
00104 typedef struct {
00105     uint8_t bit0       : 1;
00106     uint8_t bit1       : 1;
00107     uint8_t bit2       : 1;
00108     uint8_t bit3       : 1;
00109     uint8_t bit4       : 1;
00110     uint8_t bit5       : 1;
00111     uint8_t bit6       : 1;
00112     uint8_t bit7       : 1;
00113 } bitwise_t;
00114 
00115 #define PROPERTY_DISABLE                (0U)
00116 #define PROPERTY_ENABLE                 (1U)
00117 
00118 #endif /* MEMS_SHARED_TYPES */
00119 
00120 /**
00121   * @}
00122   *
00123   */
00124 
00125 /** @addtogroup  LIS2MDL_Interfaces_Functions
00126   * @brief       This section provide a set of functions used to read and
00127   *              write a generic register of the device.
00128   *              MANDATORY: return 0 -> no Error.
00129   * @{
00130   *
00131   */
00132 
00133 typedef int32_t (*lis2mdl_write_ptr)(void *, uint8_t, uint8_t *, uint16_t);
00134 typedef int32_t (*lis2mdl_read_ptr)(void *, uint8_t, uint8_t *, uint16_t);
00135 
00136 typedef struct {
00137     /** Component mandatory fields **/
00138     lis2mdl_write_ptr  write_reg;
00139     lis2mdl_read_ptr   read_reg;
00140     /** Customizable optional pointer **/
00141     void *handle;
00142 } lis2mdl_ctx_t;
00143 
00144 /**
00145   * @}
00146   *
00147   */
00148 
00149 /** @defgroup LSM9DS1_Infos
00150   * @{
00151   *
00152   */
00153 
00154 /** I2C Device Address 8 bit format **/
00155 #define LIS2MDL_I2C_ADD                 0x3DU
00156 
00157 /** Device Identification (Who am I) **/
00158 #define LIS2MDL_ID                      0x40U
00159 
00160 /**
00161   * @}
00162   *
00163   */
00164 
00165 #define LIS2MDL_OFFSET_X_REG_L          0x45U
00166 #define LIS2MDL_OFFSET_X_REG_H          0x46U
00167 #define LIS2MDL_OFFSET_Y_REG_L          0x47U
00168 #define LIS2MDL_OFFSET_Y_REG_H          0x48U
00169 #define LIS2MDL_OFFSET_Z_REG_L          0x49U
00170 #define LIS2MDL_OFFSET_Z_REG_H          0x4AU
00171 #define LIS2MDL_WHO_AM_I                0x4FU
00172 #define LIS2MDL_CFG_REG_A               0x60U
00173 typedef struct {
00174     uint8_t md                     : 2;
00175     uint8_t odr                    : 2;
00176     uint8_t lp                     : 1;
00177     uint8_t soft_rst               : 1;
00178     uint8_t reboot                 : 1;
00179     uint8_t comp_temp_en           : 1;
00180 } lis2mdl_cfg_reg_a_t;
00181 
00182 #define LIS2MDL_CFG_REG_B               0x61U
00183 typedef struct {
00184     uint8_t lpf                    : 1;
00185     uint8_t set_rst                : 2; /* OFF_CANC + Set_FREQ */
00186     uint8_t int_on_dataoff         : 1;
00187     uint8_t off_canc_one_shot      : 1;
00188     uint8_t not_used_01            : 3;
00189 } lis2mdl_cfg_reg_b_t;
00190 
00191 #define LIS2MDL_CFG_REG_C               0x62U
00192 typedef struct {
00193     uint8_t drdy_on_pin            : 1;
00194     uint8_t self_test              : 1;
00195     uint8_t not_used_01            : 1;
00196     uint8_t ble                    : 1;
00197     uint8_t bdu                    : 1;
00198     uint8_t i2c_dis                : 1;
00199     uint8_t int_on_pin             : 1;
00200     uint8_t not_used_02            : 1;
00201 } lis2mdl_cfg_reg_c_t;
00202 
00203 #define LIS2MDL_INT_CRTL_REG            0x63U
00204 typedef struct {
00205     uint8_t ien                    : 1;
00206     uint8_t iel                    : 1;
00207     uint8_t iea                    : 1;
00208     uint8_t not_used_01            : 2;
00209     uint8_t zien                   : 1;
00210     uint8_t yien                   : 1;
00211     uint8_t xien                   : 1;
00212 } lis2mdl_int_crtl_reg_t;
00213 
00214 #define LIS2MDL_INT_SOURCE_REG          0x64U
00215 typedef struct {
00216     uint8_t _int                    : 1;
00217     uint8_t mroi                   : 1;
00218     uint8_t n_th_s_z               : 1;
00219     uint8_t n_th_s_y               : 1;
00220     uint8_t n_th_s_x               : 1;
00221     uint8_t p_th_s_z               : 1;
00222     uint8_t p_th_s_y               : 1;
00223     uint8_t p_th_s_x               : 1;
00224 } lis2mdl_int_source_reg_t;
00225 
00226 #define LIS2MDL_INT_THS_L_REG           0x65U
00227 #define LIS2MDL_INT_THS_H_REG           0x66U
00228 #define LIS2MDL_STATUS_REG              0x67U
00229 typedef struct {
00230     uint8_t xda                    : 1;
00231     uint8_t yda                    : 1;
00232     uint8_t zda                    : 1;
00233     uint8_t zyxda                  : 1;
00234     uint8_t xior                   : 1;  //changed as xor is a reserved word in c++
00235     uint8_t yor                    : 1;
00236     uint8_t zor                    : 1;
00237     uint8_t zyxor                  : 1;
00238 } lis2mdl_status_reg_t;
00239 
00240 #define LIS2MDL_OUTX_L_REG              0x68U
00241 #define LIS2MDL_OUTX_H_REG              0x69U
00242 #define LIS2MDL_OUTY_L_REG              0x6AU
00243 #define LIS2MDL_OUTY_H_REG              0x6BU
00244 #define LIS2MDL_OUTZ_L_REG              0x6CU
00245 #define LIS2MDL_OUTZ_H_REG              0x6DU
00246 #define LIS2MDL_TEMP_OUT_L_REG          0x6EU
00247 #define LIS2MDL_TEMP_OUT_H_REG          0x6FU
00248 
00249 /**
00250   * @defgroup LIS2MDL_Register_Union
00251   * @brief    This union group all the registers that has a bit-field
00252   *           description.
00253   *           This union is useful but not need by the driver.
00254   *
00255   *           REMOVING this union you are compliant with:
00256   *           MISRA-C 2012 [Rule 19.2] -> " Union are not allowed "
00257   *
00258   * @{
00259   *
00260   */
00261 typedef union {
00262     lis2mdl_cfg_reg_a_t            cfg_reg_a;
00263     lis2mdl_cfg_reg_b_t            cfg_reg_b;
00264     lis2mdl_cfg_reg_c_t            cfg_reg_c;
00265     lis2mdl_int_crtl_reg_t         int_crtl_reg;
00266     lis2mdl_int_source_reg_t       int_source_reg;
00267     lis2mdl_status_reg_t           status_reg;
00268     bitwise_t                      bitwise;
00269     uint8_t                        byte;
00270 } lis2mdl_reg_t;
00271 
00272 /**
00273   * @}
00274   *
00275   */
00276 
00277 int32_t lis2mdl_read_reg(lis2mdl_ctx_t *ctx, uint8_t reg, uint8_t *data,
00278                          uint16_t len);
00279 int32_t lis2mdl_write_reg(lis2mdl_ctx_t *ctx, uint8_t reg, uint8_t *data,
00280                           uint16_t len);
00281 
00282 extern float_t lis2mdl_from_lsb_to_mgauss(int16_t lsb);
00283 extern float_t lis2mdl_from_lsb_to_celsius(int16_t lsb);
00284 
00285 int32_t lis2mdl_mag_user_offset_set(lis2mdl_ctx_t *ctx, uint8_t *buff);
00286 int32_t lis2mdl_mag_user_offset_get(lis2mdl_ctx_t *ctx, uint8_t *buff);
00287 
00288 typedef enum {
00289     LIS2MDL_CONTINUOUS_MODE  = 0,
00290     LIS2MDL_SINGLE_TRIGGER   = 1,
00291     LIS2MDL_POWER_DOWN       = 2,
00292 } lis2mdl_md_t;
00293 int32_t lis2mdl_operating_mode_set(lis2mdl_ctx_t *ctx, lis2mdl_md_t val);
00294 int32_t lis2mdl_operating_mode_get(lis2mdl_ctx_t *ctx, lis2mdl_md_t *val);
00295 
00296 typedef enum {
00297     LIS2MDL_ODR_10Hz   = 0,
00298     LIS2MDL_ODR_20Hz   = 1,
00299     LIS2MDL_ODR_50Hz   = 2,
00300     LIS2MDL_ODR_100Hz  = 3,
00301 } lis2mdl_odr_t;
00302 int32_t lis2mdl_data_rate_set(lis2mdl_ctx_t *ctx, lis2mdl_odr_t val);
00303 int32_t lis2mdl_data_rate_get(lis2mdl_ctx_t *ctx, lis2mdl_odr_t *val);
00304 
00305 typedef enum {
00306     LIS2MDL_HIGH_RESOLUTION  = 0,
00307     LIS2MDL_LOW_POWER        = 1,
00308 } lis2mdl_lp_t;
00309 int32_t lis2mdl_power_mode_set(lis2mdl_ctx_t *ctx, lis2mdl_lp_t val);
00310 int32_t lis2mdl_power_mode_get(lis2mdl_ctx_t *ctx, lis2mdl_lp_t *val);
00311 
00312 int32_t lis2mdl_offset_temp_comp_set(lis2mdl_ctx_t *ctx, uint8_t val);
00313 int32_t lis2mdl_offset_temp_comp_get(lis2mdl_ctx_t *ctx, uint8_t *val);
00314 
00315 typedef enum {
00316     LIS2MDL_ODR_DIV_2  = 0,
00317     LIS2MDL_ODR_DIV_4  = 1,
00318 } lis2mdl_lpf_t;
00319 int32_t lis2mdl_low_pass_bandwidth_set(lis2mdl_ctx_t *ctx,
00320                                        lis2mdl_lpf_t val);
00321 int32_t lis2mdl_low_pass_bandwidth_get(lis2mdl_ctx_t *ctx,
00322                                        lis2mdl_lpf_t *val);
00323 
00324 typedef enum {
00325     LIS2MDL_SET_SENS_ODR_DIV_63        = 0,
00326     LIS2MDL_SENS_OFF_CANC_EVERY_ODR    = 1,
00327     LIS2MDL_SET_SENS_ONLY_AT_POWER_ON  = 2,
00328 } lis2mdl_set_rst_t;
00329 int32_t lis2mdl_set_rst_mode_set(lis2mdl_ctx_t *ctx,
00330                                  lis2mdl_set_rst_t val);
00331 int32_t lis2mdl_set_rst_mode_get(lis2mdl_ctx_t *ctx,
00332                                  lis2mdl_set_rst_t *val);
00333 
00334 int32_t lis2mdl_set_rst_sensor_single_set(lis2mdl_ctx_t *ctx,
00335                                           uint8_t val);
00336 int32_t lis2mdl_set_rst_sensor_single_get(lis2mdl_ctx_t *ctx,
00337                                           uint8_t *val);
00338 
00339 int32_t lis2mdl_block_data_update_set(lis2mdl_ctx_t *ctx, uint8_t val);
00340 int32_t lis2mdl_block_data_update_get(lis2mdl_ctx_t *ctx, uint8_t *val);
00341 
00342 int32_t lis2mdl_mag_data_ready_get(lis2mdl_ctx_t *ctx, uint8_t *val);
00343 
00344 int32_t lis2mdl_mag_data_ovr_get(lis2mdl_ctx_t *ctx, uint8_t *val);
00345 
00346 int32_t lis2mdl_magnetic_raw_get(lis2mdl_ctx_t *ctx, uint8_t *buff);
00347 
00348 int32_t lis2mdl_temperature_raw_get(lis2mdl_ctx_t *ctx, uint8_t *buff);
00349 
00350 int32_t lis2mdl_device_id_get(lis2mdl_ctx_t *ctx, uint8_t *buff);
00351 
00352 int32_t lis2mdl_reset_set(lis2mdl_ctx_t *ctx, uint8_t val);
00353 int32_t lis2mdl_reset_get(lis2mdl_ctx_t *ctx, uint8_t *val);
00354 
00355 int32_t lis2mdl_boot_set(lis2mdl_ctx_t *ctx, uint8_t val);
00356 int32_t lis2mdl_boot_get(lis2mdl_ctx_t *ctx, uint8_t *val);
00357 
00358 int32_t lis2mdl_self_test_set(lis2mdl_ctx_t *ctx, uint8_t val);
00359 int32_t lis2mdl_self_test_get(lis2mdl_ctx_t *ctx, uint8_t *val);
00360 
00361 typedef enum {
00362     LIS2MDL_LSB_AT_LOW_ADD  = 0,
00363     LIS2MDL_MSB_AT_LOW_ADD  = 1,
00364 } lis2mdl_ble_t;
00365 int32_t lis2mdl_data_format_set(lis2mdl_ctx_t *ctx, lis2mdl_ble_t val);
00366 int32_t lis2mdl_data_format_get(lis2mdl_ctx_t *ctx, lis2mdl_ble_t *val);
00367 
00368 int32_t lis2mdl_status_get(lis2mdl_ctx_t *ctx, lis2mdl_status_reg_t *val);
00369 
00370 typedef enum {
00371     LIS2MDL_CHECK_BEFORE  = 0,
00372     LIS2MDL_CHECK_AFTER   = 1,
00373 } lis2mdl_int_on_dataoff_t;
00374 int32_t lis2mdl_offset_int_conf_set(lis2mdl_ctx_t *ctx,
00375                                     lis2mdl_int_on_dataoff_t val);
00376 int32_t lis2mdl_offset_int_conf_get(lis2mdl_ctx_t *ctx,
00377                                     lis2mdl_int_on_dataoff_t *val);
00378 
00379 int32_t lis2mdl_drdy_on_pin_set(lis2mdl_ctx_t *ctx, uint8_t val);
00380 int32_t lis2mdl_drdy_on_pin_get(lis2mdl_ctx_t *ctx, uint8_t *val);
00381 
00382 int32_t lis2mdl_int_on_pin_set(lis2mdl_ctx_t *ctx, uint8_t val);
00383 int32_t lis2mdl_int_on_pin_get(lis2mdl_ctx_t *ctx, uint8_t *val);
00384 
00385 int32_t lis2mdl_int_gen_conf_set(lis2mdl_ctx_t *ctx,
00386                                  lis2mdl_int_crtl_reg_t *val);
00387 int32_t lis2mdl_int_gen_conf_get(lis2mdl_ctx_t *ctx,
00388                                  lis2mdl_int_crtl_reg_t *val);
00389 
00390 int32_t lis2mdl_int_gen_source_get(lis2mdl_ctx_t *ctx,
00391                                    lis2mdl_int_source_reg_t *val);
00392 
00393 int32_t lis2mdl_int_gen_treshold_set(lis2mdl_ctx_t *ctx, uint8_t *buff);
00394 int32_t lis2mdl_int_gen_treshold_get(lis2mdl_ctx_t *ctx, uint8_t *buff);
00395 
00396 typedef enum {
00397     LIS2MDL_I2C_ENABLE   = 0,
00398     LIS2MDL_I2C_DISABLE  = 1,
00399 } lis2mdl_i2c_dis_t;
00400 int32_t lis2mdl_i2c_interface_set(lis2mdl_ctx_t *ctx,
00401                                   lis2mdl_i2c_dis_t val);
00402 int32_t lis2mdl_i2c_interface_get(lis2mdl_ctx_t *ctx,
00403                                   lis2mdl_i2c_dis_t *val);
00404 
00405 /**
00406   *@}
00407   *
00408   */
00409 
00410 #ifdef __cplusplus
00411 }
00412 #endif
00413 
00414 #endif /* LIS2MDL_REGS_H */
00415 
00416 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/