You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2018/12/12 13:09:35 UTC

[GitHub] kasjer closed pull request #1543: fix bmp280 bme280 issues

kasjer closed pull request #1543: fix bmp280 bme280 issues
URL: https://github.com/apache/mynewt-core/pull/1543
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/hw/drivers/sensors/bme280/pkg.yml b/hw/drivers/sensors/bme280/pkg.yml
index 8f9780ef8a..68547839ee 100644
--- a/hw/drivers/sensors/bme280/pkg.yml
+++ b/hw/drivers/sensors/bme280/pkg.yml
@@ -33,6 +33,9 @@ pkg.deps:
     - "@apache-mynewt-core/hw/sensor"
     - "@apache-mynewt-core/sys/log/modlog"
 
+pkg.deps.!BUS_DRIVER_PRESENT:
+    - "@apache-mynewt-core/hw/util/i2cn"
+
 pkg.req_apis:
     - stats
 
diff --git a/hw/drivers/sensors/bme280/src/bme280.c b/hw/drivers/sensors/bme280/src/bme280.c
index 1226e909b8..6b1818aa5e 100644
--- a/hw/drivers/sensors/bme280/src/bme280.c
+++ b/hw/drivers/sensors/bme280/src/bme280.c
@@ -199,7 +199,7 @@ bme280_compensate_temperature(int32_t rawtemp, struct bme280_pdd *pdd)
 {
     double var1, var2, comptemp;
 
-    if (rawtemp == 0x800000) {
+    if (rawtemp == 0x80000) {
         BME280_LOG(ERROR, "Invalid temp data\n");
         STATS_INC(g_bme280stats, invalid_data_errors);
         return NAN;
@@ -234,7 +234,7 @@ bme280_compensate_pressure(struct sensor_itf *itf, int32_t rawpress,
     double var1, var2, p;
     int32_t temp;
 
-    if (rawpress == 0x800000) {
+    if (rawpress == 0x80000) {
         BME280_LOG(ERROR, "Invalid press data\n");
         STATS_INC(g_bme280stats, invalid_data_errors);
         return NAN;
@@ -335,8 +335,6 @@ bme280_compensate_temperature(int32_t rawtemp, struct bme280_pdd *pdd)
         return NAN;
     }
 
-    rawtemp >>= 4;
-
     var1 = ((((rawtemp>>3) - ((int32_t)pdd->bcd.bcd_dig_T1 <<1))) *
             ((int32_t)pdd->bcd.bcd_dig_T2)) >> 11;
 
@@ -366,7 +364,7 @@ bme280_compensate_pressure(struct sensor_itf *itf, int32_t rawpress,
     int64_t var1, var2, p;
     int32_t temp;
 
-    if (rawpress == 0x800000) {
+    if (rawpress == 0x80000) {
         BME280_LOG(ERROR, "Invalid pressure data\n");
         STATS_INC(g_bme280stats, invalid_data_errors);
         return NAN;
@@ -378,8 +376,6 @@ bme280_compensate_pressure(struct sensor_itf *itf, int32_t rawpress,
         }
     }
 
-    rawpress >>= 4;
-
     var1 = ((int64_t)pdd->t_fine) - 128000;
     var2 = var1 * var1 * (int64_t)pdd->bcd.bcd_dig_P6;
     var2 = var2 + ((int64_t)(var1*(int64_t)pdd->bcd.bcd_dig_P5) << 17);
@@ -413,7 +409,7 @@ bme280_compensate_pressure(struct sensor_itf *itf, int32_t rawpress,
  */
 static float
 bme280_compensate_humidity(struct sensor_itf *itf, uint32_t rawhumid,
-                           struct bme280_calib_data *bcd)
+                           struct bme280_pdd *pdd)
 {
     int32_t h;
     int32_t temp;
@@ -425,13 +421,13 @@ bme280_compensate_humidity(struct sensor_itf *itf, uint32_t rawhumid,
         return NAN;
     }
 
-    if (!g_t_fine) {
-        if(!bme280_get_temperature(&temp)) {
-            (void)bme280_compensate_temperature(temp, bcd);
+    if (!pdd->t_fine) {
+        if(!bme280_get_temperature(itf, &temp)) {
+            (void)bme280_compensate_temperature(temp, pdd);
         }
     }
 
-    tmp32 = (g_t_fine - ((int32_t)76800));
+    tmp32 = (pdd->t_fine - ((int32_t)76800));
 
     tmp32 = (((((rawhumid << 14) - (((int32_t)pdd->bcd.bcd_dig_H4) << 20) -
              (((int32_t)pdd->bcd.bcd_dig_H5) * tmp32)) + ((int32_t)16384)) >> 15) *
@@ -493,35 +489,30 @@ bme280_sensor_read(struct sensor *sensor, sensor_type_t type,
 
     rawtemp = rawpress = rawhumid = 0;
 
+    /* Get a new temperature sample always */
+    rc = bme280_get_temperature(itf, &rawtemp);
+    if (rc) {
+        goto err;
+    }
+    databuf.std.std_temp = bme280_compensate_temperature(rawtemp, &(bme280->pdd));
+
     /* Get a new pressure sample */
     if (type & SENSOR_TYPE_PRESSURE) {
         rc = bme280_get_pressure(itf, &rawpress);
         if (rc) {
             goto err;
         }
+    }
 
-        databuf.spd.spd_press = bme280_compensate_pressure(itf, rawpress, &(bme280->pdd));
-
-        if (databuf.spd.spd_press != NAN) {
-            databuf.spd.spd_press_is_valid = 1;
-        }
-
-        /* Call data function */
-        rc = data_func(sensor, data_arg, &databuf.spd, SENSOR_TYPE_PRESSURE);
+    /* Get a new relative humidity sample */
+    if (type & SENSOR_TYPE_RELATIVE_HUMIDITY) {
+        rc = bme280_get_humidity(itf, &rawhumid);
         if (rc) {
             goto err;
         }
     }
 
-    /* Get a new temperature sample */
     if (type & SENSOR_TYPE_AMBIENT_TEMPERATURE) {
-        rc = bme280_get_temperature(itf, &rawtemp);
-        if (rc) {
-            goto err;
-        }
-
-        databuf.std.std_temp = bme280_compensate_temperature(rawtemp, &(bme280->pdd));
-
         if (databuf.std.std_temp != NAN) {
             databuf.std.std_temp_is_valid = 1;
         }
@@ -533,13 +524,21 @@ bme280_sensor_read(struct sensor *sensor, sensor_type_t type,
         }
     }
 
-    /* Get a new relative humidity sample */
-    if (type & SENSOR_TYPE_RELATIVE_HUMIDITY) {
-        rc = bme280_get_humidity(itf, &rawhumid);
+    if (type & SENSOR_TYPE_PRESSURE) {
+        databuf.spd.spd_press = bme280_compensate_pressure(itf, rawpress, &(bme280->pdd));
+
+        if (databuf.spd.spd_press != NAN) {
+            databuf.spd.spd_press_is_valid = 1;
+        }
+
+        /* Call data function */
+        rc = data_func(sensor, data_arg, &databuf.spd, SENSOR_TYPE_PRESSURE);
         if (rc) {
             goto err;
         }
+    }
 
+    if (type & SENSOR_TYPE_RELATIVE_HUMIDITY) {
         databuf.shd.shd_humid = bme280_compensate_humidity(itf, rawhumid, &(bme280->pdd));
 
         if (databuf.shd.shd_humid != NAN) {
@@ -976,13 +975,9 @@ bme280_get_temperature(struct sensor_itf *itf, int32_t *temp)
         goto err;
     }
 
-#if MYNEWT_VAL(BME280_SPEC_CALC)
     *temp = (int32_t)((((uint32_t)(tmp[0])) << 12) |
                       (((uint32_t)(tmp[1])) <<  4) |
                        ((uint32_t)tmp[2] >> 4));
-#else
-    *temp = ((tmp[0] << 16) | (tmp[1] << 8) | tmp[2]);
-#endif
 
     return 0;
 err:
@@ -1006,11 +1001,7 @@ bme280_get_humidity(struct sensor_itf *itf, int32_t *humid)
     if (rc) {
         goto err;
     }
-#if MYNEWT_VAL(BME280_SPEC_CALC)
     *humid = (tmp[0] << 8 | tmp[1]);
-#else
-    *humid = (tmp[0] << 8 | tmp[1]);
-#endif
 
     return 0;
 err:
@@ -1035,13 +1026,9 @@ bme280_get_pressure(struct sensor_itf *itf, int32_t *press)
         goto err;
     }
 
-#if MYNEWT_VAL(BME280_SPEC_CALC)
     *press = (int32_t)((((uint32_t)(tmp[0])) << 12) |
                       (((uint32_t)(tmp[1])) <<  4)  |
                        ((uint32_t)tmp[2] >> 4));
-#else
-    *press = ((tmp[0] << 16) | (tmp[1] << 8) | tmp[2]);
-#endif
 
     return 0;
 err:
diff --git a/hw/drivers/sensors/bme280/syscfg.yml b/hw/drivers/sensors/bme280/syscfg.yml
index f26087e854..054e89039b 100644
--- a/hw/drivers/sensors/bme280/syscfg.yml
+++ b/hw/drivers/sensors/bme280/syscfg.yml
@@ -34,7 +34,9 @@ syscfg.defs:
         description: 'Enable shell support for the BME280'
         value: 0
     BME280_SPEC_CALC:
-        description: 'BME280 Spec calculation insetad of built in one'
+        description:
+            When set to 1 compensation functions use double precission floating point arithmetic recomended by specification.
+            When set to 0 compensation functions use integer arithmetic.
         value : 1
     BME280_LOG_MODULE:
         description: 'Numeric module ID to use for BME280 log messages'
diff --git a/hw/drivers/sensors/bmp280/include/bmp280/bmp280.h b/hw/drivers/sensors/bmp280/include/bmp280/bmp280.h
index 94d5ebec0b..271b6a63d5 100644
--- a/hw/drivers/sensors/bmp280/include/bmp280/bmp280.h
+++ b/hw/drivers/sensors/bmp280/include/bmp280/bmp280.h
@@ -102,7 +102,14 @@ struct bmp280_pdd {
 };
 
 struct bmp280 {
+#if MYNEWT_VAL(BUS_DRIVER_PRESENT)
+    union {
+        struct bus_i2c_node i2c_node;
+        struct bus_spi_node spi_node;
+    };
+#else
     struct os_dev dev;
+#endif
     struct sensor sensor;
     struct bmp280_cfg cfg;
     struct bmp280_pdd pdd;
diff --git a/hw/drivers/sensors/bmp280/pkg.yml b/hw/drivers/sensors/bmp280/pkg.yml
index 9afd46acfd..2379ec4d05 100644
--- a/hw/drivers/sensors/bmp280/pkg.yml
+++ b/hw/drivers/sensors/bmp280/pkg.yml
@@ -32,9 +32,11 @@ pkg.deps:
     - "@apache-mynewt-core/kernel/os"
     - "@apache-mynewt-core/hw/hal"
     - "@apache-mynewt-core/hw/sensor"
-    - "@apache-mynewt-core/hw/util/i2cn"
     - "@apache-mynewt-core/sys/log/modlog"
 
+#pkg.deps.!BUS_DRIVER_PRESENT:
+#    - "@apache-mynewt-core/hw/util/i2cn"
+
 pkg.req_apis:
     - stats
 
diff --git a/hw/drivers/sensors/bmp280/src/bmp280.c b/hw/drivers/sensors/bmp280/src/bmp280.c
index 83e24b0779..10e331ce79 100644
--- a/hw/drivers/sensors/bmp280/src/bmp280.c
+++ b/hw/drivers/sensors/bmp280/src/bmp280.c
@@ -206,7 +206,7 @@ bmp280_compensate_temperature(int32_t rawtemp, struct bmp280_pdd *pdd)
 {
     double var1, var2, comptemp;
 
-    if (rawtemp == 0x800000) {
+    if (rawtemp == 0x80000) {
         BMP280_LOG(ERROR, "Invalid temp data\n");
         STATS_INC(g_bmp280stats, invalid_data_errors);
         return NAN;
@@ -241,7 +241,7 @@ bmp280_compensate_pressure(struct sensor_itf *itf, int32_t rawpress,
     double var1, var2, compp;
     int32_t temp;
 
-    if (rawpress == 0x800000) {
+    if (rawpress == 0x80000) {
         BMP280_LOG(ERROR, "Invalid press data\n");
         STATS_INC(g_bmp280stats, invalid_data_errors);
         return NAN;
@@ -292,14 +292,12 @@ bmp280_compensate_temperature(int32_t rawtemp, struct bmp280_pdd *pdd)
 {
     int32_t var1, var2, comptemp;
 
-    if (rawtemp == 0x800000) {
+    if (rawtemp == 0x80000) {
         BMP280_LOG(ERROR, "Invalid temp data\n");
         STATS_INC(g_bmp280stats, invalid_data_errors);
         return NAN;
     }
 
-    rawtemp >>= 4;
-
     var1 = ((((rawtemp>>3) - ((int32_t)pdd->bcd.bcd_dig_T1 <<1))) *
             ((int32_t)pdd->bcd.bcd_dig_T2)) >> 11;
 
@@ -330,7 +328,7 @@ bmp280_compensate_pressure(struct sensor_itf *itf, int32_t rawpress,
     int64_t var1, var2, p;
     int32_t temp;
 
-    if (rawpress == 0x800000) {
+    if (rawpress == 0x80000) {
         BMP280_LOG(ERROR, "Invalid pressure data\n");
         STATS_INC(g_bmp280stats, invalid_data_errors);
         return NAN;
@@ -342,8 +340,6 @@ bmp280_compensate_pressure(struct sensor_itf *itf, int32_t rawpress,
         }
     }
 
-    rawpress >>= 4;
-
     var1 = ((int64_t)pdd->t_fine) - 128000;
     var2 = var1 * var1 * (int64_t)pdd->bcd.bcd_dig_P6;
     var2 = var2 + ((int64_t)(var1*(int64_t)pdd->bcd.bcd_dig_P5) << 17);
@@ -409,41 +405,43 @@ bmp280_sensor_read(struct sensor *sensor, sensor_type_t type,
 
     rawtemp = rawpress = 0;
 
-    /* Get a new pressure sample */
+    /* Temperature is always needed */
+    rc = bmp280_get_temperature(itf, &rawtemp);
+    if (rc) {
+        goto err;
+    }
+    databuf.std.std_temp = bmp280_compensate_temperature(rawtemp, &(bmp280->pdd));
+
+
+    /* Get a new pressure sample if needed */
     if (type & SENSOR_TYPE_PRESSURE) {
         rc = bmp280_get_pressure(itf, &rawpress);
         if (rc) {
             goto err;
         }
+    }
 
-        databuf.spd.spd_press = bmp280_compensate_pressure(itf, rawpress, &(bmp280->pdd));
-
-        if (databuf.spd.spd_press != NAN) {
-            databuf.spd.spd_press_is_valid = 1;
+    if (type & SENSOR_TYPE_AMBIENT_TEMPERATURE) {
+        if (databuf.std.std_temp != NAN) {
+            databuf.std.std_temp_is_valid = 1;
         }
 
         /* Call data function */
-        rc = data_func(sensor, data_arg, &databuf.spd, SENSOR_TYPE_PRESSURE);
+        rc = data_func(sensor, data_arg, &databuf.std, SENSOR_TYPE_AMBIENT_TEMPERATURE);
         if (rc) {
             goto err;
         }
     }
 
-    /* Get a new temperature sample */
-    if (type & SENSOR_TYPE_AMBIENT_TEMPERATURE) {
-        rc = bmp280_get_temperature(itf, &rawtemp);
-        if (rc) {
-            goto err;
-        }
-
-        databuf.std.std_temp = bmp280_compensate_temperature(rawtemp, &(bmp280->pdd));
+    if (type & SENSOR_TYPE_PRESSURE) {
+        databuf.spd.spd_press = bmp280_compensate_pressure(itf, rawpress, &(bmp280->pdd));
 
-        if (databuf.std.std_temp != NAN) {
-            databuf.std.std_temp_is_valid = 1;
+        if (databuf.spd.spd_press != NAN) {
+            databuf.spd.spd_press_is_valid = 1;
         }
 
         /* Call data function */
-        rc = data_func(sensor, data_arg, &databuf.std, SENSOR_TYPE_AMBIENT_TEMPERATURE);
+        rc = data_func(sensor, data_arg, &databuf.spd, SENSOR_TYPE_PRESSURE);
         if (rc) {
             goto err;
         }
@@ -996,13 +994,9 @@ bmp280_get_temperature(struct sensor_itf *itf, int32_t *temp)
         goto err;
     }
 
-#if MYNEWT_VAL(BMP280_SPEC_CALC)
     *temp = (int32_t)((((uint32_t)(tmp[0])) << 12) |
                       (((uint32_t)(tmp[1])) <<  4) |
                        ((uint32_t)tmp[2] >> 4));
-#else
-    *temp = ((tmp[0] << 16) | (tmp[1] << 8) | tmp[2]);
-#endif
 
     return 0;
 err:
@@ -1027,13 +1021,9 @@ bmp280_get_pressure(struct sensor_itf *itf, int32_t *press)
         goto err;
     }
 
-#if MYNEWT_VAL(BMP280_SPEC_CALC)
     *press = (int32_t)((((uint32_t)(tmp[0])) << 12) |
                       (((uint32_t)(tmp[1])) <<  4)  |
                        ((uint32_t)tmp[2] >> 4));
-#else
-    *press = ((tmp[0] << 16) | (tmp[1] << 8) | tmp[2]);
-#endif
 
     return 0;
 err:
diff --git a/hw/drivers/sensors/bmp280/syscfg.yml b/hw/drivers/sensors/bmp280/syscfg.yml
index ca90ade0e7..8265acddad 100644
--- a/hw/drivers/sensors/bmp280/syscfg.yml
+++ b/hw/drivers/sensors/bmp280/syscfg.yml
@@ -37,7 +37,9 @@ syscfg.defs:
         description: 'Enable shell support for the BMP280'
         value: 0
     BMP280_SPEC_CALC:
-        description: 'BMP280 Spec calculation instead of built in one'
+        description:
+            When set to 1 compensation functions use double precission floating point arithmetic recomended by specification.
+            When set to 0 compensation functions use integer arithmetic.
         value : 1
     BMP280_ITF_LOCK_TMO:
         description: 'BMP280 interface lock timeout in milliseconds'


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services