Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: mDotEVBM2X MTDOT-EVBDemo-DRH MTDOT-BOX-EVB-Factory-Firmware-LIB-108 MTDOT-UDKDemo_Senet ... more
MPL3115A2.h
00001 /** 00002 * @file MPL3115A2.h 00003 * @brief Device driver - MPL3115A2 barometric pressure sensor IC w/RTOS support 00004 * @author Tim Barr 00005 * @version 1.0 00006 * @see http://cache.freescale.com/files/sensors/doc/data_sheet/MPL3115A2.pdf 00007 * 00008 * Copyright (c) 2015 00009 * 00010 * Licensed under the Apache License, Version 2.0 (the "License"); 00011 * you may not use this file except in compliance with the License. 00012 * You may obtain a copy of the License at 00013 * 00014 * http://www.apache.org/licenses/LICENSE-2.0 00015 * 00016 * Unless required by applicable law or agreed to in writing, software 00017 * distributed under the License is distributed on an "AS IS" BASIS, 00018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00019 * See the License for the specific language governing permissions and 00020 * limitations under the License. 00021 */ 00022 00023 #ifndef MPL3115A2_H 00024 #define MPL3115A2_H 00025 00026 #include "mbed.h" 00027 00028 /** Using the MultiTech Systems MTDOT-EVB 00029 * 00030 * Example: 00031 * @code 00032 * #include "mbed.h" 00033 * #include "MPL3115A2.h" 00034 * 00035 00036 * 00037 * int main() 00038 * { 00039 00040 * } 00041 * @endcode 00042 */ 00043 00044 00045 /** 00046 * @class MPL3115A2_DATA 00047 * @brief API abstraction for the MPL3115A2 barometric pressure IC data 00048 */ 00049 class MPL3115A2_DATA 00050 { 00051 public: 00052 /*!< volatile data variables */ 00053 volatile int32_t _baro ; 00054 volatile int16_t _temp; 00055 volatile int32_t _minbaro; 00056 volatile int32_t _maxbaro; 00057 volatile int16_t _mintemp; 00058 volatile int16_t _maxtemp; 00059 00060 /** Create the MPL3115A2_DATA object initialized to the parameter (or 0 if none) 00061 * @param baro - the init value of _baro 00062 * @param temp - the init value of _temperature 00063 * @param minbaro - the init value of _minbaro 00064 * @param maxbaro - the init value of _maxbaro 00065 * @param mintemp - the init value of _mintemp 00066 * @param maxtemp - the init value of _maxtemp 00067 */ 00068 MPL3115A2_DATA(int32_t baro = 0, int16_t temp = 0, int32_t minbaro = 0, int32_t maxbaro = 0, 00069 int16_t mintemp = 0, int16_t maxtemp = 0) : _baro (baro), _temp(temp), _minbaro(minbaro), 00070 _maxbaro(maxbaro), _mintemp(mintemp), _maxtemp(maxtemp){} 00071 00072 /** Overloaded '=' operator to allow shorthand coding, assigning objects to one another 00073 * @param rhs - an object of the same type to assign ourself the same values of 00074 * @return this 00075 */ 00076 MPL3115A2_DATA &operator= (MPL3115A2_DATA const &rhs) 00077 { 00078 _baro = rhs._baro ; 00079 _temp = rhs._temp; 00080 _minbaro = rhs._minbaro; 00081 _maxbaro = rhs._maxbaro; 00082 _mintemp = rhs._mintemp; 00083 _maxtemp = rhs._maxtemp; 00084 00085 return *this; 00086 } 00087 00088 /** Overloaded '=' operator to allow shorthand coding, assigning objects to one another 00089 * @param val - Assign each data member (_pressure, _temperature) this value 00090 * @return this 00091 00092 MPL3115A2_DATA &operator= (uint16_t val) 00093 { 00094 _baro = _temp = val; 00095 00096 return *this; 00097 } 00098 */ 00099 00100 /** Overloaded '==' operator to allow shorthand coding, test objects to one another 00101 * @param rhs - the object to compare against 00102 * @return 1 if the data members are the same and 0 otherwise 00103 */ 00104 bool operator== (MPL3115A2_DATA &rhs) 00105 { 00106 return ((_baro == rhs._baro )&&(_temp == rhs._temp)&& 00107 (_minbaro == rhs._minbaro) && (_maxbaro == rhs._maxbaro)&& 00108 (_mintemp == rhs._mintemp) && (_maxtemp == rhs._maxtemp)) ? 1 : 0; 00109 } 00110 }; 00111 00112 /** 00113 * @class MPL3115A2 00114 * @brief API abstraction for the MPL3115A2 3-axis barometric sensor IC 00115 * initial version will be polling only. Interrupt service and rtos support will 00116 * be added at a later point 00117 */ 00118 class MPL3115A2 00119 { 00120 public: 00121 00122 /** 00123 * @enum WHO_AM_I_VAL 00124 * @brief Device ID's that this class is compatible with 00125 */ 00126 enum WHO_AM_I_VAL 00127 { 00128 I_AM_MPL3115A2 = 0xc4, /*!< MPL3115A2 WHO_AM_I register content */ 00129 }; 00130 00131 /** 00132 * @enum SYS_MODE 00133 * @brief operating mode of MPL3115A2 00134 */ 00135 enum SYS_MODE 00136 { 00137 STANDBY = 0, 00138 ACTIVE 00139 }; 00140 00141 /** 00142 * @enum DR_STATUS_VALS 00143 * @brief flags for data overwrite and data ready 00144 */ 00145 enum DR_STATUS_VALS 00146 { 00147 TDR = 0x02, 00148 PDR = 0x04, 00149 PTDR = 0x08, 00150 TOW = 0x20, 00151 POW = 0x40, 00152 PTOW = 0x80 00153 }; 00154 00155 /** 00156 * @enum OUTPUT_MODE 00157 * @brief Select whether data is raw or post-processed 00158 */ 00159 enum OUTPUT_MODE 00160 { 00161 DATA_NORMAL = 0x00, 00162 DATA_RAW = 0x40 00163 }; 00164 00165 /** 00166 * @enum DATA_MODE 00167 * @brief Sets the pressure measurement post- processing mode for the sensor 00168 */ 00169 enum DATA_MODE 00170 { 00171 DM_BAROMETER = 0x00, 00172 DM_ALTIMETER = 0x80 00173 }; 00174 00175 /** 00176 * @enum OVERSAMPLE_RATIO 00177 * @brief values for oversample ratio 00178 * Note: sample time is 2.5 msec * ratio# i.e. OR_8 -> 2.5 * 8 = 20 msec 00179 */ 00180 enum OVERSAMPLE_RATIO 00181 { 00182 OR_1 = 0x00, 00183 OR_2 = 0x08, 00184 OR_4 = 0x10, 00185 OR_8 = 0x18, 00186 OR_16 = 0x20, 00187 OR_32 = 0x28, 00188 OR_64 = 0x30, 00189 OR_128 = 0x38 00190 }; 00191 00192 /** 00193 * @enum ACQUISITION_TIMER 00194 * @brief in active mode this sets time between samples in seconds 00195 */ 00196 enum ACQUISITION_TIMER 00197 { 00198 AT_1 = 0x00, 00199 AT_2, AT_4, AT_8, AT_16, AT_32, AT_64, AT_128, AT_256, 00200 AT_512, AT_1024, AT_2048, AT_4096, AT_8192, AT_16384, AT_32768 00201 }; 00202 00203 /** 00204 * @enum REGISTER 00205 * @brief The device register map 00206 */ 00207 enum REGISTER 00208 { 00209 STATUS = 0x0, 00210 OUT_P_MSB, OUT_P_CSB, OUT_P_LSB, OUT_T_MSB, OUT_T_LSB, DR_STATUS, 00211 OUT_P_DELTA_MSB, OUT_P_DELTA_CSB, OUT_P_DELTA_LSB, OUT_T_DELTA_MSB, OUT_T_DELTA_LSB, 00212 WHO_AM_I, F_STATUS, F_DATA, F_SETUP, TIME_DLY, SYSMOD, INT_SOURCE, 00213 PT_DATA_CFG, BAR_IN_MSB, BAR_IN_LSB, P_ARM_MSB, P_ARM_LSB, T_ARM, 00214 P_ARM_WND_MSB, P_ARM_WND_LSB, T_ARM_WND, 00215 P_MIN_MSB, P_MIN_CSB, P_MIN_LSB, T_MIN_MSB, T_MIN_LSB, 00216 P_MAX_MSB, P_MAX_CSB, P_MAX_LSB, T_MAX_MSB, T_MAX_LSB, 00217 CTRL_REG1, CTRL_REG2, CTRL_REG3, CTRL_REG4, CTRL_REG5, 00218 OFF_P, OFF_T, OFF_H 00219 }; 00220 00221 /** Create the MPL3115A2 object 00222 * @param i2c - A defined I2C object 00223 * @param int1 - A defined InterruptIn object pointer. Default NULL for polling mode 00224 * @param int2 - A defined InterruptIn object pointer. Default NULL for polling mode 00225 * TODO - Need to add interrupt support 00226 */ 00227 MPL3115A2(I2C &i2c, InterruptIn* int1 = NULL, InterruptIn* int2 = NULL); 00228 00229 /** Test the Who am I register for valid ID 00230 * @return Boolean true if valid device 00231 */ 00232 bool testWhoAmI(void) ; 00233 00234 /** Setup the MPL3115A2 for standard barometric sensor read mode 00235 * @out_mode - Turns Data post processing ON/OFF using the OUTPUT_MODE enum 00236 * @data_mode - Sets Pressure or Altitude mode using the DATA_MODE enum 00237 * @os_ratio - Sets the Oversample ration using the OVERSAMPLE_RATIO enum 00238 * @measure_time - Sets the Aquisition time for Active mode using the ACQUISITION_TIMER enum 00239 * @return status of command 00240 * 00241 * This sets the resolution, range, data rate, oversample 00242 * mode, hi and lo pass filter. 00243 */ 00244 uint8_t setParameters(OUTPUT_MODE out_mode, DATA_MODE data_mode, OVERSAMPLE_RATIO os_ratio, 00245 ACQUISITION_TIMER measure_time) ; 00246 00247 uint8_t enableFIFO(void) ; 00248 00249 /** Put the MPL3115A2 in the Standby mode 00250 * @return status of command 00251 * TODO - need to implement function 00252 */ 00253 uint8_t standbyMode(void) ; 00254 00255 /** Put the MPL3115A2 in the active mode 00256 * @return status of command 00257 */ 00258 uint8_t activeMode(void) ; 00259 00260 /** Triggers the MPL3115A2 to take one measurement in Active or Standby mode 00261 * @return status of command 00262 */ 00263 uint8_t triggerOneShot(void) ; 00264 00265 /** Set the sea level equivalent pressure for Altitude mode 00266 * @alti_calib - Value is Equivalent sea level pressure for measurement location (2 Pa resolution) 00267 * @return status byte 00268 */ 00269 uint8_t setAltitudeCalib(int16_t alti_calib) ; 00270 00271 /** Clears all minimum and maximum data registers 00272 * @return status of command 00273 */ 00274 uint8_t clearMinMaxRegs(void) ; 00275 00276 /** Check the MPL3115A2 status register 00277 * @return status byte 00278 */ 00279 uint8_t getStatus(void) ; 00280 00281 /** Get the Pressure or Altitude data 00282 * @return The last valid pressure based reading from the barometric sensor 00283 */ 00284 int32_t getBaroData(void); 00285 00286 /** Get the Temperature data 00287 * @return The last valid temperature reading from the barometric sensor 00288 */ 00289 int16_t getTempData(void); 00290 00291 /** Get the Minimum Pressure or Altitude data 00292 * @param Boolean TRUE clears the register after reading 00293 * @return The Minimum Pressure or Altitude read since last cleared 00294 */ 00295 int32_t getMinBaro(bool clear_data); 00296 00297 /** Get the Maximum Pressure or Altitude data 00298 * @param Boolean true clears the register after reading 00299 * @return The Maximum Pressure or Altitude read since last cleared 00300 */ 00301 int32_t getMaxBaro(bool clear_data); 00302 00303 /** Get the Minimum Temperature data 00304 * @param Boolean true clears the register after reading 00305 * @return The Minimum temperature read since last cleared 00306 */ 00307 int16_t getMinTemp(bool clear_data); 00308 00309 /** Get the Maximum Temperature data 00310 * @param Boolean true clears the register after reading 00311 * @return The Maximum temperature read since last cleared 00312 */ 00313 int16_t getMaxTemp(bool clear_data); 00314 00315 /** Get the MP3115A2 data structure 00316 * @param Boolean true clears all MIN/MAX registers after reading 00317 * @return MPL3115A2_DATA structure 00318 */ 00319 MPL3115A2_DATA getAllData(bool clear_data); 00320 00321 /* 00322 * Need to add interrupt support code here when I get the chance 00323 */ 00324 00325 00326 private: 00327 00328 I2C *_i2c; 00329 InterruptIn *_int1; 00330 InterruptIn *_int2; 00331 MPL3115A2_DATA _data; 00332 bool _polling_mode; 00333 uint8_t static const _i2c_addr = (0x60 <<1); 00334 00335 uint8_t init(void); 00336 00337 /** Write to a register 00338 * Note: most writes are only valid in stop mode 00339 * @param reg - The register to be written 00340 * @param data - The data to be written 00341 * @param count - number of bytes to send, assumes 1 byte if not specified 00342 * @return - status of command 00343 */ 00344 uint8_t writeRegister(uint8_t reg, char* data, uint8_t count = 1); 00345 00346 /** Read from a register 00347 * @param reg - The register to read from 00348 * @param data - buffer of data to be read 00349 * @param count - number of bytes to send, assumes 1 byte if not specified 00350 * @return - status of command 00351 */ 00352 uint8_t readRegister(uint8_t reg, char* data, uint8_t count = 1); 00353 00354 }; 00355 00356 #endif
Generated on Sun Jul 17 2022 09:03:19 by
