开发板RTC时间设置有效,但断电后自动恢复

博主使用的是天嵌的开发板型号IMX6Q_coreC。

1.发现使用开发板是时间总是被设置为2028年6月18日3时41分,一开始以为是底层驱动的问题, 故去底层加打印信息调试。

2.发现在系统内hwclock -w也正常写入, 从寄存器读取时间也正确,就是断电后又被设置成这个时间。

3.还有一个现象就是如果设置的时间大于这个时间,断电后可以读取到正常的时间,小于这个时间就会被置为2028年6月18日3时41分。

一、于是先设置一个时间再断电重启去抓这个RTC芯片的信息:

    就先随便设置一个日期:20180419

        

    看到打印信息读写寄存器都正常。现在断电重启再抓这个芯片的信息:

    

    可以看出一开始读取并设置的时间是正确的, 可是后面又被设置成了2028年的日期?是跑到系统里面才做的操作?跟底层无关?

    于是去系统里面搜索关键字2028:

     

二、发现/etc/timestamp的文件内容时间正好是2028年6月18日3时41分????

    再追,包含/etc/timestamp这个文件了有两个脚本分别是/etc/init.d目录下的bootmisc.sh和save-rtc.sh。

    bootmisc.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/timestamp存在与否,且将里面的时间与当前系统时间进行比较,如果timestamp里面的时间大于系统时间,则会使用timestamp,并将其写入RTC时间中。
    
     脚本save-rtc.sh中有如下内容:
    #! /bin/sh
    /etc/init.d/hwclock.sh stop
 
    # Update the timestamp
    date +%2m%2d%2H%2M%Y > /etc/timestamp
       上面的脚本会将当前时间写入timestamp中。
       同时,bootmis.sh脚本被链接到rcS.d中的S55bootmis.sh中,在系统启动时执行,而脚本save-rtc.sh则被链接到rc0.d和rc6.d文  件夹中,在系统关闭和重启时执行。
三、解决方法:
        找的了原因,解决的方法就简单了
        1. 删除文件 /etc/timestamp。或者修改/etc/timestamp内的时间,使其小于或等于当前系统时间。
        2. 正常关闭或重启系统而不是直接断电。
        3. 修改 bootmisc.sh脚本里面的判断条件

                

猜你喜欢

转载自blog.csdn.net/a617996505/article/details/80004095
今日推荐