www.pudn.com > RTC_ds1307.rar > testRTC.c


// 
//	Description 
//		test at91_rtcex driver 
// 
//	Copyright (C) 2005  Hyesco Technology Co.,Ltd 
// 
//	Author: Zheng Geng  
// 
//	History: 
// 
//	2005.10   Zheng Geng   
//               Original version 
// 
 
#include  
#include 		/* getenv() */ 
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
 
#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][-][-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; 
}