All of lore.kernel.org
 help / color / mirror / Atom feed
* BME-280 humidity over-/underflow
@ 2020-06-10 13:45 Hendrik Hoeth
  2020-06-10 15:26 ` Tomasz Duszynski
  0 siblings, 1 reply; 3+ messages in thread
From: Hendrik Hoeth @ 2020-06-10 13:45 UTC (permalink / raw)
  To: Jonathan Cameron; +Cc: linux-iio

[-- 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;
 };

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-06-10 15:54 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-06-10 13:45 BME-280 humidity over-/underflow Hendrik Hoeth
2020-06-10 15:26 ` Tomasz Duszynski
2020-06-10 15:54   ` Hendrik Hoeth

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.