All of lore.kernel.org
 help / color / mirror / Atom feed
From: Hendrik Hoeth <hendrik.hoeth@air-avionics.com>
To: Jonathan Cameron <jic23@kernel.org>
Cc: linux-iio@vger.kernel.org
Subject: BME-280 humidity over-/underflow
Date: Wed, 10 Jun 2020 15:45:07 +0200	[thread overview]
Message-ID: <20200610134507.GT2681@linta.de> (raw)

[-- Attachment #1: Type: text/plain, Size: 996 bytes --]

Hi,

I've discovered an over-/underflow problem for relative humidity
measurements in drivers/iio/pressure/bmp280-core.c. At very low
humidities the value measured by the sensor can fluctuate into the
negative humidity range. The kernel driver then reports this as
extremely large value (e.g. 4193085, corresponding to 4193% RH). The
same probably happens at high humidity values, but with our climate
chamber I was only able to look at the low end.

To avoid this, the reference code in the BME-280 datasheet catches
values < 0% RH and > 100% RH. These two lines are currently missing in
the kernel driver.

Attached you find a patch that fixed the problem for us. You can find
the sensor datasheet at
https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BME280-DS002.pdf
The code I'm talking about is in section 4.2.3 on page 25/26.

Thanks a lot,

   Hendrik

-- 
Garrecht Avionik GmbH | Wieslocher Str. 38 | 69190 Walldorf
https://www.air-avionics.com/
Tel.: +49 6224 98 96 999

[-- Attachment #2: bmp280-core.c.patch --]
[-- Type: text/x-diff, Size: 622 bytes --]

Fix underflow / overflow for humidity measurements

diff --git a/drivers/iio/pressure/bmp280-core.c b/drivers/iio/pressure/bmp280-core.c
index 29c209cc1108..d1f36b044f05 100644
--- a/drivers/iio/pressure/bmp280-core.c
+++ b/drivers/iio/pressure/bmp280-core.c
@@ -270,6 +270,8 @@ static u32 bmp280_compensate_humidity(struct bmp280_data *data,
 		* (((var * (s32)calib->H3) >> 11) + (s32)32768)) >> 10)
 		+ (s32)2097152) * calib->H2 + 8192) >> 14);
 	var -= ((((var >> 15) * (var >> 15)) >> 7) * (s32)calib->H1) >> 4;
+	var = ((var < 0) ? 0 : var);
+	var = ((var > 419430400) ? 419430400 : var);
 
 	return var >> 12;
 };

             reply	other threads:[~2020-06-10 13:50 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-10 13:45 Hendrik Hoeth [this message]
2020-06-10 15:26 ` BME-280 humidity over-/underflow Tomasz Duszynski
2020-06-10 15:54   ` Hendrik Hoeth

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200610134507.GT2681@linta.de \
    --to=hendrik.hoeth@air-avionics.com \
    --cc=jic23@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.