Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: ST_INTERFACES X_NUCLEO_COMMON_3W
Fork of LPS22HB by
LPS22HBSensor.cpp
00001 /** 00002 ****************************************************************************** 00003 * @file LPS22HBSensor.cpp 00004 * @author CLab 00005 * @version V1.0.0 00006 * @date 5 August 2016 00007 * @brief Implementation of an LPS22HB Pressure sensor. 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> 00012 * 00013 * Redistribution and use in source and binary forms, with or without modification, 00014 * are permitted provided that the following conditions are met: 00015 * 1. Redistributions of source code must retain the above copyright notice, 00016 * this list of conditions and the following disclaimer. 00017 * 2. Redistributions in binary form must reproduce the above copyright notice, 00018 * this list of conditions and the following disclaimer in the documentation 00019 * and/or other materials provided with the distribution. 00020 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00021 * may be used to endorse or promote products derived from this software 00022 * without specific prior written permission. 00023 * 00024 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00025 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00026 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00027 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00028 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00029 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00030 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00031 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00032 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00033 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00034 * 00035 ****************************************************************************** 00036 */ 00037 00038 00039 /* Includes ------------------------------------------------------------------*/ 00040 #include "mbed.h" 00041 //#include "SPI3W.h" 00042 #include "SPI.h" 00043 #include "DevI2C.h" 00044 #include "LPS22HBSensor.h" 00045 #include "LPS22HB_driver.h" 00046 #include <assert.h> 00047 00048 /* Class Implementation ------------------------------------------------------*/ 00049 00050 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) 00051 { 00052 assert (spi); 00053 if (cs_pin == NC) 00054 { 00055 printf ("ERROR LPS22HBSensor CS MUST NOT BE NC\n\r"); 00056 _dev_spi = NULL; 00057 _dev_i2c=NULL; 00058 return; 00059 } 00060 00061 _cs_pin = 1; 00062 _dev_i2c=NULL; 00063 00064 if (_spi_type == SPI3W) LPS22HB_Set_SpiInterface ((void *)this, LPS22HB_SPI_3_WIRE); 00065 else if (_spi_type == SPI4W) LPS22HB_Set_SpiInterface ((void *)this, LPS22HB_SPI_4_WIRE); 00066 00067 LPS22HB_Set_I2C ((void *)this, LPS22HB_DISABLE); 00068 } 00069 00070 /** Constructor 00071 * @param i2c object of an helper class which handles the I2C peripheral 00072 * @param address the address of the component's instance 00073 */ 00074 LPS22HBSensor::LPS22HBSensor(DevI2C *i2c, uint8_t address, PinName int_pin) : 00075 _dev_i2c(i2c), _address(address), _cs_pin(NC), _int_pin(int_pin) 00076 { 00077 assert (i2c); 00078 _dev_spi = NULL; 00079 LPS22HB_Set_I2C ((void *)this, LPS22HB_ENABLE); 00080 }; 00081 00082 00083 /** 00084 * @brief Initializing the component. 00085 * @param[in] init pointer to device specific initalization structure. 00086 * @retval "0" in case of success, an error code otherwise. 00087 */ 00088 int LPS22HBSensor::init(void *init) 00089 { 00090 if ( LPS22HB_Set_PowerMode( (void *)this, LPS22HB_LowPower ) == LPS22HB_ERROR ) 00091 { 00092 return 1; 00093 } 00094 00095 /* Power down the device */ 00096 if ( LPS22HB_Set_Odr( (void *)this, LPS22HB_ODR_ONE_SHOT ) == LPS22HB_ERROR ) 00097 // if ( LPS22HB_Set_Odr( (void *)this, LPS22HB_ODR_25HZ ) == LPS22HB_ERROR ) 00098 { 00099 return 1; 00100 } 00101 00102 /* Disable low-pass filter on LPS22HB pressure data */ 00103 if( LPS22HB_Set_LowPassFilter( (void *)this, LPS22HB_DISABLE) == LPS22HB_ERROR ) 00104 { 00105 return 1; 00106 } 00107 00108 /* Set low-pass filter cutoff configuration*/ 00109 if( LPS22HB_Set_LowPassFilterCutoff( (void *)this, LPS22HB_ODR_9 ) == LPS22HB_ERROR ) 00110 { 00111 return 1; 00112 } 00113 00114 /* Set block data update mode */ 00115 if ( LPS22HB_Set_Bdu( (void *)this, LPS22HB_BDU_NO_UPDATE ) == LPS22HB_ERROR ) 00116 { 00117 return 1; 00118 } 00119 00120 /* Set automatic increment for multi-byte read/write */ 00121 if( LPS22HB_Set_AutomaticIncrementRegAddress( (void *)this, LPS22HB_ENABLE) == LPS22HB_ERROR ) 00122 { 00123 return 1; 00124 } 00125 00126 _is_enabled = 0; 00127 _last_odr = 25.0f; 00128 00129 return 0; 00130 } 00131 00132 00133 /** 00134 * @brief Enable LPS22HB 00135 * @retval 0 in case of success, an error code otherwise 00136 */ 00137 int LPS22HBSensor::enable(void) 00138 { 00139 /* Check if the component is already enabled */ 00140 if ( _is_enabled == 1 ) 00141 { 00142 return 0; 00143 } 00144 00145 if(Set_ODR_When_Enabled(_last_odr) == 1) 00146 { 00147 return 1; 00148 } 00149 00150 _is_enabled = 1; 00151 00152 return 0; 00153 } 00154 00155 /** 00156 * @brief Disable LPS22HB 00157 * @retval 0 in case of success, an error code otherwise 00158 */ 00159 int LPS22HBSensor::disable(void) 00160 { 00161 /* Check if the component is already disabled */ 00162 if ( _is_enabled == 0 ) 00163 { 00164 return 0; 00165 } 00166 00167 /* Power down the device */ 00168 if ( LPS22HB_Set_Odr( (void *)this, LPS22HB_ODR_ONE_SHOT ) == LPS22HB_ERROR ) 00169 { 00170 return 1; 00171 } 00172 00173 _is_enabled = 0; 00174 00175 return 0; 00176 } 00177 00178 /** 00179 * @brief Read ID address of LPS22HB 00180 * @param id the pointer where the ID of the device is stored 00181 * @retval 0 in case of success, an error code otherwise 00182 */ 00183 int LPS22HBSensor::read_id(uint8_t *id) 00184 { 00185 if(!id) 00186 { 00187 return 1; 00188 } 00189 00190 /* Read WHO AM I register */ 00191 if ( LPS22HB_Get_DeviceID( (void *)this, id ) == LPS22HB_ERROR ) 00192 { 00193 return 1; 00194 } 00195 00196 return 0; 00197 } 00198 00199 /** 00200 * @brief Reboot memory content of LPS22HB 00201 * @param None 00202 * @retval 0 in case of success, an error code otherwise 00203 */ 00204 int LPS22HBSensor::reset(void) 00205 { 00206 /* Read WHO AM I register */ 00207 if ( LPS22HB_MemoryBoot((void *)this) == LPS22HB_ERROR ) 00208 { 00209 return 1; 00210 } 00211 00212 return 0; 00213 } 00214 00215 /** 00216 * @brief Read LPS22HB output register, and calculate the pressure in mbar 00217 * @param pfData the pressure value in mbar 00218 * @retval 0 in case of success, an error code otherwise 00219 */ 00220 int LPS22HBSensor::get_pressure(float* pfData) 00221 { 00222 int32_t int32data = 0; 00223 00224 /* Read data from LPS22HB. */ 00225 if ( LPS22HB_Get_Pressure( (void *)this, &int32data ) == LPS22HB_ERROR ) 00226 { 00227 return 1; 00228 } 00229 00230 *pfData = ( float )int32data / 100.0f; 00231 00232 return 0; 00233 } 00234 00235 /** 00236 * @brief Read LPS22HB output register, and calculate the temperature 00237 * @param pfData the temperature value 00238 * @retval 0 in case of success, an error code otherwise 00239 */ 00240 int LPS22HBSensor::get_temperature(float *pfData) 00241 { 00242 int16_t int16data = 0; 00243 00244 /* Read data from LPS22HB. */ 00245 if ( LPS22HB_Get_Temperature( (void *)this, &int16data ) == LPS22HB_ERROR ) 00246 { 00247 return 1; 00248 } 00249 00250 *pfData = ( float )int16data / 10.0f; 00251 00252 return 0; 00253 } 00254 00255 /** 00256 * @brief Read LPS22HB output data rate 00257 * @param odr the pointer to the output data rate 00258 * @retval 0 in case of success, an error code otherwise 00259 */ 00260 int LPS22HBSensor::get_odr(float* odr) 00261 { 00262 LPS22HB_Odr_et odr_low_level; 00263 00264 if ( LPS22HB_Get_Odr( (void *)this, &odr_low_level ) == LPS22HB_ERROR ) 00265 { 00266 return 1; 00267 } 00268 00269 switch( odr_low_level ) 00270 { 00271 case LPS22HB_ODR_ONE_SHOT : 00272 *odr = 0.0f; 00273 break; 00274 case LPS22HB_ODR_1HZ : 00275 *odr = 1.0f; 00276 break; 00277 case LPS22HB_ODR_10HZ : 00278 *odr = 10.0f; 00279 break; 00280 case LPS22HB_ODR_25HZ : 00281 *odr = 25.0f; 00282 break; 00283 case LPS22HB_ODR_50HZ : 00284 *odr = 50.0f; 00285 break; 00286 case LPS22HB_ODR_75HZ : 00287 *odr = 75.0f; 00288 break; 00289 default: 00290 *odr = -1.0f; 00291 return 1; 00292 } 00293 00294 return 0; 00295 } 00296 00297 /** 00298 * @brief Set ODR 00299 * @param odr the output data rate to be set 00300 * @retval 0 in case of success, an error code otherwise 00301 */ 00302 int LPS22HBSensor::set_odr(float odr) 00303 { 00304 if(_is_enabled == 1) 00305 { 00306 if(Set_ODR_When_Enabled(odr) == 1) 00307 { 00308 return 1; 00309 } 00310 } 00311 else 00312 { 00313 if(Set_ODR_When_Disabled(odr) == 1) 00314 { 00315 return 1; 00316 } 00317 } 00318 00319 return 0; 00320 } 00321 00322 00323 /** 00324 * @brief Set the LPS22HB sensor output data rate when enabled 00325 * @param odr the functional output data rate to be set 00326 * @retval 0 in case of success 00327 * @retval 1 in case of failure 00328 */ 00329 int LPS22HBSensor::Set_ODR_When_Enabled( float odr ) 00330 { 00331 LPS22HB_Odr_et new_odr; 00332 00333 new_odr = ( odr <= 1.0f ) ? LPS22HB_ODR_1HZ 00334 : ( odr <= 10.0f ) ? LPS22HB_ODR_10HZ 00335 : ( odr <= 25.0f ) ? LPS22HB_ODR_25HZ 00336 : ( odr <= 50.0f ) ? LPS22HB_ODR_50HZ 00337 : LPS22HB_ODR_75HZ ; 00338 00339 if ( LPS22HB_Set_Odr( (void *)this, new_odr ) == LPS22HB_ERROR ) 00340 { 00341 return 1; 00342 } 00343 00344 if ( get_odr( &_last_odr ) == 1 ) 00345 { 00346 return 1; 00347 } 00348 00349 return 0; 00350 } 00351 00352 /** 00353 * @brief Set the LPS22HB sensor output data rate when disabled 00354 * @param odr the functional output data rate to be set 00355 * @retval 0 in case of success 00356 * @retval 1 in case of failure 00357 */ 00358 int LPS22HBSensor::Set_ODR_When_Disabled( float odr ) 00359 { 00360 _last_odr = ( odr <= 1.0f ) ? 1.0f 00361 : ( odr <= 10.0f ) ? 10.0f 00362 : ( odr <= 25.0f ) ? 25.0f 00363 : ( odr <= 50.0f ) ? 50.0f 00364 : 75.0f; 00365 00366 return 0; 00367 } 00368 00369 00370 /** 00371 * @brief Read the data from register 00372 * @param reg register address 00373 * @param data register data 00374 * @retval 0 in case of success 00375 * @retval 1 in case of failure 00376 */ 00377 int LPS22HBSensor::read_reg( uint8_t reg, uint8_t *data ) 00378 { 00379 00380 if ( LPS22HB_read_reg( (void *)this, reg, 1, data ) == LPS22HB_ERROR ) 00381 { 00382 return 1; 00383 } 00384 00385 return 0; 00386 } 00387 00388 /** 00389 * @brief Write the data to register 00390 * @param reg register address 00391 * @param data register data 00392 * @retval 0 in case of success 00393 * @retval 1 in case of failure 00394 */ 00395 int LPS22HBSensor::write_reg( uint8_t reg, uint8_t data ) 00396 { 00397 00398 if ( LPS22HB_write_reg( (void *)this, reg, 1, &data ) == LPS22HB_ERROR ) 00399 { 00400 return 1; 00401 } 00402 00403 return 0; 00404 } 00405 00406 00407 uint8_t LPS22HB_io_write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite ) 00408 { 00409 return ((LPS22HBSensor *)handle)->io_write(pBuffer, WriteAddr, nBytesToWrite); 00410 } 00411 00412 uint8_t LPS22HB_io_read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead ) 00413 { 00414 return ((LPS22HBSensor *)handle)->io_read(pBuffer, ReadAddr, nBytesToRead); 00415 }
Generated on Thu Jul 14 2022 07:22:24 by
 1.7.2
 1.7.2 
    