Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1297023
  • 博文数量: 548
  • 博客积分: 7597
  • 博客等级: 少将
  • 技术积分: 4224
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-15 13:21
个人简介

嵌入式软件工程师&&太极拳

文章分类

全部博文(548)

文章存档

2014年(10)

2013年(76)

2012年(175)

2011年(287)

分类: 嵌入式

2012-05-18 09:47:44


点击(此处)折叠或打开

  1. rtc在linux上的测试代码 .

  2. rtc应用很广泛,在PC机和嵌入式上面几乎都能看到。下面就用最简单的代码做一个演示。相应的分析请看linux源代码中的分析文档。代码如下:
  3. #include <stdio.h>
  4. #include <linux/rtc.h>
  5. #include <sys/ioctl.h>
  6. #include <sys/time.h>
  7. #include <sys/types.h>
  8. #include <fcntl.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include <errno.h>

  12. /*
  13.  * This expects the new RTC class driver framework, working with
  14.  * clocks that will often not be clones of what the PC-AT had.
  15.  * Use the command line to specify another RTC if you need one.
  16. */
  17. static const char default_rtc[] = "/dev/rtc";
  18. int main(int argc, char **argv)
  19. {
  20.     int i, fd, retval, irqcount = 0;
  21.     unsigned long tmp, data;
  22.     struct rtc_time rtc_tm;
  23.     const char *rtc = default_rtc;
  24.     switch (argc)
  25.     {
  26.         case 2:
  27.             rtc = argv[1];
  28.             /* FALLTHROUGH */
  29.         case 1:
  30.             break;
  31.         default:
  32.             fprintf(stderr, "usage: rtctest [rtcdev]/n");
  33.         return 1;
  34.     }
  35.     fd = open(rtc, O_RDONLY);
  36.     if (fd == -1)
  37.     {
  38.         perror(rtc);
  39.         exit(errno);
  40.     }
  41.     fprintf(stderr, "/n/t/t/tRTC Driver Test Example./n/n");
  42.     /* Turn on update interrupts (one per second) */
  43.     retval = ioctl(fd, RTC_UIE_ON, 0);
  44.     if (retval == -1)
  45.     {
  46.         if (errno == ENOTTY)
  47.         {
  48.             fprintf(stderr, "/n...Update IRQs not supported./n");
  49.             goto test_READ;
  50.         }
  51.         perror("RTC_UIE_ON ioctl");
  52.         exit(errno);
  53.     }
  54.     fprintf(stderr, "Counting 5 update (1/sec) interrupts from reading %s:",rtc);
  55.     fflush(stderr);
  56.     for (i=1; i<6; i++)
  57.     {
  58.         /* This read will block */
  59.         retval = read(fd, &data, sizeof(unsigned long));
  60.         if (retval == -1)
  61.         {
  62.             perror("read");
  63.             exit(errno);
  64.         }
  65.         fprintf(stderr, " %d",i);
  66.         fflush(stderr);
  67.         irqcount++;
  68.     }
  69.     fprintf(stderr, "/nAgain, from using select(2) on /dev/rtc:");
  70.     fflush(stderr);
  71.     for (i=1; i<6; i++)
  72.     {
  73.         struct timeval tv = {5, 0}; /* 5 second timeout on select */
  74.         fd_set readfds;
  75.         FD_ZERO(&readfds);
  76.         FD_SET(fd, &readfds);
  77.         /* The select will wait until an RTC interrupt happens. */
  78.         retval = select(fd+1, &readfds, NULL, NULL, &tv);
  79.         if (retval == -1)
  80.         {
  81.             perror("select");
  82.             exit(errno);
  83.         }
  84.         /* This read won't block unlike the select-less case above. */
  85.         retval = read(fd, &data, sizeof(unsigned long));
  86.         if (retval == -1)
  87.         {
  88.             perror("read");
  89.             exit(errno);
  90.         }
  91.         fprintf(stderr, " %d",i);
  92.         fflush(stderr);
  93.         irqcount++;
  94.     }
  95.     /* Turn off update interrupts */
  96.     retval = ioctl(fd, RTC_UIE_OFF, 0);
  97.     if (retval == -1)
  98.     {
  99.         perror("RTC_UIE_OFF ioctl");
  100.         exit(errno);
  101.     }
  102.     test_READ:
  103.     /* Read the RTC time/date */
  104.     retval = ioctl(fd, RTC_RD_TIME, &rtc_tm);
  105.     if (retval == -1)
  106.     {
  107.         perror("RTC_RD_TIME ioctl");
  108.         exit(errno);
  109.     }
  110.     fprintf(stderr, "/n/nCurrent RTC date/time is %d-%d-%d, %02d:%02d:%02d./n",rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year +

  111. 1900,rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
  112.     /* Set the alarm to 5 sec in the future, and check for rollover */
  113.     rtc_tm.tm_sec += 5;
  114.     if (rtc_tm.tm_sec >= 60)
  115.     {
  116.         rtc_tm.tm_sec %= 60;
  117.         rtc_tm.tm_min++;
  118.     }
  119.     if (rtc_tm.tm_min == 60)
  120.     {
  121.         rtc_tm.tm_min = 0;
  122.         rtc_tm.tm_hour++;
  123.     }
  124.     if (rtc_tm.tm_hour == 24)
  125.         rtc_tm.tm_hour = 0;
  126.     retval = ioctl(fd, RTC_ALM_SET, &rtc_tm);
  127.     if (retval == -1)
  128.     {
  129.         if (errno == ENOTTY)
  130.         {
  131.             fprintf(stderr,"/n...Alarm IRQs not supported./n");
  132.             goto test_PIE;
  133.         }
  134.         perror("RTC_ALM_SET ioctl");
  135.         exit(errno);
  136.     }
  137.     /* Read the current alarm settings */
  138.     retval = ioctl(fd, RTC_ALM_READ, &rtc_tm);
  139.     if (retval == -1)
  140.     {
  141.         perror("RTC_ALM_READ ioctl");
  142.         exit(errno);
  143.     }
  144.     fprintf(stderr, "Alarm time now set to %02d:%02d:%02d./n",rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
  145.     /* Enable alarm interrupts */
  146.     retval = ioctl(fd, RTC_AIE_ON, 0);
  147.     if (retval == -1)
  148.     {
  149.         perror("RTC_AIE_ON ioctl");
  150.         exit(errno);
  151.     }
  152.     fprintf(stderr, "Waiting 5 seconds for alarm...");
  153.     fflush(stderr);
  154.     /* This blocks until the alarm ring causes an interrupt */
  155.     retval = read(fd, &data, sizeof(unsigned long));
  156.     if (retval == -1)
  157.     {
  158.         perror("read");
  159.         exit(errno);
  160.     }
  161.     irqcount++;
  162.     fprintf(stderr, " okay. Alarm rang./n");
  163.     /* Disable alarm interrupts */
  164.     retval = ioctl(fd, RTC_AIE_OFF, 0);
  165.     if (retval == -1)
  166.     {
  167.         perror("RTC_AIE_OFF ioctl");
  168.         exit(errno);
  169.     }
  170.     test_PIE:
  171.     /* Read periodic IRQ rate */
  172.     retval = ioctl(fd, RTC_IRQP_READ, &tmp);
  173.     if (retval == -1)
  174.     {
  175.         /* not all RTCs support periodic IRQs */
  176.         if (errno == ENOTTY)
  177.         {
  178.             fprintf(stderr, "/nNo periodic IRQ support/n");
  179.             goto done;
  180.         }
  181.         perror("RTC_IRQP_READ ioctl");
  182.         exit(errno);
  183.     }
  184.     fprintf(stderr, "/nPeriodic IRQ rate is %ldHz./n", tmp);
  185.     fprintf(stderr, "Counting 20 interrupts at:");
  186.     fflush(stderr);
  187.     /* The frequencies 128Hz, 256Hz, ... 8192Hz are only allowed for root. */
  188.     for (tmp=2; tmp<=1024; tmp*=2)
  189.     {
  190.         retval = ioctl(fd, RTC_IRQP_SET, tmp);
  191.         if (retval == -1)
  192.         {
  193.             /* not all RTCs can change their periodic IRQ rate */
  194.             if (errno == ENOTTY)
  195.             {
  196.                 fprintf(stderr,"/n...Periodic IRQ rate is fixed/n");
  197.                 goto done;
  198.             }
  199.             perror("RTC_IRQP_SET ioctl");
  200.             exit(errno);
  201.         }
  202.         fprintf(stderr, "/n%ldHz:/t", tmp);
  203.         fflush(stderr);
  204.         /* Enable periodic interrupts */
  205.         retval = ioctl(fd, RTC_PIE_ON, 0);
  206.         if (retval == -1)
  207.         {
  208.             perror("RTC_PIE_ON ioctl");
  209.             exit(errno);
  210.         }
  211.         for (i=1; i<21; i++)
  212.         {
  213.             /* This blocks */
  214.             retval = read(fd, &data, sizeof(unsigned long));
  215.             if (retval == -1)
  216.             {
  217.                 perror("read");
  218.                 exit(errno);
  219.             }
  220.             fprintf(stderr, " %d",i);
  221.             fflush(stderr);
  222.             irqcount++;
  223.         }
  224.         /* Disable periodic interrupts */
  225.         retval = ioctl(fd, RTC_PIE_OFF, 0);
  226.         if (retval == -1)
  227.         {
  228.             perror("RTC_PIE_OFF ioctl");
  229.             exit(errno);
  230.         }
  231.     }
  232.     done:
  233.     fprintf(stderr, "/n/n/t/t/t *** Test complete ***/n");
  234.     close(fd);
  235.     return 0;
  236. }

  237. 这个是linux文档中的说明代码,希望能给你做RTC程序提供相应的思路。

 

2.6.24.3 smdk2440的rtc驱动测试

2.6.24.3 内核已经有支援RTC,只要选择就可以在Device Driver –>Real Time Clock -> 选中s3c rtc就可以。

主设备号应该是kernel动态分配的,我的板子上254,mknode一个/dev/rtc  

同时加入s3c_device_rtc:
static struct platform_device *smdk2440_devices[] __initdata = {
    &s3c_device_usb,
    &s3c_device_lcd,
    &s3c_device_wdt,
    &s3c_device_i2c,
    &s3c_device_iis,
    &s3c_device_sdi,   //creator added
    &s3c_device_rtc,   //add device。
};  
                                                                       
可以通过date设定时间                                                                                                                               

                                
假如配置系统时间为2008年8月9日,10:11分,能够这样配置                                                                                             

                              
1>     date 080910112008                                                                                                                           

                               
2>     hwclock –w      //写入了                                                                                                           

                                                                                                                                                   

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

 


阅读(2387) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~