Skip to content

Commit d7e03dd

Browse files
xyzzy42nashif
authored andcommitted
drivers/sensor: si7006: Support SHT21 and HTU21D
These three sensor types are all largely compatible. The SHT21 and HTU21D can be supported by this driver by sending command 0xE3 instead of 0xE0 to read the temperature. Mention the sensor names in bindings and Kconfig to help those looking for support to find it. There have been at least five PRs attempting to add SHT21 and/or HTU21D support that did not realize the Si7006 is the same. As mentioned in PR #22862, the Sensirion SH21 is the original. The dts bindings are adjusted (in a backward compatible way!) to make the sht21 the base binding and si7006 is derived from that. Examples of dts compatibles: TE Connectivity née Measurement Sepcialties HTU21D: compatible = "meas,htu21d", "sensirion,sht21"; Sensirion SHT21: compatible = "sensirion,sht21"; Silicon Labs Si7006 compatible = "silabs,si7006"; Silicon Labs Si7021 compatible = "silabs,si7021", "silabs,si7006"; Signed-off-by: Trent Piepho <[email protected]>
1 parent 6817ac3 commit d7e03dd

File tree

5 files changed

+48
-18
lines changed

5 files changed

+48
-18
lines changed

drivers/sensor/silabs/si7006/Kconfig

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
# SPDX-License-Identifier: Apache-2.0
33

44
config SI7006
5-
bool "Si7006 Temperature and Humidity Sensor"
5+
bool "SHT21, Si7006, and HTU21D Humidity and Temperature Sensors"
66
default y
7-
depends on DT_HAS_SILABS_SI7006_ENABLED
7+
depends on DT_HAS_SILABS_SI7006_ENABLED || DT_HAS_SENSIRION_SHT21_ENABLED
88
select I2C
99
help
10-
Enable I2C-based driver for Si7006 Temperature and Humidity Sensor.
10+
Enable I2C-based driver for several humidity and temperature sensors
11+
compatible with the Sensirion SHT21, such as the Silicon Labs
12+
Si7006/13/20/21 and Measurement Specialties HTU21D

drivers/sensor/silabs/si7006/si7006.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
/*
22
* Copyright (c) 2019 Electronut Labs
3+
* Copyright (c) 2023 Trent Piepho <[email protected]>
34
*
45
* SPDX-License-Identifier: Apache-2.0
56
*/
67

7-
#define DT_DRV_COMPAT silabs_si7006
8-
98
#include <zephyr/drivers/sensor.h>
109
#include <zephyr/kernel.h>
1110
#include <zephyr/device.h>
@@ -28,6 +27,8 @@ struct si7006_data {
2827

2928
struct si7006_config {
3029
struct i2c_dt_spec i2c;
30+
/** Use "read temp" vs "read old temp" command, the latter only with SiLabs sensors. */
31+
uint8_t read_temp_cmd;
3132
};
3233

3334
/**
@@ -58,20 +59,20 @@ static int si7006_get_humidity(const struct device *dev)
5859
/**
5960
* @brief function to get temperature
6061
*
61-
* Note that si7006_get_humidity must be called before calling
62-
* si7006_get_old_temperature.
62+
* Note that for Si7006 type sensors, si7006_get_humidity must be called before
63+
* calling si7006_get_temperature, as the get old temperature command is used.
6364
*
6465
* @return int 0 on success
6566
*/
6667

67-
static int si7006_get_old_temperature(const struct device *dev)
68+
static int si7006_get_temperature(const struct device *dev)
6869
{
6970
struct si7006_data *si_data = dev->data;
7071
const struct si7006_config *config = dev->config;
7172
uint8_t temp[2];
7273
int retval;
7374

74-
retval = i2c_burst_read_dt(&config->i2c, SI7006_READ_OLD_TEMP, temp,
75+
retval = i2c_burst_read_dt(&config->i2c, config->read_temp_cmd, temp,
7576
sizeof(temp));
7677

7778
if (retval == 0) {
@@ -95,7 +96,7 @@ static int si7006_sample_fetch(const struct device *dev,
9596

9697
retval = si7006_get_humidity(dev);
9798
if (retval == 0) {
98-
retval = si7006_get_old_temperature(dev);
99+
retval = si7006_get_temperature(dev);
99100
}
100101

101102
return retval;
@@ -164,15 +165,17 @@ static int si7006_init(const struct device *dev)
164165
return 0;
165166
}
166167

167-
#define SI7006_DEFINE(inst) \
168+
#define SI7006_DEFINE(inst, temp_cmd) \
168169
static struct si7006_data si7006_data_##inst; \
169170
\
170171
static const struct si7006_config si7006_config_##inst = { \
171-
.i2c = I2C_DT_SPEC_INST_GET(inst), \
172+
.i2c = I2C_DT_SPEC_GET(inst), \
173+
.read_temp_cmd = temp_cmd, \
172174
}; \
173175
\
174-
SENSOR_DEVICE_DT_INST_DEFINE(inst, si7006_init, NULL, \
175-
&si7006_data_##inst, &si7006_config_##inst, \
176-
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, &si7006_api); \
176+
SENSOR_DEVICE_DT_DEFINE(inst, si7006_init, NULL, \
177+
&si7006_data_##inst, &si7006_config_##inst, \
178+
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, &si7006_api); \
177179

178-
DT_INST_FOREACH_STATUS_OKAY(SI7006_DEFINE)
180+
DT_FOREACH_STATUS_OKAY_VARGS(silabs_si7006, SI7006_DEFINE, SI7006_READ_OLD_TEMP);
181+
DT_FOREACH_STATUS_OKAY_VARGS(sensirion_sht21, SI7006_DEFINE, SI7006_MEAS_TEMP_MASTER_MODE);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright (c) 2023, Trent Piepho <[email protected]>
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: |
5+
Sensirion SHT21 Humidity and Temperature Sensor
6+
7+
This is compatible with the Measurement Specialties HUT21D, "meas,htu21d".
8+
9+
For the Silicon Labs Si7006/13/20/21 series, use the "silabs,si7006"
10+
compatible for slightly altered operation.
11+
12+
compatible: "sensirion,sht21"
13+
14+
include: [sensor-device.yaml, i2c-device.yaml]
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
# Copyright (c) 2019, Electronut Labs
2+
# Copyright (c) 2023, Trent Piepho <[email protected]>
23
# SPDX-License-Identifier: Apache-2.0
34

4-
description: Si7006 temperature and humidity sensor
5+
description: |
6+
Silicon Labs Si7006 Humidity and Temperature Sensor
7+
8+
This is compatible with a range of Silicon Labs sensors, such as the Si7013,
9+
Si7020, and Si7021. This binding will use a slightly different (better)
10+
command to read temperature from the "sensirion,sht21" binding.
511
612
compatible: "silabs,si7006"
713

8-
include: [sensor-device.yaml, i2c-device.yaml]
14+
include: sensirion,sht21.yaml

tests/drivers/build_all/sensor/i2c.dtsi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,3 +1062,8 @@ test_i2c_lm95234: lm95234@8f {
10621062
reg = <0x8f>;
10631063
status = "okay";
10641064
};
1065+
1066+
test_i2c_sht21@90 {
1067+
compatible = "sensirion,sht21";
1068+
reg = <0x90>;
1069+
};

0 commit comments

Comments
 (0)