📄 timer.c
字号:
ASSERT((ulBase == TIMER0_BASE) || (ulBase == TIMER1_BASE) ||
(ulBase == TIMER2_BASE));
ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B));
//
// Return the appropriate match value.
//
return((ulTimer == TIMER_A) ? HWREG(ulBase + TIMER_O_TAMATCHR) :
HWREG(ulBase + TIMER_O_TBMATCHR));
}
//*****************************************************************************
//
//! Registers an interrupt.handler for the timer interrupt.
//!
//! \param ulBase is the base address of the timer module.
//! \param ulTimer specifies the timer(s); must be one of \b TIMER_A,
//! \b TIMER_B, or \b TIMER_BOTH.
//! \param pfnHandler is a pointer to the function to be called when the timer
//! interrupt occurs.
//!
//! This sets the handler to be called when a timer interrupt occurs. This
//! will enable the global interrupt in the interrupt controller; specific
//! timer interrupts must be enabled via TimerIntEnable(). It is the interrupt
//! handler's responsibility to clear the interrupt source via TimerIntClear().
//!
//! \sa IntRegister() for important information about registering interrupt
//! handlers.
//!
//! \return None.
//
//*****************************************************************************
void
TimerIntRegister(unsigned long ulBase, unsigned long ulTimer,
void (*pfnHandler)(void))
{
//
// Check the arguments.
//
ASSERT((ulBase == TIMER0_BASE) || (ulBase == TIMER1_BASE) ||
(ulBase == TIMER2_BASE));
ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||
(ulTimer == TIMER_BOTH));
//
// Get the interrupt number for this timer module.
//
ulBase = ((ulBase == TIMER0_BASE) ? INT_TIMER0A :
((ulBase == TIMER1_BASE) ? INT_TIMER1A : INT_TIMER2A));
//
// Register an interrupt.handler for timer A if requested.
//
if(ulTimer & TIMER_A)
{
//
// Register the interrupt.handler.
//
IntRegister(ulBase, pfnHandler);
//
// Enable the interrupt.
//
IntEnable(ulBase);
}
//
// Register an interrupt.handler for timer B if requested.
//
if(ulTimer & TIMER_B)
{
//
// Register the interrupt.handler.
//
IntRegister(ulBase + 1, pfnHandler);
//
// Enable the interrupt.
//
IntEnable(ulBase + 1);
}
}
//*****************************************************************************
//
//! Unregisters an interrupt.handler for the timer interrupt.
//!
//! \param ulBase is the base address of the timer module.
//! \param ulTimer specifies the timer(s); must be one of \b TIMER_A,
//! \b TIMER_B, or \b TIMER_BOTH.
//!
//! This function will clear the handler to be called when a timer interrupt
//! occurs. This will also mask off the interrupt in the interrupt controller
//! so that the interrupt.handler no longer is called.
//!
//! \sa IntRegister() for important information about registering interrupt
//! handlers.
//!
//! \return None.
//
//*****************************************************************************
void
TimerIntUnregister(unsigned long ulBase, unsigned long ulTimer)
{
//
// Check the arguments.
//
ASSERT((ulBase == TIMER0_BASE) || (ulBase == TIMER1_BASE) ||
(ulBase == TIMER2_BASE));
ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||
(ulTimer == TIMER_BOTH));
//
// Get the interrupt number for this timer module.
//
ulBase = ((ulBase == TIMER0_BASE) ? INT_TIMER0A :
((ulBase == TIMER1_BASE) ? INT_TIMER1A : INT_TIMER2A));
//
// Unregister the interrupt.handler for timer A if requested.
//
if(ulTimer & TIMER_A)
{
//
// Disable the interrupt.
//
IntDisable(ulBase);
//
// Unregister the interrupt.handler.
//
IntUnregister(ulBase);
}
//
// Unregister the interrupt.handler for timer B if requested.
//
if(ulTimer & TIMER_B)
{
//
// Disable the interrupt.
//
IntDisable(ulBase + 1);
//
// Unregister the interrupt.handler.
//
IntUnregister(ulBase + 1);
}
}
//*****************************************************************************
//
//! Enables individual timer interrupt sources.
//!
//! \param ulBase is the base address of the timer module.
//! \param ulIntFlags is the bit mask of the interrupt sources to be enabled.
//!
//! Enables the indicated timer interrupt sources. Only the sources that are
//! enabled can be reflected to the processor interrupt; disabled sources have
//! no effect on the processor.
//!
//! The parameter \e ulIntFlags must be the logical OR of any combination of
//! the following:
//!
//! - TIMER_CAPB_EVENT - Capture B event interrupt
//! - TIMER_CAPB_MATCH - Capture B match interrupt
//! - TIMER_TIMB_TIMEOUT - Timer B timeout interrupt
//! - TIMER_RTC_MATCH - RTC interrupt mask
//! - TIMER_CAPA_EVENT - Capture A event interrupt
//! - TIMER_CAPA_MATCH - Capture A match interrupt
//! - TIMER_TIMA_TIMEOUT - Timer A timeout interrupt
//!
//! \return None.
//
//*****************************************************************************
void
TimerIntEnable(unsigned long ulBase, unsigned long ulIntFlags)
{
//
// Check the arguments.
//
ASSERT((ulBase == TIMER0_BASE) || (ulBase == TIMER1_BASE) ||
(ulBase == TIMER2_BASE));
//
// Enable the specified interrupts.
//
HWREG(ulBase + TIMER_O_IMR) |= ulIntFlags;
}
//*****************************************************************************
//
//! Disables individual timer interrupt sources.
//!
//! \param ulBase is the base address of the timer module.
//! \param ulIntFlags is the bit mask of the interrupt sources to be disabled.
//!
//! Disables the indicated timer interrupt sources. Only the sources that are
//! enabled can be reflected to the processor interrupt; disabled sources have
//! no effect on the processor.
//!
//! The parameter \e ulIntFlags has the same definition as the \e ulIntFlags
//! parameter to TimerIntEnable().
//!
//! \return None.
//
//*****************************************************************************
void
TimerIntDisable(unsigned long ulBase, unsigned long ulIntFlags)
{
//
// Check the arguments.
//
ASSERT((ulBase == TIMER0_BASE) || (ulBase == TIMER1_BASE) ||
(ulBase == TIMER2_BASE));
//
// Disable the specified interrupts.
//
HWREG(ulBase + TIMER_O_IMR) &= ~(ulIntFlags);
}
//*****************************************************************************
//
//! Gets the current interrupt status.
//!
//! \param ulBase is the base address of the timer module.
//! \param bMasked is false if the raw interrupt status is required and true if
//! the masked interrupt status is required.
//!
//! This returns the interrupt status for the timer module. Either the raw
//! interrupt status or the status of interrupts that are allowed to reflect to
//! the processor can be returned.
//!
//! \return The current interrupt status, enumerated as a bit field of
//! values described in TimerIntEnable().
//
//*****************************************************************************
unsigned long
TimerIntStatus(unsigned long ulBase, tBoolean bMasked)
{
//
// Check the arguments.
//
ASSERT((ulBase == TIMER0_BASE) || (ulBase == TIMER1_BASE) ||
(ulBase == TIMER2_BASE));
//
// Return either the interrupt status or the raw interrupt status as
// requested.
//
return(bMasked ? HWREG(ulBase + TIMER_O_MIS) :
HWREG(ulBase + TIMER_O_RIS));
}
//*****************************************************************************
//
//! Clears timer interrupt sources.
//!
//! \param ulBase is the base address of the timer module.
//! \param ulIntFlags is a bit mask of the interrupt sources to be cleared.
//!
//! The specified timer interrupt sources are cleared, so that they no longer
//! assert. This must be done in the interrupt.handler to keep it from being
//! called again immediately upon exit.
//!
//! The parameter \e ulIntFlags has the same definition as the \e ulIntFlags
//! parameter to TimerIntEnable().
//!
//! \return None.
//
//*****************************************************************************
void
TimerIntClear(unsigned long ulBase, unsigned long ulIntFlags)
{
//
// Check the arguments.
//
ASSERT((ulBase == TIMER0_BASE) || (ulBase == TIMER1_BASE) ||
(ulBase == TIMER2_BASE));
//
// Clear the requested interrupt sources.
//
HWREG(ulBase + TIMER_O_ICR) = ulIntFlags;
}
//*****************************************************************************
//
//! Puts the timer into its reset state.
//!
//! \param ulBase is the base address of the timer module.
//!
//! The specified timer is disabled, and all its interrupts are disabled,
//! cleared, and unregistered. Then the timer registers are set to their reset
//! value.
//!
//! \return None.
//
//*****************************************************************************
void
TimerQuiesce(unsigned long ulBase)
{
//
// Check the arguments.
//
ASSERT((ulBase == TIMER0_BASE) || (ulBase == TIMER1_BASE) ||
(ulBase == TIMER2_BASE));
//
// Disable the timer.
//
HWREG(ulBase + TIMER_O_CTL) = TIMER_RV_CTL;
//
// Disable all the timer interrupts.
//
HWREG(ulBase + TIMER_O_IMR) = TIMER_RV_IMR;
//
// Clear all the timer interrupts.
//
HWREG(ulBase + TIMER_O_ICR) = 0xFFFFFFFF;
//
// Unregister the interrupt.handler. This also disables interrupts to the
// core.
//
TimerIntUnregister(ulBase, TIMER_BOTH);
//
// Set all the registers to their reset value.
//
HWREG(ulBase + TIMER_O_CFG) = TIMER_RV_CFG;
HWREG(ulBase + TIMER_O_TAMR) = TIMER_RV_TAMR;
HWREG(ulBase + TIMER_O_TBMR) = TIMER_RV_TBMR;
HWREG(ulBase + TIMER_O_RIS) = TIMER_RV_RIS;
HWREG(ulBase + TIMER_O_MIS) = TIMER_RV_MIS;
HWREG(ulBase + TIMER_O_TAILR) = TIMER_RV_TAILR;
HWREG(ulBase + TIMER_O_TBILR) = TIMER_RV_TBILR;
HWREG(ulBase + TIMER_O_TAMATCHR) = TIMER_RV_TAMATCHR;
HWREG(ulBase + TIMER_O_TBMATCHR) = TIMER_RV_TBMATCHR;
HWREG(ulBase + TIMER_O_TAPR) = TIMER_RV_TAPR;
HWREG(ulBase + TIMER_O_TBPR) = TIMER_RV_TBPR;
HWREG(ulBase + TIMER_O_TAPMR) = TIMER_RV_TAPMR;
HWREG(ulBase + TIMER_O_TBPMR) = TIMER_RV_TBPMR;
HWREG(ulBase + TIMER_O_TAR) = TIMER_RV_TAR;
HWREG(ulBase + TIMER_O_TBR) = TIMER_RV_TBR;
}
//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -