I2C Speed

Forum for problems or queries regarding Flowcode Comms Components. Eg LIN, I2C, SPI, RS232, CAN, IrDA etc

Moderators: Benj, Mods

Post Reply
Mark
Posts: 209
Joined: Thu Oct 19, 2006 11:46 am
Location: Bakewell, UK
Has thanked: 20 times
Been thanked: 16 times
Contact:

I2C Speed

Post by Mark »

Hi there,

The I2C component has 3 speeds, 100, 400 and 1,000 kHz. Is this a speed against an assumed clock speed or is it calculated from the actual clock speed? If a clock speed is assumed, what is it?

Thanks,

Mark
Go with the Flow.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: I2C Speed

Post by Benj »

Hi Mark

Using the Hardware setting the Baud rate is calculated using the actual clock speed specified by Flowcode and the I2C hardware registers to give the correct frequency. Using the software setting the baud rate is fixed at around 50khz.

georgesz113
Posts: 5
Joined: Tue Mar 10, 2009 12:52 pm
Contact:

Re: I2C Speed

Post by georgesz113 »

Dear Ben,

It seems to me that there is a possible glitch in the I2C code regarding the baud rate (at least in version 3.4.7.48). I am running on a 40 MHz oscillator, so Fcyc is 10 MHz and I wanted a 1 MHz I2C clock frequency. But when you set this in the I2C component, it will produce 500 kHz instead of 1 MHz (similarly in the case of 400 kHz it will produce only 200 kHz). I had a look at the I2C code and it seems that the way you define the baud rate generator number (in the sspadd register) is a factor of two out. I had a quick look at a PIC datasheet and they list a few examples there two which also confirmed that you are a factor of two out.

For instance, one of their example is 40 MHz oscillator input and a desired 400 kHz clock. In this case, they write 18h as the BRG value, which is 24 in decimal, essentially means counting down 25 times. This value is decreased twice within the 1 clock cycle of Fcyc, therefore at a frequency of 2*Fcyc = 2*Fosc/4 = 20 MHz in this case. At 20 MHz, one count down takes 50 ns, so 25 counts would take 1250 ns or 1.25 us, which gives 800 kHz. This is the frequency of changing the logic state of the SCL pin, so the 'clock frequency' would be half of this which is indeed 400 kHz as wanted. In the C code that Flowcode generates, the BRG value is 20 which following the same logic would result in a change of logic state at 1 MHz but that would give a clock frequency of only 500 kHz.

Could you please help me to find where I can find within your code how the 1 MHz user input on the I2C component properties window gets translated to the BRG value of 20? I saw that this is parameter %c in the Get_Defines() function but I couldn't get any farther.

Thank you for your help.

Regards,
George

PS. Also within the I2C function you have 10 ms delays at the end of the stop function to make sure that the I2C bus is okay. I don't think this is necessary and it can be a real bugger to find this when someone wants a decent sampling frequency from say an I2C ADC as you won't be able to get even 100 Hz out of the system.

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times
Contact:

Re: I2C Speed

Post by Sean »

Hello George,

We will look into the I2C Baudrate issue.

If you want to edit the I2C component C code file (save the original file first), the Baudrate parameter is passed from Flowcode to the C code as the %c substitution in the line:

#define MX_MI2C_BAUD %c

You could replace this with:

#define MX_MI2C_BAUD (%c / 2)

or define a constant value if you always use the same settings.


The 10ms delay has been added to the MI2C_Stop macro to automatically guarantee completion of an EEPROM write operation - a common requirement in many of our examples. This can be removed from the C code, or commented out, if you want to increase speed of operation and manage any critical timings manually.

Post Reply