am3352 wince gt ft触摸屏驱动

gt910

/*
 * am335x_adc_touch.cpp
 *
 *  Created on: 2013-3-1
 *      Author: STEVEN
 */


#pragma warning(push)
#pragma warning(disable: 4127 4201)
//------------------------------------------------------------------------------
// Public
//
#include
#include
#include
#include


#include
#include
#include


//------------------------------------------------------------------------------
// Platform
//
#include "omap.h"
#include
#include
#include
#include
#include
#include "am335x_IIC_touch.h"
#include "am33x_clocks.h"
#include
#include
#include "sdk_gpio.h"
#include


#include "omap_prcm_regs.h"
#include "oal_alloc.h"


#include "sdk_i2c.h"
#include
#include
#include "gt9xx.h"


//#include
//#include
//#include "am33x_oal_prcm.h"


#include "bsp_cfg.h"
#include "am33x_base_regs.h"
#include "am33x_ic.h"
#include "am3xx_gpio.h"


//------------------------------------------------------------------------------
// Debug zones
//
#ifndef SHIP_BUILD


#undef ZONE_ERROR
#undef ZONE_WARN
#undef ZONE_FUNCTION
#undef ZONE_INFO
#undef ZONE_TIPSTATE


#define ZONE_ERROR           DEBUGZONE(0)
#define ZONE_WARN           DEBUGZONE(1)
#define ZONE_FUNCTION       DEBUGZONE(2)
#define ZONE_INFO           DEBUGZONE(3)
#define ZONE_TIPSTATE       DEBUGZONE(4)
#endif


static WCHAR const* s_szRegistryPath = L"\\HARDWARE\\DEVICEMAP\\TOUCH";
#define GPIO_2 ((0<<5)|(2<<0))




void StartCalibrationThread();
#define u8 UCHAR
#define u16 UINT
UINT32 g_oalPerfTimerIrq = (UINT32)-1;


//AM335X ADC Touch Internal Funcitons
DWORD gIntrTouchChanged = SYSINTR_NOP;   // Not used here.
DWORD gIntrTouch        = SYSINTR_NOP;
HANDLE m_hGpio;
void beepinitalizehardware();


HANDLE g_hGpio = NULL;
#define gpio_rest 3*32+14
#define gpio_int 0*32+7




#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


AM3XX_GPTIMER_REGS* g_pHPTimerRegs;
BOOL g_oalProfilerEnabled = FALSE;
AM33X_INTC_MPU_REGS *g_am33x_int=NULL;
AM3XX_GPIO_REGS *goio0_0=NULL;


void Delay_us(int n)


{


    LARGE_INTEGER litmp;


       LONGLONG QPart1,QPart2;


       double dfMinus,dfFreq,dfTim;


       QueryPerformanceFrequency(&litmp);


       dfFreq = (double)litmp.QuadPart;


       QueryPerformanceCounter(&litmp);


       QPart1 = litmp.QuadPart;


       do


       {


       QueryPerformanceCounter(&litmp);


          QPart2 = litmp.QuadPart;


          dfMinus = (double)(QPart2-QPart1);


          dfTim = dfMinus/dfFreq;


       }while(dfTim<0.000001*n);


}




static HANDLE                        hI2C;   // I2C Bus Driver
static void udelay(UINT32 delay)
{
volatile UINT32 tmp;
volatile UINT32 j;
UINT32 i;

for(i=0; i for(j=0; j<10000; j++)
tmp = j;
}




#define TOUCH_WRITE    (0x5d)// (0x5d)// (0x38 + 0)(0x5d)//
#define TOUCH_READ      (0x39)//(0x5e)//(0x38 + 1)(0x5e)//
static void *m_hI2CDevice = NULL;
int dwErr=1;
#define TS_DBGON 0


static  bool  IICinit()
{
DWORD dwErr = ERROR_SUCCESS, bytes;
m_hI2CDevice = I2COpen(AM_DEVICE_I2C0);
if (m_hI2CDevice == NULL)
{
OALMSG(1,(L"RTCInit: Failed to open I2C0 driver"));
return FALSE;
}
I2CSetSlaveAddress(m_hI2CDevice, TOUCH_WRITE);
I2CSetSubAddressMode(m_hI2CDevice,I2C_SUBADDRESS_MODE_16);
I2CSetBaudIndex(m_hI2CDevice,FULLSPEED_MODE);


return TRUE;
    
}




static DWORD
HW_WriteRegisters(
    DWORD startReg,
    PUCHAR pBuff,   // Optional buffer
    DWORD nRegs     // number of registers
    )
{
//RETAILMSG(1,(_T("0x%x 0x%x 0x%x\r\n"),startReg,pBuff[0],nRegs));


if (I2CWrite(m_hI2CDevice,startReg, pBuff,nRegs) != nRegs)
OALMSG(1,(TEXT("HW_WriteRegisters():Write Error!\r\n")));
    return 1;
}


static DWORD
HW_ReadRegisters(
    PUCHAR pBuff,       // Optional buffer
    DWORD StartReg,     // Start Register
    DWORD nRegs         // Number of Registers
    )
{
    if (I2CRead(m_hI2CDevice,StartReg,pBuff,nRegs) != nRegs)
OALMSG(1,(TEXT("HW_ReadRegisters():Read Error!\r\n")));

    return dwErr;
}


static DWORD
HW_ReadRegisters1(
    PUCHAR StartReg1,       // Start Register
    PUCHAR pBuff,     // Optional buffer
    DWORD nRegs         // Number of Registers
    )
{
DWORD StartReg=((StartReg1[0]<<8)+StartReg1[1]);
if (I2CRead(m_hI2CDevice,StartReg,pBuff,nRegs) != nRegs)
OALMSG(1,(TEXT("HW_ReadRegisters():Read Error!\r\n")));

    return 1;
}




static DWORD
HW_WriteRegisters1(
PUCHAR StartReg1,   // Start Register
    PUCHAR pBuff,   // Optional buffer
    DWORD nRegs     // number of registers
    )
{ DWORD StartReg=((StartReg1[0]<<8)+StartReg1[1]);

if (I2CWrite(m_hI2CDevice,StartReg, pBuff,nRegs) != nRegs)
OALMSG(1,(TEXT("HW_WriteRegisters():Write Error!0x%x0x%x0x%x\r\n"),StartReg,StartReg1[0],StartReg1[1]));
    return 1;
}


int  fisrtDown = 0;
 BOOL bTSP_DownFlag;
 
 static void gup_set_ic_msg( u16 addr, u8 val)
 {
unsigned char Wrbuf[3];
unsigned char buf[31];
int i;
 
Wrbuf[0] = addr&0xff;
Wrbuf[1] = addr>>8;
Wrbuf[2] = val;
buf[0]=val;
//HW_WriteRegisters(Wrbuf,3,TRUE,buf, 1);
 
RETAILMSG(0,(_T("\r\ngup_set_ic_msg 0x%x 0x%x\r\n"),addr,buf[0]));
HW_WriteRegisters1(Wrbuf,buf, 1);
  buf[0]=1;
HW_ReadRegisters1(Wrbuf,buf, 1);
//HW_ReadRegisters(hI2C, Wrbuf,2,TRUE,buf, 1);
RETAILMSG(0,(_T("\r\ngup_set_ic_msg 0x%x 0x%x\r\n"),addr,buf[0]));
 
 }
 DWORD  gup_red_ic_msg( u16 addr )
{
unsigned char Wrbuf[3];
unsigned char buf[31];
int i;
Wrbuf[0] = addr&0xff;


Wrbuf[1] = addr>>8;





HW_ReadRegisters1(Wrbuf,buf, 1);


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


return 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);
#if 0
gup_red_ic_msg(_rRW_MISCTL__SWRST_B0_);
gup_red_ic_msg(0x4010);
gup_red_ic_msg( _bRW_MISCTL__TMR0_EN);
gup_red_ic_msg( _bRW_MISCTL__CACHE_EN);
gup_red_ic_msg( _rRW_MISCTL__BOOTCTL_B0_);
gup_red_ic_msg( _bWO_MISCTL__CPU_SWRST_PULSE);
gup_red_ic_msg( _rRW_MISCTL__BOOT_CTL_);
gup_red_ic_msg( _rRW_MISCTL__BOOT_OPT_B0_);
gup_red_ic_msg( _bRW_MISCTL__MEM_CD_EN);
#endif
 
// gup_set_ic_msg(0x8040,2);
gup_set_ic_msg(_rRW_MISCTL__SWRST_B0_,0x0C);
//while(1);
//HW_ReadRegisters(hI2C, Wrbuf,2,TRUE,buf, 1);
 
 
Wrbuf[0] = addr&0xff;
Wrbuf[1] = addr>>8;
 
HW_ReadRegisters1( Wrbuf,buf, 1);


 
// while(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&0xff;
Wrbuf[1] = addr>>8;
 
//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 gtp_reset_guitar()
 {
 
GPIOSetMode(g_hGpio, gpio_rest,  GPIO_DIR_OUTPUT );
GPIOClrBit(g_hGpio, gpio_rest); 
 
Sleep(2);
     GPIOSetMode(g_hGpio, gpio_int, GPIO_DIR_OUTPUT );
  GPIOClrBit(g_hGpio, gpio_int); 
// GPIOSetBit(g_hGpio, gpio_int);


Sleep(2);


GPIOSetBit(g_hGpio, gpio_rest);
Sleep(5);
  
 }
 
 void gup_enter_update_mode_fl()
 {
  int i;
gtp_reset_guitar();




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)
{

    GPIOSetMode(g_hGpio, gpio_int,  GPIO_DIR_OUTPUT );
GPIOClrBit(g_hGpio, gpio_int); 
 
 
Sleep(ms);
 
GPIOSetMode(g_hGpio, gpio_int,  GPIO_DIR_INPUT |GPIO_INT_HIGH_LOW );


}
  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] = 0x41;
 Wrbuf[1] = 0x80;
 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)&0xff;
 Wrbuf[1] = (start_addr)>>8;
 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[2], 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)&0xff;
 Wrbuf[1] = (start_addr)>>8;
 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[2], 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)&0xff;
 Wrbuf[1] = (start_addr+i)>>8;
 
 Wrbuf[2] = gtp_default_FW_fl[FW_HEAD_LENGTH + start_index+i];
 //buf[0]=Wrbuf[2];
 
//  HW_WriteRegisters1(Wrbuf,Wrbuf, burn_len);
// HW_ReadRegisters1(Wrbuf,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_read_version()
{
unsigned char Wrbuf[3];
unsigned char buf[31];

unsigned char buf1[31];
int i;
u16 addr=GTP_REG_VERSION;

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

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

//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")));


}
  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)&0xff;
 Wrbuf[1] = (val)>>8;
 
 //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[2], 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
  
  }
  
  
   BOOL
  TSP_GetXY1(INT *px, INT *py)
  {
unsigned char buf[31];
int i;
int num;
  
unsigned short x,y;
 
memset(buf,0,31);
  
   // HW_ReadRegisters(buf, 0, 31);
   
   
// RETAILMSG(1,(_T("[TSP]\r\n")));
for(i = 0 ;i< 31;i++)
 {
HW_ReadRegisters(&buf[i], i, 1);
 // RETAILMSG(1,(_T(" 0x%x"),buf[i]));
  
  
 }
  //  RETAILMSG(1,(_T("[TSP]\r\n")));
  
  
num = buf[2] & 0x07;
  
   // RETAILMSG(1,(_T("num is %d\r\n"),num));
  
  
x = (buf[3] & 0x0F)<<8 | buf[4];  
y =(buf[5] & 0x0F)<<8 | buf[6]; 
  
 // if(x<10) x=10;
// if(y<10) y=10;
// if(x>790) x=790;
// if(y>390) y=390;
RETAILMSG(1,(_T("x is %d ,y is %d\r\n"),x,y));
 RETAILMSG(1, (L"waiting1240x%x 0x%x 0x%x\r\n",INREG32(&g_pHPTimerRegs->TCRR),INREG32(&g_pHPTimerRegs->TMAR)\
  ,INREG32(&g_pHPTimerRegs->IRQENABLE_SET)));


*px = x;
*py = y;
  
if (num != 0)
{
return true;
}else
return false;
  }
  
 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] =0x4E; 
Wrbuf[1] =0x81;
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] = 0x43;
Wrbuf[1] = 0x80;
//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] = 0x4e;
    Wrbuf[1] = 0x81;
start_addr=0x814e;
 //  HW_ReadRegisters(hI2C, Wrbuf,2,TRUE,buf, 10);
for(i=0;i<8;i++)
{
Wrbuf[0] = start_addr&0xff;
Wrbuf[1] = start_addr>>8;
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(0,(_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;
 }




static void tscadc_getdatapoint(
   
    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(0,(_T("[TSP]  down \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(gIntrTouch); // Not Handled in MDD

}


}




//------------------------------------------------------------------------------
// exports
//
extern "C" DWORD gdwTouchIstTimeout;    // MDD thread wait timeout.


// The MDD requires a minimum of MIN_CAL_COUNT consecutive samples before
// it will return a calibration coordinate to GWE.
extern "C" const int MIN_CAL_COUNT = 20;


//------------------------------------------------------------------------------
//
//  TouchDriverCalibrationPointGet
//
extern "C" BOOL
TouchDriverCalibrationPointGet(
    TPDC_CALIBRATION_POINT *pTCP
    )
{
    BOOL rc = FALSE;


    INT32 cDisplayWidth  = pTCP->cDisplayWidth;
    INT32 cDisplayHeight = pTCP->cDisplayHeight;


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


    DEBUGMSG(ZONE_FUNCTION, (TEXT("TouchDriverCalibrationPointGet+\r\n")));


    // Check which of the 5 calibration point is requested.
    switch ( pTCP->PointNumber )
        {
        case 0:
            pTCP->CalibrationX = cDisplayWidth / 2;
            pTCP->CalibrationY = cDisplayHeight / 2;
            rc = TRUE;
            break;


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


        case 2:
            pTCP->CalibrationX = CalibrationRadiusX * 2;
            pTCP->CalibrationY = cDisplayHeight - ( CalibrationRadiusY * 2 );
            rc = TRUE;
            break;


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


        case 4:
            pTCP->CalibrationX = cDisplayWidth - ( CalibrationRadiusX * 2 );
            pTCP->CalibrationY = CalibrationRadiusY * 2;
            rc = TRUE;
            break;


        default:
            pTCP->CalibrationX = cDisplayWidth / 2;
            pTCP->CalibrationY = cDisplayHeight / 2;


            SetLastError( ERROR_INVALID_PARAMETER );
            break;
        }


    DEBUGMSG(ZONE_FUNCTION, (TEXT("cDisplayWidth         : %4X \r\n"), cDisplayWidth     ));
    DEBUGMSG(ZONE_FUNCTION, (TEXT("cDisplayHeight       : %4X \r\n"), cDisplayHeight    ));
    DEBUGMSG(ZONE_FUNCTION, (TEXT("CalibrationRadiusX   : %4d \r\n"), CalibrationRadiusX));
    DEBUGMSG(ZONE_FUNCTION, (TEXT("CalibrationRadiusY   : %4d \r\n"), CalibrationRadiusY));
    DEBUGMSG(ZONE_FUNCTION, (TEXT("pTCP -> PointNumber  : %4d \r\n"), pTCP->PointNumber));
    DEBUGMSG(ZONE_FUNCTION, (TEXT("pTCP -> CalibrationX : %4d \r\n"), pTCP->CalibrationX));
    DEBUGMSG(ZONE_FUNCTION, (TEXT("pTCP -> CalibrationY : %4d \r\n"), pTCP->CalibrationY));


    DEBUGMSG(ZONE_FUNCTION, (TEXT("TouchDriverCalibrationPointGet-\r\n")));
    return ( rc );
}


//------------------------------------------------------------------------------
//
//  DdsiTouchPanelGetDeviceCaps
//
//


extern "C" BOOL
DdsiTouchPanelGetDeviceCaps(
    INT iIndex,
    LPVOID lpOutput
    )
{
    DEBUGMSG(ZONE_FUNCTION, (TEXT("DdsiTouchPanelGetDeviceCaps+\r\n")));


    BOOL rc = FALSE;
    if ( lpOutput == NULL )
        {
        DEBUGMSG(ZONE_ERROR, (TEXT("TouchPanelGetDeviceCaps: Invalid parameter.\r\n")));
        SetLastError( ERROR_INVALID_PARAMETER );
        }
    else
        {
        TPDC_SAMPLE_RATE *pTSR = (TPDC_SAMPLE_RATE*)lpOutput;
        TPDC_CALIBRATION_POINT_COUNT *pTCPC = (TPDC_CALIBRATION_POINT_COUNT*)lpOutput;


        // Check which of the device capabilities are requested.
        switch ( iIndex )
            {
            // Return the sample rate.
            case TPDC_SAMPLE_RATE_ID:


                pTSR->SamplesPerSecondLow       = TOUCHPANEL_SAMPLE_RATE_LOW;
                pTSR->SamplesPerSecondHigh     = TOUCHPANEL_SAMPLE_RATE_HIGH;
                pTSR->CurrentSampleRateSetting = s_TouchDevice.nSampleRate;


                rc = TRUE;
                break;


            // Return the number of calibration points used to calibrate the touch screen.
            case TPDC_CALIBRATION_POINT_COUNT_ID:


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


                rc = TRUE;
                break;


            // Return the x and y coordinates of the requested calibration point.
            // The index of the calibration point is set in lpOutput->PointNumber.
            case TPDC_CALIBRATION_POINT_ID:
                rc = TouchDriverCalibrationPointGet( (TPDC_CALIBRATION_POINT*)lpOutput );
                break;


            default:
                DEBUGMSG( ZONE_ERROR,
                           (TEXT("TouchPanelGetDeviceCaps: Invalid parameter.\r\n")));
                SetLastError(ERROR_INVALID_PARAMETER);
                break;
        }
    }
    DEBUGMSG(ZONE_FUNCTION, (TEXT("DdsiTouchPanelGetDeviceCaps-\r\n")));


    return ( rc );
}




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


    DEBUGMSG(ZONE_FUNCTION, (TEXT("DdsiTouchPanelSetMode+\r\n")));


    switch ( iIndex )
        {
        case TPSM_SAMPLERATE_LOW_ID:
        case TPSM_SAMPLERATE_HIGH_ID:
            SetLastError( ERROR_SUCCESS );
            rc = TRUE;
            break;


        default:
            DEBUGMSG( ZONE_ERROR,
                       (TEXT("DdsiTouchPanelSetMode: Invalid parameter.\r\n")));
            SetLastError( ERROR_INVALID_PARAMETER );
            break;
        }


    DEBUGMSG(ZONE_FUNCTION, (TEXT("DdsiTouchPanelSetMode-\r\n")));


    return rc;
}


BOOL SetGPIO(UINT level)
{
    DWORD rc = TRUE;

// Configure Backlight/Power pins as outputs






    /* turn on backlight non-zero level */
    if (level)
    {
    GPIOSetMode(g_hGpio, gpio_rest, GPIO_DIR_OUTPUT );
//GPIOPullup(g_hGpio, 3*32+18, GPIO_PULLUP_ENABLE);
GPIOSetBit(g_hGpio, gpio_rest);
    //GPIOSetMode(g_hGpio, 0*32+1, GPIO_DIR_OUTPUT );
    //GPIOSetBit(g_hGpio, 0*32+1);


GPIOSetMode(g_hGpio, gpio_int, GPIO_DIR_OUTPUT );
GPIOClrBit(g_hGpio, gpio_int);


RETAILMSG(1, (L"AM33x:[turn on] SetGpio0x%x 0x%x 0x%x 0x%x",GPIOGetBit(g_hGpio, gpio_rest)\
,GPIOGetBit(g_hGpio, gpio_int)\
,GPIOGetBit(g_hGpio, 0*32+7)));
 


    }
    else if (level == 0 )
    {
    GPIOClrBit(g_hGpio, gpio_rest);
    //GPIOClrBit(g_hGpio, 0*32+1);
//GPIOClrBit(g_hGpio, 0*32+7);
RETAILMSG(0, (L"AM33x: [turn off]SetGpio0x%x 0x%x 0x%x 0x%x",GPIOGetBit(g_hGpio, 3*32+18)\
,GPIOGetBit(g_hGpio, 0*32+1)\
,GPIOGetBit(g_hGpio, 0*32+7)));
    }

 

    return rc;
}


 static UINT32  s_Frequency;
 DWORD g_oalProfilerIncrement;


 VOID
TSP_SampleStart(VOID)
{



    SETREG32(&g_pHPTimerRegs->TCLR, GPTIMER_TCLR_ST);


  
    
}


 VOID
TSP_SampleStop(VOID)
{


  CLRREG32(&g_pHPTimerRegs->TCLR, GPTIMER_TCLR_ST);




}


void  TSP_EnableInt()
{
GPIOSetMode(g_hGpio, gpio_int, GPIO_INT_HIGH_LOW );


}


void TSP_DisableInt()
{
InterruptDisable(IRQ_GPIO_7);


GPIOSetMode(g_hGpio, gpio_int, GPIO_DIR_INPUT );


}
static OMAP_DEVICE          gptPerfDevice = AM_DEVICE_TIMER7;
DWORD BSPGetGPTPerfDevice1(void)
{
return AM_DEVICE_TIMER4;
}


BOOL
DdsiTouchPanelEnable()
{
    UINT32 Irq[3]={-1,OAL_INTR_FORCE_STATIC,0};
    BOOL rc = FALSE;
UINT srcClock;
    PHYSICAL_ADDRESS pa = { 0, 0 };
    DWORD tclr = 0;
unsigned char Wrbuf[3];
int addr;
unsigned char buf[31];

//=(AM3XX_GPTIMER_REGS*)OALPAtoUA(AM33X_GPTIMER4_REGS_PA);
//OMAP_DEVICE gptPerfDevice = AM_DEVICE_TIMER4;
OALMSG(1,(L"DdsiTouchPanelEnable FT502"));

EnableDeviceClocks( AM_DEVICE_GPIO3, TRUE);
EnableDeviceClocks( AM_DEVICE_GPIO0, TRUE);
EnableDeviceClocks( AM_DEVICE_I2C0, TRUE);

pa.LowPart =AM33X_INTC_MPU_REGS_PA;// GetAddressByDevice(AM_DEVICE_TIMER4);
   g_am33x_int =(AM33X_INTC_MPU_REGS*)MmMapIoSpace(pa, sizeof(AM33X_INTC_MPU_REGS), FALSE);
   if (g_am33x_int == NULL)
  {
 RETAILMSG(1, (TEXT("g_am33x_int MmMapIoSpace() Failed \r\n")));
  }
   pa.LowPart =AM33X_GPIO0_REGS_PA;// GetAddressByDevice(AM_DEVICE_TIMER4);
   goio0_0 =(AM3XX_GPIO_REGS*)MmMapIoSpace(pa, sizeof(AM3XX_GPIO_REGS), FALSE);
  // g_pHPTimerRegs =(AM3XX_GPTIMER_REGS*) OALPAtoUA(GetAddressByDevice(BSPGetGPTPerfDevice1()));
  if (g_am33x_int == NULL)
  {
 RETAILMSG(1, (TEXT("g_am33x_int MmMapIoSpace() Failed \r\n")));
  }
#if 0




   pa.LowPart =AM33X_GPTIMER4_REGS_PA;// GetAddressByDevice(AM_DEVICE_TIMER4);
   g_pHPTimerRegs = (AM3XX_GPTIMER_REGS*)MmMapIoSpace(pa, sizeof(AM3XX_GPTIMER_REGS), FALSE);
  // g_pHPTimerRegs =(AM3XX_GPTIMER_REGS*) OALPAtoUA(GetAddressByDevice(BSPGetGPTPerfDevice1()));
  if (g_pHPTimerRegs == NULL)
  {
 RETAILMSG(1, (TEXT("g_pHPTimerRegs MmMapIoSpace() Failed \r\n")));
  }


   OALMSG(1, (L"waiting3\r\n"));


   // map HighResTimer
   g_oalPerfTimerIrq = GetIrqByDevice(gptPerfDevice,NULL);
   OALMSG(1, (L"waiting2\r\n"));


   // Select high frequency source clock and frequency
   //PrcmDeviceSetSourceClocks(gptPerfDevice,1,&srcClock);
   // Enable GPTimer for high perf/monte carlo profiling
   EnableDeviceClocks(BSPGetGPTPerfDevice1(), TRUE);
   
   OALMSG(1, (L"waiting124444\r\n"));
   RETAILMSG(1, (L"waiting1240x%x\r\n",INREG32(&g_pHPTimerRegs->TCLR)));


   OUTREG32(&g_pHPTimerRegs->TCLR, 0);
   // stop timer
 OUTREG32(&g_pHPTimerRegs->TIOCP, GPTIMER_TIOCP_SOFTRESET); // Soft reset GPTIMER
 while ((INREG32(&g_pHPTimerRegs->TIOCP) & GPTIMER_TIOCP_SOFTRESET) != 0);   // While until done
   
   OALMSG(1, (L"waiting11\r\n"));
 //TODO: 0x4 for test only REMOVE later  
 OUTREG32( &g_pHPTimerRegs->TIOCP, 0x4 /*SYSCONFIG_SMARTIDLE|SYSCONFIG_ENAWAKEUP| SYSCONFIG_AUTOIDLE*/);
 OUTREG32(&g_pHPTimerRegs->TSICR, GPTIMER_TSICR_POSTED); // Enable posted mode
   
 OUTREG32(&g_pHPTimerRegs->TMAR, 0x1000);  // Set match register to avoid unwanted interrupt
 while ((INREG32(&g_pHPTimerRegs->TWPS) & GPTIMER_TWPS_TMAR) != 0); // Wait until write is done
   
   OALMSG(1, (L"waiting12\r\n"));
 OUTREG32(&g_pHPTimerRegs->TLDR,  0x00000000); 
 while ((INREG32(&g_pHPTimerRegs->TWPS) & GPTIMER_TWPS_TLDR) != 0); // Wait until write is done
   
   OALMSG(1, (L"waiting13\r\n"));
 // enable match interrupt
 OUTREG32(&g_pHPTimerRegs->IRQENABLE_SET, GPTIMER_TIER_MATCH);
 
 // enable wakeups
 OUTREG32(&g_pHPTimerRegs->IRQWAKEEN, GPTIMER_TWER_MATCH);
   
 // Enable timer in auto-reload --- and compare mode VA: not for fixed interval ---
 OUTREG32(&g_pHPTimerRegs->TCLR, GPTIMER_TCLR_AR | GPTIMER_TCLR_ST );
 while ((INREG32(&g_pHPTimerRegs->TWPS) & GPTIMER_TWPS_TCLR) != 0); // Wait until write is done
 OALMSG(1, (L"waiting14 \r\n"));
 RETAILMSG(1, (L"waiting1240x%x\r\n",INREG32(&g_pHPTimerRegs->TCLR)));


#endif
OALLog(L"\r\OEMCacheRangeFlush: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\r\n", \
INREG32(&g_am33x_int->INTC_ISR_CLEAR0),\
INREG32(&g_am33x_int->INTC_ISR_CLEAR1),\
INREG32(&g_am33x_int->INTC_ISR_CLEAR2),\
INREG32(&g_am33x_int->INTC_ISR_CLEAR3),\
INREG32(&g_am33x_int->INTC_ISR_SET0),\
INREG32(&g_am33x_int->INTC_ISR_SET1),\
INREG32(&g_am33x_int->INTC_ISR_SET2),\
INREG32(&g_am33x_int->INTC_ISR_SET3));  


OALLog(L"\r\OEMCacheRangeFlush: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\r\n", \
INREG32(&g_am33x_int->INTC_ITR0),\
INREG32(&g_am33x_int->INTC_ITR1),\
INREG32(&g_am33x_int->INTC_ITR2),\
INREG32(&g_am33x_int->INTC_ITR3),\
INREG32(&g_am33x_int->INTC_MIR0),\
INREG32(&g_am33x_int->INTC_MIR1),\
INREG32(&g_am33x_int->INTC_MIR2),\
INREG32(&g_am33x_int->INTC_MIR3)); 


OALLog(L"\r\OEMCacheRangeFlush: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\r\n", \
INREG32(&g_am33x_int->INTC_MIR_CLEAR0),\
INREG32(&g_am33x_int->INTC_MIR_CLEAR1),\
INREG32(&g_am33x_int->INTC_MIR_CLEAR2),\
INREG32(&g_am33x_int->INTC_MIR_CLEAR3),\
INREG32(&g_am33x_int->INTC_MIR_SET0),\
INREG32(&g_am33x_int->INTC_MIR_SET1),\
INREG32(&g_am33x_int->INTC_MIR_SET2),\
INREG32(&g_am33x_int->INTC_MIR_SET3)); 


OALLog(L"\r\OEMCacheRangeFlush: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\r\n", \
INREG32(&goio0_0->IRQSTATUS_SET_0),\
INREG32(&goio0_0->IRQSTATUS_SET_1),\
INREG32(&goio0_0->IRQSTATUS_0),\
INREG32(&goio0_0->IRQSTATUS_1),\
INREG32(&goio0_0->IRQSTATUS_RAW_0),\
INREG32(&goio0_0->IRQSTATUS_RAW_1));


g_hGpio = GPIOOpen();


if (g_hGpio == NULL)
{
RETAILMSG(1, (L"AM33x:Failed to open GPIO driver"));
return FALSE;
}
SetGPIO(1);
// Initialize HW
  s_TouchDevice.nPenIRQ = IRQ_GPIO_7;//IRQ_ECAP0;//GetIrqByDevice(AM_DEVICE_ADC_TSC, NULL);




    Irq[0]=-1;Irq[1]=OAL_INTR_FORCE_STATIC;Irq[2]=s_TouchDevice.nPenIRQ;
    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);
    }
 
s_TouchDevice.nPenIRQ = GetIrqByDevice(AM_DEVICE_TIMER4, NULL);//IRQ_TIMER4;//IRQ_ECAP0;//GetIrqByDevice(AM_DEVICE_ADC_TSC, NULL);
 
 
 Irq[0]=-1;Irq[1]=OAL_INTR_FORCE_STATIC;Irq[2]=s_TouchDevice.nPenIRQ;
 if (!KernelIoControl(IOCTL_HAL_REQUEST_SYSINTR, &Irq, sizeof(Irq), &gIntrTouchChanged, sizeof(UINT32), NULL))
 {
RETAILMSG(1, (TEXT("ERROR: Failed to request the touch sysintr.\r\n")));
gIntrTouch = SYSINTR_UNDEFINED;
return(FALSE);
 }


    DEBUGMSG(ZONE_FUNCTION, (TEXT("DdsiTouchPanelEnable+\r\n")));
/*
    // Initialize once only
    if (s_TouchDevice.bInitialized)
        {
        rc = TRUE;
        goto cleanup;
        }


    // Initialize HW
    if (!PddInitializeHardware())
        {
        DEBUGMSG(
            ZONE_ERROR,
            (TEXT("ERROR: TOUCH: Failed to initialize touch PDD.\r\n"))
            );
        goto cleanup;
        }
*/
    // Create a calibration thread. This thread will check to see if calibration is needed.
  //  StartCalibrationThread();


    //Done
  //  s_TouchDevice.bInitialized = TRUE;
    rc = TRUE;
//


IICinit();



TSP_EnableInt();


gtp_reset_guitar();


TSP_DisableInt();


gup_enter_update_mode_fl();



gtp_gt9xxf_init();


TSP_EnableInt();


gtp_fw_startup();
// gtp_read_version();


gtp_read_version();
Wrbuf[0] = 0xE4;


Wrbuf[1] = 0x41;


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")));
OALLog(L"\r\OEMCacheRangeFlush: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\r\n", \
INREG32(&g_am33x_int->INTC_ISR_CLEAR0),\
INREG32(&g_am33x_int->INTC_ISR_CLEAR1),\
INREG32(&g_am33x_int->INTC_ISR_CLEAR2),\
INREG32(&g_am33x_int->INTC_ISR_CLEAR3),\
INREG32(&g_am33x_int->INTC_ISR_SET0),\
INREG32(&g_am33x_int->INTC_ISR_SET1),\
INREG32(&g_am33x_int->INTC_ISR_SET2),\
INREG32(&g_am33x_int->INTC_ISR_SET3));
OALLog(L"\r\OEMCacheRangeFlush: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\r\n", \
INREG32(&g_am33x_int->INTC_ITR0),\
INREG32(&g_am33x_int->INTC_ITR1),\
INREG32(&g_am33x_int->INTC_ITR2),\
INREG32(&g_am33x_int->INTC_ITR3),\
INREG32(&g_am33x_int->INTC_MIR0),\
INREG32(&g_am33x_int->INTC_MIR1),\
INREG32(&g_am33x_int->INTC_MIR2),\
INREG32(&g_am33x_int->INTC_MIR3)); 


OALLog(L"\r\OEMCacheRangeFlush: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\r\n", \
INREG32(&g_am33x_int->INTC_MIR_CLEAR0),\
INREG32(&g_am33x_int->INTC_MIR_CLEAR1),\
INREG32(&g_am33x_int->INTC_MIR_CLEAR2),\
INREG32(&g_am33x_int->INTC_MIR_CLEAR3),\
INREG32(&g_am33x_int->INTC_MIR_SET0),\
INREG32(&g_am33x_int->INTC_MIR_SET1),\
INREG32(&g_am33x_int->INTC_MIR_SET2),\
INREG32(&g_am33x_int->INTC_MIR_SET3)); 
   // OALIntrEnableIrqs(1, &g_oalPerfTimerIrq);
    OALLog(L"\r\OEMCacheRangeFlush: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x \r\n", \
(goio0_0->IRQSTATUS_SET_0),\
(goio0_0->IRQSTATUS_SET_1),\
  (goio0_0->IRQSTATUS_0),\
  (goio0_0->IRQSTATUS_1),\
  (goio0_0->IRQSTATUS_RAW_0),\
  (goio0_0->IRQSTATUS_RAW_1));
  // OUTREG32(goio0_0->IRQSTATUS_0,1<<7));
 //  OUTREG32(goio0_0->IRQSTATUS_1,1<<7));
   goio0_0->IRQSTATUS_RAW_0 &=~(1<<7);
   goio0_0->IRQSTATUS_RAW_1 &=~(1<<7);


return rc;


TSP_EnableInt();
GPIOSetBit(g_hGpio, 3*32+14);
Sleep(5);


//Delay_us(5000);
GPIOClrBit(g_hGpio, 3*32+14);
Sleep(20);
//Delay_us(20000);
GPIOSetBit(g_hGpio, 3*32+14);


IICinit();
Wrbuf[0] = 0x00;
Wrbuf[1] = 0x0;


HW_WriteRegisters(Wrbuf[0], &Wrbuf[1],1);


RETAILMSG(0, (L"AM33x:[turn on] SetGpio0x%x 0x%x 0x%x ",GPIOGetBit(g_hGpio, 3*32+18)\
,GPIOGetBit(g_hGpio, 0*32+1)\
,GPIOGetBit(g_hGpio, 0*32+7)));


Wrbuf[0] = 0x04;
Wrbuf[1] = 0x0;

HW_WriteRegisters(Wrbuf[0], &Wrbuf[1],1);


RETAILMSG(0, (L"AM33x:[turn on] SetGpio0x%x 0x%x 0x%x ",GPIOGetBit(g_hGpio, 3*32+18)\
,GPIOGetBit(g_hGpio, 0*32+1)\
,GPIOGetBit(g_hGpio, 0*32+7)));


Wrbuf[0] = 0x07;
Wrbuf[1] = 0x0;



HW_WriteRegisters(Wrbuf[0], &Wrbuf[1],1);
RETAILMSG(0, (L"AM33x:[turn on] SetGpio0x%x 0x%x 0x%x 0x%x",GPIOGetBit(g_hGpio, 3*32+18)\
,GPIOGetBit(g_hGpio, 0*32+1)\
,GPIOGetBit(g_hGpio, 0*32+7)));


TSP_SampleStart();
RETAILMSG(1, (L"waiting1240x%x\r\n",Irq[2]));






cleanup:
    DEBUGMSG(ZONE_FUNCTION, (TEXT("DdsiTouchPanelEnable-\r\n")));
    return rc;
}




VOID
DdsiTouchPanelDisable()
{
    DEBUGMSG(ZONE_FUNCTION, (TEXT("DdsiTouchPanelDisable+\r\n")));


    // Close pen event and kill thread.
    PddDeinitializeHardware();


    // Release interrupt
    if (gIntrTouch != 0)
        {
        KernelIoControl(
            IOCTL_HAL_RELEASE_SYSINTR,
            &gIntrTouch,
            sizeof(gIntrTouch),
            NULL,
            0,
            NULL
            );
        }


    s_TouchDevice.bInitialized = FALSE;


    DEBUGMSG(ZONE_FUNCTION, (TEXT("DdsiTouchPanelDisable-\r\n")));
}


LONG
DdsiTouchPanelAttach()
{
  
  return (1);
  
}


//------------------------------------------------------------------------------
//
//  DdsiTouchPanelDetach
//
//
LONG
DdsiTouchPanelDetach()
{
return (0);


}


//TODO: NEED TO REWRITE
void
DdsiTouchPanelGetPoint(
    TOUCH_PANEL_SAMPLE_FLAGS *pTipStateFlags,
    INT *pUncalX,
    INT *pUncalY
    )
{
RETAILMSG(0,(_T("DdsiTouchPanelGetPoint\r\n")));
    // By default, any sample returned will be ignored.
    *pTipStateFlags = TouchSampleIgnore;


    tscadc_getdatapoint( pTipStateFlags, pUncalX, pUncalY);
    // Set the proper state for the next interrupt.
    InterruptDone( gIntrTouch);


    DEBUGMSG(ZONE_FUNCTION&&ZONE_SAMPLES, (TEXT("DdsiTouchPanelGetPoint+\r\n")));


    return;
}


void
DdsiTouchPanelPowerHandler(
    BOOL bOff
    )
{
   
}


DWORD dwIrqVal=(DWORD)SYSINTR_UNDEFINED;
HANDLE hIntrEvent;
HANDLE hIntrThread;


DWORD IntrThread(LPVOID lpParameter)
{
UNREFERENCED_PARAMETER(lpParameter);


    DEBUGMSG(ZONE_INIT || ZONE_VERBOSE, (L"+USBCDMA: IntrThread\n"));


    while (1)
    {
        WaitForSingleObject(hIntrEvent, INFINITE);
        
RETAILMSG(1, (L"IntrThread"));


        InterruptDone(dwIrqVal);


       
    }




    return 0;
}


BOOL
PddInitializeHardware(VOID)
{
    BOOL   rc = TRUE;
    PHYSICAL_ADDRESS pa = { 0, 0 };
    int ctrl, irqenable;
    DWORD BytesRet = 0;


    s_TouchDevice.nPenIRQ = IRQ_GPIO_7;//IRQ_ECAP0;//GetIrqByDevice(AM_DEVICE_ADC_TSC, NULL);


    if (!KernelIoControl(IOCTL_HAL_REQUEST_SYSINTR, &s_TouchDevice.nPenIRQ, sizeof(DWORD),
            &gIntrTouch, sizeof(gIntrTouch), NULL))
    {
        
    }
   
    return rc;
}
void beepinitalizehardware()
{
 

 
m_hGpio = GPIOOpen();
  if (m_hGpio == NULL)
        {
        DEBUGMSG(ZONE_ERROR, (L"ERROR: GPIO_Backlight::Initialize: "
            L"Failed allocate GPIO handle\r\n"
            ));
    //    goto cleanUp;
        }
    // setup for output mode
    GPIOSetMode(m_hGpio, GPIO_2, GPIO_DIR_OUTPUT);

GPIOSetBit(m_hGpio, GPIO_2);

GPIOClrBit(m_hGpio, GPIO_2);


}
VOID
PddDeinitializeHardware()
{
    DEBUGMSG(ZONE_FUNCTION, (TEXT("PddDeinitializeHardware+\r\n")));


    if (m_hGpio != NULL)
        {
        GPIOClose(m_hGpio);
        m_hGpio = NULL;
        }


    DEBUGMSG(ZONE_FUNCTION, (TEXT("PddDeinitializeHardware-\r\n")));
}


static DWORD CalibrationThread()
{
    HKEY hKey;
    DWORD dwType;
    LONG lResult;
    HANDLE hAPIs;


    DEBUGMSG(ZONE_FUNCTION, (TEXT("CalibrationThread+\r\n")));


    // try to open [HKLM\hardware\devicemap\touch] key
    if (ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE, RK_HARDWARE_DEVICEMAP_TOUCH, 0, KEY_ALL_ACCESS, &hKey))
    {
        DEBUGMSG(ZONE_CALIBRATE, (TEXT("touchp: calibration: Can't find [HKLM/%s]\r\n"), RK_HARDWARE_DEVICEMAP_TOUCH));
        return 0;
    }


    // check for calibration data (query the type of data only)
    lResult = RegQueryValueEx(hKey, RV_CALIBRATION_DATA, 0, &dwType, NULL, NULL);
    RegCloseKey(hKey);
    if (lResult == ERROR_SUCCESS)
    {
        // registry contains calibration data, return
        return 1;
    }


    hAPIs = OpenEvent(EVENT_ALL_ACCESS, FALSE, TEXT("SYSTEM/GweApiSetReady"));
    if (hAPIs)
    {
        WaitForSingleObject(hAPIs, INFINITE);
        CloseHandle(hAPIs);
    }


    // Perform calibration
    //TouchCalibrate();


    // try to open [HKLM\hardware\devicemap\touch] key
    if (ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE, RK_HARDWARE_DEVICEMAP_TOUCH, 0, KEY_ALL_ACCESS, &hKey))
    {
        RETAILMSG(1, (TEXT("touchp: calibration: Can't find [HKLM/%s]\r\n"), RK_HARDWARE_DEVICEMAP_TOUCH));
        return 0;
    }


    // display new calibration data
    lResult = RegQueryValueEx(hKey, RV_CALIBRATION_DATA, 0, &dwType, NULL, NULL);
    if (lResult == ERROR_SUCCESS)
    {
        TCHAR szCalibrationData[100];
        DWORD Size = sizeof(szCalibrationData);


        RegQueryValueEx(hKey, RV_CALIBRATION_DATA, 0, &dwType, (BYTE *) szCalibrationData, (DWORD *) &Size);
        RETAILMSG(1, (TEXT("touchp: calibration: new calibration data is \"%s\"\r\n"), szCalibrationData));
    }
    RegCloseKey(hKey);


    DEBUGMSG(ZONE_FUNCTION, (TEXT("CalibrationThread-\r\n")));


    return 1;
}


void StartCalibrationThread()
{
    HANDLE hThread;


    hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)CalibrationThread, NULL, 0, NULL);
    // We don't need the handle, close it here
    CloseHandle(hThread);
}



ft5206

/*
 * am335x_adc_touch.cpp
 *
 *  Created on: 2013-3-1
 *      Author: STEVEN
 */


#pragma warning(push)
#pragma warning(disable: 4127 4201)
//------------------------------------------------------------------------------
// Public
//
#include
#include
#include
#include


#include
#include
#include


//------------------------------------------------------------------------------
// Platform
//
#include "omap.h"
#include
#include
#include
#include
#include
#include "am335x_IIC_touch.h"
#include "am33x_clocks.h"
#include
#include
#include "sdk_gpio.h"
#include "omap_gptimer_regs.h"


#include "omap_prcm_regs.h"


#include "sdk_i2c.h"
#include
#include
#include "gt9xx.h"


//------------------------------------------------------------------------------
// Debug zones
//
#ifndef SHIP_BUILD


#undef ZONE_ERROR
#undef ZONE_WARN
#undef ZONE_FUNCTION
#undef ZONE_INFO
#undef ZONE_TIPSTATE


#define ZONE_ERROR           DEBUGZONE(0)
#define ZONE_WARN           DEBUGZONE(1)
#define ZONE_FUNCTION       DEBUGZONE(2)
#define ZONE_INFO           DEBUGZONE(3)
#define ZONE_TIPSTATE       DEBUGZONE(4)
#endif


static WCHAR const* s_szRegistryPath = L"\\HARDWARE\\DEVICEMAP\\TOUCH";
#define GPIO_2 ((0<<5)|(2<<0))
UINT32 g_oalPerfTimerIrq = (UINT32)-1;


void StartCalibrationThread();
#define u8 UCHAR
#define u16 UINT
OMAP_GPTIMER_REGS   *g_pPerfTimer = NULL;


//AM335X ADC Touch Internal Funcitons
DWORD gIntrTouchChanged = SYSINTR_NOP;   // Not used here.
DWORD gIntrTouch        = SYSINTR_NOP;
HANDLE m_hGpio;
void beepinitalizehardware();


HANDLE g_hGpio = NULL;


void Delay_us(int n)


{


    LARGE_INTEGER litmp;


       LONGLONG QPart1,QPart2;


       double dfMinus,dfFreq,dfTim;


       QueryPerformanceFrequency(&litmp);


       dfFreq = (double)litmp.QuadPart;


       QueryPerformanceCounter(&litmp);


       QPart1 = litmp.QuadPart;


       do


       {


       QueryPerformanceCounter(&litmp);


          QPart2 = litmp.QuadPart;


          dfMinus = (double)(QPart2-QPart1);


          dfTim = dfMinus/dfFreq;


       }while(dfTim<0.000001*n);


}




static HANDLE                        hI2C;   // I2C Bus Driver
static void udelay(UINT32 delay)
{
volatile UINT32 tmp;
volatile UINT32 j;
UINT32 i;

for(i=0; i for(j=0; j<10000; j++)
tmp = j;
}




#define TOUCH_WRITE     (0x38)// (0x38 + 0)(0x5d)//
#define TOUCH_READ      (0x39)//(0x38 + 1)(0x5e)//
static void *m_hI2CDevice = NULL;
int dwErr=1;
#define TS_DBGON 0


static  bool  IICinit()
{
DWORD dwErr = ERROR_SUCCESS, bytes;
m_hI2CDevice = I2COpen(AM_DEVICE_I2C0);
if (m_hI2CDevice == NULL)
{
OALMSG(1,(L"RTCInit: Failed to open I2C0 driver"));
return FALSE;
}
I2CSetSlaveAddress(m_hI2CDevice, 0x38);
I2CSetSubAddressMode(m_hI2CDevice,I2C_SUBADDRESS_MODE_8);


return TRUE;
    
}




static DWORD
HW_WriteRegisters(
    DWORD startReg,
    PUCHAR pBuff,   // Optional buffer
    DWORD nRegs     // number of registers
    )
{
//RETAILMSG(1,(_T("0x%x 0x%x 0x%x\r\n"),startReg,pBuff[0],nRegs));


if (I2CWrite(m_hI2CDevice,startReg, pBuff,nRegs) != nRegs)
OALMSG(1,(TEXT("HW_WriteRegisters():Write Error!\r\n")));
    return 1;
}


static DWORD
HW_ReadRegisters(
    PUCHAR pBuff,       // Optional buffer
    DWORD StartReg,     // Start Register
    DWORD nRegs         // Number of Registers
    )
{
    if (I2CRead(m_hI2CDevice,StartReg,pBuff,nRegs) != nRegs)
OALMSG(1,(TEXT("HW_ReadRegisters():Read Error!\r\n")));

    return dwErr;
}


static DWORD
HW_ReadRegisters1(
    PUCHAR StartReg1,       // Start Register
    PUCHAR pBuff,     // Optional buffer
    DWORD nRegs         // Number of Registers
    )
{
DWORD StartReg=StartReg1[0]<<8+StartReg1[1];
if (I2CRead(m_hI2CDevice,StartReg,pBuff,nRegs) != nRegs)
OALMSG(1,(TEXT("HW_ReadRegisters():Read Error!\r\n")));

    return 1;
}




static DWORD
HW_WriteRegisters1(
PUCHAR StartReg1,   // Start Register
    PUCHAR pBuff,   // Optional buffer
    DWORD nRegs     // number of registers
    )
{ DWORD StartReg=StartReg1[0]<<8+StartReg1[1];

if (I2CWrite(m_hI2CDevice,StartReg, pBuff,nRegs) != nRegs)
OALMSG(1,(TEXT("HW_WriteRegisters():Write Error!\r\n")));
    return 1;
}


int  fisrtDown = 0;
 BOOL bTSP_DownFlag;


 BOOL
TSP_GetXY(INT *px, INT *py)
{
  unsigned char buf[31];
  int i;
  int num;


  unsigned short x,y;
   
   memset(buf,0,31);


 // HW_ReadRegisters(buf, 0, 31);
 
 
  // RETAILMSG(1,(_T("[TSP]\r\n")));
  for(i = 0 ;i< 31;i++)
{
 HW_ReadRegisters(&buf[i], i, 1);
//  RETAILMSG(1,(_T(" 0x%x"),buf[i]));




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




   num =  buf[2] & 0x07;


 // RETAILMSG(1,(_T("num is %d\r\n"),num));




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




  RETAILMSG(0,(_T("x is %d ,y is %d\r\n"),x,y));
  *px = x;
  *py = y;


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


static void tscadc_getdatapoint(
   
    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(0,(_T("[TSP]  down \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

}


}




//------------------------------------------------------------------------------
// exports
//
extern "C" DWORD gdwTouchIstTimeout;    // MDD thread wait timeout.


// The MDD requires a minimum of MIN_CAL_COUNT consecutive samples before
// it will return a calibration coordinate to GWE.
extern "C" const int MIN_CAL_COUNT = 20;


//------------------------------------------------------------------------------
//
//  TouchDriverCalibrationPointGet
//
extern "C" BOOL
TouchDriverCalibrationPointGet(
    TPDC_CALIBRATION_POINT *pTCP
    )
{
    BOOL rc = FALSE;


    INT32 cDisplayWidth  = pTCP->cDisplayWidth;
    INT32 cDisplayHeight = pTCP->cDisplayHeight;


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


    DEBUGMSG(ZONE_FUNCTION, (TEXT("TouchDriverCalibrationPointGet+\r\n")));


    // Check which of the 5 calibration point is requested.
    switch ( pTCP->PointNumber )
        {
        case 0:
            pTCP->CalibrationX = cDisplayWidth / 2;
            pTCP->CalibrationY = cDisplayHeight / 2;
            rc = TRUE;
            break;


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


        case 2:
            pTCP->CalibrationX = CalibrationRadiusX * 2;
            pTCP->CalibrationY = cDisplayHeight - ( CalibrationRadiusY * 2 );
            rc = TRUE;
            break;


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


        case 4:
            pTCP->CalibrationX = cDisplayWidth - ( CalibrationRadiusX * 2 );
            pTCP->CalibrationY = CalibrationRadiusY * 2;
            rc = TRUE;
            break;


        default:
            pTCP->CalibrationX = cDisplayWidth / 2;
            pTCP->CalibrationY = cDisplayHeight / 2;


            SetLastError( ERROR_INVALID_PARAMETER );
            break;
        }


    DEBUGMSG(ZONE_FUNCTION, (TEXT("cDisplayWidth         : %4X \r\n"), cDisplayWidth     ));
    DEBUGMSG(ZONE_FUNCTION, (TEXT("cDisplayHeight       : %4X \r\n"), cDisplayHeight    ));
    DEBUGMSG(ZONE_FUNCTION, (TEXT("CalibrationRadiusX   : %4d \r\n"), CalibrationRadiusX));
    DEBUGMSG(ZONE_FUNCTION, (TEXT("CalibrationRadiusY   : %4d \r\n"), CalibrationRadiusY));
    DEBUGMSG(ZONE_FUNCTION, (TEXT("pTCP -> PointNumber  : %4d \r\n"), pTCP->PointNumber));
    DEBUGMSG(ZONE_FUNCTION, (TEXT("pTCP -> CalibrationX : %4d \r\n"), pTCP->CalibrationX));
    DEBUGMSG(ZONE_FUNCTION, (TEXT("pTCP -> CalibrationY : %4d \r\n"), pTCP->CalibrationY));


    DEBUGMSG(ZONE_FUNCTION, (TEXT("TouchDriverCalibrationPointGet-\r\n")));
    return ( rc );
}


//------------------------------------------------------------------------------
//
//  DdsiTouchPanelGetDeviceCaps
//
//


extern "C" BOOL
DdsiTouchPanelGetDeviceCaps(
    INT iIndex,
    LPVOID lpOutput
    )
{
    DEBUGMSG(ZONE_FUNCTION, (TEXT("DdsiTouchPanelGetDeviceCaps+\r\n")));


    BOOL rc = FALSE;
    if ( lpOutput == NULL )
        {
        DEBUGMSG(ZONE_ERROR, (TEXT("TouchPanelGetDeviceCaps: Invalid parameter.\r\n")));
        SetLastError( ERROR_INVALID_PARAMETER );
        }
    else
        {
        TPDC_SAMPLE_RATE *pTSR = (TPDC_SAMPLE_RATE*)lpOutput;
        TPDC_CALIBRATION_POINT_COUNT *pTCPC = (TPDC_CALIBRATION_POINT_COUNT*)lpOutput;


        // Check which of the device capabilities are requested.
        switch ( iIndex )
            {
            // Return the sample rate.
            case TPDC_SAMPLE_RATE_ID:


                pTSR->SamplesPerSecondLow       = TOUCHPANEL_SAMPLE_RATE_LOW;
                pTSR->SamplesPerSecondHigh     = TOUCHPANEL_SAMPLE_RATE_HIGH;
                pTSR->CurrentSampleRateSetting = s_TouchDevice.nSampleRate;


                rc = TRUE;
                break;


            // Return the number of calibration points used to calibrate the touch screen.
            case TPDC_CALIBRATION_POINT_COUNT_ID:


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


                rc = TRUE;
                break;


            // Return the x and y coordinates of the requested calibration point.
            // The index of the calibration point is set in lpOutput->PointNumber.
            case TPDC_CALIBRATION_POINT_ID:
                rc = TouchDriverCalibrationPointGet( (TPDC_CALIBRATION_POINT*)lpOutput );
                break;


            default:
                DEBUGMSG( ZONE_ERROR,
                           (TEXT("TouchPanelGetDeviceCaps: Invalid parameter.\r\n")));
                SetLastError(ERROR_INVALID_PARAMETER);
                break;
        }
    }
    DEBUGMSG(ZONE_FUNCTION, (TEXT("DdsiTouchPanelGetDeviceCaps-\r\n")));


    return ( rc );
}




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


    DEBUGMSG(ZONE_FUNCTION, (TEXT("DdsiTouchPanelSetMode+\r\n")));


    switch ( iIndex )
        {
        case TPSM_SAMPLERATE_LOW_ID:
        case TPSM_SAMPLERATE_HIGH_ID:
            SetLastError( ERROR_SUCCESS );
            rc = TRUE;
            break;


        default:
            DEBUGMSG( ZONE_ERROR,
                       (TEXT("DdsiTouchPanelSetMode: Invalid parameter.\r\n")));
            SetLastError( ERROR_INVALID_PARAMETER );
            break;
        }


    DEBUGMSG(ZONE_FUNCTION, (TEXT("DdsiTouchPanelSetMode-\r\n")));


    return rc;
}
#define gpio_rest 3*32+18
#define gpio_int 0*32+7
BOOL SetGPIO(UINT level)
{
    DWORD rc = TRUE;

// Configure Backlight/Power pins as outputs




    /* turn on backlight non-zero level */
    if (level)
    {
    GPIOSetMode(g_hGpio, 3*32+18, GPIO_DIR_OUTPUT );
GPIOPullup(g_hGpio, 3*32+18, GPIO_PULLUP_ENABLE);
    GPIOSetBit(g_hGpio, 3*32+18);

    //GPIOSetMode(g_hGpio, 0*32+1, GPIO_DIR_OUTPUT );
    //GPIOSetBit(g_hGpio, 0*32+1);


GPIOSetMode(g_hGpio, 0*32+7, GPIO_INT_HIGH_LOW );
//GPIOSetBit(g_hGpio, 0*32+7);


RETAILMSG(0, (L"AM33x:[turn on] SetGpio0x%x 0x%x 0x%x 0x%x",GPIOGetBit(g_hGpio, 3*32+18)\
,GPIOGetBit(g_hGpio, 0*32+1)\
,GPIOGetBit(g_hGpio, 0*32+7)));
 


    }
    else if (level == 0 )
    {
    GPIOClrBit(g_hGpio, 3*32+18);
    //GPIOClrBit(g_hGpio, 0*32+1);
//GPIOClrBit(g_hGpio, 0*32+7);
RETAILMSG(0, (L"AM33x: [turn off]SetGpio0x%x 0x%x 0x%x 0x%x",GPIOGetBit(g_hGpio, 3*32+18)\
,GPIOGetBit(g_hGpio, 0*32+1)\
,GPIOGetBit(g_hGpio, 0*32+7)));
    }

 

    return rc;
}
void gtp_reset_guitar()
{

GPIOSetMode(g_hGpio, gpio_rest, GPIO_DIR_OUTPUT );
GPIOClrBit(g_hGpio, gpio_rest);




Sleep(20);


GPIOSetMode(g_hGpio, gpio_int, GPIO_DIR_OUTPUT );
GPIOClrBit(g_hGpio, gpio_int); 


Sleep(2);
GPIOSetBit(g_hGpio, gpio_int);
Sleep(6);




}


 VOID
TSP_SampleStart(VOID)
{

 
    
    SETREG32(&g_pPerfTimer->TCLR, GPTIMER_TCLR_CE);
}


 VOID
TSP_SampleStop(VOID)
{

    CLRREG32(&g_pPerfTimer->TCLR, GPTIMER_TCLR_CE);
}


void  TSP_EnableInt()
{


}


void TSP_DisableInt()
{




}
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,buf, 1);


HW_ReadRegisters1(Wrbuf,buf, 1);


//HW_ReadRegisters(hI2C, Wrbuf,2,TRUE,buf, 1);
RETAILMSG(1,(_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_);
//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()
{


GPIOSetMode(g_hGpio, gpio_rest, GPIO_DIR_OUTPUT );
GPIOClrBit(g_hGpio, gpio_rest);


Sleep(2);




GPIOSetMode(g_hGpio, gpio_int, GPIO_DIR_OUTPUT );
GPIOClrBit(g_hGpio, gpio_int);



Sleep(2);
GPIOSetMode(g_hGpio, gpio_rest, GPIO_DIR_OUTPUT );
GPIOClrBit(g_hGpio, gpio_rest);


Sleep(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);




}


BOOL
DdsiTouchPanelEnable()
{
    UINT32 Irq[3]={-1,OAL_INTR_FORCE_STATIC,0};
    BOOL rc = FALSE;
unsigned char Wrbuf[2];  
UINT srcClock;
    PHYSICAL_ADDRESS pa = { 0, 0 };
    DWORD tclr = 0;
//OMAP_DEVICE gptPerfDevice = AM_DEVICE_TIMER4;
OALMSG(1,(L"DdsiTouchPanelEnable FT502"));

EnableDeviceClocks( AM_DEVICE_GPIO3, TRUE);

EnableDeviceClocks( AM_DEVICE_I2C0, TRUE);
#if 0


   if (gptPerfDevice == OMAP_DEVICE_NONE)
   {
//   return;
   }
   pa.LowPart = GetAddressByDevice(AM_DEVICE_TIMER4);
   g_pPerfTimer = (OMAP_GPTIMER_REGS*)MmMapIoSpace(pa, sizeof(OMAP_GPTIMER_REGS), FALSE);


   // map HighResTimer
   g_oalPerfTimerIrq = GetIrqByDevice(gptPerfDevice,NULL);


   // Select high frequency source clock and frequency
   //PrcmDeviceSetSourceClocks(gptPerfDevice,1,&srcClock);
   // Enable GPTimer for high perf/monte carlo profiling
   EnableDeviceClocks(gptPerfDevice, TRUE);




   // configure performance timer
   //---------------------------------------------------
   // Soft reset GPTIMER and wait until finished
   OUTREG32(&g_pPerfTimer->TCLR, 0);
   SETREG32(&g_pPerfTimer->TIOCP, SYSCONFIG_SOFTRESET);
   OALMSG(1, (L"waiting\r\n"));
   while ((INREG32(&g_pPerfTimer->TISTAT) & GPTIMER_TISTAT_RESETDONE) == 0);
   OALMSG(1, (L"finish\r\n"));


   // Enable smart idle and autoidle
   // Set clock activity - FCLK can be switched off, 
   // L4 interface clock is maintained during wkup.
   OUTREG32(&g_pPerfTimer->TIOCP, 
  0x200 | SYSCONFIG_SMARTIDLE|SYSCONFIG_ENAWAKEUP|
  SYSCONFIG_AUTOIDLE); 


   // Select posted mode
   SETREG32(&g_pPerfTimer->TSICR, GPTIMER_TSICR_POSTED);


   // clear match register
   OUTREG32(&g_pPerfTimer->TMAR, 0xFFFFFFFF);
   
   // clear interrupts
   OUTREG32(&g_pPerfTimer->TISR, 0x00000000);
   
   // enable match interrupt
   OUTREG32(&g_pPerfTimer->TIER, GPTIMER_TIER_MATCH);
   
   // enable wakeups
   OUTREG32(&g_pPerfTimer->TWER, GPTIMER_TWER_MATCH);


   // Set the load register value.
   OUTREG32(&g_pPerfTimer->TLDR, 0x00000000);


   // Trigger a counter reload by writing  
   OUTREG32(&g_pPerfTimer->TTGR, 0xFFFFFFFF);






   //OALMSG(1, (L"****Profiler Build****\r\n"));
//   OALMSG(1, (L"---High Performance Frequency is %d hz---\r\n", s_Frequency));
   
   // build tclr mask
   tclr |= GPTIMER_TCLR_AR;
   OUTREG32(&g_pPerfTimer->TCLR,  tclr);




   //  Start the timer.  Also set for auto reload
   SETREG32(&g_pPerfTimer->TCLR, GPTIMER_TCLR_ST);
   while ((INREG32(&g_pPerfTimer->TWPS) & GPTIMER_TWPS_TCLR) != 0);


#endif



g_hGpio = GPIOOpen();


if (g_hGpio == NULL)
{
RETAILMSG(1, (L"AM33x:Failed to open GPIO driver"));
return FALSE;
}
SetGPIO(1);


// Initialize HW
  s_TouchDevice.nPenIRQ = IRQ_GPIO_7;//IRQ_ECAP0;//GetIrqByDevice(AM_DEVICE_ADC_TSC, NULL);




    Irq[0]=-1;Irq[1]=OAL_INTR_FORCE_STATIC;Irq[2]=s_TouchDevice.nPenIRQ;
    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);
    }
 


    DEBUGMSG(ZONE_FUNCTION, (TEXT("DdsiTouchPanelEnable+\r\n")));
/*
    // Initialize once only
    if (s_TouchDevice.bInitialized)
        {
        rc = TRUE;
        goto cleanup;
        }


    // Initialize HW
    if (!PddInitializeHardware())
        {
        DEBUGMSG(
            ZONE_ERROR,
            (TEXT("ERROR: TOUCH: Failed to initialize touch PDD.\r\n"))
            );
        goto cleanup;
        }
*/
    // Create a calibration thread. This thread will check to see if calibration is needed.
  //  StartCalibrationThread();


    //Done
  //  s_TouchDevice.bInitialized = TRUE;
    rc = TRUE;
/*
IICinit();
gtp_reset_guitar();



 TSP_DisableInt();

  gup_enter_update_mode_fl();
  
  return rc;
*/
GPIOSetBit(g_hGpio, 3*32+18);
Sleep(5);


//Delay_us(5000);
GPIOClrBit(g_hGpio, 3*32+18);
Sleep(20);
//Delay_us(20000);
SetGPIO(1);


IICinit();
Wrbuf[0] = 0x00;
Wrbuf[1] = 0x0;


HW_WriteRegisters(Wrbuf[0], &Wrbuf[1],1);


RETAILMSG(0, (L"AM33x:[turn on] SetGpio0x%x 0x%x 0x%x ",GPIOGetBit(g_hGpio, 3*32+18)\
,GPIOGetBit(g_hGpio, 0*32+1)\
,GPIOGetBit(g_hGpio, 0*32+7)));


Wrbuf[0] = 0x04;
Wrbuf[1] = 0x0;

HW_WriteRegisters(Wrbuf[0], &Wrbuf[1],1);


RETAILMSG(0, (L"AM33x:[turn on] SetGpio0x%x 0x%x 0x%x ",GPIOGetBit(g_hGpio, 3*32+18)\
,GPIOGetBit(g_hGpio, 0*32+1)\
,GPIOGetBit(g_hGpio, 0*32+7)));


Wrbuf[0] = 0x07;
Wrbuf[1] = 0x0;



HW_WriteRegisters(Wrbuf[0], &Wrbuf[1],1);
RETAILMSG(0, (L"AM33x:[turn on] SetGpio0x%x 0x%x 0x%x 0x%x",GPIOGetBit(g_hGpio, 3*32+18)\
,GPIOGetBit(g_hGpio, 0*32+1)\
,GPIOGetBit(g_hGpio, 0*32+7)));


//TSP_SampleStart();


cleanup:
    DEBUGMSG(ZONE_FUNCTION, (TEXT("DdsiTouchPanelEnable-\r\n")));
    return rc;
}




VOID
DdsiTouchPanelDisable()
{
    DEBUGMSG(ZONE_FUNCTION, (TEXT("DdsiTouchPanelDisable+\r\n")));


    // Close pen event and kill thread.
    PddDeinitializeHardware();


    // Release interrupt
    if (gIntrTouch != 0)
        {
        KernelIoControl(
            IOCTL_HAL_RELEASE_SYSINTR,
            &gIntrTouch,
            sizeof(gIntrTouch),
            NULL,
            0,
            NULL
            );
        }


    s_TouchDevice.bInitialized = FALSE;


    DEBUGMSG(ZONE_FUNCTION, (TEXT("DdsiTouchPanelDisable-\r\n")));
}


LONG
DdsiTouchPanelAttach()
{
  
  return (1);
  
}


//------------------------------------------------------------------------------
//
//  DdsiTouchPanelDetach
//
//
LONG
DdsiTouchPanelDetach()
{
return (0);


}


//TODO: NEED TO REWRITE
void
DdsiTouchPanelGetPoint(
    TOUCH_PANEL_SAMPLE_FLAGS *pTipStateFlags,
    INT *pUncalX,
    INT *pUncalY
    )
{
RETAILMSG(0,(_T("DdsiTouchPanelGetPoint\r\n")));
    // By default, any sample returned will be ignored.
    *pTipStateFlags = TouchSampleIgnore;


    tscadc_getdatapoint( pTipStateFlags, pUncalX, pUncalY);
    // Set the proper state for the next interrupt.
    InterruptDone( gIntrTouch);


    DEBUGMSG(ZONE_FUNCTION&&ZONE_SAMPLES, (TEXT("DdsiTouchPanelGetPoint+\r\n")));


    return;
}


void
DdsiTouchPanelPowerHandler(
    BOOL bOff
    )
{
   
}


DWORD dwIrqVal=(DWORD)SYSINTR_UNDEFINED;
HANDLE hIntrEvent;
HANDLE hIntrThread;


DWORD IntrThread(LPVOID lpParameter)
{
UNREFERENCED_PARAMETER(lpParameter);


    DEBUGMSG(ZONE_INIT || ZONE_VERBOSE, (L"+USBCDMA: IntrThread\n"));


    while (1)
    {
        WaitForSingleObject(hIntrEvent, INFINITE);
        
RETAILMSG(1, (L"IntrThread"));


        InterruptDone(dwIrqVal);


       
    }




    return 0;
}


BOOL
PddInitializeHardware(VOID)
{
    BOOL   rc = TRUE;
    PHYSICAL_ADDRESS pa = { 0, 0 };
    int ctrl, irqenable;
    DWORD BytesRet = 0;


    s_TouchDevice.nPenIRQ = IRQ_GPIO_7;//IRQ_ECAP0;//GetIrqByDevice(AM_DEVICE_ADC_TSC, NULL);


    if (!KernelIoControl(IOCTL_HAL_REQUEST_SYSINTR, &s_TouchDevice.nPenIRQ, sizeof(DWORD),
            &gIntrTouch, sizeof(gIntrTouch), NULL))
    {
        
    }
   
    return rc;
}
void beepinitalizehardware()
{
 

 
m_hGpio = GPIOOpen();
  if (m_hGpio == NULL)
        {
        DEBUGMSG(ZONE_ERROR, (L"ERROR: GPIO_Backlight::Initialize: "
            L"Failed allocate GPIO handle\r\n"
            ));
    //    goto cleanUp;
        }
    // setup for output mode
    GPIOSetMode(m_hGpio, GPIO_2, GPIO_DIR_OUTPUT);

GPIOSetBit(m_hGpio, GPIO_2);

GPIOClrBit(m_hGpio, GPIO_2);


}
VOID
PddDeinitializeHardware()
{
    DEBUGMSG(ZONE_FUNCTION, (TEXT("PddDeinitializeHardware+\r\n")));


    if (m_hGpio != NULL)
        {
        GPIOClose(m_hGpio);
        m_hGpio = NULL;
        }


    DEBUGMSG(ZONE_FUNCTION, (TEXT("PddDeinitializeHardware-\r\n")));
}


static DWORD CalibrationThread()
{
    HKEY hKey;
    DWORD dwType;
    LONG lResult;
    HANDLE hAPIs;


    DEBUGMSG(ZONE_FUNCTION, (TEXT("CalibrationThread+\r\n")));


    // try to open [HKLM\hardware\devicemap\touch] key
    if (ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE, RK_HARDWARE_DEVICEMAP_TOUCH, 0, KEY_ALL_ACCESS, &hKey))
    {
        DEBUGMSG(ZONE_CALIBRATE, (TEXT("touchp: calibration: Can't find [HKLM/%s]\r\n"), RK_HARDWARE_DEVICEMAP_TOUCH));
        return 0;
    }


    // check for calibration data (query the type of data only)
    lResult = RegQueryValueEx(hKey, RV_CALIBRATION_DATA, 0, &dwType, NULL, NULL);
    RegCloseKey(hKey);
    if (lResult == ERROR_SUCCESS)
    {
        // registry contains calibration data, return
        return 1;
    }


    hAPIs = OpenEvent(EVENT_ALL_ACCESS, FALSE, TEXT("SYSTEM/GweApiSetReady"));
    if (hAPIs)
    {
        WaitForSingleObject(hAPIs, INFINITE);
        CloseHandle(hAPIs);
    }


    // Perform calibration
    //TouchCalibrate();


    // try to open [HKLM\hardware\devicemap\touch] key
    if (ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE, RK_HARDWARE_DEVICEMAP_TOUCH, 0, KEY_ALL_ACCESS, &hKey))
    {
        RETAILMSG(1, (TEXT("touchp: calibration: Can't find [HKLM/%s]\r\n"), RK_HARDWARE_DEVICEMAP_TOUCH));
        return 0;
    }


    // display new calibration data
    lResult = RegQueryValueEx(hKey, RV_CALIBRATION_DATA, 0, &dwType, NULL, NULL);
    if (lResult == ERROR_SUCCESS)
    {
        TCHAR szCalibrationData[100];
        DWORD Size = sizeof(szCalibrationData);


        RegQueryValueEx(hKey, RV_CALIBRATION_DATA, 0, &dwType, (BYTE *) szCalibrationData, (DWORD *) &Size);
        RETAILMSG(1, (TEXT("touchp: calibration: new calibration data is \"%s\"\r\n"), szCalibrationData));
    }
    RegCloseKey(hKey);


    DEBUGMSG(ZONE_FUNCTION, (TEXT("CalibrationThread-\r\n")));


    return 1;
}


void StartCalibrationThread()
{
    HANDLE hThread;


    hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)CalibrationThread, NULL, 0, NULL);
    // We don't need the handle, close it here
    CloseHandle(hThread);
}


你可能感兴趣的:(am3352 wince gt ft触摸屏驱动)