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

[mynewt-core] 03/06: bmp280/bme280: Fix pressure, humidity readout

This is an automated email from the ASF dual-hosted git repository.

jerzy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git

commit 57c46c88415af40cbb5efeb7ea8e47257e904ea3
Author: Jerzy Kasenberg <je...@codecoup.pl>
AuthorDate: Mon Dec 3 15:47:49 2018 +0100

    bmp280/bme280: Fix pressure, humidity readout
    
    If user wanted to read only pressure and/or humidity from sensor only first value
    read from the device would be correct.
    Further pressure humidity read request would use incorrect value of t_fine that
    was computed only for the first reading when  temperature was actually read.
    
    Even if temperature reading was requested, pressure values would be incorrect,
    since temperature used for pressure compensation was taken from previous
    read. For slow rate readings pressure would be always calculated with
    old values from temperature.
    
    Now temperature is always read even if not requested by user code to make sure
    that pressure and humidity calculations are correct.
---
 hw/drivers/sensors/bme280/src/bme280.c | 41 ++++++++++++++++++----------------
 hw/drivers/sensors/bmp280/src/bmp280.c | 36 +++++++++++++++--------------
 2 files changed, 41 insertions(+), 36 deletions(-)

diff --git a/hw/drivers/sensors/bme280/src/bme280.c b/hw/drivers/sensors/bme280/src/bme280.c
index 7c0f855..1e8eda2 100644
--- a/hw/drivers/sensors/bme280/src/bme280.c
+++ b/hw/drivers/sensors/bme280/src/bme280.c
@@ -489,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;
         }
@@ -529,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) {
diff --git a/hw/drivers/sensors/bmp280/src/bmp280.c b/hw/drivers/sensors/bmp280/src/bmp280.c
index 85bca22..10e331c 100644
--- a/hw/drivers/sensors/bmp280/src/bmp280.c
+++ b/hw/drivers/sensors/bmp280/src/bmp280.c
@@ -405,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;
         }