http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=3637782

Linux的RTC驱动相对还是比较简单的,可以将它作为一个普通的字符型设备,或者一个misc设备,也可以是一个平台设备,这都没有关系,主要还是对rtc_ops这个文件操作结构体中的成员填充,这里主要涉及到两个方面比较重要:

1. 在Linux中有硬件时钟与系统时钟等两种时钟。硬件时钟是指主机板上的时钟设备,也就是通常可在BIOS画面设定的时钟。系统时钟则是指kernel中的时钟。当Linux启动时,系统时钟会去读取硬件时钟的设定,之后系统时钟即独立运作。所有Linux相关指令与函数都是读取系统时钟的设定。

系统时钟的设定就是我们常用的date命令,而我们写的RTC驱动就是为硬件时钟服务的,它有属于自己的命令hwclock,因此使用date命令是不可能调用到我们的驱动的(在这点上开始把我郁闷到了,写完驱动之后,傻傻的用date指令来测试,当然结果是什么都没有),我们可以通过hwclock的一些指令来实现更新rtc时钟——也就是系统时钟和硬件时钟的交互。

hwclock –r         显示硬件时钟与日期

hwclock –s         将系统时钟调整为与目前的硬件时钟一致。

hwclock –w        将硬件时钟调整为与目前的系统时钟一致。

2. 第二点就是内核空间和用户空间的交互,在系统启动结束,我们实际是处在用户态,因此我们使用指令输入的内容也是在用户态,而我们的驱动是在内核态的,内核态和用户态是互相不可见的,因此我们需要特殊的函数来实现这两种形态的交互,这就是以下两个函数:

copy_from_user(从用户态到内核态)

copy_to_user   (从内核态到用户态)

当然这两个函数需要我们在内核驱动中实现。

RTC最基本的两个命令就是设置时间,读取时间。

设置时间——设置时间会调用系统默认的RTC_SET_TIME,很显然就是处在用户态的用户将自己所要设置的时间信息传递给内核态,

case RTC_SET_TIME:

{

struct rtc_time rtc_tm;

if (copy_from_user(&rtc_tm, (struct rtc_time*)arg, sizeof(struct rtc_time)))

return -EFAULT;

sep4020_rtc_settime(&rtc_tm);//把用户态得到的信息传递给设置时间这个函数

return 0;

}

读取时间——设置时间会调用系统默认的RTC_RD_TIME,很显然就是需要通过内核态的驱动将芯片时钟取出,并传递给用户态

case RTC_RD_TIME: /* Read the time/date from RTC */

{

sep4020_rtc_gettime(&septime);//通过驱动的读函数读取芯片时钟

copy_to_user((void *)arg, &septime, sizeof septime);//传递给用户态

}

--------------------------------------------------------------------------------------------------------------------

首先搞清楚RTC在kernel内的作用:

linux系统有两个时钟:一个是由主板电池驱动的“Real Time Clock”也叫做RTC或者叫CMOS时钟,
硬件时钟。当操作系统关机的时候,用这个来记录时间,但是对于运行的系统是不用这个时间的。
另一个时间是 “System clock”也叫内核时钟或者软件时钟,是由软件根据时间中断来进行计数的,
内核时钟在系统关机的情况下是不存在的,所以,当操作系统启动的时候,内核时钟是要读取RTC时间
来进行时间同步。并且在系统关机的时候将系统时间写回RTC中进行同步。

如前所述,Linux内核与RTC进行互操作的时机只有两个:
1) 内核在启动时从RTC中读取启动时的时间与日期;
2) 内核在需要时将时间与日期回写到RTC中。

系统启动时,内核通过读取RTC来初始化内核时钟,又叫墙上时间,该时间放在xtime变量中。
The current time of day (the wall time) is defined in kernel/timer.c:
struct timespec xtime;
The timespec data structure is defined in  as:
struct timespec {
        time_t tv_sec;               /* seconds */
        long tv_nsec;                /* nanoseconds */
};
问题1:系统启动时在哪读取RTC的值并设置内核时钟进行时间同步的呢?
最有可能读取RTC设置内核时钟的位置应该在arch/arm/kernel/time.c里的time_init函数内.
time.c为系统的时钟驱动部分.time_init函数会在系统初始化时,由init/main.c里的start_kernel函数内调用.X86架构就是在这里读RTC值并初始化系统时钟xtime的.

ARM架构的time_init代码如下:
/* arch/arm/kernel/time.c */
void __init time_init(void)
{
if (system_timer->offset == NULL)
  system_timer->offset = dummy_gettimeoffset;
system_timer->init();
#ifdef CONFIG_NO_IDLE_HZ
if (system_timer->dyn_tick)
  system_timer->dyn_tick->lock = SPIN_LOCK_UNLOCKED;
#endif
}

上 面system_timer->init()实际执行的是时钟驱动体系架构相关(具体平台)部分定义的init函数,若是s3c2410平台,则执 行的为arch/arm/mach-s3c2410/time.c里定义的s3c2410_timer_init函数.不过 s3c2410_timer_init()也没有读RTC的代码.整个时钟驱动初始化的过程大致就执行这些代码.
既然在系统时钟驱动初始化的过程中没有读RTC值并设置内核时钟,那会在哪设置呢?

我搜了一下,发现内核好象只有在arch/cris/kernel/time.c里有RTC相关代码,如下: 
/* arch/cris/kernel/time.c */
/* grab the time from the RTC chip */
//读RTC的函数
unsigned long get_cmos_time(void)
{
unsigned int year, mon, day, hour, min, sec;
sec = CMOS_READ(RTC_SECONDS);
min = CMOS_READ(RTC_MINUTES);
hour = CMOS_READ(RTC_HOURS);
day = CMOS_READ(RTC_DAY_OF_MONTH);
mon = CMOS_READ(RTC_MONTH);
…………
return mktime(year, mon, day, hour, min, sec);
}

这个函数会在update_xtime_from_cmos内被调用:
void update_xtime_from_cmos(void)
{
if(have_rtc) {
  xtime.tv_sec = get_cmos_time();
  xtime.tv_nsec = 0;
}
}

另外还有设置rtc的函数
int set_rtc_mmss(unsigned long nowtime); /* write time into RTC chip */

不过我加了printk测试了一下,好象arch/cris/kernel/time.c这个文件和这两个函数只是适用与X86?
ARM平台启动时并不走这边.因此执行不到这些函数。
那ARM平台启动时,系统是在哪读RTC的值并对内核时钟(WallTime)进行初始化的呢?

已解决:
嵌入式Linux内核(ARM)是在系统启动时执行/etc/init.d/hwclock.sh脚本,这个脚本会调用hwclock小程序读取RTC的值并设置系统时钟。
(换句话说,这要取决于你制作的文件系统里是否有这样的脚本)
/* /etc/init.d/hwclock.sh */
DAEMON1=/sbin/hwclock
start() {
    local RET ERROR=
    [ ! -f /etc/adjtime ] &&  echo "0.0 0 0.0" > /etc/adjtime
    log_status_msg "Setting the System Clock using the Hardware Clock as reference..." -n
    # Copies Hardware Clock time to System Clock using the correct
    # timezone for hardware clocks in local time, and sets kernel
    # timezone. DO NOT REMOVE.
    [ "$HWCLOCKACCESS" != no ] && $DAEMON1 --hctosys $GMT $BADYEAR
    #
    # Now that /usr/share/zoneinfo should be available,
    # announce the local time.
    #
    log_status_msg "System Clock set. Local time: `date`"
    log_status_msg ""
    return 0
}
hwclock最先读取的设备文件是 /dev/rtc  ,busybox里面的hwclock是这样实现的:
static int xopen_rtc(int flags)
{
int rtc;
if (!rtcname) {
  rtc = open("/dev/rtc", flags);
  if (rtc >= 0)
   return rtc;
  rtc = open("/dev/rtc0", flags);
  if (rtc >= 0)
   return rtc;
  rtcname = "/dev/misc/rtc";
}
return xopen(rtcname, flags);
}

2. 内核如何更新RTC时钟?
通过set_rtc函数指针指向的函数,set_rtc在arch/arm/kernel/time.c内
/* arch/arm/kernel/time.c */
/*
* hook for setting the RTC's idea of the current time.
*/
int (*set_rtc)(void);
但是set_rtc函数指针在哪初始化的呢?set_rtc应该是和RTC驱动相关的函数.
搜索kernel源码后发现,好象内核其他地方并没有对其初始化。待解决!
set_rtc在do_set_rtc内调用
static inline void do_set_rtc(void)
{
……
if (set_rtc())
  /*
   * rtc update failed.  Try again in 60s
   */
  next_rtc_update = xtime.tv_sec + 60;
else
  next_rtc_update = xtime.tv_sec + 660; /* update every ~11 minutes by default*/
}

do_set_rtc在timer_tick里调用
/*
* Kernel system timer support. 
*/
void timer_tick(struct pt_regs *regs)
{
profile_tick(CPU_PROFILING, regs);
do_leds();
do_set_rtc();
do_timer(1);
……
}
timer_tick为Kernel提供的体系架构无关的时钟中断处理函数,通常会在体系架构相关的时钟中断处理函数内调用它。如s3c2410是这样的:
在arch/arm/mach-s3c2410/time.c中
* IRQ handler for the timer
*/
static irqreturn_t
s3c2410_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
write_seqlock(&xtime_lock);
timer_tick(regs);
write_sequnlock(&xtime_lock);
return IRQ_HANDLED;
}

*nix 下 timer机制 标准实现,一般是用 sigalarm + setitimer() 来实现的,但这样就与 select/epoll 等逻辑有所冲突,我希望所有 event 的通知逻辑都从 select/epoll 中触发。(FreeBSD 中 kqueue 默认就有 FILTER_TIMER,多好)

ps. /dev/rtc 只能被 open() 一次,因此上面希望与 epoll 合并的想法基本不可能了~

下面是通过 /dev/rtc (real-time clock) 硬件时钟实现的 timer机制。:-)
其中 ioctl(fd, RTC_IRQP_SET, 4) 的第三个参数只能是 2, 4, 8, 16, 32 之一,表示 xx Hz。

-------------------------------------------------
#include <linux/rtc.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <err.h>

int main(void)
{
        unsigned long i = 0;
        unsigned long data = 0;
        int fd = open("/dev/rtc", O_RDONLY);

if ( fd < 0 )
                errx(1, "open() fail");

/* set the freq as 4Hz */
        if ( ioctl(fd, RTC_IRQP_SET, 4) < 0 )
                errx(1, "ioctl(RTC_IRQP_SET) fail");

/* enable periodic interrupts */
        if ( ioctl(fd, RTC_PIE_ON, 0) < 0 )
                errx(1, "ioctl(RTC_PIE_ON)");

for ( i = 0; i < 100; i++ )
        {
                if ( read(fd, &data, sizeof(data)) < 0 )
                        errx(1, "read() error");

printf("timer %d\n", time(NULL));
        }

/* enable periodic interrupts */
        if ( ioctl(fd, RTC_PIE_OFF, 0) < 0 )
                errx(1, "ioctl(RTC_PIE_OFF)");

close(fd);
        return 0;
}

--------------------------------------------------------------------------------------------------------------------

User mode test code:

#include <stdio.h>
#include <stdlib.h>
#include <linux/rtc.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

int main(void)
{
  int i, fd, retval, irqcount = 0;
  unsigned long tmp, data;
  struct rtc_time rtc_tm;
 
  fd = open ("/dev/rtc", O_RDONLY);
  if (fd == -1) {
     perror("/dev/rtc");
     exit(1);
  }

// Alarm example,10 mintues later alarm
  
  /* Read the RTC time/date */
  retval = ioctl(fd, RTC_RD_TIME, &rtc_tm);
  if (retval == -1) {
     perror("ioctl");
     exit(1);
  }
  fprintf(stderr, "Current RTC date/time is %d-%d-%d,%02d:%02d:%02d.\n", 
     rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year + 1900,
      rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
      
  // Setting alarm time
  rtc_tm.tm_min += 10;
  if (rtc_tm.tm_sec >= 60) {
     rtc_tm.tm_sec %= 60;
     rtc_tm.tm_min++;
  }
  if (rtc_tm.tm_min == 60) {
     rtc_tm.tm_min = 0;
     rtc_tm.tm_hour++;
  }
  if (rtc_tm.tm_hour == 24)
     rtc_tm.tm_hour = 0;
     
  // setting
  retval = ioctl(fd, RTC_ALM_SET, &rtc_tm);
  if (retval == -1) {
     perror("ioctl");
     exit(1);
  }

/* Read the current alarm settings */
  retval = ioctl(fd, RTC_ALM_READ, &rtc_tm);
  if (retval == -1) {
     perror("ioctl");
     exit(1);
  }
  fprintf(stderr, "Alarm time now set to %02d:%02d:%02d.\n",
      rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);

/* Enable alarm interrupts after setting*/
  retval = ioctl(fd, RTC_AIE_ON, 0);
  if (retval == -1) {
     perror("ioctl");
     exit(1);
  }

/* This blocks until the alarm ring causes an interrupt */
  retval = read(fd, &data, sizeof(unsigned long));
  if (retval == -1) {
     perror("read");
     exit(1);
  }
  irqcount++;
  fprintf(stderr, " okay. Alarm rang.\n");
}

------------------------------------------------------------------------------------------------------------------------------------------------

S3C2410 RTC(Real Time Clock)简介

实时时钟(RTC)单元可以在系统电源关半闭的情况下依靠备用电池工作。RTC可以通过使用STRB/LDDRB这两个ARM指令向CPU传递8位数据(BCD码)。数据包括秒、分、小时、日期、天、月、和年。RTC单元依靠一个外部的32.768kHZ的石晶,也可以执行报警功能。

特性

  • BCD码:秒、分、时、日期、天、月和年

  • 润年产生器

  • 报警功能:报警中断,或者从power-off状态唤醒。

  • 移除了2000年的问题

  • 独立的电源引角:RTCVDD

  • 为RTOS内核时间Tick time支持毫秒Tick time中断。

  • Round reset 功能。

RTC在power-off模式或者正常操作模式时可以在一指定的时间产生一个报警信号。在正常操作模式下,报警中断(ALMINT)被激活,在power-off模式下,电源管理唤醒信号(PMWKUP)和ALMINT一起被激活。RTC报警寄存器(RTCALM)决定报警的enable/disable状态和报警时间设定的条件。

RTC TICK TIME被用于中断请求。TICNT寄存器有一个中断使能位和中断的计数值。当计数值到达0时TICK TIME中断。所以中断的周期如下:

周期= (n+1 ) /128 秒

n:Tick time计数值(1~127)

这个RTC time tick可以被用于实时操作系统(RTOS)内核 time tick。如果time tick通过RTC time tick产生,那么RTOS的时间相关的功能就需要总是与实时时间同步。

ROUND RESET 功能

Rund reset功能可以通过RTC round reset寄存器(RTCRST)来执行。 The round boundary (30, 40, or 50 sec.) of the second carry generation can be selected, and the second value is rounded to zero in the round reset. For example, when the current time is 23:37:47 and the round boundary is selected to 40 sec, the round reset changes the current time to 23:38:00.

NOTE

All RTC registers have to be accessed for each byte unit using the STRB and LDRB instructions or char type pointer.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

在.../drivers/rtc/Makefile中与我们有关的项有

obj-$(CONFIG_RTC_LIB) += rtc-lib.o

obj-$(CONFIG_RTC_HCTOSYS) += hctosys.o

obj-$(CONFIG_RTC_CLASS) += rtc-core.o

rtc-core-y := class.o interface.o

rtc-core-$(CONFIG_RTC_INTF_DEV) += rtc-dev.o

rtc-core-$(CONFIG_RTC_INTF_PROC) += rtc-proc.o

rtc-core-$(CONFIG_RTC_INTF_SYSFS) += rtc-sysfs.o

obj-$(CONFIG_RTC_DRV_S3C) += rtc-s3c.o

其中 rtc-lib.c :提供了一些时间格式相互转化的函数。 hctosys.c:在启动时初始化系统时间。 RTC核心文件: class.c interface.c rtc-dev.c:字符设备的注册和用户层文件操作函数接口。 rtc-proc.c rtc-sysfs.c rtc-s3c.o:S3C2410 RTC的芯片平台驱动。////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

4> 在根文件系统的 做的动作, 把 pc linux上的 /etc/localtime 复制到 板子的 /etc/下面即可

5> mknod /dev/rtc c 254 0

下面的动作只需做一次 ,一旦写入RTC chip后, chip就自己计时了,除非电池没电了。

板子第一次启动后,

假如设置系统时间为2007年10月2日,13:49分,可以这样设置

1> date 100213492007

2> hwclock –w

如果没有出错, 就已经把2007年10月2日,13:49分 写入RTC chip了,

测试:

反复执行hwclock ,看看是否时间在变化。

3> 重启板子, 测试, 执行hwclock ,看看时间是否在流逝 。

Linux系统时间与RTC时间【转】的更多相关文章

  1. 时间篇之linux系统时间和RTC时间

    一.linux系统下包含两个时间:系统时间(刚启动时读取的是rtc时间)和RTC时间. 一般情况下都会选择芯片上最高精度的定时器作为系统时间的定时基准,以避免在系统运行较长时间后出现大的时间偏移.特点 ...

  2. Linux入门教程:如何检查Linux系统的最后重启时间

    问题: 是否有一个命令可以快速地检查系统已经运行了多久? 也就是我怎么知道Linux系统最后的重启时间? 有许多方法来查询系统最后的重启时间. 方法一 第一种方法是使用last命令. $ last r ...

  3. Linux系统的时区和时间调整

     linux调整系统时区: 1)tzselect命令 找到相应的时区文件/usr/share/zoneinfo/Asia/Shanghai,用这个文件替换当前的/etc/localtime文件. 或者 ...

  4. Linux系统下的单调时间函数

    欢迎转载,转载请注明出处:http://forever.blog.chinaunix.net 一.编写linux下应用程序的时候,有时候会用到高精度相对时间的概念,比如间隔100ms.那么应该使用哪个 ...

  5. 修改linux系统时间、rtc时间以及时间同步

    修改linux的系统时间用date -s [MMDDhhmm[[CC]YY][.ss]] 但是系统重启就会从新和硬件时钟同步. 要想永久修改系统时间,就需要如下命令:hwclock hwclock - ...

  6. linux 系统文件类型、系统安装时间、系统启动时间、系统运行时间、设置及显示时间、系统时间和硬件时间

    系统文件类型: 1) $mout 2) df -l:仅列出本地文件系统:-h (--human-readable):-T:文件系统类型 $df -lhf 3) file -s (--special-f ...

  7. Linux的硬件时间、校正Linux系统时间及系统时间调用流程

    第一部分: 一)概述: 事实上在Linux中有两个时钟系统,分别是系统时间和硬件时间 UTC是协调世界时(Universal Time Coordinated)英文缩写,它比北京时间早8个小时.   ...

  8. Linux下RTC时间的读写分析【转】

    本文转载自:http://blog.csdn.net/little_walt/article/details/52880840 Linux系统下包含两个时间:系统时间和RTC时间. 系统时间:是由主芯 ...

  9. 【linux 命令】:查看系统开机,关机时间【转载】

    转载原文:http://www.cnblogs.com/kerrycode/p/3759395.html 看Linux开机关机时间的方法(非常全面) 1: who 命令查看 who -b 查看最后一次 ...

随机推荐

  1. 2014_acmicpc_shanghai_google

    I http://acm.hust.edu.cn/vjudge/contest/view.action?cid=84975#problem/I 题意:我方有n个士兵,敌方有m个士兵,每个士兵有攻击力和 ...

  2. /MT /MD /ML /MTd /MDd /MLd 的区别

    Multithreaded Libraries Performance The single-threaded CRT is no longer ( in vs2005 ) available. Th ...

  3. [工作积累] Google Play Game SDK details

    https://developers.google.com/games/services/cpp/api/structgpg_1_1AndroidSupport For apps which targ ...

  4. js中常用数组方法concat join push pop slice splice shift

    javascript给我们很多常用的 数组方法,极大方便了我们做程序.下面我们来介绍下常用的集中数组方法. 比如 concat() join() push() pop() unshift() shif ...

  5. 功率单位mW 和 dBm 的换算

    无线电发射机输出的射频信号,通过馈线(电缆)输送到天线,由天线以电磁波形式辐射出去.电磁波到达接收地点后,由天线接收下来(仅仅接收很小很小一部分功率),并通过馈线送到无线电接收机.因此在无线网络的工程 ...

  6. TJU 4087. box

    题目:Tuhao and his two small partners participated in the tournament.But in the end, they lost the cha ...

  7. eclipse 自动 注释

    在使用Eclipse 编写Java代码时,自动生成的注释信息都是按照预先设置好的格式生成的. 修改作者.日期注释格式:打开Windows->Preferences->Java->Co ...

  8. Unity3D判断鼠标向右或向左滑动,响应不同的事件

    private var first = Vector2.zero; private var second = Vector2.zero; function Update () { } function ...

  9. Sqli-labs less 19

    Less-19 从源代码中我们可以看到我们获取到的是HTTP_REFERER 那和less18是基本一致的,我们从referer进行修改. 还是像less18一样,我们只给出一个示例 将referer ...

  10. Find the smallest number whose digits multiply to a given number n

    Given a number ‘n’, find the smallest number ‘p’ such that if we multiply all digits of ‘p’, we get ...