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 07:04:14 2013 +0000
Revision:
8:89ed6aeb5dbb
Parent:
6:03c24251e500
Child:
9:75a5960adf5c
Added functions to set offset for temperature, altitude and pressure, as De Cremer Tim suggest.

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 5:9edec5ee8bf4 149 * Get the altimeter or pressure, and temperature values in raw mode
clemente 5:9edec5ee8bf4 150 *
clemente 5:9edec5ee8bf4 151 * @param array of unsigned char[5]
clemente 6:03c24251e500 152 * @returns 1 if data are available, 0 if not.
clemente 5:9edec5ee8bf4 153 */
clemente 5:9edec5ee8bf4 154 unsigned int getAllDataRaw( unsigned char *dt);
clemente 2:a2fcfb7ff611 155
clemente 2:a2fcfb7ff611 156 /**
clemente 2:a2fcfb7ff611 157 * Return if there are date available
clemente 2:a2fcfb7ff611 158 *
clemente 6:03c24251e500 159 * @return 0 for no data available, bit0 set for Temp data available, bit1 set for Press/Alti data available
clemente 6:03c24251e500 160 * bit2 set for both Temp and Press/Alti data available
clemente 2:a2fcfb7ff611 161 */
clemente 2:a2fcfb7ff611 162 unsigned int isDataAvailable( void);
clemente 2:a2fcfb7ff611 163
clemente 2:a2fcfb7ff611 164 /**
clemente 2:a2fcfb7ff611 165 * Set the oversampling rate value
clemente 2:a2fcfb7ff611 166 *
clemente 2:a2fcfb7ff611 167 * @param oversampling values. See MPL3115A2.h
clemente 2:a2fcfb7ff611 168 * @return none
clemente 2:a2fcfb7ff611 169 */
clemente 2:a2fcfb7ff611 170 void Oversample_Ratio( unsigned int ratio);
clemente 0:cfecfabc5e23 171
clemente 2:a2fcfb7ff611 172 /**
clemente 2:a2fcfb7ff611 173 * Configure the sensor to streaming data using Interrupt
clemente 2:a2fcfb7ff611 174 *
clemente 2:a2fcfb7ff611 175 * @param user functin callback, oversampling values. See MPL3115A2.h
clemente 2:a2fcfb7ff611 176 * @return none
clemente 2:a2fcfb7ff611 177 */
clemente 2:a2fcfb7ff611 178 void DataReady( void(*fptr)(void), unsigned char OS);
clemente 3:a2f1752add9a 179
clemente 3:a2f1752add9a 180 /**
clemente 4:fdf14a259af8 181 * Configure the sensor to generate an Interrupt crossing the center threshold
clemente 3:a2f1752add9a 182 *
clemente 4:fdf14a259af8 183 * @param user functin callback, level in meter
clemente 3:a2f1752add9a 184 * @return none
clemente 3:a2f1752add9a 185 */
clemente 3:a2f1752add9a 186 void AltitudeTrigger( void(*fptr)(void), unsigned short level);
clemente 2:a2fcfb7ff611 187
clemente 2:a2fcfb7ff611 188 /**
clemente 2:a2fcfb7ff611 189 * Soft Reset
clemente 2:a2fcfb7ff611 190 *
clemente 2:a2fcfb7ff611 191 * @param none
clemente 2:a2fcfb7ff611 192 * @return none
clemente 2:a2fcfb7ff611 193 */
clemente 2:a2fcfb7ff611 194 void Reset( void);
clemente 0:cfecfabc5e23 195
clemente 8:89ed6aeb5dbb 196 /**
clemente 8:89ed6aeb5dbb 197 * Configure the Pressure offset.
clemente 8:89ed6aeb5dbb 198 * Pressure user accessible offset trim value expressed as an 8-bit 2's complement number.
clemente 8:89ed6aeb5dbb 199 * The user offset registers may be adjusted to enhance accuracy and optimize the system performance.
clemente 8:89ed6aeb5dbb 200 * Range is from -512 to +508 Pa, 4 Pa per LSB.
clemente 8:89ed6aeb5dbb 201 * In RAW output mode no scaling or offsets will be applied in the digital domain
clemente 8:89ed6aeb5dbb 202 *
clemente 8:89ed6aeb5dbb 203 * @param offset
clemente 8:89ed6aeb5dbb 204 * @return none
clemente 8:89ed6aeb5dbb 205 */
clemente 8:89ed6aeb5dbb 206 void SetPressureOffset( char offset);
clemente 8:89ed6aeb5dbb 207
clemente 8:89ed6aeb5dbb 208 /**
clemente 8:89ed6aeb5dbb 209 * Configure the Temperature offset.
clemente 8:89ed6aeb5dbb 210 * Temperature user accessible offset trim value expressed as an 8-bit 2's complement number.
clemente 8:89ed6aeb5dbb 211 * The user offset registers may be adjusted to enhance accuracy and optimize the system performance.
clemente 8:89ed6aeb5dbb 212 * Range is from -8 to +7.9375°C 0.0625°C per LSB.
clemente 8:89ed6aeb5dbb 213 * In RAW output mode no scaling or offsets will be applied in the digital domain
clemente 8:89ed6aeb5dbb 214 *
clemente 8:89ed6aeb5dbb 215 * @param offset
clemente 8:89ed6aeb5dbb 216 * @return none
clemente 8:89ed6aeb5dbb 217 */
clemente 8:89ed6aeb5dbb 218 void SetTemperatureOffset( char offset);
clemente 8:89ed6aeb5dbb 219
clemente 8:89ed6aeb5dbb 220 /**
clemente 8:89ed6aeb5dbb 221 * Configure the Altitude offset.
clemente 8:89ed6aeb5dbb 222 * Altitude Data User Offset Register (OFF_H) is expressed as a 2’s complement number in meters.
clemente 8:89ed6aeb5dbb 223 * The user offset register provides user adjustment to the vertical height of the Altitude output.
clemente 8:89ed6aeb5dbb 224 * The range of values are from -128 to +127 meters.
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 SetAltitudeOffset( char offset);
clemente 8:89ed6aeb5dbb 231
clemente 0:cfecfabc5e23 232 private:
clemente 2:a2fcfb7ff611 233 I2C m_i2c;
clemente 2:a2fcfb7ff611 234 int m_addr;
clemente 2:a2fcfb7ff611 235 unsigned char MPL3115A2_mode;
clemente 2:a2fcfb7ff611 236 unsigned char MPL3115A2_oversampling;
clemente 2:a2fcfb7ff611 237 void DataReady_IRQ( void);
clemente 3:a2f1752add9a 238 void AltitudeTrg_IRQ( void);
clemente 2:a2fcfb7ff611 239
clemente 2:a2fcfb7ff611 240 /** Set the device in active mode
clemente 2:a2fcfb7ff611 241 */
clemente 2:a2fcfb7ff611 242 void Active( void);
clemente 2:a2fcfb7ff611 243
clemente 2:a2fcfb7ff611 244 /** Set the device in standby mode
clemente 2:a2fcfb7ff611 245 */
clemente 2:a2fcfb7ff611 246 void Standby( void);
clemente 2:a2fcfb7ff611 247
clemente 2:a2fcfb7ff611 248 void readRegs(int addr, uint8_t * data, int len);
clemente 2:a2fcfb7ff611 249 void writeRegs(uint8_t * data, int len);
clemente 0:cfecfabc5e23 250
clemente 0:cfecfabc5e23 251 };
clemente 0:cfecfabc5e23 252
clemente 0:cfecfabc5e23 253 #endif