Source-Changes-HG archive

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

[src/trunk]: src/sys/modules/aht20temp A driver for the Aosong AHT20 temperat...



details:   https://anonhg.NetBSD.org/src/rev/8e596dacf1c9
branches:  trunk
changeset: 372360:8e596dacf1c9
user:      brad <brad%NetBSD.org@localhost>
date:      Thu Nov 17 19:20:05 2022 +0000

description:
A driver for the Aosong AHT20 temperature and humidity sensor.  While
slow for an I2C sensor it is inexpensive and should work well enough
in most indoor conditions.  All features of the chip are supported.

diffstat:

 distrib/sets/lists/debug/module.mi     |    4 +-
 distrib/sets/lists/man/mi              |    5 +-
 distrib/sets/lists/modules/mi          |    4 +-
 share/man/man4/Makefile                |    4 +-
 share/man/man4/aht20temp.4             |   76 ++++
 sys/dev/i2c/aht20.c                    |  561 +++++++++++++++++++++++++++++++++
 sys/dev/i2c/aht20reg.h                 |   37 ++
 sys/dev/i2c/aht20var.h                 |   50 ++
 sys/dev/i2c/files.i2c                  |    7 +-
 sys/modules/Makefile                   |    3 +-
 sys/modules/aht20temp/Makefile         |   11 +
 sys/modules/aht20temp/aht20temp.ioconf |    7 +
 12 files changed, 762 insertions(+), 7 deletions(-)

diffs (truncated from 886 to 300 lines):

diff -r bb6678e0aaf9 -r 8e596dacf1c9 distrib/sets/lists/debug/module.mi
--- a/distrib/sets/lists/debug/module.mi        Thu Nov 17 18:57:16 2022 +0000
+++ b/distrib/sets/lists/debug/module.mi        Thu Nov 17 19:20:05 2022 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: module.mi,v 1.20 2022/06/04 03:32:04 pgoyette Exp $
+# $NetBSD: module.mi,v 1.21 2022/11/17 19:20:06 brad Exp $
 ./usr/libdata/debug/@MODULEDIR@                                        modules-base-kernel     kmod,debug
 ./usr/libdata/debug/@MODULEDIR@/accf_dataready                 modules-base-kernel     kmod,debug
 ./usr/libdata/debug/@MODULEDIR@/accf_dataready/accf_dataready.kmod.debug       modules-base-kernel     kmod,debug
@@ -8,6 +8,8 @@
 ./usr/libdata/debug/@MODULEDIR@/adiantum/adiantum.kmod.debug   modules-base-kernel     kmod,debug
 ./usr/libdata/debug/@MODULEDIR@/adosfs                         modules-base-kernel     kmod,debug
 ./usr/libdata/debug/@MODULEDIR@/adosfs/adosfs.kmod.debug               modules-base-kernel     kmod,debug
+./usr/libdata/debug/@MODULEDIR@/aht20temp                              modules-base-kernel     kmod,debug
+./usr/libdata/debug/@MODULEDIR@/aht20temp/aht20temp.kmod.debug         modules-base-kernel     kmod,debug
 ./usr/libdata/debug/@MODULEDIR@/aio                            modules-base-kernel     kmod,debug
 ./usr/libdata/debug/@MODULEDIR@/aio/aio.kmod.debug                     modules-base-kernel     kmod,debug
 ./usr/libdata/debug/@MODULEDIR@/am2315temp                     modules-base-kernel     kmod,debug
diff -r bb6678e0aaf9 -r 8e596dacf1c9 distrib/sets/lists/man/mi
--- a/distrib/sets/lists/man/mi Thu Nov 17 18:57:16 2022 +0000
+++ b/distrib/sets/lists/man/mi Thu Nov 17 19:20:05 2022 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.1754 2022/08/27 21:53:38 dholland Exp $
+# $NetBSD: mi,v 1.1755 2022/11/17 19:20:06 brad Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -781,6 +781,7 @@
 ./usr/share/man/cat4/ahci.0                    man-sys-catman          .cat
 ./usr/share/man/cat4/ahcisata.0                        man-sys-catman          .cat
 ./usr/share/man/cat4/ahd.0                     man-sys-catman          .cat
+./usr/share/man/cat4/aht20temp.0               man-sys-catman          .cat
 ./usr/share/man/cat4/ai.0                      man-sys-catman          .cat
 ./usr/share/man/cat4/aiboost.0                 man-sys-catman          .cat
 ./usr/share/man/cat4/aibs.0                    man-sys-catman          .cat
@@ -4088,6 +4089,7 @@
 ./usr/share/man/html4/ahci.html                        man-sys-htmlman         html
 ./usr/share/man/html4/ahcisata.html            man-sys-htmlman         html
 ./usr/share/man/html4/ahd.html                 man-sys-htmlman         html
+./usr/share/man/html4/aht20temp.html           man-sys-htmlman         html
 ./usr/share/man/html4/ai.html                  man-sys-htmlman         html
 ./usr/share/man/html4/aiboost.html             man-sys-htmlman         html
 ./usr/share/man/html4/aibs.html                        man-sys-htmlman         html
@@ -7089,6 +7091,7 @@
 ./usr/share/man/man4/ahci.4                    man-sys-man             .man
 ./usr/share/man/man4/ahcisata.4                        man-sys-man             .man
 ./usr/share/man/man4/ahd.4                     man-sys-man             .man
+./usr/share/man/man4/aht20temp.4               man-sys-man             .man
 ./usr/share/man/man4/ai.4                      man-sys-man             .man
 ./usr/share/man/man4/aiboost.4                 man-sys-man             .man
 ./usr/share/man/man4/aibs.4                    man-sys-man             .man
diff -r bb6678e0aaf9 -r 8e596dacf1c9 distrib/sets/lists/modules/mi
--- a/distrib/sets/lists/modules/mi     Thu Nov 17 18:57:16 2022 +0000
+++ b/distrib/sets/lists/modules/mi     Thu Nov 17 19:20:05 2022 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.154 2022/06/04 03:32:04 pgoyette Exp $
+# $NetBSD: mi,v 1.155 2022/11/17 19:20:06 brad Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -13,6 +13,8 @@
 ./@MODULEDIR@/adosfs/adosfs.kmod               modules-base-kernel     kmod
 ./@MODULEDIR@/adiantum                         modules-base-kernel     kmod
 ./@MODULEDIR@/adiantum/adiantum.kmod           modules-base-kernel     kmod
+./@MODULEDIR@/aht20temp                                modules-base-kernel     kmod
+./@MODULEDIR@/aht20temp/aht20temp.kmod         modules-base-kernel     kmod
 ./@MODULEDIR@/aio                              modules-base-kernel     kmod
 ./@MODULEDIR@/aio/aio.kmod                     modules-base-kernel     kmod
 ./@MODULEDIR@/am2315temp                       modules-base-kernel     kmod
diff -r bb6678e0aaf9 -r 8e596dacf1c9 share/man/man4/Makefile
--- a/share/man/man4/Makefile   Thu Nov 17 18:57:16 2022 +0000
+++ b/share/man/man4/Makefile   Thu Nov 17 19:20:05 2022 +0000
@@ -1,10 +1,10 @@
-#      $NetBSD: Makefile,v 1.728 2022/08/12 11:15:41 riastradh Exp $
+#      $NetBSD: Makefile,v 1.729 2022/11/17 19:20:05 brad Exp $
 #      @(#)Makefile    8.1 (Berkeley) 6/18/93
 
 MAN=   aac.4 ac97.4 acardide.4 aceride.4 acphy.4 \
        adbbt.4 adbkbd.4 adbms.4 \
        adc.4 adm1026hm.4 admtemp.4 adv.4 adw.4 age.4 agp.4 agr.4 ahb.4 ahc.4 \
-       ahcisata.4 ahd.4 aibs.4 alc.4 ale.4 alipm.4 altmem.4 altq.4 \
+       ahcisata.4 ahd.4 aht20temp.4 aibs.4 alc.4 ale.4 alipm.4 altmem.4 altq.4 \
        am2315temp.4 amdpm.4 amdtemp.4 amhphy.4 amr.4 aps.4 asus.4 \
        an.4 aq.4 arcmsr.4 arcofi.4 aria.4 artsata.4 ata.4 atalk.4 ataraid.4 \
        ath.4 athn.4 atphy.4 atppc.4 attimer.4 atw.4 \
diff -r bb6678e0aaf9 -r 8e596dacf1c9 share/man/man4/aht20temp.4
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/share/man/man4/aht20temp.4        Thu Nov 17 19:20:05 2022 +0000
@@ -0,0 +1,76 @@
+.\" $NetBSD: aht20temp.4,v 1.1 2022/11/17 19:20:05 brad Exp $
+.\"
+.\" Copyright (c) 2022 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 November 15, 2022
+.Dt AHT20TEMP 4
+.Os
+.Sh NAME
+.Nm aht20temp
+.Nd Driver for Guangzhou Aosong AHT20 sensor chip via I2C bus
+.Sh SYNOPSIS
+.Cd "aht20temp* at iic? addr 0x38"
+.Sh DESCRIPTION
+The
+.Nm
+driver provides measurements from the AHT20 humidity/temperature
+sensors via the
+.Xr envsys 4
+framework.
+The
+.Nm
+.Ar addr
+argument selects the address at the
+.Xr iic 4
+bus.
+The 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 Li hw.aht20temp0.ignorecrc
+If set, the crc calculation for %RH and temperature will be ignored.
+.It Li hw.aht20temp0.debug
+If the driver is compiled with
+.Dv AHT20_DEBUG ,
+this node will appear and can be used to set the debugging level.
+.It Li hw.aht20temp0.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 bb6678e0aaf9 -r 8e596dacf1c9 sys/dev/i2c/aht20.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/i2c/aht20.c       Thu Nov 17 19:20:05 2022 +0000
@@ -0,0 +1,561 @@
+/*     $NetBSD: aht20.c,v 1.1 2022/11/17 19:20:06 brad Exp $   */
+
+/*
+ * Copyright (c) 2022 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: aht20.c,v 1.1 2022/11/17 19:20:06 brad Exp $");
+
+/*
+  Driver for the Guangzhou Aosong AHT20 temperature and humidity sensor
+*/
+
+#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/aht20reg.h>
+#include <dev/i2c/aht20var.h>
+
+
+static uint8_t         aht20_crc(uint8_t *, size_t);
+static int     aht20_poke(i2c_tag_t, i2c_addr_t, bool);
+static int     aht20_match(device_t, cfdata_t, void *);
+static void    aht20_attach(device_t, device_t, void *);
+static int     aht20_detach(device_t, int);
+static void    aht20_refresh(struct sysmon_envsys *, envsys_data_t *);
+static int     aht20_verify_sysctl(SYSCTLFN_ARGS);
+
+#define AHT20_DEBUG
+#ifdef AHT20_DEBUG
+#define DPRINTF(s, l, x) \
+    do { \
+       if (l <= s->sc_aht20debug) \
+           printf x; \
+    } while (/*CONSTCOND*/0)
+#else
+#define DPRINTF(s, l, x)
+#endif
+
+CFATTACH_DECL_NEW(aht20temp, sizeof(struct aht20_sc),
+    aht20_match, aht20_attach, aht20_detach, NULL);
+
+static struct aht20_sensor aht20_sensors[] = {
+       {
+               .desc = "humidity",
+               .type = ENVSYS_SRELHUMIDITY,
+       },
+       {
+               .desc = "temperature",
+               .type = ENVSYS_STEMP,
+       }
+};
+
+/*
+ * The delays are mentioned in the datasheet for the chip, except for
+ * the get status command.
+ */
+
+static struct aht20_timing aht20_timings[] = {
+       {
+               .cmd = AHT20_INITIALIZE,
+               .typicaldelay = 10000,
+       },
+       {
+               .cmd = AHT20_TRIGGER_MEASUREMENT,
+               .typicaldelay = 80000,
+       },
+       {
+               .cmd = AHT20_GET_STATUS,
+               .typicaldelay = 5000,
+       },
+       {
+               .cmd = AHT20_SOFT_RESET,
+               .typicaldelay = 20000,
+       }
+};
+
+int
+aht20_verify_sysctl(SYSCTLFN_ARGS)
+{
+       int error, t;
+       struct sysctlnode node;
+
+       node = *rnode;
+       t = *(int *)rnode->sysctl_data;
+       node.sysctl_data = &t;
+       error = sysctl_lookup(SYSCTLFN_CALL(&node));
+       if (error || newp == NULL)
+               return error;
+
+       if (t < 0)
+               return EINVAL;
+
+       *(int *)rnode->sysctl_data = t;
+
+       return 0;
+}
+
+static int
+aht20_cmddelay(uint8_t cmd)
+{
+       int r = -1;
+
+       for(int i = 0;i < __arraycount(aht20_timings);i++) {
+               if (cmd == aht20_timings[i].cmd) {
+                       r = aht20_timings[i].typicaldelay;
+                       break;
+               }
+       }
+
+       if (r == -1) {
+               panic("Bad command look up in cmd delay: cmd: %d\n",cmd);



Home | Main Index | Thread Index | Old Index