2016-02-22 16:22:16 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 Intel Corporation
|
|
|
|
*
|
2017-01-18 17:01:01 -08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2016-02-22 16:22:16 +02:00
|
|
|
*/
|
|
|
|
|
2020-03-30 10:08:08 -05:00
|
|
|
#define DT_DRV_COMPAT sensirion_sht3xd
|
|
|
|
|
2016-02-22 16:22:16 +02:00
|
|
|
#include <device.h>
|
2019-06-25 15:53:54 -04:00
|
|
|
#include <drivers/i2c.h>
|
2016-11-10 08:21:34 -05:00
|
|
|
#include <kernel.h>
|
2019-06-25 15:54:00 -04:00
|
|
|
#include <drivers/sensor.h>
|
2019-06-26 10:33:39 -04:00
|
|
|
#include <sys/__assert.h>
|
2018-10-10 12:08:54 +05:30
|
|
|
#include <logging/log.h>
|
2016-02-22 16:22:16 +02:00
|
|
|
|
2016-10-15 08:20:01 -04:00
|
|
|
#include "sht3xd.h"
|
2016-02-22 16:22:16 +02:00
|
|
|
|
2019-10-11 12:40:57 +02:00
|
|
|
LOG_MODULE_REGISTER(SHT3XD, CONFIG_SENSOR_LOG_LEVEL);
|
2018-10-10 12:08:54 +05:30
|
|
|
|
2019-09-14 13:20:36 +02:00
|
|
|
#ifdef CONFIG_SHT3XD_SINGLE_SHOT_MODE
|
2020-05-27 11:26:57 -05:00
|
|
|
static const uint16_t measure_cmd[3] = {
|
2019-09-14 13:20:36 +02:00
|
|
|
0x2400, 0x240B, 0x2416
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_SHT3XD_PERIODIC_MODE
|
2020-05-27 11:26:57 -05:00
|
|
|
static const uint16_t measure_cmd[5][3] = {
|
2018-12-09 10:07:48 -06:00
|
|
|
{ 0x202F, 0x2024, 0x2032 },
|
|
|
|
{ 0x212D, 0x2126, 0x2130 },
|
|
|
|
{ 0x222B, 0x2220, 0x2236 },
|
|
|
|
{ 0x2329, 0x2322, 0x2334 },
|
|
|
|
{ 0x272A, 0x2721, 0x2737 }
|
|
|
|
};
|
2019-09-14 13:20:36 +02:00
|
|
|
#endif
|
2018-12-09 10:07:48 -06:00
|
|
|
|
|
|
|
static const int measure_wait[3] = {
|
|
|
|
4000, 6000, 15000
|
|
|
|
};
|
|
|
|
|
2016-02-22 16:22:16 +02:00
|
|
|
/*
|
|
|
|
* CRC algorithm parameters were taken from the
|
|
|
|
* "Checksum Calculation" section of the datasheet.
|
|
|
|
*/
|
2020-05-27 11:26:57 -05:00
|
|
|
static uint8_t sht3xd_compute_crc(uint16_t value)
|
2016-02-22 16:22:16 +02:00
|
|
|
{
|
2020-05-27 11:26:57 -05:00
|
|
|
uint8_t buf[2] = { value >> 8, value & 0xFF };
|
|
|
|
uint8_t crc = 0xFF;
|
|
|
|
uint8_t polynom = 0x31;
|
2016-02-22 16:22:16 +02:00
|
|
|
int i, j;
|
|
|
|
|
|
|
|
for (i = 0; i < 2; ++i) {
|
2019-01-14 16:55:20 -06:00
|
|
|
crc = crc ^ buf[i];
|
2016-02-22 16:22:16 +02:00
|
|
|
for (j = 0; j < 8; ++j) {
|
|
|
|
if (crc & 0x80) {
|
|
|
|
crc = (crc << 1) ^ polynom;
|
|
|
|
} else {
|
|
|
|
crc = crc << 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return crc;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
int sht3xd_write_command(const struct device *dev, uint16_t cmd)
|
2016-02-22 16:22:16 +02:00
|
|
|
{
|
2020-05-27 11:26:57 -05:00
|
|
|
uint8_t tx_buf[2] = { cmd >> 8, cmd & 0xFF };
|
2016-02-22 16:22:16 +02:00
|
|
|
|
2019-01-12 09:06:12 -06:00
|
|
|
return i2c_write(sht3xd_i2c_device(dev), tx_buf, sizeof(tx_buf),
|
|
|
|
sht3xd_i2c_address(dev));
|
2016-02-22 16:22:16 +02:00
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
int sht3xd_write_reg(const struct device *dev, uint16_t cmd, uint16_t val)
|
2016-02-22 16:22:16 +02:00
|
|
|
{
|
2020-05-27 11:26:57 -05:00
|
|
|
uint8_t tx_buf[5];
|
2016-02-22 16:22:16 +02:00
|
|
|
|
|
|
|
tx_buf[0] = cmd >> 8;
|
|
|
|
tx_buf[1] = cmd & 0xFF;
|
|
|
|
tx_buf[2] = val >> 8;
|
|
|
|
tx_buf[3] = val & 0xFF;
|
|
|
|
tx_buf[4] = sht3xd_compute_crc(val);
|
|
|
|
|
2019-01-12 09:06:12 -06:00
|
|
|
return i2c_write(sht3xd_i2c_device(dev), tx_buf, sizeof(tx_buf),
|
|
|
|
sht3xd_i2c_address(dev));
|
2016-02-22 16:22:16 +02:00
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int sht3xd_sample_fetch(const struct device *dev,
|
|
|
|
enum sensor_channel chan)
|
2016-02-22 16:22:16 +02:00
|
|
|
{
|
2020-05-28 21:23:02 +02:00
|
|
|
struct sht3xd_data *data = dev->data;
|
2020-04-30 20:33:38 +02:00
|
|
|
const struct device *i2c = sht3xd_i2c_device(dev);
|
2020-05-27 11:26:57 -05:00
|
|
|
uint8_t address = sht3xd_i2c_address(dev);
|
|
|
|
uint8_t rx_buf[6];
|
|
|
|
uint16_t t_sample, rh_sample;
|
2016-02-22 16:22:16 +02:00
|
|
|
|
2016-07-27 16:56:08 -07:00
|
|
|
__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);
|
2016-04-11 14:49:26 +03:00
|
|
|
|
2019-09-14 13:20:36 +02:00
|
|
|
#ifdef CONFIG_SHT3XD_SINGLE_SHOT_MODE
|
|
|
|
/* start single shot measurement */
|
|
|
|
if (sht3xd_write_command(dev,
|
|
|
|
measure_cmd[SHT3XD_REPEATABILITY_IDX])
|
|
|
|
< 0) {
|
|
|
|
LOG_DBG("Failed to set single shot measurement mode!");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
k_sleep(K_MSEC(measure_wait[SHT3XD_REPEATABILITY_IDX] / USEC_PER_MSEC));
|
|
|
|
|
|
|
|
if (i2c_read(i2c, rx_buf, sizeof(rx_buf), address) < 0) {
|
|
|
|
LOG_DBG("Failed to read data sample!");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_SHT3XD_PERIODIC_MODE
|
2020-05-27 11:26:57 -05:00
|
|
|
uint8_t tx_buf[2] = {
|
2016-02-22 16:22:16 +02:00
|
|
|
SHT3XD_CMD_FETCH >> 8,
|
|
|
|
SHT3XD_CMD_FETCH & 0xFF
|
|
|
|
};
|
|
|
|
|
2019-01-18 12:54:36 -06:00
|
|
|
if (i2c_write_read(i2c, address, tx_buf, sizeof(tx_buf),
|
|
|
|
rx_buf, sizeof(rx_buf)) < 0) {
|
2018-10-10 12:08:54 +05:30
|
|
|
LOG_DBG("Failed to read data sample!");
|
2016-03-19 09:26:27 -04:00
|
|
|
return -EIO;
|
2016-02-22 16:22:16 +02:00
|
|
|
}
|
2019-09-14 13:20:36 +02:00
|
|
|
#endif
|
2016-02-22 16:22:16 +02:00
|
|
|
|
|
|
|
t_sample = (rx_buf[0] << 8) | rx_buf[1];
|
|
|
|
if (sht3xd_compute_crc(t_sample) != rx_buf[2]) {
|
2018-10-10 12:08:54 +05:30
|
|
|
LOG_DBG("Received invalid temperature CRC!");
|
2016-03-19 09:26:27 -04:00
|
|
|
return -EIO;
|
2016-02-22 16:22:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rh_sample = (rx_buf[3] << 8) | rx_buf[4];
|
|
|
|
if (sht3xd_compute_crc(rh_sample) != rx_buf[5]) {
|
2018-10-10 12:08:54 +05:30
|
|
|
LOG_DBG("Received invalid relative humidity CRC!");
|
2016-03-19 09:26:27 -04:00
|
|
|
return -EIO;
|
2016-02-22 16:22:16 +02:00
|
|
|
}
|
|
|
|
|
2019-01-12 09:06:12 -06:00
|
|
|
data->t_sample = t_sample;
|
|
|
|
data->rh_sample = rh_sample;
|
2016-02-22 16:22:16 +02:00
|
|
|
|
2016-03-19 09:26:27 -04:00
|
|
|
return 0;
|
2016-02-22 16:22:16 +02:00
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int sht3xd_channel_get(const struct device *dev,
|
2016-02-22 16:22:16 +02:00
|
|
|
enum sensor_channel chan,
|
|
|
|
struct sensor_value *val)
|
|
|
|
{
|
2020-05-28 21:23:02 +02:00
|
|
|
const struct sht3xd_data *data = dev->data;
|
2020-05-27 11:26:57 -05:00
|
|
|
uint64_t tmp;
|
2016-02-22 16:22:16 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* See datasheet "Conversion of Signal Output" section
|
|
|
|
* for more details on processing sample data.
|
|
|
|
*/
|
2018-03-23 09:41:09 +01:00
|
|
|
if (chan == SENSOR_CHAN_AMBIENT_TEMP) {
|
2016-02-22 16:22:16 +02:00
|
|
|
/* val = -45 + 175 * sample / (2^16 -1) */
|
2020-05-27 11:26:57 -05:00
|
|
|
tmp = (uint64_t)data->t_sample * 175U;
|
|
|
|
val->val1 = (int32_t)(tmp / 0xFFFF) - 45;
|
2019-03-28 14:57:54 -06:00
|
|
|
val->val2 = ((tmp % 0xFFFF) * 1000000U) / 0xFFFF;
|
2016-02-22 16:22:16 +02:00
|
|
|
} else if (chan == SENSOR_CHAN_HUMIDITY) {
|
sensors: Redefine SENSOR_CHAN_HUMIDITY in percents, not milli-percents.
Based on the discussion in #5693, the reason why humidity was defined
in milli-percent was likely following Linux which defines it as such
in its sensor subsystem:
http://elixir.free-electrons.com/linux/latest/source/Documentation/ABI/testing/sysfs-bus-iio#L263
However, Linux defines temperature in milli-degrees either, but
Zephyr uses degrees (similarly for most other quantities). Typical
sensor resolution/precision for humidity is also on the order of 1%.
One of the existing drivers, th02.c, already returned values in
percents, and few apps showed it without conversion and/or units,
leading to confusing output to user like "54500".
So, switching units to percents, and update all the drivers and
sample apps.
For few drivers, there was also optimized conversion arithmetics
to avoid u64_t operations. (There're probably more places to
optimize it, and temperature conversion could use such optimization
too, but that's left for another patch.)
Fixes: #5693
Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-01-25 15:06:17 +02:00
|
|
|
/* val = 100 * sample / (2^16 -1) */
|
2020-05-27 11:26:57 -05:00
|
|
|
uint32_t tmp2 = (uint32_t)data->rh_sample * 100U;
|
sensors: Redefine SENSOR_CHAN_HUMIDITY in percents, not milli-percents.
Based on the discussion in #5693, the reason why humidity was defined
in milli-percent was likely following Linux which defines it as such
in its sensor subsystem:
http://elixir.free-electrons.com/linux/latest/source/Documentation/ABI/testing/sysfs-bus-iio#L263
However, Linux defines temperature in milli-degrees either, but
Zephyr uses degrees (similarly for most other quantities). Typical
sensor resolution/precision for humidity is also on the order of 1%.
One of the existing drivers, th02.c, already returned values in
percents, and few apps showed it without conversion and/or units,
leading to confusing output to user like "54500".
So, switching units to percents, and update all the drivers and
sample apps.
For few drivers, there was also optimized conversion arithmetics
to avoid u64_t operations. (There're probably more places to
optimize it, and temperature conversion could use such optimization
too, but that's left for another patch.)
Fixes: #5693
Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-01-25 15:06:17 +02:00
|
|
|
val->val1 = tmp2 / 0xFFFF;
|
|
|
|
/* x * 100000 / 65536 == x * 15625 / 1024 */
|
2019-03-26 19:57:45 -06:00
|
|
|
val->val2 = (tmp2 % 0xFFFF) * 15625U / 1024;
|
2016-02-22 16:22:16 +02:00
|
|
|
} else {
|
2016-03-19 09:26:27 -04:00
|
|
|
return -ENOTSUP;
|
2016-02-22 16:22:16 +02:00
|
|
|
}
|
|
|
|
|
2016-03-19 09:26:27 -04:00
|
|
|
return 0;
|
2016-02-22 16:22:16 +02:00
|
|
|
}
|
|
|
|
|
2016-10-24 08:35:30 +01:00
|
|
|
static const struct sensor_driver_api sht3xd_driver_api = {
|
2016-02-22 16:22:16 +02:00
|
|
|
#ifdef CONFIG_SHT3XD_TRIGGER
|
|
|
|
.attr_set = sht3xd_attr_set,
|
|
|
|
.trigger_set = sht3xd_trigger_set,
|
|
|
|
#endif
|
|
|
|
.sample_fetch = sht3xd_sample_fetch,
|
|
|
|
.channel_get = sht3xd_channel_get,
|
|
|
|
};
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int sht3xd_init(const struct device *dev)
|
2016-02-22 16:22:16 +02:00
|
|
|
{
|
2020-05-28 21:23:02 +02:00
|
|
|
struct sht3xd_data *data = dev->data;
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct sht3xd_config *cfg = dev->config;
|
2020-04-30 20:33:38 +02:00
|
|
|
const struct device *i2c = device_get_binding(cfg->bus_name);
|
2016-02-22 16:22:16 +02:00
|
|
|
|
2019-01-12 09:06:12 -06:00
|
|
|
if (i2c == NULL) {
|
2018-10-10 12:08:54 +05:30
|
|
|
LOG_DBG("Failed to get pointer to %s device!",
|
2019-01-12 09:06:12 -06:00
|
|
|
cfg->bus_name);
|
2018-11-20 12:41:14 -06:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2019-01-12 09:06:12 -06:00
|
|
|
data->bus = i2c;
|
|
|
|
|
|
|
|
if (!cfg->base_address) {
|
2018-11-20 12:41:14 -06:00
|
|
|
LOG_DBG("No I2C address");
|
2016-03-19 09:26:27 -04:00
|
|
|
return -EINVAL;
|
2016-02-22 16:22:16 +02:00
|
|
|
}
|
2019-01-12 09:06:12 -06:00
|
|
|
data->dev = dev;
|
2016-02-22 16:22:16 +02:00
|
|
|
|
|
|
|
/* clear status register */
|
2019-01-12 09:06:12 -06:00
|
|
|
if (sht3xd_write_command(dev, SHT3XD_CMD_CLEAR_STATUS) < 0) {
|
2018-10-10 12:08:54 +05:30
|
|
|
LOG_DBG("Failed to clear status register!");
|
2016-03-19 09:26:27 -04:00
|
|
|
return -EIO;
|
2016-02-22 16:22:16 +02:00
|
|
|
}
|
|
|
|
|
2016-11-30 15:14:07 -08:00
|
|
|
k_busy_wait(SHT3XD_CLEAR_STATUS_WAIT_USEC);
|
2016-02-22 16:22:16 +02:00
|
|
|
|
2019-09-14 13:20:36 +02:00
|
|
|
#ifdef CONFIG_SHT3XD_PERIODIC_MODE
|
2016-02-22 16:22:16 +02:00
|
|
|
/* set periodic measurement mode */
|
2019-01-12 09:06:12 -06:00
|
|
|
if (sht3xd_write_command(dev,
|
2019-01-14 16:55:20 -06:00
|
|
|
measure_cmd[SHT3XD_MPS_IDX][SHT3XD_REPEATABILITY_IDX])
|
|
|
|
< 0) {
|
2018-10-10 12:08:54 +05:30
|
|
|
LOG_DBG("Failed to set measurement mode!");
|
2016-03-19 09:26:27 -04:00
|
|
|
return -EIO;
|
2016-02-22 16:22:16 +02:00
|
|
|
}
|
|
|
|
|
2018-12-09 10:07:48 -06:00
|
|
|
k_busy_wait(measure_wait[SHT3XD_REPEATABILITY_IDX]);
|
2019-09-14 13:20:36 +02:00
|
|
|
#endif
|
2016-02-22 16:22:16 +02:00
|
|
|
#ifdef CONFIG_SHT3XD_TRIGGER
|
2016-05-05 16:53:52 +03:00
|
|
|
if (sht3xd_init_interrupt(dev) < 0) {
|
2018-10-10 12:08:54 +05:30
|
|
|
LOG_DBG("Failed to initialize interrupt");
|
2016-03-19 09:26:27 -04:00
|
|
|
return -EIO;
|
2016-02-22 16:22:16 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-03-19 09:26:27 -04:00
|
|
|
return 0;
|
2016-02-22 16:22:16 +02:00
|
|
|
}
|
|
|
|
|
2019-01-12 09:06:12 -06:00
|
|
|
struct sht3xd_data sht3xd0_driver;
|
|
|
|
static const struct sht3xd_config sht3xd0_cfg = {
|
2020-03-30 10:08:08 -05:00
|
|
|
.bus_name = DT_INST_BUS_LABEL(0),
|
2019-01-12 09:06:12 -06:00
|
|
|
#ifdef CONFIG_SHT3XD_TRIGGER
|
2020-03-30 10:08:08 -05:00
|
|
|
.alert_gpio_name = DT_INST_GPIO_LABEL(0, alert_gpios),
|
2019-01-12 09:06:12 -06:00
|
|
|
#endif
|
2020-03-30 10:08:08 -05:00
|
|
|
.base_address = DT_INST_REG_ADDR(0),
|
2019-01-12 09:06:12 -06:00
|
|
|
#ifdef CONFIG_SHT3XD_TRIGGER
|
2020-03-30 10:08:08 -05:00
|
|
|
.alert_pin = DT_INST_GPIO_PIN(0, alert_gpios),
|
|
|
|
.alert_flags = DT_INST_GPIO_FLAGS(0, alert_gpios),
|
2019-01-12 09:06:12 -06:00
|
|
|
#endif
|
|
|
|
};
|
2016-02-22 16:22:16 +02:00
|
|
|
|
2021-04-28 11:51:35 +02:00
|
|
|
DEVICE_DT_INST_DEFINE(0, sht3xd_init, NULL,
|
2020-12-15 18:53:48 -06:00
|
|
|
&sht3xd0_driver, &sht3xd0_cfg,
|
2019-01-12 09:06:12 -06:00
|
|
|
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,
|
2018-02-04 17:50:16 +05:30
|
|
|
&sht3xd_driver_api);
|