drivers: sensor: mpu6050: 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:
Benjamin Björnsson 2022-06-26 06:45:11 +02:00 committed by Maureen Helm
parent 9d8f852729
commit 62c4cf91e6
3 changed files with 17 additions and 26 deletions

View File

@ -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),

View File

@ -8,6 +8,7 @@
#define ZEPHYR_DRIVERS_SENSOR_MPU6050_MPU6050_H_
#include <zephyr/device.h>
#include <zephyr/drivers/i2c.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/sys/util.h>
#include <zephyr/types.h>
@ -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;

View File

@ -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;
}