Skip to content

Commit 6e5a7df

Browse files
committed
bmp581: Move bus transfers to work over RTIO
In order to abstract details of the transport itself, improving portability. No functional changes at this point. Driver works the same as far as my local testing goes. Signed-off-by: Luis Ubieda <luisf@croxel.com>
1 parent bc7f0c9 commit 6e5a7df

File tree

6 files changed

+203
-27
lines changed

6 files changed

+203
-27
lines changed

drivers/sensor/bosch/bmp581/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@
55
#
66

77
zephyr_library()
8-
zephyr_library_sources(bmp581.c)
8+
zephyr_library_include_directories(.)
9+
zephyr_library_sources(
10+
bmp581.c
11+
bmp581_bus.c
12+
)

drivers/sensor/bosch/bmp581/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ config BMP581
88
bool "BMP581 barometric pressure sensor"
99
depends on DT_HAS_BOSCH_BMP581_ENABLED
1010
select I2C
11+
select I2C_RTIO
1112
default y

drivers/sensor/bosch/bmp581/bmp581.c

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
#include "bmp581.h"
9+
#include "bmp581_bus.h"
910

1011
#include <math.h>
1112

@@ -60,14 +61,13 @@ static int set_power_mode(enum bmp5_powermode powermode, const struct device *de
6061
* Device should be set to standby before transitioning to forced mode or normal
6162
* mode or continuous mode.
6263
*/
63-
64-
ret = i2c_reg_read_byte_dt(&conf->i2c, BMP5_REG_ODR_CONFIG, &odr);
64+
ret = bmp581_reg_read_rtio(&conf->bus, BMP5_REG_ODR_CONFIG, &odr, 1);
6565
if (ret == BMP5_OK) {
6666
/* Setting deep_dis = 1(BMP5_DEEP_DISABLED) disables the deep standby mode
6767
*/
6868
odr = BMP5_SET_BITSLICE(odr, BMP5_DEEP_DISABLE, BMP5_DEEP_DISABLED);
6969
odr = BMP5_SET_BITS_POS_0(odr, BMP5_POWERMODE, BMP5_POWERMODE_STANDBY);
70-
ret = i2c_reg_write_byte_dt(&conf->i2c, BMP5_REG_ODR_CONFIG, odr);
70+
ret = bmp581_reg_write_rtio(&conf->bus, BMP5_REG_ODR_CONFIG, &odr, 1);
7171

7272
if (ret != BMP5_OK) {
7373
LOG_DBG("Failed to set power mode to BMP5_POWERMODE_STANDBY.");
@@ -92,7 +92,7 @@ static int set_power_mode(enum bmp5_powermode powermode, const struct device *de
9292
case BMP5_POWERMODE_CONTINUOUS:
9393
odr = BMP5_SET_BITSLICE(odr, BMP5_DEEP_DISABLE, BMP5_DEEP_DISABLED);
9494
odr = BMP5_SET_BITS_POS_0(odr, BMP5_POWERMODE, powermode);
95-
ret = i2c_reg_write_byte_dt(&conf->i2c, BMP5_REG_ODR_CONFIG, odr);
95+
ret = bmp581_reg_write_rtio(&conf->bus, BMP5_REG_ODR_CONFIG, &odr, 1);
9696
break;
9797
default:
9898
/* invalid power mode */
@@ -116,7 +116,7 @@ static int get_power_mode(enum bmp5_powermode *powermode, const struct device *d
116116
uint8_t reg = 0;
117117
uint8_t raw_power_mode = 0;
118118

119-
ret = i2c_reg_read_byte_dt(&conf->i2c, BMP5_REG_ODR_CONFIG, &reg);
119+
ret = bmp581_reg_read_rtio(&conf->bus, BMP5_REG_ODR_CONFIG, &reg, 1);
120120
if (ret != BMP5_OK) {
121121
LOG_DBG("Failed to read odr config to get power mode!");
122122
return ret;
@@ -195,7 +195,7 @@ static int get_interrupt_status(uint8_t *int_status, const struct device *dev)
195195

196196
conf = (const struct bmp581_config *)dev->config;
197197

198-
return i2c_reg_read_byte_dt(&conf->i2c, BMP5_REG_INT_STATUS, int_status);
198+
return bmp581_reg_read_rtio(&conf->bus, BMP5_REG_INT_STATUS, int_status, 1);
199199
}
200200

201201
static int get_nvm_status(uint8_t *nvm_status, const struct device *dev)
@@ -208,7 +208,7 @@ static int get_nvm_status(uint8_t *nvm_status, const struct device *dev)
208208

209209
conf = (const struct bmp581_config *)dev->config;
210210

211-
return i2c_reg_read_byte_dt(&conf->i2c, BMP5_REG_STATUS, nvm_status);
211+
return bmp581_reg_read_rtio(&conf->bus, BMP5_REG_STATUS, nvm_status, 1);
212212
}
213213

214214
static int validate_chip_id(struct bmp581_data *drv)
@@ -251,7 +251,7 @@ static int get_osr_odr_press_config(struct bmp581_osr_odr_press_config *osr_odr_
251251
conf = (const struct bmp581_config *)dev->config;
252252

253253
/* Get OSR and ODR configuration in burst read */
254-
rslt = i2c_burst_read_dt(&conf->i2c, BMP5_REG_OSR_CONFIG, reg_data, 2);
254+
rslt = bmp581_reg_read_rtio(&conf->bus, BMP5_REG_OSR_CONFIG, reg_data, 2);
255255

256256
if (rslt == BMP5_OK) {
257257
osr_odr_press_cfg->osr_t = BMP5_GET_BITS_POS_0(reg_data[0], BMP5_TEMP_OSR);
@@ -275,7 +275,7 @@ static int set_osr_odr_press_config(const struct bmp581_osr_odr_press_config *os
275275
reg_data[1] = BMP5_SET_BITSLICE(reg_data[1], BMP5_POWERMODE, osr_odr_press_cfg->power_mode);
276276
reg_data[1] = BMP5_SET_BITSLICE(reg_data[1], BMP5_ODR, osr_odr_press_cfg->odr);
277277

278-
return i2c_burst_write_dt(&cfg->i2c, BMP5_REG_OSR_CONFIG, reg_data, sizeof(reg_data));
278+
return bmp581_reg_write_rtio(&cfg->bus, BMP5_REG_OSR_CONFIG, reg_data, sizeof(reg_data));
279279
}
280280

281281
static int set_iir_filters_config(const struct bmp581_osr_odr_press_config *osr_odr_press_cfg,
@@ -287,7 +287,7 @@ static int set_iir_filters_config(const struct bmp581_osr_odr_press_config *osr_
287287
reg_data = BMP5_SET_BITSLICE(reg_data, BMP5_SET_IIR_TEMP, osr_odr_press_cfg->iir_t);
288288
reg_data = BMP5_SET_BITSLICE(reg_data, BMP5_SET_IIR_PRESS, osr_odr_press_cfg->iir_p);
289289

290-
return i2c_burst_write_dt(&cfg->i2c, BMP5_REG_DSP_IIR, &reg_data, 1);
290+
return bmp581_reg_write_rtio(&cfg->bus, BMP5_REG_DSP_IIR, &reg_data, 1);
291291
}
292292

293293
static int set_osr_config(const struct sensor_value *osr, enum sensor_channel chan,
@@ -305,7 +305,7 @@ static int set_osr_config(const struct sensor_value *osr, enum sensor_channel ch
305305
uint8_t press_en = osr->val2 != 0; /* if it is not 0 then pressure is enabled */
306306
uint8_t osr_val = 0;
307307

308-
ret = i2c_reg_read_byte_dt(&conf->i2c, BMP5_REG_OSR_CONFIG, &osr_val);
308+
ret = bmp581_reg_read_rtio(&conf->bus, BMP5_REG_OSR_CONFIG, &osr_val, 1);
309309
if (ret == BMP5_OK) {
310310
switch (chan) {
311311
case SENSOR_CHAN_ALL:
@@ -326,7 +326,7 @@ static int set_osr_config(const struct sensor_value *osr, enum sensor_channel ch
326326
}
327327

328328
if (ret == BMP5_OK) {
329-
ret = i2c_reg_write_byte_dt(&conf->i2c, BMP5_REG_OSR_CONFIG, osr_val);
329+
ret = bmp581_reg_write_rtio(&conf->bus, BMP5_REG_OSR_CONFIG, &osr_val, 1);
330330
get_osr_odr_press_config(&drv->osr_odr_press_config, dev);
331331
}
332332
}
@@ -345,12 +345,12 @@ static int set_odr_config(const struct sensor_value *odr, const struct device *d
345345
int ret = 0;
346346
uint8_t odr_val = 0;
347347

348-
ret = i2c_reg_read_byte_dt(&conf->i2c, BMP5_REG_ODR_CONFIG, &odr_val);
348+
ret = bmp581_reg_read_rtio(&conf->bus, BMP5_REG_ODR_CONFIG, &odr_val, 1);
349349
if (ret != BMP5_OK) {
350350
return ret;
351351
}
352352
odr_val = BMP5_SET_BITSLICE(odr_val, BMP5_ODR, odr->val1);
353-
ret = i2c_reg_write_byte_dt(&conf->i2c, BMP5_REG_ODR_CONFIG, odr_val);
353+
ret = bmp581_reg_write_rtio(&conf->bus, BMP5_REG_ODR_CONFIG, &odr_val, 1);
354354
get_osr_odr_press_config(&drv->osr_odr_press_config, dev);
355355

356356
return ret;
@@ -367,7 +367,7 @@ static int soft_reset(const struct device *dev)
367367
return -EINVAL;
368368
}
369369

370-
ret = i2c_reg_write_byte_dt(&conf->i2c, BMP5_REG_CMD, reset_cmd);
370+
ret = bmp581_reg_write_rtio(&conf->bus, BMP5_REG_CMD, &reset_cmd, 1);
371371

372372
if (ret == BMP5_OK) {
373373
k_usleep(BMP5_DELAY_US_SOFT_RESET);
@@ -401,7 +401,7 @@ static int bmp581_sample_fetch(const struct device *dev, enum sensor_channel cha
401401
uint8_t data[6];
402402
int ret = 0;
403403

404-
ret = i2c_burst_read_dt(&conf->i2c, BMP5_REG_TEMP_DATA_XLSB, data, 6);
404+
ret = bmp581_reg_read_rtio(&conf->bus, BMP5_REG_TEMP_DATA_XLSB, data, 6);
405405
if (ret == BMP5_OK) {
406406
/* convert raw sensor data to sensor_value. Shift the decimal part by 1 decimal
407407
* place to compensate for the conversion in sensor_value_to_double()
@@ -473,7 +473,7 @@ static int set_iir_config(const struct sensor_value *iir, const struct device *d
473473
/* update IIR config */
474474
uint8_t dsp_config[2];
475475

476-
ret = i2c_burst_read_dt(&conf->i2c, BMP5_REG_DSP_CONFIG, dsp_config, 2);
476+
ret = bmp581_reg_read_rtio(&conf->bus, BMP5_REG_DSP_CONFIG, dsp_config, 2);
477477
if (ret != BMP5_OK) {
478478
LOG_DBG("Failed to read dsp config register.");
479479
return ret;
@@ -487,7 +487,7 @@ static int set_iir_config(const struct sensor_value *iir, const struct device *d
487487
dsp_config[1] = BMP5_SET_BITSLICE(dsp_config[1], BMP5_SET_IIR_PRESS, iir->val2);
488488

489489
/* Set IIR configuration */
490-
ret = i2c_burst_write_dt(&conf->i2c, BMP5_REG_DSP_CONFIG, dsp_config, 2);
490+
ret = bmp581_reg_write_rtio(&conf->bus, BMP5_REG_DSP_CONFIG, dsp_config, 2);
491491

492492
if (ret != BMP5_OK) {
493493
LOG_DBG("Failed to configure IIR filter.");
@@ -550,7 +550,7 @@ static int bmp581_init(const struct device *dev)
550550

551551
soft_reset(dev);
552552

553-
ret = i2c_reg_read_byte_dt(&conf->i2c, BMP5_REG_CHIP_ID, &drv->chip_id);
553+
ret = bmp581_reg_read_rtio(&conf->bus, BMP5_REG_CHIP_ID, &drv->chip_id, 1);
554554
if (ret != BMP5_OK) {
555555
return ret;
556556
}
@@ -592,12 +592,11 @@ static DEVICE_API(sensor, bmp581_driver_api) = {
592592
.attr_set = bmp581_attr_set,
593593
};
594594

595-
#define BMP581_CONFIG(i) \
596-
static const struct bmp581_config bmp581_config_##i = { \
597-
.i2c = I2C_DT_SPEC_INST_GET(i), \
598-
}
599-
600595
#define BMP581_INIT(i) \
596+
\
597+
RTIO_DEFINE(bmp581_rtio_ctx_##i, 8, 8); \
598+
I2C_DT_IODEV_DEFINE(bmp581_bus_##i, DT_DRV_INST(i)); \
599+
\
601600
static struct bmp581_data bmp581_data_##i = { \
602601
.osr_odr_press_config = { \
603602
.press_en = 1, \
@@ -609,7 +608,14 @@ static DEVICE_API(sensor, bmp581_driver_api) = {
609608
.power_mode = DT_INST_PROP(i, power_mode), \
610609
}, \
611610
}; \
612-
BMP581_CONFIG(i); \
611+
\
612+
static const struct bmp581_config bmp581_config_##i = { \
613+
.bus.rtio = { \
614+
.ctx = &bmp581_rtio_ctx_##i, \
615+
.iodev = &bmp581_bus_##i, \
616+
.type = BMP581_BUS_TYPE_I2C, \
617+
}, \
618+
}; \
613619
\
614620
SENSOR_DEVICE_DT_INST_DEFINE(i, bmp581_init, NULL, &bmp581_data_##i, &bmp581_config_##i, \
615621
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, \

drivers/sensor/bosch/bmp581/bmp581.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include <zephyr/types.h>
2121
#include <zephyr/drivers/sensor/bmp581_user.h>
2222

23+
#include "bmp581_bus.h"
24+
2325
#define DT_DRV_COMPAT bosch_bmp581
2426

2527
/* UTILITY MACROS */
@@ -319,7 +321,7 @@ struct bmp581_data {
319321
};
320322

321323
struct bmp581_config {
322-
struct i2c_dt_spec i2c;
324+
struct bmp581_bus bus;
323325
};
324326

325327
#endif /* ZEPHYR_DRIVERS_SENSOR_BMP581_BMP581_H_ */
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Copyright (c) 2025 Croxel, Inc.
3+
* Copyright (c) 2025 CogniPilot Foundation
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
#include "bmp581_bus.h"
9+
10+
int bmp581_prep_reg_read_rtio_async(const struct bmp581_bus *bus,
11+
uint8_t reg, uint8_t *buf, size_t size,
12+
struct rtio_sqe **out)
13+
{
14+
struct rtio *ctx = bus->rtio.ctx;
15+
struct rtio_iodev *iodev = bus->rtio.iodev;
16+
struct rtio_sqe *write_reg_sqe = rtio_sqe_acquire(ctx);
17+
struct rtio_sqe *read_buf_sqe = rtio_sqe_acquire(ctx);
18+
19+
if (!write_reg_sqe || !read_buf_sqe) {
20+
rtio_sqe_drop_all(ctx);
21+
return -ENOMEM;
22+
}
23+
24+
rtio_sqe_prep_tiny_write(write_reg_sqe, iodev, RTIO_PRIO_NORM, &reg, 1, NULL);
25+
write_reg_sqe->flags |= RTIO_SQE_TRANSACTION;
26+
rtio_sqe_prep_read(read_buf_sqe, iodev, RTIO_PRIO_NORM, buf, size, NULL);
27+
if (bus->rtio.type == BMP581_BUS_TYPE_I2C) {
28+
read_buf_sqe->iodev_flags |= RTIO_IODEV_I2C_STOP | RTIO_IODEV_I2C_RESTART;
29+
}
30+
31+
/** Send back last SQE so it can be concatenated later. */
32+
if (out) {
33+
*out = read_buf_sqe;
34+
}
35+
36+
return 2;
37+
}
38+
39+
int bmp581_prep_reg_write_rtio_async(const struct bmp581_bus *bus,
40+
uint8_t reg, const uint8_t *buf, size_t size,
41+
struct rtio_sqe **out)
42+
{
43+
struct rtio *ctx = bus->rtio.ctx;
44+
struct rtio_iodev *iodev = bus->rtio.iodev;
45+
struct rtio_sqe *write_reg_sqe = rtio_sqe_acquire(ctx);
46+
struct rtio_sqe *write_buf_sqe = rtio_sqe_acquire(ctx);
47+
48+
if (!write_reg_sqe || !write_buf_sqe) {
49+
rtio_sqe_drop_all(ctx);
50+
return -ENOMEM;
51+
}
52+
53+
/** More than 7 won't work with tiny-write */
54+
if (size > 7) {
55+
return -EINVAL;
56+
}
57+
58+
rtio_sqe_prep_tiny_write(write_reg_sqe, iodev, RTIO_PRIO_NORM, &reg, 1, NULL);
59+
write_reg_sqe->flags |= RTIO_SQE_TRANSACTION;
60+
rtio_sqe_prep_tiny_write(write_buf_sqe, iodev, RTIO_PRIO_NORM, buf, size, NULL);
61+
if (bus->rtio.type == BMP581_BUS_TYPE_I2C) {
62+
write_buf_sqe->iodev_flags |= RTIO_IODEV_I2C_STOP;
63+
}
64+
65+
/** Send back last SQE so it can be concatenated later. */
66+
if (out) {
67+
*out = write_buf_sqe;
68+
}
69+
70+
return 2;
71+
}
72+
73+
int bmp581_reg_read_rtio(const struct bmp581_bus *bus, uint8_t start, uint8_t *buf, int size)
74+
{
75+
struct rtio *ctx = bus->rtio.ctx;
76+
struct rtio_cqe *cqe;
77+
int ret;
78+
79+
ret = bmp581_prep_reg_read_rtio_async(bus, start, buf, size, NULL);
80+
if (ret < 0) {
81+
return ret;
82+
}
83+
84+
ret = rtio_submit(ctx, ret);
85+
if (ret) {
86+
return ret;
87+
}
88+
89+
do {
90+
cqe = rtio_cqe_consume(ctx);
91+
if (cqe != NULL) {
92+
ret = cqe->result;
93+
rtio_cqe_release(ctx, cqe);
94+
}
95+
} while (cqe != NULL);
96+
97+
return ret;
98+
}
99+
100+
int bmp581_reg_write_rtio(const struct bmp581_bus *bus, uint8_t reg, const uint8_t *buf, int size)
101+
{
102+
struct rtio *ctx = bus->rtio.ctx;
103+
struct rtio_cqe *cqe;
104+
int ret;
105+
106+
ret = bmp581_prep_reg_write_rtio_async(bus, reg, buf, size, NULL);
107+
if (ret < 0) {
108+
return ret;
109+
}
110+
111+
ret = rtio_submit(ctx, ret);
112+
if (ret) {
113+
return ret;
114+
}
115+
116+
do {
117+
cqe = rtio_cqe_consume(ctx);
118+
if (cqe != NULL) {
119+
ret = cqe->result;
120+
rtio_cqe_release(ctx, cqe);
121+
}
122+
} while (cqe != NULL);
123+
124+
return ret;
125+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2025 Croxel, Inc.
3+
* Copyright (c) 2025 CogniPilot Foundation
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
#ifndef ZEPHYR_DRIVERS_SENSOR_BMP581_BMP581_BUS_H_
9+
#define ZEPHYR_DRIVERS_SENSOR_BMP581_BMP581_BUS_H_
10+
11+
#include <zephyr/kernel.h>
12+
#include <zephyr/rtio/rtio.h>
13+
14+
enum bmp581_bus_type {
15+
BMP581_BUS_TYPE_I2C,
16+
};
17+
18+
struct bmp581_bus {
19+
struct {
20+
struct rtio *ctx;
21+
struct rtio_iodev *iodev;
22+
enum bmp581_bus_type type;
23+
} rtio;
24+
};
25+
26+
int bmp581_prep_reg_read_rtio_async(const struct bmp581_bus *bus,
27+
uint8_t reg, uint8_t *buf, size_t size,
28+
struct rtio_sqe **out);
29+
30+
int bmp581_prep_reg_write_rtio_async(const struct bmp581_bus *bus,
31+
uint8_t reg, const uint8_t *buf, size_t size,
32+
struct rtio_sqe **out);
33+
34+
int bmp581_reg_read_rtio(const struct bmp581_bus *bus, uint8_t start, uint8_t *buf, int size);
35+
36+
int bmp581_reg_write_rtio(const struct bmp581_bus *bus, uint8_t reg, const uint8_t *buf, int size);
37+
38+
#endif /* ZEPHYR_DRIVERS_SENSOR_BMP581_BMP581_BUS_H_ */

0 commit comments

Comments
 (0)