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: 106

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: 106

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