2
2
use crate :: gpio:: * ;
3
3
use crate :: gpio:: { AltFunction , OpenDrain , Output } ;
4
4
use crate :: i2c:: config:: Config ;
5
- use crate :: i2c:: { Error , I2c , I2cDirection , I2cExt , I2cResult , SCLPin , SDAPin } ;
5
+ use crate :: i2c:: { EndMarker , Error , I2c , I2cDirection , I2cExt , I2cResult , SCLPin , SDAPin } ;
6
6
use crate :: rcc:: * ;
7
7
use crate :: stm32:: { I2C1 , I2C2 } ;
8
8
use nb:: Error :: { Other , WouldBlock } ;
@@ -215,7 +215,8 @@ macro_rules! i2c {
215
215
length: 0 ,
216
216
errors: 0 ,
217
217
length_write_read: 0 ,
218
- data: [ 0_u8 ; 255 ]
218
+ data: [ 0_u8 ; 255 ] ,
219
+ current_direction: I2cDirection :: MasterReadSlaveWrite ,
219
220
}
220
221
}
221
222
pub fn release( self ) -> ( $I2CX, SDA , SCL ) {
@@ -306,7 +307,7 @@ macro_rules! i2c {
306
307
}
307
308
return Err ( WouldBlock )
308
309
} else
309
- if isr. rxne( ) . bit_is_set( ) {
310
+ if isr. rxne( ) . bit_is_set( ) {
310
311
// read byte from the wire
311
312
if self . index < self . length {
312
313
self . data[ self . index] = self . i2c. rxdr. read( ) . rxdata( ) . bits( ) ;
@@ -317,7 +318,8 @@ macro_rules! i2c {
317
318
}
318
319
return Err ( WouldBlock )
319
320
} else
320
- if isr. stopf( ) . bit_is_set( ) {
321
+ if isr. stopf( ) . bit_is_set( ) {
322
+
321
323
// Clear the stop condition flag
322
324
self . i2c. icr. write( |w| w. stopcf( ) . set_bit( ) ) ;
323
325
// Disable the watchdog
@@ -334,9 +336,13 @@ macro_rules! i2c {
334
336
} else {
335
337
I2cDirection :: MasterWriteSlaveRead
336
338
} ;
339
+ let index = self . index;
340
+ // Clear the length so we don't think there is still data when we reach the address phase
341
+ self . length = self . data. len( ) ;
342
+ self . index = 0 ;
337
343
// return the actual amount of data (self.index), not the requested (self.length)
338
344
// application must evaluate the size of the frame
339
- return Ok ( I2cResult :: Data ( self . address, direction, & self . data[ 0 ..self . index] ) )
345
+ return Ok ( I2cResult :: Data ( self . address, direction, & self . data[ 0 ..index] , EndMarker :: Stop ) )
340
346
}
341
347
} else
342
348
if isr. tc( ) . bit_is_set( ) {
@@ -393,7 +399,14 @@ macro_rules! i2c {
393
399
394
400
} else
395
401
if isr. addr( ) . bit_is_set( ) {
396
- // handle the slave device case, addressed by a master
402
+ // Handle the case where there was no stop bit (repeated start)
403
+ // In this case there will be data on the buffer which still needs to be delivered
404
+ if self . current_direction == I2cDirection :: MasterWriteSlaveRead && self . index >= 1 {
405
+ let index = self . index;
406
+ self . index = 0 ;
407
+ return Ok ( I2cResult :: Data ( self . address, self . current_direction, & self . data[ 0 ..index] , EndMarker :: StartRepeat ) )
408
+ }
409
+ // handle the slave device case, addressed by a master
397
410
let current_address = isr. addcode( ) . bits( ) as u16 ;
398
411
self . address = current_address;
399
412
// guard against misbehavior
@@ -419,7 +432,7 @@ macro_rules! i2c {
419
432
// return result
420
433
I2cDirection :: MasterWriteSlaveRead
421
434
} ;
422
-
435
+ self . current_direction = direction ;
423
436
// do not yet release the clock stretching here
424
437
return Ok ( I2cResult :: Addressed ( current_address, direction) )
425
438
}
0 commit comments