@@ -198,6 +198,48 @@ uint32_t i2c_get_irq_handler(i2c_t *obj)
198
198
return handler ;
199
199
}
200
200
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
+
201
243
void i2c_init (i2c_t * obj , PinName sda , PinName scl ) {
202
244
203
245
struct i2c_s * obj_s = I2C_S (obj );
@@ -215,79 +257,80 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
215
257
// Enable I2C1 clock and pinout if not done
216
258
if (obj_s -> i2c == I2C_1 ) {
217
259
obj_s -> index = 0 ;
260
+ __HAL_RCC_I2C1_CLK_ENABLE ();
218
261
// Configure I2C pins
219
262
pinmap_pinout (sda , PinMap_I2C_SDA );
220
263
pinmap_pinout (scl , PinMap_I2C_SCL );
221
264
pin_mode (sda , PullUp );
222
265
pin_mode (scl , PullUp );
223
266
obj_s -> event_i2cIRQ = I2C1_EV_IRQn ;
224
267
obj_s -> error_i2cIRQ = I2C1_ER_IRQn ;
225
- __HAL_RCC_I2C1_CLK_ENABLE ();
226
268
}
227
269
#endif
228
270
#if defined I2C2_BASE
229
271
// Enable I2C2 clock and pinout if not done
230
272
if (obj_s -> i2c == I2C_2 ) {
231
273
obj_s -> index = 1 ;
274
+ __HAL_RCC_I2C2_CLK_ENABLE ();
232
275
// Configure I2C pins
233
276
pinmap_pinout (sda , PinMap_I2C_SDA );
234
277
pinmap_pinout (scl , PinMap_I2C_SCL );
235
278
pin_mode (sda , PullUp );
236
279
pin_mode (scl , PullUp );
237
280
obj_s -> event_i2cIRQ = I2C2_EV_IRQn ;
238
281
obj_s -> error_i2cIRQ = I2C2_ER_IRQn ;
239
- __HAL_RCC_I2C2_CLK_ENABLE ();
240
282
}
241
283
#endif
242
284
#if defined I2C3_BASE
243
285
// Enable I2C3 clock and pinout if not done
244
286
if (obj_s -> i2c == I2C_3 ) {
245
287
obj_s -> index = 2 ;
288
+ __HAL_RCC_I2C3_CLK_ENABLE ();
246
289
// Configure I2C pins
247
290
pinmap_pinout (sda , PinMap_I2C_SDA );
248
291
pinmap_pinout (scl , PinMap_I2C_SCL );
249
292
pin_mode (sda , PullUp );
250
293
pin_mode (scl , PullUp );
251
294
obj_s -> event_i2cIRQ = I2C3_EV_IRQn ;
252
295
obj_s -> error_i2cIRQ = I2C3_ER_IRQn ;
253
- __HAL_RCC_I2C3_CLK_ENABLE ();
254
296
}
255
297
#endif
256
298
#if defined I2C4_BASE
257
299
// Enable I2C3 clock and pinout if not done
258
300
if (obj_s -> i2c == I2C_4 ) {
259
301
obj_s -> index = 3 ;
302
+ __HAL_RCC_I2C4_CLK_ENABLE ();
260
303
// Configure I2C pins
261
304
pinmap_pinout (sda , PinMap_I2C_SDA );
262
305
pinmap_pinout (scl , PinMap_I2C_SCL );
263
306
pin_mode (sda , PullUp );
264
307
pin_mode (scl , PullUp );
265
308
obj_s -> event_i2cIRQ = I2C4_EV_IRQn ;
266
309
obj_s -> error_i2cIRQ = I2C4_ER_IRQn ;
267
- __HAL_RCC_I2C4_CLK_ENABLE ();
268
310
}
269
311
#endif
270
312
#if defined FMPI2C1_BASE
271
313
// Enable I2C3 clock and pinout if not done
272
314
if (obj_s -> i2c == FMPI2C_1 ) {
273
315
obj_s -> index = 4 ;
316
+ __HAL_RCC_FMPI2C1_CLK_ENABLE ();
274
317
// Configure I2C pins
275
318
pinmap_pinout (sda , PinMap_I2C_SDA );
276
319
pinmap_pinout (scl , PinMap_I2C_SCL );
277
320
pin_mode (sda , PullUp );
278
321
pin_mode (scl , PullUp );
279
322
obj_s -> event_i2cIRQ = FMPI2C1_EV_IRQn ;
280
323
obj_s -> error_i2cIRQ = FMPI2C1_ER_IRQn ;
281
- __HAL_RCC_FMPI2C1_CLK_ENABLE ();
282
324
}
283
325
#endif
284
326
285
- // Reset to clear pending flags if any
286
- i2c_reset (obj );
287
-
288
327
// I2C configuration
328
+ // Default hz value used for timeout computation
289
329
if (!obj_s -> hz )
290
330
obj_s -> hz = 100000 ; // 100 kHz per default
331
+
332
+ // Reset to clear pending flags if any
333
+ i2c_hw_reset (obj );
291
334
i2c_frequency (obj , obj_s -> hz );
292
335
293
336
#if DEVICE_I2CSLAVE
@@ -399,49 +442,6 @@ i2c_t *get_i2c_obj(I2C_HandleTypeDef *hi2c){
399
442
return (obj );
400
443
}
401
444
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
-
445
445
/* SYNCHRONOUS API FUNCTIONS */
446
446
447
447
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) {
674
674
}
675
675
#endif //I2C_IP_VERSION_V2
676
676
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
+
677
685
/*
678
686
* SYNC APIS
679
687
*/
0 commit comments