3-axis MEMS ultra low power magnetometer
Dependencies: X_NUCLEO_COMMON ST_INTERFACES
Dependents: X_NUCLEO_IKS01A3 X_NUCLEO_IKS01A3
LIS2MDLSensor.cpp@1:8562ae1a0534, 2019-07-24 (annotated)
- Committer:
- cparata
- Date:
- Wed Jul 24 14:18:39 2019 +0000
- Revision:
- 1:8562ae1a0534
- Parent:
- 0:671edf39d961
Format with Astyle
Who changed what in which revision?
User | Revision | Line number | New 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>© 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 | /** |
cparata | 0:671edf39d961 | 276 | * @brief Get the LIS2MDL magnetometer sensor axes |
cparata | 0:671edf39d961 | 277 | * @param magnetic_field pointer where the values of the axes are written |
cparata | 0:671edf39d961 | 278 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:671edf39d961 | 279 | */ |
cparata | 0:671edf39d961 | 280 | int LIS2MDLSensor::get_m_axes(int32_t *magnetic_field) |
cparata | 0:671edf39d961 | 281 | { |
cparata | 1:8562ae1a0534 | 282 | axis3bit16_t data_raw; |
cparata | 1:8562ae1a0534 | 283 | float sensitivity; |
cparata | 0:671edf39d961 | 284 | |
cparata | 1:8562ae1a0534 | 285 | /* Read raw data values. */ |
cparata | 1:8562ae1a0534 | 286 | if (lis2mdl_magnetic_raw_get(&_reg_ctx, data_raw.u8bit) != 0) { |
cparata | 1:8562ae1a0534 | 287 | return 1; |
cparata | 1:8562ae1a0534 | 288 | } |
cparata | 0:671edf39d961 | 289 | |
cparata | 1:8562ae1a0534 | 290 | /* Get LIS2MDL actual sensitivity. */ |
cparata | 1:8562ae1a0534 | 291 | if (get_m_sensitivity(&sensitivity) != 0) { |
cparata | 1:8562ae1a0534 | 292 | return 1; |
cparata | 1:8562ae1a0534 | 293 | } |
cparata | 0:671edf39d961 | 294 | |
cparata | 1:8562ae1a0534 | 295 | /* Calculate the data. */ |
cparata | 1:8562ae1a0534 | 296 | magnetic_field[0] = (int32_t)((float)((float)data_raw.i16bit[0] * sensitivity)); |
cparata | 1:8562ae1a0534 | 297 | magnetic_field[1] = (int32_t)((float)((float)data_raw.i16bit[1] * sensitivity)); |
cparata | 1:8562ae1a0534 | 298 | magnetic_field[2] = (int32_t)((float)((float)data_raw.i16bit[2] * sensitivity)); |
cparata | 0:671edf39d961 | 299 | |
cparata | 1:8562ae1a0534 | 300 | return 0; |
cparata | 0:671edf39d961 | 301 | } |
cparata | 0:671edf39d961 | 302 | |
cparata | 0:671edf39d961 | 303 | /** |
cparata | 0:671edf39d961 | 304 | * @brief Get the LIS2MDL magnetometer sensor raw axes |
cparata | 0:671edf39d961 | 305 | * @param value pointer where the raw values of the axes are written |
cparata | 0:671edf39d961 | 306 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:671edf39d961 | 307 | */ |
cparata | 0:671edf39d961 | 308 | int LIS2MDLSensor::get_m_axes_raw(int16_t *value) |
cparata | 0:671edf39d961 | 309 | { |
cparata | 1:8562ae1a0534 | 310 | axis3bit16_t data_raw; |
cparata | 1:8562ae1a0534 | 311 | |
cparata | 1:8562ae1a0534 | 312 | /* Read raw data values. */ |
cparata | 1:8562ae1a0534 | 313 | if (lis2mdl_magnetic_raw_get(&_reg_ctx, data_raw.u8bit) != 0) { |
cparata | 1:8562ae1a0534 | 314 | return 1; |
cparata | 1:8562ae1a0534 | 315 | } |
cparata | 0:671edf39d961 | 316 | |
cparata | 1:8562ae1a0534 | 317 | /* Format the data. */ |
cparata | 1:8562ae1a0534 | 318 | value[0] = data_raw.i16bit[0]; |
cparata | 1:8562ae1a0534 | 319 | value[1] = data_raw.i16bit[1]; |
cparata | 1:8562ae1a0534 | 320 | value[2] = data_raw.i16bit[2]; |
cparata | 0:671edf39d961 | 321 | |
cparata | 1:8562ae1a0534 | 322 | return 0; |
cparata | 0:671edf39d961 | 323 | } |
cparata | 0:671edf39d961 | 324 | |
cparata | 0:671edf39d961 | 325 | /** |
cparata | 0:671edf39d961 | 326 | * @brief Get the LIS2MDL register value for magnetic sensor |
cparata | 0:671edf39d961 | 327 | * @param reg address to be read |
cparata | 0:671edf39d961 | 328 | * @param data pointer where the value is written |
cparata | 0:671edf39d961 | 329 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:671edf39d961 | 330 | */ |
cparata | 0:671edf39d961 | 331 | int LIS2MDLSensor::read_reg(uint8_t reg, uint8_t *data) |
cparata | 0:671edf39d961 | 332 | { |
cparata | 1:8562ae1a0534 | 333 | if (lis2mdl_read_reg(&_reg_ctx, reg, data, 1) != 0) { |
cparata | 1:8562ae1a0534 | 334 | return 1; |
cparata | 1:8562ae1a0534 | 335 | } |
cparata | 0:671edf39d961 | 336 | |
cparata | 1:8562ae1a0534 | 337 | return 0; |
cparata | 0:671edf39d961 | 338 | } |
cparata | 0:671edf39d961 | 339 | |
cparata | 0:671edf39d961 | 340 | /** |
cparata | 0:671edf39d961 | 341 | * @brief Set the LIS2MDL register value for magnetic sensor |
cparata | 0:671edf39d961 | 342 | * @param pObj the device pObj |
cparata | 0:671edf39d961 | 343 | * @param reg address to be written |
cparata | 0:671edf39d961 | 344 | * @param data value to be written |
cparata | 0:671edf39d961 | 345 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:671edf39d961 | 346 | */ |
cparata | 0:671edf39d961 | 347 | int LIS2MDLSensor::write_reg(uint8_t reg, uint8_t data) |
cparata | 0:671edf39d961 | 348 | { |
cparata | 1:8562ae1a0534 | 349 | if (lis2mdl_write_reg(&_reg_ctx, reg, &data, 1) != 0) { |
cparata | 1:8562ae1a0534 | 350 | return 1; |
cparata | 1:8562ae1a0534 | 351 | } |
cparata | 0:671edf39d961 | 352 | |
cparata | 1:8562ae1a0534 | 353 | return 0; |
cparata | 0:671edf39d961 | 354 | } |
cparata | 0:671edf39d961 | 355 | |
cparata | 0:671edf39d961 | 356 | /** |
cparata | 0:671edf39d961 | 357 | * @brief Set self test |
cparata | 0:671edf39d961 | 358 | * @param status the value of self_test in reg CFG_REG_C |
cparata | 0:671edf39d961 | 359 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:671edf39d961 | 360 | */ |
cparata | 0:671edf39d961 | 361 | int LIS2MDLSensor::set_m_self_test(uint8_t status) |
cparata | 0:671edf39d961 | 362 | { |
cparata | 1:8562ae1a0534 | 363 | if (lis2mdl_self_test_set(&_reg_ctx, status) != 0) { |
cparata | 1:8562ae1a0534 | 364 | return 1; |
cparata | 1:8562ae1a0534 | 365 | } |
cparata | 0:671edf39d961 | 366 | |
cparata | 1:8562ae1a0534 | 367 | return 0; |
cparata | 0:671edf39d961 | 368 | } |
cparata | 0:671edf39d961 | 369 | |
cparata | 0:671edf39d961 | 370 | /** |
cparata | 0:671edf39d961 | 371 | * @brief Get the LIS2MDL MAG data ready bit value |
cparata | 0:671edf39d961 | 372 | * @param status the status of data ready bit |
cparata | 0:671edf39d961 | 373 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:671edf39d961 | 374 | */ |
cparata | 0:671edf39d961 | 375 | int LIS2MDLSensor::get_m_drdy_status(uint8_t *status) |
cparata | 0:671edf39d961 | 376 | { |
cparata | 1:8562ae1a0534 | 377 | if (lis2mdl_mag_data_ready_get(&_reg_ctx, status) != 0) { |
cparata | 1:8562ae1a0534 | 378 | return 1; |
cparata | 1:8562ae1a0534 | 379 | } |
cparata | 0:671edf39d961 | 380 | |
cparata | 1:8562ae1a0534 | 381 | return 0; |
cparata | 0:671edf39d961 | 382 | } |
cparata | 0:671edf39d961 | 383 | |
cparata | 0:671edf39d961 | 384 | |
cparata | 0:671edf39d961 | 385 | |
cparata | 0:671edf39d961 | 386 | int32_t LIS2MDL_io_write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite) |
cparata | 0:671edf39d961 | 387 | { |
cparata | 1:8562ae1a0534 | 388 | return ((LIS2MDLSensor *)handle)->io_write(pBuffer, WriteAddr, nBytesToWrite); |
cparata | 0:671edf39d961 | 389 | } |
cparata | 0:671edf39d961 | 390 | |
cparata | 0:671edf39d961 | 391 | int32_t LIS2MDL_io_read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead) |
cparata | 0:671edf39d961 | 392 | { |
cparata | 1:8562ae1a0534 | 393 | return ((LIS2MDLSensor *)handle)->io_read(pBuffer, ReadAddr, nBytesToRead); |
cparata | 0:671edf39d961 | 394 | } |