fruit-uk
Posts: 609
Joined: Wed Aug 06, 2014 4:19 pm
Location: Suffolk, UK

Re: The Correct way to add a RTC

Mon Nov 02, 2015 7:30 pm

Mm... indeed it does say that - and I missed it completely!

That section appears to set hwclock based on the current timezone time then immediately after
sets hwclock from system time.

It does seem rather odd to my small brain!

man hwclock on this wheeezy desktop says

Code: Select all

--systz
[snip]
This is an alternate option to --hctosys  that does not read the hardware clock, and may be used in system startup scripts for recent 2.6 kernels where you know the System Time contains the Hardware Clock time.
So why both?

Edit:
There is an explanation of why here https://lists.debian.org/debian-kernel/ ... 00207.html though I haven't checked for other links

Al Fernandez
Posts: 1
Joined: Thu Nov 12, 2015 1:09 pm

Re: The Correct way to add a RTC

Thu Nov 12, 2015 3:33 pm

Really useful post -- it's helped me a lot setting up a hardware clock based on bq32000. This chip can use a capacitor or a standard battery as backup supply. If you choose battery backup, you can set it up as a DS1307 by following instructions posted earlier on this thread.
If, like me, you go for a capacitor backup, you'll want to use the bq32k driver which supports trickle charging. Options are passed to the driver via the device tree and it took some trial an error to get everything right. I'm posting the detailed instructions here in case someone else wants to experiment with this chip.

Step 1: Enable i2c and install i2c tools as documented earlier on this thread. Check that your rtc is detected:

Code: Select all

pi@raspi ~ $ i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- 18 -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- 77                         
If at this point you get UU instead of 68, there’s a driver using the rtc. Check the output of lsmod -- you don’t need to load any rtc driver via /etc/modules.

Step 2: We’ll now create an overlay with support for bq32000 and we need a device tree compiler for that:

Code: Select all

pi@raspi ~ $ sudo apt-get install device-tree-compiler 	
Create a .dts file from i2c-rtc-overlay.dtb in /boot/overlays. Give the output file a new name to reflect support for bq32000:

Code: Select all

pi@raspi ~ $ sudo dtc -I dtb -O dts -o /tmp/i2c-rtc-bq32k-overlay.dts /boot/overlays/i2c-rtc-overlay.dtb
Now it's time to edit the .dts file. Follow the existing tree structure and paste this entry:

Code: Select all

bq32000@68 {
        compatible = "ti,bq32000";
        trickle-resistor-ohms = <1120>;
        reg = <0x68>;
};
For syntax and more available options check out this link.
The configuration above enables trickle charging. DO NOT enable trickle charging if using battery backup!
Next, convert the .dts file to .dtb:

Code: Select all

pi@raspi ~ $ sudo dtc -I dts -O dtb -o /boot/overlays/i2c-rtc-bq32k-overlay.dtb /tmp/i2c-rtc-bq32k-overlay.dts
Step 3: Get rid of fake-hwclock.

Code: Select all

pi@raspi ~ $ sudo apt-get remove fake-hwclock
pi@raspi ~ $ sudo update-rc.d -f fake-hwclock remove
pi@raspi ~ $ sudo rm /etc/fake-hwclock.data
Unplug the network cable and reboot. The system date will go back to Jan 1st 1970 00:00:00.

Step 4: Add "dtoverlay=i2c-rtc-bq32k,bq32000” to /boot/config.txt. Reboot and check that your rtc is accessible via hwclock.

Code: Select all

pi@raspi ~ $ sudo hwclock -r
Your system is now ready to get its time from the hardware clock. However, the rtc gets updated in the early stages of booting so it’ll end up on Jan 1st 1970 even when the Pi is connected to the internet and can get its time via ntp. A small change in hwclock-set will fix this:

Code: Select all

pi@raspi ~ $ sudo nano /lib/udev/hwclock-set
Comment the following three lines near the top of the script:

Code: Select all

#if [ -e /run/systemd/system ] ; then
#    exit 0
#fi
Update the hwclock manually:

Code: Select all

pi@raspi ~ $ sudo hwclock -w
Unplug the network cable and reboot. The time should now be correct on both the Pi and hwclock.
This is it. No need to use the hwclock.sh as mentioned earlier on this thread. In fact, on my system it just doesn't run, even when enabled in rc.d. I'm using the latest release of raspbian:

Code: Select all

pi@raspi ~ $ uname -a
Linux raspi 4.1.12+ #825 PREEMPT Fri Nov 6 17:53:21 GMT 2015 armv6l GNU/Linux

gregsmith_to
Posts: 15
Joined: Mon May 14, 2012 1:56 am

Re: The Correct way to add a RTC

Sun Nov 15, 2015 10:35 pm

Al Fernandez, that looks exactly what I was looking for - I've been trying to sort through all the old info for hours - and I have the situation you describe where the time goes to Jan 1 1970 at powerup.
But I can't comment out the lines you specify since I don't have them, my /lib/udev/hwclock-set looks like the below (and the /sys/fs/cgroup directory appears to be empty... I commented out those 3 lines anyway, it made no difference. ). I'm using 2015-05-05-raspbian-wheezy fresh from the image.

I've enabled i2c and drivers in raspi-config, and added "dtoverlay=i2c-rtc,ds1307" to config.txt, and after that hwclock seems to work; I see the i2c_bcm2708 and rtc_ds1307 drivers in lsmod.

And then I've removed fake-hwclock etc. and I can't get it to read the rtc at powerup. In fact the first 'hwclock -r' after reboot now always says the rtc is corrupt, which was not the case before I removed fake-hwclock -- which makes me wonder if hwclock was actually reading fake-hwclock before. I can 'repair' it by setting the time and doing hwclock -w, and then hwclock -r works but then it's corrupt again on reboot (even if I don't remove the power).

It would be nice to try i2cdetect on my machine, I installed it and it just points out that /dev/i2c(something) is not present. There is indeed no /dev/i2c*. I've been assuming this was using a now-obsolete dev interface - but you're the first really recent post I've seen which is using i2cdetect, so I guess that's not the case. Rather baffled.

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

if [ -e /sys/fs/cgroup/systemd ] ; 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
    /sbin/hwclock --rtc=$dev --systz --badyear
else
    /sbin/hwclock --rtc=$dev --systz
fi


nospam2000
Posts: 2
Joined: Wed Nov 18, 2015 10:05 pm

Re: The Correct way to add a RTC

Thu Dec 03, 2015 5:26 am

RBISWASX wrote: After this, in the boottime parameters and bootargs of RaspberryPi, add the following argument:
rtc.i2c=pcf8563,1,0x68
Which kernel version are you using? I can't find the code which reads the "rtc.i2c" parameter in the kernel source of version "raspberrypi 4.1.11-v7+". I think it has been replaced with the devicetree options and is no longer existent.

Michael

dontjinxit123
Posts: 1
Joined: Tue Mar 01, 2016 8:56 pm

Re: The Correct way to add a RTC

Tue Mar 01, 2016 9:02 pm

itsmedoofer wrote:Hi,

Really useful post thanks, I've consolidated into a list of instructions to save darting about all over the net....
----------------------

Enable the Kernel modules:-

Code: Select all

sudo nano /etc/modules
Add the following:-

Code: Select all

i2c-bcm2708 
i2c-dev
Save

Remove them from the blacklist by adding #

Code: Select all

sudo nano /etc/modprobe.d/raspi-blacklist.conf
Make sure you see:-

Code: Select all

#blacklist spi-bcm2708
#blacklist i2c-bcm2708
Save & reboot

Install the i2c tools for testing:-

Code: Select all

sudo apt-get install i2c-tools
Run a test to see if all is well... for a rev 1 pi :

Code: Select all

sudo i2cdetect -y 0

or for later, rev 2 Pi's :

Code: Select all

sudo i2cdetect -y 1

You should see something like:-

Code: Select all

pi@raspberrypi ~ $ sudo i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- UU -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
load up the RTC module and test by running :

Code: Select all

sudo modprobe rtc-ds1307

Code: Select all

sudo bash
For older rev 1 pi's :

Code: Select all

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device
exit
or for newer, rev 2 pi's :

Code: Select all

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
exit
Check the clock is running and can be read:-

Code: Select all

sudo hwclock -r
Make sure pi is connected to internet so correct time is set....

Write the current system time to the hardware clock:-

Code: Select all

sudo hwclock -w
Add the clock to the Kernel modules:-

Code: Select all

sudo nano /etc/modules
Add:-

Code: Select all

rtc-ds1307
Save

Reconfigure the hwclock.sh script:-

Code: Select all

sudo nano /etc/init.d/hwclock.sh
After "unset TZ" at top add..

Code: Select all

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
}
Find "case "$1" in" and edit as per:-

Code: Select all

case "$1" in
	start)
	    # If the admin deleted the hwclock config, create a blank
	    # template with the defaults.
	    if [ -w /etc ] && [ ! -f /etc/adjtime ] && [ ! -e /etc/adjtime ]; then
	        printf "0.0 0 0.0\n0\nUTC" > /etc/adjtime
	    fi
		init_rtc_device

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

Update the real HW Clock and remove the fake:-

Code: Select all

sudo update-rc.d hwclock.sh enable
sudo update-rc.d fake-hwclock remove
Now that real hardware clock is installed, remove the fake package and it’s crons:-

Code: Select all

sudo apt-get remove fake-hwclock
sudo rm /etc/cron.hourly/fake-hwclock
sudo rm /etc/init.d/fake-hwclock
Reboot
After addng init_rtc_device(), i get this error.

hwclock: Cannot access the Hardware Clock via any known method.
hwclock: Use the --debug option to see the details of our search for an access method.

why?

User avatar
GTR2Fan
Posts: 1601
Joined: Sun Feb 23, 2014 9:20 pm
Location: South East UK

Re: The Correct way to add a RTC

Tue Mar 01, 2016 9:58 pm

Code: Select all

hwclock -r
...won't work.

Code: Select all

sudo hwclock -r
...should work.
Pi2B Mini-PC/Media Centre: ARM=1GHz (+3), Core=500MHz, v3d=500MHz, h264=333MHz, RAM=DDR2-1200 (+6/+4/+4+schmoo). Sandisk Ultra HC-I 32GB microSD card on '50=100' OCed slot (42MB/s read) running Raspbian/KODI16, Seagate 3.5" 1.5TB HDD mass storage.

gordon77
Posts: 3026
Joined: Sun Aug 05, 2012 3:12 pm

Re: The Correct way to add a RTC

Tue Mar 01, 2016 10:11 pm

I just did the following and it appears to work....

1. Edit the file /boot/config.txt and add the following:
dtoverlay=i2c-rtc,ds1307

2. Edit the file /lib/udev/hwclock-set, commenting out the three lines:
#if [ -e /run/systemd/system ] ; then
# exit 0
#fi

Then set the clock, then
Sudo hwclock -w
Sudo hwclock -r

ingeniapp
Posts: 4
Joined: Thu Jan 21, 2016 3:07 pm
Location: Madrid, Spain

Re: The Correct way to add a RTC

Fri May 06, 2016 2:00 pm

Hi,

Why not run the hwcloch.sh script in runlevel S?
I see that when OS (raspbian) start runlevel is unknown. I typed "runlevel" and returns "unknown". I've added some echo lines in hwclock.sh but I have clearly this script not run. Only works if I call it in rc.local script (works with "sudo /etc/init.d/hwclock.sh start" and "sudo /etc/init.d/rcS")

How it works to you to launch scripts are stored in the runlevel S?

Thanks

shadowfxd
Posts: 4
Joined: Mon Aug 22, 2016 4:47 am

Re: The Correct way to add a RTC

Mon Aug 22, 2016 4:51 am

I have tried everything in the OP and I keep getting this error:

hwclock: The Hardware Clock registers contain values that are either invalid (e.g. 50th day of month) or beyond the range we can handle (e.g. Year 2095).

with two different RTC. If I do "sudo hwclock -w" it resyncs the RTC, but won't do on fresh boot or reboot. Can anyone help?

User avatar
m1geo
Posts: 10
Joined: Tue Jul 23, 2013 12:07 pm
Location: Elm Park, Greater London
Contact: Website

Re: The Correct way to add a RTC

Fri Sep 02, 2016 12:12 am

Hello all.

I, too, am getting the error:
george@camera:~ $ sudo hwclock -r --debug
[sudo] password for george:
hwclock from util-linux 2.25.2
Using the /dev interface to the clock.
Last drift adjustment done at 1472774297 seconds after 1969
Last calibration done at 1472774297 seconds after 1969
Hardware clock is on UTC time
Assuming hardware clock is kept in UTC time.
Waiting for clock tick...
/dev/rtc does not have interrupt functions. Waiting in loop for time from /dev/rtc to change
...got clock tick
Time read from Hardware Clock: 2066/01/01 00:00:36
Invalid values in hardware clock: 2066/01/01 00:00:36
hwclock: The Hardware Clock registers contain values that are either invalid (e.g. 50th day of month) or beyond the range we can handle (e.g. Year 2095).
What I've got:

Latest Rasbian image `Raspbian GNU/Linux 8 (jessie)`.
george@camera:~ $ uname -a
Linux camera 4.4.13-v7+ #894 SMP Mon Jun 13 13:13:27 BST 2016 armv7l GNU/Linux
george@camera:~ $
What I've done:

- DS3231 correctly fitted (I had it working once, briefly, but it stopped on reboot).
- Added "dtoverlay=i2c-rtc,ds3231" to /boot/config.txt
- Commented out the 3 udev lines from /etc/init.d/hwclock.sh
- Disabled fake-hwclock, and enable hwclock.sh (ran the exact commands from allfox's post, they all completed succesfully)
- Also deleted /etc/fake-hwclock.data
- Rebooted (several times)

What I have NOT done:

- included `init_rtc_device` into `/etc/init.d/hwclock.sh` (not used as instructions from allfox didn't say to)

Upon booting, the rtc0 device is found:
george@camera:~ $ dmesg | grep -i rtc
[ 3.010136] rtc-ds1307 1-0068: rtc core: registered ds3231 as rtc0
george@camera:~ $
When I checked the system date, I found that the Pi system time was 1 Jan 1970. The RTC was set correctly, so I used NTP to re-sync the system time, as follows.
george@camera:~ $ sudo service ntp stop
george@camera:~ $ sudo ntpd -gq -c /etc/ntp.conf
ntpd: time set +1472772209.221351s
george@camera:~ $ sudo service ntp start
george@camera:~ $ date
Fri 2 Sep 00:51:20 BST 2016
george@camera:~ $
I also added `tinker panic 0` which should stop NTP from worrying about very large (50 years) offsets in daemon mode. Setting the time with `--debug` went as follows:
george@camera:~ $ sudo hwclock -w --debug
hwclock from util-linux 2.25.2
Using the /dev interface to the clock.
Last drift adjustment done at 630 seconds after 1969
Last calibration done at 630 seconds after 1969
Hardware clock is on UTC time
Assuming hardware clock is kept in UTC time.
Waiting for clock tick...
/dev/rtc does not have interrupt functions. Waiting in loop for time from /dev/rtc to change
...got clock tick
Time read from Hardware Clock: 2066/01/01 00:25:20
Invalid values in hardware clock: 2066/01/01 00:25:20
missed it - 1472773728.725485 is too far past 1472773728.500000 (0.225485 > 0.001000)
1472773729.500000 is close enough to 1472773729.500000 (0.000000 < 0.002000)
Set RTC to 1472773729 (1472773728 + 1; refsystime = 1472773728.000000)
Setting Hardware Clock to 23:48:49 = 1472773729 seconds since 1969
ioctl(RTC_SET_TIME) was successful.
Not adjusting drift factor because the Hardware Clock previously contained garbage.
george@camera:~ $
It now reads back correctly:
george@camera:~ $ sudo hwclock -r
Fri 02 Sep 2016 00:54:47 BST -0.029005 seconds
george@camera:~ $
However, after rebooting, the system time is okay, but the hardware clock is screwed again, but writing the correct system time back to the hardware and then re-reading the hardware cures it (until another reboot).
george@camera:~ $ date
Fri 2 Sep 00:56:32 BST 2016
george@camera:~ $ sudo hwclock -r --debug
hwclock from util-linux 2.25.2
Using the /dev interface to the clock.
Last drift adjustment done at 1472773728 seconds after 1969
Last calibration done at 1472773728 seconds after 1969
Hardware clock is on UTC time
Assuming hardware clock is kept in UTC time.
Waiting for clock tick...
/dev/rtc does not have interrupt functions. Waiting in loop for time from /dev/rtc to change
...got clock tick
Time read from Hardware Clock: 2066/01/01 00:01:29
Invalid values in hardware clock: 2066/01/01 00:01:29
hwclock: The Hardware Clock registers contain values that are either invalid (e.g. 50th day of month) or beyond the range we can handle (e.g. Year 2095).
george@camera:~ $
george@camera:~ $ date
Fri 2 Sep 00:58:10 BST 2016
george@camera:~ $ sudo hwclock -w
george@camera:~ $ sudo hwclock -r
Fri 02 Sep 2016 00:58:22 BST -0.925611 seconds
george@camera:~ $
It's almost as if the hardware clock is set correctly from previous boot, then re-set when the Pi3 reboots anew, but BEFORE NTP has synced the clock, thus pushing 1970 back into the hardware clock.

Any help on this would be greatly appreciated.

George
M1GEO
George Smart, M1GEO.
http://www.george-smart.co.uk/

buruhazard
Posts: 1
Joined: Mon Sep 22, 2014 5:56 pm

Re: The Correct way to add a RTC

Thu Sep 29, 2016 9:56 pm

After I did all of this:
"hwclock: Cannot access the Hardware Clock via any known method."

Any idea?

mikebar
Posts: 2
Joined: Thu Aug 24, 2017 5:26 pm

Re: The Correct way to add a RTC

Thu Aug 24, 2017 5:54 pm

After searching for a long while, and after a lot of experiments, I've putted togheter all the information I've collected all around the Net about how to connect a realtime clock to the Raspberry and allow the OS use it as system clock even if no connections to a NTP server.
I've used a DS3231, but the following should work even with a DS1307. Just use ds1307 where into the following instructions is used ds3231.

The following, definitively worked for me, using Raspbian Jessie. So:
edit the file using sudo nano /boot/config.txt and add or modify a line that looks like the following:

Code: Select all

dtoverlay=i2c-rtc,ds3231
Edit the file using sudo nano /etc/init.d/hwclock.sh and comment out the content in order to be sure that the content looks like the following:

Code: Select all

    #if [ -d /run/udev ] || [ -d /dev/.udev ]; then
    #    return 0
    #fi
Then disable the fake-hwclock and enable the hwclock.sh with the following group of commands:

Code: Select all

     sudo apt-get remove fake-hwclock
     sudo rm /etc/cron.hourly/fake-hwclock
     sudo update-rc.d -f fake-hwclock remove
     sudo rm /etc/init.d/fake-hwclock
     sudo update-rc.d hwclock.sh enable
Then modify the file using sudo nano /lib/udev/hwclock-set locate the following lines and be sure to comment it out:

Code: Select all

   # if [ -e /run/systemd/system ] ; then
   #    exit 0
   # fi
Now, create two system services in order to set the system's clock when the OS boot and also to write to the RTC at the shutdown. So, the first one:

Code: Select all

sudo nano /etc/systemd/system/hwclock-start.service
...and paste in it the following:

Code: Select all

 [Unit]
 Description=Set time from RTC on startup
 After=network.target
 
 [Service]
 Type=oneshot
 ExecStart=/sbin/hwclock -s
 TimeoutSec=0
 
 [Install]
 WantedBy=multi-user.target
and the second one:

Code: Select all

sudo nano /etc/systemd/system/hwclock-stop.service
Paste in it the following:

Code: Select all

 [Unit]
 Description=Synchronise Hardware Clock to System Clock
 DefaultDependencies=no
 Before=shutdown.target
 
 [Service]
 Type=oneshot
 ExecStart=/sbin/hwclock --systohc
 
 [Install]
 WantedBy=reboot.target halt.target poweroff.target
Finally, enable the services issuing once the following command:

Code: Select all

sudo systemctl enable hwclock-start hwclock-stop
You can also verify that the RTC module has been loaded using

Code: Select all

sudo lsmod

brunosso
Posts: 11
Joined: Mon Feb 09, 2015 12:03 pm

Re: The Correct way to add a RTC

Wed Nov 22, 2017 9:44 pm

mikebar wrote:
Thu Aug 24, 2017 5:54 pm

Now, create two system services in order to set the system's clock when the OS boot and also to write to the RTC at the shutdown.

...

and the second one:

Code: Select all

sudo nano /etc/systemd/system/hwclock-stop.service
Paste in it the following:

Code: Select all

 [Unit]
 Description=Synchronise Hardware Clock to System Clock
 DefaultDependencies=no
 Before=shutdown.target
 
 [Service]
 Type=oneshot
 ExecStart=/sbin/hwclock --systohc
 
 [Install]
 WantedBy=reboot.target halt.target poweroff.target
The second script doesn't work. I have a raspbian strech! If i run

Code: Select all

pi@raspberrypi:~ $ sudo systemctl status hwclock-stop.service
● hwclock-stop.service - Synchronise Hardware Clock to System Clock
   Loaded: loaded (/etc/systemd/system/hwclock-stop.service; enabled; vendor preset: enabled)
   Active: inactive (dead)
pi@raspberrypi:~ $

Return to “Raspbian”

Who is online

Users browsing this forum: No registered users and 22 guests