LIS2DW12 accelerometer sensor library
Dependencies: X_NUCLEO_COMMON ST_INTERFACES
LIS2DW12Sensor.cpp@5:b6ce53be44c2, 2019-09-17 (annotated)
- Committer:
- martlefebvre94
- Date:
- Tue Sep 17 08:40:17 2019 +0000
- Revision:
- 5:b6ce53be44c2
- Parent:
- 4:94c5d5546161
Addition of functions for the configuration of the accelerometer sensor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cparata | 0:dff8803aace7 | 1 | /** |
cparata | 0:dff8803aace7 | 2 | ****************************************************************************** |
cparata | 0:dff8803aace7 | 3 | * @file LIS2DW12Sensor.cpp |
cparata | 0:dff8803aace7 | 4 | * @author CLab |
cparata | 0:dff8803aace7 | 5 | * @version V1.0.0 |
cparata | 0:dff8803aace7 | 6 | * @date 15 November 2018 |
cparata | 0:dff8803aace7 | 7 | * @brief Implementation of an LIS2DW12 Inertial Measurement Unit (IMU) 3 axes |
cparata | 0:dff8803aace7 | 8 | * sensor. |
cparata | 0:dff8803aace7 | 9 | ****************************************************************************** |
cparata | 0:dff8803aace7 | 10 | * @attention |
cparata | 0:dff8803aace7 | 11 | * |
cparata | 0:dff8803aace7 | 12 | * <h2><center>© COPYRIGHT(c) 2018 STMicroelectronics</center></h2> |
cparata | 0:dff8803aace7 | 13 | * |
cparata | 0:dff8803aace7 | 14 | * Redistribution and use in source and binary forms, with or without modification, |
cparata | 0:dff8803aace7 | 15 | * are permitted provided that the following conditions are met: |
cparata | 0:dff8803aace7 | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
cparata | 0:dff8803aace7 | 17 | * this list of conditions and the following disclaimer. |
cparata | 0:dff8803aace7 | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
cparata | 0:dff8803aace7 | 19 | * this list of conditions and the following disclaimer in the documentation |
cparata | 0:dff8803aace7 | 20 | * and/or other materials provided with the distribution. |
cparata | 0:dff8803aace7 | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
cparata | 0:dff8803aace7 | 22 | * may be used to endorse or promote products derived from this software |
cparata | 0:dff8803aace7 | 23 | * without specific prior written permission. |
cparata | 0:dff8803aace7 | 24 | * |
cparata | 0:dff8803aace7 | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
cparata | 0:dff8803aace7 | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
cparata | 0:dff8803aace7 | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
cparata | 0:dff8803aace7 | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
cparata | 0:dff8803aace7 | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
cparata | 0:dff8803aace7 | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
cparata | 0:dff8803aace7 | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
cparata | 0:dff8803aace7 | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
cparata | 0:dff8803aace7 | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
cparata | 0:dff8803aace7 | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
cparata | 0:dff8803aace7 | 35 | * |
cparata | 0:dff8803aace7 | 36 | ****************************************************************************** |
cparata | 0:dff8803aace7 | 37 | */ |
cparata | 0:dff8803aace7 | 38 | |
cparata | 0:dff8803aace7 | 39 | |
cparata | 0:dff8803aace7 | 40 | /* Includes ------------------------------------------------------------------*/ |
cparata | 0:dff8803aace7 | 41 | |
cparata | 0:dff8803aace7 | 42 | #include "LIS2DW12Sensor.h" |
cparata | 0:dff8803aace7 | 43 | |
cparata | 0:dff8803aace7 | 44 | |
cparata | 0:dff8803aace7 | 45 | /* Class Implementation ------------------------------------------------------*/ |
cparata | 0:dff8803aace7 | 46 | |
cparata | 0:dff8803aace7 | 47 | /** Constructor |
cparata | 0:dff8803aace7 | 48 | * @param i2c object of an helper class which handles the I2C peripheral |
cparata | 0:dff8803aace7 | 49 | * @param address the address of the component's instance |
cparata | 0:dff8803aace7 | 50 | * @param int1_pin the interrupt 1 pin |
cparata | 0:dff8803aace7 | 51 | * @param int2_pin the interrupt 2 pin |
cparata | 0:dff8803aace7 | 52 | */ |
cparata | 0:dff8803aace7 | 53 | LIS2DW12Sensor::LIS2DW12Sensor(DevI2C *i2c, uint8_t address, PinName int1_pin, PinName int2_pin) : |
cparata | 4:94c5d5546161 | 54 | _dev_i2c(i2c), _address(address), _cs_pin(NC), _int1_irq(int1_pin), _int2_irq(int2_pin) |
cparata | 0:dff8803aace7 | 55 | { |
cparata | 4:94c5d5546161 | 56 | assert(i2c); |
cparata | 4:94c5d5546161 | 57 | _dev_spi = NULL; |
cparata | 4:94c5d5546161 | 58 | _reg_ctx.write_reg = LIS2DW12_io_write; |
cparata | 4:94c5d5546161 | 59 | _reg_ctx.read_reg = LIS2DW12_io_read; |
cparata | 4:94c5d5546161 | 60 | _reg_ctx.handle = (void *)this; |
cparata | 0:dff8803aace7 | 61 | } |
cparata | 0:dff8803aace7 | 62 | |
cparata | 0:dff8803aace7 | 63 | /** Constructor |
cparata | 0:dff8803aace7 | 64 | * @param spi object of an helper class which handles the SPI peripheral |
cparata | 0:dff8803aace7 | 65 | * @param cs_pin the chip select pin |
cparata | 0:dff8803aace7 | 66 | * @param int1_pin the interrupt 1 pin |
cparata | 0:dff8803aace7 | 67 | * @param int2_pin the interrupt 2 pin |
cparata | 0:dff8803aace7 | 68 | * @param spi_type the SPI type (4-Wires or 3-Wires) |
cparata | 0:dff8803aace7 | 69 | */ |
cparata | 4:94c5d5546161 | 70 | LIS2DW12Sensor::LIS2DW12Sensor(SPI *spi, PinName cs_pin, PinName int1_pin, PinName int2_pin, SPI_type_t spi_type) : |
cparata | 4:94c5d5546161 | 71 | _dev_spi(spi), _cs_pin(cs_pin), _int1_irq(int1_pin), _int2_irq(int2_pin), _spi_type(spi_type) |
cparata | 0:dff8803aace7 | 72 | { |
cparata | 4:94c5d5546161 | 73 | assert(spi); |
cparata | 4:94c5d5546161 | 74 | if (cs_pin == NC) { |
cparata | 4:94c5d5546161 | 75 | printf("ERROR LPS22HBSensor CS MUST NOT BE NC\n\r"); |
cparata | 4:94c5d5546161 | 76 | _dev_spi = NULL; |
cparata | 4:94c5d5546161 | 77 | _dev_i2c = NULL; |
cparata | 4:94c5d5546161 | 78 | return; |
cparata | 4:94c5d5546161 | 79 | } |
cparata | 4:94c5d5546161 | 80 | _reg_ctx.write_reg = LIS2DW12_io_write; |
cparata | 4:94c5d5546161 | 81 | _reg_ctx.read_reg = LIS2DW12_io_read; |
cparata | 4:94c5d5546161 | 82 | _reg_ctx.handle = (void *)this; |
cparata | 4:94c5d5546161 | 83 | _cs_pin = 1; |
cparata | 0:dff8803aace7 | 84 | _dev_i2c = NULL; |
cparata | 4:94c5d5546161 | 85 | _address = 0; |
cparata | 0:dff8803aace7 | 86 | |
cparata | 4:94c5d5546161 | 87 | if (_spi_type == SPI3W) { |
cparata | 4:94c5d5546161 | 88 | /* Enable SPI 3-Wires on the component */ |
cparata | 4:94c5d5546161 | 89 | uint8_t data = 0x05; |
cparata | 4:94c5d5546161 | 90 | lis2dw12_write_reg(&_reg_ctx, LIS2DW12_CTRL2, &data, 1); |
cparata | 4:94c5d5546161 | 91 | } |
cparata | 4:94c5d5546161 | 92 | |
cparata | 4:94c5d5546161 | 93 | /* Disable I2C on the component */ |
cparata | 4:94c5d5546161 | 94 | lis2dw12_i2c_interface_set(&_reg_ctx, LIS2DW12_I2C_DISABLE); |
cparata | 0:dff8803aace7 | 95 | } |
cparata | 0:dff8803aace7 | 96 | |
cparata | 0:dff8803aace7 | 97 | /** |
cparata | 0:dff8803aace7 | 98 | * @brief Initializing the component. |
cparata | 0:dff8803aace7 | 99 | * @param[in] init pointer to device specific initalization structure. |
cparata | 0:dff8803aace7 | 100 | * @retval "0" in case of success, an error code otherwise. |
cparata | 0:dff8803aace7 | 101 | */ |
cparata | 0:dff8803aace7 | 102 | int LIS2DW12Sensor::init(void *init) |
cparata | 0:dff8803aace7 | 103 | { |
cparata | 4:94c5d5546161 | 104 | /* Enable register address automatically incremented during a multiple byte |
cparata | 4:94c5d5546161 | 105 | access with a serial interface. */ |
cparata | 4:94c5d5546161 | 106 | if (lis2dw12_auto_increment_set(&_reg_ctx, PROPERTY_ENABLE) != 0) { |
cparata | 4:94c5d5546161 | 107 | return 1; |
cparata | 4:94c5d5546161 | 108 | } |
cparata | 0:dff8803aace7 | 109 | |
cparata | 4:94c5d5546161 | 110 | /* Enable BDU */ |
cparata | 4:94c5d5546161 | 111 | if (lis2dw12_block_data_update_set(&_reg_ctx, PROPERTY_ENABLE) != 0) { |
cparata | 4:94c5d5546161 | 112 | return 1; |
cparata | 4:94c5d5546161 | 113 | } |
cparata | 0:dff8803aace7 | 114 | |
cparata | 4:94c5d5546161 | 115 | /* FIFO mode selection */ |
cparata | 4:94c5d5546161 | 116 | if (lis2dw12_fifo_mode_set(&_reg_ctx, LIS2DW12_BYPASS_MODE) != 0) { |
cparata | 4:94c5d5546161 | 117 | return 1; |
cparata | 4:94c5d5546161 | 118 | } |
cparata | 4:94c5d5546161 | 119 | |
cparata | 4:94c5d5546161 | 120 | /* Power mode selection */ |
cparata | 4:94c5d5546161 | 121 | if (lis2dw12_power_mode_set(&_reg_ctx, LIS2DW12_HIGH_PERFORMANCE) != 0) { |
cparata | 4:94c5d5546161 | 122 | return 1; |
cparata | 4:94c5d5546161 | 123 | } |
cparata | 0:dff8803aace7 | 124 | |
cparata | 4:94c5d5546161 | 125 | /* Output data rate selection - power down. */ |
cparata | 4:94c5d5546161 | 126 | if (lis2dw12_data_rate_set(&_reg_ctx, LIS2DW12_XL_ODR_OFF) != 0) { |
cparata | 4:94c5d5546161 | 127 | return 1; |
cparata | 4:94c5d5546161 | 128 | } |
cparata | 0:dff8803aace7 | 129 | |
cparata | 4:94c5d5546161 | 130 | /* Full scale selection. */ |
cparata | 4:94c5d5546161 | 131 | if (lis2dw12_full_scale_set(&_reg_ctx, LIS2DW12_2g) != 0) { |
cparata | 4:94c5d5546161 | 132 | return 1; |
cparata | 4:94c5d5546161 | 133 | } |
cparata | 0:dff8803aace7 | 134 | |
cparata | 4:94c5d5546161 | 135 | /* Select default output data rate. */ |
cparata | 4:94c5d5546161 | 136 | _x_last_odr = 100.0f; |
cparata | 4:94c5d5546161 | 137 | |
cparata | 4:94c5d5546161 | 138 | _x_last_operating_mode = LIS2DW12_HIGH_PERFORMANCE_MODE; |
cparata | 0:dff8803aace7 | 139 | |
cparata | 4:94c5d5546161 | 140 | _x_last_noise = LIS2DW12_LOW_NOISE_DISABLE; |
cparata | 0:dff8803aace7 | 141 | |
cparata | 4:94c5d5546161 | 142 | _x_is_enabled = 0; |
cparata | 0:dff8803aace7 | 143 | |
cparata | 4:94c5d5546161 | 144 | return 0; |
cparata | 0:dff8803aace7 | 145 | } |
cparata | 0:dff8803aace7 | 146 | |
cparata | 0:dff8803aace7 | 147 | /** |
cparata | 0:dff8803aace7 | 148 | * @brief Enable LIS2DW12 Accelerator |
cparata | 0:dff8803aace7 | 149 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:dff8803aace7 | 150 | */ |
cparata | 0:dff8803aace7 | 151 | int LIS2DW12Sensor::enable_x(void) |
cparata | 4:94c5d5546161 | 152 | { |
cparata | 4:94c5d5546161 | 153 | /* Check if the component is already enabled */ |
cparata | 4:94c5d5546161 | 154 | if (_x_is_enabled == 1) { |
cparata | 4:94c5d5546161 | 155 | return 0; |
cparata | 4:94c5d5546161 | 156 | } |
cparata | 4:94c5d5546161 | 157 | |
cparata | 4:94c5d5546161 | 158 | /* Output data rate selection. */ |
cparata | 4:94c5d5546161 | 159 | if (set_x_odr_when_enabled(_x_last_odr, _x_last_operating_mode, _x_last_noise) == 1) { |
cparata | 4:94c5d5546161 | 160 | return 1; |
cparata | 4:94c5d5546161 | 161 | } |
cparata | 4:94c5d5546161 | 162 | |
cparata | 4:94c5d5546161 | 163 | _x_is_enabled = 1; |
cparata | 4:94c5d5546161 | 164 | |
cparata | 0:dff8803aace7 | 165 | return 0; |
cparata | 0:dff8803aace7 | 166 | } |
cparata | 0:dff8803aace7 | 167 | |
cparata | 0:dff8803aace7 | 168 | /** |
cparata | 0:dff8803aace7 | 169 | * @brief Disable LIS2DW12 Accelerator |
cparata | 0:dff8803aace7 | 170 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:dff8803aace7 | 171 | */ |
cparata | 0:dff8803aace7 | 172 | int LIS2DW12Sensor::disable_x(void) |
cparata | 4:94c5d5546161 | 173 | { |
cparata | 4:94c5d5546161 | 174 | /* Check if the component is already disabled */ |
cparata | 4:94c5d5546161 | 175 | if (_x_is_enabled == 0) { |
cparata | 4:94c5d5546161 | 176 | return 0; |
cparata | 4:94c5d5546161 | 177 | } |
cparata | 0:dff8803aace7 | 178 | |
cparata | 4:94c5d5546161 | 179 | /* Output data rate selection - power down. */ |
cparata | 4:94c5d5546161 | 180 | if (lis2dw12_data_rate_set(&_reg_ctx, LIS2DW12_XL_ODR_OFF) != 0) { |
cparata | 4:94c5d5546161 | 181 | return 1; |
cparata | 4:94c5d5546161 | 182 | } |
cparata | 4:94c5d5546161 | 183 | |
cparata | 4:94c5d5546161 | 184 | _x_is_enabled = 0; |
cparata | 4:94c5d5546161 | 185 | |
cparata | 4:94c5d5546161 | 186 | return 0; |
cparata | 0:dff8803aace7 | 187 | } |
cparata | 0:dff8803aace7 | 188 | |
cparata | 0:dff8803aace7 | 189 | /** |
cparata | 0:dff8803aace7 | 190 | * @brief Read ID of LIS2DW12 Accelerometer and Gyroscope |
cparata | 0:dff8803aace7 | 191 | * @param p_id the pointer where the ID of the device is stored |
cparata | 0:dff8803aace7 | 192 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:dff8803aace7 | 193 | */ |
cparata | 0:dff8803aace7 | 194 | int LIS2DW12Sensor::read_id(uint8_t *id) |
cparata | 0:dff8803aace7 | 195 | { |
cparata | 4:94c5d5546161 | 196 | if (!id) { |
cparata | 4:94c5d5546161 | 197 | return 1; |
cparata | 4:94c5d5546161 | 198 | } |
cparata | 0:dff8803aace7 | 199 | |
cparata | 4:94c5d5546161 | 200 | /* Read WHO AM I register */ |
cparata | 4:94c5d5546161 | 201 | if (lis2dw12_device_id_get(&_reg_ctx, id) != 0) { |
cparata | 4:94c5d5546161 | 202 | return 1; |
cparata | 4:94c5d5546161 | 203 | } |
cparata | 0:dff8803aace7 | 204 | |
cparata | 4:94c5d5546161 | 205 | return 0; |
cparata | 0:dff8803aace7 | 206 | } |
cparata | 0:dff8803aace7 | 207 | |
cparata | 0:dff8803aace7 | 208 | /** |
cparata | 0:dff8803aace7 | 209 | * @brief Read data from LIS2DW12 Accelerometer |
cparata | 0:dff8803aace7 | 210 | * @param acceleration the pointer where the accelerometer data are stored |
cparata | 0:dff8803aace7 | 211 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:dff8803aace7 | 212 | */ |
cparata | 0:dff8803aace7 | 213 | int LIS2DW12Sensor::get_x_axes(int32_t *acceleration) |
cparata | 0:dff8803aace7 | 214 | { |
cparata | 4:94c5d5546161 | 215 | int16_t data_raw[3]; |
cparata | 4:94c5d5546161 | 216 | float sensitivity = 0; |
cparata | 4:94c5d5546161 | 217 | |
cparata | 4:94c5d5546161 | 218 | /* Read raw data from LIS2DW12 output register. */ |
cparata | 4:94c5d5546161 | 219 | if (get_x_axes_raw(data_raw) == 1) { |
cparata | 4:94c5d5546161 | 220 | return 1; |
cparata | 4:94c5d5546161 | 221 | } |
cparata | 4:94c5d5546161 | 222 | |
cparata | 4:94c5d5546161 | 223 | /* Get LIS2DW12 actual sensitivity. */ |
cparata | 4:94c5d5546161 | 224 | if (get_x_sensitivity(&sensitivity) == 1) { |
cparata | 4:94c5d5546161 | 225 | return 1; |
cparata | 4:94c5d5546161 | 226 | } |
cparata | 4:94c5d5546161 | 227 | |
cparata | 4:94c5d5546161 | 228 | /* Calculate the data. */ |
cparata | 4:94c5d5546161 | 229 | acceleration[0] = (int32_t)(data_raw[0] * sensitivity); |
cparata | 4:94c5d5546161 | 230 | acceleration[1] = (int32_t)(data_raw[1] * sensitivity); |
cparata | 4:94c5d5546161 | 231 | acceleration[2] = (int32_t)(data_raw[2] * sensitivity); |
cparata | 4:94c5d5546161 | 232 | |
cparata | 4:94c5d5546161 | 233 | return 0; |
cparata | 0:dff8803aace7 | 234 | } |
cparata | 0:dff8803aace7 | 235 | |
cparata | 0:dff8803aace7 | 236 | /** |
cparata | 0:dff8803aace7 | 237 | * @brief Read Accelerometer Sensitivity |
cparata | 0:dff8803aace7 | 238 | * @param sensitivity the pointer where the accelerometer sensitivity is stored |
cparata | 0:dff8803aace7 | 239 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:dff8803aace7 | 240 | */ |
cparata | 0:dff8803aace7 | 241 | int LIS2DW12Sensor::get_x_sensitivity(float *sensitivity) |
cparata | 0:dff8803aace7 | 242 | { |
cparata | 4:94c5d5546161 | 243 | int32_t ret = 0; |
cparata | 4:94c5d5546161 | 244 | lis2dw12_fs_t full_scale; |
cparata | 4:94c5d5546161 | 245 | lis2dw12_mode_t mode; |
cparata | 4:94c5d5546161 | 246 | |
cparata | 4:94c5d5546161 | 247 | /* Read actual full scale selection from sensor. */ |
cparata | 4:94c5d5546161 | 248 | if (lis2dw12_full_scale_get(&_reg_ctx, &full_scale) != 0) { |
cparata | 4:94c5d5546161 | 249 | return 1; |
cparata | 4:94c5d5546161 | 250 | } |
cparata | 4:94c5d5546161 | 251 | |
cparata | 4:94c5d5546161 | 252 | /* Read actual power mode selection from sensor. */ |
cparata | 4:94c5d5546161 | 253 | if (lis2dw12_power_mode_get(&_reg_ctx, &mode) != 0) { |
cparata | 4:94c5d5546161 | 254 | return 1; |
cparata | 4:94c5d5546161 | 255 | } |
cparata | 0:dff8803aace7 | 256 | |
cparata | 4:94c5d5546161 | 257 | switch (mode) { |
cparata | 4:94c5d5546161 | 258 | case LIS2DW12_CONT_LOW_PWR_12bit: |
cparata | 4:94c5d5546161 | 259 | case LIS2DW12_SINGLE_LOW_PWR_12bit: |
cparata | 4:94c5d5546161 | 260 | case LIS2DW12_CONT_LOW_PWR_LOW_NOISE_12bit: |
cparata | 4:94c5d5546161 | 261 | case LIS2DW12_SINGLE_LOW_LOW_NOISE_PWR_12bit: |
cparata | 4:94c5d5546161 | 262 | switch (full_scale) { |
cparata | 4:94c5d5546161 | 263 | case LIS2DW12_2g: |
cparata | 4:94c5d5546161 | 264 | *sensitivity = LIS2DW12_ACC_SENSITIVITY_FOR_FS_2G_LOPOW1_MODE; |
cparata | 4:94c5d5546161 | 265 | break; |
cparata | 0:dff8803aace7 | 266 | |
cparata | 4:94c5d5546161 | 267 | case LIS2DW12_4g: |
cparata | 4:94c5d5546161 | 268 | *sensitivity = LIS2DW12_ACC_SENSITIVITY_FOR_FS_4G_LOPOW1_MODE; |
cparata | 4:94c5d5546161 | 269 | break; |
cparata | 4:94c5d5546161 | 270 | |
cparata | 4:94c5d5546161 | 271 | case LIS2DW12_8g: |
cparata | 4:94c5d5546161 | 272 | *sensitivity = LIS2DW12_ACC_SENSITIVITY_FOR_FS_8G_LOPOW1_MODE; |
cparata | 4:94c5d5546161 | 273 | break; |
cparata | 4:94c5d5546161 | 274 | |
cparata | 4:94c5d5546161 | 275 | case LIS2DW12_16g: |
cparata | 4:94c5d5546161 | 276 | *sensitivity = LIS2DW12_ACC_SENSITIVITY_FOR_FS_16G_LOPOW1_MODE; |
cparata | 4:94c5d5546161 | 277 | break; |
cparata | 0:dff8803aace7 | 278 | |
cparata | 4:94c5d5546161 | 279 | default: |
cparata | 4:94c5d5546161 | 280 | *sensitivity = -1.0f; |
cparata | 4:94c5d5546161 | 281 | ret = 1; |
cparata | 4:94c5d5546161 | 282 | break; |
cparata | 4:94c5d5546161 | 283 | } |
cparata | 4:94c5d5546161 | 284 | break; |
cparata | 0:dff8803aace7 | 285 | |
cparata | 4:94c5d5546161 | 286 | case LIS2DW12_HIGH_PERFORMANCE: |
cparata | 4:94c5d5546161 | 287 | case LIS2DW12_CONT_LOW_PWR_4: |
cparata | 4:94c5d5546161 | 288 | case LIS2DW12_CONT_LOW_PWR_3: |
cparata | 4:94c5d5546161 | 289 | case LIS2DW12_CONT_LOW_PWR_2: |
cparata | 4:94c5d5546161 | 290 | case LIS2DW12_SINGLE_LOW_PWR_4: |
cparata | 4:94c5d5546161 | 291 | case LIS2DW12_SINGLE_LOW_PWR_3: |
cparata | 4:94c5d5546161 | 292 | case LIS2DW12_SINGLE_LOW_PWR_2: |
cparata | 4:94c5d5546161 | 293 | case LIS2DW12_HIGH_PERFORMANCE_LOW_NOISE: |
cparata | 4:94c5d5546161 | 294 | case LIS2DW12_CONT_LOW_PWR_LOW_NOISE_4: |
cparata | 4:94c5d5546161 | 295 | case LIS2DW12_CONT_LOW_PWR_LOW_NOISE_3: |
cparata | 4:94c5d5546161 | 296 | case LIS2DW12_CONT_LOW_PWR_LOW_NOISE_2: |
cparata | 4:94c5d5546161 | 297 | case LIS2DW12_SINGLE_LOW_PWR_LOW_NOISE_4: |
cparata | 4:94c5d5546161 | 298 | case LIS2DW12_SINGLE_LOW_PWR_LOW_NOISE_3: |
cparata | 4:94c5d5546161 | 299 | case LIS2DW12_SINGLE_LOW_PWR_LOW_NOISE_2: |
cparata | 4:94c5d5546161 | 300 | switch (full_scale) { |
cparata | 4:94c5d5546161 | 301 | case LIS2DW12_2g: |
cparata | 4:94c5d5546161 | 302 | *sensitivity = LIS2DW12_ACC_SENSITIVITY_FOR_FS_2G_OTHER_MODES; |
cparata | 4:94c5d5546161 | 303 | break; |
cparata | 0:dff8803aace7 | 304 | |
cparata | 4:94c5d5546161 | 305 | case LIS2DW12_4g: |
cparata | 4:94c5d5546161 | 306 | *sensitivity = LIS2DW12_ACC_SENSITIVITY_FOR_FS_4G_OTHER_MODES; |
cparata | 4:94c5d5546161 | 307 | break; |
cparata | 4:94c5d5546161 | 308 | |
cparata | 4:94c5d5546161 | 309 | case LIS2DW12_8g: |
cparata | 4:94c5d5546161 | 310 | *sensitivity = LIS2DW12_ACC_SENSITIVITY_FOR_FS_8G_OTHER_MODES; |
cparata | 4:94c5d5546161 | 311 | break; |
cparata | 0:dff8803aace7 | 312 | |
cparata | 4:94c5d5546161 | 313 | case LIS2DW12_16g: |
cparata | 4:94c5d5546161 | 314 | *sensitivity = LIS2DW12_ACC_SENSITIVITY_FOR_FS_16G_OTHER_MODES; |
cparata | 4:94c5d5546161 | 315 | break; |
cparata | 4:94c5d5546161 | 316 | |
cparata | 4:94c5d5546161 | 317 | default: |
cparata | 4:94c5d5546161 | 318 | *sensitivity = -1.0f; |
cparata | 4:94c5d5546161 | 319 | ret = 1; |
cparata | 4:94c5d5546161 | 320 | break; |
cparata | 4:94c5d5546161 | 321 | } |
cparata | 4:94c5d5546161 | 322 | break; |
cparata | 0:dff8803aace7 | 323 | |
cparata | 0:dff8803aace7 | 324 | default: |
cparata | 4:94c5d5546161 | 325 | *sensitivity = -1.0f; |
cparata | 4:94c5d5546161 | 326 | ret = 1; |
cparata | 4:94c5d5546161 | 327 | break; |
cparata | 4:94c5d5546161 | 328 | } |
cparata | 0:dff8803aace7 | 329 | |
cparata | 4:94c5d5546161 | 330 | return ret; |
cparata | 0:dff8803aace7 | 331 | } |
cparata | 0:dff8803aace7 | 332 | |
cparata | 0:dff8803aace7 | 333 | /** |
cparata | 0:dff8803aace7 | 334 | * @brief Read raw data from LIS2DW12 Accelerometer |
cparata | 0:dff8803aace7 | 335 | * @param value the pointer where the accelerometer raw data are stored |
cparata | 0:dff8803aace7 | 336 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:dff8803aace7 | 337 | */ |
cparata | 0:dff8803aace7 | 338 | int LIS2DW12Sensor::get_x_axes_raw(int16_t *value) |
cparata | 0:dff8803aace7 | 339 | { |
cparata | 4:94c5d5546161 | 340 | axis3bit16_t data_raw; |
cparata | 4:94c5d5546161 | 341 | lis2dw12_mode_t mode; |
cparata | 4:94c5d5546161 | 342 | int32_t ret = 0; |
cparata | 4:94c5d5546161 | 343 | |
cparata | 4:94c5d5546161 | 344 | /* Read actual power mode selection from sensor. */ |
cparata | 4:94c5d5546161 | 345 | if (lis2dw12_power_mode_get(&_reg_ctx, &mode) != 0) { |
cparata | 4:94c5d5546161 | 346 | return 1; |
cparata | 4:94c5d5546161 | 347 | } |
cparata | 0:dff8803aace7 | 348 | |
cparata | 4:94c5d5546161 | 349 | /* Read raw data values. */ |
cparata | 4:94c5d5546161 | 350 | if (lis2dw12_acceleration_raw_get(&_reg_ctx, data_raw.u8bit) != 0) { |
cparata | 4:94c5d5546161 | 351 | return 1; |
cparata | 4:94c5d5546161 | 352 | } |
cparata | 0:dff8803aace7 | 353 | |
cparata | 4:94c5d5546161 | 354 | switch (mode) { |
cparata | 4:94c5d5546161 | 355 | case LIS2DW12_CONT_LOW_PWR_12bit: |
cparata | 4:94c5d5546161 | 356 | case LIS2DW12_SINGLE_LOW_PWR_12bit: |
cparata | 4:94c5d5546161 | 357 | case LIS2DW12_CONT_LOW_PWR_LOW_NOISE_12bit: |
cparata | 4:94c5d5546161 | 358 | case LIS2DW12_SINGLE_LOW_LOW_NOISE_PWR_12bit: |
cparata | 4:94c5d5546161 | 359 | /* Data format 12 bits. */ |
cparata | 4:94c5d5546161 | 360 | value[0] = (data_raw.i16bit[0] / 16); |
cparata | 4:94c5d5546161 | 361 | value[1] = (data_raw.i16bit[1] / 16); |
cparata | 4:94c5d5546161 | 362 | value[2] = (data_raw.i16bit[2] / 16); |
cparata | 4:94c5d5546161 | 363 | break; |
cparata | 0:dff8803aace7 | 364 | |
cparata | 4:94c5d5546161 | 365 | case LIS2DW12_HIGH_PERFORMANCE: |
cparata | 4:94c5d5546161 | 366 | case LIS2DW12_CONT_LOW_PWR_4: |
cparata | 4:94c5d5546161 | 367 | case LIS2DW12_CONT_LOW_PWR_3: |
cparata | 4:94c5d5546161 | 368 | case LIS2DW12_CONT_LOW_PWR_2: |
cparata | 4:94c5d5546161 | 369 | case LIS2DW12_SINGLE_LOW_PWR_4: |
cparata | 4:94c5d5546161 | 370 | case LIS2DW12_SINGLE_LOW_PWR_3: |
cparata | 4:94c5d5546161 | 371 | case LIS2DW12_SINGLE_LOW_PWR_2: |
cparata | 4:94c5d5546161 | 372 | case LIS2DW12_HIGH_PERFORMANCE_LOW_NOISE: |
cparata | 4:94c5d5546161 | 373 | case LIS2DW12_CONT_LOW_PWR_LOW_NOISE_4: |
cparata | 4:94c5d5546161 | 374 | case LIS2DW12_CONT_LOW_PWR_LOW_NOISE_3: |
cparata | 4:94c5d5546161 | 375 | case LIS2DW12_CONT_LOW_PWR_LOW_NOISE_2: |
cparata | 4:94c5d5546161 | 376 | case LIS2DW12_SINGLE_LOW_PWR_LOW_NOISE_4: |
cparata | 4:94c5d5546161 | 377 | case LIS2DW12_SINGLE_LOW_PWR_LOW_NOISE_3: |
cparata | 4:94c5d5546161 | 378 | case LIS2DW12_SINGLE_LOW_PWR_LOW_NOISE_2: |
cparata | 4:94c5d5546161 | 379 | /* Data format 14 bits. */ |
cparata | 4:94c5d5546161 | 380 | value[0] = (data_raw.i16bit[0] / 4); |
cparata | 4:94c5d5546161 | 381 | value[1] = (data_raw.i16bit[1] / 4); |
cparata | 4:94c5d5546161 | 382 | value[2] = (data_raw.i16bit[2] / 4); |
cparata | 4:94c5d5546161 | 383 | break; |
cparata | 0:dff8803aace7 | 384 | |
cparata | 4:94c5d5546161 | 385 | default: |
cparata | 4:94c5d5546161 | 386 | ret = 1; |
cparata | 4:94c5d5546161 | 387 | break; |
cparata | 4:94c5d5546161 | 388 | } |
cparata | 0:dff8803aace7 | 389 | |
cparata | 4:94c5d5546161 | 390 | return ret; |
cparata | 0:dff8803aace7 | 391 | } |
cparata | 0:dff8803aace7 | 392 | |
cparata | 0:dff8803aace7 | 393 | /** |
cparata | 0:dff8803aace7 | 394 | * @brief Read LIS2DW12 Accelerometer output data rate |
cparata | 0:dff8803aace7 | 395 | * @param odr the pointer to the output data rate |
cparata | 0:dff8803aace7 | 396 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:dff8803aace7 | 397 | */ |
cparata | 4:94c5d5546161 | 398 | int LIS2DW12Sensor::get_x_odr(float *odr) |
cparata | 0:dff8803aace7 | 399 | { |
cparata | 4:94c5d5546161 | 400 | int32_t ret = 0; |
cparata | 4:94c5d5546161 | 401 | lis2dw12_odr_t odr_low_level; |
cparata | 4:94c5d5546161 | 402 | lis2dw12_mode_t mode; |
cparata | 4:94c5d5546161 | 403 | |
cparata | 4:94c5d5546161 | 404 | /* Get current output data rate. */ |
cparata | 4:94c5d5546161 | 405 | if (lis2dw12_data_rate_get(&_reg_ctx, &odr_low_level) != 0) { |
cparata | 4:94c5d5546161 | 406 | return 1; |
cparata | 4:94c5d5546161 | 407 | } |
cparata | 0:dff8803aace7 | 408 | |
cparata | 4:94c5d5546161 | 409 | /* Read actual power mode selection from sensor. */ |
cparata | 4:94c5d5546161 | 410 | if (lis2dw12_power_mode_get(&_reg_ctx, &mode) != 0) { |
cparata | 4:94c5d5546161 | 411 | return 1; |
cparata | 4:94c5d5546161 | 412 | } |
cparata | 4:94c5d5546161 | 413 | |
cparata | 4:94c5d5546161 | 414 | switch (odr_low_level) { |
cparata | 4:94c5d5546161 | 415 | case LIS2DW12_XL_ODR_OFF: |
cparata | 4:94c5d5546161 | 416 | case LIS2DW12_XL_SET_SW_TRIG: |
cparata | 4:94c5d5546161 | 417 | case LIS2DW12_XL_SET_PIN_TRIG: |
cparata | 4:94c5d5546161 | 418 | *odr = 0.0f; |
cparata | 4:94c5d5546161 | 419 | break; |
cparata | 4:94c5d5546161 | 420 | |
cparata | 4:94c5d5546161 | 421 | case LIS2DW12_XL_ODR_1Hz6_LP_ONLY: |
cparata | 4:94c5d5546161 | 422 | switch (mode) { |
cparata | 4:94c5d5546161 | 423 | case LIS2DW12_HIGH_PERFORMANCE: |
cparata | 4:94c5d5546161 | 424 | case LIS2DW12_HIGH_PERFORMANCE_LOW_NOISE: |
cparata | 4:94c5d5546161 | 425 | *odr = 12.5f; |
cparata | 4:94c5d5546161 | 426 | break; |
cparata | 0:dff8803aace7 | 427 | |
cparata | 4:94c5d5546161 | 428 | case LIS2DW12_CONT_LOW_PWR_4: |
cparata | 4:94c5d5546161 | 429 | case LIS2DW12_CONT_LOW_PWR_3: |
cparata | 4:94c5d5546161 | 430 | case LIS2DW12_CONT_LOW_PWR_2: |
cparata | 4:94c5d5546161 | 431 | case LIS2DW12_CONT_LOW_PWR_12bit: |
cparata | 4:94c5d5546161 | 432 | case LIS2DW12_SINGLE_LOW_PWR_4: |
cparata | 4:94c5d5546161 | 433 | case LIS2DW12_SINGLE_LOW_PWR_3: |
cparata | 4:94c5d5546161 | 434 | case LIS2DW12_SINGLE_LOW_PWR_2: |
cparata | 4:94c5d5546161 | 435 | case LIS2DW12_SINGLE_LOW_PWR_12bit: |
cparata | 4:94c5d5546161 | 436 | case LIS2DW12_CONT_LOW_PWR_LOW_NOISE_4: |
cparata | 4:94c5d5546161 | 437 | case LIS2DW12_CONT_LOW_PWR_LOW_NOISE_3: |
cparata | 4:94c5d5546161 | 438 | case LIS2DW12_CONT_LOW_PWR_LOW_NOISE_2: |
cparata | 4:94c5d5546161 | 439 | case LIS2DW12_CONT_LOW_PWR_LOW_NOISE_12bit: |
cparata | 4:94c5d5546161 | 440 | case LIS2DW12_SINGLE_LOW_PWR_LOW_NOISE_4: |
cparata | 4:94c5d5546161 | 441 | case LIS2DW12_SINGLE_LOW_PWR_LOW_NOISE_3: |
cparata | 4:94c5d5546161 | 442 | case LIS2DW12_SINGLE_LOW_PWR_LOW_NOISE_2: |
cparata | 4:94c5d5546161 | 443 | case LIS2DW12_SINGLE_LOW_LOW_NOISE_PWR_12bit: |
cparata | 4:94c5d5546161 | 444 | *odr = 1.6f; |
cparata | 4:94c5d5546161 | 445 | break; |
cparata | 4:94c5d5546161 | 446 | |
cparata | 4:94c5d5546161 | 447 | default: |
cparata | 4:94c5d5546161 | 448 | *odr = -1.0f; |
cparata | 4:94c5d5546161 | 449 | ret = 1; |
cparata | 4:94c5d5546161 | 450 | break; |
cparata | 4:94c5d5546161 | 451 | } |
cparata | 4:94c5d5546161 | 452 | break; |
cparata | 0:dff8803aace7 | 453 | |
cparata | 4:94c5d5546161 | 454 | case LIS2DW12_XL_ODR_12Hz5: |
cparata | 4:94c5d5546161 | 455 | *odr = 12.5f; |
cparata | 4:94c5d5546161 | 456 | break; |
cparata | 4:94c5d5546161 | 457 | |
cparata | 4:94c5d5546161 | 458 | case LIS2DW12_XL_ODR_25Hz: |
cparata | 4:94c5d5546161 | 459 | *odr = 25.0f; |
cparata | 4:94c5d5546161 | 460 | break; |
cparata | 4:94c5d5546161 | 461 | |
cparata | 4:94c5d5546161 | 462 | case LIS2DW12_XL_ODR_50Hz: |
cparata | 4:94c5d5546161 | 463 | *odr = 50.0f; |
cparata | 4:94c5d5546161 | 464 | break; |
cparata | 4:94c5d5546161 | 465 | |
cparata | 4:94c5d5546161 | 466 | case LIS2DW12_XL_ODR_100Hz: |
cparata | 4:94c5d5546161 | 467 | *odr = 100.0f; |
cparata | 4:94c5d5546161 | 468 | break; |
cparata | 4:94c5d5546161 | 469 | |
cparata | 4:94c5d5546161 | 470 | case LIS2DW12_XL_ODR_200Hz: |
cparata | 4:94c5d5546161 | 471 | *odr = 200.0f; |
cparata | 4:94c5d5546161 | 472 | break; |
cparata | 4:94c5d5546161 | 473 | |
cparata | 4:94c5d5546161 | 474 | case LIS2DW12_XL_ODR_400Hz: |
cparata | 4:94c5d5546161 | 475 | switch (mode) { |
cparata | 4:94c5d5546161 | 476 | case LIS2DW12_HIGH_PERFORMANCE: |
cparata | 4:94c5d5546161 | 477 | case LIS2DW12_HIGH_PERFORMANCE_LOW_NOISE: |
cparata | 4:94c5d5546161 | 478 | *odr = 400.0f; |
cparata | 4:94c5d5546161 | 479 | break; |
cparata | 0:dff8803aace7 | 480 | |
cparata | 4:94c5d5546161 | 481 | case LIS2DW12_CONT_LOW_PWR_4: |
cparata | 4:94c5d5546161 | 482 | case LIS2DW12_CONT_LOW_PWR_3: |
cparata | 4:94c5d5546161 | 483 | case LIS2DW12_CONT_LOW_PWR_2: |
cparata | 4:94c5d5546161 | 484 | case LIS2DW12_CONT_LOW_PWR_12bit: |
cparata | 4:94c5d5546161 | 485 | case LIS2DW12_SINGLE_LOW_PWR_4: |
cparata | 4:94c5d5546161 | 486 | case LIS2DW12_SINGLE_LOW_PWR_3: |
cparata | 4:94c5d5546161 | 487 | case LIS2DW12_SINGLE_LOW_PWR_2: |
cparata | 4:94c5d5546161 | 488 | case LIS2DW12_SINGLE_LOW_PWR_12bit: |
cparata | 4:94c5d5546161 | 489 | case LIS2DW12_CONT_LOW_PWR_LOW_NOISE_4: |
cparata | 4:94c5d5546161 | 490 | case LIS2DW12_CONT_LOW_PWR_LOW_NOISE_3: |
cparata | 4:94c5d5546161 | 491 | case LIS2DW12_CONT_LOW_PWR_LOW_NOISE_2: |
cparata | 4:94c5d5546161 | 492 | case LIS2DW12_CONT_LOW_PWR_LOW_NOISE_12bit: |
cparata | 4:94c5d5546161 | 493 | case LIS2DW12_SINGLE_LOW_PWR_LOW_NOISE_4: |
cparata | 4:94c5d5546161 | 494 | case LIS2DW12_SINGLE_LOW_PWR_LOW_NOISE_3: |
cparata | 4:94c5d5546161 | 495 | case LIS2DW12_SINGLE_LOW_PWR_LOW_NOISE_2: |
cparata | 4:94c5d5546161 | 496 | case LIS2DW12_SINGLE_LOW_LOW_NOISE_PWR_12bit: |
cparata | 4:94c5d5546161 | 497 | *odr = 200.0f; |
cparata | 4:94c5d5546161 | 498 | break; |
cparata | 4:94c5d5546161 | 499 | |
cparata | 4:94c5d5546161 | 500 | default: |
cparata | 4:94c5d5546161 | 501 | *odr = -1.0f; |
cparata | 4:94c5d5546161 | 502 | ret = 1; |
cparata | 4:94c5d5546161 | 503 | break; |
cparata | 4:94c5d5546161 | 504 | } |
cparata | 4:94c5d5546161 | 505 | break; |
cparata | 4:94c5d5546161 | 506 | |
cparata | 4:94c5d5546161 | 507 | case LIS2DW12_XL_ODR_800Hz: |
cparata | 4:94c5d5546161 | 508 | switch (mode) { |
cparata | 4:94c5d5546161 | 509 | case LIS2DW12_HIGH_PERFORMANCE: |
cparata | 4:94c5d5546161 | 510 | case LIS2DW12_HIGH_PERFORMANCE_LOW_NOISE: |
cparata | 4:94c5d5546161 | 511 | *odr = 800.0f; |
cparata | 4:94c5d5546161 | 512 | break; |
cparata | 0:dff8803aace7 | 513 | |
cparata | 4:94c5d5546161 | 514 | case LIS2DW12_CONT_LOW_PWR_4: |
cparata | 4:94c5d5546161 | 515 | case LIS2DW12_CONT_LOW_PWR_3: |
cparata | 4:94c5d5546161 | 516 | case LIS2DW12_CONT_LOW_PWR_2: |
cparata | 4:94c5d5546161 | 517 | case LIS2DW12_CONT_LOW_PWR_12bit: |
cparata | 4:94c5d5546161 | 518 | case LIS2DW12_SINGLE_LOW_PWR_4: |
cparata | 4:94c5d5546161 | 519 | case LIS2DW12_SINGLE_LOW_PWR_3: |
cparata | 4:94c5d5546161 | 520 | case LIS2DW12_SINGLE_LOW_PWR_2: |
cparata | 4:94c5d5546161 | 521 | case LIS2DW12_SINGLE_LOW_PWR_12bit: |
cparata | 4:94c5d5546161 | 522 | case LIS2DW12_CONT_LOW_PWR_LOW_NOISE_4: |
cparata | 4:94c5d5546161 | 523 | case LIS2DW12_CONT_LOW_PWR_LOW_NOISE_3: |
cparata | 4:94c5d5546161 | 524 | case LIS2DW12_CONT_LOW_PWR_LOW_NOISE_2: |
cparata | 4:94c5d5546161 | 525 | case LIS2DW12_CONT_LOW_PWR_LOW_NOISE_12bit: |
cparata | 4:94c5d5546161 | 526 | case LIS2DW12_SINGLE_LOW_PWR_LOW_NOISE_4: |
cparata | 4:94c5d5546161 | 527 | case LIS2DW12_SINGLE_LOW_PWR_LOW_NOISE_3: |
cparata | 4:94c5d5546161 | 528 | case LIS2DW12_SINGLE_LOW_PWR_LOW_NOISE_2: |
cparata | 4:94c5d5546161 | 529 | case LIS2DW12_SINGLE_LOW_LOW_NOISE_PWR_12bit: |
cparata | 4:94c5d5546161 | 530 | *odr = 200.0f; |
cparata | 4:94c5d5546161 | 531 | break; |
cparata | 4:94c5d5546161 | 532 | |
cparata | 4:94c5d5546161 | 533 | default: |
cparata | 4:94c5d5546161 | 534 | *odr = -1.0f; |
cparata | 4:94c5d5546161 | 535 | ret = 1; |
cparata | 4:94c5d5546161 | 536 | break; |
cparata | 4:94c5d5546161 | 537 | } |
cparata | 4:94c5d5546161 | 538 | break; |
cparata | 4:94c5d5546161 | 539 | |
cparata | 4:94c5d5546161 | 540 | case LIS2DW12_XL_ODR_1k6Hz: |
cparata | 4:94c5d5546161 | 541 | switch (mode) { |
cparata | 4:94c5d5546161 | 542 | case LIS2DW12_HIGH_PERFORMANCE: |
cparata | 4:94c5d5546161 | 543 | case LIS2DW12_HIGH_PERFORMANCE_LOW_NOISE: |
cparata | 4:94c5d5546161 | 544 | *odr = 1600.0f; |
cparata | 4:94c5d5546161 | 545 | break; |
cparata | 4:94c5d5546161 | 546 | |
cparata | 4:94c5d5546161 | 547 | case LIS2DW12_CONT_LOW_PWR_4: |
cparata | 4:94c5d5546161 | 548 | case LIS2DW12_CONT_LOW_PWR_3: |
cparata | 4:94c5d5546161 | 549 | case LIS2DW12_CONT_LOW_PWR_2: |
cparata | 4:94c5d5546161 | 550 | case LIS2DW12_CONT_LOW_PWR_12bit: |
cparata | 4:94c5d5546161 | 551 | case LIS2DW12_SINGLE_LOW_PWR_4: |
cparata | 4:94c5d5546161 | 552 | case LIS2DW12_SINGLE_LOW_PWR_3: |
cparata | 4:94c5d5546161 | 553 | case LIS2DW12_SINGLE_LOW_PWR_2: |
cparata | 4:94c5d5546161 | 554 | case LIS2DW12_SINGLE_LOW_PWR_12bit: |
cparata | 4:94c5d5546161 | 555 | case LIS2DW12_CONT_LOW_PWR_LOW_NOISE_4: |
cparata | 4:94c5d5546161 | 556 | case LIS2DW12_CONT_LOW_PWR_LOW_NOISE_3: |
cparata | 4:94c5d5546161 | 557 | case LIS2DW12_CONT_LOW_PWR_LOW_NOISE_2: |
cparata | 4:94c5d5546161 | 558 | case LIS2DW12_CONT_LOW_PWR_LOW_NOISE_12bit: |
cparata | 4:94c5d5546161 | 559 | case LIS2DW12_SINGLE_LOW_PWR_LOW_NOISE_4: |
cparata | 4:94c5d5546161 | 560 | case LIS2DW12_SINGLE_LOW_PWR_LOW_NOISE_3: |
cparata | 4:94c5d5546161 | 561 | case LIS2DW12_SINGLE_LOW_PWR_LOW_NOISE_2: |
cparata | 4:94c5d5546161 | 562 | case LIS2DW12_SINGLE_LOW_LOW_NOISE_PWR_12bit: |
cparata | 4:94c5d5546161 | 563 | *odr = 200.0f; |
cparata | 4:94c5d5546161 | 564 | break; |
cparata | 4:94c5d5546161 | 565 | |
cparata | 4:94c5d5546161 | 566 | default: |
cparata | 4:94c5d5546161 | 567 | *odr = -1.0f; |
cparata | 4:94c5d5546161 | 568 | ret = 1; |
cparata | 4:94c5d5546161 | 569 | break; |
cparata | 4:94c5d5546161 | 570 | } |
cparata | 4:94c5d5546161 | 571 | break; |
cparata | 0:dff8803aace7 | 572 | |
cparata | 0:dff8803aace7 | 573 | default: |
cparata | 4:94c5d5546161 | 574 | *odr = -1.0f; |
cparata | 4:94c5d5546161 | 575 | ret = 1; |
cparata | 4:94c5d5546161 | 576 | break; |
cparata | 4:94c5d5546161 | 577 | } |
cparata | 0:dff8803aace7 | 578 | |
cparata | 4:94c5d5546161 | 579 | return ret; |
cparata | 0:dff8803aace7 | 580 | } |
cparata | 0:dff8803aace7 | 581 | |
cparata | 0:dff8803aace7 | 582 | /** |
cparata | 0:dff8803aace7 | 583 | * @brief Set LIS2DW12 Accelerometer output data rate |
cparata | 0:dff8803aace7 | 584 | * @param odr the output data rate to be set |
cparata | 0:dff8803aace7 | 585 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:dff8803aace7 | 586 | */ |
cparata | 0:dff8803aace7 | 587 | int LIS2DW12Sensor::set_x_odr(float odr) |
cparata | 4:94c5d5546161 | 588 | { |
cparata | 4:94c5d5546161 | 589 | return set_x_odr_with_mode(odr, LIS2DW12_HIGH_PERFORMANCE_MODE, LIS2DW12_LOW_NOISE_DISABLE); |
cparata | 0:dff8803aace7 | 590 | } |
cparata | 0:dff8803aace7 | 591 | |
cparata | 0:dff8803aace7 | 592 | /** |
cparata | 0:dff8803aace7 | 593 | * @brief Set LIS2DW12 Accelerometer output data rate |
cparata | 0:dff8803aace7 | 594 | * @param odr the output data rate to be set |
cparata | 0:dff8803aace7 | 595 | * @param mode the operating mode to be used |
cparata | 0:dff8803aace7 | 596 | * @param noise the low noise option |
cparata | 0:dff8803aace7 | 597 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:dff8803aace7 | 598 | */ |
cparata | 0:dff8803aace7 | 599 | int LIS2DW12Sensor::set_x_odr_with_mode(float odr, LIS2DW12_Operating_Mode_t mode, LIS2DW12_Low_Noise_t noise) |
cparata | 0:dff8803aace7 | 600 | { |
cparata | 4:94c5d5546161 | 601 | if (_x_is_enabled == 1) { |
cparata | 4:94c5d5546161 | 602 | if (set_x_odr_when_enabled(odr, mode, noise) != 0) { |
cparata | 4:94c5d5546161 | 603 | return 1; |
cparata | 4:94c5d5546161 | 604 | } |
cparata | 4:94c5d5546161 | 605 | } else { |
cparata | 4:94c5d5546161 | 606 | if (set_x_odr_when_disabled(odr, mode, noise) != 0) { |
cparata | 4:94c5d5546161 | 607 | return 1; |
cparata | 4:94c5d5546161 | 608 | } |
cparata | 0:dff8803aace7 | 609 | } |
cparata | 4:94c5d5546161 | 610 | |
cparata | 4:94c5d5546161 | 611 | return 0; |
cparata | 0:dff8803aace7 | 612 | } |
cparata | 0:dff8803aace7 | 613 | |
cparata | 0:dff8803aace7 | 614 | /** |
cparata | 0:dff8803aace7 | 615 | * @brief Set LIS2DW12 Accelerometer output data rate when enabled |
cparata | 0:dff8803aace7 | 616 | * @param odr the output data rate to be set |
cparata | 0:dff8803aace7 | 617 | * @param mode the operating mode to be used |
cparata | 0:dff8803aace7 | 618 | * @param noise the low noise option |
cparata | 0:dff8803aace7 | 619 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:dff8803aace7 | 620 | */ |
cparata | 0:dff8803aace7 | 621 | int LIS2DW12Sensor::set_x_odr_when_enabled(float odr, LIS2DW12_Operating_Mode_t mode, LIS2DW12_Low_Noise_t noise) |
cparata | 0:dff8803aace7 | 622 | { |
cparata | 4:94c5d5546161 | 623 | lis2dw12_odr_t new_odr; |
cparata | 4:94c5d5546161 | 624 | lis2dw12_mode_t new_power_mode; |
cparata | 0:dff8803aace7 | 625 | |
cparata | 4:94c5d5546161 | 626 | switch (mode) { |
cparata | 4:94c5d5546161 | 627 | case LIS2DW12_HIGH_PERFORMANCE_MODE: |
cparata | 0:dff8803aace7 | 628 | default: |
cparata | 4:94c5d5546161 | 629 | switch (noise) { |
cparata | 4:94c5d5546161 | 630 | case LIS2DW12_LOW_NOISE_DISABLE: |
cparata | 4:94c5d5546161 | 631 | default: |
cparata | 4:94c5d5546161 | 632 | new_power_mode = LIS2DW12_HIGH_PERFORMANCE; |
cparata | 4:94c5d5546161 | 633 | break; |
cparata | 4:94c5d5546161 | 634 | case LIS2DW12_LOW_NOISE_ENABLE: |
cparata | 4:94c5d5546161 | 635 | new_power_mode = LIS2DW12_HIGH_PERFORMANCE_LOW_NOISE; |
cparata | 4:94c5d5546161 | 636 | break; |
cparata | 4:94c5d5546161 | 637 | } |
cparata | 0:dff8803aace7 | 638 | |
cparata | 4:94c5d5546161 | 639 | /* If High Performance mode minimum ODR is 12.5Hz */ |
cparata | 4:94c5d5546161 | 640 | if (odr < 12.5f) { |
cparata | 4:94c5d5546161 | 641 | odr = 12.5f; |
cparata | 4:94c5d5546161 | 642 | } |
cparata | 4:94c5d5546161 | 643 | break; |
cparata | 4:94c5d5546161 | 644 | case LIS2DW12_LOW_POWER_MODE4: |
cparata | 4:94c5d5546161 | 645 | switch (noise) { |
cparata | 4:94c5d5546161 | 646 | case LIS2DW12_LOW_NOISE_DISABLE: |
cparata | 4:94c5d5546161 | 647 | default: |
cparata | 4:94c5d5546161 | 648 | new_power_mode = LIS2DW12_CONT_LOW_PWR_4; |
cparata | 4:94c5d5546161 | 649 | break; |
cparata | 4:94c5d5546161 | 650 | case LIS2DW12_LOW_NOISE_ENABLE: |
cparata | 4:94c5d5546161 | 651 | new_power_mode = LIS2DW12_CONT_LOW_PWR_LOW_NOISE_4; |
cparata | 4:94c5d5546161 | 652 | break; |
cparata | 4:94c5d5546161 | 653 | } |
cparata | 0:dff8803aace7 | 654 | |
cparata | 4:94c5d5546161 | 655 | /* If Low Power mode maximum ODR is 200Hz */ |
cparata | 4:94c5d5546161 | 656 | if (odr > 200.0f) { |
cparata | 4:94c5d5546161 | 657 | odr = 200.0f; |
cparata | 4:94c5d5546161 | 658 | } |
cparata | 4:94c5d5546161 | 659 | break; |
cparata | 4:94c5d5546161 | 660 | case LIS2DW12_LOW_POWER_MODE3: |
cparata | 4:94c5d5546161 | 661 | switch (noise) { |
cparata | 4:94c5d5546161 | 662 | case LIS2DW12_LOW_NOISE_DISABLE: |
cparata | 4:94c5d5546161 | 663 | default: |
cparata | 4:94c5d5546161 | 664 | new_power_mode = LIS2DW12_CONT_LOW_PWR_3; |
cparata | 4:94c5d5546161 | 665 | break; |
cparata | 4:94c5d5546161 | 666 | case LIS2DW12_LOW_NOISE_ENABLE: |
cparata | 4:94c5d5546161 | 667 | new_power_mode = LIS2DW12_CONT_LOW_PWR_LOW_NOISE_3; |
cparata | 4:94c5d5546161 | 668 | break; |
cparata | 4:94c5d5546161 | 669 | } |
cparata | 0:dff8803aace7 | 670 | |
cparata | 4:94c5d5546161 | 671 | /* If Low Power mode maximum ODR is 200Hz */ |
cparata | 4:94c5d5546161 | 672 | if (odr > 200.0f) { |
cparata | 4:94c5d5546161 | 673 | odr = 200.0f; |
cparata | 4:94c5d5546161 | 674 | } |
cparata | 4:94c5d5546161 | 675 | break; |
cparata | 4:94c5d5546161 | 676 | case LIS2DW12_LOW_POWER_MODE2: |
cparata | 4:94c5d5546161 | 677 | switch (noise) { |
cparata | 4:94c5d5546161 | 678 | case LIS2DW12_LOW_NOISE_DISABLE: |
cparata | 4:94c5d5546161 | 679 | default: |
cparata | 4:94c5d5546161 | 680 | new_power_mode = LIS2DW12_CONT_LOW_PWR_2; |
cparata | 4:94c5d5546161 | 681 | break; |
cparata | 4:94c5d5546161 | 682 | case LIS2DW12_LOW_NOISE_ENABLE: |
cparata | 4:94c5d5546161 | 683 | new_power_mode = LIS2DW12_CONT_LOW_PWR_LOW_NOISE_2; |
cparata | 4:94c5d5546161 | 684 | break; |
cparata | 4:94c5d5546161 | 685 | } |
cparata | 0:dff8803aace7 | 686 | |
cparata | 4:94c5d5546161 | 687 | /* If Low Power mode maximum ODR is 200Hz */ |
cparata | 4:94c5d5546161 | 688 | if (odr > 200.0f) { |
cparata | 4:94c5d5546161 | 689 | odr = 200.0f; |
cparata | 4:94c5d5546161 | 690 | } |
cparata | 4:94c5d5546161 | 691 | break; |
cparata | 4:94c5d5546161 | 692 | case LIS2DW12_LOW_POWER_MODE1: |
cparata | 4:94c5d5546161 | 693 | switch (noise) { |
cparata | 4:94c5d5546161 | 694 | case LIS2DW12_LOW_NOISE_DISABLE: |
cparata | 4:94c5d5546161 | 695 | default: |
cparata | 4:94c5d5546161 | 696 | new_power_mode = LIS2DW12_CONT_LOW_PWR_12bit; |
cparata | 4:94c5d5546161 | 697 | break; |
cparata | 4:94c5d5546161 | 698 | case LIS2DW12_LOW_NOISE_ENABLE: |
cparata | 4:94c5d5546161 | 699 | new_power_mode = LIS2DW12_CONT_LOW_PWR_LOW_NOISE_12bit; |
cparata | 4:94c5d5546161 | 700 | break; |
cparata | 4:94c5d5546161 | 701 | } |
cparata | 0:dff8803aace7 | 702 | |
cparata | 4:94c5d5546161 | 703 | /* If Low Power mode maximum ODR is 200Hz */ |
cparata | 4:94c5d5546161 | 704 | if (odr > 200.0f) { |
cparata | 4:94c5d5546161 | 705 | odr = 200.0f; |
cparata | 4:94c5d5546161 | 706 | } |
cparata | 4:94c5d5546161 | 707 | break; |
cparata | 4:94c5d5546161 | 708 | } |
cparata | 0:dff8803aace7 | 709 | |
cparata | 0:dff8803aace7 | 710 | |
cparata | 4:94c5d5546161 | 711 | new_odr = (odr <= 1.6f) ? LIS2DW12_XL_ODR_1Hz6_LP_ONLY |
cparata | 4:94c5d5546161 | 712 | : (odr <= 12.5f) ? LIS2DW12_XL_ODR_12Hz5 |
cparata | 4:94c5d5546161 | 713 | : (odr <= 25.0f) ? LIS2DW12_XL_ODR_25Hz |
cparata | 4:94c5d5546161 | 714 | : (odr <= 50.0f) ? LIS2DW12_XL_ODR_50Hz |
cparata | 4:94c5d5546161 | 715 | : (odr <= 100.0f) ? LIS2DW12_XL_ODR_100Hz |
cparata | 4:94c5d5546161 | 716 | : (odr <= 200.0f) ? LIS2DW12_XL_ODR_200Hz |
cparata | 4:94c5d5546161 | 717 | : (odr <= 400.0f) ? LIS2DW12_XL_ODR_400Hz |
cparata | 4:94c5d5546161 | 718 | : (odr <= 800.0f) ? LIS2DW12_XL_ODR_800Hz |
cparata | 4:94c5d5546161 | 719 | : LIS2DW12_XL_ODR_1k6Hz; |
cparata | 0:dff8803aace7 | 720 | |
cparata | 4:94c5d5546161 | 721 | /* Output data rate selection. */ |
cparata | 4:94c5d5546161 | 722 | if (lis2dw12_data_rate_set(&_reg_ctx, new_odr) != 0) { |
cparata | 4:94c5d5546161 | 723 | return 1; |
cparata | 4:94c5d5546161 | 724 | } |
cparata | 0:dff8803aace7 | 725 | |
cparata | 4:94c5d5546161 | 726 | /* Power mode selection. */ |
cparata | 4:94c5d5546161 | 727 | if (lis2dw12_power_mode_set(&_reg_ctx, new_power_mode) != 0) { |
cparata | 4:94c5d5546161 | 728 | return 1; |
cparata | 4:94c5d5546161 | 729 | } |
cparata | 0:dff8803aace7 | 730 | |
cparata | 4:94c5d5546161 | 731 | /* Store actual output data rate, operating mode and low noise. */ |
cparata | 4:94c5d5546161 | 732 | _x_last_odr = odr; |
cparata | 4:94c5d5546161 | 733 | _x_last_operating_mode = mode; |
cparata | 4:94c5d5546161 | 734 | _x_last_noise = noise; |
cparata | 4:94c5d5546161 | 735 | |
cparata | 4:94c5d5546161 | 736 | return 0; |
cparata | 0:dff8803aace7 | 737 | } |
cparata | 0:dff8803aace7 | 738 | |
cparata | 0:dff8803aace7 | 739 | /** |
cparata | 0:dff8803aace7 | 740 | * @brief Set LIS2DW12 Accelerometer output data rate when disabled |
cparata | 0:dff8803aace7 | 741 | * @param odr the output data rate to be set |
cparata | 0:dff8803aace7 | 742 | * @param mode the operating mode to be used |
cparata | 0:dff8803aace7 | 743 | * @param noise the low noise option |
cparata | 0:dff8803aace7 | 744 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:dff8803aace7 | 745 | */ |
cparata | 0:dff8803aace7 | 746 | int LIS2DW12Sensor::set_x_odr_when_disabled(float odr, LIS2DW12_Operating_Mode_t mode, LIS2DW12_Low_Noise_t noise) |
cparata | 0:dff8803aace7 | 747 | { |
cparata | 4:94c5d5546161 | 748 | _x_last_operating_mode = mode; |
cparata | 4:94c5d5546161 | 749 | _x_last_noise = noise; |
cparata | 0:dff8803aace7 | 750 | |
cparata | 4:94c5d5546161 | 751 | _x_last_odr = (odr <= 1.6f) ? 1.6f |
cparata | 4:94c5d5546161 | 752 | : (odr <= 12.5f) ? 12.5f |
cparata | 4:94c5d5546161 | 753 | : (odr <= 25.0f) ? 25.0f |
cparata | 4:94c5d5546161 | 754 | : (odr <= 50.0f) ? 50.0f |
cparata | 4:94c5d5546161 | 755 | : (odr <= 100.0f) ? 100.0f |
cparata | 4:94c5d5546161 | 756 | : (odr <= 200.0f) ? 200.0f |
cparata | 4:94c5d5546161 | 757 | : (odr <= 400.0f) ? 400.0f |
cparata | 4:94c5d5546161 | 758 | : (odr <= 800.0f) ? 800.0f |
cparata | 4:94c5d5546161 | 759 | : 1600.0f; |
cparata | 4:94c5d5546161 | 760 | |
cparata | 4:94c5d5546161 | 761 | return 0; |
cparata | 0:dff8803aace7 | 762 | } |
cparata | 0:dff8803aace7 | 763 | |
cparata | 0:dff8803aace7 | 764 | /** |
cparata | 0:dff8803aace7 | 765 | * @brief Read LIS2DW12 Accelerometer full scale |
cparata | 0:dff8803aace7 | 766 | * @param full_scale the pointer to the full scale |
cparata | 0:dff8803aace7 | 767 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:dff8803aace7 | 768 | */ |
cparata | 4:94c5d5546161 | 769 | int LIS2DW12Sensor::get_x_fs(float *full_scale) |
cparata | 0:dff8803aace7 | 770 | { |
cparata | 4:94c5d5546161 | 771 | int32_t ret = 0; |
cparata | 4:94c5d5546161 | 772 | lis2dw12_fs_t fs_low_level; |
cparata | 0:dff8803aace7 | 773 | |
cparata | 4:94c5d5546161 | 774 | /* Read actual full scale selection from sensor. */ |
cparata | 4:94c5d5546161 | 775 | if (lis2dw12_full_scale_get(&_reg_ctx, &fs_low_level) != 0) { |
cparata | 4:94c5d5546161 | 776 | return 1; |
cparata | 4:94c5d5546161 | 777 | } |
cparata | 0:dff8803aace7 | 778 | |
cparata | 4:94c5d5546161 | 779 | switch (fs_low_level) { |
cparata | 4:94c5d5546161 | 780 | case LIS2DW12_2g: |
cparata | 4:94c5d5546161 | 781 | *full_scale = 2; |
cparata | 4:94c5d5546161 | 782 | break; |
cparata | 0:dff8803aace7 | 783 | |
cparata | 4:94c5d5546161 | 784 | case LIS2DW12_4g: |
cparata | 4:94c5d5546161 | 785 | *full_scale = 4; |
cparata | 4:94c5d5546161 | 786 | break; |
cparata | 0:dff8803aace7 | 787 | |
cparata | 4:94c5d5546161 | 788 | case LIS2DW12_8g: |
cparata | 4:94c5d5546161 | 789 | *full_scale = 8; |
cparata | 4:94c5d5546161 | 790 | break; |
cparata | 0:dff8803aace7 | 791 | |
cparata | 4:94c5d5546161 | 792 | case LIS2DW12_16g: |
cparata | 4:94c5d5546161 | 793 | *full_scale = 16; |
cparata | 4:94c5d5546161 | 794 | break; |
cparata | 0:dff8803aace7 | 795 | |
cparata | 4:94c5d5546161 | 796 | default: |
cparata | 4:94c5d5546161 | 797 | *full_scale = -1; |
cparata | 4:94c5d5546161 | 798 | ret = 1; |
cparata | 4:94c5d5546161 | 799 | break; |
cparata | 4:94c5d5546161 | 800 | } |
cparata | 0:dff8803aace7 | 801 | |
cparata | 4:94c5d5546161 | 802 | return ret; |
cparata | 0:dff8803aace7 | 803 | } |
cparata | 0:dff8803aace7 | 804 | |
cparata | 0:dff8803aace7 | 805 | /** |
cparata | 0:dff8803aace7 | 806 | * @brief Set LIS2DW12 Accelerometer full scale |
cparata | 0:dff8803aace7 | 807 | * @param full_scale the full scale to be set |
cparata | 0:dff8803aace7 | 808 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:dff8803aace7 | 809 | */ |
cparata | 0:dff8803aace7 | 810 | int LIS2DW12Sensor::set_x_fs(float full_scale) |
cparata | 0:dff8803aace7 | 811 | { |
cparata | 4:94c5d5546161 | 812 | lis2dw12_fs_t new_fs; |
cparata | 0:dff8803aace7 | 813 | |
cparata | 4:94c5d5546161 | 814 | /* Seems like MISRA C-2012 rule 14.3a violation but only from single file statical analysis point of view because |
cparata | 4:94c5d5546161 | 815 | the parameter passed to the function is not known at the moment of analysis */ |
cparata | 4:94c5d5546161 | 816 | new_fs = (full_scale <= 2) ? LIS2DW12_2g |
cparata | 4:94c5d5546161 | 817 | : (full_scale <= 4) ? LIS2DW12_4g |
cparata | 4:94c5d5546161 | 818 | : (full_scale <= 8) ? LIS2DW12_8g |
cparata | 4:94c5d5546161 | 819 | : LIS2DW12_16g; |
cparata | 0:dff8803aace7 | 820 | |
cparata | 4:94c5d5546161 | 821 | if (lis2dw12_full_scale_set(&_reg_ctx, new_fs) != 0) { |
cparata | 4:94c5d5546161 | 822 | return 1; |
cparata | 4:94c5d5546161 | 823 | } |
cparata | 0:dff8803aace7 | 824 | |
cparata | 4:94c5d5546161 | 825 | return 0; |
cparata | 0:dff8803aace7 | 826 | } |
cparata | 0:dff8803aace7 | 827 | |
cparata | 0:dff8803aace7 | 828 | /** |
martlefebvre94 | 5:b6ce53be44c2 | 829 | * @brief Read LIS2DW12 Accelerometer filter bandwidth |
martlefebvre94 | 5:b6ce53be44c2 | 830 | * @param bw_filt the pointer to the filter bandwidth |
martlefebvre94 | 5:b6ce53be44c2 | 831 | * @retval 0 in case of success, an error code otherwise |
martlefebvre94 | 5:b6ce53be44c2 | 832 | */ |
martlefebvre94 | 5:b6ce53be44c2 | 833 | int LIS2DW12Sensor::get_x_bw_filt(uint8_t *bw_filt) |
martlefebvre94 | 5:b6ce53be44c2 | 834 | { |
martlefebvre94 | 5:b6ce53be44c2 | 835 | int ret = 0; |
martlefebvre94 | 5:b6ce53be44c2 | 836 | lis2dw12_bw_filt_t bw_filt_low_level; |
martlefebvre94 | 5:b6ce53be44c2 | 837 | |
martlefebvre94 | 5:b6ce53be44c2 | 838 | /* Get current filter bandwidth. */ |
martlefebvre94 | 5:b6ce53be44c2 | 839 | if (lis2dw12_filter_bandwidth_get(&_reg_ctx, &bw_filt_low_level) != 0) { |
martlefebvre94 | 5:b6ce53be44c2 | 840 | return 1; |
martlefebvre94 | 5:b6ce53be44c2 | 841 | } |
martlefebvre94 | 5:b6ce53be44c2 | 842 | |
martlefebvre94 | 5:b6ce53be44c2 | 843 | switch (bw_filt_low_level) { |
martlefebvre94 | 5:b6ce53be44c2 | 844 | case LIS2DW12_ODR_DIV_2: |
martlefebvre94 | 5:b6ce53be44c2 | 845 | *bw_filt = 0; |
martlefebvre94 | 5:b6ce53be44c2 | 846 | break; |
martlefebvre94 | 5:b6ce53be44c2 | 847 | |
martlefebvre94 | 5:b6ce53be44c2 | 848 | case LIS2DW12_ODR_DIV_4: |
martlefebvre94 | 5:b6ce53be44c2 | 849 | *bw_filt = 1; |
martlefebvre94 | 5:b6ce53be44c2 | 850 | break; |
martlefebvre94 | 5:b6ce53be44c2 | 851 | |
martlefebvre94 | 5:b6ce53be44c2 | 852 | case LIS2DW12_ODR_DIV_10: |
martlefebvre94 | 5:b6ce53be44c2 | 853 | *bw_filt = 2; |
martlefebvre94 | 5:b6ce53be44c2 | 854 | break; |
martlefebvre94 | 5:b6ce53be44c2 | 855 | |
martlefebvre94 | 5:b6ce53be44c2 | 856 | case LIS2DW12_ODR_DIV_20: |
martlefebvre94 | 5:b6ce53be44c2 | 857 | *bw_filt = 3; |
martlefebvre94 | 5:b6ce53be44c2 | 858 | break; |
martlefebvre94 | 5:b6ce53be44c2 | 859 | |
martlefebvre94 | 5:b6ce53be44c2 | 860 | default: |
martlefebvre94 | 5:b6ce53be44c2 | 861 | ret = 1; |
martlefebvre94 | 5:b6ce53be44c2 | 862 | break; |
martlefebvre94 | 5:b6ce53be44c2 | 863 | } |
martlefebvre94 | 5:b6ce53be44c2 | 864 | |
martlefebvre94 | 5:b6ce53be44c2 | 865 | return ret; |
martlefebvre94 | 5:b6ce53be44c2 | 866 | } |
martlefebvre94 | 5:b6ce53be44c2 | 867 | |
martlefebvre94 | 5:b6ce53be44c2 | 868 | /** |
martlefebvre94 | 5:b6ce53be44c2 | 869 | * @brief Set LIS2DW12 Accelerometer filter bandwidth |
martlefebvre94 | 5:b6ce53be44c2 | 870 | * @param bw_filt the filter bandwidth to be set |
martlefebvre94 | 5:b6ce53be44c2 | 871 | * @retval 0 in case of success, an error code otherwise |
martlefebvre94 | 5:b6ce53be44c2 | 872 | */ |
martlefebvre94 | 5:b6ce53be44c2 | 873 | int LIS2DW12Sensor::set_x_bw_filt(uint8_t bw_filt) |
martlefebvre94 | 5:b6ce53be44c2 | 874 | { |
martlefebvre94 | 5:b6ce53be44c2 | 875 | lis2dw12_bw_filt_t new_bw_filt; |
martlefebvre94 | 5:b6ce53be44c2 | 876 | |
martlefebvre94 | 5:b6ce53be44c2 | 877 | new_bw_filt = (bw_filt == 0) ? LIS2DW12_ODR_DIV_2 |
martlefebvre94 | 5:b6ce53be44c2 | 878 | : (bw_filt == 1) ? LIS2DW12_ODR_DIV_4 |
martlefebvre94 | 5:b6ce53be44c2 | 879 | : (bw_filt == 2) ? LIS2DW12_ODR_DIV_10 |
martlefebvre94 | 5:b6ce53be44c2 | 880 | : LIS2DW12_ODR_DIV_20; |
martlefebvre94 | 5:b6ce53be44c2 | 881 | |
martlefebvre94 | 5:b6ce53be44c2 | 882 | if (lis2dw12_filter_bandwidth_set(&_reg_ctx, new_bw_filt) != 0) { |
martlefebvre94 | 5:b6ce53be44c2 | 883 | return 1; |
martlefebvre94 | 5:b6ce53be44c2 | 884 | } |
martlefebvre94 | 5:b6ce53be44c2 | 885 | |
martlefebvre94 | 5:b6ce53be44c2 | 886 | return 0; |
martlefebvre94 | 5:b6ce53be44c2 | 887 | } |
martlefebvre94 | 5:b6ce53be44c2 | 888 | |
martlefebvre94 | 5:b6ce53be44c2 | 889 | /** |
martlefebvre94 | 5:b6ce53be44c2 | 890 | * @brief Read LIS2DW12 Accelerometer power mode |
martlefebvre94 | 5:b6ce53be44c2 | 891 | * @param lp_mode the pointer to the low-power mode, mode the pointer to the mode, low_noise the pointer to the low-noise configuration |
martlefebvre94 | 5:b6ce53be44c2 | 892 | * @retval 0 in case of success, an error code otherwise |
martlefebvre94 | 5:b6ce53be44c2 | 893 | */ |
martlefebvre94 | 5:b6ce53be44c2 | 894 | int LIS2DW12Sensor::get_x_power_mode(uint8_t *lp_mode, uint8_t *mode, uint8_t *low_noise) |
martlefebvre94 | 5:b6ce53be44c2 | 895 | { |
martlefebvre94 | 5:b6ce53be44c2 | 896 | int ret = 0; |
martlefebvre94 | 5:b6ce53be44c2 | 897 | lis2dw12_mode_t mode_low_level; |
martlefebvre94 | 5:b6ce53be44c2 | 898 | |
martlefebvre94 | 5:b6ce53be44c2 | 899 | /* Get current filter bandwidth. */ |
martlefebvre94 | 5:b6ce53be44c2 | 900 | if (lis2dw12_power_mode_get(&_reg_ctx, &mode_low_level) != 0) { |
martlefebvre94 | 5:b6ce53be44c2 | 901 | return 1; |
martlefebvre94 | 5:b6ce53be44c2 | 902 | } |
martlefebvre94 | 5:b6ce53be44c2 | 903 | |
martlefebvre94 | 5:b6ce53be44c2 | 904 | switch (mode_low_level) { |
martlefebvre94 | 5:b6ce53be44c2 | 905 | case LIS2DW12_HIGH_PERFORMANCE: |
martlefebvre94 | 5:b6ce53be44c2 | 906 | *lp_mode = 0; |
martlefebvre94 | 5:b6ce53be44c2 | 907 | *mode = 1; |
martlefebvre94 | 5:b6ce53be44c2 | 908 | *low_noise = 0; |
martlefebvre94 | 5:b6ce53be44c2 | 909 | break; |
martlefebvre94 | 5:b6ce53be44c2 | 910 | |
martlefebvre94 | 5:b6ce53be44c2 | 911 | case LIS2DW12_CONT_LOW_PWR_4: |
martlefebvre94 | 5:b6ce53be44c2 | 912 | *lp_mode = 3; |
martlefebvre94 | 5:b6ce53be44c2 | 913 | *mode = 0; |
martlefebvre94 | 5:b6ce53be44c2 | 914 | *low_noise = 0; |
martlefebvre94 | 5:b6ce53be44c2 | 915 | break; |
martlefebvre94 | 5:b6ce53be44c2 | 916 | |
martlefebvre94 | 5:b6ce53be44c2 | 917 | case LIS2DW12_CONT_LOW_PWR_3: |
martlefebvre94 | 5:b6ce53be44c2 | 918 | *lp_mode = 2; |
martlefebvre94 | 5:b6ce53be44c2 | 919 | *mode = 0; |
martlefebvre94 | 5:b6ce53be44c2 | 920 | *low_noise = 0; |
martlefebvre94 | 5:b6ce53be44c2 | 921 | break; |
martlefebvre94 | 5:b6ce53be44c2 | 922 | |
martlefebvre94 | 5:b6ce53be44c2 | 923 | case LIS2DW12_CONT_LOW_PWR_2: |
martlefebvre94 | 5:b6ce53be44c2 | 924 | *lp_mode = 1; |
martlefebvre94 | 5:b6ce53be44c2 | 925 | *mode = 0; |
martlefebvre94 | 5:b6ce53be44c2 | 926 | *low_noise = 0; |
martlefebvre94 | 5:b6ce53be44c2 | 927 | break; |
martlefebvre94 | 5:b6ce53be44c2 | 928 | |
martlefebvre94 | 5:b6ce53be44c2 | 929 | case LIS2DW12_CONT_LOW_PWR_12bit: |
martlefebvre94 | 5:b6ce53be44c2 | 930 | *lp_mode = 0; |
martlefebvre94 | 5:b6ce53be44c2 | 931 | *mode = 0; |
martlefebvre94 | 5:b6ce53be44c2 | 932 | *low_noise = 0; |
martlefebvre94 | 5:b6ce53be44c2 | 933 | break; |
martlefebvre94 | 5:b6ce53be44c2 | 934 | |
martlefebvre94 | 5:b6ce53be44c2 | 935 | case LIS2DW12_SINGLE_LOW_PWR_4: |
martlefebvre94 | 5:b6ce53be44c2 | 936 | *lp_mode = 3; |
martlefebvre94 | 5:b6ce53be44c2 | 937 | *mode = 2; |
martlefebvre94 | 5:b6ce53be44c2 | 938 | *low_noise = 0; |
martlefebvre94 | 5:b6ce53be44c2 | 939 | break; |
martlefebvre94 | 5:b6ce53be44c2 | 940 | |
martlefebvre94 | 5:b6ce53be44c2 | 941 | case LIS2DW12_SINGLE_LOW_PWR_3: |
martlefebvre94 | 5:b6ce53be44c2 | 942 | *lp_mode = 2; |
martlefebvre94 | 5:b6ce53be44c2 | 943 | *mode = 2; |
martlefebvre94 | 5:b6ce53be44c2 | 944 | *low_noise = 0; |
martlefebvre94 | 5:b6ce53be44c2 | 945 | break; |
martlefebvre94 | 5:b6ce53be44c2 | 946 | |
martlefebvre94 | 5:b6ce53be44c2 | 947 | case LIS2DW12_SINGLE_LOW_PWR_2: |
martlefebvre94 | 5:b6ce53be44c2 | 948 | *lp_mode = 1; |
martlefebvre94 | 5:b6ce53be44c2 | 949 | *mode = 2; |
martlefebvre94 | 5:b6ce53be44c2 | 950 | *low_noise = 0; |
martlefebvre94 | 5:b6ce53be44c2 | 951 | break; |
martlefebvre94 | 5:b6ce53be44c2 | 952 | |
martlefebvre94 | 5:b6ce53be44c2 | 953 | case LIS2DW12_SINGLE_LOW_PWR_12bit: |
martlefebvre94 | 5:b6ce53be44c2 | 954 | *lp_mode = 0; |
martlefebvre94 | 5:b6ce53be44c2 | 955 | *mode = 2; |
martlefebvre94 | 5:b6ce53be44c2 | 956 | *low_noise = 0; |
martlefebvre94 | 5:b6ce53be44c2 | 957 | break; |
martlefebvre94 | 5:b6ce53be44c2 | 958 | |
martlefebvre94 | 5:b6ce53be44c2 | 959 | case LIS2DW12_HIGH_PERFORMANCE_LOW_NOISE: |
martlefebvre94 | 5:b6ce53be44c2 | 960 | *lp_mode = 0; |
martlefebvre94 | 5:b6ce53be44c2 | 961 | *mode = 1; |
martlefebvre94 | 5:b6ce53be44c2 | 962 | *low_noise = 1; |
martlefebvre94 | 5:b6ce53be44c2 | 963 | break; |
martlefebvre94 | 5:b6ce53be44c2 | 964 | |
martlefebvre94 | 5:b6ce53be44c2 | 965 | case LIS2DW12_CONT_LOW_PWR_LOW_NOISE_4: |
martlefebvre94 | 5:b6ce53be44c2 | 966 | *lp_mode = 3; |
martlefebvre94 | 5:b6ce53be44c2 | 967 | *mode = 0; |
martlefebvre94 | 5:b6ce53be44c2 | 968 | *low_noise = 1; |
martlefebvre94 | 5:b6ce53be44c2 | 969 | break; |
martlefebvre94 | 5:b6ce53be44c2 | 970 | |
martlefebvre94 | 5:b6ce53be44c2 | 971 | case LIS2DW12_CONT_LOW_PWR_LOW_NOISE_3: |
martlefebvre94 | 5:b6ce53be44c2 | 972 | *lp_mode = 2; |
martlefebvre94 | 5:b6ce53be44c2 | 973 | *mode = 0; |
martlefebvre94 | 5:b6ce53be44c2 | 974 | *low_noise = 1; |
martlefebvre94 | 5:b6ce53be44c2 | 975 | break; |
martlefebvre94 | 5:b6ce53be44c2 | 976 | |
martlefebvre94 | 5:b6ce53be44c2 | 977 | case LIS2DW12_CONT_LOW_PWR_LOW_NOISE_2: |
martlefebvre94 | 5:b6ce53be44c2 | 978 | *lp_mode = 1; |
martlefebvre94 | 5:b6ce53be44c2 | 979 | *mode = 0; |
martlefebvre94 | 5:b6ce53be44c2 | 980 | *low_noise = 1; |
martlefebvre94 | 5:b6ce53be44c2 | 981 | break; |
martlefebvre94 | 5:b6ce53be44c2 | 982 | |
martlefebvre94 | 5:b6ce53be44c2 | 983 | case LIS2DW12_CONT_LOW_PWR_LOW_NOISE_12bit: |
martlefebvre94 | 5:b6ce53be44c2 | 984 | *lp_mode = 0; |
martlefebvre94 | 5:b6ce53be44c2 | 985 | *mode = 0; |
martlefebvre94 | 5:b6ce53be44c2 | 986 | *low_noise = 1; |
martlefebvre94 | 5:b6ce53be44c2 | 987 | break; |
martlefebvre94 | 5:b6ce53be44c2 | 988 | |
martlefebvre94 | 5:b6ce53be44c2 | 989 | case LIS2DW12_SINGLE_LOW_PWR_LOW_NOISE_4: |
martlefebvre94 | 5:b6ce53be44c2 | 990 | *lp_mode = 3; |
martlefebvre94 | 5:b6ce53be44c2 | 991 | *mode = 2; |
martlefebvre94 | 5:b6ce53be44c2 | 992 | *low_noise = 1; |
martlefebvre94 | 5:b6ce53be44c2 | 993 | break; |
martlefebvre94 | 5:b6ce53be44c2 | 994 | |
martlefebvre94 | 5:b6ce53be44c2 | 995 | case LIS2DW12_SINGLE_LOW_PWR_LOW_NOISE_3: |
martlefebvre94 | 5:b6ce53be44c2 | 996 | *lp_mode = 2; |
martlefebvre94 | 5:b6ce53be44c2 | 997 | *mode = 2; |
martlefebvre94 | 5:b6ce53be44c2 | 998 | *low_noise = 1; |
martlefebvre94 | 5:b6ce53be44c2 | 999 | break; |
martlefebvre94 | 5:b6ce53be44c2 | 1000 | |
martlefebvre94 | 5:b6ce53be44c2 | 1001 | case LIS2DW12_SINGLE_LOW_PWR_LOW_NOISE_2: |
martlefebvre94 | 5:b6ce53be44c2 | 1002 | *lp_mode = 1; |
martlefebvre94 | 5:b6ce53be44c2 | 1003 | *mode = 2; |
martlefebvre94 | 5:b6ce53be44c2 | 1004 | *low_noise = 1; |
martlefebvre94 | 5:b6ce53be44c2 | 1005 | break; |
martlefebvre94 | 5:b6ce53be44c2 | 1006 | |
martlefebvre94 | 5:b6ce53be44c2 | 1007 | case LIS2DW12_SINGLE_LOW_LOW_NOISE_PWR_12bit: |
martlefebvre94 | 5:b6ce53be44c2 | 1008 | *lp_mode = 0; |
martlefebvre94 | 5:b6ce53be44c2 | 1009 | *mode = 2; |
martlefebvre94 | 5:b6ce53be44c2 | 1010 | *low_noise = 1; |
martlefebvre94 | 5:b6ce53be44c2 | 1011 | break; |
martlefebvre94 | 5:b6ce53be44c2 | 1012 | |
martlefebvre94 | 5:b6ce53be44c2 | 1013 | default: |
martlefebvre94 | 5:b6ce53be44c2 | 1014 | ret = 1; |
martlefebvre94 | 5:b6ce53be44c2 | 1015 | break; |
martlefebvre94 | 5:b6ce53be44c2 | 1016 | } |
martlefebvre94 | 5:b6ce53be44c2 | 1017 | |
martlefebvre94 | 5:b6ce53be44c2 | 1018 | return ret; |
martlefebvre94 | 5:b6ce53be44c2 | 1019 | } |
martlefebvre94 | 5:b6ce53be44c2 | 1020 | |
martlefebvre94 | 5:b6ce53be44c2 | 1021 | /** |
martlefebvre94 | 5:b6ce53be44c2 | 1022 | * @brief Set LIS2DW12 Accelerometer power mode |
martlefebvre94 | 5:b6ce53be44c2 | 1023 | * @param power_mode the power mode to be set |
martlefebvre94 | 5:b6ce53be44c2 | 1024 | * @retval 0 in case of success, an error code otherwise |
martlefebvre94 | 5:b6ce53be44c2 | 1025 | */ |
martlefebvre94 | 5:b6ce53be44c2 | 1026 | int LIS2DW12Sensor::set_x_power_mode(uint8_t power_mode) |
martlefebvre94 | 5:b6ce53be44c2 | 1027 | { |
martlefebvre94 | 5:b6ce53be44c2 | 1028 | lis2dw12_mode_t new_mode; |
martlefebvre94 | 5:b6ce53be44c2 | 1029 | |
martlefebvre94 | 5:b6ce53be44c2 | 1030 | new_mode = (power_mode == 0x04) ? LIS2DW12_HIGH_PERFORMANCE |
martlefebvre94 | 5:b6ce53be44c2 | 1031 | : (power_mode == 0x03) ? LIS2DW12_CONT_LOW_PWR_4 |
martlefebvre94 | 5:b6ce53be44c2 | 1032 | : (power_mode == 0x02) ? LIS2DW12_CONT_LOW_PWR_3 |
martlefebvre94 | 5:b6ce53be44c2 | 1033 | : (power_mode == 0x01) ? LIS2DW12_CONT_LOW_PWR_2 |
martlefebvre94 | 5:b6ce53be44c2 | 1034 | : (power_mode == 0x00) ? LIS2DW12_CONT_LOW_PWR_12bit |
martlefebvre94 | 5:b6ce53be44c2 | 1035 | : (power_mode == 0x0B) ? LIS2DW12_SINGLE_LOW_PWR_4 |
martlefebvre94 | 5:b6ce53be44c2 | 1036 | : (power_mode == 0x0A) ? LIS2DW12_SINGLE_LOW_PWR_3 |
martlefebvre94 | 5:b6ce53be44c2 | 1037 | : (power_mode == 0x09) ? LIS2DW12_SINGLE_LOW_PWR_2 |
martlefebvre94 | 5:b6ce53be44c2 | 1038 | : (power_mode == 0x08) ? LIS2DW12_SINGLE_LOW_PWR_12bit |
martlefebvre94 | 5:b6ce53be44c2 | 1039 | : (power_mode == 0x14) ? LIS2DW12_HIGH_PERFORMANCE_LOW_NOISE |
martlefebvre94 | 5:b6ce53be44c2 | 1040 | : (power_mode == 0x13) ? LIS2DW12_CONT_LOW_PWR_LOW_NOISE_4 |
martlefebvre94 | 5:b6ce53be44c2 | 1041 | : (power_mode == 0x12) ? LIS2DW12_CONT_LOW_PWR_LOW_NOISE_3 |
martlefebvre94 | 5:b6ce53be44c2 | 1042 | : (power_mode == 0x11) ? LIS2DW12_CONT_LOW_PWR_LOW_NOISE_2 |
martlefebvre94 | 5:b6ce53be44c2 | 1043 | : (power_mode == 0x10) ? LIS2DW12_CONT_LOW_PWR_LOW_NOISE_12bit |
martlefebvre94 | 5:b6ce53be44c2 | 1044 | : (power_mode == 0x1B) ? LIS2DW12_SINGLE_LOW_PWR_LOW_NOISE_4 |
martlefebvre94 | 5:b6ce53be44c2 | 1045 | : (power_mode == 0x1A) ? LIS2DW12_SINGLE_LOW_PWR_LOW_NOISE_3 |
martlefebvre94 | 5:b6ce53be44c2 | 1046 | : (power_mode == 0x19) ? LIS2DW12_SINGLE_LOW_PWR_LOW_NOISE_2 |
martlefebvre94 | 5:b6ce53be44c2 | 1047 | : (power_mode == 0x18) ? LIS2DW12_SINGLE_LOW_LOW_NOISE_PWR_12bit |
martlefebvre94 | 5:b6ce53be44c2 | 1048 | : LIS2DW12_HIGH_PERFORMANCE; |
martlefebvre94 | 5:b6ce53be44c2 | 1049 | |
martlefebvre94 | 5:b6ce53be44c2 | 1050 | if (lis2dw12_power_mode_set(&_reg_ctx, new_mode) != 0) { |
martlefebvre94 | 5:b6ce53be44c2 | 1051 | return 1; |
martlefebvre94 | 5:b6ce53be44c2 | 1052 | } |
martlefebvre94 | 5:b6ce53be44c2 | 1053 | |
martlefebvre94 | 5:b6ce53be44c2 | 1054 | return 0; |
martlefebvre94 | 5:b6ce53be44c2 | 1055 | } |
martlefebvre94 | 5:b6ce53be44c2 | 1056 | |
martlefebvre94 | 5:b6ce53be44c2 | 1057 | /** |
cparata | 0:dff8803aace7 | 1058 | * @brief Enable the wake up detection for LIS2DW12 accelerometer sensor |
cparata | 0:dff8803aace7 | 1059 | * @note This function sets the LIS2DW12 accelerometer ODR to 200Hz and the LIS2DW12 accelerometer full scale to 2g |
cparata | 0:dff8803aace7 | 1060 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:dff8803aace7 | 1061 | */ |
cparata | 0:dff8803aace7 | 1062 | int LIS2DW12Sensor::enable_wake_up_detection(void) |
cparata | 0:dff8803aace7 | 1063 | { |
cparata | 4:94c5d5546161 | 1064 | int32_t ret = 0; |
cparata | 4:94c5d5546161 | 1065 | lis2dw12_ctrl4_int1_pad_ctrl_t val; |
cparata | 0:dff8803aace7 | 1066 | |
cparata | 4:94c5d5546161 | 1067 | /* Output Data Rate selection */ |
cparata | 4:94c5d5546161 | 1068 | if (set_x_odr(200.0f) != 0) { |
cparata | 4:94c5d5546161 | 1069 | return 1; |
cparata | 4:94c5d5546161 | 1070 | } |
cparata | 0:dff8803aace7 | 1071 | |
cparata | 4:94c5d5546161 | 1072 | /* Full scale selection */ |
cparata | 4:94c5d5546161 | 1073 | if (set_x_fs(2) != 0) { |
cparata | 4:94c5d5546161 | 1074 | return 1; |
cparata | 4:94c5d5546161 | 1075 | } |
cparata | 0:dff8803aace7 | 1076 | |
cparata | 4:94c5d5546161 | 1077 | /* WAKE_DUR setting */ |
cparata | 4:94c5d5546161 | 1078 | if (lis2dw12_wkup_dur_set(&_reg_ctx, 0x00) != 0) { |
cparata | 4:94c5d5546161 | 1079 | return 1; |
cparata | 4:94c5d5546161 | 1080 | } |
cparata | 0:dff8803aace7 | 1081 | |
cparata | 4:94c5d5546161 | 1082 | /* Set wake up threshold. */ |
cparata | 4:94c5d5546161 | 1083 | if (lis2dw12_wkup_threshold_set(&_reg_ctx, 0x02) != 0) { |
cparata | 4:94c5d5546161 | 1084 | return 1; |
cparata | 4:94c5d5546161 | 1085 | } |
cparata | 0:dff8803aace7 | 1086 | |
cparata | 4:94c5d5546161 | 1087 | if (lis2dw12_pin_int1_route_get(&_reg_ctx, &val) != 0) { |
cparata | 4:94c5d5546161 | 1088 | return 1; |
cparata | 4:94c5d5546161 | 1089 | } |
cparata | 0:dff8803aace7 | 1090 | |
cparata | 4:94c5d5546161 | 1091 | val.int1_wu = PROPERTY_ENABLE; |
cparata | 0:dff8803aace7 | 1092 | |
cparata | 4:94c5d5546161 | 1093 | if (lis2dw12_pin_int1_route_set(&_reg_ctx, &val) != 0) { |
cparata | 4:94c5d5546161 | 1094 | return 1; |
cparata | 4:94c5d5546161 | 1095 | } |
cparata | 0:dff8803aace7 | 1096 | |
cparata | 4:94c5d5546161 | 1097 | return ret; |
cparata | 0:dff8803aace7 | 1098 | } |
cparata | 0:dff8803aace7 | 1099 | |
cparata | 0:dff8803aace7 | 1100 | /** |
cparata | 0:dff8803aace7 | 1101 | * @brief Disable the wake up detection for LIS2DW12 accelerometer sensor |
cparata | 0:dff8803aace7 | 1102 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:dff8803aace7 | 1103 | */ |
cparata | 0:dff8803aace7 | 1104 | int LIS2DW12Sensor::disable_wake_up_detection(void) |
cparata | 0:dff8803aace7 | 1105 | { |
cparata | 4:94c5d5546161 | 1106 | lis2dw12_ctrl4_int1_pad_ctrl_t ctrl4_int1_reg; |
cparata | 4:94c5d5546161 | 1107 | lis2dw12_ctrl5_int2_pad_ctrl_t ctrl5_int2_reg; |
cparata | 4:94c5d5546161 | 1108 | lis2dw12_ctrl_reg7_t ctrl_reg7; |
cparata | 0:dff8803aace7 | 1109 | |
cparata | 4:94c5d5546161 | 1110 | /* Disable wake up event on INT1 pin. */ |
cparata | 4:94c5d5546161 | 1111 | if (lis2dw12_read_reg(&_reg_ctx, LIS2DW12_CTRL4_INT1_PAD_CTRL, (uint8_t *)&ctrl4_int1_reg, 1) != 0) { |
cparata | 4:94c5d5546161 | 1112 | return 1; |
cparata | 4:94c5d5546161 | 1113 | } |
cparata | 0:dff8803aace7 | 1114 | |
cparata | 4:94c5d5546161 | 1115 | ctrl4_int1_reg.int1_wu = PROPERTY_DISABLE; |
cparata | 0:dff8803aace7 | 1116 | |
cparata | 4:94c5d5546161 | 1117 | if (lis2dw12_write_reg(&_reg_ctx, LIS2DW12_CTRL4_INT1_PAD_CTRL, (uint8_t *)&ctrl4_int1_reg, 1) != 0) { |
cparata | 4:94c5d5546161 | 1118 | return 1; |
cparata | 4:94c5d5546161 | 1119 | } |
cparata | 0:dff8803aace7 | 1120 | |
cparata | 4:94c5d5546161 | 1121 | /* Read INT2 Sleep Change */ |
cparata | 4:94c5d5546161 | 1122 | if (lis2dw12_read_reg(&_reg_ctx, LIS2DW12_CTRL5_INT2_PAD_CTRL, (uint8_t *)&ctrl5_int2_reg, 1) != 0) { |
cparata | 4:94c5d5546161 | 1123 | return 1; |
cparata | 0:dff8803aace7 | 1124 | } |
cparata | 0:dff8803aace7 | 1125 | |
cparata | 4:94c5d5546161 | 1126 | /*Disable Interrupts bit if none event is still enabled */ |
cparata | 4:94c5d5546161 | 1127 | if (ctrl5_int2_reg.int2_sleep_chg == 0 && ctrl4_int1_reg.int1_wu == 0 && ctrl4_int1_reg.int1_6d == 0) { |
cparata | 4:94c5d5546161 | 1128 | if (lis2dw12_read_reg(&_reg_ctx, LIS2DW12_CTRL_REG7, (uint8_t *)&ctrl_reg7, 1) != 0) { |
cparata | 4:94c5d5546161 | 1129 | return 1; |
cparata | 4:94c5d5546161 | 1130 | } |
cparata | 0:dff8803aace7 | 1131 | |
cparata | 4:94c5d5546161 | 1132 | ctrl_reg7.interrupts_enable = PROPERTY_DISABLE; |
cparata | 0:dff8803aace7 | 1133 | |
cparata | 4:94c5d5546161 | 1134 | if (lis2dw12_write_reg(&_reg_ctx, LIS2DW12_CTRL_REG7, (uint8_t *)&ctrl_reg7, 1) != 0) { |
cparata | 4:94c5d5546161 | 1135 | return 1; |
cparata | 4:94c5d5546161 | 1136 | } |
cparata | 4:94c5d5546161 | 1137 | } |
cparata | 0:dff8803aace7 | 1138 | |
cparata | 4:94c5d5546161 | 1139 | /* Reset wake up threshold. */ |
cparata | 4:94c5d5546161 | 1140 | if (lis2dw12_wkup_threshold_set(&_reg_ctx, 0x00) != 0) { |
cparata | 4:94c5d5546161 | 1141 | return 1; |
cparata | 4:94c5d5546161 | 1142 | } |
cparata | 0:dff8803aace7 | 1143 | |
cparata | 4:94c5d5546161 | 1144 | /* WAKE_DUR setting */ |
cparata | 4:94c5d5546161 | 1145 | if (lis2dw12_wkup_dur_set(&_reg_ctx, 0x00) != 0) { |
cparata | 4:94c5d5546161 | 1146 | return 1; |
cparata | 4:94c5d5546161 | 1147 | } |
cparata | 4:94c5d5546161 | 1148 | |
cparata | 4:94c5d5546161 | 1149 | return 0; |
cparata | 0:dff8803aace7 | 1150 | } |
cparata | 0:dff8803aace7 | 1151 | |
cparata | 0:dff8803aace7 | 1152 | /** |
cparata | 0:dff8803aace7 | 1153 | * @brief Set the wake up threshold for LIS2DW12 accelerometer sensor |
cparata | 0:dff8803aace7 | 1154 | * @param thr the threshold to be set |
cparata | 0:dff8803aace7 | 1155 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:dff8803aace7 | 1156 | */ |
cparata | 0:dff8803aace7 | 1157 | int LIS2DW12Sensor::set_wake_up_threshold(uint8_t thr) |
cparata | 0:dff8803aace7 | 1158 | { |
cparata | 4:94c5d5546161 | 1159 | /* Set wake up threshold. */ |
cparata | 4:94c5d5546161 | 1160 | if (lis2dw12_wkup_threshold_set(&_reg_ctx, thr) != 0) { |
cparata | 4:94c5d5546161 | 1161 | return 1; |
cparata | 4:94c5d5546161 | 1162 | } |
cparata | 0:dff8803aace7 | 1163 | |
cparata | 4:94c5d5546161 | 1164 | return 0; |
cparata | 0:dff8803aace7 | 1165 | } |
cparata | 0:dff8803aace7 | 1166 | |
cparata | 0:dff8803aace7 | 1167 | /** |
cparata | 0:dff8803aace7 | 1168 | * @brief Set the wake up duration for LIS2DW12 accelerometer sensor |
cparata | 0:dff8803aace7 | 1169 | * @param dur the duration to be set |
cparata | 0:dff8803aace7 | 1170 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:dff8803aace7 | 1171 | */ |
cparata | 0:dff8803aace7 | 1172 | int LIS2DW12Sensor::set_wake_up_duration(uint8_t dur) |
cparata | 0:dff8803aace7 | 1173 | { |
cparata | 4:94c5d5546161 | 1174 | /* Set wake up duration. */ |
cparata | 4:94c5d5546161 | 1175 | if (lis2dw12_wkup_dur_set(&_reg_ctx, dur) != 0) { |
cparata | 4:94c5d5546161 | 1176 | return 1; |
cparata | 4:94c5d5546161 | 1177 | } |
cparata | 0:dff8803aace7 | 1178 | |
cparata | 4:94c5d5546161 | 1179 | return 0; |
cparata | 0:dff8803aace7 | 1180 | } |
cparata | 0:dff8803aace7 | 1181 | |
cparata | 0:dff8803aace7 | 1182 | /** |
cparata | 0:dff8803aace7 | 1183 | * @brief Enable the inactivity detection for LIS2DW12 accelerometer sensor |
cparata | 0:dff8803aace7 | 1184 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:dff8803aace7 | 1185 | */ |
cparata | 0:dff8803aace7 | 1186 | int LIS2DW12Sensor::enable_inactivity_detection(void) |
cparata | 0:dff8803aace7 | 1187 | { |
cparata | 4:94c5d5546161 | 1188 | int32_t ret = 0; |
cparata | 4:94c5d5546161 | 1189 | lis2dw12_ctrl5_int2_pad_ctrl_t val; |
cparata | 0:dff8803aace7 | 1190 | |
cparata | 4:94c5d5546161 | 1191 | /* Output Data Rate and Full scale must be selected externally */ |
cparata | 0:dff8803aace7 | 1192 | |
cparata | 4:94c5d5546161 | 1193 | /* SLEEP_DUR setting */ |
cparata | 4:94c5d5546161 | 1194 | if (lis2dw12_act_sleep_dur_set(&_reg_ctx, 0x01) != 0) { |
cparata | 4:94c5d5546161 | 1195 | return 1; |
cparata | 4:94c5d5546161 | 1196 | } |
cparata | 0:dff8803aace7 | 1197 | |
cparata | 4:94c5d5546161 | 1198 | /* Set wake up threshold. */ |
cparata | 4:94c5d5546161 | 1199 | if (lis2dw12_wkup_threshold_set(&_reg_ctx, 0x02) != 0) { |
cparata | 4:94c5d5546161 | 1200 | return 1; |
cparata | 4:94c5d5546161 | 1201 | } |
cparata | 0:dff8803aace7 | 1202 | |
cparata | 4:94c5d5546161 | 1203 | /* Enable inactivity detection. */ |
cparata | 4:94c5d5546161 | 1204 | if (lis2dw12_act_mode_set(&_reg_ctx, LIS2DW12_DETECT_ACT_INACT) != 0) { |
cparata | 4:94c5d5546161 | 1205 | return 1; |
cparata | 4:94c5d5546161 | 1206 | } |
cparata | 0:dff8803aace7 | 1207 | |
cparata | 4:94c5d5546161 | 1208 | if (lis2dw12_pin_int2_route_get(&_reg_ctx, &val) != 0) { |
cparata | 4:94c5d5546161 | 1209 | return 1; |
cparata | 4:94c5d5546161 | 1210 | } |
cparata | 0:dff8803aace7 | 1211 | |
cparata | 4:94c5d5546161 | 1212 | val.int2_sleep_chg = PROPERTY_ENABLE; |
cparata | 0:dff8803aace7 | 1213 | |
cparata | 4:94c5d5546161 | 1214 | if (lis2dw12_pin_int2_route_set(&_reg_ctx, &val) != 0) { |
cparata | 4:94c5d5546161 | 1215 | return 1; |
cparata | 4:94c5d5546161 | 1216 | } |
cparata | 0:dff8803aace7 | 1217 | |
cparata | 4:94c5d5546161 | 1218 | return ret; |
cparata | 0:dff8803aace7 | 1219 | } |
cparata | 0:dff8803aace7 | 1220 | |
cparata | 0:dff8803aace7 | 1221 | /** |
cparata | 0:dff8803aace7 | 1222 | * @brief Disable the inactivity detection for LIS2DW12 accelerometer sensor |
cparata | 0:dff8803aace7 | 1223 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:dff8803aace7 | 1224 | */ |
cparata | 0:dff8803aace7 | 1225 | int LIS2DW12Sensor::disable_inactivity_detection(void) |
cparata | 0:dff8803aace7 | 1226 | { |
cparata | 4:94c5d5546161 | 1227 | lis2dw12_ctrl4_int1_pad_ctrl_t ctrl4_int1_reg; |
cparata | 4:94c5d5546161 | 1228 | lis2dw12_ctrl5_int2_pad_ctrl_t ctrl5_int2_reg; |
cparata | 4:94c5d5546161 | 1229 | lis2dw12_ctrl_reg7_t ctrl_reg7; |
cparata | 0:dff8803aace7 | 1230 | |
cparata | 4:94c5d5546161 | 1231 | /* Disable inactivity event on INT2 pin */ |
cparata | 4:94c5d5546161 | 1232 | if (lis2dw12_read_reg(&_reg_ctx, LIS2DW12_CTRL5_INT2_PAD_CTRL, (uint8_t *)&ctrl5_int2_reg, 1) != 0) { |
cparata | 4:94c5d5546161 | 1233 | return 1; |
cparata | 4:94c5d5546161 | 1234 | } |
cparata | 0:dff8803aace7 | 1235 | |
cparata | 4:94c5d5546161 | 1236 | ctrl5_int2_reg.int2_sleep_chg = PROPERTY_DISABLE; |
cparata | 0:dff8803aace7 | 1237 | |
cparata | 4:94c5d5546161 | 1238 | if (lis2dw12_write_reg(&_reg_ctx, LIS2DW12_CTRL5_INT2_PAD_CTRL, (uint8_t *)&ctrl5_int2_reg, 1) != 0) { |
cparata | 4:94c5d5546161 | 1239 | return 1; |
cparata | 4:94c5d5546161 | 1240 | } |
cparata | 0:dff8803aace7 | 1241 | |
cparata | 4:94c5d5546161 | 1242 | /* Read INT1 Wake Up event and INT1 6D Orientation event */ |
cparata | 4:94c5d5546161 | 1243 | if (lis2dw12_read_reg(&_reg_ctx, LIS2DW12_CTRL4_INT1_PAD_CTRL, (uint8_t *)&ctrl4_int1_reg, 1) != 0) { |
cparata | 4:94c5d5546161 | 1244 | return 1; |
cparata | 0:dff8803aace7 | 1245 | } |
cparata | 0:dff8803aace7 | 1246 | |
cparata | 4:94c5d5546161 | 1247 | /*Disable Interrupts bit if none event is still enabled */ |
cparata | 4:94c5d5546161 | 1248 | if (ctrl5_int2_reg.int2_sleep_chg == 0 && ctrl4_int1_reg.int1_wu == 0 && ctrl4_int1_reg.int1_6d == 0) { |
cparata | 4:94c5d5546161 | 1249 | if (lis2dw12_read_reg(&_reg_ctx, LIS2DW12_CTRL_REG7, (uint8_t *)&ctrl_reg7, 1) != 0) { |
cparata | 4:94c5d5546161 | 1250 | return 1; |
cparata | 4:94c5d5546161 | 1251 | } |
cparata | 0:dff8803aace7 | 1252 | |
cparata | 4:94c5d5546161 | 1253 | ctrl_reg7.interrupts_enable = PROPERTY_DISABLE; |
cparata | 4:94c5d5546161 | 1254 | |
cparata | 4:94c5d5546161 | 1255 | if (lis2dw12_write_reg(&_reg_ctx, LIS2DW12_CTRL_REG7, (uint8_t *)&ctrl_reg7, 1) != 0) { |
cparata | 4:94c5d5546161 | 1256 | return 1; |
cparata | 4:94c5d5546161 | 1257 | } |
cparata | 0:dff8803aace7 | 1258 | } |
cparata | 0:dff8803aace7 | 1259 | |
cparata | 4:94c5d5546161 | 1260 | /* Disable inactivity detection. */ |
cparata | 4:94c5d5546161 | 1261 | if (lis2dw12_act_mode_set(&_reg_ctx, LIS2DW12_NO_DETECTION) != 0) { |
cparata | 4:94c5d5546161 | 1262 | return 1; |
cparata | 4:94c5d5546161 | 1263 | } |
cparata | 0:dff8803aace7 | 1264 | |
cparata | 4:94c5d5546161 | 1265 | /* Reset wake up threshold. */ |
cparata | 4:94c5d5546161 | 1266 | if (lis2dw12_wkup_threshold_set(&_reg_ctx, 0x00) != 0) { |
cparata | 4:94c5d5546161 | 1267 | return 1; |
cparata | 4:94c5d5546161 | 1268 | } |
cparata | 0:dff8803aace7 | 1269 | |
cparata | 4:94c5d5546161 | 1270 | /* SLEEP_DUR setting */ |
cparata | 4:94c5d5546161 | 1271 | if (lis2dw12_act_sleep_dur_set(&_reg_ctx, 0x00) != 0) { |
cparata | 4:94c5d5546161 | 1272 | return 1; |
cparata | 4:94c5d5546161 | 1273 | } |
cparata | 4:94c5d5546161 | 1274 | |
cparata | 4:94c5d5546161 | 1275 | return 0; |
cparata | 0:dff8803aace7 | 1276 | } |
cparata | 0:dff8803aace7 | 1277 | |
cparata | 0:dff8803aace7 | 1278 | /** |
cparata | 0:dff8803aace7 | 1279 | * @brief Set the sleep duration for LIS2DW12 accelerometer sensor |
cparata | 0:dff8803aace7 | 1280 | * @param dur the duration to be set |
cparata | 0:dff8803aace7 | 1281 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:dff8803aace7 | 1282 | */ |
cparata | 0:dff8803aace7 | 1283 | int LIS2DW12Sensor::set_sleep_duration(uint8_t dur) |
cparata | 0:dff8803aace7 | 1284 | { |
cparata | 4:94c5d5546161 | 1285 | /* Set sleep duration. */ |
cparata | 4:94c5d5546161 | 1286 | if (lis2dw12_act_sleep_dur_set(&_reg_ctx, dur) != 0) { |
cparata | 4:94c5d5546161 | 1287 | return 1; |
cparata | 4:94c5d5546161 | 1288 | } |
cparata | 0:dff8803aace7 | 1289 | |
cparata | 4:94c5d5546161 | 1290 | return 0; |
cparata | 0:dff8803aace7 | 1291 | } |
cparata | 0:dff8803aace7 | 1292 | |
cparata | 0:dff8803aace7 | 1293 | /** |
cparata | 0:dff8803aace7 | 1294 | * @brief Enable the 6D orientation detection for LIS2DW12 accelerometer sensor |
cparata | 0:dff8803aace7 | 1295 | * @note This function sets the LIS2DW12 accelerometer ODR to 200Hz and the LIS2DW12 accelerometer full scale to 2g |
cparata | 0:dff8803aace7 | 1296 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:dff8803aace7 | 1297 | */ |
cparata | 0:dff8803aace7 | 1298 | int LIS2DW12Sensor::enable_6d_orientation(void) |
cparata | 0:dff8803aace7 | 1299 | { |
cparata | 4:94c5d5546161 | 1300 | int32_t ret = 0; |
cparata | 4:94c5d5546161 | 1301 | lis2dw12_ctrl4_int1_pad_ctrl_t val; |
cparata | 0:dff8803aace7 | 1302 | |
cparata | 4:94c5d5546161 | 1303 | /* Output Data Rate selection */ |
cparata | 4:94c5d5546161 | 1304 | if (set_x_odr(200.0f) == 1) { |
cparata | 4:94c5d5546161 | 1305 | return 1; |
cparata | 4:94c5d5546161 | 1306 | } |
cparata | 4:94c5d5546161 | 1307 | |
cparata | 4:94c5d5546161 | 1308 | /* Full scale selection. */ |
cparata | 4:94c5d5546161 | 1309 | if (set_x_fs(2.0f) == 1) { |
cparata | 4:94c5d5546161 | 1310 | return 1; |
cparata | 4:94c5d5546161 | 1311 | } |
cparata | 0:dff8803aace7 | 1312 | |
cparata | 4:94c5d5546161 | 1313 | /* 6D orientation threshold. */ |
cparata | 4:94c5d5546161 | 1314 | if (lis2dw12_6d_threshold_set(&_reg_ctx, 2) != 0) { /* 60 degrees */ |
cparata | 4:94c5d5546161 | 1315 | return 1; |
cparata | 4:94c5d5546161 | 1316 | } |
cparata | 0:dff8803aace7 | 1317 | |
cparata | 4:94c5d5546161 | 1318 | /* Enable 6D orientation event on INT1 pin */ |
cparata | 4:94c5d5546161 | 1319 | if (lis2dw12_pin_int1_route_get(&_reg_ctx, &val) != 0) { |
cparata | 4:94c5d5546161 | 1320 | return 1; |
cparata | 4:94c5d5546161 | 1321 | } |
cparata | 0:dff8803aace7 | 1322 | |
cparata | 4:94c5d5546161 | 1323 | val.int1_6d = PROPERTY_ENABLE; |
cparata | 0:dff8803aace7 | 1324 | |
cparata | 4:94c5d5546161 | 1325 | if (lis2dw12_pin_int1_route_set(&_reg_ctx, &val) != 0) { |
cparata | 4:94c5d5546161 | 1326 | return 1; |
cparata | 4:94c5d5546161 | 1327 | } |
cparata | 0:dff8803aace7 | 1328 | |
cparata | 4:94c5d5546161 | 1329 | return ret; |
cparata | 0:dff8803aace7 | 1330 | } |
cparata | 0:dff8803aace7 | 1331 | |
cparata | 0:dff8803aace7 | 1332 | /** |
cparata | 0:dff8803aace7 | 1333 | * @brief Disable the 6D orientation detection for LIS2DW12 accelerometer sensor |
cparata | 0:dff8803aace7 | 1334 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:dff8803aace7 | 1335 | */ |
cparata | 0:dff8803aace7 | 1336 | int LIS2DW12Sensor::disable_6d_orientation(void) |
cparata | 0:dff8803aace7 | 1337 | { |
cparata | 4:94c5d5546161 | 1338 | lis2dw12_ctrl4_int1_pad_ctrl_t ctrl4_int1_reg; |
cparata | 4:94c5d5546161 | 1339 | lis2dw12_ctrl5_int2_pad_ctrl_t ctrl5_int2_reg; |
cparata | 4:94c5d5546161 | 1340 | lis2dw12_ctrl_reg7_t ctrl_reg7; |
cparata | 0:dff8803aace7 | 1341 | |
cparata | 4:94c5d5546161 | 1342 | /* Disable 6D orientation event on INT1 pin */ |
cparata | 4:94c5d5546161 | 1343 | if (lis2dw12_read_reg(&_reg_ctx, LIS2DW12_CTRL4_INT1_PAD_CTRL, (uint8_t *)&ctrl4_int1_reg, 1) != 0) { |
cparata | 4:94c5d5546161 | 1344 | return 1; |
cparata | 4:94c5d5546161 | 1345 | } |
cparata | 0:dff8803aace7 | 1346 | |
cparata | 4:94c5d5546161 | 1347 | ctrl4_int1_reg.int1_6d = PROPERTY_DISABLE; |
cparata | 0:dff8803aace7 | 1348 | |
cparata | 4:94c5d5546161 | 1349 | if (lis2dw12_write_reg(&_reg_ctx, LIS2DW12_CTRL4_INT1_PAD_CTRL, (uint8_t *)&ctrl4_int1_reg, 1) != 0) { |
cparata | 4:94c5d5546161 | 1350 | return 1; |
cparata | 4:94c5d5546161 | 1351 | } |
cparata | 0:dff8803aace7 | 1352 | |
cparata | 4:94c5d5546161 | 1353 | /* Read INT2 Sleep Change */ |
cparata | 4:94c5d5546161 | 1354 | if (lis2dw12_read_reg(&_reg_ctx, LIS2DW12_CTRL5_INT2_PAD_CTRL, (uint8_t *)&ctrl5_int2_reg, 1) != 0) { |
cparata | 4:94c5d5546161 | 1355 | return 1; |
cparata | 0:dff8803aace7 | 1356 | } |
cparata | 0:dff8803aace7 | 1357 | |
cparata | 4:94c5d5546161 | 1358 | /*Disable Interrupts bit if none event is still enabled */ |
cparata | 4:94c5d5546161 | 1359 | if (ctrl5_int2_reg.int2_sleep_chg == 0 && ctrl4_int1_reg.int1_wu == 0 && ctrl4_int1_reg.int1_6d == 0) { |
cparata | 4:94c5d5546161 | 1360 | if (lis2dw12_read_reg(&_reg_ctx, LIS2DW12_CTRL_REG7, (uint8_t *)&ctrl_reg7, 1) != 0) { |
cparata | 4:94c5d5546161 | 1361 | return 1; |
cparata | 4:94c5d5546161 | 1362 | } |
cparata | 0:dff8803aace7 | 1363 | |
cparata | 4:94c5d5546161 | 1364 | ctrl_reg7.interrupts_enable = PROPERTY_DISABLE; |
cparata | 0:dff8803aace7 | 1365 | |
cparata | 4:94c5d5546161 | 1366 | if (lis2dw12_write_reg(&_reg_ctx, LIS2DW12_CTRL_REG7, (uint8_t *)&ctrl_reg7, 1) != 0) { |
cparata | 4:94c5d5546161 | 1367 | return 1; |
cparata | 4:94c5d5546161 | 1368 | } |
cparata | 4:94c5d5546161 | 1369 | } |
cparata | 0:dff8803aace7 | 1370 | |
cparata | 4:94c5d5546161 | 1371 | /* Reset 6D orientation threshold. */ |
cparata | 4:94c5d5546161 | 1372 | if (lis2dw12_6d_threshold_set(&_reg_ctx, 0) != 0) { |
cparata | 4:94c5d5546161 | 1373 | return 1; |
cparata | 4:94c5d5546161 | 1374 | } |
cparata | 4:94c5d5546161 | 1375 | |
cparata | 4:94c5d5546161 | 1376 | return 0; |
cparata | 0:dff8803aace7 | 1377 | } |
cparata | 0:dff8803aace7 | 1378 | |
cparata | 0:dff8803aace7 | 1379 | /** |
cparata | 0:dff8803aace7 | 1380 | * @brief Set the 6D orientation threshold for LIS2DW12 accelerometer sensor |
cparata | 0:dff8803aace7 | 1381 | * @param thr the threshold to be set |
cparata | 0:dff8803aace7 | 1382 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:dff8803aace7 | 1383 | */ |
cparata | 0:dff8803aace7 | 1384 | int LIS2DW12Sensor::set_6d_orientation_threshold(uint8_t thr) |
cparata | 0:dff8803aace7 | 1385 | { |
cparata | 4:94c5d5546161 | 1386 | if (thr > 3) { |
cparata | 4:94c5d5546161 | 1387 | return 1; |
cparata | 4:94c5d5546161 | 1388 | } |
cparata | 0:dff8803aace7 | 1389 | |
cparata | 4:94c5d5546161 | 1390 | if (lis2dw12_6d_threshold_set(&_reg_ctx, thr) != 0) { |
cparata | 4:94c5d5546161 | 1391 | return 1; |
cparata | 4:94c5d5546161 | 1392 | } |
cparata | 0:dff8803aace7 | 1393 | |
cparata | 4:94c5d5546161 | 1394 | return 0; |
cparata | 0:dff8803aace7 | 1395 | } |
cparata | 0:dff8803aace7 | 1396 | |
cparata | 0:dff8803aace7 | 1397 | /** |
cparata | 0:dff8803aace7 | 1398 | * @brief Get the 6D orientation XL axis for LIS2DW12 accelerometer sensor |
cparata | 0:dff8803aace7 | 1399 | * @param xl the pointer to the 6D orientation XL axis |
cparata | 0:dff8803aace7 | 1400 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:dff8803aace7 | 1401 | */ |
cparata | 0:dff8803aace7 | 1402 | int LIS2DW12Sensor::get_6d_orientation_xl(uint8_t *xl) |
cparata | 0:dff8803aace7 | 1403 | { |
cparata | 4:94c5d5546161 | 1404 | lis2dw12_sixd_src_t data; |
cparata | 0:dff8803aace7 | 1405 | |
cparata | 4:94c5d5546161 | 1406 | if (lis2dw12_read_reg(&_reg_ctx, LIS2DW12_SIXD_SRC, (uint8_t *)&data, 1) != 0) { |
cparata | 4:94c5d5546161 | 1407 | return 1; |
cparata | 4:94c5d5546161 | 1408 | } |
cparata | 0:dff8803aace7 | 1409 | |
cparata | 4:94c5d5546161 | 1410 | *xl = data.xl; |
cparata | 0:dff8803aace7 | 1411 | |
cparata | 4:94c5d5546161 | 1412 | return 0; |
cparata | 0:dff8803aace7 | 1413 | } |
cparata | 0:dff8803aace7 | 1414 | |
cparata | 0:dff8803aace7 | 1415 | /** |
cparata | 0:dff8803aace7 | 1416 | * @brief Get the 6D orientation XH axis for LIS2DW12 accelerometer sensor |
cparata | 0:dff8803aace7 | 1417 | * @param xh the pointer to the 6D orientation XH axis |
cparata | 0:dff8803aace7 | 1418 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:dff8803aace7 | 1419 | */ |
cparata | 0:dff8803aace7 | 1420 | int LIS2DW12Sensor::get_6d_orientation_xh(uint8_t *xh) |
cparata | 0:dff8803aace7 | 1421 | { |
cparata | 4:94c5d5546161 | 1422 | lis2dw12_sixd_src_t data; |
cparata | 0:dff8803aace7 | 1423 | |
cparata | 4:94c5d5546161 | 1424 | if (lis2dw12_read_reg(&_reg_ctx, LIS2DW12_SIXD_SRC, (uint8_t *)&data, 1) != 0) { |
cparata | 4:94c5d5546161 | 1425 | return 1; |
cparata | 4:94c5d5546161 | 1426 | } |
cparata | 0:dff8803aace7 | 1427 | |
cparata | 4:94c5d5546161 | 1428 | *xh = data.xh; |
cparata | 0:dff8803aace7 | 1429 | |
cparata | 4:94c5d5546161 | 1430 | return 0; |
cparata | 0:dff8803aace7 | 1431 | } |
cparata | 0:dff8803aace7 | 1432 | |
cparata | 0:dff8803aace7 | 1433 | /** |
cparata | 0:dff8803aace7 | 1434 | * @brief Get the 6D orientation YL axis for LIS2DW12 accelerometer sensor |
cparata | 0:dff8803aace7 | 1435 | * @param yl the pointer to the 6D orientation YL axis |
cparata | 0:dff8803aace7 | 1436 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:dff8803aace7 | 1437 | */ |
cparata | 0:dff8803aace7 | 1438 | int LIS2DW12Sensor::get_6d_orientation_yl(uint8_t *yl) |
cparata | 0:dff8803aace7 | 1439 | { |
cparata | 4:94c5d5546161 | 1440 | lis2dw12_sixd_src_t data; |
cparata | 0:dff8803aace7 | 1441 | |
cparata | 4:94c5d5546161 | 1442 | if (lis2dw12_read_reg(&_reg_ctx, LIS2DW12_SIXD_SRC, (uint8_t *)&data, 1) != 0) { |
cparata | 4:94c5d5546161 | 1443 | return 1; |
cparata | 4:94c5d5546161 | 1444 | } |
cparata | 0:dff8803aace7 | 1445 | |
cparata | 4:94c5d5546161 | 1446 | *yl = data.yl; |
cparata | 0:dff8803aace7 | 1447 | |
cparata | 4:94c5d5546161 | 1448 | return 0; |
cparata | 0:dff8803aace7 | 1449 | } |
cparata | 0:dff8803aace7 | 1450 | |
cparata | 0:dff8803aace7 | 1451 | /** |
cparata | 0:dff8803aace7 | 1452 | * @brief Get the 6D orientation YH axis for LIS2DW12 accelerometer sensor |
cparata | 0:dff8803aace7 | 1453 | * @param yh the pointer to the 6D orientation YH axis |
cparata | 0:dff8803aace7 | 1454 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:dff8803aace7 | 1455 | */ |
cparata | 0:dff8803aace7 | 1456 | int LIS2DW12Sensor::get_6d_orientation_yh(uint8_t *yh) |
cparata | 0:dff8803aace7 | 1457 | { |
cparata | 4:94c5d5546161 | 1458 | lis2dw12_sixd_src_t data; |
cparata | 0:dff8803aace7 | 1459 | |
cparata | 4:94c5d5546161 | 1460 | if (lis2dw12_read_reg(&_reg_ctx, LIS2DW12_SIXD_SRC, (uint8_t *)&data, 1) != 0) { |
cparata | 4:94c5d5546161 | 1461 | return 1; |
cparata | 4:94c5d5546161 | 1462 | } |
cparata | 0:dff8803aace7 | 1463 | |
cparata | 4:94c5d5546161 | 1464 | *yh = data.yh; |
cparata | 0:dff8803aace7 | 1465 | |
cparata | 4:94c5d5546161 | 1466 | return 0; |
cparata | 0:dff8803aace7 | 1467 | } |
cparata | 0:dff8803aace7 | 1468 | |
cparata | 0:dff8803aace7 | 1469 | /** |
cparata | 0:dff8803aace7 | 1470 | * @brief Get the 6D orientation ZL axis for LIS2DW12 accelerometer sensor |
cparata | 0:dff8803aace7 | 1471 | * @param zl the pointer to the 6D orientation ZL axis |
cparata | 0:dff8803aace7 | 1472 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:dff8803aace7 | 1473 | */ |
cparata | 0:dff8803aace7 | 1474 | int LIS2DW12Sensor::get_6d_orientation_zl(uint8_t *zl) |
cparata | 0:dff8803aace7 | 1475 | { |
cparata | 4:94c5d5546161 | 1476 | lis2dw12_sixd_src_t data; |
cparata | 0:dff8803aace7 | 1477 | |
cparata | 4:94c5d5546161 | 1478 | if (lis2dw12_read_reg(&_reg_ctx, LIS2DW12_SIXD_SRC, (uint8_t *)&data, 1) != 0) { |
cparata | 4:94c5d5546161 | 1479 | return 1; |
cparata | 4:94c5d5546161 | 1480 | } |
cparata | 0:dff8803aace7 | 1481 | |
cparata | 4:94c5d5546161 | 1482 | *zl = data.zl; |
cparata | 0:dff8803aace7 | 1483 | |
cparata | 4:94c5d5546161 | 1484 | return 0; |
cparata | 0:dff8803aace7 | 1485 | } |
cparata | 0:dff8803aace7 | 1486 | |
cparata | 0:dff8803aace7 | 1487 | /** |
cparata | 0:dff8803aace7 | 1488 | * @brief Get the 6D orientation ZH axis for LIS2DW12 accelerometer sensor |
cparata | 0:dff8803aace7 | 1489 | * @param zh the pointer to the 6D orientation ZH axis |
cparata | 0:dff8803aace7 | 1490 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:dff8803aace7 | 1491 | */ |
cparata | 0:dff8803aace7 | 1492 | int LIS2DW12Sensor::get_6d_orientation_zh(uint8_t *zh) |
cparata | 0:dff8803aace7 | 1493 | { |
cparata | 4:94c5d5546161 | 1494 | lis2dw12_sixd_src_t data; |
cparata | 0:dff8803aace7 | 1495 | |
cparata | 4:94c5d5546161 | 1496 | if (lis2dw12_read_reg(&_reg_ctx, LIS2DW12_SIXD_SRC, (uint8_t *)&data, 1) != 0) { |
cparata | 4:94c5d5546161 | 1497 | return 1; |
cparata | 4:94c5d5546161 | 1498 | } |
cparata | 0:dff8803aace7 | 1499 | |
cparata | 4:94c5d5546161 | 1500 | *zh = data.zh; |
cparata | 0:dff8803aace7 | 1501 | |
cparata | 4:94c5d5546161 | 1502 | return 0; |
cparata | 0:dff8803aace7 | 1503 | } |
cparata | 0:dff8803aace7 | 1504 | |
cparata | 0:dff8803aace7 | 1505 | /** |
cparata | 0:dff8803aace7 | 1506 | * @brief Get the status of all hardware events for LIS2DW12 accelerometer sensor |
cparata | 0:dff8803aace7 | 1507 | * @param status the pointer to the status of all hardware events |
cparata | 0:dff8803aace7 | 1508 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:dff8803aace7 | 1509 | */ |
cparata | 0:dff8803aace7 | 1510 | int LIS2DW12Sensor::get_event_status(LIS2DW12_Event_Status_t *status) |
cparata | 0:dff8803aace7 | 1511 | { |
cparata | 4:94c5d5546161 | 1512 | lis2dw12_status_t status_reg; |
cparata | 4:94c5d5546161 | 1513 | lis2dw12_ctrl4_int1_pad_ctrl_t ctrl4_int1_reg; |
cparata | 4:94c5d5546161 | 1514 | lis2dw12_ctrl5_int2_pad_ctrl_t ctrl5_int2_reg; |
cparata | 0:dff8803aace7 | 1515 | |
cparata | 4:94c5d5546161 | 1516 | (void)memset((void *)status, 0x0, sizeof(LIS2DW12_Event_Status_t)); |
cparata | 0:dff8803aace7 | 1517 | |
cparata | 4:94c5d5546161 | 1518 | if (lis2dw12_read_reg(&_reg_ctx, LIS2DW12_STATUS, (uint8_t *)&status_reg, 1) != 0) { |
cparata | 4:94c5d5546161 | 1519 | return 1; |
cparata | 4:94c5d5546161 | 1520 | } |
cparata | 0:dff8803aace7 | 1521 | |
cparata | 4:94c5d5546161 | 1522 | if (lis2dw12_read_reg(&_reg_ctx, LIS2DW12_CTRL4_INT1_PAD_CTRL, (uint8_t *)&ctrl4_int1_reg, 1) != 0) { |
cparata | 4:94c5d5546161 | 1523 | return 1; |
cparata | 4:94c5d5546161 | 1524 | } |
cparata | 0:dff8803aace7 | 1525 | |
cparata | 4:94c5d5546161 | 1526 | if (lis2dw12_read_reg(&_reg_ctx, LIS2DW12_CTRL5_INT2_PAD_CTRL, (uint8_t *)&ctrl5_int2_reg, 1) != 0) { |
cparata | 4:94c5d5546161 | 1527 | return 1; |
cparata | 4:94c5d5546161 | 1528 | } |
cparata | 0:dff8803aace7 | 1529 | |
cparata | 4:94c5d5546161 | 1530 | if (ctrl4_int1_reg.int1_wu == 1U) { |
cparata | 4:94c5d5546161 | 1531 | if (status_reg.wu_ia == 1U) { |
cparata | 4:94c5d5546161 | 1532 | status->WakeUpStatus = 1; |
cparata | 4:94c5d5546161 | 1533 | } |
cparata | 0:dff8803aace7 | 1534 | } |
cparata | 0:dff8803aace7 | 1535 | |
cparata | 4:94c5d5546161 | 1536 | if (ctrl4_int1_reg.int1_6d == 1U) { |
cparata | 4:94c5d5546161 | 1537 | if (status_reg._6d_ia == 1U) { |
cparata | 4:94c5d5546161 | 1538 | status->D6DOrientationStatus = 1; |
cparata | 4:94c5d5546161 | 1539 | } |
cparata | 0:dff8803aace7 | 1540 | } |
cparata | 0:dff8803aace7 | 1541 | |
cparata | 4:94c5d5546161 | 1542 | if (ctrl5_int2_reg.int2_sleep_chg == 1U) { |
cparata | 4:94c5d5546161 | 1543 | if (status_reg.sleep_state == 1U) { |
cparata | 4:94c5d5546161 | 1544 | status->SleepStatus = 1; |
cparata | 4:94c5d5546161 | 1545 | } |
cparata | 0:dff8803aace7 | 1546 | } |
cparata | 0:dff8803aace7 | 1547 | |
cparata | 4:94c5d5546161 | 1548 | return 0; |
cparata | 0:dff8803aace7 | 1549 | } |
cparata | 0:dff8803aace7 | 1550 | |
cparata | 0:dff8803aace7 | 1551 | /** |
cparata | 0:dff8803aace7 | 1552 | * @brief Get the number of samples contained into the FIFO |
cparata | 0:dff8803aace7 | 1553 | * @param num_samples the number of samples contained into the FIFO |
cparata | 0:dff8803aace7 | 1554 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:dff8803aace7 | 1555 | */ |
cparata | 0:dff8803aace7 | 1556 | int LIS2DW12Sensor::get_fifo_num_samples(uint16_t *num_samples) |
cparata | 0:dff8803aace7 | 1557 | { |
cparata | 4:94c5d5546161 | 1558 | lis2dw12_fifo_samples_t fifo_samples; |
cparata | 0:dff8803aace7 | 1559 | |
cparata | 4:94c5d5546161 | 1560 | if (lis2dw12_read_reg(&_reg_ctx, LIS2DW12_FIFO_SAMPLES, (uint8_t *)&fifo_samples, 1) != 0) { |
cparata | 4:94c5d5546161 | 1561 | return 1; |
cparata | 4:94c5d5546161 | 1562 | } |
cparata | 0:dff8803aace7 | 1563 | |
cparata | 4:94c5d5546161 | 1564 | if (fifo_samples.diff == 0x20) { |
cparata | 4:94c5d5546161 | 1565 | *num_samples = 32; |
cparata | 4:94c5d5546161 | 1566 | } else { |
cparata | 4:94c5d5546161 | 1567 | *num_samples = fifo_samples.diff; |
cparata | 4:94c5d5546161 | 1568 | } |
cparata | 0:dff8803aace7 | 1569 | |
cparata | 4:94c5d5546161 | 1570 | return 0; |
cparata | 0:dff8803aace7 | 1571 | } |
cparata | 0:dff8803aace7 | 1572 | |
cparata | 0:dff8803aace7 | 1573 | /** |
cparata | 0:dff8803aace7 | 1574 | * @brief Set the FIFO mode |
cparata | 0:dff8803aace7 | 1575 | * @param mode FIFO mode |
cparata | 0:dff8803aace7 | 1576 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:dff8803aace7 | 1577 | */ |
cparata | 0:dff8803aace7 | 1578 | int LIS2DW12Sensor::set_fifo_mode(uint8_t mode) |
cparata | 0:dff8803aace7 | 1579 | { |
cparata | 4:94c5d5546161 | 1580 | int32_t ret = 0; |
cparata | 0:dff8803aace7 | 1581 | |
cparata | 4:94c5d5546161 | 1582 | /* Verify that the passed parameter contains one of the valid values. */ |
cparata | 4:94c5d5546161 | 1583 | switch ((lis2dw12_fmode_t)mode) { |
cparata | 4:94c5d5546161 | 1584 | case LIS2DW12_BYPASS_MODE: |
cparata | 4:94c5d5546161 | 1585 | case LIS2DW12_FIFO_MODE: |
cparata | 4:94c5d5546161 | 1586 | case LIS2DW12_STREAM_TO_FIFO_MODE: |
cparata | 4:94c5d5546161 | 1587 | case LIS2DW12_BYPASS_TO_STREAM_MODE: |
cparata | 4:94c5d5546161 | 1588 | case LIS2DW12_STREAM_MODE: |
cparata | 4:94c5d5546161 | 1589 | break; |
cparata | 0:dff8803aace7 | 1590 | |
cparata | 4:94c5d5546161 | 1591 | default: |
cparata | 4:94c5d5546161 | 1592 | ret = 1; |
cparata | 4:94c5d5546161 | 1593 | break; |
cparata | 4:94c5d5546161 | 1594 | } |
cparata | 0:dff8803aace7 | 1595 | |
cparata | 4:94c5d5546161 | 1596 | if (ret == 1) { |
cparata | 4:94c5d5546161 | 1597 | return ret; |
cparata | 4:94c5d5546161 | 1598 | } |
cparata | 4:94c5d5546161 | 1599 | |
cparata | 4:94c5d5546161 | 1600 | if (lis2dw12_fifo_mode_set(&_reg_ctx, (lis2dw12_fmode_t)mode) != 0) { |
cparata | 4:94c5d5546161 | 1601 | return 1; |
cparata | 4:94c5d5546161 | 1602 | } |
cparata | 4:94c5d5546161 | 1603 | |
cparata | 0:dff8803aace7 | 1604 | return ret; |
cparata | 0:dff8803aace7 | 1605 | } |
cparata | 0:dff8803aace7 | 1606 | |
cparata | 0:dff8803aace7 | 1607 | /** |
cparata | 0:dff8803aace7 | 1608 | * @brief Read the data from register |
cparata | 0:dff8803aace7 | 1609 | * @param reg register address |
cparata | 0:dff8803aace7 | 1610 | * @param data register data |
cparata | 0:dff8803aace7 | 1611 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:dff8803aace7 | 1612 | */ |
cparata | 0:dff8803aace7 | 1613 | int LIS2DW12Sensor::read_reg(uint8_t reg, uint8_t *data) |
cparata | 0:dff8803aace7 | 1614 | { |
cparata | 0:dff8803aace7 | 1615 | |
cparata | 4:94c5d5546161 | 1616 | if (lis2dw12_read_reg(&_reg_ctx, reg, data, 1) != 0) { |
cparata | 4:94c5d5546161 | 1617 | return 1; |
cparata | 4:94c5d5546161 | 1618 | } |
cparata | 0:dff8803aace7 | 1619 | |
cparata | 4:94c5d5546161 | 1620 | return 0; |
cparata | 0:dff8803aace7 | 1621 | } |
cparata | 0:dff8803aace7 | 1622 | |
cparata | 0:dff8803aace7 | 1623 | /** |
cparata | 0:dff8803aace7 | 1624 | * @brief Write the data to register |
cparata | 0:dff8803aace7 | 1625 | * @param reg register address |
cparata | 0:dff8803aace7 | 1626 | * @param data register data |
cparata | 0:dff8803aace7 | 1627 | * @retval 0 in case of success, an error code otherwise |
cparata | 0:dff8803aace7 | 1628 | */ |
cparata | 0:dff8803aace7 | 1629 | int LIS2DW12Sensor::write_reg(uint8_t reg, uint8_t data) |
cparata | 0:dff8803aace7 | 1630 | { |
cparata | 0:dff8803aace7 | 1631 | |
cparata | 4:94c5d5546161 | 1632 | if (lis2dw12_write_reg(&_reg_ctx, reg, &data, 1) != 0) { |
cparata | 4:94c5d5546161 | 1633 | return 1; |
cparata | 4:94c5d5546161 | 1634 | } |
cparata | 0:dff8803aace7 | 1635 | |
cparata | 4:94c5d5546161 | 1636 | return 0; |
cparata | 0:dff8803aace7 | 1637 | } |
cparata | 0:dff8803aace7 | 1638 | |
cparata | 0:dff8803aace7 | 1639 | |
cparata | 0:dff8803aace7 | 1640 | int32_t LIS2DW12_io_write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite) |
cparata | 0:dff8803aace7 | 1641 | { |
cparata | 4:94c5d5546161 | 1642 | return ((LIS2DW12Sensor *)handle)->io_write(pBuffer, WriteAddr, nBytesToWrite); |
cparata | 0:dff8803aace7 | 1643 | } |
cparata | 0:dff8803aace7 | 1644 | |
cparata | 0:dff8803aace7 | 1645 | int32_t LIS2DW12_io_read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead) |
cparata | 0:dff8803aace7 | 1646 | { |
cparata | 4:94c5d5546161 | 1647 | return ((LIS2DW12Sensor *)handle)->io_read(pBuffer, ReadAddr, nBytesToRead); |
cparata | 0:dff8803aace7 | 1648 | } |