From 4e9bd00dfbbd5b4d11ebc020b8722789d3bf138f Mon Sep 17 00:00:00 2001 From: Florian Weber Date: Fri, 10 Jan 2025 10:25:00 +0100 Subject: [PATCH] drivers: i2c: rtio extend i2c library to create rtio sqes for reading and writing bytes Signed-off-by: Florian Weber --- drivers/i2c/i2c_rtio.c | 41 ++++++++++++++++++++++++++++++++++++ include/zephyr/drivers/i2c.h | 29 +++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/drivers/i2c/i2c_rtio.c b/drivers/i2c/i2c_rtio.c index d921f5604ef..a44e2e61fee 100644 --- a/drivers/i2c/i2c_rtio.c +++ b/drivers/i2c/i2c_rtio.c @@ -52,6 +52,47 @@ struct rtio_sqe *i2c_rtio_copy(struct rtio *r, struct rtio_iodev *iodev, const s return sqe; } +struct rtio_sqe *i2c_rtio_copy_reg_write_byte(struct rtio *r, struct rtio_iodev *iodev, + uint8_t reg_addr, uint8_t data) +{ + uint8_t msg[2]; + + struct rtio_sqe *sqe = rtio_sqe_acquire(r); + + if (sqe == NULL) { + rtio_sqe_drop_all(r); + return NULL; + } + msg[0] = reg_addr; + msg[1] = data; + rtio_sqe_prep_tiny_write(sqe, iodev, RTIO_PRIO_NORM, msg, sizeof(msg), NULL); + sqe->iodev_flags = RTIO_IODEV_I2C_STOP; + return sqe; +} + +struct rtio_sqe *i2c_rtio_copy_reg_burst_read(struct rtio *r, struct rtio_iodev *iodev, + uint8_t start_addr, void *buf, size_t num_bytes) +{ + struct rtio_sqe *sqe = rtio_sqe_acquire(r); + + if (sqe == NULL) { + rtio_sqe_drop_all(r); + return NULL; + } + rtio_sqe_prep_tiny_write(sqe, iodev, RTIO_PRIO_NORM, &start_addr, 1, NULL); + sqe->flags |= RTIO_SQE_TRANSACTION; + + sqe = rtio_sqe_acquire(r); + if (sqe == NULL) { + rtio_sqe_drop_all(r); + return NULL; + } + rtio_sqe_prep_read(sqe, iodev, RTIO_PRIO_NORM, buf, num_bytes, NULL); + sqe->iodev_flags |= RTIO_IODEV_I2C_STOP | RTIO_IODEV_I2C_RESTART; + + return sqe; +} + void i2c_rtio_init(struct i2c_rtio *ctx, const struct device *dev) { k_sem_init(&ctx->lock, 1, 1); diff --git a/include/zephyr/drivers/i2c.h b/include/zephyr/drivers/i2c.h index 5f1c3fbe8ae..d8eaee81873 100644 --- a/include/zephyr/drivers/i2c.h +++ b/include/zephyr/drivers/i2c.h @@ -1092,6 +1092,35 @@ struct rtio_sqe *i2c_rtio_copy(struct rtio *r, const struct i2c_msg *msgs, uint8_t num_msgs); +/** + * @brief Copy the register address and data to a SQE + * + * @param r RTIO context + * @param iodev RTIO IODev to target for the submissions + * @param reg_addr target register address + * @param data data to be written + * + * @retval sqe Last submission in the queue added + * @retval NULL Not enough memory in the context to copy the requests + */ +struct rtio_sqe *i2c_rtio_copy_reg_write_byte(struct rtio *r, struct rtio_iodev *iodev, + uint8_t reg_addr, uint8_t data); + +/** + * @brief acquire and configure a i2c burst read transmission + * + * @param r RTIO context + * @param iodev RTIO IODev to target for the submissions + * @param start_addr target register address + * @param buf Memory pool that stores the retrieved data. + * @param num_bytes Number of bytes to read. + * + * @retval sqe Last submission in the queue added + * @retval NULL Not enough memory in the context to copy the requests + */ +struct rtio_sqe *i2c_rtio_copy_reg_burst_read(struct rtio *r, struct rtio_iodev *iodev, + uint8_t start_addr, void *buf, size_t num_bytes); + #endif /* CONFIG_I2C_RTIO */ /**