📄 lpc_timer.c
字号:
* Description: Get correponding external matching information from specific timer and match
* number.
*************************************************************************/
int TIMER_GetTimerExternalMatch(LPC_Timer_Channel_t DevNum, lpc_uint8 CHNum,
unsigned int * pAction , unsigned int *pExternalMatchValue)
{
if (CHNum >= CH_MAXNUM- 1)
return 1;
switch(DevNum)
{
case TIMER0:
*pExternalMatchValue = Timer0Config.ExtAction[CHNum];
*pAction = Timer0Config.MatchCH[CHNum].Action;
break;
case TIMER1:
*pExternalMatchValue = Timer1Config.ExtAction[CHNum];
*pAction = Timer1Config.MatchCH[CHNum].Action;
break;
default:
return 1;
}
return 0;
}
/*************************************************************************
* Function Name: TIMER_SetCaptureAction
* Parameters: LPC_Timer_Channel_t DevNum -- Device Number
* lpc_uint8 CPCHNum -- Capture Channel Number
* lpc_uint8 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 correponding capture trigger type and other information to the channel
* for specific timer.
*
* NOTE: Before setting timer match function, IO ports should be open. So you should
* configure relative PINSEL, which is not implemented in this function.
*************************************************************************/
int TIMER_SetCaptureAction (LPC_Timer_Channel_t DevNum,
lpc_uint8 CPCHNum,
lpc_uint8 TriggerType,
bool EnableInt,
void (* Fnpr)(void *),
void * FnprArg )
{
// Check parameter valid
if (CPCHNum >= CPCH_MAXNUM - 1)
return 1;
// Check parameter valid
if (TriggerType > TimerCPTrigger_Rising + TimerCPTrigger_Falling)
return 1;
switch (DevNum)
{
case TIMER0:
Timer0Config.CaptureCH[CPCHNum].Enable = true;
Timer0Config.CaptureCH[CPCHNum].TriggerType = TriggerType;
//Clear Capture actions
TIMER0_CCR &= ~(7<<(3*CPCHNum));
//Set Capture on Rising Edge
if (TriggerType & TimerCPTrigger_Rising)
TIMER0_CCR |= TimerCPTrigger_Rising << (3*CPCHNum);
//Set Capture on Falling Edge
if (TriggerType & TimerCPTrigger_Falling)
TIMER0_CCR |= TimerCPTrigger_Falling << (3*CPCHNum);
//Set Interrupt on Capture
if (EnableInt)
{
Timer0Config.CaptureCH[CPCHNum].EnableInt = true;
Timer0Config.CaptureCH[CPCHNum].Fnpr = Fnpr;
Timer0Config.CaptureCH[CPCHNum].FnprArg = FnprArg;
TIMER0_CCR |= 0x4 << (3*CPCHNum);
}
else
{
Timer0Config.CaptureCH[CPCHNum].EnableInt = false;
Timer0Config.CaptureCH[CPCHNum].Fnpr = NULL;
Timer0Config.CaptureCH[CPCHNum].FnprArg = (void *)0;
TIMER0_CCR &= ~(0x4 << (3*CPCHNum));
}
break;
case TIMER1:
Timer1Config.CaptureCH[CPCHNum].Enable = true;
Timer1Config.CaptureCH[CPCHNum].TriggerType = TriggerType;
//Clear Capture actions
TIMER1_CCR &= ~(7<<(3*CPCHNum));
//Set Capture on Rising Edge
if (TriggerType & TimerCPTrigger_Rising)
TIMER1_CCR |= TimerCPTrigger_Rising << (3*CPCHNum);
//Set Capture on Falling Edge
if (TriggerType & TimerCPTrigger_Falling)
TIMER1_CCR |= TimerCPTrigger_Falling << (3*CPCHNum);
//Set Interrupt on Capture
if (EnableInt)
{
Timer1Config.CaptureCH[CPCHNum].EnableInt = true;
Timer1Config.CaptureCH[CPCHNum].Fnpr = Fnpr;
Timer1Config.CaptureCH[CPCHNum].FnprArg = FnprArg;
TIMER1_CCR |= 0x4 << (3*CPCHNum);
}
else
{
Timer1Config.CaptureCH[CPCHNum].EnableInt = false;
Timer1Config.CaptureCH[CPCHNum].Fnpr = NULL;
Timer1Config.CaptureCH[CPCHNum].FnprArg = (void *)0;
TIMER1_CCR &= ~(0x4 << (3*CPCHNum));
}
break;
default:
return 1;
}
return 0;
}
/*************************************************************************
* Function Name: TIMER_GetTimerCapture
* Parameters: LPC_Timer_Channel_t DevNum
* lpc_uint8 CPCHNum
* unsigned long *pCaptureValue
* Return: int
* 0: success
* non-zero: error number
*
* Description: Get correponding capture information from specific timer and capture register.
*************************************************************************/
int TIMER_GetTimerCapture(LPC_Timer_Channel_t DevNum, lpc_uint8 CPCHNum,
unsigned long *pCaptureValue, lpc_uint8 *pTriggerType, bool *pEnableInt)
{
// Check parameter valid
if (CPCHNum >= CPCH_MAXNUM - 1)
return 1;
switch(DevNum)
{
case TIMER0:
*pCaptureValue = Timer0Config.CaptureCH[CPCHNum].CPValue;
*pTriggerType= Timer0Config.CaptureCH[CPCHNum].TriggerType;
*pEnableInt= Timer0Config.CaptureCH[CPCHNum].EnableInt;
break;
case TIMER1:
*pCaptureValue = Timer1Config.CaptureCH[CPCHNum].CPValue;
*pTriggerType= Timer1Config.CaptureCH[CPCHNum].TriggerType;
*pEnableInt= Timer1Config.CaptureCH[CPCHNum].EnableInt;
break;
default:
return 1;
}
return 0;
}
/*************************************************************************
* Function Name: TIMER_CheckIntSrc
* Parameters: LPC_Timer_Channel_t DevNum
* Return: int
* TIMERMR0...3Int | TIMERCR0...3Int
*
* Description: Get Timer interrupt Type
*
*************************************************************************/
int TIMER_CheckIntType(LPC_Timer_Channel_t DevNum)
{
switch (DevNum)
{
case TIMER0:
return (TIMER0_IR & 0xFF);
case TIMER1:
return (TIMER1_IR & 0xFF);
default:
return -1;
}
}
/*************************************************************************
* Function Name: TIMER_ClearInt
* Parameters: LPC_Timer_Channel_t DevNum
* int IntType
*
* Return: int
* 0: sucess
* 1: fail
*
* Description: Clear Timer interrupt.
*
*************************************************************************/
int TIMER_ClearInt(LPC_Timer_Channel_t DevNum, int IntType)
{
if (IntType<1 || IntType>0xFF)
return 1;
switch (DevNum)
{
case TIMER0:
TIMER0_IR = (IntType & 0xFF);
break;
case TIMER1:
TIMER1_IR = (IntType & 0xFF);
break;
default:
return 1;
}
return 0;
}
/*************************************************************************
* Function Name: TIMER_GetREGValue_CR
* Parameters: LPC_Timer_Channel_t DevNum
* int CRNum
* Return: unsigned long
*
* Description: Get CR register value
*
*************************************************************************/
unsigned long TIMER_GetREGValue_CR(LPC_Timer_Channel_t DevNum, int CRNum)
{
switch (DevNum)
{
case TIMER0:
switch(CRNum)
{
case CPCH0:
return TIMER0_CR0;
case CPCH1:
return TIMER0_CR1;
case CPCH2:
return TIMER0_CR2;
case CPCH3:
return TIMER0_CR3;
default:
return 0;
}
case TIMER1:
switch(CRNum)
{
case CPCH0:
return TIMER1_CR0;
case CPCH1:
return TIMER1_CR1;
case CPCH2:
return TIMER1_CR2;
case CPCH3:
return TIMER1_CR3;
default:
return 0;
}
default:
return 0;
}
}
/*************************************************************************
* Function Name: TIMER_GetREGValue_TC
* Parameters: int LPC_Timer_Channel_t
* Return: unsigned long
*
* Description: Get TC register value
*
*************************************************************************/
unsigned long TIMER_GetREGValue_TC(LPC_Timer_Channel_t DevNum)
{
switch (DevNum)
{
case TIMER0:
return TIMER0_TC;
case TIMER1:
return TIMER1_TC;
default:
return 0;
}
}
/*************************************************************************
* Function Name: TIMER0_ISR
* Parameters: void
* Return: void
*
* Description: TIMER0 interrupt subroutine
*
*************************************************************************/
__irq void TIMER0_ISR (void)
{
int IntStatus;
IntStatus = TIMER_CheckIntType(TIMER0);
TIMER_ClearInt(TIMER0, IntStatus);
/* Match Register Interrupt */
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 Interrupt */
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;
}
/*************************************************************************
* Function Name: TIMER1_ISR
* Parameters: void
* Return: void
*
* Description: TIMER1 interrupt subroutine
*
*************************************************************************/
void TIMER1_ISR (void)
{
int IntStatus;
IntStatus = TIMER_CheckIntType(TIMER1);
TIMER_ClearInt(TIMER1, IntStatus);
/* Match Register Interrupt */
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 Interrupt */
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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -