S3C6410 ADS1.2 下裸奔 3----RTC驱动代码

一、RTC寄存器定义

//
// 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.
//
//------------------------------------------------------------------------------
//
//  Header: s3c6410_rtc.h
//
//  Defines the Real Time Clock (RTC) register layout and associated
//  types and constants.
//
#ifndef __S3C6410_RTC_H__
#define __S3C6410_RTC_H__

#define RTC_BASE_REG_ADDR            0x7E005000
//------------------------------------------------------------------------------
//
//  Type: S3C6410_RTC_REG
//
//  RTC control registers. This register bank is located by the constant
//  S3C6410_BASE_REG_XX_RTC in the configuration file s3c6410_base_reg_cfg.h.
//

typedef struct
{
    uint32 PAD0[12];        // 0x00~0x2f

    uint32 INTP;            // 0x30
    uint32 PAD1[3];        // 0x34~0x3f

    uint32 RTCCON;        // 0x40
    uint32 TICCNT;        // 0x44
    uint32 PAD2[2];        // 0x48~0x4f

    uint32 RTCALM;        // 0x50
    uint32 ALMSEC;        // 0x54
    uint32 ALMMIN;        // 0x58
    uint32 ALMHOUR;    // 0x5c

    uint32 ALMDATE;        // 0x60
    uint32 ALMMON;        // 0x64
    uint32 ALMYEAR;        // 0x68
    uint32 PAD3;        // 0x6c

    uint32 BCDSEC;        // 0x70
    uint32 BCDMIN;        // 0x74
    uint32 BCDHOUR;        // 0x78
    uint32 BCDDATE;        // 0x7c

    uint32 BCDDAY;        // 0x80
    uint32 BCDMON;        // 0x84
    uint32 BCDYEAR;        // 0x88
    uint32 PAD4;        // 0x8c

    uint32 CURTICCNT;    // 0x90
    uint32 RTCLVD;        // 0x94
    uint32 PAD5[2];        // 0x98~0x9f
} RTC_REGS, *PRTC_REGS;


typedef struct _SYSTEMTIME {
    uint16 wYear;
    uint16 wMonth;
    uint16 wDayOfWeek;
    uint16 wDay;
    uint16 wHour;
    uint16 wMinute;
    uint16 wSecond;
    uint16 wMilliseconds;
} SYSTEMTIME, *LPSYSTEMTIME;
//------------------------------------------------------------------------------

 

#endif

 

二、RTC驱动代码(From WinCE6.0 BSP)

#include "..\inc\config.h"

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


//------------------------------------------------------------------------------
//
//  Function:  OEMGetRealTime
//
//  Reads the current RTC value and returns a system time.
//
BOOL OEMGetRealTime(SYSTEMTIME *pTime)
{
    volatile RTC_REGS *pRTCReg;
    uint16 seconds;

    if (!pTime) return FALSE;

    // Get uncached virtual address
    pRTCReg = (RTC_REGS *)RTC_BASE_REG_ADDR;

    // Enable RTC control.
    pRTCReg->RTCCON |= (1<<0);

    do
    {
        seconds = FROM_BCD(pRTCReg->BCDSEC & 0x7f);
        pTime->wYear        = (pRTCReg->BCDYEAR & 0xff) + RTC_YEAR_DATUM;    // BCDYEAR not contains BCD number
        pTime->wMonth        = FROM_BCD(pRTCReg->BCDMON & 0x1f);
        pTime->wDay        = FROM_BCD(pRTCReg->BCDDATE & 0x3f);
        pTime->wDayOfWeek    = pRTCReg->BCDDAY - 1;
        pTime->wHour        = FROM_BCD(pRTCReg->BCDHOUR & 0x3f);
        pTime->wMinute        = FROM_BCD(pRTCReg->BCDMIN & 0x7f);
        pTime->wSecond        = FROM_BCD(pRTCReg->BCDSEC & 0x7f);
        pTime->wMilliseconds    = 0;
    } while (pTime->wSecond != seconds);

    // Disable RTC control.
    pRTCReg->RTCCON &= ~(1<<0);

    // Done
    return TRUE;
}


//------------------------------------------------------------------------------
//
//  Function:  OEMSetRealTime
//
//  Updates the RTC with the specified system time.
//
BOOL OEMSetRealTime(LPSYSTEMTIME pTime)
{
    BOOL rc = FALSE;
    volatile RTC_REGS *pRTCReg;

    if (!pTime) return FALSE;

    // Get uncached virtual address
    pRTCReg = (RTC_REGS *)RTC_BASE_REG_ADDR;

    // Enable RTC control.
    pRTCReg->RTCCON |= (1<<0);

    pRTCReg->BCDSEC    = TO_BCD(pTime->wSecond);
    pRTCReg->BCDMIN    = TO_BCD(pTime->wMinute);
    pRTCReg->BCDHOUR    = TO_BCD(pTime->wHour);
    pRTCReg->BCDDATE    = TO_BCD(pTime->wDay);
    pRTCReg->BCDDAY    = pTime->wDayOfWeek + 1;
    pRTCReg->BCDMON    = TO_BCD(pTime->wMonth);
    pRTCReg->BCDYEAR    = (pTime->wYear - RTC_YEAR_DATUM) & 0xff;    // BCDYEAR not contains BCD number

    // Disable RTC control.
    pRTCReg->RTCCON &= ~(1<<0);

    return TRUE;
}

 

你可能感兴趣的:(S3C6410 ADS1.2 下裸奔 3----RTC驱动代码)