Skip to content

Commit 19acef9

Browse files
authored
Merge pull request #3429 from LMESTM/fix_stm_i2c_fix_init
Fix stm i2c fix init
2 parents 45fa92d + 36a0365 commit 19acef9

File tree

1 file changed

+59
-51
lines changed

1 file changed

+59
-51
lines changed

targets/TARGET_STM/i2c_api.c

Lines changed: 59 additions & 51 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);
@@ -215,79 +257,80 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
215257
// Enable I2C1 clock and pinout if not done
216258
if (obj_s->i2c == I2C_1) {
217259
obj_s->index = 0;
260+
__HAL_RCC_I2C1_CLK_ENABLE();
218261
// Configure I2C pins
219262
pinmap_pinout(sda, PinMap_I2C_SDA);
220263
pinmap_pinout(scl, PinMap_I2C_SCL);
221264
pin_mode(sda, PullUp);
222265
pin_mode(scl, PullUp);
223266
obj_s->event_i2cIRQ = I2C1_EV_IRQn;
224267
obj_s->error_i2cIRQ = I2C1_ER_IRQn;
225-
__HAL_RCC_I2C1_CLK_ENABLE();
226268
}
227269
#endif
228270
#if defined I2C2_BASE
229271
// Enable I2C2 clock and pinout if not done
230272
if (obj_s->i2c == I2C_2) {
231273
obj_s->index = 1;
274+
__HAL_RCC_I2C2_CLK_ENABLE();
232275
// Configure I2C pins
233276
pinmap_pinout(sda, PinMap_I2C_SDA);
234277
pinmap_pinout(scl, PinMap_I2C_SCL);
235278
pin_mode(sda, PullUp);
236279
pin_mode(scl, PullUp);
237280
obj_s->event_i2cIRQ = I2C2_EV_IRQn;
238281
obj_s->error_i2cIRQ = I2C2_ER_IRQn;
239-
__HAL_RCC_I2C2_CLK_ENABLE();
240282
}
241283
#endif
242284
#if defined I2C3_BASE
243285
// Enable I2C3 clock and pinout if not done
244286
if (obj_s->i2c == I2C_3) {
245287
obj_s->index = 2;
288+
__HAL_RCC_I2C3_CLK_ENABLE();
246289
// Configure I2C pins
247290
pinmap_pinout(sda, PinMap_I2C_SDA);
248291
pinmap_pinout(scl, PinMap_I2C_SCL);
249292
pin_mode(sda, PullUp);
250293
pin_mode(scl, PullUp);
251294
obj_s->event_i2cIRQ = I2C3_EV_IRQn;
252295
obj_s->error_i2cIRQ = I2C3_ER_IRQn;
253-
__HAL_RCC_I2C3_CLK_ENABLE();
254296
}
255297
#endif
256298
#if defined I2C4_BASE
257299
// Enable I2C3 clock and pinout if not done
258300
if (obj_s->i2c == I2C_4) {
259301
obj_s->index = 3;
302+
__HAL_RCC_I2C4_CLK_ENABLE();
260303
// Configure I2C pins
261304
pinmap_pinout(sda, PinMap_I2C_SDA);
262305
pinmap_pinout(scl, PinMap_I2C_SCL);
263306
pin_mode(sda, PullUp);
264307
pin_mode(scl, PullUp);
265308
obj_s->event_i2cIRQ = I2C4_EV_IRQn;
266309
obj_s->error_i2cIRQ = I2C4_ER_IRQn;
267-
__HAL_RCC_I2C4_CLK_ENABLE();
268310
}
269311
#endif
270312
#if defined FMPI2C1_BASE
271313
// Enable I2C3 clock and pinout if not done
272314
if (obj_s->i2c == FMPI2C_1) {
273315
obj_s->index = 4;
316+
__HAL_RCC_FMPI2C1_CLK_ENABLE();
274317
// Configure I2C pins
275318
pinmap_pinout(sda, PinMap_I2C_SDA);
276319
pinmap_pinout(scl, PinMap_I2C_SCL);
277320
pin_mode(sda, PullUp);
278321
pin_mode(scl, PullUp);
279322
obj_s->event_i2cIRQ = FMPI2C1_EV_IRQn;
280323
obj_s->error_i2cIRQ = FMPI2C1_ER_IRQn;
281-
__HAL_RCC_FMPI2C1_CLK_ENABLE();
282324
}
283325
#endif
284326

285-
// Reset to clear pending flags if any
286-
i2c_reset(obj);
287-
288327
// I2C configuration
328+
// Default hz value used for timeout computation
289329
if(!obj_s->hz)
290330
obj_s->hz = 100000; // 100 kHz per default
331+
332+
// Reset to clear pending flags if any
333+
i2c_hw_reset(obj);
291334
i2c_frequency(obj, obj_s->hz );
292335

293336
#if DEVICE_I2CSLAVE
@@ -399,49 +442,6 @@ i2c_t *get_i2c_obj(I2C_HandleTypeDef *hi2c){
399442
return (obj);
400443
}
401444

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

447447
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
@@ -674,6 +674,14 @@ int i2c_byte_write(i2c_t *obj, int data) {
674674
}
675675
#endif //I2C_IP_VERSION_V2
676676

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+
677685
/*
678686
* SYNC APIS
679687
*/

0 commit comments

Comments
 (0)