@@ -114,30 +114,12 @@ impl<I2C: I2cX, PINS> I2c<I2C, PINS> {
114
114
self . clear_interrupt ( ) ;
115
115
}
116
116
117
- /// Set the start bit in the control register. The next byte sent
118
- /// through the bus will be preceded by a (repeated) start condition.
119
- ///
120
- /// # Note
121
- ///
122
- /// This function does not start the transmission. You must call
123
- /// [`Self::write_to_slave`] to start the transmission.
124
- fn set_start ( & self ) {
125
- self . i2c . cr ( ) . write ( |w| w. sta ( ) . set_bit ( ) ) ;
126
- }
127
-
128
117
/// Set the stop bit in the control register.
129
118
/// A stop condition will be sent on the bus.
130
119
fn set_stop ( & self ) {
131
120
self . i2c . cr ( ) . write ( |w| w. sto ( ) . set_bit ( ) ) ;
132
121
}
133
122
134
- /// Set the ACK bit in the control register. If `ack` is `true`, the
135
- /// I2C will **NOT** acknowledge the next byte received. If `ack`
136
- /// is `false`, the I2C will acknowledge the next byte received.
137
- fn set_ack ( & self , ack : bool ) {
138
- self . i2c . cr ( ) . write ( |w| w. ack ( ) . bit ( ack) ) ;
139
- }
140
-
141
123
/// Set the next byte to be transmitted to the I2C slave device.
142
124
///
143
125
/// # Note
@@ -160,8 +142,10 @@ impl<I2C: I2cX, PINS> I2c<I2C, PINS> {
160
142
///
161
143
/// This function does not block until the write is complete. You must call
162
144
/// [`Self::wait_for_write`] to wait for the write to complete.
163
- fn write_to_slave ( & self ) {
164
- self . i2c . cr ( ) . write ( |w| w. wr ( ) . set_bit ( ) ) ;
145
+ fn trigger_write ( & self , start : bool , stop : bool ) {
146
+ self . i2c
147
+ . cr ( )
148
+ . write ( |w| w. sta ( ) . bit ( start) . wr ( ) . set_bit ( ) . sto ( ) . bit ( stop) ) ;
165
149
}
166
150
167
151
/// Trigger a read from the slave device.
@@ -171,16 +155,20 @@ impl<I2C: I2cX, PINS> I2c<I2C, PINS> {
171
155
///
172
156
/// This function does not block until the read is complete. You must call
173
157
/// [`Self::wait_for_read`] to wait for the read to complete.
174
- fn read_from_slave ( & self ) {
175
- self . i2c . cr ( ) . write ( |w| w. rd ( ) . set_bit ( ) ) ;
158
+ fn trigger_read ( & self , ack : bool , stop : bool ) {
159
+ self . i2c
160
+ . cr ( )
161
+ . write ( |w| w. rd ( ) . set_bit ( ) . ack ( ) . bit ( ack) . sto ( ) . bit ( stop) ) ;
176
162
}
177
163
178
164
/// Check if the I2C peripheral is idle.
179
- fn is_idle ( & self ) -> nb:: Result < ( ) , ErrorKind > {
180
- match self . read_sr ( ) . busy ( ) . bit_is_set ( ) {
181
- true => Err ( nb:: Error :: WouldBlock ) ,
182
- false => Ok ( ( ) ) ,
183
- }
165
+ fn is_idle ( & self ) -> bool {
166
+ !self . read_sr ( ) . busy ( ) . bit_is_set ( )
167
+ }
168
+
169
+ /// Blocking version of [`Self::is_idle`].
170
+ fn wait_idle ( & self ) {
171
+ while !self . is_idle ( ) { }
184
172
}
185
173
186
174
/// Acknowledge an interrupt.
@@ -191,36 +179,27 @@ impl<I2C: I2cX, PINS> I2c<I2C, PINS> {
191
179
/// and an [`ErrorKind::ArbitrationLoss`] is returned.
192
180
fn ack_interrupt ( & self ) -> nb:: Result < ( ) , ErrorKind > {
193
181
let sr = self . read_sr ( ) ;
194
-
195
- if sr. al ( ) . bit_is_set ( ) {
196
- self . set_stop ( ) ;
197
- Err ( nb:: Error :: Other ( ErrorKind :: ArbitrationLoss ) )
198
- } else if sr. if_ ( ) . bit_is_set ( ) {
182
+ if sr. if_ ( ) . bit_is_set ( ) {
199
183
self . clear_interrupt ( ) ;
200
- Ok ( ( ) )
184
+ if sr. al ( ) . bit_is_set ( ) {
185
+ self . set_stop ( ) ;
186
+ Err ( nb:: Error :: Other ( ErrorKind :: ArbitrationLoss ) )
187
+ } else {
188
+ Ok ( ( ) )
189
+ }
201
190
} else {
202
191
Err ( nb:: Error :: WouldBlock )
203
192
}
204
193
}
205
194
206
- /// Blocking version of [`Self::is_idle`].
207
- fn wait_idle ( & self ) {
208
- nb:: block!( self . is_idle( ) ) . unwrap ( ) ;
209
- }
210
-
211
195
/// Wait for a read operation to complete.
212
196
///
213
197
/// # Errors
214
198
///
215
199
/// In case of arbitration loss it waits until the bus is idle
216
200
/// before returning an [`ErrorKind::ArbitrationLoss`] error.
217
201
fn wait_for_read ( & self ) -> Result < ( ) , ErrorKind > {
218
- if let Err ( e) = nb:: block!( self . ack_interrupt( ) ) {
219
- self . wait_idle ( ) ;
220
- Err ( e)
221
- } else {
222
- Ok ( ( ) )
223
- }
202
+ nb:: block!( self . ack_interrupt( ) )
224
203
}
225
204
226
205
/// Wait for a write operation to complete.
@@ -232,13 +211,11 @@ impl<I2C: I2cX, PINS> I2c<I2C, PINS> {
232
211
///
233
212
/// In case of arbitration loss it waits until the bus is idle
234
213
/// before returning an [`ErrorKind::ArbitrationLoss`] error.
235
- fn wait_for_write ( & self ) -> Result < ( ) , ErrorKind > {
236
- if let Err ( e) = nb:: block!( self . ack_interrupt( ) ) {
237
- self . wait_idle ( ) ;
238
- Err ( e)
239
- } else if self . read_sr ( ) . rx_ack ( ) . bit_is_set ( ) {
214
+ fn wait_for_write ( & self , source : NoAcknowledgeSource ) -> Result < ( ) , ErrorKind > {
215
+ nb:: block!( self . ack_interrupt( ) ) ?;
216
+ if self . read_sr ( ) . rx_ack ( ) . bit_is_set ( ) {
240
217
self . set_stop ( ) ;
241
- Err ( ErrorKind :: NoAcknowledge ( NoAcknowledgeSource :: Unknown ) )
218
+ Err ( ErrorKind :: NoAcknowledge ( source ) )
242
219
} else {
243
220
Ok ( ( ) )
244
221
}
@@ -258,53 +235,54 @@ impl<I2C: I2cX, PINS> i2c::I2c for I2c<I2C, PINS> {
258
235
address : u8 ,
259
236
operations : & mut [ Operation < ' _ > ] ,
260
237
) -> Result < ( ) , Self :: Error > {
238
+ let n_ops = operations. len ( ) ;
239
+ if n_ops == 0 {
240
+ return Ok ( ( ) ) ;
241
+ }
242
+
261
243
self . wait_idle ( ) ;
262
244
self . reset ( ) ;
263
245
264
- self . set_start ( ) ;
265
- let mut last_op_was_read = false ;
266
- for operation in operations. iter_mut ( ) {
246
+ // we use this flag to detect when we need to send a (repeated) start
247
+ let mut last_op_was_read = match & operations[ 0 ] {
248
+ Operation :: Read ( _) => false ,
249
+ Operation :: Write ( _) => true ,
250
+ } ;
251
+
252
+ for ( i, operation) in operations. iter_mut ( ) . enumerate ( ) {
267
253
match operation {
268
254
Operation :: Write ( bytes) => {
269
- if last_op_was_read {
270
- self . set_start ( ) ;
271
- last_op_was_read = false ;
272
- }
273
255
// Send write command
274
256
self . write_txr ( ( address << 1 ) + FLAG_WRITE ) ;
275
- self . write_to_slave ( ) ;
276
- self . wait_for_write ( ) ?;
257
+ self . trigger_write ( last_op_was_read, false ) ;
258
+ self . wait_for_write ( NoAcknowledgeSource :: Address ) ?;
259
+ last_op_was_read = false ;
277
260
278
261
// Write bytes
279
- for byte in bytes. iter ( ) {
262
+ let n_bytes = bytes. len ( ) ;
263
+ for ( j, byte) in bytes. iter ( ) . enumerate ( ) {
280
264
self . write_txr ( * byte) ;
281
- self . write_to_slave ( ) ;
282
- self . wait_for_write ( ) ?;
265
+ self . trigger_write ( false , ( i == n_ops - 1 ) && ( j == n_bytes - 1 ) ) ;
266
+ self . wait_for_write ( NoAcknowledgeSource :: Data ) ?;
283
267
}
284
268
}
285
269
Operation :: Read ( buffer) => {
286
- if !last_op_was_read {
287
- self . set_start ( ) ;
288
- last_op_was_read = true ;
289
- }
290
270
// Send read command
291
271
self . write_txr ( ( address << 1 ) + FLAG_READ ) ;
292
- self . write_to_slave ( ) ;
293
- self . wait_for_write ( ) ?;
272
+ self . trigger_write ( !last_op_was_read, false ) ;
273
+ self . wait_for_write ( NoAcknowledgeSource :: Address ) ?;
274
+ last_op_was_read = true ;
294
275
295
276
// Read bytes
296
- let buffer_len = buffer. len ( ) ;
297
- for ( i, byte) in buffer. iter_mut ( ) . enumerate ( ) {
298
- // Set ACK on all but the last byte
299
- self . set_ack ( i == buffer_len - 1 ) ;
300
- self . read_from_slave ( ) ;
277
+ let n_bytes = buffer. len ( ) ;
278
+ for ( j, byte) in buffer. iter_mut ( ) . enumerate ( ) {
279
+ self . trigger_read ( j == n_bytes - 1 , ( i == n_ops - 1 ) && ( j == n_bytes - 1 ) ) ;
301
280
self . wait_for_read ( ) ?;
302
281
* byte = self . read_rxr ( ) ;
303
282
}
304
283
}
305
284
}
306
285
}
307
- self . set_stop ( ) ;
308
286
self . wait_idle ( ) ;
309
287
310
288
Ok ( ( ) )
0 commit comments