Browse Source

Update BME280 module to v3.4.3.

Vladimir N. Shilov 3 years ago
parent
commit
db32636ca8
3 changed files with 306 additions and 245 deletions
  1. 48 46
      Inc/bme280.h
  2. 72 72
      Inc/bme280_defs.h
  3. 186 127
      Src/bme280.c

+ 48 - 46
Inc/bme280.h

@@ -1,50 +1,40 @@
 /**
- * Copyright (C) 2018 - 2019 Bosch Sensortec GmbH
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * Neither the name of the copyright holder nor the names of the
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
- * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
- * OR CONTRIBUTORS BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
- * OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
- * ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
- *
- * The information provided is believed to be accurate and reliable.
- * The copyright holder assumes no responsibility
- * for the consequences of use
- * of such information nor for any infringement of patents or
- * other rights of third parties which may result from its use.
- * No license is granted by implication or otherwise under any patent or
- * patent rights of the copyright holder.
- *
- * @file    bme280.h
- * @date    08 Mar 2019
- * @version 3.3.6
- * @brief
- *
- */
+* Copyright (c) 2020 Bosch Sensortec GmbH. All rights reserved.
+*
+* BSD-3-Clause
+*
+* Redistribution and use in source and binary forms, with or without
+* modification, are permitted provided that the following conditions are met:
+*
+* 1. Redistributions of source code must retain the above copyright
+*    notice, this list of conditions and the following disclaimer.
+*
+* 2. Redistributions in binary form must reproduce the above copyright
+*    notice, this list of conditions and the following disclaimer in the
+*    documentation and/or other materials provided with the distribution.
+*
+* 3. Neither the name of the copyright holder nor the names of its
+*    contributors may be used to endorse or promote products derived from
+*    this software without specific prior written permission.
+*
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+* POSSIBILITY OF SUCH DAMAGE.
+*
+* @file	bme280.h
+* @date	2020-01-24
+* @version	v3.4.3
+*
+*/
 
 /*! @file bme280.h
  * @brief Sensor driver for BME280 sensor
@@ -237,6 +227,18 @@ int8_t bme280_compensate_data(uint8_t sensor_comp,
                               struct bme280_data *comp_data,
                               struct bme280_calib_data *calib_data);
 
+/*!
+ * @brief This API is used to calculate the maximum delay in milliseconds required for the
+ * temperature/pressure/humidity(which ever are enabled) measurement to complete.
+ * The delay depends upon the number of sensors enabled and their oversampling configuration.
+ *
+ * @param[in] settings : contains the oversampling configurations.
+ *
+ * @return delay required in milliseconds.
+ */
+
+uint32_t bme280_cal_meas_delay(const struct bme280_settings *settings);
+
 #ifdef __cplusplus
 }
 #endif /* End of CPP guard */

+ 72 - 72
Inc/bme280_defs.h

@@ -1,50 +1,40 @@
 /**
- * Copyright (C) 2018 - 2019 Bosch Sensortec GmbH
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * Neither the name of the copyright holder nor the names of the
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
- * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
- * OR CONTRIBUTORS BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
- * OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
- * ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
- *
- * The information provided is believed to be accurate and reliable.
- * The copyright holder assumes no responsibility
- * for the consequences of use
- * of such information nor for any infringement of patents or
- * other rights of third parties which may result from its use.
- * No license is granted by implication or otherwise under any patent or
- * patent rights of the copyright holder.
- *
- * @file    bme280_defs.h
- * @date    08 Mar 2019
- * @version 3.3.6
- * @brief
- *
- */
+* Copyright (c) 2020 Bosch Sensortec GmbH. All rights reserved.
+*
+* BSD-3-Clause
+*
+* Redistribution and use in source and binary forms, with or without
+* modification, are permitted provided that the following conditions are met:
+*
+* 1. Redistributions of source code must retain the above copyright
+*    notice, this list of conditions and the following disclaimer.
+*
+* 2. Redistributions in binary form must reproduce the above copyright
+*    notice, this list of conditions and the following disclaimer in the
+*    documentation and/or other materials provided with the distribution.
+*
+* 3. Neither the name of the copyright holder nor the names of its
+*    contributors may be used to endorse or promote products derived from
+*    this software without specific prior written permission.
+*
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+* POSSIBILITY OF SUCH DAMAGE.
+*
+* @file	bme280_defs.h
+* @date	2020-01-24
+* @version	v3.4.3
+*
+*/
 
 /*! @file bme280_defs.h
  * @brief Sensor driver for BME280 sensor
@@ -103,13 +93,12 @@
 
 /********************************************************/
 
-#ifndef BME280_FLOAT_ENABLE
+#ifndef BME280_64BIT_ENABLE /* Check if 64-bit integer (using BME280_64BIT_ENABLE) is enabled */
+#ifndef BME280_32BIT_ENABLE /* Check if 32-bit integer (using BME280_32BIT_ENABLE) is enabled */
+#define BME280_32BIT_ENABLE
+#ifndef BME280_FLOAT_ENABLE /* If any of the integer data types not enabled then enable BME280_FLOAT_ENABLE */
 /* #define BME280_FLOAT_ENABLE */
 #endif
-
-#ifndef BME280_FLOAT_ENABLE
-#ifndef BME280_64BIT_ENABLE
-/* #define BME280_64BIT_ENABLE */
 #endif
 #endif
 
@@ -121,8 +110,8 @@
 #endif
 
 /**\name I2C addresses */
-#define BME280_I2C_ADDR_PRIM              UINT8_C(0x76 << 1)
-#define BME280_I2C_ADDR_SEC               UINT8_C(0x77 << 1)
+#define BME280_I2C_ADDR_PRIM              UINT8_C(0x76)
+#define BME280_I2C_ADDR_SEC               UINT8_C(0x77)
 
 /**\name BME280 chip identifier */
 #define BME280_CHIP_ID                    UINT8_C(0x60)
@@ -147,6 +136,7 @@
 #define BME280_E_INVALID_LEN              INT8_C(-3)
 #define BME280_E_COMM_FAIL                INT8_C(-4)
 #define BME280_E_SLEEP_MODE_FAIL          INT8_C(-5)
+#define BME280_E_NVM_COPY_FAILED          INT8_C(-6)
 
 /**\name API warning codes */
 #define BME280_W_INVALID_OSR_MACRO        INT8_C(1)
@@ -219,8 +209,14 @@
 #define BME280_OVERSAMPLING_8X      UINT8_C(0x04)
 #define BME280_OVERSAMPLING_16X     UINT8_C(0x05)
 
+/**\name Measurement delay calculation macros  */
+#define BME280_MEAS_OFFSET          UINT16_C(1250)
+#define BME280_MEAS_DUR             UINT16_C(2300)
+#define BME280_PRES_HUM_MEAS_OFFSET UINT16_C(575)
+#define BME280_MEAS_SCALING_FACTOR  UINT16_C(1000)
+
 /**\name Standby duration selection macros */
-#define BME280_STANDBY_TIME_0_5_MS    (0x00)
+#define BME280_STANDBY_TIME_0_5_MS  (0x00)
 #define BME280_STANDBY_TIME_62_5_MS (0x01)
 #define BME280_STANDBY_TIME_125_MS  (0x02)
 #define BME280_STANDBY_TIME_250_MS  (0x03)
@@ -236,6 +232,10 @@
 #define BME280_FILTER_COEFF_8       (0x03)
 #define BME280_FILTER_COEFF_16      (0x04)
 
+#define BME280_STATUS_REG_ADDR      (0xF3)
+#define BME280_SOFT_RESET_COMMAND   (0xB6)
+#define BME280_STATUS_IM_UPDATE     (0x01)
+
 /*!
  * @brief Interface selection Enums
  */
@@ -263,24 +263,24 @@ struct bme280_calib_data
      */
 
     /**@{*/
-    uint16_t dig_T1;
-    int16_t dig_T2;
-    int16_t dig_T3;
-    uint16_t dig_P1;
-    int16_t dig_P2;
-    int16_t dig_P3;
-    int16_t dig_P4;
-    int16_t dig_P5;
-    int16_t dig_P6;
-    int16_t dig_P7;
-    int16_t dig_P8;
-    int16_t dig_P9;
-    uint8_t dig_H1;
-    int16_t dig_H2;
-    uint8_t dig_H3;
-    int16_t dig_H4;
-    int16_t dig_H5;
-    int8_t dig_H6;
+    uint16_t dig_t1;
+    int16_t dig_t2;
+    int16_t dig_t3;
+    uint16_t dig_p1;
+    int16_t dig_p2;
+    int16_t dig_p3;
+    int16_t dig_p4;
+    int16_t dig_p5;
+    int16_t dig_p6;
+    int16_t dig_p7;
+    int16_t dig_p8;
+    int16_t dig_p9;
+    uint8_t dig_h1;
+    int16_t dig_h2;
+    uint8_t dig_h3;
+    int16_t dig_h4;
+    int16_t dig_h5;
+    int8_t dig_h6;
     int32_t t_fine;
 
     /**@}*/

+ 186 - 127
Src/bme280.c

@@ -1,49 +1,40 @@
-/**\mainpage
- * Copyright (C) 2018 - 2019 Bosch Sensortec GmbH
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * Neither the name of the copyright holder nor the names of the
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
- * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
- * OR CONTRIBUTORS BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
- * OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
- * ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
- *
- * The information provided is believed to be accurate and reliable.
- * The copyright holder assumes no responsibility
- * for the consequences of use
- * of such information nor for any infringement of patents or
- * other rights of third parties which may result from its use.
- * No license is granted by implication or otherwise under any patent or
- * patent rights of the copyright holder.
- *
- * File     bme280.c
- * Date     08 Mar 2019
- * Version  3.3.6
- *
- */
+/**
+* Copyright (c) 2020 Bosch Sensortec GmbH. All rights reserved.
+*
+* BSD-3-Clause
+*
+* Redistribution and use in source and binary forms, with or without
+* modification, are permitted provided that the following conditions are met:
+*
+* 1. Redistributions of source code must retain the above copyright
+*    notice, this list of conditions and the following disclaimer.
+*
+* 2. Redistributions in binary form must reproduce the above copyright
+*    notice, this list of conditions and the following disclaimer in the
+*    documentation and/or other materials provided with the distribution.
+*
+* 3. Neither the name of the copyright holder nor the names of its
+*    contributors may be used to endorse or promote products derived from
+*    this software without specific prior written permission.
+*
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+* POSSIBILITY OF SUCH DAMAGE.
+*
+* @file	bme280.c
+* @date	2020-01-24
+* @version	v3.4.3
+*
+*/
 
 /*! @file bme280.c
  * @brief Sensor driver for BME280 sensor
@@ -632,9 +623,11 @@ int8_t bme280_soft_reset(const struct bme280_dev *dev)
 {
     int8_t rslt;
     uint8_t reg_addr = BME280_RESET_ADDR;
+    uint8_t status_reg = 0;
+    uint8_t try_run = 5;
 
     /* 0xB6 is the soft reset command */
-    uint8_t soft_rst_cmd = 0xB6;
+    uint8_t soft_rst_cmd = BME280_SOFT_RESET_COMMAND;
 
     /* Check for null pointer in the device structure*/
     rslt = null_ptr_check(dev);
@@ -645,8 +638,22 @@ int8_t bme280_soft_reset(const struct bme280_dev *dev)
         /* Write the soft reset command in the sensor */
         rslt = bme280_set_regs(&reg_addr, &soft_rst_cmd, 1, dev);
 
-        /* As per data sheet, startup time is 2 ms. */
-        dev->delay_ms(2);
+        if (rslt == BME280_OK)
+        {
+            /* If NVM not copied yet, Wait for NVM to copy */
+            do
+            {
+                /* As per data sheet - Table 1, startup time is 2 ms. */
+                dev->delay_ms(2);
+                rslt = bme280_get_regs(BME280_STATUS_REG_ADDR, &status_reg, 1, dev);
+            } while ((rslt == BME280_OK) && (try_run--) && (status_reg & BME280_STATUS_IM_UPDATE));
+
+            if (status_reg & BME280_STATUS_IM_UPDATE)
+            {
+                rslt = BME280_E_NVM_COPY_FAILED;
+            }
+
+        }
     }
 
     return rslt;
@@ -715,9 +722,9 @@ void bme280_parse_sensor_data(const uint8_t *reg_data, struct bme280_uncomp_data
     data_xlsb = (uint32_t)reg_data[5] >> 4;
     uncomp_data->temperature = data_msb | data_lsb | data_xlsb;
 
-    /* Store the parsed register values for temperature data */
-    data_lsb = (uint32_t)reg_data[6] << 8;
-    data_msb = (uint32_t)reg_data[7];
+    /* Store the parsed register values for humidity data */
+    data_msb = (uint32_t)reg_data[6] << 8;
+    data_lsb = (uint32_t)reg_data[7];
     uncomp_data->humidity = data_msb | data_lsb;
 }
 
@@ -765,6 +772,56 @@ int8_t bme280_compensate_data(uint8_t sensor_comp,
     return rslt;
 }
 
+/*!
+ * @brief This API is used to calculate the maximum delay in milliseconds required for the
+ * temperature/pressure/humidity(which ever at enabled) measurement to complete.
+ */
+uint32_t bme280_cal_meas_delay(const struct bme280_settings *settings)
+{
+    uint32_t max_delay;
+    uint8_t temp_osr;
+    uint8_t pres_osr;
+    uint8_t hum_osr;
+
+    /*Array to map OSR config register value to actual OSR */
+    uint8_t osr_sett_to_act_osr[] = { 0, 1, 2, 4, 8, 16 };
+
+    /* Mapping osr settings to the actual osr values e.g. 0b101 -> osr X16  */
+    if (settings->osr_t <= 5)
+    {
+        temp_osr = osr_sett_to_act_osr[settings->osr_t];
+    }
+    else
+    {
+        temp_osr = 16;
+    }
+
+    if (settings->osr_p <= 5)
+    {
+        pres_osr = osr_sett_to_act_osr[settings->osr_p];
+    }
+    else
+    {
+        pres_osr = 16;
+    }
+
+    if (settings->osr_h <= 5)
+    {
+        hum_osr = osr_sett_to_act_osr[settings->osr_h];
+    }
+    else
+    {
+        hum_osr = 16;
+    }
+
+    max_delay =
+        (uint32_t)((BME280_MEAS_OFFSET + (BME280_MEAS_DUR * temp_osr) +
+                    ((BME280_MEAS_DUR * pres_osr) + BME280_PRES_HUM_MEAS_OFFSET) +
+                    ((BME280_MEAS_DUR * hum_osr) + BME280_PRES_HUM_MEAS_OFFSET)) / BME280_MEAS_SCALING_FACTOR);
+
+    return max_delay;
+}
+
 /*!
  * @brief This internal API sets the oversampling settings for pressure,
  * temperature and humidity in the sensor.
@@ -1010,10 +1067,10 @@ static double compensate_temperature(const struct bme280_uncomp_data *uncomp_dat
     double temperature_min = -40;
     double temperature_max = 85;
 
-    var1 = ((double)uncomp_data->temperature) / 16384.0 - ((double)calib_data->dig_T1) / 1024.0;
-    var1 = var1 * ((double)calib_data->dig_T2);
-    var2 = (((double)uncomp_data->temperature) / 131072.0 - ((double)calib_data->dig_T1) / 8192.0);
-    var2 = (var2 * var2) * ((double)calib_data->dig_T3);
+    var1 = ((double)uncomp_data->temperature) / 16384.0 - ((double)calib_data->dig_t1) / 1024.0;
+    var1 = var1 * ((double)calib_data->dig_t2);
+    var2 = (((double)uncomp_data->temperature) / 131072.0 - ((double)calib_data->dig_t1) / 8192.0);
+    var2 = (var2 * var2) * ((double)calib_data->dig_t3);
     calib_data->t_fine = (int32_t)(var1 + var2);
     temperature = (var1 + var2) / 5120.0;
     if (temperature < temperature_min)
@@ -1043,21 +1100,21 @@ static double compensate_pressure(const struct bme280_uncomp_data *uncomp_data,
     double pressure_max = 110000.0;
 
     var1 = ((double)calib_data->t_fine / 2.0) - 64000.0;
-    var2 = var1 * var1 * ((double)calib_data->dig_P6) / 32768.0;
-    var2 = var2 + var1 * ((double)calib_data->dig_P5) * 2.0;
-    var2 = (var2 / 4.0) + (((double)calib_data->dig_P4) * 65536.0);
-    var3 = ((double)calib_data->dig_P3) * var1 * var1 / 524288.0;
-    var1 = (var3 + ((double)calib_data->dig_P2) * var1) / 524288.0;
-    var1 = (1.0 + var1 / 32768.0) * ((double)calib_data->dig_P1);
+    var2 = var1 * var1 * ((double)calib_data->dig_p6) / 32768.0;
+    var2 = var2 + var1 * ((double)calib_data->dig_p5) * 2.0;
+    var2 = (var2 / 4.0) + (((double)calib_data->dig_p4) * 65536.0);
+    var3 = ((double)calib_data->dig_p3) * var1 * var1 / 524288.0;
+    var1 = (var3 + ((double)calib_data->dig_p2) * var1) / 524288.0;
+    var1 = (1.0 + var1 / 32768.0) * ((double)calib_data->dig_p1);
 
     /* avoid exception caused by division by zero */
-    if (var1)
+    if (var1 > (0.0))
     {
         pressure = 1048576.0 - (double) uncomp_data->pressure;
         pressure = (pressure - (var2 / 4096.0)) * 6250.0 / var1;
-        var1 = ((double)calib_data->dig_P9) * pressure * pressure / 2147483648.0;
-        var2 = pressure * ((double)calib_data->dig_P8) / 32768.0;
-        pressure = pressure + (var1 + var2 + ((double)calib_data->dig_P7)) / 16.0;
+        var1 = ((double)calib_data->dig_p9) * pressure * pressure / 2147483648.0;
+        var2 = pressure * ((double)calib_data->dig_p8) / 32768.0;
+        pressure = pressure + (var1 + var2 + ((double)calib_data->dig_p7)) / 16.0;
         if (pressure < pressure_min)
         {
             pressure = pressure_min;
@@ -1093,13 +1150,14 @@ static double compensate_humidity(const struct bme280_uncomp_data *uncomp_data,
     double var6;
 
     var1 = ((double)calib_data->t_fine) - 76800.0;
-    var2 = (((double)calib_data->dig_H4) * 64.0 + (((double)calib_data->dig_H5) / 16384.0) * var1);
+    var2 = (((double)calib_data->dig_h4) * 64.0 + (((double)calib_data->dig_h5) / 16384.0) * var1);
     var3 = uncomp_data->humidity - var2;
-    var4 = ((double)calib_data->dig_H2) / 65536.0;
-    var5 = (1.0 + (((double)calib_data->dig_H3) / 67108864.0) * var1);
-    var6 = 1.0 + (((double)calib_data->dig_H6) / 67108864.0) * var1 * var5;
+    var4 = ((double)calib_data->dig_h2) / 65536.0;
+    var5 = (1.0 + (((double)calib_data->dig_h3) / 67108864.0) * var1);
+    var6 = 1.0 + (((double)calib_data->dig_h6) / 67108864.0) * var1 * var5;
     var6 = var3 * var4 * (var5 * var6);
-    humidity = var6 * (1.0 - ((double)calib_data->dig_H1) * var6 / 524288.0);
+    humidity = var6 * (1.0 - ((double)calib_data->dig_h1) * var6 / 524288.0);
+
     if (humidity > humidity_max)
     {
         humidity = humidity_max;
@@ -1127,12 +1185,13 @@ static int32_t compensate_temperature(const struct bme280_uncomp_data *uncomp_da
     int32_t temperature_min = -4000;
     int32_t temperature_max = 8500;
 
-    var1 = (int32_t)((uncomp_data->temperature / 8) - ((int32_t)calib_data->dig_T1 * 2));
-    var1 = (var1 * ((int32_t)calib_data->dig_T2)) / 2048;
-    var2 = (int32_t)((uncomp_data->temperature / 16) - ((int32_t)calib_data->dig_T1));
-    var2 = (((var2 * var2) / 4096) * ((int32_t)calib_data->dig_T3)) / 16384;
+    var1 = (int32_t)((uncomp_data->temperature / 8) - ((int32_t)calib_data->dig_t1 * 2));
+    var1 = (var1 * ((int32_t)calib_data->dig_t2)) / 2048;
+    var2 = (int32_t)((uncomp_data->temperature / 16) - ((int32_t)calib_data->dig_t1));
+    var2 = (((var2 * var2) / 4096) * ((int32_t)calib_data->dig_t3)) / 16384;
     calib_data->t_fine = var1 + var2;
     temperature = (calib_data->t_fine * 5 + 128) / 256;
+
     if (temperature < temperature_min)
     {
         temperature = temperature_min;
@@ -1144,7 +1203,7 @@ static int32_t compensate_temperature(const struct bme280_uncomp_data *uncomp_da
 
     return temperature;
 }
-#ifdef BME280_64BIT_ENABLE
+#ifndef BME280_32BIT_ENABLE /* 64 bit compensation for pressure data */
 
 /*!
  * @brief This internal API is used to compensate the raw pressure data and
@@ -1163,21 +1222,21 @@ static uint32_t compensate_pressure(const struct bme280_uncomp_data *uncomp_data
     uint32_t pressure_max = 11000000;
 
     var1 = ((int64_t)calib_data->t_fine) - 128000;
-    var2 = var1 * var1 * (int64_t)calib_data->dig_P6;
-    var2 = var2 + ((var1 * (int64_t)calib_data->dig_P5) * 131072);
-    var2 = var2 + (((int64_t)calib_data->dig_P4) * 34359738368);
-    var1 = ((var1 * var1 * (int64_t)calib_data->dig_P3) / 256) + ((var1 * ((int64_t)calib_data->dig_P2) * 4096));
+    var2 = var1 * var1 * (int64_t)calib_data->dig_p6;
+    var2 = var2 + ((var1 * (int64_t)calib_data->dig_p5) * 131072);
+    var2 = var2 + (((int64_t)calib_data->dig_p4) * 34359738368);
+    var1 = ((var1 * var1 * (int64_t)calib_data->dig_p3) / 256) + ((var1 * ((int64_t)calib_data->dig_p2) * 4096));
     var3 = ((int64_t)1) * 140737488355328;
-    var1 = (var3 + var1) * ((int64_t)calib_data->dig_P1) / 8589934592;
+    var1 = (var3 + var1) * ((int64_t)calib_data->dig_p1) / 8589934592;
 
     /* To avoid divide by zero exception */
     if (var1 != 0)
     {
         var4 = 1048576 - uncomp_data->pressure;
         var4 = (((var4 * INT64_C(2147483648)) - var2) * 3125) / var1;
-        var1 = (((int64_t)calib_data->dig_P9) * (var4 / 8192) * (var4 / 8192)) / 33554432;
-        var2 = (((int64_t)calib_data->dig_P8) * var4) / 524288;
-        var4 = ((var4 + var1 + var2) / 256) + (((int64_t)calib_data->dig_P7) * 16);
+        var1 = (((int64_t)calib_data->dig_p9) * (var4 / 8192) * (var4 / 8192)) / 33554432;
+        var2 = (((int64_t)calib_data->dig_p8) * var4) / 524288;
+        var4 = ((var4 + var1 + var2) / 256) + (((int64_t)calib_data->dig_p7) * 16);
         pressure = (uint32_t)(((var4 / 2) * 100) / 128);
         if (pressure < pressure_min)
         {
@@ -1195,7 +1254,7 @@ static uint32_t compensate_pressure(const struct bme280_uncomp_data *uncomp_data
 
     return pressure;
 }
-#else
+#else /* 32 bit compensation for pressure data */
 
 /*!
  * @brief This internal API is used to compensate the raw pressure data and
@@ -1214,13 +1273,13 @@ static uint32_t compensate_pressure(const struct bme280_uncomp_data *uncomp_data
     uint32_t pressure_max = 110000;
 
     var1 = (((int32_t)calib_data->t_fine) / 2) - (int32_t)64000;
-    var2 = (((var1 / 4) * (var1 / 4)) / 2048) * ((int32_t)calib_data->dig_P6);
-    var2 = var2 + ((var1 * ((int32_t)calib_data->dig_P5)) * 2);
-    var2 = (var2 / 4) + (((int32_t)calib_data->dig_P4) * 65536);
-    var3 = (calib_data->dig_P3 * (((var1 / 4) * (var1 / 4)) / 8192)) / 8;
-    var4 = (((int32_t)calib_data->dig_P2) * var1) / 2;
+    var2 = (((var1 / 4) * (var1 / 4)) / 2048) * ((int32_t)calib_data->dig_p6);
+    var2 = var2 + ((var1 * ((int32_t)calib_data->dig_p5)) * 2);
+    var2 = (var2 / 4) + (((int32_t)calib_data->dig_p4) * 65536);
+    var3 = (calib_data->dig_p3 * (((var1 / 4) * (var1 / 4)) / 8192)) / 8;
+    var4 = (((int32_t)calib_data->dig_p2) * var1) / 2;
     var1 = (var3 + var4) / 262144;
-    var1 = (((32768 + var1)) * ((int32_t)calib_data->dig_P1)) / 32768;
+    var1 = (((32768 + var1)) * ((int32_t)calib_data->dig_p1)) / 32768;
 
     /* avoid exception caused by division by zero */
     if (var1)
@@ -1235,9 +1294,9 @@ static uint32_t compensate_pressure(const struct bme280_uncomp_data *uncomp_data
         {
             pressure = (pressure / (uint32_t)var1) * 2;
         }
-        var1 = (((int32_t)calib_data->dig_P9) * ((int32_t)(((pressure / 8) * (pressure / 8)) / 8192))) / 4096;
-        var2 = (((int32_t)(pressure / 4)) * ((int32_t)calib_data->dig_P8)) / 8192;
-        pressure = (uint32_t)((int32_t)pressure + ((var1 + var2 + calib_data->dig_P7) / 16));
+        var1 = (((int32_t)calib_data->dig_p9) * ((int32_t)(((pressure / 8) * (pressure / 8)) / 8192))) / 4096;
+        var2 = (((int32_t)(pressure / 4)) * ((int32_t)calib_data->dig_p8)) / 8192;
+        pressure = (uint32_t)((int32_t)pressure + ((var1 + var2 + calib_data->dig_p7) / 16));
         if (pressure < pressure_min)
         {
             pressure = pressure_min;
@@ -1273,16 +1332,16 @@ static uint32_t compensate_humidity(const struct bme280_uncomp_data *uncomp_data
 
     var1 = calib_data->t_fine - ((int32_t)76800);
     var2 = (int32_t)(uncomp_data->humidity * 16384);
-    var3 = (int32_t)(((int32_t)calib_data->dig_H4) * 1048576);
-    var4 = ((int32_t)calib_data->dig_H5) * var1;
+    var3 = (int32_t)(((int32_t)calib_data->dig_h4) * 1048576);
+    var4 = ((int32_t)calib_data->dig_h5) * var1;
     var5 = (((var2 - var3) - var4) + (int32_t)16384) / 32768;
-    var2 = (var1 * ((int32_t)calib_data->dig_H6)) / 1024;
-    var3 = (var1 * ((int32_t)calib_data->dig_H3)) / 2048;
+    var2 = (var1 * ((int32_t)calib_data->dig_h6)) / 1024;
+    var3 = (var1 * ((int32_t)calib_data->dig_h3)) / 2048;
     var4 = ((var2 * (var3 + (int32_t)32768)) / 1024) + (int32_t)2097152;
-    var2 = ((var4 * ((int32_t)calib_data->dig_H2)) + 8192) / 16384;
+    var2 = ((var4 * ((int32_t)calib_data->dig_h2)) + 8192) / 16384;
     var3 = var5 * var2;
     var4 = ((var3 / 32768) * (var3 / 32768)) / 128;
-    var5 = var3 - ((var4 * ((int32_t)calib_data->dig_H1)) / 16);
+    var5 = var3 - ((var4 * ((int32_t)calib_data->dig_h1)) / 16);
     var5 = (var5 < 0 ? 0 : var5);
     var5 = (var5 > 419430400 ? 419430400 : var5);
     humidity = (uint32_t)(var5 / 4096);
@@ -1354,19 +1413,19 @@ static void parse_temp_press_calib_data(const uint8_t *reg_data, struct bme280_d
 {
     struct bme280_calib_data *calib_data = &dev->calib_data;
 
-    calib_data->dig_T1 = BME280_CONCAT_BYTES(reg_data[1], reg_data[0]);
-    calib_data->dig_T2 = (int16_t)BME280_CONCAT_BYTES(reg_data[3], reg_data[2]);
-    calib_data->dig_T3 = (int16_t)BME280_CONCAT_BYTES(reg_data[5], reg_data[4]);
-    calib_data->dig_P1 = BME280_CONCAT_BYTES(reg_data[7], reg_data[6]);
-    calib_data->dig_P2 = (int16_t)BME280_CONCAT_BYTES(reg_data[9], reg_data[8]);
-    calib_data->dig_P3 = (int16_t)BME280_CONCAT_BYTES(reg_data[11], reg_data[10]);
-    calib_data->dig_P4 = (int16_t)BME280_CONCAT_BYTES(reg_data[13], reg_data[12]);
-    calib_data->dig_P5 = (int16_t)BME280_CONCAT_BYTES(reg_data[15], reg_data[14]);
-    calib_data->dig_P6 = (int16_t)BME280_CONCAT_BYTES(reg_data[17], reg_data[16]);
-    calib_data->dig_P7 = (int16_t)BME280_CONCAT_BYTES(reg_data[19], reg_data[18]);
-    calib_data->dig_P8 = (int16_t)BME280_CONCAT_BYTES(reg_data[21], reg_data[20]);
-    calib_data->dig_P9 = (int16_t)BME280_CONCAT_BYTES(reg_data[23], reg_data[22]);
-    calib_data->dig_H1 = reg_data[25];
+    calib_data->dig_t1 = BME280_CONCAT_BYTES(reg_data[1], reg_data[0]);
+    calib_data->dig_t2 = (int16_t)BME280_CONCAT_BYTES(reg_data[3], reg_data[2]);
+    calib_data->dig_t3 = (int16_t)BME280_CONCAT_BYTES(reg_data[5], reg_data[4]);
+    calib_data->dig_p1 = BME280_CONCAT_BYTES(reg_data[7], reg_data[6]);
+    calib_data->dig_p2 = (int16_t)BME280_CONCAT_BYTES(reg_data[9], reg_data[8]);
+    calib_data->dig_p3 = (int16_t)BME280_CONCAT_BYTES(reg_data[11], reg_data[10]);
+    calib_data->dig_p4 = (int16_t)BME280_CONCAT_BYTES(reg_data[13], reg_data[12]);
+    calib_data->dig_p5 = (int16_t)BME280_CONCAT_BYTES(reg_data[15], reg_data[14]);
+    calib_data->dig_p6 = (int16_t)BME280_CONCAT_BYTES(reg_data[17], reg_data[16]);
+    calib_data->dig_p7 = (int16_t)BME280_CONCAT_BYTES(reg_data[19], reg_data[18]);
+    calib_data->dig_p8 = (int16_t)BME280_CONCAT_BYTES(reg_data[21], reg_data[20]);
+    calib_data->dig_p9 = (int16_t)BME280_CONCAT_BYTES(reg_data[23], reg_data[22]);
+    calib_data->dig_h1 = reg_data[25];
 }
 
 /*!
@@ -1376,20 +1435,20 @@ static void parse_temp_press_calib_data(const uint8_t *reg_data, struct bme280_d
 static void parse_humidity_calib_data(const uint8_t *reg_data, struct bme280_dev *dev)
 {
     struct bme280_calib_data *calib_data = &dev->calib_data;
-    int16_t dig_H4_lsb;
-    int16_t dig_H4_msb;
-    int16_t dig_H5_lsb;
-    int16_t dig_H5_msb;
-
-    calib_data->dig_H2 = (int16_t)BME280_CONCAT_BYTES(reg_data[1], reg_data[0]);
-    calib_data->dig_H3 = reg_data[2];
-    dig_H4_msb = (int16_t)(int8_t)reg_data[3] * 16;
-    dig_H4_lsb = (int16_t)(reg_data[4] & 0x0F);
-    calib_data->dig_H4 = dig_H4_msb | dig_H4_lsb;
-    dig_H5_msb = (int16_t)(int8_t)reg_data[5] * 16;
-    dig_H5_lsb = (int16_t)(reg_data[4] >> 4);
-    calib_data->dig_H5 = dig_H5_msb | dig_H5_lsb;
-    calib_data->dig_H6 = (int8_t)reg_data[6];
+    int16_t dig_h4_lsb;
+    int16_t dig_h4_msb;
+    int16_t dig_h5_lsb;
+    int16_t dig_h5_msb;
+
+    calib_data->dig_h2 = (int16_t)BME280_CONCAT_BYTES(reg_data[1], reg_data[0]);
+    calib_data->dig_h3 = reg_data[2];
+    dig_h4_msb = (int16_t)(int8_t)reg_data[3] * 16;
+    dig_h4_lsb = (int16_t)(reg_data[4] & 0x0F);
+    calib_data->dig_h4 = dig_h4_msb | dig_h4_lsb;
+    dig_h5_msb = (int16_t)(int8_t)reg_data[5] * 16;
+    dig_h5_lsb = (int16_t)(reg_data[4] >> 4);
+    calib_data->dig_h5 = dig_h5_msb | dig_h5_lsb;
+    calib_data->dig_h6 = (int8_t)reg_data[6];
 }
 
 /*!