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

📄 ds1501.c

📁 Curtiss-Wright Controls Embedded Computing公司的cw183板bsp源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************  Filename: ds1501.c**  Copyright 2003 by DY4 Systems.  All Rights Reserved.**  Project Name: 182 BSP **  Target:  182  **  Description: Real-time clock library **  Usage:      **  Log: *  Initial revision **  Modification History*  --------------------*  01a,10jan05,rcd ported to VME-183.*  01 ,16sep04,mb  PT 2180: replaced obsolete macro RTC_SPACE_ADRS by*                 CPU_RTC_BASE_ADRS  *****************************************************************************//* ds1501.c - Real-time clock device driver */ /* copyright 2003 Dy 4 Systems, Inc. *//*modification history--------------------01a,14jan03,dle	       written*//*DESCRIPTIONThis is the driver for a real-time clock to be used with a DALLAS Semiconductor DS1501 device.FEATURESa) 12/24 Hour modes b) Alarm - invokes user routine installed via rtcAlarmConnect()BSP CONFIGURATION/INITTALIZATIONThe BSP should invoke rtcHwInit() to reset the RTC device to a known reset state.INTERRUPT HANDLINGThe BSP must also install the RTC handler, rtcInt(), for the appropriatevector, and enable the corresponding interrupt Source.DRIVER INITIALIZATIONBefore the driver can be used, it must be first initialised by calling the routine rtcInit(). This can be done automatically upon start-up by adding to the system file "usrConfig.c", or by the user application. 	USER CALLABLE ROUTINES While most rountines in this driver are accessablethrough the I/O system via standard I/O calls (open, ioctl), the driver provides more intuitive routines to interface with the RTC. RTC DATE/TIME The date and time can be both accessed and set by the user. For setting eitherthe time, the parameters must be in numeric form with 24-hrnotation required for rtcTimeSet().    	rtcDateSet ([day], [date], [month], [year])eg. 	rtcDateSet (1, 15, 1, 1997)	which sets the date to Sunday, 15 January, 1997.Note the month is enumerated from 1 (January) to 12 (December) and the day from 1 (Sunday) to 7 (Saturday).  Use the provided macros to designatethe day (SUN .. SAT), and month (JAN .. DEC).	rtcTimeSet ([hr], [min], [sec])eg.	rtcTimeSet (15, 25, 30)	which sets the time to 15h15:30s (or 3:25:30 PM)  The read the date or time, use the calls; 	rtcDateGet()		returns date in RTC_DATE structure	rtcDateShow			displays, Date: Monday, January 15, 1997	rtcTimeGet()		returns date in RTC_TIME structure	rtcTimeShow()		e.g. displays, Time: 15:15:31s (or 3:15:31 PM) 						depending on the mode.	RTC 12/24hr MODEA special feature which allows the user to specify either 12hr (i.e. AM/PM) ora 24hr (MIL) clock mode.	rtcModeSet([mode=12/24])Note that the hour is automatically updated to reflect the new mode when the mode is changed.  	DOS-SUPPORTED DATE AND TIMEFor support of DOS file system date/time for local files, a hook, rtcDosHook, is provided to be used in conjunction with VxWorks.  Invoke dosFsDateTimeInstall()with the supplied hook.	dosFsDateTimeInstall (rtcDosHook)ALARMThe user may set the alarm, and attach, via rtcAlarmConnect(), a user routineto be invoked upon the alarm interrupt.This Driver is Y2K compliant and supports only the years from 1999 t0 2100.*//* includes */#include "vxWorks.h"#include "iosLib.h"#include "dosFsLib.h"#include "errnoLib.h"#include "logLib.h"#include "stdio.h"#include "intLib.h"#include "h/drv/dy4/boardName.h"#include "config.h"#ifdef VME_182#include "dy4182.h"#include "intCtrl182_dy4.h"#endif#ifdef VME_183#include "cwv183.h"#include "intCtrl183.h"#define RTC RTC_INT_SRC#endif#include "h/drv/rtc/ds1501.h"/* ioctl function codes */#define SET_DATE    100#define GET_DATE    101#define SET_TIME    102#define GET_TIME    103#define MODE_12HR   104#define MODE_24HR   105#define MINI_YEAR   1999#define MAXI_YEAR   2100   #define RTC_DEVICE "/rtc"#define RTC_PM_INDICATOR 	DS1501_HOURS_PM/* BSP define-able to access device */#define RTC_BASE_ADDR	(volatile UINT8 *)(CPU_RTC_BASE_ADRS)/* locals */LOCAL char *monthTable [13] ={    "", "January", "February", "March", "April", "May", "June", "July",    "August", "September", "October", "November", "December"};LOCAL char *dayTable [8] ={    "", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", 	"Saturday"};LOCAL RTC_DEV rtcDv =       /* device descriptor */{    {{NULL}},    FALSE};LOCAL RTC_DATE_TIME rtcDateTime;LOCAL int rtcDrvNum;LOCAL FUNCPTR	rtcAlarmRoutine		= NULL;LOCAL int	rtcAlarmArg		= 0;#ifndef CPU1_IMAGE  /* CPU0 only */static void DS1501Setup (BOOL forceSetup);#endifLOCAL STATUS rtcIoctl (RTC_DEV *pRtcDv, int request, int notUsed);LOCAL int rtcOpen (RTC_DEV *pRtcDv);LOCAL UINT8 rtcRegRead (UINT8 offset);LOCAL void rtcRegWrite (UINT8 offset, UINT8 val);LOCAL UINT8 rtcRegReadBcd (UINT8 offset);LOCAL void rtcRegWriteBcd (UINT8 offset, UINT8 val);LOCAL void rtcSpIoctl (int request);LOCAL void rtcUpdateEnable(void);			LOCAL void rtcUpdateDisable(void);LOCAL STATUS rtcDrv (void);LOCAL STATUS rtcDevCreate (void);LOCAL void rtcInt(void);#ifndef CPU1_IMAGE  /* CPU0 only *//******************************************************************************** rtcHwInit - initialize rtc h/w ** RETURNS: N/A ** NOMANUAL*/void rtcHwInit (void){   /* Setup DS1501 */   DS1501Setup(FALSE);}#endif/******************************************************************************** rtcInit -  initalize RTC driver** This routine creates and adds the driver to the I/O system.	** RETURNS: OK, or ERROR if the driver was unable to be initialized** SEE ALSO: rtcDrv(), rtcDevCreate()*/STATUS rtcInit (void){   if(rtcDrv() == ERROR)      return ERROR;   if(rtcDevCreate() == ERROR)      return ERROR;   rtcModeSet(24);   return OK;}/******************************************************************************** rtcDosHook - date and time hook to dosFsLib ** This routine is used as a hook to the dosFsDateTimeInstall() routine of* dosFsLib.** RETURNS: N/A** NOMANUAL*/    void rtcDosHook (DOS_DATE_TIME *pDateTime){   /* update local date and time structure */   rtcSpIoctl(GET_TIME);   rtcSpIoctl(GET_DATE);   /* pass values to DOS date and time structure */   pDateTime->dosdt_year = (rtcDateTime.century * 100) + rtcDateTime.year;   pDateTime->dosdt_month = rtcDateTime.month;   pDateTime->dosdt_day = rtcDateTime.date;	               pDateTime->dosdt_hour = rtcDateTime.hour;                 pDateTime->dosdt_minute = rtcDateTime.minute;           	   pDateTime->dosdt_second = rtcDateTime.second;           #ifdef RTC_DEBUG	   printf("CENTURY	= %d\n", rtcDateTime.century);   printf("YEAR	= %d\n", rtcDateTime.year);   printf("MONTH	= %d\n", rtcDateTime.month);   printf("DATE	= %d\n", rtcDateTime.date);   printf("DAY	= %d\n", rtcDateTime.day);#endif}/******************************************************************************** rtcModeSet - set the clock mode to 12 or 24 hr** RETURNS: OK, or ERROR if the mode could not be set ** NOMANUAL*/  STATUS rtcModeSet (int mode){   switch (mode)   {      case 12:         rtcSpIoctl(MODE_12HR);#ifdef INCLUDE_RTC_SHOW         logMsg("The mode is set to 12HR.\n",0,0,0,0,0,0);#endif /* INCLUDE_RTC_SHOW */         return (OK);      case 24:         rtcSpIoctl(MODE_24HR);#ifdef INCLUDE_RTC_SHOW         logMsg("The mode is set to 24HR.\n",0,0,0,0,0,0);#endif /* INCLUDE_RTC_SHOW */         return (OK);      default:         return (ERROR);   }}/******************************************************************************** rtcDateGet - get the date from RTC** This routine updates the RTC_DATE structure with current date from the RTC.** RETURNS: N/A** SEE ALSO: rtcSpIoctl()*/    void rtcDateGet (RTC_DATE *pDate){   rtcSpIoctl (GET_DATE);        /* pass values to DATE structure*/   pDate->year = (rtcDateTime.century * 100) + rtcDateTime.year;   pDate->month = rtcDateTime.month;   pDate->day = rtcDateTime.day;	               pDate->date = rtcDateTime.date;	  #ifdef INCLUDE_RTC_SHOW   rtcDateShow();#endif}#ifndef CPU1_IMAGE  /* CPU0 only *//******************************************************************************** rtcDateSet - set the date** This routine sets the RTC date with the given date.** RETURNS: N/A** SEE ALSO: rtcSpIoctl()*/  void rtcDateSet 		(	 	int day, 		/* 1..7  = SUN .. SAT */		int date,		 		int month, 		/* 1..12 = JAN .. DEC */		int year		){   int century; /* holds value to parse year */   /* error checks */   if (day < 1 || day > 7)      printErr("Invalid entry for day; valid range is [1 - 7].\n");   else if (month < 1 || month > 12)      printErr("Invalid entry for month; valid range is [1 - 12].\n");   /*This forces a Y2K complaint*/     else if (year < MINI_YEAR || year > MAXI_YEAR)      logMsg("Invalide entry for year; Only [%d to %d] is supported.\n", 				MINI_YEAR, MAXI_YEAR, 0,0,0,0);   else   {        century = (year / 100);		      rtcDateTime.day = day;      rtcDateTime.date = date;      rtcDateTime.month = month;      rtcDateTime.year = (year) - (century * 100);      rtcDateTime.century = century;            rtcSpIoctl (SET_DATE);   }}/******************************************************************************** rtcAlarmConnect - connect user alarm handler** This routine connects the given handler to be invoked upon an alarm.** RETURNS: OK always **/STATUS rtcAlarmConnect (FUNCPTR	routine, int arg){   rtcAlarmRoutine = routine;   rtcAlarmArg	 = arg;   return OK;}/******************************************************************************** rtcIsAlarmInUsed - check to see if the alarm is in used** This routine check the alarm status.** RETURNS: ENABLE/DISABLE**/int rtcIsAlarmInUsed(void){   UINT8 ctrlB = rtcRegRead(DS1501_CTRLB_OFFSET);   return ((ctrlB & DS1501_CTRLB_TIE) ? TRUE : FALSE);}/******************************************************************************** rtcAlarmEnable - enable the alarm ** This routine enables the alarm.** RETURNS: N/A**/void rtcAlarmEnable(void){   UINT8 ctrlB = rtcRegRead(DS1501_CTRLB_OFFSET) | DS1501_CTRLB_TIE;   rtcRegWrite(DS1501_CTRLB_OFFSET, ctrlB);}/******************************************************************************** rtcAlarmDisable - disable the alarm ** This routine disables the alarm.** RETURNS: N/A**/void rtcAlarmDisable(void){   UINT8 ctrlB = rtcRegRead(DS1501_CTRLB_OFFSET) & ~DS1501_CTRLB_TIE;   rtcRegWrite(DS1501_CTRLB_OFFSET, ctrlB);}/******************************************************************************** rtcAlarmSet - set an alarm ** This routine sets an alarm to go off at the given time in 24 hour mode.** RETURNS: OK, **/STATUS rtcAlarmSet (int hour, int minute, int second){   rtcUpdateDisable();   rtcRegWriteBcd(DS1501_SECONDS_ALARM_OFFSET, second);   rtcRegWriteBcd(DS1501_MINUTES_ALARM_OFFSET, minute);   rtcRegWriteBcd(DS1501_HOURS_ALARM_OFFSET, hour);    rtcRegWrite(DS1501_DATE_ALARM_OFFSET, 0x80); /* match hh:mm:ss */   rtcUpdateEnable();    return OK;}#endif/******************************************************************************** rtcTimeGet - get the time from RTC** This routine updates the RTC_TIME structure with current time from the RTC.** RETURNS: N/A** SEE ALSO: rtcSpIoctl()*/  void rtcTimeGet (RTC_TIME *pTime ){   rtcSpIoctl (GET_TIME);       pTime->hour = rtcDateTime.hour;                 pTime->minute = rtcDateTime.minute;           	   pTime->second = rtcDateTime.second;     #ifdef INCLUDE_RTC_SHOW   rtcTimeShow();#endif}#ifndef CPU1_IMAGE  /* CPU0 only *//******************************************************************************** rtcTimeSet - set the time** This routine sets the RTC time with the given time in 24 HR format.** RETURNS: N/A** SEE ALSO: rtcSpIoctl()*/   void rtcTimeSet (int hour, int minute, int second){   if (hour <  0 || hour > 23)      printErr("Invalid entry for hour [0-23].\n");   else if (minute < 0 || minute> 59)      printErr("Invalid entry for minute [0-59].\n");   else if (second < 0 || second > 59)      printErr("Invalid entry for seconds [0-59].\n");   else   {      rtcDateTime.hour = hour;      rtcDateTime.minute = minute;      rtcDateTime.second = second;      rtcSpIoctl (SET_TIME);   }}#endif/******************************************************************************** rtcDateShow - print the date

⌨️ 快捷键说明

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