@@ -122,10 +122,10 @@ macro_rules! i2c {
122
122
// NOTE(unsafe) This executes only during initialisation
123
123
let rcc = unsafe { & ( * crate :: stm32:: RCC :: ptr( ) ) } ;
124
124
125
- /* Enable clock for I2C */
125
+ // Enable clock for I2C
126
126
rcc. $apbenr. modify( |_, w| w. $i2cXen( ) . set_bit( ) ) ;
127
127
128
- /* Reset I2C */
128
+ // Reset I2C
129
129
rcc. $apbrstr. modify( |_, w| w. $i2cXrst( ) . set_bit( ) ) ;
130
130
rcc. $apbrstr. modify( |_, w| w. $i2cXrst( ) . clear_bit( ) ) ;
131
131
I2c { i2c, pins } . i2c_init( speed)
@@ -163,7 +163,7 @@ where
163
163
fn i2c_init ( self : Self , speed : KiloHertz ) -> Self {
164
164
use core:: cmp;
165
165
166
- /* Make sure the I2C unit is disabled so we can configure it */
166
+ // Make sure the I2C unit is disabled so we can configure it
167
167
self . i2c . cr1 . modify ( |_, w| w. pe ( ) . clear_bit ( ) ) ;
168
168
169
169
// Calculate settings for I2C speed modes
@@ -191,7 +191,7 @@ where
191
191
scldel = 3 ;
192
192
}
193
193
194
- /* Enable I2C signal generator, and configure I2C for 400KHz full speed */
194
+ // Enable I2C signal generator, and configure I2C for 400KHz full speed
195
195
self . i2c . timingr . write ( |w| {
196
196
w. presc ( )
197
197
. bits ( presc)
@@ -205,7 +205,7 @@ where
205
205
. bits ( scll)
206
206
} ) ;
207
207
208
- /* Enable the I2C processing */
208
+ // Enable the I2C processing
209
209
self . i2c . cr1 . modify ( |_, w| w. pe ( ) . set_bit ( ) ) ;
210
210
211
211
self
@@ -215,15 +215,9 @@ where
215
215
( self . i2c , self . pins )
216
216
}
217
217
218
- fn send_byte ( & self , byte : u8 ) -> Result < ( ) , Error > {
219
- /* Wait until we're ready for sending */
220
- while self . i2c . isr . read ( ) . txis ( ) . bit_is_clear ( ) { }
221
-
222
- /* Push out a byte of data */
223
- self . i2c . txdr . write ( |w| unsafe { w. bits ( u32:: from ( byte) ) } ) ;
224
-
225
- /* If we received a NACK, then this is an error */
226
- if self . i2c . isr . read ( ) . nackf ( ) . bit_is_set ( ) {
218
+ fn check_and_clear_error_flags ( & self , isr : & crate :: stm32:: i2c1:: isr:: R ) -> Result < ( ) , Error > {
219
+ // If we received a NACK, then this is an error
220
+ if isr. nackf ( ) . bit_is_set ( ) {
227
221
self . i2c
228
222
. icr
229
223
. write ( |w| w. stopcf ( ) . set_bit ( ) . nackcf ( ) . set_bit ( ) ) ;
@@ -233,8 +227,28 @@ where
233
227
Ok ( ( ) )
234
228
}
235
229
230
+ fn send_byte ( & self , byte : u8 ) -> Result < ( ) , Error > {
231
+ // Wait until we're ready for sending
232
+ while {
233
+ let isr = self . i2c . isr . read ( ) ;
234
+ self . check_and_clear_error_flags ( & isr) ?;
235
+ isr. txis ( ) . bit_is_clear ( )
236
+ } { }
237
+
238
+ // Push out a byte of data
239
+ self . i2c . txdr . write ( |w| unsafe { w. bits ( u32:: from ( byte) ) } ) ;
240
+
241
+ self . check_and_clear_error_flags ( & self . i2c . isr . read ( ) ) ?;
242
+ Ok ( ( ) )
243
+ }
244
+
236
245
fn recv_byte ( & self ) -> Result < u8 , Error > {
237
- while self . i2c . isr . read ( ) . rxne ( ) . bit_is_clear ( ) { }
246
+ while {
247
+ let isr = self . i2c . isr . read ( ) ;
248
+ self . check_and_clear_error_flags ( & isr) ?;
249
+ isr. rxne ( ) . bit_is_clear ( )
250
+ } { }
251
+
238
252
let value = self . i2c . rxdr . read ( ) . bits ( ) as u8 ;
239
253
Ok ( value)
240
254
}
@@ -248,8 +262,7 @@ where
248
262
type Error = Error ;
249
263
250
264
fn write_read ( & mut self , addr : u8 , bytes : & [ u8 ] , buffer : & mut [ u8 ] ) -> Result < ( ) , Error > {
251
- /* Set up current address, we're trying a "read" command and not going to set anything
252
- * and make sure we end a non-NACKed read (i.e. if we found a device) properly */
265
+ // Set up current slave address for writing and disable autoending
253
266
self . i2c . cr2 . modify ( |_, w| {
254
267
w. sadd ( )
255
268
. bits ( u16:: from ( addr) << 1 )
@@ -261,37 +274,29 @@ where
261
274
. clear_bit ( )
262
275
} ) ;
263
276
264
- /* Send a START condition */
277
+ // Send a START condition
265
278
self . i2c . cr2 . modify ( |_, w| w. start ( ) . set_bit ( ) ) ;
266
279
267
- /* Wait until the transmit buffer is empty and there hasn't been either a NACK or STOP
268
- * being received */
269
- let mut isr;
280
+ // Wait until the transmit buffer is empty and there hasn't been any error condition
270
281
while {
271
- isr = self . i2c . isr . read ( ) ;
272
- isr. txis ( ) . bit_is_clear ( )
273
- && isr. nackf ( ) . bit_is_clear ( )
274
- && isr. stopf ( ) . bit_is_clear ( )
275
- && isr. tc ( ) . bit_is_clear ( )
282
+ let isr = self . i2c . isr . read ( ) ;
283
+ self . check_and_clear_error_flags ( & isr) ?;
284
+ isr. txis ( ) . bit_is_clear ( ) && isr. tc ( ) . bit_is_clear ( )
276
285
} { }
277
286
278
- /* If we received a NACK, then this is an error */
279
- if isr. nackf ( ) . bit_is_set ( ) {
280
- self . i2c
281
- . icr
282
- . write ( |w| w. stopcf ( ) . set_bit ( ) . nackcf ( ) . set_bit ( ) ) ;
283
- return Err ( Error :: NACK ) ;
284
- }
285
-
287
+ // Send out all individual bytes
286
288
for c in bytes {
287
289
self . send_byte ( * c) ?;
288
290
}
289
291
290
- /* Wait until data was sent */
291
- while self . i2c . isr . read ( ) . tc ( ) . bit_is_clear ( ) { }
292
+ // Wait until data was sent
293
+ while {
294
+ let isr = self . i2c . isr . read ( ) ;
295
+ self . check_and_clear_error_flags ( & isr) ?;
296
+ isr. tc ( ) . bit_is_clear ( )
297
+ } { }
292
298
293
- /* Set up current address, we're trying a "read" command and not going to set anything
294
- * and make sure we end a non-NACKed read (i.e. if we found a device) properly */
299
+ // Set up current address for reading
295
300
self . i2c . cr2 . modify ( |_, w| {
296
301
w. sadd ( )
297
302
. bits ( u16:: from ( addr) << 1 )
@@ -301,21 +306,19 @@ where
301
306
. set_bit ( )
302
307
} ) ;
303
308
304
- /* Send a START condition */
309
+ // Send another START condition
305
310
self . i2c . cr2 . modify ( |_, w| w. start ( ) . set_bit ( ) ) ;
306
311
307
- /* Send the autoend after setting the start to get a restart */
312
+ // Send the autoend after setting the start to get a restart
308
313
self . i2c . cr2 . modify ( |_, w| w. autoend ( ) . set_bit ( ) ) ;
309
314
310
- /* Read in all bytes */
315
+ // Now read in all bytes
311
316
for c in buffer. iter_mut ( ) {
312
317
* c = self . recv_byte ( ) ?;
313
318
}
314
319
315
- /* Clear flags if they somehow ended up set */
316
- self . i2c
317
- . icr
318
- . write ( |w| w. stopcf ( ) . set_bit ( ) . nackcf ( ) . set_bit ( ) ) ;
320
+ // Check and clear flags if they somehow ended up set
321
+ self . check_and_clear_error_flags ( & self . i2c . isr . read ( ) ) ?;
319
322
320
323
Ok ( ( ) )
321
324
}
@@ -329,8 +332,7 @@ where
329
332
type Error = Error ;
330
333
331
334
fn write ( & mut self , addr : u8 , bytes : & [ u8 ] ) -> Result < ( ) , Error > {
332
- /* Set up current address, we're trying a "read" command and not going to set anything
333
- * and make sure we end a non-NACKed read (i.e. if we found a device) properly */
335
+ // Set up current slave address for writing and enable autoending
334
336
self . i2c . cr2 . modify ( |_, w| {
335
337
w. sadd ( )
336
338
. bits ( u16:: from ( addr) << 1 )
@@ -342,17 +344,17 @@ where
342
344
. set_bit ( )
343
345
} ) ;
344
346
345
- /* Send a START condition */
347
+ // Send a START condition
346
348
self . i2c . cr2 . modify ( |_, w| w. start ( ) . set_bit ( ) ) ;
347
349
350
+ // Send out all individual bytes
348
351
for c in bytes {
349
352
self . send_byte ( * c) ?;
350
353
}
351
354
352
- /* Fallthrough is success */
353
- self . i2c
354
- . icr
355
- . write ( |w| w. stopcf ( ) . set_bit ( ) . nackcf ( ) . set_bit ( ) ) ;
355
+ // Check and clear flags if they somehow ended up set
356
+ self . check_and_clear_error_flags ( & self . i2c . isr . read ( ) ) ?;
357
+
356
358
Ok ( ( ) )
357
359
}
358
360
}
0 commit comments