⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testrtc.c

📁 平台:ARM9的嵌入式LINUX RTC时钟驱动
💻 C
字号:
//
//	Description
//		test at91_rtcex driver
//
//	Copyright (C) 2005  Hyesco Technology Co.,Ltd
//
//	Author: Zheng Geng <gzheng@hyesco.com>
//
//	History:
//
//	2005.10   Zheng Geng  <gzheng@hyesco.com>
//               Original version
//

#include <stdio.h>
#include <stdlib.h>		/* getenv() */
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <sys/time.h>
#include <unistd.h>
#include <time.h>
#include <getopt.h>
#include <linux/rtc.h>

#define VERSION "1.0"

void            Usage(int status)
{
	FILE           *fp;
/* These declarations are for embedding the copyright and version information in the binary for display by utilities such as what(1).  They needn't be translated. */
	static char     version[] = "testRTC " VERSION "\n";
	static char     copyright[] =
	"copyright (c) 2004 Beijing Hyesco Co.,Ltd\n";
	fp = (status == EXIT_SUCCESS) ? stdout : stderr;
	fprintf(fp, version);
	fprintf(fp, copyright);
	fprintf(fp, "usage: testRTC [-r][-<w><YYYY.MM.DD,D,hh:mm:ss]>][-h]");
	fprintf(fp, "\n\n");
	fprintf(fp, "r: read RTC time\nw: write RTC time, for example -w2005.10.15,6,13:41:05\nh: this helpful message\n");
	exit(status);
}

int Init()
{
	int fd;
	//open device file
	fd = open("/dev/misc/rtc",O_RDWR);
	if(fd < 0)
	{
		printf("device open fail\n");
		return -1;
	}

	return fd;
}

int main(int argc,char **argv)
{
	int fd,ch;
	struct rtc_time tm;

	while((ch = getopt(argc,argv,"rw:h"))!= -1)
	switch(ch)
	{
	case 'r':
		//read RTC time
		fd=Init();
		if (fd>0)
		{
			ioctl(fd,RTC_RD_TIME,&tm);
			printf("%04d.%02d.%02d,%01d,%02d:%02d:%02d\n"
				,tm.tm_year,tm.tm_mon,tm.tm_mday,tm.tm_wday,tm.tm_hour,tm.tm_min,tm.tm_sec);
			close(fd);
		}
		break;

	case 'w':
		//write RTC time
		fd=Init();
		if (fd>0)
		{
			if (sscanf(optarg, "%d.%d.%d,%d,%d:%d:%d", &tm.tm_year,
								&tm.tm_mon, &tm.tm_mday, &tm.tm_wday, &tm.tm_hour,
								&tm.tm_min, &tm.tm_sec) == 7) 
			{
				if (ioctl(fd,RTC_SET_TIME,&tm)<0)
					printf ("ioctl error\n");
				printf("Wrte time %04d.%02d.%02d,%01d,%02d:%02d:%02d\n"
				,tm.tm_year,tm.tm_mon,tm.tm_mday,tm.tm_wday,tm.tm_hour,tm.tm_min,tm.tm_sec);
			}
			else
				printf ("Please give right date!\n");
			close(fd);
		}
		break;
	case 'h':
	default:
		Usage(EXIT_SUCCESS);
	}

	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -