@@ -598,8 +598,6 @@ int i2c_stop(i2c_t *obj)
598
598
return 0 ;
599
599
}
600
600
#endif
601
- // Disable reload mode
602
- handle -> Instance -> CR2 &= (uint32_t )~I2C_CR2_RELOAD ;
603
601
604
602
// Ensure the transmission is started before sending a stop
605
603
if ((handle -> Instance -> CR2 & (uint32_t )I2C_CR2_RD_WRN ) == 0 ) {
@@ -612,7 +610,7 @@ int i2c_stop(i2c_t *obj)
612
610
}
613
611
614
612
// Generate the STOP condition
615
- handle -> Instance -> CR2 | = I2C_CR2_STOP ;
613
+ handle -> Instance -> CR2 = I2C_CR2_STOP ;
616
614
617
615
timeout = FLAG_TIMEOUT ;
618
616
while (!__HAL_I2C_GET_FLAG (handle , I2C_FLAG_STOPF )) {
@@ -665,9 +663,16 @@ int i2c_byte_read(i2c_t *obj, int last)
665
663
}
666
664
}
667
665
668
- /* Enable reload mode as we don't know how many bytes will be sent */
669
- /* and set transfer size to 1 */
670
- tmpreg |= I2C_CR2_RELOAD | (I2C_CR2_NBYTES & (1 << 16 ));
666
+ if (last ) {
667
+ /* Disable Address Acknowledge */
668
+ tmpreg = tmpreg & (~I2C_CR2_RELOAD );
669
+ tmpreg |= I2C_CR2_NACK | (I2C_CR2_NBYTES & (1 << 16 ));
670
+ } else {
671
+ /* Enable reload mode as we don't know how many bytes will be sent */
672
+ /* and set transfer size to 1 */
673
+ tmpreg |= I2C_CR2_RELOAD | (I2C_CR2_NBYTES & (1 << 16 ));
674
+ }
675
+
671
676
/* Set the prepared configuration */
672
677
handle -> Instance -> CR2 = tmpreg ;
673
678
@@ -681,11 +686,6 @@ int i2c_byte_read(i2c_t *obj, int last)
681
686
/* Then Get Byte */
682
687
data = handle -> Instance -> RXDR ;
683
688
684
- if (last ) {
685
- /* Disable Address Acknowledge */
686
- handle -> Instance -> CR2 |= I2C_CR2_NACK ;
687
- }
688
-
689
689
return data ;
690
690
}
691
691
0 commit comments