drivers: sensor: lsm303dlhc_magn: 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
129e7d5cda
commit
45b70fc8cf
2 changed files with 16 additions and 31 deletions
|
@ -24,10 +24,8 @@ static int lsm303dlhc_sample_fetch(const struct device *dev,
|
|||
uint8_t status;
|
||||
|
||||
/* Check data ready flag */
|
||||
if (i2c_reg_read_byte(drv_data->i2c,
|
||||
config->i2c_address,
|
||||
LSM303DLHC_SR_REG_M,
|
||||
&status) < 0) {
|
||||
if (i2c_reg_read_byte_dt(&config->i2c, LSM303DLHC_SR_REG_M,
|
||||
&status) < 0) {
|
||||
LOG_ERR("Failed to read status register.");
|
||||
return -EIO;
|
||||
}
|
||||
|
@ -37,10 +35,8 @@ static int lsm303dlhc_sample_fetch(const struct device *dev,
|
|||
return -EIO;
|
||||
}
|
||||
|
||||
if (i2c_burst_read(drv_data->i2c,
|
||||
config->i2c_address,
|
||||
LSM303DLHC_REG_MAGN_X_LSB,
|
||||
magn_buf, 6) < 0) {
|
||||
if (i2c_burst_read_dt(&config->i2c, LSM303DLHC_REG_MAGN_X_LSB,
|
||||
magn_buf, 6) < 0) {
|
||||
LOG_ERR("Could not read magn axis data.");
|
||||
return -EIO;
|
||||
}
|
||||
|
@ -101,38 +97,29 @@ static const struct sensor_driver_api lsm303dlhc_magn_driver_api = {
|
|||
static int lsm303dlhc_magn_init(const struct device *dev)
|
||||
{
|
||||
const struct lsm303dlhc_magn_config *config = dev->config;
|
||||
struct lsm303dlhc_magn_data *drv_data = dev->data;
|
||||
|
||||
drv_data->i2c = device_get_binding(config->i2c_name);
|
||||
if (drv_data->i2c == NULL) {
|
||||
LOG_ERR("Could not get pointer to %s device",
|
||||
config->i2c_name);
|
||||
if (!device_is_ready(config->i2c.bus)) {
|
||||
LOG_ERR("I2C bus device not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
/* Set magnetometer output data rate */
|
||||
if (i2c_reg_write_byte(drv_data->i2c,
|
||||
config->i2c_address,
|
||||
LSM303DLHC_CRA_REG_M,
|
||||
LSM303DLHC_MAGN_ODR_BITS) < 0) {
|
||||
if (i2c_reg_write_byte_dt(&config->i2c, LSM303DLHC_CRA_REG_M,
|
||||
LSM303DLHC_MAGN_ODR_BITS) < 0) {
|
||||
LOG_ERR("Failed to configure chip.");
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
/* Set magnetometer full scale range */
|
||||
if (i2c_reg_write_byte(drv_data->i2c,
|
||||
config->i2c_address,
|
||||
LSM303DLHC_CRB_REG_M,
|
||||
LSM303DLHC_MAGN_FS_BITS) < 0) {
|
||||
if (i2c_reg_write_byte_dt(&config->i2c, LSM303DLHC_CRB_REG_M,
|
||||
LSM303DLHC_MAGN_FS_BITS) < 0) {
|
||||
LOG_ERR("Failed to set magnetometer full scale range.");
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
/* Continuous update */
|
||||
if (i2c_reg_write_byte(drv_data->i2c,
|
||||
config->i2c_address,
|
||||
LSM303DLHC_MR_REG_M,
|
||||
LSM303DLHC_MAGN_CONT_UPDATE) < 0) {
|
||||
if (i2c_reg_write_byte_dt(&config->i2c, LSM303DLHC_MR_REG_M,
|
||||
LSM303DLHC_MAGN_CONT_UPDATE) < 0) {
|
||||
LOG_ERR("Failed to enable continuous data update.");
|
||||
return -EIO;
|
||||
}
|
||||
|
@ -140,8 +127,7 @@ static int lsm303dlhc_magn_init(const struct device *dev)
|
|||
}
|
||||
|
||||
static const struct lsm303dlhc_magn_config lsm303dlhc_magn_config = {
|
||||
.i2c_name = DT_INST_BUS_LABEL(0),
|
||||
.i2c_address = DT_INST_REG_ADDR(0),
|
||||
.i2c = I2C_DT_SPEC_INST_GET(0),
|
||||
};
|
||||
|
||||
static struct lsm303dlhc_magn_data lsm303dlhc_magn_driver;
|
||||
|
|
|
@ -7,6 +7,8 @@
|
|||
#ifndef __SENSOR_LSM303DLHC_MAGN_
|
||||
#define __SENSOR_LSM303DLHC_MAGN_
|
||||
|
||||
#include <zephyr/drivers/i2c.h>
|
||||
|
||||
#define LSM303_DLHC_MAGN_X_EN_BIT BIT(0)
|
||||
#define LSM303DLHC_MAGN_Y_EN_BIT BIT(1)
|
||||
#define LSM303DLHC_MAGN_Z_EN_BIT BIT(2)
|
||||
|
@ -72,15 +74,12 @@
|
|||
#define LSM303DLHC_SR_REG_M 0x09
|
||||
|
||||
struct lsm303dlhc_magn_data {
|
||||
const struct device *i2c;
|
||||
|
||||
int16_t magn_x;
|
||||
int16_t magn_y;
|
||||
int16_t magn_z;
|
||||
};
|
||||
|
||||
struct lsm303dlhc_magn_config {
|
||||
char *i2c_name;
|
||||
uint8_t i2c_address;
|
||||
struct i2c_dt_spec i2c;
|
||||
};
|
||||
#endif /* _SENSOR_LSM303DLHC_MAGN_ */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue