devttys0 / libmpsse

Open source library for SPI/I2C control via FTDI chips
260 stars 173 forks source link

Erroneous clock frequency #41

Open andreireanu opened 4 years ago

andreireanu commented 4 years ago

Hello, I'm having a bit of trouble setting the clock frequency. For example if I create the I2C object using a 2 MHz frequency (TWO_MHZ parameter), the real observed frequency is 1,33 MHz. This ratio appears no matter the frequency I set, the real frequency is always 2/3 out of the frequency I set. Any reason for this?

Screenshot from 2020-04-09 12-22-16

l29ah commented 4 years ago

Check that your logic analyzer measures periods correctly. I had similar problems with another FX2-based LA that lied to me.

andreireanu commented 4 years ago

Thanks for the reply! The thing is I used a different library at some point (pyftdi) and the real frequency was recorded correctly by the analyser, so I don't think there's an issue with it.

l29ah commented 4 years ago

Oh, interesting. By the way, on your screenshot i observe clock blanks being twice as long as the pulses; is it the same with pyftdi?

andreireanu commented 4 years ago

Nope, the blanks were the same size as the ones.

Screenshot from 2020-04-09 13-38-46

andreireanu commented 4 years ago

I seem to have found the problem:

To make I2C working, FTDI needs to enable what they call a "3 phase clock". PyFtdi implements this feature. The issue with this mode is that it requires 3 clock phase for each clock period, hence the 2:3 ratio. PyFtdi hides this setting from the user, and silently tweaks the specified clock to match what the users expects. libmpsse seems not to do it.

Is anyone aware of the inner working of the libmpsse library and how I could make this change to it? Thanks!

l29ah commented 4 years ago

On Thu, Apr 09, 2020 at 05:25:21AM -0700, Andrei Ungureanu wrote:

Is anyone aware of the inner working of the libmpsse library and how I could make this change to it? Thanks!

Check build_block_buffer()

-- () ascii ribbon campaign - against html mail /\ http://arc.pasp.de/ - against proprietary attachments

l29ah commented 4 years ago

Also i see it being enabled in SetMode().

andreireanu commented 4 years ago

Can you please be more specific on where I can find this part of code? I searched for the definitions of the frequency constants and this led me to the compiled _pylibmpsse.so file.