Page 1 of 1
RTC Doesn't Work With Arch - PCF8523 Clock
Posted: Mon Apr 10, 2017 5:11 am
by spartacus
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:
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...
...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?
Re: RTC Doesn't Work With Arch - PCF8523 Clock
Posted: Tue Apr 18, 2017 5:21 pm
by mnemonic
I have same problem with ds3231. I think it is a problem in Archlinux-ARM.
Please, tell me when you found a solution...
Re: RTC Doesn't Work With Arch - PCF8523 Clock
Posted: Tue Apr 18, 2017 8:37 pm
by DougieLawson
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:~ #
Re: RTC Doesn't Work With Arch - PCF8523 Clock
Posted: Tue Apr 18, 2017 9:28 pm
by mnemonic
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.
Re: RTC Doesn't Work With Arch - PCF8523 Clock
Posted: Tue Apr 18, 2017 9:29 pm
by DougieLawson
That's the wrong module for a PCF8523 RTC.
Re: RTC Doesn't Work With Arch - PCF8523 Clock
Posted: Wed Apr 19, 2017 12:32 am
by spartacus
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
Re: RTC Doesn't Work With Arch - PCF8523 Clock
Posted: Thu Apr 20, 2017 5:49 pm
by DougieLawson
Don't worry about i2c-dev.
Do you get a /dev/rtc or /dev/rtc0 ?
Re: RTC Doesn't Work With Arch - PCF8523 Clock
Posted: Thu Apr 20, 2017 8:38 pm
by spartacus
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
Re: RTC Doesn't Work With Arch - PCF8523 Clock
Posted: Fri Apr 21, 2017 8:47 am
by DougieLawson
So what happens when you run
sudo hwclock -r
sudo hwclock -w
sudo hwclock -r
Re: RTC Doesn't Work With Arch - PCF8523 Clock
Posted: Fri Apr 21, 2017 10:47 pm
by spartacus
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
$
Re: RTC Doesn't Work With Arch - PCF8523 Clock
Posted: Sat Apr 22, 2017 7:06 am
by DougieLawson
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.
Re: RTC Doesn't Work With Arch - PCF8523 Clock
Posted: Sat Apr 22, 2017 4:36 pm
by spartacus
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).
Re: RTC Doesn't Work With Arch - PCF8523 Clock
Posted: Sat Apr 22, 2017 5:04 pm
by DougieLawson
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"
Re: RTC Doesn't Work With Arch - PCF8523 Clock
Posted: Sat Apr 22, 2017 10:55 pm
by spartacus
Thanks for your help. I'll take a look at those files.
Re: RTC Doesn't Work With Arch - PCF8523 Clock
Posted: Sun Apr 23, 2017 4:07 pm
by pepedog
Re: RTC Doesn't Work With Arch - PCF8523 Clock
Posted: Sun Apr 23, 2017 4:42 pm
by DougieLawson
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.
Re: RTC Doesn't Work With Arch - PCF8523 Clock
Posted: Sun Apr 23, 2017 6:16 pm
by pepedog
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)
Re: RTC Doesn't Work With Arch - PCF8523 Clock
Posted: Sun Apr 23, 2017 6:20 pm
by spartacus
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.
Re: RTC Doesn't Work With Arch - PCF8523 Clock
Posted: Sun Apr 23, 2017 6:24 pm
by DougieLawson
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.
Re: RTC Doesn't Work With Arch - PCF8523 Clock
Posted: Sun Apr 23, 2017 6:29 pm
by spartacus
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
$
Re: RTC Doesn't Work With Arch - PCF8523 Clock
Posted: Sun Apr 23, 2017 6:40 pm
by DougieLawson
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).
Re: RTC Doesn't Work With Arch - PCF8523 Clock
Posted: Wed May 10, 2017 5:19 pm
by spartacus
pepedog wrote:(don't have rtc to test)
want me to have adafruit send you one?
Re: RTC Doesn't Work With Arch - PCF8523 Clock
Posted: Fri May 12, 2017 8:42 am
by pepedog
Kind of you to think of that, but I am inundated with free arm devices.
Thank you
Re: RTC Doesn't Work With Arch - PCF8523 Clock
Posted: Wed May 24, 2017 1:42 pm
by mnemonic
DougieLawson wrote:That's the wrong module for a PCF8523 RTC.
Exactly, i am not using PCF8523 RTC.
Re: RTC Doesn't Work With Arch - PCF8523 Clock
Posted: Wed May 24, 2017 3:13 pm
by mnemonic
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.