Help with I2C RTC on RPi4

Hello ipfire-Community!
I’m MegaBernd and i am a new member here, I’m running ipfire on an Raspberry Pi 4 for about a year and would describe my technical knowledge as advanced, defintely not pro!

My problem:
I would like to add an RTC to my RPi, using an I2C-RTC module. I am not very familiar with coding but i’m able to build ipfire from source and perform little modifications. Could someone please explain a way to get this working e.g. using scripts or editing source? I don’t want anyone to post a solution to use out of the box, but if someone can roughly explain the steps to do it would be very helpful!

I know questions like this can be annoying, please be patient, i just want to learn :wink:

Greetings!
MegaBernd

This is something you should address in a PI forum. But just like any other IC2 RTC hat, you enable THE i2C bus in raspi-config then edit the startup file, and remove the fake HW clock application and comment out the configuration.

you first start be updating your packages:

sudo apt update
sudo apt upgrade

then run raspi config to tuen on I2C


sudo raspi-config

then you reboot:


sudo reboot

then you install the I2C utils:


sudo apt install python3-smbus i2c-tools

Then run this to check if your I2C is wired correctly and working, ID#68 should be the response for RTC chips:


sudo i2cdetect -y 1

This will Display ID#68. When the driver is working and RTC is in use, the above command will return UU instead.

Now you need to know what RTC chip you are using ( DS1307, DS3231, or the PCF85231)

then you have to edit the startup file:


sudo nano /boot/config.txt

and at the bottom of the file, you need to add the enable function and the chip used (DS3231 for example ):


dtoverlay=i2c-rtc,ds3231

Save and reboot


sudo reboot

Then test it:


sudo i2cdetect -y 1

it should output a wall of text, if “UU” appears instead of “68” then we have successfully loaded in the Kernel driver for the RTC.

then we clean the environment and remove fake-hwclock once the RTC is verified working:


sudo apt -y remove fake-hwclock
sudo update-rc.d -f fake-hwclock remove

now we clean up systemd udev startup script:


sudo nano /lib/udev/hwclock-set

inside this file, find the fallowing code:

if [ -e /run/systemd/system ] ; then
    exit 0
fi

and comment it out like this:

#if [ -e /run/systemd/system ] ; then
#   exit 0
#fi

now, check your current time and date on the Pi4 with the date command:


date

now if the time and date are correct, then write the correct time and date to the RTC chip:


sudo hwclock -w

You will only need to run the above command if you ever change the battery.

To read the current time from the RTC directly, use this command:

sudo hwclock -r

I know there is not many sources of documentation out there, but this will work for you.

Thanks for this very quick answer but this isn’t was i am looking for, the Raspberry Pi is my ipfire appliance, i want to use the RTC with ipfire not Raspberry Pi OS so i don’t have apt or the raspi-config option and i2c-tools are not available via pakfire, that’s why i didn’t ask in a Pi forum, sorry if my question was unclear.

if you cant configure the hardware, most likely its not going to have a file with a device tree entry.

Looking at it responses about 32bit arm, they are not supporting it anymore.

But something like the raspberry pi, you would have to build from raspbain OS instead of a general linux kernel (which ipfire looks like it was built from). Even their new one, I would do this instead of a generic aarch64 kernel.

The RPi 4 is aarch64 and building ipfire on ipfire is possible, so i can use the Pi as “build-machine” for itself, i will check the devicetree and try :wink:

one way you could try is to boot rasbain on a usb and turn on the hardware, but just like all ports, they switch dynamically., other than taking the raspi-conf and subsystem, and splicing it as a fork, just have to get the 64 bit Rasbain source files an splice it into this OS.

Hallo @megabernd

Welcome to the IPFire community.

Maybe this thread might help.

https://community.ipfire.org/t/device-tree-compiler-dtc-is-missing-in-arm-version-of-ipfire/11365

1 Like

That is interesting. I wonder how and when config.txt is used since I see it in the ipfire 2.x source at github:

if its just used at installation, maybe prompts could be used to modify config.txt at install. But I found it is strange to not support hardware fully even though to me I would think ipfire would have created a fork with a rasbain core. But maybe a contributor will come around and make a ipfire fork that will support the raspberry pi better.

It is copied from the repo directory to the /boot directory in the build of the rpi-firmware LFS file

https://git.ipfire.org/?p=ipfire-2.x.git;a=blob;f=lfs/rpi-firmware;h=51af49b075ef309191f740492d389912141468a7;hb=refs/heads/next

Looking at it further, I don’t see the files that would be needed to switch the i/o modes, but to be fair I don’t have it installed on a RPI 4. I do have one that I can jig up and see further, but it looks like this one would have to have a raspi-config rebuild like what was done on Fedora. Adding the text manually should work if the driver binary is installed since RTCs can be turned on and off at the command line…

But since this one is focused around a web GUI, I would make more sense to write a raspi-config CGI page and package the raspi hardware binaries and tools, but I don’t know CGI web. So it would take me some time to write that since everyone has moved on from it and only mess with CGI with python. But everyone has their favourite programming languages. If I was to write a GUI like this, I would have used php/perl.