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 "adxl367.h"
12
13 LOG_MODULE_DECLARE(ADXL367, CONFIG_SENSOR_LOG_LEVEL);
14
adxl367_submit_fetch(struct rtio_iodev_sqe * iodev_sqe)15 static void adxl367_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 struct adxl367_data *data = dev->data;
21 int rc;
22 uint32_t min_buffer_len = sizeof(struct adxl367_sample_data);
23 uint8_t *buffer;
24 uint32_t buffer_len;
25
26 rc = rtio_sqe_rx_buf(iodev_sqe, min_buffer_len, min_buffer_len, &buffer, &buffer_len);
27 if (rc != 0) {
28 LOG_ERR("Failed to get a read buffer of size %u bytes", min_buffer_len);
29 rtio_iodev_sqe_err(iodev_sqe, rc);
30 return;
31 }
32
33 struct adxl367_sample_data *enc_data = (struct adxl367_sample_data *)buffer;
34
35 #ifdef CONFIG_ADXL367_STREAM
36 enc_data->is_fifo = 0;
37 #endif /*CONFIG_ADXL367_STREAM*/
38
39 rc = adxl367_get_accel_data(dev, &enc_data->xyz);
40 if (rc != 0) {
41 LOG_ERR("Failed to fetch xyz samples");
42 rtio_iodev_sqe_err(iodev_sqe, rc);
43 return;
44 }
45
46 enc_data->xyz.range = data->range;
47
48 rc = adxl367_get_temp_data(dev, &enc_data->raw_temp);
49 if (rc != 0) {
50 LOG_ERR("Failed to fetch temp samples");
51 rtio_iodev_sqe_err(iodev_sqe, rc);
52 return;
53 }
54
55 rtio_iodev_sqe_ok(iodev_sqe, 0);
56 }
57
adxl367_submit(const struct device * dev,struct rtio_iodev_sqe * iodev_sqe)58 void adxl367_submit(const struct device *dev, struct rtio_iodev_sqe *iodev_sqe)
59 {
60 const struct sensor_read_config *cfg =
61 (const struct sensor_read_config *) iodev_sqe->sqe.iodev->data;
62
63 if (!cfg->is_streaming) {
64 struct rtio_work_req *req = rtio_work_req_alloc();
65
66 __ASSERT_NO_MSG(req);
67
68 rtio_work_req_submit(req, iodev_sqe, adxl367_submit_fetch);
69 } else if (IS_ENABLED(CONFIG_ADXL367_STREAM)) {
70 adxl367_submit_stream(dev, iodev_sqe);
71 } else {
72 rtio_iodev_sqe_err(iodev_sqe, -ENOTSUP);
73 }
74 }
75