arm linux rtc 时间设置

date -s “2018-04-08 12:00:00”

hwclock -r

修改完时间后,记得使用hwclock –w将系统时间同步到硬件时间。

hwclock -w 将系统时钟写入硬件时钟(hwclock -r 可以用来查看硬件时钟)

at91_rtc f80480b0.rtc: setting system clock to 2012-01-01 00:04:47 UTC (1325376287)

date -s “2017-04-08 12:00:00”

hwclock –r 显示硬件时钟与日期
hwclock –s 将系统时钟调整为与目前的硬件时钟一致。
hwclock –w 将硬件时钟调整为与目前的系统时钟一致。

RTC - real time clock维护着系统的hardware时间,当linux启动时需要用RTC hardware时钟设置system 时间。

这个过程是在drivers/rtc/hctosys.c驱动中实现的,这个驱动实际只有一个init函数,并且把自己的init 函数声明为

late_initcall,这样可以保证RTC驱动已经正常运转。

init函数从RTC设备读取当前硬件时钟,然后调用do_settimeofday改写系统时钟。

 static int __init rtc_hctosys(void)

/home/xxx/ArmProject/multi-serial/kernel/SAMA_XIN2/drivers/rtc/hctosys.c

 * published by the Free Software Foundation.
*/

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include 

/* IMPORTANT: the RTC only stores whole seconds. It is arbitrary
 * whether it stores the most close value or the value with partial
 * seconds truncated. However, it is important that we use it to store
 * the truncated value. This is because otherwise it is necessary,
 * in an rtc sync function, to read both xtime.tv_sec and
 * xtime.tv_nsec. On some processors (i.e. ARM), an atomic read
 * of >32bits is not possible. So storing the most close value would
 * slow down the sync API. So here we have the truncated value and
 * the best guess is to add 0.5s.
 */

static int __init rtc_hctosys(void)
{
        int err = -ENODEV;
        struct rtc_time tm;
        struct timespec64 tv64 = {
                .tv_nsec = NSEC_PER_SEC >> 1,
        };
        struct rtc_device *rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);

        if (rtc == NULL) {
                pr_info("unable to open rtc device (%s)\n",
                        CONFIG_RTC_HCTOSYS_DEVICE);
                goto err_open;
        }

        err = rtc_read_time(rtc, &tm);
        if (err) {
                dev_err(rtc->dev.parent,
                        "hctosys: unable to read the hardware clock\n");
                goto err_read;

        }

        tv64.tv_sec = rtc_tm_to_time64(&tm);

        err = do_settimeofday64(&tv64);

        dev_info(rtc->dev.parent,
                "setting system clock to "
                "%d-%02d-%02d %02d:%02d:%02d UTC (%lld)\n",
                tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
                tm.tm_hour, tm.tm_min, tm.tm_sec,
                (long long) tv64.tv_sec);

err_read:
        rtc_class_close(rtc);

err_open:
        rtc_hctosys_ret = err;

        return err;
}

late_initcall(rtc_hctosys);
                                     

#192.168.1.151
rdate time.nist.gov

mkdir -p /var/spool/cron/crontabs
crontab /etc/crontab
crond

正确的设置方法:

使用ntpdate校正linux系统的时间

你可能感兴趣的:(linux_arm)