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

📄 drvtimer.c

📁 mstar 776 开发的车载dvd
💻 C
字号:
////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2006-2007 MStar Semiconductor, Inc.
// All rights reserved.
//
// Unless otherwise stipulated in writing, any and all information contained
// herein regardless in any format shall remain the sole proprietary of
// MStar Semiconductor Inc. and be kept in strict confidence
// (¨MStar Confidential Information〃) by the recipient.
// Any unauthorized act including without limitation unauthorized disclosure,
// copying, use, reproduction, sale, distribution, modification, disassembling,
// reverse engineering and compiling of the contents of MStar Confidential
// Information is unlawful and strictly prohibited. MStar hereby reserves the
// rights to any and all damages, losses, costs and expenses resulting therefrom.
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
///@file Drvtimer.h
///@brief Driver interface of Timer
///
///@author MStarSemi Inc.
///
///- This is the Timer module, providing general timer functions and system clock tick.
///
///@note Please make sure the "timer clock is well defined.
////////////////////////////////////////////////////////////////////////////////

#include <intrins.h>
#include "datatype.h"
#include "mreg51.h"
#include "drvtimer.h"
#include "sysinfo.h"
#include "sramvar.h"
#include "drvISR.h"
#include "drvGlobal.h"
#include "hwreg_S2.h"

/******************************************************************************/
///Initial and enable timer2
/******************************************************************************/
void MDrv_XC51_Timer2_Init(void)
{
    T2CON = 0x11;

    // MCU run in XTAL
    if((XBYTE[REG_CT_MISC0] & MISC_MCU_CLKSEL_MASK) == MISC_MCU_CLKSEL_DFT)
    {
        CRCH = (ISR_TIMER2_COUNTER_XTAL/256);
        CRCL = (ISR_TIMER2_COUNTER_XTAL%256);
        TH2  = (ISR_TIMER2_COUNTER_XTAL/256);
        TL2  = (ISR_TIMER2_COUNTER_XTAL%256);
    }
    else
    {
        CRCH = (ISR_TIMER2_COUNTER/256);
        CRCL = (ISR_TIMER2_COUNTER%256);
        TH2  = (ISR_TIMER2_COUNTER/256);
        TL2  = (ISR_TIMER2_COUNTER%256);
    }

    TF2 = 0;
    ET2 = 1;
}
/********************************************************************************/
/// Initial and enable Timer counts. After initialize, the system tick increases while interrupt happens.
/********************************************************************************/
void MDrv_Timer_Init0() // Now support mode 1
{
    // INT0 setting
    IT0 = EXINTR0_EDGE_TRIGGERED ? 1 : 0;

    // INT1 setting
    IT1 = EXINTR1_EDGE_TRIGGERED ? 1 : 0;

    TMOD = (TMOD & 0xF0) | 0x01; // set TIMER0 mode 1

    TH0 = (ISR_TIMER0_COUNTER/256);
    TL0 = (ISR_TIMER0_COUNTER%256);

    #ifndef USE_TIMER0_FOR_SYSTEM_TIME
    // PIU timer1 -> EX3
    MDrv_Write4Byte(REG_TIMER1_MAX, MST_XTAL_CLOCK_HZ);
    MDrv_WriteByte(REG_TIMER1_CTRL + 1, 0x03);
    #endif

    TR0 = 1;      // TIMER0 enable

    gTimerCount0 = 0;
    gTimerDownCount0 = 0;
    gTimerDownCount1 = 0;
    gu8100msTH = 0;

    #ifdef USE_TIMER0_FOR_SYSTEM_TIME   // remove later
    gu16SystemTimePeriodTH = 0;
    #endif
}
/******************************************************************************/
/// Get system time.
/// @return -Current system time counts
///
/// The unit depends on the "period" which is setting by USER_TIMER_PERIOD
/******************************************************************************/
U32 MDrv_Timer_GetSystemTime0()
{
    return gSystemTimeCount;
}

/******************************************************************************/
/// Get Wakeup time.
/// @return -User setting wake up time
/******************************************************************************/
U32 MDrv_Timer_GetWakeupTime()
{
    return gWakeupSystemTime;
}

/******************************************************************************/
/// Set system time.
/// @param -u32SystemTime \b IN  System time to set
///
/// The unit is depends on the "period" which is setting by USER_TIMER_PERIOD
/******************************************************************************/
void MDrv_Timer_SetSystemTime0(U32 u32SystemTime)
{
    gSystemTimeCount = u32SystemTime;
}

/******************************************************************************/
/// Set Wakeup time.
/// @param -u32Time \b IN  Wake up time to set
/******************************************************************************/
void MDrv_Timer_SetWakeupTime(U32 u32Time)
{
    gWakeupSystemTime = u32Time;
}

/******************************************************************************/
/// Get system time ticks
/// @return -Current system time counts, the unit is system default.
/******************************************************************************/
U32 MDrv_Timer_GetTime0()
{
    U32 ret;

    UD = _testbit_(ET0);
    ret = gTimerCount0;
    if (UD)
        ET0 = 1;
    return ret;
}


/******************************************************************************/
/// Get system downtimer ticks
/// @return -Current system time counts, the unit is system default.
/******************************************************************************/
U32 MDrv_Timer_GetDownTimer0(void)
{
    U32 ret;

    UD = _testbit_(ET0);
    ret = gTimerDownCount0;
    if (UD)
        ET0 = 1;
    return ret;
}

U32 MDrv_Timer_GetDownTimer1(void)
{
    U32 ret;

    UD = _testbit_(ET0);
    ret = gTimerDownCount1;
    if (UD)
        ET0 = 1;
    return ret;
}

/******************************************************************************/
/// Set system downtimer ticks
/// @return -Current system time counts, the unit is system default.
/******************************************************************************/
void MDrv_Timer_SetDownTimer0(U32 u32Timer)
{
    gTimerDownCount0 = u32Timer;
}

void MDrv_Timer_SetDownTimer1(U32 u32Timer)
{
    gTimerDownCount1 = u32Timer;
}

/******************************************************************************/
/*
U32 MDrv_Timer_CalPeriodCount(U32 ms)
{
    return ms / TIMER_PERIOD;
}
*/
/******************************************************************************/
/// Calculating the difference of two time values. This function will handle overflow problem.
/// @param -u32Timer \b IN  First time value to calculate
/// @param -u32TaskTimer \b IN  2nd time value to calculate
/// @return -Time difference of 2 time values.
/******************************************************************************/
U32 MDrv_Timer_TimeDifference(U32 u32Timer, U32 u32TaskTimer) //unit = ms
{
    // simple minus is enough, because max(timer_counter) == max(u32)
    return (u32Timer - u32TaskTimer);
}

/******************************************************************************/
/// Calculating the difference between time in parameter and current time.
/// @param -u32TaskTimer \b IN  2nd time value to calculate
/// @return -Time difference between current time and task time
/******************************************************************************/
U32 MDrv_Timer_TimeDifferenceFromCurrent(U32 u32TaskTimer) //unit = ms
{
    return (MDrv_Timer_GetTime0() - u32TaskTimer);
}

/********************************************************************************/
/// Delay function
/// @param -u32DelayTime \b IN  required time period to delay.
/********************************************************************************/
void MDrv_Timer_Delayms(U32 u32DelayTime) //unit = ms
{
#if 0   // temporary use PIU timer0 to avoid infinite loop when ISR is blocked
    #define MAX_MS_IN_ONE_SHOT  (-1UL / MST_XTAL_CLOCK_HZ)

    IEX2 = 0;
    while (u32DelayTime > MAX_MS_IN_ONE_SHOT)
    {
        MDrv_Write4Byte(REG_TIMER0_MAX, MAX_MS_IN_ONE_SHOT * MST_XTAL_CLOCK_HZ / 1000);
        MDrv_WriteByte(REG_TIMER0_CTRL + 1, 0x03);
        while (!IEX2) ;
        MDrv_WriteByte(REG_TIMER0_CTRL + 1, 0x00);
        IEX2 = 0;
        u32DelayTime -= MAX_MS_IN_ONE_SHOT;
    }

    if (u32DelayTime)
    {
        MDrv_Write4Byte(REG_TIMER0_MAX, u32DelayTime * MST_XTAL_CLOCK_HZ / 1000);
        MDrv_WriteByte(REG_TIMER0_CTRL + 1, 0x03);
        while (!IEX2) ;
        MDrv_WriteByte(REG_TIMER0_CTRL + 1, 0x00);
        IEX2 = 0;
    }
#else
    U32 u32TempTimer;

    if (u32DelayTime)// kevin
        u32DelayTime--;

    if ( TR0 )
    {
        u32TempTimer= MDrv_Timer_GetTime0();
        while ( MDrv_Timer_TimeDifference( MDrv_Timer_GetTime0(), u32TempTimer ) <= u32DelayTime );
    }
    else  //it only happan in initial running in xtal.
    {
        for( u32TempTimer=0; u32TempTimer < (200 * u32DelayTime); u32TempTimer++);
    }
#endif
}


⌨️ 快捷键说明

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