Class module for MPL3115A2 I2C Barometric Sensor
Dependents: mDotEVBM2X MTDOT-EVBDemo-DRH MTDOT-BOX-EVB-Factory-Firmware-LIB-108 MTDOT-UDKDemo_Senet ... more
MPL3115A2.cpp@5:8be678fd9e55, 2018-01-31 (annotated)
- Committer:
- Evan Hosseini
- Date:
- Wed Jan 31 10:09:42 2018 -0600
- Revision:
- 5:8be678fd9e55
- Parent:
- 4:b612babc2c3b
Make driver thread safe by making i2c read transactions atomic
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
falingtrea | 0:3f52908a334d | 1 | /** |
falingtrea | 0:3f52908a334d | 2 | * @file MPL3115A2.cpp |
falingtrea | 0:3f52908a334d | 3 | * @brief Device driver - MPL3115A2 barometric pressure IC w/RTOS support |
falingtrea | 0:3f52908a334d | 4 | * @author Tim Barr |
falingtrea | 0:3f52908a334d | 5 | * @version 1.0 |
falingtrea | 0:3f52908a334d | 6 | * @see http://cache.freescale.com/files/sensors/doc/data_sheet/MPL3115A2.pdf |
falingtrea | 0:3f52908a334d | 7 | * |
falingtrea | 0:3f52908a334d | 8 | * Copyright (c) 2015 |
falingtrea | 0:3f52908a334d | 9 | * |
falingtrea | 0:3f52908a334d | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); |
falingtrea | 0:3f52908a334d | 11 | * you may not use this file except in compliance with the License. |
falingtrea | 0:3f52908a334d | 12 | * You may obtain a copy of the License at |
falingtrea | 0:3f52908a334d | 13 | * |
falingtrea | 0:3f52908a334d | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
falingtrea | 0:3f52908a334d | 15 | * |
falingtrea | 0:3f52908a334d | 16 | * Unless required by applicable law or agreed to in writing, software |
falingtrea | 0:3f52908a334d | 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
falingtrea | 0:3f52908a334d | 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
falingtrea | 0:3f52908a334d | 19 | * See the License for the specific language governing permissions and |
falingtrea | 0:3f52908a334d | 20 | * limitations under the License. |
falingtrea | 0:3f52908a334d | 21 | */ |
mfiore | 1:072fd24cc12b | 22 | |
falingtrea | 0:3f52908a334d | 23 | #include "MPL3115A2.h" |
falingtrea | 0:3f52908a334d | 24 | #include "mbed_debug.h" |
falingtrea | 0:3f52908a334d | 25 | #include "rtos.h" |
mfiore | 1:072fd24cc12b | 26 | |
falingtrea | 0:3f52908a334d | 27 | MPL3115A2::MPL3115A2( I2C &i2c, InterruptIn* int1, InterruptIn* int2) |
falingtrea | 0:3f52908a334d | 28 | { |
falingtrea | 0:3f52908a334d | 29 | _i2c = &i2c; |
falingtrea | 0:3f52908a334d | 30 | _int1 = int1; |
falingtrea | 0:3f52908a334d | 31 | _int2 = int2; |
mfiore | 1:072fd24cc12b | 32 | |
mfiore | 1:072fd24cc12b | 33 | MPL3115A2::init(); |
falingtrea | 0:3f52908a334d | 34 | |
falingtrea | 0:3f52908a334d | 35 | return; |
falingtrea | 0:3f52908a334d | 36 | } |
falingtrea | 0:3f52908a334d | 37 | |
falingtrea | 0:3f52908a334d | 38 | bool MPL3115A2::testWhoAmI(void) |
falingtrea | 0:3f52908a334d | 39 | { |
mfiore | 1:072fd24cc12b | 40 | char reg_val[1]; |
falingtrea | 0:3f52908a334d | 41 | |
mfiore | 1:072fd24cc12b | 42 | reg_val[0] = 0x00; |
mfiore | 1:072fd24cc12b | 43 | MPL3115A2::readRegister(WHO_AM_I,reg_val); |
falingtrea | 0:3f52908a334d | 44 | |
mfiore | 1:072fd24cc12b | 45 | return (reg_val[0] == I_AM_MPL3115A2); |
falingtrea | 0:3f52908a334d | 46 | |
falingtrea | 0:3f52908a334d | 47 | } |
falingtrea | 0:3f52908a334d | 48 | |
falingtrea | 0:3f52908a334d | 49 | uint8_t MPL3115A2::init(void) |
falingtrea | 0:3f52908a334d | 50 | { |
falingtrea | 0:3f52908a334d | 51 | uint8_t result = 0; |
falingtrea | 0:3f52908a334d | 52 | uint8_t i = 0; |
falingtrea | 0:3f52908a334d | 53 | char reg_val[1]; |
mfiore | 1:072fd24cc12b | 54 | |
falingtrea | 0:3f52908a334d | 55 | // Reset all registers to POR values |
falingtrea | 0:3f52908a334d | 56 | reg_val[0] = 0x04; |
falingtrea | 0:3f52908a334d | 57 | result = MPL3115A2::writeRegister(CTRL_REG1, reg_val); |
rklaassen | 4:b612babc2c3b | 58 | do { |
rklaassen | 4:b612babc2c3b | 59 | // wait for the reset bit to clear. readRegister may error out so we re-try 10 times |
rklaassen | 4:b612babc2c3b | 60 | osDelay(200); |
rklaassen | 4:b612babc2c3b | 61 | reg_val[0] = 0x40; |
rklaassen | 4:b612babc2c3b | 62 | result = MPL3115A2::readRegister(CTRL_REG1,reg_val); |
rklaassen | 4:b612babc2c3b | 63 | reg_val[0] = reg_val[0] & 0x04; |
rklaassen | 4:b612babc2c3b | 64 | i++; |
rklaassen | 4:b612babc2c3b | 65 | } while(((reg_val[0] != 0) || (result != 0)) && (i<=10)); |
falingtrea | 0:3f52908a334d | 66 | |
mfiore | 1:072fd24cc12b | 67 | if ((result == 0) && (MPL3115A2::testWhoAmI() == true)) { |
falingtrea | 0:3f52908a334d | 68 | |
mfiore | 1:072fd24cc12b | 69 | if ((_int1 == NULL) && (_int2 == NULL)) { |
mfiore | 1:072fd24cc12b | 70 | _polling_mode = true; |
mfiore | 1:072fd24cc12b | 71 | reg_val[0] = 0x07; |
mfiore | 1:072fd24cc12b | 72 | result |= MPL3115A2::writeRegister(PT_DATA_CFG,reg_val); |
mfiore | 1:072fd24cc12b | 73 | } else _polling_mode = false; |
mfiore | 1:072fd24cc12b | 74 | } else { |
mfiore | 1:072fd24cc12b | 75 | debug ("Device not supported by this library!\n\r"); |
mfiore | 1:072fd24cc12b | 76 | result = 1; |
falingtrea | 0:3f52908a334d | 77 | } |
falingtrea | 0:3f52908a334d | 78 | |
Evan Hosseini |
5:8be678fd9e55 | 79 | if (result != 0) { |
falingtrea | 0:3f52908a334d | 80 | debug("MPL3115A2:init failed\n\r"); |
falingtrea | 0:3f52908a334d | 81 | } |
Evan Hosseini |
5:8be678fd9e55 | 82 | |
falingtrea | 0:3f52908a334d | 83 | return result; |
falingtrea | 0:3f52908a334d | 84 | } |
falingtrea | 0:3f52908a334d | 85 | |
falingtrea | 0:3f52908a334d | 86 | uint8_t MPL3115A2::setParameters(OUTPUT_MODE out_mode, DATA_MODE data_mode, OVERSAMPLE_RATIO os_ratio, |
mfiore | 1:072fd24cc12b | 87 | ACQUISITION_TIMER measure_time) |
falingtrea | 0:3f52908a334d | 88 | { |
falingtrea | 0:3f52908a334d | 89 | uint8_t result = 0; |
falingtrea | 0:3f52908a334d | 90 | char datain[4]; |
falingtrea | 0:3f52908a334d | 91 | char dataout[4]; |
falingtrea | 0:3f52908a334d | 92 | |
falingtrea | 0:3f52908a334d | 93 | result |= MPL3115A2::readRegister(SYSMOD, datain); // Make sure MPL3115A2 is in Stand-By mode |
mfiore | 1:072fd24cc12b | 94 | if ((datain[0] & 0x01) != 0 ) { |
mfiore | 1:072fd24cc12b | 95 | debug ("MPL3115A2 not in STAND BY mode\n\f"); |
falingtrea | 0:3f52908a334d | 96 | debug("MPL3115A2:setParameters failed\n\r"); |
falingtrea | 0:3f52908a334d | 97 | result = 1; |
falingtrea | 0:3f52908a334d | 98 | return result; |
falingtrea | 0:3f52908a334d | 99 | } |
falingtrea | 0:3f52908a334d | 100 | |
falingtrea | 0:3f52908a334d | 101 | result |= MPL3115A2::readRegister(CTRL_REG1, datain, 2); |
falingtrea | 0:3f52908a334d | 102 | dataout[0] = (datain[0] & 0x07) | os_ratio | out_mode | data_mode; |
falingtrea | 0:3f52908a334d | 103 | dataout[1] = (datain[1] & 0xF0) | measure_time; |
falingtrea | 0:3f52908a334d | 104 | result |= MPL3115A2::writeRegister(CTRL_REG1, dataout, 2); |
falingtrea | 0:3f52908a334d | 105 | |
mfiore | 1:072fd24cc12b | 106 | if(result != 0) { |
falingtrea | 0:3f52908a334d | 107 | debug("MPL3115A2:setParameters failed\n\r"); |
falingtrea | 0:3f52908a334d | 108 | } |
mfiore | 1:072fd24cc12b | 109 | |
mfiore | 1:072fd24cc12b | 110 | return result; |
falingtrea | 0:3f52908a334d | 111 | } |
falingtrea | 0:3f52908a334d | 112 | |
falingtrea | 0:3f52908a334d | 113 | uint8_t MPL3115A2::enableFIFO(void) |
falingtrea | 0:3f52908a334d | 114 | { |
falingtrea | 0:3f52908a334d | 115 | uint8_t result = 0; |
falingtrea | 0:3f52908a334d | 116 | return result; |
mfiore | 1:072fd24cc12b | 117 | } |
falingtrea | 0:3f52908a334d | 118 | |
falingtrea | 0:3f52908a334d | 119 | uint8_t MPL3115A2::activeMode(void) |
falingtrea | 0:3f52908a334d | 120 | { |
falingtrea | 0:3f52908a334d | 121 | uint8_t result = 0; |
falingtrea | 0:3f52908a334d | 122 | char datain[1]; |
falingtrea | 0:3f52908a334d | 123 | char dataout[1]; |
falingtrea | 0:3f52908a334d | 124 | |
falingtrea | 0:3f52908a334d | 125 | result |= MPL3115A2::readRegister(CTRL_REG1, datain , 2); |
falingtrea | 0:3f52908a334d | 126 | dataout[0] = (datain[0] & 0xFE) | 0x01 ; |
falingtrea | 0:3f52908a334d | 127 | result |= MPL3115A2::writeRegister(CTRL_REG1, dataout); // Set to active mode |
falingtrea | 0:3f52908a334d | 128 | |
falingtrea | 0:3f52908a334d | 129 | return result; |
mfiore | 1:072fd24cc12b | 130 | } |
falingtrea | 0:3f52908a334d | 131 | uint8_t MPL3115A2::standbyMode(void) |
falingtrea | 0:3f52908a334d | 132 | { |
falingtrea | 0:3f52908a334d | 133 | uint8_t result = 0; |
falingtrea | 0:3f52908a334d | 134 | char datain[1]; |
falingtrea | 0:3f52908a334d | 135 | char dataout[1]; |
falingtrea | 0:3f52908a334d | 136 | |
falingtrea | 0:3f52908a334d | 137 | result |= MPL3115A2::readRegister(CTRL_REG1, datain); |
falingtrea | 0:3f52908a334d | 138 | dataout[0] = (datain[0] & 0xFE); |
falingtrea | 0:3f52908a334d | 139 | result |= MPL3115A2::writeRegister(CTRL_REG1, dataout); // Set to standby mode |
falingtrea | 0:3f52908a334d | 140 | |
falingtrea | 0:3f52908a334d | 141 | return result; |
mfiore | 1:072fd24cc12b | 142 | } |
falingtrea | 0:3f52908a334d | 143 | |
falingtrea | 0:3f52908a334d | 144 | uint8_t MPL3115A2::triggerOneShot(void) |
falingtrea | 0:3f52908a334d | 145 | { |
falingtrea | 0:3f52908a334d | 146 | uint8_t result = 0; |
falingtrea | 0:3f52908a334d | 147 | char datain[1]; |
falingtrea | 0:3f52908a334d | 148 | char dataout[1]; |
falingtrea | 0:3f52908a334d | 149 | |
falingtrea | 0:3f52908a334d | 150 | result |= MPL3115A2::readRegister(CTRL_REG1, datain); |
falingtrea | 0:3f52908a334d | 151 | dataout[0] = ((datain[0] & 0xFD) | 0x02); |
falingtrea | 0:3f52908a334d | 152 | result |= MPL3115A2::writeRegister(CTRL_REG1, dataout); // Trigger a measurement |
falingtrea | 0:3f52908a334d | 153 | |
falingtrea | 0:3f52908a334d | 154 | return result; |
mfiore | 1:072fd24cc12b | 155 | } |
mfiore | 1:072fd24cc12b | 156 | |
falingtrea | 0:3f52908a334d | 157 | uint8_t MPL3115A2::setAltitudeCalib(int16_t alti_calib) |
falingtrea | 0:3f52908a334d | 158 | { |
falingtrea | 0:3f52908a334d | 159 | uint8_t result = 0; |
falingtrea | 0:3f52908a334d | 160 | char dataout[1]; |
falingtrea | 0:3f52908a334d | 161 | |
falingtrea | 0:3f52908a334d | 162 | dataout[0] = alti_calib ; |
falingtrea | 0:3f52908a334d | 163 | result |= MPL3115A2::writeRegister(CTRL_REG1, dataout); // set msb of calibration value |
falingtrea | 0:3f52908a334d | 164 | |
falingtrea | 0:3f52908a334d | 165 | return result; |
mfiore | 1:072fd24cc12b | 166 | } |
falingtrea | 0:3f52908a334d | 167 | |
falingtrea | 0:3f52908a334d | 168 | uint8_t MPL3115A2::clearMinMaxRegs(void) |
falingtrea | 0:3f52908a334d | 169 | { |
falingtrea | 0:3f52908a334d | 170 | uint8_t result = 0; |
falingtrea | 0:3f52908a334d | 171 | char datain[10]; |
falingtrea | 0:3f52908a334d | 172 | |
falingtrea | 0:3f52908a334d | 173 | memset(datain, 0, 10); |
falingtrea | 0:3f52908a334d | 174 | result = MPL3115A2::writeRegister(P_MIN_MSB, datain, 10); |
falingtrea | 0:3f52908a334d | 175 | |
falingtrea | 0:3f52908a334d | 176 | return result; |
mfiore | 1:072fd24cc12b | 177 | } |
falingtrea | 0:3f52908a334d | 178 | |
falingtrea | 0:3f52908a334d | 179 | uint8_t MPL3115A2::getStatus(void) |
falingtrea | 0:3f52908a334d | 180 | { |
falingtrea | 0:3f52908a334d | 181 | char datain[1]; |
falingtrea | 0:3f52908a334d | 182 | uint8_t dataout; |
falingtrea | 0:3f52908a334d | 183 | |
falingtrea | 0:3f52908a334d | 184 | MPL3115A2::readRegister(DR_STATUS, datain); |
mfiore | 1:072fd24cc12b | 185 | dataout = datain[0]; |
falingtrea | 0:3f52908a334d | 186 | |
falingtrea | 0:3f52908a334d | 187 | return dataout; |
mfiore | 1:072fd24cc12b | 188 | } |
falingtrea | 0:3f52908a334d | 189 | |
falingtrea | 0:3f52908a334d | 190 | int32_t MPL3115A2::getBaroData(void) |
falingtrea | 0:3f52908a334d | 191 | { |
mfiore | 1:072fd24cc12b | 192 | if (_polling_mode) { |
mfiore | 1:072fd24cc12b | 193 | char datain[3]; |
falingtrea | 0:3f52908a334d | 194 | MPL3115A2::readRegister(OUT_P_MSB, datain, 3); |
mfiore | 1:072fd24cc12b | 195 | /* data is 20 bit signed/unsigned with 4 LSB = 0 Need to shift to 32 bits to preserve sign bit |
mfiore | 1:072fd24cc12b | 196 | * Altitude is 16 bit signed and pressure is 18 bit unsigned |
mfiore | 1:072fd24cc12b | 197 | */ |
falingtrea | 0:3f52908a334d | 198 | _data._baro = ((datain[0] << 24) | (datain[1] << 16) | (datain[2]<<8)); |
falingtrea | 0:3f52908a334d | 199 | } |
mfiore | 1:072fd24cc12b | 200 | return _data._baro; |
falingtrea | 0:3f52908a334d | 201 | } |
mfiore | 1:072fd24cc12b | 202 | |
falingtrea | 0:3f52908a334d | 203 | int16_t MPL3115A2::getTempData(void) |
falingtrea | 0:3f52908a334d | 204 | { |
mfiore | 1:072fd24cc12b | 205 | if (_polling_mode) { |
falingtrea | 0:3f52908a334d | 206 | char datain[2]; |
falingtrea | 0:3f52908a334d | 207 | MPL3115A2::readRegister(OUT_T_MSB, datain, 2); |
mfiore | 1:072fd24cc12b | 208 | /* data is 12 bit signed with 4 LSB = 0 Need to shift first to 16 bits to preserve sign bit then |
mfiore | 1:072fd24cc12b | 209 | * divide by 16 to remove LSBs |
mfiore | 1:072fd24cc12b | 210 | */ |
falingtrea | 0:3f52908a334d | 211 | _data._temp = ((datain[0] << 8) | datain[1]); |
falingtrea | 0:3f52908a334d | 212 | _data._temp /= 16; |
falingtrea | 0:3f52908a334d | 213 | } |
mfiore | 1:072fd24cc12b | 214 | return _data._temp; |
falingtrea | 0:3f52908a334d | 215 | } |
falingtrea | 0:3f52908a334d | 216 | |
falingtrea | 0:3f52908a334d | 217 | int32_t MPL3115A2::getMinBaro(bool clear_data) |
falingtrea | 0:3f52908a334d | 218 | { |
mfiore | 1:072fd24cc12b | 219 | if (_polling_mode) { |
falingtrea | 0:3f52908a334d | 220 | char datain[3]; |
falingtrea | 0:3f52908a334d | 221 | MPL3115A2::readRegister(P_MIN_MSB, datain, 3); |
falingtrea | 0:3f52908a334d | 222 | /* data is 20 bit signed/unsigned with 4 LSB = 0 Need to shift to 32 bits to preserve sign bit |
falingtrea | 0:3f52908a334d | 223 | * Altitude is 16 bit signed and pressure is 18 bit unsigned |
falingtrea | 0:3f52908a334d | 224 | */ |
falingtrea | 0:3f52908a334d | 225 | _data._minbaro = ((datain[0] << 24) | (datain[1] << 16) | (datain[2] << 8)); |
falingtrea | 0:3f52908a334d | 226 | |
mfiore | 1:072fd24cc12b | 227 | if (clear_data) { |
mfiore | 1:072fd24cc12b | 228 | memset(datain, 0, 3); |
falingtrea | 0:3f52908a334d | 229 | MPL3115A2::writeRegister(P_MIN_MSB, datain, 3); |
falingtrea | 0:3f52908a334d | 230 | } |
falingtrea | 0:3f52908a334d | 231 | } |
falingtrea | 0:3f52908a334d | 232 | |
falingtrea | 0:3f52908a334d | 233 | return _data._minbaro; |
falingtrea | 0:3f52908a334d | 234 | } |
falingtrea | 0:3f52908a334d | 235 | |
falingtrea | 0:3f52908a334d | 236 | int32_t MPL3115A2::getMaxBaro(bool clear_data) |
falingtrea | 0:3f52908a334d | 237 | { |
mfiore | 1:072fd24cc12b | 238 | if (_polling_mode) { |
falingtrea | 0:3f52908a334d | 239 | char datain[3]; |
falingtrea | 0:3f52908a334d | 240 | |
falingtrea | 0:3f52908a334d | 241 | MPL3115A2::readRegister(P_MAX_MSB, datain, 3); |
falingtrea | 0:3f52908a334d | 242 | /* data is 20 bit signed/unsigned with 4 LSB = 0 Need to shift to 32 bits to preserve sign bit |
falingtrea | 0:3f52908a334d | 243 | * Altitude is 16 bit signed and pressure is 18 bit unsigned |
falingtrea | 0:3f52908a334d | 244 | */ |
falingtrea | 0:3f52908a334d | 245 | _data._maxbaro = ((datain[0] << 24) | (datain[1] << 16) | (datain[2] << 8)); |
falingtrea | 0:3f52908a334d | 246 | |
mfiore | 1:072fd24cc12b | 247 | if (clear_data) { |
mfiore | 1:072fd24cc12b | 248 | memset(datain, 0, 3); |
falingtrea | 0:3f52908a334d | 249 | MPL3115A2::writeRegister(P_MAX_MSB, datain, 3); |
falingtrea | 0:3f52908a334d | 250 | } |
falingtrea | 0:3f52908a334d | 251 | } |
falingtrea | 0:3f52908a334d | 252 | |
falingtrea | 0:3f52908a334d | 253 | return _data._maxbaro; |
falingtrea | 0:3f52908a334d | 254 | } |
falingtrea | 0:3f52908a334d | 255 | |
falingtrea | 0:3f52908a334d | 256 | int16_t MPL3115A2::getMinTemp(bool clear_data) |
falingtrea | 0:3f52908a334d | 257 | { |
mfiore | 1:072fd24cc12b | 258 | if (_polling_mode) { |
falingtrea | 0:3f52908a334d | 259 | char datain[2]; |
falingtrea | 0:3f52908a334d | 260 | MPL3115A2::readRegister(T_MIN_MSB, datain, 2); |
mfiore | 1:072fd24cc12b | 261 | /* data is 12 bit signed with 4 LSB = 0 Need to shift first to 16 bits to preserve sign bit then |
mfiore | 1:072fd24cc12b | 262 | * divide by 16 to remove LSBs |
mfiore | 1:072fd24cc12b | 263 | */ |
falingtrea | 0:3f52908a334d | 264 | _data._mintemp = ((datain[0] << 8) | datain[1] ); |
falingtrea | 0:3f52908a334d | 265 | _data._mintemp /= 16; |
falingtrea | 0:3f52908a334d | 266 | |
mfiore | 1:072fd24cc12b | 267 | if (clear_data) { |
mfiore | 1:072fd24cc12b | 268 | memset(datain, 0, 2); |
falingtrea | 0:3f52908a334d | 269 | MPL3115A2::writeRegister(T_MIN_MSB, datain, 2); |
falingtrea | 0:3f52908a334d | 270 | } |
falingtrea | 0:3f52908a334d | 271 | } |
falingtrea | 0:3f52908a334d | 272 | |
falingtrea | 0:3f52908a334d | 273 | return _data._mintemp; |
mfiore | 1:072fd24cc12b | 274 | } |
mfiore | 1:072fd24cc12b | 275 | |
falingtrea | 0:3f52908a334d | 276 | int16_t MPL3115A2::getMaxTemp(bool clear_data) |
falingtrea | 0:3f52908a334d | 277 | { |
mfiore | 1:072fd24cc12b | 278 | if (_polling_mode) { |
falingtrea | 0:3f52908a334d | 279 | char datain[2]; |
falingtrea | 0:3f52908a334d | 280 | MPL3115A2::readRegister(T_MIN_MSB, datain, 2); |
mfiore | 1:072fd24cc12b | 281 | /* data is 12 bit signed with 4 LSB = 0 Need to shift first to 16 bits to preserve sign bit then |
mfiore | 1:072fd24cc12b | 282 | * divide by 16 to remove LSBs |
mfiore | 1:072fd24cc12b | 283 | */ |
falingtrea | 0:3f52908a334d | 284 | _data._maxtemp = ((datain[0] << 8) | datain[1] ); |
falingtrea | 0:3f52908a334d | 285 | _data._maxtemp /= 16; |
falingtrea | 0:3f52908a334d | 286 | |
mfiore | 1:072fd24cc12b | 287 | if (clear_data) { |
mfiore | 1:072fd24cc12b | 288 | memset(datain, 0, 2); |
falingtrea | 0:3f52908a334d | 289 | MPL3115A2::writeRegister(T_MAX_MSB, datain, 2); |
falingtrea | 0:3f52908a334d | 290 | } |
mfiore | 1:072fd24cc12b | 291 | } |
falingtrea | 0:3f52908a334d | 292 | |
falingtrea | 0:3f52908a334d | 293 | return _data._maxtemp; |
mfiore | 1:072fd24cc12b | 294 | } |
falingtrea | 0:3f52908a334d | 295 | |
falingtrea | 0:3f52908a334d | 296 | MPL3115A2_DATA MPL3115A2::getAllData(bool clear_data) |
falingtrea | 0:3f52908a334d | 297 | { |
falingtrea | 0:3f52908a334d | 298 | |
mfiore | 1:072fd24cc12b | 299 | if (_polling_mode) { |
falingtrea | 0:3f52908a334d | 300 | char datain[10]; |
falingtrea | 0:3f52908a334d | 301 | MPL3115A2::readRegister(OUT_P_MSB, datain, 5); |
falingtrea | 0:3f52908a334d | 302 | /* data is 20 bit signed/unsigned with 4 LSB = 0 Need to shift to 32 bits to preserve sign bit |
falingtrea | 0:3f52908a334d | 303 | * Altitude is 16 bit signed and pressure is 18 bit unsigned |
falingtrea | 0:3f52908a334d | 304 | */ |
falingtrea | 0:3f52908a334d | 305 | _data._baro = ((datain[0] << 24) | (datain[1] << 16) | (datain[2]<<8)); |
falingtrea | 0:3f52908a334d | 306 | |
mfiore | 1:072fd24cc12b | 307 | /* data is 12 bit signed with 4 LSB = 0 Need to shift first to 16 bits to preserve sign bit then |
mfiore | 1:072fd24cc12b | 308 | * divide by 16 to remove LSBs |
mfiore | 1:072fd24cc12b | 309 | */ |
falingtrea | 0:3f52908a334d | 310 | _data._temp = ((datain[3] << 8) | datain[4]); |
falingtrea | 0:3f52908a334d | 311 | _data._temp /= 16; |
falingtrea | 0:3f52908a334d | 312 | |
mfiore | 1:072fd24cc12b | 313 | MPL3115A2::readRegister(P_MIN_MSB, datain, 10); |
mfiore | 1:072fd24cc12b | 314 | /* data is 20 bit signed/unsigned with 4 LSB = 0 Need to shift to 32 bits to preserve sign bit |
mfiore | 1:072fd24cc12b | 315 | * Altitude is 16 bit signed and pressure is 18 bit unsigned |
mfiore | 1:072fd24cc12b | 316 | * temperature data is 12 bit signed with 4 LSB = 0 Need to shift first to 16 bits to preserve sign bit then |
mfiore | 1:072fd24cc12b | 317 | * divide by 16 to remove LSBs |
mfiore | 1:072fd24cc12b | 318 | */ |
falingtrea | 0:3f52908a334d | 319 | _data._minbaro = ((datain[0] << 24) | (datain[1] << 16) | (datain[2] << 8)); |
falingtrea | 0:3f52908a334d | 320 | _data._mintemp = ((datain[3] << 8) | datain[4] ); |
falingtrea | 0:3f52908a334d | 321 | _data._mintemp /= 16; |
falingtrea | 0:3f52908a334d | 322 | _data._maxbaro = ((datain[5] << 24) | (datain[6] << 16) | (datain[7] << 8)); |
falingtrea | 0:3f52908a334d | 323 | _data._maxtemp = ((datain[8] << 8) | datain[9] ); |
falingtrea | 0:3f52908a334d | 324 | _data._maxtemp /= 16; |
falingtrea | 0:3f52908a334d | 325 | |
mfiore | 1:072fd24cc12b | 326 | if (clear_data) { |
mfiore | 1:072fd24cc12b | 327 | MPL3115A2::clearMinMaxRegs(); |
falingtrea | 0:3f52908a334d | 328 | } |
mfiore | 1:072fd24cc12b | 329 | } |
falingtrea | 0:3f52908a334d | 330 | |
falingtrea | 0:3f52908a334d | 331 | return _data; |
falingtrea | 0:3f52908a334d | 332 | } |
falingtrea | 0:3f52908a334d | 333 | |
falingtrea | 0:3f52908a334d | 334 | uint8_t MPL3115A2::writeRegister(uint8_t reg, char* data, uint8_t count) |
falingtrea | 0:3f52908a334d | 335 | { |
falingtrea | 0:3f52908a334d | 336 | char buf[11]; |
falingtrea | 0:3f52908a334d | 337 | uint8_t result = 0; |
falingtrea | 0:3f52908a334d | 338 | |
falingtrea | 0:3f52908a334d | 339 | buf[0] = reg; |
falingtrea | 0:3f52908a334d | 340 | memcpy(buf+1,data,count); |
falingtrea | 0:3f52908a334d | 341 | |
falingtrea | 0:3f52908a334d | 342 | result |= _i2c->write(_i2c_addr, buf, (count + 1)); |
mfiore | 1:072fd24cc12b | 343 | |
Evan Hosseini |
5:8be678fd9e55 | 344 | if (result != 0) { |
Evan Hosseini |
5:8be678fd9e55 | 345 | debug("MPL3115A2::writeRegister failed r-%d\n\r",result); |
falingtrea | 0:3f52908a334d | 346 | } |
mfiore | 1:072fd24cc12b | 347 | |
falingtrea | 0:3f52908a334d | 348 | return result; |
falingtrea | 0:3f52908a334d | 349 | } |
falingtrea | 0:3f52908a334d | 350 | |
falingtrea | 0:3f52908a334d | 351 | uint8_t MPL3115A2::readRegister(uint8_t reg, char* data, uint8_t count) |
falingtrea | 0:3f52908a334d | 352 | { |
falingtrea | 0:3f52908a334d | 353 | uint8_t result = 0; |
falingtrea | 0:3f52908a334d | 354 | char reg_out[1]; |
mfiore | 1:072fd24cc12b | 355 | |
falingtrea | 0:3f52908a334d | 356 | reg_out[0] = reg; |
Evan Hosseini |
5:8be678fd9e55 | 357 | _i2c->lock(); |
Evan Hosseini |
5:8be678fd9e55 | 358 | |
Evan Hosseini |
5:8be678fd9e55 | 359 | // MPL3115A2 expects a repeated start from the master |
mfiore | 1:072fd24cc12b | 360 | result |= _i2c->write(_i2c_addr,reg_out,1,true); |
falingtrea | 0:3f52908a334d | 361 | |
Evan Hosseini |
5:8be678fd9e55 | 362 | if (result != 0) { |
falingtrea | 0:3f52908a334d | 363 | debug("MPL3115A2::readRegister failed write\n\r"); |
Evan Hosseini |
5:8be678fd9e55 | 364 | goto exit; |
falingtrea | 0:3f52908a334d | 365 | } |
mfiore | 1:072fd24cc12b | 366 | |
mfiore | 1:072fd24cc12b | 367 | result |= _i2c->read(_i2c_addr,data,count,false); |
falingtrea | 0:3f52908a334d | 368 | |
Evan Hosseini |
5:8be678fd9e55 | 369 | if (result != 0) { |
falingtrea | 0:3f52908a334d | 370 | debug("MPL3115A2::readRegister failed read\n\r"); |
falingtrea | 0:3f52908a334d | 371 | } |
mfiore | 1:072fd24cc12b | 372 | |
Evan Hosseini |
5:8be678fd9e55 | 373 | exit: |
Evan Hosseini |
5:8be678fd9e55 | 374 | _i2c->unlock(); |
falingtrea | 0:3f52908a334d | 375 | return result; |
falingtrea | 0:3f52908a334d | 376 | } |