Chinaunix首页 | 论坛 | 博客
  • 博客访问: 209582
  • 博文数量: 93
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 978
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-10 15:46
个人简介

青春无悔

文章分类

全部博文(93)

文章存档

2015年(16)

2014年(77)

我的朋友

分类: LINUX

2014-11-11 17:55:48


Makefile

点击(此处)折叠或打开

  1. all: rtc_app
  2. CROSS_COMPILE = arm-linux-gnueabihf-
  3. src := $(shell ls *.c)

  4. rtc_app: $(src)
  5.     $(CROSS_COMPILE)gcc -static -o $@ $^
  6.     $(CROSS_COMPILE)strip $@
  7.     @cp -f ./rtc_app /tftproot
  8. clean:
  9.     @rm -vf rtc_app *.o *~
main.c

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <linux/rtc.h>
  3. #include <sys/ioctl.h>
  4. #include <sys/time.h>
  5. #include <sys/types.h>
  6. #include <fcntl.h>
  7. #include <unistd.h>
  8. #include <stdlib.h>
  9. #include <errno.h>

  10. int main(int argc, char **argv)
  11. {
  12.     int ret, fd;
  13.     struct rtc_time rtc_tm;
  14.     int dev_numb, cnt = 10;

  15.     printf("\r\nPlease Select Devie: 0(rtc0), 1(rtc1), 2(rtc2):");
  16.     scanf("%d",&dev_numb);

  17.     if (0 == dev_numb) fd = open("/dev/rtc0", O_RDWR);
  18.     else if (1 == dev_numb) fd = open("/dev/rtc1", O_RDWR);
  19.     else if (2 == dev_numb) fd = open("/dev/rtc2", O_RDWR);
  20.     else
  21.     {
  22.          printf( "Fail to Select Device%d!\n", dev_numb);
  23.          exit(1);
  24.     }

  25.     if (fd < 0)
  26.     {
  27.          printf( "Fail to open PCF8563!\n" );
  28.          exit(1);
  29.     }
  30.     
  31.     ret=ioctl(fd, RTC_RD_TIME, &rtc_tm);
  32.     if (ret < 0)
  33.     {
  34.            printf( "Fail to read_time!\n" );
  35.     }
  36.     else
  37.     {
  38.            printf( "\r\nRTC_RD_TIME as follow:" );
  39.            printf( "\r\n>>rtc_tm.tm_isdst %d",rtc_tm.tm_isdst);
  40.            printf( "\r\n>>rtc_tm.tm_yday %d",rtc_tm.tm_yday);
  41.            printf( "\r\n>>rtc_tm.tm_wday %d",rtc_tm.tm_wday);
  42.            printf( "\r\n>>rtc_tm.tm_year %d",rtc_tm.tm_year);
  43.            printf( "\r\n>>rtc_tm.tm_mon %d",rtc_tm.tm_mon);
  44.            printf( "\r\n>>rtc_tm.tm_mday %d",rtc_tm.tm_mday);
  45.            printf( "\r\n>>rtc_tm.tm_hour %d",rtc_tm.tm_hour);
  46.            printf( "\r\n>>rtc_tm.tm_min %d",rtc_tm.tm_min);
  47.            printf( "\r\n>>rtc_tm.tm_sec %d",rtc_tm.tm_sec);
  48.         printf("\r\nRead PCF8563 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);
  49.     }

  50.     rtc_tm.tm_year = rtc_tm.tm_year+1;
  51.     rtc_tm.tm_mon = rtc_tm.tm_mon+1;
  52.     rtc_tm.tm_mday = rtc_tm.tm_mday+1;
  53.     rtc_tm.tm_hour = rtc_tm.tm_hour+1;
  54.     rtc_tm.tm_min = rtc_tm.tm_min+1;
  55.     rtc_tm.tm_sec = rtc_tm.tm_sec+1;
  56.     

  57.     ret = ioctl(fd, RTC_SET_TIME, &rtc_tm);
  58.     if (ret < 0) {printf("Fail to set_time!%d\n",errno);}
  59.     else {printf("set_time ok!\n");}

  60.     while (cnt--)
  61.     {
  62.         ret=ioctl(fd, RTC_RD_TIME, &rtc_tm);
  63.         if (ret < 0)
  64.         {
  65.              printf( "Fail to read_time!\n" );
  66.         }
  67.         printf("\r\nRead PCF8563 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);
  68.         sleep(1);
  69.     }

  70.     close(fd);

  71.     // sync system time as hardware rtc time
  72.     ret = system( "/sbin/hwclock -s" );
  73.     printf("hwclock opt %d\n",ret);

  74.     return 0;
  75. }



调试结果如下:
Please Select Devie: 0(rtc0), 1(rtc1), 2(rtc2):1                                
                                                                                
RTC_RD_TIME as follow:                                                          
>>rtc_tm.tm_isdst 0                                                             
>>rtc_tm.tm_yday  0                                                             
>>rtc_tm.tm_wday  1                                                             
>>rtc_tm.tm_year  114                                                           
>>rtc_tm.tm_mon   10                                                            
>>rtc_tm.tm_mday  11                                                            
>>rtc_tm.tm_hour  1                                                             
>>rtc_tm.tm_min   9                                                             
>>rtc_tm.tm_sec   36                                                            
Read PCF8563 time is 11-11-2014, 01:09:36.                                      
set_time ok!                                                                    
                                                                                
Read PCF8563 time is 12-12-2015, 02:10:37.                                      
                                                                                
Read PCF8563 time is 12-12-2015, 02:10:38.



硬件时钟与系统时钟同步:# hwclock --hctosys(hc代表硬件时间,sys代表系统时间)或者# clock --hctosys
系统时钟和硬件时钟同步:# hwclock --systohc或者# clock --systohc

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