2025-03-25 2:25 AM
Hi,
I am trying to calculate the Timing register value for an I2C peripheral using STM32CubeMX. I am doubtful on how to calculate the rise and fall time (tr and tf) values for this. What is the preferred way to choose this tr and tf values for a particular board.
2025-03-25 2:35 AM
Hi,
>What is the preferred way to choose this tr and tf values
afaik - let it at 0 value.
You cannot change any rise or fall time with this settings , its just for setting the best timing for checking hi or lo level.
So its just: if you know, there a slow rise time (or fall) on the I2C line, you can try to set here some extra delay for checking the line level, according to the setting in this tr and tf values .
So usually , with standard "good" signals on I2C , leave this at 0 .
More important is switching ON the analog + digital filter, to reduce the influence of spikes on the lines.
2025-03-25 2:44 AM
Thank you for your quick response!
I was noticing that in Fast mode, the I2C SCL was actually measured to be around 456kHz in my hardware, in contrast to the 400kHz maximum SCL for the Fast mode. This was the reason why I was looking more into the Timing register value. I noticed that on changing the rise and fall time, the Timing register gets recalculated, and I was seeing some improvements in the SCL frequency. From the STM32CubeMX it was noted that the rise and fall time can take values upto 300ns. So is it fine to arbitrarily select the rise and fall time within 300ns and see if the SCL frequency fall within the 400kHz range
2025-03-25 12:26 PM
So look yourself, what Cube puzzling together :
its in rm, I2C -> registers -> I2C timing register , here you can see or mod yourself, what is set:
The speed setting (ie 400kHz) depends in general on the clock, you set in clock tree for the I2C ,
then its own Timing prescaler and the scl settings.
So if your I2C clocking is not what you expect, try other clock setting in clock tree first; usually Cube calculating correct, to get the desired speed.
But just try , play around with the rise/fall setting, to see, what then comes out.
Or modify the timing setting value as you want it and write this in your main, to see, whether you do it better:
static void MX_I2C1_Init(void)
{
/* USER CODE BEGIN I2C1_Init 0 */
/* USER CODE END I2C1_Init 0 */
/* USER CODE BEGIN I2C1_Init 1 */
/* USER CODE END I2C1_Init 1 */
hi2c1.Instance = I2C1;
hi2c1.Init.Timing = 0x007072B1;
hi2c1.Init.OwnAddress1 = 0;
2025-03-27 5:41 AM
Thank you!
In my case the peripheral clock is 160MHz and the I2C frequency I am trying to achieve is 400kHz.
1. If I am to trial with different rise and fall time in the STM32CubeMx to calculate different Timing registers and thus achieve the expected I2C clock frequency, is there any restrictions in the rise and fall time I provide as input to the STM32CubeMx. I have already noticed that the maximum high value the rise and fall time can take up is 300ns. So does that mean I am free to choose any of the rise and fall times between 0 to 300ns for the calculation of Timing register?
2. Is there any way to calculate the SCL rise and fall from the hardware, based on the Capacitance and resistance chosen?
3. Also it was noted that the rise and fall time of the SCL varies with the timing registers chosen. Is this an expected behavior?
2025-03-27 3:38 PM
1. yes, try it. It will not explode...just might not be better than before.
2. yes, use a simulation (LT swcad , i use) , or calculate the R-C yourself. (pullup - stray.cap. , just this is more a guess ). And check with a scope (10:1 probe setting ! ).
3. yes...the "auto" calculation in Cube is to make it more easy to use, without reading the rm for every detail;
but maybe, its calculation is not always, what you expect, so you could write it yourself , and try, to do it better.
And enable analog filter, + dig.filter ; set pin speed lo , to avoid reflections.