@@ -186,6 +186,48 @@ uint32_t i2c_get_irq_handler(i2c_t *obj)
186
186
return handler ;
187
187
}
188
188
189
+ void i2c_hw_reset (i2c_t * obj ) {
190
+ int timeout ;
191
+ struct i2c_s * obj_s = I2C_S (obj );
192
+ I2C_HandleTypeDef * handle = & (obj_s -> handle );
193
+
194
+ handle -> Instance = (I2C_TypeDef * )(obj_s -> i2c );
195
+
196
+ // wait before reset
197
+ timeout = BYTE_TIMEOUT ;
198
+ while ((__HAL_I2C_GET_FLAG (handle , I2C_FLAG_BUSY )) && (-- timeout != 0 ));
199
+ #if defined I2C1_BASE
200
+ if (obj_s -> i2c == I2C_1 ) {
201
+ __HAL_RCC_I2C1_FORCE_RESET ();
202
+ __HAL_RCC_I2C1_RELEASE_RESET ();
203
+ }
204
+ #endif
205
+ #if defined I2C2_BASE
206
+ if (obj_s -> i2c == I2C_2 ) {
207
+ __HAL_RCC_I2C2_FORCE_RESET ();
208
+ __HAL_RCC_I2C2_RELEASE_RESET ();
209
+ }
210
+ #endif
211
+ #if defined I2C3_BASE
212
+ if (obj_s -> i2c == I2C_3 ) {
213
+ __HAL_RCC_I2C3_FORCE_RESET ();
214
+ __HAL_RCC_I2C3_RELEASE_RESET ();
215
+ }
216
+ #endif
217
+ #if defined I2C4_BASE
218
+ if (obj_s -> i2c == I2C_4 ) {
219
+ __HAL_RCC_I2C4_FORCE_RESET ();
220
+ __HAL_RCC_I2C4_RELEASE_RESET ();
221
+ }
222
+ #endif
223
+ #if defined FMPI2C1_BASE
224
+ if (obj_s -> i2c == FMPI2C_1 ) {
225
+ __HAL_RCC_FMPI2C1_FORCE_RESET ();
226
+ __HAL_RCC_FMPI2C1_RELEASE_RESET ();
227
+ }
228
+ #endif
229
+ }
230
+
189
231
void i2c_init (i2c_t * obj , PinName sda , PinName scl ) {
190
232
191
233
struct i2c_s * obj_s = I2C_S (obj );
@@ -276,7 +318,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
276
318
obj_s -> hz = 100000 ; // 100 kHz per default
277
319
278
320
// Reset to clear pending flags if any
279
- i2c_reset (obj );
321
+ i2c_hw_reset (obj );
280
322
i2c_frequency (obj , obj_s -> hz );
281
323
282
324
#if DEVICE_I2CSLAVE
@@ -388,49 +430,6 @@ i2c_t *get_i2c_obj(I2C_HandleTypeDef *hi2c){
388
430
return (obj );
389
431
}
390
432
391
- void i2c_reset (i2c_t * obj ) {
392
-
393
- int timeout ;
394
- struct i2c_s * obj_s = I2C_S (obj );
395
- I2C_HandleTypeDef * handle = & (obj_s -> handle );
396
-
397
- handle -> Instance = (I2C_TypeDef * )(obj_s -> i2c );
398
-
399
- // wait before reset
400
- timeout = BYTE_TIMEOUT ;
401
- while ((__HAL_I2C_GET_FLAG (handle , I2C_FLAG_BUSY )) && (-- timeout != 0 ));
402
- #if defined I2C1_BASE
403
- if (obj_s -> i2c == I2C_1 ) {
404
- __HAL_RCC_I2C1_FORCE_RESET ();
405
- __HAL_RCC_I2C1_RELEASE_RESET ();
406
- }
407
- #endif
408
- #if defined I2C2_BASE
409
- if (obj_s -> i2c == I2C_2 ) {
410
- __HAL_RCC_I2C2_FORCE_RESET ();
411
- __HAL_RCC_I2C2_RELEASE_RESET ();
412
- }
413
- #endif
414
- #if defined I2C3_BASE
415
- if (obj_s -> i2c == I2C_3 ) {
416
- __HAL_RCC_I2C3_FORCE_RESET ();
417
- __HAL_RCC_I2C3_RELEASE_RESET ();
418
- }
419
- #endif
420
- #if defined I2C4_BASE
421
- if (obj_s -> i2c == I2C_4 ) {
422
- __HAL_RCC_I2C4_FORCE_RESET ();
423
- __HAL_RCC_I2C4_RELEASE_RESET ();
424
- }
425
- #endif
426
- #if defined FMPI2C1_BASE
427
- if (obj_s -> i2c == FMPI2C_1 ) {
428
- __HAL_RCC_FMPI2C1_FORCE_RESET ();
429
- __HAL_RCC_FMPI2C1_RELEASE_RESET ();
430
- }
431
- #endif
432
- }
433
-
434
433
/* SYNCHRONOUS API FUNCTIONS */
435
434
436
435
int i2c_read (i2c_t * obj , int address , char * data , int length , int stop ) {
@@ -663,6 +662,14 @@ int i2c_byte_write(i2c_t *obj, int data) {
663
662
}
664
663
#endif //I2C_IP_VERSION_V2
665
664
665
+ void i2c_reset (i2c_t * obj ) {
666
+ struct i2c_s * obj_s = I2C_S (obj );
667
+ /* As recommended in i2c_api.h, mainly send stop */
668
+ i2c_stop (obj );
669
+ /* then re-init */
670
+ i2c_init (obj , obj_s -> sda , obj_s -> scl );
671
+ }
672
+
666
673
/*
667
674
* SYNC APIS
668
675
*/
0 commit comments