sensor: bme280: convert to _dt_spec
Convert bme280 driver to use `spi_dt_spec` and `i2c_dt_spec`. Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
This commit is contained in:
parent
75682f7d0e
commit
757bb42d5f
4 changed files with 39 additions and 84 deletions
|
@ -58,9 +58,8 @@ struct bme280_data {
|
|||
};
|
||||
|
||||
struct bme280_config {
|
||||
const struct device *bus;
|
||||
union bme280_bus bus;
|
||||
const struct bme280_bus_io *bus_io;
|
||||
const union bme280_bus_config bus_config;
|
||||
};
|
||||
|
||||
static inline struct bme280_data *to_data(const struct device *dev)
|
||||
|
@ -68,39 +67,27 @@ static inline struct bme280_data *to_data(const struct device *dev)
|
|||
return dev->data;
|
||||
}
|
||||
|
||||
static inline const struct bme280_config *to_config(const struct device *dev)
|
||||
{
|
||||
return dev->config;
|
||||
}
|
||||
|
||||
static inline const struct device *to_bus(const struct device *dev)
|
||||
{
|
||||
return to_config(dev)->bus;
|
||||
}
|
||||
|
||||
static inline const union bme280_bus_config*
|
||||
to_bus_config(const struct device *dev)
|
||||
{
|
||||
return &to_config(dev)->bus_config;
|
||||
}
|
||||
|
||||
static inline int bme280_bus_check(const struct device *dev)
|
||||
{
|
||||
return to_config(dev)->bus_io->check(to_bus(dev), to_bus_config(dev));
|
||||
const struct bme280_config *cfg = dev->config;
|
||||
|
||||
return cfg->bus_io->check(&cfg->bus);
|
||||
}
|
||||
|
||||
static inline int bme280_reg_read(const struct device *dev,
|
||||
uint8_t start, uint8_t *buf, int size)
|
||||
{
|
||||
return to_config(dev)->bus_io->read(to_bus(dev), to_bus_config(dev),
|
||||
start, buf, size);
|
||||
const struct bme280_config *cfg = dev->config;
|
||||
|
||||
return cfg->bus_io->read(&cfg->bus, start, buf, size);
|
||||
}
|
||||
|
||||
static inline int bme280_reg_write(const struct device *dev, uint8_t reg,
|
||||
uint8_t val)
|
||||
{
|
||||
return to_config(dev)->bus_io->write(to_bus(dev), to_bus_config(dev),
|
||||
reg, val);
|
||||
const struct bme280_config *cfg = dev->config;
|
||||
|
||||
return cfg->bus_io->write(&cfg->bus, reg, val);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -340,9 +327,6 @@ static int bme280_chip_init(const struct device *dev)
|
|||
struct bme280_data *data = to_data(dev);
|
||||
int err;
|
||||
|
||||
LOG_DBG("initializing \"%s\" on bus \"%s\"",
|
||||
dev->name, to_bus(dev)->name);
|
||||
|
||||
err = bme280_bus_check(dev);
|
||||
if (err < 0) {
|
||||
LOG_DBG("bus check failed: %d", err);
|
||||
|
@ -438,22 +422,18 @@ int bme280_pm_ctrl(const struct device *dev, enum pm_device_action action)
|
|||
#endif /* CONFIG_PM_DEVICE */
|
||||
|
||||
/* Initializes a struct bme280_config for an instance on a SPI bus. */
|
||||
#define BME280_CONFIG_SPI(inst) \
|
||||
{ \
|
||||
.bus = DEVICE_DT_GET(DT_INST_BUS(inst)), \
|
||||
.bus_io = &bme280_bus_io_spi, \
|
||||
.bus_config.spi_cfg = \
|
||||
SPI_CONFIG_DT_INST(inst, \
|
||||
BME280_SPI_OPERATION, \
|
||||
0), \
|
||||
#define BME280_CONFIG_SPI(inst) \
|
||||
{ \
|
||||
.bus.spi = SPI_DT_SPEC_INST_GET( \
|
||||
inst, BME280_SPI_OPERATION, 0), \
|
||||
.bus_io = &bme280_bus_io_spi, \
|
||||
}
|
||||
|
||||
/* Initializes a struct bme280_config for an instance on an I2C bus. */
|
||||
#define BME280_CONFIG_I2C(inst) \
|
||||
{ \
|
||||
.bus = DEVICE_DT_GET(DT_INST_BUS(inst)), \
|
||||
.bus_io = &bme280_bus_io_i2c, \
|
||||
.bus_config.i2c_addr = DT_INST_REG_ADDR(inst), \
|
||||
#define BME280_CONFIG_I2C(inst) \
|
||||
{ \
|
||||
.bus.i2c = I2C_DT_SPEC_INST_GET(inst), \
|
||||
.bus_io = &bme280_bus_io_i2c, \
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -20,22 +20,19 @@
|
|||
#define BME280_BUS_SPI DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
|
||||
#define BME280_BUS_I2C DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
|
||||
|
||||
union bme280_bus_config {
|
||||
union bme280_bus {
|
||||
#if BME280_BUS_SPI
|
||||
struct spi_config spi_cfg;
|
||||
struct spi_dt_spec spi;
|
||||
#endif
|
||||
#if BME280_BUS_I2C
|
||||
uint16_t i2c_addr;
|
||||
struct i2c_dt_spec i2c;
|
||||
#endif
|
||||
};
|
||||
|
||||
typedef int (*bme280_bus_check_fn)(const struct device *bus,
|
||||
const union bme280_bus_config *bus_config);
|
||||
typedef int (*bme280_reg_read_fn)(const struct device *bus,
|
||||
const union bme280_bus_config *bus_config,
|
||||
typedef int (*bme280_bus_check_fn)(const union bme280_bus *bus);
|
||||
typedef int (*bme280_reg_read_fn)(const union bme280_bus *bus,
|
||||
uint8_t start, uint8_t *buf, int size);
|
||||
typedef int (*bme280_reg_write_fn)(const struct device *bus,
|
||||
const union bme280_bus_config *bus_config,
|
||||
typedef int (*bme280_reg_write_fn)(const union bme280_bus *bus,
|
||||
uint8_t reg, uint8_t val);
|
||||
|
||||
struct bme280_bus_io {
|
||||
|
|
|
@ -13,26 +13,21 @@
|
|||
#include "bme280.h"
|
||||
|
||||
#if BME280_BUS_I2C
|
||||
static int bme280_bus_check_i2c(const struct device *bus,
|
||||
const union bme280_bus_config *bus_config)
|
||||
static int bme280_bus_check_i2c(const union bme280_bus *bus)
|
||||
{
|
||||
return device_is_ready(bus) ? 0 : -ENODEV;
|
||||
return device_is_ready(bus->i2c.bus) ? 0 : -ENODEV;
|
||||
}
|
||||
|
||||
static int bme280_reg_read_i2c(const struct device *bus,
|
||||
const union bme280_bus_config *bus_config,
|
||||
static int bme280_reg_read_i2c(const union bme280_bus *bus,
|
||||
uint8_t start, uint8_t *buf, int size)
|
||||
{
|
||||
return i2c_burst_read(bus, bus_config->i2c_addr,
|
||||
start, buf, size);
|
||||
return i2c_burst_read_dt(&bus->i2c, start, buf, size);
|
||||
}
|
||||
|
||||
static int bme280_reg_write_i2c(const struct device *bus,
|
||||
const union bme280_bus_config *bus_config,
|
||||
static int bme280_reg_write_i2c(const union bme280_bus *bus,
|
||||
uint8_t reg, uint8_t val)
|
||||
{
|
||||
return i2c_reg_write_byte(bus, bus_config->i2c_addr,
|
||||
reg, val);
|
||||
return i2c_reg_write_byte_dt(&bus->i2c, reg, val);
|
||||
}
|
||||
|
||||
const struct bme280_bus_io bme280_bus_io_i2c = {
|
||||
|
@ -40,4 +35,4 @@ const struct bme280_bus_io bme280_bus_io_i2c = {
|
|||
.read = bme280_reg_read_i2c,
|
||||
.write = bme280_reg_write_i2c,
|
||||
};
|
||||
#endif /* BME280_BUS_I2C */
|
||||
#endif /* BME280_BUS_I2C */
|
||||
|
|
|
@ -17,28 +17,12 @@
|
|||
|
||||
LOG_MODULE_DECLARE(BME280, CONFIG_SENSOR_LOG_LEVEL);
|
||||
|
||||
static int bme280_bus_check_spi(const struct device *bus,
|
||||
const union bme280_bus_config *bus_config)
|
||||
static int bme280_bus_check_spi(const union bme280_bus *bus)
|
||||
{
|
||||
const struct spi_cs_control *cs;
|
||||
|
||||
if (!device_is_ready(bus)) {
|
||||
LOG_DBG("SPI bus %s not ready", bus->name);
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
cs = bus_config->spi_cfg.cs;
|
||||
if (cs && !device_is_ready(cs->gpio_dev)) {
|
||||
LOG_DBG("SPI CS GPIO controller %s not ready",
|
||||
cs->gpio_dev->name);
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return spi_is_ready(&bus->spi) ? 0 : -ENODEV;
|
||||
}
|
||||
|
||||
static int bme280_reg_read_spi(const struct device *bus,
|
||||
const union bme280_bus_config *bus_config,
|
||||
static int bme280_reg_read_spi(const union bme280_bus *bus,
|
||||
uint8_t start, uint8_t *buf, int size)
|
||||
{
|
||||
uint8_t addr;
|
||||
|
@ -68,7 +52,7 @@ static int bme280_reg_read_spi(const struct device *bus,
|
|||
addr = (start + i) | 0x80;
|
||||
rx_buf[1].buf = &buf[i];
|
||||
|
||||
ret = spi_transceive(bus, &bus_config->spi_cfg, &tx, &rx);
|
||||
ret = spi_transceive_dt(&bus->spi, &tx, &rx);
|
||||
if (ret) {
|
||||
LOG_DBG("spi_transceive FAIL %d\n", ret);
|
||||
return ret;
|
||||
|
@ -78,8 +62,7 @@ static int bme280_reg_read_spi(const struct device *bus,
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int bme280_reg_write_spi(const struct device *bus,
|
||||
const union bme280_bus_config *bus_config,
|
||||
static int bme280_reg_write_spi(const union bme280_bus *bus,
|
||||
uint8_t reg, uint8_t val)
|
||||
{
|
||||
uint8_t cmd[] = { reg & 0x7F, val };
|
||||
|
@ -93,7 +76,7 @@ static int bme280_reg_write_spi(const struct device *bus,
|
|||
};
|
||||
int ret;
|
||||
|
||||
ret = spi_write(bus, &bus_config->spi_cfg, &tx);
|
||||
ret = spi_write_dt(&bus->spi, &tx);
|
||||
if (ret) {
|
||||
LOG_DBG("spi_write FAIL %d\n", ret);
|
||||
return ret;
|
||||
|
@ -106,4 +89,4 @@ const struct bme280_bus_io bme280_bus_io_spi = {
|
|||
.read = bme280_reg_read_spi,
|
||||
.write = bme280_reg_write_spi,
|
||||
};
|
||||
#endif /* BME280_BUS_SPI */
|
||||
#endif /* BME280_BUS_SPI */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue