henryhanselscott
Posts: 107
Joined: Sun Jan 18, 2015 1:31 pm

Real time clock reset issue

Thu Jun 04, 2015 9:34 am

I have installed an RTC module like this one:
http://thepihut.com/products/mini-rtc-m ... spberry-pi


part of the configuration is like this
sudo nano /etc/rc.local

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

date
exit 0

The problem is that this causes the time on the RTC to reset back to default values. If I comment out "sudo hwclock -s" and leave everything else alone in the rc.local file it does not reset the RTC. After bootup is complete, I can then open the terminal and type sudo hwclock -s and the system time updates to the correct time and date from the RTC. So the RTC seems to be working correctly, but it is getting reset when it is accessed from rc.local, but not from the terminal after boot is complete. So is this a timing issue?

Help please!

Thanks,
Henry

rayjoh
Posts: 27
Joined: Thu May 23, 2013 11:48 am

Re: Real time clock reset issue

Thu Jun 04, 2015 7:20 pm

If I comment out "sudo hwclock -s" and leave everything else alone in the rc.local file it does not reset the RTC.
rc.local is running as root so there is no need for "sudo".

Check the battery. A new battery should be at ~3.1V. I have three of them in use with no problem. My rc.local calls this script (and one that sets hostname depending on serial number in /proc/cpuinfo).

Code: Select all

#!/bin/bash

modprobe i2c-dev
modprobe i2c-bcm2708
modprobe rtc-ds1307

revision=`cat /proc/cpuinfo | egrep '^Revision' | awk '{print $3}'`
if [ "$revision" = "0002" -o "$revision" = "0003" ] ; then
   i2c=0
else
   i2c=1
fi

rtc=`i2cdetect -y $i2c | egrep '^60: ' | cut -c 29-30`
if [ "X$rtc" = "X68" ] ; then
    echo "RTC found" > /tmp/rtc.txt
    echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-${i2c}/new_device
    ls /sys/class/i2c-adapter/i2c-${i2c}/. >> /tmp/rtc.txt
    if ls /sys/class/i2c-adapter/i2c-${i2c}/. | grep -q 0068 ; then
        hwclock -s >> /tmp/rtc.txt
    else
        echo "no hwclock" >> /tmp/rtc.txt
    fi
    echo "RTC done" >> /tmp/rtc.txt
fi
-- Raymond

User avatar
DougieLawson
Posts: 38441
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Real time clock reset issue

Fri Jun 05, 2015 2:53 pm

What kernel are you running? If it's 3.18.7 or later then you can do the RTC stuff easier with

Code: Select all

dtoverlay=i2c-rtc,ds1307
in /boot/config.txt, no modprobes, no funky echo commands needed.


hwclock has a few parameters

Code: Select all

[email protected] ~ # hwclock -?

Usage:
 hwclock [function] [option...]

Functions:
 -h, --help           show this help text and exit
 -r, --show           read hardware clock and print result
     --set            set the RTC to the time given with --date
 -s, --hctosys        set the system time from the hardware clock
 -w, --systohc        set the hardware clock from the current system time
     --systz          set the system time based on the current timezone
     --adjust         adjust the RTC to account for systematic drift since
                        the clock was last set or adjusted
     --getepoch       print out the kernel's hardware clock epoch value
     --setepoch       set the kernel's hardware clock epoch value to the
                        value given with --epoch
     --predict        predict RTC reading at time given with --date
 -V, --version        display version information and exit

Options:
 -u, --utc            the hardware clock is kept in UTC
     --localtime      the hardware clock is kept in local time
 -f, --rtc <file>     special /dev/... file to use instead of default
     --directisa      access the ISA bus directly instead of /dev/rtc
     --badyear        ignore RTC's year because the BIOS is broken
     --date <time>    specifies the time to which to set the hardware clock
     --epoch <year>   specifies the year which is the beginning of the
                        hardware clock's epoch value
     --noadjfile      do not access /etc/adjtime; this requires the use of
                        either --utc or --localtime
     --adjfile <file> specifies the path to the adjust file;
                        the default is /etc/adjtime
     --test           do not update anything, just show what would happen
 -D, --debug          debugging mode

[email protected] ~ #
When you first install the RTC you need to set the time/date on it.

sudo hwclock --systohc

To set the system clock from the RTC
sudo hwclock --hctosys

You should also ensure your system is running on the right time zone with
sudo dpkg-reconfigure tzdata

Note: after doing that you may need to reset your clock to your local time (or at least restart ntp)
sudo /etc/init.d/ntp stop
sudo date --set '2015-06-05 15:53:00'
sudo /etc/init.d/ntp start
sudo hwclock --systohc
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

I'll do your homework for you for a suitable fee.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

henryhanselscott
Posts: 107
Joined: Sun Jan 18, 2015 1:31 pm

Re: Real time clock reset issue

Fri Jun 05, 2015 3:17 pm

Thank you for the responses. I checked the battery, it measures 3.059 volts with a Fluke DVOM. I am running the latest raspbian image so I should be able to take advantage of "dtoverlay=i2c-rtc,ds1307"

One question yet, using "dtoverlay=i2c-rtc,ds1307", how would I go about setting the time on the RTC manually, as if I do not have a network connection. The reason that I ask is that the end user will have a touch screen interface built in tk. One of the screens is a setup screen where the system time and date is entered, I need to have Python3 write the system time to the RTC.

Thank you!

Henry

User avatar
DougieLawson
Posts: 38441
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Real time clock reset issue

Fri Jun 05, 2015 7:02 pm

To set it manually
sudo date --set '2015-06-05 20:02:35'
sudo hwclock --systohc
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

I'll do your homework for you for a suitable fee.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Return to “General discussion”