Implementing RTC on Rock Pi 4 C Plus

I am using Debian a Rock Pi 4 C Plus. I would like to implement my real time clock the DS1307. If someone can help point me in the right direction, that would be great.

I notice there is no dtoverlay specific to this chip. I have been able to enable the i2c via intfc:i2c7=on in /boot/hw_intfc.conf. I can also talk with the chip via i2c-tools. I noticed there is an rtc_ds1307 module in /lib/modules/4.4.194--- but I am not sure how to implement that from there.

It looks like this has been somewhat answered on this thread:

I followed paal’s answer and modified their script. I would include it here, but I don’t know how to format it correctly… And you can just create a systemd service that calls the script once your system has booted or add it to the cron.

Hi, I’m having the same problem as you with the DS1307. I’ve followed this guy’s steps and I haven’t been able to solve it. Could you indicate how you managed to solve it?

You’re going to have to be more specific about what isn’t working. I described how I solved it above.

Yeah, sorry, I’m following Paal’s answer, but in my case the kernel is 4.4.194-11-rk3399, I’m using debian 11 on a Rock 4c+. Following the steps of many forums and trying to do it in a similar way to raspberry, I have detected my rtc on bus 7, therefore I assume that I should change the script in that sense, but it still does not work, when I run it directly, the following message appears: RTC not detected, exiting. I guess it will be because the output of my i2cdetect -y 7 is not 68 since it has changed to UU.
I have already tried to modify the /etc/modules with what I have been investigating, also in /etc/rc.local I have added the following before the exit: echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-7 /new_device
hwclock -s
Despite all this, I am still not able to get my RTC to do its job every time I turn off the Rock, since the date always returns to August 4, 2017. The RTC ds1307 module is loaded and the drivers too, but I don’t know what else. do.

I hope I have been clear enough and that you have been able to understand me, I am new to this and perhaps there is something that I am not taking into account, thank you very much for your interest.

It looks like you are trying to use the methods described on the adafruit website to use the RTC module. That is for the raspberry pi and I am not sure that this will work, the two operating systems are quite different. If you have been messing around for a while with trying to get this RTC to work, it is possible that you may have made some unintended change that can make it difficult to fix. If you have an extra SD card, try restarting the process with a fresh install, or do a fresh install on the SD card you are using now.

You shouldn’t need to modify either /etc/modules or /etc/rc.local, and you don’t need to echo ds1307 0x68 > sys/class… (In fact that might be what is causing the paal’s script to fail).

After a fresh install, you have to enable the i2c in /boot/hw_intf.conf and then install i2c-tools. Reboot, and then look to see if you can see the RTC with the cmd i2cdetect -y 7. If you see it then try and run Paal’s script and see what happens. You do have to modify paal’s script a little bit to include the corrent i2c channel you are using, but I think that is only in I2C_GET, I2C_SET and I2C_DETECT definitions.

I have started from scratch as you told me and indeed when I did the cmd i2cdetectl -y 7 I could see the RTC, however having modified the script in the way that I had thought and which is just the one you commented the message continues to appear:
line 48: i2cdetect: command not found
RTC not detected, exiting.

I am attaching the modified script in case you made any other changes and could let me know. Once again thanks for your help

#!/bin/bash

I2C_ADDR=“68”
RTC_REG=“0x00”

I2C_GET=“i2cget -y 7 0x$I2C_ADDR”
I2C_SET=“i2cset -y 7 0x$I2C_ADDR”
I2C_DETECT=“i2cdetect -y 7 0x$I2C_ADDR 0x$I2C_ADDR”

DATE_CHECK="^[12][0-9]{3}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01]) ([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$";

MYNAMEIS="$(basename $0)"

read_rtc () {
NEW=""; for NN in {0…6}; do NEW="$NEW $($I2C_GET $(($RTC_REG + $NN)) b)"; done
NEW="$(echo $NEW | awk ‘{printf “20%02X-%02X-%02X %02X:%02X:%02X”, $7, $6, $5, $3, $2, $1}’)"
echo “RTC time data: $NEW”
if [[ $NEW =~ $DATE_CHECK ]]; then
echo “Setting system time, please wait (10 sec.) …”
timedatectl set-ntp 0
sleep 5
timedatectl set-time “$NEW”
if [ $? -ne 0 ]; then echo “FAILED: timedatectl (Setting too often?)”; fi
sleep 5
timedatectl set-ntp 1
echo “Finished.”
else
echo “RTC time is not correct. (Maybe battery replaced?)”
echo “Try resetting RTC first by running: $MYNAMEIS reset-rtc”
echo “Nothing changed.”
fi
}

write_rtc () {
NEW="$(date +‘0x%02S 0x%02M 0x%02H 0x%02u 0x%02d 0x%02m 0x%02y’)"
echo “Sending data to RTC: $NEW”
echo " sec. min. hour wkday day mnth year"
$I2C_SET $RTC_REG $NEW i
if [ $? -ne 0 ]; then echo “FAILED: i2cset (Some epic fail?)”; fi
echo “Giving it a sec, then reading back …”
sleep 1
NEW=""; for NN in {0…6}; do NEW="$NEW $($I2C_GET $(($RTC_REG + $NN)) b)"; done
NEW="$(echo $NEW | awk ‘{printf “20%02X-%02X-%02X %02X:%02X:%02X”, $7, $6, $5, $3, $2, $1}’)"
echo “RTC time data: $NEW”
}

if [ -z "$($I2C_DETECT | grep " $I2C_ADDR “)” ]; then
echo “RTC not detected, exiting.”
exit 1
fi

if [[ $# -gt 0 ]]; then
if [ $1 == ‘reset-rtc’ ]; then
echo “Resetting RTC with current system time …”
write_rtc
exit 0
fi
fi

if [ -z “$(timedatectl status | sed -n ‘s/.System clock synchronized[^a-zA-Z](yes).*/\1/Ip’)” ]; then
echo “NTP time is NOT syncronised, getting RTC-time …”
read_rtc
else
echo “NTP time is syncronised, overwriting RTC-time …”
write_rtc
fi

How are you running the script? Are you using sudo sh script.sh?

No, im using bash script.sh but it said RCT not detected and line 48 i2cdetect command not found, so I think the problem might be in the script. Update: line 48 i2cdetect dissapeared changing it the script for /usr/sbin/i2cdetect. I have also given permission to the scrip so now im running it simply with ./script.sh, however RTC not detected still there.

You have tried running i2cdetect via the console and it works. The error you are having is saying that i2cdetect command not found. Think for a second at what this might mean, error messages can give very useful information.

There shouldn’t be a problem with the script, I would recommend running the script with sudo.

I honestly believe that my problem comes directly from the rtc, if I run dmesg | grep rtc shows the following:

[ 2.131175] rk808-rtc rk808-rtc: rtc core: registered rk808-rtc as rtc0
[ 2.749175] rockchip-vop ff900000.vop: [drm:vop_crtc_enable] Update mode to 1920x1080p60, type: 11
[ 3.155619] rk808-rtc rk808-rtc: setting system clock to 2017-08-04 09:00:16 UTC (1501837216)
[ 10.786369] rtc-ds1307 7-0068: rtc core: registered ds1307 as rtc1
[ 10.786965] rtc-ds1307 7-0068: 56 byte nvram

So I think it is that rtc0 that the system is using when I do any hwclock -r or -w, instead of the ds1307 that I am trying to configure, which would explain that when turning off the device it always returns to the date that appears there for the rtc0. Also when I run hwclock -r -f /dev/rtc1 I get an invalid arguments error.

It could be the RTC, but unless did something to fry it, it probably isn’t. If you want to keep at it, there should be enough information in this thread to get it to work.

You can test to see if the RTC works by using the i2cget command, try something like i2cget -y 7 0x68 0. Look at the data sheet for the RTC if you want to understand more about how this works.

Also, the method described here doesn’t attempt to overwrite the default hwclock. You are supposed to run the script at boot to set the clock via timedatectl. The OS will have the wrong time until you run the script. You can set the script to run at boot with systemctl.

I managed to write the date in rtc1 with hwclock -w -f /de/rtc1, in fact if I turn off my device this rtc works correctly since when it starts it has the correct date and time, but if I do the same with rtc0 and with the system time both appear with the date of 2017, that is why I think I must include or modify a file so that my system takes the date of rtc1 at startup