Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
lps25h.c
00001 /** 00002 ****************************************************************************** 00003 * @file LPS25H.c 00004 * @author AST Robotics Team 00005 * @version V0.0.1 00006 * @date 03-April-2014 00007 * @brief This file provides a set of functions needed to manage the lps25h. 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© 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 /* Includes ------------------------------------------------------------------*/ 00038 #include "lps25h.h" 00039 00040 /** @addtogroup BSP 00041 * @{ 00042 */ 00043 00044 /** @addtogroup STM32F439_SENSITRON 00045 * @{ 00046 */ 00047 00048 /** @addtogroup LPS25H 00049 * @{ 00050 */ 00051 00052 00053 /** @defgroup LPS25H_Private_TypesDefinitions 00054 * @{ 00055 */ 00056 00057 /** 00058 * @} 00059 */ 00060 00061 /** @defgroup LPS25H_Private_Defines 00062 * @{ 00063 */ 00064 00065 /** 00066 * @} 00067 */ 00068 00069 /** @defgroup LPS25H_Private_Macros 00070 * @{ 00071 */ 00072 00073 /** 00074 * @} 00075 */ 00076 00077 /** @defgroup LPS25H_Private_Variables 00078 * @{ 00079 */ 00080 00081 PRESSURE_DrvTypeDef LPS25HDrv = 00082 { 00083 LPS25H_Init, 00084 LPS25H_PowerOff, 00085 LPS25H_ReadID, 00086 LPS25H_RebootCmd, 00087 0,//LPS25H_INT1InterruptConfig, 00088 0,//LPS25H_EnableIT, 00089 0,//LPS25H_DisableIT, 00090 0, 00091 0, 00092 LPS25H_GetPressure, 00093 LPS25H_GetTemperature, 00094 LPS25H_SlaveAddrRemap 00095 }; 00096 00097 uint8_t LPS25H_SlaveAddress = LPS25H_ADDRESS_LOW; 00098 00099 /** 00100 * @} 00101 */ 00102 00103 /** @defgroup LPS25H_Private_FunctionPrototypes 00104 * @{ 00105 */ 00106 00107 /** 00108 * @brief Exit the shutdown mode for LPS25H. 00109 * @param None 00110 * @retval None 00111 */ 00112 void LPS25H_PowerOn(void); 00113 00114 void LPS25H_I2C_ReadRawPressure(uint32_t *raw_press); 00115 00116 void LPS25H_I2C_ReadRawTemperature(int16_t *raw_data); 00117 00118 00119 /** 00120 * @} 00121 */ 00122 00123 /** @defgroup LPS25H_Private_Functions 00124 * @{ 00125 */ 00126 00127 00128 /** 00129 * @brief Set LPS25H Initialization. 00130 * @param InitStruct: it contains the configuration setting for the LPS25H. 00131 * @retval Error Code (PressureError_Enum) 00132 */ 00133 void LPS25H_Init(PRESSURE_InitTypeDef *LPS25H_Init) 00134 { 00135 uint8_t tmp1 = 0x00; 00136 00137 /* Configure the low level interface ---------------------------------------*/ 00138 PRESSURE_IO_Init(); 00139 00140 LPS25H_PowerOn(); 00141 00142 PRESSURE_IO_Read(&tmp1, LPS25H_SlaveAddress, LPS25H_CTRL_REG1_ADDR, 1); 00143 00144 /* Output Data Rate selection */ 00145 tmp1 &= ~(LPS25H_ODR_MASK); 00146 tmp1 |= LPS25H_Init->OutputDataRate; 00147 00148 /* Interrupt circuit selection */ 00149 tmp1 &= ~(LPS25H_DIFF_EN_MASK); 00150 tmp1 |= LPS25H_Init->DiffEnable; 00151 00152 /* Block Data Update selection */ 00153 tmp1 &= ~(LPS25H_BDU_MASK); 00154 tmp1 |= LPS25H_Init->BlockDataUpdate; 00155 00156 /* Serial Interface Mode selection */ 00157 tmp1 &= ~(LPS25H_SPI_SIM_MASK); 00158 tmp1 |= LPS25H_Init->SPIMode; 00159 00160 PRESSURE_IO_Write(&tmp1, LPS25H_SlaveAddress, LPS25H_CTRL_REG1_ADDR, 1); 00161 00162 PRESSURE_IO_Read(&tmp1, LPS25H_SlaveAddress, LPS25H_RES_CONF_ADDR, 1); 00163 00164 /* Serial Interface Mode selection */ 00165 tmp1 &= ~(LPS25H_P_RES_MASK); 00166 tmp1 |= LPS25H_Init->PressureResolution; 00167 00168 /* Serial Interface Mode selection */ 00169 tmp1 &= ~(LPS25H_T_RES_MASK); 00170 tmp1 |= LPS25H_Init->TemperatureResolution; 00171 00172 PRESSURE_IO_Write(&tmp1, LPS25H_SlaveAddress, LPS25H_RES_CONF_ADDR, 1); 00173 } 00174 00175 /** 00176 * @brief Read ID address of LPS25H 00177 * @param Device ID address 00178 * @retval ID name 00179 */ 00180 uint8_t LPS25H_ReadID(void) 00181 { 00182 uint8_t tmp; 00183 00184 /* Read WHO I AM register */ 00185 PRESSURE_IO_Read(&tmp, LPS25H_SlaveAddress, LPS25H_WHO_AM_I_ADDR, 1); 00186 00187 /* Return the ID */ 00188 return (uint8_t)tmp; 00189 } 00190 00191 /** 00192 * @brief Reboot memory content of LPS25H 00193 * @param None 00194 * @retval None 00195 */ 00196 void LPS25H_RebootCmd(void) 00197 { 00198 uint8_t tmpreg; 00199 00200 /* Read CTRL_REG5 register */ 00201 PRESSURE_IO_Read(&tmpreg, LPS25H_SlaveAddress, LPS25H_CTRL_REG2_ADDR, 1); 00202 00203 /* Enable or Disable the reboot memory */ 00204 tmpreg |= LPS25H_RESET_MEMORY; 00205 00206 /* Write value to MEMS CTRL_REG5 regsister */ 00207 PRESSURE_IO_Write(&tmpreg, LPS25H_SlaveAddress, LPS25H_CTRL_REG2_ADDR, 1); 00208 } 00209 00210 /** 00211 * @brief Set LPS25H Interrupt INT1 configuration 00212 * @param LPS25H_InterruptConfig_TypeDef: pointer to a LPS25H_InterruptConfig_TypeDef 00213 * structure that contains the configuration setting for the LPS25H Interrupt. 00214 * @retval None 00215 */ 00216 void LPS25H_INT1InterruptConfig(uint16_t Int1Config) 00217 { 00218 00219 } 00220 00221 /** 00222 * @brief Enable INT1 00223 * @retval None 00224 */ 00225 void LPS25H_EnableIT() 00226 { 00227 00228 } 00229 00230 /** 00231 * @brief Disable INT1 00232 * @retval None 00233 */ 00234 void LPS25H_DisableIT() 00235 { 00236 00237 } 00238 00239 00240 /** 00241 * @brief Read LPS25H output register, and calculate the raw pressure. 00242 * @param uint32_t: raw_press. Pressure raw value. 00243 * @retval LPS25H_ERROR or LPS25H_OK. 00244 */ 00245 void LPS25H_I2C_ReadRawPressure(uint32_t *raw_press) 00246 { 00247 uint8_t buffer[3], i; 00248 uint32_t tempVal=0; 00249 00250 /* Read the register content */ 00251 00252 PRESSURE_IO_Read(buffer, LPS25H_SlaveAddress, LPS25H_PRESS_POUT_XL_ADDR+0x80, 3); 00253 // LPS25H_I2C_Read(LPS25H_PRESS_POUT_XL_ADDR+0x80, 3, buffer); 00254 00255 /* Build the raw data */ 00256 for (i = 0 ; i < 3 ; i++) 00257 tempVal |= (((uint32_t) buffer[i]) << (8 * i)); 00258 00259 /* convert the 2's complement 24 bit to 2's complement 32 bit */ 00260 if (tempVal & 0x00800000) 00261 tempVal |= 0xFF000000; 00262 00263 /* return the built value */ 00264 *raw_press = ((uint32_t) tempVal); 00265 } 00266 00267 /** 00268 * @brief Read LPS25H output register, and calculate the pressure in mbar. 00269 * @param float *pressure. Pressure value in mbar. 00270 * @retval LPS25H_ERROR or LPS25H_OK. 00271 */ 00272 void LPS25H_GetPressure(float* pfData) 00273 { 00274 uint32_t raw_press = 0; 00275 00276 LPS25H_I2C_ReadRawPressure(&raw_press); 00277 00278 /* return the built value */ 00279 //tempInt = raw_press / 4096; 00280 00281 *pfData = (float)raw_press /4096.0f; 00282 } 00283 00284 /** 00285 * @brief Read LPS25H output register, and calculate the raw temperature. 00286 * @param int16_t *raw_data: temperature raw value. 00287 * @retval LPS25H_ERROR or LPS25H_OK. 00288 */ 00289 void LPS25H_I2C_ReadRawTemperature(int16_t *raw_data) 00290 { 00291 uint8_t buffer[2]; 00292 uint16_t tempVal=0; 00293 00294 /* Read the register content */ 00295 PRESSURE_IO_Read(buffer, LPS25H_SlaveAddress, LPS25H_TEMP_OUT_L_ADDR+0x80, 2); 00296 // LPS25H_I2C_Read(LPS25H_TEMP_OUT_L_ADDR+0x80, 2, buffer); 00297 00298 /* Build the raw value */ 00299 tempVal = (((uint16_t)buffer[1]) << 8)+(uint16_t)buffer[0]; 00300 00301 /* Return it */ 00302 *raw_data = ((int16_t)tempVal); 00303 } 00304 00305 /** 00306 * @brief Read LPS25H output register, and calculate the temperature. 00307 * @param float *temperature : temperature value.. 00308 * @retval LPS25H_ERROR or LPS25H_OK. 00309 */ 00310 void LPS25H_GetTemperature(float* pfData) 00311 { 00312 int16_t raw_data; 00313 00314 LPS25H_I2C_ReadRawTemperature(&raw_data); 00315 00316 //*data_out = (int16_t)((((float)raw_data/480.0) + 42.5)*100); 00317 *pfData = (int16_t)((((float)raw_data/480.0) + 42.5)); 00318 } 00319 /** 00320 * @brief Exit the shutdown mode for LPS25H. 00321 * @param None 00322 * @retval LPS25H_ERROR or LPS25H_OK 00323 */ 00324 void LPS25H_PowerOn(void) 00325 { 00326 uint8_t tmpreg; 00327 00328 /* Read the register content */ 00329 // LPS25H_I2C_Read(LPS25H_CTRL_REG1_ADDR,1,&tmpReg); 00330 PRESSURE_IO_Read(&tmpreg, LPS25H_SlaveAddress, LPS25H_CTRL_REG1_ADDR, 1); 00331 00332 /* Set the power down bit */ 00333 tmpreg |= LPS25H_MODE_ACTIVE; 00334 00335 /* Write register */ 00336 // PRESSURE_IO_Write(LPS25H_CTRL_REG1_ADDR,1,&tmpReg); 00337 PRESSURE_IO_Write(&tmpreg, LPS25H_SlaveAddress, LPS25H_CTRL_REG1_ADDR, 1); 00338 00339 } 00340 00341 00342 /** 00343 * @brief Enter the shutdown mode for LPS25H. 00344 * @param None 00345 * @retval LPS25H_ERROR or LPS25H_OK 00346 */ 00347 void LPS25H_PowerOff(void) 00348 { 00349 uint8_t tmpreg; 00350 00351 /* Read the register content */ 00352 // PRESSURE_IO_Read( LPS25H_CTRL_REG1_ADDR,1,&tmpReg); 00353 PRESSURE_IO_Read(&tmpreg, LPS25H_SlaveAddress, LPS25H_CTRL_REG1_ADDR, 1); 00354 00355 /* Reset the power down bit */ 00356 tmpreg &= ~(LPS25H_MODE_ACTIVE); 00357 00358 /* Write register */ 00359 // PRESSURE_IO_Write( LPS25H_CTRL_REG1_ADDR,1,&tmpReg); 00360 PRESSURE_IO_Write(&tmpreg, LPS25H_SlaveAddress, LPS25H_CTRL_REG1_ADDR, 1); 00361 } 00362 00363 00364 /** 00365 * @brief Set the slave address according to SA0 bit. 00366 * @param SA0_Bit_Status: LPS25H_SA0_LOW or LPS25H_SA0_HIGH 00367 * @retval None 00368 */ 00369 void LPS25H_SlaveAddrRemap(uint8_t SA0_Bit_Status) 00370 { 00371 LPS25H_SlaveAddress = (SA0_Bit_Status==LPS25H_SA0_LOW?LPS25H_ADDRESS_LOW:LPS25H_ADDRESS_HIGH); 00372 } 00373 00374 /** 00375 * @} 00376 */ 00377 00378 /** 00379 * @} 00380 */ 00381 00382 /** 00383 * @} 00384 */ 00385 00386 /** 00387 * @} 00388 */ 00389 00390 00391 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 00392
Generated on Tue Jul 12 2022 17:21:52 by
1.7.2