3 years, 7 months ago.

I2C on FRDM-K64F

Dear colleagues, I’m trying to port a program that already is running on LPC1768 and LPC4088 to the FRDM-K64F platform. Most things seem to work fine but I had problems with the I2C bus. The SoC is working as master and should send data to a DAC. After declaring an instance by

<<code>> I2C i2c(I2C_SDA, I2C_SCK) ; <<\code>> I call the <<code>> i2c.start(); <<\code>> function to bring the port in a defined state. Then, I’m trying to send my data. This works fine with the LPCs but not with the K64. By checking the lines with an oscilloscope I found, that both line are drawn down with the i2c.start() to zero. Is this intended? Thus, I tested the program with commenting out this function and it works. That surprises me.

1 Answer

3 years, 7 months ago.

Please use the <<code>> and <</code>> tags on different lines surrounding your posted code.

You did not show the complete I2C code, but I assume the i2c.start() is followed by a write operation. Something like:

...
i2c.start(); // will generate a Start condition

i2c.write(address, buffer, number of bytes); // will try to generate another Start condition followed by address and data
...

The i2c.start() is used to generate an I2C start condition. That would typically result in both SDA and SCL being low after completion. The i2c.start() should then be followed by two or more I2C write operations that each send only one byte.

..
i2c.start();
i2c.write(address);
i2c.write(data);
etc

Note that there are two different i2c.write() methods in the mbed I2C library: one that sends a single byte and the so called blockwrite operation that generates the Start condition, then sends bytes from a buffer followed by sending the final Stop condition (although this can also be suppressed).

You should not combine the i2c.start() with the i2c blockwrite operation. Doing that may confuse the i2C hardware engine on the master. That is probably what happens on the K64 and that is why it works when you comment out the i2c.start(). Some other platforms (like the LPC1768) might be OK with this combination.

Also note that preferably you should always use the blockwrite operation rather than the start(), write(), stop(). Some platforms cant implement this properly due to the way the I2C engine is designed. It does work on the LPC1768 but is not fully portable to other mbed platforms.

Accepted Answer

Hi Wim, it is exactly as you have described. But I think it should be noted in the reference. Thank, you very much.

posted by Uwe Haertel 25 Feb 2018