Skip to content

Commit 88649da

Browse files
xyzzy42nashif
authored andcommitted
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: Trent Piepho <[email protected]>
1 parent c7b3b13 commit 88649da

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

drivers/sensor/silabs/si7006/si7006.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,15 @@ static int si7006_get_humidity(const struct device *dev)
4040
struct si7006_data *si_data = dev->data;
4141
const struct si7006_config *config = dev->config;
4242
int retval;
43-
uint8_t hum[2];
43+
uint16_t hum;
4444

45-
retval = i2c_burst_read_dt(&config->i2c,
46-
SI7006_MEAS_REL_HUMIDITY_MASTER_MODE, hum,
47-
sizeof(hum));
45+
retval = i2c_burst_read_dt(&config->i2c, SI7006_MEAS_REL_HUMIDITY_MASTER_MODE,
46+
(uint8_t *)&hum, sizeof(hum));
4847

4948
if (retval == 0) {
50-
si_data->humidity = (hum[0] << 8) | hum[1];
49+
si_data->humidity = sys_be16_to_cpu(hum) & ~3;
5150
} else {
52-
LOG_ERR("read register err");
51+
LOG_ERR("read register err: %d", retval);
5352
}
5453

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

74-
retval = i2c_burst_read_dt(&config->i2c, config->read_temp_cmd, temp,
75-
sizeof(temp));
73+
retval = i2c_burst_read_dt(&config->i2c, config->read_temp_cmd,
74+
(uint8_t *)&temp, sizeof(temp));
7675

7776
if (retval == 0) {
78-
si_data->temperature = (temp[0] << 8) | temp[1];
77+
si_data->temperature = sys_be16_to_cpu(temp) & ~3;
7978
} else {
80-
LOG_ERR("read register err");
79+
LOG_ERR("read register err: %d", retval);
8180
}
8281

8382
return retval;

0 commit comments

Comments
 (0)