LIS2MDL magnetometer modified library

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Committer:
martlefebvre94
Date:
Thu Sep 05 15:41:07 2019 +0000
Revision:
3:a16e6dd1ee7f
Parent:
2:0d9d7f8f871b
Temperature compensation and offset cancellation added to the set/get methods

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 1:8562ae1a0534 54 assert(spi);
cparata 1:8562ae1a0534 55 if (cs_pin == NC) {
cparata 1:8562ae1a0534 56 printf("ERROR LIS2MDL CS MUST NOT BE NC\n\r");
cparata 1:8562ae1a0534 57 _dev_spi = NULL;
cparata 1:8562ae1a0534 58 _dev_i2c = NULL;
cparata 1:8562ae1a0534 59 return;
cparata 1:8562ae1a0534 60 }
cparata 0:671edf39d961 61
cparata 1:8562ae1a0534 62 _reg_ctx.write_reg = LIS2MDL_io_write;
cparata 1:8562ae1a0534 63 _reg_ctx.read_reg = LIS2MDL_io_read;
cparata 1:8562ae1a0534 64 _reg_ctx.handle = (void *)this;
cparata 1:8562ae1a0534 65 _cs_pin = 1;
cparata 1:8562ae1a0534 66 _dev_i2c = NULL;
cparata 1:8562ae1a0534 67 _address = 0;
cparata 1:8562ae1a0534 68
cparata 1:8562ae1a0534 69 if (_spi_type == SPI4W) {
cparata 1:8562ae1a0534 70 /* Enable SPI 4-Wires on the component (in this way we lose the usage of INT pin) */
cparata 1:8562ae1a0534 71 uint8_t data = 0x34;
cparata 1:8562ae1a0534 72 lis2mdl_write_reg(&_reg_ctx, LIS2MDL_CFG_REG_C, &data, 1);
cparata 1:8562ae1a0534 73 }
cparata 0:671edf39d961 74 }
cparata 0:671edf39d961 75
cparata 0:671edf39d961 76 /** Constructor
cparata 0:671edf39d961 77 * @param i2c object of an helper class which handles the I2C peripheral
cparata 0:671edf39d961 78 * @param address the address of the component's instance
cparata 0:671edf39d961 79 * @param int_pin the interrupt pin
cparata 0:671edf39d961 80 */
cparata 0:671edf39d961 81 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 82 {
cparata 1:8562ae1a0534 83 assert(i2c);
cparata 1:8562ae1a0534 84 _dev_spi = NULL;
cparata 1:8562ae1a0534 85 _reg_ctx.write_reg = LIS2MDL_io_write;
cparata 1:8562ae1a0534 86 _reg_ctx.read_reg = LIS2MDL_io_read;
cparata 1:8562ae1a0534 87 _reg_ctx.handle = (void *)this;
cparata 0:671edf39d961 88 }
cparata 0:671edf39d961 89
cparata 0:671edf39d961 90 /**
cparata 0:671edf39d961 91 * @brief Initializing the component
cparata 0:671edf39d961 92 * @param init pointer to device specific initalization structure
cparata 0:671edf39d961 93 * @retval 0 in case of success, an error code otherwise
cparata 0:671edf39d961 94 */
cparata 0:671edf39d961 95 int LIS2MDLSensor::init(void *init)
cparata 0:671edf39d961 96 {
cparata 1:8562ae1a0534 97 /* Enable BDU */
cparata 1:8562ae1a0534 98 if (lis2mdl_block_data_update_set(&(_reg_ctx), PROPERTY_ENABLE) != 0) {
cparata 1:8562ae1a0534 99 return 1;
cparata 1:8562ae1a0534 100 }
cparata 0:671edf39d961 101
cparata 1:8562ae1a0534 102 /* Operating mode selection - power down */
cparata 1:8562ae1a0534 103 if (lis2mdl_operating_mode_set(&(_reg_ctx), LIS2MDL_POWER_DOWN) != 0) {
cparata 1:8562ae1a0534 104 return 1;
cparata 1:8562ae1a0534 105 }
cparata 0:671edf39d961 106
cparata 1:8562ae1a0534 107 /* Output data rate selection */
cparata 1:8562ae1a0534 108 if (lis2mdl_data_rate_set(&(_reg_ctx), LIS2MDL_ODR_100Hz) != 0) {
cparata 1:8562ae1a0534 109 return 1;
cparata 1:8562ae1a0534 110 }
cparata 0:671edf39d961 111
cparata 1:8562ae1a0534 112 /* Self Test disabled. */
cparata 1:8562ae1a0534 113 if (lis2mdl_self_test_set(&(_reg_ctx), PROPERTY_DISABLE) != 0) {
cparata 1:8562ae1a0534 114 return 1;
cparata 1:8562ae1a0534 115 }
cparata 0:671edf39d961 116
cparata 1:8562ae1a0534 117 _mag_is_enabled = 0;
cparata 0:671edf39d961 118
cparata 1:8562ae1a0534 119 return 0;
cparata 0:671edf39d961 120 }
cparata 0:671edf39d961 121
cparata 0:671edf39d961 122 /**
cparata 0:671edf39d961 123 * @brief Read component ID
cparata 0:671edf39d961 124 * @param id the WHO_AM_I value
cparata 0:671edf39d961 125 * @retval 0 in case of success, an error code otherwise
cparata 0:671edf39d961 126 */
cparata 0:671edf39d961 127 int LIS2MDLSensor::read_id(uint8_t *id)
cparata 0:671edf39d961 128 {
cparata 1:8562ae1a0534 129 if (lis2mdl_device_id_get(&_reg_ctx, id) != 0) {
cparata 1:8562ae1a0534 130 return 1;
cparata 1:8562ae1a0534 131 }
cparata 0:671edf39d961 132
cparata 1:8562ae1a0534 133 return 0;
cparata 0:671edf39d961 134 }
cparata 0:671edf39d961 135
cparata 0:671edf39d961 136
cparata 0:671edf39d961 137 /**
cparata 0:671edf39d961 138 * @brief Enable the LIS2MDL magnetometer sensor
cparata 0:671edf39d961 139 * @retval 0 in case of success, an error code otherwise
cparata 0:671edf39d961 140 */
cparata 0:671edf39d961 141 int LIS2MDLSensor::enable()
cparata 0:671edf39d961 142 {
cparata 1:8562ae1a0534 143 /* Check if the component is already enabled */
cparata 1:8562ae1a0534 144 if (_mag_is_enabled == 1U) {
cparata 1:8562ae1a0534 145 return 0;
cparata 1:8562ae1a0534 146 }
cparata 0:671edf39d961 147
cparata 1:8562ae1a0534 148 /* Output data rate selection. */
cparata 1:8562ae1a0534 149 if (lis2mdl_operating_mode_set(&_reg_ctx, LIS2MDL_CONTINUOUS_MODE) != 0) {
cparata 1:8562ae1a0534 150 return 1;
cparata 1:8562ae1a0534 151 }
cparata 0:671edf39d961 152
cparata 1:8562ae1a0534 153 _mag_is_enabled = 1;
cparata 0:671edf39d961 154
cparata 1:8562ae1a0534 155 return 0;
cparata 0:671edf39d961 156 }
cparata 0:671edf39d961 157
cparata 0:671edf39d961 158 /**
cparata 0:671edf39d961 159 * @brief Disable the LIS2MDL magnetometer sensor
cparata 0:671edf39d961 160 * @retval 0 in case of success, an error code otherwise
cparata 0:671edf39d961 161 */
cparata 0:671edf39d961 162 int LIS2MDLSensor::disable()
cparata 0:671edf39d961 163 {
cparata 1:8562ae1a0534 164 /* Check if the component is already disabled */
cparata 1:8562ae1a0534 165 if (_mag_is_enabled == 0U) {
cparata 1:8562ae1a0534 166 return 0;
cparata 1:8562ae1a0534 167 }
cparata 0:671edf39d961 168
cparata 1:8562ae1a0534 169 /* Output data rate selection - power down. */
cparata 1:8562ae1a0534 170 if (lis2mdl_operating_mode_set(&_reg_ctx, LIS2MDL_POWER_DOWN) != 0) {
cparata 1:8562ae1a0534 171 return 1;
cparata 1:8562ae1a0534 172 }
cparata 0:671edf39d961 173
cparata 1:8562ae1a0534 174 _mag_is_enabled = 0;
cparata 0:671edf39d961 175
cparata 1:8562ae1a0534 176 return 0;
cparata 0:671edf39d961 177 }
cparata 0:671edf39d961 178
cparata 0:671edf39d961 179 /**
cparata 0:671edf39d961 180 * @brief Get the LIS2MDL magnetometer sensor sensitivity
cparata 0:671edf39d961 181 * @param sensitivity pointer where the sensitivity is written
cparata 0:671edf39d961 182 * @retval 0 in case of success, an error code otherwise
cparata 0:671edf39d961 183 */
cparata 0:671edf39d961 184 int LIS2MDLSensor::get_m_sensitivity(float *sensitivity)
cparata 0:671edf39d961 185 {
cparata 1:8562ae1a0534 186 *sensitivity = LIS2MDL_MAG_SENSITIVITY_FS_50GAUSS;
cparata 0:671edf39d961 187
cparata 1:8562ae1a0534 188 return 0;
cparata 0:671edf39d961 189 }
cparata 0:671edf39d961 190
cparata 0:671edf39d961 191 /**
cparata 0:671edf39d961 192 * @brief Get the LIS2MDL magnetometer sensor output data rate
cparata 0:671edf39d961 193 * @param odr pointer where the output data rate is written
cparata 0:671edf39d961 194 * @retval 0 in case of success, an error code otherwise
cparata 0:671edf39d961 195 */
cparata 0:671edf39d961 196 int LIS2MDLSensor::get_m_odr(float *odr)
cparata 0:671edf39d961 197 {
cparata 1:8562ae1a0534 198 int ret = 0;
cparata 1:8562ae1a0534 199 lis2mdl_odr_t odr_low_level;
cparata 0:671edf39d961 200
cparata 1:8562ae1a0534 201 /* Get current output data rate. */
cparata 1:8562ae1a0534 202 if (lis2mdl_data_rate_get(&_reg_ctx, &odr_low_level) != 0) {
cparata 1:8562ae1a0534 203 return 1;
cparata 1:8562ae1a0534 204 }
cparata 0:671edf39d961 205
cparata 1:8562ae1a0534 206 switch (odr_low_level) {
cparata 1:8562ae1a0534 207 case LIS2MDL_ODR_10Hz:
cparata 1:8562ae1a0534 208 *odr = 10.0f;
cparata 1:8562ae1a0534 209 break;
cparata 0:671edf39d961 210
cparata 1:8562ae1a0534 211 case LIS2MDL_ODR_20Hz:
cparata 1:8562ae1a0534 212 *odr = 20.0f;
cparata 1:8562ae1a0534 213 break;
cparata 0:671edf39d961 214
cparata 1:8562ae1a0534 215 case LIS2MDL_ODR_50Hz:
cparata 1:8562ae1a0534 216 *odr = 50.0f;
cparata 1:8562ae1a0534 217 break;
cparata 0:671edf39d961 218
cparata 1:8562ae1a0534 219 case LIS2MDL_ODR_100Hz:
cparata 1:8562ae1a0534 220 *odr = 100.0f;
cparata 1:8562ae1a0534 221 break;
cparata 0:671edf39d961 222
cparata 1:8562ae1a0534 223 default:
cparata 1:8562ae1a0534 224 ret = 1;
cparata 1:8562ae1a0534 225 break;
cparata 1:8562ae1a0534 226 }
cparata 0:671edf39d961 227
cparata 1:8562ae1a0534 228 return ret;
cparata 0:671edf39d961 229 }
cparata 0:671edf39d961 230
cparata 0:671edf39d961 231 /**
cparata 0:671edf39d961 232 * @brief Set the LIS2MDL magnetometer sensor output data rate
cparata 0:671edf39d961 233 * @param odr the output data rate value to be set
cparata 0:671edf39d961 234 * @retval 0 in case of success, an error code otherwise
cparata 0:671edf39d961 235 */
cparata 0:671edf39d961 236 int LIS2MDLSensor::set_m_odr(float odr)
cparata 0:671edf39d961 237 {
cparata 1:8562ae1a0534 238 lis2mdl_odr_t new_odr;
cparata 0:671edf39d961 239
cparata 1:8562ae1a0534 240 new_odr = (odr <= 10.000f) ? LIS2MDL_ODR_10Hz
cparata 1:8562ae1a0534 241 : (odr <= 20.000f) ? LIS2MDL_ODR_20Hz
cparata 1:8562ae1a0534 242 : (odr <= 50.000f) ? LIS2MDL_ODR_50Hz
cparata 1:8562ae1a0534 243 : LIS2MDL_ODR_100Hz;
cparata 0:671edf39d961 244
cparata 1:8562ae1a0534 245 if (lis2mdl_data_rate_set(&_reg_ctx, new_odr) != 0) {
cparata 1:8562ae1a0534 246 return 1;
cparata 1:8562ae1a0534 247 }
cparata 1:8562ae1a0534 248
cparata 1:8562ae1a0534 249 return 0;
cparata 0:671edf39d961 250 }
cparata 0:671edf39d961 251
cparata 0:671edf39d961 252 /**
cparata 0:671edf39d961 253 * @brief Get the LIS2MDL magnetometer sensor full scale
cparata 0:671edf39d961 254 * @param full_scale pointer where the full scale is written
cparata 0:671edf39d961 255 * @retval 0 in case of success, an error code otherwise
cparata 0:671edf39d961 256 */
cparata 0:671edf39d961 257 int LIS2MDLSensor::get_m_fs(float *full_scale)
cparata 0:671edf39d961 258 {
cparata 1:8562ae1a0534 259 *full_scale = 50.0f;
cparata 0:671edf39d961 260
cparata 1:8562ae1a0534 261 return 0;
cparata 0:671edf39d961 262 }
cparata 0:671edf39d961 263
cparata 0:671edf39d961 264 /**
cparata 0:671edf39d961 265 * @brief Set the LIS2MDL magnetometer sensor full scale
cparata 0:671edf39d961 266 * @param full_scale the functional full scale to be set
cparata 0:671edf39d961 267 * @retval 0 in case of success, an error code otherwise
cparata 0:671edf39d961 268 */
cparata 0:671edf39d961 269 int LIS2MDLSensor::set_m_fs(float full_scale)
cparata 0:671edf39d961 270 {
cparata 1:8562ae1a0534 271 (void)full_scale;
cparata 1:8562ae1a0534 272 return 0;
cparata 0:671edf39d961 273 }
cparata 0:671edf39d961 274
cparata 0:671edf39d961 275 /**
martlefebvre94 2:0d9d7f8f871b 276 * @brief Get the LIS2MDL magnetometer sensor power mode
martlefebvre94 2:0d9d7f8f871b 277 * @param lp pointer where the power mode is written
martlefebvre94 2:0d9d7f8f871b 278 * @retval 0 in case of success, an error code otherwise
martlefebvre94 2:0d9d7f8f871b 279 */
martlefebvre94 2:0d9d7f8f871b 280 int LIS2MDLSensor::get_m_lp(uint8_t *lp)
martlefebvre94 2:0d9d7f8f871b 281 {
martlefebvre94 2:0d9d7f8f871b 282 int ret = 0;
martlefebvre94 2:0d9d7f8f871b 283 lis2mdl_lp_t current_lp;
martlefebvre94 2:0d9d7f8f871b 284
martlefebvre94 2:0d9d7f8f871b 285 /* Get current power mode. */
martlefebvre94 2:0d9d7f8f871b 286 if (lis2mdl_power_mode_get(&_reg_ctx, &current_lp) != 0) {
martlefebvre94 2:0d9d7f8f871b 287 return 1;
martlefebvre94 2:0d9d7f8f871b 288 }
martlefebvre94 2:0d9d7f8f871b 289
martlefebvre94 2:0d9d7f8f871b 290 switch (current_lp) {
martlefebvre94 2:0d9d7f8f871b 291 case LIS2MDL_HIGH_RESOLUTION:
martlefebvre94 2:0d9d7f8f871b 292 *lp = 0;
martlefebvre94 2:0d9d7f8f871b 293 break;
martlefebvre94 2:0d9d7f8f871b 294
martlefebvre94 2:0d9d7f8f871b 295 case LIS2MDL_LOW_POWER:
martlefebvre94 2:0d9d7f8f871b 296 *lp = 1;
martlefebvre94 2:0d9d7f8f871b 297 break;
martlefebvre94 2:0d9d7f8f871b 298
martlefebvre94 2:0d9d7f8f871b 299 default:
martlefebvre94 2:0d9d7f8f871b 300 ret = 1;
martlefebvre94 2:0d9d7f8f871b 301 break;
martlefebvre94 2:0d9d7f8f871b 302 }
martlefebvre94 2:0d9d7f8f871b 303
martlefebvre94 2:0d9d7f8f871b 304 return ret;
martlefebvre94 2:0d9d7f8f871b 305 }
martlefebvre94 2:0d9d7f8f871b 306
martlefebvre94 2:0d9d7f8f871b 307 /**
martlefebvre94 2:0d9d7f8f871b 308 * @brief Set the LIS2MDL magnetometer sensor power mode
martlefebvre94 2:0d9d7f8f871b 309 * @param lp the power mode value to be set
martlefebvre94 2:0d9d7f8f871b 310 * @retval 0 in case of success, an error code otherwise
martlefebvre94 2:0d9d7f8f871b 311 */
martlefebvre94 2:0d9d7f8f871b 312 int LIS2MDLSensor::set_m_lp(uint8_t lp)
martlefebvre94 2:0d9d7f8f871b 313 {
martlefebvre94 2:0d9d7f8f871b 314 lis2mdl_lp_t new_lp;
martlefebvre94 2:0d9d7f8f871b 315
martlefebvre94 2:0d9d7f8f871b 316 new_lp = (lp == 0) ? LIS2MDL_HIGH_RESOLUTION
martlefebvre94 2:0d9d7f8f871b 317 : LIS2MDL_LOW_POWER;
martlefebvre94 2:0d9d7f8f871b 318
martlefebvre94 2:0d9d7f8f871b 319 if (lis2mdl_power_mode_set(&_reg_ctx, new_lp) != 0) {
martlefebvre94 2:0d9d7f8f871b 320 return 1;
martlefebvre94 2:0d9d7f8f871b 321 }
martlefebvre94 2:0d9d7f8f871b 322
martlefebvre94 2:0d9d7f8f871b 323 return 0;
martlefebvre94 2:0d9d7f8f871b 324 }
martlefebvre94 2:0d9d7f8f871b 325
martlefebvre94 2:0d9d7f8f871b 326 /**
martlefebvre94 2:0d9d7f8f871b 327 * @brief Get the LIS2MDL magnetometer sensor bandwidth
martlefebvre94 2:0d9d7f8f871b 328 * @param lpf pointer where the bandwidth is written
martlefebvre94 2:0d9d7f8f871b 329 * @retval 0 in case of success, an error code otherwise
martlefebvre94 2:0d9d7f8f871b 330 */
martlefebvre94 2:0d9d7f8f871b 331 int LIS2MDLSensor::get_m_lpf(uint8_t *lpf)
martlefebvre94 2:0d9d7f8f871b 332 {
martlefebvre94 2:0d9d7f8f871b 333 int ret = 0;
martlefebvre94 2:0d9d7f8f871b 334 lis2mdl_lpf_t current_lpf;
martlefebvre94 2:0d9d7f8f871b 335
martlefebvre94 3:a16e6dd1ee7f 336 /* Get current bandwidth. */
martlefebvre94 2:0d9d7f8f871b 337 if (lis2mdl_low_pass_bandwidth_get(&_reg_ctx, &current_lpf) != 0) {
martlefebvre94 2:0d9d7f8f871b 338 return 1;
martlefebvre94 2:0d9d7f8f871b 339 }
martlefebvre94 2:0d9d7f8f871b 340
martlefebvre94 2:0d9d7f8f871b 341 switch (current_lpf) {
martlefebvre94 2:0d9d7f8f871b 342 case LIS2MDL_ODR_DIV_2:
martlefebvre94 2:0d9d7f8f871b 343 *lpf = 0;
martlefebvre94 2:0d9d7f8f871b 344 break;
martlefebvre94 2:0d9d7f8f871b 345
martlefebvre94 2:0d9d7f8f871b 346 case LIS2MDL_ODR_DIV_4:
martlefebvre94 2:0d9d7f8f871b 347 *lpf = 1;
martlefebvre94 2:0d9d7f8f871b 348 break;
martlefebvre94 2:0d9d7f8f871b 349
martlefebvre94 2:0d9d7f8f871b 350 default:
martlefebvre94 2:0d9d7f8f871b 351 ret = 1;
martlefebvre94 2:0d9d7f8f871b 352 break;
martlefebvre94 2:0d9d7f8f871b 353 }
martlefebvre94 2:0d9d7f8f871b 354
martlefebvre94 2:0d9d7f8f871b 355 return ret;
martlefebvre94 2:0d9d7f8f871b 356 }
martlefebvre94 2:0d9d7f8f871b 357
martlefebvre94 2:0d9d7f8f871b 358 /**
martlefebvre94 2:0d9d7f8f871b 359 * @brief Set the LIS2MDL magnetometer sensor bandwidth
martlefebvre94 2:0d9d7f8f871b 360 * @param lpf the bandwidth value to be set
martlefebvre94 2:0d9d7f8f871b 361 * @retval 0 in case of success, an error code otherwise
martlefebvre94 2:0d9d7f8f871b 362 */
martlefebvre94 2:0d9d7f8f871b 363 int LIS2MDLSensor::set_m_lpf(uint8_t lpf)
martlefebvre94 2:0d9d7f8f871b 364 {
martlefebvre94 2:0d9d7f8f871b 365 lis2mdl_lpf_t new_lpf;
martlefebvre94 2:0d9d7f8f871b 366
martlefebvre94 2:0d9d7f8f871b 367 new_lpf = (lpf == 0) ? LIS2MDL_ODR_DIV_2
martlefebvre94 2:0d9d7f8f871b 368 : LIS2MDL_ODR_DIV_4;
martlefebvre94 2:0d9d7f8f871b 369
martlefebvre94 2:0d9d7f8f871b 370 if (lis2mdl_low_pass_bandwidth_set(&_reg_ctx, new_lpf) != 0) {
martlefebvre94 2:0d9d7f8f871b 371 return 1;
martlefebvre94 2:0d9d7f8f871b 372 }
martlefebvre94 2:0d9d7f8f871b 373
martlefebvre94 2:0d9d7f8f871b 374 return 0;
martlefebvre94 2:0d9d7f8f871b 375 }
martlefebvre94 2:0d9d7f8f871b 376
martlefebvre94 2:0d9d7f8f871b 377 /**
martlefebvre94 3:a16e6dd1ee7f 378 * @brief Get the LIS2MDL magnetometer sensor temperature compensation
martlefebvre94 3:a16e6dd1ee7f 379 * @param comp_temp_en pointer where the temperature compensation is written
martlefebvre94 3:a16e6dd1ee7f 380 * @retval 0 in case of success, an error code otherwise
martlefebvre94 3:a16e6dd1ee7f 381 */
martlefebvre94 3:a16e6dd1ee7f 382 int LIS2MDLSensor::get_m_comp_temp_en(uint8_t *comp_temp_en)
martlefebvre94 3:a16e6dd1ee7f 383 {
martlefebvre94 3:a16e6dd1ee7f 384 if (lis2mdl_offset_temp_comp_get(&_reg_ctx, comp_temp_en) != 0) {
martlefebvre94 3:a16e6dd1ee7f 385 return 1;
martlefebvre94 3:a16e6dd1ee7f 386 }
martlefebvre94 3:a16e6dd1ee7f 387 return 0;
martlefebvre94 3:a16e6dd1ee7f 388 }
martlefebvre94 3:a16e6dd1ee7f 389
martlefebvre94 3:a16e6dd1ee7f 390 /**
martlefebvre94 3:a16e6dd1ee7f 391 * @brief Set the LIS2MDL magnetometer sensor temperature compensation
martlefebvre94 3:a16e6dd1ee7f 392 * @param comp_temp_en the temperature compensation value to be set
martlefebvre94 3:a16e6dd1ee7f 393 * @retval 0 in case of success, an error code otherwise
martlefebvre94 3:a16e6dd1ee7f 394 */
martlefebvre94 3:a16e6dd1ee7f 395 int LIS2MDLSensor::set_m_comp_temp_en(uint8_t comp_temp_en)
martlefebvre94 3:a16e6dd1ee7f 396 {
martlefebvre94 3:a16e6dd1ee7f 397 if (lis2mdl_offset_temp_comp_set(&_reg_ctx, comp_temp_en) != 0) {
martlefebvre94 3:a16e6dd1ee7f 398 return 1;
martlefebvre94 3:a16e6dd1ee7f 399 }
martlefebvre94 3:a16e6dd1ee7f 400 return 0;
martlefebvre94 3:a16e6dd1ee7f 401 }
martlefebvre94 3:a16e6dd1ee7f 402
martlefebvre94 3:a16e6dd1ee7f 403 /**
martlefebvre94 3:a16e6dd1ee7f 404 * @brief Get the LIS2MDL magnetometer sensor offset cancellation
martlefebvre94 3:a16e6dd1ee7f 405 * @param off_canc pointer where the offset cancellation is written
martlefebvre94 3:a16e6dd1ee7f 406 * @retval 0 in case of success, an error code otherwise
martlefebvre94 3:a16e6dd1ee7f 407 */
martlefebvre94 3:a16e6dd1ee7f 408 int LIS2MDLSensor::get_m_off_canc(uint8_t *off_canc)
martlefebvre94 3:a16e6dd1ee7f 409 {
martlefebvre94 3:a16e6dd1ee7f 410 int ret = 0;
martlefebvre94 3:a16e6dd1ee7f 411 lis2mdl_set_rst_t current_off_canc;
martlefebvre94 3:a16e6dd1ee7f 412
martlefebvre94 3:a16e6dd1ee7f 413 /* Get current offset cancellation. */
martlefebvre94 3:a16e6dd1ee7f 414 if (lis2mdl_set_rst_mode_get(&_reg_ctx, &current_off_canc) != 0) {
martlefebvre94 3:a16e6dd1ee7f 415 return 1;
martlefebvre94 3:a16e6dd1ee7f 416 }
martlefebvre94 3:a16e6dd1ee7f 417
martlefebvre94 3:a16e6dd1ee7f 418 switch (current_off_canc) {
martlefebvre94 3:a16e6dd1ee7f 419 case LIS2MDL_SET_SENS_ODR_DIV_63:
martlefebvre94 3:a16e6dd1ee7f 420 *off_canc = 0;
martlefebvre94 3:a16e6dd1ee7f 421 break;
martlefebvre94 3:a16e6dd1ee7f 422
martlefebvre94 3:a16e6dd1ee7f 423 case LIS2MDL_SENS_OFF_CANC_EVERY_ODR:
martlefebvre94 3:a16e6dd1ee7f 424 *off_canc = 1;
martlefebvre94 3:a16e6dd1ee7f 425 break;
martlefebvre94 3:a16e6dd1ee7f 426
martlefebvre94 3:a16e6dd1ee7f 427 case LIS2MDL_SET_SENS_ONLY_AT_POWER_ON:
martlefebvre94 3:a16e6dd1ee7f 428 *off_canc = 2;
martlefebvre94 3:a16e6dd1ee7f 429 break;
martlefebvre94 3:a16e6dd1ee7f 430
martlefebvre94 3:a16e6dd1ee7f 431 default:
martlefebvre94 3:a16e6dd1ee7f 432 ret = 1;
martlefebvre94 3:a16e6dd1ee7f 433 break;
martlefebvre94 3:a16e6dd1ee7f 434 }
martlefebvre94 3:a16e6dd1ee7f 435
martlefebvre94 3:a16e6dd1ee7f 436 return ret;
martlefebvre94 3:a16e6dd1ee7f 437 }
martlefebvre94 3:a16e6dd1ee7f 438
martlefebvre94 3:a16e6dd1ee7f 439 /**
martlefebvre94 3:a16e6dd1ee7f 440 * @brief Set the LIS2MDL magnetometer sensor offset cancellation
martlefebvre94 3:a16e6dd1ee7f 441 * @param off_canc the offset cancellation value to be set
martlefebvre94 3:a16e6dd1ee7f 442 * @retval 0 in case of success, an error code otherwise
martlefebvre94 3:a16e6dd1ee7f 443 */
martlefebvre94 3:a16e6dd1ee7f 444 int LIS2MDLSensor::set_m_off_canc(uint8_t off_canc)
martlefebvre94 3:a16e6dd1ee7f 445 {
martlefebvre94 3:a16e6dd1ee7f 446 lis2mdl_set_rst_t new_off_canc;
martlefebvre94 3:a16e6dd1ee7f 447
martlefebvre94 3:a16e6dd1ee7f 448 new_off_canc = (off_canc == 0) ? LIS2MDL_SET_SENS_ODR_DIV_63
martlefebvre94 3:a16e6dd1ee7f 449 : (off_canc == 1) ? LIS2MDL_SENS_OFF_CANC_EVERY_ODR
martlefebvre94 3:a16e6dd1ee7f 450 : LIS2MDL_SET_SENS_ONLY_AT_POWER_ON;
martlefebvre94 3:a16e6dd1ee7f 451
martlefebvre94 3:a16e6dd1ee7f 452 if (lis2mdl_set_rst_mode_set(&_reg_ctx, new_off_canc) != 0) {
martlefebvre94 3:a16e6dd1ee7f 453 return 1;
martlefebvre94 3:a16e6dd1ee7f 454 }
martlefebvre94 3:a16e6dd1ee7f 455
martlefebvre94 3:a16e6dd1ee7f 456 return 0;
martlefebvre94 3:a16e6dd1ee7f 457 }
martlefebvre94 3:a16e6dd1ee7f 458
martlefebvre94 3:a16e6dd1ee7f 459 /**
cparata 0:671edf39d961 460 * @brief Get the LIS2MDL magnetometer sensor axes
cparata 0:671edf39d961 461 * @param magnetic_field pointer where the values of the axes are written
cparata 0:671edf39d961 462 * @retval 0 in case of success, an error code otherwise
cparata 0:671edf39d961 463 */
cparata 0:671edf39d961 464 int LIS2MDLSensor::get_m_axes(int32_t *magnetic_field)
cparata 0:671edf39d961 465 {
cparata 1:8562ae1a0534 466 axis3bit16_t data_raw;
cparata 1:8562ae1a0534 467 float sensitivity;
cparata 0:671edf39d961 468
cparata 1:8562ae1a0534 469 /* Read raw data values. */
cparata 1:8562ae1a0534 470 if (lis2mdl_magnetic_raw_get(&_reg_ctx, data_raw.u8bit) != 0) {
cparata 1:8562ae1a0534 471 return 1;
cparata 1:8562ae1a0534 472 }
cparata 0:671edf39d961 473
cparata 1:8562ae1a0534 474 /* Get LIS2MDL actual sensitivity. */
cparata 1:8562ae1a0534 475 if (get_m_sensitivity(&sensitivity) != 0) {
cparata 1:8562ae1a0534 476 return 1;
cparata 1:8562ae1a0534 477 }
cparata 0:671edf39d961 478
cparata 1:8562ae1a0534 479 /* Calculate the data. */
cparata 1:8562ae1a0534 480 magnetic_field[0] = (int32_t)((float)((float)data_raw.i16bit[0] * sensitivity));
cparata 1:8562ae1a0534 481 magnetic_field[1] = (int32_t)((float)((float)data_raw.i16bit[1] * sensitivity));
cparata 1:8562ae1a0534 482 magnetic_field[2] = (int32_t)((float)((float)data_raw.i16bit[2] * sensitivity));
cparata 0:671edf39d961 483
cparata 1:8562ae1a0534 484 return 0;
cparata 0:671edf39d961 485 }
cparata 0:671edf39d961 486
cparata 0:671edf39d961 487 /**
cparata 0:671edf39d961 488 * @brief Get the LIS2MDL magnetometer sensor raw axes
cparata 0:671edf39d961 489 * @param value pointer where the raw values of the axes are written
cparata 0:671edf39d961 490 * @retval 0 in case of success, an error code otherwise
cparata 0:671edf39d961 491 */
cparata 0:671edf39d961 492 int LIS2MDLSensor::get_m_axes_raw(int16_t *value)
cparata 0:671edf39d961 493 {
cparata 1:8562ae1a0534 494 axis3bit16_t data_raw;
cparata 1:8562ae1a0534 495
cparata 1:8562ae1a0534 496 /* Read raw data values. */
cparata 1:8562ae1a0534 497 if (lis2mdl_magnetic_raw_get(&_reg_ctx, data_raw.u8bit) != 0) {
cparata 1:8562ae1a0534 498 return 1;
cparata 1:8562ae1a0534 499 }
cparata 0:671edf39d961 500
cparata 1:8562ae1a0534 501 /* Format the data. */
cparata 1:8562ae1a0534 502 value[0] = data_raw.i16bit[0];
cparata 1:8562ae1a0534 503 value[1] = data_raw.i16bit[1];
cparata 1:8562ae1a0534 504 value[2] = data_raw.i16bit[2];
cparata 0:671edf39d961 505
cparata 1:8562ae1a0534 506 return 0;
cparata 0:671edf39d961 507 }
cparata 0:671edf39d961 508
cparata 0:671edf39d961 509 /**
cparata 0:671edf39d961 510 * @brief Get the LIS2MDL register value for magnetic sensor
cparata 0:671edf39d961 511 * @param reg address to be read
cparata 0:671edf39d961 512 * @param data pointer where the value is written
cparata 0:671edf39d961 513 * @retval 0 in case of success, an error code otherwise
cparata 0:671edf39d961 514 */
cparata 0:671edf39d961 515 int LIS2MDLSensor::read_reg(uint8_t reg, uint8_t *data)
cparata 0:671edf39d961 516 {
cparata 1:8562ae1a0534 517 if (lis2mdl_read_reg(&_reg_ctx, reg, data, 1) != 0) {
cparata 1:8562ae1a0534 518 return 1;
cparata 1:8562ae1a0534 519 }
cparata 0:671edf39d961 520
cparata 1:8562ae1a0534 521 return 0;
cparata 0:671edf39d961 522 }
cparata 0:671edf39d961 523
cparata 0:671edf39d961 524 /**
cparata 0:671edf39d961 525 * @brief Set the LIS2MDL register value for magnetic sensor
cparata 0:671edf39d961 526 * @param pObj the device pObj
cparata 0:671edf39d961 527 * @param reg address to be written
cparata 0:671edf39d961 528 * @param data value to be written
cparata 0:671edf39d961 529 * @retval 0 in case of success, an error code otherwise
cparata 0:671edf39d961 530 */
cparata 0:671edf39d961 531 int LIS2MDLSensor::write_reg(uint8_t reg, uint8_t data)
cparata 0:671edf39d961 532 {
cparata 1:8562ae1a0534 533 if (lis2mdl_write_reg(&_reg_ctx, reg, &data, 1) != 0) {
cparata 1:8562ae1a0534 534 return 1;
cparata 1:8562ae1a0534 535 }
cparata 0:671edf39d961 536
cparata 1:8562ae1a0534 537 return 0;
cparata 0:671edf39d961 538 }
cparata 0:671edf39d961 539
cparata 0:671edf39d961 540 /**
cparata 0:671edf39d961 541 * @brief Set self test
cparata 0:671edf39d961 542 * @param status the value of self_test in reg CFG_REG_C
cparata 0:671edf39d961 543 * @retval 0 in case of success, an error code otherwise
cparata 0:671edf39d961 544 */
cparata 0:671edf39d961 545 int LIS2MDLSensor::set_m_self_test(uint8_t status)
cparata 0:671edf39d961 546 {
cparata 1:8562ae1a0534 547 if (lis2mdl_self_test_set(&_reg_ctx, status) != 0) {
cparata 1:8562ae1a0534 548 return 1;
cparata 1:8562ae1a0534 549 }
cparata 0:671edf39d961 550
cparata 1:8562ae1a0534 551 return 0;
cparata 0:671edf39d961 552 }
cparata 0:671edf39d961 553
cparata 0:671edf39d961 554 /**
cparata 0:671edf39d961 555 * @brief Get the LIS2MDL MAG data ready bit value
cparata 0:671edf39d961 556 * @param status the status of data ready bit
cparata 0:671edf39d961 557 * @retval 0 in case of success, an error code otherwise
cparata 0:671edf39d961 558 */
cparata 0:671edf39d961 559 int LIS2MDLSensor::get_m_drdy_status(uint8_t *status)
cparata 0:671edf39d961 560 {
cparata 1:8562ae1a0534 561 if (lis2mdl_mag_data_ready_get(&_reg_ctx, status) != 0) {
cparata 1:8562ae1a0534 562 return 1;
cparata 1:8562ae1a0534 563 }
cparata 0:671edf39d961 564
cparata 1:8562ae1a0534 565 return 0;
cparata 0:671edf39d961 566 }
cparata 0:671edf39d961 567
cparata 0:671edf39d961 568
cparata 0:671edf39d961 569
cparata 0:671edf39d961 570 int32_t LIS2MDL_io_write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite)
cparata 0:671edf39d961 571 {
cparata 1:8562ae1a0534 572 return ((LIS2MDLSensor *)handle)->io_write(pBuffer, WriteAddr, nBytesToWrite);
cparata 0:671edf39d961 573 }
cparata 0:671edf39d961 574
cparata 0:671edf39d961 575 int32_t LIS2MDL_io_read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead)
cparata 0:671edf39d961 576 {
cparata 1:8562ae1a0534 577 return ((LIS2MDLSensor *)handle)->io_read(pBuffer, ReadAddr, nBytesToRead);
cparata 0:671edf39d961 578 }