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

📄 drvtimer.c

📁 cortex-m0 LCD1602程序
💻 C
📖 第 1 页 / 共 5 页
字号:
/*---------------------------------------------------------------------------------------------------------*/
/*                                                                                                         */
/* Copyright(c) 2009 Nuvoton Technology Corp. All rights reserved.                                         */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/
 
/*---------------------------------------------------------------------------------------------------------*/
/* Includes of system headers                                                                              */
/*---------------------------------------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------------------------------------*/
/* Includes of local headers                                                                               */
/*---------------------------------------------------------------------------------------------------------*/
#include "DrvTIMER.h"
#include "DrvSYS.h"
#include "DrvGPIO.h"

/*---------------------------------------------------------------------------------------------------------*/
/* Macro, type and constant definitions                                                                    */
/*---------------------------------------------------------------------------------------------------------*/
#define TIMER_EVENT_COUNT   1
const uint32_t CH_OFFSET[] = {0x0, 0x20, 0x100000, 0x100020};


/*---------------------------------------------------------------------------------------------------------*/
/* Global file scope (static) variables                                                                    */
/*---------------------------------------------------------------------------------------------------------*/
static TIMER_EVENT_T tTime0Event[TIMER_EVENT_COUNT],
                     tTime1Event[TIMER_EVENT_COUNT],
                     tTime2Event[TIMER_EVENT_COUNT],
                     tTime3Event[TIMER_EVENT_COUNT];

int32_t volatile bIsTimer0Initial     = FALSE,
                 bIsTimer1Initial     = FALSE,
                 bIsTimer2Initial     = FALSE,
                 bIsTimer3Initial     = FALSE,
                 bIsTimer0Used        = FALSE,
                 bIsTimer1Used        = FALSE,
                 bIsTimer2Used        = FALSE,
                 bIsTimer3Used        = FALSE,
                 bIsSetTime0Event     = FALSE,
                 bIsSetTime1Event     = FALSE,
                 bIsSetTime2Event     = FALSE,
                 bIsSetTime3Event     = FALSE;

static uint32_t volatile uTimer0Tick = 0,
                         uTimer1Tick = 0,
                         uTimer2Tick = 0,
                         uTimer3Tick = 0,
                         uTime0EventCount = 0,
                         uTime1EventCount = 0,
                         uTime2EventCount = 0,
                         uTime3EventCount = 0;

static uint32_t volatile _sys_uTimer0TickPerSecond,
                         _sys_uTimer1TickPerSecond,
                         _sys_uTimer2TickPerSecond, 
                         _sys_uTimer3TickPerSecond;

uint32_t volatile u32EXTClockFreq = 12000000;

static WDT_CALLBACK fnCallBack_WDT;

/*---------------------------------------------------------------------------------------------------------*/
/* Function:        GetTimerClock                                                                          */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*                  ch - [in]                                                                              */
/*                      E_TIMER_CHANNEL, it could be E_TMR0/E_TMR1/E_TMR2/E_TMR3                           */   
/* Returns:                                                                                                */   
/*                  u32clk                                  The timer clock (Hz)                           */
/*                  E_DRVTIMER_CHANNEL                      Invalid Timer channel                          */
/*                  E_DRVTIMER_CLOCK_RATE                   Invalid Timer clock source                     */
/* Description:                                                                                            */
/*                  Get the timer clock from the specified timer channel.                                  */
/*---------------------------------------------------------------------------------------------------------*/
static uint32_t GetTimerClock(E_TIMER_CHANNEL ch)
{
    volatile uint8_t u8ClockSrc;
    volatile uint32_t u32clk = 0;
        
    if ((ch == E_TMR0) || (ch == E_TMR1) || (ch == E_TMR2) || (ch == E_TMR3))
    {
        u8ClockSrc = (inpw(&SYSCLK->CLKSEL1) >> (8+(ch*4))) & 0x7;

        if (u8ClockSrc == 0)           
        {
            u32clk = DrvSYS_GetExtClockFreq() ;     /* Option 0: Get External Clock From DrvSYS Setting */
        }
        else if(u8ClockSrc == 1)
        {
            u32clk = __RTC_XTAL;                    /* Option 1: 32K */
        }
        else if(u8ClockSrc == 2)
        {
            u32clk = DrvSYS_GetHCLKFreq();          /* Option 2: HCLK */
        }
        else if(u8ClockSrc == 7)
        {
            u32clk = __IRC22M;                      /* Option 7: 22MHz*/
        }else
        {
            return E_DRVTIMER_CLOCK_RATE;
        }
    }
    else 
        return E_DRVTIMER_CHANNEL;
            
    return u32clk;
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function:        CalTimerInitValue                                                                      */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*                  u32ClockValue - [in]                                                                   */
/*                      Input the clock value of Timer                                                     */
/*                  u32TicksPerSec - [in]                                                                  */
/*                      Specify the ticks per second of Timer                                              */
/* Returns:                                                                                                */
/*                  Return 32 bits unsigned integer where                                                  */
/*                          bits [ 0:23] - The Timer Compare Value(TCMPR) for Timer                        */
/*                          bits [24:31] - The pre-scale value for Timer                                   */
/*                  (uint32_t)-1                            Out of range                                   */
/* Description:                                                                                            */
/*                  Calculate the Timer Compare Value and pre-scale value for Timer                        */
/*---------------------------------------------------------------------------------------------------------*/
static uint32_t CalTimerInitValue(uint32_t u32ClockValue, uint32_t u32TicksPerSec)
{
    uint32_t u32PreScale;
    uint32_t u32TCMPRValue;

    if ((u32ClockValue < 2) || (u32TicksPerSec == 0))
        return (uint32_t)-1;

    for (u32PreScale=1; u32PreScale<256; u32PreScale++)
    {
        u32TCMPRValue = u32ClockValue / (u32TicksPerSec * u32PreScale);

        /* The TCMPR value must > 1 */
        if ((u32TCMPRValue > 1) && (u32TCMPRValue < 0x1000000))
            return (((u32PreScale-1) << 24) | u32TCMPRValue);
    }
    
    return (uint32_t)-1;
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function:        TMR0_IRQHandler                                                                        */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*                  None                                                                                   */
/* Returns:                                                                                                */
/*                  None                                                                                   */
/* Description:                                                                                            */
/*                  The TIMER0 default IRQ, declared in startup_NUC1xx.s                                   */
/*---------------------------------------------------------------------------------------------------------*/
void TMR0_IRQHandler(void)
{
    int32_t i;
   
    if ((TIMER0->TCSR.IE == 1) && (TIMER0->TISR.TIF == 1))
        TIMER0->TISR.TIF = 1;

    if ((TIMER0->TEXCON.TEXEN == 1) && (TIMER0->TEXCON.TEXIEN == 1) && (TIMER0->TEXISR.TEXIF == 1))
        TIMER0->TEXISR.TEXIF = 1;

    uTimer0Tick++;
    if (bIsSetTime0Event)                       /* Timer Event Handle */
    {
        for (i=0; i<TIMER_EVENT_COUNT; i++)
        {
            if (tTime0Event[i].active)
            {
                tTime0Event[i].curTick--;
                if (tTime0Event[i].curTick == 0)
                {
                    (*tTime0Event[i].funPtr)(tTime0Event[i].transParam);
                    tTime0Event[i].curTick = tTime0Event[i].initTick;
                }
            }
        }
    }
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function:        TMR1_IRQHandler                                                                        */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*                  None                                                                                   */
/* Returns:                                                                                                */
/*                  None                                                                                   */
/* Description:                                                                                            */
/*                  The TIMER1 default IRQ, declared in startup_NUC1xx.s                                   */
/*---------------------------------------------------------------------------------------------------------*/
void TMR1_IRQHandler(void)
{
    int32_t i;
    
    if ((TIMER1->TCSR.IE == 1) && (TIMER1->TISR.TIF == 1))
        TIMER1->TISR.TIF = 1;

    if ((TIMER1->TEXCON.TEXEN == 1) && (TIMER1->TEXCON.TEXIEN == 1) && (TIMER1->TEXISR.TEXIF == 1))
        TIMER1->TEXISR.TEXIF = 1;

    uTimer1Tick++;
    if (bIsSetTime1Event)                       /* Timer Event Handle */
    {
        for (i=0; i<TIMER_EVENT_COUNT; i++)
        {
            if (tTime1Event[i].active)
            {
                tTime1Event[i].curTick--;
                if (tTime1Event[i].curTick == 0)
                {
                    (*tTime1Event[i].funPtr)(tTime1Event[i].transParam);
                    tTime1Event[i].curTick = tTime1Event[i].initTick;
                }
            }
        }
    }
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function:        TMR2_IRQHandler                                                                        */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*                  None                                                                                   */
/* Returns:                                                                                                */
/*                  None                                                                                   */
/* Description:                                                                                            */
/*                  The TIMER2 default IRQ, declared in startup_NUC1xx.s                                   */
/*---------------------------------------------------------------------------------------------------------*/
void TMR2_IRQHandler(void)
{
    int32_t i;
    
    if ((TIMER2->TCSR.IE == 1) && (TIMER2->TISR.TIF == 1))
        TIMER2->TISR.TIF = 1;

    if ((TIMER2->TEXCON.TEXEN == 1) && (TIMER2->TEXCON.TEXIEN == 1) && (TIMER2->TEXISR.TEXIF == 1))
        TIMER2->TEXISR.TEXIF = 1;

    uTimer2Tick++;
    if (bIsSetTime2Event)                       /* Timer Event Handle */
    {
        for (i=0; i<TIMER_EVENT_COUNT; i++)
        {
            if (tTime2Event[i].active)
            {
                tTime2Event[i].curTick--;
                if (tTime2Event[i].curTick == 0)
                {
                    (*tTime2Event[i].funPtr)(tTime2Event[i].transParam);
                    tTime2Event[i].curTick = tTime2Event[i].initTick;
                }
            }
        }
    }
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function:        TMR3_IRQHandler                                                                        */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*                  None                                                                                   */
/* Returns:                                                                                                */
/*                  None                                                                                   */
/* Description:                                                                                            */
/*                  The TIMER3 default IRQ, declared in startup_NUC1xx.s                                   */
/*---------------------------------------------------------------------------------------------------------*/
void TMR3_IRQHandler(void)

⌨️ 快捷键说明

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