Firmware Library for X-NUCLEO-IKS01A1 (MEMS Inertial & Environmental Sensors) Expansion Board

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Dependents:   MultiTech_Dragonfly_2015_ATT_Gov_Solutions_Hackathon_Example HelloWorld_IKS01A1 LoRaWAN-test-10secs ServoMotorDemo ... more

Fork of X_NUCLEO_IKS01A1 by ST Expansion SW Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lps25h_class.cpp Source File

lps25h_class.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    lps25h_class.cpp
00004  * @author  AST / EST
00005  * @version V0.0.1
00006  * @date    14-April-2015
00007  * @brief   Implementation file for the LPS25H driver class
00008  ******************************************************************************
00009  * @attention
00010  *
00011  * <h2><center>&copy; COPYRIGHT(c) 2015 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 "lps25h_class.h"
00040 #include "lps25h.h"
00041 
00042 /* Methods -------------------------------------------------------------------*/
00043 /* betzw - based on:
00044            X-CUBE-MEMS1/trunk/Drivers/BSP/Components/lps25h/lps25h.c: revision #400,
00045            X-CUBE-MEMS1/trunk: revision #416
00046 */
00047 
00048 /**
00049  * @brief  Set LPS25H Initialization
00050  * @param  LPS25H_Init the configuration setting for the LPS25H
00051  * @retval PRESSURE_OK in case of success, an error code otherwise
00052  */
00053 PRESSURE_StatusTypeDef LPS25H::LPS25H_Init(PRESSURE_InitTypeDef *LPS25H_Init)
00054 {
00055   uint8_t tmp1 = 0x00;
00056   
00057   /* Configure the low level interface ---------------------------------------*/
00058   if(LPS25H_IO_Init() != PRESSURE_OK)
00059   {
00060     return PRESSURE_ERROR;
00061   }
00062   
00063   if(LPS25H_PowerOn() != PRESSURE_OK)
00064   {
00065     return PRESSURE_ERROR;
00066   }
00067   
00068   if(LPS25H_IO_Read(&tmp1, LPS25H_CTRL_REG1_ADDR, 1) != PRESSURE_OK)
00069   {
00070     return PRESSURE_ERROR;
00071   }
00072   
00073   /* Output Data Rate selection */
00074   tmp1 &= ~(LPS25H_ODR_MASK);
00075   tmp1 |= LPS25H_Init->OutputDataRate;
00076   
00077   /* Interrupt circuit selection */
00078   tmp1 &= ~(LPS25H_DIFF_EN_MASK);
00079   tmp1 |= LPS25H_Init->DiffEnable;
00080   
00081   /* Block Data Update selection */
00082   tmp1 &= ~(LPS25H_BDU_MASK);
00083   tmp1 |= LPS25H_Init->BlockDataUpdate;
00084   
00085   /* Serial Interface Mode selection */
00086   tmp1 &= ~(LPS25H_SPI_SIM_MASK);
00087   tmp1 |= LPS25H_Init->SPIMode;
00088   
00089   if(LPS25H_IO_Write(&tmp1, LPS25H_CTRL_REG1_ADDR, 1) != PRESSURE_OK)
00090   {
00091     return PRESSURE_ERROR;
00092   }
00093   
00094   if(LPS25H_IO_Read(&tmp1, LPS25H_RES_CONF_ADDR, 1) != PRESSURE_OK)
00095   {
00096     return PRESSURE_ERROR;
00097   }
00098   
00099   /* Serial Interface Mode selection */
00100   tmp1 &= ~(LPS25H_P_RES_MASK);
00101   tmp1 |= LPS25H_Init->PressureResolution;
00102   
00103   /* Serial Interface Mode selection */
00104   tmp1 &= ~(LPS25H_T_RES_MASK);
00105   tmp1 |= LPS25H_Init->TemperatureResolution;
00106   
00107   if(LPS25H_IO_Write(&tmp1, LPS25H_RES_CONF_ADDR, 1) != PRESSURE_OK)
00108   {
00109     return PRESSURE_ERROR;
00110   }
00111   
00112   LPS25H_IO_ITConfig();
00113   
00114   return PRESSURE_OK;
00115 }
00116 
00117 /**
00118  * @brief  Read ID address of LPS25H
00119  * @param  ht_id the pointer where the ID of the device is stored
00120  * @retval PRESSURE_OK in case of success, an error code otherwise
00121  */
00122 PRESSURE_StatusTypeDef LPS25H::LPS25H_ReadID(uint8_t *p_id)
00123 {
00124   if(!p_id)
00125   {
00126     return PRESSURE_ERROR;
00127   }
00128   
00129   return LPS25H_IO_Read(p_id, LPS25H_WHO_AM_I_ADDR, 1);
00130 }
00131 
00132 /**
00133  * @brief  Reboot memory content of LPS25H
00134  * @retval PRESSURE_OK in case of success, an error code otherwise
00135  */
00136 PRESSURE_StatusTypeDef LPS25H::LPS25H_RebootCmd(void)
00137 {
00138   uint8_t tmpreg;
00139   
00140   /* Read CTRL_REG5 register */
00141   if(LPS25H_IO_Read(&tmpreg, LPS25H_CTRL_REG2_ADDR, 1) != PRESSURE_OK)
00142   {
00143     return PRESSURE_ERROR;
00144   }
00145   
00146   /* Enable or Disable the reboot memory */
00147   tmpreg |= LPS25H_RESET_MEMORY;
00148   
00149   /* Write value to MEMS CTRL_REG5 regsister */
00150   if(LPS25H_IO_Write(&tmpreg, LPS25H_CTRL_REG2_ADDR, 1) != PRESSURE_OK)
00151   {
00152     return PRESSURE_ERROR;
00153   }
00154   
00155   return PRESSURE_OK;
00156 }
00157 
00158 
00159 /**
00160  * @brief  Read LPS25H output register, and calculate the raw pressure
00161  * @param  raw_press the pressure raw value
00162  * @retval PRESSURE_OK in case of success, an error code otherwise
00163  */
00164 PRESSURE_StatusTypeDef LPS25H::LPS25H_I2C_ReadRawPressure(int32_t *raw_press)
00165 {
00166   uint8_t buffer[3], i;
00167   uint32_t tempVal = 0;
00168   
00169   /* Read the register content */
00170   
00171   if(LPS25H_IO_Read(buffer, (LPS25H_PRESS_POUT_XL_ADDR | LPS25H_I2C_MULTIPLEBYTE_CMD),
00172                     3) != PRESSURE_OK)
00173   {
00174     return PRESSURE_ERROR;
00175   }
00176   
00177   /* Build the raw data */
00178   for (i = 0 ; i < 3 ; i++)
00179     tempVal |= (((uint32_t) buffer[i]) << (8 * i));
00180     
00181   /* convert the 2's complement 24 bit to 2's complement 32 bit */
00182   if (tempVal & 0x00800000)
00183     tempVal |= 0xFF000000;
00184     
00185   /* return the built value */
00186   *raw_press = ((int32_t) tempVal);
00187   
00188   return PRESSURE_OK;
00189 }
00190 
00191 /**
00192  * @brief  Read LPS25H output register, and calculate the pressure in mbar
00193  * @param  pfData the pressure value in mbar
00194  * @retval PRESSURE_OK in case of success, an error code otherwise
00195  */
00196 PRESSURE_StatusTypeDef LPS25H::LPS25H_GetPressure(float* pfData)
00197 {
00198   int32_t raw_press = 0;
00199   
00200   if(LPS25H_I2C_ReadRawPressure(&raw_press) != PRESSURE_OK)
00201   {
00202     return PRESSURE_ERROR;
00203   }
00204   
00205   *pfData = (float)raw_press / 4096.0f;
00206   
00207   return PRESSURE_OK;
00208 }
00209 
00210 /**
00211  * @brief  Read LPS25H output register, and calculate the raw temperature
00212  * @param  raw_data the temperature raw value
00213  * @retval PRESSURE_OK in case of success, an error code otherwise
00214  */
00215 PRESSURE_StatusTypeDef LPS25H::LPS25H_I2C_ReadRawTemperature(int16_t *raw_data)
00216 {
00217   uint8_t buffer[2];
00218   uint16_t tempVal = 0;
00219   
00220   /* Read the register content */
00221   if(LPS25H_IO_Read(buffer, (LPS25H_TEMP_OUT_L_ADDR | LPS25H_I2C_MULTIPLEBYTE_CMD),
00222                     2) != PRESSURE_OK)
00223   {
00224     return PRESSURE_ERROR;
00225   }
00226   
00227   /* Build the raw value */
00228   tempVal = (((uint16_t)buffer[1]) << 8) + (uint16_t)buffer[0];
00229   
00230   /* Return it */
00231   *raw_data = ((int16_t)tempVal);
00232   
00233   return PRESSURE_OK;
00234 }
00235 
00236 /**
00237  * @brief  Read LPS25H output register, and calculate the temperature
00238  * @param  pfData the temperature value
00239  * @retval PRESSURE_OK in case of success, an error code otherwise
00240  */
00241 PRESSURE_StatusTypeDef LPS25H::LPS25H_GetTemperature(float *pfData)
00242 {
00243   int16_t raw_data;
00244   
00245   if(LPS25H_I2C_ReadRawTemperature(&raw_data) != PRESSURE_OK)
00246   {
00247     return PRESSURE_ERROR;
00248   }
00249   
00250   *pfData = (float)((((float)raw_data / 480.0f) + 42.5f));
00251   
00252   return PRESSURE_OK;
00253 }
00254 /**
00255  * @brief  Exit the shutdown mode for LPS25H
00256  * @retval PRESSURE_OK in case of success, an error code otherwise
00257  */
00258 PRESSURE_StatusTypeDef LPS25H::LPS25H_PowerOn(void)
00259 {
00260   uint8_t tmpreg;
00261   
00262   /* Read the register content */
00263   if(LPS25H_IO_Read(&tmpreg, LPS25H_CTRL_REG1_ADDR, 1) != PRESSURE_OK)
00264   {
00265     return PRESSURE_ERROR;
00266   }
00267   
00268   /* Set the power down bit */
00269   tmpreg |= LPS25H_MODE_ACTIVE;
00270   
00271   /* Write register */
00272   if(LPS25H_IO_Write(&tmpreg, LPS25H_CTRL_REG1_ADDR, 1) != PRESSURE_OK)
00273   {
00274     return PRESSURE_ERROR;
00275   }
00276   
00277   return PRESSURE_OK;
00278 }
00279 
00280 
00281 /**
00282  * @brief  Enter the shutdown mode for LPS25H
00283  * @retval PRESSURE_OK in case of success, an error code otherwise
00284  */
00285 PRESSURE_StatusTypeDef LPS25H::LPS25H_PowerOff(void)
00286 {
00287   uint8_t tmpreg;
00288   
00289   /* Read the register content */
00290   if(LPS25H_IO_Read(&tmpreg, LPS25H_CTRL_REG1_ADDR, 1) != PRESSURE_OK)
00291   {
00292     return PRESSURE_ERROR;
00293   }
00294   
00295   /* Reset the power down bit */
00296   tmpreg &= ~(LPS25H_MODE_ACTIVE);
00297   
00298   /* Write register */
00299   if(LPS25H_IO_Write(&tmpreg, LPS25H_CTRL_REG1_ADDR, 1) != PRESSURE_OK)
00300   {
00301     return PRESSURE_ERROR;
00302   }
00303   
00304   return PRESSURE_OK;
00305 }
00306 
00307 /**
00308  * @brief  Set the slave address according to SA0 bit
00309  * @param  SA0_Bit_Status LPS25H_SA0_LOW or LPS25H_SA0_HIGH
00310  * @retval None
00311  */
00312 void LPS25H::LPS25H_SlaveAddrRemap(uint8_t SA0_Bit_Status)
00313 {
00314   LPS25H_SlaveAddress = (SA0_Bit_Status == LPS25H_SA0_LOW ? LPS25H_ADDRESS_LOW : LPS25H_ADDRESS_HIGH);
00315 }
00316 
00317 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/