Skip to main content

Subsection 10.4.6 Where the TIMINGR Numbers Come From

i2c1_init() writes four values into I2C_TIMINGR, and in class we take them as given. This is where they come from, for anyone moving the code to a board whose clock runs at a different speed.
The register holds five fields (PRESC, SCLL, SCLH, SDADEL and SCLDEL), and between them they set how long SCL spends HIGH, how long it spends LOW, and how long the peripheral waits around the edges before changing or sampling SDA. You are not expected to compute them from the I2C specification’s timing requirements: the reference manual has already done it, for a set of common peripheral clock frequencies, in RM0490 §23.4.10, I2C_TIMINGR register configuration examples (Figure 10.4.2). Ours is 12 MHz: after reset the STM32C031C6 runs from HSI48 with HSIDIV at its reset value of divide-by-four (RM0490 §5.4.3), and RCC_CCIPR resets with I2C1SEL selecting PCLK. There is no table for 12 MHz.
Figure 10.4.2. The three worked examples in RM0490 §23.4.10: Table 94 for a peripheral clock of 8 MHz, Table 95 for 16 MHz and Table 96 for 48 MHz. Read down the Standard-mode 100 kHz column of each. SCLL is 0x13, SCLH is 0xF, SDADEL is 0x2 and SCLDEL is 0x4 in all three; the only field that changes with the clock is PRESC, which is 0x1, 0x3 and 0xB. There is no table for 12 MHz.
The prescaler divides the peripheral clock by PRESC + 1, so 8 MHz divided by (0x1 + 1) is 4 MHz; 16 MHz divided by (0x3 + 1) is 4 MHz; and 48 MHz divided by (0xB + 1), that is 12, is 4 MHz. Every table divides its clock down to the same 4 MHz, and 250 ns is that clock’s period. Everything after the prescaler is counted in units of 250 ns, which is why the other four fields do not change from table to table: 20 of them LOW and 16 of them HIGH is 5.0 µs and 4.0 µs whatever clock the chip started from.
Those two do not quite add to the 10 µs a 100 kHz clock needs, and the manual says why: the t_SCL row of each table reads “~10 µs”, and RM0490 §23.4.10 footnotes it “t_SCL is greater than t_SCLL + t_SCLH due to SCL internal detection delay”. (The crops here stop above the footnote block.) The missing microsecond is the peripheral synchronizing to its own clock line, and 10 µs is what the AD2 measures between two adjacent SCL rising edges.
So PRESC is not a constant belonging to a board. It is \(f_\text{I2CCLK} / 4\,\text{MHz} - 1\text{,}\) which for a 12 MHz clock is 2, and that is the one number to change if this code moves.
The other four are copied from any of the three tables, and this section does not derive them. SCLL and SCLH are checked above against the bus speed they have to produce. SDADEL and SCLDEL are not: they set how long the peripheral waits after an SCL edge before changing SDA, and how long it waits before sampling it, and both come out of the I2C specification’s setup and hold requirements rather than out of the bus frequency. One warning if you extend the pattern by analogy: RM0490’s formula for SDADEL does not carry the +1 that the other four fields do.