From 7bc0ea694624ff127eff6243ecffb127f18074b2 Mon Sep 17 00:00:00 2001 From: raiden00pl Date: Wed, 8 Jan 2025 17:10:23 +0100 Subject: [PATCH] arch/arm/stm32{|f0l0g0|f7|h5|h7}/i2c: raise compilation error when configuration is invalid Raise compilation error when I2C configuration is invalid. It's better to catch invalid clock configuration during compilation instead of producing binary that doesn't work as expected anyway. Signed-off-by: raiden00pl --- arch/arm/src/stm32/stm32_i2c.c | 17 ++++++++--------- arch/arm/src/stm32/stm32_i2c_alt.c | 17 ++++++++--------- arch/arm/src/stm32/stm32_i2c_v2.c | 3 +-- arch/arm/src/stm32/stm32f40xxx_i2c.c | 17 ++++++++--------- arch/arm/src/stm32f0l0g0/stm32_i2c.c | 9 +-------- arch/arm/src/stm32f7/stm32_i2c.c | 9 ++++----- arch/arm/src/stm32h5/stm32_i2c.c | 9 ++++----- arch/arm/src/stm32h7/stm32_i2c.c | 9 ++++----- 8 files changed, 38 insertions(+), 52 deletions(-) diff --git a/arch/arm/src/stm32/stm32_i2c.c b/arch/arm/src/stm32/stm32_i2c.c index 0c7f26676fc32..a8fd44c269fe6 100644 --- a/arch/arm/src/stm32/stm32_i2c.c +++ b/arch/arm/src/stm32/stm32_i2c.c @@ -95,6 +95,14 @@ * Pre-processor Definitions ****************************************************************************/ +#if STM32_PCLK1_FREQUENCY < 4000000 +# warning STM32_I2C: Peripheral clock must be at least 4 MHz to support 400 kHz operation. +#endif + +#if STM32_PCLK1_FREQUENCY < 2000000 +# error STM32_I2C: Peripheral clock must be at least 2 MHz to support 100 kHz operation. +#endif + /* Configuration ************************************************************/ /* CONFIG_I2C_POLLED may be set so that I2C interrupts will not be used. @@ -1859,15 +1867,6 @@ struct i2c_master_s *stm32_i2cbus_initialize(int port) { struct stm32_i2c_priv_s *priv = NULL; -#if STM32_PCLK1_FREQUENCY < 4000000 -# warning STM32_I2C_INIT: Peripheral clock must be at least 4 MHz to support 400 kHz operation. -#endif - -#if STM32_PCLK1_FREQUENCY < 2000000 -# warning STM32_I2C_INIT: Peripheral clock must be at least 2 MHz to support 100 kHz operation. - return NULL; -#endif - /* Get I2C private structure */ switch (port) diff --git a/arch/arm/src/stm32/stm32_i2c_alt.c b/arch/arm/src/stm32/stm32_i2c_alt.c index 0a003dd7520ee..a23a66ec62679 100644 --- a/arch/arm/src/stm32/stm32_i2c_alt.c +++ b/arch/arm/src/stm32/stm32_i2c_alt.c @@ -118,6 +118,14 @@ * Pre-processor Definitions ****************************************************************************/ +#if STM32_PCLK1_FREQUENCY < 4000000 +# warning STM32_I2C: Peripheral clock must be at least 4 MHz to support 400 kHz operation. +#endif + +#if STM32_PCLK1_FREQUENCY < 2000000 +# error STM32_I2C: Peripheral clock must be at least 2 MHz to support 100 kHz operation. +#endif + /* Configuration ************************************************************/ /* CONFIG_I2C_POLLED may be set so that I2C interrupts will not be used. @@ -2366,15 +2374,6 @@ struct i2c_master_s *stm32_i2cbus_initialize(int port) { struct stm32_i2c_priv_s *priv = NULL; -#if STM32_PCLK1_FREQUENCY < 4000000 -# warning STM32_I2C_INIT: Peripheral clock must be at least 4 MHz to support 400 kHz operation. -#endif - -#if STM32_PCLK1_FREQUENCY < 2000000 -# warning STM32_I2C_INIT: Peripheral clock must be at least 2 MHz to support 100 kHz operation. - return NULL; -#endif - /* Get I2C private structure */ switch (port) diff --git a/arch/arm/src/stm32/stm32_i2c_v2.c b/arch/arm/src/stm32/stm32_i2c_v2.c index 0f88986feb091..3ddf200be78e9 100644 --- a/arch/arm/src/stm32/stm32_i2c_v2.c +++ b/arch/arm/src/stm32/stm32_i2c_v2.c @@ -249,6 +249,7 @@ * Pre-processor Definitions ****************************************************************************/ +#warning TODO: check I2C clock source. It must be HSI! #undef INVALID_CLOCK_SOURCE #if defined(CONFIG_STM32_STM32F30XX) || defined(CONFIG_STM32_STM32F33XX) || \ @@ -264,8 +265,6 @@ # error STM32_I2C: Device not Supported. #endif -#warning TODO: check I2C clock source. It must be HSI! - /* CONFIG_I2C_POLLED may be set so that I2C interrupts will not be used. * Instead, CPU-intensive polling will be used. */ diff --git a/arch/arm/src/stm32/stm32f40xxx_i2c.c b/arch/arm/src/stm32/stm32f40xxx_i2c.c index 6331210c64bf4..fac079150c54a 100644 --- a/arch/arm/src/stm32/stm32f40xxx_i2c.c +++ b/arch/arm/src/stm32/stm32f40xxx_i2c.c @@ -95,6 +95,14 @@ * Pre-processor Definitions ****************************************************************************/ +#if STM32_PCLK1_FREQUENCY < 4000000 +# warning STM32_I2C: Peripheral clock must be at least 4 MHz to support 400 kHz operation. +#endif + +#if STM32_PCLK1_FREQUENCY < 2000000 +# error STM32_I2C: Peripheral clock must be at least 2 MHz to support 100 kHz operation. +#endif + /* Configuration ************************************************************/ /* CONFIG_I2C_POLLED may be set so that I2C interrupts will not be used. @@ -2587,15 +2595,6 @@ struct i2c_master_s *stm32_i2cbus_initialize(int port) { struct stm32_i2c_priv_s *priv = NULL; -#if STM32_PCLK1_FREQUENCY < 4000000 -# warning STM32_I2C_INIT: Peripheral clock must be at least 4 MHz to support 400 kHz operation. -#endif - -#if STM32_PCLK1_FREQUENCY < 2000000 -# warning STM32_I2C_INIT: Peripheral clock must be at least 2 MHz to support 100 kHz operation. - return NULL; -#endif - /* Get I2C private structure */ switch (port) diff --git a/arch/arm/src/stm32f0l0g0/stm32_i2c.c b/arch/arm/src/stm32f0l0g0/stm32_i2c.c index 070d425054128..93ba0f2f4c240 100644 --- a/arch/arm/src/stm32f0l0g0/stm32_i2c.c +++ b/arch/arm/src/stm32f0l0g0/stm32_i2c.c @@ -252,7 +252,7 @@ #undef INVALID_CLOCK_SOURCE -#warning TODO: check I2C clock source. It must be HSI! +#warning TODO: check I2C clock source and clock frequency. It must be HSI! /* CONFIG_I2C_POLLED may be set so that I2C interrupts will not be used. * Instead, CPU-intensive polling will be used. @@ -2702,13 +2702,6 @@ struct i2c_master_s *stm32_i2cbus_initialize(int port) struct stm32_i2c_priv_s *priv = NULL; /* private data of device with multiple instances */ struct stm32_i2c_inst_s *inst = NULL; /* device, single instance */ -#if 0 /* REVISIT: this is not true for all STM32 M0 */ -#if STM32_HSI_FREQUENCY != 8000000 || defined(INVALID_CLOCK_SOURCE) -# warning STM32_I2C_INIT: Peripheral clock is HSI and it must be 16mHz or the speed/timing calculations need to be redone. - return NULL; -#endif -#endif - /* Get I2C private structure */ switch (port) diff --git a/arch/arm/src/stm32f7/stm32_i2c.c b/arch/arm/src/stm32f7/stm32_i2c.c index b5fba286de1a1..ca4b3b2e9d356 100644 --- a/arch/arm/src/stm32f7/stm32_i2c.c +++ b/arch/arm/src/stm32f7/stm32_i2c.c @@ -290,6 +290,10 @@ # endif #endif +#if STM32_HSI_FREQUENCY != 16000000 || defined(INVALID_CLOCK_SOURCE) +# error STM32_I2C: Peripheral clock is HSI and it must be 16mHz or the speed/timing calculations need to be redone. +#endif + /* CONFIG_I2C_POLLED may be set so that I2C interrupts will not be used. * Instead, CPU-intensive polling will be used. */ @@ -2731,11 +2735,6 @@ struct i2c_master_s *stm32_i2cbus_initialize(int port) { struct stm32_i2c_priv_s *priv = NULL; /* private data of device with multiple instances */ -#if STM32_HSI_FREQUENCY != 16000000 || defined(INVALID_CLOCK_SOURCE) -# warning STM32_I2C_INIT: Peripheral clock is HSI and it must be 16mHz or the speed/timing calculations need to be redone. - return NULL; -#endif - /* Get I2C private structure */ switch (port) diff --git a/arch/arm/src/stm32h5/stm32_i2c.c b/arch/arm/src/stm32h5/stm32_i2c.c index 6f299beda828b..91698c16da047 100644 --- a/arch/arm/src/stm32h5/stm32_i2c.c +++ b/arch/arm/src/stm32h5/stm32_i2c.c @@ -236,6 +236,10 @@ #undef INVALID_CLOCK_SOURCE +#if defined(INVALID_CLOCK_SOURCE) +# error STM32_I2C: Peripheral input clock must be HSI or the speed/timing calculations need to be redone. +#endif + /* CONFIG_I2C_POLLED may be set so that I2C interrupts will not be used. * Instead, CPU-intensive polling will be used. */ @@ -3005,11 +3009,6 @@ struct i2c_master_s *stm32_i2cbus_initialize(int port) struct stm32_i2c_priv_s *priv = NULL; /* private data of device with multiple instances */ struct stm32_i2c_inst_s *inst = NULL; /* device, single instance */ -#if defined(INVALID_CLOCK_SOURCE) -# warning STM32_I2C_INIT: Peripheral input clock must be HSI or the speed/timing calculations need to be redone. - return NULL; -#endif - /* Get I2C private structure */ switch (port) diff --git a/arch/arm/src/stm32h7/stm32_i2c.c b/arch/arm/src/stm32h7/stm32_i2c.c index 99ace3cbf0cf2..9f96a33e53b83 100644 --- a/arch/arm/src/stm32h7/stm32_i2c.c +++ b/arch/arm/src/stm32h7/stm32_i2c.c @@ -248,6 +248,10 @@ # endif #endif +#if STM32_HSI_FREQUENCY != 16000000 || defined(INVALID_CLOCK_SOURCE) +# error STM32_I2C: Peripheral clock is HSI and it must be 16mHz or the speed/timing calculations need to be redone. +#endif + /* CONFIG_I2C_POLLED may be set so that I2C interrupts will not be used. * Instead, CPU-intensive polling will be used. */ @@ -2706,11 +2710,6 @@ struct i2c_master_s *stm32_i2cbus_initialize(int port) struct stm32_i2c_priv_s *priv = NULL; /* private data of device with multiple instances */ struct stm32_i2c_inst_s *inst = NULL; /* device, single instance */ -#if STM32_HSI_FREQUENCY != 16000000 || defined(INVALID_CLOCK_SOURCE) -# warning STM32_I2C_INIT: Peripheral clock is HSI and it must be 16mHz or the speed/timing calculations need to be redone. - return NULL; -#endif - /* Get I2C private structure */ switch (port)