# STM32 I2C calculating clock control register

How to set up clock control register CCR and TRISE for I2C in stm32f429i mcu for Standard Mode (SCL up to 100 kHz).
In this example I am using 16MHz internal oscillator (HSI) as clock source.

1. I2C peripheral must be disabled to set clock speed. Set PE[0] = 0 in CR1 register.
2. Select peripheral clock speed in Control Register 2 bitfield FREQ[5:0].
Example: for 16MHz, FREQ should be set to 16 (0x10), for 10MHz -> 10 (0x0A), and so on.
3. Set bit F/S[15] = 0 (Standard Mode) in Clock Control Register CCR.
4. Calculate CCR[11:0] bitfield value in CCR register for SM using formula provided by Reference Manual:

Looking at above we can assume that for SM  and CCR is unknown:

Since  is needed, this basic formula can be used:

then time is:

PCLK1 is peripheral clock speed, in this case 16MHz.
For f = 16 000 000 [Hz], t = 0,0000000625 [s] => t = 62,5 [ns].
= 62.5 [ns], since  and

To get TSCL:

To generate I2C 100 kHz SCL frequency: , then

And finally formula can be used:
or 0x50 in hex.
CCR[11:0] = 0x50
5. Calculate and set TRISE register. According to Reference Manual, for SM maximum allowed SCL rise is 1000 ns.
Using provided formula:
, so , TRISE = 16 + 1 = 17 => 0x11
TRISE[5:0] = 0x11
6. After CCR and TRISE are set, I2C peripheral can be enabled byt setting PE[0] = 1 in Control Register 1.