Skip to content

Commit cf1ea9c

Browse files
LMESTMadbridge
authored andcommitted
STM32 I2C: differentiate HW reset and driver reset
Make a distinct i2c_reset function as defined in MBED HAL api, from the i2C_hw_reset which simply drives the HW reset signals
1 parent aec4f2d commit cf1ea9c

File tree

1 file changed

+51
-44
lines changed

1 file changed

+51
-44
lines changed

targets/TARGET_STM/i2c_api.c

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,48 @@ uint32_t i2c_get_irq_handler(i2c_t *obj)
198198
return handler;
199199
}
200200

201+
void i2c_hw_reset(i2c_t *obj) {
202+
int timeout;
203+
struct i2c_s *obj_s = I2C_S(obj);
204+
I2C_HandleTypeDef *handle = &(obj_s->handle);
205+
206+
handle->Instance = (I2C_TypeDef *)(obj_s->i2c);
207+
208+
// wait before reset
209+
timeout = BYTE_TIMEOUT;
210+
while ((__HAL_I2C_GET_FLAG(handle, I2C_FLAG_BUSY)) && (--timeout != 0));
211+
#if defined I2C1_BASE
212+
if (obj_s->i2c == I2C_1) {
213+
__HAL_RCC_I2C1_FORCE_RESET();
214+
__HAL_RCC_I2C1_RELEASE_RESET();
215+
}
216+
#endif
217+
#if defined I2C2_BASE
218+
if (obj_s->i2c == I2C_2) {
219+
__HAL_RCC_I2C2_FORCE_RESET();
220+
__HAL_RCC_I2C2_RELEASE_RESET();
221+
}
222+
#endif
223+
#if defined I2C3_BASE
224+
if (obj_s->i2c == I2C_3) {
225+
__HAL_RCC_I2C3_FORCE_RESET();
226+
__HAL_RCC_I2C3_RELEASE_RESET();
227+
}
228+
#endif
229+
#if defined I2C4_BASE
230+
if (obj_s->i2c == I2C_4) {
231+
__HAL_RCC_I2C4_FORCE_RESET();
232+
__HAL_RCC_I2C4_RELEASE_RESET();
233+
}
234+
#endif
235+
#if defined FMPI2C1_BASE
236+
if (obj_s->i2c == FMPI2C_1) {
237+
__HAL_RCC_FMPI2C1_FORCE_RESET();
238+
__HAL_RCC_FMPI2C1_RELEASE_RESET();
239+
}
240+
#endif
241+
}
242+
201243
void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
202244

203245
struct i2c_s *obj_s = I2C_S(obj);
@@ -288,7 +330,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
288330
obj_s->hz = 100000; // 100 kHz per default
289331

290332
// Reset to clear pending flags if any
291-
i2c_reset(obj);
333+
i2c_hw_reset(obj);
292334
i2c_frequency(obj, obj_s->hz );
293335

294336
#if DEVICE_I2CSLAVE
@@ -400,49 +442,6 @@ i2c_t *get_i2c_obj(I2C_HandleTypeDef *hi2c){
400442
return (obj);
401443
}
402444

403-
void i2c_reset(i2c_t *obj) {
404-
405-
int timeout;
406-
struct i2c_s *obj_s = I2C_S(obj);
407-
I2C_HandleTypeDef *handle = &(obj_s->handle);
408-
409-
handle->Instance = (I2C_TypeDef *)(obj_s->i2c);
410-
411-
// wait before reset
412-
timeout = BYTE_TIMEOUT;
413-
while ((__HAL_I2C_GET_FLAG(handle, I2C_FLAG_BUSY)) && (--timeout != 0));
414-
#if defined I2C1_BASE
415-
if (obj_s->i2c == I2C_1) {
416-
__HAL_RCC_I2C1_FORCE_RESET();
417-
__HAL_RCC_I2C1_RELEASE_RESET();
418-
}
419-
#endif
420-
#if defined I2C2_BASE
421-
if (obj_s->i2c == I2C_2) {
422-
__HAL_RCC_I2C2_FORCE_RESET();
423-
__HAL_RCC_I2C2_RELEASE_RESET();
424-
}
425-
#endif
426-
#if defined I2C3_BASE
427-
if (obj_s->i2c == I2C_3) {
428-
__HAL_RCC_I2C3_FORCE_RESET();
429-
__HAL_RCC_I2C3_RELEASE_RESET();
430-
}
431-
#endif
432-
#if defined I2C4_BASE
433-
if (obj_s->i2c == I2C_4) {
434-
__HAL_RCC_I2C4_FORCE_RESET();
435-
__HAL_RCC_I2C4_RELEASE_RESET();
436-
}
437-
#endif
438-
#if defined FMPI2C1_BASE
439-
if (obj_s->i2c == FMPI2C_1) {
440-
__HAL_RCC_FMPI2C1_FORCE_RESET();
441-
__HAL_RCC_FMPI2C1_RELEASE_RESET();
442-
}
443-
#endif
444-
}
445-
446445
/* SYNCHRONOUS API FUNCTIONS */
447446

448447
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
@@ -675,6 +674,14 @@ int i2c_byte_write(i2c_t *obj, int data) {
675674
}
676675
#endif //I2C_IP_VERSION_V2
677676

677+
void i2c_reset(i2c_t *obj) {
678+
struct i2c_s *obj_s = I2C_S(obj);
679+
/* As recommended in i2c_api.h, mainly send stop */
680+
i2c_stop(obj);
681+
/* then re-init */
682+
i2c_init(obj, obj_s->sda, obj_s->scl);
683+
}
684+
678685
/*
679686
* SYNC APIS
680687
*/

0 commit comments

Comments
 (0)