From 62c4cf91e6ab3d05f231ef2ca2bbe4a366b2da66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Bj=C3=B6rnsson?= Date: Sun, 26 Jun 2022 06:45:11 +0200 Subject: [PATCH] drivers: sensor: mpu6050: Update driver to use i2c_dt_spec MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Simplify driver by using i2c_dt_spec for bus access. Signed-off-by: Benjamin Björnsson --- drivers/sensor/mpu6050/mpu6050.c | 33 ++++++++++-------------- drivers/sensor/mpu6050/mpu6050.h | 6 ++--- drivers/sensor/mpu6050/mpu6050_trigger.c | 4 +-- 3 files changed, 17 insertions(+), 26 deletions(-) diff --git a/drivers/sensor/mpu6050/mpu6050.c b/drivers/sensor/mpu6050/mpu6050.c index a17dbc0ddaf..b603c5878d2 100644 --- a/drivers/sensor/mpu6050/mpu6050.c +++ b/drivers/sensor/mpu6050/mpu6050.c @@ -116,8 +116,8 @@ static int mpu6050_sample_fetch(const struct device *dev, const struct mpu6050_config *cfg = dev->config; int16_t buf[7]; - if (i2c_burst_read(drv_data->i2c, cfg->i2c_addr, - MPU6050_REG_DATA_START, (uint8_t *)buf, 14) < 0) { + if (i2c_burst_read_dt(&cfg->i2c, MPU6050_REG_DATA_START, (uint8_t *)buf, + 14) < 0) { LOG_ERR("Failed to read data sample."); return -EIO; } @@ -147,16 +147,13 @@ int mpu6050_init(const struct device *dev) const struct mpu6050_config *cfg = dev->config; uint8_t id, i; - drv_data->i2c = device_get_binding(cfg->i2c_label); - if (drv_data->i2c == NULL) { - LOG_ERR("Failed to get pointer to %s device", - cfg->i2c_label); - return -EINVAL; + if (!device_is_ready(cfg->i2c.bus)) { + LOG_ERR("Bus device is not ready"); + return -ENODEV; } /* check chip ID */ - if (i2c_reg_read_byte(drv_data->i2c, cfg->i2c_addr, - MPU6050_REG_CHIP_ID, &id) < 0) { + if (i2c_reg_read_byte_dt(&cfg->i2c, MPU6050_REG_CHIP_ID, &id) < 0) { LOG_ERR("Failed to read chip ID."); return -EIO; } @@ -167,9 +164,8 @@ int mpu6050_init(const struct device *dev) } /* wake up chip */ - if (i2c_reg_update_byte(drv_data->i2c, cfg->i2c_addr, - MPU6050_REG_PWR_MGMT1, MPU6050_SLEEP_EN, - 0) < 0) { + if (i2c_reg_update_byte_dt(&cfg->i2c, MPU6050_REG_PWR_MGMT1, + MPU6050_SLEEP_EN, 0) < 0) { LOG_ERR("Failed to wake up chip."); return -EIO; } @@ -186,9 +182,8 @@ int mpu6050_init(const struct device *dev) return -EINVAL; } - if (i2c_reg_write_byte(drv_data->i2c, cfg->i2c_addr, - MPU6050_REG_ACCEL_CFG, - i << MPU6050_ACCEL_FS_SHIFT) < 0) { + if (i2c_reg_write_byte_dt(&cfg->i2c, MPU6050_REG_ACCEL_CFG, + i << MPU6050_ACCEL_FS_SHIFT) < 0) { LOG_ERR("Failed to write accel full-scale range."); return -EIO; } @@ -207,9 +202,8 @@ int mpu6050_init(const struct device *dev) return -EINVAL; } - if (i2c_reg_write_byte(drv_data->i2c, cfg->i2c_addr, - MPU6050_REG_GYRO_CFG, - i << MPU6050_GYRO_FS_SHIFT) < 0) { + if (i2c_reg_write_byte_dt(&cfg->i2c, MPU6050_REG_GYRO_CFG, + i << MPU6050_GYRO_FS_SHIFT) < 0) { LOG_ERR("Failed to write gyro full-scale range."); return -EIO; } @@ -228,8 +222,7 @@ int mpu6050_init(const struct device *dev) static struct mpu6050_data mpu6050_driver; static const struct mpu6050_config mpu6050_cfg = { - .i2c_label = DT_INST_BUS_LABEL(0), - .i2c_addr = DT_INST_REG_ADDR(0), + .i2c = I2C_DT_SPEC_INST_GET(0), #ifdef CONFIG_MPU6050_TRIGGER .int_pin = DT_INST_GPIO_PIN(0, int_gpios), .int_flags = DT_INST_GPIO_FLAGS(0, int_gpios), diff --git a/drivers/sensor/mpu6050/mpu6050.h b/drivers/sensor/mpu6050/mpu6050.h index c3f4b50ca30..576093197ad 100644 --- a/drivers/sensor/mpu6050/mpu6050.h +++ b/drivers/sensor/mpu6050/mpu6050.h @@ -8,6 +8,7 @@ #define ZEPHYR_DRIVERS_SENSOR_MPU6050_MPU6050_H_ #include +#include #include #include #include @@ -36,8 +37,6 @@ static const uint16_t mpu6050_gyro_sensitivity_x10[] = { }; struct mpu6050_data { - const struct device *i2c; - int16_t accel_x; int16_t accel_y; int16_t accel_z; @@ -70,8 +69,7 @@ struct mpu6050_data { }; struct mpu6050_config { - const char *i2c_label; - uint16_t i2c_addr; + struct i2c_dt_spec i2c; #ifdef CONFIG_MPU6050_TRIGGER uint8_t int_pin; uint8_t int_flags; diff --git a/drivers/sensor/mpu6050/mpu6050_trigger.c b/drivers/sensor/mpu6050/mpu6050_trigger.c index 3a07fbd3965..2af4cc63a87 100644 --- a/drivers/sensor/mpu6050/mpu6050_trigger.c +++ b/drivers/sensor/mpu6050/mpu6050_trigger.c @@ -123,8 +123,8 @@ int mpu6050_init_interrupt(const struct device *dev) } /* enable data ready interrupt */ - if (i2c_reg_write_byte(drv_data->i2c, cfg->i2c_addr, - MPU6050_REG_INT_EN, MPU6050_DRDY_EN) < 0) { + if (i2c_reg_write_byte_dt(&cfg->i2c, MPU6050_REG_INT_EN, + MPU6050_DRDY_EN) < 0) { LOG_ERR("Failed to enable data ready interrupt."); return -EIO; }