Skip to content

Commit ec95aa5

Browse files
committed
STM32: I2C: Reset I2C in case of errors to recover
This is to avoid an IP / bus deadlock. This requires to store scl and sda in order to call the init function.
1 parent 42d89b0 commit ec95aa5

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

targets/TARGET_STM/TARGET_STM32F4/common_objects.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ struct i2c_s {
9191
I2CName i2c;
9292
I2C_HandleTypeDef handle;
9393
uint8_t index;
94+
PinName sda;
95+
PinName scl;
9496
IRQn_Type event_i2cIRQ;
9597
IRQn_Type error_i2cIRQ;
9698
volatile uint8_t event;

targets/TARGET_STM/TARGET_STM32F4/i2c_api.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
174174
// Determine the I2C to use
175175
I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
176176
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
177+
obj_s->sda = sda;
178+
obj_s->scl = scl;
177179

178180
obj_s->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
179181
MBED_ASSERT(obj_s->i2c != (I2CName)NC);
@@ -710,12 +712,11 @@ void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c){
710712
/* Get object ptr based on handler ptr */
711713
i2c_t *obj = get_i2c_obj(hi2c);
712714
struct i2c_s *obj_s = I2C_S(obj);
713-
I2C_HandleTypeDef *handle = &(obj_s->handle);
714715

715-
/* Disable IT. Not always done before calling macro */
716-
__HAL_I2C_DISABLE_IT(handle, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
716+
/* re-init IP to try and get back in a working state */
717+
i2c_init(obj, obj_s->sda, obj_s->scl);
717718

718-
/* Set event flag */
719+
/* Keep Set event flag */
719720
obj_s->event = I2C_EVENT_ERROR;
720721
}
721722

0 commit comments

Comments
 (0)