Skip to content

Commit 0f62594

Browse files
benjaminbjornssonMaureenHelm
authored andcommitted
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 <[email protected]>
1 parent 5afcea0 commit 0f62594

File tree

2 files changed

+33
-36
lines changed

2 files changed

+33
-36
lines changed

drivers/sensor/ens210/ens210.c

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ static uint32_t ens210_crc7(uint32_t bitstream)
4343
static int ens210_measure(const struct device *dev, enum sensor_channel chan)
4444
{
4545
struct ens210_data *drv_data = dev->data;
46+
const struct ens210_config *config = dev->config;
4647
uint8_t buf;
4748
int ret;
4849
const struct ens210_sens_start sense_start = {
@@ -53,9 +54,7 @@ static int ens210_measure(const struct device *dev, enum sensor_channel chan)
5354
};
5455

5556
/* Start measuring */
56-
ret = i2c_reg_write_byte(drv_data->i2c,
57-
DT_INST_REG_ADDR(0),
58-
ENS210_REG_SENS_START, *(uint8_t *)&sense_start);
57+
ret = i2c_reg_write_byte_dt(&config->i2c, ENS210_REG_SENS_START, *(uint8_t *)&sense_start);
5958

6059
if (ret < 0) {
6160
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)
6665
/* Wait for measurement to be completed */
6766
do {
6867
k_sleep(K_MSEC(2));
69-
ret = i2c_reg_read_byte(drv_data->i2c,
70-
DT_INST_REG_ADDR(0),
71-
ENS210_REG_SENS_START, &buf);
68+
ret = i2c_reg_read_byte_dt(&config->i2c, ENS210_REG_SENS_START, &buf);
7269

7370
if (ret < 0) {
7471
LOG_ERR("Failed to read SENS_STAT");
@@ -83,6 +80,7 @@ static int ens210_sample_fetch(const struct device *dev,
8380
enum sensor_channel chan)
8481
{
8582
struct ens210_data *drv_data = dev->data;
83+
const struct ens210_config *config = dev->config;
8684
struct ens210_value_data data[2];
8785
int ret, cnt;
8886

@@ -104,8 +102,8 @@ static int ens210_sample_fetch(const struct device *dev,
104102
#endif /* Single shot mode */
105103

106104
for (cnt = 0; cnt <= CONFIG_ENS210_MAX_READ_RETRIES; cnt++) {
107-
ret = i2c_burst_read(drv_data->i2c, DT_INST_REG_ADDR(0),
108-
ENS210_REG_T_VAL, (uint8_t *)&data, sizeof(data));
105+
ret = i2c_burst_read_dt(&config->i2c, ENS210_REG_T_VAL, (uint8_t *)&data,
106+
sizeof(data));
109107
if (ret < 0) {
110108
LOG_ERR("Failed to read data");
111109
continue;
@@ -192,16 +190,15 @@ static int ens210_channel_get(const struct device *dev,
192190

193191
static int ens210_sys_reset(const struct device *dev)
194192
{
195-
struct ens210_data *drv_data = dev->data;
193+
const struct ens210_config *config = dev->config;
196194

197195
const struct ens210_sys_ctrl sys_ctrl = {
198196
.low_power = 0,
199197
.reset = 1
200198
};
201199
int ret;
202200

203-
ret = i2c_reg_write_byte(drv_data->i2c, DT_INST_REG_ADDR(0),
204-
ENS210_REG_SYS_CTRL, *(uint8_t *)&sys_ctrl);
201+
ret = i2c_reg_write_byte_dt(&config->i2c, ENS210_REG_SYS_CTRL, *(uint8_t *)&sys_ctrl);
205202
if (ret < 0) {
206203
LOG_ERR("Failed to set SYS_CTRL to 0x%x", *(uint8_t *)&sys_ctrl);
207204
}
@@ -210,16 +207,15 @@ static int ens210_sys_reset(const struct device *dev)
210207

211208
static int ens210_sys_enable(const struct device *dev, uint8_t low_power)
212209
{
213-
struct ens210_data *drv_data = dev->data;
210+
const struct ens210_config *config = dev->config;
214211

215212
const struct ens210_sys_ctrl sys_ctrl = {
216213
.low_power = low_power,
217214
.reset = 0
218215
};
219216
int ret;
220217

221-
ret = i2c_reg_write_byte(drv_data->i2c, DT_INST_REG_ADDR(0),
222-
ENS210_REG_SYS_CTRL, *(uint8_t *)&sys_ctrl);
218+
ret = i2c_reg_write_byte_dt(&config->i2c, ENS210_REG_SYS_CTRL, *(uint8_t *)&sys_ctrl);
223219
if (ret < 0) {
224220
LOG_ERR("Failed to set SYS_CTRL to 0x%x", *(uint8_t *)&sys_ctrl);
225221
}
@@ -228,16 +224,14 @@ static int ens210_sys_enable(const struct device *dev, uint8_t low_power)
228224

229225
static int ens210_wait_boot(const struct device *dev)
230226
{
231-
struct ens210_data *drv_data = dev->data;
227+
const struct ens210_config *config = dev->config;
232228

233229
int cnt;
234230
int ret;
235231
struct ens210_sys_stat sys_stat;
236232

237233
for (cnt = 0; cnt <= CONFIG_ENS210_MAX_STAT_RETRIES; cnt++) {
238-
ret = i2c_reg_read_byte(drv_data->i2c, DT_INST_REG_ADDR(0),
239-
ENS210_REG_SYS_STAT,
240-
(uint8_t *)&sys_stat);
234+
ret = i2c_reg_read_byte_dt(&config->i2c, ENS210_REG_SYS_STAT, (uint8_t *)&sys_stat);
241235

242236
if (ret < 0) {
243237
k_sleep(K_MSEC(1));
@@ -273,7 +267,7 @@ static const struct sensor_driver_api en210_driver_api = {
273267

274268
static int ens210_init(const struct device *dev)
275269
{
276-
struct ens210_data *drv_data = dev->data;
270+
const struct ens210_config *config = dev->config;
277271
const struct ens210_sens_run sense_run = {
278272
.t_run = ENS210_T_RUN,
279273
.h_run = ENS210_H_RUN
@@ -290,11 +284,9 @@ static int ens210_init(const struct device *dev)
290284
int ret;
291285
uint16_t part_id;
292286

293-
drv_data->i2c = device_get_binding(DT_INST_BUS_LABEL(0));
294-
if (drv_data->i2c == NULL) {
295-
LOG_ERR("Failed to get pointer to %s device!",
296-
DT_INST_BUS_LABEL(0));
297-
return -EINVAL;
287+
if (!device_is_ready(config->i2c.bus)) {
288+
LOG_ERR("I2C bus device not ready");
289+
return -ENODEV;
298290
}
299291

300292
/* Wait until the device is ready. */
@@ -306,9 +298,8 @@ static int ens210_init(const struct device *dev)
306298
/* Check Hardware ID. This is only possible after device is ready
307299
* and active
308300
*/
309-
ret = i2c_burst_read(drv_data->i2c, DT_INST_REG_ADDR(0),
310-
ENS210_REG_PART_ID, (uint8_t *)&part_id,
311-
sizeof(part_id));
301+
ret = i2c_burst_read_dt(&config->i2c, ENS210_REG_PART_ID, (uint8_t *)&part_id,
302+
sizeof(part_id));
312303
if (ret < 0) {
313304
LOG_ERR("Failed to read Part ID register");
314305
return -EIO;
@@ -326,8 +317,7 @@ static int ens210_init(const struct device *dev)
326317
}
327318

328319
/* Set measurement mode*/
329-
ret = i2c_reg_write_byte(drv_data->i2c, DT_INST_REG_ADDR(0),
330-
ENS210_REG_SENS_RUN, *(uint8_t *)&sense_run);
320+
ret = i2c_reg_write_byte_dt(&config->i2c, ENS210_REG_SENS_RUN, *(uint8_t *)&sense_run);
331321
if (ret < 0) {
332322
LOG_ERR("Failed to set SENS_RUN to 0x%x",
333323
*(uint8_t *)&sense_run);
@@ -337,8 +327,7 @@ static int ens210_init(const struct device *dev)
337327
#if defined(CONFIG_ENS210_TEMPERATURE_CONTINUOUS) \
338328
|| defined(CONFIG_ENS210_HUMIDITY_CONTINUOUS)
339329
/* Start measuring */
340-
ret = i2c_reg_write_byte(drv_data->i2c, DT_INST_REG_ADDR(0),
341-
ENS210_REG_SENS_START, *(uint8_t *)&sense_start);
330+
ret = i2c_reg_write_byte_dt(&config->i2c, ENS210_REG_SENS_START, *(uint8_t *)&sense_start);
342331
if (ret < 0) {
343332
LOG_ERR("Failed to set SENS_START to 0x%x",
344333
*(uint8_t *)&sense_start);
@@ -348,8 +337,12 @@ static int ens210_init(const struct device *dev)
348337
return 0;
349338
}
350339

351-
static struct ens210_data ens210_driver;
340+
static struct ens210_data ens210_data_inst;
341+
342+
static const struct ens210_config ens210_config_inst = {
343+
.i2c = I2C_DT_SPEC_INST_GET(0),
344+
};
352345

353-
DEVICE_DT_INST_DEFINE(0, ens210_init, NULL, &ens210_driver,
354-
NULL, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,
355-
&en210_driver_api);
346+
DEVICE_DT_INST_DEFINE(0, ens210_init, NULL, &ens210_data_inst,
347+
&ens210_config_inst, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,
348+
&en210_driver_api);

drivers/sensor/ens210/ens210.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define ZEPHYR_DRIVERS_SENSOR_ENS210_ENS210_H_
99

1010
#include <zephyr/device.h>
11+
#include <zephyr/drivers/i2c.h>
1112
#include <zephyr/drivers/gpio.h>
1213
#include <zephyr/sys/util.h>
1314

@@ -101,9 +102,12 @@ struct ens210_sens_stat {
101102
} __packed;
102103

103104
struct ens210_data {
104-
const struct device *i2c;
105105
struct ens210_value_data temp;
106106
struct ens210_value_data humidity;
107107
};
108108

109+
struct ens210_config {
110+
struct i2c_dt_spec i2c;
111+
};
112+
109113
#endif /* ZEPHYR_DRIVERS_SENSOR_ENS210_ENS210_H_ */

0 commit comments

Comments
 (0)