Skip to content

Commit

Permalink
sensor: bmi160: convert to _dt_spec
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Jordan Yates authored and cfriedt committed Aug 10, 2021
1 parent 757bb42 commit 3682eb9
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 80 deletions.
100 changes: 46 additions & 54 deletions drivers/sensor/bmi160/bmi160.c
Expand Up @@ -31,7 +31,6 @@ static int bmi160_transceive(const struct device *dev, uint8_t reg,
bool write, void *buf, size_t length)
{
const struct bmi160_cfg *cfg = to_config(dev);
struct bmi160_data *data = to_data(dev);
const struct spi_buf tx_buf[2] = {
{
.buf = &reg,
Expand All @@ -53,57 +52,63 @@ static int bmi160_transceive(const struct device *dev, uint8_t reg,
.count = 2
};

return spi_transceive(data->bus, cfg->bus_cfg.spi_cfg, &tx,
&rx);
return spi_transceive_dt(&cfg->bus.spi, &tx, &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,
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_READ, false,
buf, len);
}

int bmi160_write_spi(const struct device *dev,
const struct bmi160_bus_cfg *bus_config,
uint8_t reg_addr, void *buf, uint8_t len)
{
return bmi160_transceive(dev, reg_addr & BMI160_REG_MASK, true,
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,
.write = bmi160_write_spi,
};
#endif /* BMI160_BUS_SPI */

#if BMI160_BUS_I2C

bool bmi160_bus_ready_i2c(const struct device *dev)
{
return device_is_ready(to_config(dev)->bus.i2c.bus);
}

int bmi160_read_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_read(data->bus, bus_config->i2c_addr, reg_addr, buf,
len);
return i2c_burst_read_dt(&cfg->bus.i2c, reg_addr, buf, len);
}

int bmi160_write_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);
const struct bmi160_cfg *cfg = to_config(dev);

return i2c_burst_write(data->bus, bus_config->i2c_addr, reg_addr, buf,
len);
return i2c_burst_write_dt(&cfg->bus.i2c, reg_addr, buf, 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,
.write = bmi160_write_i2c,
};
Expand All @@ -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);

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)
Expand Down Expand Up @@ -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);

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,
Expand Down Expand Up @@ -874,9 +879,8 @@ int bmi160_init(const struct device *dev)
uint8_t val = 0U;
int32_t acc_range, gyr_range;

data->bus = device_get_binding(cfg->bus_label);
if (!data->bus) {
LOG_DBG("SPI master controller not found: %s.", cfg->bus_label);
if (!cfg->bus_io->ready(dev)) {
LOG_ERR("Bus not ready");
return -EINVAL;
}

Expand Down Expand Up @@ -972,50 +976,38 @@ int bmi160_init(const struct device *dev)
}

#if defined(CONFIG_BMI160_TRIGGER)
#define BMI160_TRIGGER_CFG(inst) \
.gpio_port = DT_INST_GPIO_LABEL(inst, int_gpios), \
.int_pin = DT_INST_GPIO_PIN(inst, int_gpios), \
.int_flags = DT_INST_GPIO_FLAGS(inst, int_gpios),
#define BMI160_TRIGGER_CFG(inst) \
.interrupt = GPIO_DT_SPEC_INST_GET(inst, int_gpios),
#else
#define BMI160_TRIGGER_CFG(inst)
#endif

#define BMI160_DEVICE_INIT(inst) \
DEVICE_DT_INST_DEFINE(inst, bmi160_init, NULL, \
&bmi160_data_##inst, &bmi160_cfg_##inst, \
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, \
&bmi160_api);
&bmi160_data_##inst, &bmi160_cfg_##inst, \
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, \
&bmi160_api);

/* Instantiation macros used when a device is on a SPI bus */
#define BMI160_DEFINE_SPI(inst) \
static struct bmi160_data bmi160_data_##inst; \
static const struct bmi160_cfg bmi160_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), \
}), \
}, \
}; \
#define BMI160_DEFINE_SPI(inst) \
static struct bmi160_data bmi160_data_##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_DEVICE_INIT(inst)

/* Instantiation macros used when a device is on an I2C bus */
#define BMI160_CONFIG_I2C(inst) \
{ \
.bus_label = DT_INST_BUS_LABEL(inst), \
.reg_io = &bmi160_reg_io_i2c, \
.bus_cfg = { .i2c_addr = DT_INST_REG_ADDR(inst), } \
#define BMI160_CONFIG_I2C(inst) \
{ \
.bus.i2c = I2C_DT_SPEC_INST_GET(inst), \
.bus_io = &bmi160_bus_io_i2c, \
}

#define BMI160_DEFINE_I2C(inst) \
static struct bmi160_data bmi160_data_##inst; \
static const struct bmi160_cfg bmi160_cfg_##inst = \
BMI160_CONFIG_I2C(inst); \
#define BMI160_DEFINE_I2C(inst) \
static struct bmi160_data bmi160_data_##inst; \
static const struct bmi160_cfg bmi160_cfg_##inst = BMI160_CONFIG_I2C(inst); \
BMI160_DEVICE_INIT(inst)

/*
Expand Down
28 changes: 12 additions & 16 deletions drivers/sensor/bmi160/bmi160.h
Expand Up @@ -8,6 +8,7 @@
#ifndef ZEPHYR_DRIVERS_SENSOR_BMI160_BMI160_H_
#define ZEPHYR_DRIVERS_SENSOR_BMI160_BMI160_H_

#include <drivers/i2c.h>
#include <drivers/gpio.h>
#include <drivers/sensor.h>
#include <drivers/spi.h>
Expand Down Expand Up @@ -402,37 +403,32 @@ struct bmi160_range {
#define BMI160_BUS_SPI DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
#define BMI160_BUS_I2C DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)

struct bmi160_bus_cfg {
union {
union bmi160_bus {
#if BMI160_BUS_SPI
const struct spi_config *spi_cfg;
struct spi_dt_spec spi;
#endif
#if BMI160_BUS_I2C
uint16_t i2c_addr;
struct i2c_dt_spec i2c;
#endif
};
};

typedef int (*bmi160_reg_read_fn)(const struct device *bus,
const struct bmi160_bus_cfg *bus_cfg,
typedef bool (*bmi160_bus_ready_fn)(const struct device *dev);
typedef int (*bmi160_reg_read_fn)(const struct device *dev,
uint8_t reg_addr, void *data, uint8_t len);
typedef int (*bmi160_reg_write_fn)(const struct device *bus,
const struct bmi160_bus_cfg *bus_cfg,
typedef int (*bmi160_reg_write_fn)(const struct device *dev,
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_write_fn write;
};

struct bmi160_cfg {
struct bmi160_bus_cfg bus_cfg;
const struct bmi160_reg_io *reg_io;
const char *bus_label;
union bmi160_bus bus;
const struct bmi160_bus_io *bus_io;
#if defined(CONFIG_BMI160_TRIGGER)
const char *gpio_port;
gpio_pin_t int_pin;
gpio_dt_flags_t int_flags;
struct gpio_dt_spec interrupt;
#endif
};

Expand Down
17 changes: 7 additions & 10 deletions drivers/sensor/bmi160/bmi160_trigger.c
Expand Up @@ -268,9 +268,8 @@ int bmi160_trigger_mode_init(const struct device *dev)
struct bmi160_data *data = to_data(dev);
const struct bmi160_cfg *cfg = to_config(dev);

data->gpio = device_get_binding((char *)cfg->gpio_port);
if (!data->gpio) {
LOG_DBG("Gpio controller %s not found.", cfg->gpio_port);
if (!device_is_ready(cfg->interrupt.port)) {
LOG_DBG("GPIO port %s not ready", cfg->interrupt.port->name);
return -EINVAL;
}

Expand All @@ -284,7 +283,7 @@ int bmi160_trigger_mode_init(const struct device *dev)
(k_thread_entry_t)bmi160_thread_main,
data, NULL, NULL,
K_PRIO_COOP(CONFIG_BMI160_THREAD_PRIORITY),
0, K_NO_WAIT);
0, K_NO_WAIT);
#elif defined(CONFIG_BMI160_TRIGGER_GLOBAL_THREAD)
data->work.handler = bmi160_work_handler;
#endif
Expand All @@ -295,16 +294,14 @@ int bmi160_trigger_mode_init(const struct device *dev)
return -EIO;
}

gpio_pin_configure(data->gpio, cfg->int_pin,
GPIO_INPUT | cfg->int_flags);
gpio_pin_configure_dt(&cfg->interrupt, GPIO_INPUT);

gpio_init_callback(&data->gpio_cb,
bmi160_gpio_callback,
BIT(cfg->int_pin));
BIT(cfg->interrupt.pin));

gpio_add_callback(data->gpio, &data->gpio_cb);
gpio_pin_interrupt_configure(data->gpio, cfg->int_pin,
GPIO_INT_EDGE_TO_ACTIVE);
gpio_add_callback(cfg->interrupt.port, &data->gpio_cb);
gpio_pin_interrupt_configure_dt(&cfg->interrupt, GPIO_INT_EDGE_TO_ACTIVE);

return bmi160_byte_write(dev, BMI160_REG_INT_OUT_CTRL,
BMI160_INT1_OUT_EN | BMI160_INT1_EDGE_CTRL);
Expand Down

0 comments on commit 3682eb9

Please sign in to comment.