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:
Sat Dec 13 01:09:48 2014 +0000
Revision:
0:0433918efb54
Child:
1:354f61ffdcfb
Create Nucleo sensor shield library.

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 0:0433918efb54 84 /* Build the raw data */
dangriffin 0:0433918efb54 85 for (i = 0 ; i < 3 ; i++)
dangriffin 0:0433918efb54 86 tempVal |= (((uint32_t) buffer[i]) << (8 * i));
dangriffin 0:0433918efb54 87
dangriffin 0:0433918efb54 88 /* convert the 2's complement 24 bit to 2's complement 32 bit */
dangriffin 0:0433918efb54 89 if (tempVal & 0x00800000)
dangriffin 0:0433918efb54 90 tempVal |= 0xFF000000;
dangriffin 0:0433918efb54 91
dangriffin 0:0433918efb54 92 /* return the built value */
dangriffin 0:0433918efb54 93 *raw_press = ((uint32_t) tempVal);
dangriffin 0:0433918efb54 94 }
dangriffin 0:0433918efb54 95
dangriffin 0:0433918efb54 96 /**
dangriffin 0:0433918efb54 97 * @brief Read ID address of HTS221
dangriffin 0:0433918efb54 98 * @param Device ID address
dangriffin 0:0433918efb54 99 * @retval ID name
dangriffin 0:0433918efb54 100 */
dangriffin 0:0433918efb54 101 uint8_t LPS25H::ReadID(void)
dangriffin 0:0433918efb54 102 {
dangriffin 0:0433918efb54 103 uint8_t tmp;
dangriffin 0:0433918efb54 104
dangriffin 0:0433918efb54 105 /* Read the register content */
dangriffin 0:0433918efb54 106 int ret;
dangriffin 0:0433918efb54 107 //PRESSURE_IO_Read(&tmp, LPS25H_SlaveAddress, LPS25H_WHO_AM_I_ADDR, 1);
dangriffin 0:0433918efb54 108 ret = dev_i2c.i2c_read(&tmp, LPS25H_ADDRESS_HIGH, LPS25H_WHO_AM_I_ADDR, 1);
dangriffin 0:0433918efb54 109
dangriffin 0:0433918efb54 110 /* Return the ID */
dangriffin 0:0433918efb54 111 return (uint8_t)tmp;
dangriffin 0:0433918efb54 112 }
dangriffin 0:0433918efb54 113
dangriffin 0:0433918efb54 114 /**
dangriffin 0:0433918efb54 115 * @brief Set HTS221 Initialization.
dangriffin 0:0433918efb54 116 * @param InitStruct: it contains the configuration setting for the HTS221.
dangriffin 0:0433918efb54 117 * @retval None
dangriffin 0:0433918efb54 118 */
dangriffin 0:0433918efb54 119 void LPS25H::Init() {
dangriffin 0:0433918efb54 120 int ret;
dangriffin 0:0433918efb54 121 uint8_t tmp1 = 0x00;
dangriffin 0:0433918efb54 122
dangriffin 0:0433918efb54 123 Power_ON();
dangriffin 0:0433918efb54 124
dangriffin 0:0433918efb54 125 //PRESSURE_IO_Read(&tmp1, LPS25H_SlaveAddress, LPS25H_CTRL_REG1_ADDR, 1);
dangriffin 0:0433918efb54 126 ret = dev_i2c.i2c_read(&tmp1, LPS25H_ADDRESS_HIGH, LPS25H_CTRL_REG1_ADDR, 1);
dangriffin 0:0433918efb54 127
dangriffin 0:0433918efb54 128 /* Output Data Rate selection */
dangriffin 0:0433918efb54 129 tmp1 &= ~(LPS25H_ODR_MASK);
dangriffin 0:0433918efb54 130 tmp1 |= LPS25H_ODR_1Hz;
dangriffin 0:0433918efb54 131
dangriffin 0:0433918efb54 132 /* Interrupt circuit selection */
dangriffin 0:0433918efb54 133 tmp1 &= ~(LPS25H_DIFF_EN_MASK);
dangriffin 0:0433918efb54 134 tmp1 |= LPS25H_DIFF_ENABLE;
dangriffin 0:0433918efb54 135
dangriffin 0:0433918efb54 136 /* Block Data Update selection */
dangriffin 0:0433918efb54 137 tmp1 &= ~(LPS25H_BDU_MASK);
dangriffin 0:0433918efb54 138 tmp1 |= LPS25H_BDU_CONT;
dangriffin 0:0433918efb54 139
dangriffin 0:0433918efb54 140 /* Serial Interface Mode selection */
dangriffin 0:0433918efb54 141 tmp1 &= ~(LPS25H_SPI_SIM_MASK);
dangriffin 0:0433918efb54 142 tmp1 |= LPS25H_SPI_SIM_3W;
dangriffin 0:0433918efb54 143
dangriffin 0:0433918efb54 144 //PRESSURE_IO_Write(&tmp1, LPS25H_SlaveAddress, LPS25H_CTRL_REG1_ADDR, 1);
dangriffin 0:0433918efb54 145 ret = dev_i2c.i2c_write(&tmp1, LPS25H_ADDRESS_HIGH, LPS25H_CTRL_REG1_ADDR, 1);
dangriffin 0:0433918efb54 146
dangriffin 0:0433918efb54 147 //PRESSURE_IO_Read(&tmp1, LPS25H_SlaveAddress, LPS25H_RES_CONF_ADDR, 1);
dangriffin 0:0433918efb54 148 ret = dev_i2c.i2c_read(&tmp1, LPS25H_ADDRESS_HIGH, LPS25H_RES_CONF_ADDR, 1);
dangriffin 0:0433918efb54 149
dangriffin 0:0433918efb54 150 /* Pressure Res selection */
dangriffin 0:0433918efb54 151 tmp1 &= ~(LPS25H_P_RES_MASK);
dangriffin 0:0433918efb54 152 tmp1 |= LPS25H_P_RES_AVG_32;
dangriffin 0:0433918efb54 153
dangriffin 0:0433918efb54 154 /* Temperature Res selection */
dangriffin 0:0433918efb54 155 tmp1 &= ~(LPS25H_T_RES_MASK);
dangriffin 0:0433918efb54 156 tmp1 |= LPS25H_T_RES_AVG_16;
dangriffin 0:0433918efb54 157
dangriffin 0:0433918efb54 158 //PRESSURE_IO_Write(&tmp1, LPS25H_SlaveAddress, LPS25H_RES_CONF_ADDR, 1);
dangriffin 0:0433918efb54 159 ret = dev_i2c.i2c_write(&tmp1, LPS25H_ADDRESS_HIGH, LPS25H_RES_CONF_ADDR, 1);
dangriffin 0:0433918efb54 160
dangriffin 0:0433918efb54 161 if(ReadID() == I_AM_LPS25H)
dangriffin 0:0433918efb54 162 {
dangriffin 0:0433918efb54 163 Lps25hInitialized = 1;
dangriffin 0:0433918efb54 164 //ret = HUM_TEMP_OK;
dangriffin 0:0433918efb54 165 }
dangriffin 0:0433918efb54 166
dangriffin 0:0433918efb54 167 return;
dangriffin 0:0433918efb54 168 }
dangriffin 0:0433918efb54 169
dangriffin 0:0433918efb54 170 int LPS25H::Power_ON() {
dangriffin 0:0433918efb54 171 uint8_t tmpreg;
dangriffin 0:0433918efb54 172 int ret;
dangriffin 0:0433918efb54 173
dangriffin 0:0433918efb54 174 /* Read the register content */
dangriffin 0:0433918efb54 175 //PRESSURE_IO_Read(&tmpreg, LPS25H_SlaveAddress, LPS25H_CTRL_REG1_ADDR, 1);
dangriffin 0:0433918efb54 176 ret = dev_i2c.i2c_read(&tmpreg, 0xBA, 0x20, 1);
dangriffin 0:0433918efb54 177
dangriffin 0:0433918efb54 178 /* Set the power down bit */
dangriffin 0:0433918efb54 179 tmpreg |= LPS25H_MODE_ACTIVE;
dangriffin 0:0433918efb54 180
dangriffin 0:0433918efb54 181 /* Write register */
dangriffin 0:0433918efb54 182 //PRESSURE_IO_Write(&tmpreg, LPS25H_SlaveAddress, LPS25H_CTRL_REG1_ADDR, 1);
dangriffin 0:0433918efb54 183 ret = dev_i2c.i2c_write(&tmpreg, LPS25H_ADDRESS_HIGH, LPS25H_CTRL_REG1_ADDR, 1);
dangriffin 0:0433918efb54 184 return ret;
dangriffin 0:0433918efb54 185
dangriffin 0:0433918efb54 186 }
dangriffin 0:0433918efb54 187
dangriffin 0:0433918efb54 188 int LPS25H::LPS25H_Calibration() {
dangriffin 0:0433918efb54 189
dangriffin 0:0433918efb54 190 int ret;
dangriffin 0:0433918efb54 191
dangriffin 0:0433918efb54 192 if(Lps25hInitialized == 1)
dangriffin 0:0433918efb54 193 {
dangriffin 0:0433918efb54 194 return 1; //TODO: Error Codes definitions
dangriffin 0:0433918efb54 195 }
dangriffin 0:0433918efb54 196
dangriffin 0:0433918efb54 197
dangriffin 0:0433918efb54 198
dangriffin 0:0433918efb54 199 return ret;
dangriffin 0:0433918efb54 200 }