I2C device driver for lpc1xxx

originally posted: July 2010

I needed an I2C driver for my project. The I2C example provided by NXP was just not good enough, a nice example but not good enough to be used as a device driver for my projects.

So I decided to fix some bugs, clean up the code for readability and add handling for situations where a NACK is received instead of an ACK. Also the I2CEngine function now returns the result of the I2C transaction.
I did some testing on a 24LC08 EEPROM device; write and read after write (using a restart) transactions work. If the device returns a NACK on the slave address this is returned as a separate cause of an aborted transaction.

Testing the device driver

Here is an image of the logic analyzer output showing the write transaction together with the "Acknowledge polling" (sending of the address, followed by a NACK from the slave) and the read transaction from a 24LC08 EEPROM (click on the image for large view).

The logic analyzer picture shows the following information:

On the first line, a complete write transaction is shown: A start condition is generated and the slave address (50h) + write bit are sent on the I2C bus, the slave (a 24LC08 EEPROM) responds with an ACK. Now the EEPROM address (00h) and data (55h AAh 12h 34h) are being sent and after each byte the 24LC08 answers with an ACK.
When the lpc1xxx is done and all Acks have been received, a stop condition is generated and the I2C bus is idle again.

The second line shows the read transaction that immediately takes place after the write.
The lpc1xxx sends a start condition and the slave address (50h) but since the EEPROM is still busy writing the data into memory it will not respond, resulting in a NACK. This is shown twice and then the 24LC08 responds with an ACK, now the lpc1xxx send the EEPROM address (00h) and generates a restart condition (shown as "Sr" in the picture).
After the restart again the slave address is being sent but now with the write bit set to 1 indication this is a read transaction.

The third line shows reading of 4 bytes (with the content previously written into the EEPROM). After each byte the lpc1xxx generates an ACK with the exception of a NACK on the last byte to indicate that no morre data will be read. The stop condition then releases the I2C bus again.

Work in progress ...

This I2C driver is a fairly 'old'  piece of work, it has been ported from lpc1114 to 1334 and 17xx. Porting is a big word ... it's just a matter of selecting the right I/O configuration, clocking and selecting the correct peripheral (for devices having multiple I2C interfaces available).

Unfortunately the changes were "lost during transition" and there is no documentation available (yet).

Today (March 25, 2012) I am taking some time to make up for this and I'll try to create at least some documentation.

Download

Download the I2C driver from my LPC1754 Platform repository using this link (The driver is in Drivers/lpc17xx/i2c.c, the include file in Drivers/inc/i2c.h)
Note that although this project is created for the lpc17xx it an also be used for the lpc11xx or 13xx devices since these use the same I2C interface.

If you are not able to view the complete picture from the logic anaylzer, download the image using this link.