thof
Posts: 18
Joined: Mon Mar 04, 2013 2:08 pm

[SOLVED] PCF8523 loses time on reboot

Wed Jan 06, 2016 6:01 pm

Hi all,

First of all I'm not sure if this is the right subforum, so admins feel free to move it...

I have a PCF8523 connected to a Raspberry Pi 2 B. I load the drivers through dtoverlay=i2c-rtc,pcf8523
The drivers are loaded and communication works with hwclock --debug for example, but after boot hwclock -r gives:

Code: Select all

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).
I can correct it with hwclock -w and after this it read correct. But when I reboot it is reset again...
Digging into the datasheet of the PCF8523 it reads that it's default switching state is disabled, meaning it will only work with VDD power.
See: http://www.nxp.com/documents/data_sheet/PCF8523.pdf page 15

Is this something that is wrong in the driver? Can I do something the set the registers differently to make it switch on power loss?

Thanks!

Thanks!
Last edited by thof on Thu Jan 07, 2016 12:22 pm, edited 1 time in total.

User avatar
mikronauts
Posts: 2783
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: PCF8523 loses time on reboot

Wed Jan 06, 2016 6:26 pm

Did you add a battery for the RTC to run from when main power is off?
thof wrote:Hi all,

First of all I'm not sure if this is the right subforum, so admins feel free to move it...

I have a PCF8523 connected to a Raspberry Pi 2 B. I load the drivers through dtoverlay=i2c-rtc,pcf8523
The drivers are loaded and communication works with hwclock --debug for example, but after boot hwclock -r gives:

Code: Select all

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).
I can correct it with hwclock -w and after this it read correct. But when I reboot it is reset again...
Digging into the datasheet of the PCF8523 it reads that it's default switching state is disabled, meaning it will only work with VDD power.
See: http://www.nxp.com/documents/data_sheet/PCF8523.pdf page 15

Is this something that is wrong in the driver? Can I do something the set the registers differently to make it switch on power loss?

Thanks!

Thanks!
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

thof
Posts: 18
Joined: Mon Mar 04, 2013 2:08 pm

Re: PCF8523 loses time on reboot

Thu Jan 07, 2016 8:29 am

Yes I did, I checked all connections and while digging through the datasheet it looks like this is the default behaviour. So I could try to interface with it and change the power switching mode, but I'm not sure it is stored or if it should be changed in a linux driver?

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

Re: PCF8523 loses time on reboot

Thu Jan 07, 2016 10:34 am

Have you tried writing the current time to the RTC? sudo hwclock --systohc. Shut the system down, pull power, reboot and read the clock with sudo hwclock.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

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

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2944
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: PCF8523 loses time on reboot

Thu Jan 07, 2016 10:37 am

1) Remove fake-hwclock

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

2) Re-enable the pre-systemd hwclock-set script

pi@raspi ~ $ sudo nano /lib/udev/hwclock-set
# comment out these three lines
#if [ -e /run/systemd/system ] ; then
# exit 0
#fi

3) Add dtoverlay=i2c-rtc,whatever to config.txt

4) Reboot with network

5) Wait for the time to settle

6) sudo hwclock -w

7) Unplug network and reboot

8) See that the time is correct.

thof
Posts: 18
Joined: Mon Mar 04, 2013 2:08 pm

Re: PCF8523 loses time on reboot

Thu Jan 07, 2016 12:21 pm

Excellent! I already removed fake-hwclock, but commenting out those lines seems to have done the trick!

Thanks!

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

Re: PCF8523 loses time on reboot

Thu Jan 07, 2016 12:54 pm

thof wrote:Excellent! I already removed fake-hwclock, but commenting out those lines seems to have done the trick!

Thanks!
Shouldn't that stuff be done with a simple switch in /etc/defaults/hwclock to make it easier to find and amend. Editing bash scripts to hack three lines of code isn't the stuff that new/novice users should be doing.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

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

thof
Posts: 18
Joined: Mon Mar 04, 2013 2:08 pm

Re: [SOLVED] PCF8523 loses time on reboot

Thu Jan 07, 2016 12:59 pm

It would be interesting to see in what situations this modification would be necessary, for me it's no problem doing this, but I would not have come up with this myself...

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

Re: [SOLVED] PCF8523 loses time on reboot

Thu Jan 07, 2016 1:32 pm

thof wrote:It would be interesting to see in what situations this modification would be necessary, for me it's no problem doing this, but I would not have come up with this myself...
It looks like everyone running Raspbian Jessie with a RTC chip connected needs to do that stuff. It should be a

Code: Select all

# Do you have a hardware RTC
HWRTCINSTALLED=yes
in /etc/defaults/hwclock and the program Phil got you to edit would just do the right thing.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

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

jnavratil
Posts: 4
Joined: Fri Sep 26, 2014 2:44 pm

Re: [SOLVED] PCF8523 loses time on reboot

Mon Jan 25, 2016 11:47 pm

Thanks PhilE!

I do agree with 'DougieLawson' that this behavior should be more directly configurable. I'm not a sysadmin by any stretch and like 'thof' would have had a devil of a time coming up with a solution. What I'm more concerned about is ongoing maintenance. I can imagine a fix unfixing my fix. Should I be worried?

thof
Posts: 18
Joined: Mon Mar 04, 2013 2:08 pm

Re: [SOLVED] PCF8523 loses time on reboot

Thu Jan 28, 2016 1:34 pm

Hmm, while the solution works for Jessie, under Wheezy this does not work...
Removed fake-hwclock, edited /lib/udev/hwclock-set (although this shouldn't matter because there is no systemd).

Now after a reboot I'm getting my original error again:

Code: Select all

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).

thof
Posts: 18
Joined: Mon Mar 04, 2013 2:08 pm

Re: [SOLVED] PCF8523 loses time on reboot

Thu Jan 28, 2016 1:47 pm

Oke, solved it again...

in the file /lib/udev/hwclock-set you have to change two occurences of --systz to --hctosys:

Code: Select all

if [ yes = "$BADYEAR" ] ; then
    /sbin/hwclock --rtc=$dev --hctosys --badyear
else
    /sbin/hwclock --rtc=$dev --hctosys
fi

stg45
Posts: 2
Joined: Sun May 01, 2016 11:48 am

Re: [SOLVED] PCF8523 loses time on reboot

Wed Oct 11, 2017 8:05 pm

Hi,

I have two Adafruit PCF8523 RTC which installed on my Raspberry Pi 2B and 3B. In both cases the PCF8523 have a coin battery correctly installed to ensure successful operation in the event of a power failure or reboot.
I have followed all the instructions, however when I reboot the system time is not being set correctly from the PCF8523, no errors are being reported in syslog. After I do login the date has revert back to 2011 and so has the hwclock.
I am currently using Ubuntu Mate on both.

I am currently using the pair for NTP (client and server), DNS Server (with DNSSEC enabled) and DHCP (with DHCP Failover configured).
As DNSSEC requires an accurate time, I need to be able to get DNS working with a fairly accurate clock and then allow more precise time once NTP has started.

Any advice would be gratefully received

stg45
Posts: 2
Joined: Sun May 01, 2016 11:48 am

Re: [SOLVED] PCF8523 loses time on reboot

Sun Oct 15, 2017 11:19 am

Hi,

I found some instructions on the Ubuntu form which I had to adapt to get to work on my Raspberry PI 2 B and 3 B servers.

If you have followed the previous instructions then you need verify that the hwclock works correctly.

In order to get the PI to read from the RTC you need to

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.europe.pool.ntp.org 1.europe.pool.ntp.org 2.europe.pool.ntp.org 3.europe.pool.ntp.org

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

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
systemctl disable fake-hwclock
apt-get -y remove fake-hwclock

Step 5: To finally enable the automatic start-up of the RTC synchonrisation during bootup 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

Now verify correct operation by using the timedatectl command to obtain results similar to below

Local time: Sun 2017-10-15 12:13:55 BST
Universal time: Sun 2017-10-15 11:13:55 UTC
RTC time: Sun 2017-10-15 11:13:55
Time zone: Europe/London (BST, +0100)
Network time on: yes
NTP synchronized: yes
RTC in local TZ: no

If you get the report that NTP synchronised : no after 5 minutes, then

Step 6 Install ntp

apt-get install ntp
edit /etc/ntp.conf and add

server 0.europe.pool.ntp.org
server 1.europe.pool.ntp.org
server 2.europe.pool.ntp.org
server 3.europe.pool.ntp.org

run the following commands to activate ntp
$ sudo systemctl enable ntp
$ sudo systemctl start ntp

After five minutes, run ntpq and use the command peers to show the clocks ntp is synchronised with

ntpq> peers

You can then run timedatectl and NTP should show synchronised

Return to “Interfacing (DSI, CSI, I2C, etc.)”