📄 ds1302.c
字号:
/*!****************************************************************************!*! FILE NAME : ds1302.c*!*! DESCRIPTION: Implements an interface for the DS1302 RTC through Etrax I/O*!*! Functions exported: ds1302_readreg, ds1302_writereg, ds1302_init, get_rtc_status*!*! $Log: ds1302.c,v $*! Revision 1.11 2001/06/14 12:35:52 jonashg*! The ATA hack is back. It is unfortunately the only way to set g27 to output.*!*! Revision 1.9 2001/06/14 10:00:14 jonashg*! No need for tempudelay to be inline anymore (had to adjust the usec to*! loops conversion because of this to make it slow enough to be a udelay).*!*! Revision 1.8 2001/06/14 08:06:32 jonashg*! Made tempudelay delay usecs (well, just a tad more).*!*! Revision 1.7 2001/06/13 14:18:11 jonashg*! Only allow processes with SYS_TIME capability to set time and charge.*!*! Revision 1.6 2001/06/12 15:22:07 jonashg*! * Made init function __init.*! * Parameter to out_byte() is unsigned char.*! * The magic number 42 has got a name.*! * Removed comment about /proc (nothing is exported there).*!*! Revision 1.5 2001/06/12 14:35:13 jonashg*! Gave the module a name and added it to printk's.*!*! Revision 1.4 2001/05/31 14:53:40 jonashg*! Made tempudelay() inline so that the watchdog doesn't reset (see*! function comment).*!*! Revision 1.3 2001/03/26 16:03:06 bjornw*! Needs linux/config.h*!*! Revision 1.2 2001/03/20 19:42:00 bjornw*! Use the ETRAX prefix on the DS1302 options*!*! Revision 1.1 2001/03/20 09:13:50 magnusmn*! Linux 2.4 port*!*! Revision 1.10 2000/07/05 15:38:23 bjornw*! Dont update kernel time when a RTC_SET_TIME is done*!*! Revision 1.9 2000/03/02 15:42:59 macce*! * Hack to make RTC work on all 2100/2400*!*! Revision 1.8 2000/02/23 16:59:18 torbjore*! added setup of R_GEN_CONFIG when RTC is connected to the generic port.*!*! Revision 1.7 2000/01/17 15:51:43 johana*! Added RTC_SET_CHARGE ioctl to enable trickle charger.*!*! Revision 1.6 1999/10/27 13:19:47 bjornw*! Added update_xtime_from_cmos which reads back the updated RTC into the kernel.*! /dev/rtc calls it now.*!*! Revision 1.5 1999/10/27 12:39:37 bjornw*! Disabled superuser check. Anyone can now set the time.*!*! Revision 1.4 1999/09/02 13:27:46 pkj*! Added shadow for R_PORT_PB_CONFIG.*! Renamed port_g_shadow to port_g_data_shadow.*!*! Revision 1.3 1999/09/02 08:28:06 pkj*! Made it possible to select either port PB or the generic port for the RST*! signal line to the DS1302 RTC.*! Also make sure the RST bit is configured as output on Port PB (if used).*!*! Revision 1.2 1999/09/01 14:47:20 bjornw*! Added support for /dev/rtc operations with ioctl RD_TIME and SET_TIME to read*! and set the date. Register as major 121.*!*! Revision 1.1 1999/09/01 09:45:29 bjornw*! Implemented a DS1302 RTC driver.*!*!*! ---------------------------------------------------------------------------*!*! (C) Copyright 1999, 2000, 2001 Axis Communications AB, LUND, SWEDEN*!*! $Id: ds1302.c,v 1.11 2001/06/14 12:35:52 jonashg Exp $*!*!***************************************************************************//*#include <linux/config.h>#include <linux/fs.h>#include <linux/init.h>#include <linux/mm.h>#include <linux/module.h>#include <linux/miscdevice.h>#include <linux/delay.h>#include <linux/blkdev.h>#include <asm/uaccess.h>#include <asm/system.h>//#include <asm/svinto.h>#include <asm/io.h>*/#include <linux/fs.h>
#include <linux/iobuf.h>
//#include <linux/major.h>
#include <linux/blkdev.h>
#include <linux/capability.h>
#include <linux/smp_lock.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include <linux/module.h>//#include <asm/rtc.h>//#include <linux/rtc.h>#include "ds1302.h"int ds1302_open(struct inode*, struct file *);
int ds1302_close(struct inode*, struct file *);
int rtc_ioctl(struct inode*inode,struct file *flip,unsigned int cmd,unsigned long arg);#define DS1302_RST GPIO_F7#define DS1302_SCL GPIO_F4#define DS1302_SDA GPIO_F6#define INN 0#define OUTT 1//#define RTC_MAJOR_NR 121 /* local major, change later */static int RTC_MAJOR_NR=0;static devfs_handle_t devfs_handle;static const char ds1302_name[] = "ds1302";/* The DS1302 might be connected to different bits on different products. * It has three signals - SDA, SCL and RST. RST and SCL are always outputs, * but SDA can have a selected direction. * For now, only PORT_PB is hardcoded. *//* The RST bit may be on either the Generic Port or Port PB. *//*#ifdef CONFIG_ETRAX_DS1302_RST_ON_GENERIC_PORT#define TK_RST_OUT(x) REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow, CONFIG_ETRAX_DS1302_RSTBIT, x)#define TK_RST_DIR(x)#else//#define TK_RST_OUT(x) REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, CONFIG_ETRAX_DS1302_RSTBIT, x)//#define TK_RST_DIR(x) REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, CONFIG_ETRAX_DS1302_RSTBIT, x)#define TK_RST_OUT(x) write_gpio_bit(GPIO_MODE_OUT | DS1302_RST,x)#define TK_RST_DIR(x)#endif//#define TK_SDA_OUT(x) REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, CONFIG_ETRAX_DS1302_SDABIT, x)#define TK_SDA_OUT(x) write_gpio_bit(GPIO_MODE_OUT | DS1302_SDA,x)//#define TK_SCL_OUT(x) REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, CONFIG_ETRAX_DS1302_SCLBIT, x)#define TK_SCL_OUT(x) write_gpio_bit(GPIO_MODE_OUT | DS1302_SCL,x)//#define TK_SDA_IN() ((*R_PORT_PB_READ >> CONFIG_ETRAX_DS1302_SDABIT) & 1)#define TK_SDA_IN() read_gpio_bit(DS1302_SDA)//#define TK_SDA_DIR(x) REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, CONFIG_ETRAX_DS1302_SDABIT, x)//#define TK_SDA_DIR(x) (if(x==1) set_gpio_ctrl(GPIO_MODE_OUT | DS1302_SDA); else set_gpio_ctrl(GPIO_MODE_IN | DS1302_SDA); )#define TK_SDA_DIR(x) set_gpio_ctrl(x | DS1302_SDA)//#define TK_SCL_DIR(x) REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, CONFIG_ETRAX_DS1302_SCLBIT, x)#define TK_SCL_DIR(x)*//* 1 is out, 0 is in */void TK_SDA_OUT(char x){ write_gpio_bit(GPIO_MODE_OUT | DS1302_SDA,x);}char TK_SDA_IN(void){ return(read_gpio_bit(DS1302_SDA));}void TK_SCL_OUT(char x){ write_gpio_bit(GPIO_MODE_OUT | DS1302_SCL,x);}void TK_RST_OUT(char x){ write_gpio_bit(GPIO_MODE_OUT | DS1302_RST,x);}void TK_SDA_DIR(char dir)
{
if(dir==1)
set_gpio_ctrl(GPIO_MODE_OUT | DS1302_SDA);
else if(dir==0)
set_gpio_ctrl(GPIO_MODE_IN | DS1302_SDA);
else;
}void TK_SCL_DIR(char dir)
{
}void TK_RST_DIR(char dir)
{
}/* * The reason for tempudelay and not udelay is that loops_per_usec * (used in udelay) is not set when functions here are called from time.c */static void tempudelay(int usecs) { volatile int loops; for(loops = usecs * 12; loops > 0; loops--) /* nothing */; }/* Send 8 bits. */static voidout_byte(unsigned char x) { int i; //TK_SDA_DIR(1); TK_SDA_DIR(OUTT); for (i = 8; i--;) { /* The chip latches incoming bits on the rising edge of SCL. */ TK_SCL_OUT(0); TK_SDA_OUT(x & 1); tempudelay(1); TK_SCL_OUT(1); tempudelay(1); x >>= 1; } //TK_SDA_DIR(0); TK_SDA_DIR(INN);}static unsigned charin_byte(void) { unsigned char x = 0; int i; /* Read byte. Bits come LSB first, on the falling edge of SCL. * Assume SDA is in input direction already. */ //TK_SDA_DIR(0); TK_SDA_DIR(INN); for (i = 8; i--;) { TK_SCL_OUT(0); tempudelay(1); x >>= 1; x |= (TK_SDA_IN() << 7); TK_SCL_OUT(1); tempudelay(1); } return x;}/* Prepares for a transaction by de-activating RST (active-low). */static voidstart(void) { TK_SCL_OUT(0); tempudelay(1); TK_RST_OUT(0); tempudelay(5); TK_RST_OUT(1); }/* Ends a transaction by taking RST active again. */static voidstop(void) { tempudelay(2); TK_RST_OUT(0);}/* Enable writing. */static voidds1302_wenable(void) { start(); out_byte(0x8e); /* Write control register */ out_byte(0x00); /* Disable write protect bit 7 = 0 */ stop();}static voidds1302_osc_enable(void){ char tmp; start(); out_byte(0x81); tmp=in_byte(); stop(); if((tmp&0x80)==0x00); else { start(); out_byte(0x80); out_byte(0x00); stop(); }}/* Disable writing. */static voidds1302_wdisable(void) { start(); out_byte(0x8e); /* Write control register */ out_byte(0x80); /* Disable write protect bit 7 = 0 */ stop();}/* Probe for the chip by writing something to its RAM and try reading it back. */#define MAGIC_PATTERN 0x42static intds1302_probe(void) { int retval, res; TK_RST_DIR(1); TK_SCL_DIR(1); //TK_SDA_DIR(0); TK_SDA_DIR(INN); ds1302_osc_enable(); ds1302_writereg(RTC_TRICKLECHARGER, 0x00);//disable the trickle charger /* Try to talk to timekeeper. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -