-
Notifications
You must be signed in to change notification settings - Fork 9.9k
Expand file tree
/
Copy pathrm3100_bus.h
More file actions
103 lines (86 loc) · 2.4 KB
/
Copy pathrm3100_bus.h
File metadata and controls
103 lines (86 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* Copyright (c) 2025 Croxel Inc.
* Copyright (c) 2025 CogniPilot Foundation
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_DRIVERS_SENSOR_RM3100_BUS_H_
#define ZEPHYR_DRIVERS_SENSOR_RM3100_BUS_H_
#include <stdint.h>
#include <zephyr/rtio/rtio.h>
#include "rm3100.h"
#include "rm3100_reg.h"
static inline int rm3100_bus_read(const struct device *dev,
uint8_t reg,
uint8_t *buf,
uint16_t len)
{
struct rm3100_data *data = dev->data;
struct rtio *ctx = data->rtio.ctx;
struct rtio_iodev *iodev = data->rtio.iodev;
struct rtio_sqe *write_sqe = rtio_sqe_acquire(ctx);
struct rtio_sqe *read_sqe = rtio_sqe_acquire(ctx);
struct rtio_cqe *cqe;
int err;
if (!write_sqe || !read_sqe) {
return -ENOMEM;
}
/* The read-address flag is an SPI-only convention: on I2C the
* register address must be sent raw, or reads hit a nonexistent
* register and return zeros.
*/
if (!rtio_is_i2c(data->rtio.type)) {
reg = reg | REG_READ_BIT;
}
rtio_sqe_prep_write(write_sqe, iodev, RTIO_PRIO_HIGH, ®, 1, NULL);
write_sqe->flags |= RTIO_SQE_TRANSACTION;
rtio_sqe_prep_read(read_sqe, iodev, RTIO_PRIO_HIGH, buf, len, NULL);
if (rtio_is_i2c(data->rtio.type)) {
read_sqe->iodev_flags |= RTIO_IODEV_I2C_STOP | RTIO_IODEV_I2C_RESTART;
}
err = rtio_submit(ctx, 2);
if (err) {
return err;
}
do {
cqe = rtio_cqe_consume(ctx);
if (cqe != NULL) {
err = cqe->result;
rtio_cqe_release(ctx, cqe);
}
} while (cqe != NULL);
return err;
}
static inline int rm3100_bus_write(const struct device *dev,
uint8_t reg,
const uint8_t *buf,
uint16_t len)
{
struct rm3100_data *data = dev->data;
struct rtio *ctx = data->rtio.ctx;
struct rtio_iodev *iodev = data->rtio.iodev;
struct rtio_sqe *write_reg_sqe = rtio_sqe_acquire(ctx);
struct rtio_sqe *write_buf_sqe = rtio_sqe_acquire(ctx);
struct rtio_cqe *cqe;
int err;
if (!write_reg_sqe || !write_buf_sqe) {
return -ENOMEM;
}
rtio_sqe_prep_write(write_reg_sqe, iodev, RTIO_PRIO_HIGH, ®, 1, NULL);
write_reg_sqe->flags |= RTIO_SQE_TRANSACTION;
rtio_sqe_prep_write(write_buf_sqe, iodev, RTIO_PRIO_HIGH, buf, len, NULL);
write_buf_sqe->iodev_flags |= RTIO_IODEV_I2C_STOP;
err = rtio_submit(ctx, 2);
if (err) {
return err;
}
do {
cqe = rtio_cqe_consume(ctx);
if (cqe != NULL) {
err = cqe->result;
rtio_cqe_release(ctx, cqe);
}
} while (cqe != NULL);
return err;
}
#endif /* ZEPHYR_DRIVERS_SENSOR_RM3100_BUS_H_ */