Skip to content

Commit f9d1607

Browse files
teburdcarlescufi
authored andcommitted
rtio: Add macro to define iodev submission queue
The iodev submission queue existed but wasn't easily constructed as there was no macro to define it. Adds a simple macro wrapper around RTIO_SPSC_DEFINE for creating the RTIO IO Device Submission Queue enabling each IO device to have its own pending queue of requests. The sample has been updated to use the iodev submission queue rather than a k_msgq. Signed-off-by: Tom Burdick <[email protected]>
1 parent 9e25668 commit f9d1607

File tree

2 files changed

+41
-45
lines changed

2 files changed

+41
-45
lines changed

include/zephyr/rtio/rtio.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,16 @@ static inline void rtio_sqe_prep_write(struct rtio_sqe *sqe,
391391
#define RTIO_CQ_DEFINE(name, len) \
392392
static RTIO_SPSC_DEFINE(name, struct rtio_cqe, len)
393393

394+
395+
/**
396+
* @brief Statically define and initialize a fixed length iodev submission queue
397+
*
398+
* @param name Name of the queue.
399+
* @param len Queue length, power of 2 required
400+
*/
401+
#define RTIO_IODEV_SQ_DEFINE(name, len) \
402+
static RTIO_SPSC_DEFINE(name, struct rtio_iodev_sqe, len)
403+
394404
/**
395405
* @brief Statically define and initialize an RTIO context
396406
*

samples/subsys/rtio/sensor_batch_processing/src/vnd_sensor.c

Lines changed: 31 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,9 @@
1313

1414
LOG_MODULE_REGISTER(vnd_sensor);
1515

16-
struct vnd_sensor_msg {
17-
struct rtio_sqe sqe;
18-
struct rtio *r;
19-
};
20-
2116
struct vnd_sensor_config {
2217
uint32_t sample_period;
2318
size_t sample_size;
24-
struct vnd_sensor_msg *msgs;
25-
size_t max_msgs;
2619
};
2720

2821
struct vnd_sensor_data {
@@ -82,13 +75,13 @@ static void vnd_sensor_iodev_submit(const struct rtio_sqe *sqe, struct rtio *r)
8275
{
8376
struct vnd_sensor_data *data = (struct vnd_sensor_data *) sqe->iodev;
8477
const struct device *dev = data->dev;
78+
struct rtio_iodev_sqe *iodev_sqe = rtio_spsc_acquire(data->iodev.iodev_sq);
8579

86-
struct vnd_sensor_msg msg = {
87-
.sqe = *sqe,
88-
.r = r,
89-
};
90-
91-
if (k_msgq_put(&data->msgq, &msg, K_NO_WAIT) != 0) {
80+
if (iodev_sqe != NULL) {
81+
iodev_sqe->sqe = sqe;
82+
iodev_sqe->r = r;
83+
rtio_spsc_produce(data->iodev.iodev_sq);
84+
} else {
9285
LOG_ERR("%s: Could not put a msg", dev->name);
9386
rtio_sqe_err(r, sqe, -EWOULDBLOCK);
9487
}
@@ -97,12 +90,13 @@ static void vnd_sensor_iodev_submit(const struct rtio_sqe *sqe, struct rtio *r)
9790
static void vnd_sensor_handle_int(const struct device *dev)
9891
{
9992
struct vnd_sensor_data *data = dev->data;
100-
struct vnd_sensor_msg msg;
93+
struct rtio_iodev_sqe *iodev_sqe = rtio_spsc_consume(data->iodev.iodev_sq);
10194

102-
if (k_msgq_get(&data->msgq, &msg, K_NO_WAIT) != 0) {
103-
LOG_ERR("%s: Could not get a msg", dev->name);
95+
if (iodev_sqe != NULL) {
96+
vnd_sensor_iodev_execute(dev, iodev_sqe->sqe, iodev_sqe->r);
97+
rtio_spsc_release(data->iodev.iodev_sq);
10498
} else {
105-
vnd_sensor_iodev_execute(dev, &msg.sqe, msg.r);
99+
LOG_ERR("%s: Could not get a msg", dev->name);
106100
}
107101
}
108102

@@ -122,9 +116,6 @@ static int vnd_sensor_init(const struct device *dev)
122116

123117
data->dev = dev;
124118

125-
k_msgq_init(&data->msgq, (char *) config->msgs,
126-
sizeof(struct vnd_sensor_msg), config->max_msgs);
127-
128119
k_timer_init(&data->timer, vnd_sensor_timer_expiry, NULL);
129120

130121
k_timer_start(&data->timer, K_MSEC(sample_period),
@@ -137,30 +128,25 @@ static const struct rtio_iodev_api vnd_sensor_iodev_api = {
137128
.submit = vnd_sensor_iodev_submit,
138129
};
139130

140-
#define VND_SENSOR_INIT(n) \
141-
static struct vnd_sensor_msg \
142-
vnd_sensor_msgs_##n[DT_INST_PROP(n, max_msgs)]; \
143-
\
144-
static const struct vnd_sensor_config vnd_sensor_config_##n = { \
145-
.sample_period = DT_INST_PROP(n, sample_period), \
146-
.sample_size = DT_INST_PROP(n, sample_size), \
147-
.msgs = vnd_sensor_msgs_##n, \
148-
.max_msgs = DT_INST_PROP(n, max_msgs), \
149-
}; \
150-
\
151-
static struct vnd_sensor_data vnd_sensor_data_##n = { \
152-
.iodev = { \
153-
.api = &vnd_sensor_iodev_api, \
154-
}, \
155-
}; \
156-
\
157-
DEVICE_DT_INST_DEFINE(n, \
158-
vnd_sensor_init, \
159-
NULL, \
160-
&vnd_sensor_data_##n, \
161-
&vnd_sensor_config_##n, \
162-
POST_KERNEL, \
163-
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \
164-
NULL);
131+
#define VND_SENSOR_INIT(n) \
132+
\
133+
static const struct vnd_sensor_config vnd_sensor_config_##n = { \
134+
.sample_period = DT_INST_PROP(n, sample_period), \
135+
.sample_size = DT_INST_PROP(n, sample_size), \
136+
}; \
137+
\
138+
RTIO_IODEV_SQ_DEFINE(vnd_sensor_iodev_sq_##n, DT_INST_PROP(n, max_msgs)); \
139+
\
140+
static struct vnd_sensor_data vnd_sensor_data_##n = { \
141+
.iodev = \
142+
{ \
143+
.api = &vnd_sensor_iodev_api, \
144+
.iodev_sq = (struct rtio_iodev_sq *)&vnd_sensor_iodev_sq_##n, \
145+
}, \
146+
}; \
147+
\
148+
DEVICE_DT_INST_DEFINE(n, vnd_sensor_init, NULL, &vnd_sensor_data_##n, \
149+
&vnd_sensor_config_##n, POST_KERNEL, \
150+
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, NULL);
165151

166152
DT_INST_FOREACH_STATUS_OKAY(VND_SENSOR_INIT)

0 commit comments

Comments
 (0)