drivers: sensor: bma280: Update driver to use i2c_dt_spec
Simplify driver by using i2c_dt_spec for bus access. Signed-off-by: Benjamin Björnsson <benjamin.bjornsson@gmail.com>
This commit is contained in:
parent
f232db4630
commit
3be45afe79
3 changed files with 81 additions and 73 deletions
|
@ -20,6 +20,7 @@ static int bma280_sample_fetch(const struct device *dev,
|
||||||
enum sensor_channel chan)
|
enum sensor_channel chan)
|
||||||
{
|
{
|
||||||
struct bma280_data *drv_data = dev->data;
|
struct bma280_data *drv_data = dev->data;
|
||||||
|
const struct bma280_config *config = dev->config;
|
||||||
uint8_t buf[6];
|
uint8_t buf[6];
|
||||||
uint8_t lsb;
|
uint8_t lsb;
|
||||||
|
|
||||||
|
@ -29,8 +30,8 @@ static int bma280_sample_fetch(const struct device *dev,
|
||||||
* since all accel data register addresses are consecutive,
|
* since all accel data register addresses are consecutive,
|
||||||
* a burst read can be used to read all the samples
|
* a burst read can be used to read all the samples
|
||||||
*/
|
*/
|
||||||
if (i2c_burst_read(drv_data->i2c, BMA280_I2C_ADDRESS,
|
if (i2c_burst_read_dt(&config->i2c,
|
||||||
BMA280_REG_ACCEL_X_LSB, buf, 6) < 0) {
|
BMA280_REG_ACCEL_X_LSB, buf, 6) < 0) {
|
||||||
LOG_DBG("Could not read accel axis data");
|
LOG_DBG("Could not read accel axis data");
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
@ -44,9 +45,9 @@ static int bma280_sample_fetch(const struct device *dev,
|
||||||
lsb = (buf[4] & BMA280_ACCEL_LSB_MASK) >> BMA280_ACCEL_LSB_SHIFT;
|
lsb = (buf[4] & BMA280_ACCEL_LSB_MASK) >> BMA280_ACCEL_LSB_SHIFT;
|
||||||
drv_data->z_sample = (((int8_t)buf[5]) << BMA280_ACCEL_LSB_BITS) | lsb;
|
drv_data->z_sample = (((int8_t)buf[5]) << BMA280_ACCEL_LSB_BITS) | lsb;
|
||||||
|
|
||||||
if (i2c_reg_read_byte(drv_data->i2c, BMA280_I2C_ADDRESS,
|
if (i2c_reg_read_byte_dt(&config->i2c,
|
||||||
BMA280_REG_TEMP,
|
BMA280_REG_TEMP,
|
||||||
(uint8_t *)&drv_data->temp_sample) < 0) {
|
(uint8_t *)&drv_data->temp_sample) < 0) {
|
||||||
LOG_DBG("Could not read temperature data");
|
LOG_DBG("Could not read temperature data");
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
@ -116,19 +117,17 @@ static const struct sensor_driver_api bma280_driver_api = {
|
||||||
|
|
||||||
int bma280_init(const struct device *dev)
|
int bma280_init(const struct device *dev)
|
||||||
{
|
{
|
||||||
struct bma280_data *drv_data = dev->data;
|
const struct bma280_config *config = dev->config;
|
||||||
uint8_t id = 0U;
|
uint8_t id = 0U;
|
||||||
|
|
||||||
drv_data->i2c = device_get_binding(DT_INST_BUS_LABEL(0));
|
if (!device_is_ready(config->i2c.bus)) {
|
||||||
if (drv_data->i2c == NULL) {
|
LOG_ERR("I2C bus device not ready");
|
||||||
LOG_DBG("Could not get pointer to %s device",
|
return -ENODEV;
|
||||||
DT_INST_BUS_LABEL(0));
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* read device ID */
|
/* read device ID */
|
||||||
if (i2c_reg_read_byte(drv_data->i2c, BMA280_I2C_ADDRESS,
|
if (i2c_reg_read_byte_dt(&config->i2c,
|
||||||
BMA280_REG_CHIP_ID, &id) < 0) {
|
BMA280_REG_CHIP_ID, &id) < 0) {
|
||||||
LOG_DBG("Could not read chip id");
|
LOG_DBG("Could not read chip id");
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
@ -138,15 +137,15 @@ int bma280_init(const struct device *dev)
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i2c_reg_write_byte(drv_data->i2c, BMA280_I2C_ADDRESS,
|
if (i2c_reg_write_byte_dt(&config->i2c,
|
||||||
BMA280_REG_PMU_BW, BMA280_PMU_BW) < 0) {
|
BMA280_REG_PMU_BW, BMA280_PMU_BW) < 0) {
|
||||||
LOG_DBG("Could not set data filter bandwidth");
|
LOG_DBG("Could not set data filter bandwidth");
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* set g-range */
|
/* set g-range */
|
||||||
if (i2c_reg_write_byte(drv_data->i2c, BMA280_I2C_ADDRESS,
|
if (i2c_reg_write_byte_dt(&config->i2c,
|
||||||
BMA280_REG_PMU_RANGE, BMA280_PMU_RANGE) < 0) {
|
BMA280_REG_PMU_RANGE, BMA280_PMU_RANGE) < 0) {
|
||||||
LOG_DBG("Could not set data g-range");
|
LOG_DBG("Could not set data g-range");
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
@ -161,8 +160,12 @@ int bma280_init(const struct device *dev)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct bma280_data bma280_driver;
|
static struct bma280_data bma280_inst_data;
|
||||||
|
|
||||||
DEVICE_DT_INST_DEFINE(0, bma280_init, NULL, &bma280_driver,
|
static const struct bma280_config bma280_inst_config = {
|
||||||
NULL, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,
|
.i2c = I2C_DT_SPEC_INST_GET(0),
|
||||||
&bma280_driver_api);
|
};
|
||||||
|
|
||||||
|
DEVICE_DT_INST_DEFINE(0, bma280_init, NULL, &bma280_inst_data,
|
||||||
|
&bma280_inst_config, POST_KERNEL,
|
||||||
|
CONFIG_SENSOR_INIT_PRIORITY, &bma280_driver_api);
|
||||||
|
|
|
@ -10,10 +10,9 @@
|
||||||
#include <zephyr/device.h>
|
#include <zephyr/device.h>
|
||||||
#include <zephyr/sys/util.h>
|
#include <zephyr/sys/util.h>
|
||||||
#include <zephyr/types.h>
|
#include <zephyr/types.h>
|
||||||
|
#include <zephyr/drivers/i2c.h>
|
||||||
#include <zephyr/drivers/gpio.h>
|
#include <zephyr/drivers/gpio.h>
|
||||||
|
|
||||||
#define BMA280_I2C_ADDRESS DT_INST_REG_ADDR(0)
|
|
||||||
|
|
||||||
#define BMA280_REG_CHIP_ID 0x00
|
#define BMA280_REG_CHIP_ID 0x00
|
||||||
#if DT_INST_PROP(0, is_bmc150)
|
#if DT_INST_PROP(0, is_bmc150)
|
||||||
#define BMA280_CHIP_ID 0xFA
|
#define BMA280_CHIP_ID 0xFA
|
||||||
|
@ -114,7 +113,6 @@
|
||||||
#define BMA280_THREAD_STACKSIZE_UNIT 1024
|
#define BMA280_THREAD_STACKSIZE_UNIT 1024
|
||||||
|
|
||||||
struct bma280_data {
|
struct bma280_data {
|
||||||
const struct device *i2c;
|
|
||||||
int16_t x_sample;
|
int16_t x_sample;
|
||||||
int16_t y_sample;
|
int16_t y_sample;
|
||||||
int16_t z_sample;
|
int16_t z_sample;
|
||||||
|
@ -142,6 +140,10 @@ struct bma280_data {
|
||||||
#endif /* CONFIG_BMA280_TRIGGER */
|
#endif /* CONFIG_BMA280_TRIGGER */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct bma280_config {
|
||||||
|
struct i2c_dt_spec i2c;
|
||||||
|
};
|
||||||
|
|
||||||
#ifdef CONFIG_BMA280_TRIGGER
|
#ifdef CONFIG_BMA280_TRIGGER
|
||||||
int bma280_trigger_set(const struct device *dev,
|
int bma280_trigger_set(const struct device *dev,
|
||||||
const struct sensor_trigger *trig,
|
const struct sensor_trigger *trig,
|
||||||
|
|
|
@ -34,7 +34,7 @@ int bma280_attr_set(const struct device *dev,
|
||||||
enum sensor_attribute attr,
|
enum sensor_attribute attr,
|
||||||
const struct sensor_value *val)
|
const struct sensor_value *val)
|
||||||
{
|
{
|
||||||
struct bma280_data *drv_data = dev->data;
|
const struct bma280_config *config = dev->config;
|
||||||
uint64_t slope_th;
|
uint64_t slope_th;
|
||||||
|
|
||||||
if (chan != SENSOR_CHAN_ACCEL_XYZ) {
|
if (chan != SENSOR_CHAN_ACCEL_XYZ) {
|
||||||
|
@ -45,18 +45,18 @@ int bma280_attr_set(const struct device *dev,
|
||||||
/* slope_th = (val * 10^6 * 2^10) / BMA280_PMU_FULL_RAGE */
|
/* slope_th = (val * 10^6 * 2^10) / BMA280_PMU_FULL_RAGE */
|
||||||
slope_th = (uint64_t)val->val1 * 1000000U + (uint64_t)val->val2;
|
slope_th = (uint64_t)val->val1 * 1000000U + (uint64_t)val->val2;
|
||||||
slope_th = (slope_th * (1 << 10)) / BMA280_PMU_FULL_RANGE;
|
slope_th = (slope_th * (1 << 10)) / BMA280_PMU_FULL_RANGE;
|
||||||
if (i2c_reg_write_byte(drv_data->i2c, BMA280_I2C_ADDRESS,
|
if (i2c_reg_write_byte_dt(&config->i2c,
|
||||||
BMA280_REG_SLOPE_TH, (uint8_t)slope_th)
|
BMA280_REG_SLOPE_TH, (uint8_t)slope_th)
|
||||||
< 0) {
|
< 0) {
|
||||||
LOG_DBG("Could not set slope threshold");
|
LOG_DBG("Could not set slope threshold");
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
} else if (attr == SENSOR_ATTR_SLOPE_DUR) {
|
} else if (attr == SENSOR_ATTR_SLOPE_DUR) {
|
||||||
if (i2c_reg_update_byte(drv_data->i2c, BMA280_I2C_ADDRESS,
|
if (i2c_reg_update_byte_dt(&config->i2c,
|
||||||
BMA280_REG_INT_5,
|
BMA280_REG_INT_5,
|
||||||
BMA280_SLOPE_DUR_MASK,
|
BMA280_SLOPE_DUR_MASK,
|
||||||
val->val1 << BMA280_SLOPE_DUR_SHIFT)
|
val->val1 << BMA280_SLOPE_DUR_SHIFT)
|
||||||
< 0) {
|
< 0) {
|
||||||
LOG_DBG("Could not set slope duration");
|
LOG_DBG("Could not set slope duration");
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
@ -87,12 +87,13 @@ static void bma280_gpio_callback(const struct device *dev,
|
||||||
static void bma280_thread_cb(const struct device *dev)
|
static void bma280_thread_cb(const struct device *dev)
|
||||||
{
|
{
|
||||||
struct bma280_data *drv_data = dev->data;
|
struct bma280_data *drv_data = dev->data;
|
||||||
|
const struct bma280_config *config = dev->config;
|
||||||
uint8_t status = 0U;
|
uint8_t status = 0U;
|
||||||
int err = 0;
|
int err = 0;
|
||||||
|
|
||||||
/* check for data ready */
|
/* check for data ready */
|
||||||
err = i2c_reg_read_byte(drv_data->i2c, BMA280_I2C_ADDRESS,
|
err = i2c_reg_read_byte_dt(&config->i2c,
|
||||||
BMA280_REG_INT_STATUS_1, &status);
|
BMA280_REG_INT_STATUS_1, &status);
|
||||||
if (status & BMA280_BIT_DATA_INT_STATUS &&
|
if (status & BMA280_BIT_DATA_INT_STATUS &&
|
||||||
drv_data->data_ready_handler != NULL &&
|
drv_data->data_ready_handler != NULL &&
|
||||||
err == 0) {
|
err == 0) {
|
||||||
|
@ -101,8 +102,8 @@ static void bma280_thread_cb(const struct device *dev)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* check for any motion */
|
/* check for any motion */
|
||||||
err = i2c_reg_read_byte(drv_data->i2c, BMA280_I2C_ADDRESS,
|
err = i2c_reg_read_byte_dt(&config->i2c,
|
||||||
BMA280_REG_INT_STATUS_0, &status);
|
BMA280_REG_INT_STATUS_0, &status);
|
||||||
if (status & BMA280_BIT_SLOPE_INT_STATUS &&
|
if (status & BMA280_BIT_SLOPE_INT_STATUS &&
|
||||||
drv_data->any_motion_handler != NULL &&
|
drv_data->any_motion_handler != NULL &&
|
||||||
err == 0) {
|
err == 0) {
|
||||||
|
@ -110,10 +111,10 @@ static void bma280_thread_cb(const struct device *dev)
|
||||||
&drv_data->data_ready_trigger);
|
&drv_data->data_ready_trigger);
|
||||||
|
|
||||||
/* clear latched interrupt */
|
/* clear latched interrupt */
|
||||||
err = i2c_reg_update_byte(drv_data->i2c, BMA280_I2C_ADDRESS,
|
err = i2c_reg_update_byte_dt(&config->i2c,
|
||||||
BMA280_REG_INT_RST_LATCH,
|
BMA280_REG_INT_RST_LATCH,
|
||||||
BMA280_BIT_INT_LATCH_RESET,
|
BMA280_BIT_INT_LATCH_RESET,
|
||||||
BMA280_BIT_INT_LATCH_RESET);
|
BMA280_BIT_INT_LATCH_RESET);
|
||||||
|
|
||||||
if (err < 0) {
|
if (err < 0) {
|
||||||
LOG_DBG("Could not update clear the interrupt");
|
LOG_DBG("Could not update clear the interrupt");
|
||||||
|
@ -149,12 +150,13 @@ int bma280_trigger_set(const struct device *dev,
|
||||||
sensor_trigger_handler_t handler)
|
sensor_trigger_handler_t handler)
|
||||||
{
|
{
|
||||||
struct bma280_data *drv_data = dev->data;
|
struct bma280_data *drv_data = dev->data;
|
||||||
|
const struct bma280_config *config = dev->config;
|
||||||
|
|
||||||
if (trig->type == SENSOR_TRIG_DATA_READY) {
|
if (trig->type == SENSOR_TRIG_DATA_READY) {
|
||||||
/* disable data ready interrupt while changing trigger params */
|
/* disable data ready interrupt while changing trigger params */
|
||||||
if (i2c_reg_update_byte(drv_data->i2c, BMA280_I2C_ADDRESS,
|
if (i2c_reg_update_byte_dt(&config->i2c,
|
||||||
BMA280_REG_INT_EN_1,
|
BMA280_REG_INT_EN_1,
|
||||||
BMA280_BIT_DATA_EN, 0) < 0) {
|
BMA280_BIT_DATA_EN, 0) < 0) {
|
||||||
LOG_DBG("Could not disable data ready interrupt");
|
LOG_DBG("Could not disable data ready interrupt");
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
@ -166,18 +168,18 @@ int bma280_trigger_set(const struct device *dev,
|
||||||
drv_data->data_ready_trigger = *trig;
|
drv_data->data_ready_trigger = *trig;
|
||||||
|
|
||||||
/* enable data ready interrupt */
|
/* enable data ready interrupt */
|
||||||
if (i2c_reg_update_byte(drv_data->i2c, BMA280_I2C_ADDRESS,
|
if (i2c_reg_update_byte_dt(&config->i2c,
|
||||||
BMA280_REG_INT_EN_1,
|
BMA280_REG_INT_EN_1,
|
||||||
BMA280_BIT_DATA_EN,
|
BMA280_BIT_DATA_EN,
|
||||||
BMA280_BIT_DATA_EN) < 0) {
|
BMA280_BIT_DATA_EN) < 0) {
|
||||||
LOG_DBG("Could not enable data ready interrupt");
|
LOG_DBG("Could not enable data ready interrupt");
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
} else if (trig->type == SENSOR_TRIG_DELTA) {
|
} else if (trig->type == SENSOR_TRIG_DELTA) {
|
||||||
/* disable any-motion interrupt while changing trigger params */
|
/* disable any-motion interrupt while changing trigger params */
|
||||||
if (i2c_reg_update_byte(drv_data->i2c, BMA280_I2C_ADDRESS,
|
if (i2c_reg_update_byte_dt(&config->i2c,
|
||||||
BMA280_REG_INT_EN_0,
|
BMA280_REG_INT_EN_0,
|
||||||
BMA280_SLOPE_EN_XYZ, 0) < 0) {
|
BMA280_SLOPE_EN_XYZ, 0) < 0) {
|
||||||
LOG_DBG("Could not disable data ready interrupt");
|
LOG_DBG("Could not disable data ready interrupt");
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
@ -189,10 +191,10 @@ int bma280_trigger_set(const struct device *dev,
|
||||||
drv_data->any_motion_trigger = *trig;
|
drv_data->any_motion_trigger = *trig;
|
||||||
|
|
||||||
/* enable any-motion interrupt */
|
/* enable any-motion interrupt */
|
||||||
if (i2c_reg_update_byte(drv_data->i2c, BMA280_I2C_ADDRESS,
|
if (i2c_reg_update_byte_dt(&config->i2c,
|
||||||
BMA280_REG_INT_EN_0,
|
BMA280_REG_INT_EN_0,
|
||||||
BMA280_SLOPE_EN_XYZ,
|
BMA280_SLOPE_EN_XYZ,
|
||||||
BMA280_SLOPE_EN_XYZ) < 0) {
|
BMA280_SLOPE_EN_XYZ) < 0) {
|
||||||
LOG_DBG("Could not enable data ready interrupt");
|
LOG_DBG("Could not enable data ready interrupt");
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
@ -206,12 +208,13 @@ int bma280_trigger_set(const struct device *dev,
|
||||||
int bma280_init_interrupt(const struct device *dev)
|
int bma280_init_interrupt(const struct device *dev)
|
||||||
{
|
{
|
||||||
struct bma280_data *drv_data = dev->data;
|
struct bma280_data *drv_data = dev->data;
|
||||||
|
const struct bma280_config *config = dev->config;
|
||||||
|
|
||||||
/* set latched interrupts */
|
/* set latched interrupts */
|
||||||
if (i2c_reg_write_byte(drv_data->i2c, BMA280_I2C_ADDRESS,
|
if (i2c_reg_write_byte_dt(&config->i2c,
|
||||||
BMA280_REG_INT_RST_LATCH,
|
BMA280_REG_INT_RST_LATCH,
|
||||||
BMA280_BIT_INT_LATCH_RESET |
|
BMA280_BIT_INT_LATCH_RESET |
|
||||||
BMA280_INT_MODE_LATCH) < 0) {
|
BMA280_INT_MODE_LATCH) < 0) {
|
||||||
LOG_DBG("Could not set latched interrupts");
|
LOG_DBG("Could not set latched interrupts");
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
@ -240,34 +243,34 @@ int bma280_init_interrupt(const struct device *dev)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* map data ready interrupt to INT1 */
|
/* map data ready interrupt to INT1 */
|
||||||
if (i2c_reg_update_byte(drv_data->i2c, BMA280_I2C_ADDRESS,
|
if (i2c_reg_update_byte_dt(&config->i2c,
|
||||||
BMA280_REG_INT_MAP_1,
|
BMA280_REG_INT_MAP_1,
|
||||||
BMA280_INT_MAP_1_BIT_DATA,
|
BMA280_INT_MAP_1_BIT_DATA,
|
||||||
BMA280_INT_MAP_1_BIT_DATA) < 0) {
|
BMA280_INT_MAP_1_BIT_DATA) < 0) {
|
||||||
LOG_DBG("Could not map data ready interrupt pin");
|
LOG_DBG("Could not map data ready interrupt pin");
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* map any-motion interrupt to INT1 */
|
/* map any-motion interrupt to INT1 */
|
||||||
if (i2c_reg_update_byte(drv_data->i2c, BMA280_I2C_ADDRESS,
|
if (i2c_reg_update_byte_dt(&config->i2c,
|
||||||
BMA280_REG_INT_MAP_0,
|
BMA280_REG_INT_MAP_0,
|
||||||
BMA280_INT_MAP_0_BIT_SLOPE,
|
BMA280_INT_MAP_0_BIT_SLOPE,
|
||||||
BMA280_INT_MAP_0_BIT_SLOPE) < 0) {
|
BMA280_INT_MAP_0_BIT_SLOPE) < 0) {
|
||||||
LOG_DBG("Could not map any-motion interrupt pin");
|
LOG_DBG("Could not map any-motion interrupt pin");
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i2c_reg_update_byte(drv_data->i2c, BMA280_I2C_ADDRESS,
|
if (i2c_reg_update_byte_dt(&config->i2c,
|
||||||
BMA280_REG_INT_EN_1,
|
BMA280_REG_INT_EN_1,
|
||||||
BMA280_BIT_DATA_EN, 0) < 0) {
|
BMA280_BIT_DATA_EN, 0) < 0) {
|
||||||
LOG_DBG("Could not disable data ready interrupt");
|
LOG_DBG("Could not disable data ready interrupt");
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* disable any-motion interrupt */
|
/* disable any-motion interrupt */
|
||||||
if (i2c_reg_update_byte(drv_data->i2c, BMA280_I2C_ADDRESS,
|
if (i2c_reg_update_byte_dt(&config->i2c,
|
||||||
BMA280_REG_INT_EN_0,
|
BMA280_REG_INT_EN_0,
|
||||||
BMA280_SLOPE_EN_XYZ, 0) < 0) {
|
BMA280_SLOPE_EN_XYZ, 0) < 0) {
|
||||||
LOG_DBG("Could not disable data ready interrupt");
|
LOG_DBG("Could not disable data ready interrupt");
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue