RTC的使用

2024-5-31 19:00| 发布者: Espoir| 查看: 679| 评论: 0

类目:  >  知识库     文档编号: 1425

1.RTC简介

1.1 RTC是什么

RTC (Real Time Clock):实时时钟,是指可以像时钟一样输出实际时间的电子设备。简单来说,RTC就是个32位定时器,每隔1秒,定时器记一次数。修改定时器的值可以修改时间和日期。

为了保证RTC时间断电不丢失,一般还会单独接一个小电池给RTC供电。一旦小电池被取下来,断电之后,RTC的时间会丢失。

但是,电脑右下角的时间、linux下用date看到的时间,都不是RTC时间。系统运行时,会将芯片上的一个高精度寄存器作为系统时间基准。为了避免系统运行时间比较长以后出现偏移,以及掉电之后系统时间丢失的情况,每次启动系统会从RTC获取时间,作为系统时间。

1.2 内部RTC和外部RTC的区别

相较于内部RTC,外部RTC的主要优势有:

  • 功耗低

  • 误差小

所以一般采用外部RTC。

2.unix时间戳

  • Unix 时间戳(Unix Timestamp)定义为从UTC/GMT的1970年1月1日0时0分0秒开始所经过的秒数,不考虑闰秒

  • 时间戳存储在一个秒计数器中,秒计数器为32位/64位的整型变量

  • 世界上所有时区的秒计数器相同,不同时区通过添加偏移来得到当地时间

3.常用操作

3.1 date

date命令可以读/写当前系统时间,也可以查看时区。常用的几种操作:

查看时间:

修改时区之前
root@fl-imx6ull:~# date
Thu Sep 21 04:43:03 UTC 2017
​
修改时区之后
root@fl-imx6ull:~# date  
Thu Sep 21 12:43:50 HKT 2017

查看时区:

修改时区之前
root@fl-imx6ull:~# date -R
Fri, 26 Apr 2019 19:07:20 +0000
​
修改时区之后
root@fl-imx6ull:~# date -R
Thu, 21 Sep 2017 12:45:37 +0800

设置时间:

root@fl-imx6ull:~# date -s "20240531 12:00:00"
Fri May 31 12:00:00 UTC 2024

3.2 修改时区

时区文件为/etc/localtime,当需要修改时区时,只需要将对应的时区文件软链接到/etc/localtime。

root@fl-imx6ull:~# rm /etc/localtime 
root@fl-imx6ull:~# ln -s /usr/share/zoneinfo/Asia/Hong_Kong /etc/localtime

3.3 hwclock

读取rtc时间

root@fl-imx6ull:~# hwclock -r
Thu Sep 21 04:47:03 2017 0.000000 seconds

写入时间

hwclock -w
​
root@fl-imx6ull:~# date
Thu Sep 21 12:50:41 HKT 2017
root@fl-imx6ull:~# hwclock -w
root@fl-imx6ull:~# hwclock -r
Thu Sep 21 12:51:36 2017 0.000000 seconds
​
hwclock -wu
​
root@fl-imx6ull:~# date
Thu Sep 21 12:52:51 HKT 2017
root@fl-imx6ull:~# hwclock -wu
root@fl-imx6ull:~# hwclock -r
Thu Sep 21 04:52:56 2017 0.000000 seconds

注意:如果开启了UTC,并且修改了时区,应该使用hwclock -wu

系统时间同步到RTC

hwclock --systohc

RTC时间同步到系统

hwclock --hctosys

4.系统同步RTC

前文提到,系统启动时,会将RTC时间读出来,作为系统时间。以imx6ull为例,上电之后,关于RTC的操作如下:

在/etc/rcS.d/S55bootmisc.sh 当中,有以下内容

#
# This is as good a place as any for a sanity check
#  
# Set the system clock from hardware clock                    
# If the timestamp is more recent than the current time,
# use the timestamp instead.
test -x /etc/init.d/hwclock.sh && /etc/init.d/hwclock.sh start
if test -e /etc/timestamp
then                                    
      SYSTEMDATE=`date -u +%4Y%2m%2d%2H%2M%2S`
      read TIMESTAMP < /etc/timestamp
      if [ ${TIMESTAMP} -gt $SYSTEMDATE ]; then
              # format the timestamp as date expects it (2m2d2H2M4Y.2S)
              TS_YR=${TIMESTAMP%??????????}  
              TS_SEC=${TIMESTAMP#????????????}
              TS_FIRST12=${TIMESTAMP%??}
              TS_MIDDLE8=${TS_FIRST12#????}
              date -u ${TS_MIDDLE8}${TS_YR}.${TS_SEC}
              test -x /etc/init.d/hwclock.sh && /etc/init.d/hwclock.sh stop
      fi                    
fi    

这部分内容的主要作用是:

  • 调用/etc/init.d/hwclock.sh脚本,参数为start

  • 读取时间戳/etc/timestamp,和现有的系统时间进行对比,如果时间戳时间更靠后,则将时间戳当中的时间通过date写入系统,然后再次调用/etc/init.d/hwclock.sh,参数为stop;

/etc/init.d/hwclock.sh内容如下,篇幅原因,只保留部分内容

......
[ "$UTC" = "yes" ] && tz="--utc" || tz="--localtime"
case "$1" in
      start)
......
if [ "$HWCLOCKACCESS" != no ]
then
if [ -z "$TZ" ]
then
                  hwclock $tz --hctosys

已解决

未解决

只是看看

最新评论