This chapter provides a brief tutorial on how to make a DS3231 RTC real-time clock source work within UbuntuMATE Linux OS from release 15.10 onward, and is based on the original article "Reset RTC chip date & time during bootup".
To start with, you need the original DS3231 (or a similar) chip that you can get from any online auction store, and is depicted with the external link picture showing the DS3231 RTC chip.
This RTC chip needs to be plugged onto the GPIO pins of your Raspberry Pi board towards the boardś edge and on the inside corner with the RTC board pointing inwards as depicted with the external link picture showing the RTC DS3231 plugged on the Raspberry Pi board.
Before powering on the Raspberry Pi computer with the RTC chip hooked onto the board, the OS kernel must be instructed to recognise the add-on module and create the respective device file.
To accomplish this, the "/boot/config.txt" configuration needs to be amended with the following entry:
dtoverlay=i2c-rtc,ds3231
Once the computer has been powered up, and you're able to login, the UNIX command "dmesg" shall be fired on a "root" shell prompt, which will output the kernel startup log file, and an entry shall point out that a RTC chip has been recognised as exemplary with:
[ 5.033249] rtc-ds1307 1-0068: rtc core: registered ds3231 as rtc0
You may want to verify that the corresponding OS device file has been created by firing the command "ls -l /dev/rtc0" that list the corresponding file as follows:
crw------- 1 root root 254, 0 May 1 17:40 /dev/rtc0
From now onward, we're sure that the DS3231 chip is being recognised properly during kernel boot-up.
Note: Especially during first time installation, the RTC chip may not yet be synchronised with the OS or network time! Thus, you may need to reset the RTC chip time in UTC format at any later time; execute the following command on the shell prompt:
timedatectl set-local-rtc false
Next step will be to instruct the OS to read the date and time from the RTC source during a boot-up, and periodically synchronise its time from an NTP source if the computer is connected to the Internet.
For this purpose, we will be setting up the relevant startup services using the systemd daemon. If you are unfamiliar with systemd, checkout for some nice primers given with the articles "Systemd Essentials: Working with Services, Units, and the Journal", "Managing Services on Linux with systemd" and "Here We Go Again, Another Linux Init: Intro to systemd".
First, let's check whether the process has been already been setup by firing on the "root" shell prompt the following command:
systemctl is-active systemd-timesyncd
If the output of this command is "inactive", then we can go ahead with the following steps using a shell prompt with the UNIX user ID "root" and your favorite text file editor:
Step 1: Edit the file "/etc/systemd/timesyncd.conf" with the following entries:
[Time]
NTP=0.europe.pool.ntp.org 1.europe.pool.ntp.org 2.europe.pool.ntp.org 3.europe.pool.ntp.org
FallbackNTP=0.pool.ntp.org 1.pool.ntp.org 2.pool.ntp.org 3.pool.ntp.org
or, pending on the version of systemd the underlting OS is using, with the following entries:
[Time]
Servers=0.europe.pool.ntp.org 1.pool.ntp.org 2.pool.ntp.org 3.pool.ntp.org
You may choose the NTP sources according to your region as described with the "NTP Pool Time Servers" list or simply consult the "How do I use pool.ntp.org" instructions.
Step 2: Update the file "/etc/rc.local" by adding the following lines:
[ ! -e /var/lib/systemd/clock -a "`systemctl is-active systemd-timesyncd | grep -i active`" ] && timedatectl set-ntp 1 > /dev/null 2>&1
sleep 2
Note: Keep the back-quotes in the "systemctl" command; keep the "sleep" statement to allow enough time for the RTC chip to sync!
Step 3: Create a file “/etc/systemd/system/hwclock-sync.service” with the following contents:
[Unit]
Description=Time Synchronisation from RTC Source
After=systemd-modules-load.service
RequiresMountsFor=/dev/rtc
Conflicts=shutdown.target
[Service]
Type=oneshot
ExecStart=/sbin/hwclock -s
TimeoutSec=0
[Install]
WantedBy=time-sync.target
Step 4: Now we need to shutdown and disable services for "ntp" and "fake-hwclock" and remove the corresponding packages that come by default with the OS by executing the following set of commands on the "root" shell prompt:
systemctl stop fake-hwclock ntp
systemctl disable fake-hwclock ntp
apt-get -y remove fake-hwclock ntp
rm -f /etc/init.d/{fake-hwclock,ntp}
rm -f /etc/default/{fake-hwclock,ntp}
rm -f /etc/rc?.d/{S,K}??fake-hwclock
rm -f /etc/rc?.d/{S,K}??ntp
rm -f /etc/ntp.conf
rm -f /etc/cron.daily/ntp
rm -f /etc/cron.hourly/fake-hwclock
rm -f /var/log/ntpstats
rm -rf /var/log/ntpstats
rm -rf /var/lib/ntp
Step 5: To finally enable the automatic start-up of the RTC synchronisation during boot-up using the "systemd-timesyncd" service, execute the following commands on the shell prompt:
systemctl enable hwclock-sync systemd-timesyncd
systemctl start hwclock-sync systemd-timesyncd
timedatectl set-ntp 1
With this approach, we leave it up to systemd to start up the relevant services for date and time synchronisation from the RTC chip during boot up, and if the computer is also connected to the Internet, to synchronise the OS and RTC with the corresponding source of network time.
To verify its proper operation, after a little while, simply fire the UNIX command "timedatectl" on the shell prompt that shall output the following:
Local time: Wed 2015-11-18 09:44:46 CET
Universal time: Wed 2015-11-18 08:44:46 UTC
RTC time: Wed 2015-11-18 08:44:46
Time zone: … (CET, +0100)
Network time on: yes
NTP synchronized: yes
RTC in local TZ: no
Similar to the legacy "fake-hwclock" mechanism, a file "/var/lib/systemd/clock" will be created and regularly updated with the time of last clock synchronisation with the network time.
Above same steps can also be applied without an RTC chip to simply let the OS synchronise with the network time as described in the article "Use timedatectl to Control System Time and Date in Linux" and avoid using the OS default NTP service .
If you do not want to use systemd or your preferred Linux OS does not have it installed, you can still use the alternative first approach as described in my original article "UbuntuMATE 15.10 reset RTC chip date & time during bootup" and described with other articles "RasPi2 DS3231 RTC" and "Adding a Real Time Clock to your Raspberry Pi" taking into account potentially still ongoing bugs like "hwclock-set misconfigures kernel for NTP when RTC holds local time" or of a "broken hwclock package" as elaborated further in the forum posting with title "UbuntuMATE 15.10 keeps the time of last shutdown in RTC chip".
Hope this helps!