Module Name:    src
Committed By:   brad
Date:           Wed Nov 23 23:45:30 UTC 2022

Modified Files:
        src/share/man/man4: bmx280thp.4
        src/sys/dev/i2c: bmx280.c bmx280var.h

Log Message:
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.


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/share/man/man4/bmx280thp.4
cvs rdiff -u -r1.2 -r1.3 src/sys/dev/i2c/bmx280.c
cvs rdiff -u -r1.1 -r1.2 src/sys/dev/i2c/bmx280var.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/share/man/man4/bmx280thp.4
diff -u src/share/man/man4/bmx280thp.4:1.2 src/share/man/man4/bmx280thp.4:1.3
--- src/share/man/man4/bmx280thp.4:1.2	Mon Nov 21 23:51:10 2022
+++ src/share/man/man4/bmx280thp.4	Wed Nov 23 23:45:30 2022
@@ -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 <b...@anduin.eldar.org>
 .\"
@@ -55,6 +55,15 @@ IRR is a filter that can be used to redu
 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

Index: src/sys/dev/i2c/bmx280.c
diff -u src/sys/dev/i2c/bmx280.c:1.2 src/sys/dev/i2c/bmx280.c:1.3
--- src/sys/dev/i2c/bmx280.c:1.2	Tue Nov 22 19:40:31 2022
+++ src/sys/dev/i2c/bmx280.c	Wed Nov 23 23:45:29 2022
@@ -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 <b...@anduin.eldar.org>
@@ -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 @@ __KERNEL_RCSID(0, "$NetBSD: bmx280.c,v 1
 #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 @@ bmx280_sysctl_init(struct bmx280_sc *sc)
 {
 	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 @@ bmx280_sysctl_init(struct bmx280_sc *sc)
 	    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 @@ bmx280_attach(device_t parent, device_t 
 	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 @@ bmx280_set_control_and_trigger(struct bm
 		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;
 }

Index: src/sys/dev/i2c/bmx280var.h
diff -u src/sys/dev/i2c/bmx280var.h:1.1 src/sys/dev/i2c/bmx280var.h:1.2
--- src/sys/dev/i2c/bmx280var.h:1.1	Mon Nov 21 21:24:01 2022
+++ src/sys/dev/i2c/bmx280var.h	Wed Nov 23 23:45:29 2022
@@ -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 <b...@anduin.eldar.org>
@@ -65,6 +65,9 @@ struct bmx280_sc {
 	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 {

Reply via email to