RTC
RTC全称为Real Time Clock,是一个专门用来记录时间的硬件设备,一般可以集成在soc内部,或者选择外挂,通过i2c与其通信。
那为什么会需要RTC,因为Linux的系统时间(也就是我们常说的wall time)只能在系统运行时使用,系统关机时间就丢了,而RTC可以在系统关闭后,依靠外部电池或其他supply继续工作,这才将时间保存下来。
RTC的使用
开发板使用外部RTC(hym8563),默认关闭了rk809的rtc功能。
使用外部RTC时需要接入纽扣电池

RTC用户接口调用
Linux 提供了三种用户空间调用接口。在板卡中对应的路径为:
SYSFS接口:/sys/class/rtc/rtc0/
PROCFS接口: /proc/driver/rtc
IOCTL接口: /dev/rtc0
SYSFS接口
cat proc/driver/rtc
rtc_time : 02:01:53
rtc_date : 2025-02-21
alrm_time : 00:00:00
alrm_date : 1970-01-01
alarm_IRQ : no
alrm_pending : no
update IRQ enabled : no
periodic IRQ enabled : no
periodic IRQ frequency : 1
max user IRQ frequency : 64
24hr : yes
PROCFS接口
cat /proc/driver/rtc
rtc_time : 09:50:05
rtc_date : 2024-10-21
alrm_time : 00:00:00
alrm_date : 1970-01-01
alarm_IRQ : no
alrm_pending : no
update IRQ enabled : no
periodic IRQ enabled : no
periodic IRQ frequency : 1
max user IRQ frequency : 64
24hr : yes
IOCTL接口
可以参考内核文档作为例子 kernel/tools/testing/selftests/timers/rtcpie.c
常用的命令
date //修改系统时钟,具体命令使用可以man下
hwclock -s //将硬件时间同步到系统时间
hwclock -w //将系统时间同步到硬件时间
timedatectl //显示系统时间等
#以下手动设置时间或者网络同步时间后,-w将系统时间写入到硬件rtc,-s再将rtc时间写回系统,这样每次重启板卡都会进行rtc时间同步到系统时间。
sudo date -s "2025-02-14 08:00:00" //手动设置时间
sudo hwclock -w //系统时间同步到硬件rtc
sudo hwclock -s //硬件rtc同步到系统