嵌入式linux应用层对硬件时间的设…

设备文件: /dev/rtc或/dev/rtc0

打开函数:

int FAST_FUNC rtc_xopen(const char **default_rtc, int flags)
{
 int rtc;

 if (!*default_rtc) {
  *default_rtc = "/dev/rtc";
  rtc = open(*default_rtc, flags);
  if (rtc >= 0)
   return rtc;
  *default_rtc = "/dev/rtc0";
  rtc = open(*default_rtc, flags);
  if (rtc >= 0)
   return rtc;
  *default_rtc = "/dev/misc/rtc";
 }

 return xopen(*default_rtc, flags);
}

 

调用示例:

 int rtc;

 rtc = rtc_xopen(pp_rtcname, O_WRONLY);

 

命令方法:

struct tm tm_time;

xioctl(rtc, RTC_SET_TIME, &tm_time);//设置硬件时间

 

xioctl(rtc, RTC_RD_TIME, &tm_time);//读取硬件时间到tm_time

 

示例:

 

static void from_sys_clock(const char **pp_rtcname, int utc)
{

 struct timeval tv;
 struct tm tm_time;
 int rtc;

 rtc = rtc_xopen(pp_rtcname, O_WRONLY);
 gettimeofday(&tv, NULL);
 
 if (sizeof(time_t) == sizeof(tv.tv_sec)) {
  if (utc)
   gmtime_r((time_t*)&tv.tv_sec, &tm_time);
  else
   localtime_r((time_t*)&tv.tv_sec, &tm_time);
 } else {
  time_t t = tv.tv_sec;
  if (utc)
   gmtime_r(&t, &tm_time);
  else
   localtime_r(&t, &tm_time);
 }

 

 tm_time.tm_isdst = 0;
 xioctl(rtc, RTC_SET_TIME, &tm_time);

 if (ENABLE_FEATURE_CLEAN_UP)
  close(rtc);
}

你可能感兴趣的:(嵌入式开发)