clock-arch.c

来自「NXP LPC系列AMR7的开发程序源码(LCD」· C语言 代码 · 共 918 行 · 第 1/2 页

C
918
字号
/*************************************************************************
 *
 *    Used with ICCARM and AARM.
 *
 *    (c) Copyright IAR Systems 2006
 *
 *    File name   : clock-arch.c
 *    Description : Implementation of architecture-specific clock functionality
 *
 *    History :
 *    1. Date        : June 15, 2006
 *       Author      : Stanimir Bonev
 *       Description : Create
 *
 *    2. Date        : June 19, 2006
 *       Author      : Todor Atanasov
 *       Description : New functions added for TIMERs initialization, operating,
 *                     capture and compare functionality, Timers' interrupt
 *                     service routines.
 *
 *    $Revision: 13961 $
**************************************************************************/
#include "clock-arch.h"
#include "board.h"
#include "LPC_Timer.h"

LPC_Timer_Config_t Timer0Config, Timer1Config;
volatile clock_time_t Ticks;

unsigned int Tim0Per, TickSysFlag;

/*************************************************************************
 * Function Name: TIM0_IntrHandler
 * Parameters: void *arg
 *
 * Return: none
 *
 * Description: TIM0 interrupt handler
 *
 *************************************************************************/
void  TIM0_IntrHandler (void *arg)
{
    ++Ticks;
    T0IR_bit.MR1INT = 1;   // clear the interrupt flag
}
/*************************************************************************
 * Function Name: TIMER_Init
 * Parameters:  LPC_TimerChannel_t DevNum
 *          unsigned int precision -- the timer precision (Unit: us), general setting is 10 us
 * Return: int
 *              0:  success
 *      non-zero:   error number
 * Description: Initialize Timer, Set the PR register that represents the precision of timer.
 *
 *************************************************************************/
int TIMER_Init(LPC_TimerChannel_t DevNum, unsigned long precision)
{
int i;
  //all registers are set to 0;
  switch (DevNum)
  {
  case TIMER0:
    // Set globe variable
    Timer0Config.Precision = precision;
    // PR = Precision(us) * Pclk
    if(precision)
    {
      Timer0Config.Prescaler = (precision * SYS_GetFpclk()) / 1000000;
    }
    else
    {
      Timer0Config.Prescaler = 1;
    }
    for (i=0; i<CH_MAXNUM; ++i)
    {
      Timer0Config.MatchCH[i].Enable = false;
      Timer0Config.MatchCH[i].Action = 0;
      Timer0Config.MatchCH[i].TimeValue =0;
      Timer0Config.MatchCH[i].Fnpr = NULL;
      Timer0Config.MatchCH[i].FnprArg = (void *)0;

      Timer0Config.CaptureCH[i].Enable = false;
      Timer0Config.CaptureCH[i].TriggerType= 0;
      Timer0Config.CaptureCH[i].EnableInt = 0;
      Timer0Config.CaptureCH[i].Fnpr = NULL;
      Timer0Config.CaptureCH[i].FnprArg = (void *)0;
      Timer0Config.CaptureCH[i].CPValue= 0;

      Timer0Config.ExtAction[i]= DONOTHING;
      Timer0Config.ExtBitValue[i]= 0;
    }
    // Clear interrupts flags
    T0IR=0xFF;
    // Disable counting
    T0TCR=0;
    // Clear timer counter
    T0TC=0;
    // PR = Prescaler - 1
    T0PR= Timer0Config.Prescaler - 1;
    // Clear prescaler timer counter
    T0PC=0;
    // Reset Compare modules
    T0MCR=0;
    T0MR0=0;
    T0MR1=0;
    T0MR2=0;
    T0MR3=0;
    // Reset Capture modules
    T0CCR=0;
    // Reset External Compare module
    T0EMR=0;
    break;
  case TIMER1:
    // Set globe variable
    Timer1Config.Precision = precision;
    // PR = Precision(us) * Pclk
    if(precision)
    {
      Timer1Config.Prescaler = (precision * SYS_GetFpclk()) / 1000000;
    }
    else
    {
      Timer1Config.Prescaler = 1;
    }
    for (i=0; i<CH_MAXNUM; ++i)
    {
      Timer1Config.MatchCH[i].Enable = false;
      Timer1Config.MatchCH[i].Action = 0;
      Timer1Config.MatchCH[i].TimeValue =0;
      Timer1Config.MatchCH[i].Fnpr = NULL;
      Timer1Config.MatchCH[i].FnprArg = (void *)0;

      Timer1Config.CaptureCH[i].Enable = false;
      Timer1Config.CaptureCH[i].TriggerType= 0;
      Timer1Config.CaptureCH[i].EnableInt = 0;
      Timer1Config.CaptureCH[i].Fnpr = NULL;
      Timer1Config.CaptureCH[i].FnprArg = (void *)0;
      Timer1Config.CaptureCH[i].CPValue= 0;

      Timer1Config.ExtAction[i]= DONOTHING;
      Timer1Config.ExtBitValue[i]= 0;
    }
    // Clear interrupt flags
    T1IR=0xFF;
    // Disable counting
    T1TCR=0;
    // Clear timer counter
    T1TC=0;
    // PR = Prescaler - 1
    T1PR=Timer1Config.Prescaler - 1;
    // Clear prescaler timer counter
    T1PC=0;
    // Reset Compare modules
    T1MCR=0;
    T1MR0=0;
    T1MR1=0;
    T1MR2=0;
    T1MR3=0;
    // Reset Capture modules
    T1CCR=0;
    // Reset External Compare module
    T1EMR=0;
    break;
  default:
    return 1;
  }
  return 0;
}
/*************************************************************************
 * Function Name: TIMER_GetPrescaler
 * Parameters:    LPC_TimerChannel_t DevNum
 * Return:        unsigned int
 *
 * Description:   Return prescaler value
 *
 *************************************************************************/
unsigned long TIMER_GetPrescaler(LPC_TimerChannel_t DevNum)
{
  switch (DevNum)
  {
  case TIMER0:
    return Timer0Config.Prescaler;
  case TIMER1:
    return Timer1Config.Prescaler;
  }
  return 0;
}
/*************************************************************************
 * Function Name: TIMER_Reset
 * Parameters:    LPC_TimerChannel_t DevNum
  * Return: int
 *              0: success
 *       non-zero: error number
 * Description:    When next pclk arrives, only the TC and PC will be reset.
 *                 While the other registers remain unchanged.
 *
 *************************************************************************/
int TIMER_Reset(LPC_TimerChannel_t DevNum)
{
  switch (DevNum)
  {
  case TIMER0:
    T0TCR |= 2;
    return 0;
  case TIMER1:
    T1TCR |= 2;
    return 0;
  }
  return 1;
}
/*************************************************************************
 * Function Name: TIMER_Start
 * Parameters:    LPC_TimerChannel_t DevNum
 * Return:        int
 *               0: success
 *        non-zero: error number
 * Description:   Starts (enables) the Timer
 *
 *************************************************************************/
int TIMER_Start(LPC_TimerChannel_t DevNum)
{
  switch (DevNum)
  {
  case TIMER0:
    // Clear reset flag
    T0TCR &= ~2;
    // Counting enable
    T0TCR_bit.CE = true;
    return 0;
  case TIMER1:
    // Clear reset flag
    T1TCR &= ~2;
    // Counting enable
    T1TCR_bit.CE = true;
    return 0;
  }
  return 1;
}
/*************************************************************************
 * Function Name: TIMER_Stop
 * Parameters:  LPC_TimerChannel_t DevNum
 * Return: int
 *              0: success
 *       non-zero: error number
 * Description: Just stops the Timer (disable it), all registers remain unchanged.
 *
 *************************************************************************/
int TIMER_Stop(LPC_TimerChannel_t DevNum)
{
  switch (DevNum)
  {
  case TIMER0:
    T0TCR_bit.CE = false;
    return 0;
  case TIMER1:
    T1TCR_bit.CE = false;
    return 0;
  }
  return 1;
}
/*************************************************************************
 * Function Name: TIMER_GetREGValue_CR
 * Parameters:    LPC_TimerChannel_t DevNum
 *                int CRNum
 * Return:        unsigned long
 *
 * Description:   Get CR register value
 *
 *************************************************************************/
unsigned long TIMER_GetREGValue_CR(LPC_TimerChannel_t DevNum, int CRNum)
{
  switch (DevNum)
  {
  case TIMER0:
    switch(CRNum)
    {
    case CPCH0:
      return T0CR0;
    case CPCH1:
      return T0CR1;
    case CPCH2:
      return T0CR2;
    case CPCH3:
      return T0CR3;
    }
  case TIMER1:
    switch(CRNum)
    {
    case CPCH0:
      return T1CR0;
    case CPCH1:
      return T1CR1;
    case CPCH2:
      return T1CR2;
    case CPCH3:
      return T1CR3;
    }
  }
  return (unsigned int)-1;
}
/*************************************************************************
 * Function Name: TIMER_GetREGValue_TC
 * Parameters:    LPC_TimerChannel_t DevNum
 * Return:        unsigned long
 *
 * Description:   Get TC register value
 *
 *************************************************************************/
unsigned long TIMER_GetREGValue_TC(LPC_TimerChannel_t DevNum)
{
  switch (DevNum)
  {
  case TIMER0:
    return T0TC;
  case TIMER1:
    return T1TC;
  }
  return (unsigned int)-1;
}
/*************************************************************************
 * Function Name: TIMER_SetMatchAction
 * Parameters:    LPC_TimerChannel_t DevNum  -- Device Number
 *                unsigned int MRNum        -- Match Channel Number
 *                unsigned int action       -- General Interrupt | Reset Timer | Stop Timer
 *                unsigned int Timevalue    -- the time value (Unit: us)
 *                void (* Fnpr)(void *)     -- ISR function pointer
 *                void * FnprArg            -- relative argument
 *                LPC_Timer_ExtAction_t ExtAction -- External Match Control Action
 *
 * Return:        int
 *                0: success
 *         non-zero: error number
 * Description:   Set corresponding match action and other information to the channel
 *                for the specified timer.
 *
 *************************************************************************/
int TIMER_SetMatchAction(LPC_TimerChannel_t DevNum,
                          unsigned int MRNum,
                          unsigned int action ,
                          unsigned long TimeValue,
                          void (* Fnpr)(void *),
                          void * FnprArg,
                          LPC_Timer_ExtAction_t ExtAction)
{
  // Check parameter valid
  if (action>TimerAction_StopTimer+TimerAction_ResetTimer+TimerAction_Interrupt)
    return 1;
  if (ExtAction > TOGGLE)
    return 1;
  switch (DevNum)
  {
  case TIMER0:
    // Set Match register
    switch (MRNum)
    {
    case CH0:
      T0MR0=TimeValue;
      break;
    case CH1:
      T0MR1=TimeValue;
      break;
    case CH2:
      T0MR2=TimeValue;
      break;
    case CH3:
      T0MR3=TimeValue;
      break;
    default:
      return 1;
    }
    Timer0Config.MatchCH[MRNum].Enable = true;
    Timer0Config.MatchCH[MRNum].Action = action;
    Timer0Config.MatchCH[MRNum].TimeValue= TimeValue;
    Timer0Config.ExtAction[MRNum]= ExtAction;
    // Clear actions
    T0MCR &= ~(7<<(MRNum*3));
    //Set Reset on Match
    if (action & TimerAction_ResetTimer)
      T0MCR |= TimerAction_ResetTimer << (3*MRNum);
    //Set StopWatch on Match
    if (action & TimerAction_StopTimer)
      T0MCR |= TimerAction_StopTimer << (3*MRNum);
    //Set Interrupt on Match
    if (action & TimerAction_Interrupt)
    {
      Timer0Config.MatchCH[MRNum].Fnpr = Fnpr;
      Timer0Config.MatchCH[MRNum].FnprArg = FnprArg;
      T0MCR |= TimerAction_Interrupt << (3*MRNum);
    }
    // Clear External action
    T0EMR &= ~(3 << (4 + 2*MRNum));
    // Set External action
    switch (MRNum)
    {
    case CH0:
      // Set External action type
      T0EMR_bit.EMC0 = ExtAction;
      // Assign pin to match module
      if (ExtAction != DONOTHING)
        PINSEL0_bit.P0_3 = 0x2;
      break;
    case CH1:
      // Set External action type
      T0EMR_bit.EMC1 = ExtAction;
      // Assign pin to match module
      if (ExtAction != DONOTHING)
        PINSEL0_bit.P0_5 = 0x2;
      break;
    case CH2:
      // Set External action type
      T0EMR_bit.EMC2 = ExtAction;
      // Assign pin to match module
      if (ExtAction != DONOTHING)
        PINSEL1_bit.P0_16 = 0x2;
      break;
    case CH3:
      // Set External action type
      T0EMR_bit.EMC3 = ExtAction;
      break;
    }
    return 0;
  case TIMER1:
    // Set Mash register
    switch (MRNum)
    {
    case CH0:
      T1MR0=TimeValue;
      break;
    case CH1:
      T1MR1=TimeValue;
      break;
    case CH2:
      T1MR2=TimeValue;
      break;
    case CH3:
      T1MR3=TimeValue;
      break;
    default:
      return 1;
    }

    Timer1Config.MatchCH[MRNum].Enable = true;
    Timer1Config.MatchCH[MRNum].Action = action;
    Timer1Config.MatchCH[MRNum].TimeValue= TimeValue;
    Timer1Config.ExtAction[MRNum]= ExtAction;
    // Clear actions
    T1MCR &= ~(7<<(MRNum*3));
    //Set Reset on Match
    if (action & TimerAction_ResetTimer)
      T1MCR |= TimerAction_ResetTimer << (3*MRNum);
    //Set StopWatch on Match
    if (action & TimerAction_StopTimer)
      T1MCR |= TimerAction_StopTimer << (3*MRNum);
    //Set Interrupt on Match
    if (action & TimerAction_Interrupt)
    {
      Timer1Config.MatchCH[MRNum].Fnpr = Fnpr;
      Timer1Config.MatchCH[MRNum].FnprArg = FnprArg;
      T1MCR |= TimerAction_Interrupt << (3*MRNum);

⌨️ 快捷键说明

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