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:40 UTC

[mynewt-core] 02/06: bmp280/bme280: Cleanup temperature and pressure reading function

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 2bcc631747b581620c4a4b536b316a8d612b4646
Author: Jerzy Kasenberg <je...@codecoup.pl>
AuthorDate: Mon Dec 3 15:36:51 2018 +0100

    bmp280/bme280: Cleanup temperature and pressure reading function
    
    There was unnecessary conditional code in functions:
    bmp280_get_temperature(), bmp280_get_pressure().
    It made functions return different values depending on compilation flag
    one version returned 20 bits values shifted by 4 bits left.
    Later on, one set of compensation functions shifted raw values anyway.
    
    Compensation functions that uses double float arithmetic also compared
    invalid value from sensor with incorrect (24 bit value) value 0x800000,
    instead of 0x80000 (20 bit value).
    Code was correct for 32 bits arithmetic because shifting was done after
    check and input value was 24 bit value.
---
 hw/drivers/sensors/bme280/src/bme280.c | 22 +++-------------------
 hw/drivers/sensors/bmp280/src/bmp280.c | 20 ++++----------------
 2 files changed, 7 insertions(+), 35 deletions(-)

diff --git a/hw/drivers/sensors/bme280/src/bme280.c b/hw/drivers/sensors/bme280/src/bme280.c
index 689368c..7c0f855 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);
@@ -974,13 +970,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:
@@ -1004,11 +996,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:
@@ -1033,13 +1021,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/bmp280/src/bmp280.c b/hw/drivers/sensors/bmp280/src/bmp280.c
index 83e24b0..85bca22 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);
@@ -996,13 +992,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 +1019,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: