嵌入式Linux:不使用NTP设置时间

jpsalm

在使用yocto构建的嵌入式Linux设备上工作,该设备正在通过uart从具有卫星连接的另一台设备上获取日期。在我们可以访问此日期之前,我们已经完全进入了一个完全初始化的多线程应用程序以及各种正在运行的守护程序等。

据我所读,使用stime()settimeofday()可能会导致破坏任何依赖计时器的东西。我猜想像std :: this_thread :: sleep_for()这样的东西会包含在其中吗?

此时是否有一种安全的方法来设置系统时间,或者大多数没有Internet访问权限的嵌入式Linux设备如何解决此问题?

编辑:我们确实有一个RTC,但是我们正在使用的hwclock的实现不允许我们直接写,至少是通过hwclock api。这是--help

BusyBox v1.24.1 (2018-11-14 12:40:41 PST) multi-call binary. 
Usage: hwclock [-r|--show] [-s|--hctosys] [-w|--systohc] [-t|--systz] [-l|--localtime] [-u|--utc] [-f|--rtc FILE]

链接到hwclock源。

jpsalm

我的解决方案是使用ioctrl直接写入rtc。这是遇到此问题的任何人的实现:

#include <linux/rtc.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>


int main(void)
{
  int fd;
  fd = open("/dev/rtc0",0);
  if (fd < 0)
    printf("Can't open rtc!");

  struct rtc_time time;

  time.tm_sec = 12;
  time.tm_min = 12;
  time.tm_hour = 7;
  time.tm_mday = 12;
  time.tm_mon = 7;
  time.tm_year = 118;

  if (ioctl(fd, RTC_SET_TIME, &time) < 0 )
    printf("Set rtc failed!");

  return 0;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章