clock-arch.c

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

C
918
字号
    }
    // Clear External action
    T1EMR &= ~(3 << (4 + 2*MRNum));
    // Set External action
    switch (MRNum)
    {
    case CH0:
      // Set External action type
      T1EMR_bit.EMC0 = ExtAction;
      // Assign pin to match module
      if (ExtAction != DONOTHING)
        PINSEL0_bit.P0_12 = 0x2;
      break;
    case CH1:
      // Set External action type
      T1EMR_bit.EMC1 = ExtAction;
      // Assign pin to match module
      if (ExtAction != DONOTHING)
        PINSEL0_bit.P0_13 = 0x2;
      break;
    case CH2:
      // Set External action type
      T1EMR_bit.EMC2 = ExtAction;
      // Assign pin to match module
      if (ExtAction != DONOTHING)
        PINSEL1_bit.P0_19 = 0x2;
      break;
    case CH3:
      // Set External action type
      T0EMR_bit.EMC3 = ExtAction;
      // Assign pin to match module
      if (ExtAction != DONOTHING)
        PINSEL1_bit.P0_20 = 0x2;
      break;
    }
    return 0;
  }
  return 1;
}

/*************************************************************************
 * Function Name: TIMER_GetTimerExternalMatch
 * Parameters:    LPC_TimerChannel_t DevNum
 *                unsigned int MRNum,
 *                unsigned int *pAction,
 *                unsigned int *pExternalMatchValue
 * Return: int
 *               0: success
 *        non-zero: error number
 * Description:     Get corresponding external match information from specified timer.
 *
 *************************************************************************/
int TIMER_GetTimerExternalMatch(LPC_TimerChannel_t DevNum, unsigned int MRNum,
            unsigned int * pAction , unsigned int *pExternalMatchValue)
{
  if (MRNum >= CH_MAXNUM)
    return 1;
  switch(DevNum)
  {
  case TIMER0:
    *pExternalMatchValue = Timer0Config.ExtAction[MRNum];
    *pAction = Timer0Config.MatchCH[MRNum].Action;
    break;
  case TIMER1:
    *pExternalMatchValue = Timer1Config.ExtAction[MRNum];
    *pAction = Timer1Config.MatchCH[MRNum].Action;
    break;
  default:
    return 1;
  }
  return 0;
}
/*************************************************************************
 * Function Name: TIMER_GetTimerCapture
 * Parameters:    LPC_TimerChannel_t DevNum,
 *                unsigned int CRNum,
 *                unsigned int * pCaptureValue
 * Return:        int
 *                  0: success
 *           non-zero: error number
 * Description:   Get corresponding capture information from specified timer.
 *
 *************************************************************************/
int TIMER_GetTimerCapture(LPC_TimerChannel_t DevNum, unsigned int CRNum,
            unsigned int * pCaptureValue)
{
  switch(DevNum)
  {
  case TIMER0:
    if (CRNum >= CPCH_MAXNUM-1)
      return 1;
    *pCaptureValue = Timer0Config.CaptureCH[CRNum].CPValue;
    break;
  case TIMER1:
    if (CRNum >= CPCH_MAXNUM)
      return 1;
    *pCaptureValue = Timer1Config.CaptureCH[CRNum].CPValue;
    break;
  default:
    return 1;
  }
  return 0;
}
/*************************************************************************
 * Function Name: TIMER_SetCaptureAction
 * Parameters:    LPC_TimerChannel_t DevNum  -- Device Number
 *                unsigned char CPCHNum -- Capture Channel Number
 *                unsigned char TriggerType -- Rising edge | Falling edge
 *                bool EnableInt -- whether interrupt is generated
 *                void (* Fnpr)(void *) -- ISR function pointer
 *                void * FnprArg -- relative argument
 *
 * Return:        int
 *                  0: success
 *           non-zero: error number
 * Description:   Set corresponding capture trigger type and other information to the channel
 *                for specified timer.
 *
 *************************************************************************/
int TIMER_SetCaptureAction (LPC_TimerChannel_t DevNum,
                              unsigned char CPCHNum,
                              unsigned char  TriggerType,
                              bool EnableInt,
                              void (* Fnpr)(void *),
                              void * FnprArg )
{
  // Check parameter valid
  if (TriggerType > TimerCPTrigger_Rising+TimerCPTrigger_Falling)
    return 1;
  switch (DevNum)
  {
  case TIMER0:
    if (CPCHNum > CPCH_MAXNUM-2)  // for timer 0 capture channel 3 does not exist
      return 1;
    Timer0Config.CaptureCH[CPCHNum].Enable = true;
    Timer0Config.CaptureCH[CPCHNum].TriggerType = TriggerType;

    // Clear Capture actions
    T0CCR &= ~(7 << (3*CPCHNum));
    // Assign pin to capture module
    switch (CPCHNum)
    {
    case CH0:
      if (TriggerType == 0)
        PINSEL0_bit.P0_2 = 0;
      else
        PINSEL0_bit.P0_2 = 0x2;
      break;
    case CH1:
      if (TriggerType == 0)
        PINSEL0_bit.P0_4 = 0;
      else
        PINSEL0_bit.P0_4 = 0x2;
      break;
    case CH2:
      if (TriggerType == 0)
        PINSEL0_bit.P0_6 = 0;
      else
        PINSEL0_bit.P0_6 = 0x2;
      break;
    }
    //Set Capture on Rising Edge
    if (TriggerType & TimerCPTrigger_Rising)
      T0CCR |= TimerCPTrigger_Rising << (3*CPCHNum);
    //Set Capture on Falling Edge
    if (TriggerType & TimerCPTrigger_Falling)
      T0CCR |= TimerCPTrigger_Falling << (3*CPCHNum);

    //Set Interrupt on Capture
    if (EnableInt && TriggerType)
    {
      Timer0Config.CaptureCH[CPCHNum].EnableInt = true;
      Timer0Config.CaptureCH[CPCHNum].Fnpr = Fnpr;
      Timer0Config.CaptureCH[CPCHNum].FnprArg = FnprArg;
      T0CCR |= 0x4 << (3*CPCHNum);
    }
    else
    {
      Timer0Config.CaptureCH[CPCHNum].EnableInt = false;
      Timer0Config.CaptureCH[CPCHNum].Fnpr = NULL;
      Timer0Config.CaptureCH[CPCHNum].FnprArg = (void *)0;
    }
    return 0;
  case TIMER1:
    if (CPCHNum > CPCH_MAXNUM-1)
      return 1;
    Timer1Config.CaptureCH[CPCHNum].Enable = true;
    Timer1Config.CaptureCH[CPCHNum].TriggerType = TriggerType;

    // Clear Capture actions
    T1CCR &= ~(7 << (3*CPCHNum));
    // Assign pin to capture module
    switch (CPCHNum)
    {
    case CH0:
      if (TriggerType == 0)
        PINSEL0_bit.P0_10 = 0;
      else
        PINSEL0_bit.P0_10 = 0x2;
      break;
    case CH1:
      if (TriggerType)
        PINSEL0_bit.P0_11 = 0x2;
      break;
    case CH2:
      if (TriggerType == 0)
        PINSEL1_bit.P0_17 = 0;
      else
        PINSEL1_bit.P0_17 = 0x2;
      break;
    case CH3:
      if (TriggerType == 0)
        PINSEL1_bit.P0_18 = 0;
      else
        PINSEL1_bit.P0_18 = 0x2;
      break;
    }
    //Set Capture on Rising Edge
    if (TriggerType & TimerCPTrigger_Rising)
      T1CCR |= TimerCPTrigger_Rising << (3*CPCHNum);

    //Set Capture on Falling Edge
    if (TriggerType & TimerCPTrigger_Falling)
      T1CCR |= TimerCPTrigger_Falling << (3*CPCHNum);

    //Set Interrupt on Capture
    if (EnableInt && TriggerType)
    {
      Timer1Config.CaptureCH[CPCHNum].EnableInt = true;
      Timer1Config.CaptureCH[CPCHNum].Fnpr = Fnpr;
      Timer1Config.CaptureCH[CPCHNum].FnprArg = FnprArg;
      T1CCR |= 0x4 << (3*CPCHNum);
    }
    else
    {
      Timer1Config.CaptureCH[CPCHNum].EnableInt = false;
      Timer1Config.CaptureCH[CPCHNum].Fnpr = NULL;
      Timer1Config.CaptureCH[CPCHNum].FnprArg = (void *)0;
    }
    return 0;
  }
  return 1;
}
/*************************************************************************
 * Function Name: TIMER_CheckIntSrc
 * Parameters:    LPC_TimerChannel_t DevNum
 * Return:        unsigned long
 *                TIMERMR0...3Int | TIMERCR0...3Int
 *
 * Description:   Get Timer interrupt Type
 *
 *************************************************************************/
unsigned long TIMER_CheckIntType(LPC_TimerChannel_t DevNum)
{
  switch (DevNum)
  {
  case TIMER0:
    return (T0IR & 0xFF);
  case TIMER1:
    return (T1IR & 0xFF);
  default:
    return (unsigned long)-1;
  }
}
/*************************************************************************
 * Function Name: RTC_ClearInt
 * Parameters:    LPC_TimerChannel_t DevNum
 *                int IntType
 *
 * Return:        unsigned long
 *                  0: sucess
 *                  1: fail
 *
 * Description:   Clear Timer interrupt.
 *
 *************************************************************************/
unsigned long TIMER_ClearInt(LPC_TimerChannel_t DevNum, int IntType)
{
  if (IntType<1 || IntType>0xFF)
    return 1;

  switch (DevNum)
  {
  case TIMER0:
    T0IR = (IntType & 0xFF);
    break;
  case TIMER1:
    T1IR = (IntType & 0xFF);
    break;
  default:
    return 1;
  }
  return 0;
}
/*************************************************************************
 * Function Name: T0ISR
 * Parameters:    none
 * Return:        none
 *
 * Description:   TIMER0 interrupt subroutine
 *
 *************************************************************************/
void TIMER0_ISR ()
{
int IntStatus;
  IntStatus = TIMER_CheckIntType(TIMER0);
  TIMER_ClearInt(TIMER0, IntStatus);

  /* Match Register Interrupts */
  if (IntStatus & TIMERMR0Int)
  {
    (Timer0Config.MatchCH[0].Fnpr)((void *)Timer0Config.MatchCH[0].FnprArg);
  }

  if (IntStatus & TIMERMR1Int)
  {
    (Timer0Config.MatchCH[1].Fnpr)((void *)Timer0Config.MatchCH[1].FnprArg);
  }

  if (IntStatus & TIMERMR2Int)
  {
    (Timer0Config.MatchCH[2].Fnpr)((void *)Timer0Config.MatchCH[2].FnprArg);
  }

  if (IntStatus & TIMERMR3Int)
  {
    (Timer0Config.MatchCH[3].Fnpr)((void *)Timer0Config.MatchCH[3].FnprArg);
  }

  /* Capture Register Interrupts */
  if (IntStatus & TIMERCR0Int)
  {
    Timer0Config.CaptureCH[0].CPValue = TIMER_GetREGValue_CR(TIMER0, CPCH0);
    (Timer0Config.CaptureCH[0].Fnpr)((void *)Timer0Config.CaptureCH[0].FnprArg);
  }

  if (IntStatus & TIMERCR1Int)
  {
    Timer0Config.CaptureCH[1].CPValue = TIMER_GetREGValue_CR(TIMER0, CPCH1);
    (Timer0Config.CaptureCH[1].Fnpr)((void *)Timer0Config.CaptureCH[1].FnprArg);
  }

  if (IntStatus & TIMERCR2Int)
  {
    Timer0Config.CaptureCH[2].CPValue = TIMER_GetREGValue_CR(TIMER0, CPCH2);
    (Timer0Config.CaptureCH[2].Fnpr)((void *)Timer0Config.CaptureCH[2].FnprArg);
  }

  if (IntStatus & TIMERCR3Int)
  {
    Timer0Config.CaptureCH[3].CPValue = TIMER_GetREGValue_CR(TIMER0, CPCH3);
    (Timer0Config.CaptureCH[3].Fnpr)((void *)Timer0Config.CaptureCH[3].FnprArg);
  }
  VICVectAddr = 0;    // Clear interrupt in VIC.
}

/*************************************************************************
 * Function Name: T1ISR
 * Parameters:    none
 * Return:        none
 *
 * Description:   TIMER1 interrupt subroutine
 *
 *************************************************************************/
void TIMER1_ISR ()
{
  int IntStatus;

  IntStatus = TIMER_CheckIntType(TIMER1);
  TIMER_ClearInt(TIMER1, IntStatus);

  /* Match Register Interrupts */
  if (IntStatus & TIMERMR0Int)
  {
    (Timer1Config.MatchCH[0].Fnpr)((void *)Timer1Config.MatchCH[0].FnprArg);
  }

  if (IntStatus & TIMERMR1Int)
  {
    (Timer1Config.MatchCH[1].Fnpr)((void *)Timer1Config.MatchCH[1].FnprArg);
  }

  if (IntStatus & TIMERMR2Int)
  {
    (Timer1Config.MatchCH[2].Fnpr)((void *)Timer1Config.MatchCH[2].FnprArg);
  }

  if (IntStatus & TIMERMR3Int)
  {
    (Timer1Config.MatchCH[3].Fnpr)((void *)Timer1Config.MatchCH[3].FnprArg);
  }

  /* Capture Register Interrupts */
  if (IntStatus & TIMERCR0Int)
  {
    Timer1Config.CaptureCH[0].CPValue = TIMER_GetREGValue_CR(TIMER1, CPCH0);
    (Timer1Config.CaptureCH[0].Fnpr)((void *)Timer1Config.CaptureCH[0].FnprArg);
  }

  if (IntStatus & TIMERCR1Int)
  {
    Timer1Config.CaptureCH[1].CPValue = TIMER_GetREGValue_CR(TIMER1, CPCH1);
    (Timer1Config.CaptureCH[1].Fnpr)((void *)Timer1Config.CaptureCH[1].FnprArg);
  }

  if (IntStatus & TIMERCR2Int)
  {
    Timer1Config.CaptureCH[2].CPValue = TIMER_GetREGValue_CR(TIMER1, CPCH2);
    (Timer1Config.CaptureCH[2].Fnpr)((void *)Timer1Config.CaptureCH[2].FnprArg);
  }

  if (IntStatus & TIMERCR3Int)
  {
    Timer1Config.CaptureCH[3].CPValue = TIMER_GetREGValue_CR(TIMER1, CPCH3);
    (Timer1Config.CaptureCH[3].Fnpr)((void *)Timer1Config.CaptureCH[3].FnprArg);
  }

  VICVectAddr = 0;      // Clear interrupt in VIC.
}

/*************************************************************************
 * Function Name: clock_init
 * Parameters:    none
 *
 * Return:        none
 *
 * Description:   Timer init
 *
 *************************************************************************/
void clock_init(void)
{
  Ticks = 0;
  // Init Timer0
  TIMER_Init(TIMER0, TIMER_PRECISION);

  // Registered TIMER0 interrupt handler
  VIC_SetVectoredIRQ(TIMER0_ISR,VIC_Slot0,VIC_TIMER0);
  VIC_EnableInt(1<<VIC_TIMER0);

  // Set Timer, Match channel, Action, time period and corresponding ISR function
  TIMER_SetMatchAction(TIMER0, CH1, TimerAction_Interrupt | TimerAction_ResetTimer ,
  1sec_T0/TICK_PER_SECOND, TIM0_IntrHandler, (void *)&TickSysFlag, DONOTHING);

  TIMER_Start(TIMER0);
}
/*************************************************************************
 * Function Name: clock_init
 * Parameters:    none
 *
 * Return:        none
 *
 * Description:   The current clock time, measured in system ticks
 *
 *************************************************************************/
clock_time_t clock_time(void)
{
  return(Ticks);
}

⌨️ 快捷键说明

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