嵌入式linux C语言 如何同步系统时钟到硬件时钟

来源:互联网 发布:可爱表情贴图软件 编辑:程序博客网 时间:2024/05/01 10:30


使用settimeofday设置时间后, 重启系统时间并没有设置成功是因为settimeofday,紧紧能设置系统时间并不能设置硬件时间,

开机时linux会从硬件的rtc的时钟芯片中获取一次硬件时间,然后以此为基础来运行系统时间。
settimeofday  相当于date shell命令,紧紧能设置系统时间。

如果想设置硬件时间,需要一个hwclock -w shell原理的函数来写硬件的rtc。

扒一扒 busybox的  hwclock.c源码(http://www.codeforge.cn/read/97203/hwclock.c__html)

可以知道这么抄写功能源码:

static void write_rtc(time_t t, int utc)                                                                                                                  {int rtc;struct tm tm;if (( rtc = open ( "/dev/rtc", O_WRONLY )) < 0 ){if (( rtc = open ( "/dev/misc/rtc", O_WRONLY )) < 0 )bb_perror_msg_and_die ( "Could not access RTC" );}tm = *( utc ? gmtime ( &t ) : localtime ( &t ));tm. tm_isdst = 0;if ( ioctl ( rtc, RTC_SET_TIME, &tm ) < 0 )bb_perror_msg_and_die ( "Could not set the RTC time" );close ( rtc );}




也可执行system系统调用。


0 0
原创粉丝点击