@@ -866,17 +866,26 @@ static inline int msg_done(const struct device *dev,
866
866
{
867
867
const struct i2c_stm32_config * cfg = dev -> config ;
868
868
I2C_TypeDef * i2c = cfg -> i2c ;
869
+ int64_t start_time = k_uptime_get ();
869
870
870
871
/* Wait for transfer to complete */
871
872
while (!LL_I2C_IsActiveFlag_TC (i2c ) && !LL_I2C_IsActiveFlag_TCR (i2c )) {
872
873
if (check_errors (dev , __func__ )) {
873
874
return - EIO ;
874
875
}
876
+ if ((k_uptime_get () - start_time ) >
877
+ STM32_I2C_TRANSFER_TIMEOUT_MSEC ) {
878
+ return - ETIMEDOUT ;
879
+ }
875
880
}
876
881
/* Issue stop condition if necessary */
877
882
if (current_msg_flags & I2C_MSG_STOP ) {
878
883
LL_I2C_GenerateStopCondition (i2c );
879
884
while (!LL_I2C_IsActiveFlag_STOP (i2c )) {
885
+ if ((k_uptime_get () - start_time ) >
886
+ STM32_I2C_TRANSFER_TIMEOUT_MSEC ) {
887
+ return - ETIMEDOUT ;
888
+ }
880
889
}
881
890
882
891
LL_I2C_ClearFlag_STOP (i2c );
@@ -893,6 +902,7 @@ static int i2c_stm32_msg_write(const struct device *dev, struct i2c_msg *msg,
893
902
I2C_TypeDef * i2c = cfg -> i2c ;
894
903
unsigned int len = 0U ;
895
904
uint8_t * buf = msg -> buf ;
905
+ int64_t start_time = k_uptime_get ();
896
906
897
907
msg_init (dev , msg , next_msg_flags , slave , LL_I2C_REQUEST_WRITE );
898
908
@@ -906,6 +916,11 @@ static int i2c_stm32_msg_write(const struct device *dev, struct i2c_msg *msg,
906
916
if (check_errors (dev , __func__ )) {
907
917
return - EIO ;
908
918
}
919
+
920
+ if ((k_uptime_get () - start_time ) >
921
+ STM32_I2C_TRANSFER_TIMEOUT_MSEC ) {
922
+ return - ETIMEDOUT ;
923
+ }
909
924
}
910
925
911
926
LL_I2C_TransmitData8 (i2c , * buf );
@@ -923,6 +938,7 @@ static int i2c_stm32_msg_read(const struct device *dev, struct i2c_msg *msg,
923
938
I2C_TypeDef * i2c = cfg -> i2c ;
924
939
unsigned int len = 0U ;
925
940
uint8_t * buf = msg -> buf ;
941
+ int64_t start_time = k_uptime_get ();
926
942
927
943
msg_init (dev , msg , next_msg_flags , slave , LL_I2C_REQUEST_READ );
928
944
@@ -932,6 +948,10 @@ static int i2c_stm32_msg_read(const struct device *dev, struct i2c_msg *msg,
932
948
if (check_errors (dev , __func__ )) {
933
949
return - EIO ;
934
950
}
951
+ if ((k_uptime_get () - start_time ) >
952
+ STM32_I2C_TRANSFER_TIMEOUT_MSEC ) {
953
+ return - ETIMEDOUT ;
954
+ }
935
955
}
936
956
937
957
* buf = LL_I2C_ReceiveData8 (i2c );
@@ -1302,5 +1322,26 @@ int i2c_stm32_transaction(const struct device *dev,
1302
1322
msg .len = rest ;
1303
1323
} while (rest > 0U );
1304
1324
1325
+ #ifndef CONFIG_I2C_STM32_INTERRUPT
1326
+ struct i2c_stm32_data * data = dev -> data ;
1327
+
1328
+ if (ret == - ETIMEDOUT ) {
1329
+ if (LL_I2C_IsEnabledReloadMode (i2c )) {
1330
+ LL_I2C_DisableReloadMode (i2c );
1331
+ }
1332
+ #if defined(CONFIG_I2C_TARGET )
1333
+ data -> master_active = false;
1334
+ if (!data -> slave_attached && !data -> smbalert_active ) {
1335
+ LL_I2C_Disable (i2c );
1336
+ }
1337
+ #else
1338
+ if (!data -> smbalert_active ) {
1339
+ LL_I2C_Disable (i2c );
1340
+ }
1341
+ #endif
1342
+ return - EIO ;
1343
+ }
1344
+ #endif /* !CONFIG_I2C_STM32_INTERRUPT */
1345
+
1305
1346
return ret ;
1306
1347
}
0 commit comments