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:
Tue Sep 24 20:22:25 2013 +0000
Revision:
10:82ac06669316
Parent:
9:75a5960adf5c
Child:
11:a5ce3ee460b6
Added a function to return delta values. Corrected but in case of negative numbers.

Who changed what in which revision?

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