@@ -78,151 +78,151 @@ void arduino::ZephyrI2C::setClock(uint32_t freq) {
78
78
uint8_t speed;
79
79
80
80
if (freq == 100000 ) {
81
- speed = I2C_SPEED_STANDARD;
81
+ speed = I2C_SPEED_STANDARD;
82
82
} else if (freq == 400000 ) {
83
- speed = I2C_SPEED_FAST;
83
+ speed = I2C_SPEED_FAST;
84
84
} else if (freq == 1000000 ) {
85
- speed = I2C_SPEED_FAST_PLUS;
85
+ speed = I2C_SPEED_FAST_PLUS;
86
86
} else {
87
- speed = I2C_SPEED_STANDARD;
87
+ speed = I2C_SPEED_STANDARD;
88
88
}
89
89
90
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);
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
+ }
223
224
}
224
- }
225
- return 0 ;
225
+ return 0 ;
226
226
}
227
227
228
228
#if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), i2cs)
0 commit comments