fixes from Brad Spenser

This commit is contained in:
christos 2017-12-30 03:18:26 +00:00
parent e11969ea5d
commit 43e3a4888e
2 changed files with 42 additions and 34 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: am2315.c,v 1.1 2017/12/28 23:23:47 christos Exp $ */
/* $NetBSD: am2315.c,v 1.2 2017/12/30 03:18:26 christos Exp $ */
/*
* Copyright (c) 2017 Brad Spencer <brad@anduin.eldar.org>
@ -17,7 +17,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: am2315.c,v 1.1 2017/12/28 23:23:47 christos Exp $");
__KERNEL_RCSID(0, "$NetBSD: am2315.c,v 1.2 2017/12/30 03:18:26 christos Exp $");
/*
* Driver for the Aosong AM2315
@ -153,16 +153,16 @@ am2315_poke_m(i2c_tag_t tag, i2c_addr_t addr, const char *name, bool debug)
if (debug)
printf("%s: poke 1: %d\n", name, error);
if (error != 0)
if (error)
delay(2800);
error = am2315_cmd(tag, addr, AM2315_READ_REGISTERS,
AM2315_REGISTER_STATUS, 1, buf, __arraycount(buf));
if (debug)
printf("%s: poke 2: %d %02x %02x %02x %02x%02x\n", name, error,
buf[0], buf[1], buf[2], buf[3], buf[4]);
printf("%s: poke 2: %d %02x %02x %02x %02x%02x\n", name, error,
buf[0], buf[1], buf[2], buf[3], buf[4]);
if (error != 0)
if (error)
delay(2800);
return error;
}
@ -204,6 +204,7 @@ am2315_match(device_t parent, cfdata_t match, void *aux)
return 0;
}
iic_release_bus(ia->ia_tag, 0);
return 1;
}
@ -299,7 +300,7 @@ am2315_attach(device_t parent, device_t self, void *aux)
&& crc == readcrc;
error = am2315_read_regs(sc, AM2315_REGISTER_VERSION, 1, buf, 5);
if (error != 0)
if (error)
aprint_error_dev(self, "read chipver: %d\n", error);
readcrc = buf[4] << 8 | buf[3];
crc = am2315_crc(buf, 3);
@ -309,7 +310,7 @@ am2315_attach(device_t parent, device_t self, void *aux)
&& crc == readcrc;
error = am2315_read_regs(sc, AM2315_REGISTER_ID_PT_24_31, 2, buf, 6);
if (error != 0)
if (error)
aprint_error_dev(self, "read id 1: %d\n", error);
readcrc = buf[5] << 8 | buf[4];
crc = am2315_crc(buf, 4);
@ -319,7 +320,7 @@ am2315_attach(device_t parent, device_t self, void *aux)
&& crc == readcrc;
error = am2315_read_regs(sc, AM2315_REGISTER_ID_PT_8_15, 2, buf, 6);
if (error != 0)
if (error)
aprint_error_dev(self, "read id 2: %d\n", error);
readcrc = buf[5] << 8 | buf[4];
crc = am2315_crc(buf, 4);
@ -392,7 +393,7 @@ am2315_refresh(struct sysmon_envsys * sme, envsys_data_t * edata)
mutex_enter(&sc->sc_mutex);
error = iic_acquire_bus(sc->sc_tag, 0);
if (error == 0) {
if (error) {
DPRINTF(sc, 2, ("%s: Could not acquire i2c bus: %d\n",
device_xname(sc->sc_dev), error));
goto out;
@ -415,8 +416,8 @@ am2315_refresh(struct sysmon_envsys * sme, envsys_data_t * edata)
am2315_poke(sc);
if ((error = am2315_read_regs(sc, thecommand, 2, buf, 6)) != 0)
aprint_error_dev(sc->sc_dev,
"Read sensor %d error: %d\n", edata->sensor, error);
DPRINTF(sc, 2, ("%s: Read sensor %d error: %d\n",
device_xname(sc->sc_dev),edata->sensor, error));
readcrc = buf[5] << 8 | buf[4];
crc = am2315_crc(buf, 4);

View File

@ -1,4 +1,4 @@
/* $NetBSD: si70xx.c,v 1.2 2017/12/29 02:19:45 christos Exp $ */
/* $NetBSD: si70xx.c,v 1.3 2017/12/30 03:18:26 christos Exp $ */
/*
* Copyright (c) 2017 Brad Spencer <brad@anduin.eldar.org>
@ -17,7 +17,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: si70xx.c,v 1.2 2017/12/29 02:19:45 christos Exp $");
__KERNEL_RCSID(0, "$NetBSD: si70xx.c,v 1.3 2017/12/30 03:18:26 christos Exp $");
/*
Driver for the Silicon Labs SI7013/SI7020/SI7021
@ -202,12 +202,10 @@ si70xx_verify_sysctl_heatervalue(SYSCTLFN_ARGS)
return error;
}
static int
si70xx_cmd(i2c_tag_t tag, i2c_addr_t addr, uint8_t *cmd,
uint8_t clen, uint8_t *buf, size_t blen)
static uint8_t
si70xx_dir(uint8_t cmd, size_t len)
{
uint8_t dir;
switch (cmd[0]) {
switch (cmd) {
case SI70XX_READ_USER_REG_1:
case SI70XX_READ_HEATER_REG:
case SI70XX_READ_ID_PT1A:
@ -216,22 +214,33 @@ si70xx_cmd(i2c_tag_t tag, i2c_addr_t addr, uint8_t *cmd,
case SI70XX_READ_ID_PT2B:
case SI70XX_READ_FW_VERA:
case SI70XX_READ_FW_VERB:
dir = I2C_OP_READ_WITH_STOP;
break;
return I2C_OP_READ_WITH_STOP;
case SI70XX_WRITE_USER_REG_1:
case SI70XX_WRITE_HEATER_REG:
case SI70XX_RESET:
dir = I2C_OP_WRITE_WITH_STOP;
break;
return I2C_OP_WRITE_WITH_STOP;
case SI70XX_MEASURE_RH_NOHOLD:
case SI70XX_MEASURE_TEMP_NOHOLD:
dir = blen == 0 ? I2C_OP_READ : I2C_OP_READ_WITH_STOP;
break;
return len == 0 ? I2C_OP_READ : I2C_OP_READ_WITH_STOP;
default:
panic("%s: bad command %#x\n", __func__, cmd[0]);
panic("%s: bad command %#x\n", __func__, cmd);
return 0;
}
}
static int
si70xx_cmd(i2c_tag_t tag, i2c_addr_t addr, uint8_t *cmd,
uint8_t clen, uint8_t *buf, size_t blen)
{
uint8_t dir;
if (clen == 0)
dir = blen == 0 ? I2C_OP_READ : I2C_OP_READ_WITH_STOP;
else
dir = si70xx_dir(cmd[0], blen);
if (dir == I2C_OP_READ || dir == I2C_OP_READ_WITH_STOP)
memset(buf, 0, blen);
memset(buf, 0, blen);
return iic_exec(tag, dir, addr, cmd, clen, buf, blen, 0);
}
@ -359,7 +368,7 @@ si70xx_set_heatervalue(struct si70xx_sc * sc, size_t index)
DPRINTF(sc, 2, ("%s:%s: heater values after: %#x\n",
device_xname(sc->sc_dev), __func__, heaterregister));
error = si70xx_cmd1(sc, SI70XX_WRITE_USER_REG_1, &heaterregister, 1);
error = si70xx_cmd1(sc, SI70XX_WRITE_HEATER_REG, &heaterregister, 1);
if (error) {
DPRINTF(sc, 2, ("%s: Failed to write heater register: %d\n",
device_xname(sc->sc_dev), error));
@ -610,7 +619,7 @@ si70xx_attach(device_t parent, device_t self, void *aux)
#ifdef HAVE_I2C_EXECV
sc->sc_clockstretch = 2048;
#endif
sc->sc_readattempts = 15;
sc->sc_readattempts = 25;
sc->sc_ignorecrc = false;
sc->sc_sme = NULL;
@ -877,9 +886,7 @@ si70xx_exec(struct si70xx_sc *sc, uint8_t cmd, envsys_data_t *edata)
break;
DPRINTF(sc, 2, ("%s: Failed to read NO HOLD RH"
" %d %d\n", device_xname(sc->sc_dev), 2, error));
if (aint < 10) {
delay(1000);
}
delay(1000);
}
#endif
@ -933,11 +940,11 @@ si70xx_refresh(struct sysmon_envsys * sme, envsys_data_t * edata)
}
switch (edata->sensor) {
case SI70XX_HUMIDITY_SENSOR:
error = si70xx_exec(sc, SI70XX_MEASURE_RH_HOLD, edata);
error = si70xx_exec(sc, SI70XX_MEASURE_RH_NOHOLD, edata);
break;
case SI70XX_TEMP_SENSOR:
error = si70xx_exec(sc, SI70XX_MEASURE_TEMP_HOLD, edata);
error = si70xx_exec(sc, SI70XX_MEASURE_TEMP_NOHOLD, edata);
break;
default:
error = EINVAL;