Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i2c: introduce struct i2c_dt_spec #37359

Merged
merged 4 commits into from
Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 33 additions & 33 deletions drivers/sensor/bme680/bme680.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@
#include <logging/log.h>
LOG_MODULE_REGISTER(bme680, CONFIG_SENSOR_LOG_LEVEL);

static int bme680_reg_read(struct bme680_data *data, uint8_t start, uint8_t *buf,
int size)
static int bme680_reg_read(const struct device *dev, uint8_t start,
uint8_t *buf, int size)
{
return i2c_burst_read(data->i2c_master, data->i2c_slave_addr, start,
buf, size);
return 0;
const struct bme680_config *config = dev->config;

return i2c_burst_read_dt(&config->bus, start, buf, size);
}

static int bme680_reg_write(struct bme680_data *data, uint8_t reg, uint8_t val)
static int bme680_reg_write(const struct device *dev, uint8_t reg, uint8_t val)
{
return i2c_reg_write_byte(data->i2c_master, data->i2c_slave_addr,
reg, val);
return 0;
const struct bme680_config *config = dev->config;

return i2c_reg_write_byte_dt(&config->bus, reg, val);
}

static void bme680_calc_temp(struct bme680_data *data, uint32_t adc_temp)
Expand Down Expand Up @@ -201,7 +201,7 @@ static int bme680_sample_fetch(const struct device *dev,

__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);

ret = bme680_reg_read(data, BME680_REG_FIELD0, buff, size);
ret = bme680_reg_read(dev, BME680_REG_FIELD0, buff, size);
if (ret < 0) {
return ret;
}
Expand All @@ -225,7 +225,7 @@ static int bme680_sample_fetch(const struct device *dev,
}

/* Trigger the next measurement */
ret = bme680_reg_write(data, BME680_REG_CTRL_MEAS,
ret = bme680_reg_write(dev, BME680_REG_CTRL_MEAS,
BME680_CTRL_MEAS_VAL);
if (ret < 0) {
return ret;
Expand Down Expand Up @@ -280,23 +280,24 @@ static int bme680_channel_get(const struct device *dev,
return 0;
}

static int bme680_read_compensation(struct bme680_data *data)
static int bme680_read_compensation(const struct device *dev)
{
struct bme680_data *data = dev->data;
uint8_t buff[BME680_LEN_COEFF_ALL];
int err = 0;

err = bme680_reg_read(data, BME680_REG_COEFF1, buff, BME680_LEN_COEFF1);
err = bme680_reg_read(dev, BME680_REG_COEFF1, buff, BME680_LEN_COEFF1);
if (err < 0) {
return err;
}

err = bme680_reg_read(data, BME680_REG_COEFF2, &buff[BME680_LEN_COEFF1],
err = bme680_reg_read(dev, BME680_REG_COEFF2, &buff[BME680_LEN_COEFF1],
16);
if (err < 0) {
return err;
}

err = bme680_reg_read(data, BME680_REG_COEFF3,
err = bme680_reg_read(dev, BME680_REG_COEFF3,
&buff[BME680_LEN_COEFF1 + BME680_LEN_COEFF2],
BME680_LEN_COEFF3);
if (err < 0) {
Expand Down Expand Up @@ -346,7 +347,7 @@ static int bme680_chip_init(const struct device *dev)
struct bme680_data *data = (struct bme680_data *)dev->data;
int err;

err = bme680_reg_read(data, BME680_REG_CHIP_ID, &data->chip_id, 1);
err = bme680_reg_read(dev, BME680_REG_CHIP_ID, &data->chip_id, 1);
if (err < 0) {
return err;
}
Expand All @@ -358,40 +359,40 @@ static int bme680_chip_init(const struct device *dev)
return -ENOTSUP;
}

err = bme680_read_compensation(data);
err = bme680_read_compensation(dev);
if (err < 0) {
return err;
}

err = bme680_reg_write(data, BME680_REG_CTRL_HUM, BME680_HUMIDITY_OVER);
err = bme680_reg_write(dev, BME680_REG_CTRL_HUM, BME680_HUMIDITY_OVER);
if (err < 0) {
return err;
}

err = bme680_reg_write(data, BME680_REG_CONFIG, BME680_CONFIG_VAL);
err = bme680_reg_write(dev, BME680_REG_CONFIG, BME680_CONFIG_VAL);
if (err < 0) {
return err;
}

err = bme680_reg_write(data, BME680_REG_CTRL_GAS_1,
err = bme680_reg_write(dev, BME680_REG_CTRL_GAS_1,
BME680_CTRL_GAS_1_VAL);
if (err < 0) {
return err;
}

err = bme680_reg_write(data, BME680_REG_RES_HEAT0,
err = bme680_reg_write(dev, BME680_REG_RES_HEAT0,
bme680_calc_res_heat(data, BME680_HEATR_TEMP));
if (err < 0) {
return err;
}

err = bme680_reg_write(data, BME680_REG_GAS_WAIT0,
err = bme680_reg_write(dev, BME680_REG_GAS_WAIT0,
bme680_calc_gas_wait(BME680_HEATR_DUR_MS));
if (err < 0) {
return err;
}

err = bme680_reg_write(data, BME680_REG_CTRL_MEAS,
err = bme680_reg_write(dev, BME680_REG_CTRL_MEAS,
BME680_CTRL_MEAS_VAL);
if (err < 0) {
return err;
Expand All @@ -402,18 +403,13 @@ static int bme680_chip_init(const struct device *dev)

static int bme680_init(const struct device *dev)
{
struct bme680_data *data = dev->data;
const struct bme680_config *config = dev->config;

data->i2c_master = device_get_binding(
DT_INST_BUS_LABEL(0));
if (!data->i2c_master) {
LOG_ERR("I2C master not found: %s",
DT_INST_BUS_LABEL(0));
if (!device_is_ready(config->bus.bus)) {
LOG_ERR("I2C master %s not ready", config->bus.bus->name);
return -EINVAL;
}

data->i2c_slave_addr = DT_INST_REG_ADDR(0);

if (bme680_chip_init(dev) < 0) {
return -EINVAL;
}
Expand All @@ -428,6 +424,10 @@ static const struct sensor_driver_api bme680_api_funcs = {

static struct bme680_data bme680_data;

static const struct bme680_config bme680_config = {
.bus = I2C_DT_SPEC_INST_GET(0)
};

DEVICE_DT_INST_DEFINE(0, bme680_init, NULL, &bme680_data,
NULL, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,
&bme680_api_funcs);
&bme680_config, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,
&bme680_api_funcs);
10 changes: 5 additions & 5 deletions drivers/sensor/bme680/bme680.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
#define __SENSOR_BME680_H__

#include <device.h>
#include <zephyr/types.h>

#include <drivers/i2c.h>

#define BME680_CHIP_ID 0x61

Expand Down Expand Up @@ -120,9 +119,6 @@
#define BME680_CONCAT_BYTES(msb, lsb) (((uint16_t)msb << 8) | (uint16_t)lsb)

struct bme680_data {
const struct device *i2c_master;
uint16_t i2c_slave_addr;

/* Compensation parameters. */
uint16_t par_h1;
uint16_t par_h2;
Expand Down Expand Up @@ -167,4 +163,8 @@ struct bme680_data {
uint8_t chip_id;
};

struct bme680_config {
struct i2c_dt_spec bus;
};

#endif /* __SENSOR_BME680_H__ */
43 changes: 17 additions & 26 deletions drivers/sensor/sht3xd/sht3xd.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,31 +52,30 @@ static uint8_t sht3xd_compute_crc(uint16_t value)

int sht3xd_write_command(const struct device *dev, uint16_t cmd)
{
const struct sht3xd_config *config = dev->config;
uint8_t tx_buf[2];

sys_put_be16(cmd, tx_buf);
return i2c_write(sht3xd_i2c_device(dev), tx_buf, sizeof(tx_buf),
sht3xd_i2c_address(dev));
return i2c_write_dt(&config->bus, tx_buf, sizeof(tx_buf));
}

int sht3xd_write_reg(const struct device *dev, uint16_t cmd, uint16_t val)
{
const struct sht3xd_config *config = dev->config;
uint8_t tx_buf[5];

sys_put_be16(cmd, &tx_buf[0]);
sys_put_be16(val, &tx_buf[2]);
tx_buf[4] = sht3xd_compute_crc(val);

return i2c_write(sht3xd_i2c_device(dev), tx_buf, sizeof(tx_buf),
sht3xd_i2c_address(dev));
return i2c_write_dt(&config->bus, tx_buf, sizeof(tx_buf));
}

static int sht3xd_sample_fetch(const struct device *dev,
enum sensor_channel chan)
{
const struct sht3xd_config *config = dev->config;
struct sht3xd_data *data = dev->data;
const struct device *i2c = sht3xd_i2c_device(dev);
uint8_t address = sht3xd_i2c_address(dev);
uint8_t rx_buf[6];
uint16_t t_sample, rh_sample;

Expand All @@ -92,7 +91,7 @@ static int sht3xd_sample_fetch(const struct device *dev,
}
k_sleep(K_MSEC(measure_wait[SHT3XD_REPEATABILITY_IDX] / USEC_PER_MSEC));

if (i2c_read(i2c, rx_buf, sizeof(rx_buf), address) < 0) {
if (i2c_read_dt(&config->bus, rx_buf, sizeof(rx_buf)) < 0) {
LOG_DBG("Failed to read data sample!");
return -EIO;
}
Expand All @@ -102,8 +101,8 @@ static int sht3xd_sample_fetch(const struct device *dev,

sys_put_be16(SHT3XD_CMD_FETCH, tx_buf);

if (i2c_write_read(i2c, address, tx_buf, sizeof(tx_buf),
rx_buf, sizeof(rx_buf)) < 0) {
if (i2c_write_read_dt(&config->bus, tx_buf, sizeof(tx_buf),
rx_buf, sizeof(rx_buf)) < 0) {
LOG_DBG("Failed to read data sample!");
return -EIO;
}
Expand Down Expand Up @@ -167,22 +166,12 @@ static const struct sensor_driver_api sht3xd_driver_api = {

static int sht3xd_init(const struct device *dev)
{
struct sht3xd_data *data = dev->data;
const struct sht3xd_config *cfg = dev->config;
const struct device *i2c = device_get_binding(cfg->bus_name);

if (i2c == NULL) {
LOG_DBG("Failed to get pointer to %s device!",
cfg->bus_name);
return -EINVAL;
}
data->bus = i2c;

if (!cfg->base_address) {
LOG_DBG("No I2C address");
if (!device_is_ready(cfg->bus.bus)) {
LOG_ERR("I2C bus %s is not ready!", cfg->bus.bus->name);
return -EINVAL;
}
data->dev = dev;

/* clear status register */
if (sht3xd_write_command(dev, SHT3XD_CMD_CLEAR_STATUS) < 0) {
Expand All @@ -204,6 +193,9 @@ static int sht3xd_init(const struct device *dev)
k_busy_wait(measure_wait[SHT3XD_REPEATABILITY_IDX]);
#endif
#ifdef CONFIG_SHT3XD_TRIGGER
struct sht3xd_data *data = dev->data;

data->dev = dev;
if (sht3xd_init_interrupt(dev) < 0) {
LOG_DBG("Failed to initialize interrupt");
return -EIO;
Expand All @@ -215,18 +207,17 @@ static int sht3xd_init(const struct device *dev)

struct sht3xd_data sht3xd0_driver;
static const struct sht3xd_config sht3xd0_cfg = {
.bus_name = DT_INST_BUS_LABEL(0),
.bus = I2C_DT_SPEC_INST_GET(0),
#ifdef CONFIG_SHT3XD_TRIGGER
.alert_gpio_name = DT_INST_GPIO_LABEL(0, alert_gpios),
#endif
.base_address = DT_INST_REG_ADDR(0),
#ifdef CONFIG_SHT3XD_TRIGGER
.alert_pin = DT_INST_GPIO_PIN(0, alert_gpios),
.alert_flags = DT_INST_GPIO_FLAGS(0, alert_gpios),
#endif
};

DEVICE_DT_INST_DEFINE(0, sht3xd_init, NULL,
&sht3xd0_driver, &sht3xd0_cfg,
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,
&sht3xd_driver_api);
&sht3xd0_driver, &sht3xd0_cfg,
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,
&sht3xd_driver_api);
23 changes: 4 additions & 19 deletions drivers/sensor/sht3xd/sht3xd.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <device.h>
#include <kernel.h>
#include <drivers/gpio.h>
#include <drivers/i2c.h>

#define SHT3XD_CMD_FETCH 0xE000
#define SHT3XD_CMD_ART 0x2B32
Expand Down Expand Up @@ -44,26 +45,24 @@
#define SHT3XD_CLEAR_STATUS_WAIT_USEC 1000

struct sht3xd_config {
char *bus_name;
struct i2c_dt_spec bus;

#ifdef CONFIG_SHT3XD_TRIGGER
char *alert_gpio_name;
#endif /* CONFIG_SHT3XD_TRIGGER */

uint8_t base_address;
#ifdef CONFIG_SHT3XD_TRIGGER
uint8_t alert_pin;
uint8_t alert_flags;
#endif /* CONFIG_SHT3XD_TRIGGER */
};

struct sht3xd_data {
const struct device *dev;
const struct device *bus;

uint16_t t_sample;
uint16_t rh_sample;

#ifdef CONFIG_SHT3XD_TRIGGER
const struct device *dev;
const struct device *alert_gpio;
struct gpio_callback alert_cb;

Expand All @@ -86,20 +85,6 @@ struct sht3xd_data {
#endif /* CONFIG_SHT3XD_TRIGGER */
};

static inline uint8_t sht3xd_i2c_address(const struct device *dev)
{
const struct sht3xd_config *dcp = dev->config;

return dcp->base_address;
}

static inline const struct device *sht3xd_i2c_device(const struct device *dev)
{
const struct sht3xd_data *ddp = dev->data;

return ddp->bus;
}

#ifdef CONFIG_SHT3XD_TRIGGER
int sht3xd_write_command(const struct device *dev, uint16_t cmd);

Expand Down
Loading