drivers/sensor: lis2ds12: Add multi-instance support
This commit aligns lis2ds12 sensor driver to latest multi instance sensor driver model. In particular it makes use of the stmemsc common routines and move ctx handler inside struct config, so that the bus_init routines can be totally avoided. Signed-off-by: Armando Visconti <armando.visconti@st.com>
This commit is contained in:
parent
1c0acad364
commit
f3ad909abd
6 changed files with 186 additions and 284 deletions
|
@ -3,6 +3,6 @@
|
|||
zephyr_library()
|
||||
|
||||
zephyr_library_sources(lis2ds12.c)
|
||||
zephyr_library_sources(lis2ds12_i2c.c)
|
||||
zephyr_library_sources(lis2ds12_spi.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_LIS2DS12_TRIGGER lis2ds12_trigger.c)
|
||||
|
||||
zephyr_library_include_directories(../stmemsc)
|
||||
|
|
|
@ -23,28 +23,10 @@
|
|||
|
||||
LOG_MODULE_REGISTER(LIS2DS12, CONFIG_SENSOR_LOG_LEVEL);
|
||||
|
||||
static struct lis2ds12_data lis2ds12_data;
|
||||
|
||||
static struct lis2ds12_config lis2ds12_config = {
|
||||
.comm_master_dev_name = DT_INST_BUS_LABEL(0),
|
||||
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
|
||||
.bus_init = lis2ds12_spi_init,
|
||||
#elif DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
|
||||
.bus_init = lis2ds12_i2c_init,
|
||||
#else
|
||||
#error "BUS MACRO NOT DEFINED IN DTS"
|
||||
#endif
|
||||
#ifdef CONFIG_LIS2DS12_TRIGGER
|
||||
.irq_port = DT_INST_GPIO_LABEL(0, irq_gpios),
|
||||
.irq_pin = DT_INST_GPIO_PIN(0, irq_gpios),
|
||||
.irq_flags = DT_INST_GPIO_FLAGS(0, irq_gpios),
|
||||
#endif
|
||||
};
|
||||
|
||||
static int lis2ds12_set_odr(const struct device *dev, uint16_t odr)
|
||||
{
|
||||
const struct lis2ds12_data *data = dev->data;
|
||||
stmdev_ctx_t *ctx = (stmdev_ctx_t *)data->ctx;
|
||||
const struct lis2ds12_config *cfg = dev->config;
|
||||
stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
|
||||
uint8_t val;
|
||||
|
||||
/* check if power off */
|
||||
|
@ -65,7 +47,8 @@ static int lis2ds12_set_range(const struct device *dev, uint8_t range)
|
|||
{
|
||||
int err;
|
||||
struct lis2ds12_data *data = dev->data;
|
||||
stmdev_ctx_t *ctx = (stmdev_ctx_t *)data->ctx;
|
||||
const struct lis2ds12_config *cfg = dev->config;
|
||||
stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
|
||||
|
||||
switch (range) {
|
||||
default:
|
||||
|
@ -127,7 +110,8 @@ static int lis2ds12_attr_set(const struct device *dev,
|
|||
static int lis2ds12_sample_fetch_accel(const struct device *dev)
|
||||
{
|
||||
struct lis2ds12_data *data = dev->data;
|
||||
stmdev_ctx_t *ctx = (stmdev_ctx_t *)data->ctx;
|
||||
const struct lis2ds12_config *cfg = dev->config;
|
||||
stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
|
||||
int16_t buf[3];
|
||||
|
||||
/* fetch raw data sample */
|
||||
|
@ -220,7 +204,7 @@ static int lis2ds12_channel_get(const struct device *dev,
|
|||
return lis2ds12_get_channel(chan, val, data, data->gain);
|
||||
}
|
||||
|
||||
static const struct sensor_driver_api lis2ds12_api_funcs = {
|
||||
static const struct sensor_driver_api lis2ds12_driver_api = {
|
||||
.attr_set = lis2ds12_attr_set,
|
||||
#if defined(CONFIG_LIS2DS12_TRIGGER)
|
||||
.trigger_set = lis2ds12_trigger_set,
|
||||
|
@ -231,31 +215,20 @@ static const struct sensor_driver_api lis2ds12_api_funcs = {
|
|||
|
||||
static int lis2ds12_init(const struct device *dev)
|
||||
{
|
||||
const struct lis2ds12_config * const config = dev->config;
|
||||
struct lis2ds12_data *data = dev->data;
|
||||
stmdev_ctx_t *ctx;
|
||||
const struct lis2ds12_config * const cfg = dev->config;
|
||||
stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
|
||||
uint8_t chip_id;
|
||||
int ret;
|
||||
|
||||
data->comm_master = device_get_binding(config->comm_master_dev_name);
|
||||
if (!data->comm_master) {
|
||||
LOG_ERR("master not found: %s",
|
||||
config->comm_master_dev_name);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
config->bus_init(dev);
|
||||
|
||||
ctx = (stmdev_ctx_t *)data->ctx;
|
||||
/* check chip ID */
|
||||
ret = lis2ds12_device_id_get(ctx, &chip_id);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Not able to read dev id");
|
||||
LOG_ERR("%s: Not able to read dev id", dev->name);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (chip_id != LIS2DS12_ID) {
|
||||
LOG_ERR("Invalid chip ID 0x%02x", chip_id);
|
||||
LOG_ERR("%s: Invalid chip ID 0x%02x", dev->name, chip_id);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
|
@ -267,32 +240,119 @@ static int lis2ds12_init(const struct device *dev)
|
|||
|
||||
k_busy_wait(100);
|
||||
|
||||
LOG_DBG("chip id 0x%x", chip_id);
|
||||
LOG_DBG("%s: chip id 0x%x", dev->name, chip_id);
|
||||
|
||||
#ifdef CONFIG_LIS2DS12_TRIGGER
|
||||
if (lis2ds12_trigger_init(dev) < 0) {
|
||||
LOG_ERR("Failed to initialize triggers.");
|
||||
return -EIO;
|
||||
ret = lis2ds12_trigger_init(dev);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("%s: Failed to initialize triggers", dev->name);
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* set sensor default odr */
|
||||
ret = lis2ds12_set_odr(dev, 12);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("odr init error (12.5 Hz)");
|
||||
LOG_ERR("%s: odr init error (12.5 Hz)", dev->name);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* set sensor default scale */
|
||||
ret = lis2ds12_set_range(dev, CONFIG_LIS2DS12_FS);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("range init error %d", CONFIG_LIS2DS12_FS);
|
||||
LOG_ERR("%s: range init error %d", dev->name, CONFIG_LIS2DS12_FS);
|
||||
return ret;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
DEVICE_DT_INST_DEFINE(0, lis2ds12_init, NULL,
|
||||
&lis2ds12_data, &lis2ds12_config, POST_KERNEL,
|
||||
CONFIG_SENSOR_INIT_PRIORITY, &lis2ds12_api_funcs);
|
||||
#if DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) == 0
|
||||
#warning "LIS2DS12 driver enabled without any devices"
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Device creation macro, shared by LIS2DS12_DEFINE_SPI() and
|
||||
* LIS2DS12_DEFINE_I2C().
|
||||
*/
|
||||
|
||||
#define LIS2DS12_DEVICE_INIT(inst) \
|
||||
DEVICE_DT_INST_DEFINE(inst, \
|
||||
lis2ds12_init, \
|
||||
NULL, \
|
||||
&lis2ds12_data_##inst, \
|
||||
&lis2ds12_config_##inst, \
|
||||
POST_KERNEL, \
|
||||
CONFIG_SENSOR_INIT_PRIORITY, \
|
||||
&lis2ds12_driver_api);
|
||||
|
||||
/*
|
||||
* Instantiation macros used when a device is on a SPI bus.
|
||||
*/
|
||||
|
||||
#ifdef CONFIG_LIS2DS12_TRIGGER
|
||||
#define LIS2DS12_CFG_IRQ(inst) \
|
||||
.gpio_int = GPIO_DT_SPEC_INST_GET(inst, irq_gpios),
|
||||
#else
|
||||
#define LIS2DS12_CFG_IRQ(inst)
|
||||
#endif /* CONFIG_LIS2DS12_TRIGGER */
|
||||
|
||||
#define LIS2DS12_SPI_OPERATION (SPI_WORD_SET(8) | \
|
||||
SPI_OP_MODE_MASTER | \
|
||||
SPI_MODE_CPOL | \
|
||||
SPI_MODE_CPHA) \
|
||||
|
||||
#define LIS2DS12_CONFIG_SPI(inst) \
|
||||
{ \
|
||||
.ctx = { \
|
||||
.read_reg = \
|
||||
(stmdev_read_ptr) stmemsc_spi_read, \
|
||||
.write_reg = \
|
||||
(stmdev_write_ptr) stmemsc_spi_write, \
|
||||
.handle = \
|
||||
(void *)&lis2ds12_config_##inst.stmemsc_cfg, \
|
||||
}, \
|
||||
.stmemsc_cfg = { \
|
||||
.spi = SPI_DT_SPEC_INST_GET(inst, \
|
||||
LIS2DS12_SPI_OPERATION, \
|
||||
0), \
|
||||
}, \
|
||||
COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, irq_gpios), \
|
||||
(LIS2DS12_CFG_IRQ(inst)), ()) \
|
||||
}
|
||||
|
||||
/*
|
||||
* Instantiation macros used when a device is on an I2C bus.
|
||||
*/
|
||||
|
||||
#define LIS2DS12_CONFIG_I2C(inst) \
|
||||
{ \
|
||||
.ctx = { \
|
||||
.read_reg = \
|
||||
(stmdev_read_ptr) stmemsc_i2c_read, \
|
||||
.write_reg = \
|
||||
(stmdev_write_ptr) stmemsc_i2c_write, \
|
||||
.handle = \
|
||||
(void *)&lis2ds12_config_##inst.stmemsc_cfg, \
|
||||
}, \
|
||||
.stmemsc_cfg = { \
|
||||
.i2c = I2C_DT_SPEC_INST_GET(inst), \
|
||||
}, \
|
||||
COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, irq_gpios), \
|
||||
(LIS2DS12_CFG_IRQ(inst)), ()) \
|
||||
}
|
||||
|
||||
/*
|
||||
* Main instantiation macro. Use of COND_CODE_1() selects the right
|
||||
* bus-specific macro at preprocessor time.
|
||||
*/
|
||||
|
||||
#define LIS2DS12_DEFINE(inst) \
|
||||
static struct lis2ds12_data lis2ds12_data_##inst; \
|
||||
static const struct lis2ds12_config lis2ds12_config_##inst = \
|
||||
COND_CODE_1(DT_INST_ON_BUS(inst, spi), \
|
||||
(LIS2DS12_CONFIG_SPI(inst)), \
|
||||
(LIS2DS12_CONFIG_I2C(inst))); \
|
||||
LIS2DS12_DEVICE_INIT(inst)
|
||||
|
||||
DT_INST_FOREACH_STATUS_OKAY(LIS2DS12_DEFINE)
|
||||
|
|
|
@ -14,33 +14,44 @@
|
|||
#include <zephyr/types.h>
|
||||
#include <drivers/sensor.h>
|
||||
#include <drivers/gpio.h>
|
||||
#include <stmemsc.h>
|
||||
#include "lis2ds12_reg.h"
|
||||
|
||||
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
|
||||
#include <drivers/spi.h>
|
||||
#endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) */
|
||||
|
||||
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
|
||||
#include <drivers/i2c.h>
|
||||
#endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) */
|
||||
|
||||
/* Return ODR reg value based on data rate set */
|
||||
#define LIS2DS12_HR_ODR_TO_REG(_odr) \
|
||||
((_odr <= 12) ? LIS2DS12_XL_ODR_12Hz5_HR : \
|
||||
((31 - __builtin_clz(_odr / 25))) + 2)
|
||||
|
||||
struct lis2ds12_config {
|
||||
char *comm_master_dev_name;
|
||||
int (*bus_init)(const struct device *dev);
|
||||
stmdev_ctx_t ctx;
|
||||
union {
|
||||
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
|
||||
const struct i2c_dt_spec i2c;
|
||||
#endif
|
||||
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
|
||||
const struct spi_dt_spec spi;
|
||||
#endif
|
||||
} stmemsc_cfg;
|
||||
#ifdef CONFIG_LIS2DS12_TRIGGER
|
||||
const char *irq_port;
|
||||
gpio_pin_t irq_pin;
|
||||
gpio_dt_flags_t irq_flags;
|
||||
struct gpio_dt_spec gpio_int;
|
||||
#endif
|
||||
};
|
||||
|
||||
struct lis2ds12_data {
|
||||
stmdev_ctx_t *ctx;
|
||||
const struct device *comm_master;
|
||||
int sample_x;
|
||||
int sample_y;
|
||||
int sample_z;
|
||||
float gain;
|
||||
|
||||
#ifdef CONFIG_LIS2DS12_TRIGGER
|
||||
const struct device *gpio;
|
||||
struct gpio_callback gpio_cb;
|
||||
|
||||
struct sensor_trigger data_ready_trigger;
|
||||
|
@ -58,9 +69,6 @@ struct lis2ds12_data {
|
|||
#endif /* CONFIG_LIS2DS12_TRIGGER */
|
||||
};
|
||||
|
||||
int lis2ds12_spi_init(const struct device *dev);
|
||||
int lis2ds12_i2c_init(const struct device *dev);
|
||||
|
||||
#ifdef CONFIG_LIS2DS12_TRIGGER
|
||||
int lis2ds12_trigger_set(const struct device *dev,
|
||||
const struct sensor_trigger *trig,
|
||||
|
|
|
@ -1,53 +0,0 @@
|
|||
/* ST Microelectronics LIS2DS12 3-axis accelerometer driver
|
||||
*
|
||||
* Copyright (c) 2019 STMicroelectronics
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Datasheet:
|
||||
* https://www.st.com/resource/en/datasheet/lis2ds12.pdf
|
||||
*/
|
||||
|
||||
#define DT_DRV_COMPAT st_lis2ds12
|
||||
|
||||
#include <string.h>
|
||||
#include <drivers/i2c.h>
|
||||
#include <logging/log.h>
|
||||
|
||||
#include "lis2ds12.h"
|
||||
|
||||
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
|
||||
|
||||
static uint16_t lis2ds12_i2c_slave_addr = DT_INST_REG_ADDR(0);
|
||||
|
||||
LOG_MODULE_DECLARE(LIS2DS12, CONFIG_SENSOR_LOG_LEVEL);
|
||||
|
||||
static int lis2ds12_i2c_read(struct lis2ds12_data *data, uint8_t reg_addr,
|
||||
uint8_t *value, uint16_t len)
|
||||
{
|
||||
return i2c_burst_read(data->comm_master, lis2ds12_i2c_slave_addr,
|
||||
reg_addr, value, len);
|
||||
}
|
||||
|
||||
static int lis2ds12_i2c_write(struct lis2ds12_data *data, uint8_t reg_addr,
|
||||
uint8_t *value, uint16_t len)
|
||||
{
|
||||
return i2c_burst_write(data->comm_master, lis2ds12_i2c_slave_addr,
|
||||
reg_addr, value, len);
|
||||
}
|
||||
|
||||
stmdev_ctx_t lis2ds12_i2c_ctx = {
|
||||
.read_reg = (stmdev_read_ptr) lis2ds12_i2c_read,
|
||||
.write_reg = (stmdev_write_ptr) lis2ds12_i2c_write,
|
||||
};
|
||||
|
||||
int lis2ds12_i2c_init(const struct device *dev)
|
||||
{
|
||||
struct lis2ds12_data *data = dev->data;
|
||||
|
||||
data->ctx = &lis2ds12_i2c_ctx;
|
||||
data->ctx->handle = data;
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) */
|
|
@ -1,134 +0,0 @@
|
|||
/* ST Microelectronics LIS2DS12 3-axis accelerometer driver
|
||||
*
|
||||
* Copyright (c) 2019 STMicroelectronics
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Datasheet:
|
||||
* https://www.st.com/resource/en/datasheet/lis2ds12.pdf
|
||||
*/
|
||||
|
||||
#define DT_DRV_COMPAT st_lis2ds12
|
||||
|
||||
|
||||
#include <string.h>
|
||||
#include <drivers/spi.h>
|
||||
#include "lis2ds12.h"
|
||||
#include <logging/log.h>
|
||||
|
||||
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
|
||||
|
||||
#define LIS2DS12_SPI_READ (1 << 7)
|
||||
|
||||
LOG_MODULE_DECLARE(LIS2DS12, CONFIG_SENSOR_LOG_LEVEL);
|
||||
|
||||
#if DT_INST_SPI_DEV_HAS_CS_GPIOS(0)
|
||||
static struct spi_cs_control lis2ds12_cs_ctrl;
|
||||
#endif
|
||||
|
||||
static struct spi_config lis2ds12_spi_conf = {
|
||||
.frequency = DT_INST_PROP(0, spi_max_frequency),
|
||||
.operation = (SPI_OP_MODE_MASTER | SPI_MODE_CPOL |
|
||||
SPI_MODE_CPHA | SPI_WORD_SET(8) | SPI_LINES_SINGLE),
|
||||
.slave = DT_INST_REG_ADDR(0),
|
||||
.cs = NULL,
|
||||
};
|
||||
|
||||
static int lis2ds12_spi_read(struct lis2ds12_data *data, uint8_t reg,
|
||||
uint8_t *value, uint16_t len)
|
||||
{
|
||||
struct spi_config *spi_cfg = &lis2ds12_spi_conf;
|
||||
uint8_t buffer_tx[2] = { reg | LIS2DS12_SPI_READ, 0 };
|
||||
const struct spi_buf tx_buf = {
|
||||
.buf = buffer_tx,
|
||||
.len = 2,
|
||||
};
|
||||
const struct spi_buf_set tx = {
|
||||
.buffers = &tx_buf,
|
||||
.count = 1
|
||||
};
|
||||
const struct spi_buf rx_buf[2] = {
|
||||
{
|
||||
.buf = NULL,
|
||||
.len = 1,
|
||||
},
|
||||
{
|
||||
.buf = value,
|
||||
.len = len,
|
||||
}
|
||||
};
|
||||
const struct spi_buf_set rx = {
|
||||
.buffers = rx_buf,
|
||||
.count = 2
|
||||
};
|
||||
|
||||
|
||||
if (spi_transceive(data->comm_master, spi_cfg, &tx, &rx)) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int lis2ds12_spi_write(struct lis2ds12_data *data, uint8_t reg,
|
||||
uint8_t *value, uint16_t len)
|
||||
{
|
||||
struct spi_config *spi_cfg = &lis2ds12_spi_conf;
|
||||
uint8_t buffer_tx[1] = { reg & ~LIS2DS12_SPI_READ };
|
||||
const struct spi_buf tx_buf[2] = {
|
||||
{
|
||||
.buf = buffer_tx,
|
||||
.len = 1,
|
||||
},
|
||||
{
|
||||
.buf = value,
|
||||
.len = len,
|
||||
}
|
||||
};
|
||||
const struct spi_buf_set tx = {
|
||||
.buffers = tx_buf,
|
||||
.count = 2
|
||||
};
|
||||
|
||||
if (spi_write(data->comm_master, spi_cfg, &tx)) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
stmdev_ctx_t lis2ds12_spi_ctx = {
|
||||
.read_reg = (stmdev_read_ptr)lis2ds12_spi_read,
|
||||
.write_reg = (stmdev_write_ptr)lis2ds12_spi_write,
|
||||
};
|
||||
|
||||
int lis2ds12_spi_init(const struct device *dev)
|
||||
{
|
||||
struct lis2ds12_data *data = dev->data;
|
||||
|
||||
data->ctx = &lis2ds12_spi_ctx;
|
||||
data->ctx->handle = data;
|
||||
|
||||
#if DT_INST_SPI_DEV_HAS_CS_GPIOS(0)
|
||||
/* handle SPI CS thru GPIO if it is the case */
|
||||
lis2ds12_cs_ctrl.gpio_dev = device_get_binding(
|
||||
DT_INST_SPI_DEV_CS_GPIOS_LABEL(0));
|
||||
if (!lis2ds12_cs_ctrl.gpio_dev) {
|
||||
LOG_ERR("Unable to get GPIO SPI CS device");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
lis2ds12_cs_ctrl.gpio_pin = DT_INST_SPI_DEV_CS_GPIOS_PIN(0);
|
||||
lis2ds12_cs_ctrl.gpio_dt_flags = DT_INST_SPI_DEV_CS_GPIOS_FLAGS(0);
|
||||
lis2ds12_cs_ctrl.delay = 0U;
|
||||
|
||||
lis2ds12_spi_conf.cs = &lis2ds12_cs_ctrl;
|
||||
|
||||
LOG_DBG("SPI GPIO CS configured on %s:%u",
|
||||
DT_INST_SPI_DEV_CS_GPIOS_LABEL(0),
|
||||
DT_INST_SPI_DEV_CS_GPIOS_PIN(0));
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) */
|
|
@ -8,12 +8,8 @@
|
|||
* https://www.st.com/resource/en/datasheet/lis2ds12.pdf
|
||||
*/
|
||||
|
||||
#include <device.h>
|
||||
#include <drivers/i2c.h>
|
||||
#include <sys/__assert.h>
|
||||
#include <sys/util.h>
|
||||
#include <kernel.h>
|
||||
#include <drivers/sensor.h>
|
||||
#define DT_DRV_COMPAT st_lis2ds12
|
||||
|
||||
#include <logging/log.h>
|
||||
#include "lis2ds12.h"
|
||||
|
||||
|
@ -25,11 +21,14 @@ static void lis2ds12_gpio_callback(const struct device *dev,
|
|||
struct lis2ds12_data *data =
|
||||
CONTAINER_OF(cb, struct lis2ds12_data, gpio_cb);
|
||||
const struct lis2ds12_config *cfg = data->dev->config;
|
||||
int ret;
|
||||
|
||||
ARG_UNUSED(pins);
|
||||
|
||||
gpio_pin_interrupt_configure(data->gpio, cfg->irq_pin,
|
||||
GPIO_INT_DISABLE);
|
||||
ret = gpio_pin_interrupt_configure_dt(&cfg->gpio_int, GPIO_INT_DISABLE);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("%s: Not able to configure pin_int", dev->name);
|
||||
}
|
||||
|
||||
#if defined(CONFIG_LIS2DS12_TRIGGER_OWN_THREAD)
|
||||
k_sem_give(&data->trig_sem);
|
||||
|
@ -49,10 +48,10 @@ static void lis2ds12_handle_drdy_int(const struct device *dev)
|
|||
|
||||
static void lis2ds12_handle_int(const struct device *dev)
|
||||
{
|
||||
struct lis2ds12_data *data = dev->data;
|
||||
const struct lis2ds12_config *cfg = dev->config;
|
||||
stmdev_ctx_t *ctx = (stmdev_ctx_t *)data->ctx;
|
||||
stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
|
||||
lis2ds12_all_sources_t sources;
|
||||
int ret;
|
||||
|
||||
lis2ds12_all_sources_get(ctx, &sources);
|
||||
|
||||
|
@ -60,8 +59,11 @@ static void lis2ds12_handle_int(const struct device *dev)
|
|||
lis2ds12_handle_drdy_int(dev);
|
||||
}
|
||||
|
||||
gpio_pin_interrupt_configure(data->gpio, cfg->irq_pin,
|
||||
GPIO_INT_EDGE_TO_ACTIVE);
|
||||
ret = gpio_pin_interrupt_configure_dt(&cfg->gpio_int,
|
||||
GPIO_INT_EDGE_TO_ACTIVE);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("%s: Not able to configure pin_int", dev->name);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef CONFIG_LIS2DS12_TRIGGER_OWN_THREAD
|
||||
|
@ -86,8 +88,8 @@ static void lis2ds12_work_cb(struct k_work *work)
|
|||
|
||||
static int lis2ds12_init_interrupt(const struct device *dev)
|
||||
{
|
||||
struct lis2ds12_data *data = dev->data;
|
||||
stmdev_ctx_t *ctx = (stmdev_ctx_t *)data->ctx;
|
||||
const struct lis2ds12_config *cfg = dev->config;
|
||||
stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
|
||||
lis2ds12_pin_int1_route_t route;
|
||||
int err;
|
||||
|
||||
|
@ -117,26 +119,40 @@ int lis2ds12_trigger_init(const struct device *dev)
|
|||
{
|
||||
struct lis2ds12_data *data = dev->data;
|
||||
const struct lis2ds12_config *cfg = dev->config;
|
||||
int ret;
|
||||
|
||||
/* setup data ready gpio interrupt */
|
||||
data->gpio = device_get_binding(cfg->irq_port);
|
||||
if (data->gpio == NULL) {
|
||||
LOG_ERR("Cannot get pointer to %s device.", cfg->irq_port);
|
||||
return -EINVAL;
|
||||
/* setup data ready gpio interrupt (INT1 or INT2) */
|
||||
if (!device_is_ready(cfg->gpio_int.port)) {
|
||||
if (cfg->gpio_int.port) {
|
||||
LOG_ERR("%s: device %s is not ready", dev->name,
|
||||
cfg->gpio_int.port->name);
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
LOG_DBG("%s: gpio_int not defined in DT", dev->name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
gpio_pin_configure(data->gpio, cfg->irq_pin,
|
||||
GPIO_INPUT | cfg->irq_flags);
|
||||
data->dev = dev;
|
||||
|
||||
ret = gpio_pin_configure_dt(&cfg->gpio_int, GPIO_INPUT);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Could not configure gpio");
|
||||
return ret;
|
||||
}
|
||||
|
||||
LOG_INF("%s: int on %s.%02u", dev->name, cfg->gpio_int.port->name,
|
||||
cfg->gpio_int.pin);
|
||||
|
||||
gpio_init_callback(&data->gpio_cb,
|
||||
lis2ds12_gpio_callback,
|
||||
BIT(cfg->irq_pin));
|
||||
BIT(cfg->gpio_int.pin));
|
||||
|
||||
if (gpio_add_callback(data->gpio, &data->gpio_cb) < 0) {
|
||||
LOG_ERR("Could not set gpio callback.");
|
||||
return -EIO;
|
||||
ret = gpio_add_callback(cfg->gpio_int.port, &data->gpio_cb);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Could not set gpio callback");
|
||||
return ret;
|
||||
}
|
||||
data->dev = dev;
|
||||
|
||||
#if defined(CONFIG_LIS2DS12_TRIGGER_OWN_THREAD)
|
||||
k_sem_init(&data->trig_sem, 0, K_SEM_MAX_LIMIT);
|
||||
|
@ -151,10 +167,8 @@ int lis2ds12_trigger_init(const struct device *dev)
|
|||
data->work.handler = lis2ds12_work_cb;
|
||||
#endif
|
||||
|
||||
gpio_pin_interrupt_configure(data->gpio, cfg->irq_pin,
|
||||
GPIO_INT_EDGE_TO_ACTIVE);
|
||||
|
||||
return 0;
|
||||
return gpio_pin_interrupt_configure_dt(&cfg->gpio_int,
|
||||
GPIO_INT_EDGE_TO_ACTIVE);
|
||||
}
|
||||
|
||||
int lis2ds12_trigger_set(const struct device *dev,
|
||||
|
@ -162,14 +176,23 @@ int lis2ds12_trigger_set(const struct device *dev,
|
|||
sensor_trigger_handler_t handler)
|
||||
{
|
||||
struct lis2ds12_data *data = dev->data;
|
||||
stmdev_ctx_t *ctx = (stmdev_ctx_t *)data->ctx;
|
||||
const struct lis2ds12_config *cfg = dev->config;
|
||||
stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
|
||||
int16_t raw[3];
|
||||
int ret;
|
||||
|
||||
__ASSERT_NO_MSG(trig->type == SENSOR_TRIG_DATA_READY);
|
||||
|
||||
gpio_pin_interrupt_configure(data->gpio, cfg->irq_pin,
|
||||
GPIO_INT_DISABLE);
|
||||
if (cfg->gpio_int.port == NULL) {
|
||||
LOG_ERR("trigger_set is not supported");
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
ret = gpio_pin_interrupt_configure_dt(&cfg->gpio_int, GPIO_INT_DISABLE);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("%s: Not able to configure pin_int", dev->name);
|
||||
return ret;
|
||||
}
|
||||
|
||||
data->data_ready_handler = handler;
|
||||
if (handler == NULL) {
|
||||
|
@ -183,8 +206,6 @@ int lis2ds12_trigger_set(const struct device *dev,
|
|||
data->data_ready_trigger = *trig;
|
||||
|
||||
lis2ds12_init_interrupt(dev);
|
||||
gpio_pin_interrupt_configure(data->gpio, cfg->irq_pin,
|
||||
GPIO_INT_EDGE_TO_ACTIVE);
|
||||
|
||||
return 0;
|
||||
return gpio_pin_interrupt_configure_dt(&cfg->gpio_int,
|
||||
GPIO_INT_EDGE_TO_ACTIVE);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue