Workshop example

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

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