Setting up the RTC with DS3231(DS1307) or NTP

Post Reply
radudorurus
Posts: 37
Joined: Fri Jun 16, 2017 6:48 pm
languages_spoken: english, romanian
Has thanked: 2 times
Been thanked: 3 times
Contact:

Setting up the RTC with DS3231(DS1307) or NTP

Post by radudorurus »

Hello.
I need some help.
I have one RTC module DS3231.
I installed Ubuntu 18 latest at this date (06.06.2020).

without RTC hardware

First I wanted to work without RTC hardware but there is no way to make it work and update via NTP. (I remember the Ubuntu 16 worked properly 2-3 years ago without RTC over i2c.
The machine boots every time with default time.
My router is also ntp server so I typed its IP in the conf file.
But this should work without this since the router catches the NTP requests and responds itself back because it is faster.
(I have many sonoff devces with tasmota and NTP is working very good)

with RTC hardware
There is no hint in wiki about this i2c module.
I tried to follow the simple instructions from viewtopic.php?p=162124#p162124
I checked the modules are loaded but there is no way to access hwclock (error)
I tried to insert the module DS1307 in dtb file using rtc_ds1307@68 instead pcf8563@51
fdtput -t s /media/boot/meson64_odroidc2.dtb /i2c@c1108500/pcf8563@51 status "okay"
error result: FDT_ERR_NOTFOUND.

I have the modules specified in the modules file and rc.local.
The lib for DS1307 exists with the name rtc-ds1307.ko

dmesg result is:
root@odroid:~# dmesg | tail -3
[ 32.182015] i2sin_fifo0_set_buf
[ 37.235031] aiu i2s playback disable
[ 37.235047] audio_hw_958_enable 0
(I tried more lines but no i2c module showed up)

lsmod answer is:
root@odroid:~# lsmod
Module Size Used by
rtc_ds1307 10615 0
fuse 90892 3
squashfs 32435 0
joydev 11035 0
w1_gpio 3801 0
wire 28614 1 w1_gpio
meson_gpiomem 4040 0
gxbb_wdt 3395 0
sch_fq_codel 7827 2
sx865x 5179 0
aml_i2c 16735 0
ip_tables 17220 0
ipv6 387317 44
autofs4 27592 2

Please note the name of the module here rtc_ds1307 versus the filename in lib rtc-ds1307 (underscore vs minus)
Could this be the problem?

If the time is not set properly, internet browsing result in errors.
So is there any tutorial about making the system time/date work properly with NTP or/and RTC DS3231 (DS1307)?

User avatar
odroid
Site Admin
Posts: 34901
Joined: Fri Feb 22, 2013 11:14 pm
languages_spoken: English, Korean, Japanese
ODROIDs: ODROID
Has thanked: 953 times
Been thanked: 768 times
Contact:

Re: Setting up the RTC with DS3231(DS1307) or NTP

Post by odroid »

As far as I remember, the NTP function worked fine without any hardware RTC.

BTW, does "hwclock" command work fine?
https://wiki.odroid.com/accessory/add-o ... figuration
These users thanked the author odroid for the post:
radudorurus (Fri Jun 12, 2020 2:09 am)

radudorurus
Posts: 37
Joined: Fri Jun 16, 2017 6:48 pm
languages_spoken: english, romanian
Has thanked: 2 times
Been thanked: 3 times
Contact:

Re: Setting up the RTC with DS3231(DS1307) or NTP

Post by radudorurus »

Exactly this is I remember using C2 with Ubuntu 16 in the past. NTP worked ok. I do not remember having to mess with files and CLI.
If NTP would have worked out of the box, I would not considered the RTC i2c module.

The NTP may not work because it is set to sync at boot (network manager) and also to load RTC modules which does not work.

As about the hwclock, the tutorial may work with the RTC PCF module but not with the DS one.
I was stuck at compliling de rtc module in meson64_odroidc2.dtb file.(I explained previously).

Any hwclock command returns error:
"There are no known methods to access hwclock. Use debug option"
It is clear the rtc device is not loaded.

Maybe somebody experienced and who has this DS3232 module will be able to give me some hints.

User avatar
joerg
Posts: 1142
Joined: Tue Apr 01, 2014 2:14 am
languages_spoken: german, english, español
ODROIDs: C1, C1+, C2, N1, N2, C4
Location: Germany
Has thanked: 55 times
Been thanked: 126 times
Contact:

Re: Setting up the RTC with DS3231(DS1307) or NTP

Post by joerg »

Have you tried it this way?

Code: Select all

fdtput -r  meson64_odroidc2.dtb /i2c@c1108500/pcf8563@51
fdtput -c  meson64_odroidc2.dtb /i2c@c1108500/rtc@68
fdtput -t s  meson64_odroidc2.dtb /i2c@c1108500/rtc@68 compatible "ds3231"
fdtput -t s  meson64_odroidc2.dtb /i2c@c1108500/rtc@68 reg "<0x68>"
fdtput -t s  meson64_odroidc2.dtb /i2c@c1108500/rtc@68 status "okay"
When I check with fdtdump this is looking as it should:

Code: Select all

    i2c@c1108500 {
        compatible = "amlogic, meson-i2c";
        dev_name = "i2c-A";
        status = "okay";
        reg = <0x00000000 0xc1108500 0x00000000 0x00000020>;
        device_id = <0x00000001>;
        pinctrl-names = "default";
        pinctrl-0 = <0x0000000c>;
        #address-cells = <0x00000001>;
        #size-cells = <0x00000000>;
        use_pio = <0x00000000>;
        master_i2c_speed = <0x000493e0>;
        clocks = <0x00000008 0x0000000a>;
        clock-names = "clk_i2c";
        resets = <0x00000008 0x00000009>;
        rtc@68 {
            status = "okay";
            reg = "<0x68>";
            compatible = "ds3231";
        };
    };
But I can't check it with C2 as all my C2 are in use.

radudorurus
Posts: 37
Joined: Fri Jun 16, 2017 6:48 pm
languages_spoken: english, romanian
Has thanked: 2 times
Been thanked: 3 times
Contact:

Re: Setting up the RTC with DS3231(DS1307) or NTP

Post by radudorurus »

Thanks.
I will check and come with feedback.

But... shouldn't the device rtc@68 fit with the name of the module (rtc-ds1307 or rtc_ds1307)?

User avatar
joerg
Posts: 1142
Joined: Tue Apr 01, 2014 2:14 am
languages_spoken: german, english, español
ODROIDs: C1, C1+, C2, N1, N2, C4
Location: Germany
Has thanked: 55 times
Been thanked: 126 times
Contact:

Re: Setting up the RTC with DS3231(DS1307) or NTP

Post by joerg »

This, how I wrote, works on my C4 on Android/LineageOS. I think with compatible="ds3231" starts the right driver. I will try it on my C4 with Armbian to see if it works on Linux.

radudorurus
Posts: 37
Joined: Fri Jun 16, 2017 6:48 pm
languages_spoken: english, romanian
Has thanked: 2 times
Been thanked: 3 times
Contact:

Re: Setting up the RTC with DS3231(DS1307) or NTP

Post by radudorurus »

Sorry it does not work on my side.
The fdt file dump looks ok

Code: Select all

    i2c@c1108500 {
        compatible = "amlogic, meson-i2c";
        dev_name = "i2c-A";
        status = "okay";
        reg = <0x00000000 0xc1108500 0x00000000 0x00000020>;
        device_id = <0x00000001>;
        pinctrl-names = "default";
        pinctrl-0 = <0x00000011>;
        #address-cells = <0x00000001>;
        #size-cells = <0x00000000>;
        use_pio = <0x00000000>;
        master_i2c_speed = <0x000493e0>;
        clocks = <0x0000000d 0x0000000a>;
        clock-names = "clk_i2c";
        resets = <0x0000000d 0x00000009>;
        rtc_ds1307@68 {
            status = "okay";
            reg = "<0x68>";
            compatible = "ds3231";
        };
ls /dev/rtc* command returns nothing
there is no RTC device/file/directory in the directory /dev
As a result there is no way to access hwclock command

The module seems to be loaded properly

Code: Select all

root@odroid:/media/boot# lsmod
Module                  Size  Used by
sg                     31094  0
fuse                   90892  3
squashfs               32435  0
joydev                 11035  0
w1_gpio                 3801  0
wire                   28614  1 w1_gpio
meson_gpiomem           4040  0
gxbb_wdt                3395  0
sch_fq_codel            7827  2
rtc_ds1307             10615  0
sx865x                  5179  0
aml_i2c                16735  0
ip_tables              17220  0
ipv6                  387317  44
autofs4                27592  2
The NTP service is disabled so that do not start at boot.

Any ideas?

User avatar
joerg
Posts: 1142
Joined: Tue Apr 01, 2014 2:14 am
languages_spoken: german, english, español
ODROIDs: C1, C1+, C2, N1, N2, C4
Location: Germany
Has thanked: 55 times
Been thanked: 126 times
Contact:

Re: Setting up the RTC with DS3231(DS1307) or NTP

Post by joerg »

I tried it now on my C2, but there I have Armbian running and a real DS1307 on the interface board. So it is a bit different.

Code: Select all

sudo fdtput -c meson-gxbb-odroidc2.dtb /soc/bus@c1100000/i2c@8500/rtc_ds1307@68
sudo fdtput -t s  meson-gxbb-odroidc2.dtb /soc/bus@c1100000/i2c@8500/rtc_ds1307@68 compatible "ds1307"
sudo fdtput -t s  meson-gxbb-odroidc2.dtb /soc/bus@c1100000/i2c@8500/rtc_ds1307@68 reg "<0x68>"
sudo fdtput -t s  meson-gxbb-odroidc2.dtb /soc/bus@c1100000/i2c@8500/rtc_ds1307@68 status "okay"
Failure:

Code: Select all

dmesg|grep rtc
[    1.872851] i2c i2c-0: of_i2c: Failure registering /soc/bus@c1100000/i2c@8500/rtc_ds1307@68
[    1.872855] i2c i2c-0: Failed to create I2C device for /soc/bus@c1100000/i2c@8500/rtc_ds1307@68
Once more, now rtc@68:

Code: Select all

sudo fdtput -r meson-gxbb-odroidc2.dtb /soc/bus@c1100000/i2c@8500/rtc_ds1307@68
sudo fdtput -c meson-gxbb-odroidc2.dtb /soc/bus@c1100000/i2c@8500/rtc@68
sudo fdtput -t s  meson-gxbb-odroidc2.dtb /soc/bus@c1100000/i2c@8500/rtc@68 compatible "ds1307"
sudo fdtput -t s  meson-gxbb-odroidc2.dtb /soc/bus@c1100000/i2c@8500/rtc@68 reg "<0x68>"
sudo fdtput -t s  meson-gxbb-odroidc2.dtb /soc/bus@c1100000/i2c@8500/rtc@68 status "okay"
Failure:

Code: Select all

dmesg|grep rtc
[    1.868154] i2c i2c-0: of_i2c: Failure registering /soc/bus@c1100000/i2c@8500/rtc@68
[    1.868158] i2c i2c-0: Failed to create I2C device for /soc/bus@c1100000/i2c@8500/rtc@68
It's not important how the node is named. :idea:

Code: Select all

sudo modprobe rtc-ds1307
lsmod
Module                  Size  Used by
rtc_ds1307             40960  0
No rtc device.

Next try, add ds1307 by sysfs:

Code: Select all

su root
sudo echo ds1307 0x68 > /sys/bus/i2c/devices/i2c-0/new_device
dmesg|grep rtc
[  853.659981] rtc-ds1307 0-0068: registered as rtc0
ls -la /dev/rtc*
lrwxrwxrwx 1 root root      4 Jun 10 18:54 /dev/rtc -> rtc0
crw------- 1 root root 252, 0 Jun 10 18:54 /dev/rtc0
hwclock
2020-06-10 18:59:35.553789+0200
Need to investigate more why it does not work with devicetree.
I find it difficult to manage the sysfs method by systemd, as (I see on Armbian) the new_device has permission --w-------, I had to change to root.
These users thanked the author joerg for the post:
radudorurus (Fri Jun 12, 2020 2:08 am)

radudorurus
Posts: 37
Joined: Fri Jun 16, 2017 6:48 pm
languages_spoken: english, romanian
Has thanked: 2 times
Been thanked: 3 times
Contact:

Re: Setting up the RTC with DS3231(DS1307) or NTP

Post by radudorurus »

I am preparing now another card with Ubuntu 16 to check RTC and NTC.

User avatar
joerg
Posts: 1142
Joined: Tue Apr 01, 2014 2:14 am
languages_spoken: german, english, español
ODROIDs: C1, C1+, C2, N1, N2, C4
Location: Germany
Has thanked: 55 times
Been thanked: 126 times
Contact:

Re: Setting up the RTC with DS3231(DS1307) or NTP

Post by joerg »

Maybe you can give a try: compatible = "maxim,ds3231".
I would not make more experiments for today with my C2. There is running my home automation. ;)

radudorurus
Posts: 37
Joined: Fri Jun 16, 2017 6:48 pm
languages_spoken: english, romanian
Has thanked: 2 times
Been thanked: 3 times
Contact:

Re: Setting up the RTC with DS3231(DS1307) or NTP

Post by radudorurus »

Thanks.
It worked after all.
but...
After reboot I had the default time, ..2016 bla bla
I checked the rtc/hwclock... nothing.

I had to run again
echo ds3231 0x68 > /sys/bus/i2c/devices/i2c-1/new_device (I have i2c 1 and 2 not 0 and 1)
(Is it still necesary to follow the tree-compiler way? I will check it to see it it works directly.)

So I had to put this command in etc/rc.local along with the hwclock -s command.
Probably I have to write a tutorial since the one from wiki does not work for the module DS3231/1307 and I have do all the job again on the Ubuntu 18 instalation.

Is there any chance to make the NTP service run from time to time to sync the clock with ntp server?

mad_ady
Posts: 8329
Joined: Wed Jul 15, 2015 5:00 pm
languages_spoken: english
ODROIDs: XU4, C1+, C2, C4, N1, N2, H2, Go, Go Advance
Location: Bucharest, Romania
Has thanked: 573 times
Been thanked: 438 times
Contact:

Re: Setting up the RTC with DS3231(DS1307) or NTP

Post by mad_ady »

NTP runs as a daemon and keeps in sync with NTP peers at all times. It might take a long time (or refuse) to sync when there is more than 1000s time difference. You could use ntpdate (deprecated) with cron, but it requires that ntp is off.

An alternative to RTC is fakehwclock which saves your current time on shutdown and restores it on boot. Time will be wrong until it syncs but correct enough that certificates are still valid.

radudorurus
Posts: 37
Joined: Fri Jun 16, 2017 6:48 pm
languages_spoken: english, romanian
Has thanked: 2 times
Been thanked: 3 times
Contact:

Re: Setting up the RTC with DS3231(DS1307) or NTP

Post by radudorurus »

So... thanks to @joerg and @odroid I was able to put things head to head and write this method.
Keep in mind I am not a linux geek so that I wrote this like everybody (including noobs) to understand.

Environment:
- OdroidC2
- Ubuntu 16 or 18 image flashed on 64GB card (it suits on 16GB without problem)
- RTC module DS3231 (made for RaspberryPi) with the little battery bent at 90deg (as in picture)
(I bought this module 1 year ago, I did not used and the battery has 3.2V)
- NTP service is stopped and does not run at boot.
IMG_20200611_181320.jpg
IMG_20200611_181320.jpg (273.81 KiB) Viewed 1134 times
IMG_20200611_181243.jpg
IMG_20200611_181243.jpg (289.27 KiB) Viewed 1134 times
Tasks:

Step 0.
Optional task - clear the i2c node from the meson64_odroidc2.dtb
(I mean it does not harm and it does not help but I preferred the i2c node to be not assigned):
There is not necessary to compile the rtc node in the /media/boot/meson64_odroidc2.dtb file
Anyway it won't work at all if you follow the method from the wiki.

I did dump the file to check what it is on the first i2c node

Code: Select all

root@odroid:~# fdtdump -s /media/boot/meson64_odroidc2.dtb
See I was logged as root. If you are not logged as root, the command has to start with sudo in front:

Code: Select all

odrois@odroid:~  sudo fdtdump -s /media/boot/meson64_odroidc2.dtb
The result is a long list. If you copy in a text editor, i2c nodes are mentioned around the line 580 from a total 1420.(my case)
Maybe a search after the string "i2c@c1108500" is better to use. This was in my list after trying to make it work using the odroid wiki method.

Code: Select all

    i2c@c1108500 {
        compatible = "amlogic, meson-i2c";
        dev_name = "i2c-A";
        status = "okay";
        reg = <0x00000000 0xc1108500 0x00000000 0x00000020>;
        device_id = <0x00000001>;
        pinctrl-names = "default";
        pinctrl-0 = <0x00000011>;
        #address-cells = <0x00000001>;
        #size-cells = <0x00000000>;
        use_pio = <0x00000000>;
        master_i2c_speed = <0x000493e0>;
        clocks = <0x0000000d 0x0000000a>;
        clock-names = "clk_i2c";
        resets = <0x0000000d 0x00000009>;
        rtc_ds1307@68 {
            status = "okay";
            reg = "<0x68>";
            compatible = "ds3231";
        };
     };

In short, the node can be cleared by (my node name was rtc_ds1307@68 because I compiled it; you may have something different but you already found the name using the method above) :

Code: Select all

sudo fdtput -r  /media/boot/meson64_odroidc2.dtb /i2c@c1108500/rtc_ds1307@68
If you wish to check if the i2c was cleared, just run again the fdtdump command.

Step 1.
The modules for i2c communication and rtc have to be loaded at start, so they need to be inserted in the modules file.

a. This can be done with text editor directly on the odroidc2 desktop.
Open the file browser caja or another, navigate to filesystem /etc
Locate the file "modules", right click, choose "edit as Administrator", type your odroid password.
Now in the text editor, add these 2 lines at the end of the file:
aml_i2c
rtc_ds1307
Save the file and close.

b. The second method is to run 2 commands in terminal (already explained in the wiki):

Code: Select all

echo "aml_i2c"|sudo tee -a /etc/modules
echo "rtc_ds1307"|sudo tee -a /etc/modules
Step 2.
Edit the rc.local file as in step 1.a. (this is also mentioned in the wiki; we add supplementary the line beginning with "echo")
Add the following lines at the end of the file:

Code: Select all

if [ -f /aafirstboot ]; then /aafirstboot start ; fi

echo ds3231 0x68 > /sys/bus/i2c/devices/i2c-1/new_device

hwclock -s

exit 0
Now save the changes to the file.

What does all of this?
- run the commands at every boot
- load the rtc device and name it "ds3231" (you can name it whatever you vant)
- load the time from the i2c clock in the system
- exits the script

Step 3.
You can observe what time and date do you have on the upper right corner.
Reboot the odroid by mouse > GUI interface (restart) ot type in the terminal "reboot" command.

Step 4.
After reboot, login and observe the time/date is different from the instance before the reboot.
This is the time from the i2c module.
Right click on the clock> Preferences > Locations (set your time zone if you did not before)
Press "time settings" button and set everything there.
When you are ready, press "set system time". Now there will be no error (like before when there was no hardware clock and some error appeared)
The time/date you set is written in the i2c module.

Step 5.
Perform another reboot to check if the time and date are really saved and loaded from the Real Time Clock module.

Other commands for help you can run in terminal for debugging:

Code: Select all

lsmod
(displays the loaded modules; you can use to check if the modules aml_i2c and rtc_ds1307 are loaded)

Code: Select all

ls -la /dev/rtc*
(displays the devices named rtc; running this command before starting this all process, will return "no such directory")

Code: Select all

dmesg|grep ds3231
(run this command and you will get lines of confirmation DS3231 is loaded as rtc0 and it is registered at address 0x68)

Code: Select all

hwclock
no argument returns the current system time/date
Last edited by radudorurus on Fri Jun 12, 2020 1:35 pm, edited 2 times in total.
These users thanked the author radudorurus for the post:
odroid (Fri Jun 12, 2020 8:45 am)

User avatar
joerg
Posts: 1142
Joined: Tue Apr 01, 2014 2:14 am
languages_spoken: german, english, español
ODROIDs: C1, C1+, C2, N1, N2, C4
Location: Germany
Has thanked: 55 times
Been thanked: 126 times
Contact:

Re: Setting up the RTC with DS3231(DS1307) or NTP

Post by joerg »

Fine that you could solve it and post it for others. And nobody is linux geek from from the first day.

User avatar
rooted
Posts: 7888
Joined: Fri Dec 19, 2014 9:12 am
languages_spoken: english
Location: Gulf of Mexico, US
Has thanked: 724 times
Been thanked: 227 times
Contact:

Re: Setting up the RTC with DS3231(DS1307) or NTP

Post by rooted »

@radudorurus What case is that?

radudorurus
Posts: 37
Joined: Fri Jun 16, 2017 6:48 pm
languages_spoken: english, romanian
Has thanked: 2 times
Been thanked: 3 times
Contact:

Re: Setting up the RTC with DS3231(DS1307) or NTP

Post by radudorurus »

It is described in detail above and the thread title.

User avatar
joerg
Posts: 1142
Joined: Tue Apr 01, 2014 2:14 am
languages_spoken: german, english, español
ODROIDs: C1, C1+, C2, N1, N2, C4
Location: Germany
Has thanked: 55 times
Been thanked: 126 times
Contact:

Re: Setting up the RTC with DS3231(DS1307) or NTP

Post by joerg »

I think @rooted asked for the housing of the C2.
These users thanked the author joerg for the post:
rooted (Fri Jun 12, 2020 7:53 pm)

User avatar
rooted
Posts: 7888
Joined: Fri Dec 19, 2014 9:12 am
languages_spoken: english
Location: Gulf of Mexico, US
Has thanked: 724 times
Been thanked: 227 times
Contact:

Re: Setting up the RTC with DS3231(DS1307) or NTP

Post by rooted »

joerg wrote:I think @rooted asked for the housing of the C2.
Indeed, I was talking about the transparent enclosure.

Thanks joerg.

radudorurus
Posts: 37
Joined: Fri Jun 16, 2017 6:48 pm
languages_spoken: english, romanian
Has thanked: 2 times
Been thanked: 3 times
Contact:

Re: Setting up the RTC with DS3231(DS1307) or NTP

Post by radudorurus »

These users thanked the author radudorurus for the post:
rooted (Fri Jun 12, 2020 9:21 pm)

User avatar
rooted
Posts: 7888
Joined: Fri Dec 19, 2014 9:12 am
languages_spoken: english
Location: Gulf of Mexico, US
Has thanked: 724 times
Been thanked: 227 times
Contact:

Re: Setting up the RTC with DS3231(DS1307) or NTP

Post by rooted »

radudorurus wrote:I think this is the enclosure.
https://www.amazon.com/LoveRPi-Performa ... 01ESKQYE8/
I believe that's it, too bad it's for a Pi unless modified.

I don't understand why there aren't more cases for Odroid devices, especially the C2 and XU4 as those have been super popular.

radudorurus
Posts: 37
Joined: Fri Jun 16, 2017 6:48 pm
languages_spoken: english, romanian
Has thanked: 2 times
Been thanked: 3 times
Contact:

Re: Setting up the RTC with DS3231(DS1307) or NTP

Post by radudorurus »

You are right.
I had to drill another hole for the power barrel.
Also I had to use a drill/mill to make place and opening for the memory card.

But I've got it as gift. No complains.
These users thanked the author radudorurus for the post:
rooted (Sat Jun 13, 2020 4:34 am)

Post Reply

Return to “Hardware and peripherals”

Who is online

Users browsing this forum: No registered users and 0 guests