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