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

📄 timex20t.c

📁 ARM9基于WINDOWSCE的BSP源代码
💻 C
字号:
/*   The content of this file or document is CONFIDENTIAL and PROPRIETARY
*   to Jade Technologies Co., Ltd.  It is subjected to the terms of a
*   License Agreement between Licensee and Jade Technologies Co., Ltd.
*   restricting among other things, the use, reproduction, distribution
*   and transfer.  Each of the embodiments, including this information 
*   and any derivative work shall retain this copyright notice.
* 
*   Copyright (c) 2004 - 2005 Jade Technologies Co., Ltd. 
*   All rights reserved.
 * ----------------------------------------------------------------
 * File:     timex20t.c,v
 * Revision: 1.0
 * ----------------------------------------------------------------
 * $
 */

#include <windows.h>
#include <nkintr.h>
#include <oalintr.h>

#include "win_plat.h"
#include <oalfuncs.h>
#include <pl031.h>  // RTC

extern DWORD OEMCount1ms;

DWORD dwReschedIncrement;

unsigned __int64 RealTimeBias = 0;  /* Number of 100-nanosecond intervals since */
                                    /* January 1, 1601.                         */

void HalTimerInit(void)
{
    pvstRTCRegs pRTC = (pvstRTCRegs) VA_RTC_BASE;
    
    pRTC->CR = 1;           /* Set the RTC running */
}

void InitClock(void)
{
    pvstRTCRegs pRTC = (pvstRTCRegs) VA_RTC_BASE;
    
    /* InitClock() is called when a suspend mode is exited.  Be careful that 
     * debug is not included when developing power handling code, as the
     * system will not exit cleanly and it is tricky to debug. 
     */

    /* Set up timers */
    uHALr_InitTimers();
    
    /* disable RTC interrupts at the RTC */
    pRTC->IMSC = 0;

    /* initialize and start scheduler timer */
    dwReschedIncrement = RESCHED_INCREMENT;
    OEMCount1ms = OEM_COUNT_1MS;

    OEMClockFreq = OEM_CLOCK_FREQ;

    CPUSetSysTimerCount(RESCHED_PERIOD);
}

BOOL OEMGetRealTime(LPSYSTEMTIME lpst)
{
    unsigned __int64 realTime;
    FILETIME ft;
    pvstRTCRegs pRTC = (pvstRTCRegs) VA_RTC_BASE;
    DWORD dwRTCDR = pRTC->DR / TIMERSCALE;

    /* 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;
    pvstRTCRegs pRTC = (pvstRTCRegs) VA_RTC_BASE;
    DWORD dwRTCDR = pRTC->DR / TIMERSCALE;
    
    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;
    pvstRTCRegs pRTC = (pvstRTCRegs) VA_RTC_BASE;
    DWORD dwRTCDR = pRTC->DR / TIMERSCALE;
    
    /* 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->ICR = 0;                          /* Clears pending interrupts */
                pRTC->MR = dwRTCDR + deltaTime.LowPart; /* Set match reg value */
                pRTC->IMSC = 0x1;                       /* Enable RTC interrupt */
                pHALir_EnableIrq(LOGINTR_RTC);          /* Enable VIC Interrupt */
                bRet = TRUE;
            }
        }
    }

    /* return TRUE if alarm set, FALSE otherwise */
    return bRet;
}


/* EOF timex20t.c */

⌨️ 快捷键说明

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