Llibrary for the WiGo MPL3115A2, I2C Precision Altimeter sensor. This is a temp fork
Dependents: sensor AerCloud_MutliTech_Socket_Modem_Example Freescale_Multi-Sensor_Shield 2lemetry_Sensor_Example ... more
Fork of MPL3115A2 by
Revision 2:a2fcfb7ff611, committed 2013-05-29
- Comitter:
- clemente
- Date:
- Wed May 29 05:57:15 2013 +0000
- Parent:
- 1:30a04f651efe
- Child:
- 3:a2f1752add9a
- Commit message:
- Streaming data using Interrupt. Not tested.
Changed in this revision
MPL3115A2.cpp | Show annotated file Show diff for this revision Revisions of this file |
MPL3115A2.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/MPL3115A2.cpp Thu May 23 12:42:45 2013 +0000 +++ b/MPL3115A2.cpp Wed May 29 05:57:15 2013 +0000 @@ -1,8 +1,10 @@ #include "MPL3115A2.h" -#define REG_WHO_AM_I 0x0C // 0xC4 by default +#define REG_WHO_AM_I 0x0C // return 0xC4 by default #define REG_STATUS 0x00 #define REG_CTRL_REG_1 0x26 +#define REG_CTRL_REG_4 0x29 +#define REG_CTRL_REG_5 0x2A #define REG_PRESSURE_MSB 0x01 // 3 byte pressure data #define REG_ALTIMETER_MSB 0x01 // 3 byte altimeter data #define REG_TEMP_MSB 0x04 // 2 byte temperature data @@ -10,26 +12,94 @@ #define UINT14_MAX 16383 +void (*user2_fptr)(void); // Pointers to user function called after +void (*user1_fptr)(void); // IRQ assertion. + +// +InterruptIn MPL3115A2_Int1( PTD4); // INT1 +InterruptIn MPL3115A2_Int2( PTA12); // INT2 + MPL3115A2::MPL3115A2(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) { MPL3115A2_mode = BAROMETRIC_MODE; MPL3115A2_oversampling = OVERSAMPLE_RATIO_1; } +void MPL3115A2::Reset( void) +{ + unsigned char t; + + // soft reset... + readRegs( REG_CTRL_REG_1, &t, 1); + unsigned char data[2] = { REG_CTRL_REG_1, t|0x04}; + writeRegs(data, 2); + wait( 0.1); + +} + +void MPL3115A2::DataReady( void(*fptr)(void), unsigned char OS) +{ + unsigned char dt[5]; + unsigned char data[2]; + + Reset(); + + /* + ** Read contents of CTRL_REG_1 + ** Clear SBYB mask while holding all other values of CTRL_REG_1. + ** To put part into Standby mode + */ + Standby(); + + /* + ** Clear all interrupts by reading the output registers. + ** Activate sensor in Altimeter mode. + */ + readRegs( REG_ALTIMETER_MSB, &dt[0], 5); + getStatus(); + // Enable Interrupt fot data ready + data[0] = REG_CTRL_REG_4; + data[1] = 0x80; + writeRegs(data, 2); + // Configure Interrupt route to INT2 + data[0] = REG_CTRL_REG_5; + data[1] = 0x00; + writeRegs(data, 2); + + /* + ** Write a 1 to the ALT and SBYB bits to go into Active Altimeter mode + */ + // Configure the OverSampling rate, Altimeter mode and set the sensor Active + data[0] = REG_CTRL_REG_1; + data[1] = 0x81 | (OS<<3); + writeRegs(data, 2); + + user2_fptr = fptr; + MPL3115A2_Int2.fall( this, &MPL3115A2::DataReady_IRQ); + +} + +void MPL3115A2::DataReady_IRQ( void) +{ + // Clear the IRQ flag + getStatus(); + // Run the user supplied function + user2_fptr(); +} + void MPL3115A2::Barometric_Mode( void) { unsigned char t; + unsigned char data[2]; Standby(); - readRegs( REG_CTRL_REG_1, &t, 1); // soft reset... - unsigned char data[2] = { REG_CTRL_REG_1, t|0x04}; - writeRegs(data, 2); - wait( 0.2); - + Reset(); + Standby(); readRegs( REG_CTRL_REG_1, &t, 1); + // Set the Barometric mode data[0] = REG_CTRL_REG_1; data[1] = t&0x7F; writeRegs(data, 2); @@ -48,14 +118,12 @@ void MPL3115A2::Altimeter_Mode( void) { unsigned char t; - + unsigned char data[2]; + Standby(); - readRegs( REG_CTRL_REG_1, &t, 1); // soft reset... - unsigned char data[2] = { REG_CTRL_REG_1, t|0x04}; - writeRegs(data, 2); - wait( 0.2); + Reset(); Standby(); readRegs( REG_CTRL_REG_1, &t, 1);
--- a/MPL3115A2.h Thu May 23 12:42:45 2013 +0000 +++ b/MPL3115A2.h Wed May 29 05:57:15 2013 +0000 @@ -55,101 +55,120 @@ class MPL3115A2 { public: -/** -* MPL3115A2 constructor -* -* @param sda SDA pin -* @param sdl SCL pin -* @param addr addr of the I2C peripheral -*/ -MPL3115A2(PinName sda, PinName scl, int addr); - -/** -* Get the value of the WHO_AM_I register -* -* @returns DEVICE_ID value == 0xC4 -*/ -uint8_t getDeviceID(); - -/** -* Return the STATUS register value -* -* @returns STATUS register value -*/ -unsigned char getStatus( void); - -/** -* Get the altimeter value -* -* @returns altimeter value as float -*/ -float getAltimeter( void); - -/** -* Get the pressure value -* -* @returns pressure value as float -*/ -float getPressure( void); + /** + * MPL3115A2 constructor + * + * @param sda SDA pin + * @param sdl SCL pin + * @param addr addr of the I2C peripheral + */ + MPL3115A2(PinName sda, PinName scl, int addr); + + /** + * Get the value of the WHO_AM_I register + * + * @returns DEVICE_ID value == 0xC4 + */ + uint8_t getDeviceID(); + + /** + * Return the STATUS register value + * + * @returns STATUS register value + */ + unsigned char getStatus( void); + + /** + * Get the altimeter value + * + * @returns altimeter value as float + */ + float getAltimeter( void); + + /** + * Get the pressure value + * + * @returns pressure value as float + */ + float getPressure( void); + + /** + * Get the temperature value + * + * @returns temperature value as float + */ + float getTemperature( void); + + /** + * Set the Altimeter Mode + * + * @returns none + */ + void Altimeter_Mode( void); + + /** + * Set the Barometric Mode + * + * @returns none + */ + void Barometric_Mode( void); + + /** + * Get the altimeter, pressure and temperature values + * + * @param array of float f[3] + * @returns none + */ + void getAllData( float *f); + + /** + * Return if there are date available + * + * @return 1 for data available, 0 for no data available + */ + unsigned int isDataAvailable( void); + + /** + * Set the oversampling rate value + * + * @param oversampling values. See MPL3115A2.h + * @return none + */ + void Oversample_Ratio( unsigned int ratio); -/** -* Get the temperature value -* -* @returns temperature value as float -*/ -float getTemperature( void); - -/** -* Set the Altimeter Mode -* -* @returns none -*/ -void Altimeter_Mode( void); - -/** -* Set the Barometric Mode -* -* @returns none -*/ -void Barometric_Mode( void); - -/** -* Get the altimeter, pressure and temperature values -* -* @param array of float f[3] -* @returns none -*/ -void getAllData( float *f); - -/** Return is there are date available -* -* @return 1 for data available, 0 for no data available -*/ -unsigned int isDataAvailable( void); - -/** Set the oversampling rate value -* -* @param oversampling values. See MPL3115A2.h -* @return none -*/ -void Oversample_Ratio( unsigned int ratio); + /** + * Configure the sensor to streaming data using Interrupt + * + * @param user functin callback, oversampling values. See MPL3115A2.h + * @return none + */ + void DataReady( void(*fptr)(void), unsigned char OS); + + /** + * Soft Reset + * + * @param none + * @return none + */ + void Reset( void); private: - I2C m_i2c; - int m_addr; - unsigned char MPL3115A2_mode; - unsigned char MPL3115A2_oversampling; - - /** Set the device in active mode - */ - void Active( void); - - /** Set the device in standby mode - */ - void Standby( void); - - void readRegs(int addr, uint8_t * data, int len); - void writeRegs(uint8_t * data, int len); + I2C m_i2c; + int m_addr; + unsigned char MPL3115A2_mode; + unsigned char MPL3115A2_oversampling; + void DataReady_IRQ( void); + + /** Set the device in active mode + */ + void Active( void); + + /** Set the device in standby mode + */ + void Standby( void); + + void readRegs(int addr, uint8_t * data, int len); + void writeRegs(uint8_t * data, int len); };