Farnell-Element14 Bologna IOT Team / BSP_B-L475E-IOT01

Dependencies:   VL53L0X

Fork of BSP_B-L475E-IOT01 by ST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lps22hb.c Source File

lps22hb.c

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    lps22hb.c
00004  * @author  MCD Application Team
00005  * @version V1.0.0
00006  * @date    14-February-2017
00007  * @brief   This file provides a set of functions needed to manage the LPS22HB
00008  *          pressure and temperature devices
00009  ******************************************************************************
00010  * @attention
00011  *
00012  * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
00013  *
00014  * Redistribution and use in source and binary forms, with or without modification,
00015  * are permitted provided that the following conditions are met:
00016  *   1. Redistributions of source code must retain the above copyright notice,
00017  *      this list of conditions and the following disclaimer.
00018  *   2. Redistributions in binary form must reproduce the above copyright notice,
00019  *      this list of conditions and the following disclaimer in the documentation
00020  *      and/or other materials provided with the distribution.
00021  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00022  *      may be used to endorse or promote products derived from this software
00023  *      without specific prior written permission.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00028  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00029  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00031  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00033  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  ******************************************************************************
00037  */
00038 
00039 /* Includes ------------------------------------------------------------------*/
00040 #include "lps22hb.h"
00041 
00042 /** @addtogroup BSP
00043   * @{
00044   */
00045 
00046 /** @addtogroup Component
00047   * @{
00048   */
00049 
00050 /** @defgroup LPS22HB LPS22HB
00051   * @{
00052   */
00053 
00054 /** @defgroup LPS22HB_Private_FunctionsPrototypes LPS22HB Private Functions Prototypes
00055   * @{
00056   */
00057 static void LPS22HB_Init(uint16_t DeviceAddr);
00058 /**
00059   * @}
00060   */
00061 
00062 /** @defgroup LPS22HB_Private_Variables LPS22HB Private Variables
00063   * @{
00064   */
00065 /* Pressure Private Variables */
00066 PSENSOR_DrvTypeDef LPS22HB_P_Drv =
00067 {
00068   LPS22HB_P_Init,
00069   LPS22HB_P_ReadID,
00070   LPS22HB_P_ReadPressure
00071 };
00072 
00073 /* Temperature Private Variables */ 
00074 TSENSOR_DrvTypeDef LPS22HB_T_Drv =
00075 {
00076   LPS22HB_T_Init,
00077   0,
00078   0,
00079   LPS22HB_T_ReadTemp
00080 };
00081 /**
00082   * @}
00083   */ 
00084 
00085 /** @defgroup LPS22HB_Pressure_Private_Functions LPS22HB Pressure Private Functions
00086   * @{
00087   */
00088 /**
00089   * @brief  Set LPS22HB pressure sensor Initialization.
00090   */
00091 void LPS22HB_P_Init(uint16_t DeviceAddr)
00092 {
00093   LPS22HB_Init(DeviceAddr);
00094 }
00095 
00096 /**
00097   * @brief  Read LPS22HB ID.
00098   * @retval ID 
00099   */
00100 uint8_t LPS22HB_P_ReadID(uint16_t DeviceAddr)
00101 {  
00102   uint8_t ctrl = 0x00;
00103 
00104   /* IO interface initialization */
00105   SENSOR_IO_Init();  
00106   
00107   /* Read value at Who am I register address */
00108   ctrl = SENSOR_IO_Read(DeviceAddr, LPS22HB_WHO_AM_I_REG);
00109   
00110   return ctrl;
00111 }
00112 
00113 /**
00114   * @brief  Read pressure value of LPS22HB
00115   * @retval pressure value
00116   */
00117 float LPS22HB_P_ReadPressure(uint16_t DeviceAddr)
00118 {
00119   int32_t raw_press;
00120   uint8_t buffer[3];
00121   uint32_t tmp = 0;
00122   uint8_t i;
00123 
00124   for(i = 0; i < 3; i++)
00125   {
00126     buffer[i] = SENSOR_IO_Read(DeviceAddr, (LPS22HB_PRESS_OUT_XL_REG + i));
00127   }
00128 
00129   /* Build the raw data */
00130   for(i = 0; i < 3; i++)
00131     tmp |= (((uint32_t)buffer[i]) << (8 * i));
00132 
00133   /* convert the 2's complement 24 bit to 2's complement 32 bit */
00134   if(tmp & 0x00800000)
00135     tmp |= 0xFF000000;
00136 
00137   raw_press = ((int32_t)tmp);
00138 
00139   raw_press = (raw_press * 100) / 4096;
00140 
00141   return (float)((float)raw_press / 100.0f);
00142 }
00143 
00144 
00145 /**
00146   * @}
00147   */ 
00148 
00149 /** @defgroup LPS22HB_Temperature_Private_Functions LPS22HB Temperature Private Functions
00150   * @{
00151   */
00152 
00153 /**
00154   * @brief  Set LPS22HB temperature sensor Initialization.
00155   * @param  DeviceAddr: I2C device address
00156   * @param  InitStruct: pointer to a TSENSOR_InitTypeDef structure 
00157   *         that contains the configuration setting for the HTS221.
00158   * @retval None
00159   */
00160 void LPS22HB_T_Init(uint16_t DeviceAddr, TSENSOR_InitTypeDef *pInitStruct)
00161 {  
00162   LPS22HB_Init(DeviceAddr);
00163 }
00164 
00165 /**
00166   * @brief  Read temperature value of LPS22HB
00167   * @param  DeviceAddr: I2C device address
00168   * @retval temperature value
00169   */
00170 float LPS22HB_T_ReadTemp(uint16_t DeviceAddr)
00171 {
00172   int16_t raw_data;
00173   uint8_t buffer[2];
00174   uint16_t tmp;
00175   uint8_t i;
00176 
00177   for(i = 0; i < 2; i++)
00178   {
00179     buffer[i] = SENSOR_IO_Read(DeviceAddr, (LPS22HB_TEMP_OUT_L_REG + i));
00180   }
00181 
00182   /* Build the raw tmp */
00183   tmp = (((uint16_t)buffer[1]) << 8) + (uint16_t)buffer[0];
00184 
00185   raw_data = (tmp * 10) / 100;
00186   
00187   return ((float)(raw_data / 10.0f));
00188 }
00189 
00190 /**
00191   * @}
00192   */
00193 
00194 /** @addtogroup LPS22HB_Private_Functions LPS22HB Private functions
00195  * @{
00196  */
00197 
00198 /**
00199   * @brief  Set LPS22HB Initialization.
00200   * @param  DeviceAddr: I2C device address
00201   * @retval None
00202   */
00203 static void LPS22HB_Init(uint16_t DeviceAddr)
00204 {
00205   uint8_t tmp;
00206 
00207   /* Set Power mode */
00208   tmp = SENSOR_IO_Read(DeviceAddr, LPS22HB_RES_CONF_REG);
00209 
00210   tmp &= ~LPS22HB_LCEN_MASK;
00211   tmp |= (uint8_t)0x01; /* Set low current mode */
00212 
00213   SENSOR_IO_Write(DeviceAddr, LPS22HB_RES_CONF_REG, tmp);
00214 
00215   /* Read CTRL_REG1 */
00216   tmp = SENSOR_IO_Read(DeviceAddr, LPS22HB_CTRL_REG1);
00217 
00218   /* Set default ODR */
00219   tmp &= ~LPS22HB_ODR_MASK;
00220   tmp |= (uint8_t)0x30; /* Set ODR to 25Hz */
00221 
00222   /* Enable BDU */
00223   tmp &= ~LPS22HB_BDU_MASK;
00224   tmp |= ((uint8_t)0x02);
00225 
00226   /* Apply settings to CTRL_REG1 */
00227   SENSOR_IO_Write(DeviceAddr, LPS22HB_CTRL_REG1, tmp);
00228 }  
00229 
00230 /**
00231   * @}
00232   */
00233 
00234 /**
00235   * @}
00236   */
00237   
00238 /**
00239   * @}
00240   */
00241   
00242 /**
00243   * @}
00244   */
00245   
00246 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/