⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tchpdd.cpp

📁 Cirrus EP9315 wince bsp
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//**********************************************************************
//                                                                      
// Filename: tchpdd.cpp
//                                                                      
// Description: Touch panel driver for the ep9312 board.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Use of this source code is subject to the terms of the Cirrus end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to 
// use this source code. For a copy of the EULA, please see the 
// LICENSE.RTF on your install media.
//
// Copyright(c) Cirrus Logic Corporation 2003, All Rights Reserved                       
//                                                                      
//**********************************************************************
//  
//Functions:
//
//  TouchDriverCalibrationPointGet
//  DdsiTouchPanelGetDeviceCaps
//  DdsiTouchPanelSetMode
//  DdsiTouchPanelEnable
//  DdsiTouchPanelDisable
//  DdsiTouchPanelAttach
//  DdsiTouchPanelDetach
//  DdsiTouchPanelGetPoint
//  DdsiTouchPanelPowerHandler
//  

#include    <windows.h>
#include    <types.h>
#include    <memory.h>
#include    <nkintr.h>
#include    <oalintr.h>
#include    <tchddsi.h>
#include    <hwdefs.h>
#include    <clocks.h>
#include    <touchscr.h>
#include    <ceddk.h>
#include    <haluser.h>

#define    ZONE_PDD        ZONE_6
#define    ZONE_POINT      ZONE_7






//#define TSSETUP2_DEFAULT        (TSSETUP2_UNSIGND | TSSETUP2_S28EN)
#define TSSETUP2_DEFAULT            (TSSETUP2_UNSIGND)

//
// The Switch settings.
//
#define SWITCH_DISCHARGE            0x0007fe04
#define SWITCH_DETECT               0x00403604
#define SWITCH_X                    0x00081604
#define SWITCH_Y                    0x00104601
#define SWITCH_Z1                   0x00101601
#define SWITCH_Z2                   0x00101608


//
// Calculate the real values later.
//
#define     TOUCHPANEL_SAMPLE_RATE_LOW      50
#define     TOUCHPANEL_SAMPLE_RATE_HIGH     100

#define     SAMPLE_DELAY                    5


// The MDD requires a minimum of MIN_CAL_COUNT consecutive samples before
// it will return a calibration coordinate to GWE.  This value is defined
// in the PDD so that each OEM can control the behaviour of the touch
// panel and still use the Microsoft supplied MDD.  Note that the extern "C"
// is required so that the variable name doesn't get decorated, and
// since we have an initializer the 'extern' is actually ignored and
// space is allocated.
extern "C" const int MIN_CAL_COUNT = 25;

//INT         CurrentSampleRateSetting = 0;       //  Low sample rate setting
#define     CALIBRATION_POINTS              5



//
// This is the minimum Z1 Value that is valid.  I found that 20 is a good 
// numbers.
//
#define     MIN_Z1_VALUE                    0x50

//
// Registry value names
//
#define REG_VALUE_MIN_INV_PRESSURE  TEXT("MinInvPressure")
#define REG_VALUE_MAX_INV_PRESSURE  TEXT("MaxInvPressure")
#define REG_VALUE_MAX_CHANGE        TEXT("MaxChange")

DWORD   gIntrTouch          = SYSINTR_TOUCH;
DWORD   gIntrTouchChanged   = SYSINTR_NOP;
HANDLE  ghTouchEvent          = 0;
ULONG   gulSetup;
BOOL    gbInterruptMode;
ULONG   gulMinInvPressure; //  = 0x1500;
ULONG   gulMaxInvPressure; //  = 0x5000;
ULONG   gulLastX = 0;
ULONG   gulLastY = 0;
ULONG   gulMaxChange; //    = 0x300;
ULONG   gulTouchSampleRate = TOUCHPANEL_SAMPLE_RATE_HIGH;
ULONG   gulSampleRateID    = TPSM_SAMPLERATE_HIGH_ID;
HANDLE  ghTimer2Event;


extern "C" 
{
    extern DWORD    gdwTouchIstTimeout;
    extern HANDLE   hTouchPanelEvent;
};
DWORD ReadRegistryValue(HKEY hKey, PTSTR szValueName, DWORD dwDefault);


//
// Function Declaration.
//
struct TouchStruct
{
    ULONG   ulX;
    ULONG   ulY;
    ULONG   ulZ1;
    ULONG   ulZ2;
};
static ULONG CalculateInvPressure(TouchStruct *pTouch);  //static BOOL IsTouchValid(TouchStruct *pTouch);
ULONG ADCGetData(ULONG ulADCSwitch, ULONG ulSamples, ULONG ulMaxDiff);
static void DischargeScreen(void);
static void GetTouchValues( TouchStruct *pTouch);
static void TouchPollingMode(void);
static void TouchInterruptMode(void);
static void DelayuS(ULONG ulDelay);

//****************************************************************************
//
// DDSI Implementation
//
//
//  @DOC    EX_TOUCH_DDSI EXTERNAL DRIVERS TOUCH_DRIVER
//  @FUNC   BOOL | TouchDriverCalibrationPointGet |
//
// Gives a single calibration point.
//
//    @XREF
//        <l TouchPanelReadCalibrationPoint.TouchPanelReadCalibrationPoint>
//        <l TouchPanelSetCalibration.TouchPanelSetCalibration>
//        <l TouchPanelReadCalibrationAbort.TouchPanelReadCalibrationAbort>
//
//
//    @COMM
//
// This function is called to get a single calibration point, in screen
// coordinates, when the input system is calibrating the touch driver.  The 
// input system will then draw a target on the screen for the user to press on.
//
// The driver may use the cDisplayX and cDisplayY to compute a coordinate.
// It does not need to remember this computed value internally since it will
// be passed back when the input system has collected all of the points and
// calls <l TouchPanelSetCalibration.TouchPanelSetCalibration>.
//
//****************************************************************************

extern "C" BOOL TouchDriverCalibrationPointGet(TPDC_CALIBRATION_POINT *pTCP)
{
    INT32   cDisplayWidth = pTCP->cDisplayWidth;
    INT32   cDisplayHeight = pTCP->cDisplayHeight;

    int CalibrationRadiusX = cDisplayWidth/10;
    int CalibrationRadiusY = cDisplayHeight/10;


    switch (pTCP->PointNumber) 
    {
        case    0:  // Middle
            pTCP->CalibrationX = cDisplayWidth/2;
            pTCP->CalibrationY = cDisplayHeight/2;
            break;

        case    1:  // Upper Left
            pTCP->CalibrationX = CalibrationRadiusX*2;
            pTCP->CalibrationY = CalibrationRadiusY*2;
            break;

        case    2:  // Lower Left
            pTCP->CalibrationX = CalibrationRadiusX*2;
            pTCP->CalibrationY = cDisplayHeight - CalibrationRadiusY*2;
            break;

        case    3:  // Lower Right
            pTCP->CalibrationX = cDisplayWidth - CalibrationRadiusX*2;
            pTCP->CalibrationY = cDisplayHeight - CalibrationRadiusY*2;
            break;

        case    4:  // Upper Right
            pTCP->CalibrationX = cDisplayWidth - CalibrationRadiusX*2;
            pTCP->CalibrationY = CalibrationRadiusY*2;
            break;

        default:
            pTCP->CalibrationX = cDisplayWidth/2;
            pTCP->CalibrationY = cDisplayHeight/2;
            SetLastError(ERROR_INVALID_PARAMETER);
            return FALSE;
        }

    return TRUE;
}

//****************************************************************************
// @doc EX_TOUCH_DDSI EXTERNAL DRIVERS DDSI TOUCH_PANEL
//
// @func ULONG | DdsiTouchPanelGetDeviceCaps |
//
// Queries capabilities about the physical touch panel device.
//
// @parm ULONG | iIndex |
//
// Specifies the capability to query. They are one of the following:
//
// @flag TPDC_SAMPLERATE_ID |
// The sample rate.
// @flag TPDC_CALIBRATIONPOINTS_ID |
// The X and Y coordinates used for calibration.
// @flag TPDC_CALIBRATIONDATA_ID |
// The X and Y coordinates used for calibration mapping.
//
// @parm LPVOID | lpOutput |
// Points to the memory location(s) where the queried information
// will be placed. The format of the memory referenced depends on
// the setting of iIndex. If 0, returns the number of words
// required for the output data.
//
// @rdesc
// The return values is set to the amount of information supplied and depends
// on the setting of the iIndex argument.
//
// @comm
// Implemented in the PDD.
//
//****************************************************************************

extern "C" BOOL DdsiTouchPanelGetDeviceCaps(INT iIndex,LPVOID  lpOutput)
{

    if (lpOutput == NULL) 
    {
        ERRORMSG(1,(__TEXT("TouchPanelGetDeviceCaps: invalid parameter.\r\n")));
        SetLastError(ERROR_INVALID_PARAMETER);
        return FALSE;
    }

    switch(iIndex) 
    {
        case TPDC_SAMPLE_RATE_ID:
        {
            TPDC_SAMPLE_RATE    *pTSR = (TPDC_SAMPLE_RATE*)lpOutput;

            pTSR->SamplesPerSecondLow       = TOUCHPANEL_SAMPLE_RATE_LOW;
            pTSR->SamplesPerSecondHigh      = TOUCHPANEL_SAMPLE_RATE_HIGH;
            pTSR->CurrentSampleRateSetting  = (1 - gulSampleRateID);
            break;
        }

        case TPDC_CALIBRATION_POINT_COUNT_ID:
        {
            TPDC_CALIBRATION_POINT_COUNT *pTCPC = (TPDC_CALIBRATION_POINT_COUNT*)lpOutput;
            pTCPC->flags = 0;
            pTCPC->cCalibrationPoints = CALIBRATION_POINTS;
            break;
        }

        case TPDC_CALIBRATION_POINT_ID:
            return(TouchDriverCalibrationPointGet((TPDC_CALIBRATION_POINT*)lpOutput));

        default:
            ERRORMSG(1,(__TEXT("TouchPanelGetDeviceCaps: invalid parameter.\r\n")));
            SetLastError(ERROR_INVALID_PARAMETER);
            return FALSE;
    }
    return TRUE;
}

//****************************************************************************
//
// @doc EX_TOUCH_DDSI EXTERNAL DRIVERS DDSI TOUCH_PANEL
//
// @func BOOL | DdsiTouchPanelSetMode |
// Sets information about the physical touch panel device.
//
// @parm ULONG | iIndex |
// Specifies the mode to set. They are one of the following:
//
// @flag TPSM_SAMPLERATE_HIGH_ID |
// Sets the sample rate to the high rate.
// @flag TPSM_SAMPLERATE_LOW_ID |
// Sets the sample rate to the low rate.
//
// @parm LPVOID | lpInput |
// Points to the memory location(s) where the update information
// resides. The format of the memory referenced depends on the
// Points to the memory location(s) where the queried information
// will be placed.
//
// @rdesc
// If the function succeeds the return value is TRUE, otherwise, it is FALSE.
//
// @comm
// Implemented in the PDD.
//
//****************************************************************************

BOOL DdsiTouchPanelSetMode(INT iIndex,LPVOID  lpInput)
{
    BOOL  ReturnCode = FALSE;

    //RETAILMSG(1,(TEXT("DdsiTouchPanelSetMode(0x%x)\r\n"),iIndex));

    switch (iIndex) 
    {
        case TPSM_SAMPLERATE_LOW_ID:
            gulTouchSampleRate = TOUCHPANEL_SAMPLE_RATE_LOW;
            gulSampleRateID    = TPSM_SAMPLERATE_LOW_ID; 
            SetLastError(ERROR_SUCCESS);
            ReturnCode = TRUE;
            break;
        case TPSM_SAMPLERATE_HIGH_ID:
            gulTouchSampleRate = TOUCHPANEL_SAMPLE_RATE_HIGH;
            gulSampleRateID    = TPSM_SAMPLERATE_HIGH_ID; 
            SetLastError(ERROR_SUCCESS);
            ReturnCode = TRUE;
            break;
        default:
            SetLastError(ERROR_INVALID_PARAMETER);
            break;
    }
    return (ReturnCode);
}



//****************************************************************************
//
// @doc EX_TOUCH_DDSI EXTERNAL DRIVERS DDSI TOUCH_PANEL
//
// @func BOOL | DdsiTouchPanelEnable |
// Powers up and initializes the touch panel hardware for operation.
//
// @rdesc
// If the function succeeds, the return value is TRUE; otherwise, it is FALSE.
//
// @comm
// Implemented in the PDD.
//
//****************************************************************************

BOOL DdsiTouchPanelEnable()
{
    DEBUGMSG(ZONE_INIT || ZONE_PDD,(TEXT("DdsiTouchPanelEnable\r\n")));
    BOOL    bReturnValue = TRUE;
    HKEY    hKey;


    //
    // Program the Setup register.
    //
    *TOUCH_TSSETUP  = gulSetup =  
                        TSSETUP_NSMP_32 | TSSETUP_DEV_64 | 
                        ((128<<TSSETUP_SDLY_SHIFT) & TSSETUP_SDLY_MASK) |
                        ((128<<TSSETUP_DLY_SHIFT)  & TSSETUP_DLY_MASK);

    //
    // Initialize the second program setup register to zero.
    //
    *TOUCH_TSSETUP2  = TSSETUP2_DEFAULT;

    //
    // Set the touch settings.
    //
    *TOUCH_TSSWLOCK = 0xAA;
    *TOUCH_TSDIRECT = SWITCH_DISCHARGE;

    //
    // Set the rest of the touch settings. 
    //
    *TOUCH_TSSWLOCK = 0xAA;
    *TOUCH_XSAMPLE  = SWITCH_Z1;

    *TOUCH_TSSWLOCK = 0xAA;
    *TOUCH_YSAMPLE  = SWITCH_Z2;

    *TOUCH_TSSWLOCK = 0xAA;
    *TOUCH_TSDETECT = SWITCH_DETECT;


    *TOUCH_TSSWLOCK = 0xAA;
    *TOUCH_DISCARGE = SWITCH_DISCHARGE;

    // X,YMin set to 0x40 = have to drag that many pixels for a new irq.
    // X,YMax set to 0x40 = 1024 pixels is the maximum movement within the
    // time scan limit.
    //
    *TOUCH_TSMAXMIN =   ((50 <<TSMAXMIN_XMIN_SHIFT) & TSMAXMIN_XMIN_MASK) |
                        ((50 <<TSMAXMIN_YMIN_SHIFT) & TSMAXMIN_YMIN_MASK) |
                        ((0xFF<<TSMAXMIN_XMAX_SHIFT) & TSMAXMIN_XMAX_MASK) |                            
                        ((0xFF<<TSMAXMIN_YMAX_SHIFT) & TSMAXMIN_YMAX_MASK);

    //
    // Callibrate the DDK stall counter.
    //
    CalibrateStallCounter();


    //
    // Change the touch screen to interrupt mode.
    //
    TouchInterruptMode();

    RegOpenKeyEx
    ( 
        HKEY_LOCAL_MACHINE,
        TEXT("\\HARDWARE\\DEVICEMAP\\TOUCH"),
        0,
        0,
        &hKey
    ); 


    //
    // Read in the registry values.
    //
    gulMinInvPressure   = ReadRegistryValue(hKey, REG_VALUE_MIN_INV_PRESSURE, 0x1500);
    gulMaxInvPressure   = ReadRegistryValue(hKey, REG_VALUE_MAX_INV_PRESSURE, 0x4400);
    gulMaxChange        = ReadRegistryValue(hKey, REG_VALUE_MAX_CHANGE, 0x120);

    //
    // Close the registry key value.
    //
    RegCloseKey( hKey);

    //
    // Create an event for sub 1 Millisecond times.
    //
    ghTimer2Event = CreateEvent(NULL,FALSE, FALSE, NULL);
    if(!ghTimer2Event)
    {
        bReturnValue = FALSE;
    }

    //
    // Allocate timer2.
    //
    if(bReturnValue)
    {
        bReturnValue = InterruptInitialize(SYSINTR_TIMER2, ghTimer2Event, NULL, 0);
    }


    return(bReturnValue);
}



//****************************************************************************
// @doc EX_TOUCH_DDSI EXTERNAL DRIVERS DDSI TOUCH_PANEL
//
// @func ULONG | DdsiTouchPanelDisable |
// Powers down the touch panel device.
//
// @comm
// Implemented in the PDD.
//
//****************************************************************************

VOID DdsiTouchPanelDisable()
{
    DEBUGMSG(ZONE_INIT|| ZONE_PDD,(TEXT("DdsiTouchPanelDisable\r\n")));

    InterruptDisable( SYSINTR_TIMER2) ;

    TouchPollingMode( );
}

//****************************************************************************
//
// @doc EX_TOUCH_DDSI EXTERNAL DRIVERS DDSI TOUCH_PANEL
//
// @func LONG | DdsiTouchPanelAttach |
// This routine no longer does anything.  All functionallity has been moved
// from here into DdsiTouchPanelEnable to allow this code to be statically
// linked with GWE rather than existing as a DLL.  Technically, when built
// as a DLL we should keep an attach count and only allow touh.dll to be
// loaded once.  But, since we are loaded at boot time by GWE, there is
// no real concern about multiple loads (unless gwe has a bug!).
//
// @rdesc
// Always returns 0
//
// @comm
// Implemented in the PDD.
//
//****************************************************************************

LONG DdsiTouchPanelAttach()
{
    DEBUGMSG(ZONE_INIT|| ZONE_PDD,(TEXT("DdsiTouchPanelAttach\r\n")));


    return(1); 
}

//****************************************************************************
//
// @doc EX_TOUCH_DDSI EXTERNAL DRIVERS DDSI TOUCH_PANEL
//
// @func LONG | DdsiTouchPanelDetach |
// See the descrition for attach.  All functionallity has been moved into
// DdsiTouchPanelDisable.
//
// @rdesc
// The updated global counter.  If the initializations failed, the returned
// count is 0.
//
// @comm
// Implemented in the PDD.
//

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -