40
40
#include "pinmap.h"
41
41
#include "PeripheralPins.h"
42
42
#include "i2c_device.h" // family specific defines
43
+ #include "mbed_error.h"
43
44
44
45
#ifndef DEBUG_STDIO
45
46
# define DEBUG_STDIO 0
@@ -597,8 +598,6 @@ int i2c_stop(i2c_t *obj)
597
598
return 0 ;
598
599
}
599
600
#endif
600
- // Disable reload mode
601
- handle -> Instance -> CR2 &= (uint32_t )~I2C_CR2_RELOAD ;
602
601
603
602
// Ensure the transmission is started before sending a stop
604
603
if ((handle -> Instance -> CR2 & (uint32_t )I2C_CR2_RD_WRN ) == 0 ) {
@@ -611,7 +610,7 @@ int i2c_stop(i2c_t *obj)
611
610
}
612
611
613
612
// Generate the STOP condition
614
- handle -> Instance -> CR2 | = I2C_CR2_STOP ;
613
+ handle -> Instance -> CR2 = I2C_CR2_STOP ;
615
614
616
615
timeout = FLAG_TIMEOUT ;
617
616
while (!__HAL_I2C_GET_FLAG (handle , I2C_FLAG_STOPF )) {
@@ -664,9 +663,16 @@ int i2c_byte_read(i2c_t *obj, int last)
664
663
}
665
664
}
666
665
667
- /* Enable reload mode as we don't know how many bytes will be sent */
668
- /* and set transfer size to 1 */
669
- 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
+
670
676
/* Set the prepared configuration */
671
677
handle -> Instance -> CR2 = tmpreg ;
672
678
@@ -680,11 +686,6 @@ int i2c_byte_read(i2c_t *obj, int last)
680
686
/* Then Get Byte */
681
687
data = handle -> Instance -> RXDR ;
682
688
683
- if (last ) {
684
- /* Disable Address Acknowledge */
685
- handle -> Instance -> CR2 |= I2C_CR2_NACK ;
686
- }
687
-
688
689
return data ;
689
690
}
690
691
@@ -760,7 +761,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
760
761
I2C_HandleTypeDef * handle = & (obj_s -> handle );
761
762
int count = I2C_ERROR_BUS_BUSY , ret = 0 ;
762
763
uint32_t timeout = 0 ;
763
-
764
+ #if defined( I2C_IP_VERSION_V1 )
764
765
// Trick to remove compiler warning "left and right operands are identical" in some cases
765
766
uint32_t op1 = I2C_FIRST_AND_LAST_FRAME ;
766
767
uint32_t op2 = I2C_LAST_FRAME ;
@@ -778,6 +779,18 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
778
779
obj_s -> XferOperation = I2C_NEXT_FRAME ;
779
780
}
780
781
}
782
+ #elif defined(I2C_IP_VERSION_V2 )
783
+ if ((obj_s -> XferOperation == I2C_FIRST_FRAME ) || (obj_s -> XferOperation == I2C_FIRST_AND_LAST_FRAME ) || (obj_s -> XferOperation == I2C_LAST_FRAME )) {
784
+ if (stop ) {
785
+ obj_s -> XferOperation = I2C_FIRST_AND_LAST_FRAME ;
786
+ } else {
787
+ obj_s -> XferOperation = I2C_FIRST_FRAME ;
788
+ }
789
+ } else {
790
+ // should not happend
791
+ error ("I2C: abnormal case should not happend" );
792
+ }
793
+ #endif
781
794
782
795
obj_s -> event = 0 ;
783
796
@@ -818,6 +831,7 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
818
831
int count = I2C_ERROR_BUS_BUSY , ret = 0 ;
819
832
uint32_t timeout = 0 ;
820
833
834
+ #if defined(I2C_IP_VERSION_V1 )
821
835
// Trick to remove compiler warning "left and right operands are identical" in some cases
822
836
uint32_t op1 = I2C_FIRST_AND_LAST_FRAME ;
823
837
uint32_t op2 = I2C_LAST_FRAME ;
@@ -835,6 +849,18 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
835
849
obj_s -> XferOperation = I2C_NEXT_FRAME ;
836
850
}
837
851
}
852
+ #elif defined(I2C_IP_VERSION_V2 )
853
+ if ((obj_s -> XferOperation == I2C_FIRST_FRAME ) || (obj_s -> XferOperation == I2C_FIRST_AND_LAST_FRAME ) || (obj_s -> XferOperation == I2C_LAST_FRAME )) {
854
+ if (stop ) {
855
+ obj_s -> XferOperation = I2C_FIRST_AND_LAST_FRAME ;
856
+ } else {
857
+ obj_s -> XferOperation = I2C_FIRST_FRAME ;
858
+ }
859
+ } else {
860
+ // should not happend
861
+ error ("I2C: abnormal case should not happend" );
862
+ }
863
+ #endif
838
864
839
865
obj_s -> event = 0 ;
840
866
@@ -874,11 +900,19 @@ void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c)
874
900
#if DEVICE_I2C_ASYNCH
875
901
/* Handle potential Tx/Rx use case */
876
902
if ((obj -> tx_buff .length ) && (obj -> rx_buff .length )) {
903
+ #if defined(I2C_IP_VERSION_V1 )
877
904
if (obj_s -> stop ) {
878
905
obj_s -> XferOperation = I2C_LAST_FRAME ;
879
906
} else {
880
907
obj_s -> XferOperation = I2C_NEXT_FRAME ;
881
908
}
909
+ #elif defined(I2C_IP_VERSION_V2 )
910
+ if (obj_s -> stop ) {
911
+ obj_s -> XferOperation = I2C_FIRST_AND_LAST_FRAME ;
912
+ } else {
913
+ obj_s -> XferOperation = I2C_FIRST_FRAME ;
914
+ }
915
+ #endif
882
916
883
917
HAL_I2C_Master_Sequential_Receive_IT (hi2c , obj_s -> address , (uint8_t * )obj -> rx_buff .buffer , obj -> rx_buff .length , obj_s -> XferOperation );
884
918
} else
@@ -1143,6 +1177,7 @@ void i2c_transfer_asynch(i2c_t *obj, const void *tx, size_t tx_length, void *rx,
1143
1177
1144
1178
/* Set operation step depending if stop sending required or not */
1145
1179
if ((tx_length && !rx_length ) || (!tx_length && rx_length )) {
1180
+ #if defined(I2C_IP_VERSION_V1 )
1146
1181
// Trick to remove compiler warning "left and right operands are identical" in some cases
1147
1182
uint32_t op1 = I2C_FIRST_AND_LAST_FRAME ;
1148
1183
uint32_t op2 = I2C_LAST_FRAME ;
@@ -1160,7 +1195,18 @@ void i2c_transfer_asynch(i2c_t *obj, const void *tx, size_t tx_length, void *rx,
1160
1195
obj_s -> XferOperation = I2C_NEXT_FRAME ;
1161
1196
}
1162
1197
}
1163
-
1198
+ #elif defined(I2C_IP_VERSION_V2 )
1199
+ if ((obj_s -> XferOperation == I2C_FIRST_FRAME ) || (obj_s -> XferOperation == I2C_FIRST_AND_LAST_FRAME ) || (obj_s -> XferOperation == I2C_LAST_FRAME )) {
1200
+ if (stop ) {
1201
+ obj_s -> XferOperation = I2C_FIRST_AND_LAST_FRAME ;
1202
+ } else {
1203
+ obj_s -> XferOperation = I2C_FIRST_FRAME ;
1204
+ }
1205
+ } else {
1206
+ // should not happend
1207
+ error ("I2C: abnormal case should not happend" );
1208
+ }
1209
+ #endif
1164
1210
if (tx_length > 0 ) {
1165
1211
HAL_I2C_Master_Sequential_Transmit_IT (handle , address , (uint8_t * )tx , tx_length , obj_s -> XferOperation );
1166
1212
}
@@ -1169,6 +1215,7 @@ void i2c_transfer_asynch(i2c_t *obj, const void *tx, size_t tx_length, void *rx,
1169
1215
}
1170
1216
} else if (tx_length && rx_length ) {
1171
1217
/* Two steps operation, don't modify XferOperation, keep it for next step */
1218
+ #if defined(I2C_IP_VERSION_V1 )
1172
1219
// Trick to remove compiler warning "left and right operands are identical" in some cases
1173
1220
uint32_t op1 = I2C_FIRST_AND_LAST_FRAME ;
1174
1221
uint32_t op2 = I2C_LAST_FRAME ;
@@ -1178,6 +1225,9 @@ void i2c_transfer_asynch(i2c_t *obj, const void *tx, size_t tx_length, void *rx,
1178
1225
(obj_s -> XferOperation == I2C_NEXT_FRAME )) {
1179
1226
HAL_I2C_Master_Sequential_Transmit_IT (handle , address , (uint8_t * )tx , tx_length , I2C_NEXT_FRAME );
1180
1227
}
1228
+ #elif defined(I2C_IP_VERSION_V2 )
1229
+ HAL_I2C_Master_Sequential_Transmit_IT (handle , address , (uint8_t * )tx , tx_length , I2C_FIRST_FRAME );
1230
+ #endif
1181
1231
}
1182
1232
}
1183
1233
0 commit comments