Added SPI3w support through DevSPI
Dependencies: ST_INTERFACES X_NUCLEO_COMMON_3W
Fork of LPS22HB by
LPS22HBSensor.cpp@2:fba15124c67b, 2017-09-26 (annotated)
- Committer:
- mapellil
- Date:
- Tue Sep 26 08:18:06 2017 +0000
- Revision:
- 2:fba15124c67b
- Parent:
- 1:8813c9be1720
Removed DevSPI dependency, now SPI3W is supported directly through SPI
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| nikapov | 0:3de9cb936fd1 | 1 | /** |
| nikapov | 0:3de9cb936fd1 | 2 | ****************************************************************************** |
| nikapov | 0:3de9cb936fd1 | 3 | * @file LPS22HBSensor.cpp |
| nikapov | 0:3de9cb936fd1 | 4 | * @author CLab |
| nikapov | 0:3de9cb936fd1 | 5 | * @version V1.0.0 |
| nikapov | 0:3de9cb936fd1 | 6 | * @date 5 August 2016 |
| nikapov | 0:3de9cb936fd1 | 7 | * @brief Implementation of an LPS22HB Pressure sensor. |
| nikapov | 0:3de9cb936fd1 | 8 | ****************************************************************************** |
| nikapov | 0:3de9cb936fd1 | 9 | * @attention |
| nikapov | 0:3de9cb936fd1 | 10 | * |
| nikapov | 0:3de9cb936fd1 | 11 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
| nikapov | 0:3de9cb936fd1 | 12 | * |
| nikapov | 0:3de9cb936fd1 | 13 | * Redistribution and use in source and binary forms, with or without modification, |
| nikapov | 0:3de9cb936fd1 | 14 | * are permitted provided that the following conditions are met: |
| nikapov | 0:3de9cb936fd1 | 15 | * 1. Redistributions of source code must retain the above copyright notice, |
| nikapov | 0:3de9cb936fd1 | 16 | * this list of conditions and the following disclaimer. |
| nikapov | 0:3de9cb936fd1 | 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| nikapov | 0:3de9cb936fd1 | 18 | * this list of conditions and the following disclaimer in the documentation |
| nikapov | 0:3de9cb936fd1 | 19 | * and/or other materials provided with the distribution. |
| nikapov | 0:3de9cb936fd1 | 20 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
| nikapov | 0:3de9cb936fd1 | 21 | * may be used to endorse or promote products derived from this software |
| nikapov | 0:3de9cb936fd1 | 22 | * without specific prior written permission. |
| nikapov | 0:3de9cb936fd1 | 23 | * |
| nikapov | 0:3de9cb936fd1 | 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| nikapov | 0:3de9cb936fd1 | 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| nikapov | 0:3de9cb936fd1 | 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| nikapov | 0:3de9cb936fd1 | 27 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| nikapov | 0:3de9cb936fd1 | 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| nikapov | 0:3de9cb936fd1 | 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| nikapov | 0:3de9cb936fd1 | 30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| nikapov | 0:3de9cb936fd1 | 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| nikapov | 0:3de9cb936fd1 | 32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| nikapov | 0:3de9cb936fd1 | 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| nikapov | 0:3de9cb936fd1 | 34 | * |
| nikapov | 0:3de9cb936fd1 | 35 | ****************************************************************************** |
| nikapov | 0:3de9cb936fd1 | 36 | */ |
| nikapov | 0:3de9cb936fd1 | 37 | |
| nikapov | 0:3de9cb936fd1 | 38 | |
| nikapov | 0:3de9cb936fd1 | 39 | /* Includes ------------------------------------------------------------------*/ |
| mapellil | 1:8813c9be1720 | 40 | #include "mbed.h" |
| mapellil | 1:8813c9be1720 | 41 | //#include "SPI3W.h" |
| mapellil | 1:8813c9be1720 | 42 | #include "SPI.h" |
| mapellil | 1:8813c9be1720 | 43 | #include "DevI2C.h" |
| nikapov | 0:3de9cb936fd1 | 44 | #include "LPS22HBSensor.h" |
| mapellil | 1:8813c9be1720 | 45 | #include "LPS22HB_driver.h" |
| mapellil | 1:8813c9be1720 | 46 | #include <assert.h> |
| nikapov | 0:3de9cb936fd1 | 47 | |
| nikapov | 0:3de9cb936fd1 | 48 | /* Class Implementation ------------------------------------------------------*/ |
| nikapov | 0:3de9cb936fd1 | 49 | |
| mapellil | 2:fba15124c67b | 50 | LPS22HBSensor::LPS22HBSensor(SPI *spi, PinName cs_pin, PinName int_pin, SPI_type_t spi_type) : _dev_spi(spi), _cs_pin(cs_pin), _int_pin(int_pin), _spi_type(spi_type) |
| mapellil | 1:8813c9be1720 | 51 | { |
| mapellil | 1:8813c9be1720 | 52 | assert (spi); |
| mapellil | 1:8813c9be1720 | 53 | if (cs_pin == NC) |
| mapellil | 1:8813c9be1720 | 54 | { |
| mapellil | 1:8813c9be1720 | 55 | printf ("ERROR LPS22HBSensor CS MUST NOT BE NC\n\r"); |
| mapellil | 1:8813c9be1720 | 56 | _dev_spi = NULL; |
| mapellil | 1:8813c9be1720 | 57 | _dev_i2c=NULL; |
| mapellil | 1:8813c9be1720 | 58 | return; |
| mapellil | 1:8813c9be1720 | 59 | } |
| mapellil | 1:8813c9be1720 | 60 | |
| mapellil | 1:8813c9be1720 | 61 | _cs_pin = 1; |
| mapellil | 1:8813c9be1720 | 62 | _dev_i2c=NULL; |
| mapellil | 1:8813c9be1720 | 63 | |
| mapellil | 2:fba15124c67b | 64 | if (_spi_type == SPI3W) LPS22HB_Set_SpiInterface ((void *)this, LPS22HB_SPI_3_WIRE); |
| mapellil | 2:fba15124c67b | 65 | else if (_spi_type == SPI4W) LPS22HB_Set_SpiInterface ((void *)this, LPS22HB_SPI_4_WIRE); |
| mapellil | 1:8813c9be1720 | 66 | |
| mapellil | 1:8813c9be1720 | 67 | LPS22HB_Set_I2C ((void *)this, LPS22HB_DISABLE); |
| mapellil | 1:8813c9be1720 | 68 | } |
| mapellil | 1:8813c9be1720 | 69 | |
| nikapov | 0:3de9cb936fd1 | 70 | /** Constructor |
| nikapov | 0:3de9cb936fd1 | 71 | * @param i2c object of an helper class which handles the I2C peripheral |
| nikapov | 0:3de9cb936fd1 | 72 | * @param address the address of the component's instance |
| nikapov | 0:3de9cb936fd1 | 73 | */ |
| mapellil | 1:8813c9be1720 | 74 | LPS22HBSensor::LPS22HBSensor(DevI2C *i2c, uint8_t address, PinName int_pin) : |
| mapellil | 1:8813c9be1720 | 75 | _dev_i2c(i2c), _address(address), _cs_pin(NC), _int_pin(int_pin) |
| nikapov | 0:3de9cb936fd1 | 76 | { |
| mapellil | 1:8813c9be1720 | 77 | assert (i2c); |
| mapellil | 1:8813c9be1720 | 78 | _dev_spi = NULL; |
| mapellil | 1:8813c9be1720 | 79 | LPS22HB_Set_I2C ((void *)this, LPS22HB_ENABLE); |
| nikapov | 0:3de9cb936fd1 | 80 | }; |
| nikapov | 0:3de9cb936fd1 | 81 | |
| nikapov | 0:3de9cb936fd1 | 82 | |
| nikapov | 0:3de9cb936fd1 | 83 | /** |
| nikapov | 0:3de9cb936fd1 | 84 | * @brief Initializing the component. |
| nikapov | 0:3de9cb936fd1 | 85 | * @param[in] init pointer to device specific initalization structure. |
| nikapov | 0:3de9cb936fd1 | 86 | * @retval "0" in case of success, an error code otherwise. |
| nikapov | 0:3de9cb936fd1 | 87 | */ |
| nikapov | 0:3de9cb936fd1 | 88 | int LPS22HBSensor::init(void *init) |
| nikapov | 0:3de9cb936fd1 | 89 | { |
| nikapov | 0:3de9cb936fd1 | 90 | if ( LPS22HB_Set_PowerMode( (void *)this, LPS22HB_LowPower) == LPS22HB_ERROR ) |
| nikapov | 0:3de9cb936fd1 | 91 | { |
| nikapov | 0:3de9cb936fd1 | 92 | return 1; |
| nikapov | 0:3de9cb936fd1 | 93 | } |
| nikapov | 0:3de9cb936fd1 | 94 | |
| nikapov | 0:3de9cb936fd1 | 95 | /* Power down the device */ |
| nikapov | 0:3de9cb936fd1 | 96 | if ( LPS22HB_Set_Odr( (void *)this, LPS22HB_ODR_ONE_SHOT ) == LPS22HB_ERROR ) |
| mapellil | 1:8813c9be1720 | 97 | // if ( LPS22HB_Set_Odr( (void *)this, LPS22HB_ODR_25HZ ) == LPS22HB_ERROR ) |
| nikapov | 0:3de9cb936fd1 | 98 | { |
| nikapov | 0:3de9cb936fd1 | 99 | return 1; |
| nikapov | 0:3de9cb936fd1 | 100 | } |
| nikapov | 0:3de9cb936fd1 | 101 | |
| nikapov | 0:3de9cb936fd1 | 102 | /* Disable low-pass filter on LPS22HB pressure data */ |
| nikapov | 0:3de9cb936fd1 | 103 | if( LPS22HB_Set_LowPassFilter( (void *)this, LPS22HB_DISABLE) == LPS22HB_ERROR ) |
| nikapov | 0:3de9cb936fd1 | 104 | { |
| nikapov | 0:3de9cb936fd1 | 105 | return 1; |
| nikapov | 0:3de9cb936fd1 | 106 | } |
| nikapov | 0:3de9cb936fd1 | 107 | |
| nikapov | 0:3de9cb936fd1 | 108 | /* Set low-pass filter cutoff configuration*/ |
| nikapov | 0:3de9cb936fd1 | 109 | if( LPS22HB_Set_LowPassFilterCutoff( (void *)this, LPS22HB_ODR_9) == LPS22HB_ERROR ) |
| nikapov | 0:3de9cb936fd1 | 110 | { |
| nikapov | 0:3de9cb936fd1 | 111 | return 1; |
| nikapov | 0:3de9cb936fd1 | 112 | } |
| nikapov | 0:3de9cb936fd1 | 113 | |
| nikapov | 0:3de9cb936fd1 | 114 | /* Set block data update mode */ |
| nikapov | 0:3de9cb936fd1 | 115 | if ( LPS22HB_Set_Bdu( (void *)this, LPS22HB_BDU_NO_UPDATE ) == LPS22HB_ERROR ) |
| nikapov | 0:3de9cb936fd1 | 116 | { |
| nikapov | 0:3de9cb936fd1 | 117 | return 1; |
| nikapov | 0:3de9cb936fd1 | 118 | } |
| nikapov | 0:3de9cb936fd1 | 119 | |
| nikapov | 0:3de9cb936fd1 | 120 | /* Set automatic increment for multi-byte read/write */ |
| nikapov | 0:3de9cb936fd1 | 121 | if( LPS22HB_Set_AutomaticIncrementRegAddress( (void *)this, LPS22HB_ENABLE) == LPS22HB_ERROR ) |
| nikapov | 0:3de9cb936fd1 | 122 | { |
| nikapov | 0:3de9cb936fd1 | 123 | return 1; |
| nikapov | 0:3de9cb936fd1 | 124 | } |
| nikapov | 0:3de9cb936fd1 | 125 | |
| nikapov | 0:3de9cb936fd1 | 126 | _is_enabled = 0; |
| nikapov | 0:3de9cb936fd1 | 127 | _last_odr = 25.0f; |
| nikapov | 0:3de9cb936fd1 | 128 | |
| nikapov | 0:3de9cb936fd1 | 129 | return 0; |
| nikapov | 0:3de9cb936fd1 | 130 | } |
| nikapov | 0:3de9cb936fd1 | 131 | |
| nikapov | 0:3de9cb936fd1 | 132 | |
| nikapov | 0:3de9cb936fd1 | 133 | /** |
| nikapov | 0:3de9cb936fd1 | 134 | * @brief Enable LPS22HB |
| nikapov | 0:3de9cb936fd1 | 135 | * @retval 0 in case of success, an error code otherwise |
| nikapov | 0:3de9cb936fd1 | 136 | */ |
| nikapov | 0:3de9cb936fd1 | 137 | int LPS22HBSensor::enable(void) |
| nikapov | 0:3de9cb936fd1 | 138 | { |
| nikapov | 0:3de9cb936fd1 | 139 | /* Check if the component is already enabled */ |
| nikapov | 0:3de9cb936fd1 | 140 | if ( _is_enabled == 1 ) |
| nikapov | 0:3de9cb936fd1 | 141 | { |
| nikapov | 0:3de9cb936fd1 | 142 | return 0; |
| nikapov | 0:3de9cb936fd1 | 143 | } |
| nikapov | 0:3de9cb936fd1 | 144 | |
| nikapov | 0:3de9cb936fd1 | 145 | if(Set_ODR_When_Enabled(_last_odr) == 1) |
| nikapov | 0:3de9cb936fd1 | 146 | { |
| nikapov | 0:3de9cb936fd1 | 147 | return 1; |
| nikapov | 0:3de9cb936fd1 | 148 | } |
| nikapov | 0:3de9cb936fd1 | 149 | |
| nikapov | 0:3de9cb936fd1 | 150 | _is_enabled = 1; |
| nikapov | 0:3de9cb936fd1 | 151 | |
| nikapov | 0:3de9cb936fd1 | 152 | return 0; |
| nikapov | 0:3de9cb936fd1 | 153 | } |
| nikapov | 0:3de9cb936fd1 | 154 | |
| nikapov | 0:3de9cb936fd1 | 155 | /** |
| nikapov | 0:3de9cb936fd1 | 156 | * @brief Disable LPS22HB |
| nikapov | 0:3de9cb936fd1 | 157 | * @retval 0 in case of success, an error code otherwise |
| nikapov | 0:3de9cb936fd1 | 158 | */ |
| nikapov | 0:3de9cb936fd1 | 159 | int LPS22HBSensor::disable(void) |
| nikapov | 0:3de9cb936fd1 | 160 | { |
| nikapov | 0:3de9cb936fd1 | 161 | /* Check if the component is already disabled */ |
| nikapov | 0:3de9cb936fd1 | 162 | if ( _is_enabled == 0 ) |
| nikapov | 0:3de9cb936fd1 | 163 | { |
| nikapov | 0:3de9cb936fd1 | 164 | return 0; |
| nikapov | 0:3de9cb936fd1 | 165 | } |
| nikapov | 0:3de9cb936fd1 | 166 | |
| nikapov | 0:3de9cb936fd1 | 167 | /* Power down the device */ |
| nikapov | 0:3de9cb936fd1 | 168 | if ( LPS22HB_Set_Odr( (void *)this, LPS22HB_ODR_ONE_SHOT ) == LPS22HB_ERROR ) |
| nikapov | 0:3de9cb936fd1 | 169 | { |
| nikapov | 0:3de9cb936fd1 | 170 | return 1; |
| nikapov | 0:3de9cb936fd1 | 171 | } |
| nikapov | 0:3de9cb936fd1 | 172 | |
| nikapov | 0:3de9cb936fd1 | 173 | _is_enabled = 0; |
| nikapov | 0:3de9cb936fd1 | 174 | |
| nikapov | 0:3de9cb936fd1 | 175 | return 0; |
| nikapov | 0:3de9cb936fd1 | 176 | } |
| nikapov | 0:3de9cb936fd1 | 177 | |
| nikapov | 0:3de9cb936fd1 | 178 | /** |
| nikapov | 0:3de9cb936fd1 | 179 | * @brief Read ID address of LPS22HB |
| nikapov | 0:3de9cb936fd1 | 180 | * @param id the pointer where the ID of the device is stored |
| nikapov | 0:3de9cb936fd1 | 181 | * @retval 0 in case of success, an error code otherwise |
| nikapov | 0:3de9cb936fd1 | 182 | */ |
| nikapov | 0:3de9cb936fd1 | 183 | int LPS22HBSensor::read_id(uint8_t *id) |
| nikapov | 0:3de9cb936fd1 | 184 | { |
| nikapov | 0:3de9cb936fd1 | 185 | if(!id) |
| nikapov | 0:3de9cb936fd1 | 186 | { |
| nikapov | 0:3de9cb936fd1 | 187 | return 1; |
| nikapov | 0:3de9cb936fd1 | 188 | } |
| mapellil | 1:8813c9be1720 | 189 | |
| nikapov | 0:3de9cb936fd1 | 190 | /* Read WHO AM I register */ |
| nikapov | 0:3de9cb936fd1 | 191 | if ( LPS22HB_Get_DeviceID( (void *)this, id ) == LPS22HB_ERROR ) |
| nikapov | 0:3de9cb936fd1 | 192 | { |
| nikapov | 0:3de9cb936fd1 | 193 | return 1; |
| nikapov | 0:3de9cb936fd1 | 194 | } |
| nikapov | 0:3de9cb936fd1 | 195 | |
| nikapov | 0:3de9cb936fd1 | 196 | return 0; |
| nikapov | 0:3de9cb936fd1 | 197 | } |
| nikapov | 0:3de9cb936fd1 | 198 | |
| nikapov | 0:3de9cb936fd1 | 199 | /** |
| nikapov | 0:3de9cb936fd1 | 200 | * @brief Reboot memory content of LPS22HB |
| nikapov | 0:3de9cb936fd1 | 201 | * @param None |
| nikapov | 0:3de9cb936fd1 | 202 | * @retval 0 in case of success, an error code otherwise |
| nikapov | 0:3de9cb936fd1 | 203 | */ |
| nikapov | 0:3de9cb936fd1 | 204 | int LPS22HBSensor::reset(void) |
| nikapov | 0:3de9cb936fd1 | 205 | { |
| nikapov | 0:3de9cb936fd1 | 206 | /* Read WHO AM I register */ |
| nikapov | 0:3de9cb936fd1 | 207 | if ( LPS22HB_MemoryBoot((void *)this) == LPS22HB_ERROR ) |
| nikapov | 0:3de9cb936fd1 | 208 | { |
| nikapov | 0:3de9cb936fd1 | 209 | return 1; |
| nikapov | 0:3de9cb936fd1 | 210 | } |
| nikapov | 0:3de9cb936fd1 | 211 | |
| nikapov | 0:3de9cb936fd1 | 212 | return 0; |
| nikapov | 0:3de9cb936fd1 | 213 | } |
| nikapov | 0:3de9cb936fd1 | 214 | |
| nikapov | 0:3de9cb936fd1 | 215 | /** |
| nikapov | 0:3de9cb936fd1 | 216 | * @brief Read LPS22HB output register, and calculate the pressure in mbar |
| nikapov | 0:3de9cb936fd1 | 217 | * @param pfData the pressure value in mbar |
| nikapov | 0:3de9cb936fd1 | 218 | * @retval 0 in case of success, an error code otherwise |
| nikapov | 0:3de9cb936fd1 | 219 | */ |
| nikapov | 0:3de9cb936fd1 | 220 | int LPS22HBSensor::get_pressure(float* pfData) |
| nikapov | 0:3de9cb936fd1 | 221 | { |
| nikapov | 0:3de9cb936fd1 | 222 | int32_t int32data = 0; |
| nikapov | 0:3de9cb936fd1 | 223 | |
| nikapov | 0:3de9cb936fd1 | 224 | /* Read data from LPS22HB. */ |
| nikapov | 0:3de9cb936fd1 | 225 | if ( LPS22HB_Get_Pressure( (void *)this, &int32data ) == LPS22HB_ERROR ) |
| nikapov | 0:3de9cb936fd1 | 226 | { |
| nikapov | 0:3de9cb936fd1 | 227 | return 1; |
| nikapov | 0:3de9cb936fd1 | 228 | } |
| nikapov | 0:3de9cb936fd1 | 229 | |
| nikapov | 0:3de9cb936fd1 | 230 | *pfData = ( float )int32data / 100.0f; |
| nikapov | 0:3de9cb936fd1 | 231 | |
| nikapov | 0:3de9cb936fd1 | 232 | return 0; |
| nikapov | 0:3de9cb936fd1 | 233 | } |
| nikapov | 0:3de9cb936fd1 | 234 | |
| nikapov | 0:3de9cb936fd1 | 235 | /** |
| nikapov | 0:3de9cb936fd1 | 236 | * @brief Read LPS22HB output register, and calculate the temperature |
| nikapov | 0:3de9cb936fd1 | 237 | * @param pfData the temperature value |
| nikapov | 0:3de9cb936fd1 | 238 | * @retval 0 in case of success, an error code otherwise |
| nikapov | 0:3de9cb936fd1 | 239 | */ |
| nikapov | 0:3de9cb936fd1 | 240 | int LPS22HBSensor::get_temperature(float *pfData) |
| nikapov | 0:3de9cb936fd1 | 241 | { |
| nikapov | 0:3de9cb936fd1 | 242 | int16_t int16data = 0; |
| mapellil | 1:8813c9be1720 | 243 | |
| nikapov | 0:3de9cb936fd1 | 244 | /* Read data from LPS22HB. */ |
| nikapov | 0:3de9cb936fd1 | 245 | if ( LPS22HB_Get_Temperature( (void *)this, &int16data ) == LPS22HB_ERROR ) |
| nikapov | 0:3de9cb936fd1 | 246 | { |
| nikapov | 0:3de9cb936fd1 | 247 | return 1; |
| nikapov | 0:3de9cb936fd1 | 248 | } |
| nikapov | 0:3de9cb936fd1 | 249 | |
| nikapov | 0:3de9cb936fd1 | 250 | *pfData = ( float )int16data / 10.0f; |
| nikapov | 0:3de9cb936fd1 | 251 | |
| nikapov | 0:3de9cb936fd1 | 252 | return 0; |
| nikapov | 0:3de9cb936fd1 | 253 | } |
| nikapov | 0:3de9cb936fd1 | 254 | |
| nikapov | 0:3de9cb936fd1 | 255 | /** |
| nikapov | 0:3de9cb936fd1 | 256 | * @brief Read LPS22HB output data rate |
| nikapov | 0:3de9cb936fd1 | 257 | * @param odr the pointer to the output data rate |
| nikapov | 0:3de9cb936fd1 | 258 | * @retval 0 in case of success, an error code otherwise |
| nikapov | 0:3de9cb936fd1 | 259 | */ |
| nikapov | 0:3de9cb936fd1 | 260 | int LPS22HBSensor::get_odr(float* odr) |
| nikapov | 0:3de9cb936fd1 | 261 | { |
| nikapov | 0:3de9cb936fd1 | 262 | LPS22HB_Odr_et odr_low_level; |
| nikapov | 0:3de9cb936fd1 | 263 | |
| nikapov | 0:3de9cb936fd1 | 264 | if ( LPS22HB_Get_Odr( (void *)this, &odr_low_level ) == LPS22HB_ERROR ) |
| nikapov | 0:3de9cb936fd1 | 265 | { |
| nikapov | 0:3de9cb936fd1 | 266 | return 1; |
| nikapov | 0:3de9cb936fd1 | 267 | } |
| nikapov | 0:3de9cb936fd1 | 268 | |
| nikapov | 0:3de9cb936fd1 | 269 | switch( odr_low_level ) |
| nikapov | 0:3de9cb936fd1 | 270 | { |
| nikapov | 0:3de9cb936fd1 | 271 | case LPS22HB_ODR_ONE_SHOT: |
| nikapov | 0:3de9cb936fd1 | 272 | *odr = 0.0f; |
| nikapov | 0:3de9cb936fd1 | 273 | break; |
| nikapov | 0:3de9cb936fd1 | 274 | case LPS22HB_ODR_1HZ: |
| nikapov | 0:3de9cb936fd1 | 275 | *odr = 1.0f; |
| nikapov | 0:3de9cb936fd1 | 276 | break; |
| nikapov | 0:3de9cb936fd1 | 277 | case LPS22HB_ODR_10HZ: |
| nikapov | 0:3de9cb936fd1 | 278 | *odr = 10.0f; |
| nikapov | 0:3de9cb936fd1 | 279 | break; |
| nikapov | 0:3de9cb936fd1 | 280 | case LPS22HB_ODR_25HZ: |
| nikapov | 0:3de9cb936fd1 | 281 | *odr = 25.0f; |
| nikapov | 0:3de9cb936fd1 | 282 | break; |
| nikapov | 0:3de9cb936fd1 | 283 | case LPS22HB_ODR_50HZ: |
| nikapov | 0:3de9cb936fd1 | 284 | *odr = 50.0f; |
| nikapov | 0:3de9cb936fd1 | 285 | break; |
| nikapov | 0:3de9cb936fd1 | 286 | case LPS22HB_ODR_75HZ: |
| nikapov | 0:3de9cb936fd1 | 287 | *odr = 75.0f; |
| nikapov | 0:3de9cb936fd1 | 288 | break; |
| nikapov | 0:3de9cb936fd1 | 289 | default: |
| nikapov | 0:3de9cb936fd1 | 290 | *odr = -1.0f; |
| nikapov | 0:3de9cb936fd1 | 291 | return 1; |
| nikapov | 0:3de9cb936fd1 | 292 | } |
| nikapov | 0:3de9cb936fd1 | 293 | |
| nikapov | 0:3de9cb936fd1 | 294 | return 0; |
| nikapov | 0:3de9cb936fd1 | 295 | } |
| nikapov | 0:3de9cb936fd1 | 296 | |
| nikapov | 0:3de9cb936fd1 | 297 | /** |
| nikapov | 0:3de9cb936fd1 | 298 | * @brief Set ODR |
| nikapov | 0:3de9cb936fd1 | 299 | * @param odr the output data rate to be set |
| nikapov | 0:3de9cb936fd1 | 300 | * @retval 0 in case of success, an error code otherwise |
| nikapov | 0:3de9cb936fd1 | 301 | */ |
| nikapov | 0:3de9cb936fd1 | 302 | int LPS22HBSensor::set_odr(float odr) |
| nikapov | 0:3de9cb936fd1 | 303 | { |
| nikapov | 0:3de9cb936fd1 | 304 | if(_is_enabled == 1) |
| nikapov | 0:3de9cb936fd1 | 305 | { |
| nikapov | 0:3de9cb936fd1 | 306 | if(Set_ODR_When_Enabled(odr) == 1) |
| nikapov | 0:3de9cb936fd1 | 307 | { |
| nikapov | 0:3de9cb936fd1 | 308 | return 1; |
| nikapov | 0:3de9cb936fd1 | 309 | } |
| nikapov | 0:3de9cb936fd1 | 310 | } |
| nikapov | 0:3de9cb936fd1 | 311 | else |
| nikapov | 0:3de9cb936fd1 | 312 | { |
| nikapov | 0:3de9cb936fd1 | 313 | if(Set_ODR_When_Disabled(odr) == 1) |
| nikapov | 0:3de9cb936fd1 | 314 | { |
| nikapov | 0:3de9cb936fd1 | 315 | return 1; |
| nikapov | 0:3de9cb936fd1 | 316 | } |
| nikapov | 0:3de9cb936fd1 | 317 | } |
| nikapov | 0:3de9cb936fd1 | 318 | |
| nikapov | 0:3de9cb936fd1 | 319 | return 0; |
| nikapov | 0:3de9cb936fd1 | 320 | } |
| nikapov | 0:3de9cb936fd1 | 321 | |
| nikapov | 0:3de9cb936fd1 | 322 | |
| nikapov | 0:3de9cb936fd1 | 323 | /** |
| nikapov | 0:3de9cb936fd1 | 324 | * @brief Set the LPS22HB sensor output data rate when enabled |
| nikapov | 0:3de9cb936fd1 | 325 | * @param odr the functional output data rate to be set |
| nikapov | 0:3de9cb936fd1 | 326 | * @retval 0 in case of success |
| nikapov | 0:3de9cb936fd1 | 327 | * @retval 1 in case of failure |
| nikapov | 0:3de9cb936fd1 | 328 | */ |
| nikapov | 0:3de9cb936fd1 | 329 | int LPS22HBSensor::Set_ODR_When_Enabled( float odr ) |
| nikapov | 0:3de9cb936fd1 | 330 | { |
| nikapov | 0:3de9cb936fd1 | 331 | LPS22HB_Odr_et new_odr; |
| nikapov | 0:3de9cb936fd1 | 332 | |
| nikapov | 0:3de9cb936fd1 | 333 | new_odr = ( odr <= 1.0f ) ? LPS22HB_ODR_1HZ |
| nikapov | 0:3de9cb936fd1 | 334 | : ( odr <= 10.0f ) ? LPS22HB_ODR_10HZ |
| nikapov | 0:3de9cb936fd1 | 335 | : ( odr <= 25.0f ) ? LPS22HB_ODR_25HZ |
| nikapov | 0:3de9cb936fd1 | 336 | : ( odr <= 50.0f ) ? LPS22HB_ODR_50HZ |
| nikapov | 0:3de9cb936fd1 | 337 | : LPS22HB_ODR_75HZ; |
| nikapov | 0:3de9cb936fd1 | 338 | |
| nikapov | 0:3de9cb936fd1 | 339 | if ( LPS22HB_Set_Odr( (void *)this, new_odr ) == LPS22HB_ERROR ) |
| nikapov | 0:3de9cb936fd1 | 340 | { |
| nikapov | 0:3de9cb936fd1 | 341 | return 1; |
| nikapov | 0:3de9cb936fd1 | 342 | } |
| nikapov | 0:3de9cb936fd1 | 343 | |
| nikapov | 0:3de9cb936fd1 | 344 | if ( get_odr( &_last_odr ) == 1 ) |
| nikapov | 0:3de9cb936fd1 | 345 | { |
| nikapov | 0:3de9cb936fd1 | 346 | return 1; |
| nikapov | 0:3de9cb936fd1 | 347 | } |
| nikapov | 0:3de9cb936fd1 | 348 | |
| nikapov | 0:3de9cb936fd1 | 349 | return 0; |
| nikapov | 0:3de9cb936fd1 | 350 | } |
| nikapov | 0:3de9cb936fd1 | 351 | |
| nikapov | 0:3de9cb936fd1 | 352 | /** |
| nikapov | 0:3de9cb936fd1 | 353 | * @brief Set the LPS22HB sensor output data rate when disabled |
| nikapov | 0:3de9cb936fd1 | 354 | * @param odr the functional output data rate to be set |
| nikapov | 0:3de9cb936fd1 | 355 | * @retval 0 in case of success |
| nikapov | 0:3de9cb936fd1 | 356 | * @retval 1 in case of failure |
| nikapov | 0:3de9cb936fd1 | 357 | */ |
| nikapov | 0:3de9cb936fd1 | 358 | int LPS22HBSensor::Set_ODR_When_Disabled( float odr ) |
| nikapov | 0:3de9cb936fd1 | 359 | { |
| nikapov | 0:3de9cb936fd1 | 360 | _last_odr = ( odr <= 1.0f ) ? 1.0f |
| nikapov | 0:3de9cb936fd1 | 361 | : ( odr <= 10.0f ) ? 10.0f |
| nikapov | 0:3de9cb936fd1 | 362 | : ( odr <= 25.0f ) ? 25.0f |
| nikapov | 0:3de9cb936fd1 | 363 | : ( odr <= 50.0f ) ? 50.0f |
| nikapov | 0:3de9cb936fd1 | 364 | : 75.0f; |
| nikapov | 0:3de9cb936fd1 | 365 | |
| nikapov | 0:3de9cb936fd1 | 366 | return 0; |
| nikapov | 0:3de9cb936fd1 | 367 | } |
| nikapov | 0:3de9cb936fd1 | 368 | |
| nikapov | 0:3de9cb936fd1 | 369 | |
| nikapov | 0:3de9cb936fd1 | 370 | /** |
| nikapov | 0:3de9cb936fd1 | 371 | * @brief Read the data from register |
| nikapov | 0:3de9cb936fd1 | 372 | * @param reg register address |
| nikapov | 0:3de9cb936fd1 | 373 | * @param data register data |
| nikapov | 0:3de9cb936fd1 | 374 | * @retval 0 in case of success |
| nikapov | 0:3de9cb936fd1 | 375 | * @retval 1 in case of failure |
| nikapov | 0:3de9cb936fd1 | 376 | */ |
| nikapov | 0:3de9cb936fd1 | 377 | int LPS22HBSensor::read_reg( uint8_t reg, uint8_t *data ) |
| nikapov | 0:3de9cb936fd1 | 378 | { |
| nikapov | 0:3de9cb936fd1 | 379 | |
| nikapov | 0:3de9cb936fd1 | 380 | if ( LPS22HB_read_reg( (void *)this, reg, 1, data ) == LPS22HB_ERROR ) |
| nikapov | 0:3de9cb936fd1 | 381 | { |
| nikapov | 0:3de9cb936fd1 | 382 | return 1; |
| nikapov | 0:3de9cb936fd1 | 383 | } |
| nikapov | 0:3de9cb936fd1 | 384 | |
| nikapov | 0:3de9cb936fd1 | 385 | return 0; |
| nikapov | 0:3de9cb936fd1 | 386 | } |
| nikapov | 0:3de9cb936fd1 | 387 | |
| nikapov | 0:3de9cb936fd1 | 388 | /** |
| nikapov | 0:3de9cb936fd1 | 389 | * @brief Write the data to register |
| nikapov | 0:3de9cb936fd1 | 390 | * @param reg register address |
| nikapov | 0:3de9cb936fd1 | 391 | * @param data register data |
| nikapov | 0:3de9cb936fd1 | 392 | * @retval 0 in case of success |
| nikapov | 0:3de9cb936fd1 | 393 | * @retval 1 in case of failure |
| nikapov | 0:3de9cb936fd1 | 394 | */ |
| nikapov | 0:3de9cb936fd1 | 395 | int LPS22HBSensor::write_reg( uint8_t reg, uint8_t data ) |
| nikapov | 0:3de9cb936fd1 | 396 | { |
| nikapov | 0:3de9cb936fd1 | 397 | |
| nikapov | 0:3de9cb936fd1 | 398 | if ( LPS22HB_write_reg( (void *)this, reg, 1, &data ) == LPS22HB_ERROR ) |
| nikapov | 0:3de9cb936fd1 | 399 | { |
| nikapov | 0:3de9cb936fd1 | 400 | return 1; |
| nikapov | 0:3de9cb936fd1 | 401 | } |
| nikapov | 0:3de9cb936fd1 | 402 | |
| nikapov | 0:3de9cb936fd1 | 403 | return 0; |
| nikapov | 0:3de9cb936fd1 | 404 | } |
| nikapov | 0:3de9cb936fd1 | 405 | |
| nikapov | 0:3de9cb936fd1 | 406 | |
| nikapov | 0:3de9cb936fd1 | 407 | uint8_t LPS22HB_io_write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite ) |
| nikapov | 0:3de9cb936fd1 | 408 | { |
| nikapov | 0:3de9cb936fd1 | 409 | return ((LPS22HBSensor *)handle)->io_write(pBuffer, WriteAddr, nBytesToWrite); |
| nikapov | 0:3de9cb936fd1 | 410 | } |
| nikapov | 0:3de9cb936fd1 | 411 | |
| nikapov | 0:3de9cb936fd1 | 412 | uint8_t LPS22HB_io_read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead ) |
| nikapov | 0:3de9cb936fd1 | 413 | { |
| nikapov | 0:3de9cb936fd1 | 414 | return ((LPS22HBSensor *)handle)->io_read(pBuffer, ReadAddr, nBytesToRead); |
| nikapov | 0:3de9cb936fd1 | 415 | } |

