Beta

Dependencies:   ST_INTERFACES X_NUCLEO_COMMON

Fork of X_NUCLEO_IKS01A2 by ST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LPS22HBSensor.cpp Source File

LPS22HBSensor.cpp

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