|
10 | 10 |
|
11 | 11 | // Helper function to get ZephyrI2C instance from config pointer. |
12 | 12 | static arduino::ZephyrI2C* getInstance(struct i2c_target_config *config) { |
13 | | - return reinterpret_cast<arduino::ZephyrI2C*>( |
14 | | - reinterpret_cast<char*>(config) - offsetof(arduino::ZephyrI2C, i2c_cfg) |
15 | | - ); |
| 13 | + return reinterpret_cast<arduino::ZephyrI2C*>( |
| 14 | + reinterpret_cast<char*>(config) - offsetof(arduino::ZephyrI2C, i2c_cfg) |
| 15 | + ); |
16 | 16 | } |
17 | 17 |
|
18 | 18 | static int i2c_target_stop_cb(struct i2c_target_config *config) { |
19 | | - arduino::ZephyrI2C *instance = getInstance(config); |
20 | | - return instance->stopCallback(config); |
| 19 | + arduino::ZephyrI2C *instance = getInstance(config); |
| 20 | + return instance->stopCallback(config); |
21 | 21 | } |
22 | 22 |
|
23 | 23 | static int i2c_target_write_requested_cb(struct i2c_target_config *config) { |
24 | | - arduino::ZephyrI2C *instance = getInstance(config); |
25 | | - return instance->writeRequestedCallback(config); |
| 24 | + arduino::ZephyrI2C *instance = getInstance(config); |
| 25 | + return instance->writeRequestedCallback(config); |
26 | 26 | } |
27 | 27 |
|
28 | 28 | static int i2c_target_write_received_cb(struct i2c_target_config *config, uint8_t val) { |
29 | | - arduino::ZephyrI2C *instance = getInstance(config); |
30 | | - return instance->writeReceivedCallback(config, val); |
| 29 | + arduino::ZephyrI2C *instance = getInstance(config); |
| 30 | + return instance->writeReceivedCallback(config, val); |
31 | 31 | } |
32 | 32 |
|
33 | 33 | static int i2c_target_read_requested_cb(struct i2c_target_config *config, uint8_t *val) { |
34 | | - arduino::ZephyrI2C *instance = getInstance(config); |
35 | | - return instance->readRequestedCallback(config, val); |
| 34 | + arduino::ZephyrI2C *instance = getInstance(config); |
| 35 | + return instance->readRequestedCallback(config, val); |
36 | 36 | } |
37 | 37 |
|
38 | 38 | static int i2c_target_read_processed_cb(struct i2c_target_config *config, uint8_t *val) { |
39 | | - arduino::ZephyrI2C *instance = getInstance(config); |
40 | | - return instance->readProcessedCallback(config, val); |
| 39 | + arduino::ZephyrI2C *instance = getInstance(config); |
| 40 | + return instance->readProcessedCallback(config, val); |
41 | 41 | } |
42 | 42 |
|
43 | 43 | // I2C target callback structure. |
44 | 44 | static struct i2c_target_callbacks target_callbacks = { |
45 | | - .write_requested = i2c_target_write_requested_cb, |
46 | | - .read_requested = i2c_target_read_requested_cb, |
47 | | - .write_received = i2c_target_write_received_cb, |
48 | | - .read_processed = i2c_target_read_processed_cb, |
49 | | - .stop = i2c_target_stop_cb, |
| 45 | + .write_requested = i2c_target_write_requested_cb, |
| 46 | + .read_requested = i2c_target_read_requested_cb, |
| 47 | + .write_received = i2c_target_write_received_cb, |
| 48 | + .read_processed = i2c_target_read_processed_cb, |
| 49 | + .stop = i2c_target_stop_cb, |
50 | 50 | }; |
51 | 51 |
|
52 | 52 | arduino::ZephyrI2C::ZephyrI2C(const struct device *i2c) : i2c_dev(i2c), i2c_cfg({0}) { |
53 | | - ring_buf_init(&txRingBuffer.rb, sizeof(txRingBuffer.buffer), txRingBuffer.buffer); |
54 | | - ring_buf_init(&rxRingBuffer.rb, sizeof(rxRingBuffer.buffer), rxRingBuffer.buffer); |
| 53 | + ring_buf_init(&txRingBuffer.rb, sizeof(txRingBuffer.buffer), txRingBuffer.buffer); |
| 54 | + ring_buf_init(&rxRingBuffer.rb, sizeof(rxRingBuffer.buffer), rxRingBuffer.buffer); |
55 | 55 | } |
56 | 56 |
|
57 | 57 | void arduino::ZephyrI2C::begin() { |
58 | 58 |
|
59 | 59 | } |
60 | 60 |
|
61 | 61 | void arduino::ZephyrI2C::begin(uint8_t slaveAddr) { |
62 | | - i2c_cfg.address = slaveAddr; |
63 | | - i2c_cfg.callbacks = &target_callbacks; |
| 62 | + i2c_cfg.address = slaveAddr; |
| 63 | + i2c_cfg.callbacks = &target_callbacks; |
64 | 64 |
|
65 | | - // Register I2C target |
66 | | - i2c_target_register(i2c_dev, &i2c_cfg); |
| 65 | + // Register I2C target |
| 66 | + i2c_target_register(i2c_dev, &i2c_cfg); |
67 | 67 | } |
68 | 68 |
|
69 | 69 | void arduino::ZephyrI2C::end() { |
70 | | - // Unregister I2C target |
71 | | - if (i2c_cfg.address) { |
72 | | - i2c_target_unregister(i2c_dev, &i2c_cfg); |
73 | | - memset(&i2c_cfg, 0, sizeof(i2c_cfg)); |
74 | | - } |
| 70 | + // Unregister I2C target |
| 71 | + if (i2c_cfg.address) { |
| 72 | + i2c_target_unregister(i2c_dev, &i2c_cfg); |
| 73 | + memset(&i2c_cfg, 0, sizeof(i2c_cfg)); |
| 74 | + } |
75 | 75 | } |
76 | 76 |
|
77 | 77 | void arduino::ZephyrI2C::setClock(uint32_t freq) { |
78 | | - uint8_t speed; |
| 78 | + uint8_t speed; |
79 | 79 |
|
80 | | - if (freq == 100000) { |
81 | | - speed = I2C_SPEED_STANDARD; |
82 | | - } else if (freq == 400000) { |
83 | | - speed = I2C_SPEED_FAST; |
84 | | - } else if (freq == 1000000) { |
85 | | - speed = I2C_SPEED_FAST_PLUS; |
86 | | - } else { |
87 | | - speed = I2C_SPEED_STANDARD; |
88 | | - } |
| 80 | + if (freq == 100000) { |
| 81 | + speed = I2C_SPEED_STANDARD; |
| 82 | + } else if (freq == 400000) { |
| 83 | + speed = I2C_SPEED_FAST; |
| 84 | + } else if (freq == 1000000) { |
| 85 | + speed = I2C_SPEED_FAST_PLUS; |
| 86 | + } else { |
| 87 | + speed = I2C_SPEED_STANDARD; |
| 88 | + } |
89 | 89 |
|
90 | | - i2c_configure(i2c_dev, I2C_SPEED_SET(speed) | I2C_MODE_CONTROLLER); |
| 90 | + i2c_configure(i2c_dev, I2C_SPEED_SET(speed) | I2C_MODE_CONTROLLER); |
91 | 91 | } |
92 | 92 |
|
93 | 93 | void arduino::ZephyrI2C::beginTransmission(uint8_t address) { |
94 | | - _address = address; |
95 | | - ring_buf_reset(&txRingBuffer.rb); |
96 | | - ring_buf_reset(&rxRingBuffer.rb); |
| 94 | + _address = address; |
| 95 | + ring_buf_reset(&txRingBuffer.rb); |
| 96 | + ring_buf_reset(&rxRingBuffer.rb); |
97 | 97 | } |
98 | 98 |
|
99 | 99 | uint8_t arduino::ZephyrI2C::endTransmission(bool stopBit) { |
100 | | - int ret = 0; |
101 | | - uint8_t *buf = NULL; |
102 | | - size_t max = ring_buf_capacity_get(&txRingBuffer.rb); |
103 | | - size_t len = ring_buf_get_claim(&txRingBuffer.rb, &buf, max); |
| 100 | + int ret = 0; |
| 101 | + uint8_t *buf = NULL; |
| 102 | + size_t max = ring_buf_capacity_get(&txRingBuffer.rb); |
| 103 | + size_t len = ring_buf_get_claim(&txRingBuffer.rb, &buf, max); |
104 | 104 |
|
105 | | - if (len && buf) { |
106 | | - ret = i2c_write(i2c_dev, buf, len, _address); |
107 | | - } |
| 105 | + if (len && buf) { |
| 106 | + ret = i2c_write(i2c_dev, buf, len, _address); |
| 107 | + } |
108 | 108 |
|
109 | | - // Must be called even if 0 bytes claimed. |
110 | | - ring_buf_get_finish(&txRingBuffer.rb, len); |
| 109 | + // Must be called even if 0 bytes claimed. |
| 110 | + ring_buf_get_finish(&txRingBuffer.rb, len); |
111 | 111 |
|
112 | | - return ret ? 1 : 0; |
| 112 | + return ret ? 1 : 0; |
113 | 113 | } |
114 | 114 |
|
115 | 115 | uint8_t arduino::ZephyrI2C::endTransmission(void) { |
116 | | - return endTransmission(true); |
| 116 | + return endTransmission(true); |
117 | 117 | } |
118 | 118 |
|
119 | 119 | size_t arduino::ZephyrI2C::requestFrom(uint8_t address, size_t len_in, bool stopBit) { |
120 | | - int ret = 0; |
121 | | - uint8_t *buf = NULL; |
122 | | - size_t len = ring_buf_put_claim(&rxRingBuffer.rb, &buf, len_in); |
| 120 | + int ret = 0; |
| 121 | + uint8_t *buf = NULL; |
| 122 | + size_t len = ring_buf_put_claim(&rxRingBuffer.rb, &buf, len_in); |
123 | 123 |
|
124 | | - if (len && buf) { |
125 | | - ret = i2c_read(i2c_dev, buf, len, address); |
126 | | - } |
| 124 | + if (len && buf) { |
| 125 | + ret = i2c_read(i2c_dev, buf, len, address); |
| 126 | + } |
127 | 127 |
|
128 | | - // Must be called even if 0 bytes claimed. |
129 | | - ring_buf_put_finish(&rxRingBuffer.rb, len); |
| 128 | + // Must be called even if 0 bytes claimed. |
| 129 | + ring_buf_put_finish(&rxRingBuffer.rb, len); |
130 | 130 |
|
131 | | - return ret ? 1 : 0; |
| 131 | + return ret ? 1 : 0; |
132 | 132 | } |
133 | 133 |
|
134 | 134 | size_t arduino::ZephyrI2C::requestFrom(uint8_t address, size_t len) { |
135 | | - return requestFrom(address, len, true); |
| 135 | + return requestFrom(address, len, true); |
136 | 136 | } |
137 | 137 |
|
138 | 138 | size_t arduino::ZephyrI2C::write(uint8_t data) { |
139 | | - return ring_buf_put(&txRingBuffer.rb, &data, 1); |
| 139 | + return ring_buf_put(&txRingBuffer.rb, &data, 1); |
140 | 140 | } |
141 | 141 |
|
142 | 142 | size_t arduino::ZephyrI2C::write(const uint8_t *buffer, size_t size) { |
143 | | - return ring_buf_put(&txRingBuffer.rb, buffer, size); |
| 143 | + return ring_buf_put(&txRingBuffer.rb, buffer, size); |
144 | 144 | } |
145 | 145 |
|
146 | 146 | int arduino::ZephyrI2C::read() { |
147 | | - uint8_t buf; |
148 | | - if (ring_buf_get(&rxRingBuffer.rb, &buf, 1)) { |
149 | | - return (int) buf; |
150 | | - } |
151 | | - return -1; |
| 147 | + uint8_t buf; |
| 148 | + if (ring_buf_get(&rxRingBuffer.rb, &buf, 1)) { |
| 149 | + return (int) buf; |
| 150 | + } |
| 151 | + return -1; |
152 | 152 | } |
153 | 153 |
|
154 | 154 | int arduino::ZephyrI2C::available() { |
155 | | - return ring_buf_size_get(&rxRingBuffer.rb); |
| 155 | + return ring_buf_size_get(&rxRingBuffer.rb); |
156 | 156 | } |
157 | 157 |
|
158 | 158 | int arduino::ZephyrI2C::peek() { |
159 | | - uint8_t buf; |
160 | | - if (ring_buf_peek(&rxRingBuffer.rb, &buf, 1)) { |
161 | | - return (int) buf; |
162 | | - } |
163 | | - return -1; |
| 159 | + uint8_t buf; |
| 160 | + if (ring_buf_peek(&rxRingBuffer.rb, &buf, 1)) { |
| 161 | + return (int) buf; |
| 162 | + } |
| 163 | + return -1; |
164 | 164 | } |
165 | 165 |
|
166 | 166 | void arduino::ZephyrI2C::flush() { |
167 | 167 |
|
168 | 168 | } |
169 | 169 |
|
170 | 170 | void arduino::ZephyrI2C::onReceive(voidFuncPtrParamInt cb) { |
171 | | - onReceiveCb = cb; |
| 171 | + onReceiveCb = cb; |
172 | 172 | } |
173 | 173 |
|
174 | 174 | void arduino::ZephyrI2C::onRequest(voidFuncPtr cb) { |
175 | | - onRequestCb = cb; |
| 175 | + onRequestCb = cb; |
176 | 176 | } |
177 | 177 |
|
178 | 178 | int arduino::ZephyrI2C::writeRequestedCallback(struct i2c_target_config *config) { |
179 | | - // Reset the buffer on write requests. |
180 | | - ring_buf_reset(&rxRingBuffer.rb); |
181 | | - return 0; |
| 179 | + // Reset the buffer on write requests. |
| 180 | + ring_buf_reset(&rxRingBuffer.rb); |
| 181 | + return 0; |
182 | 182 | } |
183 | 183 |
|
184 | 184 | int arduino::ZephyrI2C::writeReceivedCallback(struct i2c_target_config *config, uint8_t val) { |
185 | | - size_t len = ring_buf_size_get(&rxRingBuffer.rb); |
186 | | - size_t max = ring_buf_capacity_get(&rxRingBuffer.rb); |
| 185 | + size_t len = ring_buf_size_get(&rxRingBuffer.rb); |
| 186 | + size_t max = ring_buf_capacity_get(&rxRingBuffer.rb); |
187 | 187 |
|
188 | | - // If the buffer is about to overflow, invoke the callback |
189 | | - // with the current length. |
190 | | - if (onReceiveCb && ((len + 1) > max)) { |
191 | | - onReceiveCb(len); |
192 | | - } |
| 188 | + // If the buffer is about to overflow, invoke the callback |
| 189 | + // with the current length. |
| 190 | + if (onReceiveCb && ((len + 1) > max)) { |
| 191 | + onReceiveCb(len); |
| 192 | + } |
193 | 193 |
|
194 | | - return ring_buf_put(&rxRingBuffer.rb, &val, 1) ? 0 : -1; |
| 194 | + return ring_buf_put(&rxRingBuffer.rb, &val, 1) ? 0 : -1; |
195 | 195 | } |
196 | 196 |
|
197 | 197 | int arduino::ZephyrI2C::readRequestedCallback(struct i2c_target_config *config, uint8_t *val) { |
198 | | - // Reset the buffer on read requests. |
199 | | - ring_buf_reset(&txRingBuffer.rb); |
| 198 | + // Reset the buffer on read requests. |
| 199 | + ring_buf_reset(&txRingBuffer.rb); |
200 | 200 |
|
201 | | - if (onRequestCb) { |
202 | | - onRequestCb(); |
203 | | - } |
| 201 | + if (onRequestCb) { |
| 202 | + onRequestCb(); |
| 203 | + } |
204 | 204 |
|
205 | | - return readProcessedCallback(config, val); |
| 205 | + return readProcessedCallback(config, val); |
206 | 206 | } |
207 | 207 |
|
208 | 208 | int arduino::ZephyrI2C::readProcessedCallback(struct i2c_target_config *config, uint8_t *val) { |
209 | | - *val = 0xFF; |
210 | | - ring_buf_get(&txRingBuffer.rb, val, 1); |
211 | | - // Returning a negative value here is not handled gracefully and |
212 | | - // causes the target/board to stop responding (at least with ST). |
213 | | - return 0; |
| 209 | + *val = 0xFF; |
| 210 | + ring_buf_get(&txRingBuffer.rb, val, 1); |
| 211 | + // Returning a negative value here is not handled gracefully and |
| 212 | + // causes the target/board to stop responding (at least with ST). |
| 213 | + return 0; |
214 | 214 | } |
215 | 215 |
|
216 | 216 | int arduino::ZephyrI2C::stopCallback(struct i2c_target_config *config) { |
217 | | - // If the RX buffer is not empty invoke the callback with the |
218 | | - // remaining data length. |
219 | | - if (onReceiveCb) { |
220 | | - size_t len = ring_buf_size_get(&rxRingBuffer.rb); |
221 | | - if (len) { |
222 | | - onReceiveCb(len); |
223 | | - } |
224 | | - } |
225 | | - return 0; |
| 217 | + // If the RX buffer is not empty invoke the callback with the |
| 218 | + // remaining data length. |
| 219 | + if (onReceiveCb) { |
| 220 | + size_t len = ring_buf_size_get(&rxRingBuffer.rb); |
| 221 | + if (len) { |
| 222 | + onReceiveCb(len); |
| 223 | + } |
| 224 | + } |
| 225 | + return 0; |
226 | 226 | } |
227 | 227 |
|
228 | 228 | #if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), i2cs) |
|
0 commit comments