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

📄 ds1302.c

📁 标准linux 2.4.20 针对ds1302 的RTC 源码,具有充电功能!
💻 C
📖 第 1 页 / 共 2 页
字号:
	ds1302_wenable();  	start();	out_byte(0xc0); /* write RAM byte 0 */		out_byte(MAGIC_PATTERN); /* write something magic */	start();	out_byte(0xc1); /* read RAM byte 0 */	if((res = in_byte()) == MAGIC_PATTERN) {		char buf[100];		stop();		ds1302_wdisable();		printk("%s: RTC found.\n", ds1302_name);		/*		printk("%s: SDA, SCL, RST on PB%i, PB%i, %s%i\n",		       ds1302_name,		       CONFIG_ETRAX_DS1302_SDABIT,		       CONFIG_ETRAX_DS1302_SCLBIT,#ifdef CONFIG_ETRAX_DS1302_RST_ON_GENERIC_PORT		       "GENIO",#else		       "PB",#endif		       CONFIG_ETRAX_DS1302_RSTBIT);*/    get_rtc_status(buf);    //printk(buf);		retval = 1;	} else {		stop();		printk("%s: RTC not found.\n", ds1302_name);		retval = 0;	}	return retval;}/* Read a byte from the selected register in the DS1302. */unsigned chards1302_readreg(int reg) {	unsigned char x;	start();	//out_byte(0x81 | (reg << 1)); /* read register */	out_byte(reg|0x01);	x = in_byte();	stop();	return x;}/* Write a byte to the selected register. */voidds1302_writereg( unsigned char val,int reg) {	ds1302_wenable();	start();	//out_byte(0x80 | (reg << 1)); /* write register */	out_byte(reg&0xfe);	out_byte(val);	stop();	ds1302_wdisable();}voidget_rtc_time(struct rtc_time *rtc_tm) {	unsigned long flags;	save_flags(flags);	cli();	/*	rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);	rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);	rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);	rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);	rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);	rtc_tm->tm_year = CMOS_READ(RTC_YEAR);  */	rtc_tm->tm_sec= ds1302_readreg(RTC_SECONDS);	rtc_tm->tm_min = ds1302_readreg(RTC_MINUTES);	rtc_tm->tm_hour = ds1302_readreg(RTC_HOURS);	rtc_tm->tm_mday = ds1302_readreg(RTC_DAY_OF_MONTH);	rtc_tm->tm_mon = ds1302_readreg(RTC_MONTH);	rtc_tm->tm_year = ds1302_readreg(RTC_YEAR);	rtc_tm->tm_wday = ds1302_readreg(RTC_WEEKDAY);	restore_flags(flags);		BCD_TO_BIN(rtc_tm->tm_sec);	BCD_TO_BIN(rtc_tm->tm_min);	BCD_TO_BIN(rtc_tm->tm_hour);	BCD_TO_BIN(rtc_tm->tm_mday);	BCD_TO_BIN(rtc_tm->tm_mon);	BCD_TO_BIN(rtc_tm->tm_year);	BCD_TO_BIN(rtc_tm->tm_wday);	/*	 * Account for differences between how the RTC uses the values	 * and how they are defined in a struct rtc_time;	 */  rtc_tm->tm_wday-=1;	if (rtc_tm->tm_year <= 69)		rtc_tm->tm_year += 100;	rtc_tm->tm_mon--;}static unsigned char days_in_mo[] =     {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};/* ioctl that supports RTC_RD_TIME and RTC_SET_TIME (read and set time/date). */static intrtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,	  unsigned long arg) {        unsigned long flags;	switch(cmd) {		case RTC_RD_TIME:	/* read the time/date from RTC	*/		{			struct rtc_time rtc_tm;									get_rtc_time(&rtc_tm);									if (copy_to_user((struct rtc_time*)arg, &rtc_tm, sizeof(struct rtc_time)))				return -EFAULT;				return 0;		}		case RTC_SET_TIME:	/* set the RTC */		{			struct rtc_time rtc_tm;			unsigned char mon, day, hrs, min, sec, leap_yr,wday;			unsigned char save_control, save_freq_select;			unsigned int yrs;			if (!capable(CAP_SYS_TIME))				return -EPERM;			if (copy_from_user(&rtc_tm, (struct rtc_time*)arg, sizeof(struct rtc_time)))				return -EFAULT;    				yrs = rtc_tm.tm_year + 1900;			mon = rtc_tm.tm_mon + 1;   /* tm_mon starts at zero */			day = rtc_tm.tm_mday;			hrs = rtc_tm.tm_hour;			min = rtc_tm.tm_min;			sec = rtc_tm.tm_sec;			wday= rtc_tm.tm_wday+1;												if ((yrs < 1970) || (yrs > 2069))				return -EINVAL;			leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));			if ((mon > 12) || (day == 0))				return -EINVAL;			if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))				return -EINVAL;						if ((hrs >= 24) || (min >= 60) || (sec >= 60))				return -EINVAL;            if(wday>7)        return -EINVAL;      			if (yrs >= 2000)				yrs -= 2000;	/* RTC (0, 1, ... 69) */			else				yrs -= 1900;	/* RTC (70, 71, ... 99) */      			BIN_TO_BCD(sec);			BIN_TO_BCD(min);			BIN_TO_BCD(hrs);			BIN_TO_BCD(day);			BIN_TO_BCD(mon);			BIN_TO_BCD(yrs);			BIN_TO_BCD(wday);			save_flags(flags);			cli();			/*			CMOS_WRITE(yrs, RTC_YEAR);			CMOS_WRITE(mon, RTC_MONTH);			CMOS_WRITE(day, RTC_DAY_OF_MONTH);			CMOS_WRITE(hrs, RTC_HOURS);			CMOS_WRITE(min, RTC_MINUTES);			CMOS_WRITE(sec, RTC_SECONDS);			*/			ds1302_writereg(yrs, RTC_YEAR);			ds1302_writereg(mon, RTC_MONTH);			ds1302_writereg(day, RTC_DAY_OF_MONTH);			ds1302_writereg(hrs, RTC_HOURS);			ds1302_writereg(min, RTC_MINUTES);			ds1302_writereg(sec, RTC_SECONDS);			ds1302_writereg(wday, RTC_WEEKDAY);						restore_flags(flags);			/* Notice that at this point, the RTC is updated but			 * the kernel is still running with the old time.			 * You need to set that separately with settimeofday			 * or adjtimex.			 */			return 0;		}		case RTC_SET_CHARGE: /* set the RTC TRICKLE CHARGE register */		{			int tcs_val;                        			unsigned char save_control, save_freq_select;			if (!capable(CAP_SYS_TIME))				return -EPERM;						if(copy_from_user(&tcs_val, (int*)arg, sizeof(int)))				return -EFAULT;			//tcs_val = RTC_TCR_PATTERN | (tcs_val & 0x0F);			tcs_val &= 0x0F;                              // disable  the trickle charger			ds1302_writereg(RTC_TRICKLECHARGER, tcs_val);			return 0;		}                		default:			return -ENOIOCTLCMD;	}}intget_rtc_status(char *buf) {	char *p;	struct rtc_time tm;	p = buf;	get_rtc_time(&tm);	/*	 * There is no way to tell if the luser has the RTC set for local	 * time or for Universal Standard Time (GMT). Probably local though.	 */	p += sprintf(p,		"rtc_time\t: %02d:%02d:%02d\n"		"rtc_date\t: %04d-%02d-%02d\n",		tm.tm_hour, tm.tm_min, tm.tm_sec,		tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);	return  p - buf;}/* The various file operations we support. */static struct file_operations rtc_fops = {        owner:          THIS_MODULE,        ioctl:          rtc_ioctl,	        open:           ds1302_open,        release:        ds1302_close,}; /* Just probe for the RTC and register the device to handle the ioctl needed. */int __initds1302_init(void) { 	int ret;	set_gpio_ctrl(GPIO_MODE_OUT | DS1302_SCL);	set_gpio_ctrl(GPIO_MODE_OUT | DS1302_RST);	set_gpio_pullup(DS1302_SCL);	set_gpio_pullup(DS1302_RST);	if (!ds1302_probe()) {//#ifdef CONFIG_ETRAX_DS1302_RST_ON_GENERIC_PORT		/*		 * The only way to set g27 to output is to enable ATA.		 *		 * Make sure that R_GEN_CONFIG is setup correct.		 */    	//	genconfig_shadow = ((genconfig_shadow &				    // ~IO_MASK(R_GEN_CONFIG, ata))				  // | 				   //(IO_STATE(R_GEN_CONFIG, ata, select)));        	//	*R_GEN_CONFIG = genconfig_shadow;    		//if (!ds1302_probe())      			//return -1;//#else     	printk("probe fail....\n");      return -1;//#endif  	}  /*	if (ret=register_chrdev(RTC_MAJOR_NR, ds1302_name, &rtc_fops)) {		printk(KERN_INFO "%s: unable to get major %d for rtc\n", 		       ds1302_name, RTC_MAJOR_NR);		return -1;		*/		ret = register_chrdev(0, ds1302_name, &rtc_fops); 
		if (ret < 0) { 
			printk("Driver register error!\n"); 
			return ret; 			}
	    RTC_MAJOR_NR=ret;		  devfs_handle = devfs_register(NULL, ds1302_name, DEVFS_FL_DEFAULT,RTC_MAJOR_NR, 0, S_IFCHR | S_IRUSR | S_IWUSR,&rtc_fops, NULL);	  return 0;}int ds1302_release(struct inode*inode, struct file *filp)
{ 
  devfs_unregister(devfs_handle);
	unregister_chrdev(RTC_MAJOR_NR, ds1302_name); 
}int ds1302_open(struct inode*inode, struct file *filp)
{
	return 0;
}int  ds1302_close(struct inode*inode, struct file *filp)
{ 
	return 0;
}module_init(ds1302_init); //用户加载该驱动时执行insmod gpio_driv.o就会自动调用gpio_init函数,它是驱动
//的入口点,相当于应用程序的main函数。
module_exit(ds1302_release); //用户卸载该驱动rmmod gpio_driv时执行

⌨️ 快捷键说明

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