Using i2c with a modded router is simple enough, if you have two spare GPIO then the module package kmod-i2c-gpio-custom allows selected GPIO pins to be bound to SCL and SDA respectively when the module is loaded.
However for inexplicable reasons the ability to bind an i2c RTC module to the Linux hardware clock is disabled by default by the OpenWRT configuration mechanism for ar71xx and other consumer router architectures, and there is no way to turn it on without patching!
Regardless, here is how to use an i2c RTC with the Carambola2 or any other ar71xx architecture router (e.g. WRTnode, etc.)
- Patch the file target/linux/ar71xx/generic/target.mk as follows:
Default12-FEATURES += squashfs+FEATURES += squashfs +rtc
- Patch the kernel configuration target/linux/ar71xx/config-3.xx where (xx depends on your version of OpenWRT) as follows:
- Note: the kernel configuration can be modified via the kernel build system using the command make kernel_menuconfig
- Note: add other kernel i2c RTC modules as required
- Add the module to your image:
- If you have previously built OpenWRT then remove the tmp/ directory, or the change ‘+rtc’ will be ignored and the DS1307 module will not be included in your image
- Run make defconfig
- Build your image: make -j2
If everything worked, then the the file /lib/modules/3.xx…/rtc-ds1307.ko should be in the resulting image
Following is an aggregation of information I was already able to find elsewhere on the net.
- Ensure that i2c-tools package is installed as well. This may require the ‘oldpackages’ feed.
- Configure the module as follows by creating a file /etc/modules.d/99gpio-i2c-rtc
- You can also put this file into files/etc/modules.d/99gpio-i2c-rtc for it to be automatically added to your image
- Create the following content, where in this example 18 == SDA pin id and 19 == SCL pin id
- There are additional arguments controlling delays, etc.; refer to package/kernel/i2c-gpio-custom/src/i2c-gpio-custom.c
- Create a script, /etc/init.d/rtc-driver to load the device driver and set the time.
Default123456789#!/bin/sh /etc/rc.commonlogger "Setup i2c RTC"echo ds1307 0x68 > '/sys/class/i2c-dev/i2c-0/device/new_device'if hwclock | grep 'Jan' | grep -q 2000 ; thenlogger "RTC appears to have a flat battery..."elselogger "RTC set hwclock"hwclock -sfi
- Create a symlink…
Default1ln -s /etc/init.d/rtc-driver /etc/rc.d/S11rtc-driver
- Note, if you are running ntp that will take over anyway, but for system with an intermittent or no network connection, or if the network is down on boot, the RTC will provide a better time than 1 Jan 2012 or whatever…
You can test the above out before scripting it by booting the system and manually stepping through:
modprobe i2c-gpio-custom bus0=0,18,19
echo ds1307 0x68 > '/sys/class/i2c-dev/i2c-0/device/new_device'
PS Dont forget pullup presistors, and take care interfacing between 5V and 3.3V systems and peripherals…