|
12 | 12 | #include <zephyr/logging/log.h> |
13 | 13 | LOG_MODULE_DECLARE(i2c_rtio, CONFIG_I2C_LOG_LEVEL); |
14 | 14 |
|
15 | | -static int i2c_iodev_submit_rx(struct rtio_iodev_sqe *iodev_sqe, struct i2c_msg msgs[2], |
16 | | - uint8_t *num_msgs) |
| 15 | +static inline void i2c_msg_from_rx(const struct rtio_iodev_sqe *iodev_sqe, struct i2c_msg *msg) |
17 | 16 | { |
18 | 17 | __ASSERT_NO_MSG(iodev_sqe->sqe.op == RTIO_OP_RX); |
19 | 18 |
|
20 | | - msgs[0].buf = iodev_sqe->sqe.rx.buf; |
21 | | - msgs[0].len = iodev_sqe->sqe.rx.buf_len; |
22 | | - msgs[0].flags = |
| 19 | + msg->buf = iodev_sqe->sqe.rx.buf; |
| 20 | + msg->len = iodev_sqe->sqe.rx.buf_len; |
| 21 | + msg->flags = |
23 | 22 | ((iodev_sqe->sqe.iodev_flags & RTIO_IODEV_I2C_STOP) ? I2C_MSG_STOP : 0) | |
24 | 23 | ((iodev_sqe->sqe.iodev_flags & RTIO_IODEV_I2C_RESTART) ? I2C_MSG_RESTART : 0) | |
25 | 24 | ((iodev_sqe->sqe.iodev_flags & RTIO_IODEV_I2C_10_BITS) ? I2C_MSG_ADDR_10_BITS : 0) | |
26 | 25 | I2C_MSG_READ; |
27 | | - *num_msgs = 1; |
28 | | - return 0; |
29 | 26 | } |
30 | 27 |
|
31 | | -static int i2c_iodev_submit_tx(struct rtio_iodev_sqe *iodev_sqe, struct i2c_msg msgs[2], |
32 | | - uint8_t *num_msgs) |
| 28 | +static inline void i2c_msg_from_tx(const struct rtio_iodev_sqe *iodev_sqe, struct i2c_msg *msg) |
33 | 29 | { |
34 | 30 | __ASSERT_NO_MSG(iodev_sqe->sqe.op == RTIO_OP_TX); |
35 | 31 |
|
36 | | - msgs[0].buf = (uint8_t *)iodev_sqe->sqe.tx.buf; |
37 | | - msgs[0].len = iodev_sqe->sqe.tx.buf_len; |
38 | | - msgs[0].flags = |
| 32 | + msg->buf = (uint8_t *)iodev_sqe->sqe.tx.buf; |
| 33 | + msg->len = iodev_sqe->sqe.tx.buf_len; |
| 34 | + msg->flags = |
39 | 35 | ((iodev_sqe->sqe.iodev_flags & RTIO_IODEV_I2C_STOP) ? I2C_MSG_STOP : 0) | |
40 | 36 | ((iodev_sqe->sqe.iodev_flags & RTIO_IODEV_I2C_RESTART) ? I2C_MSG_RESTART : 0) | |
41 | 37 | ((iodev_sqe->sqe.iodev_flags & RTIO_IODEV_I2C_10_BITS) ? I2C_MSG_ADDR_10_BITS : 0) | |
42 | 38 | I2C_MSG_WRITE; |
43 | | - *num_msgs = 1; |
44 | | - return 0; |
45 | 39 | } |
46 | 40 |
|
47 | | -static int i2c_iodev_submit_tiny_tx(struct rtio_iodev_sqe *iodev_sqe, struct i2c_msg msgs[2], |
48 | | - uint8_t *num_msgs) |
| 41 | +static inline void i2c_msg_from_tiny_tx(const struct rtio_iodev_sqe *iodev_sqe, struct i2c_msg *msg) |
49 | 42 | { |
50 | 43 | __ASSERT_NO_MSG(iodev_sqe->sqe.op == RTIO_OP_TINY_TX); |
51 | 44 |
|
52 | | - msgs[0].buf = (uint8_t *)iodev_sqe->sqe.tiny_tx.buf; |
53 | | - msgs[0].len = iodev_sqe->sqe.tiny_tx.buf_len; |
54 | | - msgs[0].flags = |
| 45 | + msg->buf = (uint8_t *)iodev_sqe->sqe.tiny_tx.buf; |
| 46 | + msg->len = iodev_sqe->sqe.tiny_tx.buf_len; |
| 47 | + msg->flags = |
55 | 48 | ((iodev_sqe->sqe.iodev_flags & RTIO_IODEV_I2C_STOP) ? I2C_MSG_STOP : 0) | |
56 | 49 | ((iodev_sqe->sqe.iodev_flags & RTIO_IODEV_I2C_RESTART) ? I2C_MSG_RESTART : 0) | |
57 | 50 | ((iodev_sqe->sqe.iodev_flags & RTIO_IODEV_I2C_10_BITS) ? I2C_MSG_ADDR_10_BITS : 0) | |
58 | 51 | I2C_MSG_WRITE; |
59 | | - *num_msgs = 1; |
60 | | - return 0; |
61 | 52 | } |
62 | 53 |
|
63 | | -void i2c_iodev_submit_work_handler(struct rtio_iodev_sqe *iodev_sqe) |
| 54 | +void i2c_iodev_submit_work_handler(struct rtio_iodev_sqe *txn_first) |
64 | 55 | { |
65 | | - const struct i2c_dt_spec *dt_spec = (const struct i2c_dt_spec *)iodev_sqe->sqe.iodev->data; |
| 56 | + const struct i2c_dt_spec *dt_spec = (const struct i2c_dt_spec *)txn_first->sqe.iodev->data; |
66 | 57 | const struct device *dev = dt_spec->bus; |
67 | 58 |
|
68 | | - LOG_DBG("Sync RTIO work item for: %p", (void *)iodev_sqe); |
69 | | - |
70 | | - struct rtio_iodev_sqe *transaction_current = iodev_sqe; |
71 | | - struct i2c_msg msgs[2]; |
72 | | - uint8_t num_msgs; |
| 59 | + LOG_DBG("Sync RTIO work item for: %p", (void *)txn_first); |
| 60 | + uint32_t num_msgs = 0; |
73 | 61 | int rc = 0; |
| 62 | + struct rtio_iodev_sqe *txn_last = txn_first; |
74 | 63 |
|
| 64 | + /* We allocate the i2c_msg's on the stack, to do so |
| 65 | + * the count of messages needs to be determined to |
| 66 | + * ensure we don't go over the statically sized array. |
| 67 | + */ |
75 | 68 | do { |
76 | | - /* Convert the iodev_sqe back to an i2c_msg */ |
77 | | - switch (transaction_current->sqe.op) { |
| 69 | + switch (txn_last->sqe.op) { |
| 70 | + case RTIO_OP_RX: |
| 71 | + case RTIO_OP_TX: |
| 72 | + case RTIO_OP_TINY_TX: |
| 73 | + num_msgs++; |
| 74 | + break; |
| 75 | + default: |
| 76 | + LOG_ERR("Invalid op code %d for submission %p", txn_last->sqe.op, |
| 77 | + (void *)&txn_last->sqe); |
| 78 | + rc = -EIO; |
| 79 | + break; |
| 80 | + } |
| 81 | + txn_last = rtio_txn_next(txn_last); |
| 82 | + } while (rc == 0 && txn_last != NULL); |
| 83 | + |
| 84 | + if (rc != 0) { |
| 85 | + rtio_iodev_sqe_err(txn_first, rc); |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + /* Allocate msgs on the stack, MISRA doesn't like VLAs so we need a statically |
| 90 | + * sized array here. It's pretty unlikely we have more than 4 i2c messages |
| 91 | + * in a transaction as we typically would only have 2, one to write a |
| 92 | + * register address, and another to read/write the register into an array |
| 93 | + */ |
| 94 | + if (num_msgs > CONFIG_I2C_RTIO_FALLBACK_MSGS) { |
| 95 | + LOG_ERR("At most CONFIG_I2C_RTIO_FALLBACK_MSGS" |
| 96 | + " submissions in a transaction are" |
| 97 | + " allowed in the default handler"); |
| 98 | + rtio_iodev_sqe_err(txn_first, -ENOMEM); |
| 99 | + return; |
| 100 | + } |
| 101 | + struct i2c_msg msgs[CONFIG_I2C_RTIO_FALLBACK_MSGS]; |
| 102 | + |
| 103 | + rc = 0; |
| 104 | + txn_last = txn_first; |
| 105 | + |
| 106 | + /* Copy the transaction into the stack allocated msgs */ |
| 107 | + for (int i = 0; i < num_msgs; i++) { |
| 108 | + switch (txn_last->sqe.op) { |
78 | 109 | case RTIO_OP_RX: |
79 | | - rc = i2c_iodev_submit_rx(transaction_current, msgs, &num_msgs); |
| 110 | + i2c_msg_from_rx(txn_last, &msgs[i]); |
80 | 111 | break; |
81 | 112 | case RTIO_OP_TX: |
82 | | - rc = i2c_iodev_submit_tx(transaction_current, msgs, &num_msgs); |
| 113 | + i2c_msg_from_tx(txn_last, &msgs[i]); |
83 | 114 | break; |
84 | 115 | case RTIO_OP_TINY_TX: |
85 | | - rc = i2c_iodev_submit_tiny_tx(transaction_current, msgs, &num_msgs); |
| 116 | + i2c_msg_from_tiny_tx(txn_last, &msgs[i]); |
86 | 117 | break; |
87 | 118 | default: |
88 | | - LOG_ERR("Invalid op code %d for submission %p", transaction_current->sqe.op, |
89 | | - (void *)&transaction_current->sqe); |
90 | 119 | rc = -EIO; |
91 | 120 | break; |
92 | 121 | } |
93 | 122 |
|
94 | | - if (rc == 0) { |
95 | | - __ASSERT_NO_MSG(num_msgs > 0); |
| 123 | + txn_last = rtio_txn_next(txn_last); |
| 124 | + } |
96 | 125 |
|
97 | | - rc = i2c_transfer(dev, msgs, num_msgs, dt_spec->addr); |
98 | | - transaction_current = rtio_txn_next(transaction_current); |
99 | | - } |
100 | | - } while (rc == 0 && transaction_current != NULL); |
| 126 | + if (rc == 0) { |
| 127 | + __ASSERT_NO_MSG(num_msgs > 0); |
| 128 | + |
| 129 | + rc = i2c_transfer(dev, msgs, num_msgs, dt_spec->addr); |
| 130 | + } |
101 | 131 |
|
102 | 132 | if (rc != 0) { |
103 | | - rtio_iodev_sqe_err(iodev_sqe, rc); |
| 133 | + rtio_iodev_sqe_err(txn_first, rc); |
104 | 134 | } else { |
105 | | - rtio_iodev_sqe_ok(iodev_sqe, 0); |
| 135 | + rtio_iodev_sqe_ok(txn_first, 0); |
106 | 136 | } |
107 | 137 | } |
108 | 138 |
|
|
0 commit comments