drivers: sensor: lis3mdl: 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
0a386dbe6c
commit
37b59c3427
3 changed files with 27 additions and 26 deletions
|
@ -63,13 +63,14 @@ static int lis3mdl_channel_get(const struct device *dev,
|
|||
int lis3mdl_sample_fetch(const struct device *dev, enum sensor_channel chan)
|
||||
{
|
||||
struct lis3mdl_data *drv_data = dev->data;
|
||||
const struct lis3mdl_config *config = dev->config;
|
||||
int16_t buf[4];
|
||||
|
||||
__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);
|
||||
|
||||
/* fetch magnetometer sample */
|
||||
if (i2c_burst_read(drv_data->i2c, DT_INST_REG_ADDR(0),
|
||||
LIS3MDL_REG_SAMPLE_START, (uint8_t *)buf, 8) < 0) {
|
||||
if (i2c_burst_read_dt(&config->i2c, LIS3MDL_REG_SAMPLE_START,
|
||||
(uint8_t *)buf, 8) < 0) {
|
||||
LOG_DBG("Failed to fetch magnetometer sample.");
|
||||
return -EIO;
|
||||
}
|
||||
|
@ -79,8 +80,7 @@ int lis3mdl_sample_fetch(const struct device *dev, enum sensor_channel chan)
|
|||
* the same read as magnetometer data, so do another
|
||||
* burst read to fetch the temperature sample
|
||||
*/
|
||||
if (i2c_burst_read(drv_data->i2c, DT_INST_REG_ADDR(0),
|
||||
LIS3MDL_REG_SAMPLE_START + 6,
|
||||
if (i2c_burst_read_dt(&config->i2c, LIS3MDL_REG_SAMPLE_START + 6,
|
||||
(uint8_t *)(buf + 3), 2) < 0) {
|
||||
LOG_DBG("Failed to fetch temperature sample.");
|
||||
return -EIO;
|
||||
|
@ -104,21 +104,17 @@ static const struct sensor_driver_api lis3mdl_driver_api = {
|
|||
|
||||
int lis3mdl_init(const struct device *dev)
|
||||
{
|
||||
struct lis3mdl_data *drv_data = dev->data;
|
||||
const struct lis3mdl_config *config = dev->config;
|
||||
uint8_t chip_cfg[6];
|
||||
uint8_t id, idx;
|
||||
|
||||
drv_data->i2c = device_get_binding(DT_INST_BUS_LABEL(0));
|
||||
|
||||
if (drv_data->i2c == NULL) {
|
||||
LOG_ERR("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;
|
||||
}
|
||||
|
||||
/* check chip ID */
|
||||
if (i2c_reg_read_byte(drv_data->i2c, DT_INST_REG_ADDR(0),
|
||||
LIS3MDL_REG_WHO_AM_I, &id) < 0) {
|
||||
if (i2c_reg_read_byte_dt(&config->i2c, LIS3MDL_REG_WHO_AM_I, &id) < 0) {
|
||||
LOG_ERR("Failed to read chip ID.");
|
||||
return -EIO;
|
||||
}
|
||||
|
@ -150,8 +146,7 @@ int lis3mdl_init(const struct device *dev)
|
|||
LIS3MDL_OM_SHIFT) << LIS3MDL_OMZ_SHIFT;
|
||||
chip_cfg[5] = LIS3MDL_BDU_EN;
|
||||
|
||||
if (i2c_write(drv_data->i2c,
|
||||
chip_cfg, 6, DT_INST_REG_ADDR(0)) < 0) {
|
||||
if (i2c_write_dt(&config->i2c, chip_cfg, 6) < 0) {
|
||||
LOG_DBG("Failed to configure chip.");
|
||||
return -EIO;
|
||||
}
|
||||
|
@ -166,8 +161,12 @@ int lis3mdl_init(const struct device *dev)
|
|||
return 0;
|
||||
}
|
||||
|
||||
struct lis3mdl_data lis3mdl_driver;
|
||||
static struct lis3mdl_data lis3mdl_data_inst;
|
||||
|
||||
DEVICE_DT_INST_DEFINE(0, lis3mdl_init, NULL,
|
||||
&lis3mdl_driver, NULL, POST_KERNEL,
|
||||
static struct lis3mdl_config lis3mdl_config_inst = {
|
||||
.i2c = I2C_DT_SPEC_INST_GET(0),
|
||||
};
|
||||
|
||||
DEVICE_DT_INST_DEFINE(0, lis3mdl_init, NULL, &lis3mdl_data_inst,
|
||||
&lis3mdl_config_inst, POST_KERNEL,
|
||||
CONFIG_SENSOR_INIT_PRIORITY, &lis3mdl_driver_api);
|
||||
|
|
|
@ -10,11 +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 LIS3MDL_I2C_ADDR_BASE 0x1C
|
||||
#define LIS3MDL_I2C_ADDR_MASK (~BIT(1))
|
||||
|
||||
#define LIS3MDL_REG_WHO_AM_I 0x0F
|
||||
#define LIS3MDL_CHIP_ID 0x3D
|
||||
|
||||
|
@ -111,7 +109,6 @@ static const uint16_t lis3mdl_magn_gain[] = {
|
|||
};
|
||||
|
||||
struct lis3mdl_data {
|
||||
const struct device *i2c;
|
||||
int16_t x_sample;
|
||||
int16_t y_sample;
|
||||
int16_t z_sample;
|
||||
|
@ -136,6 +133,10 @@ struct lis3mdl_data {
|
|||
#endif /* CONFIG_LIS3MDL_TRIGGER */
|
||||
};
|
||||
|
||||
struct lis3mdl_config {
|
||||
struct i2c_dt_spec i2c;
|
||||
};
|
||||
|
||||
#ifdef CONFIG_LIS3MDL_TRIGGER
|
||||
int lis3mdl_trigger_set(const struct device *dev,
|
||||
const struct sensor_trigger *trig,
|
||||
|
|
|
@ -22,14 +22,15 @@ int lis3mdl_trigger_set(const struct device *dev,
|
|||
sensor_trigger_handler_t handler)
|
||||
{
|
||||
struct lis3mdl_data *drv_data = dev->data;
|
||||
const struct lis3mdl_config *config = dev->config;
|
||||
int16_t buf[3];
|
||||
int ret;
|
||||
|
||||
__ASSERT_NO_MSG(trig->type == SENSOR_TRIG_DATA_READY);
|
||||
|
||||
/* dummy read: re-trigger interrupt */
|
||||
ret = i2c_burst_read(drv_data->i2c, DT_INST_REG_ADDR(0),
|
||||
LIS3MDL_REG_SAMPLE_START, (uint8_t *)buf, 6);
|
||||
ret = i2c_burst_read_dt(&config->i2c, LIS3MDL_REG_SAMPLE_START,
|
||||
(uint8_t *)buf, 6);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue