drivers/sensors: Enable 3-wire SPI access to HTS221 sensor driver
HTS221 is a humidity and temperature sensor (thus HTS) that can be wired on i2c or SPI bus. On SPI bus however, it uses the 3-wire mode, aka: half-duplex. Now that SPI API exposes half duplex operation, let's enable the SPI bus on that sensor. Let's move to a better DTS integrated driver as well, and also use stmemsc interface. Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
This commit is contained in:
parent
f6be2835bd
commit
d5de0788ad
5 changed files with 210 additions and 104 deletions
|
@ -4,3 +4,5 @@ zephyr_library()
|
||||||
|
|
||||||
zephyr_library_sources(hts221.c)
|
zephyr_library_sources(hts221.c)
|
||||||
zephyr_library_sources_ifdef(CONFIG_HTS221_TRIGGER hts221_trigger.c)
|
zephyr_library_sources_ifdef(CONFIG_HTS221_TRIGGER hts221_trigger.c)
|
||||||
|
|
||||||
|
zephyr_library_include_directories(../stmemsc)
|
||||||
|
|
|
@ -3,9 +3,11 @@
|
||||||
|
|
||||||
menuconfig HTS221
|
menuconfig HTS221
|
||||||
bool "HTS221 temperature and humidity sensor"
|
bool "HTS221 temperature and humidity sensor"
|
||||||
depends on I2C
|
depends on I2C || SPI
|
||||||
|
select HAS_STMEMSC
|
||||||
|
select USE_STDC_HTS221
|
||||||
help
|
help
|
||||||
Enable driver for HTS221 I2C-based temperature and humidity sensor.
|
Enable driver for HTS221 I2C/SPI-based temperature and humidity sensor.
|
||||||
|
|
||||||
if HTS221
|
if HTS221
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,6 @@
|
||||||
#include <init.h>
|
#include <init.h>
|
||||||
#include <sys/__assert.h>
|
#include <sys/__assert.h>
|
||||||
#include <sys/byteorder.h>
|
#include <sys/byteorder.h>
|
||||||
#include <drivers/sensor.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <logging/log.h>
|
#include <logging/log.h>
|
||||||
|
|
||||||
|
@ -18,8 +17,15 @@
|
||||||
|
|
||||||
LOG_MODULE_REGISTER(HTS221, CONFIG_SENSOR_LOG_LEVEL);
|
LOG_MODULE_REGISTER(HTS221, CONFIG_SENSOR_LOG_LEVEL);
|
||||||
|
|
||||||
static const char * const hts221_odr_strings[] = {
|
struct str2odr {
|
||||||
"1", "7", "12.5"
|
const char *str;
|
||||||
|
hts221_odr_t odr;
|
||||||
|
};
|
||||||
|
|
||||||
|
static const struct str2odr hts221_odrs[] = {
|
||||||
|
{ "1", HTS221_ODR_1Hz },
|
||||||
|
{ "7", HTS221_ODR_7Hz },
|
||||||
|
{ "12.5", HTS221_ODR_12Hz5 },
|
||||||
};
|
};
|
||||||
|
|
||||||
static int hts221_channel_get(const struct device *dev,
|
static int hts221_channel_get(const struct device *dev,
|
||||||
|
@ -63,15 +69,17 @@ static int hts221_sample_fetch(const struct device *dev,
|
||||||
{
|
{
|
||||||
struct hts221_data *data = dev->data;
|
struct hts221_data *data = dev->data;
|
||||||
const struct hts221_config *cfg = dev->config;
|
const struct hts221_config *cfg = dev->config;
|
||||||
|
stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
|
||||||
uint8_t buf[4];
|
uint8_t buf[4];
|
||||||
|
int status;
|
||||||
|
|
||||||
__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);
|
__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);
|
||||||
|
|
||||||
if (i2c_burst_read(data->i2c, cfg->i2c_addr,
|
status = hts221_read_reg(ctx, HTS221_HUMIDITY_OUT_L |
|
||||||
HTS221_REG_DATA_START | HTS221_AUTOINCREMENT_ADDR,
|
HTS221_AUTOINCREMENT_ADDR, buf, 4);
|
||||||
buf, 4) < 0) {
|
if (status < 0) {
|
||||||
LOG_ERR("Failed to fetch data sample.");
|
LOG_ERR("Failed to fetch data sample.");
|
||||||
return -EIO;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
data->rh_sample = sys_le16_to_cpu(buf[0] | (buf[1] << 8));
|
data->rh_sample = sys_le16_to_cpu(buf[0] | (buf[1] << 8));
|
||||||
|
@ -84,13 +92,15 @@ static int hts221_read_conversion_data(const struct device *dev)
|
||||||
{
|
{
|
||||||
struct hts221_data *data = dev->data;
|
struct hts221_data *data = dev->data;
|
||||||
const struct hts221_config *cfg = dev->config;
|
const struct hts221_config *cfg = dev->config;
|
||||||
|
stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
|
||||||
uint8_t buf[16];
|
uint8_t buf[16];
|
||||||
|
int status;
|
||||||
|
|
||||||
if (i2c_burst_read(data->i2c, cfg->i2c_addr,
|
status = hts221_read_reg(ctx, HTS221_H0_RH_X2 |
|
||||||
HTS221_REG_CONVERSION_START |
|
HTS221_AUTOINCREMENT_ADDR, buf, 16);
|
||||||
HTS221_AUTOINCREMENT_ADDR, buf, 16) < 0) {
|
if (status < 0) {
|
||||||
LOG_ERR("Failed to read conversion data.");
|
LOG_ERR("Failed to read conversion data.");
|
||||||
return -EIO;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
data->h0_rh_x2 = buf[0];
|
data->h0_rh_x2 = buf[0];
|
||||||
|
@ -116,45 +126,51 @@ static const struct sensor_driver_api hts221_driver_api = {
|
||||||
int hts221_init(const struct device *dev)
|
int hts221_init(const struct device *dev)
|
||||||
{
|
{
|
||||||
const struct hts221_config *cfg = dev->config;
|
const struct hts221_config *cfg = dev->config;
|
||||||
struct hts221_data *data = dev->data;
|
stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
|
||||||
uint8_t id, idx;
|
uint8_t id, idx;
|
||||||
|
int status;
|
||||||
data->i2c = device_get_binding(cfg->i2c_bus);
|
|
||||||
if (data->i2c == NULL) {
|
|
||||||
LOG_ERR("Could not get pointer to %s device.", cfg->i2c_bus);
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* check chip ID */
|
/* check chip ID */
|
||||||
if (i2c_reg_read_byte(data->i2c, cfg->i2c_addr,
|
|
||||||
HTS221_REG_WHO_AM_I, &id) < 0) {
|
status = hts221_device_id_get(ctx, &id);
|
||||||
|
if (status < 0) {
|
||||||
LOG_ERR("Failed to read chip ID.");
|
LOG_ERR("Failed to read chip ID.");
|
||||||
return -EIO;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (id != HTS221_CHIP_ID) {
|
if (id != HTS221_ID) {
|
||||||
LOG_ERR("Invalid chip ID.");
|
LOG_ERR("Invalid chip ID.");
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* check if CONFIG_HTS221_ODR is valid */
|
/* check if CONFIG_HTS221_ODR is valid */
|
||||||
for (idx = 0U; idx < ARRAY_SIZE(hts221_odr_strings); idx++) {
|
for (idx = 0U; idx < ARRAY_SIZE(hts221_odrs); idx++) {
|
||||||
if (!strcmp(hts221_odr_strings[idx], CONFIG_HTS221_ODR)) {
|
if (!strcmp(hts221_odrs[idx].str, CONFIG_HTS221_ODR)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (idx == ARRAY_SIZE(hts221_odr_strings)) {
|
if (idx == ARRAY_SIZE(hts221_odrs)) {
|
||||||
LOG_ERR("Invalid ODR value.");
|
LOG_ERR("Invalid ODR value %s.", CONFIG_HTS221_ODR);
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i2c_reg_write_byte(data->i2c, cfg->i2c_addr,
|
status = hts221_data_rate_set(ctx, hts221_odrs[idx].odr);
|
||||||
HTS221_REG_CTRL1,
|
if (status < 0) {
|
||||||
(idx + 1) << HTS221_ODR_SHIFT | HTS221_BDU_BIT |
|
LOG_ERR("Could not set output data rate");
|
||||||
HTS221_PD_BIT) < 0) {
|
return status;
|
||||||
LOG_ERR("Failed to configure chip.");
|
}
|
||||||
return -EIO;
|
|
||||||
|
status = hts221_block_data_update_set(ctx, 1);
|
||||||
|
if (status < 0) {
|
||||||
|
LOG_ERR("Could not set BDU bit");
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
status = hts221_power_on_set(ctx, 1);
|
||||||
|
if (status < 0) {
|
||||||
|
LOG_ERR("Could not set PD bit");
|
||||||
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -163,15 +179,17 @@ int hts221_init(const struct device *dev)
|
||||||
*/
|
*/
|
||||||
k_sleep(K_MSEC(3));
|
k_sleep(K_MSEC(3));
|
||||||
|
|
||||||
if (hts221_read_conversion_data(dev) < 0) {
|
status = hts221_read_conversion_data(dev);
|
||||||
|
if (status < 0) {
|
||||||
LOG_ERR("Failed to read conversion data.");
|
LOG_ERR("Failed to read conversion data.");
|
||||||
return -EINVAL;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if HTS221_TRIGGER_ENABLED
|
#if HTS221_TRIGGER_ENABLED
|
||||||
if (hts221_init_interrupt(dev) < 0) {
|
status = hts221_init_interrupt(dev);
|
||||||
|
if (status < 0) {
|
||||||
LOG_ERR("Failed to initialize interrupt.");
|
LOG_ERR("Failed to initialize interrupt.");
|
||||||
return -EIO;
|
return status;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
LOG_INF("Cannot enable trigger without drdy-gpios");
|
LOG_INF("Cannot enable trigger without drdy-gpios");
|
||||||
|
@ -180,17 +198,92 @@ int hts221_init(const struct device *dev)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct hts221_data hts221_driver;
|
#if DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) == 0
|
||||||
static const struct hts221_config hts221_cfg = {
|
#warning "HTS221 driver enabled without any devices"
|
||||||
.i2c_bus = DT_INST_BUS_LABEL(0),
|
#endif
|
||||||
.i2c_addr = DT_INST_REG_ADDR(0),
|
|
||||||
#if HTS221_TRIGGER_ENABLED
|
|
||||||
.drdy_pin = DT_INST_GPIO_PIN(0, drdy_gpios),
|
|
||||||
.drdy_flags = DT_INST_GPIO_FLAGS(0, drdy_gpios),
|
|
||||||
.drdy_controller = DT_INST_GPIO_LABEL(0, drdy_gpios),
|
|
||||||
#endif /* HTS221_TRIGGER_ENABLED */
|
|
||||||
};
|
|
||||||
|
|
||||||
DEVICE_DT_INST_DEFINE(0, hts221_init, NULL,
|
/*
|
||||||
&hts221_driver, &hts221_cfg, POST_KERNEL,
|
* Device creation macros
|
||||||
CONFIG_SENSOR_INIT_PRIORITY, &hts221_driver_api);
|
*/
|
||||||
|
|
||||||
|
#define HTS221_DEVICE_INIT(inst) \
|
||||||
|
DEVICE_DT_INST_DEFINE(inst, \
|
||||||
|
hts221_init, \
|
||||||
|
NULL, \
|
||||||
|
&hts221_data_##inst, \
|
||||||
|
&hts221_config_##inst, \
|
||||||
|
POST_KERNEL, \
|
||||||
|
CONFIG_SENSOR_INIT_PRIORITY, \
|
||||||
|
&hts221_driver_api);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Instantiation macros used when a device is on a SPI bus.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifdef CONFIG_HTS221_TRIGGER
|
||||||
|
#define HTS221_CFG_IRQ(inst) \
|
||||||
|
.gpio_drdy = GPIO_DT_SPEC_INST_GET(inst, irq_gpios)
|
||||||
|
#else
|
||||||
|
#define HTS221_CFG_IRQ(inst)
|
||||||
|
#endif /* CONFIG_HTS221_TRIGGER */
|
||||||
|
|
||||||
|
#define HTS221_SPI_OPERATION (SPI_WORD_SET(8) | \
|
||||||
|
SPI_OP_MODE_MASTER | \
|
||||||
|
SPI_MODE_CPOL | \
|
||||||
|
SPI_MODE_CPHA | \
|
||||||
|
SPI_HALF_DUPLEX) \
|
||||||
|
|
||||||
|
#define HTS221_CONFIG_SPI(inst) \
|
||||||
|
{ \
|
||||||
|
.ctx = { \
|
||||||
|
.read_reg = \
|
||||||
|
(stmdev_read_ptr) stmemsc_spi_read, \
|
||||||
|
.write_reg = \
|
||||||
|
(stmdev_write_ptr) stmemsc_spi_write, \
|
||||||
|
.handle = \
|
||||||
|
(void *)&hts221_config_##inst.stmemsc_cfg, \
|
||||||
|
}, \
|
||||||
|
.stmemsc_cfg = { \
|
||||||
|
.spi = SPI_DT_SPEC_INST_GET(inst, \
|
||||||
|
HTS221_SPI_OPERATION, \
|
||||||
|
0), \
|
||||||
|
}, \
|
||||||
|
COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, irq_gpios), \
|
||||||
|
(HTS221_CFG_IRQ(inst)), ()) \
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Instantiation macros used when a device is on an I2C bus.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define HTS221_CONFIG_I2C(inst) \
|
||||||
|
{ \
|
||||||
|
.ctx = { \
|
||||||
|
.read_reg = \
|
||||||
|
(stmdev_read_ptr) stmemsc_i2c_read, \
|
||||||
|
.write_reg = \
|
||||||
|
(stmdev_write_ptr) stmemsc_i2c_write, \
|
||||||
|
.handle = \
|
||||||
|
(void *)&hts221_config_##inst.stmemsc_cfg, \
|
||||||
|
}, \
|
||||||
|
.stmemsc_cfg = { \
|
||||||
|
.i2c = I2C_DT_SPEC_INST_GET(inst), \
|
||||||
|
}, \
|
||||||
|
COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, irq_gpios), \
|
||||||
|
(HTS221_CFG_IRQ(inst)), ()) \
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Main instantiation macro. Use of COND_CODE_1() selects the right
|
||||||
|
* bus-specific macro at preprocessor time.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define HTS221_DEFINE(inst) \
|
||||||
|
static struct hts221_data hts221_data_##inst; \
|
||||||
|
static const struct hts221_config hts221_config_##inst = \
|
||||||
|
COND_CODE_1(DT_INST_ON_BUS(inst, spi), \
|
||||||
|
(HTS221_CONFIG_SPI(inst)), \
|
||||||
|
(HTS221_CONFIG_I2C(inst))); \
|
||||||
|
HTS221_DEVICE_INIT(inst)
|
||||||
|
|
||||||
|
DT_INST_FOREACH_STATUS_OKAY(HTS221_DEFINE)
|
||||||
|
|
|
@ -10,29 +10,23 @@
|
||||||
#include <device.h>
|
#include <device.h>
|
||||||
#include <sys/util.h>
|
#include <sys/util.h>
|
||||||
#include <zephyr/types.h>
|
#include <zephyr/types.h>
|
||||||
|
#include <stmemsc.h>
|
||||||
#include <drivers/gpio.h>
|
#include <drivers/gpio.h>
|
||||||
|
#include <drivers/sensor.h>
|
||||||
|
|
||||||
#define HTS221_TRIGGER_ENABLED (DT_INST_NODE_HAS_PROP(0, drdy_gpios) && \
|
#include "hts221_reg.h"
|
||||||
IS_ENABLED(CONFIG_HTS221_TRIGGER))
|
|
||||||
|
|
||||||
#define HTS221_AUTOINCREMENT_ADDR BIT(7)
|
#define HTS221_AUTOINCREMENT_ADDR BIT(7)
|
||||||
|
|
||||||
#define HTS221_REG_WHO_AM_I 0x0F
|
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
|
||||||
#define HTS221_CHIP_ID 0xBC
|
#include <drivers/spi.h>
|
||||||
|
#endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) */
|
||||||
|
|
||||||
#define HTS221_REG_CTRL1 0x20
|
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
|
||||||
#define HTS221_PD_BIT BIT(7)
|
#include <drivers/i2c.h>
|
||||||
#define HTS221_BDU_BIT BIT(2)
|
#endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) */
|
||||||
#define HTS221_ODR_SHIFT 0
|
|
||||||
|
|
||||||
#define HTS221_REG_CTRL3 0x22
|
|
||||||
#define HTS221_DRDY_EN BIT(2)
|
|
||||||
|
|
||||||
#define HTS221_REG_DATA_START 0x28
|
|
||||||
#define HTS221_REG_CONVERSION_START 0x30
|
|
||||||
|
|
||||||
struct hts221_data {
|
struct hts221_data {
|
||||||
const struct device *i2c;
|
|
||||||
int16_t rh_sample;
|
int16_t rh_sample;
|
||||||
int16_t t_sample;
|
int16_t t_sample;
|
||||||
|
|
||||||
|
@ -45,9 +39,8 @@ struct hts221_data {
|
||||||
int16_t t0_out;
|
int16_t t0_out;
|
||||||
int16_t t1_out;
|
int16_t t1_out;
|
||||||
|
|
||||||
#if HTS221_TRIGGER_ENABLED
|
#ifdef CONFIG_HTS221_TRIGGER
|
||||||
const struct device *dev;
|
const struct device *dev;
|
||||||
const struct device *drdy_dev;
|
|
||||||
struct gpio_callback drdy_cb;
|
struct gpio_callback drdy_cb;
|
||||||
|
|
||||||
struct sensor_trigger data_ready_trigger;
|
struct sensor_trigger data_ready_trigger;
|
||||||
|
@ -60,26 +53,35 @@ struct hts221_data {
|
||||||
#elif defined(CONFIG_HTS221_TRIGGER_GLOBAL_THREAD)
|
#elif defined(CONFIG_HTS221_TRIGGER_GLOBAL_THREAD)
|
||||||
struct k_work work;
|
struct k_work work;
|
||||||
#endif
|
#endif
|
||||||
|
#endif /* CONFIG_HTS221_TRIGGER */
|
||||||
#endif /* HTS221_TRIGGER_ENABLED */
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct hts221_config {
|
struct hts221_config {
|
||||||
const char *i2c_bus;
|
stmdev_ctx_t ctx;
|
||||||
uint16_t i2c_addr;
|
union {
|
||||||
#if HTS221_TRIGGER_ENABLED
|
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
|
||||||
gpio_pin_t drdy_pin;
|
const struct i2c_dt_spec i2c;
|
||||||
gpio_flags_t drdy_flags;
|
#endif
|
||||||
const char *drdy_controller;
|
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
|
||||||
#endif /* HTS221_TRIGGER_ENABLED */
|
const struct spi_dt_spec spi;
|
||||||
|
#endif
|
||||||
|
} stmemsc_cfg;
|
||||||
|
|
||||||
|
#ifdef CONFIG_HTS221_TRIGGER
|
||||||
|
const struct gpio_dt_spec gpio_drdy;
|
||||||
|
const struct gpio_dt_spec gpio_int;
|
||||||
|
#endif /* CONFIG_HTS221_TRIGGER */
|
||||||
};
|
};
|
||||||
|
|
||||||
#if HTS221_TRIGGER_ENABLED
|
#ifdef CONFIG_HTS221_TRIGGER
|
||||||
int hts221_trigger_set(const struct device *dev,
|
int hts221_trigger_set(const struct device *dev,
|
||||||
const struct sensor_trigger *trig,
|
const struct sensor_trigger *trig,
|
||||||
sensor_trigger_handler_t handler);
|
sensor_trigger_handler_t handler);
|
||||||
|
|
||||||
int hts221_init_interrupt(const struct device *dev);
|
int hts221_init_interrupt(const struct device *dev);
|
||||||
#endif
|
#endif /* CONFIG_HTS221_TRIGGER */
|
||||||
|
|
||||||
#endif /* __SENSOR_HTS221__ */
|
int hts221_spi_init(const struct device *dev);
|
||||||
|
int hts221_i2c_init(const struct device *dev);
|
||||||
|
|
||||||
|
#endif /* ZEPHYR_DRIVERS_SENSOR_HTS221_HTS221_H_ */
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2016 Intel Corporation
|
* Copyright (c) 2016-2021 Intel Corporation
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
@ -7,27 +7,24 @@
|
||||||
#define DT_DRV_COMPAT st_hts221
|
#define DT_DRV_COMPAT st_hts221
|
||||||
|
|
||||||
#include <device.h>
|
#include <device.h>
|
||||||
#include <drivers/i2c.h>
|
|
||||||
#include <sys/__assert.h>
|
#include <sys/__assert.h>
|
||||||
#include <sys/util.h>
|
#include <sys/util.h>
|
||||||
#include <kernel.h>
|
#include <kernel.h>
|
||||||
#include <drivers/sensor.h>
|
|
||||||
#include <logging/log.h>
|
#include <logging/log.h>
|
||||||
|
|
||||||
#include "hts221.h"
|
#include "hts221.h"
|
||||||
|
|
||||||
#if HTS221_TRIGGER_ENABLED
|
|
||||||
LOG_MODULE_DECLARE(HTS221, CONFIG_SENSOR_LOG_LEVEL);
|
LOG_MODULE_DECLARE(HTS221, CONFIG_SENSOR_LOG_LEVEL);
|
||||||
|
|
||||||
static inline void setup_drdy(const struct device *dev,
|
static inline void setup_drdy(const struct device *dev,
|
||||||
bool enable)
|
bool enable)
|
||||||
{
|
{
|
||||||
struct hts221_data *data = dev->data;
|
|
||||||
const struct hts221_config *cfg = dev->config;
|
const struct hts221_config *cfg = dev->config;
|
||||||
unsigned int flags = enable
|
unsigned int flags = enable
|
||||||
? GPIO_INT_EDGE_TO_ACTIVE
|
? GPIO_INT_EDGE_TO_ACTIVE
|
||||||
: GPIO_INT_DISABLE;
|
: GPIO_INT_DISABLE;
|
||||||
|
|
||||||
gpio_pin_interrupt_configure(data->drdy_dev, cfg->drdy_pin, flags);
|
gpio_pin_interrupt_configure_dt(&cfg->gpio_drdy, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void handle_drdy(const struct device *dev)
|
static inline void handle_drdy(const struct device *dev)
|
||||||
|
@ -79,7 +76,7 @@ int hts221_trigger_set(const struct device *dev,
|
||||||
/* If DRDY is active we probably won't get the rising edge, so
|
/* If DRDY is active we probably won't get the rising edge, so
|
||||||
* invoke the callback manually.
|
* invoke the callback manually.
|
||||||
*/
|
*/
|
||||||
if (gpio_pin_get(data->drdy_dev, cfg->drdy_pin) > 0) {
|
if (gpio_pin_get_dt(&cfg->gpio_drdy) > 0) {
|
||||||
handle_drdy(dev);
|
handle_drdy(dev);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,33 +118,44 @@ int hts221_init_interrupt(const struct device *dev)
|
||||||
{
|
{
|
||||||
struct hts221_data *data = dev->data;
|
struct hts221_data *data = dev->data;
|
||||||
const struct hts221_config *cfg = dev->config;
|
const struct hts221_config *cfg = dev->config;
|
||||||
|
stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
|
||||||
|
int status;
|
||||||
|
|
||||||
|
if (cfg->gpio_drdy.port == NULL) {
|
||||||
|
LOG_DBG("gpio_drdy not defined in DT");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!device_is_ready(cfg->gpio_drdy.port)) {
|
||||||
|
LOG_ERR("device %s is not ready", cfg->gpio_drdy.port->name);
|
||||||
|
return -ENODEV;
|
||||||
|
}
|
||||||
|
|
||||||
data->dev = dev;
|
data->dev = dev;
|
||||||
|
|
||||||
/* setup data ready gpio interrupt */
|
/* setup data ready gpio interrupt */
|
||||||
data->drdy_dev = device_get_binding(cfg->drdy_controller);
|
status = gpio_pin_configure_dt(&cfg->gpio_drdy, GPIO_INPUT);
|
||||||
if (data->drdy_dev == NULL) {
|
if (status < 0) {
|
||||||
LOG_ERR("Cannot get pointer to %s device.",
|
LOG_ERR("Could not configure %s.%02u",
|
||||||
cfg->drdy_controller);
|
cfg->gpio_drdy.port->name, cfg->gpio_drdy.pin);
|
||||||
return -EINVAL;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
gpio_pin_configure(data->drdy_dev, cfg->drdy_pin,
|
gpio_init_callback(&data->drdy_cb,
|
||||||
GPIO_INPUT | cfg->drdy_flags);
|
hts221_drdy_callback,
|
||||||
|
BIT(cfg->gpio_drdy.pin));
|
||||||
|
|
||||||
gpio_init_callback(&data->drdy_cb, hts221_drdy_callback,
|
status = gpio_add_callback(cfg->gpio_drdy.port, &data->drdy_cb);
|
||||||
BIT(cfg->drdy_pin));
|
if (status < 0) {
|
||||||
|
|
||||||
if (gpio_add_callback(data->drdy_dev, &data->drdy_cb) < 0) {
|
|
||||||
LOG_ERR("Could not set gpio callback.");
|
LOG_ERR("Could not set gpio callback.");
|
||||||
return -EIO;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* enable data-ready interrupt */
|
/* enable data-ready interrupt */
|
||||||
if (i2c_reg_write_byte(data->i2c, cfg->i2c_addr,
|
status = hts221_drdy_on_int_set(ctx, 1);
|
||||||
HTS221_REG_CTRL3, HTS221_DRDY_EN) < 0) {
|
if (status < 0) {
|
||||||
LOG_ERR("Could not enable data-ready interrupt.");
|
LOG_ERR("Could not enable data-ready interrupt.");
|
||||||
return -EIO;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(CONFIG_HTS221_TRIGGER_OWN_THREAD)
|
#if defined(CONFIG_HTS221_TRIGGER_OWN_THREAD)
|
||||||
|
@ -166,4 +174,3 @@ int hts221_init_interrupt(const struct device *dev)
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
#endif /* HTS221_TRIGGER_ENABLED */
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue