arm linux rtc 时间设置

本文介绍如何在Linux系统中使用hwclock命令进行硬件时钟与系统时钟的同步,包括具体的命令操作方法,以及如何通过ntpdate命令校正系统时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 <linux/rtc.h>

/* 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系统的时间

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值