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

📄 ds1744rtc.c

📁 MPC826X挂接实时时钟芯片DS1744的驱动
💻 C
字号:
/* ds1744Rtc.c - DS1744 Real-Time Clock chip driver *//*modification history--------------------20050215,sd,    modified for mpc8265 board01a,12mar02,rab  derived from ds12887Rtc.c.*//*DESCRIPTIONThis module is to be used by including it from <target>/sysLib.cfile, it adds two functions to handle to RTC chip for the BSP.This driver is written for the Dallas Semiconductor 1744.The date and time format here is compatible with ANSI time,and one should define INCLUDE_RTC in <target>config.hto enable these functions.Although it is much easier to use this chip in bonary mode,this driver uses it in BCD mode, so that the time and datesettings are compatible with the BIOS and MS-DOS.*//* includes */#include "vxWorks.h"#include "time.h"#include "intLib.h"#include "DS1744RTC.h"/* macro to check that <v> is between <l> and <h> inclusive range */#define CHECK_RANGE(v,l,h)  (((v)>=(l))&&((v)<=(h)))/* Macros to convert 2-digit BCD into binary and vice versa */#define BIN2BCD(b)  (((b)%10) | (((b)/10)<<4))#define BCD2BIN(b)  (((b)&0xf) + ((b)>>4)*10)/* RTC register access macros */#define RTC_JMP     1#define RTC_BASE    NV_RAM_RTC_BASE#define RTC_REG(o)      ((volatile UINT8 *)(RTC_BASE + o))#define RTC_REG_SET(reg,val)    (*RTC_REG(reg) = (val))#define RTC_REG_GET(reg)    (*RTC_REG(reg))/******************************************************************************** sysRtcGet - get the current date abd time from a Real Time Clock chip** The values are returned in a ANSI time structure.* During initialization phase, the POSIX clock of the system is* set according to the Real Time Clock time and date, thus* it is recommended to use the POSIX functions and avoid calling* this function from the application software,* to acheive similar results but with greater portability.** NOTE: Interrupts are locked during reading of the values from the chip.** RETURNS: OK or ERROR** SEE ALSO* clockLib(), ansiTime**/STATUS sysRtcGet	(	struct tm *tm	)	{	FAST int ipl ;	UINT8 cent;	ipl = intLock();	/*RTC_REG_SET( DS1744_RTC_CENTURY, DS1744_BIT_RED);*/	tm->tm_hour = RTC_REG_GET( DS1744_RTC_HOUR)&0x3f; /* get BCD regs as is */	tm->tm_min  = RTC_REG_GET( DS1744_RTC_MIN)&0x7f;    /* while ints are off, */	tm->tm_sec  = RTC_REG_GET( DS1744_RTC_SEC)&0x7f;    /* and decode later */	tm->tm_mon  = RTC_REG_GET( DS1744_RTC_MONTH)&0x1f;	tm->tm_mday = RTC_REG_GET( DS1744_RTC_DAY_OF_MN)&0x3f;	tm->tm_year = RTC_REG_GET( DS1744_RTC_YEAR);	tm->tm_wday = RTC_REG_GET( DS1744_RTC_DAY_OF_WK)&0x7;	cent = RTC_REG_GET( DS1744_RTC_CENTURY)&0x3f;	/*RTC_REG_SET( DS1744_RTC_CENTURY, cent);*/	intUnlock(ipl);	/* corrections - all registers are BCD, we need them in binary */	tm->tm_hour = BCD2BIN( tm->tm_hour );	tm->tm_min  = BCD2BIN( tm->tm_min  );	tm->tm_sec  = BCD2BIN( tm->tm_sec  );	tm->tm_mon  = BCD2BIN( tm->tm_mon  );	tm->tm_mday = BCD2BIN( tm->tm_mday );	tm->tm_year = BCD2BIN( tm->tm_year );	tm->tm_wday = BCD2BIN( tm->tm_wday );	cent = BCD2BIN(cent);	/* corrections -  some fields range is defined differently */	/*tm->tm_mon -- ;*/ /* chip does 1-12, POSIX needs 0-11 */	/*tm->tm_wday -- ;*/    /* chip does 1-7, POSIX needs 0-6 */	/* corrections - handle year after y2k */	tm->tm_year += 100*cent ;	/* These fields are unknown, filled with 0 */	tm->tm_yday = 0;    /* days since January 1     - [0, 365] */	tm->tm_isdst= 0;    /* Daylight Saving Time flag */	return OK ;	}/******************************************************************************** sysRtcSet  - Set the time and date into the RTC chip** NOTE* Setting the time is done with interrupts locked, but it is expected* to be called rarely.** RETURNS: OK or ERROR if values are out of range.*/STATUS sysRtcSet	(	struct tm *timedate	)	{	struct tm t1 = * timedate ;	FAST struct tm *tm = &t1 ;      /* make a local copy of the argument */	FAST int ipl ;	UINT8 cent;		/* Check value ranges */	if( !CHECK_RANGE( tm->tm_sec,  0, 59)) return ERROR ;	if( !CHECK_RANGE( tm->tm_min,  0, 59)) return ERROR ;	if( !CHECK_RANGE( tm->tm_hour, 0, 23)) return ERROR ;	if( !CHECK_RANGE( tm->tm_mday, 1, 31)) return ERROR ;	/*if( !CHECK_RANGE( tm->tm_mon , 0, 11)) return ERROR ;*/	if( !CHECK_RANGE( tm->tm_mon , 1, 12)) return ERROR ;	if( !CHECK_RANGE( tm->tm_year, 1900, 2199)) return ERROR ;	cent = tm->tm_year/100;	tm->tm_year %= 100;		/* corrections - convert to BCD and add offset where needed. */	tm->tm_hour = BIN2BCD( tm->tm_hour ); 	tm->tm_min  = BIN2BCD( tm->tm_min ); 	tm->tm_sec  = BIN2BCD( tm->tm_sec ); 	/*tm->tm_mon  = BIN2BCD( tm->tm_mon+1 ); */	tm->tm_mon  = BIN2BCD( tm->tm_mon ); 	tm->tm_mday = BIN2BCD( tm->tm_mday ); 	tm->tm_year = BIN2BCD( tm->tm_year ); 	/*tm->tm_wday = BIN2BCD( tm->tm_wday+1 ); */	tm->tm_wday = BIN2BCD( tm->tm_wday );	cent = BIN2BCD(cent);	ipl = intLock();	/* enable SET bit to update clock registers */	RTC_REG_SET( DS1744_RTC_CENTURY, DS1744_BIT_WRT);	RTC_REG_SET( DS1744_RTC_HOUR,      tm->tm_hour );	RTC_REG_SET( DS1744_RTC_MIN,       tm->tm_min );	RTC_REG_SET( DS1744_RTC_SEC,       tm->tm_sec );	RTC_REG_SET( DS1744_RTC_MONTH,     tm->tm_mon );	RTC_REG_SET( DS1744_RTC_DAY_OF_MN, tm->tm_mday);	RTC_REG_SET( DS1744_RTC_YEAR,      tm->tm_year);	RTC_REG_SET( DS1744_RTC_DAY_OF_WK, tm->tm_wday);	RTC_REG_SET( DS1744_RTC_CENTURY, cent);	/* update complete, clear SET bit */	RTC_REG_SET( DS1744_RTC_CENTURY , cent);	intUnlock(ipl);	return OK ;	}/******************************************************************************** sysRtcInit - initialize the RTC chip** This function should called from sysHwInit2().**/STATUS sysRtcInit ( void)	{	/* turn the oscilator on, just in case */	RTC_REG_SET( DS1744_RTC_SEC , ~DS1744_BIT_OSC);	return (OK) ;	}/******************************************************************************** sysRtcShutdown - put the RTC chip in to sleep mode** The sleep mode is designed to save on battery life during inactive* storage of the equipment. During this time the date & time do not* progress.**/void sysRtcShutdown(void)	{	RTC_REG_SET( DS1744_RTC_SEC, DS1744_BIT_OSC);	}#undef __RTCTEST#ifdef __RTCTEST/*get current time and print it*/void rtcget(){	struct tm tm1;	printf("8265 RTC timer get function demo.\n");	sysRtcGet(&tm1);	printf("Current time is:\n");	printf("%d-%d-%d(%d:%d:%d)\n",   tm1.tm_year,tm1.tm_mon,tm1.tm_mday,		tm1.tm_hour,tm1.tm_min,tm1.tm_sec);}/*set current time*/void rtcset(){	struct tm tm1;	printf("8265 RTC timer set function demo.\n");	printf("Please enter current time.\n");	printf("year:");	scanf("%d",&tm1.tm_year);	printf("month:");	scanf("%d",&tm1.tm_mon);	printf("month day:");	scanf("%d",&tm1.tm_mday);	printf("week day:");	scanf("%d",&tm1.tm_wday);	printf("hour:");	scanf("%d",&tm1.tm_hour);	printf("minute:");	scanf("%d",&tm1.tm_min);	printf("second:");	scanf("%d",&tm1.tm_sec);	printf("\nUpdating......");	sysRtcSet(&tm1);	printf("done.\n");	printf("Please use rtcget() to show results.\n");}#endif

⌨️ 快捷键说明

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