3-axis MEMS ultra low power magnetometer

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Dependents:   X_NUCLEO_IKS01A3

Committer:
cparata
Date:
Tue Mar 05 18:06:37 2019 +0000
Revision:
0:671edf39d961
Child:
1:8562ae1a0534
First version of LIS2MDL library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cparata 0:671edf39d961 1 /**
cparata 0:671edf39d961 2 ******************************************************************************
cparata 0:671edf39d961 3 * @file LIS2MDLSensor.cpp
cparata 0:671edf39d961 4 * @author SRA
cparata 0:671edf39d961 5 * @version V1.0.0
cparata 0:671edf39d961 6 * @date February 2019
cparata 0:671edf39d961 7 * @brief Implementation of an LIS2MDL 3 axes magnetometer sensor.
cparata 0:671edf39d961 8 ******************************************************************************
cparata 0:671edf39d961 9 * @attention
cparata 0:671edf39d961 10 *
cparata 0:671edf39d961 11 * <h2><center>&copy; COPYRIGHT(c) 2019 STMicroelectronics</center></h2>
cparata 0:671edf39d961 12 *
cparata 0:671edf39d961 13 * Redistribution and use in source and binary forms, with or without modification,
cparata 0:671edf39d961 14 * are permitted provided that the following conditions are met:
cparata 0:671edf39d961 15 * 1. Redistributions of source code must retain the above copyright notice,
cparata 0:671edf39d961 16 * this list of conditions and the following disclaimer.
cparata 0:671edf39d961 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
cparata 0:671edf39d961 18 * this list of conditions and the following disclaimer in the documentation
cparata 0:671edf39d961 19 * and/or other materials provided with the distribution.
cparata 0:671edf39d961 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
cparata 0:671edf39d961 21 * may be used to endorse or promote products derived from this software
cparata 0:671edf39d961 22 * without specific prior written permission.
cparata 0:671edf39d961 23 *
cparata 0:671edf39d961 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
cparata 0:671edf39d961 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
cparata 0:671edf39d961 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
cparata 0:671edf39d961 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
cparata 0:671edf39d961 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
cparata 0:671edf39d961 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
cparata 0:671edf39d961 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
cparata 0:671edf39d961 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
cparata 0:671edf39d961 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
cparata 0:671edf39d961 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
cparata 0:671edf39d961 34 *
cparata 0:671edf39d961 35 ******************************************************************************
cparata 0:671edf39d961 36 */
cparata 0:671edf39d961 37
cparata 0:671edf39d961 38
cparata 0:671edf39d961 39 /* Includes ------------------------------------------------------------------*/
cparata 0:671edf39d961 40
cparata 0:671edf39d961 41 #include "LIS2MDLSensor.h"
cparata 0:671edf39d961 42
cparata 0:671edf39d961 43
cparata 0:671edf39d961 44 /* Class Implementation ------------------------------------------------------*/
cparata 0:671edf39d961 45
cparata 0:671edf39d961 46 /** Constructor
cparata 0:671edf39d961 47 * @param spi object of an helper class which handles the SPI peripheral
cparata 0:671edf39d961 48 * @param cs_pin the chip select pin
cparata 0:671edf39d961 49 * @param int_pin the interrupt pin
cparata 0:671edf39d961 50 * @param spi_type the SPI type
cparata 0:671edf39d961 51 */
cparata 0:671edf39d961 52 LIS2MDLSensor::LIS2MDLSensor(SPI *spi, PinName cs_pin, PinName int_pin, SPI_type_t spi_type) : _dev_spi(spi), _cs_pin(cs_pin), _int_irq(int_pin), _spi_type(spi_type)
cparata 0:671edf39d961 53 {
cparata 0:671edf39d961 54 assert (spi);
cparata 0:671edf39d961 55 if (cs_pin == NC)
cparata 0:671edf39d961 56 {
cparata 0:671edf39d961 57 printf ("ERROR LIS2MDL CS MUST NOT BE NC\n\r");
cparata 0:671edf39d961 58 _dev_spi = NULL;
cparata 0:671edf39d961 59 _dev_i2c = NULL;
cparata 0:671edf39d961 60 return;
cparata 0:671edf39d961 61 }
cparata 0:671edf39d961 62
cparata 0:671edf39d961 63 _reg_ctx.write_reg = LIS2MDL_io_write;
cparata 0:671edf39d961 64 _reg_ctx.read_reg = LIS2MDL_io_read;
cparata 0:671edf39d961 65 _reg_ctx.handle = (void *)this;
cparata 0:671edf39d961 66 _cs_pin = 1;
cparata 0:671edf39d961 67 _dev_i2c = NULL;
cparata 0:671edf39d961 68 _address = 0;
cparata 0:671edf39d961 69
cparata 0:671edf39d961 70 if (_spi_type == SPI4W)
cparata 0:671edf39d961 71 {
cparata 0:671edf39d961 72 /* Enable SPI 4-Wires on the component (in this way we lose the usage of INT pin) */
cparata 0:671edf39d961 73 uint8_t data = 0x34;
cparata 0:671edf39d961 74 lis2mdl_write_reg(&_reg_ctx, LIS2MDL_CFG_REG_C, &data, 1);
cparata 0:671edf39d961 75 }
cparata 0:671edf39d961 76 }
cparata 0:671edf39d961 77
cparata 0:671edf39d961 78 /** Constructor
cparata 0:671edf39d961 79 * @param i2c object of an helper class which handles the I2C peripheral
cparata 0:671edf39d961 80 * @param address the address of the component's instance
cparata 0:671edf39d961 81 * @param int_pin the interrupt pin
cparata 0:671edf39d961 82 */
cparata 0:671edf39d961 83 LIS2MDLSensor::LIS2MDLSensor(DevI2C *i2c, uint8_t address, PinName int_pin) : _dev_i2c(i2c), _address(address), _cs_pin(NC), _int_irq(int_pin)
cparata 0:671edf39d961 84 {
cparata 0:671edf39d961 85 assert (i2c);
cparata 0:671edf39d961 86 _dev_spi = NULL;
cparata 0:671edf39d961 87 _reg_ctx.write_reg = LIS2MDL_io_write;
cparata 0:671edf39d961 88 _reg_ctx.read_reg = LIS2MDL_io_read;
cparata 0:671edf39d961 89 _reg_ctx.handle = (void *)this;
cparata 0:671edf39d961 90 }
cparata 0:671edf39d961 91
cparata 0:671edf39d961 92 /**
cparata 0:671edf39d961 93 * @brief Initializing the component
cparata 0:671edf39d961 94 * @param init pointer to device specific initalization structure
cparata 0:671edf39d961 95 * @retval 0 in case of success, an error code otherwise
cparata 0:671edf39d961 96 */
cparata 0:671edf39d961 97 int LIS2MDLSensor::init(void *init)
cparata 0:671edf39d961 98 {
cparata 0:671edf39d961 99 /* Enable BDU */
cparata 0:671edf39d961 100 if (lis2mdl_block_data_update_set(&(_reg_ctx), PROPERTY_ENABLE) != 0)
cparata 0:671edf39d961 101 {
cparata 0:671edf39d961 102 return 1;
cparata 0:671edf39d961 103 }
cparata 0:671edf39d961 104
cparata 0:671edf39d961 105 /* Operating mode selection - power down */
cparata 0:671edf39d961 106 if (lis2mdl_operating_mode_set(&(_reg_ctx), LIS2MDL_POWER_DOWN) != 0)
cparata 0:671edf39d961 107 {
cparata 0:671edf39d961 108 return 1;
cparata 0:671edf39d961 109 }
cparata 0:671edf39d961 110
cparata 0:671edf39d961 111 /* Output data rate selection */
cparata 0:671edf39d961 112 if (lis2mdl_data_rate_set(&(_reg_ctx), LIS2MDL_ODR_100Hz) != 0)
cparata 0:671edf39d961 113 {
cparata 0:671edf39d961 114 return 1;
cparata 0:671edf39d961 115 }
cparata 0:671edf39d961 116
cparata 0:671edf39d961 117 /* Self Test disabled. */
cparata 0:671edf39d961 118 if (lis2mdl_self_test_set(&(_reg_ctx), PROPERTY_DISABLE) != 0)
cparata 0:671edf39d961 119 {
cparata 0:671edf39d961 120 return 1;
cparata 0:671edf39d961 121 }
cparata 0:671edf39d961 122
cparata 0:671edf39d961 123 _mag_is_enabled = 0;
cparata 0:671edf39d961 124
cparata 0:671edf39d961 125 return 0;
cparata 0:671edf39d961 126 }
cparata 0:671edf39d961 127
cparata 0:671edf39d961 128 /**
cparata 0:671edf39d961 129 * @brief Read component ID
cparata 0:671edf39d961 130 * @param id the WHO_AM_I value
cparata 0:671edf39d961 131 * @retval 0 in case of success, an error code otherwise
cparata 0:671edf39d961 132 */
cparata 0:671edf39d961 133 int LIS2MDLSensor::read_id(uint8_t *id)
cparata 0:671edf39d961 134 {
cparata 0:671edf39d961 135 if (lis2mdl_device_id_get(&_reg_ctx, id) != 0)
cparata 0:671edf39d961 136 {
cparata 0:671edf39d961 137 return 1;
cparata 0:671edf39d961 138 }
cparata 0:671edf39d961 139
cparata 0:671edf39d961 140 return 0;
cparata 0:671edf39d961 141 }
cparata 0:671edf39d961 142
cparata 0:671edf39d961 143
cparata 0:671edf39d961 144 /**
cparata 0:671edf39d961 145 * @brief Enable the LIS2MDL magnetometer sensor
cparata 0:671edf39d961 146 * @retval 0 in case of success, an error code otherwise
cparata 0:671edf39d961 147 */
cparata 0:671edf39d961 148 int LIS2MDLSensor::enable()
cparata 0:671edf39d961 149 {
cparata 0:671edf39d961 150 /* Check if the component is already enabled */
cparata 0:671edf39d961 151 if (_mag_is_enabled == 1U)
cparata 0:671edf39d961 152 {
cparata 0:671edf39d961 153 return 0;
cparata 0:671edf39d961 154 }
cparata 0:671edf39d961 155
cparata 0:671edf39d961 156 /* Output data rate selection. */
cparata 0:671edf39d961 157 if (lis2mdl_operating_mode_set(&_reg_ctx, LIS2MDL_CONTINUOUS_MODE) != 0)
cparata 0:671edf39d961 158 {
cparata 0:671edf39d961 159 return 1;
cparata 0:671edf39d961 160 }
cparata 0:671edf39d961 161
cparata 0:671edf39d961 162 _mag_is_enabled = 1;
cparata 0:671edf39d961 163
cparata 0:671edf39d961 164 return 0;
cparata 0:671edf39d961 165 }
cparata 0:671edf39d961 166
cparata 0:671edf39d961 167 /**
cparata 0:671edf39d961 168 * @brief Disable the LIS2MDL magnetometer sensor
cparata 0:671edf39d961 169 * @retval 0 in case of success, an error code otherwise
cparata 0:671edf39d961 170 */
cparata 0:671edf39d961 171 int LIS2MDLSensor::disable()
cparata 0:671edf39d961 172 {
cparata 0:671edf39d961 173 /* Check if the component is already disabled */
cparata 0:671edf39d961 174 if (_mag_is_enabled == 0U)
cparata 0:671edf39d961 175 {
cparata 0:671edf39d961 176 return 0;
cparata 0:671edf39d961 177 }
cparata 0:671edf39d961 178
cparata 0:671edf39d961 179 /* Output data rate selection - power down. */
cparata 0:671edf39d961 180 if (lis2mdl_operating_mode_set(&_reg_ctx, LIS2MDL_POWER_DOWN) != 0)
cparata 0:671edf39d961 181 {
cparata 0:671edf39d961 182 return 1;
cparata 0:671edf39d961 183 }
cparata 0:671edf39d961 184
cparata 0:671edf39d961 185 _mag_is_enabled = 0;
cparata 0:671edf39d961 186
cparata 0:671edf39d961 187 return 0;
cparata 0:671edf39d961 188 }
cparata 0:671edf39d961 189
cparata 0:671edf39d961 190 /**
cparata 0:671edf39d961 191 * @brief Get the LIS2MDL magnetometer sensor sensitivity
cparata 0:671edf39d961 192 * @param sensitivity pointer where the sensitivity is written
cparata 0:671edf39d961 193 * @retval 0 in case of success, an error code otherwise
cparata 0:671edf39d961 194 */
cparata 0:671edf39d961 195 int LIS2MDLSensor::get_m_sensitivity(float *sensitivity)
cparata 0:671edf39d961 196 {
cparata 0:671edf39d961 197 *sensitivity = LIS2MDL_MAG_SENSITIVITY_FS_50GAUSS;
cparata 0:671edf39d961 198
cparata 0:671edf39d961 199 return 0;
cparata 0:671edf39d961 200 }
cparata 0:671edf39d961 201
cparata 0:671edf39d961 202 /**
cparata 0:671edf39d961 203 * @brief Get the LIS2MDL magnetometer sensor output data rate
cparata 0:671edf39d961 204 * @param odr pointer where the output data rate is written
cparata 0:671edf39d961 205 * @retval 0 in case of success, an error code otherwise
cparata 0:671edf39d961 206 */
cparata 0:671edf39d961 207 int LIS2MDLSensor::get_m_odr(float *odr)
cparata 0:671edf39d961 208 {
cparata 0:671edf39d961 209 int ret = 0;
cparata 0:671edf39d961 210 lis2mdl_odr_t odr_low_level;
cparata 0:671edf39d961 211
cparata 0:671edf39d961 212 /* Get current output data rate. */
cparata 0:671edf39d961 213 if (lis2mdl_data_rate_get(&_reg_ctx, &odr_low_level) != 0)
cparata 0:671edf39d961 214 {
cparata 0:671edf39d961 215 return 1;
cparata 0:671edf39d961 216 }
cparata 0:671edf39d961 217
cparata 0:671edf39d961 218 switch (odr_low_level)
cparata 0:671edf39d961 219 {
cparata 0:671edf39d961 220 case LIS2MDL_ODR_10Hz:
cparata 0:671edf39d961 221 *odr = 10.0f;
cparata 0:671edf39d961 222 break;
cparata 0:671edf39d961 223
cparata 0:671edf39d961 224 case LIS2MDL_ODR_20Hz:
cparata 0:671edf39d961 225 *odr = 20.0f;
cparata 0:671edf39d961 226 break;
cparata 0:671edf39d961 227
cparata 0:671edf39d961 228 case LIS2MDL_ODR_50Hz:
cparata 0:671edf39d961 229 *odr = 50.0f;
cparata 0:671edf39d961 230 break;
cparata 0:671edf39d961 231
cparata 0:671edf39d961 232 case LIS2MDL_ODR_100Hz:
cparata 0:671edf39d961 233 *odr = 100.0f;
cparata 0:671edf39d961 234 break;
cparata 0:671edf39d961 235
cparata 0:671edf39d961 236 default:
cparata 0:671edf39d961 237 ret = 1;
cparata 0:671edf39d961 238 break;
cparata 0:671edf39d961 239 }
cparata 0:671edf39d961 240
cparata 0:671edf39d961 241 return ret;
cparata 0:671edf39d961 242 }
cparata 0:671edf39d961 243
cparata 0:671edf39d961 244 /**
cparata 0:671edf39d961 245 * @brief Set the LIS2MDL magnetometer sensor output data rate
cparata 0:671edf39d961 246 * @param odr the output data rate value to be set
cparata 0:671edf39d961 247 * @retval 0 in case of success, an error code otherwise
cparata 0:671edf39d961 248 */
cparata 0:671edf39d961 249 int LIS2MDLSensor::set_m_odr(float odr)
cparata 0:671edf39d961 250 {
cparata 0:671edf39d961 251 lis2mdl_odr_t new_odr;
cparata 0:671edf39d961 252
cparata 0:671edf39d961 253 new_odr = (odr <= 10.000f) ? LIS2MDL_ODR_10Hz
cparata 0:671edf39d961 254 : (odr <= 20.000f) ? LIS2MDL_ODR_20Hz
cparata 0:671edf39d961 255 : (odr <= 50.000f) ? LIS2MDL_ODR_50Hz
cparata 0:671edf39d961 256 : LIS2MDL_ODR_100Hz;
cparata 0:671edf39d961 257
cparata 0:671edf39d961 258 if (lis2mdl_data_rate_set(&_reg_ctx, new_odr) != 0)
cparata 0:671edf39d961 259 {
cparata 0:671edf39d961 260 return 1;
cparata 0:671edf39d961 261 }
cparata 0:671edf39d961 262
cparata 0:671edf39d961 263 return 0;
cparata 0:671edf39d961 264 }
cparata 0:671edf39d961 265
cparata 0:671edf39d961 266 /**
cparata 0:671edf39d961 267 * @brief Get the LIS2MDL magnetometer sensor full scale
cparata 0:671edf39d961 268 * @param full_scale pointer where the full scale is written
cparata 0:671edf39d961 269 * @retval 0 in case of success, an error code otherwise
cparata 0:671edf39d961 270 */
cparata 0:671edf39d961 271 int LIS2MDLSensor::get_m_fs(float *full_scale)
cparata 0:671edf39d961 272 {
cparata 0:671edf39d961 273 *full_scale = 50.0f;
cparata 0:671edf39d961 274
cparata 0:671edf39d961 275 return 0;
cparata 0:671edf39d961 276 }
cparata 0:671edf39d961 277
cparata 0:671edf39d961 278 /**
cparata 0:671edf39d961 279 * @brief Set the LIS2MDL magnetometer sensor full scale
cparata 0:671edf39d961 280 * @param full_scale the functional full scale to be set
cparata 0:671edf39d961 281 * @retval 0 in case of success, an error code otherwise
cparata 0:671edf39d961 282 */
cparata 0:671edf39d961 283 int LIS2MDLSensor::set_m_fs(float full_scale)
cparata 0:671edf39d961 284 {
cparata 0:671edf39d961 285 (void)full_scale;
cparata 0:671edf39d961 286 return 0;
cparata 0:671edf39d961 287 }
cparata 0:671edf39d961 288
cparata 0:671edf39d961 289 /**
cparata 0:671edf39d961 290 * @brief Get the LIS2MDL magnetometer sensor axes
cparata 0:671edf39d961 291 * @param magnetic_field pointer where the values of the axes are written
cparata 0:671edf39d961 292 * @retval 0 in case of success, an error code otherwise
cparata 0:671edf39d961 293 */
cparata 0:671edf39d961 294 int LIS2MDLSensor::get_m_axes(int32_t *magnetic_field)
cparata 0:671edf39d961 295 {
cparata 0:671edf39d961 296 axis3bit16_t data_raw;
cparata 0:671edf39d961 297 float sensitivity;
cparata 0:671edf39d961 298
cparata 0:671edf39d961 299 /* Read raw data values. */
cparata 0:671edf39d961 300 if (lis2mdl_magnetic_raw_get(&_reg_ctx, data_raw.u8bit) != 0)
cparata 0:671edf39d961 301 {
cparata 0:671edf39d961 302 return 1;
cparata 0:671edf39d961 303 }
cparata 0:671edf39d961 304
cparata 0:671edf39d961 305 /* Get LIS2MDL actual sensitivity. */
cparata 0:671edf39d961 306 if (get_m_sensitivity(&sensitivity) != 0)
cparata 0:671edf39d961 307 {
cparata 0:671edf39d961 308 return 1;
cparata 0:671edf39d961 309 }
cparata 0:671edf39d961 310
cparata 0:671edf39d961 311 /* Calculate the data. */
cparata 0:671edf39d961 312 magnetic_field[0] = (int32_t)((float)((float)data_raw.i16bit[0] * sensitivity));
cparata 0:671edf39d961 313 magnetic_field[1] = (int32_t)((float)((float)data_raw.i16bit[1] * sensitivity));
cparata 0:671edf39d961 314 magnetic_field[2] = (int32_t)((float)((float)data_raw.i16bit[2] * sensitivity));
cparata 0:671edf39d961 315
cparata 0:671edf39d961 316 return 0;
cparata 0:671edf39d961 317 }
cparata 0:671edf39d961 318
cparata 0:671edf39d961 319 /**
cparata 0:671edf39d961 320 * @brief Get the LIS2MDL magnetometer sensor raw axes
cparata 0:671edf39d961 321 * @param value pointer where the raw values of the axes are written
cparata 0:671edf39d961 322 * @retval 0 in case of success, an error code otherwise
cparata 0:671edf39d961 323 */
cparata 0:671edf39d961 324 int LIS2MDLSensor::get_m_axes_raw(int16_t *value)
cparata 0:671edf39d961 325 {
cparata 0:671edf39d961 326 axis3bit16_t data_raw;
cparata 0:671edf39d961 327
cparata 0:671edf39d961 328 /* Read raw data values. */
cparata 0:671edf39d961 329 if (lis2mdl_magnetic_raw_get(&_reg_ctx, data_raw.u8bit) != 0)
cparata 0:671edf39d961 330 {
cparata 0:671edf39d961 331 return 1;
cparata 0:671edf39d961 332 }
cparata 0:671edf39d961 333
cparata 0:671edf39d961 334 /* Format the data. */
cparata 0:671edf39d961 335 value[0] = data_raw.i16bit[0];
cparata 0:671edf39d961 336 value[1] = data_raw.i16bit[1];
cparata 0:671edf39d961 337 value[2] = data_raw.i16bit[2];
cparata 0:671edf39d961 338
cparata 0:671edf39d961 339 return 0;
cparata 0:671edf39d961 340 }
cparata 0:671edf39d961 341
cparata 0:671edf39d961 342 /**
cparata 0:671edf39d961 343 * @brief Get the LIS2MDL register value for magnetic sensor
cparata 0:671edf39d961 344 * @param reg address to be read
cparata 0:671edf39d961 345 * @param data pointer where the value is written
cparata 0:671edf39d961 346 * @retval 0 in case of success, an error code otherwise
cparata 0:671edf39d961 347 */
cparata 0:671edf39d961 348 int LIS2MDLSensor::read_reg(uint8_t reg, uint8_t *data)
cparata 0:671edf39d961 349 {
cparata 0:671edf39d961 350 if (lis2mdl_read_reg(&_reg_ctx, reg, data, 1) != 0)
cparata 0:671edf39d961 351 {
cparata 0:671edf39d961 352 return 1;
cparata 0:671edf39d961 353 }
cparata 0:671edf39d961 354
cparata 0:671edf39d961 355 return 0;
cparata 0:671edf39d961 356 }
cparata 0:671edf39d961 357
cparata 0:671edf39d961 358 /**
cparata 0:671edf39d961 359 * @brief Set the LIS2MDL register value for magnetic sensor
cparata 0:671edf39d961 360 * @param pObj the device pObj
cparata 0:671edf39d961 361 * @param reg address to be written
cparata 0:671edf39d961 362 * @param data value to be written
cparata 0:671edf39d961 363 * @retval 0 in case of success, an error code otherwise
cparata 0:671edf39d961 364 */
cparata 0:671edf39d961 365 int LIS2MDLSensor::write_reg(uint8_t reg, uint8_t data)
cparata 0:671edf39d961 366 {
cparata 0:671edf39d961 367 if (lis2mdl_write_reg(&_reg_ctx, reg, &data, 1) != 0)
cparata 0:671edf39d961 368 {
cparata 0:671edf39d961 369 return 1;
cparata 0:671edf39d961 370 }
cparata 0:671edf39d961 371
cparata 0:671edf39d961 372 return 0;
cparata 0:671edf39d961 373 }
cparata 0:671edf39d961 374
cparata 0:671edf39d961 375 /**
cparata 0:671edf39d961 376 * @brief Set self test
cparata 0:671edf39d961 377 * @param status the value of self_test in reg CFG_REG_C
cparata 0:671edf39d961 378 * @retval 0 in case of success, an error code otherwise
cparata 0:671edf39d961 379 */
cparata 0:671edf39d961 380 int LIS2MDLSensor::set_m_self_test(uint8_t status)
cparata 0:671edf39d961 381 {
cparata 0:671edf39d961 382 if (lis2mdl_self_test_set(&_reg_ctx, status) != 0)
cparata 0:671edf39d961 383 {
cparata 0:671edf39d961 384 return 1;
cparata 0:671edf39d961 385 }
cparata 0:671edf39d961 386
cparata 0:671edf39d961 387 return 0;
cparata 0:671edf39d961 388 }
cparata 0:671edf39d961 389
cparata 0:671edf39d961 390 /**
cparata 0:671edf39d961 391 * @brief Get the LIS2MDL MAG data ready bit value
cparata 0:671edf39d961 392 * @param status the status of data ready bit
cparata 0:671edf39d961 393 * @retval 0 in case of success, an error code otherwise
cparata 0:671edf39d961 394 */
cparata 0:671edf39d961 395 int LIS2MDLSensor::get_m_drdy_status(uint8_t *status)
cparata 0:671edf39d961 396 {
cparata 0:671edf39d961 397 if (lis2mdl_mag_data_ready_get(&_reg_ctx, status) != 0)
cparata 0:671edf39d961 398 {
cparata 0:671edf39d961 399 return 1;
cparata 0:671edf39d961 400 }
cparata 0:671edf39d961 401
cparata 0:671edf39d961 402 return 0;
cparata 0:671edf39d961 403 }
cparata 0:671edf39d961 404
cparata 0:671edf39d961 405
cparata 0:671edf39d961 406
cparata 0:671edf39d961 407 int32_t LIS2MDL_io_write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite)
cparata 0:671edf39d961 408 {
cparata 0:671edf39d961 409 return ((LIS2MDLSensor *)handle)->io_write(pBuffer, WriteAddr, nBytesToWrite);
cparata 0:671edf39d961 410 }
cparata 0:671edf39d961 411
cparata 0:671edf39d961 412 int32_t LIS2MDL_io_read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead)
cparata 0:671edf39d961 413 {
cparata 0:671edf39d961 414 return ((LIS2MDLSensor *)handle)->io_read(pBuffer, ReadAddr, nBytesToRead);
cparata 0:671edf39d961 415 }