Archive for June, 2012

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
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 ( ) 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
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

 - 	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 


Building a new kernel for the raspberry pi, including support for i2c, spi and working version of the driver rtl8192cu.

This guide will show you the entire step that I had to do in order to produce my custom kernel. I wanted to have the latest kernel of Chris boot, which include support for spi and i2c, but I also need to include the latest realtek driver for my wifi dongle.

The Chris Boot’s website contains a lot of information, but it does not contains this information required to build his kernel from scratch by real beginner. I will try to fill the gap.


First install Ubuntu 12.04 LTS

Since I have a mac, I installed it on Parallels and it is working like a charm.

install the cross compiler and the dev tools

sudo apt-get install git-core gcc-4.6-arm-linux-gnueabi
sudo ln -s /usr/bin/arm-linux-gnueabi-gcc-4.6 /usr/bin/arm-linux-gnueabi-gcc
sudo apt-get install git-core gcc-4.6-arm-linux-gnueabi

Build the kernel

Download the source code

mkdir rpi-3.2.20
cd rpi-3.2.20
git clone
git clone -b rpi-3.2.20

Download the realtek driver RTL819xCU, and copy it ro the rpi-3.2.20 directory

Copy the default config from the previous kernel.

There are several way of doing that, you can start simply download the Chris Boot images, and install his latest kernel.
The config is located in /boot/config-3.2.20-rpi1+, or you can simply download it from my site config-3.2.20-rpi1+
Copy this to your ubuntu system as .config

cd ~/rpi-3.2.20/linux
cp config-3.2.20-rpi1+ .config

Change the kernel name, so it will not conflict with any other one.

Edit the Makefile and change the first line from :

To :

Import the config and disable the old rlt8192 module

make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- oldconfig
make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- menuconfig

Open the following menu :
Device Drivers
Network device support
Wireless LAN
And unselect the driver by typing [space]
Realtek RTL8192CU/RTL8188CU USB Wireless Network Adapter
Hit [Tab] to select exit multiple time, and save the configuration.

Build the kernel.

make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- -k -j5

Prepare the file to be copied to the system.

mkdir -p ~/rpi-3.2.20/install/boot
make ARCH=arm INSTALL_MOD_PATH=~/rpi-3.2.20/install CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- modules_install
make INSTALL_PATH=~/rpi-3.2.20/install/boot ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- INSTALLKERNEL=none install

Build the 8192cu kernel driver.

Copy and unpack the driver.

cd ~/rpi-3.2.20
cd RTL8188C_8192C_8192D_USB_linux_v3.4.2_3727.20120404/driver
tar -xvzf rtl8188C_8192C_8192D_usb_linux_v3.4.2_3727.20120404.tar.gz

Build the 8192cu kernel driver.

cd ~/rpi-3.2.20/RTL8188C_8192C_8192D_USB_linux_v3.4.2_3727.20120404/driver/rtl8188C_8192C_8192D_usb_linux_v3.4.2_3727.20120404
make KSRC=~/rpi-3.2.20/linux ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- -k -j5

Copy the 8192cu to the modules tree.

cp 8192cu.ko ~/rpi-3.2.20/install/lib/modules/3.2.20-spi-i2c-rpi1+/kernel/drivers/net/wireless

Install the new kernel

Now you simply need to install the new files to you sd card.
Plug you sd card, and if you are working from a virtual machine connect the sd card reader to the ubuntu vm.
The boot partition will be the fat partition, on my system it is mount into /media/00D7-4C15
The system partition is the ext2 partition, on my system it is mount into /media/1bee6d88-1731-433f-8f74-49a7c1ce1035

df -T
/dev/sdb1      vfat        102182   41764     60418  41% /media/00D7-4C15
/dev/sdb2      ext4       1824112 1054740    677980  61% /media/1bee6d88-1731-433f-8f74-49a7c1ce1035

Copy the new firmware

cp ~/rpi-3.2.20/firmware/boot/* /media/00D7-4C15

Copy the kernel and the modules

I really do not know if it is required to install the kernel in the boot partition and in the system partition ( in the /boot ).
But since it does not harm, I copy it on both location.

cp ~/rpi-3.2.20/install/boot/* /media/00D7-4C15
cp ~/rpi-3.2.20/install/boot/* /media/1bee6d88-1731-433f-8f74-49a7c1ce1035/boot
cp -R ~/rpi-3.2.20/install/lib/modules/* /media/1bee6d88-1731-433f-8f74-49a7c1ce1035/lib/modules

Activate the kernel

The latest firmware is not able to directly start the linux image, you do not need to create a disk image anymore.
So to activate it, simply override the kernel.img file with the new kernel.

cp /media/00D7-4C15/vmlinux-3.2.20-spi-i2c-rpi1+ /media/00D7-4C15/kernel.img

Final step

The only remaining step required to start you raspberry pi, and connect a keybord and a screen to it !

And issue a simple depmod command and reboot.

depmod -a

I delibered choosed to not merge the RTL819x driver to the kernel source tree.
Doing so slighly complicate the process, but if you want to do it check the website of lumux which explain how to do it.
If you do so, this step is not required :)

The next article will explain how to modify this image make it usable without a keyboard by doing

  • Run the depmod -a on the first boot
  • Do an offline configuration of the wifi
  • Unattented installation of the required package like ssh