Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/modules/sht4xtemp A driver for the Sensirion SHT40/SHT41...



details:   https://anonhg.NetBSD.org/src/rev/4a48021b5364
branches:  trunk
changeset: 1023930:4a48021b5364
user:      brad <brad%NetBSD.org@localhost>
date:      Sun Oct 03 17:27:02 2021 +0000

description:
A driver for the Sensirion SHT40/SHT41/SHT45 temperature and humidity
sensor.  An example of this chip is:

https://www.adafruit.com/product/4885

This is a lower cost chip that provides higher then usual precision
according to the data sheet.  This driver supports all of the published
functions that the chip has.

diffstat:

 distrib/sets/lists/man/mi              |    5 +-
 distrib/sets/lists/modules/mi          |    4 +-
 share/man/man4/Makefile                |    4 +-
 share/man/man4/sht4xtemp.4             |   93 +++
 sys/dev/i2c/files.i2c                  |    7 +-
 sys/dev/i2c/sht4x.c                    |  886 +++++++++++++++++++++++++++++++++
 sys/dev/i2c/sht4xreg.h                 |   44 +
 sys/dev/i2c/sht4xvar.h                 |   72 ++
 sys/modules/Makefile                   |    3 +-
 sys/modules/sht4xtemp/Makefile         |   11 +
 sys/modules/sht4xtemp/sht4xtemp.ioconf |    7 +
 11 files changed, 1130 insertions(+), 6 deletions(-)

diffs (truncated from 1244 to 300 lines):

diff -r 26f8d58bf23c -r 4a48021b5364 distrib/sets/lists/man/mi
--- a/distrib/sets/lists/man/mi Sat Oct 02 22:14:32 2021 +0000
+++ b/distrib/sets/lists/man/mi Sun Oct 03 17:27:02 2021 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.1725 2021/08/01 21:56:26 andvar Exp $
+# $NetBSD: mi,v 1.1726 2021/10/03 17:27:02 brad Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -1723,6 +1723,7 @@
 ./usr/share/man/cat4/shb.0                     man-sys-catman          .cat
 ./usr/share/man/cat4/shmif.0                   man-sys-catman          .cat
 ./usr/share/man/cat4/shpcic.0                  man-sys-catman          .cat
+./usr/share/man/cat4/sht4xtemp.0               man-sys-catman          .cat
 ./usr/share/man/cat4/si.0                      man-sys-catman          .cat
 ./usr/share/man/cat4/si70xxtemp.0              man-sys-catman          .cat
 ./usr/share/man/cat4/siisata.0                 man-sys-catman          .cat
@@ -4896,6 +4897,7 @@
 ./usr/share/man/html4/shb.html                 man-sys-htmlman         html
 ./usr/share/man/html4/shmif.html               man-sys-htmlman         html
 ./usr/share/man/html4/shpcic.html              man-sys-htmlman         html
+./usr/share/man/html4/sht4xtemp.html           man-sys-htmlman         html
 ./usr/share/man/html4/si.html                  man-sys-htmlman         html
 ./usr/share/man/html4/si70xxtemp.html          man-sys-htmlman         html
 ./usr/share/man/html4/siisata.html             man-sys-htmlman         html
@@ -7975,6 +7977,7 @@
 ./usr/share/man/man4/shb.4                     man-sys-man             .man
 ./usr/share/man/man4/shmif.4                   man-sys-man             .man
 ./usr/share/man/man4/shpcic.4                  man-sys-man             .man
+./usr/share/man/man4/sht4xtemp.4               man-sys-man             .man
 ./usr/share/man/man4/si.4                      man-sys-man             .man
 ./usr/share/man/man4/si70xxtemp.4              man-sys-man             .man
 ./usr/share/man/man4/siisata.4                 man-sys-man             .man
diff -r 26f8d58bf23c -r 4a48021b5364 distrib/sets/lists/modules/mi
--- a/distrib/sets/lists/modules/mi     Sat Oct 02 22:14:32 2021 +0000
+++ b/distrib/sets/lists/modules/mi     Sun Oct 03 17:27:02 2021 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.146 2021/09/11 16:10:37 pgoyette Exp $
+# $NetBSD: mi,v 1.147 2021/10/03 17:27:02 brad Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -397,6 +397,8 @@
 ./@MODULEDIR@/securelevel/securelevel.kmod     modules-base-kernel     kmod
 ./@MODULEDIR@/sequencer                                modules-base-kernel     kmod
 ./@MODULEDIR@/sequencer/sequencer.kmod         modules-base-kernel     kmod
+./@MODULEDIR@/sht4xtemp                                modules-base-kernel     kmod
+./@MODULEDIR@/sht4xtemp/sht4xtemp.kmod         modules-base-kernel     kmod
 ./@MODULEDIR@/si70xxtemp                       modules-base-kernel     kmod
 ./@MODULEDIR@/si70xxtemp/si70xxtemp.kmod       modules-base-kernel     kmod
 ./@MODULEDIR@/skipjack                         modules-base-kernel     kmod
diff -r 26f8d58bf23c -r 4a48021b5364 share/man/man4/Makefile
--- a/share/man/man4/Makefile   Sat Oct 02 22:14:32 2021 +0000
+++ b/share/man/man4/Makefile   Sun Oct 03 17:27:02 2021 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile,v 1.715 2021/08/01 21:56:27 andvar Exp $
+#      $NetBSD: Makefile,v 1.716 2021/10/03 17:27:02 brad Exp $
 #      @(#)Makefile    8.1 (Berkeley) 6/18/93
 
 MAN=   aac.4 ac97.4 acardide.4 aceride.4 acphy.4 \
@@ -56,7 +56,7 @@
        rnd.4 route.4 rs5c372rtc.4 rtk.4 rtsx.4 rtw.4 rtwn.4 rum.4 run.4 \
        s390rtc.4 satalink.4 sbus.4 schide.4 \
        scsi.4 sctp.4 sd.4 se.4 seeprom.4 sem.4 \
-       ses.4 sf.4 sfb.4 sgsmix.4 shb.4 shmif.4 shpcic.4 si70xxtemp.4 \
+       ses.4 sf.4 sfb.4 sgsmix.4 shb.4 shmif.4 shpcic.4 sht4xtemp.4 si70xxtemp.4 \
        siisata.4 siop.4 sip.4 siside.4 sk.4 sl.4 slide.4 \
        sm.4 smscphy.4 smsh.4 sn.4 sony.4 spc.4 speaker.4 spif.4 sqphy.4 \
        srt.4 ss.4 \
diff -r 26f8d58bf23c -r 4a48021b5364 share/man/man4/sht4xtemp.4
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/share/man/man4/sht4xtemp.4        Sun Oct 03 17:27:02 2021 +0000
@@ -0,0 +1,93 @@
+.\" $NetBSD: sht4xtemp.4,v 1.1 2021/10/03 17:27:02 brad Exp $
+.\"
+.\" Copyright (c) 2021 Brad Spencer <brad%anduin.eldar.org@localhost>
+.\"
+.\" Permission to use, copy, modify, and distribute this software for any
+.\" purpose with or without fee is hereby granted, provided that the above
+.\" copyright notice and this permission notice appear in all copies.
+.\"
+.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+.\"
+.Dd September 28th, 2021
+.Dt SHT4XTEMP 4
+.Os
+.Sh NAME
+.Nm sht4xtemp
+.Nd Driver for Sensirion SHT40/SHT41/SHT45 sensor chip via I2C bus
+.Sh SYNOPSIS
+.Cd "sht4xtemp* at iic? addr 0x44"
+.Sh DESCRIPTION
+The
+.Nm
+driver provides measurements from the SHT40/SHT41/SHT45 humidity/temperature
+sensors via the
+.Xr envsys 4
+framework.
+The
+.Nm
+.Ar addr
+argument selects the address at the
+.Xr iic 4
+bus.
+The resolution, heater controls and crc validity can be changed through
+.Xr sysctl 8
+nodes.
+.Sh SYSCTL VARIABLES
+The following
+.Xr sysctl 3
+variables are provided:
+.Bl -tag -width indent
+.It hw.sht4xtemp0.resolutions
+Lists the resolutions supported by the driver and chip.
+.It hw.sht4xtemp0.resolution
+Set the resolution, or number of bits, used for %RH and temperature.
+Use one of the strings listed in hw.sht4xtemp.resolutions.
+.It hw.sht4xtemp0.ignorecrc
+If set, the crc calculation for %RH and temperature will be ignored.
+.It hw.sht4xtemp0.heateron
+Turn the heater on and off.  Please note that the heater is turned on right
+before the measurement and runs for a pulse width of time.  Then the measurement
+is taken and the heater is turned off.  There is no way to keep the heater running
+with this chip.
+.It hw.sht4xtemp0.heaterstrength
+From 1 to 3, the amount of energy put into the heater.
+The higher the number, the more power used.
+.It hw.sht4xtemp0.heaterpulses
+Lists the valid heater pulses supported by the driver and chip.
+.It hw.sht4xtemp0.heaterpulse
+Set the heater pulse length.  Use one of the strings listed in
+hw.sht4xtemp.heaterpulses
+.It hw.sht4xtemp0.debug
+If the driver is compiled with
+.Dv SI70XX_DEBUG ,
+this node will appear and can be used to set the debugging level.
+.It hw.sht4xtemp0.readattempts
+To read %RH or temperature the chip requires that the command be sent,
+then a delay must be observed before a read can be done to get the values
+back.  The delays are documented in the datasheet for the chip.
+The driver will attempt to read back the values readattempts number of
+times.
+The default is 10 which should be more than enough for most purposes.
+.El
+.Sh SEE ALSO
+.Xr envsys 4 ,
+.Xr iic 4 ,
+.Xr envstat 8 ,
+.Xr sysctl 8
+.Sh HISTORY
+The
+.Nm
+driver first appeared in
+.Nx 10.0 .
+.Sh AUTHORS
+.An -nosplit
+The
+.Nm
+driver was written by
+.An Brad Spencer Aq Mt brad%anduin.eldar.org@localhost .
diff -r 26f8d58bf23c -r 4a48021b5364 sys/dev/i2c/files.i2c
--- a/sys/dev/i2c/files.i2c     Sat Oct 02 22:14:32 2021 +0000
+++ b/sys/dev/i2c/files.i2c     Sun Oct 03 17:27:02 2021 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: files.i2c,v 1.116 2021/07/27 20:23:41 macallan Exp $
+#      $NetBSD: files.i2c,v 1.117 2021/10/03 17:27:02 brad Exp $
 
 obsolete defflag       opt_i2cbus.h            I2C_SCAN
 define i2cbus { }
@@ -395,6 +395,11 @@
 attach cwfg at iic
 file   dev/i2c/cwfg.c                          cwfg
 
+# Sensirion SHT40/SHT41/SHT45 Temperature and Humidity sensor
+device sht4xtemp
+attach sht4xtemp at iic
+file dev/i2c/sht4x.c                           sht4xtemp
+
 # Philips PCA955x GPIO
 device pcagpio: leds
 attach pcagpio at iic
diff -r 26f8d58bf23c -r 4a48021b5364 sys/dev/i2c/sht4x.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/i2c/sht4x.c       Sun Oct 03 17:27:02 2021 +0000
@@ -0,0 +1,886 @@
+/*     $NetBSD: sht4x.c,v 1.1 2021/10/03 17:27:02 brad Exp $   */
+
+/*
+ * Copyright (c) 2021 Brad Spencer <brad%anduin.eldar.org@localhost>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: sht4x.c,v 1.1 2021/10/03 17:27:02 brad Exp $");
+
+/*
+  Driver for the Sensirion SHT40/SHT41/SHT45
+*/
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/device.h>
+#include <sys/module.h>
+#include <sys/sysctl.h>
+#include <sys/mutex.h>
+
+#include <dev/sysmon/sysmonvar.h>
+#include <dev/i2c/i2cvar.h>
+#include <dev/i2c/sht4xreg.h>
+#include <dev/i2c/sht4xvar.h>
+
+
+static uint8_t         sht4x_crc(uint8_t *, size_t);
+static int     sht4x_poke(i2c_tag_t, i2c_addr_t, bool);
+static int     sht4x_match(device_t, cfdata_t, void *);
+static void    sht4x_attach(device_t, device_t, void *);
+static int     sht4x_detach(device_t, int);
+static void    sht4x_refresh(struct sysmon_envsys *, envsys_data_t *);
+static int     sht4x_verify_sysctl(SYSCTLFN_ARGS);
+static int     sht4x_verify_sysctl_resolution(SYSCTLFN_ARGS);
+static int     sht4x_verify_sysctl_heateron(SYSCTLFN_ARGS);
+static int     sht4x_verify_sysctl_heatervalue(SYSCTLFN_ARGS);
+static int     sht4x_verify_sysctl_heaterpulse(SYSCTLFN_ARGS);
+
+#define SHT4X_DEBUG
+#ifdef SHT4X_DEBUG
+#define DPRINTF(s, l, x) \
+    do { \
+       if (l <= s->sc_sht4xdebug) \
+           printf x; \
+    } while (/*CONSTCOND*/0)
+#else
+#define DPRINTF(s, l, x)
+#endif
+
+CFATTACH_DECL_NEW(sht4xtemp, sizeof(struct sht4x_sc),
+    sht4x_match, sht4x_attach, sht4x_detach, NULL);
+
+static struct sht4x_sensor sht4x_sensors[] = {
+       {
+               .desc = "humidity",
+               .type = ENVSYS_SRELHUMIDITY,
+       },
+       {
+               .desc = "temperature",
+               .type = ENVSYS_STEMP,
+       }
+};
+
+/* The typical delays are documented in the datasheet for the chip.
+   There is no need to be very accurate with these, just rough estimates
+   will work fine.
+*/
+
+static struct sht4x_timing sht4x_timings[] = {
+       {
+               .cmd = SHT4X_READ_SERIAL,
+               .typicaldelay = 5000,
+       },
+       {
+               .cmd = SHT4X_SOFT_RESET,
+               .typicaldelay = 1000,
+       },
+       {
+               .cmd = SHT4X_MEASURE_HIGH_PRECISION,
+               .typicaldelay = 8000,
+       },
+       {
+               .cmd = SHT4X_MEASURE_MEDIUM_PRECISION,
+               .typicaldelay = 4000,
+       },
+       {
+               .cmd = SHT4X_MEASURE_LOW_PRECISION,
+               .typicaldelay = 2000,
+       },
+       {
+               .cmd = SHT4X_MEASURE_HIGH_PRECISION_HIGH_HEAT_1_S,
+               .typicaldelay = 1000000,
+       },
+       {
+               .cmd = SHT4X_MEASURE_HIGH_PRECISION_MEDIUM_HEAT_1_S,
+               .typicaldelay = 1000000,



Home | Main Index | Thread Index | Old Index