Skip to content

Commit 745c200

Browse files
committed
[I2C] Timing can be hard coded
To avoid time spent to compute the I2C timing, it can be defined in the variant.h or build_opt.h or hal_conf_extra.h with: * I2C_TIMING_SM for Standard Mode (100kHz) * I2C_TIMING_FM for Fast Mode (400kHz) * I2C_TIMING_FMP for Fast Mode Plus (1000kHz) Example for a STM32F0xx using HSI clock as I2C clock source: #define I2C_TIMING_SM 0x00201D2B #define I2C_TIMING_FM 0x0010020A Signed-off-by: Frederic.Pillon <[email protected]>
1 parent 73d90c5 commit 745c200

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

cores/arduino/stm32/twi.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,13 +476,25 @@ static uint32_t i2c_getTiming(i2c_t *obj, uint32_t frequency)
476476
switch (i2c_speed) {
477477
default:
478478
case 100000:
479+
#ifdef I2C_TIMING_SM
480+
ret = I2C_TIMING_SM;
481+
#else
479482
ret = i2c_computeTiming(i2c_getClkFreq(obj->i2c), I2C_SPEED_FREQ_STANDARD);
483+
#endif
480484
break;
481485
case 400000:
486+
#ifdef I2C_TIMING_FM
487+
ret = I2C_TIMING_FM;
488+
#else
482489
ret = i2c_computeTiming(i2c_getClkFreq(obj->i2c), I2C_SPEED_FREQ_FAST);
490+
#endif
483491
break;
484492
case 1000000:
493+
#ifdef I2C_TIMING_FMP
494+
ret = I2C_TIMING_FMP;
495+
#else
485496
ret = i2c_computeTiming(i2c_getClkFreq(obj->i2c), I2C_SPEED_FREQ_FAST_PLUS);
497+
#endif
486498
break;
487499
}
488500
}

variants/board_template/variant.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ extern "C" {
113113
//#define PIN_WIRE_SDA 14 // Default for Arduino connector compatibility
114114
//#define PIN_WIRE_SCL 15 // Default for Arduino connector compatibility
115115

116+
// I2C timing definitions (optional), avoid time spent to compute if defined
117+
// * I2C_TIMING_SM for Standard Mode (100kHz)
118+
// * I2C_TIMING_FM for Fast Mode (400kHz)
119+
// * I2C_TIMING_FMP for Fast Mode Plus (1000kHz)
120+
//#define I2C_TIMING_SM 0x00000000
121+
//#define I2C_TIMING_FM 0x00000000
122+
//#define I2C_TIMING_FMP 0x00000000
123+
116124
// Timer Definitions (optional)
117125
//Do not use timer used by PWM pins when possible. See PinMap_PWM in PeripheralPins.c
118126
#define TIMER_TONE TIMx

0 commit comments

Comments
 (0)