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

Warning: Deprecated!

Supported drivers and applications can be found at this link.

Committer:
dangriffin
Date:
Tue Dec 16 21:14:32 2014 +0000
Revision:
2:57888ec40e75
Parent:
1:354f61ffdcfb
Additional checking of return code in pressure sensor driver.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dangriffin 0:0433918efb54 1 /**
dangriffin 0:0433918efb54 2 ******************************************************************************
dangriffin 0:0433918efb54 3 * @file x_cube_mems_lps25.cpp
dangriffin 0:0433918efb54 4 * @author AST / EST
dangriffin 0:0433918efb54 5 * @version V0.0.1
dangriffin 0:0433918efb54 6 * @date 1-December-2014
dangriffin 0:0433918efb54 7 * @brief Implementation file for component LPS25H
dangriffin 0:0433918efb54 8 ******************************************************************************
dangriffin 0:0433918efb54 9 * @attention
dangriffin 0:0433918efb54 10 *
dangriffin 0:0433918efb54 11 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
dangriffin 0:0433918efb54 12 *
dangriffin 0:0433918efb54 13 * Redistribution and use in source and binary forms, with or without modification,
dangriffin 0:0433918efb54 14 * are permitted provided that the following conditions are met:
dangriffin 0:0433918efb54 15 * 1. Redistributions of source code must retain the above copyright notice,
dangriffin 0:0433918efb54 16 * this list of conditions and the following disclaimer.
dangriffin 0:0433918efb54 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
dangriffin 0:0433918efb54 18 * this list of conditions and the following disclaimer in the documentation
dangriffin 0:0433918efb54 19 * and/or other materials provided with the distribution.
dangriffin 0:0433918efb54 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
dangriffin 0:0433918efb54 21 * may be used to endorse or promote products derived from this software
dangriffin 0:0433918efb54 22 * without specific prior written permission.
dangriffin 0:0433918efb54 23 *
dangriffin 0:0433918efb54 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
dangriffin 0:0433918efb54 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
dangriffin 0:0433918efb54 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
dangriffin 0:0433918efb54 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
dangriffin 0:0433918efb54 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
dangriffin 0:0433918efb54 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
dangriffin 0:0433918efb54 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
dangriffin 0:0433918efb54 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
dangriffin 0:0433918efb54 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
dangriffin 0:0433918efb54 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
dangriffin 0:0433918efb54 34 *
dangriffin 0:0433918efb54 35 ******************************************************************************
dangriffin 0:0433918efb54 36 */
dangriffin 0:0433918efb54 37
dangriffin 0:0433918efb54 38 /* Includes ------------------------------------------------------------------*/
dangriffin 0:0433918efb54 39 #include "mbed.h"
dangriffin 0:0433918efb54 40 #include "lps25h.h"
dangriffin 0:0433918efb54 41 #include "lps25h_platform.h"
dangriffin 0:0433918efb54 42
dangriffin 0:0433918efb54 43 /* Methods -------------------------------------------------------------------*/
dangriffin 0:0433918efb54 44
dangriffin 0:0433918efb54 45 /**
dangriffin 0:0433918efb54 46 * @brief Read LPS25H output register, and calculate the pressure in mbar.
dangriffin 0:0433918efb54 47 * @param float *pressure. Pressure value in mbar.
dangriffin 0:0433918efb54 48 * @retval LPS25H_ERROR or LPS25H_OK.
dangriffin 0:0433918efb54 49 */
dangriffin 0:0433918efb54 50 void LPS25H::GetPressure(float* pfData)
dangriffin 0:0433918efb54 51 {
dangriffin 0:0433918efb54 52
dangriffin 0:0433918efb54 53 uint32_t raw_press = 0;
dangriffin 0:0433918efb54 54
dangriffin 0:0433918efb54 55 if(isInitialized()==0)
dangriffin 0:0433918efb54 56 {
dangriffin 0:0433918efb54 57 pfData = 0;
dangriffin 0:0433918efb54 58 return;
dangriffin 0:0433918efb54 59 }
dangriffin 0:0433918efb54 60
dangriffin 0:0433918efb54 61 ReadRawPressure(&raw_press);
dangriffin 0:0433918efb54 62
dangriffin 0:0433918efb54 63 /* return the built value */
dangriffin 0:0433918efb54 64 //tempInt = raw_press / 4096;
dangriffin 0:0433918efb54 65
dangriffin 0:0433918efb54 66 *pfData = (float)raw_press /4096.0f;
dangriffin 0:0433918efb54 67 }
dangriffin 0:0433918efb54 68
dangriffin 0:0433918efb54 69 /**
dangriffin 0:0433918efb54 70 * @brief Read LPS25H output register, and calculate the raw pressure.
dangriffin 0:0433918efb54 71 * @param uint32_t: raw_press. Pressure raw value.
dangriffin 0:0433918efb54 72 * @retval LPS25H_ERROR or LPS25H_OK.
dangriffin 0:0433918efb54 73 */
dangriffin 0:0433918efb54 74 void LPS25H::ReadRawPressure(uint32_t *raw_press)
dangriffin 0:0433918efb54 75 {
dangriffin 0:0433918efb54 76 uint8_t buffer[3], i;
dangriffin 0:0433918efb54 77 uint32_t tempVal=0;
dangriffin 0:0433918efb54 78 int ret;
dangriffin 0:0433918efb54 79
dangriffin 0:0433918efb54 80 /* Read the register content */
dangriffin 0:0433918efb54 81 //PRESSURE_IO_Read(buffer, LPS25H_SlaveAddress, LPS25H_PRESS_POUT_XL_ADDR+0x80, 3);
dangriffin 0:0433918efb54 82 ret = dev_i2c.i2c_read(buffer, LPS25H_ADDRESS_HIGH, LPS25H_PRESS_POUT_XL_ADDR+0x80, 3);
dangriffin 0:0433918efb54 83
dangriffin 1:354f61ffdcfb 84 if (ret == 0)
dangriffin 1:354f61ffdcfb 85 {
dangriffin 1:354f61ffdcfb 86 /* Build the raw data */
dangriffin 1:354f61ffdcfb 87 for (i = 0 ; i < 3 ; i++)
dangriffin 1:354f61ffdcfb 88 tempVal |= (((uint32_t) buffer[i]) << (8 * i));
dangriffin 0:0433918efb54 89
dangriffin 1:354f61ffdcfb 90 /* convert the 2's complement 24 bit to 2's complement 32 bit */
dangriffin 1:354f61ffdcfb 91 if (tempVal & 0x00800000)
dangriffin 1:354f61ffdcfb 92 tempVal |= 0xFF000000;
dangriffin 0:0433918efb54 93
dangriffin 1:354f61ffdcfb 94 /* return the built value */
dangriffin 1:354f61ffdcfb 95 *raw_press = ((uint32_t) tempVal);
dangriffin 1:354f61ffdcfb 96 }
dangriffin 0:0433918efb54 97 }
dangriffin 0:0433918efb54 98
dangriffin 0:0433918efb54 99 /**
dangriffin 0:0433918efb54 100 * @brief Read ID address of HTS221
dangriffin 0:0433918efb54 101 * @param Device ID address
dangriffin 0:0433918efb54 102 * @retval ID name
dangriffin 0:0433918efb54 103 */
dangriffin 0:0433918efb54 104 uint8_t LPS25H::ReadID(void)
dangriffin 0:0433918efb54 105 {
dangriffin 0:0433918efb54 106 uint8_t tmp;
dangriffin 0:0433918efb54 107
dangriffin 0:0433918efb54 108 /* Read the register content */
dangriffin 0:0433918efb54 109 int ret;
dangriffin 0:0433918efb54 110 //PRESSURE_IO_Read(&tmp, LPS25H_SlaveAddress, LPS25H_WHO_AM_I_ADDR, 1);
dangriffin 0:0433918efb54 111 ret = dev_i2c.i2c_read(&tmp, LPS25H_ADDRESS_HIGH, LPS25H_WHO_AM_I_ADDR, 1);
dangriffin 0:0433918efb54 112
dangriffin 0:0433918efb54 113 /* Return the ID */
dangriffin 1:354f61ffdcfb 114 return ((ret == 0) ? (uint8_t)tmp : 0);
dangriffin 0:0433918efb54 115 }
dangriffin 0:0433918efb54 116
dangriffin 0:0433918efb54 117 /**
dangriffin 0:0433918efb54 118 * @brief Set HTS221 Initialization.
dangriffin 0:0433918efb54 119 * @param InitStruct: it contains the configuration setting for the HTS221.
dangriffin 0:0433918efb54 120 * @retval None
dangriffin 0:0433918efb54 121 */
dangriffin 0:0433918efb54 122 void LPS25H::Init() {
dangriffin 0:0433918efb54 123 int ret;
dangriffin 0:0433918efb54 124 uint8_t tmp1 = 0x00;
dangriffin 0:0433918efb54 125
dangriffin 0:0433918efb54 126 Power_ON();
dangriffin 0:0433918efb54 127
dangriffin 0:0433918efb54 128 //PRESSURE_IO_Read(&tmp1, LPS25H_SlaveAddress, LPS25H_CTRL_REG1_ADDR, 1);
dangriffin 0:0433918efb54 129 ret = dev_i2c.i2c_read(&tmp1, LPS25H_ADDRESS_HIGH, LPS25H_CTRL_REG1_ADDR, 1);
dangriffin 0:0433918efb54 130
dangriffin 1:354f61ffdcfb 131 if (ret == 0)
dangriffin 1:354f61ffdcfb 132 {
dangriffin 1:354f61ffdcfb 133 /* Output Data Rate selection */
dangriffin 1:354f61ffdcfb 134 tmp1 &= ~(LPS25H_ODR_MASK);
dangriffin 1:354f61ffdcfb 135 tmp1 |= LPS25H_ODR_1Hz;
dangriffin 0:0433918efb54 136
dangriffin 1:354f61ffdcfb 137 /* Interrupt circuit selection */
dangriffin 1:354f61ffdcfb 138 tmp1 &= ~(LPS25H_DIFF_EN_MASK);
dangriffin 1:354f61ffdcfb 139 tmp1 |= LPS25H_DIFF_ENABLE;
dangriffin 0:0433918efb54 140
dangriffin 1:354f61ffdcfb 141 /* Block Data Update selection */
dangriffin 1:354f61ffdcfb 142 tmp1 &= ~(LPS25H_BDU_MASK);
dangriffin 1:354f61ffdcfb 143 tmp1 |= LPS25H_BDU_CONT;
dangriffin 1:354f61ffdcfb 144
dangriffin 1:354f61ffdcfb 145 /* Serial Interface Mode selection */
dangriffin 1:354f61ffdcfb 146 tmp1 &= ~(LPS25H_SPI_SIM_MASK);
dangriffin 1:354f61ffdcfb 147 tmp1 |= LPS25H_SPI_SIM_3W;
dangriffin 0:0433918efb54 148
dangriffin 1:354f61ffdcfb 149 //PRESSURE_IO_Write(&tmp1, LPS25H_SlaveAddress, LPS25H_CTRL_REG1_ADDR, 1);
dangriffin 1:354f61ffdcfb 150 ret = dev_i2c.i2c_write(&tmp1, LPS25H_ADDRESS_HIGH, LPS25H_CTRL_REG1_ADDR, 1);
dangriffin 1:354f61ffdcfb 151 }
dangriffin 1:354f61ffdcfb 152
dangriffin 1:354f61ffdcfb 153 if (ret == 0)
dangriffin 1:354f61ffdcfb 154 {
dangriffin 1:354f61ffdcfb 155 //PRESSURE_IO_Read(&tmp1, LPS25H_SlaveAddress, LPS25H_RES_CONF_ADDR, 1);
dangriffin 1:354f61ffdcfb 156 ret = dev_i2c.i2c_read(&tmp1, LPS25H_ADDRESS_HIGH, LPS25H_RES_CONF_ADDR, 1);
dangriffin 2:57888ec40e75 157 }
dangriffin 0:0433918efb54 158
dangriffin 2:57888ec40e75 159 if (ret == 0)
dangriffin 2:57888ec40e75 160 {
dangriffin 1:354f61ffdcfb 161 /* Pressure Res selection */
dangriffin 1:354f61ffdcfb 162 tmp1 &= ~(LPS25H_P_RES_MASK);
dangriffin 1:354f61ffdcfb 163 tmp1 |= LPS25H_P_RES_AVG_32;
dangriffin 0:0433918efb54 164
dangriffin 1:354f61ffdcfb 165 /* Temperature Res selection */
dangriffin 1:354f61ffdcfb 166 tmp1 &= ~(LPS25H_T_RES_MASK);
dangriffin 1:354f61ffdcfb 167 tmp1 |= LPS25H_T_RES_AVG_16;
dangriffin 0:0433918efb54 168
dangriffin 1:354f61ffdcfb 169 //PRESSURE_IO_Write(&tmp1, LPS25H_SlaveAddress, LPS25H_RES_CONF_ADDR, 1);
dangriffin 1:354f61ffdcfb 170 ret = dev_i2c.i2c_write(&tmp1, LPS25H_ADDRESS_HIGH, LPS25H_RES_CONF_ADDR, 1);
dangriffin 1:354f61ffdcfb 171 }
dangriffin 0:0433918efb54 172
dangriffin 1:354f61ffdcfb 173 if (ret == 0)
dangriffin 0:0433918efb54 174 {
dangriffin 1:354f61ffdcfb 175 if(ReadID() == I_AM_LPS25H)
dangriffin 1:354f61ffdcfb 176 {
dangriffin 1:354f61ffdcfb 177 Lps25hInitialized = 1;
dangriffin 1:354f61ffdcfb 178 //ret = HUM_TEMP_OK;
dangriffin 1:354f61ffdcfb 179 }
dangriffin 0:0433918efb54 180 }
dangriffin 0:0433918efb54 181
dangriffin 0:0433918efb54 182 return;
dangriffin 0:0433918efb54 183 }
dangriffin 0:0433918efb54 184
dangriffin 0:0433918efb54 185 int LPS25H::Power_ON() {
dangriffin 0:0433918efb54 186 uint8_t tmpreg;
dangriffin 0:0433918efb54 187 int ret;
dangriffin 0:0433918efb54 188
dangriffin 0:0433918efb54 189 /* Read the register content */
dangriffin 0:0433918efb54 190 //PRESSURE_IO_Read(&tmpreg, LPS25H_SlaveAddress, LPS25H_CTRL_REG1_ADDR, 1);
dangriffin 0:0433918efb54 191 ret = dev_i2c.i2c_read(&tmpreg, 0xBA, 0x20, 1);
dangriffin 0:0433918efb54 192
dangriffin 0:0433918efb54 193 /* Set the power down bit */
dangriffin 0:0433918efb54 194 tmpreg |= LPS25H_MODE_ACTIVE;
dangriffin 0:0433918efb54 195
dangriffin 0:0433918efb54 196 /* Write register */
dangriffin 0:0433918efb54 197 //PRESSURE_IO_Write(&tmpreg, LPS25H_SlaveAddress, LPS25H_CTRL_REG1_ADDR, 1);
dangriffin 0:0433918efb54 198 ret = dev_i2c.i2c_write(&tmpreg, LPS25H_ADDRESS_HIGH, LPS25H_CTRL_REG1_ADDR, 1);
dangriffin 0:0433918efb54 199 return ret;
dangriffin 0:0433918efb54 200
dangriffin 0:0433918efb54 201 }
dangriffin 0:0433918efb54 202
dangriffin 0:0433918efb54 203 int LPS25H::LPS25H_Calibration() {
dangriffin 0:0433918efb54 204
dangriffin 1:354f61ffdcfb 205 int ret = 0;
dangriffin 0:0433918efb54 206
dangriffin 0:0433918efb54 207 if(Lps25hInitialized == 1)
dangriffin 0:0433918efb54 208 {
dangriffin 0:0433918efb54 209 return 1; //TODO: Error Codes definitions
dangriffin 0:0433918efb54 210 }
dangriffin 0:0433918efb54 211
dangriffin 0:0433918efb54 212 return ret;
dangriffin 0:0433918efb54 213 }