only test purpose not official

Committer:
kenjiArai
Date:
Fri Feb 07 00:53:03 2020 +0000
Revision:
7:0414b9099641
Parent:
6:c97ae96fea8f
only test purpose not official

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 6:c97ae96fea8f 7 * Modified for LPS22HB by Taro Watanabe
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 6:c97ae96fea8f 91 /////////////// Read raw data from sensor /////////////////
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 6:c97ae96fea8f 103 /////////////// Read raw data from sensor /////////////////
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
kenjiArai 7:0414b9099641 109 /////////////// Check data ready //////////////////////////
kenjiArai 7:0414b9099641 110 uint8_t LPS22HB::data_ready()
kenjiArai 7:0414b9099641 111 {
kenjiArai 7:0414b9099641 112 dt[0] = LPS22HB_STATUS_REG;
kenjiArai 7:0414b9099641 113 _i2c.write(LPS22HB_addr, dt, 1, true);
kenjiArai 7:0414b9099641 114 _i2c.read(LPS22HB_addr, dt, 1, false);
kenjiArai 7:0414b9099641 115 if (dt[0]) {
kenjiArai 7:0414b9099641 116 return 1;
kenjiArai 7:0414b9099641 117 } else {
kenjiArai 7:0414b9099641 118 return 0;
kenjiArai 7:0414b9099641 119 }
kenjiArai 7:0414b9099641 120 }
kenjiArai 7:0414b9099641 121
feunoir 1:5f21b0eac2c2 122 /////////////// ID ////////////////////////////////////////
feunoir 1:5f21b0eac2c2 123 uint8_t LPS22HB::read_id()
feunoir 1:5f21b0eac2c2 124 {
feunoir 1:5f21b0eac2c2 125 dt[0] = LPS22HB_WHO_AM_I;
feunoir 1:5f21b0eac2c2 126 _i2c.write(LPS22HB_addr, dt, 1, true);
feunoir 1:5f21b0eac2c2 127 _i2c.read(LPS22HB_addr, dt, 1, false);
feunoir 1:5f21b0eac2c2 128 return (uint8_t)dt[0];
feunoir 1:5f21b0eac2c2 129 }
feunoir 1:5f21b0eac2c2 130
feunoir 1:5f21b0eac2c2 131 /////////////// I2C Freq. /////////////////////////////////
feunoir 1:5f21b0eac2c2 132 void LPS22HB::frequency(int hz)
feunoir 1:5f21b0eac2c2 133 {
feunoir 1:5f21b0eac2c2 134 _i2c.frequency(hz);
feunoir 1:5f21b0eac2c2 135 }
feunoir 1:5f21b0eac2c2 136
feunoir 1:5f21b0eac2c2 137 /////////////// General purpose R/W ///////////////////////
feunoir 1:5f21b0eac2c2 138 uint8_t LPS22HB::read_reg(uint8_t addr)
feunoir 1:5f21b0eac2c2 139 {
feunoir 1:5f21b0eac2c2 140 if (LPS22HB_ready == 1) {
feunoir 1:5f21b0eac2c2 141 dt[0] = addr;
feunoir 1:5f21b0eac2c2 142 _i2c.write(LPS22HB_addr, dt, 1, true);
feunoir 1:5f21b0eac2c2 143 _i2c.read(LPS22HB_addr, dt, 1, false);
feunoir 1:5f21b0eac2c2 144 } else {
feunoir 1:5f21b0eac2c2 145 dt[0] = 0xff;
feunoir 1:5f21b0eac2c2 146 }
feunoir 1:5f21b0eac2c2 147 return (uint8_t)dt[0];
feunoir 1:5f21b0eac2c2 148 }
feunoir 1:5f21b0eac2c2 149
feunoir 1:5f21b0eac2c2 150 void LPS22HB::write_reg(uint8_t addr, uint8_t data)
feunoir 1:5f21b0eac2c2 151 {
feunoir 1:5f21b0eac2c2 152 if (LPS22HB_ready == 1) {
feunoir 1:5f21b0eac2c2 153 dt[0] = addr;
feunoir 1:5f21b0eac2c2 154 dt[1] = data;
feunoir 1:5f21b0eac2c2 155 _i2c.write(LPS22HB_addr, dt, 2, false);
feunoir 1:5f21b0eac2c2 156 }
feunoir 1:5f21b0eac2c2 157 }
feunoir 1:5f21b0eac2c2 158
feunoir 1:5f21b0eac2c2 159 /////////////// ODR ///////////////////////////////////////
feunoir 1:5f21b0eac2c2 160 void LPS22HB::set_odr(lps22hb_odr odrcfg)
feunoir 1:5f21b0eac2c2 161 {
feunoir 1:5f21b0eac2c2 162 uint8_t temp = read_reg(LPS22HB_CTRL_REG1);
feunoir 1:5f21b0eac2c2 163 temp &= 0xff ^ 0x70;
feunoir 1:5f21b0eac2c2 164 temp |= odrcfg;
feunoir 1:5f21b0eac2c2 165 write_reg(LPS22HB_CTRL_REG1, temp);
feunoir 1:5f21b0eac2c2 166 }
feunoir 1:5f21b0eac2c2 167
feunoir 1:5f21b0eac2c2 168 /////////////// LPF ///////////////////////////////////////
feunoir 1:5f21b0eac2c2 169 void LPS22HB::set_lpf(lps22hb_lpf lpfcfg)
feunoir 1:5f21b0eac2c2 170 {
feunoir 1:5f21b0eac2c2 171 uint8_t temp = read_reg(LPS22HB_CTRL_REG1);
feunoir 1:5f21b0eac2c2 172 temp &= 0xff ^ 0x0c;
feunoir 1:5f21b0eac2c2 173 temp |= lpfcfg;
feunoir 1:5f21b0eac2c2 174 write_reg(LPS22HB_CTRL_REG1, temp);
feunoir 1:5f21b0eac2c2 175 }
feunoir 1:5f21b0eac2c2 176
feunoir 1:5f21b0eac2c2 177 /////////////// DRDY //////////////////////////////////////
feunoir 1:5f21b0eac2c2 178 void LPS22HB::drdy(lps22hb_drdy drdycfg)
feunoir 1:5f21b0eac2c2 179 {
feunoir 1:5f21b0eac2c2 180 uint8_t temp = read_reg(LPS22HB_CTRL_REG3);
feunoir 1:5f21b0eac2c2 181 temp &= 0xff ^ 0x04;
feunoir 1:5f21b0eac2c2 182 temp |= drdycfg;
feunoir 1:5f21b0eac2c2 183 write_reg(LPS22HB_CTRL_REG3, temp);
feunoir 1:5f21b0eac2c2 184 }