Skip to content

Commit

Permalink
arch/arm/stm32{|f0l0g0|f7|h5|h7}/i2c: raise compilation error when co…
Browse files Browse the repository at this point in the history
…nfiguration 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 <[email protected]>
  • Loading branch information
raiden00pl committed Jan 8, 2025
1 parent 40bd8f9 commit 7bc0ea6
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 52 deletions.
17 changes: 8 additions & 9 deletions arch/arm/src/stm32/stm32_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 8 additions & 9 deletions arch/arm/src/stm32/stm32_i2c_alt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions arch/arm/src/stm32/stm32_i2c_v2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) || \
Expand All @@ -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.
*/
Expand Down
17 changes: 8 additions & 9 deletions arch/arm/src/stm32/stm32f40xxx_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 1 addition & 8 deletions arch/arm/src/stm32f0l0g0/stm32_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 4 additions & 5 deletions arch/arm/src/stm32f7/stm32_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 4 additions & 5 deletions arch/arm/src/stm32h5/stm32_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 4 additions & 5 deletions arch/arm/src/stm32h7/stm32_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7bc0ea6

Please sign in to comment.