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_SPI3W
Fork of X_NUCLEO_IKS01A2 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) : _dev_spi(spi), _cs_pin(cs_pin), _int_pin(int_pin) 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_spi3w = NULL; 00058 _dev_i2c=NULL; 00059 return; 00060 } 00061 _cs_pin = 1; 00062 _dev_spi3w = 0; 00063 _dev_i2c=NULL; 00064 00065 LPS22HB_Set_SpiInterface ((void *)this, LPS22HB_SPI_4_WIRE); 00066 LPS22HB_Set_I2C ((void *)this, LPS22HB_DISABLE); 00067 } 00068 00069 /** Constructor 00070 * @param spi3w object of an helper class which handles the spi peripheral 00071 * @param 00072 */ 00073 LPS22HBSensor::LPS22HBSensor(SPI3W *spi3w, PinName cs_pin, PinName int_pin) : _dev_spi3w(spi3w), _cs_pin(cs_pin), _int_pin(int_pin) 00074 { 00075 assert (spi3w); 00076 if (cs_pin == NC) 00077 { 00078 printf ("ERROR LPS22HBSensor CS MUST NOT BE NC\n\r"); 00079 _dev_spi = NULL; 00080 _dev_spi3w = NULL; 00081 _dev_i2c=NULL; 00082 return; 00083 } 00084 _cs_pin = 1; 00085 _dev_spi = NULL; 00086 _dev_i2c=NULL; 00087 00088 LPS22HB_Set_SpiInterface ((void *)this, LPS22HB_SPI_3_WIRE); 00089 LPS22HB_Set_I2C ((void *)this, LPS22HB_DISABLE); 00090 }; 00091 00092 00093 /** Constructor 00094 * @param i2c object of an helper class which handles the I2C peripheral 00095 * @param address the address of the component's instance 00096 */ 00097 LPS22HBSensor::LPS22HBSensor(DevI2C *i2c, uint8_t address, PinName int_pin) : 00098 _dev_i2c(i2c), _address(address), _cs_pin(NC), _int_pin(int_pin) 00099 { 00100 assert (i2c); 00101 _dev_spi = NULL; 00102 _dev_spi3w = NULL; 00103 LPS22HB_Set_I2C ((void *)this, LPS22HB_ENABLE); 00104 }; 00105 00106 00107 /** 00108 * @brief Initializing the component. 00109 * @param[in] init pointer to device specific initalization structure. 00110 * @retval "0" in case of success, an error code otherwise. 00111 */ 00112 int LPS22HBSensor::init(void *init) 00113 { 00114 if ( LPS22HB_Set_PowerMode( (void *)this, LPS22HB_LowPower ) == LPS22HB_ERROR ) 00115 { 00116 return 1; 00117 } 00118 00119 /* Power down the device */ 00120 if ( LPS22HB_Set_Odr( (void *)this, LPS22HB_ODR_ONE_SHOT ) == LPS22HB_ERROR ) 00121 // if ( LPS22HB_Set_Odr( (void *)this, LPS22HB_ODR_25HZ ) == LPS22HB_ERROR ) 00122 { 00123 return 1; 00124 } 00125 00126 /* Disable low-pass filter on LPS22HB pressure data */ 00127 if( LPS22HB_Set_LowPassFilter( (void *)this, LPS22HB_DISABLE) == LPS22HB_ERROR ) 00128 { 00129 return 1; 00130 } 00131 00132 /* Set low-pass filter cutoff configuration*/ 00133 if( LPS22HB_Set_LowPassFilterCutoff( (void *)this, LPS22HB_ODR_9 ) == LPS22HB_ERROR ) 00134 { 00135 return 1; 00136 } 00137 00138 /* Set block data update mode */ 00139 if ( LPS22HB_Set_Bdu( (void *)this, LPS22HB_BDU_NO_UPDATE ) == LPS22HB_ERROR ) 00140 { 00141 return 1; 00142 } 00143 00144 /* Set automatic increment for multi-byte read/write */ 00145 if( LPS22HB_Set_AutomaticIncrementRegAddress( (void *)this, LPS22HB_ENABLE) == LPS22HB_ERROR ) 00146 { 00147 return 1; 00148 } 00149 00150 _is_enabled = 0; 00151 _last_odr = 25.0f; 00152 00153 return 0; 00154 } 00155 00156 00157 /** 00158 * @brief Enable LPS22HB 00159 * @retval 0 in case of success, an error code otherwise 00160 */ 00161 int LPS22HBSensor::enable(void) 00162 { 00163 /* Check if the component is already enabled */ 00164 if ( _is_enabled == 1 ) 00165 { 00166 return 0; 00167 } 00168 00169 if(Set_ODR_When_Enabled(_last_odr) == 1) 00170 { 00171 return 1; 00172 } 00173 00174 _is_enabled = 1; 00175 00176 return 0; 00177 } 00178 00179 /** 00180 * @brief Disable LPS22HB 00181 * @retval 0 in case of success, an error code otherwise 00182 */ 00183 int LPS22HBSensor::disable(void) 00184 { 00185 /* Check if the component is already disabled */ 00186 if ( _is_enabled == 0 ) 00187 { 00188 return 0; 00189 } 00190 00191 /* Power down the device */ 00192 if ( LPS22HB_Set_Odr( (void *)this, LPS22HB_ODR_ONE_SHOT ) == LPS22HB_ERROR ) 00193 { 00194 return 1; 00195 } 00196 00197 _is_enabled = 0; 00198 00199 return 0; 00200 } 00201 00202 /** 00203 * @brief Read ID address of LPS22HB 00204 * @param id the pointer where the ID of the device is stored 00205 * @retval 0 in case of success, an error code otherwise 00206 */ 00207 int LPS22HBSensor::read_id(uint8_t *id) 00208 { 00209 if(!id) 00210 { 00211 return 1; 00212 } 00213 00214 /* Read WHO AM I register */ 00215 if ( LPS22HB_Get_DeviceID( (void *)this, id ) == LPS22HB_ERROR ) 00216 { 00217 return 1; 00218 } 00219 00220 return 0; 00221 } 00222 00223 /** 00224 * @brief Reboot memory content of LPS22HB 00225 * @param None 00226 * @retval 0 in case of success, an error code otherwise 00227 */ 00228 int LPS22HBSensor::reset(void) 00229 { 00230 /* Read WHO AM I register */ 00231 if ( LPS22HB_MemoryBoot((void *)this) == LPS22HB_ERROR ) 00232 { 00233 return 1; 00234 } 00235 00236 return 0; 00237 } 00238 00239 /** 00240 * @brief Read LPS22HB output register, and calculate the pressure in mbar 00241 * @param pfData the pressure value in mbar 00242 * @retval 0 in case of success, an error code otherwise 00243 */ 00244 int LPS22HBSensor::get_pressure(float* pfData) 00245 { 00246 int32_t int32data = 0; 00247 00248 /* Read data from LPS22HB. */ 00249 if ( LPS22HB_Get_Pressure( (void *)this, &int32data ) == LPS22HB_ERROR ) 00250 { 00251 return 1; 00252 } 00253 00254 *pfData = ( float )int32data / 100.0f; 00255 00256 return 0; 00257 } 00258 00259 /** 00260 * @brief Read LPS22HB output register, and calculate the temperature 00261 * @param pfData the temperature value 00262 * @retval 0 in case of success, an error code otherwise 00263 */ 00264 int LPS22HBSensor::get_temperature(float *pfData) 00265 { 00266 int16_t int16data = 0; 00267 00268 /* Read data from LPS22HB. */ 00269 if ( LPS22HB_Get_Temperature( (void *)this, &int16data ) == LPS22HB_ERROR ) 00270 { 00271 return 1; 00272 } 00273 00274 *pfData = ( float )int16data / 10.0f; 00275 00276 return 0; 00277 } 00278 00279 /** 00280 * @brief Read LPS22HB output data rate 00281 * @param odr the pointer to the output data rate 00282 * @retval 0 in case of success, an error code otherwise 00283 */ 00284 int LPS22HBSensor::get_odr(float* odr) 00285 { 00286 LPS22HB_Odr_et odr_low_level; 00287 00288 if ( LPS22HB_Get_Odr( (void *)this, &odr_low_level ) == LPS22HB_ERROR ) 00289 { 00290 return 1; 00291 } 00292 00293 switch( odr_low_level ) 00294 { 00295 case LPS22HB_ODR_ONE_SHOT : 00296 *odr = 0.0f; 00297 break; 00298 case LPS22HB_ODR_1HZ : 00299 *odr = 1.0f; 00300 break; 00301 case LPS22HB_ODR_10HZ : 00302 *odr = 10.0f; 00303 break; 00304 case LPS22HB_ODR_25HZ : 00305 *odr = 25.0f; 00306 break; 00307 case LPS22HB_ODR_50HZ : 00308 *odr = 50.0f; 00309 break; 00310 case LPS22HB_ODR_75HZ : 00311 *odr = 75.0f; 00312 break; 00313 default: 00314 *odr = -1.0f; 00315 return 1; 00316 } 00317 00318 return 0; 00319 } 00320 00321 /** 00322 * @brief Set ODR 00323 * @param odr the output data rate to be set 00324 * @retval 0 in case of success, an error code otherwise 00325 */ 00326 int LPS22HBSensor::set_odr(float odr) 00327 { 00328 if(_is_enabled == 1) 00329 { 00330 if(Set_ODR_When_Enabled(odr) == 1) 00331 { 00332 return 1; 00333 } 00334 } 00335 else 00336 { 00337 if(Set_ODR_When_Disabled(odr) == 1) 00338 { 00339 return 1; 00340 } 00341 } 00342 00343 return 0; 00344 } 00345 00346 00347 /** 00348 * @brief Set the LPS22HB sensor output data rate when enabled 00349 * @param odr the functional output data rate to be set 00350 * @retval 0 in case of success 00351 * @retval 1 in case of failure 00352 */ 00353 int LPS22HBSensor::Set_ODR_When_Enabled( float odr ) 00354 { 00355 LPS22HB_Odr_et new_odr; 00356 00357 new_odr = ( odr <= 1.0f ) ? LPS22HB_ODR_1HZ 00358 : ( odr <= 10.0f ) ? LPS22HB_ODR_10HZ 00359 : ( odr <= 25.0f ) ? LPS22HB_ODR_25HZ 00360 : ( odr <= 50.0f ) ? LPS22HB_ODR_50HZ 00361 : LPS22HB_ODR_75HZ ; 00362 00363 if ( LPS22HB_Set_Odr( (void *)this, new_odr ) == LPS22HB_ERROR ) 00364 { 00365 return 1; 00366 } 00367 00368 if ( get_odr( &_last_odr ) == 1 ) 00369 { 00370 return 1; 00371 } 00372 00373 return 0; 00374 } 00375 00376 /** 00377 * @brief Set the LPS22HB sensor output data rate when disabled 00378 * @param odr the functional output data rate to be set 00379 * @retval 0 in case of success 00380 * @retval 1 in case of failure 00381 */ 00382 int LPS22HBSensor::Set_ODR_When_Disabled( float odr ) 00383 { 00384 _last_odr = ( odr <= 1.0f ) ? 1.0f 00385 : ( odr <= 10.0f ) ? 10.0f 00386 : ( odr <= 25.0f ) ? 25.0f 00387 : ( odr <= 50.0f ) ? 50.0f 00388 : 75.0f; 00389 00390 return 0; 00391 } 00392 00393 00394 /** 00395 * @brief Read the data from register 00396 * @param reg register address 00397 * @param data register data 00398 * @retval 0 in case of success 00399 * @retval 1 in case of failure 00400 */ 00401 int LPS22HBSensor::read_reg( uint8_t reg, uint8_t *data ) 00402 { 00403 00404 if ( LPS22HB_read_reg( (void *)this, reg, 1, data ) == LPS22HB_ERROR ) 00405 { 00406 return 1; 00407 } 00408 00409 return 0; 00410 } 00411 00412 /** 00413 * @brief Write the data to register 00414 * @param reg register address 00415 * @param data register data 00416 * @retval 0 in case of success 00417 * @retval 1 in case of failure 00418 */ 00419 int LPS22HBSensor::write_reg( uint8_t reg, uint8_t data ) 00420 { 00421 00422 if ( LPS22HB_write_reg( (void *)this, reg, 1, &data ) == LPS22HB_ERROR ) 00423 { 00424 return 1; 00425 } 00426 00427 return 0; 00428 } 00429 00430 00431 uint8_t LPS22HB_io_write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite ) 00432 { 00433 return ((LPS22HBSensor *)handle)->io_write(pBuffer, WriteAddr, nBytesToWrite); 00434 } 00435 00436 uint8_t LPS22HB_io_read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead ) 00437 { 00438 return ((LPS22HBSensor *)handle)->io_read(pBuffer, ReadAddr, nBytesToRead); 00439 }
Generated on Wed Jul 13 2022 22:58:05 by
1.7.2
