10. RTC

RTC全称为Real Time Clock,是一个专门用来记录时间的硬件设备,一般可以集成在soc内部,或者选择外挂,通过i2c与其通信。

那为什么会需要RTC,因为Linux的系统时间(也就是我们常说的wall time)只能在系统运行时使用,系统关机时间就丢了,而RTC可以在系统关闭后,依靠外部电池或其他supply继续工作,这才将时间保存下来。

LubanCat-RK系列板卡需要RTC电池才能开启RTC的功能,RTC电池的选型如下:

注解

链接仅供参考,如需要,请自行根据上述链接的关键词搜索购买

接线的正负极如下:

未找到该图片

如果线序不对,可以使用镊子调整RTC电池的接口:

未找到该图片

10.1. 使用方法

Linux 提供了三种用户空间调用接口。在板卡中对应的路径为:

  • SYSFS接口:/sys/class/rtc/rtc0/

  • PROCFS接口: /proc/driver/rtc

  • IOCTL接口: /dev/rtc0

SYSFS接口:

1
2
3
4
root@lubancat:~# cat /sys/class/rtc/rtc0/date
2023-06-19
root@lubancat:~# cat /sys/class/rtc/rtc0/time
03:15:22

PROCFS接口:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
root@lubancat:~# cat /proc/driver/rtc
rtc_time        : 07:06:56
rtc_date        : 2023-06-19
alrm_time       : 03:18:50
alrm_date       : 2023-06-19
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
root@lubancat:~#

IOCTL接口:

1
2
可以参考内核文档作为例子
tools/testing/selftests/timers/rtctest.c

10.2. 一些常用的命令

1
2
3
4
5
6
7
8
9
date          //修改系统时钟,具体命令使用可以man下
hwclock -s    //将硬件时间同步到系统时间
hwclock -w    //将系统时间同步到硬件时间
timedatectl   //显示系统时间等

#以下手动设置时间或者网络同步时间后,-w将系统时间写入到硬件rtc,-s再将rtc时间写回系统,这样每次重启板卡都会进行rtc时间同步到系统时间。
sudo date -s "2024-08-14 08:00:00"  //手动设置时间
sudo  hwclock -w     //系统时间同步到硬件rtc
sudo  hwclock -s    //硬件rtc同步到系统