@@ -82,7 +82,7 @@ static I2C_HandleTypeDef *i2c_handles[I2C_NUM];
82
82
#define FLAG_TIMEOUT ((int)0x1000)
83
83
84
84
/* Declare i2c_init_internal to be used in this file */
85
- void i2c_init_internal (i2c_t * obj , PinName sda , PinName scl );
85
+ void i2c_init_internal (i2c_t * obj , const i2c_pinmap_t * pinmap );
86
86
87
87
/* GENERIC INIT and HELPERS FUNCTIONS */
88
88
@@ -271,24 +271,20 @@ void i2c_sw_reset(i2c_t *obj)
271
271
handle -> Instance -> CR1 |= I2C_CR1_PE ;
272
272
}
273
273
274
- void i2c_init (i2c_t * obj , PinName sda , PinName scl )
275
- {
276
- memset (obj , 0 , sizeof (* obj ));
277
- i2c_init_internal (obj , sda , scl );
278
- }
279
-
280
- void i2c_init_internal (i2c_t * obj , PinName sda , PinName scl )
274
+ void i2c_init_internal (i2c_t * obj , const i2c_pinmap_t * pinmap )
281
275
{
282
276
struct i2c_s * obj_s = I2C_S (obj );
283
277
284
278
// Determine the I2C to use
285
- I2CName i2c_sda = (I2CName )pinmap_peripheral (sda , PinMap_I2C_SDA );
286
- I2CName i2c_scl = (I2CName )pinmap_peripheral (scl , PinMap_I2C_SCL );
287
- obj_s -> sda = sda ;
288
- obj_s -> scl = scl ;
289
-
290
- obj_s -> i2c = (I2CName )pinmap_merge (i2c_sda , i2c_scl );
291
- MBED_ASSERT (obj_s -> i2c != (I2CName )NC );
279
+ if (pinmap != NULL ) {
280
+ obj_s -> sda = pinmap -> sda_pin ;
281
+ obj_s -> scl = pinmap -> scl_pin ;
282
+ obj_s -> sda_func = pinmap -> sda_function ;
283
+ obj_s -> scl_func = pinmap -> scl_function ;
284
+
285
+ obj_s -> i2c = (I2CName )pinmap -> peripheral ;
286
+ MBED_ASSERT (obj_s -> i2c != (I2CName )NC );
287
+ }
292
288
293
289
#if defined I2C1_BASE
294
290
// Enable I2C1 clock and pinout if not done
@@ -338,10 +334,10 @@ void i2c_init_internal(i2c_t *obj, PinName sda, PinName scl)
338
334
#endif
339
335
340
336
// Configure I2C pins
341
- pinmap_pinout ( sda , PinMap_I2C_SDA );
342
- pinmap_pinout ( scl , PinMap_I2C_SCL );
343
- pin_mode (sda , OpenDrainNoPull );
344
- pin_mode (scl , OpenDrainNoPull );
337
+ pin_function ( obj_s -> sda , obj_s -> sda_func );
338
+ pin_function ( obj_s -> scl , obj_s -> scl_func );
339
+ pin_mode (obj_s -> sda , OpenDrainNoPull );
340
+ pin_mode (obj_s -> scl , OpenDrainNoPull );
345
341
346
342
// I2C configuration
347
343
// Default hz value used for timeout computation
@@ -368,6 +364,28 @@ void i2c_init_internal(i2c_t *obj, PinName sda, PinName scl)
368
364
#endif
369
365
}
370
366
367
+ void i2c_init_direct (i2c_t * obj , const i2c_pinmap_t * pinmap )
368
+ {
369
+ memset (obj , 0 , sizeof (* obj ));
370
+ i2c_init_internal (obj , pinmap );
371
+ }
372
+
373
+ void i2c_init (i2c_t * obj , PinName sda , PinName scl )
374
+ {
375
+ uint32_t i2c_sda = pinmap_peripheral (sda , PinMap_I2C_SDA );
376
+ uint32_t i2c_scl = pinmap_peripheral (scl , PinMap_I2C_SCL );
377
+
378
+ int peripheral = (int )pinmap_merge (i2c_sda , i2c_scl );
379
+
380
+ int sda_function = (int )pinmap_find_function (sda , PinMap_I2C_SDA );
381
+ int scl_function = (int )pinmap_find_function (scl , PinMap_I2C_SCL );
382
+
383
+ const i2c_pinmap_t explicit_i2c_pinmap = {peripheral , sda , sda_function , scl , scl_function };
384
+
385
+ i2c_init_direct (obj , & explicit_i2c_pinmap );
386
+ }
387
+
388
+
371
389
void i2c_frequency (i2c_t * obj , int hz )
372
390
{
373
391
int timeout ;
@@ -488,7 +506,7 @@ void i2c_reset(i2c_t *obj)
488
506
/* As recommended in i2c_api.h, mainly send stop */
489
507
i2c_stop (obj );
490
508
/* then re-init */
491
- i2c_init_internal (obj , obj_s -> sda , obj_s -> scl );
509
+ i2c_init_internal (obj , NULL );
492
510
}
493
511
494
512
/*
@@ -542,7 +560,7 @@ int i2c_stop(i2c_t *obj)
542
560
* re-init HAL state
543
561
*/
544
562
if (obj_s -> XferOperation != I2C_FIRST_AND_LAST_FRAME ) {
545
- i2c_init_internal (obj , obj_s -> sda , obj_s -> scl );
563
+ i2c_init_internal (obj , NULL );
546
564
}
547
565
548
566
return 0 ;
@@ -618,7 +636,7 @@ int i2c_stop(i2c_t *obj)
618
636
#if DEVICE_I2CSLAVE
619
637
if (obj_s -> slave ) {
620
638
/* re-init slave when stop is requested */
621
- i2c_init_internal (obj , obj_s -> sda , obj_s -> scl );
639
+ i2c_init_internal (obj , NULL );
622
640
return 0 ;
623
641
}
624
642
#endif
@@ -659,7 +677,7 @@ int i2c_stop(i2c_t *obj)
659
677
/* In case of mixed usage of the APIs (unitary + SYNC)
660
678
* re-init HAL state */
661
679
if (obj_s -> XferOperation != I2C_FIRST_AND_LAST_FRAME ) {
662
- i2c_init_internal (obj , obj_s -> sda , obj_s -> scl );
680
+ i2c_init_internal (obj , NULL );
663
681
}
664
682
665
683
return 0 ;
@@ -837,7 +855,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
837
855
if ((timeout == 0 ) || (obj_s -> event != I2C_EVENT_TRANSFER_COMPLETE )) {
838
856
DEBUG_PRINTF (" TIMEOUT or error in i2c_read\r\n" );
839
857
/* re-init IP to try and get back in a working state */
840
- i2c_init_internal (obj , obj_s -> sda , obj_s -> scl );
858
+ i2c_init_internal (obj , NULL );
841
859
} else {
842
860
count = length ;
843
861
}
@@ -904,7 +922,7 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
904
922
if ((timeout == 0 ) || (obj_s -> event != I2C_EVENT_TRANSFER_COMPLETE )) {
905
923
DEBUG_PRINTF (" TIMEOUT or error in i2c_write\r\n" );
906
924
/* re-init IP to try and get back in a working state */
907
- i2c_init_internal (obj , obj_s -> sda , obj_s -> scl );
925
+ i2c_init_internal (obj , NULL );
908
926
} else {
909
927
count = length ;
910
928
}
@@ -974,7 +992,7 @@ void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
974
992
DEBUG_PRINTF ("HAL_I2C_ErrorCallback:%d, index=%d\r\n" , (int ) hi2c -> ErrorCode , obj_s -> index );
975
993
976
994
/* re-init IP to try and get back in a working state */
977
- i2c_init_internal (obj , obj_s -> sda , obj_s -> scl );
995
+ i2c_init_internal (obj , NULL );
978
996
979
997
#if DEVICE_I2CSLAVE
980
998
/* restore slave address */
0 commit comments