Commit 88649dad authored by Trent Piepho's avatar Trent Piepho Committed by Anas Nashif
Browse files

drivers/sensor: si7006: Mask off low two bits of data reads



The low two bits are not part of the data, but rather "status" bits that
should be masked off.

This is documented in the HTU21D datasheet Edition 8, date 05/2017, pp.
15, and Sensirion SHT21 datasheet version 6, date 10/2022, §6 (wording
exactly the same):

"The two status bits, the last bits of LSB, must be set to ‘0’ before
calculating physical values."

Also Silicon Labs Si7006 example driver code:
        /* Swap the bytes and clear the status bits */
        return ((data.byte[0] * 256) + data.byte[1]) & ~3;

Since these are the LSBs, it has only a small effect and might not have
been noticed despite being wrong.

While editing this code, switch to using the Zephyr endian conversion
functions intead of a written out conversion.

Add error code to error log message.

Signed-off-by: default avatarTrent Piepho <tpiepho@gmail.com>
parent c7b3b131
Loading
Loading
Loading
Loading
+10 −11
Original line number Diff line number Diff line
@@ -40,16 +40,15 @@ static int si7006_get_humidity(const struct device *dev)
	struct si7006_data *si_data = dev->data;
	const struct si7006_config *config = dev->config;
	int retval;
	uint8_t hum[2];
	uint16_t hum;

	retval = i2c_burst_read_dt(&config->i2c,
				   SI7006_MEAS_REL_HUMIDITY_MASTER_MODE, hum,
				   sizeof(hum));
	retval = i2c_burst_read_dt(&config->i2c, SI7006_MEAS_REL_HUMIDITY_MASTER_MODE,
				   (uint8_t *)&hum, sizeof(hum));

	if (retval == 0) {
		si_data->humidity = (hum[0] << 8) | hum[1];
		si_data->humidity = sys_be16_to_cpu(hum) & ~3;
	} else {
		LOG_ERR("read register err");
		LOG_ERR("read register err: %d", retval);
	}

	return retval;
@@ -68,16 +67,16 @@ static int si7006_get_temperature(const struct device *dev)
{
	struct si7006_data *si_data = dev->data;
	const struct si7006_config *config = dev->config;
	uint8_t temp[2];
	uint16_t temp;
	int retval;

	retval = i2c_burst_read_dt(&config->i2c, config->read_temp_cmd, temp,
				   sizeof(temp));
	retval = i2c_burst_read_dt(&config->i2c, config->read_temp_cmd,
				   (uint8_t *)&temp, sizeof(temp));

	if (retval == 0) {
		si_data->temperature = (temp[0] << 8) | temp[1];
		si_data->temperature = sys_be16_to_cpu(temp) & ~3;
	} else {
		LOG_ERR("read register err");
		LOG_ERR("read register err: %d", retval);
	}

	return retval;