spartacus
Posts: 28
Joined: Tue Jan 01, 2013 5:06 am
Location: california

RTC Doesn't Work With Arch - PCF8523 Clock

Mon Apr 10, 2017 5:11 am

I purchased this RTC:

https://www.adafruit.com/products/3386

I can't get it working with Arch. I'm using ArchLinuxARM-2017.04-rpi-2-rootfs.tar.gz. I added this to /boot/config.txt:

Code: Select all

dtoverlay=i2c-rtc,pcf8523
Arch sets the RTC time. If I set the wrong time...

Code: Select all

hwclock --set --date='2015-03-14 12:45:05'
...and wait 10 minutes...

Code: Select all

hwclock --get
...will return the correct time.

But it doesn't use the time from the RTC when booting. I still get this message:

Code: Select all

Jan 31 17:10:49.080561 pi systemd-timesyncd[271]: System clock time unset or jumped backwards, restoring from recorded timestamp: Sun 2017-04-09 08:56:09 PDT
I downloaded Rasbian (2017-03-02-raspbian-jessie-lite.zip), and added the same line to config.txt. It works perfectly.

What am I missing?

mnemonic
Posts: 7
Joined: Tue Apr 18, 2017 5:17 pm

Re: RTC Doesn't Work With Arch - PCF8523 Clock

Tue Apr 18, 2017 5:21 pm

I have same problem with ds3231. I think it is a problem in Archlinux-ARM.

Please, tell me when you found a solution...

User avatar
DougieLawson
Posts: 31226
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: RTC Doesn't Work With Arch - PCF8523 Clock

Tue Apr 18, 2017 8:37 pm

What does lsmod show?

On Raspbian with a working pcf8523 there's a couple of kernel modules that are loaded.

Code: Select all

root@beaufort:~ # lsmod | grep rtc
rtc_pcf8523             3648  0
root@beaufort:~ # lsmod | grep i2c
i2c_bcm2835             7081  0
i2c_dev                 6913  0
root@beaufort:~ #
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

mnemonic
Posts: 7
Joined: Tue Apr 18, 2017 5:17 pm

Re: RTC Doesn't Work With Arch - PCF8523 Clock

Tue Apr 18, 2017 9:28 pm

I am almost sure it is a bug in archlinux-ARM, see:

Code: Select all

mnemonic@redbird[~]$ journalctl -b 0 | grep rtc
Jan 31 23:10:48 redbird kernel: rtc-ds1307 1-0068: rtc core: registered ds3231 as rtc0
systemd-timesyncd is executed after the ds3231 module is initialized:

Code: Select all

Jan 31 23:10:49 redbird systemd-timesyncd[280]: System clock time unset or jumped backwards, restoring from recorded timestamp: Tue 2017-04-18 01:44:32 -03
And, yes, modules are loaded ofc:

Code: Select all

mnemonic@redbird[~]$ lsmod | grep rtc
rtc_ds1307             12438  0
hwmon                   9525  1 rtc_ds1307
mnemonic@redbird[~]$ lsmod | grep i2c
i2c_bcm2835             5973  0
i2c_bcm2708             5510  0
i2c_dev                 6673  0
Maybe the reason for that is because the rtc_ds1307 module is compiled like an external module in archlinux-ARM, it should be builted-in kernel. I really don´t know... For sure is a bug or missing feature in the OS.

User avatar
DougieLawson
Posts: 31226
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: RTC Doesn't Work With Arch - PCF8523 Clock

Tue Apr 18, 2017 9:29 pm

That's the wrong module for a PCF8523 RTC.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

spartacus
Posts: 28
Joined: Tue Jan 01, 2013 5:06 am
Location: california

Re: RTC Doesn't Work With Arch - PCF8523 Clock

Wed Apr 19, 2017 12:32 am

thanks for looking at this.

lsmod before making any changes:

Code: Select all

Module                  Size  Used by
bcm2835_gpiomem         3515  0
bcm2835_rng             2679  0
rng_core                7657  2 bcm2835_rng
uio_pdrv_genirq         3456  0
fixed                   2921  0
uio                     9072  1 uio_pdrv_genirq
sch_fq_codel            9294  2
ip_tables              12363  0
x_tables               17452  1 ip_tables
ipv6                  384973  26
lsmod after editing config.txt and connecting RTC:

Code: Select all

Module                  Size  Used by
rtc_pcf8523             3765  0
i2c_bcm2835             5973  0
bcm2835_gpiomem         3515  0
bcm2835_rng             2679  0
rng_core                7657  2 bcm2835_rng
uio_pdrv_genirq         3456  0
uio                     9072  1 uio_pdrv_genirq
fixed                   2921  0
sch_fq_codel            9294  2
ip_tables              12363  0
x_tables               17452  1 ip_tables
ipv6                  384973  26
i don't see i2c_dev

User avatar
DougieLawson
Posts: 31226
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: RTC Doesn't Work With Arch - PCF8523 Clock

Thu Apr 20, 2017 5:49 pm

Don't worry about i2c-dev.

Do you get a /dev/rtc or /dev/rtc0 ?
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

spartacus
Posts: 28
Joined: Tue Jan 01, 2013 5:06 am
Location: california

Re: RTC Doesn't Work With Arch - PCF8523 Clock

Thu Apr 20, 2017 8:38 pm

DougieLawson wrote:Do you get a /dev/rtc or /dev/rtc0 ?

Code: Select all

lrwxrwxrwx 1 root root       4 Jan 31 17:10 rtc -> rtc0
crw------- 1 root root 253,  0 Jan 31 17:10 rtc0

User avatar
DougieLawson
Posts: 31226
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: RTC Doesn't Work With Arch - PCF8523 Clock

Fri Apr 21, 2017 8:47 am

So what happens when you run
sudo hwclock -r
sudo hwclock -w
sudo hwclock -r
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

spartacus
Posts: 28
Joined: Tue Jan 01, 2013 5:06 am
Location: california

Re: RTC Doesn't Work With Arch - PCF8523 Clock

Fri Apr 21, 2017 10:47 pm

Code: Select all

$ hwclock -r
2017-04-21 15:45:10.899044-0700
$ hwclock -w
$ hwclock -r
2017-04-21 15:45:20.745898-0700
$

Code: Select all

$ hwclock -r --debug
hwclock from util-linux 2.29.2
Using the /dev interface to the clock.
Last drift adjustment done at 1492814716 seconds after 1969
Last calibration done at 1492814716 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: 2017/04/21 22:46:33
Hw clock time : 2017/04/21 22:46:33 = 1492814793 seconds since 1969
Time since last adjustment is 77 seconds
Calculated Hardware Clock drift is 0.000000 seconds
2017-04-21 15:46:32.434720-0700
$

User avatar
DougieLawson
Posts: 31226
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: RTC Doesn't Work With Arch - PCF8523 Clock

Sat Apr 22, 2017 7:06 am

It's working, leave it alone now to chugg away happily.

You'll now want to look at running hwclock periodically (to keep the RTC set when the time is available from NTP) and using the RTC to set the system clock at boot time.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

spartacus
Posts: 28
Joined: Tue Jan 01, 2013 5:06 am
Location: california

Re: RTC Doesn't Work With Arch - PCF8523 Clock

Sat Apr 22, 2017 4:36 pm

During boot, the kernel doesn't read the time from the RTC. It first reads it from the backup file, then gets it from SNTP. Raspbian gets the time directly from the RTC.

Code: Select all

Jan 31 17:10:48.137358 rtctest systemd-timesyncd[267]: System clock time unset or jumped backwards, restoring from recorded timestamp: Fri 2017-04-21 15:39:03 PDT
Apr 21 15:39:03.992959 rtctest systemd[1]: Time has been changed

Apr 21 15:39:18.537606 rtctest systemd-networkd[275]: eth0: Configured
Apr 21 15:39:18.539811 rtctest systemd-timesyncd[267]: Network configuration changed, trying to establish connection.
Apr 21 15:39:18.551818 rtctest systemd-resolved[288]: Switching to DNS server 192.168.1.1 for interface eth0.
Apr 21 15:40:08.659052 rtctest systemd[1]: Time has been changed
Apr 21 15:40:08.659114 rtctest systemd-timesyncd[267]: Synchronized to time server 209.242.224.117:123 (1.us.pool.ntp.org).

User avatar
DougieLawson
Posts: 31226
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: RTC Doesn't Work With Arch - PCF8523 Clock

Sat Apr 22, 2017 5:04 pm

Then you need to look on the Arch Wiki/Arch Forum to find out how to configure hwclock to run on a Raspberry at boot time.

Raspbian does it with an /etc/init.d script.

Code: Select all

!/bin/sh
# hwclock.sh    Set and adjust the CMOS clock.
#
# Version:      @(#)hwclock.sh  2.00  14-Dec-1998  miquels@cistron.nl
#
# Patches:
#               2000-01-30 Henrique M. Holschuh <hmh@rcm.org.br>
#                - Minor cosmetic changes in an attempt to help new
#                  users notice something IS changing their clocks
#                  during startup/shutdown.
#                - Added comments to alert users of hwclock issues
#                  and discourage tampering without proper doc reading.
#               2012-02-16 Roger Leigh <rleigh@debian.org>
#                - Use the UTC/LOCAL setting in /etc/adjtime rather than
#                  the UTC setting in /etc/default/rcS.  Additionally
#                  source /etc/default/hwclock to permit configuration.

# WARNING:      Please read /usr/share/doc/util-linux/README.Debian.hwclock
#               before changing this file. You risk serious clock
#               misbehaviour otherwise.

### BEGIN INIT INFO
# Provides:          hwclock
# Required-Start:    mountdevsubfs
# Required-Stop:     mountdevsubfs
# Should-Stop:       umountfs
# Default-Start:     S
# X-Start-Before:    checkroot
# Default-Stop:      0 6
### END INIT INFO

# These defaults are user-overridable in /etc/default/hwclock
BADYEAR=no
HWCLOCKACCESS=yes
HWCLOCKPARS=
HCTOSYS_DEVICE=rtc0

# We only want to use the system timezone or else we'll get
# potential inconsistency at startup.
unset TZ

hwclocksh()
{
    [ ! -x /sbin/hwclock ] && return 0
    [ ! -r /etc/default/rcS ] || . /etc/default/rcS
    [ ! -r /etc/default/hwclock ] || . /etc/default/hwclock

    . /lib/lsb/init-functions
    verbose_log_action_msg() { [ "$VERBOSE" = no ] || log_action_msg "$@"; }

    case "$BADYEAR" in
       no|"")   BADYEAR="" ;;
       yes)     BADYEAR="--badyear" ;;
       *)       log_action_msg "unknown BADYEAR setting: \"$BADYEAR\""; return 1 ;;
    esac

    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\n" > /etc/adjtime
            fi

            if [ -d /run/udev ] || [ -d /dev/.udev ]; then
                return 0
            fi

            if [ "$HWCLOCKACCESS" != no ]; then
                log_action_msg "Setting the system clock"

                # Just for reporting.
                if head -n 3 /etc/adjtime | tail -n 1 | grep -q '^UTC$' ; then
                    UTC="--utc"
                else
                    UTC=
                fi
                # Copies Hardware Clock time to System Clock using the correct
                # timezone for hardware clocks in local time, and sets kernel
                # timezone. DO NOT REMOVE.
                if /sbin/hwclock --rtc=/dev/$HCTOSYS_DEVICE --hctosys $HWCLOCKPARS $BADYEAR; then
                    #   Announce the local time.
                    verbose_log_action_msg "System Clock set to: `date $UTC`"
                else
                    log_warning_msg "Unable to set System Clock to: `date $UTC`"
                fi
            else
                verbose_log_action_msg "Not setting System Clock"
            fi
            ;;
        stop|restart|reload|force-reload)
            #
            # Updates the Hardware Clock with the System Clock time.
            # This will *override* any changes made to the Hardware Clock.
            #
            # WARNING: If you disable this, any changes to the system
            #          clock will not be carried across reboots.
            #

            if [ "$HWCLOCKACCESS" != no ]; then
                log_action_msg "Saving the system clock"
                if /sbin/hwclock --rtc=/dev/$HCTOSYS_DEVICE --systohc $HWCLOCKPARS $BADYEAR; then
                    verbose_log_action_msg "Hardware Clock updated to `date`"
                fi
            else
                verbose_log_action_msg "Not saving System Clock"
            fi
            ;;
        show)
            if [ "$HWCLOCKACCESS" != no ]; then
                /sbin/hwclock --rtc=/dev/$HCTOSYS_DEVICE --show $HWCLOCKPARS $BADYEAR
            fi
            ;;
        *)
            log_success_msg "Usage: hwclock.sh {start|stop|reload|force-reload|show}"
            log_success_msg "       start sets kernel (system) clock from hardware (RTC) clock"
            log_success_msg "       stop and reload set hardware (RTC) clock from kernel (system) clock"
            return 1
            ;;
    esac
}

hwclocksh "$@"
and there's a script in /lib/udev

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 /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
#    /sbin/hwclock --rtc=$dev --systz --badyear
    /sbin/hwclock --rtc=$dev --hctosys --badyear
else
#    /sbin/hwclock --rtc=$dev --systz
    /sbin/hwclock --rtc=$dev --hctosys
fi

# Note 'touch' may not be available in initramfs
> /run/udev/hwclock-set
and a matching rule /lib/udev/rules.d/85-hwclock.rules to drive that

Code: Select all

# Set the System Time from the Hardware Clock and set the kernel's timezone
# value to the local timezone when the kernel clock module is loaded.

KERNEL=="rtc0", RUN+="/lib/udev/hwclock-set $root/$name"
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

spartacus
Posts: 28
Joined: Tue Jan 01, 2013 5:06 am
Location: california

Re: RTC Doesn't Work With Arch - PCF8523 Clock

Sat Apr 22, 2017 10:55 pm

Thanks for your help. I'll take a look at those files.


User avatar
DougieLawson
Posts: 31226
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: RTC Doesn't Work With Arch - PCF8523 Clock

Sun Apr 23, 2017 4:42 pm

pepedog wrote:Hmm, timedatectl
https://wiki.archlinux.org/index.php/Time
Hmmm, WiKi page not written with the reader in mind. That one is about as clear as mud since it doesn't explain how timedatectl works with an RTC device.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

pepedog
Posts: 1044
Joined: Fri Oct 07, 2011 9:55 am

Re: RTC Doesn't Work With Arch - PCF8523 Clock

Sun Apr 23, 2017 6:16 pm

It says here https://wiki.archlinux.org/index.php/Time#Time_standard
Later, the system clock is set again from the hardware clock by systemd, dependent on values in /etc/adjtime
So timedatectl set-local-rtc 0 creates that file (don't have rtc to test)

spartacus
Posts: 28
Joined: Tue Jan 01, 2013 5:06 am
Location: california

Re: RTC Doesn't Work With Arch - PCF8523 Clock

Sun Apr 23, 2017 6:20 pm

DougieLawson wrote:Raspbian does it with an /etc/init.d script.
it doesn't look like systemd uses that file. i deleted it and the RTC still worked.
https://groups.google.com/forum/#!topic ... Rd31JYP0Ss
DougieLawson wrote:/lib/udev/hwclock-set
/lib/udev/rules.d85-hwclock.rules
these two look important and don't exist in arch. i copied them to arch but it didn't help.

User avatar
DougieLawson
Posts: 31226
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: RTC Doesn't Work With Arch - PCF8523 Clock

Sun Apr 23, 2017 6:24 pm

You're going to have to hit the Arch forum as it looks like they've forgotten to do the pieces needed for Raspberries which have an add-on RTC.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

spartacus
Posts: 28
Joined: Tue Jan 01, 2013 5:06 am
Location: california

Re: RTC Doesn't Work With Arch - PCF8523 Clock

Sun Apr 23, 2017 6:29 pm

pepedog wrote:So timedatectl set-local-rtc 0 creates that file
it didn't create that file. it looks like the RTC was already using UTC before i ran the command.

Code: Select all

$ timedatectl
      Local time: Sun 2017-04-23 11:22:29 PDT
  Universal time: Sun 2017-04-23 18:22:29 UTC
        RTC time: Sun 2017-04-23 18:22:29
       Time zone: America/Los_Angeles (PDT, -0700)
 Network time on: yes
NTP synchronized: yes
 RTC in local TZ: no
$ timedatectl set-local-rtc 0
$ timedatectl
      Local time: Sun 2017-04-23 11:22:39 PDT
  Universal time: Sun 2017-04-23 18:22:39 UTC
        RTC time: Sun 2017-04-23 18:22:39
       Time zone: America/Los_Angeles (PDT, -0700)
 Network time on: yes
NTP synchronized: yes
 RTC in local TZ: no
$

User avatar
DougieLawson
Posts: 31226
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: RTC Doesn't Work With Arch - PCF8523 Clock

Sun Apr 23, 2017 6:40 pm

Ah, I think I understand how this things works now. ('Tis the same in Raspbian).

You can tell systemd / timedatectl that you have an RTC, your RTC is set to local or UTC, you have NTP sync and it then does some magic when it finds that /dev/rtc0 (or /dev/rtc) exists in the /dev/ virtual filesystem folder.

sudo timedatectl set-ntp true
sudo timedatectl set-local-rtc 0
# or timedatectl set-local-rtc 1 (if you run with the RTC set to local time).
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

spartacus
Posts: 28
Joined: Tue Jan 01, 2013 5:06 am
Location: california

Re: RTC Doesn't Work With Arch - PCF8523 Clock

Wed May 10, 2017 5:19 pm

pepedog wrote:(don't have rtc to test)
want me to have adafruit send you one?

pepedog
Posts: 1044
Joined: Fri Oct 07, 2011 9:55 am

Re: RTC Doesn't Work With Arch - PCF8523 Clock

Fri May 12, 2017 8:42 am

Kind of you to think of that, but I am inundated with free arm devices.
Thank you

mnemonic
Posts: 7
Joined: Tue Apr 18, 2017 5:17 pm

Re: RTC Doesn't Work With Arch - PCF8523 Clock

Wed May 24, 2017 1:42 pm

DougieLawson wrote:That's the wrong module for a PCF8523 RTC.
Exactly, i am not using PCF8523 RTC.

mnemonic
Posts: 7
Joined: Tue Apr 18, 2017 5:17 pm

Re: RTC Doesn't Work With Arch - PCF8523 Clock

Wed May 24, 2017 3:13 pm

I found a confirmation of the problem in: https://wiki.archlinux.org/index.php/Time#Time_standard

"During kernel startup, at the point when the RTC driver is loaded, the system clock may be set from the hardware clock. Whether this occurs depends on the hardware platform, the version of the kernel and kernel build options. If this does occur, at this point in the boot sequence, the hardware clock time is assumed to be UTC and the value of /sys/class/rtc/rtcN/hctosys (N=0,1,2,..) will be set to 1."

So, i am sure my problem could be easly solved if alarm kernel had the modules builted-in.

Anyways, thank you for point em in correct way.

Return to “Arch”

Who is online

Users browsing this forum: No registered users and 3 guests