1 /*
2 * Copyright (c) 2023 Google LLC
3 * Copyright (c) 2024 Croxel Inc.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include <zephyr/drivers/sensor.h>
9 #include <zephyr/rtio/work.h>
10 #include "icm42688.h"
11 #include "icm42688_decoder.h"
12 #include "icm42688_reg.h"
13 #include "icm42688_rtio.h"
14 #include "icm42688_spi.h"
15
16 #include <zephyr/logging/log.h>
17 LOG_MODULE_REGISTER(ICM42688_RTIO, CONFIG_SENSOR_LOG_LEVEL);
18
icm42688_rtio_sample_fetch(const struct device * dev,int16_t readings[7])19 static int icm42688_rtio_sample_fetch(const struct device *dev, int16_t readings[7])
20 {
21 uint8_t status;
22 const struct icm42688_dev_cfg *cfg = dev->config;
23 uint8_t *buffer = (uint8_t *)readings;
24
25 int res = icm42688_spi_read(&cfg->spi, REG_INT_STATUS, &status, 1);
26
27 if (res) {
28 return res;
29 }
30
31 if (!FIELD_GET(BIT_INT_STATUS_DATA_RDY, status)) {
32 return -EBUSY;
33 }
34
35 res = icm42688_read_all(dev, buffer);
36
37 if (res) {
38 return res;
39 }
40
41 for (int i = 0; i < 7; i++) {
42 readings[i] = sys_le16_to_cpu((buffer[i * 2] << 8) | buffer[i * 2 + 1]);
43 }
44
45 return 0;
46 }
47
icm42688_submit_one_shot(const struct device * dev,struct rtio_iodev_sqe * iodev_sqe)48 static void icm42688_submit_one_shot(const struct device *dev, struct rtio_iodev_sqe *iodev_sqe)
49 {
50 const struct sensor_read_config *cfg = iodev_sqe->sqe.iodev->data;
51 const struct sensor_chan_spec *const channels = cfg->channels;
52 const size_t num_channels = cfg->count;
53 uint32_t min_buf_len = sizeof(struct icm42688_encoded_data);
54 int rc;
55 uint8_t *buf;
56 uint32_t buf_len;
57 struct icm42688_encoded_data *edata;
58
59 /* Get the buffer for the frame, it may be allocated dynamically by the rtio context */
60 rc = rtio_sqe_rx_buf(iodev_sqe, min_buf_len, min_buf_len, &buf, &buf_len);
61 if (rc != 0) {
62 LOG_ERR("Failed to get a read buffer of size %u bytes", min_buf_len);
63 rtio_iodev_sqe_err(iodev_sqe, rc);
64 return;
65 }
66
67 edata = (struct icm42688_encoded_data *)buf;
68
69 icm42688_encode(dev, channels, num_channels, buf);
70
71 rc = icm42688_rtio_sample_fetch(dev, edata->readings);
72 /* Check that the fetch succeeded */
73 if (rc != 0) {
74 LOG_ERR("Failed to fetch samples");
75 rtio_iodev_sqe_err(iodev_sqe, rc);
76 return;
77 }
78
79 rtio_iodev_sqe_ok(iodev_sqe, 0);
80 }
81
icm42688_submit_sync(struct rtio_iodev_sqe * iodev_sqe)82 void icm42688_submit_sync(struct rtio_iodev_sqe *iodev_sqe)
83 {
84 const struct sensor_read_config *cfg = iodev_sqe->sqe.iodev->data;
85 const struct device *dev = cfg->sensor;
86
87 if (!cfg->is_streaming) {
88 icm42688_submit_one_shot(dev, iodev_sqe);
89 } else if (IS_ENABLED(CONFIG_ICM42688_STREAM)) {
90 icm42688_submit_stream(dev, iodev_sqe);
91 } else {
92 rtio_iodev_sqe_err(iodev_sqe, -ENOTSUP);
93 }
94 }
95
icm42688_submit(const struct device * dev,struct rtio_iodev_sqe * iodev_sqe)96 void icm42688_submit(const struct device *dev, struct rtio_iodev_sqe *iodev_sqe)
97 {
98 struct rtio_work_req *req = rtio_work_req_alloc();
99
100 if (req == NULL) {
101 LOG_ERR("RTIO work item allocation failed. Consider to increase "
102 "CONFIG_RTIO_WORKQ_POOL_ITEMS.");
103 rtio_iodev_sqe_err(iodev_sqe, -ENOMEM);
104 return;
105 }
106
107 rtio_work_req_submit(req, iodev_sqe, icm42688_submit_sync);
108 }
109
110 BUILD_ASSERT(sizeof(struct icm42688_decoder_header) == 9);
111