MAX32620HSP (MAXREFDES100) RPC Example for Graphical User Interface

Dependencies:   USBDevice

Fork of HSP_Release by Jerry Bradshaw

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BMP280.cpp Source File

BMP280.cpp

00001 /*******************************************************************************
00002  * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a
00005  * copy of this software and associated documentation files (the "Software"),
00006  * to deal in the Software without restriction, including without limitation
00007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008  * and/or sell copies of the Software, and to permit persons to whom the
00009  * Software is furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included
00012  * in all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017  * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018  * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020  * OTHER DEALINGS IN THE SOFTWARE.
00021  *
00022  * Except as contained in this notice, the name of Maxim Integrated
00023  * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024  * Products, Inc. Branding Policy.
00025  *
00026  * The mere transfer of this software does not imply any licenses
00027  * of trade secrets, proprietary technology, copyrights, patents,
00028  * trademarks, maskwork rights, or any other form of intellectual
00029  * property whatsoever. Maxim Integrated Products, Inc. retains all
00030  * ownership rights.
00031  *******************************************************************************
00032  */
00033 #include "mbed.h"
00034 #include "BMP280.h"
00035 
00036 //******************************************************************************
00037 BMP280::BMP280(PinName sda, PinName scl, int slaveAddress) : 
00038         slaveAddress(slaveAddress) {
00039 
00040   i2c = new I2C(sda, scl);
00041   isOwner = true;
00042 }
00043 
00044 //******************************************************************************
00045 BMP280::BMP280(I2C *i2c, int slaveAddress) : 
00046         slaveAddress(slaveAddress) {
00047 
00048   this->i2c = i2c;
00049   isOwner = false;
00050 
00051   i2c->frequency(100000);
00052   loggingEnabled = 0;
00053   loggingSampleRate = 5;
00054 }
00055 
00056 //******************************************************************************
00057 BMP280::~BMP280(void) {
00058 
00059   if (isOwner == true) {
00060     delete i2c;
00061   }
00062 }
00063 
00064 //******************************************************************************
00065 int BMP280::init(BMP280::bmp280_osrs_P_t Osrs_p, BMP280::bmp280_osrs_T_t Osrs_t,
00066                  BMP280::bmp280_FILT_t Filter, BMP280::bmp280_MODE_t Mode,
00067                  BMP280::bmp280_TSB_t T_sb) {
00068 
00069   char raw_Tn[6];
00070   char raw_Pn[20];
00071   
00072   bmp280_ctrl_meas_t ctrl_meas;
00073   bmp280_config_t    config;
00074 
00075   ///
00076   /// Read all the temp coeffecients from the BMP280 memory. It will be used in
00077   /// calculation
00078   ///
00079   if (reg_read(BMP280_CALIB00, raw_Tn, 6) != 0) { 
00080     return -1;
00081   }
00082 
00083   dig_T1 = (((uint16_t)raw_Tn[1]) << 8) | raw_Tn[0];
00084   dig_T2 = (((int16_t)raw_Tn[3]) << 8) | raw_Tn[2];
00085   dig_T3 = (((int16_t)raw_Tn[5]) << 8) | raw_Tn[4];
00086 
00087   ///
00088   /// Read all the press coeffecients from the BMP280 memory. It will be used in
00089   /// calculation
00090   ///
00091   if (reg_read((BMP280_REG_map_t )(BMP280_CALIB00+6) /*reg*/, raw_Pn, 20) != 0) {
00092     return -1;
00093   }
00094 
00095   dig_P1 = (((uint16_t)raw_Pn[1]) << 8) | raw_Pn[0];
00096   dig_P2 = (((int16_t)raw_Pn[3]) << 8) | raw_Pn[2];
00097   dig_P3 = (((int16_t)raw_Pn[5]) << 8) | raw_Pn[4];
00098   dig_P4 = (((int16_t)raw_Pn[7]) << 8) | raw_Pn[6];
00099   dig_P5 = (((int16_t)raw_Pn[9]) << 8) | raw_Pn[8];
00100   dig_P6 = (((int16_t)raw_Pn[11]) << 8) | raw_Pn[10];
00101   dig_P7 = (((int16_t)raw_Pn[13]) << 8) | raw_Pn[12];
00102   dig_P8 = (((int16_t)raw_Pn[15]) << 8) | raw_Pn[14];
00103   dig_P9 = (((int16_t)raw_Pn[17]) << 8) | raw_Pn[16];
00104 
00105 
00106   wait(1.0 / 10.0);
00107 
00108   /****/
00109   if (reg_read(BMP280_CTRL_MEAS, &ctrl_meas.all, 1) != 0) {
00110     return -1;
00111   }
00112 
00113   ctrl_meas.bit.osrs_p = Osrs_p;
00114   ctrl_meas.bit.osrs_t = Osrs_t;
00115 
00116   ctrl_meas.bit.mode = Mode;
00117 
00118   if (reg_write(BMP280_CTRL_MEAS, ctrl_meas.all) != 0) {
00119     return -1;
00120   }
00121 
00122   /****/
00123 
00124   if (reg_read(BMP280_CONFIG, &config.all, 1) != 0) {
00125     return -1;
00126   }
00127 
00128   config.bit.filter = Filter;
00129 
00130   if (Mode == 0b11) {
00131     config.bit.t_sb = T_sb;
00132   }
00133 
00134   if (reg_write(BMP280_CONFIG, config.all) != 0) {
00135     return -1;
00136   }
00137 
00138   return 0;
00139 }
00140 
00141 //******************************************************************************
00142 float BMP280::ToFahrenheit(float temperature) {
00143 
00144   return temperature * (9 / 5.0f) + 32;
00145 }
00146 
00147 //******************************************************************************
00148 int BMP280::ReadCompDataRaw2(char *bmp280_rawData) {
00149 
00150   int i;
00151   char data[6];
00152   float temp;
00153   float pressure;
00154   int iPressure;
00155   char str[32];
00156   ReadCompDataRaw(data);
00157   ToFloat(data, &temp, &pressure);
00158   iPressure = (int)pressure;
00159   sprintf(str, "%d  ", iPressure);
00160   for (i = 0; i < 6; i++) {
00161     bmp280_rawData[i] = str[i];
00162   }
00163   return 0;
00164 }
00165 
00166 //******************************************************************************
00167 int BMP280::ReadCompDataRaw(char *bmp280_rawData) {
00168 
00169   if (reg_read(BMP280_PRESS_MSB, bmp280_rawData, 6) != 0) {
00170     return -1;
00171   }
00172   return 0;
00173 }
00174 
00175 //******************************************************************************
00176 void BMP280::ToFloat(char *bmp280_rawData, float *Temp_degC, float *Press_Pa) {
00177 
00178   bmp280_rawPress = (uint32_t)(bmp280_rawData[0] << 12) |
00179                     (bmp280_rawData[1] << 4) | (bmp280_rawData[2] >> 4);
00180 
00181   bmp280_rawTemp = (uint32_t)(bmp280_rawData[3] << 12) |
00182                    (bmp280_rawData[4] << 4) | (bmp280_rawData[5] >> 4);
00183 
00184   *Temp_degC = compensate_T_float(bmp280_rawTemp);
00185   *Press_Pa = compensate_P_float(bmp280_rawPress);
00186 }
00187 
00188 //******************************************************************************
00189 int BMP280::ReadCompData(float *Temp_degC, float *Press_Pa) {
00190 
00191   char bmp280_rawData[6];
00192 
00193   if (ReadCompDataRaw(bmp280_rawData) != 0) {
00194     return -1;
00195   }
00196   ToFloat(bmp280_rawData, Temp_degC, Press_Pa);
00197   return 0;
00198 }
00199 
00200 //******************************************************************************
00201 int BMP280::reg_write(BMP280_REG_map_t  reg, char value) {
00202 
00203   int result;
00204   char cmdData[2] = {(char)reg, value};
00205   result = i2c->write(slaveAddress, cmdData, 2);
00206   if (result != 0) {
00207     return -1;
00208   }
00209   return 0;
00210 }
00211 
00212 //******************************************************************************
00213 int BMP280::reg_read(BMP280_REG_map_t  reg, char *value, char number) {
00214 
00215   int result;
00216   char cmdData[1] = {(char)reg};
00217 
00218   result = i2c->write(slaveAddress, cmdData, 1);
00219   if (result != 0) {
00220     return -1;
00221   }
00222   result = i2c->read(slaveAddress, value, number);
00223   if (result != 0) {
00224     return -1;
00225   }
00226   return 0;
00227 }
00228 
00229 //******************************************************************************
00230 int BMP280::Sleep(void) {
00231   
00232   bmp280_ctrl_meas_t ctrl_meas;
00233 
00234   if (reg_read(BMP280_CTRL_MEAS, &ctrl_meas.all, 1) != 0) {
00235     return -1;
00236   }
00237   ctrl_meas.bit.mode = 0b00; // put everything to sleep mode...
00238 
00239   if (reg_write(BMP280_CTRL_MEAS, ctrl_meas.all) != 0) {
00240     return -1;
00241   }
00242   return 0;
00243 }
00244 
00245 //******************************************************************************
00246 void BMP280::Reset(void) {
00247 
00248   reg_write(BMP280_RESET, 0xB6); // Initiate a Soft Reset
00249 }
00250 
00251 //******************************************************************************
00252 int BMP280::Detect(void) {
00253 
00254   if (reg_read(BMP280_ID, &bmp280_id, 1) != 0) {
00255     return -1;
00256   }
00257 
00258   if (bmp280_id == 0x58) {
00259     return 1;
00260   }
00261   return 0;
00262 }
00263 
00264 //******************************************************************************
00265 int BMP280::ReadId(void) {
00266 
00267   if (reg_read(BMP280_ID, &bmp280_id, 1) != 0) {
00268     return -1;
00269   }
00270   return bmp280_id;
00271 }
00272 
00273 //******************************************************************************
00274 float BMP280::compensate_T_float(int32_t adc_T) {
00275 
00276   float var1, var2, T;
00277   var1 =
00278       (((float)adc_T) / 16384.0f - ((float)dig_T1) / 1024.0f) * ((float)dig_T2);
00279 
00280   var2 = ((((float)adc_T) / 131072.0f - ((float)dig_T1) / 8192.0f) *
00281           (((float)adc_T) / 131072.0f - ((float)dig_T1) / 8192.0f)) *
00282          ((float)dig_T3);
00283 
00284   t_fine = (int32_t)(var1 + var2);
00285 
00286   T = (var1 + var2) / 5120.0f;
00287 
00288   return T;
00289 }
00290 
00291 //******************************************************************************
00292 float BMP280::compensate_P_float(int32_t adc_P) {
00293 
00294   float var1, var2, p;
00295   var1 = ((float)t_fine / 2.0f) - 64000.0f;
00296   var2 = var1 * var1 * ((float)dig_P6) / 32768.0f;
00297   var2 = var2 + var1 * ((float)dig_P5) * 2.0f;
00298   var2 = (var2 / 4.0f) + (((float)dig_P4) * 65536.0f);
00299   var1 = (((float)dig_P3) * var1 * var1 / 524288.0f + ((float)dig_P2) * var1) / 524288.0f;
00300   var1 = (1.0f + var1 / 32768.0f) * ((float)dig_P1);
00301   if (var1 == 0.0f) {
00302     return 0; // avoid exception caused by division by zero
00303   }
00304   p = 1048576.0f - (float)adc_P;
00305   p = (p - (var2 / 4096.0f)) * 6250.0f / var1;
00306   var1 = ((float)dig_P9) * p * p / 2147483648.0f;
00307   var2 = p * ((float)dig_P8) / 32768.0f;
00308   p = p + (var1 + var2 + ((float)dig_P7)) / 16.0f;
00309   return p;
00310 }