Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Installation of a H/W clock (a.k.a RTC) on the Raspberry Pi
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
christoph_peter_s
Tux's lil' helper
Tux's lil' helper


Joined: 30 Nov 2015
Posts: 115

PostPosted: Tue Jan 21, 2020 8:05 pm    Post subject: Installation of a H/W clock (a.k.a RTC) on the Raspberry Pi Reply with quote

There is lot of useful information on the Raspi out there. But when one decides to run Gentoo on it, then one is forced to collect information from different sources - and to combine material, which more offen than not is contradicting. So it took my quite a bit of time to get it up and running. Maybe these notes help others...

I describe the installation of a module based on the DS1307 chip. There are at least to more others (DS3231 and PCF8523, see https://learn.adafruit.com/adding-a-real-time-clock-to-raspberry-pi/set-rtc-time, which supposedly require a nearly identical installation procedure. In any case the first step, is to enable the i2c bus in config.txt file, which in allmost all cases should reside in /boot/config.txt. So uncomment or add these lines:
Code:
dtparam=i2c_arm=on
dtparam=i2c1=on

Next one should make sure, that the necessary kernel modules are loaded, e.g by editing /etc/modules-load.d/hwclock.conf (see: https://wiki.gentoo.org/wiki/Kernel_Modules)
Code:
i2c_bcm2835
i2c-bcm2708
i2c-dev
rtc-ds1307

Then one should ensure, that sys-apps/i2c-tools is installed, as it is necessary for the checking the connection to the module by i2cdetect -y 1
Code:
brutus ~ # i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

Note, that it will take a reboot to activate the config.txt, while the kernel modules might also be loaded temporarily by issuing a modprobe command.
In general things are not completely done by now (see: https://forums.gentoo.org/viewtopic-t-1092142-highlight-hwclock.html), so we might install a small service by editing /etc/init.d/init_ds1307
Code:
#!/sbin/openrc-run
# Copyright 2020 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

name="init_ds1307 daemon"
description="initialize i2c connection to ds1307 based RTC module"

start() {
   #
   # see: https://forums.gentoo.org/viewtopic-t-1092142-highlight-hwclock.html
   #
   echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
}

depend() {
   before hwclock
}

The command echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device will create the rtc device, which is necessary for the hwclock service to run properly. The command hwclock -r --verbose will show, whether the installation was successful. When first starting the RTC, it might show a date in 1970. It can be set by hwclock -a. The only thing, that remains, is to stop the swclock service and adjust the service startup by:
Code:
rc-update delete swclock boot
rc-update add hwclock boot
rc-service swclock stop
rc-service hwclock start
rc-update add init_ds1307 boot

When I was done with all that, I found out, that activating an overlay by adding
Code:
dtoverlay=i2c-rtc,ds1307
to /boot/config.txt will bring the kernel to generate the rtc device without issueing that echo-new-i2c-device command.

Happy Hacking!

NB: these RTC modules are pretty inexpensive. And they might save You quite a bit of grieve, if You operate Your raspi offline from time to time. Or they let You rest save, if You operate a Samba server on the raspi, as this could not tolerate a time mismatch.
Back to top
View user's profile Send private message
mike155
Advocate
Advocate


Joined: 17 Sep 2010
Posts: 4438
Location: Frankfurt, Germany

PostPosted: Wed Jan 22, 2020 2:42 am    Post subject: Reply with quote

Nice post! Thanks for sharing this!
Back to top
View user's profile Send private message
christoph_peter_s
Tux's lil' helper
Tux's lil' helper


Joined: 30 Nov 2015
Posts: 115

PostPosted: Thu Jan 23, 2020 7:38 pm    Post subject: Reply with quote

The information was too close... I recommend to read /boot/overlay/README (provided You mounted the firmware partition to /boot). There it states:
Code:
Overlays are loaded using the "dtoverlay" config.txt setting. As an example,
consider I2C Real Time Clock drivers. In the pre-DT world these would be loaded
by writing a magic string comprising a device identifier and an I2C address to
a special file in /sys/class/i2c-adapter, having first loaded the driver for
the I2C interface and the RTC device - something like this:

    modprobe i2c-bcm2835
    modprobe rtc-ds1307
    echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device

With DT enabled, this becomes a line in config.txt:

    dtoverlay=i2c-rtc,ds1307

This causes the file /boot/overlays/i2c-rtc.dtbo to be loaded and a "node"
describing the DS1307 I2C device to be added to the Device Tree for the Pi. By
default it usees address 0x68, but this can be modified with an additional DT
parameter:

    dtoverlay=i2c-rtc,ds1307,addr=0x68

Parameters usually have default values, although certain parameters are
mandatory. See the list of overlays below for a description of the parameters
and their defaults.


... and below ...
Code:
Name:   i2c-rtc
Info:   Adds support for a number of I2C Real Time Clock devices
Load:   dtoverlay=i2c-rtc,<param>=<val>
Params: abx80x                  Select one of the ABx80x family:
                                  AB0801, AB0803, AB0804, AB0805,
                                  AB1801, AB1803, AB1804, AB1805

        ds1307                  Select the DS1307 device

        ds1339                  Select the DS1339 device

        ds3231                  Select the DS3231 device
...


So I just added in /boot/config.txt:
Code:
dtoverlay=i2c-rtc,ds1307=1


And this did the trick. No need for an extra service. Much cleaner now. :-)

And, err, besides: the entries in /etc/modules-load.d/hwclock.conf are no longer necessary, too. I'd just recomend to keep the i2c-dev as it is necessary for the debugging.
Back to top
View user's profile Send private message
christoph_peter_s
Tux's lil' helper
Tux's lil' helper


Joined: 30 Nov 2015
Posts: 115

PostPosted: Tue Apr 15, 2025 8:19 am    Post subject: Reply with quote

A long time since the original post...
I had to upgrade a few of my raspis to aarch64. This is the time to fix things...

Tested on a Pi4 this is the required entry in /boot/config.txt for a DS3231 RTC:
Code:
dtparam=i2c_arm=on
dtoverlay=i2c-rtc,ds3231=1

The command i2cdetect -y 1 does not work, as the i2c1 device is not generated. But in /dev the device rtc0 is present. So it did the trick.
I just wanted to clarify the somewhat misleading info from 2020. And of course confirm, that the approach has not changed since.
I have checked it by pulling power and ethernet - and waiting a few minutes. I will try out the onboard RTC of the Pi5 as soon as my RTC-battery arrives (the rtc0 device is there, so I expect it to work - and it seems to confirm that the Pi5's RTC is indeed a DS3231).
Back to top
View user's profile Send private message
NeddySeagoon
Administrator
Administrator


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

PostPosted: Tue Apr 15, 2025 10:25 am    Post subject: Reply with quote

christoph_peter_s,

This would be good on the Wiki. These things tend to sink without trace on the forums.
It might fit as a sub page to https://wiki.gentoo.org/wiki/Raspberry_Pi_Install_Guide too.
_________________
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
christoph_peter_s
Tux's lil' helper
Tux's lil' helper


Joined: 30 Nov 2015
Posts: 115

PostPosted: Tue Apr 15, 2025 11:23 am    Post subject: Reply with quote

Hi Neddy,

I am ready to hack this into the wiki...
Is there a tutorial, which explains what to do - and how?

Best regards
Peter

PS: I got NVME-boot on the raspi up and running. This may also be worth to write down.
It was not really difficult - it is just, that nobody explained to me, how picky the Pi5 is w.r.t. working SSDs. From 3 sticks I got laying around, only one was working... Bad for me, that I first spend quite a few hours with a WD Green (that I thought was nice, because it consumes little power).


Last edited by christoph_peter_s on Tue Apr 15, 2025 11:36 am; edited 1 time in total
Back to top
View user's profile Send private message
NeddySeagoon
Administrator
Administrator


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

PostPosted: Tue Apr 15, 2025 11:30 am    Post subject: Reply with quote

christoph_peter_s,

There is a sandbox you can play in. Lots of help pages and you can look (carefully) at other user editable pages.
There is also #gentoo-wiki on Libera IRC.

Essentially, build and polish your page in your user space. That stops others from changing it. They can leave comments on the Discussion page.
When you are ready, move it to the main Wiki space.
I've just lean/practice in my user space.
_________________
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
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks 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