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:
    T_{high} = CCR \ast T_{PCLK1}
    T_{low} = CCR \ast T_{PCLK1}
    Looking at above we can assume that for SM T_{high} = T_{low} and CCR is unknown:
    CCR = \frac{T_{high}}{T_{PCLK1}}
    Since T_{PCLK1} is needed, this basic formula can be used:
    f[hz] = \frac{1}{t[s]}
    then time is:
    t[s] = \frac{1}{f[Hz]}
    PCLK1 is peripheral clock speed, in this case 16MHz.
    For f = 16 000 000 [Hz], t = 0,0000000625 [s] => t = 62,5 [ns].
    T_{PCLK1} = 62.5 [ns], since  T_{SCL} = T_{high} + T_{low} and T_{high} = T_{low}
    T_{High} = \frac{T_{SCL}}{2}
    To get TSCL:
    T_{SCL} = \frac{1}{F_{SCL}}
    To generate I2C 100 kHz SCL frequency: F_{SCL} = 100 000 [Hz], then T_{SCL} = 10 \mu s
    T_{high} = \frac{10 [us]}{2} = 5000 [ns]
    And finally CCR = \frac{T_{high}}{T_{PCLK1}} formula can be used:
    CCR = \frac{5000[ns]]}{62.5[ns]} = 80 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:
    TRISE = \frac{T_{maxrise}}{T_{PCLK1}} +1, so TRISE = \frac{1000[ns]}{62.5[ns]]} +1, 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.

Leave a Reply

Your email address will not be published. Required fields are marked *