philipBC
Posts: 14
Joined: Fri Aug 12, 2016 1:15 pm

RP3 Raspian Jessie Lite install RTC DS3231

Fri Aug 12, 2016 2:27 pm

I have spent several days in forums looking for the 'right way' to install an RTC onto my RPi3 which is running the latest flavour of Raspbian Jesse Lite. I have found many of the explanations are out of date due to the arrival of Device Tree support in Rasbian Jessie and I do not want to disable Device Tree. I also found that many of the explanations do not work, in various circumstances.

My RTC is based on the DS3231 chip and fits directly on the GPIO pins of the RPi3.

My requirements are to ensure upon boot that I have the correct time as my RPi is used for a datalogging application. Normally I will get access to an NTP server using the inbuilt WiFi interface... BUT I dont want to wait for it or rely upon it being present.

Without an RTC or access to NTP the RPi relies upon fake-hwclock to provide the system time at boot. This package writes the system time to a file once per hour while the system is running and then reads it back upon boot. If your power outtage has been several hours, then that is how far out your system clock will be.

To install a DS3231 RTC on a RPi3 running Raspbian Jessie (with Device Tree) most writers agree that you need to
1. Use raspi-config to enable i2c support
2. Add to /boot/config.txt.... dtoverlay=i2c-rtc,ds3231

Once this is done and reboot then hwclock can access the installed RTC.

However ... the NTP daemon (enabled by default), fake-hwclock and the kernel boot process do not behave well with one another.

Many writers suggest removing fake-hwclock, but if you do then the RTC seems to be corrupted by the initialisation of the NTP daemon. Disabling the NTP daemon after removing fake-hwclock works, but that leaves you without NTP support.

My recommendation is ...

1. Enable i2C support using raspi-config

Code: Select all

[sudo] raspi-config
2. Add to the /boot/config.txt file

Code: Select all

dtoverlay=i2c-rtc, ds3231
3. Remove the fake-hwclock package

Code: Select all

[sudo] apt-get remove fake-hwclock
4. Edit the /lib/udev/hwclock-set file as follows.

Comment out these three lines

Code: Select all

if [ -e /run/systemd/system ] ; then
     exit 0
fi
Then comment out any lines containing --systz
My resultant file looks like this

Code: Select all

#!/bin/sh
# Reset the System Clock to UTC if the hardware clock from which it
# was copied by the kernel was in localtime.

dev=$1

# Commented out to support Raspian Jessie with Device Tree
# Prvents coruption of RTC by NTP start-up
# if [ -e /run/systemd/system ] ; then
#     exit 0
# fi

if [ -e /run/udev/hwclock-set ]; then
    exit 0
fi

if [ -f /etc/default/rcS ] ; then
    . /etc/default/rcS
fi

# These defaults are user-overridable in /etc/default/hwclock
BADYEAR=no
HWCLOCKACCESS=yes
HWCLOCKPARS=
HCTOSYS_DEVICE=rtc0
if [ -f /etc/default/hwclock ] ; then
    . /etc/default/hwclock
fi

if [ yes = "$BADYEAR" ] ; then
# Commented out - see above
#    /sbin/hwclock --rtc=$dev --systz --badyear
    /sbin/hwclock --rtc=$dev --hctosys --badyear
else
# Commented out - see above
#    /sbin/hwclock --rtc=$dev --systz
    /sbin/hwclock --rtc=$dev --hctosys
fi

# Note 'touch' may not be available in initramfs
> /run/udev/hwclock-set

5. Reboot

Upon reboot the kernel udev process should load the RTC time to the System Time.
Then the NTP daemon is started and will write the system time back to the RTC at roughly 15 minute intervals.
Configure your own selection of NTP servers or use the debian,pool.ntp.org defaults.

I expect that other flavors of RTC can be handled so long as Raspian has an overlay file for your RTC chip. I have only been able to test this method for my DS3231.

This solution was adapted from advice at https://afterthoughtsoftware.com/products/rasclock on installing a RasClock RTC, with my own adjustments made for the different RTC board/chip. It took a long while to find that page via google search. I hope this post proves useful to others in the Raspbian Community.

Do let me know how this works for you.

wh7qq
Posts: 1448
Joined: Thu Oct 09, 2014 2:50 am

Re: RP3 Raspian Jessie Lite install RTC DS3231

Sun Aug 14, 2016 1:12 am

This may be one of the reasons if not the reason I abandoned Jessie Lite and bit the bullet installing the Full Raspbian Jessie for a headless B+ I run for water heater control. I found there was just too much stuff missing. I'll try to remember this if I ever try that route again.

philipBC
Posts: 14
Joined: Fri Aug 12, 2016 1:15 pm

Re: RP3 Raspian Jessie Lite install RTC DS3231

Thu Aug 18, 2016 6:31 pm

In addition my earlier post, I have arranged that my Pi can become a local network NTP server even when it cannot connect to the internet by adding the local RTC to the NTP configuration.

In /etc/ntp.conf add to the list of servers the following lines

Code: Select all

server 127.127.1.0
fudge 127.127.1.0 stratum 10
Upon boot the system reads the time from the RTC and ntpq -p will report that the NTP daemon is using the .LOCL. server. This is indicated by an asterisc (*) in the first column of the output. Once NTP establishes contact with the other configured servers and measures their offset, drift etc it will switch away from using .LOCL. (it has been configured as a stratum 10 server) as there are better servers available.

This is my typical output of ntp-q

Code: Select all

     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 LOCAL(0)        .LOCL.          10 l 1291   64    0    0.000    0.000   0.000
*cdns01.plus.net 195.66.241.2     2 u   83  128  377   57.748   -3.035   1.997
+77.92.75.253    45.162.145.187   3 u   75  128  377   62.966   -0.941   0.620
+ntp1.exa-networ 195.66.241.10    2 u   84  128  177   68.115   -2.112   0.325
-rilynn.me.uk    81.2.117.235     2 u   15  128  337   63.168    2.425  21.584
-lon.jonesey.net 81.174.136.35    2 u  113  128  377   58.497    0.892   1.841
The RTC will not be used again until the next reboot.

In addition the Raspbian Jesse Lite kernel is configured to check every 11 minutes if NTP is in synchronised. If it is in SYNC then it will update the RTC with the current System Time (the equivalent of using [sudo] hwclock -w. So if the RTC drifts it will be reset to the system time every 11 minutes.

The address 127.127.1.0 is not a real IP address, but a flag used by NTPD to indicate the local RTC.

To check if this is working for you... set the RTC to a silly date & time with

Code: Select all

[sudo] hwclock --set --date="2011-08-14 16:45:05"
then check the time on the RTC with

Code: Select all

[sudo] hwclock
after 11 minutes you should find the RTC has been reset by the Kernel to the System Time.

Finally, if you need to know if the NTP Daemon is Synchronised then try using

Code: Select all

ntp-wait -v -n 5
This function will only return when NTP is in SYNC or it times out. The -v option should tell you which occurred. Option -n 5 selects five tries at the default interval of 6 secs.

Return to “Troubleshooting”