Skip to content

Commit

Permalink
sensors: Add a blocking sensor_read call
Browse files Browse the repository at this point in the history
Rename the current sensor_read to sensor_read_async_pool and create a
new blocking sensor_read helper.

Signed-off-by: Tom Burdick <[email protected]>
  • Loading branch information
teburd authored and laurenmurphyx64 committed May 28, 2024
1 parent 3570408 commit 147a01b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
2 changes: 1 addition & 1 deletion drivers/sensor/sensor_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ static int cmd_get_sensor(const struct shell *sh, size_t argc, char *argv[])

ctx.dev = dev;
ctx.sh = sh;
err = sensor_read(&iodev_sensor_shell_read, &sensor_read_rtio, &ctx);
err = sensor_read_async_mempool(&iodev_sensor_shell_read, &sensor_read_rtio, &ctx);
if (err < 0) {
shell_error(sh, "Failed to read sensor: %d", err);
}
Expand Down
52 changes: 48 additions & 4 deletions include/zephyr/drivers/sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ struct sensor_read_config {
* { SENSOR_CHAN_GYRO_XYZ, 0 });
*
* int main(void) {
* sensor_read(&icm42688_accelgyro, &rtio);
* sensor_read_async_mempool(&icm42688_accelgyro, &rtio);
* }
* @endcode
*/
Expand Down Expand Up @@ -1027,7 +1027,50 @@ static inline int sensor_stream(struct rtio_iodev *iodev, struct rtio *ctx, void
}

/**
* @brief Read data from a sensor.
* @brief Blocking one shot read of samples from a sensor into a buffer
*
* Using @p cfg, read data from the device by using the provided RTIO context
* @p ctx. This call will generate a @ref rtio_sqe that will be given the provided buffer. The call
* will wait for the read to complete before returning to the caller.
*
* @param[in] iodev The iodev created by @ref SENSOR_DT_READ_IODEV
* @param[in] ctx The RTIO context to service the read
* @param[in] buf Pointer to memory to read sample data into
* @param[in] buf_len Size in bytes of the given memory that are valid to read into
* @return 0 on success
* @return < 0 on error
*/
static inline int sensor_read(struct rtio_iodev *iodev, struct rtio *ctx, uint8_t *buf,
size_t buf_len)
{
if (IS_ENABLED(CONFIG_USERSPACE)) {
struct rtio_sqe sqe;

rtio_sqe_prep_read(&sqe, iodev, RTIO_PRIO_NORM, buf, buf_len, buf);
rtio_sqe_copy_in(ctx, &sqe, 1);
} else {
struct rtio_sqe *sqe = rtio_sqe_acquire(ctx);

if (sqe == NULL) {
return -ENOMEM;
}
rtio_sqe_prep_read(sqe, iodev, RTIO_PRIO_NORM, buf, buf_len, buf);
}
rtio_submit(ctx, 1);

struct rtio_cqe *cqe = rtio_cqe_consume(ctx);
int res = cqe->result;

__ASSERT(cqe->userdata != buf,
"consumed non-matching completion for sensor read into buffer %p\n", buf);

rtio_cqe_release(ctx, cqe);

return res;
}

/**
* @brief One shot non-blocking read with pool allocated buffer
*
* Using @p cfg, read one snapshot of data from the device by using the provided RTIO context
* @p ctx. This call will generate a @ref rtio_sqe that will leverage the RTIO's internal
Expand All @@ -1039,7 +1082,8 @@ static inline int sensor_stream(struct rtio_iodev *iodev, struct rtio *ctx, void
* @return 0 on success
* @return < 0 on error
*/
static inline int sensor_read(struct rtio_iodev *iodev, struct rtio *ctx, void *userdata)
static inline int sensor_read_async_mempool(struct rtio_iodev *iodev, struct rtio *ctx,
void *userdata)
{
if (IS_ENABLED(CONFIG_USERSPACE)) {
struct rtio_sqe sqe;
Expand Down Expand Up @@ -1067,7 +1111,7 @@ static inline int sensor_read(struct rtio_iodev *iodev, struct rtio *ctx, void *
* @param[in] result The result code of the read (0 being success)
* @param[in] buf The data buffer holding the sensor data
* @param[in] buf_len The length (in bytes) of the @p buf
* @param[in] userdata The optional userdata passed to sensor_read()
* @param[in] userdata The optional userdata passed to sensor_read_async_mempool()
*/
typedef void (*sensor_processing_callback_t)(int result, uint8_t *buf, uint32_t buf_len,
void *userdata);
Expand Down
2 changes: 1 addition & 1 deletion subsys/sensing/sensor_mgmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ static void sensing_sensor_polling_timer(struct k_timer *timer_id)
struct sensing_sensor, timer);

/* TODO: move it into sensing_runtime_thread */
sensor_read(sensor->iodev, &sensing_rtio_ctx, sensor);
sensor_read_async_mempool(sensor->iodev, &sensing_rtio_ctx, sensor);
}

static int init_sensor(struct sensing_sensor *sensor)
Expand Down
2 changes: 1 addition & 1 deletion tests/drivers/build_all/sensor/src/generic_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ static void run_generic_test(const struct device *dev)
}

/* Perform the actual sensor read */
rv = sensor_read(&iodev_read, &sensor_read_rtio_ctx, NULL);
rv = sensor_read_async_mempool(&iodev_read, &sensor_read_rtio_ctx, NULL);
zassert_ok(rv, "Could not read sensor (error %d, iteration %d/%d)", rv,
iteration + 1, CONFIG_GENERIC_SENSOR_TEST_NUM_EXPECTED_VALS);

Expand Down

0 comments on commit 147a01b

Please sign in to comment.