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
|
|
|
*/
|
|
|
|
|
|
|
|
#include <device.h>
|
|
|
|
#include <i2c.h>
|
2016-11-10 08:21:34 -05:00
|
|
|
#include <kernel.h>
|
2016-02-22 16:22:16 +02:00
|
|
|
#include <sensor.h>
|
2016-04-11 14:49:26 +03:00
|
|
|
#include <misc/__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
|
|
|
|
2018-10-10 12:08:54 +05:30
|
|
|
#define LOG_LEVEL CONFIG_SENSOR_LOG_LEVEL
|
|
|
|
LOG_MODULE_REGISTER(SHT3XD);
|
|
|
|
|
2018-12-09 10:07:48 -06:00
|
|
|
static const u16_t measure_cmd[5][3] = {
|
|
|
|
{ 0x202F, 0x2024, 0x2032 },
|
|
|
|
{ 0x212D, 0x2126, 0x2130 },
|
|
|
|
{ 0x222B, 0x2220, 0x2236 },
|
|
|
|
{ 0x2329, 0x2322, 0x2334 },
|
|
|
|
{ 0x272A, 0x2721, 0x2737 }
|
|
|
|
};
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2017-04-21 10:03:20 -05:00
|
|
|
static u8_t sht3xd_compute_crc(u16_t value)
|
2016-02-22 16:22:16 +02:00
|
|
|
{
|
2019-01-14 16:55:20 -06:00
|
|
|
u8_t buf[2] = { value >> 8, value & 0xFF };
|
2017-04-21 10:03:20 -05:00
|
|
|
u8_t crc = 0xFF;
|
|
|
|
u8_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;
|
|
|
|
}
|
|
|
|
|
2019-01-12 09:06:12 -06:00
|
|
|
int sht3xd_write_command(struct device *dev, u16_t cmd)
|
2016-02-22 16:22:16 +02:00
|
|
|
{
|
2019-01-14 16:55:20 -06:00
|
|
|
u8_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
|
|
|
}
|
|
|
|
|
2019-01-12 09:06:12 -06:00
|
|
|
int sht3xd_write_reg(struct device *dev, u16_t cmd, u16_t val)
|
2016-02-22 16:22:16 +02:00
|
|
|
{
|
2017-04-21 10:03:20 -05:00
|
|
|
u8_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
|
|
|
}
|
|
|
|
|
2016-04-11 14:49:26 +03:00
|
|
|
static int sht3xd_sample_fetch(struct device *dev, enum sensor_channel chan)
|
2016-02-22 16:22:16 +02:00
|
|
|
{
|
2019-01-12 09:06:12 -06:00
|
|
|
struct sht3xd_data *data = dev->driver_data;
|
|
|
|
struct device *i2c = sht3xd_i2c_device(dev);
|
|
|
|
u8_t address = sht3xd_i2c_address(dev);
|
2017-04-21 10:03:20 -05:00
|
|
|
u8_t rx_buf[6];
|
|
|
|
u16_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
|
|
|
|
2017-04-21 10:03:20 -05:00
|
|
|
u8_t tx_buf[2] = {
|
2016-02-22 16:22:16 +02:00
|
|
|
SHT3XD_CMD_FETCH >> 8,
|
|
|
|
SHT3XD_CMD_FETCH & 0xFF
|
|
|
|
};
|
|
|
|
|
|
|
|
struct i2c_msg msgs[2] = {
|
|
|
|
{
|
|
|
|
.buf = tx_buf,
|
|
|
|
.len = sizeof(tx_buf),
|
|
|
|
.flags = I2C_MSG_WRITE | I2C_MSG_RESTART,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.buf = rx_buf,
|
|
|
|
.len = sizeof(rx_buf),
|
|
|
|
.flags = I2C_MSG_READ | I2C_MSG_STOP,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-01-12 09:06:12 -06:00
|
|
|
if (i2c_transfer(i2c, msgs, 2, address) < 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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static int sht3xd_channel_get(struct device *dev,
|
|
|
|
enum sensor_channel chan,
|
|
|
|
struct sensor_value *val)
|
|
|
|
{
|
2019-01-12 09:06:12 -06:00
|
|
|
const struct sht3xd_data *data = dev->driver_data;
|
2017-04-21 10:03:20 -05:00
|
|
|
u64_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) */
|
2019-01-12 09:06:12 -06:00
|
|
|
tmp = 175 * (u64_t)data->t_sample;
|
2017-04-21 10:03:20 -05:00
|
|
|
val->val1 = (s32_t)(tmp / 0xFFFF) - 45;
|
2016-02-22 16:22:16 +02:00
|
|
|
val->val2 = (1000000 * (tmp % 0xFFFF)) / 0xFFFF;
|
|
|
|
} 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) */
|
2019-01-12 09:06:12 -06:00
|
|
|
u32_t tmp2 = 100 * (u32_t)data->rh_sample;
|
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 */
|
|
|
|
val->val2 = (tmp2 % 0xFFFF) * 15625 / 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,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int sht3xd_init(struct device *dev)
|
|
|
|
{
|
2019-01-12 09:06:12 -06:00
|
|
|
struct sht3xd_data *data = dev->driver_data;
|
|
|
|
const struct sht3xd_config *cfg = dev->config->config_info;
|
|
|
|
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
|
|
|
|
|
|
|
/* 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]);
|
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 = {
|
|
|
|
.bus_name = DT_SENSIRION_SHT3XD_0_BUS_NAME,
|
|
|
|
#ifdef CONFIG_SHT3XD_TRIGGER
|
|
|
|
.alert_gpio_name = DT_SENSIRION_SHT3XD_0_ALERT_GPIOS_CONTROLLER,
|
|
|
|
#endif
|
|
|
|
.base_address = DT_SENSIRION_SHT3XD_0_BASE_ADDRESS,
|
|
|
|
#ifdef CONFIG_SHT3XD_TRIGGER
|
|
|
|
.alert_pin = DT_SENSIRION_SHT3XD_0_ALERT_GPIOS_PIN,
|
|
|
|
#endif
|
|
|
|
};
|
2016-02-22 16:22:16 +02:00
|
|
|
|
2019-01-12 09:06:12 -06:00
|
|
|
DEVICE_AND_API_INIT(sht3xd0, DT_SENSIRION_SHT3XD_0_LABEL,
|
|
|
|
sht3xd_init, &sht3xd0_driver, &sht3xd0_cfg,
|
|
|
|
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,
|
2018-02-04 17:50:16 +05:30
|
|
|
&sht3xd_driver_api);
|