📄 gptpdd.c
字号:
// Returns:
// None
//
//------------------------------------------------------------------------------
void GptStopTimer(void)
{
GPT_FUNCTION_ENTRY();
EnterCriticalSection(&g_hGptLock);
INSREG32BF(&g_pGPT->CR, GPT_CR_EN, GPT_CR_EN_DISABLE);
INSREG32BF(&g_pGPT->CR, GPT_CR_WAITEN, GPT_CR_WAITEN_DISABLE);
LeaveCriticalSection(&g_hGptLock);
GPT_FUNCTION_EXIT();
}
//------------------------------------------------------------------------------
//
// Function: GptEnableTimerInterrupt
//
// This function is used to enable the timer interrupt.
//
// Parameters:
// None
//
// Returns:
// None
//
//------------------------------------------------------------------------------
void GptEnableTimerInterrupt(void)
{
GPT_FUNCTION_ENTRY();
EnterCriticalSection(&g_hGptLock);
INSREG32BF(&g_pGPT->IR, GPT_IR_OF1IE, GPT_IR_OF1IE_INT_ENABLE);
LeaveCriticalSection(&g_hGptLock);
GPT_FUNCTION_EXIT();
}
//------------------------------------------------------------------------------
//
// Function: GptDisableTimerInterrupt
//
// This function is used to disable the timer interrupt.
//
// Parameters:
// None
//
// Returns:
// None
//
//------------------------------------------------------------------------------
void GptDisableTimerInterrupt(void)
{
GPT_FUNCTION_ENTRY();
EnterCriticalSection(&g_hGptLock);
INSREG32BF(&g_pGPT->IR, GPT_IR_OF1IE, GPT_IR_OF1IE_INT_DISABLE);
LeaveCriticalSection(&g_hGptLock);
GPT_FUNCTION_EXIT();
}
//------------------------------------------------------------------------------
//
// Function: GptSetTimerDelay
//
// This function is used to configure the timer and
// set the timer for some period delay.
//
// Parameters:
// timerMode
// [in] This parametor is used to set the timer mode.
// Set to timerModePeriodic for periodic mode, and
// timerModeFreeRunning for free running mode.
//
// period
// [in] This parameter is the period in milliseconds.
//
// Returns:
// TRUE - If success.
//
// FALSE - If the timer is not allocated.
//
//------------------------------------------------------------------------------
BOOL GptSetTimerDelay(PGPT_TIMER_SET_PKT pSetTimerPkt)
{
GPT_FUNCTION_ENTRY();
EnterCriticalSection(&g_hGptLock);
// Stop timer to set delay
//GptStopTimer();
// Value in compare register set to the desired period
// (in ms), multiplied by the number of ticks per ms
OUTREG32(&g_pGPT->OCR1, BSPGptCalculateCompareVal(pSetTimerPkt->period));
DEBUGMSG (ZONE_INFO, (TEXT("%s: period: 0x%x, compare val: 0x%x\r\n"),
__WFUNCTION__, pSetTimerPkt->period,
BSPGptCalculateCompareVal(pSetTimerPkt->period)));
// If requested, set mode to Free Running
if (pSetTimerPkt->timerMode == timerModeFreeRunning)
{
INSREG32BF(&g_pGPT->CR, GPT_CR_FRR, GPT_CR_FRR_FREERUN);
}
// If requested, set mode to Periodic
else if(pSetTimerPkt->timerMode == timerModePeriodic)
{
INSREG32BF(&g_pGPT->CR, GPT_CR_FRR, GPT_CR_FRR_RESTART);
}
LeaveCriticalSection(&g_hGptLock);
GptStatus();
GPT_FUNCTION_EXIT();
return TRUE;
}
//------------------------------------------------------------------------------
//
// Function: GptClearInterruptStatus
//
// This function is used to clear the GPT interrupt status and signal to the
// kernel that interrupt processing is completed.
//
// Parameters:
// None
//
// Returns:
// None
//
//------------------------------------------------------------------------------
void GptClearInterruptStatus(void)
{
GPT_FUNCTION_ENTRY();
EnterCriticalSection(&g_hGptLock);
// Clear Interrupt Status Bits
INSREG32BF(&g_pGPT->SR, GPT_SR_OF1, GPT_SR_OF1_STATUS_CLEAR);
LeaveCriticalSection(&g_hGptLock);
// Kernel call to unmask the interrupt so that it can be signalled again
InterruptDone(g_gptIntr);
GPT_FUNCTION_EXIT();
}
//------------------------------------------------------------------------------
//
// Function: GptRegInit
//
// Set GPT registers to initial state.
//
// Parameters:
// None.
//
// Returns:
// None
//
//------------------------------------------------------------------------------
void GptRegInit(void)
{
UINT32 clkSrc;
GPT_FUNCTION_ENTRY();
EnterCriticalSection(&g_hGptLock);
// Must configure the clock gating mode before trying to access the GPT
// control registers. Otherwise, we will hang here when the driver is
// loaded during system boot.
BSPGptSetClockGatingMode(TRUE);
// Disable GPT and clear all configuration bits
OUTREG32(&g_pGPT->CR, 0);
// Assert software reset for the timer
INSREG32BF(&g_pGPT->CR, GPT_CR_SWR, GPT_CR_SWR_RESET);
// Wait for the software reset to complete
while (EXTREG32(&g_pGPT->CR, CSP_BITFMASK(GPT_CR_SWR), GPT_CR_SWR_LSH));
// Get BSP-specific clock source
clkSrc = BSPGptGetClockSource();
// Initialize GPT in freerun mode, with capture and compare turned off
OUTREG32 (&g_pGPT->CR,
(CSP_BITFVAL(GPT_CR_EN, GPT_CR_EN_DISABLE) |
CSP_BITFVAL(GPT_CR_ENMOD, GPT_CR_ENMOD_RETAIN) |
CSP_BITFVAL(GPT_CR_DBGEN, GPT_CR_DBGEN_DISABLE) |
CSP_BITFVAL(GPT_CR_WAITEN, GPT_CR_WAITEN_DISABLE) |
CSP_BITFVAL(GPT_CR_DOZEN, GPT_CR_DOZEN_DISABLE) |
CSP_BITFVAL(GPT_CR_STOPEN, GPT_CR_STOPEN_DISABLE) |
CSP_BITFVAL(GPT_CR_CLKSRC, clkSrc) |
CSP_BITFVAL(GPT_CR_FRR, GPT_CR_FRR_FREERUN) |
CSP_BITFVAL(GPT_CR_SWR, GPT_CR_SWR_NORESET) |
CSP_BITFVAL(GPT_CR_IM1, GPT_CR_IM1_DISABLE) |
CSP_BITFVAL(GPT_CR_IM2, GPT_CR_IM2_DISABLE) |
CSP_BITFVAL(GPT_CR_OM1, GPT_CR_OM1_DISABLE) |
CSP_BITFVAL(GPT_CR_OM2, GPT_CR_OM2_DISABLE) |
CSP_BITFVAL(GPT_CR_OM3, GPT_CR_OM3_DISABLE) |
CSP_BITFVAL(GPT_CR_FO1, GPT_CR_FO1_NOFORCE) |
CSP_BITFVAL(GPT_CR_FO2, GPT_CR_FO2_NOFORCE)));
// Initialize GPT prescaler value
OUTREG32(&g_pGPT->PR, GPT_PRESCALER_VAL);
// Disable GPT interrupts
OUTREG32(&g_pGPT->IR,
(CSP_BITFVAL(GPT_IR_OF1IE, GPT_IR_OF1IE_INT_DISABLE) |
CSP_BITFVAL(GPT_IR_OF2IE, GPT_IR_OF2IE_INT_DISABLE) |
CSP_BITFVAL(GPT_IR_OF3IE, GPT_IR_OF3IE_INT_DISABLE) |
CSP_BITFVAL(GPT_IR_IF1IE, GPT_IR_IF1IE_INT_DISABLE) |
CSP_BITFVAL(GPT_IR_IF2IE, GPT_IR_IF2IE_INT_DISABLE) |
CSP_BITFVAL(GPT_IR_ROVIE, GPT_IR_ROVIE_INT_DISABLE)));
// Clear timer compare interrupt flag (write-1-clear)
INSREG32BF(&g_pGPT->SR, GPT_SR_OF1, GPT_SR_OF1_STATUS_CLEAR);
// Done with init, disable GPT clocks
BSPGptSetClockGatingMode(FALSE);
LeaveCriticalSection(&g_hGptLock);
GPT_FUNCTION_EXIT();
}
//------------------------------------------------------------------------------
//
// Function: GptIntrThread
//
// This function is the IST thread.
//
// Parameters:
// None
//
// Returns:
// None
//
//------------------------------------------------------------------------------
void GptIntrThread(void)
{
GPT_FUNCTION_ENTRY();
GptISRLoop(INFINITE);
GPT_FUNCTION_ENTRY();
return;
}
//------------------------------------------------------------------------------
//
// Function: GptISRLoop
//
// This function is the interrupt handler for the GPT.
// It waits for the GPT interrupt event, and signals
// the timer registered by the user of this timer.
//
// Parameters:
// timeout
// [in] Timeout value while waiting for GPT timer interrupt.
//
// Returns:
// None
//
//------------------------------------------------------------------------------
static void GptISRLoop(UINT32 timeout)
{
GPT_FUNCTION_ENTRY();
// loop here
while(TRUE)
{
DEBUGMSG (ZONE_INFO, (TEXT("%s: In the loop\r\n"), __WFUNCTION__));
if (WaitForSingleObject(g_hGptIntrEvent, timeout) == WAIT_OBJECT_0)
{
DEBUGMSG (ZONE_INFO, (TEXT("%s: Interrupt received\r\n"), __WFUNCTION__));
if (g_hTimerEvent)
{
// Trigger timer event
SetEvent(g_hTimerEvent);
}
}
else
{
// Timeout as requested
DEBUGMSG (ZONE_INFO, (TEXT("%s: Time out\r\n"), __WFUNCTION__));
}
// Clear interrupt bits
GptClearInterruptStatus();
}
GPT_FUNCTION_ENTRY();
return;
}
//------------------------------------------------------------------------------
//
// Function: GptStatus
//
// This function is used to print out the GPT register status.
//
// Parameters:
// None
//
// Returns:
// None
//
//------------------------------------------------------------------------------
static void GptStatus(void)
{
DEBUGMSG (ZONE_INFO, (TEXT("ctrl: %x prescaler: %x compare: %x status: %x cnt: %x\r\n"),
INREG32(&g_pGPT->CR),
INREG32(&g_pGPT->PR),
INREG32(&g_pGPT->OCR1),
INREG32(&g_pGPT->SR),
INREG32(&g_pGPT->CNT)));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -