January 17, 2014

Adding RTC to Embedded devices running OpenWrt (part 1)

We take real time clock (RTC) for granted, as all of our gadgets keep correct time even when we turn them off.

Now imagine that you have to set correct time each time you power on any of your gadgets, that would be a nightmare, right? :)

Most of smaller devices like routers we use don't have RTC onboard, and as I have been using OpenWrt on lots of different devices I know that OpenWrt can use bitbangging to simulate i2c protocol on GPIO pins.

With real time clock all of my smaller embedded gadgets would be one step closer to being independent as their bigger android and pc cousins.

There are really cheap RTC devices on Ebay, I got one based on DS1307 chip and AT24C32 memory which uses i2c protocol for communication.

First step is to get i2c working OpenWrt and then to connect these two devices.
Installing necessary drivers and tools is the first step:

opkg update
opkg install kmod-i2c-gpio-custom i2c-tools

Now choose which two GPIO pins you would like to use as i2c SDA and SCL pins. Choosing GPIO pins depends on which device you are using. Some devices has unused GPIO pins, but some don't and you will have to remove LEDs and use those pins as GPIO pins for i2c communication. For this example I'll create i2c bus0 with GPIO 3 and 4 pins as SDA and SCL pins.

insmod i2c-dev
insmod i2c-gpio-custom bus0=0,3,4

Now test that bus is correctly initialized and search the bus for i2c devices connected to the bus.

i2cdetect -l
i2cdetect 0

#  i2cdetect -y 0
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- 57 -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 6f 
70: -- -- -- -- -- -- -- --                         

root@OpenWrt:/# i2cdump -y 0 0x6f
No size specified (using byte-data access)
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f    0123456789abcdef
00: 05 00 00 01 01 01 01 80 00 00 00 00 00 01 01 01    ?..?????.....???
10: 01 00 00 00 01 01 01 01 00 00 00 00 00 00 00 00    ?...????........
20: 54 11 8a 61 00 21 00 49 a2 00 85 00 93 02 49 0a    T??a.!.I?.?.??I?
30: 0a c2 b7 a2 03 e0 05 00 a0 da 98 40 28 a0 18 20    ???????.???@(?? 
40: 88 60 8a 14 22 2e 08 00 42 45 ce 30 54 4c c2 4e    ?`??".?.BE?0TL?N
50: a6 25 08 60 82 93 05 81 82 30 28 a0 01 c4 06 b0    ?%?`?????0(?????
60: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
70: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
80: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
90: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
a0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
b0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
c0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
d0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
e0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
f0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX

To be continued.