Skip to main content
Visitor II
February 4, 2019
Solved

How to configure I2C speed for STM32H7?

  • February 4, 2019
  • 4 replies
  • 4948 views

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.

    This topic has been closed for replies.
    Best answer by Amel NASRI

    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

    4 replies

    fglauAuthor
    Visitor II
    February 4, 2019

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

    Graduate II
    February 4, 2019

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

    fglauAuthor
    Visitor II
    February 4, 2019

    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??

    Technical Moderator
    February 5, 2019

    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

    fglauAuthor
    Visitor II
    February 6, 2019

    OK thanks Amel that worked just fine.