Difference between revisions of "RTC"

From FriendlyELEC WiKi
Jump to: navigation, search
(S5P4418/S5P6818)
(Step3: Verify that the RTC is working properly)
 
(11 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
[[RTC/zh|查看中文]]
 
[[RTC/zh|查看中文]]
==S5P4418/S5P6818==
+
 
===Read current datetime===
+
==Step1: Set timezone==
 +
===View available time zones===
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
sudo hwclock -r -f /dev/rtc0
+
timedatectl list-timezones
 
</syntaxhighlight>
 
</syntaxhighlight>
The output results are summarized below: <br />
+
===Change time zone===
Tue 01 Sep 2009 12:07:52 AM UTC  .371098 seconds
+
===Set and save datetime===
+
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
sudo date -s "2019-10-09 17:00:00”
+
sudo timedatectl set-timezone Asia/Hong_Kong
sudo hwclock -w -f /dev/rtc0
+
</syntaxhighlight>
 +
===Check current system time===
 +
<syntaxhighlight lang="bash">
 +
date
 +
</syntaxhighlight>
 +
 
 +
==Step2: Change time synchronization service to ntpd (requires network connection)==
 +
===Use ntpd instead of timesyncd===
 +
<syntaxhighlight lang="bash">
 +
sudo timedatectl set-ntp no
 +
sudo apt update
 +
sudo apt install ntp
 +
</syntaxhighlight>
 +
==Step2: Setting up RTC device (Use Matrix-RTC as example)==
 +
===Build the RTC driver into the kernel (not an external module)===
 +
Go to the following location:
 +
<syntaxhighlight lang="bash">
 +
  │  Location:                                                                                                                  │
 +
  │    -> Device Drivers                                                                                                        │
 +
  │      -> Real Time Clock (RTC_CLASS [=y])                                                                                    │
 +
</syntaxhighlight>
 +
Set CONFIG_RTC_DRV_DS1307 to 'y'.
 +
===Specify the correct RTC device===
 +
Since the default kernel rtc device is rtc0, and when we use external rtc in Allwinner platform, the external rtc is usually initialized to rtc1, so we also need to modify the following kernel configuration to set the value to rtc1:
 +
<syntaxhighlight lang="bash">
 +
CONFIG_RTC_HCTOSYS_DEVICE="rtc1"
 +
CONFIG_RTC_SYSTOHC_DEVICE="rtc1"
 +
</syntaxhighlight>
 +
The kernel configuration:
 +
<syntaxhighlight lang="bash">
 +
diff --git a/arch/arm/configs/sunxi_defconfig b/arch/arm/configs/sunxi_defconfig
 +
index 4c88bbb8..4dbee1a9 100644
 +
--- a/arch/arm/configs/sunxi_defconfig
 +
+++ b/arch/arm/configs/sunxi_defconfig
 +
@@ -1027,11 +1027,12 @@ CONFIG_LEDS_TRIGGER_DEFAULT_ON=y
 +
CONFIG_LEDS_TRIGGER_PANIC=y
 +
CONFIG_LEDS_TRIGGER_NETDEV=y
 +
CONFIG_RTC_CLASS=y
 +
+CONFIG_RTC_HCTOSYS_DEVICE="rtc1"
 +
CONFIG_RTC_INTF_DEV_UIE_EMUL=y
 +
CONFIG_RTC_DRV_TEST=m
 +
CONFIG_RTC_DRV_ABB5ZES3=m
 +
CONFIG_RTC_DRV_ABX80X=m
 +
-CONFIG_RTC_DRV_DS1307=m
 +
+CONFIG_RTC_DRV_DS1307=y
 +
CONFIG_RTC_DRV_DS1307_CENTURY=y
 +
CONFIG_RTC_DRV_DS1374=m
 +
CONFIG_RTC_DRV_DS1374_WDT=y
 +
</syntaxhighlight>
 +
===Check the driver information of rtc===
 +
<syntaxhighlight lang="bash">
 +
pi@NanoPi-M1-Plus:~$ dmesg | grep rtc
 +
[    2.470518] sun6i-rtc 1f00000.rtc: rtc core: registered rtc-sun6i as rtc0
 +
[    2.477351] sun6i-rtc 1f00000.rtc: RTC enabled
 +
[    2.489685] rtc-ds1307 0-0068: registered as rtc1
 +
[    4.009223] rtc-ds1307 0-0068: setting system clock to 2022-03-04 08:22:13 UTC (1646382133)
 +
</syntaxhighlight>
 +
From the above information, we can see that the external RTC is initialized as rtc1
 +
===Testing external RTC===
 +
<syntaxhighlight lang="bash">
 +
$ cat /sys/class/rtc/rtc1/name
 +
rtc-ds1307 0-0068
 +
$ cat /sys/class/rtc/rtc1/time
 +
00:32:27
 +
$ cat /sys/class/rtc/rtc1/hctosys
 +
1
 +
</syntaxhighlight>
 +
Once again, rtc1 is our external RTC, and a value of 1 for hctosys means that the kernel will set the system time at boot time.
 +
===set the RTC from the system time===
 +
You can also use the following command:
 +
<syntaxhighlight lang="bash">
 +
sudo hwclock -f /dev/rtc1 -w
 +
</syntaxhighlight>
 +
View current hardware clock:
 +
<syntaxhighlight lang="bash">
 +
sudo hwclock -f /dev/rtc1 -r
 +
</syntaxhighlight>
 +
 
 +
===set the system time from the RTC===
 +
<syntaxhighlight lang="bash">
 +
sudo hwclock -f /dev/rtc1  -s
 +
</syntaxhighlight>
 +
 
 +
===Some other modifications===
 +
Edit the following file, replacing rtc0 with rtc1:
 +
<syntaxhighlight lang="bash">
 +
/etc/crontab
 +
/etc/init.d/hwclock.sh
 +
/etc/init.d/sync_ntp_rtc
 +
</syntaxhighlight>
 +
 
 +
==Step3: Verify that the RTC is working properly==
 +
===Test read time from RTC===
 +
Shut down the board and disconnect the network cable, reboot after a while, check whether the system time is correct:
 +
<syntaxhighlight lang="bash">
 +
echo "sys time: $(date)" && echo "hw time: " $(sudo hwclock -f /dev/rtc1 -r)
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 09:53, 1 September 2022

查看中文

1 Step1: Set timezone

1.1 View available time zones

timedatectl list-timezones

1.2 Change time zone

sudo timedatectl set-timezone Asia/Hong_Kong

1.3 Check current system time

date

2 Step2: Change time synchronization service to ntpd (requires network connection)

2.1 Use ntpd instead of timesyncd

sudo timedatectl set-ntp no
sudo apt update
sudo apt install ntp

3 Step2: Setting up RTC device (Use Matrix-RTC as example)

3.1 Build the RTC driver into the kernel (not an external module)

Go to the following location:

  │   Location:                                                                                                                  │
  │     -> Device Drivers                                                                                                        │
  │       -> Real Time Clock (RTC_CLASS [=y])

Set CONFIG_RTC_DRV_DS1307 to 'y'.

3.2 Specify the correct RTC device

Since the default kernel rtc device is rtc0, and when we use external rtc in Allwinner platform, the external rtc is usually initialized to rtc1, so we also need to modify the following kernel configuration to set the value to rtc1:

CONFIG_RTC_HCTOSYS_DEVICE="rtc1"
CONFIG_RTC_SYSTOHC_DEVICE="rtc1"

The kernel configuration:

diff --git a/arch/arm/configs/sunxi_defconfig b/arch/arm/configs/sunxi_defconfig
index 4c88bbb8..4dbee1a9 100644
--- a/arch/arm/configs/sunxi_defconfig
+++ b/arch/arm/configs/sunxi_defconfig
@@ -1027,11 +1027,12 @@ CONFIG_LEDS_TRIGGER_DEFAULT_ON=y
 CONFIG_LEDS_TRIGGER_PANIC=y
 CONFIG_LEDS_TRIGGER_NETDEV=y
 CONFIG_RTC_CLASS=y
+CONFIG_RTC_HCTOSYS_DEVICE="rtc1"
 CONFIG_RTC_INTF_DEV_UIE_EMUL=y
 CONFIG_RTC_DRV_TEST=m
 CONFIG_RTC_DRV_ABB5ZES3=m
 CONFIG_RTC_DRV_ABX80X=m
-CONFIG_RTC_DRV_DS1307=m
+CONFIG_RTC_DRV_DS1307=y
 CONFIG_RTC_DRV_DS1307_CENTURY=y
 CONFIG_RTC_DRV_DS1374=m
 CONFIG_RTC_DRV_DS1374_WDT=y

3.3 Check the driver information of rtc

pi@NanoPi-M1-Plus:~$ dmesg | grep rtc
[    2.470518] sun6i-rtc 1f00000.rtc: rtc core: registered rtc-sun6i as rtc0
[    2.477351] sun6i-rtc 1f00000.rtc: RTC enabled
[    2.489685] rtc-ds1307 0-0068: registered as rtc1
[    4.009223] rtc-ds1307 0-0068: setting system clock to 2022-03-04 08:22:13 UTC (1646382133)

From the above information, we can see that the external RTC is initialized as rtc1

3.4 Testing external RTC

$ cat /sys/class/rtc/rtc1/name
rtc-ds1307 0-0068
$ cat /sys/class/rtc/rtc1/time
00:32:27
$ cat /sys/class/rtc/rtc1/hctosys
1

Once again, rtc1 is our external RTC, and a value of 1 for hctosys means that the kernel will set the system time at boot time.

3.5 set the RTC from the system time

You can also use the following command:

sudo hwclock -f /dev/rtc1 -w

View current hardware clock:

sudo hwclock -f /dev/rtc1 -r

3.6 set the system time from the RTC

sudo hwclock -f /dev/rtc1  -s

3.7 Some other modifications

Edit the following file, replacing rtc0 with rtc1:

/etc/crontab
/etc/init.d/hwclock.sh
/etc/init.d/sync_ntp_rtc

4 Step3: Verify that the RTC is working properly

4.1 Test read time from RTC

Shut down the board and disconnect the network cable, reboot after a while, check whether the system time is correct:

echo "sys time: $(date)" && echo "hw time: " $(sudo hwclock -f /dev/rtc1 -r)