📄 d1643rtc.c
字号:
/* d1643RTC.c - RTC DS1643 real time clock driver *//******************************************************************************* This source and object code has been made available to you by IBM on an AS-IS basis. IT IS PROVIDED WITHOUT WARRANTY OF ANY KIND, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE OR OF NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL IBM OR ITS LICENSORS BE LIABLE FOR INCIDENTAL, CONSEQUENTIAL OR PUNITIVE DAMAGES. IBM芐 OR ITS LICENSOR芐 DAMAGES FOR ANY CAUSE OF ACTION, WHETHER IN CONTRACT OR IN TORT, AT LAW OR AT EQUITY, SHALL BE LIMITED TO A MAXIMUM OF $1,000 PER LICENSE. No license under IBM patents or patent applications is to be implied by the copyright license. Any user of this software should understand that neither IBM nor its licensors will be responsible for any consequences resulting from the use of this software. Any person who transfers this source code or any derivative work must include the IBM copyright notice, this paragraph, and the preceding two paragraphs in the transferred software. Any person who transfers this object code or any derivative work must include the IBM copyright notice in the transferred software. COPYRIGHT I B M CORPORATION 2000 LICENSED MATERIAL - PROGRAM PROPERTY OF I B M"*******************************************************************************//* Copyright 1984-1994, Wind River Systems, Inc. *//*modification history--------------------01a,22apr99,mcg created*//*DESCRIPTIONThis is the driver for the real time clock portion of the Dallas SemiconductorDS1643 Nonvolative Timekeeping RAMUSER CALLABLE ROUTINESMost of the routines in this driver are accessible only through the I/Osystem. Two routines, however, must be called directly, rtcDrv toinitialize the driver, and rtcDevCreate to create the device.INSTALLING THE DRIVERBefore using the driver, it must be initialized by calling the routine:.CSrtcDrv ().CEThis routine should be called exactly once, before any ioctls orDevCreates. Normally, it is called from usrRoot.CREATING YOUR DEVICEBefore the real time clock device can be used, it has to be created.This is done with the rtcDevCreate call:.CSSTATUS rtcDevCreate (UINT).CEThe default name for this device is "/rtc".RTC DATE AND TIMEThis driver maintains the date and time in an internal structure.There are two routines provided to set the date and the time:.CSrtcDateSet (1992, 1, 13, 1); set date to Jan-13-1992, Mon.CS.CErtcTimeSet (14, 30, 22); set time to 14:30:22.CETo set the time, 24 hour notation is required.To read the date and the time the following two routineshave to be called:.CSrtcDateGet (); print current date.CS.CertcTimeGet (); print current time.CEDOS DATE AND TIMETo support date and time of local DOS files, a date/time hookroutine is provided. After installing the RTC driver andcreating the RTC device with the calls described above, this routinehas to be installed with the call:.CSdosFsDateTimeInstall (rtcHook);.CE*//* includes */#include "vxWorks.h"#include "stdio.h"#include "types.h"#include "iosLib.h"#include "errnoLib.h"#include "dosFsLib.h"/* typedefs */typedef struct /* RTC_DEV */ { DEV_HDR ioDev; BOOL created; UINT regBase; } RTC_DEV;typedef struct /* rtc date and time structure */ { UCHAR rtcYear; UCHAR rtcMonth; UCHAR rtcDayOfMonth; UCHAR rtcDayOfWeek; UCHAR rtcHour; UCHAR rtcMinute; UCHAR rtcSecond; } RTC_DATE_TIME;/* defines */#define RTC_DEVICE "/rtc"#define SET_DATE 100#define GET_DATE 101#define SET_TIME 102#define GET_TIME 103#define STOP_RTC 104#define START_RTC 105#define rtc_Year rtcDateTime.rtcYear#define rtc_Month rtcDateTime.rtcMonth#define rtc_DayOfMonth rtcDateTime.rtcDayOfMonth#define rtc_DayOfWeek rtcDateTime.rtcDayOfWeek#define rtc_Hour rtcDateTime.rtcHour#define rtc_Minute rtcDateTime.rtcMinute#define rtc_Second rtcDateTime.rtcSecond/* offsets of 8 bit RTC fields relative to the base address */#define CLOCK_CONTROL 0#define CLOCK_SECONDS 1#define CLOCK_MINUTES 2#define CLOCK_HOUR 3#define CLOCK_DAY 4#define CLOCK_DATE 5#define CLOCK_MONTH 6#define CLOCK_YEAR 7#define CLOCK_CAL_BITS 0x3f#define CLOCK_READ_BIT 0x40#define CLOCK_WRITE_BIT 0x80#define CLOCK_STOP_BIT 0x80#define CLOCK_YEAR_BITS 0xff#define CLOCK_MONTH_BITS 0x1f#define CLOCK_DATE_BITS 0x3f#define CLOCK_DAY_BITS 0x07#define CLOCK_HOUR_BITS 0x3f#define CLOCK_MINUTE_BITS 0x7f#define CLOCK_SECOND_BITS 0x7f/* locals */LOCAL RTC_DEV rtcDv = /* device descriptor */ { {{NULL}}, FALSE };LOCAL char *monthTable [12] = { "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" };LOCAL char *dayTable [7] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };LOCAL int rtcDrvNum; /* driver number assigned to this driver */LOCAL RTC_DATE_TIME rtcDateTime;/* forward declarations */LOCAL int rtcOpen (RTC_DEV *pRtcDv);LOCAL int rtcClose (RTC_DEV *pRtcDv);LOCAL STATUS rtcIoctl (RTC_DEV *pRtcDv, int request, BOOL print);LOCAL int toBCD (int intValue);LOCAL int fromBCD (int bcdValue);LOCAL void rtcSpIoctl (int request, BOOL print);/********************************************************************************* clockOutByte - writes one of the real time clock registers** RETURNS: N/A*/void clockOutByte ( UINT index, UCHAR value ) { sysOutByte(rtcDv.regBase+index, value); }/********************************************************************************* clockInByte - reads one of the real time clock registers** RETURNS: contents of register read*/UCHAR clockInByte ( UINT index ) { return sysInByte(rtcDv.regBase+index); }/********************************************************************************* rtcDrv - adding the driver to the driver system** This routine must be called in supervisor state.** RETURNS: OK or ERROR*/STATUS rtcDrv ( void ) { if (rtcDrvNum > 0) return (OK); rtcDrvNum = iosDrvInstall (rtcOpen, (FUNCPTR) NULL, rtcOpen, rtcClose, (FUNCPTR) NULL, (FUNCPTR) NULL, rtcIoctl); return (rtcDrvNum == ERROR ? ERROR : OK); }/********************************************************************************* rtcDevCreate - create a device for the real time clock** RETURN: OK or ERROR, if already created.*/STATUS rtcDevCreate ( UINT rtcDevRegBase ) { /* test if driver already installed */ if (rtcDrvNum <= 0) { (void) errnoSet (S_ioLib_NO_DRIVER); return (ERROR); } /* * if there is a device already created, don't do it */ if (rtcDv.created) return (ERROR); /* * Save the base address of the clock, * mark the device as having been created, * and add it to the I/O system. */ rtcDv.regBase = rtcDevRegBase; rtcDv.created = TRUE; return (iosDevAdd ((DEV_HDR *) &rtcDv, RTC_DEVICE, rtcDrvNum)); }/********************************************************************************* rtcOpen - open file to RTC*/LOCAL int rtcOpen ( RTC_DEV *pRtcDv ) { return ((int) pRtcDv); }/********************************************************************************* rtcClose - close file to RTC*/LOCAL int rtcClose ( RTC_DEV *pRtcDv ) { return (OK); }/********************************************************************************* rtcHook - date/time hook routine to set DOS file date and time** NOMANUAL*/void rtcHook ( DOS_DATE_TIME *pDateTime ) { /* * update local date and time structure */ rtcSpIoctl (GET_DATE, FALSE); rtcSpIoctl (GET_TIME, FALSE); /* * pass values to DOS date and time structure */ if (rtc_Year >= 70) pDateTime->dosdt_year = 2000 + rtc_Year; /* current year */ else pDateTime->dosdt_year = 1900 + rtc_Year; /* current year */ pDateTime->dosdt_month = rtc_Month; /* current month */ pDateTime->dosdt_day = rtc_DayOfMonth; /* current day */ pDateTime->dosdt_hour = rtc_Hour; /* current hour */ pDateTime->dosdt_minute = rtc_Minute; /* current minute */ pDateTime->dosdt_second = rtc_Second; /* current second */ }/********************************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -