1 /*
2 * Copyright (c) 2024 Analog Devices Inc.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/rtio/work.h>
8 #include <zephyr/logging/log.h>
9 #include <zephyr/drivers/sensor.h>
10
11 #include "adxl362.h"
12
13 LOG_MODULE_DECLARE(ADXL362, CONFIG_SENSOR_LOG_LEVEL);
14
adxl362_submit_fetch(struct rtio_iodev_sqe * iodev_sqe)15 static void adxl362_submit_fetch(struct rtio_iodev_sqe *iodev_sqe)
16 {
17 const struct sensor_read_config *cfg =
18 (const struct sensor_read_config *) iodev_sqe->sqe.iodev->data;
19 const struct device *dev = cfg->sensor;
20 int rc;
21 uint32_t min_buffer_len = sizeof(struct adxl362_sample_data);
22 uint8_t *buffer;
23 uint32_t buffer_len;
24
25 rc = rtio_sqe_rx_buf(iodev_sqe, min_buffer_len, min_buffer_len, &buffer, &buffer_len);
26 if (rc != 0) {
27 LOG_ERR("Failed to get a read buffer of size %u bytes", min_buffer_len);
28 rtio_iodev_sqe_err(iodev_sqe, rc);
29 return;
30 }
31
32 struct adxl362_sample_data *data = (struct adxl362_sample_data *)buffer;
33
34 rc = adxl362_rtio_fetch(dev, data);
35 if (rc != 0) {
36 LOG_ERR("Failed to fetch samples");
37 rtio_iodev_sqe_err(iodev_sqe, rc);
38 return;
39 }
40
41 rtio_iodev_sqe_ok(iodev_sqe, 0);
42 }
43
adxl362_submit(const struct device * dev,struct rtio_iodev_sqe * iodev_sqe)44 void adxl362_submit(const struct device *dev, struct rtio_iodev_sqe *iodev_sqe)
45 {
46 const struct sensor_read_config *cfg =
47 (const struct sensor_read_config *) iodev_sqe->sqe.iodev->data;
48
49 if (!cfg->is_streaming) {
50 struct rtio_work_req *req = rtio_work_req_alloc();
51
52 __ASSERT_NO_MSG(req);
53
54 rtio_work_req_submit(req, iodev_sqe, adxl362_submit_fetch);
55 } else if (IS_ENABLED(CONFIG_ADXL362_STREAM)) {
56 adxl362_submit_stream(dev, iodev_sqe);
57 } else {
58 rtio_iodev_sqe_err(iodev_sqe, -ENOTSUP);
59 }
60 }
61