Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[SOLVED] How to make my RTC available on boot on an RPi 3?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Gentoo on ARM
View previous topic :: View next topic  
Author Message
Jt0
n00b
n00b


Joined: 28 Oct 2018
Posts: 33

PostPosted: Mon Jan 21, 2019 6:12 pm    Post subject: [SOLVED] How to make my RTC available on boot on an RPi 3? Reply with quote

I've just installed a DS3231M RTC on my Pi 3B+. I'm able to get it to work if I execute "echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device" but when I reboot the RTC isn't recognized again.

I've got the DS1307 module built into the kernel and in my config.txt I've enabled "dtparam=i2c_arm=on" and "dtoverlay=i2c-rtc,ds3231". The device tree overlay doesn't seem to do anything (maybe it's supposed to load modules on boot?), nor does the udev rule I added from a post on the Raspberry Pi forums.

My only other thought would be to create an init script to add the RTC with the above "echo" command and ensure it runs before the system clock is set from the RTC. Is there a better way to accomplish adding the RTC on boot, or is the init script the way to go?


Last edited by Jt0 on Tue Jan 22, 2019 6:43 am; edited 1 time in total
Back to top
View user's profile Send private message
Logicien
Veteran
Veteran


Joined: 16 Sep 2005
Posts: 1405
Location: Montréal

PostPosted: Mon Jan 21, 2019 9:15 pm    Post subject: Reply with quote

Have you check if you have any /dev/rtc node in the /dev and /dev/misc directories after you boot? What does give the command
Code:
hwclock --show --verbose

The question is to know if the hardware clock keep it's time after you have write it from the system clock who itself have been set from an Internet time server. It's specially true when the Pi is poweroff and you boot your Pi with the mini Usb cable that you have to disconnect and reconnect everytime you boot the Pi. After you echo the command who make the rtc work is the hardware clock time correct using the above command?

Maybe you need to pass a kernel parameter in /boot/cmdline.txt to make the kernel set the i2c device. I think passing your echo command in /etc/rc.local must be enough if it's the only way. Before using the hardware clock to set the system time at boot be sure that it is correct. Anyway you have the Systemd timedatectl command who allow you to set your system time from Internet. I set all configuration options of timedatectl to get everything right.

I have the Raspberry 3 B+ with no hardware clock added. My system time is always good Systemd set it from Internet. The next command give good time information too.
Code:
timedatectl status


With all computers who have an hardware clock I set the hardware clock in UTC, the standard.

Here is a french web page who talk about adding your RTC hardware to the Pi.

DS3231 Raspberry pi RTC : ajouter une horloge temps réel I2C – Alitest
_________________
Paul
Back to top
View user's profile Send private message
NeddySeagoon
Administrator
Administrator


Joined: 05 Jul 2003
Posts: 46743
Location: 56N 3W

PostPosted: Mon Jan 21, 2019 9:50 pm    Post subject: Reply with quote

Jt0,

From the i2c-rtc-overlay.dts ...
Code:
        fragment@3 {
                target = <&i2c_arm>;
                __dormant__ {
                        #address-cells = <1>;
                        #size-cells = <0>;
                        status = "okay";

                        ds3231: ds3231@68 {
                                compatible = "maxim,ds3231";
                                reg = <0x68>;
                                status = "okay";
                        };
                };
        };
It describes the ds3231 to the kernel and loads modules if they are required.

When do you try to set the system clock from the RTC?
If you let the kernel do it, it happens very early, before root is mounted and before module loading is possible.
If you use the hwclock init script, its in the boot runlevel. Both work before the network is started. That may be important, if your time is a long way out you will reject ssl certificates for not being valid.

Silly question time. Did you fit a battery to your RTC?
I have a Raspberry Pi GPS ntp server. It contains a RTC too but it was supplied without a battery.
_________________
Regards,

NeddySeagoon

Computer users fall into two groups:-
those that do backups
those that have never had a hard drive fail.
Back to top
View user's profile Send private message
Jt0
n00b
n00b


Joined: 28 Oct 2018
Posts: 33

PostPosted: Tue Jan 22, 2019 1:25 am    Post subject: Reply with quote

Logicien,
I don't have /dev/rtc or /dev/misc on boot. The output of that command is:
Code:
# hwclock --show --verbose
hwclock from util-linux 2.33.1
System Time: 1548119825.979465
Trying to open: /dev/rtc0
Trying to open: /dev/rtc
Trying to open: /dev/misc/rtc
No usable clock interface found.
hwclock: Cannot access the Hardware Clock via any known method.


timedatectl status isn't found. Using emerge --search timedatectl I found dev-perl/TimeDate. Is this an important package to have?
Edit: looks like this is part of systemd, which I don't have installed.
Code:
ntpctl -s all
reports that the system clock is currently synced with the default 20 NTP servers.

NeddySeagoon,
I'd like to set the system clock from the RTC on boot, and that seems to be what it currently tries to do (this is the default behavior of hwclock, isn't it?). I don't have the option set in the kernel config to set the system clock from the RTC on boot, but it seems to try when the hwclock service is started.
And yes, I put a battery in. :lol: When I manually re-add the RTC, it's got the correct time, even if I unplug the Pi between boots.
Back to top
View user's profile Send private message
Jt0
n00b
n00b


Joined: 28 Oct 2018
Posts: 33

PostPosted: Tue Jan 22, 2019 6:42 am    Post subject: Reply with quote

Alright, I think I've got this solved!

The closest I could find to a definitive answer was this StackExchange answer, which advised putting the echo command into /etc/rc.local. However, this has the drawback of being run after other init scripts, which is a problem if it runs after hwclock.

To ensure the RTC is available when hwclock tries to set the system clock from it, I put the echo command into /etc/init.d/hwclock:
Code:
start()
{
        local retval=0 errstr="" modname
        setupopts

        # === Added to add hardware clock ===
        echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
        sleep 0.5
        # === End edited section ===

        if [ -z "$utc_cmd" ]; then
                ewarn "Not setting clock for $utc system"
                return 0
        fi

        ebegin "Setting system clock using the hardware clock [$utc]"
.
.
.


After rebooting, the system clock is set successfully, and /dev/rtc0 is available. hwclock commands are able to read and write to the RTC successfully. This has the side effect of /dev/i2c-1 not showing up (when added manually after boot the device normally appears along with /dev/rtc0), so i2cdetect won't find anything, but the RTC is functional.

I don't know if this will be overwritten by an update in the future, but if it is, it's a simple two lines to add back in.

Also, in case it's helpful to anyone reading this later, here's the output of hwclock --show --verbose after a reboot with this change in place:
Code:
# hwclock --show --verbose
hwclock from util-linux 2.33.1
System Time: 1548139585.017021
Trying to open: /dev/rtc0
Using the rtc interface to the clock.
Last drift adjustment done at 1548088369 seconds after 1969
Last calibration done at 1548088369 seconds after 1969
Hardware clock is on UTC time
Assuming hardware clock is kept in UTC time.
Waiting for clock tick...
ioctl(3, RTC_UIE_ON, 0): Invalid argument
Waiting in loop for time from /dev/rtc0 to change
...got clock tick
Time read from Hardware Clock: 2019/01/22 06:46:26
Hw clock time : 2019/01/22 06:46:26 = 1548139586 seconds since 1969
Time since last adjustment is 51217 seconds
Calculated Hardware Clock drift is 0.000000 seconds
2019-01-22 01:46:25.016873-05:00
Back to top
View user's profile Send private message
NeddySeagoon
Administrator
Administrator


Joined: 05 Jul 2003
Posts: 46743
Location: 56N 3W

PostPosted: Sat Jan 26, 2019 7:06 pm    Post subject: Reply with quote

Jt0,

Its in danger of being overwritten in the future.

It would be cleaner to make a separate init script, say ds1307 that is in the boot runlevel with hwclock with a
Code:
before hwclock
constraint.
Then your new script is self contained.
_________________
Regards,

NeddySeagoon

Computer users fall into two groups:-
those that do backups
those that have never had a hard drive fail.
Back to top
View user's profile Send private message
Jt0
n00b
n00b


Joined: 28 Oct 2018
Posts: 33

PostPosted: Fri Feb 08, 2019 4:26 am    Post subject: Reply with quote

Noted, I've moved the relevant portion to its own init script and added it to the boot runlevel. Seems like everything is still working, now in a future-proof way. Thanks for your help.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Gentoo on ARM All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum