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)
|
||||
{
|
||||
struct bma280_data *drv_data = dev->data;
|
||||
const struct bma280_config *config = dev->config;
|
||||
uint8_t buf[6];
|
||||
uint8_t lsb;
|
||||
|
||||
|
@ -29,7 +30,7 @@ static int bma280_sample_fetch(const struct device *dev,
|
|||
* since all accel data register addresses are consecutive,
|
||||
* 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) {
|
||||
LOG_DBG("Could not read accel axis data");
|
||||
return -EIO;
|
||||
|
@ -44,7 +45,7 @@ static int bma280_sample_fetch(const struct device *dev,
|
|||
lsb = (buf[4] & BMA280_ACCEL_LSB_MASK) >> BMA280_ACCEL_LSB_SHIFT;
|
||||
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,
|
||||
(uint8_t *)&drv_data->temp_sample) < 0) {
|
||||
LOG_DBG("Could not read temperature data");
|
||||
|
@ -116,18 +117,16 @@ static const struct sensor_driver_api bma280_driver_api = {
|
|||
|
||||
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;
|
||||
|
||||
drv_data->i2c = device_get_binding(DT_INST_BUS_LABEL(0));
|
||||
if (drv_data->i2c == NULL) {
|
||||
LOG_DBG("Could not get pointer to %s device",
|
||||
DT_INST_BUS_LABEL(0));
|
||||
return -EINVAL;
|
||||
if (!device_is_ready(config->i2c.bus)) {
|
||||
LOG_ERR("I2C bus device not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
/* 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) {
|
||||
LOG_DBG("Could not read chip id");
|
||||
return -EIO;
|
||||
|
@ -138,14 +137,14 @@ int bma280_init(const struct device *dev)
|
|||
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) {
|
||||
LOG_DBG("Could not set data filter bandwidth");
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
/* 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) {
|
||||
LOG_DBG("Could not set data g-range");
|
||||
return -EIO;
|
||||
|
@ -161,8 +160,12 @@ int bma280_init(const struct device *dev)
|
|||
return 0;
|
||||
}
|
||||
|
||||
struct bma280_data bma280_driver;
|
||||
static struct bma280_data bma280_inst_data;
|
||||
|
||||
DEVICE_DT_INST_DEFINE(0, bma280_init, NULL, &bma280_driver,
|
||||
NULL, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,
|
||||
&bma280_driver_api);
|
||||
static const struct bma280_config bma280_inst_config = {
|
||||
.i2c = I2C_DT_SPEC_INST_GET(0),
|
||||
};
|
||||
|
||||
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/sys/util.h>
|
||||
#include <zephyr/types.h>
|
||||
#include <zephyr/drivers/i2c.h>
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
|
||||
#define BMA280_I2C_ADDRESS DT_INST_REG_ADDR(0)
|
||||
|
||||
#define BMA280_REG_CHIP_ID 0x00
|
||||
#if DT_INST_PROP(0, is_bmc150)
|
||||
#define BMA280_CHIP_ID 0xFA
|
||||
|
@ -114,7 +113,6 @@
|
|||
#define BMA280_THREAD_STACKSIZE_UNIT 1024
|
||||
|
||||
struct bma280_data {
|
||||
const struct device *i2c;
|
||||
int16_t x_sample;
|
||||
int16_t y_sample;
|
||||
int16_t z_sample;
|
||||
|
@ -142,6 +140,10 @@ struct bma280_data {
|
|||
#endif /* CONFIG_BMA280_TRIGGER */
|
||||
};
|
||||
|
||||
struct bma280_config {
|
||||
struct i2c_dt_spec i2c;
|
||||
};
|
||||
|
||||
#ifdef CONFIG_BMA280_TRIGGER
|
||||
int bma280_trigger_set(const struct device *dev,
|
||||
const struct sensor_trigger *trig,
|
||||
|
|
|
@ -34,7 +34,7 @@ int bma280_attr_set(const struct device *dev,
|
|||
enum sensor_attribute attr,
|
||||
const struct sensor_value *val)
|
||||
{
|
||||
struct bma280_data *drv_data = dev->data;
|
||||
const struct bma280_config *config = dev->config;
|
||||
uint64_t slope_th;
|
||||
|
||||
if (chan != SENSOR_CHAN_ACCEL_XYZ) {
|
||||
|
@ -45,14 +45,14 @@ int bma280_attr_set(const struct device *dev,
|
|||
/* slope_th = (val * 10^6 * 2^10) / BMA280_PMU_FULL_RAGE */
|
||||
slope_th = (uint64_t)val->val1 * 1000000U + (uint64_t)val->val2;
|
||||
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)
|
||||
< 0) {
|
||||
LOG_DBG("Could not set slope threshold");
|
||||
return -EIO;
|
||||
}
|
||||
} 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_SLOPE_DUR_MASK,
|
||||
val->val1 << BMA280_SLOPE_DUR_SHIFT)
|
||||
|
@ -87,11 +87,12 @@ static void bma280_gpio_callback(const struct device *dev,
|
|||
static void bma280_thread_cb(const struct device *dev)
|
||||
{
|
||||
struct bma280_data *drv_data = dev->data;
|
||||
const struct bma280_config *config = dev->config;
|
||||
uint8_t status = 0U;
|
||||
int err = 0;
|
||||
|
||||
/* 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);
|
||||
if (status & BMA280_BIT_DATA_INT_STATUS &&
|
||||
drv_data->data_ready_handler != NULL &&
|
||||
|
@ -101,7 +102,7 @@ static void bma280_thread_cb(const struct device *dev)
|
|||
}
|
||||
|
||||
/* 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);
|
||||
if (status & BMA280_BIT_SLOPE_INT_STATUS &&
|
||||
drv_data->any_motion_handler != NULL &&
|
||||
|
@ -110,7 +111,7 @@ static void bma280_thread_cb(const struct device *dev)
|
|||
&drv_data->data_ready_trigger);
|
||||
|
||||
/* 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_BIT_INT_LATCH_RESET,
|
||||
BMA280_BIT_INT_LATCH_RESET);
|
||||
|
@ -149,10 +150,11 @@ int bma280_trigger_set(const struct device *dev,
|
|||
sensor_trigger_handler_t handler)
|
||||
{
|
||||
struct bma280_data *drv_data = dev->data;
|
||||
const struct bma280_config *config = dev->config;
|
||||
|
||||
if (trig->type == SENSOR_TRIG_DATA_READY) {
|
||||
/* 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_BIT_DATA_EN, 0) < 0) {
|
||||
LOG_DBG("Could not disable data ready interrupt");
|
||||
|
@ -166,7 +168,7 @@ int bma280_trigger_set(const struct device *dev,
|
|||
drv_data->data_ready_trigger = *trig;
|
||||
|
||||
/* 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_BIT_DATA_EN,
|
||||
BMA280_BIT_DATA_EN) < 0) {
|
||||
|
@ -175,7 +177,7 @@ int bma280_trigger_set(const struct device *dev,
|
|||
}
|
||||
} else if (trig->type == SENSOR_TRIG_DELTA) {
|
||||
/* 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_SLOPE_EN_XYZ, 0) < 0) {
|
||||
LOG_DBG("Could not disable data ready interrupt");
|
||||
|
@ -189,7 +191,7 @@ int bma280_trigger_set(const struct device *dev,
|
|||
drv_data->any_motion_trigger = *trig;
|
||||
|
||||
/* 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_SLOPE_EN_XYZ,
|
||||
BMA280_SLOPE_EN_XYZ) < 0) {
|
||||
|
@ -206,9 +208,10 @@ int bma280_trigger_set(const struct device *dev,
|
|||
int bma280_init_interrupt(const struct device *dev)
|
||||
{
|
||||
struct bma280_data *drv_data = dev->data;
|
||||
const struct bma280_config *config = dev->config;
|
||||
|
||||
/* 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_BIT_INT_LATCH_RESET |
|
||||
BMA280_INT_MODE_LATCH) < 0) {
|
||||
|
@ -240,7 +243,7 @@ int bma280_init_interrupt(const struct device *dev)
|
|||
}
|
||||
|
||||
/* 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_INT_MAP_1_BIT_DATA,
|
||||
BMA280_INT_MAP_1_BIT_DATA) < 0) {
|
||||
|
@ -249,7 +252,7 @@ int bma280_init_interrupt(const struct device *dev)
|
|||
}
|
||||
|
||||
/* 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_INT_MAP_0_BIT_SLOPE,
|
||||
BMA280_INT_MAP_0_BIT_SLOPE) < 0) {
|
||||
|
@ -257,7 +260,7 @@ int bma280_init_interrupt(const struct device *dev)
|
|||
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_BIT_DATA_EN, 0) < 0) {
|
||||
LOG_DBG("Could not disable data ready interrupt");
|
||||
|
@ -265,7 +268,7 @@ int bma280_init_interrupt(const struct device *dev)
|
|||
}
|
||||
|
||||
/* 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_SLOPE_EN_XYZ, 0) < 0) {
|
||||
LOG_DBG("Could not disable data ready interrupt");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue