atsams-rs / atsamx7x-rust

Rust HAL, PACs, and examples for the Microship SAM E70/S70/V70/V71
Apache License 2.0
25 stars 11 forks source link

TWI Clock Frequency Half Of User Input #58

Closed martinmortsell closed 1 year ago

martinmortsell commented 1 year ago

When setting the clock frequency for the twi peripheral the actual Clock Frequency ends up being half of your input value.

This is because the formula does not take into account that the period is twice the time given in 43.8.5, as that is only for the high or low time of the pulse.

martinmortsell commented 1 year ago

I took a stab at fixing this and it turns out to be a bit more complex than I thought.

The Physical Layer Definition is not for frequencies, it is for t_high and t_low (as in the register implementation) and the transition time from low to high is not included in that time. Thus, after my fix the clock frequency I measure is 325kHz, instead of the nominal 400kHz I asked for, as the rise time of 640ns is added to each period.

I think assuming no rise time and adding a note in the documentation is fine as a solution here. If the user really care about the exact frequency they clock their I2C bus at they can manually fiddle around with the values until they're satisfied.