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

📄 rtc.c

📁 i.mx27 soc for wince 6.0
💻 C
📖 第 1 页 / 共 2 页
字号:
//
// 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.
//
//------------------------------------------------------------------------------
//
//  Copyright (C) 2004-2006, Freescale Semiconductor, Inc. All Rights Reserved.
//  THIS SOURCE CODE, AND ITS USE AND DISTRIBUTION, IS SUBJECT TO THE TERMS
//  AND CONDITIONS OF THE APPLICABLE LICENSE AGREEMENT
//
//------------------------------------------------------------------------------
//
//  Module: rtc.c
//
//  PQOAL Real-time clock (RTC) routines for the MC13783 PMIC RTC.
//
//------------------------------------------------------------------------------
#include <windows.h>
#include <oal.h>
#include "nkintr.h"
#include "regs.h"
#include "regs_rtc.h"

#include "pmic_ioctl.h"
#include "regs_regulator.h"
#include "pmic_basic_types.h"
#include "pmic_lla.h"


//-----------------------------------------------------------------------------
// External Functions
extern BOOL OALPmicRead(UINT32 addr, PUINT32 pData);
extern BOOL OALPmicWrite(UINT32 addr, UINT32 data);
extern BOOL OALPmicWriteMasked(UINT32 addr, UINT32 mask, UINT32 data);
extern BOOL OALIoCtlHalUnforceIrq(UINT32 code, VOID *pInpBuffer,
                                  UINT32 inpSize, VOID *pOutBuffer,
                                  UINT32 outSize, UINT32 *pOutSize);

//-----------------------------------------------------------------------------
// Global Variables

extern UINT32 g_IRQ_RTC;

//------------------------------------------------------------------------------
// Global Variables
//These macro define some default information of RTC
#define ORIGINYEAR       1980                  // the begin year
#define MAXYEAR          (ORIGINYEAR + 100)    // the maxium year
#define JAN1WEEK         2                     // Jan 1 1980 is a Tuesday
#define GetDayOfWeek(X) (((X-1)+JAN1WEEK)%7)

#define TYPE_TIME        0
#define TYPE_ALRM        1

static const UINT8 monthtable[12] = { 31, 28, 31, 30, 31, 30,
                                      31, 31, 30, 31, 30, 31
                                    };
static const UINT8 monthtable_leap[12] = { 31, 29, 31, 30, 31, 30,
                                           31, 31, 30, 31, 30, 31
                                         };

BOOL OEMSetRealTime(LPSYSTEMTIME lpst);
BOOL OEMGetRealTime(LPSYSTEMTIME lpst);
BOOL CheckRealTime(LPSYSTEMTIME lpst);

// Note that, since MC13783 RTC only support up to 32767 days, for simplicity, we just calculate up to year. 
// Not going down to day and month.
// From calculation, the last day should be 2069.9.16 but we just set anything after 2068 as invalid
#define MAX_DAY_COUNTER     32767
DWORD gdwMaxYear = MAXYEAR;

static int IsLeapYear(int Year);

static void FindMaxDate(void)
{
    int dayCount = MAX_DAY_COUNTER;
    int year = ORIGINYEAR;
    while (dayCount > 365)
    {
        if (IsLeapYear(year))
            dayCount = dayCount - 366;
        else
            dayCount = dayCount - 365;
        year++;
    }

    gdwMaxYear = year-1;    
    OALMSG(1, (TEXT("From %d to %d\r\n"), ORIGINYEAR, gdwMaxYear));


}
//------------------------------------------------------------------------------
//
//  Function:  OALIoCtlHalInitRTC
//
//  This function is called by WinCE OS to initialize the time after boot.
//  Input buffer contains SYSTEMTIME structure with default time value. If
//  hardware has persistent real time clock it will ignore this value
//  (or all call).
//
//------------------------------------------------------------------------------
BOOL OALIoCtlHalInitRTC( UINT32 code, VOID *pInpBuffer, UINT32 inpSize,
                         VOID *pOutBuffer, UINT32 outSize, UINT32 *pOutSize)
{
    BOOL rc = FALSE;
    UINT32 tempISR1;
    SYSTEMTIME *pTime = (SYSTEMTIME*)pInpBuffer;

    OALMSG(OAL_IOCTL && OAL_FUNC, (L"+OALIoCtlHalInitRTC(...)\r\n"));

    // Validate inputs
    if (pInpBuffer == NULL || inpSize < sizeof(SYSTEMTIME)) {
        NKSetLastError(ERROR_INVALID_PARAMETER);
        OALMSG(OAL_ERROR, (
            L"ERROR: OALIoCtlHalInitRTC: Invalid parameter\r\n"
        ));
        goto cleanUp;
    }

    FindMaxDate();

    // Add static mapping for RTC alarm
    OALIntrStaticTranslate(SYSINTR_RTC_ALARM, g_IRQ_RTC);

    OALPmicRead(MC13783_INT_STAT1_ADDR, &tempISR1);

    // Check if we need to reinitialize the timer. Otherwise, don't
    // do anything since the RTC is still valid.
    if ( tempISR1 & MC13783_RTCRSTI_MASK )
    {
        // Set time
        rc = OEMSetRealTime(pTime);

        // clear the status bit
        OALPmicWriteMasked(MC13783_INT_STAT1_ADDR,MC13783_RTCRSTI_MASK,
                           tempISR1);
    }

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

//-----------------------------------------------------------------------------
// Local Functions

//------------------------------------------------------------------------------
//
// Function: IsLeapYear
//
// Local helper function checks if the year is a leap year
//
// Parameters:
//
// Returns:
//
//
//------------------------------------------------------------------------------
static int IsLeapYear(int Year)
{
    int Leap;

    Leap = 0;
    if ((Year % 4) == 0) {
        Leap = 1;
        if ((Year % 100) == 0) {
            Leap = (Year%400) ? 0 : 1;
        }
    }

    return (Leap);
}

//------------------------------------------------------------------------------
//
// Function: CalculateDays
//
// Local helper function calculate the total number of days in lpTime,
// since Jan 1, ORIGINYEAR
//
// Parameters:
//
// Returns:
//      days
//
//------------------------------------------------------------------------------
UINT32 CalculateDays(SYSTEMTIME* lpTime)
{
    UINT8 *month_tab;
    int days, year, month;
    int i;

    days = lpTime->wDay;
    month = lpTime->wMonth;
    year = lpTime->wYear;

    // Calculate number of days spent so far from beginning of this year
    month_tab = (UINT8 *)(IsLeapYear(year) ? monthtable_leap : monthtable);

    for (i = 0; i < month - 1; i++) 
    {
        days += month_tab[i];
    }

    // calculate the number of days in the previous years
    for (i = ORIGINYEAR; i < year; i++)
    {
        days += (IsLeapYear(i) ? 366 : 365);
    }

    return days;
}

//------------------------------------------------------------------------------
//
// Function: CalculateSeconds
//
// Local helper function that calculates the number of seconds in lpTime since
// the beginning of the day.
//
// Parameters:
//
// Returns:
//      seconds
//
//------------------------------------------------------------------------------
UINT32 CalculateSeconds(SYSTEMTIME* lpTime)
{
    return (lpTime->wHour * 60 * 60 + lpTime->wMinute * 60 + lpTime->wSecond);
}

//------------------------------------------------------------------------------
//
// Function: ConvertDays
//
// Local helper function that split total days since Jan 1, ORIGINYEAR into
// year, month and day.
//
// Parameters:
//
// Returns:
//      Returns TRUE if successful, otherwise returns FALSE.
//
//------------------------------------------------------------------------------
BOOL ConvertDays(UINT32 days, SYSTEMTIME* lpTime)
{
    int dayofweek, month, year;
    UINT8 *month_tab;

    //Calculate current day of the week
    dayofweek = GetDayOfWeek(days);

    year = ORIGINYEAR;


    while (days > 365)
    {
        if (IsLeapYear(year))
        {
            if (days > 366)
            {
                days -= 366;
                year += 1;
            }

⌨️ 快捷键说明

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