January 8, 2018

Adding RTC to OpenWrt devices (part 2)

Adding RTC to OpenWrt devices (part 2)

Some time ago I wrote how to add Real Time Clock (RTC) support to OpenWrt.

Currently support for RTC chip DS1370 is still not 100% there in OpenWrt because there is a bug that causes OpenWrt builder to just skip over compiling RTC kernel modules. Tools are there, driver is there, but you have to compile them manually.

In this article I'll give detailed step by step instructions on how to compile all needed kernel modules and to get them up and running.

Compile OpenWrt firmware

First step is to download whole OpenWrt source tree from github and to do basic steps for configuring default config make file:

git clone git@github.com:openwrt/openwrt.git
./scripts/feeds update -a
./scripts/feeds install -a
make menuconfig

and to add few config options via make menuconfig, in this example I used Carambola 2 board so I configured ar71xx as targed and carambola2 as device.

echo "CONFIG_TARGET_ar71xx=y
CONFIG_TARGET_ar71xx_generic=y
CONFIG_TARGET_ar71xx_generic_DEVICE_carambola2=y
CONFIG_BUSYBOX_CUSTOM=y
CONFIG_PACKAGE_i2c-tools=y
CONFIG_PACKAGE_kmod-i2c-algo-bit=y
CONFIG_PACKAGE_kmod-i2c-core=y
CONFIG_PACKAGE_kmod-i2c-gpio=y
CONFIG_PACKAGE_kmod-i2c-gpio-custom=y
CONFIG_PACKAGE_kmod-rtc-ds1307=y" >> .config

make defconfig

Make sure to use correct TARGET and DEVICE for your own hardware.

Next step is to download all dependencies and then compile:

make download
ionice -c 3 nice -n19 make -j10
Install OpenWrt firmware

After compile finished you should have firmware image in bin/targets/ar71xx/generic/ If you use use some other target and not ar71xx then your folder is different.

RTC configuration

First add one new line to /etc/rc.local:

echo ds1307 0x68 > /sys/bus/i2c/devices/i2c-0/new_device

and maka sure i2c via bitbanging is correctly initialized:

echo "i2c-gpio-custom bus0=0,19,18" > /etc/modules.d/i2c-gpio-custom

In my example gpio19 is configured as SDA, and gpio18 is configured as SCL.

RTC testdrive

If you get a response from i2cdetect -y 0 then you can start using your RTC hardware clock:

root@OpenWrt:~# 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: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --
Example
hwclock -r ... to "read" contents of RTC
hwclock -s ... to "set" time/date from contents of RTC
hwclock -w ... to "write" current date into RTC

Photo by: