📄 drvtimer.c
字号:
for (i=0; i<TIMER_EVENT_COUNT; i++)
{
tTime0Event[i].active = FALSE;
}
uTimer0Tick = 0;
break;
}
case E_TMR1:
{
if (bIsTimer1Used != FALSE)
return E_DRVTIMER_EIO;
bIsTimer1Used = TRUE;
SYSCLK->APBCLK.TMR1_EN = 1;
outpw((uint32_t)&TIMER1->TCSR ,0 ); /* Disable timer */
for (i=0; i<TIMER_EVENT_COUNT; i++)
{
tTime1Event[i].active = FALSE;
}
uTimer1Tick = 0;
break;
}
case E_TMR2:
{
if (bIsTimer2Used != FALSE)
return E_DRVTIMER_EIO;
bIsTimer2Used = TRUE;
SYSCLK->APBCLK.TMR2_EN = 1;
outpw((uint32_t)&TIMER2->TCSR ,0 ); /* Disable timer */
for (i=0; i<TIMER_EVENT_COUNT; i++)
{
tTime2Event[i].active = FALSE;
}
uTimer2Tick = 0;
break;
}
case E_TMR3:
{
if (bIsTimer3Used != FALSE)
return E_DRVTIMER_EIO;
bIsTimer3Used = TRUE;
SYSCLK->APBCLK.TMR3_EN = 1;
outpw((uint32_t)&TIMER3->TCSR ,0 ); /* Disable timer */
for (i=0; i<TIMER_EVENT_COUNT; i++)
{
tTime3Event[i].active = FALSE;
}
uTimer3Tick = 0;
break;
}
default:
{
return E_DRVTIMER_CHANNEL ;
}
}
switch (ch)
{
case E_TMR0:
case E_TMR1:
case E_TMR2:
case E_TMR3:
{
/* TIMER clock source should be set as HCLK */
DrvSYS_SelectIPClockSource((E_SYS_IP_CLKSRC)((uint32_t)E_SYS_TMR0_CLKSRC+(uint32_t)ch), 2);
DrvGPIO_InitFunction((E_DRVGPIO_FUNC)((uint32_t)E_FUNC_TMR0 + (uint32_t)ch)); /* Open external Timer Counter source */
tTMR = (TIMER_T *)((uint32_t)TIMER0 + CH_OFFSET[ch]);
tTMR->TISR.TIF = 1; /* Write 1 to clear for safty */
tTMR->TCSR.MODE = op_mode; /* Set operation mode */
tTMR->TCMPR = uCounterBoundary;
tTMR->TCSR.PRESCALE = 0;
tTMR->TCSR.TDR_EN = 1;
tTMR->TCSR.IE = 0;
tTMR->TCSR.CTB = 1;
break;
}
}
return E_SUCCESS;
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvTIMER_StartCounter */
/* */
/* Parameters: */
/* ch - [in] */
/* E_TIMER_CHANNEL, it could be E_TMR0/E_TMR1/E_TMR2/E_TMR3 */
/* Returns: */
/* E_SUCCESS Operation successful */
/* E_DRVTIMER_CHANNEL Invalid Timer channel */
/* Description: */
/* Start counting of the specified timer channel. */
/* Only NuMicro NUC1x0xxxBx and NUC1x0xxxCx series support this function, */
/* ex:NUC140RD2BN, NUC140VE3CN. */
/*---------------------------------------------------------------------------------------------------------*/
int32_t DrvTIMER_StartCounter(E_TIMER_CHANNEL ch)
{
TIMER_T * tTMR;
switch (ch)
{
case E_TMR0:
case E_TMR1:
case E_TMR2:
case E_TMR3:
{
tTMR = (TIMER_T *)((uint32_t)TIMER0 + CH_OFFSET[ch]);
if (tTMR->TCSR.IE == 0)
tTMR->TCMPR = 0; // If use Polling Mode, do not configure Timer Compare Register.
tTMR->TCSR.CRST = 1;
tTMR->TCSR.CEN = 1;
return E_SUCCESS;
}
default:
{
return E_DRVTIMER_CHANNEL ;
}
}
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvTIMER_GetCounters */
/* */
/* Parameters: */
/* ch - [in] */
/* E_TIMER_CHANNEL, it could be E_TMR0/E_TMR1/E_TMR2/E_TMR3 */
/* Returns: */
/* u32Counters Return current counters */
/* E_DRVTIMER_CHANNEL Invalid Timer channel */
/* Description: */
/* This function is used to get the current counters of the specified timer channel. */
/* Only NuMicro NUC1x0xxxBx and NUC1x0xxxCx series support this function, */
/* ex:NUC140RD2BN, NUC140VE3CN. */
/*---------------------------------------------------------------------------------------------------------*/
uint32_t DrvTIMER_GetCounters(E_TIMER_CHANNEL ch)
{
TIMER_T * tTMR;
switch (ch)
{
case E_TMR0:
case E_TMR1:
case E_TMR2:
case E_TMR3:
{
tTMR = (TIMER_T *)((uint32_t)TIMER0 + CH_OFFSET[ch]);
return tTMR->TDR;
}
default:
{
return E_DRVTIMER_CHANNEL ;
}
}
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvTIMER_OpenCapture */
/* */
/* Parameters: */
/* ch - [in] */
/* E_TIMER_CHANNEL, it could be E_TMR0/E_TMR1/E_TMR2/E_TMR3 */
/* mode - [in] */
/* E_TIMER_RSTCAP_MODE, */
/* E_CAPTURE : Run capture function */
/* E_RESET : Reset counter value of specified timer channel */
/* Returns: */
/* E_SUCCESS Operation successful */
/* E_DRVTIMER_CHANNEL Invalid Timer channel */
/* Description: */
/* This function is used to initial the external timer capture source and */
/* set to start catpure or reset specified timer counter. */
/* The TIMER clock source should be set as HCLK. */
/* Only NuMicro NUC1x0xxxCx series support this function, ex:NUC140VE3CN. */
/*---------------------------------------------------------------------------------------------------------*/
int32_t DrvTIMER_OpenCapture(E_TIMER_CHANNEL ch, E_TIMER_RSTCAP_MODE mode)
{
TIMER_T * tTMR;
switch (ch)
{
case E_TMR0:
case E_TMR1:
case E_TMR2:
case E_TMR3:
{
/* TIMER clock source should be set as HCLK */
DrvSYS_SelectIPClockSource((E_SYS_IP_CLKSRC)((uint32_t)E_SYS_TMR0_CLKSRC+(uint32_t)ch), 2);
DrvGPIO_InitFunction((E_DRVGPIO_FUNC)((uint32_t)E_FUNC_T0EX + (uint32_t)ch));
tTMR = (TIMER_T *)((uint32_t)TIMER0 + CH_OFFSET[ch]);
tTMR->TEXCON.RSTCAPSEL = mode;
return E_SUCCESS;
}
default:
{
return E_DRVTIMER_CHANNEL ;
}
}
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvTIMER_CloseCapture */
/* */
/* Parameters: */
/* ch - [in] */
/* E_TIMER_CHANNEL, it could be E_TMR0/E_TMR1/E_TMR2/E_TMR3 */
/* Returns: */
/* E_SUCCESS Operation successful */
/* E_DRVTIMER_CHANNEL Invalid Timer channel */
/* Description: */
/* This function is used to close the external timer capture source. */
/* Only NuMicro NUC1x0xxxCx series support this function, ex:NUC140VE3CN. */
/*---------------------------------------------------------------------------------------------------------*/
int32_t DrvTIMER_CloseCapture(E_TIMER_CHANNEL ch)
{
TIMER_T * tTMR;
switch (ch)
{
case E_TMR0:
case E_TMR1:
case E_TMR2:
case E_TMR3:
{
tTMR = (TIMER_T *)((uint32_t)TIMER0 + CH_OFFSET[ch]);
outpw((uint32_t)&tTMR->TEXCON ,0 );
return E_SUCCESS;
}
default:
{
return E_DRVTIMER_CHANNEL ;
}
}
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvTIMER_SelectExternalMode */
/* */
/* Parameters: */
/* ch - [in] */
/* E_TIMER_CHANNEL, it could be E_TMR0/E_TMR1/E_TMR2/E_TMR3 */
/* mode - [in] */
/* E_TIMER_RSTCAP_MODE, */
/* E_CAPTURE : Run capture function */
/* E_RESET : Reset counter value of specified timer channel */
/* Returns: */
/* E_SUCCESS Operation successful */
/* E_DRVTIMER_CHANNEL Invalid Timer channel */
/* Description: */
/*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -