Monthly Archives: Červen 2012

Raspberry Pi (3) – I2C interface + cheap batery backed real time clock (RTC) module – step-by-step how-to

Raspberry Pi has no RTC (real time clock) module. If you start it without internet connection, time is incorrect.

(This is not exact – there is a smart hack in Debian squeeze image. If you boot without internet connection, time is set to the time of last shutdown. So time is always bigger than any time reported before – not rolling backward to 1970/1/1.)

No RTC means that RPi is not usable as autonomous „data logger“ where data will be transferred offline (or connectivity will be available only time-to-time).

How to solve it? Get some RTC module, of course.

 

Hardware

I’ve found extra cheap batery backed RTC module in chinese e-shop – for less than 6 USD, with free shipping to Europe.

RTC module is based on PCF8563 chip (datasheet) with crystal and backup battery. Interface is I2C and chip is 3.3V-compatible – so no voltage level conversion is need for connecting to RPi!

So you have to connect it simple – VCC to 3.3V pin on RPi, and GND, SDA and SCL to their equivalents on RPi. That’s all.

Dsc_2050

Software

Standard RPi debian squeeze kernel has no support for I2C (at 2012/06/06 – may change in future).

But this man did some work for us.

I did the following steps:

1) I ran rpi-update to get the newest firmware and kernel (3.2.18), with memory split 192 MB.

2) Downloaded patched kernel (linux-image-3.2.18-rpi1+_5_armel.deb) from link there. (Always check home page for versions equal to your kernel/firmware version!)

3) Ran

dpkg -i linux-image-3.2.18-rpi1+_5_armel.deb 

which created /boot/vmlinuz-3.2.18-rpi1+

4) Copied /boot/vmlinuz-3.2.18-rpi1+ to /boot/kernel.img and restarted.

Nothing bad happens after RPi restart – the system is running as good as before.

So it’s a time for I2C experiments.

5) I think that almost all commands needs root privileges. So:

sudo bash

and all the following commands will be ran in privileged mode.

6) We need to load I2C drivers first:

modprobe i2c-dev

And then    /dev/i2c-0  and /dev/i2c-1 appears magically. I2C bus 0 is connected to GPIO connector, bus 1 is in camera connector (so unusable for me atd this time).

7) I2C is present. We need some tools for manipulating it.

apt-get install i2c-tools

8) Let’s try it:

i2cdetect 0

WARNING! This program can confuse your I2C bus, cause data loss and worse!

I will probe file /dev/i2c-0.

I will probe address range 0x03-0x77.

Continue? [Y/n] 

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f

00:          — — — — — — — — — — — — —

10: — — — — — — — — — — — — — — — —

20: — — — — — — — — — — — — — — — —

30: — — — — — — — — — — — — — — — —

40: — — — — — — — — — — — — — — — —

50: — 51 — — — — — — — — — — — — — —

60: — — — — — — — — — — — — — — — —

70: — — — — — — — —

pi@raspberrypi:~$

Yes! There is device shown on the I2C bus! I’m so happy!

OK. I’ve got RTC module connected to working I2C bus, and I’ve got datasheet for chip. So it’s time to start coding some application for reading/writing the time from/to RTC module, right?

Oh … wait … somebody did it before, didn’t it? Let’s use the f* Google… wow! It is there! And it is much better than I think.

9) We have to tell the linux kernel, that the device on address 0x51 on I2C bus 0 is RTC chip PCF8563:

echo pcf8563 0x51 > /sys/class/i2c-adapter/i2c-0/new_device

10) And then … magic happens:

hwclock -r

Wed 06 Jun 2012 18:08:46 BST  -0.057931 seconds

It works! With no coding at all!

 

Real world test

So I tried the whole trick with freshly booted RPi with no internet conectivity. Current (real) time was 18:07.

 

pi@raspberrypi:~$ sudo bash

root@raspberrypi:/home/pi# date

Wed Jun  6 17:52:28 BST 2012

There we see that the system clock is incorrect – it is the time of the last shutdown. Smart trick from debian squeeze (much better than Fedora, which always starts at 1970/1/1), but we need better time!

root@raspberrypi:/home/pi# modprobe i2c-dev

root@raspberrypi:/home/pi# echo pcf8563 0x51 > /sys/class/i2c-adapter/i2c-0/new_device

root@raspberrypi:/home/pi# hwclock -r

Wed 06 Jun 2012 18:08:46 BST  -0.057931 seconds

Time in RTC is correct. Battery backup is working.

root@raspberrypi:/home/pi# date

Wed Jun  6 17:53:33 BST 2012

But system time is still bad…

root@raspberrypi:/home/pi# hwclock -s

… so let set it from the RTC

root@raspberrypi:/home/pi# date

Wed Jun  6 18:09:24 BST 2012

System time is right!

Dsc_2051

 

 

 

 

Reklamy

1 komentář

Filed under Počítače, vývoj HW a SW