summaryrefslogtreecommitdiff
path: root/sys/dev
diff options
context:
space:
mode:
authorbrad <brad@NetBSD.org>2022-11-23 23:45:29 +0000
committerbrad <brad@NetBSD.org>2022-11-23 23:45:29 +0000
commitcbdb278a4b88f8e19e397bf6a303acc17a2bc5a2 (patch)
tree0bc0cd95c8f15d31732c7b07114e7b51b2c1b745 /sys/dev
parentd0935da8ab9d1c6d9ddef1d835c9cc525422c572 (diff)
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 (limited to 'sys/dev')
-rw-r--r--sys/dev/i2c/bmx280.c61
-rw-r--r--sys/dev/i2c/bmx280var.h5
2 files changed, 58 insertions, 8 deletions
diff --git a/sys/dev/i2c/bmx280.c b/sys/dev/i2c/bmx280.c
index b9f5bed4761..fe58787e511 100644
--- a/sys/dev/i2c/bmx280.c
+++ b/sys/dev/i2c/bmx280.c
@@ -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>
@@ -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.2 2022/11/22 19:40:31 brad Exp $");
#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 self, void *aux)
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 bmx280_sc *sc,
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 --git a/sys/dev/i2c/bmx280var.h b/sys/dev/i2c/bmx280var.h
index b49e890b4f2..19cc99ceb9f 100644
--- a/sys/dev/i2c/bmx280var.h
+++ b/sys/dev/i2c/bmx280var.h
@@ -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>
@@ -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 {