sensor: bmi160: convert to _dt_spec
Convert bmi160 driver to use `spi_dt_spec`, `i2c_dt_spec` and `gpio_dt_spec`. Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
This commit is contained in:
parent
757bb42d5f
commit
3682eb9714
3 changed files with 67 additions and 82 deletions
|
@ -31,7 +31,6 @@ static int bmi160_transceive(const struct device *dev, uint8_t reg,
|
||||||
bool write, void *buf, size_t length)
|
bool write, void *buf, size_t length)
|
||||||
{
|
{
|
||||||
const struct bmi160_cfg *cfg = to_config(dev);
|
const struct bmi160_cfg *cfg = to_config(dev);
|
||||||
struct bmi160_data *data = to_data(dev);
|
|
||||||
const struct spi_buf tx_buf[2] = {
|
const struct spi_buf tx_buf[2] = {
|
||||||
{
|
{
|
||||||
.buf = ®,
|
.buf = ®,
|
||||||
|
@ -53,57 +52,63 @@ static int bmi160_transceive(const struct device *dev, uint8_t reg,
|
||||||
.count = 2
|
.count = 2
|
||||||
};
|
};
|
||||||
|
|
||||||
return spi_transceive(data->bus, cfg->bus_cfg.spi_cfg, &tx,
|
return spi_transceive_dt(&cfg->bus.spi, &tx, &rx);
|
||||||
&rx);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return spi_write(data->bus, cfg->bus_cfg.spi_cfg, &tx);
|
return spi_write_dt(&cfg->bus.spi, &tx);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool bmi160_bus_ready_spi(const struct device *dev)
|
||||||
|
{
|
||||||
|
return spi_is_ready(&to_config(dev)->bus.spi);
|
||||||
}
|
}
|
||||||
|
|
||||||
int bmi160_read_spi(const struct device *dev,
|
int bmi160_read_spi(const struct device *dev,
|
||||||
const struct bmi160_bus_cfg *bus_config, uint8_t reg_addr,
|
uint8_t reg_addr, void *buf, uint8_t len)
|
||||||
void *buf, uint8_t len)
|
|
||||||
{
|
{
|
||||||
return bmi160_transceive(dev, reg_addr | BMI160_REG_READ, false,
|
return bmi160_transceive(dev, reg_addr | BMI160_REG_READ, false,
|
||||||
buf, len);
|
buf, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
int bmi160_write_spi(const struct device *dev,
|
int bmi160_write_spi(const struct device *dev,
|
||||||
const struct bmi160_bus_cfg *bus_config,
|
|
||||||
uint8_t reg_addr, void *buf, uint8_t len)
|
uint8_t reg_addr, void *buf, uint8_t len)
|
||||||
{
|
{
|
||||||
return bmi160_transceive(dev, reg_addr & BMI160_REG_MASK, true,
|
return bmi160_transceive(dev, reg_addr & BMI160_REG_MASK, true,
|
||||||
buf, len);
|
buf, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
static const struct bmi160_reg_io bmi160_reg_io_spi = {
|
static const struct bmi160_bus_io bmi160_bus_io_spi = {
|
||||||
|
.ready = bmi160_bus_ready_spi,
|
||||||
.read = bmi160_read_spi,
|
.read = bmi160_read_spi,
|
||||||
.write = bmi160_write_spi,
|
.write = bmi160_write_spi,
|
||||||
};
|
};
|
||||||
#endif /* BMI160_BUS_SPI */
|
#endif /* BMI160_BUS_SPI */
|
||||||
|
|
||||||
#if BMI160_BUS_I2C
|
#if BMI160_BUS_I2C
|
||||||
int bmi160_read_i2c(const struct device *dev,
|
|
||||||
const struct bmi160_bus_cfg *bus_config, uint8_t reg_addr,
|
|
||||||
void *buf, uint8_t len)
|
|
||||||
{
|
|
||||||
struct bmi160_data *data = to_data(dev);
|
|
||||||
|
|
||||||
return i2c_burst_read(data->bus, bus_config->i2c_addr, reg_addr, buf,
|
bool bmi160_bus_ready_i2c(const struct device *dev)
|
||||||
len);
|
{
|
||||||
|
return device_is_ready(to_config(dev)->bus.i2c.bus);
|
||||||
|
}
|
||||||
|
|
||||||
|
int bmi160_read_i2c(const struct device *dev,
|
||||||
|
uint8_t reg_addr, void *buf, uint8_t len)
|
||||||
|
{
|
||||||
|
const struct bmi160_cfg *cfg = to_config(dev);
|
||||||
|
|
||||||
|
return i2c_burst_read_dt(&cfg->bus.i2c, reg_addr, buf, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
int bmi160_write_i2c(const struct device *dev,
|
int bmi160_write_i2c(const struct device *dev,
|
||||||
const struct bmi160_bus_cfg *bus_config,
|
|
||||||
uint8_t reg_addr, void *buf, uint8_t len)
|
uint8_t reg_addr, void *buf, uint8_t len)
|
||||||
{
|
{
|
||||||
struct bmi160_data *data = to_data(dev);
|
const struct bmi160_cfg *cfg = to_config(dev);
|
||||||
|
|
||||||
return i2c_burst_write(data->bus, bus_config->i2c_addr, reg_addr, buf,
|
return i2c_burst_write_dt(&cfg->bus.i2c, reg_addr, buf, len);
|
||||||
len);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static const struct bmi160_reg_io bmi160_reg_io_i2c = {
|
static const struct bmi160_bus_io bmi160_bus_io_i2c = {
|
||||||
|
.ready = bmi160_bus_ready_i2c,
|
||||||
.read = bmi160_read_i2c,
|
.read = bmi160_read_i2c,
|
||||||
.write = bmi160_write_i2c,
|
.write = bmi160_write_i2c,
|
||||||
};
|
};
|
||||||
|
@ -114,7 +119,7 @@ int bmi160_read(const struct device *dev, uint8_t reg_addr, void *buf,
|
||||||
{
|
{
|
||||||
const struct bmi160_cfg *cfg = to_config(dev);
|
const struct bmi160_cfg *cfg = to_config(dev);
|
||||||
|
|
||||||
return cfg->reg_io->read(dev, &cfg->bus_cfg, reg_addr, buf, len);
|
return cfg->bus_io->read(dev, reg_addr, buf, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
int bmi160_byte_read(const struct device *dev, uint8_t reg_addr, uint8_t *byte)
|
int bmi160_byte_read(const struct device *dev, uint8_t reg_addr, uint8_t *byte)
|
||||||
|
@ -142,7 +147,7 @@ int bmi160_write(const struct device *dev, uint8_t reg_addr, void *buf,
|
||||||
{
|
{
|
||||||
const struct bmi160_cfg *cfg = to_config(dev);
|
const struct bmi160_cfg *cfg = to_config(dev);
|
||||||
|
|
||||||
return cfg->reg_io->write(dev, &cfg->bus_cfg, reg_addr, buf, len);
|
return cfg->bus_io->write(dev, reg_addr, buf, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
int bmi160_byte_write(const struct device *dev, uint8_t reg_addr,
|
int bmi160_byte_write(const struct device *dev, uint8_t reg_addr,
|
||||||
|
@ -874,9 +879,8 @@ int bmi160_init(const struct device *dev)
|
||||||
uint8_t val = 0U;
|
uint8_t val = 0U;
|
||||||
int32_t acc_range, gyr_range;
|
int32_t acc_range, gyr_range;
|
||||||
|
|
||||||
data->bus = device_get_binding(cfg->bus_label);
|
if (!cfg->bus_io->ready(dev)) {
|
||||||
if (!data->bus) {
|
LOG_ERR("Bus not ready");
|
||||||
LOG_DBG("SPI master controller not found: %s.", cfg->bus_label);
|
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -973,9 +977,7 @@ int bmi160_init(const struct device *dev)
|
||||||
|
|
||||||
#if defined(CONFIG_BMI160_TRIGGER)
|
#if defined(CONFIG_BMI160_TRIGGER)
|
||||||
#define BMI160_TRIGGER_CFG(inst) \
|
#define BMI160_TRIGGER_CFG(inst) \
|
||||||
.gpio_port = DT_INST_GPIO_LABEL(inst, int_gpios), \
|
.interrupt = GPIO_DT_SPEC_INST_GET(inst, int_gpios),
|
||||||
.int_pin = DT_INST_GPIO_PIN(inst, int_gpios), \
|
|
||||||
.int_flags = DT_INST_GPIO_FLAGS(inst, int_gpios),
|
|
||||||
#else
|
#else
|
||||||
#define BMI160_TRIGGER_CFG(inst)
|
#define BMI160_TRIGGER_CFG(inst)
|
||||||
#endif
|
#endif
|
||||||
|
@ -990,32 +992,22 @@ int bmi160_init(const struct device *dev)
|
||||||
#define BMI160_DEFINE_SPI(inst) \
|
#define BMI160_DEFINE_SPI(inst) \
|
||||||
static struct bmi160_data bmi160_data_##inst; \
|
static struct bmi160_data bmi160_data_##inst; \
|
||||||
static const struct bmi160_cfg bmi160_cfg_##inst = { \
|
static const struct bmi160_cfg bmi160_cfg_##inst = { \
|
||||||
|
.bus.spi = SPI_DT_SPEC_INST_GET(inst, SPI_WORD_SET(8), 0), \
|
||||||
|
.bus_io = &bmi160_bus_io_spi, \
|
||||||
BMI160_TRIGGER_CFG(inst) \
|
BMI160_TRIGGER_CFG(inst) \
|
||||||
.reg_io = &bmi160_reg_io_spi, \
|
|
||||||
.bus_label = DT_INST_BUS_LABEL(inst), \
|
|
||||||
.bus_cfg = { \
|
|
||||||
.spi_cfg = (&(struct spi_config) { \
|
|
||||||
.operation = SPI_WORD_SET(8), \
|
|
||||||
.frequency = DT_INST_PROP(inst, \
|
|
||||||
spi_max_frequency), \
|
|
||||||
.slave = DT_INST_REG_ADDR(inst), \
|
|
||||||
}), \
|
|
||||||
}, \
|
|
||||||
}; \
|
}; \
|
||||||
BMI160_DEVICE_INIT(inst)
|
BMI160_DEVICE_INIT(inst)
|
||||||
|
|
||||||
/* Instantiation macros used when a device is on an I2C bus */
|
/* Instantiation macros used when a device is on an I2C bus */
|
||||||
#define BMI160_CONFIG_I2C(inst) \
|
#define BMI160_CONFIG_I2C(inst) \
|
||||||
{ \
|
{ \
|
||||||
.bus_label = DT_INST_BUS_LABEL(inst), \
|
.bus.i2c = I2C_DT_SPEC_INST_GET(inst), \
|
||||||
.reg_io = &bmi160_reg_io_i2c, \
|
.bus_io = &bmi160_bus_io_i2c, \
|
||||||
.bus_cfg = { .i2c_addr = DT_INST_REG_ADDR(inst), } \
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#define BMI160_DEFINE_I2C(inst) \
|
#define BMI160_DEFINE_I2C(inst) \
|
||||||
static struct bmi160_data bmi160_data_##inst; \
|
static struct bmi160_data bmi160_data_##inst; \
|
||||||
static const struct bmi160_cfg bmi160_cfg_##inst = \
|
static const struct bmi160_cfg bmi160_cfg_##inst = BMI160_CONFIG_I2C(inst); \
|
||||||
BMI160_CONFIG_I2C(inst); \
|
|
||||||
BMI160_DEVICE_INIT(inst)
|
BMI160_DEVICE_INIT(inst)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
#ifndef ZEPHYR_DRIVERS_SENSOR_BMI160_BMI160_H_
|
#ifndef ZEPHYR_DRIVERS_SENSOR_BMI160_BMI160_H_
|
||||||
#define ZEPHYR_DRIVERS_SENSOR_BMI160_BMI160_H_
|
#define ZEPHYR_DRIVERS_SENSOR_BMI160_BMI160_H_
|
||||||
|
|
||||||
|
#include <drivers/i2c.h>
|
||||||
#include <drivers/gpio.h>
|
#include <drivers/gpio.h>
|
||||||
#include <drivers/sensor.h>
|
#include <drivers/sensor.h>
|
||||||
#include <drivers/spi.h>
|
#include <drivers/spi.h>
|
||||||
|
@ -402,37 +403,32 @@ struct bmi160_range {
|
||||||
#define BMI160_BUS_SPI DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
|
#define BMI160_BUS_SPI DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
|
||||||
#define BMI160_BUS_I2C DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
|
#define BMI160_BUS_I2C DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
|
||||||
|
|
||||||
struct bmi160_bus_cfg {
|
union bmi160_bus {
|
||||||
union {
|
|
||||||
#if BMI160_BUS_SPI
|
#if BMI160_BUS_SPI
|
||||||
const struct spi_config *spi_cfg;
|
struct spi_dt_spec spi;
|
||||||
#endif
|
#endif
|
||||||
#if BMI160_BUS_I2C
|
#if BMI160_BUS_I2C
|
||||||
uint16_t i2c_addr;
|
struct i2c_dt_spec i2c;
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
};
|
|
||||||
|
|
||||||
typedef int (*bmi160_reg_read_fn)(const struct device *bus,
|
typedef bool (*bmi160_bus_ready_fn)(const struct device *dev);
|
||||||
const struct bmi160_bus_cfg *bus_cfg,
|
typedef int (*bmi160_reg_read_fn)(const struct device *dev,
|
||||||
uint8_t reg_addr, void *data, uint8_t len);
|
uint8_t reg_addr, void *data, uint8_t len);
|
||||||
typedef int (*bmi160_reg_write_fn)(const struct device *bus,
|
typedef int (*bmi160_reg_write_fn)(const struct device *dev,
|
||||||
const struct bmi160_bus_cfg *bus_cfg,
|
|
||||||
uint8_t reg_addr, void *data, uint8_t len);
|
uint8_t reg_addr, void *data, uint8_t len);
|
||||||
|
|
||||||
struct bmi160_reg_io {
|
struct bmi160_bus_io {
|
||||||
|
bmi160_bus_ready_fn ready;
|
||||||
bmi160_reg_read_fn read;
|
bmi160_reg_read_fn read;
|
||||||
bmi160_reg_write_fn write;
|
bmi160_reg_write_fn write;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct bmi160_cfg {
|
struct bmi160_cfg {
|
||||||
struct bmi160_bus_cfg bus_cfg;
|
union bmi160_bus bus;
|
||||||
const struct bmi160_reg_io *reg_io;
|
const struct bmi160_bus_io *bus_io;
|
||||||
const char *bus_label;
|
|
||||||
#if defined(CONFIG_BMI160_TRIGGER)
|
#if defined(CONFIG_BMI160_TRIGGER)
|
||||||
const char *gpio_port;
|
struct gpio_dt_spec interrupt;
|
||||||
gpio_pin_t int_pin;
|
|
||||||
gpio_dt_flags_t int_flags;
|
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -268,9 +268,8 @@ int bmi160_trigger_mode_init(const struct device *dev)
|
||||||
struct bmi160_data *data = to_data(dev);
|
struct bmi160_data *data = to_data(dev);
|
||||||
const struct bmi160_cfg *cfg = to_config(dev);
|
const struct bmi160_cfg *cfg = to_config(dev);
|
||||||
|
|
||||||
data->gpio = device_get_binding((char *)cfg->gpio_port);
|
if (!device_is_ready(cfg->interrupt.port)) {
|
||||||
if (!data->gpio) {
|
LOG_DBG("GPIO port %s not ready", cfg->interrupt.port->name);
|
||||||
LOG_DBG("Gpio controller %s not found.", cfg->gpio_port);
|
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -295,16 +294,14 @@ int bmi160_trigger_mode_init(const struct device *dev)
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
gpio_pin_configure(data->gpio, cfg->int_pin,
|
gpio_pin_configure_dt(&cfg->interrupt, GPIO_INPUT);
|
||||||
GPIO_INPUT | cfg->int_flags);
|
|
||||||
|
|
||||||
gpio_init_callback(&data->gpio_cb,
|
gpio_init_callback(&data->gpio_cb,
|
||||||
bmi160_gpio_callback,
|
bmi160_gpio_callback,
|
||||||
BIT(cfg->int_pin));
|
BIT(cfg->interrupt.pin));
|
||||||
|
|
||||||
gpio_add_callback(data->gpio, &data->gpio_cb);
|
gpio_add_callback(cfg->interrupt.port, &data->gpio_cb);
|
||||||
gpio_pin_interrupt_configure(data->gpio, cfg->int_pin,
|
gpio_pin_interrupt_configure_dt(&cfg->interrupt, GPIO_INT_EDGE_TO_ACTIVE);
|
||||||
GPIO_INT_EDGE_TO_ACTIVE);
|
|
||||||
|
|
||||||
return bmi160_byte_write(dev, BMI160_REG_INT_OUT_CTRL,
|
return bmi160_byte_write(dev, BMI160_REG_INT_OUT_CTRL,
|
||||||
BMI160_INT1_OUT_EN | BMI160_INT1_EDGE_CTRL);
|
BMI160_INT1_OUT_EN | BMI160_INT1_EDGE_CTRL);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue