Skip to content

Commit 792b7e3

Browse files
avisconticfriedt
authored andcommitted
rtio: add helper function rtio_read_transaction()
Add a helper function that constructs a rtio SQE chain with the purpose to perform a bus read operation on a list of registers. Usage: struct rtio_regs regs; struct rtio_reg_list regs_list[] = {{regs_addr1, mem_addr_1, mem_len_1}, {regs_addr2, mem_addr_2, mem_len_2}, ... }; regs.rtio_regs_list = regs_list; regs.rtio_regs_num = ARRAY_SIZE(regs_list); rtio_read_regs_async(rtio, iodev, RTIO_BUS_SPI, &regs, sqe, dev, op_cb); Signed-off-by: Armando Visconti <[email protected]>
1 parent 37d8ec2 commit 792b7e3

File tree

5 files changed

+251
-62
lines changed

5 files changed

+251
-62
lines changed

doc/releases/release-notes-4.3.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ https://docs.zephyrproject.org/latest/security/vulnerabilities.html
4848
API Changes
4949
***********
5050

51+
* RTIO
52+
53+
* :c:func:`rtio_is_spi`
54+
* :c:func:`rtio_is_cspi`
55+
* :c:func:`rtio_is_i3c`
56+
* :c:func:`rtio_read_regs_async`
57+
5158
Removed APIs and options
5259
========================
5360

drivers/sensor/st/lsm6dsv16x/lsm6dsv16x.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,7 +1331,7 @@ static int lsm6dsv16x_pm_action(const struct device *dev, enum pm_device_action
13311331
CONFIG_SPI_RTIO), \
13321332
(.rtio_ctx = &prefix##_rtio_ctx_##inst, \
13331333
.iodev = &prefix##_iodev_##inst, \
1334-
.bus_type = BUS_SPI,)) \
1334+
.bus_type = RTIO_BUS_SPI,)) \
13351335
}; \
13361336
static const struct lsm6dsv16x_config prefix##_config_##inst = \
13371337
LSM6DSV16X_CONFIG_SPI(inst, prefix);
@@ -1362,7 +1362,7 @@ static int lsm6dsv16x_pm_action(const struct device *dev, enum pm_device_action
13621362
CONFIG_I2C_RTIO), \
13631363
(.rtio_ctx = &prefix##_rtio_ctx_##inst, \
13641364
.iodev = &prefix##_iodev_##inst, \
1365-
.bus_type = BUS_I2C,)) \
1365+
.bus_type = RTIO_BUS_I2C,)) \
13661366
}; \
13671367
static const struct lsm6dsv16x_config prefix##_config_##inst = \
13681368
LSM6DSV16X_CONFIG_I2C(inst, prefix);
@@ -1398,7 +1398,7 @@ static int lsm6dsv16x_pm_action(const struct device *dev, enum pm_device_action
13981398
CONFIG_I3C_RTIO), \
13991399
(.rtio_ctx = &prefix##_rtio_ctx_##inst, \
14001400
.iodev = &prefix##_i3c_iodev_##inst, \
1401-
.bus_type = BUS_I3C,)) \
1401+
.bus_type = RTIO_BUS_I3C,)) \
14021402
}; \
14031403
static const struct lsm6dsv16x_config prefix##_config_##inst = \
14041404
LSM6DSV16X_CONFIG_I3C(inst, prefix);

drivers/sensor/st/lsm6dsv16x/lsm6dsv16x.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <zephyr/sys/util.h>
1818
#include <stmemsc.h>
1919
#include "lsm6dsv16x_reg.h"
20+
#include <zephyr/rtio/regmap.h>
2021

2122
#define DT_DRV_COMPAT_LSM6DSV16X st_lsm6dsv16x
2223
#define DT_DRV_COMPAT_LSM6DSV32X st_lsm6dsv32x
@@ -171,9 +172,9 @@ struct lsm6dsv16x_data {
171172
uint16_t accel_batch_odr : 4;
172173
uint16_t gyro_batch_odr : 4;
173174
uint16_t temp_batch_odr : 2;
174-
uint16_t bus_type : 2; /* I2C is 0, SPI is 1, I3C is 2 */
175175
uint16_t sflp_batch_odr : 3;
176-
uint16_t reserved : 1;
176+
uint16_t reserved : 3;
177+
rtio_bus_type bus_type;
177178
int32_t gbias_x_udps;
178179
int32_t gbias_y_udps;
179180
int32_t gbias_z_udps;
@@ -206,13 +207,9 @@ struct lsm6dsv16x_data {
206207
};
207208

208209
#ifdef CONFIG_LSM6DSV16X_STREAM
209-
#define BUS_I2C 0
210-
#define BUS_SPI 1
211-
#define BUS_I3C 2
212-
213-
static inline uint8_t lsm6dsv16x_bus_reg(struct lsm6dsv16x_data *data, uint8_t x)
210+
static inline uint8_t lsm6dsv16x_bus_reg(rtio_bus_type bus, uint8_t addr)
214211
{
215-
return (data->bus_type == BUS_SPI) ? x | 0x80 : x;
212+
return (rtio_is_spi(bus)) ? addr | 0x80 : addr;
216213
}
217214

218215
#define LSM6DSV16X_FIFO_ITEM_LEN 7

drivers/sensor/st/lsm6dsv16x/lsm6dsv16x_rtio_stream.c

Lines changed: 67 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -16,48 +16,6 @@
1616
#include <zephyr/logging/log.h>
1717
LOG_MODULE_DECLARE(LSM6DSV16X_RTIO);
1818

19-
/*
20-
* Create a chain of SQEs representing a bus transaction to read a reg.
21-
* The RTIO-enabled bus driver will:
22-
*
23-
* - write "reg" address
24-
* - read "len" data bytes into "buf".
25-
* - call complete_op callback
26-
*
27-
* If drdy_xl is active it reads XL data (6 bytes) from LSM6DSV16X_OUTX_L_A reg.
28-
*/
29-
static void lsm6dsv16x_rtio_rw_transaction(const struct device *dev, uint8_t reg,
30-
uint8_t *buf, uint32_t len,
31-
rtio_callback_t complete_op_cb)
32-
{
33-
struct lsm6dsv16x_data *lsm6dsv16x = dev->data;
34-
struct rtio *rtio = lsm6dsv16x->rtio_ctx;
35-
struct rtio_iodev *iodev = lsm6dsv16x->iodev;
36-
struct rtio_sqe *write_addr = rtio_sqe_acquire(rtio);
37-
struct rtio_sqe *read_reg = rtio_sqe_acquire(rtio);
38-
struct rtio_sqe *complete_op = rtio_sqe_acquire(rtio);
39-
struct rtio_iodev_sqe *sqe = lsm6dsv16x->streaming_sqe;
40-
uint8_t reg_bus = lsm6dsv16x_bus_reg(lsm6dsv16x, reg);
41-
42-
/* check we have been able to acquire sqe */
43-
if (write_addr == NULL || read_reg == NULL || complete_op == NULL) {
44-
return;
45-
}
46-
47-
rtio_sqe_prep_tiny_write(write_addr, iodev, RTIO_PRIO_NORM, &reg_bus, 1, NULL);
48-
write_addr->flags = RTIO_SQE_TRANSACTION;
49-
rtio_sqe_prep_read(read_reg, iodev, RTIO_PRIO_NORM, buf, len, NULL);
50-
read_reg->flags = RTIO_SQE_CHAINED;
51-
if (lsm6dsv16x->bus_type == BUS_I2C) {
52-
read_reg->iodev_flags |= RTIO_IODEV_I2C_STOP | RTIO_IODEV_I2C_RESTART;
53-
} else if (lsm6dsv16x->bus_type == BUS_I3C) {
54-
read_reg->iodev_flags |= RTIO_IODEV_I3C_STOP | RTIO_IODEV_I3C_RESTART;
55-
}
56-
57-
rtio_sqe_prep_callback_no_cqe(complete_op, complete_op_cb, (void *)dev, sqe);
58-
rtio_submit(rtio, 0);
59-
}
60-
6119
static void lsm6dsv16x_config_drdy(const struct device *dev, struct trigger_config trig_cfg)
6220
{
6321
const struct lsm6dsv16x_config *config = dev->config;
@@ -500,6 +458,19 @@ static void lsm6dsv16x_read_fifo_cb(struct rtio *r, const struct rtio_sqe *sqe,
500458
read_buf = buf + sizeof(hdr);
501459
buf_avail = buf_len - sizeof(hdr);
502460

461+
uint8_t reg_addr = lsm6dsv16x_bus_reg(lsm6dsv16x->bus_type, LSM6DSV16X_FIFO_DATA_OUT_TAG);
462+
struct rtio_regs fifo_regs;
463+
struct rtio_regs_list regs_list[] = {
464+
{
465+
reg_addr,
466+
read_buf,
467+
buf_avail,
468+
},
469+
};
470+
471+
fifo_regs.rtio_regs_list = regs_list;
472+
fifo_regs.rtio_regs_num = ARRAY_SIZE(regs_list);
473+
503474
/*
504475
* Prepare rtio enabled bus to read all fifo_count entries from
505476
* LSM6DSV16X_FIFO_DATA_OUT_TAG. Then lsm6dsv16x_complete_op_cb
@@ -515,8 +486,8 @@ static void lsm6dsv16x_read_fifo_cb(struct rtio *r, const struct rtio_sqe *sqe,
515486
* lsm6dsv16x_fifo_out_raw_get(&dev_ctx, &f_data);
516487
* }
517488
*/
518-
lsm6dsv16x_rtio_rw_transaction(dev, LSM6DSV16X_FIFO_DATA_OUT_TAG,
519-
read_buf, buf_avail, lsm6dsv16x_complete_op_cb);
489+
rtio_read_regs_async(lsm6dsv16x->rtio_ctx, lsm6dsv16x->iodev, lsm6dsv16x->bus_type,
490+
&fifo_regs, lsm6dsv16x->streaming_sqe, dev, lsm6dsv16x_complete_op_cb);
520491
}
521492

522493
/*
@@ -646,6 +617,19 @@ static void lsm6dsv16x_read_status_cb(struct rtio *r, const struct rtio_sqe *sqe
646617
memcpy(buf, &hdr, sizeof(hdr));
647618
read_buf = (uint8_t *)&((struct lsm6dsv16x_rtio_data *)buf)->acc[0];
648619

620+
uint8_t reg_addr = lsm6dsv16x_bus_reg(lsm6dsv16x->bus_type, LSM6DSV16X_OUTX_L_A);
621+
struct rtio_regs fifo_regs;
622+
struct rtio_regs_list regs_list[] = {
623+
{
624+
reg_addr,
625+
read_buf,
626+
6,
627+
},
628+
};
629+
630+
fifo_regs.rtio_regs_list = regs_list;
631+
fifo_regs.rtio_regs_num = ARRAY_SIZE(regs_list);
632+
649633
/*
650634
* Prepare rtio enabled bus to read LSM6DSV16X_OUTX_L_A register
651635
* where accelerometer data is available.
@@ -657,8 +641,9 @@ static void lsm6dsv16x_read_status_cb(struct rtio *r, const struct rtio_sqe *sqe
657641
*
658642
* lsm6dsv16x_acceleration_raw_get(&dev_ctx, accel_raw);
659643
*/
660-
lsm6dsv16x_rtio_rw_transaction(dev, LSM6DSV16X_OUTX_L_A,
661-
read_buf, 6, lsm6dsv16x_complete_op_cb);
644+
rtio_read_regs_async(lsm6dsv16x->rtio_ctx, lsm6dsv16x->iodev, lsm6dsv16x->bus_type,
645+
&fifo_regs, lsm6dsv16x->streaming_sqe, dev,
646+
lsm6dsv16x_complete_op_cb);
662647
}
663648
}
664649

@@ -708,12 +693,26 @@ void lsm6dsv16x_stream_irq_handler(const struct device *dev)
708693
struct rtio_sqe *check_fifo_status_reg = rtio_sqe_acquire(rtio);
709694

710695
rtio_sqe_prep_callback_no_cqe(check_fifo_status_reg,
711-
lsm6dsv16x_read_fifo_cb, (void *)dev, NULL);
696+
lsm6dsv16x_read_fifo_cb, (void *)dev, NULL);
712697
rtio_submit(rtio, 0);
713698
} else {
714699
#endif
715700
lsm6dsv16x->fifo_status[0] = lsm6dsv16x->fifo_status[1] = 0;
716701

702+
uint8_t reg_addr =
703+
lsm6dsv16x_bus_reg(lsm6dsv16x->bus_type, LSM6DSV16X_FIFO_STATUS1);
704+
struct rtio_regs fifo_regs;
705+
struct rtio_regs_list regs_list[] = {
706+
{
707+
reg_addr,
708+
lsm6dsv16x->fifo_status,
709+
2,
710+
},
711+
};
712+
713+
fifo_regs.rtio_regs_list = regs_list;
714+
fifo_regs.rtio_regs_num = ARRAY_SIZE(regs_list);
715+
717716
/*
718717
* Prepare rtio enabled bus to read LSM6DSV16X_FIFO_STATUS1 and
719718
* LSM6DSV16X_FIFO_STATUS2 registers where FIFO threshold condition and
@@ -726,8 +725,11 @@ void lsm6dsv16x_stream_irq_handler(const struct device *dev)
726725
*
727726
* lsm6dsv16x_fifo_status_get(&dev_ctx, &fifo_status);
728727
*/
729-
lsm6dsv16x_rtio_rw_transaction(dev, LSM6DSV16X_FIFO_STATUS1,
730-
lsm6dsv16x->fifo_status, 2, lsm6dsv16x_read_fifo_cb);
728+
rtio_read_regs_async(lsm6dsv16x->rtio_ctx, lsm6dsv16x->iodev,
729+
lsm6dsv16x->bus_type, &fifo_regs,
730+
lsm6dsv16x->streaming_sqe, dev,
731+
lsm6dsv16x_read_fifo_cb);
732+
731733
#if LSM6DSVXXX_ANY_INST_ON_BUS_STATUS_OKAY(i3c)
732734
}
733735
#endif
@@ -737,6 +739,19 @@ void lsm6dsv16x_stream_irq_handler(const struct device *dev)
737739
if (lsm6dsv16x->trig_cfg.int_drdy) {
738740
lsm6dsv16x->status = 0;
739741

742+
uint8_t reg_addr = lsm6dsv16x_bus_reg(lsm6dsv16x->bus_type, LSM6DSV16X_STATUS_REG);
743+
struct rtio_regs fifo_regs;
744+
struct rtio_regs_list regs_list[] = {
745+
{
746+
reg_addr,
747+
&lsm6dsv16x->status,
748+
1,
749+
},
750+
};
751+
752+
fifo_regs.rtio_regs_list = regs_list;
753+
fifo_regs.rtio_regs_num = ARRAY_SIZE(regs_list);
754+
740755
/*
741756
* Prepare rtio enabled bus to read LSM6DSV16X_STATUS_REG register
742757
* where accelerometer and gyroscope data ready status is available.
@@ -748,7 +763,8 @@ void lsm6dsv16x_stream_irq_handler(const struct device *dev)
748763
*
749764
* lsm6dsv16x_flag_data_ready_get(&dev_ctx, &drdy);
750765
*/
751-
lsm6dsv16x_rtio_rw_transaction(dev, LSM6DSV16X_STATUS_REG,
752-
&lsm6dsv16x->status, 1, lsm6dsv16x_read_status_cb);
766+
rtio_read_regs_async(lsm6dsv16x->rtio_ctx, lsm6dsv16x->iodev, lsm6dsv16x->bus_type,
767+
&fifo_regs, lsm6dsv16x->streaming_sqe, dev,
768+
lsm6dsv16x_read_status_cb);
753769
}
754770
}

0 commit comments

Comments
 (0)