📄 at91timer.c
字号:
* This routine handles an auxiliary clock interrupt. It acknowledges the
* interrupt and calls the routine installed by sysAuxClkConnect().
*
* RETURNS: N/A
*/
LOCAL void sysAuxClkInt (void)
{
UINT32 dummy;
/* acknowledge interrupt: a read of the Status Register clears interrupt */
AT91_TIMER_READ (AUX_TIMER_STATUS, dummy);
/* If any routine is attached via sysAuxClkConnect(), call it */
if (sysAuxClkRoutine != NULL)
(*sysAuxClkRoutine) (sysAuxClkArg);
}
/*******************************************************************************
*
* sysAuxClkConnect - connect a routine to the auxiliary clock interrupt
*
* This routine specifies the interrupt service routine to be called at each
* auxiliary clock interrupt. It also connects the clock error interrupt
* service routine.
*
* RETURNS: OK, or ERROR if the routine cannot be connected to the interrupt.
*
* SEE ALSO: intConnect(), sysAuxClkEnable()
*/
STATUS sysAuxClkConnect
(
FUNCPTR routine, /* routine called at each aux clock interrupt */
int arg /* argument with which to call routine */
)
{
sysAuxClkRoutine = NULL; /* ensure routine not called with wrong arg */
sysAuxClkArg = arg;
sysAuxClkRoutine = routine;
return OK;
}
/*******************************************************************************
*
* sysAuxClkDisable - turn off auxiliary clock interrupts
*
* This routine disables auxiliary clock interrupts.
*
* RETURNS: N/A
*
* SEE ALSO: sysAuxClkEnable()
*/
void sysAuxClkDisable (void)
{
if (sysAuxClkRunning)
{
/* Disable timer in Channel Control Register */
AT91_TIMER_WRITE (AUX_TIMER_CCR, AT91_TIMER_CCR_CLKDIS);
/* Disable Register C Compare Interrupt in the timer */
AT91_TIMER_WRITE (AUX_TIMER_IDR, AT91_TIMER_IER_CPCS);
/* Disable the timer interrupt in the Interrupt Controller */
AT91_TIMER_INT_DISABLE (AUX_TIMER_INT_LVL);
sysAuxClkRunning = FALSE;
}
}
/*******************************************************************************
*
* sysAuxClkEnable - turn on auxiliary clock interrupts
*
* This routine enables auxiliary clock interrupts.
*
* RETURNS: N/A
*
* SEE ALSO: sysAuxClkDisable()
*/
void sysAuxClkEnable (void)
{
UINT32 tm;
if (!sysAuxClkRunning)
{
/*
* Calculate the timer match value:
* counter match value = (clock rate / sysAuxClkTicksPerSecond)
*/
tm = (AUX_TIMER_CLK / sysAuxClkTicksPerSecond);
/* Load match value into Timer Register C */
AT91_TIMER_WRITE (AUX_TIMER_REG_C, tm);
/*
* Set up Channel Mode Register: waveform mode, divide MCKI by 128,
* Register C compare trigger (reset counter and start counter).
*/
AT91_TIMER_WRITE (AUX_TIMER_CMR, (AT91_TIMER_CMR_MCKI_128 | \
AT91_TIMER_CMR_CPCTRG | \
AT91_TIMER_CMR_WAVE));
/* Clear Interrupt Status */
AT91_TIMER_READ (AUX_TIMER_STATUS, tm);
/* Enable Register C Compare Interrupt in the timer */
AT91_TIMER_WRITE (AUX_TIMER_IER, AT91_TIMER_IER_CPCS);
/* Enable the timer */
AT91_TIMER_WRITE (AUX_TIMER_CCR, AT91_TIMER_CCR_CLKEN);
/* Trigger the timer */
AT91_TIMER_WRITE (AUX_TIMER_CCR, AT91_TIMER_CCR_SWTRG);
/* enable clock interrupt in interrupt controller */
AT91_TIMER_INT_ENABLE (AUX_TIMER_INT_LVL);
sysAuxClkRunning = TRUE;
}
}
/*******************************************************************************
*
* sysAuxClkRateGet - get the auxiliary clock rate
*
* This routine returns the interrupt rate of the auxiliary clock.
*
* RETURNS: The number of ticks per second of the auxiliary clock.
*
* SEE ALSO: sysAuxClkEnable(), sysAuxClkRateSet()
*/
int sysAuxClkRateGet (void)
{
return sysAuxClkTicksPerSecond;
}
/*******************************************************************************
*
* sysAuxClkRateSet - set the auxiliary clock rate
*
* This routine sets the interrupt rate of the auxiliary clock. It does
* not enable axuxiliary clock interrupts unilaterally, but if the
* auxiliary clock is currently enabled, the clock is disabled and then
* re-enabled with the new rate.
*
*
* RETURNS: OK or ERROR.
*
* SEE ALSO: sysAuxClkEnable(), sysAuxClkRateGet()
*/
STATUS sysAuxClkRateSet
(
int ticksPerSecond /* number of clock interrupts per second */
)
{
if (ticksPerSecond < AUX_CLK_RATE_MIN || ticksPerSecond > AUX_CLK_RATE_MAX)
return ERROR;
sysAuxClkTicksPerSecond = ticksPerSecond;
if (sysAuxClkRunning)
{
sysAuxClkDisable ();
sysAuxClkEnable ();
}
return OK;
}
#ifdef INCLUDE_TIMESTAMP
/*******************************************************************************
*
* sysTimestampConnect - connect a user routine to a timestamp timer interrupt
*
* This routine specifies the user interrupt routine to be called at each
* timestamp timer interrupt.
*
* RETURNS: ERROR, always.
*/
STATUS sysTimestampConnect
(
FUNCPTR routine, /* routine called at each timestamp timer interrupt */
int arg /* argument with which to call routine */
)
{
return ERROR;
}
/*******************************************************************************
*
* sysTimestampEnable - enable a timestamp timer interrupt
*
* This routine enables timestamp timer interrupts and resets the counter.
*
* RETURNS: OK, always.
*
* SEE ALSO: sysTimestampDisable()
*/
STATUS sysTimestampEnable (void)
{
if (sysTimestampRunning)
{
return OK;
}
if (!sysClkRunning) /* timestamp timer is derived from the sysClk */
return ERROR;
sysTimestampRunning = TRUE;
return OK;
}
/*******************************************************************************
*
* sysTimestampDisable - disable a timestamp timer interrupt
*
* This routine disables timestamp timer interrupts.
*
* RETURNS: OK, always.
*
* SEE ALSO: sysTimestampEnable()
*/
STATUS sysTimestampDisable (void)
{
if (sysTimestampRunning)
sysTimestampRunning = FALSE;
return OK;
}
/*******************************************************************************
*
* sysTimestampPeriod - get the period of a timestamp timer
*
* This routine gets the period of the timestamp timer, in ticks. The
* period, or terminal count, is the number of ticks to which the timestamp
* timer counts before rolling over and restarting the counting process.
*
* RETURNS: The period of the timestamp timer in counter ticks.
*/
UINT32 sysTimestampPeriod (void)
{
/*
* The period of the timestamp depends on the clock rate of the system
* clock.
*/
return ((UINT32)(SYS_TIMER_CLK / sysClkTicksPerSecond));
}
/*******************************************************************************
*
* sysTimestampFreq - get a timestamp timer clock frequency
*
* This routine gets the frequency of the timer clock, in ticks per
* second. The rate of the timestamp timer is set explicitly by the
* hardware and typically cannot be altered.
*
* NOTE: Because the system clock serves as the timestamp timer,
* the system clock frequency is also the timestamp timer frequency.
*
* RETURNS: The timestamp timer clock frequency, in ticks per second.
*/
UINT32 sysTimestampFreq (void)
{
return ((UINT32)SYS_TIMER_CLK);
}
/*******************************************************************************
*
* sysTimestamp - get a timestamp timer tick count
*
* This routine returns the current value of the timestamp timer tick counter.
* The tick count can be converted to seconds by dividing it by the return of
* sysTimestampFreq().
*
* This routine should be called with interrupts locked. If interrupts are
* not locked, sysTimestampLock() should be used instead.
*
* RETURNS: The current timestamp timer tick count.
*
* SEE ALSO: sysTimestampFreq(), sysTimestampLock()
*/
UINT32 sysTimestamp (void)
{
UINT32 t;
AT91_TIMER_READ (SYS_TIMER_CVR, t);
return t;
}
/*******************************************************************************
*
* sysTimestampLock - lock interrupts and get the timestamp timer tick count
*
* This routine locks interrupts when the tick counter must be stopped
* in order to read it or when two independent counters must be read.
* It then returns the current value of the timestamp timer tick
* counter.
*
* The tick count can be converted to seconds by dividing it by the return of
* sysTimestampFreq().
*
* If interrupts are already locked, sysTimestamp() should be
* used instead.
*
* RETURNS: The current timestamp timer tick count.
*
* SEE ALSO: sysTimestampFreq(), sysTimestamp()
*/
UINT32 sysTimestampLock (void)
{
UINT32 t;
AT91_TIMER_READ (SYS_TIMER_CVR, t);
return t;
}
#endif /* INCLUDE_TIMESTAMP */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -