Skip to content

Commit 73d90c5

Browse files
committed
[I2C] Limit iteration number to compute timing
Computing all timing values consumes huge time. By default only the first 8 valid timing will be computed. It can be redefined thanks the variant.h or build_opt.h or hal_conf_extra.h using: I2C_VALID_TIMING_NBR Higher number ensure lowest clock error but require more time to compute. Signed-off-by: Frederic.Pillon <[email protected]>
1 parent 07bf5a1 commit 73d90c5

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

cores/arduino/stm32/twi.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ extern "C" {
5959
#endif
6060

6161
#ifdef I2C_TIMING
62+
#ifndef I2C_VALID_TIMING_NBR
63+
#define I2C_VALID_TIMING_NBR 8U
64+
#endif
6265
#define I2C_SPEED_FREQ_STANDARD 0U /* 100 kHz */
6366
#define I2C_SPEED_FREQ_FAST 1U /* 400 kHz */
6467
#define I2C_SPEED_FREQ_FAST_PLUS 2U /* 1 MHz */
@@ -337,6 +340,7 @@ valid config.
337340
static uint32_t i2c_computeTiming(uint32_t clkSrcFreq, uint32_t i2c_speed)
338341
{
339342
uint32_t ret = 0xFFFFFFFFU;
343+
uint32_t valid_timing_nbr = 0;
340344
uint32_t ti2cclk;
341345
uint32_t ti2cspeed;
342346
uint32_t prev_error;
@@ -399,6 +403,10 @@ static uint32_t i2c_computeTiming(uint32_t clkSrcFreq, uint32_t i2c_speed)
399403
if ((tsdadel >= (uint32_t)tsdadel_min) && (tsdadel <=
400404
(uint32_t)tsdadel_max)) {
401405
if (presc != prev_presc) {
406+
valid_timing_nbr ++;
407+
if (valid_timing_nbr >= I2C_VALID_TIMING_NBR) {
408+
return ret;
409+
}
402410
/* tPRESC = (PRESC+1) x tI2CCLK*/
403411
uint32_t tpresc = (presc + 1U) * ti2cclk;
404412
for (scll = 0; scll < I2C_SCLL_MAX; scll++) {

0 commit comments

Comments
 (0)