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

Fork of LPS25H by Kenji Arai

Committer:
feunoir
Date:
Sun Mar 19 09:07:48 2017 +0000
Revision:
3:755ac86eb6fd
Parent:
1:5f21b0eac2c2
Child:
4:50be6522da7f
some optimization

Who changed what in which revision?

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