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

📄 xsrtc.c

📁 优龙YLP270开发板 光盘自带的BIOS和实验例程源码 强烈推荐
💻 C
字号:
/******************************************************************************
**
**  COPYRIGHT (C) 2000, 2001 Intel Corporation.
**
**  This software as well as the software described in it is furnished under 
**  license and may only be used or copied in accordance with the terms of the 
**  license. The information in this file is furnished for informational use 
**  only, is subject to change without notice, and should not be construed as 
**  a commitment by Intel Corporation. Intel Corporation assumes no 
**  responsibility or liability for any errors or inaccuracies that may appear 
**  in this document or any software that may be provided in association with 
**  this document. 
**  Except as permitted by such license, no part of this document may be 
**  reproduced, stored in a retrieval system, or transmitted in any form or by 
**  any means without the express written consent of Intel Corporation. 
**
**  FILENAME:       xsrtc.c
**
**  PURPOSE:        Real Time Clock functions
**
**  LAST MODIFIED:  02/08/2001
******************************************************************************/

/*
*******************************************************************************
*   HEADER FILES
*******************************************************************************
*/
#include "systypes.h"
#include "dm_errors.h"
#include "xsIntCtrlApi.h"
#define RTC_GLOBALS
#include "xsrtc.h"
#undef RTC_GLOBALS
#include <math.h>


/*
*******************************************************************************
*   GLOBAL DEFINITIONS
*******************************************************************************
*/
volatile RTCRegsT *RTCControlRegsP = (RTCRegsT *)RTC_REGISTER_BASE;
UINT32 RTCInterruptFlag = 0;

/*
*******************************************************************************
*   LOCAL DEFINITIONS
*******************************************************************************
*/

/*
*******************************************************************************
*
* FUNCTION:         RTCSWInit
*
* DESCRIPTION:      Initialize the devices software interface
*
* INPUT PARAMETERS: void
*
* RETURNS:          void
*
* GLOBAL EFFECTS:   None
*
* ASSUMPTIONS:      None
*
* CALLS:            None
*
* CALLED BY:        DM startup procedure
*
* PROTOTYPE:        void RTCSWInit (void);
*
*******************************************************************************
*/
void RTCSWInit (void)
{
// not implemented yet. May not need to.    
}

/*
*******************************************************************************
*
* FUNCTION:         RTCHWSetup
*
* DESCRIPTION:      Setup the hardware to a reset state
*
* INPUT PARAMETERS: void
*
* RETURNS:          UINT32 - 0 means no problem
*
* GLOBAL EFFECTS:   Will reset any previous set up including a trim value that 
*                   may have been set.
*
* ASSUMPTIONS:      None
*
* CALLS:            None
*
* CALLED BY:        Anyone
*
* PROTOTYPE:        UINT32 RTCHWSetup(void);
*
*******************************************************************************
*/
UINT32 RTCHWSetup(void)
{
    UINT32 returnValue = 0;
    
    // clear the RTC count register
//    RTCControlRegsP->RCNR = 0x0;
    
    // clear the RTC Alarm register
//    RTCControlRegsP->RTAR = 0x0;
    
    // clear the RTC Status register. Note that HZ and AL are not software writeable
    // So we will mask out the software write to these bits.
//    RTCControlRegsP->RTSR = (0x0 & ~0x3);
    
    // verify that the registers were reset
//    if((RTCControlRegsP->RCNR) || (RTCControlRegsP->RTAR) ||
//       (RTCControlRegsP->RTTR != (RTCControlRegsP->RTTR & RTTR_DEFAULT_VALUE)) ||
//       (RTCControlRegsP->RTSR != (0x0 & ~0x3)));
//    {
        // handle the error
//        returnValue = ERR_FAIL_CODE;
//    }    
    
    return returnValue;    
}

/*
*******************************************************************************
*
* FUNCTION:         RTCTrim
*
* DESCRIPTION:      Loads the trim register by extracting the integer and 
*                   fractional parts of the actual crystal frequency according 
*                   to the formula cited in the EAS. This is supposed to make
*                   the accuracy of the timer better than +/- 5 seconds per 
*                   month, 
*
* INPUT PARAMETERS: Float - the actual measured frequency of the 32KHz crystal
*
* RETURNS:          UINT32 - 0 means no problem
*
* GLOBAL EFFECTS:   adjusts the accuracy of the 1Hz output 
*
* ASSUMPTIONS:      None
*
* CALLS:            None
*
* CALLED BY:        Anyone
*
* PROTOTYPE:        UINT32 RTCTrim (float);
*
*******************************************************************************
*/
UINT32 RTCTrim (UINT32 actualFreqInteger, UINT32 actualFreqFractional )
{
    UINT32 returnValue = 0;
    UINT32 integerPart;
    UINT32 fractionalPart;
    
    
    // get the values out
    integerPart = actualFreqInteger;
    fractionalPart = actualFreqFractional;
    
    // store the values in the trim register.
    RTCControlRegsP->RTTR &= ~RTTR_LCK;     // unlock the register
    RTCControlRegsP->RTTR = (UINT32) (
                                        ((1023 * (UINT32)fractionalPart) << RTTR_TDC_SHIFT) |
                                        (((UINT32)integerPart - 1) << RTTR_CDC_SHIFT)
                                     );
    // relock the register
    RTCControlRegsP->RTTR |= RTTR_LCK;
    return returnValue;
}

/*
*******************************************************************************
*
* FUNCTION:         RTCEnableAlarm
*
* DESCRIPTION:      Enables the RTC Alarm interrupt 
*
* INPUT PARAMETERS: void
*
* RETURNS:          void
*
* GLOBAL EFFECTS:   None
*
* ASSUMPTIONS:      None
*
* CALLS:            None
*
* CALLED BY:        Anyone
*
* PROTOTYPE:        void RTCEnableAlarm(void);
*
*******************************************************************************
*/
void RTCEnableAlarm(void)
{
    RTCControlRegsP->RTSR |= RTSR_ALE;
}

/*
*******************************************************************************
*
* FUNCTION:         RTCDisableAlarm
*
* DESCRIPTION:      Disables the RTC Alarm interrupt 
*
* INPUT PARAMETERS: void
*
* RETURNS:          void
*
* GLOBAL EFFECTS:   None
*
* ASSUMPTIONS:      None
*
* CALLS:            None
*
* CALLED BY:        Anyone
*
* PROTOTYPE:        void RTCDisableAlarm(void);
*
*******************************************************************************
*/
void RTCDisableAlarm(void)
{
    RTCControlRegsP->RTSR &= ~RTSR_ALE;
}

/*
*******************************************************************************
*
* FUNCTION:         RTCEnable1HzAlarm
*
* DESCRIPTION:      Enables the RTC 1 Hz Alarm interrupt 
*
* INPUT PARAMETERS: void
*
* RETURNS:          void
*
* GLOBAL EFFECTS:   None
*
* ASSUMPTIONS:      None
*
* CALLS:            None
*
* CALLED BY:        Anyone
*
* PROTOTYPE:        void RTCEnable1HzAlarm(void);
*
*******************************************************************************
*/
void RTCEnable1HzAlarm(void)
{
    RTCControlRegsP->RTSR |= RTSR_HZE;
}

/*
*******************************************************************************
*
* FUNCTION:         RTCDisable1HzAlarm
*
* DESCRIPTION:      Disables the 1 Hz RTC Alarm interrupt 
*
* INPUT PARAMETERS: void
*
* RETURNS:          void
*
* GLOBAL EFFECTS:   None
*
* ASSUMPTIONS:      None
*
* CALLS:            None
*
* CALLED BY:        Anyone
*
* PROTOTYPE:        void RTCDisable1HzAlarm(void);
*
*******************************************************************************
*/
void RTCDisable1HzAlarm(void)
{
    RTCControlRegsP->RTSR &= ~RTSR_HZE;
}

/*
*******************************************************************************
*
* FUNCTION:         RTCClear1HzAlarm
*
* DESCRIPTION:      Clears the 1 Hz RTC Alarm interrupt 
*
* INPUT PARAMETERS: void
*
* RETURNS:          void
*
* GLOBAL EFFECTS:   None
*
* ASSUMPTIONS:      None
*
* CALLS:            None
*
* CALLED BY:        Anyone
*
* PROTOTYPE:        void RTCClear1HzAlarm(void);
*
*******************************************************************************
*/
void RTCClear1HzAlarm(void)
{
    RTCControlRegsP->RTSR |= RTSR_HZ;
}

/*
*******************************************************************************
*
* FUNCTION:         RTCGeneratePulse
*
* DESCRIPTION:      This is a demo that activates the RTC and generates an
*                   interrupt for every number of integer seconds specified. 
*                   RTCHandleAlarmInterrupt is used to reset the interrupt 
*                   and display the result on the output device.
*
* INPUT PARAMETERS: UINT32 - The number of seconds between pulses 
*
* RETURNS:          UINT32 - 0 means no error
*
* GLOBAL EFFECTS:   None
*
* ASSUMPTIONS:      None
*
* CALLS:            None
*
* CALLED BY:        Anyone
*
* PROTOTYPE:        UINT32 RTCGeneratePulse (UINT32 seconds);
*
*******************************************************************************
*/

UINT32 RTCGeneratePulse (UINT32 seconds, UINT32 loopCount)
{
    UINT32 returnValue = 0;
    
    // check parameters
    if ((loopCount == 0) || (seconds == 0)) 
    {   
        LOGERROR(returnValue, ERR_L_XSRTC, ERR_RTC_GENPULSE, 
                 ERR_T_ILLPARAM, loopCount, seconds, 0);
    }
    else
    {
        do
        {
            // As a matter of safety, just unregister any previous interrupt handlers
            // for this interrupt.

            returnValue = XsIcUnRegisterHandler (XSIC_RTC_ALRM_SGNL);
            if (!returnValue)
            {
                // setup the interrupt handler
                returnValue = XsIcRegisterHandler (XSIC_RTC_ALRM_SGNL, 
                                                   (XsIcL1IntHandlerFnPT)RTCHandleAlarmInterrupt, 0);
                if(!returnValue)
                {
                    // setup the RTC registers.....
                    RTCControlRegsP->RTSR |= RTSR_ALE;  // enable the alarm
                    RTCControlRegsP->RTAR = seconds;    // set the timeout
                    RTCControlRegsP->RCNR = 0x0;        // reset the counter register
                    
                    // Spin here until the interrupt is handled.
                    while(!RTCInterruptFlag);
                                        
                    // immediately clear the flag 
                    RTCInterruptFlag = 0;
                }

            }
        } while (loopCount--);
    }
    return returnValue;
}

                                                                          /*
*******************************************************************************
*
* FUNCTION:         RTCHandleAlarmInterrupt
*
* DESCRIPTION:      This is the interrupt handler for the function 
*                   RTCGeneratePulse.
*
* INPUT PARAMETERS: void
*
* RETURNS:          void
*
* GLOBAL EFFECTS:   None
*
* ASSUMPTIONS:      None
*
* CALLS:            None
*
* CALLED BY:        The Interrupt controller 
*
* PROTOTYPE:        void RTCHandleAlarmInterrupt(void);
*
*******************************************************************************
*/

void RTCHandleAlarmInterrupt (void)
{
    // clear the interrupt reason right away to prevent any race conditions.
    // Do this bu resetting the counter register.
    RTCControlRegsP->RCNR = 0x0;        // reset the counter register
    
    // Now set the global flag so that the the pulse generating routine can
    // continue.
    
    RTCInterruptFlag = 1;
} 

⌨️ 快捷键说明

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