Quantcast
Channel: Elevendroids' Blog » Raspberry Pi
Viewing all articles
Browse latest Browse all 6

Setting up hardware RTC in Raspbian

$
0
0

My Raspberries are expanded with DS3231 I2C realtime clock. It’s a very simple solution, as only required external component is a backup battery (crystal is integrated into the chip!). And it’s also claimed to be very accurate :)

By default, Raspbian installation uses “fake hwclock” to read previously stored date/time from file just before clock is synced by NTP client. We’re going to switch to “full” hwclock, first ensuring that our RTC is seen by kernel.

First, modify /etc/init.d/hwclock.sh to initialize clock’s i2c device.
Create RTC initialization function:

init_rtc_device()
{
  [ -e /dev/rtc0 ] && return 0;

  # load i2c and RTC kernel modules
  modprobe i2c-dev
  modprobe rtc-ds1307

  # iterate over every i2c bus as we're supporting Raspberry Pi rev. 1 and 2
  # (different I2C busses on GPIO header!)
  for bus in $(ls -d /sys/bus/i2c/devices/i2c-*);
  do
    echo ds1307 0x68 >> $bus/new_device;
    if [ -e /dev/rtc0 ];
    then
      log_action_msg "RTC found on bus `cat $bus/name`";
      break; # RTC found, bail out of the loop
    else
      echo 0x68 >> $bus/delete_device
    fi
  done
}

Call it from the “start” section.
We also have to comment out/remove udev check as our RTC won’t be handled by it.

...
init_rtc_device

# Raspberry Pi doesn't have udev detectable RTC
# if [ -d /run/udev ] || [ -d /dev/.udev ]; then
# return 0
# fi
...

Next, reboot and check if /dev/rtc0 is present and/or “hwclock -r” successfully gets time from RTC.
If everything seems working OK, our RasPi will get proper time even without network connectivity or with ntp client disabled.

We can now disable fake-hwclock from running:

# update-rc.d fake-hwclock remove

FacebookTwitterGoogle+LinkedInRedditShare

Flattr this!


Viewing all articles
Browse latest Browse all 6

Trending Articles