Fri Oct 29 23:23:33 2021 UTC ()
Correct an off by one degree error in the temperature conversion.


(brad)
diff -r1.1 -r1.2 src/sys/dev/i2c/sht4x.c

cvs diff -r1.1 -r1.2 src/sys/dev/i2c/sht4x.c (expand / switch to unified diff)

--- src/sys/dev/i2c/sht4x.c 2021/10/03 17:27:02 1.1
+++ src/sys/dev/i2c/sht4x.c 2021/10/29 23:23:33 1.2
@@ -1,33 +1,33 @@ @@ -1,33 +1,33 @@
1/* $NetBSD: sht4x.c,v 1.1 2021/10/03 17:27:02 brad Exp $ */ 1/* $NetBSD: sht4x.c,v 1.2 2021/10/29 23:23:33 brad Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2021 Brad Spencer <brad@anduin.eldar.org> 4 * Copyright (c) 2021 Brad Spencer <brad@anduin.eldar.org>
5 * 5 *
6 * Permission to use, copy, modify, and distribute this software for any 6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above 7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies. 8 * copyright notice and this permission notice appear in all copies.
9 * 9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */ 17 */
18 18
19#include <sys/cdefs.h> 19#include <sys/cdefs.h>
20__KERNEL_RCSID(0, "$NetBSD: sht4x.c,v 1.1 2021/10/03 17:27:02 brad Exp $"); 20__KERNEL_RCSID(0, "$NetBSD: sht4x.c,v 1.2 2021/10/29 23:23:33 brad Exp $");
21 21
22/* 22/*
23 Driver for the Sensirion SHT40/SHT41/SHT45 23 Driver for the Sensirion SHT40/SHT41/SHT45
24*/ 24*/
25 25
26#include <sys/param.h> 26#include <sys/param.h>
27#include <sys/systm.h> 27#include <sys/systm.h>
28#include <sys/kernel.h> 28#include <sys/kernel.h>
29#include <sys/device.h> 29#include <sys/device.h>
30#include <sys/module.h> 30#include <sys/module.h>
31#include <sys/sysctl.h> 31#include <sys/sysctl.h>
32#include <sys/mutex.h> 32#include <sys/mutex.h>
33 33
@@ -713,27 +713,27 @@ sht4x_refresh(struct sysmon_envsys * sme @@ -713,27 +713,27 @@ sht4x_refresh(struct sysmon_envsys * sme
713 device_xname(sc->sc_dev), error)); 713 device_xname(sc->sc_dev), error));
714 goto out; 714 goto out;
715 } 715 }
716 716
717 /* 717 /*
718 The documented conversion calculations for the raw values are as follows: 718 The documented conversion calculations for the raw values are as follows:
719 719
720 %RH = (-6 + 125 * rawvalue / 65535) 720 %RH = (-6 + 125 * rawvalue / 65535)
721 721
722 T in Celsius = (-45 + 175 * rawvalue / 65535) 722 T in Celsius = (-45 + 175 * rawvalue / 65535)
723 723
724 It follows then: 724 It follows then:
725 725
726 T in Kelvin = (229.15 + 175 * rawvalue / 65535) 726 T in Kelvin = (228.15 + 175 * rawvalue / 65535)
727 727
728 given the relationship between Celsius and Kelvin. 728 given the relationship between Celsius and Kelvin.
729 729
730 What follows reorders the calculation a bit and scales it up to avoid 730 What follows reorders the calculation a bit and scales it up to avoid
731 the use of any floating point. All that would really have to happen 731 the use of any floating point. All that would really have to happen
732 is a scale up to 10^6 for the sysenv framework, which wants 732 is a scale up to 10^6 for the sysenv framework, which wants
733 temperature in micro-kelvin and percent relative humidity scaled up 733 temperature in micro-kelvin and percent relative humidity scaled up
734 10^6, but since this conversion uses 64 bits due to intermediate 734 10^6, but since this conversion uses 64 bits due to intermediate
735 values that are bigger than 32 bits the conversion first scales up to 735 values that are bigger than 32 bits the conversion first scales up to
736 10^9 and the scales back down by 10^3 at the end. This preserves some 736 10^9 and the scales back down by 10^3 at the end. This preserves some
737 precision in the conversion that would otherwise be lost. 737 precision in the conversion that would otherwise be lost.
738 */ 738 */
739 739
@@ -761,27 +761,27 @@ sht4x_refresh(struct sysmon_envsys * sme @@ -761,27 +761,27 @@ sht4x_refresh(struct sysmon_envsys * sme
761 uint8_t *svalptr; 761 uint8_t *svalptr;
762 uint64_t svalue; 762 uint64_t svalue;
763 int64_t v1; 763 int64_t v1;
764 uint64_t v2; 764 uint64_t v2;
765 uint64_t d1 = 65535; 765 uint64_t d1 = 65535;
766 uint64_t mul1; 766 uint64_t mul1;
767 uint64_t mul2; 767 uint64_t mul2;
768 uint64_t div1 = 10000; 768 uint64_t div1 = 10000;
769 uint64_t q; 769 uint64_t q;
770 770
771 switch (edata->sensor) { 771 switch (edata->sensor) {
772 case SHT4X_TEMP_SENSOR: 772 case SHT4X_TEMP_SENSOR:
773 svalptr = &rawdata[0]; 773 svalptr = &rawdata[0];
774 v1 = 22915; /* this is scaled up already from 229.15 */ 774 v1 = 22815; /* this is scaled up already from 228.15 */
775 v2 = 175; 775 v2 = 175;
776 mul1 = 10000000000; 776 mul1 = 10000000000;
777 mul2 = 100000000; 777 mul2 = 100000000;
778 break; 778 break;
779 case SHT4X_HUMIDITY_SENSOR: 779 case SHT4X_HUMIDITY_SENSOR:
780 svalptr = &rawdata[3]; 780 svalptr = &rawdata[3];
781 v1 = -6; 781 v1 = -6;
782 v2 = 125; 782 v2 = 125;
783 mul1 = 10000000000; 783 mul1 = 10000000000;
784 mul2 = 10000000000; 784 mul2 = 10000000000;
785 break; 785 break;
786 default: 786 default:
787 error = EINVAL; 787 error = EINVAL;