drivers/sensor: Add basic TI HDC20XX support

Add basic support for TI HDC20XX series (e.g. HDC2010, HDC2021, HDC2022,
HDC2080). It is able to get temperature and humidity in the default
14-bit resolution. Triggers, resolution selection, interrupt line, auto
measurement mode are currently not supported.

Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
This commit is contained in:
Aurelien Jarno 2021-06-16 22:12:25 +02:00 committed by Christopher Friedt
commit 0d543f1742
10 changed files with 252 additions and 0 deletions

View file

@ -27,6 +27,7 @@ add_subdirectory_ifdef(CONFIG_FXAS21002 fxas21002)
add_subdirectory_ifdef(CONFIG_FXOS8700 fxos8700) add_subdirectory_ifdef(CONFIG_FXOS8700 fxos8700)
add_subdirectory(grove) add_subdirectory(grove)
add_subdirectory_ifdef(CONFIG_TI_HDC ti_hdc) add_subdirectory_ifdef(CONFIG_TI_HDC ti_hdc)
add_subdirectory_ifdef(CONFIG_TI_HDC20XX ti_hdc20xx)
add_subdirectory_ifdef(CONFIG_HMC5883L hmc5883l) add_subdirectory_ifdef(CONFIG_HMC5883L hmc5883l)
add_subdirectory_ifdef(CONFIG_HP206C hp206c) add_subdirectory_ifdef(CONFIG_HP206C hp206c)
add_subdirectory_ifdef(CONFIG_HTS221 hts221) add_subdirectory_ifdef(CONFIG_HTS221 hts221)

View file

@ -92,6 +92,8 @@ source "drivers/sensor/grove/Kconfig"
source "drivers/sensor/ti_hdc/Kconfig" source "drivers/sensor/ti_hdc/Kconfig"
source "drivers/sensor/ti_hdc20xx/Kconfig"
source "drivers/sensor/hmc5883l/Kconfig" source "drivers/sensor/hmc5883l/Kconfig"
source "drivers/sensor/hp206c/Kconfig" source "drivers/sensor/hp206c/Kconfig"

View file

@ -0,0 +1,5 @@
# SPDX-License-Identifier: Apache-2.0
zephyr_library()
zephyr_library_sources(ti_hdc20xx.c)

View file

@ -0,0 +1,11 @@
# TI_HDC20XX temperature and humidity sensor configuration options
# Copyright (c) 2021 Aurelien Jarno
# SPDX-License-Identifier: Apache-2.0
config TI_HDC20XX
bool "Texas Instruments HDC20XX Temperature and Humidity Sensor"
depends on I2C
help
Enable driver for TI HDC20XX temperature and humidity sensors
(e.g. HDC2010, HDC2021, HDC2022, HDC2080).

View file

@ -0,0 +1,193 @@
/*
* Copyright (c) 2021 Aurelien Jarno
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <kernel.h>
#include <drivers/i2c.h>
#include <drivers/sensor.h>
#include <sys/byteorder.h>
#include <sys/__assert.h>
#include <logging/log.h>
LOG_MODULE_REGISTER(TI_HDC20XX, CONFIG_SENSOR_LOG_LEVEL);
/* Register addresses */
#define TI_HDC20XX_REG_TEMP 0x00
#define TI_HDC20XX_REG_HUMIDITY 0x02
#define TI_HDC20XX_REG_MEAS_CFG 0x0F
#define TI_HDC20XX_REG_MANUFACTURER_ID 0xFC
#define TI_HDC20XX_REG_DEVICE_ID 0xFE
/* Register values */
#define TI_HDC20XX_MANUFACTURER_ID 0x5449
#define TI_HDC20XX_DEVICE_ID 0x07D0
/* Conversion time for 14-bit resolution. Temperature needs 660us and humidity 610us */
#define TI_HDC20XX_CONVERSION_TIME K_MSEC(2)
/* Temperature and humidity scale and factors from the datasheet ("Register Maps" section) */
#define TI_HDC20XX_RH_SCALE 100U
#define TI_HDC20XX_TEMP_OFFSET -40U
#define TI_HDC20XX_TEMP_SCALE 165U
struct ti_hdc20xx_config {
const struct device *bus;
uint16_t i2c_addr;
};
struct ti_hdc20xx_data {
int32_t t_sample;
int32_t rh_sample;
};
static int ti_hdc20xx_sample_fetch(const struct device *dev,
enum sensor_channel chan)
{
const struct ti_hdc20xx_config *config = dev->config;
struct ti_hdc20xx_data *data = dev->data;
uint16_t buf[2];
int rc;
__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);
/* start conversion of both temperature and humidity with the default accuracy (14 bits) */
rc = i2c_reg_write_byte(config->bus, config->i2c_addr, TI_HDC20XX_REG_MEAS_CFG, 0x01);
if (rc < 0) {
LOG_ERR("Failed to write measurement configuration register");
return rc;
}
/* wait for the conversion to finish */
k_sleep(TI_HDC20XX_CONVERSION_TIME);
/* temperature and humidity registers are consecutive, read them in the same burst */
rc = i2c_burst_read(config->bus, config->i2c_addr,
TI_HDC20XX_REG_TEMP, (uint8_t *)buf, sizeof(buf));
if (rc < 0) {
LOG_ERR("Failed to read sample data");
return rc;
}
data->t_sample = sys_le16_to_cpu(buf[0]);
data->rh_sample = sys_le16_to_cpu(buf[1]);
return 0;
}
static int ti_hdc20xx_channel_get(const struct device *dev,
enum sensor_channel chan,
struct sensor_value *val)
{
struct ti_hdc20xx_data *data = dev->data;
int32_t tmp;
/* See datasheet "Register Maps" section for more details on processing sample data. */
switch (chan) {
case SENSOR_CHAN_AMBIENT_TEMP:
/* val = -40 + 165 * sample / 2^16 */
tmp = data->t_sample * TI_HDC20XX_TEMP_SCALE;
val->val1 = TI_HDC20XX_TEMP_OFFSET + (tmp >> 16);
/* x * 1000000 / 2^16 = x * 15625 / 2^10 */
val->val2 = ((tmp & 0xFFFF) * 15625U) >> 10;
break;
case SENSOR_CHAN_HUMIDITY:
/* val = 100 * sample / 2^16 */
tmp = data->rh_sample * TI_HDC20XX_RH_SCALE;
val->val1 = tmp >> 16;
/* x * 1000000 / 2^16 = x * 15625 / 2^10 */
val->val2 = ((tmp & 0xFFFF) * 15625U) >> 10;
break;
default:
return -EINVAL;
}
return 0;
}
static const struct sensor_driver_api ti_hdc20xx_api_funcs = {
.sample_fetch = ti_hdc20xx_sample_fetch,
.channel_get = ti_hdc20xx_channel_get,
};
static int ti_hdc20xx_init(const struct device *dev)
{
const struct ti_hdc20xx_config *config = dev->config;
uint16_t buf[2];
int rc;
if (!device_is_ready(config->bus)) {
LOG_ERR("I2C bus %s not ready", config->bus->name);
return -ENODEV;
}
/* manufacturer and device ID registers are consecutive, read them in the same burst */
rc = i2c_burst_read(config->bus, config->i2c_addr,
TI_HDC20XX_REG_MANUFACTURER_ID, (uint8_t *)buf, sizeof(buf));
if (rc < 0) {
LOG_ERR("Failed to read manufacturer and device IDs");
return rc;
}
if (sys_le16_to_cpu(buf[0]) != TI_HDC20XX_MANUFACTURER_ID) {
LOG_ERR("Failed to get correct manufacturer ID");
return -EINVAL;
}
if (sys_le16_to_cpu(buf[1]) != TI_HDC20XX_DEVICE_ID) {
LOG_ERR("Unsupported device ID");
return -EINVAL;
}
return 0;
}
/* Main instantiation macro */
#define TI_HDC20XX_DEFINE(inst, compat) \
static struct ti_hdc20xx_data ti_hdc20xx_data_##compat##inst; \
static const struct ti_hdc20xx_config ti_hdc20xx_config_##compat##inst = { \
.bus = DEVICE_DT_GET(DT_BUS(DT_INST(inst, compat))), \
.i2c_addr = DT_REG_ADDR(DT_INST(inst, compat)) \
}; \
DEVICE_DT_DEFINE(DT_INST(inst, compat), \
ti_hdc20xx_init, \
NULL, \
&ti_hdc20xx_data_##compat##inst, \
&ti_hdc20xx_config_##compat##inst, \
POST_KERNEL, \
CONFIG_SENSOR_INIT_PRIORITY, \
&ti_hdc20xx_api_funcs);
/* Create the struct device for every status "okay" node in the devicetree. */
#define TI_HDC20XX_FOREACH_STATUS_OKAY(compat, fn) \
COND_CODE_1(DT_HAS_COMPAT_STATUS_OKAY(compat), \
(UTIL_CAT(DT_FOREACH_OKAY_INST_, \
compat)(fn)), \
())
/*
* HDC2010 Low-Power Humidity and Temperature Digital Sensors
*/
#define TI_HDC2010_DEFINE(inst) TI_HDC20XX_DEFINE(inst, ti_hdc2010)
TI_HDC20XX_FOREACH_STATUS_OKAY(ti_hdc2010, TI_HDC2010_DEFINE)
/*
* HDC2021 High-Accuracy, Low-Power Humidity and Temperature Sensor
* With Assembly Protection Cover
*/
#define TI_HDC2021_DEFINE(inst) TI_HDC20XX_DEFINE(inst, ti_hdc2021)
TI_HDC20XX_FOREACH_STATUS_OKAY(ti_hdc2021, TI_HDC2021_DEFINE)
/*
* HDC2022 High-Accuracy, Low-Power Humidity and Temperature Sensor
* With IP67 Rated Water and Dust Protection Cover
*/
#define TI_HDC2022_DEFINE(inst) TI_HDC20XX_DEFINE(inst, ti_hdc2022)
TI_HDC20XX_FOREACH_STATUS_OKAY(ti_hdc2022, TI_HDC2022_DEFINE)
/*
* HDC2080 Low-Power Humidity and Temperature Digital Sensor
*/
#define TI_HDC2080_DEFINE(inst) TI_HDC20XX_DEFINE(inst, ti_hdc2080)
TI_HDC20XX_FOREACH_STATUS_OKAY(ti_hdc2080, TI_HDC2080_DEFINE)

View file

@ -0,0 +1,8 @@
# Copyright (c) 2021, Aurelien Jarno
# SPDX-License-Identifier: Apache-2.0
description: Texas Instruments HDC2010 Temperature and Humidity Sensor
compatible: "ti,hdc2010"
include: ti,hdc20xx.yaml

View file

@ -0,0 +1,8 @@
# Copyright (c) 2021, Aurelien Jarno
# SPDX-License-Identifier: Apache-2.0
description: Texas Instruments HDC2021 Temperature and Humidity Sensor
compatible: "ti,hdc2021"
include: ti,hdc20xx.yaml

View file

@ -0,0 +1,8 @@
# Copyright (c) 2021, Aurelien Jarno
# SPDX-License-Identifier: Apache-2.0
description: Texas Instruments HDC2022 Temperature and Humidity Sensor
compatible: "ti,hdc2022"
include: ti,hdc20xx.yaml

View file

@ -0,0 +1,8 @@
# Copyright (c) 2021, Aurelien Jarno
# SPDX-License-Identifier: Apache-2.0
description: Texas Instruments HDC2080 Temperature and Humidity Sensor
compatible: "ti,hdc2080"
include: ti,hdc20xx.yaml

View file

@ -0,0 +1,8 @@
# Copyright (c) 2021, Aurelien Jarno
# SPDX-License-Identifier: Apache-2.0
description: Texas Instruments HDC20XX Temperature and Humidity Sensor
compatible: "ti,hdc20xx"
include: i2c-device.yaml