Skip to content

Commit

Permalink
drivers: i2c: rtio
Browse files Browse the repository at this point in the history
extend i2c library to create rtio sqes for reading and writing bytes

Signed-off-by: Florian Weber <[email protected]>
  • Loading branch information
FlorianWeber1018 committed Dec 14, 2024
1 parent e8a5e97 commit 1b352ef
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 10 deletions.
41 changes: 41 additions & 0 deletions drivers/i2c/i2c_rtio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
49 changes: 39 additions & 10 deletions include/zephyr/drivers/i2c.h
Original file line number Diff line number Diff line change
Expand Up @@ -1092,22 +1092,51 @@ struct rtio_sqe *i2c_rtio_copy(struct rtio *r,
const struct i2c_msg *msgs,
uint8_t num_msgs);

#endif /* CONFIG_I2C_RTIO */

/**
* @brief Perform data transfer to another I2C device in controller mode.
* @brief Copy the register address and data to a SQE
*
* This is equivalent to:
* @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
*
* i2c_transfer(spec->bus, msgs, num_msgs, spec->addr);
* @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 spec I2C specification from devicetree.
* @param msgs Array of messages to transfer.
* @param num_msgs Number of messages to transfer.
* @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.
*
* @return a value from i2c_transfer()
* @retval sqe Last submission in the queue added
* @retval NULL Not enough memory in the context to copy the requests
*/
static inline int i2c_transfer_dt(const struct i2c_dt_spec *spec,
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 */

/**
* @brief Perform data transfer to another I2C device in controller mode.
*
* This is equivalent to:
*
* i2c_transfer(spec->bus, msgs, num_msgs, spec->addr);
*
* @param spec I2C specification from devicetree.
* @param msgs Array of messages to transfer.
* @param num_msgs Number of messages to transfer.
*
* @return a value from i2c_transfer()
*/
static inline int i2c_transfer_dt(const struct i2c_dt_spec *spec,
struct i2c_msg *msgs, uint8_t num_msgs)
{
return i2c_transfer(spec->bus, msgs, num_msgs, spec->addr);
Expand Down

0 comments on commit 1b352ef

Please sign in to comment.