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

📄 rtc.c

📁 S3C2443 WINCE6.0 BSP
💻 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.
//
//------------------------------------------------------------------------------
//
//  Module: rtc.c
//
//  Real-time clock (RTC) routines for the Samsung S3C2443 processor
//
#include <windows.h>
#include <ceddk.h>
#include <nkintr.h>
#include <oal.h>
#include <s3c2443.h>

//------------------------------------------------------------------------------
// Defines 

#define FROM_BCD(n)     ((((n) >> 4) * 10) + ((n) & 0xf))
#define TO_BCD(n)       ((((n) / 10) << 4) | ((n) % 10))
#define RTC_YEAR_DATUM  1980

static SYSTEMTIME defsystime = {2006,9,5,1,0,0,00,0};  //Friday, SEP 1 2006
//------------------------------------------------------------------------------
//
//  External:  g_oalRtcResetTime
//
//  RTC init time after a RTC reset has occured. It should
//  be defined in platform code.

extern SYSTEMTIME g_oalRtcResetTime;


//------------------------------------------------------------------------------
//
//  Function:  OEMGetRealTime
//
//  Reads the current RTC value and returns a system time.
//
BOOL OEMGetRealTime(SYSTEMTIME *pTime)
{
    BOOL rc = FALSE;
    S3C2443_RTC_REG *pRTCReg;
    UINT32 data;
    UINT16 seconds;


    OALMSG(OAL_RTC&&OAL_FUNC, (L"+OEMGetRealTime(pTime = 0x%x)\r\n", pTime));

    if (pTime == NULL) goto cleanUp;
    
    // Get uncached virtual address
    pRTCReg = OALPAtoVA(S3C2443_BASE_REG_PA_RTC, FALSE);
    do {
        data = INPORT32(&pRTCReg->BCDSEC) & 0x7f;
        seconds = FROM_BCD(data);
        data = INPORT32(&pRTCReg->BCDYEAR);
        pTime->wYear = FROM_BCD(data) + RTC_YEAR_DATUM;
        data = INPORT32(&pRTCReg->BCDMON) & 0x1f;
        pTime->wMonth = FROM_BCD(data);
        data = INPORT32(&pRTCReg->BCDDAY) & 0x3f;
        pTime->wDay = FROM_BCD(data);
        pTime->wDayOfWeek = (WORD)INPORT32(&pRTCReg->BCDDATE) - 1;
        data = INPORT32(&pRTCReg->BCDHOUR) & 0x3f;
        pTime->wHour = FROM_BCD(data);
        data = INPORT32(&pRTCReg->BCDMIN) & 0x7f;
        pTime->wMinute = FROM_BCD(data);
        data = INPORT32(&pRTCReg->BCDSEC) & 0x7f;
        pTime->wSecond = FROM_BCD(data);
        pTime->wMilliseconds = 0;
    } while (pTime->wSecond != seconds);

    // Done
    rc = TRUE;

cleanUp:
    //OALMSG(OAL_FUNC, (L"-OEMGetRealTime(rc = %d)\r\n", rc));
	OALMSG(OAL_FUNC, (L"."));
    return rc;
}


//------------------------------------------------------------------------------
//
//  Function:  OEMSetRealTime
//
//  Updates the RTC with the specified system time.
//
#if 1
static SYSTEMTIME defst = {2004,1,4,1,0,0,00,0}; /* Thursday, Jan 1 2004 */
#endif


BOOL OEMSetRealTime(LPSYSTEMTIME pTime) 
{
    BOOL rc = FALSE;
    S3C2443_RTC_REG *pRTCReg;
	static int atbootup = 1;

    OALMSG(OAL_RTC&&OAL_FUNC, (
        L"+OEMSetRealTime(%d/%d/%d %d:%d:%d.%03d)\r\n", 
        pTime->wYear, pTime->wMonth, pTime->wDay, pTime->wHour, pTime->wMinute,
        pTime->wSecond, pTime->wMilliseconds
    ));
    
    RETAILMSG(1, (TEXT("+OEMSetRealTime(%d/%d/%d %d:%d:%d.%03d)\r\n"), 
        pTime->wYear, pTime->wMonth, pTime->wDay, pTime->wHour, pTime->wMinute,
        pTime->wSecond, pTime->wMilliseconds
    ));
    if (pTime == NULL) goto cleanUp;
    
    if (atbootup == 1 &&
        pTime->wYear == 2003 && pTime->wMonth == 1 && pTime->wDay == 1 &&
        pTime->wHour == 12 && pTime->wMinute == 0 && pTime->wSecond == 0){
        pTime = &defsystime;
        atbootup = 0;
       }     
    // The RTC will only support a BCD year value of 0 - 99.  The year datum is
    // 1980, so any dates greater than 2079 will fail unless the datum is
    // adjusted.
    if (pTime->wYear < RTC_YEAR_DATUM || (pTime->wYear - RTC_YEAR_DATUM) > 99) {
        OALMSG(OAL_ERROR, (L"ERROR: OEMSetRealTime: "
            L"RTC cannot support a year greater than %d or less than %d "
            L"(value %d)\r\n", (RTC_YEAR_DATUM + 99), RTC_YEAR_DATUM, 
            pTime->wYear
        ));
        goto cleanUp;
    }

    // Get uncached virtual address
    pRTCReg = OALPAtoVA(S3C2443_BASE_REG_PA_RTC, FALSE);

    // Enable RTC control.
    SETREG32(&pRTCReg->RTCCON, 1);

    OUTPORT32(&pRTCReg->BCDSEC,  TO_BCD(pTime->wSecond));
    OUTPORT32(&pRTCReg->BCDMIN,  TO_BCD(pTime->wMinute));
    OUTPORT32(&pRTCReg->BCDHOUR, TO_BCD(pTime->wHour));
    OUTPORT32(&pRTCReg->BCDDATE, pTime->wDayOfWeek + 1);
    OUTPORT32(&pRTCReg->BCDDAY,  TO_BCD(pTime->wDay));
    OUTPORT32(&pRTCReg->BCDMON,  TO_BCD(pTime->wMonth));
    OUTPORT32(&pRTCReg->BCDYEAR, TO_BCD(pTime->wYear - RTC_YEAR_DATUM));

    // Disable RTC control.
    CLRREG32(&pRTCReg->RTCCON, 1);
    
    // Done
    rc = TRUE;

	RETAILMSG(1,(TEXT("-OEMSetRealTime(rc=%d)\r\n"),rc));

cleanUp:
    OALMSG(OAL_RTC&&OAL_FUNC, (L"-OEMSetRealTime(rc = %d)\r\n", rc));
    return rc;
}


//------------------------------------------------------------------------------
//
//  Function:  OEMSetAlarmTime
//
//  Set the RTC alarm time.
//
BOOL OEMSetAlarmTime(SYSTEMTIME *pTime)
{
    BOOL rc = FALSE;
    S3C2443_RTC_REG *pRTCReg;
    UINT32 irq;

    OALMSG(OAL_RTC&&OAL_FUNC, (
        L"+OEMSetAlarmTime(%d/%d/%d %d:%d:%d.%03d)\r\n", 
        pTime->wMonth, pTime->wDay, pTime->wYear, pTime->wHour, pTime->wMinute,
        pTime->wSecond, pTime->wMilliseconds
    ));
    
    RETAILMSG(1,(TEXT("+OEMSetAlarmTime(%d/%d/%d %d:%d:%d.%03d)\r\n"), 
        pTime->wMonth, pTime->wDay, pTime->wYear, pTime->wHour, pTime->wMinute,
        pTime->wSecond, pTime->wMilliseconds
    ));

    if (pTime == NULL) goto cleanUp;
    
    // Get uncached virtual address
    pRTCReg = OALPAtoVA(S3C2443_BASE_REG_PA_RTC, FALSE);
    
    // Enable RTC control
    SETREG32(&pRTCReg->RTCCON, 1);

    OUTPORT32(&pRTCReg->ALMSEC,  TO_BCD(pTime->wSecond));
    OUTPORT32(&pRTCReg->ALMMIN,  TO_BCD(pTime->wMinute));
    OUTPORT32(&pRTCReg->ALMHOUR, TO_BCD(pTime->wHour));
    OUTPORT32(&pRTCReg->ALMDATE, TO_BCD(pTime->wDay));
    OUTPORT32(&pRTCReg->ALMMON,  TO_BCD(pTime->wMonth));
    OUTPORT32(&pRTCReg->ALMYEAR, TO_BCD(pTime->wYear - RTC_YEAR_DATUM));
   
    // Enable the RTC alarm interrupt
    OUTPORT32(&pRTCReg->RTCALM, 0x7F);
 
    // Disable RTC control.
    CLRREG32(&pRTCReg->RTCCON, 1);

    // Enable/clear RTC interrupt
    irq = IRQ_RTC;
    OALIntrDoneIrqs(1, &irq);

    // Done
    rc = TRUE;
    
cleanUp:
    OALMSG(OAL_RTC&&OAL_FUNC, (L"-OEMSetAlarmTime(rc = %d)\r\n", rc));
    return rc;
}

//------------------------------------------------------------------------------

⌨️ 快捷键说明

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