?? bsptouch.cpp
字號:
//------------------------------------------------------------------------------
//
// Copyright (C) 2004-2005, Motorola Inc. All Rights Reserved
//
//------------------------------------------------------------------------------
//
// Copyright (C) 2004-2006, Freescale Semiconductor, Inc. All Rights Reserved.
// THIS SOURCE CODE, AND ITS USE AND DISTRIBUTION, IS SUBJECT TO THE TERMS
// AND CONDITIONS OF THE APPLICABLE LICENSE AGREEMENT
//
//------------------------------------------------------------------------------
//
// File: bsptouch.cpp
//
// Provides the implementation for BSP-specific touch support.
//
//------------------------------------------------------------------------------
#include <windows.h>
#include <tchddsi.h>
#include "bsp.h"
#include "pmic_ioctl.h"
#include "pmic_lla.h"
#include "pmic_adc.h"
#include "regs.h"
#include "regs_adc.h"
//-----------------------------------------------------------------------------
// External Functions
//-----------------------------------------------------------------------------
// External Variables
extern "C" const int MIN_CAL_COUNT = 20;
//-----------------------------------------------------------------------------
// Defines
// Maximum allowed variance in the X coordinate samples.
#define DELTA_X_COORD_VARIANCE 24
// Maximum allowed variance in the X coordinate samples.
#define DELTA_Y_COORD_VARIANCE 24
#define TOUCHCLKSRC_FREQ BSP_CLK_CKIL_FREQ // clock source is CKIL
#define TOUCHCLKSRC_VAL EPIT_CR_CLKSRC_CKIL
#define TOUCHPRESCALAR_VAL 0x000 // divide by 1
#define TOUCH_EPIT_TICKS_PER_MSEC ((TOUCHCLKSRC_FREQ/(TOUCHPRESCALAR_VAL+1))/1000)
#define ABS(x) ((x) >= 0 ? (x) : (-(x)))
//-----------------------------------------------------------------------------
// Types
//-----------------------------------------------------------------------------
// Global Variables
extern "C" UINT32 gTouchTimerIrq = IRQ_EPIT2;
//-----------------------------------------------------------------------------
// Local Variables
static PCSP_EPIT_REG g_pEPIT;
static HANDLE hPMI;
static TCHAR *gEventNamePri = TEXT("EVENT_TS_PRI");
static TCHAR *gEventNameAlt = TEXT("EVENT_TS_ALT");
// Named event for interrupt registration
static TCHAR *gTouchEventName;
//-----------------------------------------------------------------------------
// Local Functions
//-----------------------------------------------------------------------------
//
// Function: PmicTouchDeinit
//
// This function uninitializes the PMIC touch controller.
//
// Parameters:
// None.
//
// Returns:
// None.
//
//-----------------------------------------------------------------------------
static void PmicTouchDeinit(void)
{
DEBUGMSG(ZONE_FUNCTION, (TEXT("-%s()\r\n"), __WFUNCTION__));
// Deregister for PMIC pen down interrupts.
if (PmicInterruptDeregister(PMIC_MC13783_INT_TSI) != PMIC_SUCCESS)
{
ERRORMSG(TRUE, (_T("PmicTouchDeinit: PmicInterruptDeregister failed\r\n")));
}
// Disable PMIC touch interrupt
// Set to inactive mode
if (PmicADCTouchStandby(false) != PMIC_SUCCESS)
{
ERRORMSG(TRUE, (_T("PmicTouchDeinit: PmicTouchStandby failed\r\n")));
}
DEBUGMSG(ZONE_FUNCTION, (TEXT("-%s()\r\n"), __WFUNCTION__));
}
//------------------------------------------------------------------------------
//
// Function: PmicTouchInit
//
// This function initializes the PMIC touch controller.
//
// Parameters:
// None.
//
// Returns:
// Status.
//
//------------------------------------------------------------------------------
static PMIC_STATUS PmicTouchInit(void)
{
PMIC_STATUS rc = PMIC_ERROR;
DEBUGMSG(ZONE_FUNCTION, (TEXT("+%s()\r\n"), __WFUNCTION__));
// Register for PMIC pen down interrupts. Event name must match one given
// to event created in MDD for IST signaling.
if (PmicInterruptRegister(PMIC_MC13783_INT_TSI, gTouchEventName)
!= PMIC_SUCCESS)
{
ERRORMSG(TRUE, (_T("PmicTouchInit: PmicInterruptRegister failed\r\n")));
goto cleanUp;
}
// Put PMIC touch ADC into standby (required for pen down interrupt)
if (PmicADCTouchStandby(true) != PMIC_SUCCESS)
{
ERRORMSG(TRUE, (_T("PmicTouchInit: PmicTouchStandby failed\r\n")));
goto cleanUp;
}
// Make sure pen down interrupt are unmasked
if (PmicInterruptHandlingComplete(PMIC_MC13783_INT_TSI) != PMIC_SUCCESS)
{
ERRORMSG(TRUE, (_T("PmicTouchInit: PmicInterruptHandlingComplete failed\r\n")));
goto cleanUp;
}
rc = PMIC_SUCCESS;
cleanUp:
if (rc != PMIC_SUCCESS) PmicTouchDeinit();
DEBUGMSG(ZONE_FUNCTION, (TEXT("-%s()\r\n"), __WFUNCTION__));
return rc;
}
//-----------------------------------------------------------------------------
//
// Function: BSPTouchSetSampleRate
//
// This function configures the rate for acquiring touch samples by
// configuring the periodic interval of the EPIT2 timer.
//
// Parameters:
// dwIntervalMsec
// [in] Specifies the interval in msec to be used for generating
// a periodic timer interrupt. This interval sets the sample
// rate of the touch driver.
//
// Returns:
// None
//
//-----------------------------------------------------------------------------
void BSPTouchSetSampleRate(DWORD dwIntervalMsec)
{
DEBUGMSG(ZONE_FUNCTION, (TEXT("+%s()\r\n"), __WFUNCTION__));
// Enable EPIT2 clocks
DDKClockSetGatingMode(DDK_CLOCK_GATE_INDEX_EPIT2,
DDK_CLOCK_GATE_MODE_ENABLED_ALL);
// Configure load register with the number of EPIT ticks required
// to achieve the specified touch timer tick interval
OUTREG32(&g_pEPIT->LR, dwIntervalMsec*TOUCH_EPIT_TICKS_PER_MSEC);
// Configure the compare register to generate interrupt when
// timer reloads from EPITLR
// OUTREG32(&g_pEPIT->CMPR, dwIntervalMsec*TOUCH_EPIT_TICKS_PER_MSEC);
// Disable EPIT2 clocks
DDKClockSetGatingMode(DDK_CLOCK_GATE_INDEX_EPIT2,
DDK_CLOCK_GATE_MODE_DISABLED);
DEBUGMSG(ZONE_FUNCTION, (TEXT("-%s()\r\n"), __WFUNCTION__));
}
//-----------------------------------------------------------------------------
//
// Function: BSPTouchTimerInit
//
// This function initializes the EPIT2 timer for use as the touch
// periodic timer.
//
// Parameters:
// dwIntervalMsec
// [in] Specifies the interval in msec to be used for generating
// a periodic timer interrupt. This interval sets the sample
// rate of the touch driver.
//
// Returns:
// TRUE if successful, otherwise FALSE.
//
//-----------------------------------------------------------------------------
static BOOL BSPTouchTimerInit(DWORD dwIntervalMsec)
{
DEBUGMSG(ZONE_FUNCTION, (TEXT("+%s()\r\n"), __WFUNCTION__));
// Enable EPIT2 clocks
DDKClockSetGatingMode(DDK_CLOCK_GATE_INDEX_EPIT2,
DDK_CLOCK_GATE_MODE_ENABLED_ALL);
// Disable EPIT and clear all configuration bits
OUTREG32(&g_pEPIT->CR, 0);
// Assert software reset for the timer
OUTREG32(&g_pEPIT->CR, CSP_BITFMASK(EPIT_CR_SWR));
// Wait for the software reset to complete
while (INREG32(&g_pEPIT->CR) & CSP_BITFMASK(EPIT_CR_SWR));
// Clear timer compare interrupt flag (write-1-clear)
OUTREG32(&g_pEPIT->SR, CSP_BITFMASK(EPIT_SR_OCIF));
// Enable timer for "set-and-forget" mode where timer is
// loaded with EPITLR when count down to zero is reached
OUTREG32(&g_pEPIT->CR,
CSP_BITFVAL(EPIT_CR_EN, EPIT_CR_EN_DISABLE) |
CSP_BITFVAL(EPIT_CR_ENMOD, EPIT_CR_ENMOD_LOAD) |
CSP_BITFVAL(EPIT_CR_OCIEN, EPIT_CR_OCIEN_ENABLE) |
CSP_BITFVAL(EPIT_CR_RLD, EPIT_CR_RLD_RELOAD) |
CSP_BITFVAL(EPIT_CR_PRESCALAR, TOUCHPRESCALAR_VAL) |
CSP_BITFVAL(EPIT_CR_SWR, EPIT_CR_SWR_NORESET) |
CSP_BITFVAL(EPIT_CR_IOVW, EPIT_CR_IOVW_OVR) |
CSP_BITFVAL(EPIT_CR_DBGEN, EPIT_CR_DBGEN_ACTIVE) |
CSP_BITFVAL(EPIT_CR_WAITEN, EPIT_CR_WAITEN_ENABLE) |
CSP_BITFVAL(EPIT_CR_DOZEN, EPIT_CR_DOZEN_ENABLE) |
CSP_BITFVAL(EPIT_CR_STOPEN, EPIT_CR_STOPEN_ENABLE) |
CSP_BITFVAL(EPIT_CR_OM, EPIT_CR_OM_DICONNECT) |
CSP_BITFVAL(EPIT_CR_CLKSRC, TOUCHCLKSRC_VAL));
// Configure the sample rate
BSPTouchSetSampleRate(dwIntervalMsec);
// Disable EPIT2 clocks
DDKClockSetGatingMode(DDK_CLOCK_GATE_INDEX_EPIT2,
DDK_CLOCK_GATE_MODE_DISABLED);
DEBUGMSG(ZONE_FUNCTION, (TEXT("-%s()\r\n"), __WFUNCTION__));
return TRUE;
}
//-----------------------------------------------------------------------------
//
// Function: BSPTouchTimerClrIntr
//
// This function clears EPIT2 compare interrupt flag( w1c )
//
// Parameters:
// None.
//
// Returns:
// None.
//
//-----------------------------------------------------------------------------
void BSPTouchTimerClrIntr(void)
{
DEBUGMSG(ZONE_FUNCTION, (TEXT("+%s()\r\n"), __WFUNCTION__));
// Enable EPIT2 clocks
DDKClockSetGatingMode(DDK_CLOCK_GATE_INDEX_EPIT2,
DDK_CLOCK_GATE_MODE_ENABLED_ALL);
// Clear timer compare interrupt flag (write-1-clear)
OUTREG32(&g_pEPIT->SR, CSP_BITFMASK(EPIT_SR_OCIF));
DEBUGMSG(ZONE_FUNCTION, (TEXT("-%s()\r\n"), __WFUNCTION__));
return;
}
//-----------------------------------------------------------------------------
//
// Function: BSPTouchTimerEnable
//
// This function activates the touch timer for the specified
// periodic timer interrupt.
//
// Parameters:
// None.
//
// Returns:
// None.
//
//-----------------------------------------------------------------------------
void BSPTouchTimerEnable(void)
{
DEBUGMSG(ZONE_FUNCTION, (TEXT("+%s()\r\n"), __WFUNCTION__));
// Enable EPIT2 clocks
DDKClockSetGatingMode(DDK_CLOCK_GATE_INDEX_EPIT2,
DDK_CLOCK_GATE_MODE_ENABLED_ALL);
// Clear timer compare interrupt flag (write-1-clear)
OUTREG32(&g_pEPIT->SR, CSP_BITFMASK(EPIT_SR_OCIF));
// Enable the timer
INSREG32(&g_pEPIT->CR, CSP_BITFMASK(EPIT_CR_EN),
CSP_BITFVAL(EPIT_CR_EN, EPIT_CR_EN_ENABLE));
DEBUGMSG(ZONE_FUNCTION, (TEXT("-%s()\r\n"), __WFUNCTION__));
return;
}
//-----------------------------------------------------------------------------
//
// Function: BSPTouchTimerDisable
//
// DESCRIPTION:
// This function disables the touch timer and clears any pending
// timer interrupt.
//
// Parameters:
// None.
//
// Returns:
// None.
//
//-----------------------------------------------------------------------------
void BSPTouchTimerDisable(void)
{
DDK_CLOCK_GATE_MODE cgMode;
DEBUGMSG(ZONE_FUNCTION, (TEXT("+%s()\r\n"), __WFUNCTION__));
// If the touch timer is enabled
if (DDKClockGetGatingMode(DDK_CLOCK_GATE_INDEX_EPIT2, &cgMode) &&
(cgMode != DDK_CLOCK_GATE_MODE_DISABLED))
{
// Disable the timer
CLRREG32(&g_pEPIT->CR, CSP_BITFMASK(EPIT_CR_EN));
// Clear timer compare interrupt flag (write-1-clear)
OUTREG32(&g_pEPIT->SR, CSP_BITFMASK(EPIT_SR_OCIF));
DEBUGMSG(ZONE_FUNCTION, (TEXT("-%s()\r\n"), __WFUNCTION__));
// Disable EPIT2 clocks
DDKClockSetGatingMode(DDK_CLOCK_GATE_INDEX_EPIT2,
DDK_CLOCK_GATE_MODE_DISABLED);
}
return;
}
//-----------------------------------------------------------------------------
//
// Function: BSPTouchTimerIrqQuery
//
// DESCRIPTION:
// This function queries the touch timer interrupt status.
//
// Parameters:
// None.
//
// Returns:
// None.
//
//-----------------------------------------------------------------------------
BOOL BSPTouchTimerIrqQuery(void)
{
DEBUGMSG(ZONE_FUNCTION, (TEXT("+%s()\r\n"), __WFUNCTION__));
// Return timer compare interrupt flag
return INREG32(&g_pEPIT->SR);
DEBUGMSG(ZONE_FUNCTION, (TEXT("-%s()\r\n"), __WFUNCTION__));
}
//-----------------------------------------------------------------------------
//
// Function: BSPTouchDealloc
//
// This function frees the resources allocated by the touch driver.
//
// Parameters:
// None.
//
// RETURNS:
// None.
//
//-----------------------------------------------------------------------------
static void BSPTouchDealloc(void)
{
DEBUGMSG(ZONE_FUNCTION, (TEXT("+%s()\r\n"), __WFUNCTION__));
// If touch timer was allocated
if (g_pEPIT != NULL)
{
// Free virtual space allocated for peripheral registers
MmUnmapIoSpace(g_pEPIT, sizeof(CSP_EPIT_REG));
g_pEPIT = NULL;
}
DEBUGMSG(ZONE_FUNCTION, (TEXT("-%s()\r\n"), __WFUNCTION__));
return;
}
///-----------------------------------------------------------------------------
//
// Function: BSPTouchAlloc
//
// This function allocates the resources needed by the touch driver.
//
// Parameters:
// None.
//
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -