@@ -149,6 +149,7 @@ i2c_pins! {
149
149
pub enum Error {
150
150
OVERRUN ,
151
151
NACK ,
152
+ BUS ,
152
153
}
153
154
154
155
macro_rules! i2c {
@@ -257,8 +258,22 @@ where
257
258
( self . i2c , self . pins )
258
259
}
259
260
260
- fn check_and_clear_error_flags ( & self , isr : & crate :: pac:: i2c1:: isr:: R ) -> Result < ( ) , Error > {
261
- // If we received a NACK, then this is an error
261
+ fn check_and_clear_error_flags ( & self , isr : & crate :: stm32:: i2c1:: isr:: R ) -> Result < ( ) , Error > {
262
+ // If we have a set overrun flag, clear it and return an OVERRUN error
263
+ if isr. ovr ( ) . bit_is_set ( ) {
264
+ self . i2c . icr . write ( |w| w. ovrcf ( ) . set_bit ( ) ) ;
265
+ return Err ( Error :: OVERRUN ) ;
266
+ }
267
+
268
+ // If we have a set arbitration error or bus error flag, clear it and return an BUS error
269
+ if isr. arlo ( ) . bit_is_set ( ) | isr. berr ( ) . bit_is_set ( ) {
270
+ self . i2c
271
+ . icr
272
+ . write ( |w| w. arlocf ( ) . set_bit ( ) . berrcf ( ) . set_bit ( ) ) ;
273
+ return Err ( Error :: BUS ) ;
274
+ }
275
+
276
+ // If we received a NACK, then signal as a NACK error
262
277
if isr. nackf ( ) . bit_is_set ( ) {
263
278
self . i2c
264
279
. icr
@@ -271,11 +286,13 @@ where
271
286
272
287
fn send_byte ( & self , byte : u8 ) -> Result < ( ) , Error > {
273
288
// Wait until we're ready for sending
274
- while {
289
+ loop {
275
290
let isr = self . i2c . isr . read ( ) ;
276
291
self . check_and_clear_error_flags ( & isr) ?;
277
- isr. txis ( ) . bit_is_clear ( )
278
- } { }
292
+ if isr. txis ( ) . bit_is_set ( ) {
293
+ break ;
294
+ }
295
+ }
279
296
280
297
// Push out a byte of data
281
298
self . i2c . txdr . write ( |w| unsafe { w. bits ( u32:: from ( byte) ) } ) ;
@@ -285,11 +302,13 @@ where
285
302
}
286
303
287
304
fn recv_byte ( & self ) -> Result < u8 , Error > {
288
- while {
305
+ loop {
289
306
let isr = self . i2c . isr . read ( ) ;
290
307
self . check_and_clear_error_flags ( & isr) ?;
291
- isr. rxne ( ) . bit_is_clear ( )
292
- } { }
308
+ if isr. rxne ( ) . bit_is_set ( ) {
309
+ break ;
310
+ }
311
+ }
293
312
294
313
let value = self . i2c . rxdr . read ( ) . bits ( ) as u8 ;
295
314
Ok ( value)
@@ -319,23 +338,27 @@ where
319
338
self . i2c . cr2 . modify ( |_, w| w. start ( ) . set_bit ( ) ) ;
320
339
321
340
// Wait until the transmit buffer is empty and there hasn't been any error condition
322
- while {
341
+ loop {
323
342
let isr = self . i2c . isr . read ( ) ;
324
343
self . check_and_clear_error_flags ( & isr) ?;
325
- isr. txis ( ) . bit_is_clear ( ) && isr. tc ( ) . bit_is_clear ( )
326
- } { }
344
+ if isr. txis ( ) . bit_is_set ( ) || isr. tc ( ) . bit_is_set ( ) {
345
+ break ;
346
+ }
347
+ }
327
348
328
349
// Send out all individual bytes
329
350
for c in bytes {
330
351
self . send_byte ( * c) ?;
331
352
}
332
353
333
354
// Wait until data was sent
334
- while {
355
+ loop {
335
356
let isr = self . i2c . isr . read ( ) ;
336
357
self . check_and_clear_error_flags ( & isr) ?;
337
- isr. tc ( ) . bit_is_clear ( )
338
- } { }
358
+ if isr. tc ( ) . bit_is_set ( ) {
359
+ break ;
360
+ }
361
+ }
339
362
340
363
// Set up current address for reading
341
364
self . i2c . cr2 . modify ( |_, w| {
0 commit comments