@@ -79,6 +79,9 @@ static I2C_HandleTypeDef *i2c_handles[I2C_NUM];
79
79
*/
80
80
#define FLAG_TIMEOUT ((int)0x1000)
81
81
82
+ /* Declare i2c_init_internal to be used in this file */
83
+ void i2c_init_internal (i2c_t * obj , PinName sda , PinName scl );
84
+
82
85
/* GENERIC INIT and HELPERS FUNCTIONS */
83
86
84
87
#if defined(I2C1_BASE )
@@ -260,7 +263,12 @@ void i2c_sw_reset(i2c_t *obj)
260
263
261
264
void i2c_init (i2c_t * obj , PinName sda , PinName scl )
262
265
{
266
+ memset (obj , 0 , sizeof (* obj ));
267
+ i2c_init_internal (obj , sda , scl );
268
+ }
263
269
270
+ void i2c_init_internal (i2c_t * obj , PinName sda , PinName scl )
271
+ {
264
272
struct i2c_s * obj_s = I2C_S (obj );
265
273
266
274
// Determine the I2C to use
@@ -454,7 +462,7 @@ void i2c_reset(i2c_t *obj)
454
462
/* As recommended in i2c_api.h, mainly send stop */
455
463
i2c_stop (obj );
456
464
/* then re-init */
457
- i2c_init (obj , obj_s -> sda , obj_s -> scl );
465
+ i2c_init_internal (obj , obj_s -> sda , obj_s -> scl );
458
466
}
459
467
460
468
/*
@@ -508,7 +516,7 @@ int i2c_stop(i2c_t *obj)
508
516
* re-init HAL state
509
517
*/
510
518
if (obj_s -> XferOperation != I2C_FIRST_AND_LAST_FRAME ) {
511
- i2c_init (obj , obj_s -> sda , obj_s -> scl );
519
+ i2c_init_internal (obj , obj_s -> sda , obj_s -> scl );
512
520
}
513
521
514
522
return 0 ;
@@ -584,7 +592,7 @@ int i2c_stop(i2c_t *obj)
584
592
#if DEVICE_I2CSLAVE
585
593
if (obj_s -> slave ) {
586
594
/* re-init slave when stop is requested */
587
- i2c_init (obj , obj_s -> sda , obj_s -> scl );
595
+ i2c_init_internal (obj , obj_s -> sda , obj_s -> scl );
588
596
return 0 ;
589
597
}
590
598
#endif
@@ -627,7 +635,7 @@ int i2c_stop(i2c_t *obj)
627
635
/* In case of mixed usage of the APIs (unitary + SYNC)
628
636
* re-init HAL state */
629
637
if (obj_s -> XferOperation != I2C_FIRST_AND_LAST_FRAME ) {
630
- i2c_init (obj , obj_s -> sda , obj_s -> scl );
638
+ i2c_init_internal (obj , obj_s -> sda , obj_s -> scl );
631
639
}
632
640
633
641
return 0 ;
@@ -791,7 +799,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
791
799
if ((timeout == 0 ) || (obj_s -> event != I2C_EVENT_TRANSFER_COMPLETE )) {
792
800
DEBUG_PRINTF (" TIMEOUT or error in i2c_read\r\n" );
793
801
/* re-init IP to try and get back in a working state */
794
- i2c_init (obj , obj_s -> sda , obj_s -> scl );
802
+ i2c_init_internal (obj , obj_s -> sda , obj_s -> scl );
795
803
} else {
796
804
count = length ;
797
805
}
@@ -845,7 +853,7 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
845
853
if ((timeout == 0 ) || (obj_s -> event != I2C_EVENT_TRANSFER_COMPLETE )) {
846
854
DEBUG_PRINTF (" TIMEOUT or error in i2c_write\r\n" );
847
855
/* re-init IP to try and get back in a working state */
848
- i2c_init (obj , obj_s -> sda , obj_s -> scl );
856
+ i2c_init_internal (obj , obj_s -> sda , obj_s -> scl );
849
857
} else {
850
858
count = length ;
851
859
}
@@ -907,7 +915,7 @@ void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
907
915
DEBUG_PRINTF ("HAL_I2C_ErrorCallback:%d, index=%d\r\n" , (int ) hi2c -> ErrorCode , obj_s -> index );
908
916
909
917
/* re-init IP to try and get back in a working state */
910
- i2c_init (obj , obj_s -> sda , obj_s -> scl );
918
+ i2c_init_internal (obj , obj_s -> sda , obj_s -> scl );
911
919
912
920
#if DEVICE_I2CSLAVE
913
921
/* restore slave address */
0 commit comments