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:
Sun Sep 22 11:17:28 2013 +0000
Revision:
9:75a5960adf5c
Parent:
8:89ed6aeb5dbb
Child:
10:82ac06669316
Added function to read the maximum and minimum value captured. Some modification to the internal function.

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 9:75a5960adf5c 149 * Get the altimeter or pressure and temperature captured maximum value
clemente 9:75a5960adf5c 150 *
clemente 9:75a5960adf5c 151 * @param array of float f[2]
clemente 9:75a5960adf5c 152 * @returns 0 no data available, 1 for data available
clemente 9:75a5960adf5c 153 */
clemente 9:75a5960adf5c 154 void getAllMaximumData( float *f);
clemente 9:75a5960adf5c 155
clemente 9:75a5960adf5c 156 /**
clemente 9:75a5960adf5c 157 * Get the altimeter or pressure and temperature captured minimum 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 getAllMinimumData( float *f);
clemente 9:75a5960adf5c 163
clemente 9:75a5960adf5c 164 /**
clemente 5:9edec5ee8bf4 165 * Get the altimeter or pressure, and temperature values in raw mode
clemente 5:9edec5ee8bf4 166 *
clemente 5:9edec5ee8bf4 167 * @param array of unsigned char[5]
clemente 6:03c24251e500 168 * @returns 1 if data are available, 0 if not.
clemente 5:9edec5ee8bf4 169 */
clemente 5:9edec5ee8bf4 170 unsigned int getAllDataRaw( unsigned char *dt);
clemente 2:a2fcfb7ff611 171
clemente 2:a2fcfb7ff611 172 /**
clemente 2:a2fcfb7ff611 173 * Return if there are date available
clemente 2:a2fcfb7ff611 174 *
clemente 6:03c24251e500 175 * @return 0 for no data available, bit0 set for Temp data available, bit1 set for Press/Alti data available
clemente 6:03c24251e500 176 * bit2 set for both Temp and Press/Alti data available
clemente 2:a2fcfb7ff611 177 */
clemente 2:a2fcfb7ff611 178 unsigned int isDataAvailable( void);
clemente 2:a2fcfb7ff611 179
clemente 2:a2fcfb7ff611 180 /**
clemente 2:a2fcfb7ff611 181 * Set the oversampling rate value
clemente 2:a2fcfb7ff611 182 *
clemente 2:a2fcfb7ff611 183 * @param oversampling values. See MPL3115A2.h
clemente 2:a2fcfb7ff611 184 * @return none
clemente 2:a2fcfb7ff611 185 */
clemente 2:a2fcfb7ff611 186 void Oversample_Ratio( unsigned int ratio);
clemente 0:cfecfabc5e23 187
clemente 2:a2fcfb7ff611 188 /**
clemente 2:a2fcfb7ff611 189 * Configure the sensor to streaming data using Interrupt
clemente 2:a2fcfb7ff611 190 *
clemente 2:a2fcfb7ff611 191 * @param user functin callback, oversampling values. See MPL3115A2.h
clemente 2:a2fcfb7ff611 192 * @return none
clemente 2:a2fcfb7ff611 193 */
clemente 2:a2fcfb7ff611 194 void DataReady( void(*fptr)(void), unsigned char OS);
clemente 3:a2f1752add9a 195
clemente 3:a2f1752add9a 196 /**
clemente 4:fdf14a259af8 197 * Configure the sensor to generate an Interrupt crossing the center threshold
clemente 3:a2f1752add9a 198 *
clemente 4:fdf14a259af8 199 * @param user functin callback, level in meter
clemente 3:a2f1752add9a 200 * @return none
clemente 3:a2f1752add9a 201 */
clemente 3:a2f1752add9a 202 void AltitudeTrigger( void(*fptr)(void), unsigned short level);
clemente 2:a2fcfb7ff611 203
clemente 2:a2fcfb7ff611 204 /**
clemente 2:a2fcfb7ff611 205 * Soft Reset
clemente 2:a2fcfb7ff611 206 *
clemente 2:a2fcfb7ff611 207 * @param none
clemente 2:a2fcfb7ff611 208 * @return none
clemente 2:a2fcfb7ff611 209 */
clemente 2:a2fcfb7ff611 210 void Reset( void);
clemente 0:cfecfabc5e23 211
clemente 8:89ed6aeb5dbb 212 /**
clemente 8:89ed6aeb5dbb 213 * Configure the Pressure offset.
clemente 8:89ed6aeb5dbb 214 * Pressure user accessible offset trim value expressed as an 8-bit 2's complement number.
clemente 8:89ed6aeb5dbb 215 * The user offset registers may be adjusted to enhance accuracy and optimize the system performance.
clemente 8:89ed6aeb5dbb 216 * Range is from -512 to +508 Pa, 4 Pa per LSB.
clemente 8:89ed6aeb5dbb 217 * In RAW output mode no scaling or offsets will be applied in the digital domain
clemente 8:89ed6aeb5dbb 218 *
clemente 8:89ed6aeb5dbb 219 * @param offset
clemente 8:89ed6aeb5dbb 220 * @return none
clemente 8:89ed6aeb5dbb 221 */
clemente 8:89ed6aeb5dbb 222 void SetPressureOffset( char offset);
clemente 8:89ed6aeb5dbb 223
clemente 8:89ed6aeb5dbb 224 /**
clemente 8:89ed6aeb5dbb 225 * Configure the Temperature offset.
clemente 8:89ed6aeb5dbb 226 * Temperature user accessible offset trim value expressed as an 8-bit 2's complement number.
clemente 8:89ed6aeb5dbb 227 * The user offset registers may be adjusted to enhance accuracy and optimize the system performance.
clemente 8:89ed6aeb5dbb 228 * Range is from -8 to +7.9375°C 0.0625°C per LSB.
clemente 8:89ed6aeb5dbb 229 * In RAW output mode no scaling or offsets will be applied in the digital domain
clemente 8:89ed6aeb5dbb 230 *
clemente 8:89ed6aeb5dbb 231 * @param offset
clemente 8:89ed6aeb5dbb 232 * @return none
clemente 8:89ed6aeb5dbb 233 */
clemente 8:89ed6aeb5dbb 234 void SetTemperatureOffset( char offset);
clemente 8:89ed6aeb5dbb 235
clemente 8:89ed6aeb5dbb 236 /**
clemente 8:89ed6aeb5dbb 237 * Configure the Altitude offset.
clemente 8:89ed6aeb5dbb 238 * Altitude Data User Offset Register (OFF_H) is expressed as a 2’s complement number in meters.
clemente 8:89ed6aeb5dbb 239 * The user offset register provides user adjustment to the vertical height of the Altitude output.
clemente 8:89ed6aeb5dbb 240 * The range of values are from -128 to +127 meters.
clemente 8:89ed6aeb5dbb 241 * In RAW output mode no scaling or offsets will be applied in the digital domain
clemente 8:89ed6aeb5dbb 242 *
clemente 8:89ed6aeb5dbb 243 * @param offset
clemente 8:89ed6aeb5dbb 244 * @return none
clemente 8:89ed6aeb5dbb 245 */
clemente 8:89ed6aeb5dbb 246 void SetAltitudeOffset( char offset);
clemente 8:89ed6aeb5dbb 247
clemente 0:cfecfabc5e23 248 private:
clemente 2:a2fcfb7ff611 249 I2C m_i2c;
clemente 2:a2fcfb7ff611 250 int m_addr;
clemente 2:a2fcfb7ff611 251 unsigned char MPL3115A2_mode;
clemente 2:a2fcfb7ff611 252 unsigned char MPL3115A2_oversampling;
clemente 2:a2fcfb7ff611 253 void DataReady_IRQ( void);
clemente 3:a2f1752add9a 254 void AltitudeTrg_IRQ( void);
clemente 2:a2fcfb7ff611 255
clemente 2:a2fcfb7ff611 256 /** Set the device in active mode
clemente 2:a2fcfb7ff611 257 */
clemente 2:a2fcfb7ff611 258 void Active( void);
clemente 2:a2fcfb7ff611 259
clemente 2:a2fcfb7ff611 260 /** Set the device in standby mode
clemente 2:a2fcfb7ff611 261 */
clemente 2:a2fcfb7ff611 262 void Standby( void);
clemente 2:a2fcfb7ff611 263
clemente 9:75a5960adf5c 264 /** Get the altimiter value from the sensor.
clemente 9:75a5960adf5c 265 *
clemente 9:75a5960adf5c 266 * @param reg the register from which read the data.
clemente 9:75a5960adf5c 267 * Can be: REG_ALTIMETER_MSB for altimeter value
clemente 9:75a5960adf5c 268 * REG_ALTI_MIN_MSB for the minimum value captured
clemente 9:75a5960adf5c 269 * REG_ALTI_MAX_MSB for the maximum value captured
clemente 9:75a5960adf5c 270 */
clemente 9:75a5960adf5c 271 float getAltimeter( unsigned char reg);
clemente 9:75a5960adf5c 272
clemente 9:75a5960adf5c 273 /** Get the pressure value from the sensor.
clemente 9:75a5960adf5c 274 *
clemente 9:75a5960adf5c 275 * @param reg the register from which read the data.
clemente 9:75a5960adf5c 276 * Can be: REG_PRESSURE_MSB for altimeter value
clemente 9:75a5960adf5c 277 * REG_PRES_MIN_MSB for the minimum value captured
clemente 9:75a5960adf5c 278 * REG_PRES_MAX_MSB for the maximum value captured
clemente 9:75a5960adf5c 279 */
clemente 9:75a5960adf5c 280 float getPressure( unsigned char reg);
clemente 9:75a5960adf5c 281
clemente 9:75a5960adf5c 282 /** Get the altimiter value from the sensor.
clemente 9:75a5960adf5c 283 *
clemente 9:75a5960adf5c 284 * @param reg the register from which read the data.
clemente 9:75a5960adf5c 285 * Can be: REG_TEMP_MSB for altimeter value
clemente 9:75a5960adf5c 286 * REG_TEMP_MIN_MSB for the minimum value captured
clemente 9:75a5960adf5c 287 * REG_TEMP_MAX_MSB for the maximum value captured
clemente 9:75a5960adf5c 288 */
clemente 9:75a5960adf5c 289 float getTemperature( unsigned char reg);
clemente 9:75a5960adf5c 290
clemente 2:a2fcfb7ff611 291 void readRegs(int addr, uint8_t * data, int len);
clemente 2:a2fcfb7ff611 292 void writeRegs(uint8_t * data, int len);
clemente 0:cfecfabc5e23 293
clemente 0:cfecfabc5e23 294 };
clemente 0:cfecfabc5e23 295
clemente 0:cfecfabc5e23 296 #endif