STMicroelectronics barometric pressure sensor library. This library modified from KenjiArai's LPS25H library.

Fork of LPS25H by Kenji Arai

Committer:
feunoir
Date:
Mon Jun 19 15:17:49 2017 +0000
Revision:
5:5f8c19996d44
Parent:
4:50be6522da7f
Child:
6:c97ae96fea8f
remove comment out about freq setting in init func.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
feunoir 1:5f21b0eac2c2 1 /*
feunoir 1:5f21b0eac2c2 2 * mbed library program
feunoir 4:50be6522da7f 3 * LPS22HB MEMS pressure sensor: 260-1260 hPa absolute digital output barometer
feunoir 1:5f21b0eac2c2 4 * made by STMicroelectronics
feunoir 4:50be6522da7f 5 * http://www.st.com/ja/mems-and-sensors/lps22hb.html
feunoir 1:5f21b0eac2c2 6 *
feunoir 4:50be6522da7f 7 * Modified for LPS22HB by feunoir
feunoir 4:50be6522da7f 8 * http://mbed.org/users/feunoir/
feunoir 4:50be6522da7f 9 *
feunoir 1:5f21b0eac2c2 10 * Copyright (c) 2015 Kenji Arai / JH1PJL
feunoir 1:5f21b0eac2c2 11 * http://www.page.sannet.ne.jp/kenjia/index.html
feunoir 1:5f21b0eac2c2 12 * http://mbed.org/users/kenjiArai/
feunoir 1:5f21b0eac2c2 13 *
feunoir 1:5f21b0eac2c2 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
feunoir 1:5f21b0eac2c2 15 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
feunoir 1:5f21b0eac2c2 16 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
feunoir 1:5f21b0eac2c2 17 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
feunoir 1:5f21b0eac2c2 18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
feunoir 1:5f21b0eac2c2 19 */
feunoir 1:5f21b0eac2c2 20
feunoir 1:5f21b0eac2c2 21 #include "LPS22HB.h"
feunoir 1:5f21b0eac2c2 22
feunoir 3:755ac86eb6fd 23 LPS22HB::LPS22HB (PinName p_sda, PinName p_scl, uint8_t addr)
feunoir 3:755ac86eb6fd 24 :
feunoir 3:755ac86eb6fd 25 i2c_p(new I2C(p_sda, p_scl)),
feunoir 3:755ac86eb6fd 26 _i2c(*i2c_p)
feunoir 1:5f21b0eac2c2 27 {
feunoir 1:5f21b0eac2c2 28 LPS22HB_addr = addr;
feunoir 1:5f21b0eac2c2 29 init();
feunoir 1:5f21b0eac2c2 30 }
feunoir 1:5f21b0eac2c2 31
feunoir 3:755ac86eb6fd 32 LPS22HB::LPS22HB (I2C& p_i2c, uint8_t addr)
feunoir 3:755ac86eb6fd 33 :
feunoir 3:755ac86eb6fd 34 i2c_p(NULL),
feunoir 3:755ac86eb6fd 35 _i2c(p_i2c)
feunoir 1:5f21b0eac2c2 36 {
feunoir 1:5f21b0eac2c2 37 LPS22HB_addr = addr;
feunoir 1:5f21b0eac2c2 38 init();
feunoir 1:5f21b0eac2c2 39 }
feunoir 1:5f21b0eac2c2 40
feunoir 3:755ac86eb6fd 41 LPS22HB::~LPS22HB()
feunoir 1:5f21b0eac2c2 42 {
feunoir 3:755ac86eb6fd 43 if( NULL != i2c_p )
feunoir 3:755ac86eb6fd 44 delete i2c_p;
feunoir 3:755ac86eb6fd 45 }
feunoir 1:5f21b0eac2c2 46
feunoir 1:5f21b0eac2c2 47 /////////////// Initialize ////////////////////////////////
feunoir 1:5f21b0eac2c2 48 void LPS22HB::init(void)
feunoir 1:5f21b0eac2c2 49 {
feunoir 5:5f8c19996d44 50 _i2c.frequency(400000);
feunoir 1:5f21b0eac2c2 51 // Check acc is available of not
feunoir 1:5f21b0eac2c2 52 dt[0] = LPS22HB_WHO_AM_I;
feunoir 1:5f21b0eac2c2 53 _i2c.write(LPS22HB_addr, dt, 1, true);
feunoir 1:5f21b0eac2c2 54 _i2c.read(LPS22HB_addr, dt, 1, false);
feunoir 1:5f21b0eac2c2 55 if (dt[0] == I_AM_LPS22HB) {
feunoir 1:5f21b0eac2c2 56 LPS22HB_id = I_AM_LPS22HB;
feunoir 1:5f21b0eac2c2 57 LPS22HB_ready = 1;
feunoir 1:5f21b0eac2c2 58 } else {
feunoir 1:5f21b0eac2c2 59 LPS22HB_id = 0;
feunoir 1:5f21b0eac2c2 60 LPS22HB_ready = 0;
feunoir 1:5f21b0eac2c2 61 return; // acc chip is NOT on I2C line then terminate
feunoir 1:5f21b0eac2c2 62 }
feunoir 1:5f21b0eac2c2 63
feunoir 3:755ac86eb6fd 64 set_odr(LPS22HB_ODR_1HZ);
feunoir 1:5f21b0eac2c2 65 }
feunoir 1:5f21b0eac2c2 66
feunoir 1:5f21b0eac2c2 67 /////////////// Start conv. and gwt all data //////////////
feunoir 1:5f21b0eac2c2 68 void LPS22HB::get(void)
feunoir 1:5f21b0eac2c2 69 {
feunoir 1:5f21b0eac2c2 70 if (LPS22HB_ready == 0) {
feunoir 1:5f21b0eac2c2 71 press = 0;
feunoir 1:5f21b0eac2c2 72 temp = 0;
feunoir 1:5f21b0eac2c2 73 return;
feunoir 1:5f21b0eac2c2 74 }
feunoir 1:5f21b0eac2c2 75 dt[0] = LPS22HB_PRESS_POUT_XL | 0x80;
feunoir 1:5f21b0eac2c2 76 _i2c.write(LPS22HB_addr, dt, 1, true);
feunoir 1:5f21b0eac2c2 77 _i2c.read(LPS22HB_addr, dt, 3, false);
feunoir 1:5f21b0eac2c2 78 press = dt[2] << 16 | dt[1] << 8 | dt[0];
feunoir 1:5f21b0eac2c2 79 dt[0] = LPS22HB_TEMP_OUT_L | 0x80;
feunoir 1:5f21b0eac2c2 80 _i2c.write(LPS22HB_addr, dt, 1, true);
feunoir 1:5f21b0eac2c2 81 _i2c.read(LPS22HB_addr, dt, 2, false);
feunoir 1:5f21b0eac2c2 82 temp = dt[1] << 8 | dt[0];
feunoir 1:5f21b0eac2c2 83 }
feunoir 1:5f21b0eac2c2 84
feunoir 1:5f21b0eac2c2 85 /////////////// Read data from sensor /////////////////////
feunoir 1:5f21b0eac2c2 86 float LPS22HB::pressure()
feunoir 1:5f21b0eac2c2 87 {
feunoir 3:755ac86eb6fd 88 return (float)press / 4096.0f;
feunoir 3:755ac86eb6fd 89 }
feunoir 3:755ac86eb6fd 90
feunoir 3:755ac86eb6fd 91 //add by user
feunoir 3:755ac86eb6fd 92 uint32_t LPS22HB::pressure_raw()
feunoir 3:755ac86eb6fd 93 {
feunoir 3:755ac86eb6fd 94 return press;
feunoir 1:5f21b0eac2c2 95 }
feunoir 1:5f21b0eac2c2 96
feunoir 1:5f21b0eac2c2 97 /////////////// Read data from sensor /////////////////////
feunoir 1:5f21b0eac2c2 98 float LPS22HB::temperature()
feunoir 1:5f21b0eac2c2 99 {
feunoir 3:755ac86eb6fd 100 return (float)temp / 100.0f;
feunoir 3:755ac86eb6fd 101 }
feunoir 3:755ac86eb6fd 102
feunoir 3:755ac86eb6fd 103 //add by user
feunoir 3:755ac86eb6fd 104 int16_t LPS22HB::temperature_raw()
feunoir 3:755ac86eb6fd 105 {
feunoir 3:755ac86eb6fd 106 return temp;
feunoir 1:5f21b0eac2c2 107 }
feunoir 1:5f21b0eac2c2 108
feunoir 1:5f21b0eac2c2 109 /////////////// ID ////////////////////////////////////////
feunoir 1:5f21b0eac2c2 110 uint8_t LPS22HB::read_id()
feunoir 1:5f21b0eac2c2 111 {
feunoir 1:5f21b0eac2c2 112 dt[0] = LPS22HB_WHO_AM_I;
feunoir 1:5f21b0eac2c2 113 _i2c.write(LPS22HB_addr, dt, 1, true);
feunoir 1:5f21b0eac2c2 114 _i2c.read(LPS22HB_addr, dt, 1, false);
feunoir 1:5f21b0eac2c2 115 return (uint8_t)dt[0];
feunoir 1:5f21b0eac2c2 116 }
feunoir 1:5f21b0eac2c2 117
feunoir 1:5f21b0eac2c2 118 /////////////// I2C Freq. /////////////////////////////////
feunoir 1:5f21b0eac2c2 119 void LPS22HB::frequency(int hz)
feunoir 1:5f21b0eac2c2 120 {
feunoir 1:5f21b0eac2c2 121 _i2c.frequency(hz);
feunoir 1:5f21b0eac2c2 122 }
feunoir 1:5f21b0eac2c2 123
feunoir 1:5f21b0eac2c2 124 /////////////// General purpose R/W ///////////////////////
feunoir 1:5f21b0eac2c2 125 uint8_t LPS22HB::read_reg(uint8_t addr)
feunoir 1:5f21b0eac2c2 126 {
feunoir 1:5f21b0eac2c2 127 if (LPS22HB_ready == 1) {
feunoir 1:5f21b0eac2c2 128 dt[0] = addr;
feunoir 1:5f21b0eac2c2 129 _i2c.write(LPS22HB_addr, dt, 1, true);
feunoir 1:5f21b0eac2c2 130 _i2c.read(LPS22HB_addr, dt, 1, false);
feunoir 1:5f21b0eac2c2 131 } else {
feunoir 1:5f21b0eac2c2 132 dt[0] = 0xff;
feunoir 1:5f21b0eac2c2 133 }
feunoir 1:5f21b0eac2c2 134 return (uint8_t)dt[0];
feunoir 1:5f21b0eac2c2 135 }
feunoir 1:5f21b0eac2c2 136
feunoir 1:5f21b0eac2c2 137 void LPS22HB::write_reg(uint8_t addr, uint8_t data)
feunoir 1:5f21b0eac2c2 138 {
feunoir 1:5f21b0eac2c2 139 if (LPS22HB_ready == 1) {
feunoir 1:5f21b0eac2c2 140 dt[0] = addr;
feunoir 1:5f21b0eac2c2 141 dt[1] = data;
feunoir 1:5f21b0eac2c2 142 _i2c.write(LPS22HB_addr, dt, 2, false);
feunoir 1:5f21b0eac2c2 143 }
feunoir 1:5f21b0eac2c2 144 }
feunoir 1:5f21b0eac2c2 145
feunoir 1:5f21b0eac2c2 146 /////////////// ODR ///////////////////////////////////////
feunoir 1:5f21b0eac2c2 147 void LPS22HB::set_odr(lps22hb_odr odrcfg)
feunoir 1:5f21b0eac2c2 148 {
feunoir 1:5f21b0eac2c2 149 uint8_t temp = read_reg(LPS22HB_CTRL_REG1);
feunoir 1:5f21b0eac2c2 150 temp &= 0xff ^ 0x70;
feunoir 1:5f21b0eac2c2 151 temp |= odrcfg;
feunoir 1:5f21b0eac2c2 152 write_reg(LPS22HB_CTRL_REG1, temp);
feunoir 1:5f21b0eac2c2 153 }
feunoir 1:5f21b0eac2c2 154
feunoir 1:5f21b0eac2c2 155 /////////////// LPF ///////////////////////////////////////
feunoir 1:5f21b0eac2c2 156 void LPS22HB::set_lpf(lps22hb_lpf lpfcfg)
feunoir 1:5f21b0eac2c2 157 {
feunoir 1:5f21b0eac2c2 158 uint8_t temp = read_reg(LPS22HB_CTRL_REG1);
feunoir 1:5f21b0eac2c2 159 temp &= 0xff ^ 0x0c;
feunoir 1:5f21b0eac2c2 160 temp |= lpfcfg;
feunoir 1:5f21b0eac2c2 161 write_reg(LPS22HB_CTRL_REG1, temp);
feunoir 1:5f21b0eac2c2 162 }
feunoir 1:5f21b0eac2c2 163
feunoir 1:5f21b0eac2c2 164 /////////////// DRDY //////////////////////////////////////
feunoir 1:5f21b0eac2c2 165 void LPS22HB::drdy(lps22hb_drdy drdycfg)
feunoir 1:5f21b0eac2c2 166 {
feunoir 1:5f21b0eac2c2 167 uint8_t temp = read_reg(LPS22HB_CTRL_REG3);
feunoir 1:5f21b0eac2c2 168 temp &= 0xff ^ 0x04;
feunoir 1:5f21b0eac2c2 169 temp |= drdycfg;
feunoir 1:5f21b0eac2c2 170 write_reg(LPS22HB_CTRL_REG3, temp);
feunoir 1:5f21b0eac2c2 171 }