Library for supporting the Nucleo Sensor Shield.

Dependents:   Nucleo_Sensors_Demo m2x-temp_ethernet_demo m2x-MEMS_ACKme_Wifi_demo m2x_MEMS_Ublox_Cellular_demo ... more

Fork of Nucleo_Sensor_Shield by Daniel Griffin

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lps25.cpp Source File

lps25.cpp

00001 /**
00002 ******************************************************************************
00003 * @file    x_cube_mems_lps25.cpp
00004 * @author  AST / EST
00005 * @version V0.0.1
00006 * @date    1-December-2014
00007 * @brief   Implementation file for component LPS25H
00008 ******************************************************************************
00009 * @attention
00010 *
00011 * <h2><center>&copy; COPYRIGHT(c) 2014 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 /* Includes ------------------------------------------------------------------*/
00039 #include "mbed.h"
00040 #include "lps25h.h"
00041 #include "lps25h_platform.h"
00042 
00043 /* Methods -------------------------------------------------------------------*/
00044 
00045 /**
00046  * @brief Read LPS25H output register, and calculate the pressure in mbar.
00047  * @param float *pressure. Pressure value in mbar.
00048  * @retval LPS25H_ERROR or LPS25H_OK.
00049  */
00050 void LPS25H::GetPressure(float* pfData)
00051 {
00052     
00053     uint32_t raw_press = 0;
00054     
00055     if(isInitialized()==0)
00056       {
00057         pfData = 0;
00058         return;
00059       }
00060     
00061     ReadRawPressure(&raw_press);
00062 
00063     /* return the built value */
00064     //tempInt = raw_press / 4096;
00065 
00066     *pfData = (float)raw_press /4096.0f;
00067 }
00068 
00069 /**
00070  * @brief Read LPS25H output register, and calculate the raw  pressure.
00071  * @param uint32_t: raw_press. Pressure raw value.
00072  * @retval LPS25H_ERROR or LPS25H_OK.
00073  */
00074 void LPS25H::ReadRawPressure(uint32_t *raw_press)
00075 {
00076     uint8_t buffer[3], i;
00077     uint32_t tempVal=0;
00078     int ret;
00079 
00080     /* Read the register content */
00081     //PRESSURE_IO_Read(buffer, LPS25H_SlaveAddress, LPS25H_PRESS_POUT_XL_ADDR+0x80, 3);
00082     ret = dev_i2c.i2c_read(buffer, LPS25H_ADDRESS_HIGH, LPS25H_PRESS_POUT_XL_ADDR+0x80, 3);
00083 
00084     if (ret == 0)
00085     {
00086       /* Build the raw data */
00087       for (i = 0 ; i < 3 ; i++)
00088           tempVal |= (((uint32_t) buffer[i]) << (8 * i));
00089 
00090       /* convert the 2's complement 24 bit to 2's complement 32 bit */
00091       if (tempVal & 0x00800000)
00092           tempVal |= 0xFF000000;
00093 
00094       /* return the built value */
00095       *raw_press = ((uint32_t) tempVal);
00096     }
00097 }
00098 
00099 /**
00100  * @brief  Read ID address of HTS221
00101  * @param  Device ID address
00102  * @retval ID name
00103  */
00104 uint8_t LPS25H::ReadID(void)
00105 {
00106     uint8_t tmp;
00107 
00108     /* Read the register content */
00109     int ret;    
00110     //PRESSURE_IO_Read(&tmp, LPS25H_SlaveAddress, LPS25H_WHO_AM_I_ADDR, 1);
00111     ret = dev_i2c.i2c_read(&tmp, LPS25H_ADDRESS_HIGH, LPS25H_WHO_AM_I_ADDR, 1);
00112         
00113     /* Return the ID */
00114     return ((ret == 0) ? (uint8_t)tmp : 0);
00115 }
00116 
00117 /**
00118  * @brief  Set HTS221 Initialization.
00119  * @param  InitStruct: it contains the configuration setting for the HTS221.
00120  * @retval None
00121  */
00122 void LPS25H::Init() {
00123     int ret;    
00124     uint8_t tmp1 = 0x00;
00125 
00126     Power_ON();
00127 
00128     //PRESSURE_IO_Read(&tmp1, LPS25H_SlaveAddress, LPS25H_CTRL_REG1_ADDR, 1);
00129     ret = dev_i2c.i2c_read(&tmp1, LPS25H_ADDRESS_HIGH, LPS25H_CTRL_REG1_ADDR, 1);
00130     
00131     if (ret == 0)
00132     {
00133       /* Output Data Rate selection */
00134       tmp1 &= ~(LPS25H_ODR_MASK);
00135       tmp1 |= LPS25H_ODR_1Hz;
00136 
00137       /* Interrupt circuit selection */
00138       tmp1 &= ~(LPS25H_DIFF_EN_MASK);
00139       tmp1 |= LPS25H_DIFF_ENABLE;
00140 
00141       /* Block Data Update selection */
00142       tmp1 &= ~(LPS25H_BDU_MASK);
00143       tmp1 |= LPS25H_BDU_CONT;
00144 
00145       /* Serial Interface Mode selection */
00146       tmp1 &= ~(LPS25H_SPI_SIM_MASK);
00147       tmp1 |= LPS25H_SPI_SIM_3W;
00148 
00149       //PRESSURE_IO_Write(&tmp1, LPS25H_SlaveAddress, LPS25H_CTRL_REG1_ADDR, 1);
00150       ret = dev_i2c.i2c_write(&tmp1, LPS25H_ADDRESS_HIGH, LPS25H_CTRL_REG1_ADDR, 1);
00151     }
00152     
00153     if (ret == 0)
00154     {        
00155       //PRESSURE_IO_Read(&tmp1, LPS25H_SlaveAddress, LPS25H_RES_CONF_ADDR, 1);
00156       ret = dev_i2c.i2c_read(&tmp1, LPS25H_ADDRESS_HIGH, LPS25H_RES_CONF_ADDR, 1);
00157     }
00158 
00159     if (ret == 0)
00160     {
00161       /* Pressure Res selection */
00162       tmp1 &= ~(LPS25H_P_RES_MASK);
00163       tmp1 |= LPS25H_P_RES_AVG_32;
00164 
00165       /* Temperature Res selection */
00166       tmp1 &= ~(LPS25H_T_RES_MASK);
00167       tmp1 |= LPS25H_T_RES_AVG_16;
00168 
00169       //PRESSURE_IO_Write(&tmp1, LPS25H_SlaveAddress, LPS25H_RES_CONF_ADDR, 1);
00170       ret = dev_i2c.i2c_write(&tmp1, LPS25H_ADDRESS_HIGH, LPS25H_RES_CONF_ADDR, 1);
00171     }
00172     
00173     if (ret == 0)
00174     {
00175       if(ReadID() == I_AM_LPS25H)
00176       {
00177           Lps25hInitialized = 1;
00178           //ret = HUM_TEMP_OK;
00179       }
00180     }
00181     
00182     return;
00183 }
00184 
00185 int LPS25H::Power_ON() {
00186     uint8_t tmpreg;
00187     int ret;
00188 
00189     /* Read the register content */
00190     //PRESSURE_IO_Read(&tmpreg, LPS25H_SlaveAddress, LPS25H_CTRL_REG1_ADDR, 1);
00191     ret = dev_i2c.i2c_read(&tmpreg, 0xBA, 0x20, 1);
00192     
00193     /* Set the power down bit */
00194     tmpreg |= LPS25H_MODE_ACTIVE;
00195 
00196     /* Write register */
00197     //PRESSURE_IO_Write(&tmpreg, LPS25H_SlaveAddress, LPS25H_CTRL_REG1_ADDR, 1);
00198     ret = dev_i2c.i2c_write(&tmpreg, LPS25H_ADDRESS_HIGH, LPS25H_CTRL_REG1_ADDR, 1);
00199     return ret;
00200     
00201 }
00202 
00203 int LPS25H::LPS25H_Calibration() {
00204   
00205     int ret = 0;
00206     
00207     if(Lps25hInitialized == 1)
00208     {
00209       return 1; //TODO: Error Codes definitions
00210     }
00211     
00212     return ret;
00213 }