Browse Source

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 <tpiepho@gmail.com>
pull/74334/head
Trent Piepho 2 years ago committed by Anas Nashif
parent
commit
d7e03dd148
  1. 8
      drivers/sensor/silabs/si7006/Kconfig
  2. 29
      drivers/sensor/silabs/si7006/si7006.c
  3. 14
      dts/bindings/sensor/sensirion,sht21.yaml
  4. 10
      dts/bindings/sensor/silabs,si7006.yaml
  5. 5
      tests/drivers/build_all/sensor/i2c.dtsi

8
drivers/sensor/silabs/si7006/Kconfig

@ -2,9 +2,11 @@ @@ -2,9 +2,11 @@
# SPDX-License-Identifier: Apache-2.0
config SI7006
bool "Si7006 Temperature and Humidity Sensor"
bool "SHT21, Si7006, and HTU21D Humidity and Temperature Sensors"
default y
depends on DT_HAS_SILABS_SI7006_ENABLED
depends on DT_HAS_SILABS_SI7006_ENABLED || DT_HAS_SENSIRION_SHT21_ENABLED
select I2C
help
Enable I2C-based driver for Si7006 Temperature and Humidity Sensor.
Enable I2C-based driver for several humidity and temperature sensors
compatible with the Sensirion SHT21, such as the Silicon Labs
Si7006/13/20/21 and Measurement Specialties HTU21D

29
drivers/sensor/silabs/si7006/si7006.c

@ -1,11 +1,10 @@ @@ -1,11 +1,10 @@
/*
* Copyright (c) 2019 Electronut Labs
* Copyright (c) 2023 Trent Piepho <tpiepho@gmail.com>
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT silabs_si7006
#include <zephyr/drivers/sensor.h>
#include <zephyr/kernel.h>
#include <zephyr/device.h>
@ -28,6 +27,8 @@ struct si7006_data { @@ -28,6 +27,8 @@ struct si7006_data {
struct si7006_config {
struct i2c_dt_spec i2c;
/** Use "read temp" vs "read old temp" command, the latter only with SiLabs sensors. */
uint8_t read_temp_cmd;
};
/**
@ -58,20 +59,20 @@ static int si7006_get_humidity(const struct device *dev) @@ -58,20 +59,20 @@ static int si7006_get_humidity(const struct device *dev)
/**
* @brief function to get temperature
*
* Note that si7006_get_humidity must be called before calling
* si7006_get_old_temperature.
* Note that for Si7006 type sensors, si7006_get_humidity must be called before
* calling si7006_get_temperature, as the get old temperature command is used.
*
* @return int 0 on success
*/
static int si7006_get_old_temperature(const struct device *dev)
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];
int retval;
retval = i2c_burst_read_dt(&config->i2c, SI7006_READ_OLD_TEMP, temp,
retval = i2c_burst_read_dt(&config->i2c, config->read_temp_cmd, temp,
sizeof(temp));
if (retval == 0) {
@ -95,7 +96,7 @@ static int si7006_sample_fetch(const struct device *dev, @@ -95,7 +96,7 @@ static int si7006_sample_fetch(const struct device *dev,
retval = si7006_get_humidity(dev);
if (retval == 0) {
retval = si7006_get_old_temperature(dev);
retval = si7006_get_temperature(dev);
}
return retval;
@ -164,15 +165,17 @@ static int si7006_init(const struct device *dev) @@ -164,15 +165,17 @@ static int si7006_init(const struct device *dev)
return 0;
}
#define SI7006_DEFINE(inst) \
#define SI7006_DEFINE(inst, temp_cmd) \
static struct si7006_data si7006_data_##inst; \
\
static const struct si7006_config si7006_config_##inst = { \
.i2c = I2C_DT_SPEC_INST_GET(inst), \
.i2c = I2C_DT_SPEC_GET(inst), \
.read_temp_cmd = temp_cmd, \
}; \
\
SENSOR_DEVICE_DT_INST_DEFINE(inst, si7006_init, NULL, \
&si7006_data_##inst, &si7006_config_##inst, \
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, &si7006_api); \
SENSOR_DEVICE_DT_DEFINE(inst, si7006_init, NULL, \
&si7006_data_##inst, &si7006_config_##inst, \
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, &si7006_api); \
DT_INST_FOREACH_STATUS_OKAY(SI7006_DEFINE)
DT_FOREACH_STATUS_OKAY_VARGS(silabs_si7006, SI7006_DEFINE, SI7006_READ_OLD_TEMP);
DT_FOREACH_STATUS_OKAY_VARGS(sensirion_sht21, SI7006_DEFINE, SI7006_MEAS_TEMP_MASTER_MODE);

14
dts/bindings/sensor/sensirion,sht21.yaml

@ -0,0 +1,14 @@ @@ -0,0 +1,14 @@
# Copyright (c) 2023, Trent Piepho <tpiepho@gmail.com>
# SPDX-License-Identifier: Apache-2.0
description: |
Sensirion SHT21 Humidity and Temperature Sensor
This is compatible with the Measurement Specialties HUT21D, "meas,htu21d".
For the Silicon Labs Si7006/13/20/21 series, use the "silabs,si7006"
compatible for slightly altered operation.
compatible: "sensirion,sht21"
include: [sensor-device.yaml, i2c-device.yaml]

10
dts/bindings/sensor/silabs,si7006.yaml

@ -1,8 +1,14 @@ @@ -1,8 +1,14 @@
# Copyright (c) 2019, Electronut Labs
# Copyright (c) 2023, Trent Piepho <tpiepho@gmail.com>
# SPDX-License-Identifier: Apache-2.0
description: Si7006 temperature and humidity sensor
description: |
Silicon Labs Si7006 Humidity and Temperature Sensor
This is compatible with a range of Silicon Labs sensors, such as the Si7013,
Si7020, and Si7021. This binding will use a slightly different (better)
command to read temperature from the "sensirion,sht21" binding.
compatible: "silabs,si7006"
include: [sensor-device.yaml, i2c-device.yaml]
include: sensirion,sht21.yaml

5
tests/drivers/build_all/sensor/i2c.dtsi

@ -1062,3 +1062,8 @@ test_i2c_lm95234: lm95234@8f { @@ -1062,3 +1062,8 @@ test_i2c_lm95234: lm95234@8f {
reg = <0x8f>;
status = "okay";
};
test_i2c_sht21@90 {
compatible = "sensirion,sht21";
reg = <0x90>;
};

Loading…
Cancel
Save