Llibrary for the WiGo MPL3115A2, I2C Precision Altimeter sensor.

Dependents:   KL25Z_Batt_Test WIGO_MPL3115A2 Multi-Sensor SPACEmk2 ... more

30/05/2013 Added and tested the data acquisition using Interrupt. Added code for Altimeter trigger Interrupt but not yet tested.

Very basic library. Under development. Need to add in order: 1. IRQ configuration. 2. FIFO mode configuration.

Committer:
clemente
Date:
Fri Jul 10 14:58:19 2020 +0000
Revision:
11:a5ce3ee460b6
Parent:
10:82ac06669316
Added License

Who changed what in which revision?

UserRevisionLine numberNew contents of line
clemente 11:a5ce3ee460b6 1 /* MPL3115A2 I2C precision pressure sensor with altimetry Library
clemente 11:a5ce3ee460b6 2 *
clemente 11:a5ce3ee460b6 3 * @author: Clemente di Caprio
clemente 11:a5ce3ee460b6 4 * @date: September 24, 2013
clemente 11:a5ce3ee460b6 5 * @license: Apache License, Version 2.0
clemente 11:a5ce3ee460b6 6 *
clemente 11:a5ce3ee460b6 7 * Licensed under the Apache License, Version 2.0 (the "License");
clemente 11:a5ce3ee460b6 8 * you may not use this file except in compliance with the License.
clemente 11:a5ce3ee460b6 9 * You may obtain a copy of the License at
clemente 11:a5ce3ee460b6 10 *
clemente 11:a5ce3ee460b6 11 * http://www.apache.org/licenses/LICENSE-2.0
clemente 11:a5ce3ee460b6 12 *
clemente 11:a5ce3ee460b6 13 * Unless required by applicable law or agreed to in writing, software
clemente 11:a5ce3ee460b6 14 * distributed under the License is distributed on an "AS IS" BASIS,
clemente 11:a5ce3ee460b6 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
clemente 11:a5ce3ee460b6 16 * See the License for the specific language governing permissions and
clemente 11:a5ce3ee460b6 17 * limitations under the License.
clemente 11:a5ce3ee460b6 18 */
clemente 11:a5ce3ee460b6 19
clemente 0:cfecfabc5e23 20 #ifndef MPL3115A2_H
clemente 0:cfecfabc5e23 21 #define MPL3115A2_H
clemente 0:cfecfabc5e23 22
clemente 0:cfecfabc5e23 23 #include "mbed.h"
clemente 0:cfecfabc5e23 24
clemente 3:a2f1752add9a 25 // Oversampling value and minimum time between sample
clemente 3:a2f1752add9a 26 #define OVERSAMPLE_RATIO_1 0 // 6 ms
clemente 3:a2f1752add9a 27 #define OVERSAMPLE_RATIO_2 1 // 10 ms
clemente 3:a2f1752add9a 28 #define OVERSAMPLE_RATIO_4 2 // 18 ms
clemente 3:a2f1752add9a 29 #define OVERSAMPLE_RATIO_8 3 // 34 ms
clemente 3:a2f1752add9a 30 #define OVERSAMPLE_RATIO_16 4 // 66 ms
clemente 3:a2f1752add9a 31 #define OVERSAMPLE_RATIO_32 5 // 130 ms
clemente 3:a2f1752add9a 32 #define OVERSAMPLE_RATIO_64 6 // 258 ms
clemente 3:a2f1752add9a 33 #define OVERSAMPLE_RATIO_128 7 // 512 ms
clemente 0:cfecfabc5e23 34
clemente 0:cfecfabc5e23 35 /* Mode */
clemente 0:cfecfabc5e23 36 #define ALTIMETER_MODE 1
clemente 0:cfecfabc5e23 37 #define BAROMETRIC_MODE 2
clemente 0:cfecfabc5e23 38
clemente 0:cfecfabc5e23 39 /**
clemente 0:cfecfabc5e23 40 * MPL3115A2 Altimeter example
clemente 0:cfecfabc5e23 41 *
clemente 0:cfecfabc5e23 42 * @code
clemente 0:cfecfabc5e23 43 * #include "mbed.h"
clemente 0:cfecfabc5e23 44 * #include "MPL3115A2.h"
clemente 0:cfecfabc5e23 45 *
clemente 0:cfecfabc5e23 46 * #define MPL3115A2_I2C_ADDRESS (0x60<<1)
clemente 1:30a04f651efe 47 * MPL3115A2 wigo_sensor1(PTE0, PTE1, MPL3115A2_I2C_ADDRESS);
clemente 0:cfecfabc5e23 48 * Serial pc(USBTX, USBRX);
clemente 0:cfecfabc5e23 49 *
clemente 1:30a04f651efe 50 * // pos [0] = altitude or pressure value
clemente 1:30a04f651efe 51 * // pos [1] = temperature value
clemente 1:30a04f651efe 52 * float sensor_data[2];
clemente 1:30a04f651efe 53 *
clemente 0:cfecfabc5e23 54 * int main(void) {
clemente 0:cfecfabc5e23 55 *
clemente 0:cfecfabc5e23 56 * pc.baud( 230400);
clemente 0:cfecfabc5e23 57 * pc.printf("MPL3115A2 Altimeter mode. [%d]\r\n", wigo_sensor1.getDeviceID());
clemente 0:cfecfabc5e23 58 *
clemente 0:cfecfabc5e23 59 * wigo_sensor1.Oversample_Ratio( OVERSAMPLE_RATIO_32);
clemente 0:cfecfabc5e23 60 *
clemente 0:cfecfabc5e23 61 * while(1) {
clemente 0:cfecfabc5e23 62 * //
clemente 0:cfecfabc5e23 63 * if ( wigo_sensor1.isDataAvailable()) {
clemente 0:cfecfabc5e23 64 * wigo_sensor1.getAllData( &sensor_data[0]);
clemente 1:30a04f651efe 65 * pc.printf("\tAltitude: %f\tTemperature: %f\r\n", sensor_data[0], sensor_data[1]);
clemente 0:cfecfabc5e23 66 * }
clemente 0:cfecfabc5e23 67 * //
clemente 0:cfecfabc5e23 68 * wait( 0.001);
clemente 0:cfecfabc5e23 69 * }
clemente 0:cfecfabc5e23 70 *
clemente 0:cfecfabc5e23 71 * }
clemente 0:cfecfabc5e23 72 * @endcode
clemente 0:cfecfabc5e23 73 */
clemente 0:cfecfabc5e23 74 class MPL3115A2
clemente 0:cfecfabc5e23 75 {
clemente 0:cfecfabc5e23 76 public:
clemente 2:a2fcfb7ff611 77 /**
clemente 2:a2fcfb7ff611 78 * MPL3115A2 constructor
clemente 2:a2fcfb7ff611 79 *
clemente 2:a2fcfb7ff611 80 * @param sda SDA pin
clemente 2:a2fcfb7ff611 81 * @param sdl SCL pin
clemente 2:a2fcfb7ff611 82 * @param addr addr of the I2C peripheral
clemente 2:a2fcfb7ff611 83 */
clemente 2:a2fcfb7ff611 84 MPL3115A2(PinName sda, PinName scl, int addr);
clemente 2:a2fcfb7ff611 85
clemente 2:a2fcfb7ff611 86 /**
clemente 2:a2fcfb7ff611 87 * Get the value of the WHO_AM_I register
clemente 2:a2fcfb7ff611 88 *
clemente 2:a2fcfb7ff611 89 * @returns DEVICE_ID value == 0xC4
clemente 2:a2fcfb7ff611 90 */
clemente 2:a2fcfb7ff611 91 uint8_t getDeviceID();
clemente 2:a2fcfb7ff611 92
clemente 2:a2fcfb7ff611 93 /**
clemente 2:a2fcfb7ff611 94 * Return the STATUS register value
clemente 2:a2fcfb7ff611 95 *
clemente 2:a2fcfb7ff611 96 * @returns STATUS register value
clemente 2:a2fcfb7ff611 97 */
clemente 2:a2fcfb7ff611 98 unsigned char getStatus( void);
clemente 2:a2fcfb7ff611 99
clemente 2:a2fcfb7ff611 100 /**
clemente 2:a2fcfb7ff611 101 * Get the altimeter value
clemente 2:a2fcfb7ff611 102 *
clemente 2:a2fcfb7ff611 103 * @returns altimeter value as float
clemente 2:a2fcfb7ff611 104 */
clemente 2:a2fcfb7ff611 105 float getAltimeter( void);
clemente 5:9edec5ee8bf4 106
clemente 5:9edec5ee8bf4 107 /**
clemente 5:9edec5ee8bf4 108 * Get the altimeter value in raw mode
clemente 5:9edec5ee8bf4 109 *
clemente 5:9edec5ee8bf4 110 * @param dt pointer to unsigned char array
clemente 6:03c24251e500 111 * @returns 1 if data are available, 0 if not.
clemente 5:9edec5ee8bf4 112 */
clemente 5:9edec5ee8bf4 113 unsigned int getAltimeterRaw( unsigned char *dt);
clemente 2:a2fcfb7ff611 114
clemente 2:a2fcfb7ff611 115 /**
clemente 2:a2fcfb7ff611 116 * Get the pressure value
clemente 2:a2fcfb7ff611 117 *
clemente 2:a2fcfb7ff611 118 * @returns pressure value as float
clemente 2:a2fcfb7ff611 119 */
clemente 2:a2fcfb7ff611 120 float getPressure( void);
clemente 5:9edec5ee8bf4 121
clemente 5:9edec5ee8bf4 122 /**
clemente 5:9edec5ee8bf4 123 * Get the pressure value in raw mode
clemente 5:9edec5ee8bf4 124 *
clemente 5:9edec5ee8bf4 125 * @param dt pointer to unsigned char array
clemente 6:03c24251e500 126 * @returns 1 if data are available, 0 if not.
clemente 5:9edec5ee8bf4 127 */
clemente 5:9edec5ee8bf4 128 unsigned int getPressureRaw( unsigned char *dt);
clemente 2:a2fcfb7ff611 129
clemente 2:a2fcfb7ff611 130 /**
clemente 2:a2fcfb7ff611 131 * Get the temperature value
clemente 2:a2fcfb7ff611 132 *
clemente 2:a2fcfb7ff611 133 * @returns temperature value as float
clemente 2:a2fcfb7ff611 134 */
clemente 2:a2fcfb7ff611 135 float getTemperature( void);
clemente 5:9edec5ee8bf4 136
clemente 5:9edec5ee8bf4 137 /**
clemente 5:9edec5ee8bf4 138 * Get the temperature value in raw mode
clemente 5:9edec5ee8bf4 139 *
clemente 5:9edec5ee8bf4 140 * @param dt pointer to unsigned char array
clemente 6:03c24251e500 141 * @returns 1 if data are available, 0 if not.
clemente 5:9edec5ee8bf4 142 */
clemente 5:9edec5ee8bf4 143 unsigned int getTemperatureRaw( unsigned char *dt);
clemente 2:a2fcfb7ff611 144
clemente 2:a2fcfb7ff611 145 /**
clemente 2:a2fcfb7ff611 146 * Set the Altimeter Mode
clemente 2:a2fcfb7ff611 147 *
clemente 2:a2fcfb7ff611 148 * @returns none
clemente 2:a2fcfb7ff611 149 */
clemente 2:a2fcfb7ff611 150 void Altimeter_Mode( void);
clemente 2:a2fcfb7ff611 151
clemente 2:a2fcfb7ff611 152 /**
clemente 2:a2fcfb7ff611 153 * Set the Barometric Mode
clemente 2:a2fcfb7ff611 154 *
clemente 2:a2fcfb7ff611 155 * @returns none
clemente 2:a2fcfb7ff611 156 */
clemente 2:a2fcfb7ff611 157 void Barometric_Mode( void);
clemente 2:a2fcfb7ff611 158
clemente 2:a2fcfb7ff611 159 /**
clemente 5:9edec5ee8bf4 160 * Get the altimeter or pressure and temperature values
clemente 2:a2fcfb7ff611 161 *
clemente 5:9edec5ee8bf4 162 * @param array of float f[2]
clemente 6:03c24251e500 163 * @returns 0 no data available, 1 for data available
clemente 2:a2fcfb7ff611 164 */
clemente 6:03c24251e500 165 unsigned int getAllData( float *f);
clemente 5:9edec5ee8bf4 166
clemente 5:9edec5ee8bf4 167 /**
clemente 10:82ac06669316 168 * Get the altimeter or pressure and temperature values and the delta values
clemente 10:82ac06669316 169 *
clemente 10:82ac06669316 170 * @param array of float f[2], array of float d[2]
clemente 10:82ac06669316 171 * @returns 0 no data available, 1 for data available
clemente 10:82ac06669316 172 */
clemente 10:82ac06669316 173 unsigned int getAllData( float *f, float *d);
clemente 10:82ac06669316 174
clemente 10:82ac06669316 175 /**
clemente 9:75a5960adf5c 176 * Get the altimeter or pressure and temperature captured maximum value
clemente 9:75a5960adf5c 177 *
clemente 9:75a5960adf5c 178 * @param array of float f[2]
clemente 9:75a5960adf5c 179 * @returns 0 no data available, 1 for data available
clemente 9:75a5960adf5c 180 */
clemente 9:75a5960adf5c 181 void getAllMaximumData( float *f);
clemente 9:75a5960adf5c 182
clemente 9:75a5960adf5c 183 /**
clemente 9:75a5960adf5c 184 * Get the altimeter or pressure and temperature captured minimum value
clemente 9:75a5960adf5c 185 *
clemente 9:75a5960adf5c 186 * @param array of float f[2]
clemente 9:75a5960adf5c 187 * @returns 0 no data available, 1 for data available
clemente 9:75a5960adf5c 188 */
clemente 9:75a5960adf5c 189 void getAllMinimumData( float *f);
clemente 9:75a5960adf5c 190
clemente 9:75a5960adf5c 191 /**
clemente 5:9edec5ee8bf4 192 * Get the altimeter or pressure, and temperature values in raw mode
clemente 5:9edec5ee8bf4 193 *
clemente 5:9edec5ee8bf4 194 * @param array of unsigned char[5]
clemente 6:03c24251e500 195 * @returns 1 if data are available, 0 if not.
clemente 5:9edec5ee8bf4 196 */
clemente 5:9edec5ee8bf4 197 unsigned int getAllDataRaw( unsigned char *dt);
clemente 2:a2fcfb7ff611 198
clemente 2:a2fcfb7ff611 199 /**
clemente 2:a2fcfb7ff611 200 * Return if there are date available
clemente 2:a2fcfb7ff611 201 *
clemente 6:03c24251e500 202 * @return 0 for no data available, bit0 set for Temp data available, bit1 set for Press/Alti data available
clemente 6:03c24251e500 203 * bit2 set for both Temp and Press/Alti data available
clemente 2:a2fcfb7ff611 204 */
clemente 2:a2fcfb7ff611 205 unsigned int isDataAvailable( void);
clemente 2:a2fcfb7ff611 206
clemente 2:a2fcfb7ff611 207 /**
clemente 2:a2fcfb7ff611 208 * Set the oversampling rate value
clemente 2:a2fcfb7ff611 209 *
clemente 2:a2fcfb7ff611 210 * @param oversampling values. See MPL3115A2.h
clemente 2:a2fcfb7ff611 211 * @return none
clemente 2:a2fcfb7ff611 212 */
clemente 2:a2fcfb7ff611 213 void Oversample_Ratio( unsigned int ratio);
clemente 0:cfecfabc5e23 214
clemente 2:a2fcfb7ff611 215 /**
clemente 2:a2fcfb7ff611 216 * Configure the sensor to streaming data using Interrupt
clemente 2:a2fcfb7ff611 217 *
clemente 2:a2fcfb7ff611 218 * @param user functin callback, oversampling values. See MPL3115A2.h
clemente 2:a2fcfb7ff611 219 * @return none
clemente 2:a2fcfb7ff611 220 */
clemente 2:a2fcfb7ff611 221 void DataReady( void(*fptr)(void), unsigned char OS);
clemente 3:a2f1752add9a 222
clemente 3:a2f1752add9a 223 /**
clemente 4:fdf14a259af8 224 * Configure the sensor to generate an Interrupt crossing the center threshold
clemente 3:a2f1752add9a 225 *
clemente 4:fdf14a259af8 226 * @param user functin callback, level in meter
clemente 3:a2f1752add9a 227 * @return none
clemente 3:a2f1752add9a 228 */
clemente 3:a2f1752add9a 229 void AltitudeTrigger( void(*fptr)(void), unsigned short level);
clemente 2:a2fcfb7ff611 230
clemente 2:a2fcfb7ff611 231 /**
clemente 2:a2fcfb7ff611 232 * Soft Reset
clemente 2:a2fcfb7ff611 233 *
clemente 2:a2fcfb7ff611 234 * @param none
clemente 2:a2fcfb7ff611 235 * @return none
clemente 2:a2fcfb7ff611 236 */
clemente 2:a2fcfb7ff611 237 void Reset( void);
clemente 0:cfecfabc5e23 238
clemente 8:89ed6aeb5dbb 239 /**
clemente 8:89ed6aeb5dbb 240 * Configure the Pressure offset.
clemente 8:89ed6aeb5dbb 241 * Pressure user accessible offset trim value expressed as an 8-bit 2's complement number.
clemente 8:89ed6aeb5dbb 242 * The user offset registers may be adjusted to enhance accuracy and optimize the system performance.
clemente 8:89ed6aeb5dbb 243 * Range is from -512 to +508 Pa, 4 Pa per LSB.
clemente 8:89ed6aeb5dbb 244 * In RAW output mode no scaling or offsets will be applied in the digital domain
clemente 8:89ed6aeb5dbb 245 *
clemente 8:89ed6aeb5dbb 246 * @param offset
clemente 8:89ed6aeb5dbb 247 * @return none
clemente 8:89ed6aeb5dbb 248 */
clemente 8:89ed6aeb5dbb 249 void SetPressureOffset( char offset);
clemente 8:89ed6aeb5dbb 250
clemente 8:89ed6aeb5dbb 251 /**
clemente 8:89ed6aeb5dbb 252 * Configure the Temperature offset.
clemente 8:89ed6aeb5dbb 253 * Temperature user accessible offset trim value expressed as an 8-bit 2's complement number.
clemente 8:89ed6aeb5dbb 254 * The user offset registers may be adjusted to enhance accuracy and optimize the system performance.
clemente 8:89ed6aeb5dbb 255 * Range is from -8 to +7.9375°C 0.0625°C per LSB.
clemente 8:89ed6aeb5dbb 256 * In RAW output mode no scaling or offsets will be applied in the digital domain
clemente 8:89ed6aeb5dbb 257 *
clemente 8:89ed6aeb5dbb 258 * @param offset
clemente 8:89ed6aeb5dbb 259 * @return none
clemente 8:89ed6aeb5dbb 260 */
clemente 8:89ed6aeb5dbb 261 void SetTemperatureOffset( char offset);
clemente 8:89ed6aeb5dbb 262
clemente 8:89ed6aeb5dbb 263 /**
clemente 8:89ed6aeb5dbb 264 * Configure the Altitude offset.
clemente 8:89ed6aeb5dbb 265 * Altitude Data User Offset Register (OFF_H) is expressed as a 2’s complement number in meters.
clemente 8:89ed6aeb5dbb 266 * The user offset register provides user adjustment to the vertical height of the Altitude output.
clemente 8:89ed6aeb5dbb 267 * The range of values are from -128 to +127 meters.
clemente 8:89ed6aeb5dbb 268 * In RAW output mode no scaling or offsets will be applied in the digital domain
clemente 8:89ed6aeb5dbb 269 *
clemente 8:89ed6aeb5dbb 270 * @param offset
clemente 8:89ed6aeb5dbb 271 * @return none
clemente 8:89ed6aeb5dbb 272 */
clemente 8:89ed6aeb5dbb 273 void SetAltitudeOffset( char offset);
clemente 8:89ed6aeb5dbb 274
clemente 0:cfecfabc5e23 275 private:
clemente 2:a2fcfb7ff611 276 I2C m_i2c;
clemente 2:a2fcfb7ff611 277 int m_addr;
clemente 2:a2fcfb7ff611 278 unsigned char MPL3115A2_mode;
clemente 2:a2fcfb7ff611 279 unsigned char MPL3115A2_oversampling;
clemente 2:a2fcfb7ff611 280 void DataReady_IRQ( void);
clemente 3:a2f1752add9a 281 void AltitudeTrg_IRQ( void);
clemente 2:a2fcfb7ff611 282
clemente 2:a2fcfb7ff611 283 /** Set the device in active mode
clemente 2:a2fcfb7ff611 284 */
clemente 2:a2fcfb7ff611 285 void Active( void);
clemente 2:a2fcfb7ff611 286
clemente 2:a2fcfb7ff611 287 /** Set the device in standby mode
clemente 2:a2fcfb7ff611 288 */
clemente 2:a2fcfb7ff611 289 void Standby( void);
clemente 2:a2fcfb7ff611 290
clemente 9:75a5960adf5c 291 /** Get the altimiter value from the sensor.
clemente 9:75a5960adf5c 292 *
clemente 9:75a5960adf5c 293 * @param reg the register from which read the data.
clemente 9:75a5960adf5c 294 * Can be: REG_ALTIMETER_MSB for altimeter value
clemente 9:75a5960adf5c 295 * REG_ALTI_MIN_MSB for the minimum value captured
clemente 9:75a5960adf5c 296 * REG_ALTI_MAX_MSB for the maximum value captured
clemente 9:75a5960adf5c 297 */
clemente 9:75a5960adf5c 298 float getAltimeter( unsigned char reg);
clemente 9:75a5960adf5c 299
clemente 9:75a5960adf5c 300 /** Get the pressure value from the sensor.
clemente 9:75a5960adf5c 301 *
clemente 9:75a5960adf5c 302 * @param reg the register from which read the data.
clemente 9:75a5960adf5c 303 * Can be: REG_PRESSURE_MSB for altimeter value
clemente 9:75a5960adf5c 304 * REG_PRES_MIN_MSB for the minimum value captured
clemente 9:75a5960adf5c 305 * REG_PRES_MAX_MSB for the maximum value captured
clemente 9:75a5960adf5c 306 */
clemente 9:75a5960adf5c 307 float getPressure( unsigned char reg);
clemente 9:75a5960adf5c 308
clemente 9:75a5960adf5c 309 /** Get the altimiter value from the sensor.
clemente 9:75a5960adf5c 310 *
clemente 9:75a5960adf5c 311 * @param reg the register from which read the data.
clemente 9:75a5960adf5c 312 * Can be: REG_TEMP_MSB for altimeter value
clemente 9:75a5960adf5c 313 * REG_TEMP_MIN_MSB for the minimum value captured
clemente 9:75a5960adf5c 314 * REG_TEMP_MAX_MSB for the maximum value captured
clemente 9:75a5960adf5c 315 */
clemente 9:75a5960adf5c 316 float getTemperature( unsigned char reg);
clemente 9:75a5960adf5c 317
clemente 2:a2fcfb7ff611 318 void readRegs(int addr, uint8_t * data, int len);
clemente 2:a2fcfb7ff611 319 void writeRegs(uint8_t * data, int len);
clemente 0:cfecfabc5e23 320
clemente 0:cfecfabc5e23 321 };
clemente 0:cfecfabc5e23 322
clemente 0:cfecfabc5e23 323 #endif