RTC

From linux-sunxi.org
Jump to navigation Jump to search

Please note that this guide is written for AllWinner H3 although the concepts could be applied to others. There are many H3 boards and some of them have on-board RTC clock while others dont.

On-Board RTC

For Boards that have on-board RTC, the kernel will enumerate I2C device to /dev/rtc1 because /dev/rtc0 is occupied by built-in Allwinner's RTC (sunxi-rtc). That means you have to modify a symlink "/dev/rtc" to point to /dev/rtc1 instead of /dev/rtc0 like this: sudo ln -f -s /dev/rtc1 /dev/rtc

After that standard hwclock will work fine however its recommended to use an init-script to make sure time is automatically adjusted. Here is an example (/etc/init.d/rtc_ds1307)

#! /bin/sh
### BEGIN INIT INFO
# Provides:          rtc_ds1307
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: DS1307 real-time clock usage script
# Description:       This file should be used to construct scripts to be
#                    placed in /etc/init.d.
### END INIT INFO
# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="ds1307_rtc maintenance service"
do_start()
{
       echo "Selecting correct  RTC instance "
       echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device
       sudo ln -f -s /dev/rtc1 /dev/rtc
       echo "Syncing system time to RTC"
       sudo hwclock -s
}
do_stop()
{
       echo "Syncing RTC to system time"
       sudo hwclock -w
}
case "$1" in
 start)
       do_start
       ;;
 stop)
       do_stop
       ;;
 status)
       echo "RTC time:"
       hwclock -r
       echo "System time:"
       date
       ;;
 restart|force-reload)
       do_stop
       ;;
 *)
       echo "Usage: rtc_ds1307 {start|stop|status|restart}" >&2
       exit 3
       ;;
esac

No on-board RTC

Nanopi M1 is one board where it doesnt have on-board RTC clock. I have used DS1307 and DS3231 IC based RTC Clocks and both work. Make you include the I2C Kernel Modules in the kernel config namely under I2C RTC drivers CONFIG_RTC_DRV_DS1307=y and CONFIG_RTC_DRV_DS3232=y. Please understand DS1307 module supports both DS1307 and DS3231. Next after booting type the following line which will help add make the RTC module load at boot time as a new device. Second command help us to test the RTC module.

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device
hwclock -r

Here is how its supposed to be connected = Nanopi m1 to RTC Clock

  • RTC Pin Spec
Board-Pin# Name RTC-Pin# Name
1 SYS_3.3V VCC
3 I2C0_SDA SDA
5 I2C0_SCL SCL
7 GPIOG11 DoNOTConnect
9 GND GN

Please note in-case of on-board RTC, make you use I2C1_SDA, I2C1_SCL etc.

To ensure that the device is created at boot and the time is set from the RTC, we edit /etc/rc.local :

 sudo vi /etc/rc.local

and add the following lines before ‘exit 0’ (change /i2c-0/new_device to i2c-1/new_device if required):

 echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device
 hwclock -s

Trouble Shooting Commands

ls -l /sys/bus/i2c/devices/i2c-*
dmesg

some "other" notes:

sun4i has an internal RTC sun5i does not have an internal RTC, rtc-sun5i.c is a chopped up rtc-pcf8563.c with some i2c stuff