drivers/sensor: lsm6dso: Add multi-instance support
Make this driver multi-instance and use the new API. This commit makes use of some DT macro helpers In particular: - get bus devices with DEVICE_DT_GET - get SPI information with SPI_CONFIG_DT_INST - get drdy gpios with GPIO_DT_SPEC_GET Moreover the driver is now using the stmemsc common routines as requested in issue #33440 and it avoids the unnecessary declaration of both ctx_i2c and ctx_spi in the data structure. Signed-off-by: Armando Visconti <armando.visconti@st.com>
This commit is contained in:
parent
36eceba7e4
commit
c4b35c1d36
7 changed files with 161 additions and 241 deletions
|
@ -11,3 +11,5 @@ zephyr_library_sources_ifdef(CONFIG_LSM6DSO lsm6dso_i2c.c)
|
|||
zephyr_library_sources_ifdef(CONFIG_LSM6DSO lsm6dso_spi.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_LSM6DSO_SENSORHUB lsm6dso_shub.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_LSM6DSO_TRIGGER lsm6dso_trigger.c)
|
||||
|
||||
zephyr_library_include_directories(../stmemsc)
|
||||
|
|
|
@ -92,7 +92,7 @@ static inline int lsm6dso_reboot(const struct device *dev)
|
|||
{
|
||||
struct lsm6dso_data *data = dev->data;
|
||||
|
||||
if (lsm6dso_boot_set(data->ctx, 1) < 0) {
|
||||
if (lsm6dso_boot_set(&data->ctx, 1) < 0) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
|
@ -106,7 +106,7 @@ static int lsm6dso_accel_set_fs_raw(const struct device *dev, uint8_t fs)
|
|||
{
|
||||
struct lsm6dso_data *data = dev->data;
|
||||
|
||||
if (lsm6dso_xl_full_scale_set(data->ctx, fs) < 0) {
|
||||
if (lsm6dso_xl_full_scale_set(&data->ctx, fs) < 0) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
|
@ -119,7 +119,7 @@ static int lsm6dso_accel_set_odr_raw(const struct device *dev, uint8_t odr)
|
|||
{
|
||||
struct lsm6dso_data *data = dev->data;
|
||||
|
||||
if (lsm6dso_xl_data_rate_set(data->ctx, odr) < 0) {
|
||||
if (lsm6dso_xl_data_rate_set(&data->ctx, odr) < 0) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
|
@ -132,7 +132,7 @@ static int lsm6dso_gyro_set_fs_raw(const struct device *dev, uint8_t fs)
|
|||
{
|
||||
struct lsm6dso_data *data = dev->data;
|
||||
|
||||
if (lsm6dso_gy_full_scale_set(data->ctx, fs) < 0) {
|
||||
if (lsm6dso_gy_full_scale_set(&data->ctx, fs) < 0) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
|
@ -143,7 +143,7 @@ static int lsm6dso_gyro_set_odr_raw(const struct device *dev, uint8_t odr)
|
|||
{
|
||||
struct lsm6dso_data *data = dev->data;
|
||||
|
||||
if (lsm6dso_gy_data_rate_set(data->ctx, odr) < 0) {
|
||||
if (lsm6dso_gy_data_rate_set(&data->ctx, odr) < 0) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
|
@ -303,7 +303,7 @@ static int lsm6dso_sample_fetch_accel(const struct device *dev)
|
|||
struct lsm6dso_data *data = dev->data;
|
||||
union axis3bit16_t buf;
|
||||
|
||||
if (lsm6dso_acceleration_raw_get(data->ctx, buf.u8bit) < 0) {
|
||||
if (lsm6dso_acceleration_raw_get(&data->ctx, buf.u8bit) < 0) {
|
||||
LOG_DBG("Failed to read sample");
|
||||
return -EIO;
|
||||
}
|
||||
|
@ -320,7 +320,7 @@ static int lsm6dso_sample_fetch_gyro(const struct device *dev)
|
|||
struct lsm6dso_data *data = dev->data;
|
||||
union axis3bit16_t buf;
|
||||
|
||||
if (lsm6dso_angular_rate_raw_get(data->ctx, buf.u8bit) < 0) {
|
||||
if (lsm6dso_angular_rate_raw_get(&data->ctx, buf.u8bit) < 0) {
|
||||
LOG_DBG("Failed to read sample");
|
||||
return -EIO;
|
||||
}
|
||||
|
@ -338,7 +338,7 @@ static int lsm6dso_sample_fetch_temp(const struct device *dev)
|
|||
struct lsm6dso_data *data = dev->data;
|
||||
union axis1bit16_t buf;
|
||||
|
||||
if (lsm6dso_temperature_raw_get(data->ctx, buf.u8bit) < 0) {
|
||||
if (lsm6dso_temperature_raw_get(&data->ctx, buf.u8bit) < 0) {
|
||||
LOG_DBG("Failed to read sample");
|
||||
return -EIO;
|
||||
}
|
||||
|
@ -680,7 +680,7 @@ static int lsm6dso_channel_get(const struct device *dev,
|
|||
return 0;
|
||||
}
|
||||
|
||||
static const struct sensor_driver_api lsm6dso_api_funcs = {
|
||||
static const struct sensor_driver_api lsm6dso_driver_api = {
|
||||
.attr_set = lsm6dso_attr_set,
|
||||
#if CONFIG_LSM6DSO_TRIGGER
|
||||
.trigger_set = lsm6dso_trigger_set,
|
||||
|
@ -694,7 +694,7 @@ static int lsm6dso_init_chip(const struct device *dev)
|
|||
struct lsm6dso_data *lsm6dso = dev->data;
|
||||
uint8_t chip_id;
|
||||
|
||||
if (lsm6dso_device_id_get(lsm6dso->ctx, &chip_id) < 0) {
|
||||
if (lsm6dso_device_id_get(&lsm6dso->ctx, &chip_id) < 0) {
|
||||
LOG_DBG("Failed reading chip id");
|
||||
return -EIO;
|
||||
}
|
||||
|
@ -707,7 +707,7 @@ static int lsm6dso_init_chip(const struct device *dev)
|
|||
}
|
||||
|
||||
/* reset device */
|
||||
if (lsm6dso_reset_set(lsm6dso->ctx, 1) < 0) {
|
||||
if (lsm6dso_reset_set(&lsm6dso->ctx, 1) < 0) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
|
@ -739,12 +739,12 @@ static int lsm6dso_init_chip(const struct device *dev)
|
|||
}
|
||||
|
||||
/* Set FIFO bypass mode */
|
||||
if (lsm6dso_fifo_mode_set(lsm6dso->ctx, LSM6DSO_BYPASS_MODE) < 0) {
|
||||
if (lsm6dso_fifo_mode_set(&lsm6dso->ctx, LSM6DSO_BYPASS_MODE) < 0) {
|
||||
LOG_DBG("failed to set FIFO mode");
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
if (lsm6dso_block_data_update_set(lsm6dso->ctx, 1) < 0) {
|
||||
if (lsm6dso_block_data_update_set(&lsm6dso->ctx, 1) < 0) {
|
||||
LOG_DBG("failed to set BDU mode");
|
||||
return -EIO;
|
||||
}
|
||||
|
@ -752,56 +752,17 @@ static int lsm6dso_init_chip(const struct device *dev)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static struct lsm6dso_data lsm6dso_data;
|
||||
|
||||
static const struct lsm6dso_config lsm6dso_config = {
|
||||
.bus_name = DT_INST_BUS_LABEL(0),
|
||||
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
|
||||
.bus_init = lsm6dso_spi_init,
|
||||
.spi_conf.frequency = DT_INST_PROP(0, spi_max_frequency),
|
||||
.spi_conf.operation = (SPI_OP_MODE_MASTER | SPI_MODE_CPOL |
|
||||
SPI_MODE_CPHA | SPI_WORD_SET(8) |
|
||||
SPI_LINES_SINGLE),
|
||||
.spi_conf.slave = DT_INST_REG_ADDR(0),
|
||||
#if DT_INST_SPI_DEV_HAS_CS_GPIOS(0)
|
||||
.gpio_cs_port = DT_INST_SPI_DEV_CS_GPIOS_LABEL(0),
|
||||
.cs_gpio = DT_INST_SPI_DEV_CS_GPIOS_PIN(0),
|
||||
.cs_gpio_flags = DT_INST_SPI_DEV_CS_GPIOS_FLAGS(0),
|
||||
|
||||
.spi_conf.cs = &lsm6dso_data.cs_ctrl,
|
||||
#else
|
||||
.spi_conf.cs = NULL,
|
||||
#endif
|
||||
#elif DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
|
||||
.bus_init = lsm6dso_i2c_init,
|
||||
.i2c_slv_addr = DT_INST_REG_ADDR(0),
|
||||
#else
|
||||
#error "BUS MACRO NOT DEFINED IN DTS"
|
||||
#endif
|
||||
#ifdef CONFIG_LSM6DSO_TRIGGER
|
||||
.int_gpio_port = DT_INST_GPIO_LABEL(0, irq_gpios),
|
||||
.int_gpio_pin = DT_INST_GPIO_PIN(0, irq_gpios),
|
||||
.int_gpio_flags = DT_INST_GPIO_FLAGS(0, irq_gpios),
|
||||
.int_pin = DT_INST_PROP(0, int_pin),
|
||||
#endif /* CONFIG_LSM6DSO_TRIGGER */
|
||||
};
|
||||
|
||||
static int lsm6dso_init(const struct device *dev)
|
||||
{
|
||||
const struct lsm6dso_config * const config = dev->config;
|
||||
const struct lsm6dso_config * const cfg = dev->config;
|
||||
struct lsm6dso_data *data = dev->data;
|
||||
|
||||
data->dev = dev;
|
||||
|
||||
data->bus = device_get_binding(config->bus_name);
|
||||
if (!data->bus) {
|
||||
LOG_DBG("master not found: %s",
|
||||
config->bus_name);
|
||||
if (cfg->bus_init(dev) < 0) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
config->bus_init(dev);
|
||||
|
||||
#ifdef CONFIG_LSM6DSO_TRIGGER
|
||||
if (lsm6dso_init_interrupt(dev) < 0) {
|
||||
LOG_ERR("Failed to initialize interrupt.");
|
||||
|
@ -824,9 +785,79 @@ static int lsm6dso_init(const struct device *dev)
|
|||
return 0;
|
||||
}
|
||||
|
||||
#if DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) == 0
|
||||
#warning "LSM6DSO driver enabled without any devices"
|
||||
#endif
|
||||
|
||||
static struct lsm6dso_data lsm6dso_data;
|
||||
/*
|
||||
* Device creation macro, shared by LSM6DSO_DEFINE_SPI() and
|
||||
* LSM6DSO_DEFINE_I2C().
|
||||
*/
|
||||
|
||||
DEVICE_DT_INST_DEFINE(0, lsm6dso_init, device_pm_control_nop,
|
||||
&lsm6dso_data, &lsm6dso_config, POST_KERNEL,
|
||||
CONFIG_SENSOR_INIT_PRIORITY, &lsm6dso_api_funcs);
|
||||
#define LSM6DSO_DEVICE_INIT(inst) \
|
||||
DEVICE_DT_INST_DEFINE(inst, \
|
||||
lsm6dso_init, \
|
||||
device_pm_control_nop, \
|
||||
&lsm6dso_data_##inst, \
|
||||
&lsm6dso_config_##inst, \
|
||||
POST_KERNEL, \
|
||||
CONFIG_SENSOR_INIT_PRIORITY, \
|
||||
&lsm6dso_driver_api);
|
||||
|
||||
/*
|
||||
* Instantiation macros used when a device is on a SPI bus.
|
||||
*/
|
||||
|
||||
#ifdef CONFIG_LSM6DSO_TRIGGER
|
||||
#define LSM6DSO_CFG_IRQ(inst) \
|
||||
.gpio_drdy = GPIO_DT_SPEC_GET(DT_DRV_INST(inst), irq_gpios), \
|
||||
.int_pin = DT_INST_PROP(inst, int_pin)
|
||||
#else
|
||||
#define LSM6DSO_CFG_IRQ(inst)
|
||||
#endif /* CONFIG_LSM6DSO_TRIGGER */
|
||||
|
||||
#define LSM6DSO_SPI_OP (SPI_WORD_SET(8) | \
|
||||
SPI_OP_MODE_MASTER | \
|
||||
SPI_LINES_SINGLE | \
|
||||
SPI_MODE_CPOL | \
|
||||
SPI_MODE_CPHA) \
|
||||
|
||||
#define LSM6DSO_CONFIG_SPI(inst) \
|
||||
{ \
|
||||
.stmemsc_cfg.spi.bus = DEVICE_DT_GET(DT_INST_BUS(inst)),\
|
||||
.stmemsc_cfg.spi.spi_cfg = \
|
||||
SPI_CONFIG_DT_INST(inst, \
|
||||
LSM6DSO_SPI_OP, \
|
||||
0), \
|
||||
.bus_init = lsm6dso_spi_init, \
|
||||
COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, irq_gpios), \
|
||||
(LSM6DSO_CFG_IRQ(inst)), ()) \
|
||||
}
|
||||
|
||||
/*
|
||||
* Instantiation macros used when a device is on an I2C bus.
|
||||
*/
|
||||
|
||||
#define LSM6DSO_CONFIG_I2C(inst) \
|
||||
{ \
|
||||
.stmemsc_cfg.i2c.bus = DEVICE_DT_GET(DT_INST_BUS(inst)),\
|
||||
.stmemsc_cfg.i2c.i2c_slv_addr = DT_INST_REG_ADDR(inst), \
|
||||
.bus_init = lsm6dso_i2c_init, \
|
||||
COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, irq_gpios), \
|
||||
(LSM6DSO_CFG_IRQ(inst)), ()) \
|
||||
}
|
||||
|
||||
/*
|
||||
* Main instantiation macro. Use of COND_CODE_1() selects the right
|
||||
* bus-specific macro at preprocessor time.
|
||||
*/
|
||||
|
||||
#define LSM6DSO_DEFINE(inst) \
|
||||
static struct lsm6dso_data lsm6dso_data_##inst; \
|
||||
static const struct lsm6dso_config lsm6dso_config_##inst = \
|
||||
COND_CODE_1(DT_INST_ON_BUS(inst, spi), \
|
||||
(LSM6DSO_CONFIG_SPI(inst)), \
|
||||
(LSM6DSO_CONFIG_I2C(inst))); \
|
||||
LSM6DSO_DEVICE_INIT(inst)
|
||||
|
||||
DT_INST_FOREACH_STATUS_OKAY(LSM6DSO_DEFINE)
|
||||
|
|
|
@ -16,8 +16,17 @@
|
|||
#include <drivers/gpio.h>
|
||||
#include <drivers/spi.h>
|
||||
#include <sys/util.h>
|
||||
#include <stmemsc.h>
|
||||
#include "lsm6dso_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) */
|
||||
|
||||
union axis3bit16_t {
|
||||
int16_t i16bit[3];
|
||||
uint8_t u8bit[6];
|
||||
|
@ -92,24 +101,19 @@ union axis1bit16_t {
|
|||
#endif
|
||||
|
||||
struct lsm6dso_config {
|
||||
char *bus_name;
|
||||
union {
|
||||
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
|
||||
const struct stmemsc_cfg_i2c i2c;
|
||||
#endif
|
||||
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
|
||||
const struct stmemsc_cfg_spi spi;
|
||||
#endif
|
||||
} stmemsc_cfg;
|
||||
int (*bus_init)(const struct device *dev);
|
||||
#ifdef CONFIG_LSM6DSO_TRIGGER
|
||||
const char *int_gpio_port;
|
||||
uint8_t int_gpio_pin;
|
||||
uint8_t int_gpio_flags;
|
||||
const struct gpio_dt_spec gpio_drdy;
|
||||
uint8_t int_pin;
|
||||
#endif /* CONFIG_LSM6DSO_TRIGGER */
|
||||
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
|
||||
uint16_t i2c_slv_addr;
|
||||
#elif DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
|
||||
struct spi_config spi_conf;
|
||||
#if DT_INST_SPI_DEV_HAS_CS_GPIOS(0)
|
||||
const char *gpio_cs_port;
|
||||
uint8_t cs_gpio;
|
||||
uint8_t cs_gpio_flags;
|
||||
#endif /* DT_INST_SPI_DEV_HAS_CS_GPIOS(0) */
|
||||
#endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) */
|
||||
};
|
||||
|
||||
union samples {
|
||||
|
@ -139,7 +143,6 @@ struct lsm6dso_tf {
|
|||
|
||||
struct lsm6dso_data {
|
||||
const struct device *dev;
|
||||
const struct device *bus;
|
||||
int16_t acc[3];
|
||||
uint32_t acc_gain;
|
||||
int16_t gyro[3];
|
||||
|
@ -159,13 +162,7 @@ struct lsm6dso_data {
|
|||
} hts221;
|
||||
#endif /* CONFIG_LSM6DSO_SENSORHUB */
|
||||
|
||||
stmdev_ctx_t *ctx;
|
||||
|
||||
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
|
||||
stmdev_ctx_t ctx_i2c;
|
||||
#elif DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
|
||||
stmdev_ctx_t ctx_spi;
|
||||
#endif
|
||||
stmdev_ctx_t ctx;
|
||||
|
||||
uint16_t accel_freq;
|
||||
uint8_t accel_fs;
|
||||
|
@ -173,7 +170,6 @@ struct lsm6dso_data {
|
|||
uint8_t gyro_fs;
|
||||
|
||||
#ifdef CONFIG_LSM6DSO_TRIGGER
|
||||
const struct device *gpio;
|
||||
struct gpio_callback gpio_cb;
|
||||
sensor_trigger_handler_t handler_drdy_acc;
|
||||
sensor_trigger_handler_t handler_drdy_gyr;
|
||||
|
@ -187,10 +183,6 @@ struct lsm6dso_data {
|
|||
struct k_work work;
|
||||
#endif
|
||||
#endif /* CONFIG_LSM6DSO_TRIGGER */
|
||||
|
||||
#if DT_INST_SPI_DEV_HAS_CS_GPIOS(0)
|
||||
struct spi_cs_control cs_ctrl;
|
||||
#endif
|
||||
};
|
||||
|
||||
int lsm6dso_spi_init(const struct device *dev);
|
||||
|
|
|
@ -10,43 +10,28 @@
|
|||
|
||||
#define DT_DRV_COMPAT st_lsm6dso
|
||||
|
||||
#include <string.h>
|
||||
#include <drivers/i2c.h>
|
||||
#include <logging/log.h>
|
||||
|
||||
#include "lsm6dso.h"
|
||||
|
||||
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
|
||||
|
||||
LOG_MODULE_DECLARE(LSM6DSO, CONFIG_SENSOR_LOG_LEVEL);
|
||||
|
||||
static int lsm6dso_i2c_read(struct lsm6dso_data *data, uint8_t reg_addr,
|
||||
uint8_t *value, uint8_t len)
|
||||
{
|
||||
const struct lsm6dso_config *cfg = data->dev->config;
|
||||
|
||||
return i2c_burst_read(data->bus, cfg->i2c_slv_addr,
|
||||
reg_addr, value, len);
|
||||
}
|
||||
|
||||
static int lsm6dso_i2c_write(struct lsm6dso_data *data, uint8_t reg_addr,
|
||||
uint8_t *value, uint8_t len)
|
||||
{
|
||||
const struct lsm6dso_config *cfg = data->dev->config;
|
||||
|
||||
return i2c_burst_write(data->bus, cfg->i2c_slv_addr,
|
||||
reg_addr, value, len);
|
||||
}
|
||||
|
||||
int lsm6dso_i2c_init(const struct device *dev)
|
||||
{
|
||||
struct lsm6dso_data *data = dev->data;
|
||||
const struct lsm6dso_config *cfg = dev->config;
|
||||
|
||||
data->ctx_i2c.read_reg = (stmdev_read_ptr) lsm6dso_i2c_read,
|
||||
data->ctx_i2c.write_reg = (stmdev_write_ptr) lsm6dso_i2c_write,
|
||||
if (!device_is_ready(cfg->stmemsc_cfg.i2c.bus)) {
|
||||
LOG_ERR("Cannot get pointer to bus device");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
data->ctx = &data->ctx_i2c;
|
||||
data->ctx->handle = data;
|
||||
/* Use generic stmemsc routine for read/write I2C bus */
|
||||
data->ctx.read_reg = (stmdev_read_ptr) stmemsc_i2c_read,
|
||||
data->ctx.write_reg = (stmdev_write_ptr) stmemsc_i2c_write,
|
||||
|
||||
data->ctx.handle = (void *)&cfg->stmemsc_cfg.i2c;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -428,9 +428,9 @@ static inline void lsm6dso_shub_wait_completed(struct lsm6dso_data *data)
|
|||
static inline void lsm6dso_shub_embedded_en(struct lsm6dso_data *data, bool on)
|
||||
{
|
||||
if (on) {
|
||||
(void) lsm6dso_mem_bank_set(data->ctx, LSM6DSO_SENSOR_HUB_BANK);
|
||||
(void) lsm6dso_mem_bank_set(&data->ctx, LSM6DSO_SENSOR_HUB_BANK);
|
||||
} else {
|
||||
(void) lsm6dso_mem_bank_set(data->ctx, LSM6DSO_USER_BANK);
|
||||
(void) lsm6dso_mem_bank_set(&data->ctx, LSM6DSO_USER_BANK);
|
||||
}
|
||||
|
||||
k_busy_wait(150);
|
||||
|
@ -442,7 +442,7 @@ static int lsm6dso_shub_read_embedded_regs(struct lsm6dso_data *data,
|
|||
{
|
||||
lsm6dso_shub_embedded_en(data, true);
|
||||
|
||||
if (lsm6dso_read_reg(data->ctx, reg_addr, value, len) < 0) {
|
||||
if (lsm6dso_read_reg(&data->ctx, reg_addr, value, len) < 0) {
|
||||
LOG_DBG("shub: failed to read external reg: %02x", reg_addr);
|
||||
lsm6dso_shub_embedded_en(data, false);
|
||||
return -EIO;
|
||||
|
@ -459,7 +459,7 @@ static int lsm6dso_shub_write_embedded_regs(struct lsm6dso_data *data,
|
|||
{
|
||||
lsm6dso_shub_embedded_en(data, true);
|
||||
|
||||
if (lsm6dso_write_reg(data->ctx, reg_addr, value, len) < 0) {
|
||||
if (lsm6dso_write_reg(&data->ctx, reg_addr, value, len) < 0) {
|
||||
LOG_DBG("shub: failed to write external reg: %02x", reg_addr);
|
||||
lsm6dso_shub_embedded_en(data, false);
|
||||
return -EIO;
|
||||
|
@ -476,7 +476,7 @@ static void lsm6dso_shub_enable(struct lsm6dso_data *data, uint8_t enable)
|
|||
if (!data->accel_freq) {
|
||||
uint8_t odr = (enable) ? 2 : 0;
|
||||
|
||||
if (lsm6dso_xl_data_rate_set(data->ctx, odr) < 0) {
|
||||
if (lsm6dso_xl_data_rate_set(&data->ctx, odr) < 0) {
|
||||
LOG_DBG("shub: failed to set XL sampling rate");
|
||||
return;
|
||||
}
|
||||
|
@ -484,7 +484,7 @@ static void lsm6dso_shub_enable(struct lsm6dso_data *data, uint8_t enable)
|
|||
|
||||
lsm6dso_shub_embedded_en(data, true);
|
||||
|
||||
if (lsm6dso_sh_master_set(data->ctx, enable) < 0) {
|
||||
if (lsm6dso_sh_master_set(&data->ctx, enable) < 0) {
|
||||
LOG_DBG("shub: failed to set master on");
|
||||
lsm6dso_shub_embedded_en(data, false);
|
||||
return;
|
||||
|
@ -537,7 +537,7 @@ static int lsm6dso_shub_read_slave_reg(struct lsm6dso_data *data,
|
|||
|
||||
/* read data from external slave */
|
||||
lsm6dso_shub_embedded_en(data, true);
|
||||
if (lsm6dso_read_reg(data->ctx, LSM6DSO_SHUB_DATA_OUT,
|
||||
if (lsm6dso_read_reg(&data->ctx, LSM6DSO_SHUB_DATA_OUT,
|
||||
value, len) < 0) {
|
||||
LOG_DBG("shub: error reading sensor data");
|
||||
return -EIO;
|
||||
|
@ -642,14 +642,14 @@ static int lsm6dso_shub_set_data_channel(struct lsm6dso_data *data)
|
|||
/* Configure the master */
|
||||
lsm6dso_aux_sens_on_t aux = LSM6DSO_SLV_0_1_2;
|
||||
|
||||
if (lsm6dso_sh_slave_connected_set(data->ctx, aux) < 0) {
|
||||
if (lsm6dso_sh_slave_connected_set(&data->ctx, aux) < 0) {
|
||||
LOG_DBG("shub: error setting aux sensors");
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
lsm6dso_write_once_t wo = LSM6DSO_ONLY_FIRST_CYCLE;
|
||||
|
||||
if (lsm6dso_sh_write_mode_set(data->ctx, wo) < 0) {
|
||||
if (lsm6dso_sh_write_mode_set(&data->ctx, wo) < 0) {
|
||||
LOG_DBG("shub: error setting write once");
|
||||
return -EIO;
|
||||
}
|
||||
|
@ -689,7 +689,7 @@ int lsm6dso_shub_fetch_external_devs(const struct device *dev)
|
|||
for (n = 0; n < num_ext_dev; n++) {
|
||||
sp = &lsm6dso_shub_slist[shub_ext[n]];
|
||||
|
||||
if (lsm6dso_read_reg(data->ctx, sp->sh_out_reg,
|
||||
if (lsm6dso_read_reg(&data->ctx, sp->sh_out_reg,
|
||||
data->ext_data[n], sp->out_data_len) < 0) {
|
||||
LOG_DBG("shub: failed to read sample");
|
||||
return -EIO;
|
||||
|
|
|
@ -10,117 +10,28 @@
|
|||
|
||||
#define DT_DRV_COMPAT st_lsm6dso
|
||||
|
||||
#include <string.h>
|
||||
#include "lsm6dso.h"
|
||||
#include <logging/log.h>
|
||||
|
||||
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
|
||||
|
||||
#define LSM6DSO_SPI_READ (1 << 7)
|
||||
|
||||
LOG_MODULE_DECLARE(LSM6DSO, CONFIG_SENSOR_LOG_LEVEL);
|
||||
|
||||
static int lsm6dso_spi_read(struct lsm6dso_data *data, uint8_t reg_addr,
|
||||
uint8_t *value, uint8_t len)
|
||||
{
|
||||
const struct lsm6dso_config *cfg = data->dev->config;
|
||||
const struct spi_config *spi_cfg = &cfg->spi_conf;
|
||||
uint8_t buffer_tx[2] = { reg_addr | LSM6DSO_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 (len > 64) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
if (spi_transceive(data->bus, spi_cfg, &tx, &rx)) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int lsm6dso_spi_write(struct lsm6dso_data *data, uint8_t reg_addr,
|
||||
uint8_t *value, uint8_t len)
|
||||
{
|
||||
const struct lsm6dso_config *cfg = data->dev->config;
|
||||
const struct spi_config *spi_cfg = &cfg->spi_conf;
|
||||
uint8_t buffer_tx[1] = { reg_addr & ~LSM6DSO_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 (len > 64) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
if (spi_write(data->bus, spi_cfg, &tx)) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lsm6dso_spi_init(const struct device *dev)
|
||||
{
|
||||
struct lsm6dso_data *data = dev->data;
|
||||
|
||||
data->ctx_spi.read_reg = (stmdev_read_ptr) lsm6dso_spi_read;
|
||||
data->ctx_spi.write_reg = (stmdev_write_ptr) lsm6dso_spi_write;
|
||||
|
||||
data->ctx = &data->ctx_spi;
|
||||
data->ctx->handle = data;
|
||||
|
||||
#if DT_INST_SPI_DEV_HAS_CS_GPIOS(0)
|
||||
const struct lsm6dso_config *cfg = dev->config;
|
||||
|
||||
/* handle SPI CS thru GPIO if it is the case */
|
||||
data->cs_ctrl.gpio_dev = device_get_binding(cfg->gpio_cs_port);
|
||||
if (!data->cs_ctrl.gpio_dev) {
|
||||
LOG_ERR("Unable to get GPIO SPI CS device");
|
||||
if (!device_is_ready(cfg->stmemsc_cfg.spi.bus)) {
|
||||
LOG_ERR("Cannot get pointer to bus device");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
data->cs_ctrl.gpio_pin = cfg->cs_gpio;
|
||||
data->cs_ctrl.gpio_dt_flags = cfg->cs_gpio_flags;
|
||||
data->cs_ctrl.delay = 0;
|
||||
/* Use generic stmemsc routine for read/write SPI bus */
|
||||
data->ctx.read_reg = (stmdev_read_ptr) stmemsc_spi_read;
|
||||
data->ctx.write_reg = (stmdev_write_ptr) stmemsc_spi_write;
|
||||
|
||||
LOG_DBG("SPI GPIO CS configured on %s:%u",
|
||||
cfg->gpio_cs_port, cfg->cs_gpio);
|
||||
#endif
|
||||
data->ctx.handle = (void *)&cfg->stmemsc_cfg.spi;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -33,17 +33,17 @@ static int lsm6dso_enable_t_int(const struct device *dev, int enable)
|
|||
union axis1bit16_t buf;
|
||||
|
||||
/* dummy read: re-trigger interrupt */
|
||||
lsm6dso_temperature_raw_get(lsm6dso->ctx, buf.u8bit);
|
||||
lsm6dso_temperature_raw_get(&lsm6dso->ctx, buf.u8bit);
|
||||
}
|
||||
|
||||
/* set interrupt (TEMP DRDY interrupt is only on INT2) */
|
||||
if (cfg->int_pin == 1)
|
||||
return -EIO;
|
||||
|
||||
lsm6dso_read_reg(lsm6dso->ctx, LSM6DSO_INT2_CTRL,
|
||||
lsm6dso_read_reg(&lsm6dso->ctx, LSM6DSO_INT2_CTRL,
|
||||
(uint8_t *)&int2_ctrl, 1);
|
||||
int2_route.int2_ctrl.int2_drdy_temp = enable;
|
||||
return lsm6dso_write_reg(lsm6dso->ctx, LSM6DSO_INT2_CTRL,
|
||||
return lsm6dso_write_reg(&lsm6dso->ctx, LSM6DSO_INT2_CTRL,
|
||||
(uint8_t *)&int2_ctrl, 1);
|
||||
}
|
||||
#endif
|
||||
|
@ -60,26 +60,26 @@ static int lsm6dso_enable_xl_int(const struct device *dev, int enable)
|
|||
union axis3bit16_t buf;
|
||||
|
||||
/* dummy read: re-trigger interrupt */
|
||||
lsm6dso_acceleration_raw_get(lsm6dso->ctx, buf.u8bit);
|
||||
lsm6dso_acceleration_raw_get(&lsm6dso->ctx, buf.u8bit);
|
||||
}
|
||||
|
||||
/* set interrupt */
|
||||
if (cfg->int_pin == 1) {
|
||||
lsm6dso_int1_ctrl_t int1_ctrl;
|
||||
|
||||
lsm6dso_read_reg(lsm6dso->ctx, LSM6DSO_INT1_CTRL,
|
||||
lsm6dso_read_reg(&lsm6dso->ctx, LSM6DSO_INT1_CTRL,
|
||||
(uint8_t *)&int1_ctrl, 1);
|
||||
|
||||
int1_ctrl.int1_drdy_xl = enable;
|
||||
return lsm6dso_write_reg(lsm6dso->ctx, LSM6DSO_INT1_CTRL,
|
||||
return lsm6dso_write_reg(&lsm6dso->ctx, LSM6DSO_INT1_CTRL,
|
||||
(uint8_t *)&int1_ctrl, 1);
|
||||
} else {
|
||||
lsm6dso_int2_ctrl_t int2_ctrl;
|
||||
|
||||
lsm6dso_read_reg(lsm6dso->ctx, LSM6DSO_INT2_CTRL,
|
||||
lsm6dso_read_reg(&lsm6dso->ctx, LSM6DSO_INT2_CTRL,
|
||||
(uint8_t *)&int2_ctrl, 1);
|
||||
int2_ctrl.int2_drdy_xl = enable;
|
||||
return lsm6dso_write_reg(lsm6dso->ctx, LSM6DSO_INT2_CTRL,
|
||||
return lsm6dso_write_reg(&lsm6dso->ctx, LSM6DSO_INT2_CTRL,
|
||||
(uint8_t *)&int2_ctrl, 1);
|
||||
}
|
||||
}
|
||||
|
@ -96,25 +96,25 @@ static int lsm6dso_enable_g_int(const struct device *dev, int enable)
|
|||
union axis3bit16_t buf;
|
||||
|
||||
/* dummy read: re-trigger interrupt */
|
||||
lsm6dso_angular_rate_raw_get(lsm6dso->ctx, buf.u8bit);
|
||||
lsm6dso_angular_rate_raw_get(&lsm6dso->ctx, buf.u8bit);
|
||||
}
|
||||
|
||||
/* set interrupt */
|
||||
if (cfg->int_pin == 1) {
|
||||
lsm6dso_int1_ctrl_t int1_ctrl;
|
||||
|
||||
lsm6dso_read_reg(lsm6dso->ctx, LSM6DSO_INT1_CTRL,
|
||||
lsm6dso_read_reg(&lsm6dso->ctx, LSM6DSO_INT1_CTRL,
|
||||
(uint8_t *)&int1_ctrl, 1);
|
||||
int1_ctrl.int1_drdy_g = enable;
|
||||
return lsm6dso_write_reg(lsm6dso->ctx, LSM6DSO_INT1_CTRL,
|
||||
return lsm6dso_write_reg(&lsm6dso->ctx, LSM6DSO_INT1_CTRL,
|
||||
(uint8_t *)&int1_ctrl, 1);
|
||||
} else {
|
||||
lsm6dso_int2_ctrl_t int2_ctrl;
|
||||
|
||||
lsm6dso_read_reg(lsm6dso->ctx, LSM6DSO_INT2_CTRL,
|
||||
lsm6dso_read_reg(&lsm6dso->ctx, LSM6DSO_INT2_CTRL,
|
||||
(uint8_t *)&int2_ctrl, 1);
|
||||
int2_ctrl.int2_drdy_g = enable;
|
||||
return lsm6dso_write_reg(lsm6dso->ctx, LSM6DSO_INT2_CTRL,
|
||||
return lsm6dso_write_reg(&lsm6dso->ctx, LSM6DSO_INT2_CTRL,
|
||||
(uint8_t *)&int2_ctrl, 1);
|
||||
}
|
||||
}
|
||||
|
@ -171,7 +171,7 @@ static void lsm6dso_handle_interrupt(const struct device *dev)
|
|||
lsm6dso_status_reg_t status;
|
||||
|
||||
while (1) {
|
||||
if (lsm6dso_status_reg_get(lsm6dso->ctx, &status) < 0) {
|
||||
if (lsm6dso_status_reg_get(&lsm6dso->ctx, &status) < 0) {
|
||||
LOG_DBG("failed reading status reg");
|
||||
return;
|
||||
}
|
||||
|
@ -199,7 +199,7 @@ static void lsm6dso_handle_interrupt(const struct device *dev)
|
|||
#endif
|
||||
}
|
||||
|
||||
gpio_pin_interrupt_configure(lsm6dso->gpio, cfg->int_gpio_pin,
|
||||
gpio_pin_interrupt_configure(cfg->gpio_drdy.port, cfg->gpio_drdy.pin,
|
||||
GPIO_INT_EDGE_TO_ACTIVE);
|
||||
}
|
||||
|
||||
|
@ -212,7 +212,7 @@ static void lsm6dso_gpio_callback(const struct device *dev,
|
|||
|
||||
ARG_UNUSED(pins);
|
||||
|
||||
gpio_pin_interrupt_configure(lsm6dso->gpio, cfg->int_gpio_pin,
|
||||
gpio_pin_interrupt_configure(cfg->gpio_drdy.port, cfg->gpio_drdy.pin,
|
||||
GPIO_INT_DISABLE);
|
||||
|
||||
#if defined(CONFIG_LSM6DSO_TRIGGER_OWN_THREAD)
|
||||
|
@ -249,10 +249,8 @@ int lsm6dso_init_interrupt(const struct device *dev)
|
|||
int ret;
|
||||
|
||||
/* setup data ready gpio interrupt (INT1 or INT2) */
|
||||
lsm6dso->gpio = device_get_binding(cfg->int_gpio_port);
|
||||
if (lsm6dso->gpio == NULL) {
|
||||
LOG_DBG("Cannot get pointer to %s device",
|
||||
cfg->int_gpio_port);
|
||||
if (!device_is_ready(cfg->gpio_drdy.port)) {
|
||||
LOG_ERR("Cannot get pointer to drdy_gpio device");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
|
@ -268,8 +266,8 @@ int lsm6dso_init_interrupt(const struct device *dev)
|
|||
lsm6dso->work.handler = lsm6dso_work_cb;
|
||||
#endif /* CONFIG_LSM6DSO_TRIGGER_OWN_THREAD */
|
||||
|
||||
ret = gpio_pin_configure(lsm6dso->gpio, cfg->int_gpio_pin,
|
||||
GPIO_INPUT | cfg->int_gpio_flags);
|
||||
ret = gpio_pin_configure(cfg->gpio_drdy.port, cfg->gpio_drdy.pin,
|
||||
GPIO_INPUT | cfg->gpio_drdy.dt_flags);
|
||||
if (ret < 0) {
|
||||
LOG_DBG("Could not configure gpio");
|
||||
return ret;
|
||||
|
@ -277,20 +275,21 @@ int lsm6dso_init_interrupt(const struct device *dev)
|
|||
|
||||
gpio_init_callback(&lsm6dso->gpio_cb,
|
||||
lsm6dso_gpio_callback,
|
||||
BIT(cfg->int_gpio_pin));
|
||||
BIT(cfg->gpio_drdy.pin));
|
||||
|
||||
if (gpio_add_callback(lsm6dso->gpio, &lsm6dso->gpio_cb) < 0) {
|
||||
if (gpio_add_callback(cfg->gpio_drdy.port, &lsm6dso->gpio_cb) < 0) {
|
||||
LOG_DBG("Could not set gpio callback");
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
/* enable interrupt on int1/int2 in pulse mode */
|
||||
if (lsm6dso_int_notification_set(lsm6dso->ctx,
|
||||
if (lsm6dso_int_notification_set(&lsm6dso->ctx,
|
||||
LSM6DSO_ALL_INT_PULSED) < 0) {
|
||||
LOG_DBG("Could not set pulse mode");
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
return gpio_pin_interrupt_configure(lsm6dso->gpio, cfg->int_gpio_pin,
|
||||
return gpio_pin_interrupt_configure(cfg->gpio_drdy.port,
|
||||
cfg->gpio_drdy.pin,
|
||||
GPIO_INT_EDGE_TO_ACTIVE);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue