Difference between revisions of "RTC"

From FriendlyELEC WiKi
Jump to: navigation, search
(set hardware clock time to system local timeas)
(Step3: Verify that the RTC is working properly)
 
(2 intermediate revisions by the same user not shown)
Line 77: Line 77:
 
</syntaxhighlight>
 
</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.
 
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 hardware clock as local time===
+
===set the RTC from the system time===
 
You can also use the following command:
 
You can also use the following command:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
Line 86: Line 86:
 
sudo hwclock -f /dev/rtc1 -r
 
sudo hwclock -f /dev/rtc1 -r
 
</syntaxhighlight>
 
</syntaxhighlight>
===set hardware clock time to system local times ===
+
 
 +
===set the system time from the RTC===
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
sudo hwclock --hctosys -f /dev/rtc1
+
sudo hwclock -f /dev/rtc1 -s
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 104: Line 105:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
echo "sys time: $(date)" && echo "hw time: " $(sudo hwclock -f /dev/rtc1 -r)
 
echo "sys time: $(date)" && echo "hw time: " $(sudo hwclock -f /dev/rtc1 -r)
</syntaxhighlight>
 
===Test writing time to RTC===
 
Shut down the board, disconnect the RTC battery let the RTC time loss, then plug-in the network cable and power on the board, it is best to wait a few minutes to complete the synchronization, and then check the status, because the synchronization time is set using the crontab timer task, may have to wait a little longer:
 
<syntaxhighlight lang="bash">
 
$ echo "sys time: $(date)" && echo "hw time: " $(sudo hwclock -f /dev/rtc1 -w)
 
sys time: Fri Mar  4 17:42:15 HKT 2022
 
hw time:  2022-03-04 17:42:16.493946+08:00
 
 
</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)