cancel
Showing results for 
Search instead for 
Did you mean: 

How to configure I2C speed for STM32H7?

fglau
Associate II

I am looking for a way to change the HAL I2C lib clock speed during runtime to either 100k or 400k or 1M.

I tried the following.....

hi2c1.Init.ClockSpeed = 100000;

However when compiling this the compiler do not recognize the parameter.

1 ACCEPTED SOLUTION

Accepted Solutions
Amel NASRI
ST Employee

Hi @fglau​ ,

The I2C speed configuration is based on I2C_TIMINGR register content.

So, in the HAL structure, you need to program properly the parameter hi2c1.Init.Timing:

hi2c1.Init.Timing      = I2C_TIMING;

I2C_TIMING value depends on:

  • I2C clock source
  • the speed you need to set
  • rise and fall time
  • filter's configuration

The appropriate value may be calculated using STM32CubeMX interface: you will see that a suitable value for TIMINGR will be suggested depending on what you provide as inputs.

-Amel

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

View solution in original post

7 REPLIES 7
fglau
Associate II

I forgot to mention - this is for the STM32H7 HAL I2C lib

You'd need to reintialize with the new values in the structure or disable the peripheral, change the clock parameters, and then enable again.​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
fglau
Associate II

Yes changing the speed and then calling MX_I2C1_Init(); again make sense - however

I cannot find the register for the STM32H7xx in the HAL I2C lib to set the new SCL clock rate.

I tried hi2c1.Init.ClockSpeed = 100000; but the Atollic compiler did not recognize the HAL parameter

Which parameter should i change to update the speed??

Amel NASRI
ST Employee

Hi @fglau​ ,

The I2C speed configuration is based on I2C_TIMINGR register content.

So, in the HAL structure, you need to program properly the parameter hi2c1.Init.Timing:

hi2c1.Init.Timing      = I2C_TIMING;

I2C_TIMING value depends on:

  • I2C clock source
  • the speed you need to set
  • rise and fall time
  • filter's configuration

The appropriate value may be calculated using STM32CubeMX interface: you will see that a suitable value for TIMINGR will be suggested depending on what you provide as inputs.

-Amel

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

OK thanks Amel that worked just fine.

Hello @Amel NASRI​ 

where can I find the formula used by CubeMx to calculate I2C_TIMING?

Thanks.

Hello @RDums.1​ ,

I2C_TIMING calculation in STM32CubeMX is based on formulas described in the RM. You need to refer to I2C timing register (I2C_TIMINGR).

For any follow-up question, please submit a new discussion as the current one is old.

-Amel

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.