From 8087d8d3ed1565b8e2861cba76dd0fff02b29dc7 Mon Sep 17 00:00:00 2001 From: Alexandre Bourdiol Date: Wed, 11 Sep 2019 17:23:54 +0200 Subject: [PATCH 1/5] Avoid I2C timing computation when input clock is unchanged With U8g2 library, I2C clock is set for each transmission. With some I2C harwdware version, timings are then computed for each time. This cause very slow display on LCD. --- cores/arduino/stm32/twi.c | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/cores/arduino/stm32/twi.c b/cores/arduino/stm32/twi.c index 8f68aeae7d..83e70891d8 100644 --- a/cores/arduino/stm32/twi.c +++ b/cores/arduino/stm32/twi.c @@ -62,9 +62,6 @@ extern "C" { #ifndef I2C_VALID_TIMING_NBR #define I2C_VALID_TIMING_NBR 8U #endif -#define I2C_SPEED_FREQ_STANDARD 0U /* 100 kHz */ -#define I2C_SPEED_FREQ_FAST 1U /* 400 kHz */ -#define I2C_SPEED_FREQ_FAST_PLUS 2U /* 1 MHz */ #define I2C_ANALOG_FILTER_DELAY_MIN 50U /* ns */ #ifndef I2C_ANALOG_FILTER_DELAY_MAX #define I2C_ANALOG_FILTER_DELAY_MAX 260U /* ns */ @@ -82,6 +79,22 @@ extern "C" { #define I2C_SCLL_MAX 256U #define SEC2NSEC 1000000000UL +typedef enum { + I2C_SPEED_FREQ_STANDARD, /* 100 kHz */ + I2C_SPEED_FREQ_FAST, /* 400 kHz */ + I2C_SPEED_FREQ_FAST_PLUS, /* 1 MHz */ + I2C_SPEED_FREQ_NUMBER /* Must be the last entry */ +} i2c_speed_freq_t; + +#ifdef I2C_TIMING +typedef struct { + uint32_t input_clock; /* I2C Input clock */ + uint32_t timing; /* I2C timing corresponding to Input clock */ +} I2C_timing_t; + +static I2C_timing_t I2C_ClockTiming[I2C_SPEED_FREQ_NUMBER] = {0}; +#endif + typedef struct { uint32_t freq; /* Frequency in Hz */ uint32_t freq_min; /* Minimum frequency in Hz */ @@ -354,6 +367,16 @@ static uint32_t i2c_computeTiming(uint32_t clkSrcFreq, uint32_t i2c_speed) uint8_t presc, scldel, sdadel; uint32_t tafdel_min, tafdel_max; + if (i2c_speed > I2C_SPEED_FREQ_NUMBER) { + return ret; + } + /* Don't compute timing if already available value for the requested speed with the same I2C input frequency */ + if ((I2C_ClockTiming[i2c_speed].input_clock == clkSrcFreq) && (I2C_ClockTiming[i2c_speed].timing != 0U)) { + return I2C_ClockTiming[i2c_speed].timing; + } + + I2C_ClockTiming[i2c_speed].input_clock = clkSrcFreq; // Save the I2C input clock for which we will save timing + ti2cclk = (SEC2NSEC + (clkSrcFreq / 2U)) / clkSrcFreq; ti2cspeed = (SEC2NSEC + (I2C_Charac[i2c_speed].freq / 2U)) / I2C_Charac[i2c_speed].freq; @@ -437,6 +460,8 @@ static uint32_t i2c_computeTiming(uint32_t clkSrcFreq, uint32_t i2c_speed) ((sclh & 0xFFU) << 8) | \ ((scll & 0xFFU) << 0); prev_presc = presc; + /* Save I2C Timing found for further reuse (and avoid to compute again) */ + I2C_ClockTiming[i2c_speed].timing = ret; } } } @@ -736,7 +761,6 @@ i2c_status_e i2c_master_write(i2c_t *obj, uint8_t dev_address, Master restarts communication */ } while (((HAL_I2C_GetError(&(obj->handle)) & HAL_I2C_ERROR_AF) == HAL_I2C_ERROR_AF) && (delta < I2C_TIMEOUT_TICK)); - return ret; } From 67b44c26529b9dd8f958cd4ba7624c0370535d64 Mon Sep 17 00:00:00 2001 From: Alexandre Bourdiol <50730894+ABOSTM@users.noreply.github.com> Date: Thu, 12 Sep 2019 11:59:55 +0200 Subject: [PATCH 2/5] Update cores/arduino/stm32/twi.c Co-Authored-By: Frederic Pillon --- cores/arduino/stm32/twi.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cores/arduino/stm32/twi.c b/cores/arduino/stm32/twi.c index 83e70891d8..a3d71eda47 100644 --- a/cores/arduino/stm32/twi.c +++ b/cores/arduino/stm32/twi.c @@ -375,7 +375,8 @@ static uint32_t i2c_computeTiming(uint32_t clkSrcFreq, uint32_t i2c_speed) return I2C_ClockTiming[i2c_speed].timing; } - I2C_ClockTiming[i2c_speed].input_clock = clkSrcFreq; // Save the I2C input clock for which we will save timing + /* Save the I2C input clock for which the timing will be saved */ + I2C_ClockTiming[i2c_speed].input_clock = clkSrcFreq; ti2cclk = (SEC2NSEC + (clkSrcFreq / 2U)) / clkSrcFreq; ti2cspeed = (SEC2NSEC + (I2C_Charac[i2c_speed].freq / 2U)) / I2C_Charac[i2c_speed].freq; From 1e72e4097d0ee336460ed9d2858fe152ffcb82fa Mon Sep 17 00:00:00 2001 From: Alexandre Bourdiol <50730894+ABOSTM@users.noreply.github.com> Date: Thu, 12 Sep 2019 14:37:34 +0200 Subject: [PATCH 3/5] Update cores/arduino/stm32/twi.c Co-Authored-By: Frederic Pillon --- cores/arduino/stm32/twi.c | 1 - 1 file changed, 1 deletion(-) diff --git a/cores/arduino/stm32/twi.c b/cores/arduino/stm32/twi.c index a3d71eda47..b22a9e4bd4 100644 --- a/cores/arduino/stm32/twi.c +++ b/cores/arduino/stm32/twi.c @@ -86,7 +86,6 @@ typedef enum { I2C_SPEED_FREQ_NUMBER /* Must be the last entry */ } i2c_speed_freq_t; -#ifdef I2C_TIMING typedef struct { uint32_t input_clock; /* I2C Input clock */ uint32_t timing; /* I2C timing corresponding to Input clock */ From 1df79aed1cd41f959eea03da4a33ec46649c0012 Mon Sep 17 00:00:00 2001 From: Alexandre Bourdiol <50730894+ABOSTM@users.noreply.github.com> Date: Thu, 12 Sep 2019 14:37:53 +0200 Subject: [PATCH 4/5] Update cores/arduino/stm32/twi.c Co-Authored-By: Frederic Pillon --- cores/arduino/stm32/twi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/arduino/stm32/twi.c b/cores/arduino/stm32/twi.c index b22a9e4bd4..1853194cc0 100644 --- a/cores/arduino/stm32/twi.c +++ b/cores/arduino/stm32/twi.c @@ -84,7 +84,7 @@ typedef enum { I2C_SPEED_FREQ_FAST, /* 400 kHz */ I2C_SPEED_FREQ_FAST_PLUS, /* 1 MHz */ I2C_SPEED_FREQ_NUMBER /* Must be the last entry */ -} i2c_speed_freq_t; +} I2C_speed_freq_t; typedef struct { uint32_t input_clock; /* I2C Input clock */ From 574ea0fa095ffb6ef74de51e587cfc0bfa5f3371 Mon Sep 17 00:00:00 2001 From: Alexandre Bourdiol <50730894+ABOSTM@users.noreply.github.com> Date: Thu, 12 Sep 2019 14:38:03 +0200 Subject: [PATCH 5/5] Update cores/arduino/stm32/twi.c Co-Authored-By: Frederic Pillon --- cores/arduino/stm32/twi.c | 1 - 1 file changed, 1 deletion(-) diff --git a/cores/arduino/stm32/twi.c b/cores/arduino/stm32/twi.c index 1853194cc0..b36048fd77 100644 --- a/cores/arduino/stm32/twi.c +++ b/cores/arduino/stm32/twi.c @@ -92,7 +92,6 @@ typedef struct { } I2C_timing_t; static I2C_timing_t I2C_ClockTiming[I2C_SPEED_FREQ_NUMBER] = {0}; -#endif typedef struct { uint32_t freq; /* Frequency in Hz */