Class module for MPL3115A2 I2C Barometric Sensor
Dependents: mDotEVBM2X MTDOT-EVBDemo-DRH MTDOT-BOX-EVB-Factory-Firmware-LIB-108 MTDOT-UDKDemo_Senet ... more
Revision 0:3f52908a334d, committed 2015-07-06
- Comitter:
- falingtrea
- Date:
- Mon Jul 06 19:33:37 2015 +0000
- Child:
- 1:072fd24cc12b
- Commit message:
- Initial release
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 |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MPL3115A2.cpp Mon Jul 06 19:33:37 2015 +0000 @@ -0,0 +1,403 @@ +/** + * @file MPL3115A2.cpp + * @brief Device driver - MPL3115A2 barometric pressure IC w/RTOS support + * @author Tim Barr + * @version 1.0 + * @see http://cache.freescale.com/files/sensors/doc/data_sheet/MPL3115A2.pdf + * + * Copyright (c) 2015 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "MPL3115A2.h" +#include "mbed_debug.h" +#include "rtos.h" + +MPL3115A2::MPL3115A2( I2C &i2c, InterruptIn* int1, InterruptIn* int2) +{ + _i2c = &i2c; + _int1 = int1; + _int2 = int2; + + MPL3115A2::init(); + + return; +} + +bool MPL3115A2::testWhoAmI(void) +{ + char reg_val[1]; + + reg_val[0] = 0x00; + MPL3115A2::readRegister(WHO_AM_I,reg_val); + + return (reg_val[0] == I_AM_MPL3115A2); + +} + +uint8_t MPL3115A2::init(void) +{ + uint8_t result = 0; + uint8_t i = 0; + char reg_val[1]; + + _i2c->frequency(400000); + + // Reset all registers to POR values + reg_val[0] = 0x04; + result = MPL3115A2::writeRegister(CTRL_REG1, reg_val); + if (result == 0){ + do{ + // wait for the reset bit to clear. readRegister may error out so we re-try 10 times + osDelay(200); + reg_val[0] = 0x40; + result = MPL3115A2::readRegister(CTRL_REG1,reg_val); + reg_val[0] = reg_val[0] & 0x04; + i++; + }while(((reg_val[0] != 0) || (result != 0)) && (i<=10)); + } + + if ((result == 0) && (MPL3115A2::testWhoAmI() == true)){ + + if ((_int1 == NULL) && (_int2 == NULL)) + { + _polling_mode = true; + reg_val[0] = 0x07; + result |= MPL3115A2::writeRegister(PT_DATA_CFG,reg_val); + } + else _polling_mode = false; + } + else { + debug ("Device not supported by this library!\n\r"); + result = 1; + } + + if(result != 0) + { + debug("MPL3115A2:init failed\n\r"); + } + + return result; +} + +uint8_t MPL3115A2::setParameters(OUTPUT_MODE out_mode, DATA_MODE data_mode, OVERSAMPLE_RATIO os_ratio, + ACQUISITION_TIMER measure_time) +{ + uint8_t result = 0; + char datain[4]; + char dataout[4]; + + result |= MPL3115A2::readRegister(SYSMOD, datain); // Make sure MPL3115A2 is in Stand-By mode + if ((datain[0] & 0x01) != 0 ){ + debug ("MPL3115A2 not in STAND BY mode\n\f"); + debug("MPL3115A2:setParameters failed\n\r"); + result = 1; + return result; + } + + result |= MPL3115A2::readRegister(CTRL_REG1, datain, 2); + dataout[0] = (datain[0] & 0x07) | os_ratio | out_mode | data_mode; + dataout[1] = (datain[1] & 0xF0) | measure_time; + result |= MPL3115A2::writeRegister(CTRL_REG1, dataout, 2); + + if(result != 0) + { + debug("MPL3115A2:setParameters failed\n\r"); + } + + return result; +} + +uint8_t MPL3115A2::enableFIFO(void) +{ + uint8_t result = 0; + return result; + } + +uint8_t MPL3115A2::activeMode(void) +{ + uint8_t result = 0; + char datain[1]; + char dataout[1]; + + result |= MPL3115A2::readRegister(CTRL_REG1, datain , 2); + dataout[0] = (datain[0] & 0xFE) | 0x01 ; + result |= MPL3115A2::writeRegister(CTRL_REG1, dataout); // Set to active mode + + return result; + } +uint8_t MPL3115A2::standbyMode(void) +{ + uint8_t result = 0; + char datain[1]; + char dataout[1]; + + result |= MPL3115A2::readRegister(CTRL_REG1, datain); + dataout[0] = (datain[0] & 0xFE); + result |= MPL3115A2::writeRegister(CTRL_REG1, dataout); // Set to standby mode + + return result; + } + +uint8_t MPL3115A2::triggerOneShot(void) +{ + uint8_t result = 0; + char datain[1]; + char dataout[1]; + + result |= MPL3115A2::readRegister(CTRL_REG1, datain); + dataout[0] = ((datain[0] & 0xFD) | 0x02); + result |= MPL3115A2::writeRegister(CTRL_REG1, dataout); // Trigger a measurement + + return result; + } + +uint8_t MPL3115A2::setAltitudeCalib(int16_t alti_calib) +{ + uint8_t result = 0; + char dataout[1]; + + dataout[0] = alti_calib ; + result |= MPL3115A2::writeRegister(CTRL_REG1, dataout); // set msb of calibration value + + return result; + } + +uint8_t MPL3115A2::clearMinMaxRegs(void) +{ + uint8_t result = 0; + char datain[10]; + + memset(datain, 0, 10); + result = MPL3115A2::writeRegister(P_MIN_MSB, datain, 10); + + return result; + } + +uint8_t MPL3115A2::getStatus(void) +{ + char datain[1]; + uint8_t dataout; + + MPL3115A2::readRegister(DR_STATUS, datain); + dataout = datain[0]; + + return dataout; + } + +int32_t MPL3115A2::getBaroData(void) +{ + if (_polling_mode) + { + char datain[3]; + MPL3115A2::readRegister(OUT_P_MSB, datain, 3); + /* data is 20 bit signed/unsigned with 4 LSB = 0 Need to shift to 32 bits to preserve sign bit + * Altitude is 16 bit signed and pressure is 18 bit unsigned + */ + _data._baro = ((datain[0] << 24) | (datain[1] << 16) | (datain[2]<<8)); + } + return _data._baro; +} + +int16_t MPL3115A2::getTempData(void) +{ + if (_polling_mode) + { + char datain[2]; + MPL3115A2::readRegister(OUT_T_MSB, datain, 2); + /* data is 12 bit signed with 4 LSB = 0 Need to shift first to 16 bits to preserve sign bit then + * divide by 16 to remove LSBs + */ + _data._temp = ((datain[0] << 8) | datain[1]); + _data._temp /= 16; + } + return _data._temp; +} + +int32_t MPL3115A2::getMinBaro(bool clear_data) +{ + if (_polling_mode) + { + char datain[3]; + MPL3115A2::readRegister(P_MIN_MSB, datain, 3); + /* data is 20 bit signed/unsigned with 4 LSB = 0 Need to shift to 32 bits to preserve sign bit + * Altitude is 16 bit signed and pressure is 18 bit unsigned + */ + _data._minbaro = ((datain[0] << 24) | (datain[1] << 16) | (datain[2] << 8)); + + if (clear_data) + { + memset(datain, 0, 3); + MPL3115A2::writeRegister(P_MIN_MSB, datain, 3); + } + } + + return _data._minbaro; +} + +int32_t MPL3115A2::getMaxBaro(bool clear_data) +{ + if (_polling_mode) + { + char datain[3]; + + MPL3115A2::readRegister(P_MAX_MSB, datain, 3); + /* data is 20 bit signed/unsigned with 4 LSB = 0 Need to shift to 32 bits to preserve sign bit + * Altitude is 16 bit signed and pressure is 18 bit unsigned + */ + _data._maxbaro = ((datain[0] << 24) | (datain[1] << 16) | (datain[2] << 8)); + + if (clear_data) + { + memset(datain, 0, 3); + MPL3115A2::writeRegister(P_MAX_MSB, datain, 3); + } + } + + return _data._maxbaro; +} + +int16_t MPL3115A2::getMinTemp(bool clear_data) +{ + if (_polling_mode) + { + char datain[2]; + MPL3115A2::readRegister(T_MIN_MSB, datain, 2); + /* data is 12 bit signed with 4 LSB = 0 Need to shift first to 16 bits to preserve sign bit then + * divide by 16 to remove LSBs + */ + _data._mintemp = ((datain[0] << 8) | datain[1] ); + _data._mintemp /= 16; + + if (clear_data) + { + memset(datain, 0, 2); + MPL3115A2::writeRegister(T_MIN_MSB, datain, 2); + } + } + + return _data._mintemp; + } + +int16_t MPL3115A2::getMaxTemp(bool clear_data) +{ + if (_polling_mode) + { + char datain[2]; + MPL3115A2::readRegister(T_MIN_MSB, datain, 2); + /* data is 12 bit signed with 4 LSB = 0 Need to shift first to 16 bits to preserve sign bit then + * divide by 16 to remove LSBs + */ + _data._maxtemp = ((datain[0] << 8) | datain[1] ); + _data._maxtemp /= 16; + + if (clear_data) + { + memset(datain, 0, 2); + MPL3115A2::writeRegister(T_MAX_MSB, datain, 2); + } + } + + return _data._maxtemp; + } + +MPL3115A2_DATA MPL3115A2::getAllData(bool clear_data) +{ + + if (_polling_mode) + { + char datain[10]; + MPL3115A2::readRegister(OUT_P_MSB, datain, 5); + /* data is 20 bit signed/unsigned with 4 LSB = 0 Need to shift to 32 bits to preserve sign bit + * Altitude is 16 bit signed and pressure is 18 bit unsigned + */ + _data._baro = ((datain[0] << 24) | (datain[1] << 16) | (datain[2]<<8)); + + /* data is 12 bit signed with 4 LSB = 0 Need to shift first to 16 bits to preserve sign bit then + * divide by 16 to remove LSBs + */ + _data._temp = ((datain[3] << 8) | datain[4]); + _data._temp /= 16; + + MPL3115A2::readRegister(P_MIN_MSB, datain, 10); + /* data is 20 bit signed/unsigned with 4 LSB = 0 Need to shift to 32 bits to preserve sign bit + * Altitude is 16 bit signed and pressure is 18 bit unsigned + * temperature data is 12 bit signed with 4 LSB = 0 Need to shift first to 16 bits to preserve sign bit then + * divide by 16 to remove LSBs + */ + _data._minbaro = ((datain[0] << 24) | (datain[1] << 16) | (datain[2] << 8)); + _data._mintemp = ((datain[3] << 8) | datain[4] ); + _data._mintemp /= 16; + _data._maxbaro = ((datain[5] << 24) | (datain[6] << 16) | (datain[7] << 8)); + _data._maxtemp = ((datain[8] << 8) | datain[9] ); + _data._maxtemp /= 16; + + if (clear_data) + { + MPL3115A2::clearMinMaxRegs(); + } + } + + return _data; +} + +uint8_t MPL3115A2::writeRegister(uint8_t reg, char* data, uint8_t count) +{ + char buf[11]; + uint8_t result = 0; + + buf[0] = reg; + memcpy(buf+1,data,count); + +// __disable_irq(); // Tickers and other timebase events can jack up the I2C bus for some devices + result |= _i2c->write(_i2c_addr, buf, (count + 1)); +// __enable_irq(); // Just need to block during the transaction + + if(result != 0) + { + debug("MPL3115A2:writeRegister failed\n\r"); + } + + return result; +} + +uint8_t MPL3115A2::readRegister(uint8_t reg, char* data, uint8_t count) +{ + uint8_t result = 0; + char reg_out[1]; + + reg_out[0] = reg; +// __disable_irq(); // Tickers and other timebase events can jack up the I2C bus for some devices + result |= _i2c->write(_i2c_addr,reg_out,1,true); +// __enable_irq(); // Just need to block during the transaction + + if(result != 0) + { + debug("MPL3115A2::readRegister failed write\n\r"); + return result; + } + +// __disable_irq(); // Tickers and other timebase events can jack up the I2C bus for some devices + result |= _i2c->read(_i2c_addr,data,count,false); +// __enable_irq(); // Just need to block during the transaction + + if(result != 0) + { + debug("MPL3115A2::readRegister failed read\n\r"); + } + + return result; +} + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MPL3115A2.h Mon Jul 06 19:33:37 2015 +0000 @@ -0,0 +1,356 @@ +/** + * @file MPL3115A2.h + * @brief Device driver - MPL3115A2 barometric pressure sensor IC w/RTOS support + * @author Tim Barr + * @version 1.0 + * @see http://cache.freescale.com/files/sensors/doc/data_sheet/MPL3115A2.pdf + * + * Copyright (c) 2015 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MPL3115A2_H +#define MPL3115A2_H + +#include "mbed.h" + +/** Using the MultiTech Systems MTDOT-EVB + * + * Example: + * @code + * #include "mbed.h" + * #include "MPL3115A2.h" + * + + * + * int main() + * { + + * } + * @endcode + */ + + +/** + * @class MPL3115A2_DATA + * @brief API abstraction for the MPL3115A2 barometric pressure IC data + */ +class MPL3115A2_DATA +{ +public: +/*!< volatile data variables */ + volatile int32_t _baro; + volatile int16_t _temp; + volatile int32_t _minbaro; + volatile int32_t _maxbaro; + volatile int16_t _mintemp; + volatile int16_t _maxtemp; + + /** Create the MPL3115A2_DATA object initialized to the parameter (or 0 if none) + * @param baro - the init value of _baro + * @param temp - the init value of _temperature + * @param minbaro - the init value of _minbaro + * @param maxbaro - the init value of _maxbaro + * @param mintemp - the init value of _mintemp + * @param maxtemp - the init value of _maxtemp + */ + MPL3115A2_DATA(int32_t baro = 0, int16_t temp = 0, int32_t minbaro = 0, int32_t maxbaro = 0, + int16_t mintemp = 0, int16_t maxtemp = 0) : _baro(baro), _temp(temp), _minbaro(minbaro), + _maxbaro(maxbaro), _mintemp(mintemp), _maxtemp(maxtemp){} + + /** Overloaded '=' operator to allow shorthand coding, assigning objects to one another + * @param rhs - an object of the same type to assign ourself the same values of + * @return this + */ + MPL3115A2_DATA &operator= (MPL3115A2_DATA const &rhs) + { + _baro = rhs._baro; + _temp = rhs._temp; + _minbaro = rhs._minbaro; + _maxbaro = rhs._maxbaro; + _mintemp = rhs._mintemp; + _maxtemp = rhs._maxtemp; + + return *this; + } + + /** Overloaded '=' operator to allow shorthand coding, assigning objects to one another + * @param val - Assign each data member (_pressure, _temperature) this value + * @return this + + MPL3115A2_DATA &operator= (uint16_t val) + { + _baro = _temp = val; + + return *this; + } + */ + + /** Overloaded '==' operator to allow shorthand coding, test objects to one another + * @param rhs - the object to compare against + * @return 1 if the data members are the same and 0 otherwise + */ + bool operator== (MPL3115A2_DATA &rhs) + { + return ((_baro == rhs._baro)&&(_temp == rhs._temp)&& + (_minbaro == rhs._minbaro) && (_maxbaro == rhs._maxbaro)&& + (_mintemp == rhs._mintemp) && (_maxtemp == rhs._maxtemp)) ? 1 : 0; + } +}; + +/** + * @class MPL3115A2 + * @brief API abstraction for the MPL3115A2 3-axis barometric sensor IC + * initial version will be polling only. Interrupt service and rtos support will + * be added at a later point + */ +class MPL3115A2 +{ +public: + + /** + * @enum WHO_AM_I_VAL + * @brief Device ID's that this class is compatible with + */ + enum WHO_AM_I_VAL + { + I_AM_MPL3115A2 = 0xc4, /*!< MPL3115A2 WHO_AM_I register content */ + }; + + /** + * @enum SYS_MODE + * @brief operating mode of MPL3115A2 + */ + enum SYS_MODE + { + STANDBY = 0, + ACTIVE + }; + + /** + * @enum DR_STATUS_VALS + * @brief flags for data overwrite and data ready + */ + enum DR_STATUS_VALS + { + TDR = 0x02, + PDR = 0x04, + PTDR = 0x08, + TOW = 0x20, + POW = 0x40, + PTOW = 0x80 + }; + + /** + * @enum OUTPUT_MODE + * @brief Select whether data is raw or post-processed + */ + enum OUTPUT_MODE + { + DATA_NORMAL = 0x00, + DATA_RAW = 0x40 + }; + + /** + * @enum DATA_MODE + * @brief Sets the pressure measurement post- processing mode for the sensor + */ + enum DATA_MODE + { + DM_BAROMETER = 0x00, + DM_ALTIMETER = 0x80 + }; + + /** + * @enum OVERSAMPLE_RATIO + * @brief values for oversample ratio + * Note: sample time is 2.5 msec * ratio# i.e. OR_8 -> 2.5 * 8 = 20 msec + */ + enum OVERSAMPLE_RATIO + { + OR_1 = 0x00, + OR_2 = 0x08, + OR_4 = 0x10, + OR_8 = 0x18, + OR_16 = 0x20, + OR_32 = 0x28, + OR_64 = 0x30, + OR_128 = 0x38 + }; + + /** + * @enum ACQUISITION_TIMER + * @brief in active mode this sets time between samples in seconds + */ + enum ACQUISITION_TIMER + { + AT_1 = 0x00, + AT_2, AT_4, AT_8, AT_16, AT_32, AT_64, AT_128, AT_256, + AT_512, AT_1024, AT_2048, AT_4096, AT_8192, AT_16384, AT_32768 + }; + + /** + * @enum REGISTER + * @brief The device register map + */ + enum REGISTER + { + STATUS = 0x0, + OUT_P_MSB, OUT_P_CSB, OUT_P_LSB, OUT_T_MSB, OUT_T_LSB, DR_STATUS, + OUT_P_DELTA_MSB, OUT_P_DELTA_CSB, OUT_P_DELTA_LSB, OUT_T_DELTA_MSB, OUT_T_DELTA_LSB, + WHO_AM_I, F_STATUS, F_DATA, F_SETUP, TIME_DLY, SYSMOD, INT_SOURCE, + PT_DATA_CFG, BAR_IN_MSB, BAR_IN_LSB, P_ARM_MSB, P_ARM_LSB, T_ARM, + P_ARM_WND_MSB, P_ARM_WND_LSB, T_ARM_WND, + P_MIN_MSB, P_MIN_CSB, P_MIN_LSB, T_MIN_MSB, T_MIN_LSB, + P_MAX_MSB, P_MAX_CSB, P_MAX_LSB, T_MAX_MSB, T_MAX_LSB, + CTRL_REG1, CTRL_REG2, CTRL_REG3, CTRL_REG4, CTRL_REG5, + OFF_P, OFF_T, OFF_H + }; + + /** Create the MPL3115A2 object + * @param i2c - A defined I2C object + * @param int1 - A defined InterruptIn object pointer. Default NULL for polling mode + * @param int2 - A defined InterruptIn object pointer. Default NULL for polling mode + * TODO - Need to add interrupt support + */ + MPL3115A2(I2C &i2c, InterruptIn* int1 = NULL, InterruptIn* int2 = NULL); + + /** Test the Who am I register for valid ID + * @return Boolean true if valid device + */ + bool testWhoAmI(void) ; + + /** Setup the MPL3115A2 for standard barometric sensor read mode + * @out_mode - Turns Data post processing ON/OFF using the OUTPUT_MODE enum + * @data_mode - Sets Pressure or Altitude mode using the DATA_MODE enum + * @os_ratio - Sets the Oversample ration using the OVERSAMPLE_RATIO enum + * @measure_time - Sets the Aquisition time for Active mode using the ACQUISITION_TIMER enum + * @return status of command + * + * This sets the resolution, range, data rate, oversample + * mode, hi and lo pass filter. + */ + uint8_t setParameters(OUTPUT_MODE out_mode, DATA_MODE data_mode, OVERSAMPLE_RATIO os_ratio, + ACQUISITION_TIMER measure_time) ; + + uint8_t enableFIFO(void) ; + + /** Put the MPL3115A2 in the Standby mode + * @return status of command + * TODO - need to implement function + */ + uint8_t standbyMode(void) ; + + /** Put the MPL3115A2 in the active mode + * @return status of command + */ + uint8_t activeMode(void) ; + + /** Triggers the MPL3115A2 to take one measurement in Active or Standby mode + * @return status of command + */ + uint8_t triggerOneShot(void) ; + + /** Set the sea level equivalent pressure for Altitude mode + * @alti_calib - Value is Equivalent sea level pressure for measurement location (2 Pa resolution) + * @return status byte + */ + uint8_t setAltitudeCalib(int16_t alti_calib) ; + + /** Clears all minimum and maximum data registers + * @return status of command + */ + uint8_t clearMinMaxRegs(void) ; + + /** Check the MPL3115A2 status register + * @return status byte + */ + uint8_t getStatus(void) ; + + /** Get the Pressure or Altitude data + * @return The last valid pressure based reading from the barometric sensor + */ + int32_t getBaroData(void); + + /** Get the Temperature data + * @return The last valid temperature reading from the barometric sensor + */ + int16_t getTempData(void); + + /** Get the Minimum Pressure or Altitude data + * @param Boolean TRUE clears the register after reading + * @return The Minimum Pressure or Altitude read since last cleared + */ + int32_t getMinBaro(bool clear_data); + + /** Get the Maximum Pressure or Altitude data + * @param Boolean true clears the register after reading + * @return The Maximum Pressure or Altitude read since last cleared + */ + int32_t getMaxBaro(bool clear_data); + + /** Get the Minimum Temperature data + * @param Boolean true clears the register after reading + * @return The Minimum temperature read since last cleared + */ + int16_t getMinTemp(bool clear_data); + + /** Get the Maximum Temperature data + * @param Boolean true clears the register after reading + * @return The Maximum temperature read since last cleared + */ + int16_t getMaxTemp(bool clear_data); + + /** Get the MP3115A2 data structure + * @param Boolean true clears all MIN/MAX registers after reading + * @return MPL3115A2_DATA structure + */ + MPL3115A2_DATA getAllData(bool clear_data); + + /* + * Need to add interrupt support code here when I get the chance + */ + + +private: + + I2C *_i2c; + InterruptIn *_int1; + InterruptIn *_int2; + MPL3115A2_DATA _data; + bool _polling_mode; + uint8_t static const _i2c_addr = (0x60 <<1); + + uint8_t init(void); + + /** Write to a register + * Note: most writes are only valid in stop mode + * @param reg - The register to be written + * @param data - The data to be written + * @param count - number of bytes to send, assumes 1 byte if not specified + * @return - status of command + */ + uint8_t writeRegister(uint8_t reg, char* data, uint8_t count = 1); + + /** Read from a register + * @param reg - The register to read from + * @param data - buffer of data to be read + * @param count - number of bytes to send, assumes 1 byte if not specified + * @return - status of command + */ + uint8_t readRegister(uint8_t reg, char* data, uint8_t count = 1); + +}; + +#endif