Source-Changes-HG archive

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

[src/trunk]: src Use kpause() instead of delay() in the measurement cycle. T...



details:   https://anonhg.NetBSD.org/src/rev/0f3383a1046e
branches:  trunk
changeset: 372413:0f3383a1046e
user:      brad <brad%NetBSD.org@localhost>
date:      Wed Nov 23 23:45:29 2022 +0000

description:
Use kpause() instead of delay() in the measurement cycle.  Try and
derive the proper wait delay for the measurement based upon the over
sampling setting and allow the wait factor multiplier to be adjusted.

diffstat:

 share/man/man4/bmx280thp.4 |  11 +++++++-
 sys/dev/i2c/bmx280.c       |  61 ++++++++++++++++++++++++++++++++++++++++-----
 sys/dev/i2c/bmx280var.h    |   5 +++-
 3 files changed, 68 insertions(+), 9 deletions(-)

diffs (155 lines):

diff -r d4f8f6426c31 -r 0f3383a1046e share/man/man4/bmx280thp.4
--- a/share/man/man4/bmx280thp.4        Wed Nov 23 19:20:34 2022 +0000
+++ b/share/man/man4/bmx280thp.4        Wed Nov 23 23:45:29 2022 +0000
@@ -1,4 +1,4 @@
-.\" $NetBSD: bmx280thp.4,v 1.2 2022/11/21 23:51:10 wiz Exp $
+.\" $NetBSD: bmx280thp.4,v 1.3 2022/11/23 23:45:30 brad Exp $
 .\"
 .\" Copyright (c) 2022 Brad Spencer <brad%anduin.eldar.org@localhost>
 .\"
@@ -55,6 +55,15 @@
 measurement.
 The value values are 1 (or off), 2, 5, 11 and 22 samples
 to reach >= 75% of the step response.
+.It Li hw.bmx280thp0.waitfactor.t
+.It Li hw.bmx280thp0.waitfactor.p
+.It Li hw.bmx280thp0.waitfactor.h
+These control the wait multiplication factor for a measurement cycle.
+This factor is different for temperature, pressure and humidity and
+is based upon the values of osrs_t, osrs_p and osrs_h.  If the chip
+does not return the correct measurements for a given over sampling
+then the wait factors can be adjusted to allow more time for the
+measurement to complete successfully.
 .It Li hw.bmx280thp0.debug
 .It Li hw.bmx280thp0.dump_calibration
 If the driver is compiled with
diff -r d4f8f6426c31 -r 0f3383a1046e sys/dev/i2c/bmx280.c
--- a/sys/dev/i2c/bmx280.c      Wed Nov 23 19:20:34 2022 +0000
+++ b/sys/dev/i2c/bmx280.c      Wed Nov 23 23:45:29 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: bmx280.c,v 1.2 2022/11/22 19:40:31 brad Exp $  */
+/*     $NetBSD: bmx280.c,v 1.3 2022/11/23 23:45:29 brad Exp $  */
 
 /*
  * Copyright (c) 2022 Brad Spencer <brad%anduin.eldar.org@localhost>
@@ -17,7 +17,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: bmx280.c,v 1.2 2022/11/22 19:40:31 brad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bmx280.c,v 1.3 2022/11/23 23:45:29 brad Exp $");
 
 /*
   Driver for the Bosch BMP280/BME280 temperature, humidity (sometimes) and
@@ -31,6 +31,7 @@
 #include <sys/module.h>
 #include <sys/sysctl.h>
 #include <sys/mutex.h>
+#include <sys/proc.h>
 
 #include <dev/sysmon/sysmonvar.h>
 #include <dev/i2c/i2cvar.h>
@@ -349,7 +350,7 @@
 {
        int error;
        const struct sysctlnode *cnode;
-       int sysctlroot_num;
+       int sysctlroot_num, sysctlwait_num;
 
        if ((error = sysctl_createv(&sc->sc_bmx280log, 0, NULL, &cnode,
            0, CTLTYPE_NODE, device_xname(sc->sc_dev),
@@ -414,6 +415,36 @@
            0, CTL_HW, sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
                return error;
 
+       if ((error = sysctl_createv(&sc->sc_bmx280log, 0, NULL, &cnode,
+           0, CTLTYPE_NODE, "waitfactor",
+           SYSCTL_DESCR("bmx280 wait factors"), NULL, 0, NULL, 0, CTL_HW,
+           sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
+               return error;
+       sysctlwait_num = cnode->sysctl_num;
+
+       if ((error = sysctl_createv(&sc->sc_bmx280log, 0, NULL, &cnode,
+           CTLFLAG_READWRITE, CTLTYPE_INT, "t",
+           SYSCTL_DESCR("Temperature wait multiplier"),
+           bmx280_verify_sysctl, 0, &sc->sc_waitfactor_t,
+           0, CTL_HW, sysctlroot_num, sysctlwait_num, CTL_CREATE, CTL_EOL)) != 0)
+               return error;
+
+       if ((error = sysctl_createv(&sc->sc_bmx280log, 0, NULL, &cnode,
+           CTLFLAG_READWRITE, CTLTYPE_INT, "p",
+           SYSCTL_DESCR("Pressure wait multiplier"),
+           bmx280_verify_sysctl, 0, &sc->sc_waitfactor_p,
+           0, CTL_HW, sysctlroot_num, sysctlwait_num, CTL_CREATE, CTL_EOL)) != 0)
+               return error;
+
+       if (sc->sc_has_humidity) {
+               if ((error = sysctl_createv(&sc->sc_bmx280log, 0, NULL, &cnode,
+                   CTLFLAG_READWRITE, CTLTYPE_INT, "h",
+                   SYSCTL_DESCR("Humidity wait multiplier"),
+                   bmx280_verify_sysctl, 0, &sc->sc_waitfactor_h,
+                   0, CTL_HW, sysctlroot_num, sysctlwait_num, CTL_CREATE, CTL_EOL)) != 0)
+                       return error;
+       }
+
        return 0;
 }
 
@@ -472,6 +503,9 @@
        sc->sc_osrs_h = 1;
        sc->sc_irr_samples = 1;
        sc->sc_previous_irr = 0xff;
+       sc->sc_waitfactor_t = 6;
+       sc->sc_waitfactor_p = 2;
+       sc->sc_waitfactor_h = 2;
        sc->sc_sme = NULL;
 
        aprint_normal("\n");
@@ -718,11 +752,24 @@
                error = EINVAL;
        }
 
-       /* Hmm... this delay is not documented well..  mostly just a guess...
-        * If it is too short, you will get junk returned as it is possible to try
-        * to ask for the data before the chip has even started... it seems...
+       /* The wait needed is not well documented, so this is somewhat of a guess.
+        * There is an attempt with this to only wait as long as needed.
         */
-       delay(35000);
+
+       int p1, p2;
+
+       p1 = (osrs_t_mask * sc->sc_waitfactor_t) + (osrs_p_mask * sc->sc_waitfactor_p);
+       if (sc->sc_has_humidity) {
+               p1 = p1 + (osrs_h_mask * sc->sc_waitfactor_h);
+       }
+       p2 = mstohz(p1);
+       if (p2 == 0) {
+               p2 = 1;
+       }
+       /* Be careful with this...  the print itself will cause extra delay */
+       DPRINTF(sc, 2, ("%s: p1: %d ; %d\n",
+           device_xname(sc->sc_dev), p1, p2));
+       kpause("b280mea",false,p2,NULL);
 
        return error;
 }
diff -r d4f8f6426c31 -r 0f3383a1046e sys/dev/i2c/bmx280var.h
--- a/sys/dev/i2c/bmx280var.h   Wed Nov 23 19:20:34 2022 +0000
+++ b/sys/dev/i2c/bmx280var.h   Wed Nov 23 23:45:29 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: bmx280var.h,v 1.1 2022/11/21 21:24:01 brad Exp $       */
+/*     $NetBSD: bmx280var.h,v 1.2 2022/11/23 23:45:29 brad Exp $       */
 
 /*
  * Copyright (c) 2022 Brad Spencer <brad%anduin.eldar.org@localhost>
@@ -65,6 +65,9 @@
        int             sc_irr_samples;
        uint8_t         sc_previous_irr;
        bool            sc_bmx280dump;
+       int             sc_waitfactor_t;
+       int             sc_waitfactor_p;
+       int             sc_waitfactor_h;
 };
 
 struct bmx280_sensor {



Home | Main Index | Thread Index | Old Index