Skip to content

Commit 897d15a

Browse files
authored
Merge pull request #9895 from mrcoulter45/issue-9890-fix
Fix for i2c_t object not being initialized to 0 causing timeout
2 parents 17f8bc1 + c59c1cb commit 897d15a

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

targets/TARGET_STM/i2c_api.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ static I2C_HandleTypeDef *i2c_handles[I2C_NUM];
7979
*/
8080
#define FLAG_TIMEOUT ((int)0x1000)
8181

82+
/* Declare i2c_init_internal to be used in this file */
83+
void i2c_init_internal(i2c_t *obj, PinName sda, PinName scl);
84+
8285
/* GENERIC INIT and HELPERS FUNCTIONS */
8386

8487
#if defined(I2C1_BASE)
@@ -260,7 +263,12 @@ void i2c_sw_reset(i2c_t *obj)
260263

261264
void i2c_init(i2c_t *obj, PinName sda, PinName scl)
262265
{
266+
memset(obj, 0, sizeof(*obj));
267+
i2c_init_internal(obj, sda, scl);
268+
}
263269

270+
void i2c_init_internal(i2c_t *obj, PinName sda, PinName scl)
271+
{
264272
struct i2c_s *obj_s = I2C_S(obj);
265273

266274
// Determine the I2C to use
@@ -454,7 +462,7 @@ void i2c_reset(i2c_t *obj)
454462
/* As recommended in i2c_api.h, mainly send stop */
455463
i2c_stop(obj);
456464
/* then re-init */
457-
i2c_init(obj, obj_s->sda, obj_s->scl);
465+
i2c_init_internal(obj, obj_s->sda, obj_s->scl);
458466
}
459467

460468
/*
@@ -508,7 +516,7 @@ int i2c_stop(i2c_t *obj)
508516
* re-init HAL state
509517
*/
510518
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);
512520
}
513521

514522
return 0;
@@ -584,7 +592,7 @@ int i2c_stop(i2c_t *obj)
584592
#if DEVICE_I2CSLAVE
585593
if (obj_s->slave) {
586594
/* 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);
588596
return 0;
589597
}
590598
#endif
@@ -627,7 +635,7 @@ int i2c_stop(i2c_t *obj)
627635
/* In case of mixed usage of the APIs (unitary + SYNC)
628636
* re-init HAL state */
629637
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);
631639
}
632640

633641
return 0;
@@ -791,7 +799,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
791799
if ((timeout == 0) || (obj_s->event != I2C_EVENT_TRANSFER_COMPLETE)) {
792800
DEBUG_PRINTF(" TIMEOUT or error in i2c_read\r\n");
793801
/* 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);
795803
} else {
796804
count = length;
797805
}
@@ -845,7 +853,7 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
845853
if ((timeout == 0) || (obj_s->event != I2C_EVENT_TRANSFER_COMPLETE)) {
846854
DEBUG_PRINTF(" TIMEOUT or error in i2c_write\r\n");
847855
/* 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);
849857
} else {
850858
count = length;
851859
}
@@ -907,7 +915,7 @@ void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
907915
DEBUG_PRINTF("HAL_I2C_ErrorCallback:%d, index=%d\r\n", (int) hi2c->ErrorCode, obj_s->index);
908916

909917
/* 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);
911919

912920
#if DEVICE_I2CSLAVE
913921
/* restore slave address */

0 commit comments

Comments
 (0)