Raspberry pi and i2c. Connecting a 24Cxx eeprom.

If you followed my previous blog entry, you have a nice raspberry pi system with support for i2c.

Now it is time to test if the i2c bus and the driver is working.
Let’s try to connect a i2c eeprom to the raspberry, since it is the only i2c device that I have in my drawer.

This code has been tested with a 24C16. It will probably work with smaller one, and bigger with code changes ( page size, and page number )

Connect the device

Easy, just connect the 3,3v, the gnd, the SCL and the SDA pins.


Load the i2c driver

modprob i2c-dev

You can also add the line i2c-dev to the /dev/modules file

Detecting the device.

root@raspberrypi:~# i2cdetect -y -a 0
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: 50 51 52 53 54 55 56 57 -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 

Reading the eeprom

For testing the reading I am using a test program from i2ctools

root# wget http://www.lm-sensors.org/browser/i2c-tools/trunk/eepromer/eeprom.c?format=txt
root# gcc –o eeprom eeprom.c
root# ./eeprom –r –f data
base-address of eeproms       : 0x50
number of pages to read       : 8 (0x50 .. 0x57)
file opened for writing       : d
            on filedescriptor : 3
i2c-devicenode is             : /dev/i2c-0
            on filedescriptor : 4

Read 16 bytes from eeprom at 0x50, offset 00000000
...
Read 16 bytes from eeprom at 0x57, offset 000000f0

The program should work out of the box, and the content of the eeprom must be save into data.
If you are using a 24C16 the file should be 2048 bytes length.

Writing the eeprom.

The program provided by lmsensor seems to be bugged, basically when it write to the eeprom, we must way until the content has been written before continuing.
This is well documented in the datasheet (http://www.bookly.com/images/24C64-BM.pdf ) as Acknowledge (ACK) Polling
Is it a bug in the driver, or in the application level ?
Anyway, I find a solution, I simply reposition the eeprom read-pointer until it succeded.
If you have a more elegant solution, I will be glad to hear about it.

root# wget https://raw.github.com/dgallot/i2c-tools/master/eepromer/eeprom.c
root# gcc –o eeprom eeprom.c
root# ./eeprom -f data -w
base-address of eeproms       : 0x50
number of pages to read       : 8 (0x50 .. 0x57)
file opened for reading       : data
            on filedescriptor : 3
i2c-devicenode is             : /dev/i2c-0
            on filedescriptor : 4

**WARNING**
 - 	You have chosen to WRITE to this eeprom.
	Make sure that this tiny chip is *NOT* vital to the
	operation of your computer as you can easily corrupt
	the configuration memory of your SDRAM-memory-module,
	your IBM ThinkPad or whatnot...! Fixing these errors can be
	a time-consuming and very costly process!

Things to consider:
 - 	You can have more than one i2c-bus, check in /proc/bus/i2c
	and specify the correct one with -d
	right now you have chosen to use '/dev/i2c-0'
 - 	A eeprom can occupy several i2c-addresses (one per page)
	so please make sure that there is no vital eeprom in your computer
	sitting at addresses between 0x50 and 0x57
Enter 'yes' to continue:yes

Wrote 16 bytes to eeprom at 0x50, offset 00000000
... acked 
Wrote 16 bytes to eeprom at 0x50, offset 00000010
..... acked 
Wrote 16 bytes to eeprom at 0x50, offset 00000020
...... acked 
Wrote 16 bytes to eeprom at 0x50, offset 00000030
...... acked 
Wrote 16 bytes to eeprom at 0x57, offset 000000f0
....... acked 

Credits:

http://elinux.org/RPi_Low-level_peripherals
http://www.lm-sensors.org/browser/i2c-tools/trunk/eepromer/eeprom.c

11 Responses to “Raspberry pi and i2c. Connecting a 24Cxx eeprom.”


Leave a Reply