drivers: sensor: ens210: 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
5afcea07b2
commit
0f62594f65
2 changed files with 33 additions and 36 deletions
|
@ -43,6 +43,7 @@ static uint32_t ens210_crc7(uint32_t bitstream)
|
|||
static int ens210_measure(const struct device *dev, enum sensor_channel chan)
|
||||
{
|
||||
struct ens210_data *drv_data = dev->data;
|
||||
const struct ens210_config *config = dev->config;
|
||||
uint8_t buf;
|
||||
int ret;
|
||||
const struct ens210_sens_start sense_start = {
|
||||
|
@ -53,9 +54,7 @@ static int ens210_measure(const struct device *dev, enum sensor_channel chan)
|
|||
};
|
||||
|
||||
/* Start measuring */
|
||||
ret = i2c_reg_write_byte(drv_data->i2c,
|
||||
DT_INST_REG_ADDR(0),
|
||||
ENS210_REG_SENS_START, *(uint8_t *)&sense_start);
|
||||
ret = i2c_reg_write_byte_dt(&config->i2c, ENS210_REG_SENS_START, *(uint8_t *)&sense_start);
|
||||
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Failed to set SENS_START to 0x%x",
|
||||
|
@ -66,9 +65,7 @@ static int ens210_measure(const struct device *dev, enum sensor_channel chan)
|
|||
/* Wait for measurement to be completed */
|
||||
do {
|
||||
k_sleep(K_MSEC(2));
|
||||
ret = i2c_reg_read_byte(drv_data->i2c,
|
||||
DT_INST_REG_ADDR(0),
|
||||
ENS210_REG_SENS_START, &buf);
|
||||
ret = i2c_reg_read_byte_dt(&config->i2c, ENS210_REG_SENS_START, &buf);
|
||||
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Failed to read SENS_STAT");
|
||||
|
@ -83,6 +80,7 @@ static int ens210_sample_fetch(const struct device *dev,
|
|||
enum sensor_channel chan)
|
||||
{
|
||||
struct ens210_data *drv_data = dev->data;
|
||||
const struct ens210_config *config = dev->config;
|
||||
struct ens210_value_data data[2];
|
||||
int ret, cnt;
|
||||
|
||||
|
@ -104,8 +102,8 @@ static int ens210_sample_fetch(const struct device *dev,
|
|||
#endif /* Single shot mode */
|
||||
|
||||
for (cnt = 0; cnt <= CONFIG_ENS210_MAX_READ_RETRIES; cnt++) {
|
||||
ret = i2c_burst_read(drv_data->i2c, DT_INST_REG_ADDR(0),
|
||||
ENS210_REG_T_VAL, (uint8_t *)&data, sizeof(data));
|
||||
ret = i2c_burst_read_dt(&config->i2c, ENS210_REG_T_VAL, (uint8_t *)&data,
|
||||
sizeof(data));
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Failed to read data");
|
||||
continue;
|
||||
|
@ -192,7 +190,7 @@ static int ens210_channel_get(const struct device *dev,
|
|||
|
||||
static int ens210_sys_reset(const struct device *dev)
|
||||
{
|
||||
struct ens210_data *drv_data = dev->data;
|
||||
const struct ens210_config *config = dev->config;
|
||||
|
||||
const struct ens210_sys_ctrl sys_ctrl = {
|
||||
.low_power = 0,
|
||||
|
@ -200,8 +198,7 @@ static int ens210_sys_reset(const struct device *dev)
|
|||
};
|
||||
int ret;
|
||||
|
||||
ret = i2c_reg_write_byte(drv_data->i2c, DT_INST_REG_ADDR(0),
|
||||
ENS210_REG_SYS_CTRL, *(uint8_t *)&sys_ctrl);
|
||||
ret = i2c_reg_write_byte_dt(&config->i2c, ENS210_REG_SYS_CTRL, *(uint8_t *)&sys_ctrl);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Failed to set SYS_CTRL to 0x%x", *(uint8_t *)&sys_ctrl);
|
||||
}
|
||||
|
@ -210,7 +207,7 @@ static int ens210_sys_reset(const struct device *dev)
|
|||
|
||||
static int ens210_sys_enable(const struct device *dev, uint8_t low_power)
|
||||
{
|
||||
struct ens210_data *drv_data = dev->data;
|
||||
const struct ens210_config *config = dev->config;
|
||||
|
||||
const struct ens210_sys_ctrl sys_ctrl = {
|
||||
.low_power = low_power,
|
||||
|
@ -218,8 +215,7 @@ static int ens210_sys_enable(const struct device *dev, uint8_t low_power)
|
|||
};
|
||||
int ret;
|
||||
|
||||
ret = i2c_reg_write_byte(drv_data->i2c, DT_INST_REG_ADDR(0),
|
||||
ENS210_REG_SYS_CTRL, *(uint8_t *)&sys_ctrl);
|
||||
ret = i2c_reg_write_byte_dt(&config->i2c, ENS210_REG_SYS_CTRL, *(uint8_t *)&sys_ctrl);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Failed to set SYS_CTRL to 0x%x", *(uint8_t *)&sys_ctrl);
|
||||
}
|
||||
|
@ -228,16 +224,14 @@ static int ens210_sys_enable(const struct device *dev, uint8_t low_power)
|
|||
|
||||
static int ens210_wait_boot(const struct device *dev)
|
||||
{
|
||||
struct ens210_data *drv_data = dev->data;
|
||||
const struct ens210_config *config = dev->config;
|
||||
|
||||
int cnt;
|
||||
int ret;
|
||||
struct ens210_sys_stat sys_stat;
|
||||
|
||||
for (cnt = 0; cnt <= CONFIG_ENS210_MAX_STAT_RETRIES; cnt++) {
|
||||
ret = i2c_reg_read_byte(drv_data->i2c, DT_INST_REG_ADDR(0),
|
||||
ENS210_REG_SYS_STAT,
|
||||
(uint8_t *)&sys_stat);
|
||||
ret = i2c_reg_read_byte_dt(&config->i2c, ENS210_REG_SYS_STAT, (uint8_t *)&sys_stat);
|
||||
|
||||
if (ret < 0) {
|
||||
k_sleep(K_MSEC(1));
|
||||
|
@ -273,7 +267,7 @@ static const struct sensor_driver_api en210_driver_api = {
|
|||
|
||||
static int ens210_init(const struct device *dev)
|
||||
{
|
||||
struct ens210_data *drv_data = dev->data;
|
||||
const struct ens210_config *config = dev->config;
|
||||
const struct ens210_sens_run sense_run = {
|
||||
.t_run = ENS210_T_RUN,
|
||||
.h_run = ENS210_H_RUN
|
||||
|
@ -290,11 +284,9 @@ static int ens210_init(const struct device *dev)
|
|||
int ret;
|
||||
uint16_t part_id;
|
||||
|
||||
drv_data->i2c = device_get_binding(DT_INST_BUS_LABEL(0));
|
||||
if (drv_data->i2c == NULL) {
|
||||
LOG_ERR("Failed to 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;
|
||||
}
|
||||
|
||||
/* Wait until the device is ready. */
|
||||
|
@ -306,9 +298,8 @@ static int ens210_init(const struct device *dev)
|
|||
/* Check Hardware ID. This is only possible after device is ready
|
||||
* and active
|
||||
*/
|
||||
ret = i2c_burst_read(drv_data->i2c, DT_INST_REG_ADDR(0),
|
||||
ENS210_REG_PART_ID, (uint8_t *)&part_id,
|
||||
sizeof(part_id));
|
||||
ret = i2c_burst_read_dt(&config->i2c, ENS210_REG_PART_ID, (uint8_t *)&part_id,
|
||||
sizeof(part_id));
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Failed to read Part ID register");
|
||||
return -EIO;
|
||||
|
@ -326,8 +317,7 @@ static int ens210_init(const struct device *dev)
|
|||
}
|
||||
|
||||
/* Set measurement mode*/
|
||||
ret = i2c_reg_write_byte(drv_data->i2c, DT_INST_REG_ADDR(0),
|
||||
ENS210_REG_SENS_RUN, *(uint8_t *)&sense_run);
|
||||
ret = i2c_reg_write_byte_dt(&config->i2c, ENS210_REG_SENS_RUN, *(uint8_t *)&sense_run);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Failed to set SENS_RUN to 0x%x",
|
||||
*(uint8_t *)&sense_run);
|
||||
|
@ -337,8 +327,7 @@ static int ens210_init(const struct device *dev)
|
|||
#if defined(CONFIG_ENS210_TEMPERATURE_CONTINUOUS) \
|
||||
|| defined(CONFIG_ENS210_HUMIDITY_CONTINUOUS)
|
||||
/* Start measuring */
|
||||
ret = i2c_reg_write_byte(drv_data->i2c, DT_INST_REG_ADDR(0),
|
||||
ENS210_REG_SENS_START, *(uint8_t *)&sense_start);
|
||||
ret = i2c_reg_write_byte_dt(&config->i2c, ENS210_REG_SENS_START, *(uint8_t *)&sense_start);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Failed to set SENS_START to 0x%x",
|
||||
*(uint8_t *)&sense_start);
|
||||
|
@ -348,8 +337,12 @@ static int ens210_init(const struct device *dev)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static struct ens210_data ens210_driver;
|
||||
static struct ens210_data ens210_data_inst;
|
||||
|
||||
DEVICE_DT_INST_DEFINE(0, ens210_init, NULL, &ens210_driver,
|
||||
NULL, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,
|
||||
&en210_driver_api);
|
||||
static const struct ens210_config ens210_config_inst = {
|
||||
.i2c = I2C_DT_SPEC_INST_GET(0),
|
||||
};
|
||||
|
||||
DEVICE_DT_INST_DEFINE(0, ens210_init, NULL, &ens210_data_inst,
|
||||
&ens210_config_inst, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,
|
||||
&en210_driver_api);
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#define ZEPHYR_DRIVERS_SENSOR_ENS210_ENS210_H_
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/drivers/i2c.h>
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
#include <zephyr/sys/util.h>
|
||||
|
||||
|
@ -101,9 +102,12 @@ struct ens210_sens_stat {
|
|||
} __packed;
|
||||
|
||||
struct ens210_data {
|
||||
const struct device *i2c;
|
||||
struct ens210_value_data temp;
|
||||
struct ens210_value_data humidity;
|
||||
};
|
||||
|
||||
struct ens210_config {
|
||||
struct i2c_dt_spec i2c;
|
||||
};
|
||||
|
||||
#endif /* ZEPHYR_DRIVERS_SENSOR_ENS210_ENS210_H_ */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue