wince gt9xx

//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft 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.
//
/*++
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.
Copyright (c) 2005. Samsung Electronics, co. ltd  All rights reserved.


Module Name:  


Abstract:


    Platform dependent TOUCH initialization functions


rev:
2004.4.27 : S3C2440 port (Hoyjoon Kim)
2005.05.23  : Magneto porting revision (junkim)
2006.06.29  :  S3C2416 port(JJG)


Notes: 
--*/




#include
#include
#include
#include
#include
#include


#include
#include
#include "i2c.h"


#include "gt9xx.h"
#define PUBLIC
#define PRIVATE static


#define TOUCH_12BIT_SUPPORT


#define TSP_SAMPLE_RATE_LOW 50  
#define TSP_SAMPLE_RATE_HIGH 100
#define TSP_TIMER_DIVIDER D2 // 
#define TSP_TICK_1S (S3C2416_PCLK / (PRESCALER+1) / TSP_TIMER_DIVIDER /1000)
#define TSP_SAMPLETICK_LOW (TSP_TICK_1S*1000/ TSP_SAMPLE_RATE_LOW - 1)
#define TSP_SAMPLETICK_HIGH (TSP_TICK_1S*1000/ TSP_SAMPLE_RATE_HIGH - 1) // 1020 = PCLK/PRESCALE(=250)/DIV3(=2)/1000(1SEC)*(1000/100)




#define ADCPRS 49 // 200
#define TS_DBGON 0


/*
#define TSP_MINX 74 //105  //85
#define TSP_MINY 98 //70 //105


#define TSP_MAXX 932 //910 //965
#define TSP_MAXY 920 // 940 //980
*/


#define TSP_CHANGE 15
#define TSP_INVALIDLIMIT 40




//#define TSP_LCDX (LCD_XSIZE_TFT * 4)
//#define TSP_LCDY (LCD_YSIZE_TFT * 4)


#define TSP_SAMPLE_NUM




#ifndef OAL_INTR_FORCE_STATIC
#define OAL_INTR_FORCE_STATIC       (1 << 2)
#endif


DWORD gIntrTouch        = SYSINTR_NOP;
DWORD gIntrTouchChanged = SYSINTR_NOP;


extern "C" const int MIN_CAL_COUNT   = 1;


PRIVATE INT TSP_CurRate = 1;


static BOOL Touch_Pen_Filtering(INT *px, INT *py); 


PRIVATE volatile S3C2416_IOPORT_REG * v_pIOPregs;
PRIVATE volatile S3C2416_INTR_REG * v_pINTregs;
PRIVATE volatile S3C2416_PWM_REG * v_pPWMregs;


PRIVATE BOOL bTSP_DownFlag;


VOID TSP_VirtualFree(VOID);
BOOL TSP_VirtualAlloc(VOID);
#define   TOUCH_ADDR        0xBA


#define TOUCH_WRITE     (0xBA)// (0x38 + 0)
#define TOUCH_READ      (0xBB)//(0x38 + 1)




#define GTP_POLL_TIME         10    
#define GTP_ADDR_LENGTH       2
#define GTP_CONFIG_MIN_LENGTH 186
#define GTP_CONFIG_MAX_LENGTH 240
#define FAIL                  0
#define SUCCESS               1
#define SWITCH_OFF            0
#define SWITCH_ON             1


//******************** For GT9XXF Start **********************//
#define GTP_REG_BAK_REF                 0x99D0
#define GTP_REG_MAIN_CLK                0x8020
#define GTP_REG_CHIP_TYPE               0x8000
#define GTP_REG_HAVE_KEY                0x804E
#define GTP_REG_MATRIX_DRVNUM           0x8069     
#define GTP_REG_MATRIX_SENNUM           0x806A


#define GTP_FL_FW_BURN              0x00
#define GTP_FL_ESD_RECOVERY         0x01
#define GTP_FL_READ_REPAIR          0x02


#define GTP_BAK_REF_SEND                0
#define GTP_BAK_REF_STORE               1
#define CFG_LOC_DRVA_NUM                29
#define CFG_LOC_DRVB_NUM                30
#define CFG_LOC_SENS_NUM                31


#define GTP_CHK_FW_MAX                  40
#define GTP_CHK_FS_MNT_MAX              300
#define GTP_BAK_REF_PATH                "/data/gtp_ref.bin"
#define GTP_MAIN_CLK_PATH               "/data/gtp_clk.bin"
#define GTP_RQST_CONFIG                 0x01
#define GTP_RQST_BAK_REF                0x02
#define GTP_RQST_RESET                  0x03
#define GTP_RQST_MAIN_CLOCK             0x04
#define GTP_RQST_RESPONDED              0x00
#define GTP_RQST_IDLE                   0xFF


//******************** For GT9XXF End **********************//
// Registers define
#define GTP_READ_COOR_ADDR    0x814E
#define GTP_REG_SLEEP         0x8040
#define GTP_REG_SENSOR_ID     0x814A
#define GTP_REG_CONFIG_DATA   0x8047
#define GTP_REG_VERSION       0x8140


#define RESOLUTION_LOC        3
#define TRIGGER_LOC           8
#define u8 UCHAR
#define u16 UINT


u8 config[GTP_CONFIG_MAX_LENGTH + GTP_ADDR_LENGTH]
                = {GTP_REG_CONFIG_DATA >> 8, GTP_REG_CONFIG_DATA & 0xff};
static HANDLE                        hI2C;   // I2C Bus Driver












#define RETRY_COUNT     3




static  bool  IICinit( WORD wSlaveAddr, DWORD dwSpead)
{
DWORD dwErr = ERROR_SUCCESS, bytes;
   
   I2C_INIT_DESC I2CInitDesc;
hI2C = CreateFile( L"I2C0:",
                             GENERIC_READ|GENERIC_WRITE,
                             FILE_SHARE_READ|FILE_SHARE_WRITE,
                             NULL, OPEN_EXISTING, 0, 0);
                   
    if ( INVALID_HANDLE_VALUE == hI2C ) {
        dwErr = GetLastError();
        RETAILMSG(1, (TEXT("Error %d opening device '%s' \r\n"), dwErr, L"I2C0:" ));
        return FALSE;
    }
  /*
I2CInitDesc.IN_wSlaveAddr    = wSlaveAddr; 
I2CInitDesc.IN_dwClockSpeed     = dwSpead;
I2CInitDesc.OUT_dwActualSpeed   = 0;


if(!DeviceIoControl(hI2C,
                   IOCTL_I2C_INIT, 
                   &I2CInitDesc, sizeof(I2CInitDesc), 
                   NULL, 0,
                   NULL, NULL) ) 
{
 dwErr = GetLastError();
        RETAILMSG(1,(TEXT("I2CInitDesc ERROR: %u \r\n"), dwErr));
  return FALSE;



RETAILMSG(0, (L"[TOCUH] ACTUAL I2C CLCOK SPEAD : %d Khz\r\n", I2CInitDesc.OUT_dwActualSpeed));
*/
return TRUE;
    
}




static DWORD
HW_WriteRegisters(
  //  PUCHAR pBuff,   // Optional buffer
 //   DWORD nRegs     // number of registers
// PBYTE pWData, DWORD dwWDatas
PBYTE pWData, DWORD dwWDatas, BOOL bStop, PBYTE pRData, DWORD dwRDatas
    )
{
   BOOL bWrite = TRUE;
    DWORD dwTry;
    I2C_WRITE_DESC I2CWriteDesc;
    
    DWORD dwErr=0;
I2CWriteDesc.IO_pbtData = pWData;
I2CWriteDesc.IN_dwData  = dwWDatas;
I2CWriteDesc.IN_bStop   = TRUE;


    for(dwTry=0; dwTry     {


    if(!DeviceIoControl(hI2C,
                       IOCTL_I2C_GENERAL_WRITE, 
                       &I2CWriteDesc, sizeof(I2C_WRITE_DESC), 
                       NULL, 0,
                       NULL, NULL)) 
         {
             dwErr = GetLastError();
        RETAILMSG(1,(TEXT("IOCTL_IIC_WRITE ERROR: %u \r\n"), dwErr));
   
  if(dwTry == (RETRY_COUNT-1)) bWrite = FALSE;
            continue;
        }
/*
I2CWriteDesc.IO_pbtData = pRData;
I2CWriteDesc.IN_dwData = dwRDatas;
I2CWriteDesc.IN_bStop = TRUE;
if(!DeviceIoControl(hI2C,
IOCTL_I2C_GENERAL_WRITE, 
&I2CWriteDesc, sizeof(I2C_WRITE_DESC), 
NULL, 0,
NULL, NULL)) 
{
ERRMSG((L"[TOCUH] IOCTL_I2C_GENERAL_WRITE ERROR: %u \r\n", GetLastError()));
if(dwTry == (RETRY_COUNT-1)) bWrite = FALSE;
continue;
}
*/


        break;
    }




return bWrite;
}




static BOOL
HW_ReadRegisters(HANDLE hI2C, PBYTE pWData, DWORD dwWDatas, BOOL bStop, PBYTE pRData, DWORD dwRDatas)
{
BOOL bRead = TRUE;
    DWORD dwTry;
I2C_WRITE_DESC I2CWriteDesc;
    I2C_READ_DESC I2CReadDesc;
    DWORD dwErr=0;


I2CWriteDesc.IO_pbtData = pWData;
I2CWriteDesc.IN_dwData  = dwWDatas;
I2CWriteDesc.IN_bStop   = FALSE;


    for(dwTry=0; dwTry     {


    if (!DeviceIoControl(hI2C,
                        IOCTL_I2C_GENERAL_WRITE, 
                        &I2CWriteDesc, sizeof(I2C_WRITE_DESC), 
                        NULL, 0,
                        NULL, NULL)) 
    {
     dwErr = GetLastError();
        RETAILMSG(1,(TEXT("IOCTL_IIC_READ_W ERROR: %u \r\n"), dwErr));
    if(dwTry == (RETRY_COUNT-1)) bRead = FALSE;
            continue;
   
    else
    {
    I2CReadDesc.IO_pbtData = pRData;
    I2CReadDesc.IN_dwData = dwRDatas;
    I2CReadDesc.IN_bStop = TRUE;


    if (!DeviceIoControl(hI2C,
                        IOCTL_I2C_GENERAL_READ, 
                        &I2CReadDesc, sizeof(I2C_READ_DESC), 
                        NULL, 0,
                        NULL, NULL))
    {
    RETAILMSG((L"[Read_HDCP_I2C] IOCTL_I2C_GENERAL_READ ERROR: %u \r\n", GetLastError()));
    if(dwTry == (RETRY_COUNT-1)) bRead = FALSE;
                continue;
    }
    }


        break;
    }
    
return bRead;
}






static DWORD
HW_ReadRegisters1(
    PUCHAR StartReg,       // Start Register
    PUCHAR pBuff,     // Optional buffer
    DWORD nRegs         // Number of Registers
    )
{
//return TRUE;
    DWORD dwErr=0;
    DWORD bytes;

//RETAILMSG(1,(TEXT("StartReg++++ 0x%x 0x%x\r\n"),StartReg[0],StartReg[1])); 
    I2C_IO_DESC IIC_AddressData, IIC_Data;


//RETAILMSG(1,(TEXT("HW_ReadRegisters++ \r\n"))); 

    //IIC_AddressData.SlaveAddr = TOUCH_WRITE;
    //IIC_AddressData.Data = &StartReg;
    //IIC_AddressData.Count = 1;
    
    IIC_Data.SlaveAddr = TOUCH_READ;
IIC_Data.WordAddr = StartReg;
    IIC_Data.Data =  pBuff;
    IIC_Data.Count = nRegs;
    
    // use iocontrol to read    
    if ( !DeviceIoControl(hI2C,
                          IOCTL_I2C_READ, 
                          &IIC_Data, sizeof(I2C_IO_DESC), 
                          NULL, 0,
                          &bytes, NULL) ) 
    {
        dwErr = GetLastError();
        RETAILMSG(1,(TEXT("IOCTL_IIC_WRITE ERROR: %u \r\n"), dwErr));
    }   


    
    if ( !dwErr ) {




    } else {        
        RETAILMSG(1,(TEXT("I2CRead ERROR: %u \r\n"), dwErr));
    }            


RETAILMSG(0,(TEXT("HW_ReadRegisters-- \r\n"))); 

    return dwErr;
}




static DWORD
HW_WriteRegisters1(
PUCHAR StartReg,   // Start Register
    PUCHAR pBuff,   // Optional buffer
    DWORD nRegs     // number of registers
    )
{
//return TRUE;
    DWORD dwErr=0;
    DWORD bytes;
    I2C_IO_DESC IIC_Data;
    
    RETAILMSG(0,(TEXT("HW_WriteRegisters++ \r\n")));    
    
    IIC_Data.SlaveAddr = TOUCH_WRITE;
    IIC_Data.Count    =  nRegs;
IIC_Data.WordAddr = StartReg;

    IIC_Data.Data = pBuff;
    
    // use iocontrol to write
    if ( !DeviceIoControl(hI2C,
                          IOCTL_I2C_WRITE, 
                          &IIC_Data, sizeof(I2C_IO_DESC), 
                          NULL, 0,
                          &bytes, NULL) ) 
    {
        dwErr = GetLastError();
        RETAILMSG(1,(TEXT("IOCTL_IIC_WRITE ERROR: %u \r\n"), dwErr));
    }   


    if ( dwErr ) {
        RETAILMSG(1, (TEXT("I2CWrite ERROR: %u \r\n"), dwErr));
    }            
    RETAILMSG(0,(TEXT("HW_WriteRegisters-- \r\n")));    


    return dwErr;
}




void gtp_read_version()
{
unsigned char Wrbuf[3];
unsigned char buf[31];

unsigned char buf1[31];
int i;
u16 addr=GTP_REG_VERSION;
Wrbuf[0] = addr>>8;

Wrbuf[1] = addr&0xff;
RETAILMSG(1,(_T("\r\ngtp_read_version")));
for(i=0;i<6;i++)
{
Wrbuf[0] = addr>>8;

Wrbuf[1] = addr&0xff;
//HW_ReadRegisters(hI2C, Wrbuf,2,TRUE,buf, 1);

HW_ReadRegisters1( Wrbuf,buf, 1);
buf1[i]=buf[0];
//RETAILMSG(1,(_T("0x%x  0x%x 0x%x\r\n"),Wrbuf[0],Wrbuf[1],buf[0]));
addr++;
}
RETAILMSG(1,(_T("%c%c%c_%02x%02x"),buf1[0],buf1[1],buf1[2],buf1[5],buf1[4]));
//RETAILMSG(1,(_T("0x%x 0x%x 0x%x_0x%x 0x%x"),buf1[0],buf1[1],buf1[2],buf1[5],buf1[4]));


RETAILMSG(1,(_T("\r\n")));


}
static void gup_set_ic_msg( u16 addr, u8 val)
{
unsigned char Wrbuf[3];
unsigned char buf[31];
int i;


Wrbuf[0] = addr>>8;
Wrbuf[1] = addr&0xff;

Wrbuf[2] = val;
buf[0]=val;
//HW_WriteRegisters(Wrbuf,3,TRUE,buf, 1);
HW_WriteRegisters1(Wrbuf,Wrbuf, 1);


HW_ReadRegisters1(Wrbuf,buf, 1);


//HW_ReadRegisters(hI2C, Wrbuf,2,TRUE,buf, 1);
RETAILMSG(0,(_T("\r\ngup_set_ic_msg 0x%x"),buf[0]));


}


void gup_hold_ss51_dsp()
{
unsigned char Wrbuf[3];
unsigned char buf[31];
int i;
u16 addr=_rRW_MISCTL__SWRST_B0_;


//Wrbuf[0] = 0x41;
//Wrbuf[1] = 0xe4;


//HW_ReadRegisters1(Wrbuf,buf,1);



//RETAILMSG(1,(_T("\r\ngup_hold_ss51_dsp gup_hold_ss51_dsp 0x%x"),buf[0]));
//while(1);
gup_set_ic_msg(_rRW_MISCTL__SWRST_B0_,0x0C);
//HW_ReadRegisters(hI2C, Wrbuf,2,TRUE,buf, 1);
Wrbuf[0] = addr>>8;
Wrbuf[1] = addr&0xff;

HW_ReadRegisters1( Wrbuf,buf, 1);
if(0x0C ==buf[0] )
RETAILMSG(1,(_T("\r\n[enter_update_mode]Hold ss51 & dsp confirm SUCCESS")));
else 
{
RETAILMSG(1,(_T("\r\nerror gup_hold_ss51_dsp 0x%x"),buf[0]));


addr=0x41e4;
Wrbuf[0] = addr>>8;
Wrbuf[1] = addr&0xff;

//HW_ReadRegisters(hI2C, Wrbuf,2,TRUE,buf, 1);
HW_ReadRegisters1(Wrbuf,buf, 1);
RETAILMSG(1,(_T("\r\n0x41E4 0x%x"),buf[0]));
}


gup_set_ic_msg(0x4010, 0x00);
gup_set_ic_msg( _bRW_MISCTL__TMR0_EN, 0x00);
gup_set_ic_msg( _bRW_MISCTL__CACHE_EN, 0x00);
gup_set_ic_msg( _rRW_MISCTL__BOOTCTL_B0_, 0x02);
gup_set_ic_msg( _bWO_MISCTL__CPU_SWRST_PULSE, 0x01);

}
void gup_enter_update_mode_fl()
{
v_pIOPregs->GPFCON = (v_pIOPregs->GPFCON & ~(0x3<<10)) | (0x1<<10);
v_pIOPregs->GPFDAT &= ~(0x1<<5);


Sleep(2);


//delay_ms(2);

v_pIOPregs->GPGCON &= (0x3<<8);
v_pIOPregs->GPGCON |= (0x1<<8);
v_pIOPregs->GPGDAT&= ~(0x1<<4);





Sleep(2);
//delay_ms(2);
v_pIOPregs->GPFCON = (v_pIOPregs->GPFCON & ~(0x3<<10)) | (0x1<<10);
v_pIOPregs->GPFDAT |= (0x1<<5);


Sleep(5);


//delay_ms(5);


gup_hold_ss51_dsp();
gup_set_ic_msg( _rRW_MISCTL__BOOT_CTL_, 0x00);
gup_set_ic_msg( _rRW_MISCTL__BOOT_OPT_B0_, 0x00);
gup_set_ic_msg( _bRW_MISCTL__MEM_CD_EN, 0x01);




}




void gtp_int_sync(long ms)
{
v_pIOPregs->GPGCON &=~(0x3<<8);
v_pIOPregs->GPGCON |= (0x1<<8);
v_pIOPregs->GPGDAT&= ~(0x1<<4);
   // delay_ms(ms);

Sleep(ms);
   v_pIOPregs->GPGCON &=~(0x3<<8);
   v_pIOPregs->GPGCON |= (0x2<<8);
}
void gtp_reset_guitar()
{
v_pIOPregs->GPFCON = (v_pIOPregs->GPFCON & ~(0x3<<10)) | (0x1<<10);
v_pIOPregs->GPFDAT &= ~(0x1<<5);




Sleep(20);
// delay_ms(20);
v_pIOPregs->GPGCON &=~(0x3<<8);
v_pIOPregs->GPGCON |= (0x1<<8);
v_pIOPregs->GPGDAT&= ~(0x1<<4);


Sleep(2);
//delay_ms(2);
v_pIOPregs->GPFDAT |= (0x1<<5);


Sleep(6);
// delay_ms(6);


//  gtp_int_sync(50);  






}


void gtp_fw_startup()
{
unsigned char Wrbuf[3];
unsigned char buf[31];


gup_set_ic_msg(0x8041,0xAA);
gup_set_ic_msg(0x4180,0x00);
Wrbuf[0] = 0x80;
Wrbuf[1] = 0x41;
gtp_int_sync(25);  
 
//HW_ReadRegisters(hI2C, Wrbuf,2,TRUE,buf, 1);
HW_ReadRegisters1(Wrbuf,buf, 1);


if(0xAA == buf[0])
{

RETAILMSG(1,(_T("error 0x%x"),buf[0]));
}
else
{
RETAILMSG(1,(_T("gtp_fw_startup succed 0x%x"),buf[0]));
gup_set_ic_msg(0x8041,0xAA);
}


}




static long gup_burn_fw_proc( int start_addr, int start_index, int burn_len)
{


unsigned char Wrbuf[1026];//={0};
unsigned char buf[31];
int i;
int j;
int m;
j=burn_len/1024;
for(m=0;m {
memset(Wrbuf,0,1026);
Wrbuf[0] = (start_addr)>>8;
Wrbuf[1] = (start_addr)&0xff;
for(i=0;i<1024;i++)
{
Wrbuf[i+2] = gtp_default_FW_fl[FW_HEAD_LENGTH + start_index+i];


}
// RETAILMSG(1,(_T("\r\gup_burn_fw_proc%x 0x%x 0x%x"),start_addr,start_index,m));

HW_WriteRegisters1(Wrbuf,Wrbuf, 1024);
//HW_WriteRegisters(Wrbuf,2+1024,TRUE,buf, 1026);
start_index=start_index+1024;
start_addr=start_addr+1024;
}
/*
Wrbuf[2] = gtp_default_FW_fl[FW_HEAD_LENGTH + start_index];
for(i=0;i {
Wrbuf[0] = (start_addr+i)>>8;
Wrbuf[1] = (start_addr+i)&0xff;

Wrbuf[2] = gtp_default_FW_fl[FW_HEAD_LENGTH + start_index+i];
buf[0]=Wrbuf[2];
HW_WriteRegisters(Wrbuf,3,TRUE,buf, 1);
//RETAILMSG(1,(_T("gup_burn_fw_proc%x 0x%x 0x%x"),buf[0],buf[1],Wrbuf[i]));





}
*/
    return 1;


}


static long gup_burn_fw_proc1( int start_addr, int start_index, int burn_len)
{


unsigned char Wrbuf[1026];//={0};
unsigned char buf[31];
int i;
int j;
int m;
u8 rqst_buf[3] = {0x80, 0x43};
u8 p_main_clk[6] = {71,71,71,71,71,157};
u8 p_bak_ref[276]={0};
p_bak_ref[275]=1;
j=burn_len/1024;
if(j==0)
j=1;
for(m=0;m {
memset(Wrbuf,0,1026);
Wrbuf[0] = (start_addr)>>8;
Wrbuf[1] = (start_addr)&0xff;
for(i=0;i {
Wrbuf[i+2] = p_bak_ref[i];


}
// RETAILMSG(1,(_T("\r\gup_burn_fw_proc%x 0x%x 0x%x"),start_addr,start_index,m));
HW_WriteRegisters1(Wrbuf,Wrbuf, burn_len);


// HW_WriteRegisters(Wrbuf,2+burn_len,TRUE,buf, burn_len+2);
//start_index=start_index+1024;
//start_addr=start_addr+1024;
}
/*
Wrbuf[2] = gtp_default_FW_fl[FW_HEAD_LENGTH + start_index];
for(i=0;i {
Wrbuf[0] = (start_addr+i)>>8;
Wrbuf[1] = (start_addr+i)&0xff;

Wrbuf[2] = gtp_default_FW_fl[FW_HEAD_LENGTH + start_index+i];
buf[0]=Wrbuf[2];
HW_WriteRegisters(Wrbuf,3,TRUE,buf, 1);
//RETAILMSG(1,(_T("gup_burn_fw_proc%x 0x%x 0x%x"),buf[0],buf[1],Wrbuf[i]));





}
*/
    return 1;


}


void gup_check_and_repair( int start_addr, int start_index, int burn_len)
{
unsigned char Wrbuf[3];
unsigned char buf[31];
int i;



Wrbuf[2] = gtp_default_FW_fl[FW_HEAD_LENGTH + start_index];
for(i=0;i {
Wrbuf[0] = (start_addr+i)>>8;
Wrbuf[1] = (start_addr+i)&0xff;

Wrbuf[2] = gtp_default_FW_fl[FW_HEAD_LENGTH + start_index+i];
//buf[0]=Wrbuf[2];

//HW_ReadRegisters(hI2C, Wrbuf,2,TRUE,buf, 1);
//if(i<10)
// RETAILMSG(1,(_T("\r\ngup_check_and_repair%x 0x%x 0x%x"),start_addr+i,buf[0],Wrbuf[2]));





}
// RETAILMSG(1,(_T("\r\ngup_check_and_repair123%x 0x%x 0x%x\r\n"),burn_len,buf[0],Wrbuf[2]));



}


void gtp_gt9xxf_init()
{
gup_set_ic_msg( _bRW_MISCTL__SRAM_BANK, 0);
gup_burn_fw_proc( 0xC000, 0x0000, 0x4000);
gup_check_and_repair( 0xC000, 0x0000, 0x4000);


gup_set_ic_msg( _bRW_MISCTL__SRAM_BANK, 1);
gup_burn_fw_proc( 0xC000, 0x4000, 0x4000);
gup_check_and_repair( 0xC000, 0x4000, 0x4000);

gup_set_ic_msg( _bRW_MISCTL__SRAM_BANK, 2);
gup_burn_fw_proc( 0xC000, 0x8000, 0x1000);
gup_check_and_repair( 0xC000, 0x8000, 0x1000);




}
void gtp_init_panel()
{
unsigned char Wrbuf[244];
unsigned char buf[31];
int val;
int i;
long value=0;
u8 cfg_info_group1[] = CTP_CFG_GROUP1;
  //  memset(&config[GTP_ADDR_LENGTH], 0, GTP_CONFIG_MAX_LENGTH);
//memcpy(&config[GTP_ADDR_LENGTH], send_cfg_buf[0], 242);
val=0x8047;
  for(i=0;i<226;i++)
{
 value+=cfg_info_group1[i];


}
  
  RETAILMSG(1,(_T("value 0x%x\r\n"),value));
 // cfg_info_group1[226]=
for(i=0;i<240;i++)
{
Wrbuf[i+2]=cfg_info_group1[i];
}
//RETAILMSG(1,(_T("gtp_init_panel")));
//for(i=0;i<242;i++)
{
Wrbuf[0] = (val)>>8;
Wrbuf[1] = (val)&0xff;

//Wrbuf[2] = cfg_info_group1[i];
buf[0]=cfg_info_group1[0];

//RETAILMSG(1,(_T("gtp_init_panel0x%x 0x%x 0x%x"),buf[0],buf[1],cfg_info_group1[i]));
//HW_WriteRegisters(Wrbuf,242,TRUE,buf, 242);
HW_WriteRegisters1(Wrbuf,Wrbuf, 240);




}
val=0x8047;
#if 0
RETAILMSG(1,(_T("++++++HW_ReadRegisters\r\n")));


for(i=0;i<242;i++)
{
Wrbuf[0] = (val+i)>>8;
Wrbuf[1] = (val+i)&0xff;


HW_ReadRegisters1(Wrbuf,buf, 1);
RETAILMSG(1,(_T("0x%x \r\n"),buf[0]));
if(((i+1)%10==0))
{
//RETAILMSG(1,(_T("\r\n")));


}
}

RETAILMSG(1,(_T("-------------HW_ReadRegisters\r\n")));
#endif

}
void RQST_MAIN_CLOCK()
{
#if 1
   
UINT32 irqBuf;
unsigned char Wrbuf[3];
int addr;
unsigned char buf[31];
int i;
int j;
u8 rqst_buf[3] = {0x80, 0x43};
u8 p_main_clk[6] = {71,71,71,71,71,157};
u8 p_bak_ref[276]={0};
p_bak_ref[275]=1;

addr=GTP_REG_MAIN_CLK;
for(i=0;i<6;i++)
gup_set_ic_msg( addr++, p_main_clk[i]);

gup_burn_fw_proc1(GTP_REG_BAK_REF,0,276);
gup_set_ic_msg(0x8043, 0);


#endif


}








PRIVATE PVOID
TSP_RegAlloc(PVOID addr, INT sz)
{
PVOID reg;


reg = (PVOID)VirtualAlloc(0, sz, MEM_RESERVE, PAGE_NOACCESS);


if (reg)
{
if (!VirtualCopy(reg, (PVOID)((UINT32)addr >> 8), sz, PAGE_PHYSICAL | PAGE_READWRITE | PAGE_NOCACHE )) 
{
VirtualFree(reg, 0, MEM_RELEASE);
reg = NULL;
}
}


return reg;
}




PRIVATE BOOL
TSP_VirtualAlloc(VOID)
{
BOOL r = FALSE;


    RETAILMSG(0,(TEXT("::: TSP_VirtualAlloc()\r\n")));


do
{
v_pIOPregs = (volatile S3C2416_IOPORT_REG *)TSP_RegAlloc((PVOID)S3C2416_BASE_REG_PA_IOPORT, sizeof(S3C2416_IOPORT_REG));
if (v_pIOPregs == NULL) 
{
ERRORMSG(1,(TEXT("For IOPreg: VirtualAlloc failed!\r\n")));
break;
}


v_pINTregs = (volatile S3C2416_INTR_REG *)TSP_RegAlloc((PVOID)S3C2416_BASE_REG_PA_INTR, sizeof(S3C2416_INTR_REG));
if (v_pINTregs == NULL) 
{
    ERRORMSG(1,(TEXT("For INTregs: VirtualAlloc failed!\r\n")));
   break;
}


v_pPWMregs = (volatile S3C2416_PWM_REG *)TSP_RegAlloc((PVOID)S3C2416_BASE_REG_PA_PWM, sizeof(S3C2416_PWM_REG));
if (v_pPWMregs == NULL) 
{
    ERRORMSG(1,(TEXT("For PWMregs: VirtualAlloc failed!\r\n")));
   break;
}

r = TRUE;
} while (0);


if (!r)
{
TSP_VirtualFree();


RETAILMSG(0,(TEXT("::: TSP_VirtualAlloc() - Fail\r\n")));
}
else
{
RETAILMSG(0,(TEXT("::: TSP_VirtualAlloc() - Success\r\n")));
}




return r;
}


PRIVATE void
TSP_VirtualFree(VOID)
{
    RETAILMSG(0,(TEXT("::: TSP_VirtualFree()\r\n")));


if (v_pIOPregs)
    {
        VirtualFree((PVOID)v_pIOPregs, 0, MEM_RELEASE);
        v_pIOPregs = NULL;
    }
 
    if (v_pINTregs)
    {   
        VirtualFree((PVOID)v_pINTregs, 0, MEM_RELEASE);
        v_pINTregs = NULL;
    } 
    if (v_pPWMregs)
    {   
        VirtualFree((PVOID)v_pPWMregs, 0, MEM_RELEASE);
        v_pPWMregs = NULL;
    } 
}


PRIVATE VOID
TSP_SampleStart(VOID)
{
DWORD tmp;


if (v_pINTregs->INTMSK1 & (1<

tmp = v_pPWMregs->TCON & (~(0xf << 16));


v_pPWMregs->TCON = tmp | (2 << 16); /* update TCVNTB3, stop */
v_pPWMregs->TCON = tmp | (9 << 16); /* interval mode,  start */
// v_pPWMregs->TCON = tmp | (1 << 16); /* one-shot mode,  start */
}


PRIVATE VOID
TSP_SampleStop(VOID)
{
v_pPWMregs->TCON &= ~(1 << 16); /* Timer3, stop */
}




PRIVATE VOID
TSP_PowerOn(VOID)
{
    RETAILMSG(TS_DBGON,(TEXT("::: TSP_PowerOn()\r\n")));




//RETAILMSG(TS_DBGON,(TEXT("Touch::ADCPRS=%d\r\n"),ADCPRS));






    v_pPWMregs->TCFG1  &= ~(0xf << 12); /* Timer3's Divider Value */
#if( TSP_TIMER_DIVIDER == D2 )
    v_pPWMregs->TCFG1  |=  (D1_2   << 12); /* 1/2 */
#elif ( TSP_TIMER_DIVIDER == D4 )
    v_pPWMregs->TCFG1  |=  (D1_4   << 12); /* 1/4 */
#elif ( TSP_TIMER_DIVIDER == D8 )
    v_pPWMregs->TCFG1  |=  (D1_8   << 12); /* 1/8 */
#elif ( TSP_TIMER_DIVIDER == D16 )
    v_pPWMregs->TCFG1  |=  (D1_16   << 12); /* 1/16 */
#endif
     
 
    v_pPWMregs->TCNTB3  = TSP_SAMPLETICK_LOW;//TSP_SAMPLETICK_HIGH; /* Interrupt Frequency */
// v_pPWMregs->TCMPB3  = 0; /**/
}


PRIVATE VOID
TSP_PowerOff(VOID)
{
    RETAILMSG(TS_DBGON,(TEXT("::: TSP_PowerOff()\r\n")));


}


PRIVATE BOOL
TSP_CalibrationPointGet(TPDC_CALIBRATION_POINT *pTCP)
{
    
INT32 cDisplayWidth  = pTCP->cDisplayWidth;
INT32 cDisplayHeight = pTCP->cDisplayHeight;


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


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


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


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


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


case 4:
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;
}

RETAILMSG(TS_DBGON, (TEXT("::: TSP_CalibrationPointGet()\r\n")));
    RETAILMSG(TS_DBGON, (TEXT("cDisplayWidth        : %4d\r\n"), cDisplayWidth     ));
    RETAILMSG(TS_DBGON, (TEXT("cDisplayHeight       : %4d\r\n"), cDisplayHeight    ));
    RETAILMSG(TS_DBGON, (TEXT("CalibrationRadiusX   : %4d\r\n"), CalibrationRadiusX));
    RETAILMSG(TS_DBGON, (TEXT("CalibrationRadiusY   : %4d\r\n"), CalibrationRadiusY));
    RETAILMSG(TS_DBGON, (TEXT("pTCP -> PointNumber  : %4d\r\n"), pTCP->PointNumber));
    RETAILMSG(TS_DBGON, (TEXT("pTCP -> CalibrationX : %4d\r\n"), pTCP->CalibrationX));
    RETAILMSG(TS_DBGON, (TEXT("pTCP -> CalibrationY : %4d\r\n"), pTCP->CalibrationY));


return TRUE;
}
void  TSP_EnableInt()
{


v_pIOPregs->EINTMASK &= ~(0x1<<12);
}


void TSP_DisableInt()
{


v_pIOPregs->EINTMASK |= (0x1<<12); //enable interrupt eint12


}




PRIVATE BOOL
TSP_GetXY(INT *px, INT *py)
{
  int i;
  int num;


  unsigned short x,y;
   
unsigned char Wrbuf[3];
unsigned char buf[31];
int ii;
int start_addr=0x814E;
ii=0;


Wrbuf[0] =0x81;
Wrbuf[1] =0x4E; 
   memset(buf,0,31);


 // HW_ReadRegisters(buf, 0, 31);
 
 
  // RETAILMSG(1,(_T("[TSP]\r\n")));
  
 HW_ReadRegisters1(Wrbuf,buf, 1);
// HW_ReadRegisters(hI2C, Wrbuf,2,TRUE,buf, 1);
if(buf[0]==0)
{
//RETAILMSG(1,(_T("0x%x 0x%x 0x%x "),buf[0],Get_PinData(v_pGPIOregs,GPH01_Input)\
//, Get_EXTINT_TRLVL(v_pGPIOregs, EXT_INT_1)
//));
Wrbuf[0] = 0x80;
Wrbuf[1] = 0x43;
//HW_ReadRegisters(hI2C, Wrbuf,2,TRUE,buf, 1);

HW_ReadRegisters1(Wrbuf,buf, 1);
if(buf[0]==4)


{
RETAILMSG(1,(_T("\r\n 0x%x"),buf[0]));
}

if(buf[0]==2)
{
RETAILMSG(1,(_T("\r\n 0x%x"),buf[0]));
}

if(buf[0]==1)
{
RETAILMSG(1,(_T("\r\n 0x%x"),buf[0]));
gtp_init_panel();RQST_MAIN_CLOCK();
}

if(buf[0]==3)
{
RETAILMSG(1,(_T("\r\n 0x%x"),buf[0]));
}

//RETAILMSG(1,(_T("\r\n")));
}

Wrbuf[0] = 0x81;
Wrbuf[1] = 0x4e;
start_addr=0x814e;
// HW_ReadRegisters(hI2C, Wrbuf,2,TRUE,buf, 10);
for(i=0;i<8;i++)
{
Wrbuf[0] = start_addr>>8;
Wrbuf[1] = start_addr&0xff;
HW_ReadRegisters1(Wrbuf,buf, 1);
start_addr++;
buf[i+1]=buf[0];


}




num =  buf[1] & 0x07;


y = (buf[4] & 0x0F)<<8 | buf[3];  
x  =(buf[6] & 0x0F)<<8 | buf[5]; 




*px =  800-x;
*py =y;
gup_set_ic_msg(GTP_READ_COOR_ADDR,0);

 
// RETAILMSG(1,(_T("\r\ngetxy %d %d %d %d %d"),x,y,num,\
 // buf[4],buf[5],buf[6],buf[5]));


  if (num != 0)
  {
 return true;
  }else
 return false;
}


/* :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
/* :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */


PUBLIC BOOL
DdsiTouchPanelGetDeviceCaps(INT iIndex, LPVOID  lpOutput)
{


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





switch ( iIndex )
{
case TPDC_SAMPLE_RATE_ID:
{
TPDC_SAMPLE_RATE *pTSR = (TPDC_SAMPLE_RATE*)lpOutput;
RETAILMSG(TS_DBGON, (TEXT("TouchPanelGetDeviceCaps::TPDC_SAMPLE_RATE_ID\r\n")));
            
pTSR->SamplesPerSecondLow      = TSP_SAMPLE_RATE_LOW;
pTSR->SamplesPerSecondHigh     = TSP_SAMPLE_RATE_HIGH;
pTSR->CurrentSampleRateSetting = TSP_CurRate;
RETAILMSG(TS_DBGON,(TEXT("SamplesPerSecondLow= %d, SamplesPerSecondHigh=%d, Setting=%d \r\n"),TSP_SAMPLE_RATE_LOW,TSP_SAMPLE_RATE_HIGH,TSP_CurRate));
}
break;


case TPDC_CALIBRATION_POINT_COUNT_ID:
{
TPDC_CALIBRATION_POINT_COUNT *pTCPC = (TPDC_CALIBRATION_POINT_COUNT*)lpOutput;
RETAILMSG(TS_DBGON, (TEXT("TouchPanelGetDeviceCaps::TPDC_CALIBRATION_POINT_COUNT_ID\r\n")));


pTCPC->flags              = 0;
pTCPC->cCalibrationPoints = 5;
}
break;


case TPDC_CALIBRATION_POINT_ID:
RETAILMSG(TS_DBGON, (TEXT("TouchPanelGetDeviceCaps::TPDC_CALIBRATION_POINT_ID\r\n")));
return(TSP_CalibrationPointGet((TPDC_CALIBRATION_POINT*)lpOutput));


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


}


return TRUE;
}


/* :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
/* :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
PUBLIC BOOL
DdsiTouchPanelSetMode(INT iIndex, LPVOID  lpInput)
{
    BOOL  ReturnCode = FALSE;


RETAILMSG(TS_DBGON, (TEXT("::: DdsiTouchPanelSetMode()\r\n")));


    switch ( iIndex )
    {
case TPSM_SAMPLERATE_LOW_ID:
TSP_CurRate = 0;


v_pPWMregs->TCNTB3  = TSP_SAMPLETICK_LOW;


SetLastError( ERROR_SUCCESS );
ReturnCode = TRUE;
RETAILMSG(TS_DBGON,(TEXT("TPSM_SAMPLERATE_LOW_ID\r\n")));
break;
case TPSM_SAMPLERATE_HIGH_ID:
TSP_CurRate = 1;


v_pPWMregs->TCNTB3  = TSP_SAMPLETICK_HIGH;


SetLastError( ERROR_SUCCESS );
RETAILMSG(TS_DBGON,(TEXT("TPSM_SAMPLERATE_HIGH_ID\r\n")));
ReturnCode = TRUE;
break;


default:
SetLastError( ERROR_INVALID_PARAMETER );
break;
    }




    return ( ReturnCode );
}


/* :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
/* :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */


PUBLIC BOOL
DdsiTouchPanelEnable(VOID)
{
UINT32 irqBuf;
unsigned char Wrbuf[3];
int addr;
unsigned char buf[31];
int i;
int j;
u8 rqst_buf[3] = {0x80, 0x43};
u8 p_main_clk[6] = {71,71,71,71,71,157};
u8 p_bak_ref[276]={0};
p_bak_ref[275]=1;


    BOOL r;
    UINT32 Irq[3]={-1,OAL_INTR_FORCE_STATIC,0};


RETAILMSG(1,(TEXT("DdsiTouchPanelEnable+++\r\n")));

r = TSP_VirtualAlloc();


if (r) 
{
TSP_PowerOn();
}


    // Obtain sysintr values from the OAL for the touch and touch changed interrupts.
    //
//RETAILMSG(TS_DBGON, (TEXT("enable touch sysintr.\r\n")));




    Irq[0]=-1;Irq[1]=OAL_INTR_FORCE_STATIC;Irq[2]=IRQ_EINT12;
    if (!KernelIoControl(IOCTL_HAL_REQUEST_SYSINTR, &Irq, sizeof(Irq), &gIntrTouch, sizeof(UINT32), NULL))
    {
        RETAILMSG(1, (TEXT("ERROR: Failed to request the touch sysintr.\r\n")));
        gIntrTouch = SYSINTR_UNDEFINED;
        return(FALSE);
    }
    Irq[0]=-1;Irq[1]=OAL_INTR_FORCE_STATIC;Irq[2]=IRQ_TIMER3;
    if (!KernelIoControl(IOCTL_HAL_REQUEST_SYSINTR, &Irq, sizeof(Irq), &gIntrTouchChanged, sizeof(UINT32), NULL))
    {
        RETAILMSG(1, (TEXT("ERROR: Failed to request the touch changed sysintr.\r\n")));
        gIntrTouchChanged = SYSINTR_UNDEFINED;
        return(FALSE);
    }

TSP_SampleStop();
//return ture
    
//enable interrupt
v_pIOPregs->EINTMASK &= ~(0x1<<12); //enable interrupt eint12



    //reset panel
   


//int IIC
   IICinit(TOUCH_ADDR,200);


v_pIOPregs->GPGCON &=~(0x3<<8);
v_pIOPregs->GPGCON |= (0x2<<8);


v_pIOPregs->EXTINT1  &= (0x7<<16);
v_pIOPregs->EXTINT1  |= (0x2<<16);


gtp_reset_guitar();




 TSP_DisableInt();

   gup_enter_update_mode_fl();
  // gtp_read_version();
   gtp_gt9xxf_init();
   
   TSP_EnableInt();
   
   gtp_fw_startup();
  // gtp_read_version();


   gtp_read_version();


   Wrbuf[0] = 0x41;
   Wrbuf[1] = 0xE4;
   
   HW_ReadRegisters1(Wrbuf,buf, 1);
//   HW_ReadRegisters(hI2C, Wrbuf,2,TRUE,buf, 1);
   //HW_ReadRegisters(Wrbuf, 0x41e4, 2);
   RETAILMSG(1,(_T("\r\n0x41E4 0x%x"),buf[0]));
RETAILMSG(1,(TEXT("DdsiTouchPanelEnable---\r\n")));
    return r;
}






/* :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
/* :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */


PUBLIC VOID
DdsiTouchPanelDisable(VOID)
{


}


/* :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
/* :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */


LONG
DdsiTouchPanelAttach(VOID)
{
    return (1);
}




LONG
DdsiTouchPanelDetach(VOID)
{
    return (0);
}




/* :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
/* :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
PUBLIC VOID
DdsiTouchPanelPowerHandler(BOOL bOff)
{
RETAILMSG(TS_DBGON, (TEXT("::: DdsiTouchPanelPowerHandler()\r\n")));
    if (bOff)
{
        TSP_PowerOff();
    }
    else
{
        TSP_PowerOn();
    }
}




/* :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
/* :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */




int  fisrtDown = 0;
PUBLIC VOID
DdsiTouchPanelGetPoint(TOUCH_PANEL_SAMPLE_FLAGS * pTipState,
  INT * pUncalX,
  INT * pUncalY)
{
    static int PrevX=0;
    static int PrevY=0;
    int TmpX = 0;
    int TmpY = 0;
int ret;
bool rr;





    RETAILMSG(TS_DBGON,(_T("[TSP]  DdsiTouchPanelGetPoint \r\n")));
if (fisrtDown  == 0) //down firt interrup here
{
RETAILMSG(TS_DBGON,(_T("[TSP]  down \r\n")));

fisrtDown = 1;
// input
v_pIOPregs->GPGCON = (v_pIOPregs->GPGCON & ~(0x3<<8)) ;
v_pIOPregs->EINTMASK |= (0x1<<12); //enable interrupt eint12




bTSP_DownFlag = TRUE;




         *pTipState |= TouchSampleIgnore;


         *pUncalX = PrevX;
         *pUncalY = PrevY;


         *pTipState |= TouchSampleDownFlag;


         TSP_SampleStart();
}else
{




rr = TSP_GetXY(&TmpX, &TmpY);





if (rr)
{
RETAILMSG(TS_DBGON,(_T("[TSP] report  here\r\n")));
*pTipState = TouchSampleValidFlag | TouchSampleDownFlag;
*pTipState &= ~TouchSampleIgnore;


 
}else
{
   bTSP_DownFlag = FALSE;


       
*pUncalX = PrevX;
*pUncalY = PrevY;


*pTipState = TouchSampleValidFlag ;
//*pTipState |= TouchSampleIgnore;


TSP_SampleStop();


fisrtDown =  0;
InterruptDone(gIntrTouch);
v_pIOPregs->GPGCON = (v_pIOPregs->GPGCON & ~(0x3<<8)) | (0x2<<8);
v_pIOPregs->EINTMASK &= ~(0x1<<12); //enable interrupt eint12
goto exit;

}


*pUncalX = PrevX = TmpX;
*pUncalY = PrevY = TmpY;


exit:
// timer3 interrupt status clear
       
        InterruptDone(gIntrTouchChanged);        // Not Handled in MDD

}


// Sleep(5);


    //TSPMSG((_T("[TSP] --DdsiTouchPanelGetPoint()\r\n")));
}


#define FILTER_LIMIT 25


static BOOL Touch_Pen_Filtering(INT *px, INT *py)
{
BOOL RetVal = TRUE;
// TRUE  : Valid pen sample
// FALSE : Invalid pen sample
static int count = 0;
static INT x[2], y[2];
INT TmpX, TmpY;
INT dx, dy;



count++;


if (count > 2) 

// apply filtering rule
count = 2;

// average between x,y[0] and *px,y
TmpX = (x[0] + *px) / 2;
TmpY = (y[0] + *py) / 2;

// difference between x,y[1] and TmpX,Y
dx = (x[1] > TmpX) ? (x[1] - TmpX) : (TmpX - x[1]);
dy = (y[1] > TmpY) ? (y[1] - TmpY) : (TmpY - y[1]);

if ((dx > FILTER_LIMIT) || (dy > FILTER_LIMIT)) {


// Invalid pen sample


*px = x[1];
*py = y[1]; // previous valid sample
RetVal = FALSE;
count = 0;

} else {
// Valid pen sample
x[0] = x[1]; y[0] = y[1];
x[1] = *px; y[1] = *py; // reserve pen samples

RetVal = TRUE;
}

} else { // till 2 samples, no filtering rule

x[0] = x[1]; y[0] = y[1];
x[1] = *px; y[1] = *py; // reserve pen samples

RetVal = FALSE; // <- TRUE jylee 2003.03.04 
}

return RetVal;
}

你可能感兴趣的:(wince gt9xx)