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 "adxl372.h"
12
13 LOG_MODULE_DECLARE(ADXL372, CONFIG_SENSOR_LOG_LEVEL);
14
adxl372_submit_fetch(struct rtio_iodev_sqe * iodev_sqe)15 static void adxl372_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 adxl372_xyz_accel_data);
22 uint8_t *buffer;
23 uint32_t buffer_len;
24
25 const struct adxl372_dev_config *cfg_372 = (const struct adxl372_dev_config *)dev->config;
26
27 rc = rtio_sqe_rx_buf(iodev_sqe, min_buffer_len, min_buffer_len, &buffer, &buffer_len);
28 if (rc != 0) {
29 LOG_ERR("Failed to get a read buffer of size %u bytes", min_buffer_len);
30 rtio_iodev_sqe_err(iodev_sqe, rc);
31 return;
32 }
33
34 struct adxl372_xyz_accel_data *data = (struct adxl372_xyz_accel_data *)buffer;
35
36 rc = adxl372_get_accel_data(dev, cfg_372->max_peak_detect_mode, data);
37 if (rc != 0) {
38 LOG_ERR("Failed to fetch samples");
39 rtio_iodev_sqe_err(iodev_sqe, rc);
40 return;
41 }
42
43 rtio_iodev_sqe_ok(iodev_sqe, 0);
44 }
45
adxl372_submit(const struct device * dev,struct rtio_iodev_sqe * iodev_sqe)46 void adxl372_submit(const struct device *dev, struct rtio_iodev_sqe *iodev_sqe)
47 {
48 const struct sensor_read_config *cfg =
49 (const struct sensor_read_config *)iodev_sqe->sqe.iodev->data;
50
51 if (!cfg->is_streaming) {
52 struct rtio_work_req *req = rtio_work_req_alloc();
53
54 __ASSERT_NO_MSG(req);
55
56 rtio_work_req_submit(req, iodev_sqe, adxl372_submit_fetch);
57 } else if (IS_ENABLED(CONFIG_ADXL372_STREAM)) {
58 adxl372_submit_stream(dev, iodev_sqe);
59 } else {
60 rtio_iodev_sqe_err(iodev_sqe, -ENOTSUP);
61 }
62 }
63