📄 realtimeclock.c
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
/* -*-C-*-
*
* $Revision: 1.2 $
* $Author: rgorsegn $
* $Date: 2002/07/08 19:41:00 $
*
* Copyright (c) 1999 ARM Limited
* All Rights Reserved
*
* timex20t.c - ARMX20T (X = 7, 9, 10) platform timer routines
*/
#include <windows.h>
#include <nkintr.h>
#include <hwdefs.h>
DWORD dwReschedIncrement;
unsigned __int64 RealTimeBias = 0; // Number of 100-nanosecond intervals since
// January 1, 1601.
typedef volatile struct _rtcif_tag
{
DWORD dr; // data register
DWORD mr; // match register
union {
DWORD stat; // interrupt status (read only)
DWORD eoi; // interrupt clear (write only)
};
DWORD lr; // load register
DWORD cr; // control register
} RTCIF, *PRTCIF;
BOOL OEMGetRealTime(LPSYSTEMTIME lpst)
{
unsigned __int64 realTime;
FILETIME ft;
//
// Implement the realtime clock.
//
PRTCIF pRTC = (PRTCIF) RTC_BASE;
DWORD dwRTCDR = pRTC->dr;
//
// read the clock registers
//
realTime = (unsigned __int64) dwRTCDR; // data in seconds
realTime *= 1000; // convert to ms
realTime *= 10000; // convert to 100ns
realTime += RealTimeBias; // convert to "real" time
//
// load time/data structure
//
ft.dwLowDateTime = (DWORD)realTime;
ft.dwHighDateTime = (DWORD)(realTime >> 32);
return KFileTimeToSystemTime( &ft, lpst );
}
BOOL OEMSetRealTime(LPSYSTEMTIME lpst)
{
unsigned __int64 realTime;
FILETIME ft;
BOOL bRet;
//
// Impliment battery backed real time clock.
//
//
PRTCIF pRTC = (PRTCIF) RTC_BASE;
DWORD dwRTCDR = pRTC->dr;
if (bRet = KSystemTimeToFileTime(lpst, &ft))
{
// read the clock registers
realTime = (unsigned __int64) dwRTCDR; // data in seconds
realTime *= 1000; // convert to ms
realTime *= 10000; // convert to 100ns
// get the desired "real" time
RealTimeBias = (unsigned __int64) ft.dwHighDateTime << 32;
RealTimeBias += ft.dwLowDateTime;
// compensate for the clock time
RealTimeBias -= realTime;
}
return bRet;
}
BOOL OEMSetAlarmTime(LPSYSTEMTIME lpst)
{
FILETIME ft;
BOOL bRet = FALSE;
ULARGE_INTEGER alarmTime, currentTime, deltaTime;
PRTCIF pRTC = (PRTCIF) RTC_BASE;
DWORD dwRTCDR = pRTC->dr;
// get the desired alarm time
if (bRet = KSystemTimeToFileTime(lpst, &ft)) {
alarmTime.LowPart = ft.dwLowDateTime;
alarmTime.HighPart = ft.dwHighDateTime;
alarmTime.QuadPart -= RealTimeBias;
// get the current time
currentTime.QuadPart = (unsigned __int64) dwRTCDR; // data in seconds
currentTime.QuadPart *= 1000; // convert to ms
currentTime.QuadPart *= 10000; // convert to 100ns
// make sure the alarm occurs in the future
if(alarmTime.QuadPart < currentTime.QuadPart) {
DEBUGMSG(FALSE, (_T("OEMSetAlarmTime: alarm 0x%08x:%08x occurs before 0x%08x:%08x\r\n"),
alarmTime.HighPart, alarmTime.LowPart, currentTime.HighPart, currentTime.LowPart));
} else {
// round up to the nearest number of seconds
deltaTime.QuadPart = alarmTime.QuadPart - currentTime.QuadPart;
deltaTime.QuadPart += 9999999; // msecs, usecs, 100ns
deltaTime.QuadPart /= 10000000; // convert to seconds
// do we have enough resolution in our timer to handle the request?
if(deltaTime.HighPart != 0) {
DEBUGMSG(FALSE, (_T("OEMSetAlarmTime: alarm 0x%08x:%08x delta with 0x%08x:%08x (0x%08x:%08x) is too large\r\n"),
alarmTime.HighPart, alarmTime.LowPart, currentTime.HighPart, currentTime.LowPart, deltaTime.HighPart, deltaTime.LowPart));
} else {
// clear interrupts, write the comparator, and enable interrupts
pRTC->eoi = 0; // any value clears pending interrupts
pRTC->mr = dwRTCDR + deltaTime.LowPart;
pRTC->cr = 1; // enable match interrupt
OEMInterruptEnable(SYSINTR_RTC_ALARM, NULL, 0);
bRet = TRUE;
}
}
}
// return TRUE if alarm set, FALSE otherwise
return bRet;
}
/* EOF timex20t.c */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -