📄 gptpdd.c
字号:
//-----------------------------------------------------------------------------
//
// Copyright (C) 2004, Motorola Inc. All Rights Reserved
//
//-----------------------------------------------------------------------------
//
// Copyright (C) 2004, Freescale Semiconductor, Inc. All Rights Reserved
// THIS SOURCE CODE IS CONFIDENTIAL AND PROPRIETARY AND MAY NOT
// BE USED OR DISTRIBUTED WITHOUT THE WRITTEN PERMISSION OF
// Freescale Semiconductor, Inc.
//
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//
// File: gptpdd.c
//
// Implementation of General Purpose Timer Driver
//
//-----------------------------------------------------------------------------
#include <windows.h>
#include <nkintr.h>
#include <ceddk.h>
#include "mxarm11.h"
#include "gpt.h"
#include "gpt_priv.h"
//-----------------------------------------------------------------------------
// External Functions
extern UINT32 BSPGptCalculateCompareVal(UINT32 period);
extern UINT32 BSPGptGetClockSource();
extern BOOL BSPGptSetClockGatingMode(BOOL);
//-----------------------------------------------------------------------------
// External Variables
//-----------------------------------------------------------------------------
// Defines
#define GPT_PRESCALER_VAL 0 // No prescaling
#define THREAD_PRIORITY 250
//-----------------------------------------------------------------------------
// Types
//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------
// Local Variables
static PCSP_GPT_REGS g_pGPT;
static CRITICAL_SECTION g_hGptLock;
static DWORD g_gptIntr;
static HANDLE g_hGptIntrEvent = NULL;
static HANDLE g_hTimerThread = NULL;
static HANDLE g_hTimerEvent = NULL;
static LPCTSTR g_TimerEventString = NULL;
//-----------------------------------------------------------------------------
// Local Functions
static void GptStatus(void);
static void GptIntrThread(void);
static void GptISRLoop(UINT32);
//-----------------------------------------------------------------------------
//
// Function: GptInitialize
//
// This function will call the PMU function to enable the timer source
// clock and create the timer irq event and create the IST thread.
//
// Parameters:
// None
//
// Returns:
// TRUE - If success
//
// FALSE - If failure
//
//-----------------------------------------------------------------------------
BOOL GptInitialize(void)
{
PHYSICAL_ADDRESS phyAddr;
DWORD gptIrq = IRQ_GPT;
GPT_FUNCTION_ENTRY();
phyAddr.QuadPart = CSP_BASE_REG_PA_GPT;
g_pGPT = (PCSP_GPT_REGS) MmMapIoSpace(phyAddr, sizeof(CSP_GPT_REGS), FALSE);
// check if Map Virtual Address failed
if (g_pGPT == NULL)
{
DEBUGMSG(ZONE_ERROR,
(TEXT("%s: MmMapIoSpace failed!\r\n"), __WFUNCTION__));
return FALSE;
}
DEBUGMSG(ZONE_INFO, (TEXT("%s: CreateEvent\r\n"), __WFUNCTION__));
g_hGptIntrEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (g_hGptIntrEvent == NULL)
{
DEBUGMSG(ZONE_ERROR,
(TEXT("%s: CreateEvent failed\r\n"), __WFUNCTION__));
goto Error;
}
// Translate IRQ to SYSINTR
if (!KernelIoControl(IOCTL_HAL_REQUEST_SYSINTR, &gptIrq, sizeof(gptIrq),
&g_gptIntr, sizeof(g_gptIntr), NULL))
{
DEBUGMSG(ZONE_ERROR,
(TEXT("%s: Request SYSINTR failed\r\n"), __WFUNCTION__));
goto Error;
}
if(!InterruptInitialize(g_gptIntr, g_hGptIntrEvent, NULL, 0))
{
CloseHandle(g_hGptIntrEvent);
g_hGptIntrEvent = NULL;
DEBUGMSG(ZONE_ERROR,
(TEXT("%s: Interrupt initialization failed! \r\n"), __WFUNCTION__));
goto Error;
}
// Initialize critical section for GPT
InitializeCriticalSection(&g_hGptLock);
if (!(g_hTimerThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)GptIntrThread, NULL, 0, NULL)))
{
DEBUGMSG(ZONE_ERROR,(TEXT("%s: CreateThread failed!\r\n"), __WFUNCTION__));
return 0;
}
else
{
DEBUGMSG(ZONE_INFO, (TEXT("%s: create timer thread success\r\n"), __WFUNCTION__));
// Set our interrupt thread's priority
CeSetThreadPriority(g_hTimerThread, THREAD_PRIORITY);
}
// Set initial state for GPT registers
GptRegInit();
GptStatus();
GPT_FUNCTION_EXIT();
return TRUE;
Error:
GptRelease();
return FALSE;
}
//------------------------------------------------------------------------------
//
// Function: GptRelease
//
// This function will release all resources and terminate the IST thread.
//
// Parameters:
// None
//
// Returns:
// None
//
//------------------------------------------------------------------------------
void GptRelease(void)
{
GPT_FUNCTION_ENTRY();
GptStopTimer();
GptDisableTimerInterrupt();
// Also set the clock gating mode to minimize power consumption when the
// GPT is no longer being used.
BSPGptSetClockGatingMode(FALSE);
// Release SYSINTR
KernelIoControl(IOCTL_HAL_RELEASE_SYSINTR, &g_gptIntr, sizeof(DWORD), NULL, 0, NULL);
g_gptIntr = SYSINTR_UNDEFINED;
// deregister the system interrupt
if (g_gptIntr != SYSINTR_UNDEFINED)
{
InterruptDisable(g_gptIntr);
g_gptIntr = SYSINTR_UNDEFINED;
}
if (g_hGptIntrEvent)
{
CloseHandle(g_hGptIntrEvent);
g_hGptIntrEvent = NULL;
}
if (g_hTimerEvent)
{
CloseHandle(g_hTimerEvent);
g_hTimerEvent = NULL;
}
if (g_hTimerThread)
{
CloseHandle(g_hTimerThread);
g_hTimerThread = NULL;
}
// Delete GPT critical section
DeleteCriticalSection(&g_hGptLock);
GPT_FUNCTION_EXIT();
return;
}
//------------------------------------------------------------------------------
//
// Function: GptTimerCreateEvent
//
// This function is used to create a GPT timer event handle
// triggered in the GPT ISR.
//
// Parameters:
// None
//
// Returns:
// TRUE if success
// FALSE if handle creation fails or if event
// hasn't already been created by caller
//
//------------------------------------------------------------------------------
BOOL GptTimerCreateEvent(LPTSTR eventString)
{
GPT_FUNCTION_ENTRY();
g_hTimerEvent = CreateEvent(NULL, FALSE, FALSE, eventString);
if ((g_hTimerEvent == NULL) || (GetLastError() != ERROR_ALREADY_EXISTS))
{
DEBUGMSG(ZONE_ERROR,
(TEXT("%s: CreateEvent failed or event does not already exist\r\n"), __WFUNCTION__));
return FALSE;
}
g_TimerEventString = eventString;
GPT_FUNCTION_EXIT();
return TRUE;
}
//------------------------------------------------------------------------------
//
// Function: GptTimerReleaseEvent
//
// This function is used to close the handle to the
// GPT timer event.
//
// Parameters:
// None
//
// Returns:
// TRUE if success
// FALSE if string name does not match
//
//------------------------------------------------------------------------------
BOOL GptTimerReleaseEvent(LPTSTR eventString)
{
GPT_FUNCTION_ENTRY();
if (strcmp((const char *) g_TimerEventString, (const char *) eventString) != 0)
{
DEBUGMSG(ZONE_ERROR,
(TEXT("%s: Cannot close GPT timer event handle: Wrong event string name.\r\n"), __WFUNCTION__));
return FALSE;
}
CloseHandle(g_hTimerEvent);
GPT_FUNCTION_EXIT();
return TRUE;
}
//------------------------------------------------------------------------------
//
// Function: GptStartTimer
//
// This function is used to start the timer. It will set the enable
// bit in the GPT control register. This function should be called to
// enable the timer after configuring the timer.
//
// Parameters:
// None
//
// Returns:
// TRUE if success. FALSE if timer has not been set.
//
//------------------------------------------------------------------------------
BOOL GptStartTimer(void)
{
GPT_FUNCTION_ENTRY();
EnterCriticalSection(&g_hGptLock);
if (INREG32(&g_pGPT->OCR1) == 0xFFFFFFFF)
{
DEBUGMSG (ZONE_ERROR, (TEXT("%s: Timer period has not been set. Cannot start timer.\r\n"), __WFUNCTION__));
LeaveCriticalSection(&g_hGptLock);
return FALSE;
}
INSREG32BF(&g_pGPT->CR, GPT_CR_EN, GPT_CR_EN_ENABLE);
INSREG32BF(&g_pGPT->CR, GPT_CR_WAITEN, GPT_CR_WAITEN_ENABLE);
LeaveCriticalSection(&g_hGptLock);
GPT_FUNCTION_EXIT();
return TRUE;
}
//------------------------------------------------------------------------------
//
// Function: GptStopTimer
//
// This function is used to stop the timer. It will
// clear the enable bit in the GPT control register.
// After this, the counter will reset to 0x00000000.
//
// Parameters:
// None
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -