Difference between revisions of "RTC/zh"
From FriendlyELEC WiKi
(Created page with "English ===S5P4418/S5P6818=== Use the following command to see the GPIO pins.<br /> <syntaxhighlight lang="bash"> </syntaxhighlight>") |
(updated by API) |
||
(5 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
[[RTC|English]] | [[RTC|English]] | ||
− | === | + | |
− | + | ==Step1: 设置时区== | |
+ | ===查看可用时区=== | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
+ | timedatectl list-timezones | ||
+ | </syntaxhighlight> | ||
+ | ===更改时区=== | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | sudo timedatectl set-timezone Asia/Hong_Kong | ||
+ | </syntaxhighlight> | ||
+ | ===查看当前系统时间=== | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | date | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ==Step2: 更换Linux系统的时间同步服务为ntpd (需连接网络)== | ||
+ | ===使用ntpd代替timesyncd=== | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | sudo timedatectl set-ntp no | ||
+ | sudo apt update | ||
+ | sudo apt install ntp | ||
+ | </syntaxhighlight> | ||
+ | ==Step2: 设置RTC设备 (以H3平台外挂Matrix-RTC为例)== | ||
+ | ===将RTC驱动编译进内核 (非外部模块)=== | ||
+ | 开机时, 内核会从RTC硬件读取时间并设置为系统时间, 为了让RTC驱动在这之前能初始化完成, 所以需要将外挂的RTC DS1307驱动编译进内核, 而不是外部模块, 进入以下位置进行配置: | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | │ Location: │ | ||
+ | │ -> Device Drivers │ | ||
+ | │ -> Real Time Clock (RTC_CLASS [=y]) │ | ||
+ | </syntaxhighlight> | ||
+ | 配置项为: | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | CONFIG_RTC_DRV_DS1307 | ||
+ | </syntaxhighlight> | ||
+ | ===指定正确的RTC设备=== | ||
+ | 由于内核默认使用的rtc设备是rtc0, 而我们在全志平台外挂rtc时, 通常外挂rtc被初始化为rtc1, 所以还需要修改以下内核配置, 指定为rtc1: | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | CONFIG_RTC_HCTOSYS_DEVICE="rtc1" | ||
+ | CONFIG_RTC_SYSTOHC_DEVICE="rtc1" | ||
+ | </syntaxhighlight> | ||
+ | 最终对内核配置的改动如下: | ||
+ | <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> | ||
+ | ===开机查看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> | ||
+ | 从上述信息可以获知, 外挂的RTC被初始化为 rtc1 | ||
+ | ===测试外部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> | ||
+ | 看型号再次确实rtc1就是我们外挂的RTC没错了, hctosys的值为1表示内核会在开机时设置系统时间. | ||
+ | ===将系统时间写入RTC硬件=== | ||
+ | 也可以用以下命令: | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | sudo hwclock --systohc -f /dev/rtc1 | ||
+ | </syntaxhighlight> | ||
+ | 查看一下当前的硬件时间: | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | sudo hwclock --show -f /dev/rtc1 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ===其他文件系统修改=== | ||
+ | 用vim编辑如下文件, 将rtc0改为rtc1: | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | /etc/crontab | ||
+ | /etc/init.d/hwclock.sh | ||
+ | /etc/init.d/sync_ntp_rtc | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ==Step3: 验证RTC是否正常工作== | ||
+ | ===测试开机从RTC读取时间=== | ||
+ | 关机并拨掉网线, 过一段时间重新开机, 检查一下系统时间是否正确: | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | echo "sys time: $(date)" && echo "hw time: " $(sudo hwclock --show -f /dev/rtc1) | ||
+ | </syntaxhighlight> | ||
+ | 如果是现实世界的时间, 说明RTC时钟工作了. | ||
+ | ===测试写入系统时间到RTC=== | ||
+ | 关机, 插拨一次RTC电池让RTC时间丢失, 再插上网线开机, 最好等几分钟同步完成, 再查看状态, 因为同步时间是使用crontab设置的定时任务, 可能要等久一点: | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | $ echo "sys time: $(date)" && echo "hw time: " $(sudo hwclock --show -f /dev/rtc1) | ||
+ | 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 11:47, 4 March 2022
Contents
1 Step1: 设置时区
1.1 查看可用时区
timedatectl list-timezones
1.2 更改时区
sudo timedatectl set-timezone Asia/Hong_Kong
1.3 查看当前系统时间
date
2 Step2: 更换Linux系统的时间同步服务为ntpd (需连接网络)
2.1 使用ntpd代替timesyncd
sudo timedatectl set-ntp no sudo apt update sudo apt install ntp
3 Step2: 设置RTC设备 (以H3平台外挂Matrix-RTC为例)
3.1 将RTC驱动编译进内核 (非外部模块)
开机时, 内核会从RTC硬件读取时间并设置为系统时间, 为了让RTC驱动在这之前能初始化完成, 所以需要将外挂的RTC DS1307驱动编译进内核, 而不是外部模块, 进入以下位置进行配置:
│ Location: │ │ -> Device Drivers │ │ -> Real Time Clock (RTC_CLASS [=y]) │
配置项为:
CONFIG_RTC_DRV_DS1307
3.2 指定正确的RTC设备
由于内核默认使用的rtc设备是rtc0, 而我们在全志平台外挂rtc时, 通常外挂rtc被初始化为rtc1, 所以还需要修改以下内核配置, 指定为rtc1:
CONFIG_RTC_HCTOSYS_DEVICE="rtc1" CONFIG_RTC_SYSTOHC_DEVICE="rtc1"
最终对内核配置的改动如下:
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 开机查看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)
从上述信息可以获知, 外挂的RTC被初始化为 rtc1
3.4 测试外部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
看型号再次确实rtc1就是我们外挂的RTC没错了, hctosys的值为1表示内核会在开机时设置系统时间.
3.5 将系统时间写入RTC硬件
也可以用以下命令:
sudo hwclock --systohc -f /dev/rtc1
查看一下当前的硬件时间:
sudo hwclock --show -f /dev/rtc1
3.6 其他文件系统修改
用vim编辑如下文件, 将rtc0改为rtc1:
/etc/crontab /etc/init.d/hwclock.sh /etc/init.d/sync_ntp_rtc
4 Step3: 验证RTC是否正常工作
4.1 测试开机从RTC读取时间
关机并拨掉网线, 过一段时间重新开机, 检查一下系统时间是否正确:
echo "sys time: $(date)" && echo "hw time: " $(sudo hwclock --show -f /dev/rtc1)
如果是现实世界的时间, 说明RTC时钟工作了.
4.2 测试写入系统时间到RTC
关机, 插拨一次RTC电池让RTC时间丢失, 再插上网线开机, 最好等几分钟同步完成, 再查看状态, 因为同步时间是使用crontab设置的定时任务, 可能要等久一点:
$ echo "sys time: $(date)" && echo "hw time: " $(sudo hwclock --show -f /dev/rtc1) sys time: Fri Mar 4 17:42:15 HKT 2022 hw time: 2022-03-04 17:42:16.493946+08:00