@@ -62,9 +62,6 @@ extern "C" {
62
62
#ifndef I2C_VALID_TIMING_NBR
63
63
#define I2C_VALID_TIMING_NBR 8U
64
64
#endif
65
- #define I2C_SPEED_FREQ_STANDARD 0U /* 100 kHz */
66
- #define I2C_SPEED_FREQ_FAST 1U /* 400 kHz */
67
- #define I2C_SPEED_FREQ_FAST_PLUS 2U /* 1 MHz */
68
65
#define I2C_ANALOG_FILTER_DELAY_MIN 50U /* ns */
69
66
#ifndef I2C_ANALOG_FILTER_DELAY_MAX
70
67
#define I2C_ANALOG_FILTER_DELAY_MAX 260U /* ns */
@@ -82,6 +79,22 @@ extern "C" {
82
79
#define I2C_SCLL_MAX 256U
83
80
#define SEC2NSEC 1000000000UL
84
81
82
+ typedef enum {
83
+ I2C_SPEED_FREQ_STANDARD , /* 100 kHz */
84
+ I2C_SPEED_FREQ_FAST , /* 400 kHz */
85
+ I2C_SPEED_FREQ_FAST_PLUS , /* 1 MHz */
86
+ I2C_SPEED_FREQ_NUMBER /* Must be the last entry */
87
+ } i2c_speed_freq_t ;
88
+
89
+ #ifdef I2C_TIMING
90
+ typedef struct {
91
+ uint32_t input_clock ; /* I2C Input clock */
92
+ uint32_t timing ; /* I2C timing corresponding to Input clock */
93
+ } I2C_timing_t ;
94
+
95
+ static I2C_timing_t I2C_ClockTiming [I2C_SPEED_FREQ_NUMBER ] = {0 };
96
+ #endif
97
+
85
98
typedef struct {
86
99
uint32_t freq ; /* Frequency in Hz */
87
100
uint32_t freq_min ; /* Minimum frequency in Hz */
@@ -354,6 +367,16 @@ static uint32_t i2c_computeTiming(uint32_t clkSrcFreq, uint32_t i2c_speed)
354
367
uint8_t presc , scldel , sdadel ;
355
368
uint32_t tafdel_min , tafdel_max ;
356
369
370
+ if (i2c_speed > I2C_SPEED_FREQ_NUMBER ) {
371
+ return ret ;
372
+ }
373
+ /* Don't compute timing if already available value for the requested speed with the same I2C input frequency */
374
+ if ((I2C_ClockTiming [i2c_speed ].input_clock == clkSrcFreq ) && (I2C_ClockTiming [i2c_speed ].timing != 0U )) {
375
+ return I2C_ClockTiming [i2c_speed ].timing ;
376
+ }
377
+
378
+ I2C_ClockTiming [i2c_speed ].input_clock = clkSrcFreq ; // Save the I2C input clock for which we will save timing
379
+
357
380
ti2cclk = (SEC2NSEC + (clkSrcFreq / 2U )) / clkSrcFreq ;
358
381
ti2cspeed = (SEC2NSEC + (I2C_Charac [i2c_speed ].freq / 2U )) / I2C_Charac [i2c_speed ].freq ;
359
382
@@ -437,6 +460,8 @@ static uint32_t i2c_computeTiming(uint32_t clkSrcFreq, uint32_t i2c_speed)
437
460
((sclh & 0xFFU ) << 8 ) | \
438
461
((scll & 0xFFU ) << 0 );
439
462
prev_presc = presc ;
463
+ /* Save I2C Timing found for further reuse (and avoid to compute again) */
464
+ I2C_ClockTiming [i2c_speed ].timing = ret ;
440
465
}
441
466
}
442
467
}
@@ -736,7 +761,6 @@ i2c_status_e i2c_master_write(i2c_t *obj, uint8_t dev_address,
736
761
Master restarts communication */
737
762
} while (((HAL_I2C_GetError (& (obj -> handle )) & HAL_I2C_ERROR_AF ) == HAL_I2C_ERROR_AF )
738
763
&& (delta < I2C_TIMEOUT_TICK ));
739
-
740
764
return ret ;
741
765
}
742
766
0 commit comments