DSP

am3352 wince gt ft触摸屏驱动

2019-07-13 16:22发布

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 "),startReg,pBuff[0],nRegs));


if (I2CWrite(m_hI2CDevice,startReg, pBuff,nRegs) != nRegs)
OALMSG(1,(TEXT("HW_WriteRegisters():Write Error! ")));
    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! ")));

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

    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 "),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(" gup_set_ic_msg 0x%x 0x%x "),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(" gup_set_ic_msg 0x%x 0x%x "),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(" gup_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(" gup_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(" [enter_update_mode]Hold ss51 & dsp confirm SUCCESS")));
else 
{
RETAILMSG(1,(_T(" error 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(" 0x41E4 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(" 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(" 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(" gup_check_and_repair%x 0x%x 0x%x"),start_addr+i,buf[0],Wrbuf[2]));
 
  
  
 }
  //  RETAILMSG(1,(_T(" gup_check_and_repair123%x 0x%x 0x%x "),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(" gtp_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 "),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(" ")));


}
  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 "),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 ")));
  
 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 "),buf[0]));
  if(((i+1)%10==0))
  {
 //RETAILMSG(1,(_T(" ")));
  
  }
 }
 
 RETAILMSG(1,(_T("-------------HW_ReadRegisters ")));
#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] ")));
for(i = 0 ;i< 31;i++)
 {
HW_ReadRegisters(&buf[i], i, 1);
 // RETAILMSG(1,(_T(" 0x%x"),buf[i]));
  
  
 }
  //  RETAILMSG(1,(_T("[TSP] ")));
  
  
num = buf[2] & 0x07;
  
   // RETAILMSG(1,(_T("num is %d "),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 "),x,y));
 RETAILMSG(1, (L"waiting1240x%x 0x%x 0x%x ",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] ")));
   
  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(" 0x%x"),buf[0]));
}
 
if(buf[0]==2)
{
RETAILMSG(1,(_T(" 0x%x"),buf[0]));
}
 
if(buf[0]==1)
{
RETAILMSG(1,(_T(" 0x%x"),buf[0]));
gtp_init_panel();RQST_MAIN_CLOCK();
}
 
if(buf[0]==3)
{
RETAILMSG(1,(_T(" 0x%x"),buf[0]));
}
 
//RETAILMSG(1,(_T(" ")));
}
 
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(" getxy %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 ")));
if (fisrtDown  == 0) //down firt interrup here
{
RETAILMSG(TS_DBGON,(_T("[TSP]  down ")));

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 ")));
*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+ ")));


    // 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 "), cDisplayWidth     ));
    DEBUGMSG(ZONE_FUNCTION, (TEXT("cDisplayHeight       : %4X "), cDisplayHeight    ));
    DEBUGMSG(ZONE_FUNCTION, (TEXT("CalibrationRadiusX   : %4d "), CalibrationRadiusX));
    DEBUGMSG(ZONE_FUNCTION, (TEXT("CalibrationRadiusY   : %4d "), CalibrationRadiusY));
    DEBUGMSG(ZONE_FUNCTION, (TEXT("pTCP -> PointNumber  : %4d "), pTCP->PointNumber));
    DEBUGMSG(ZONE_FUNCTION, (TEXT("pTCP -> CalibrationX : %4d "), pTCP->CalibrationX));
    DEBUGMSG(ZONE_FUNCTION, (TEXT("pTCP -> CalibrationY : %4d "), pTCP->CalibrationY));


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


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


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


    BOOL rc = FALSE;
    if ( lpOutput == NULL )
        {
        DEBUGMSG(ZONE_ERROR, (TEXT("TouchPanelGetDeviceCaps: Invalid parameter. ")));
        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. ")));
                SetLastError(ERROR_INVALID_PARAMETER);
                break;
        }
    }
    DEBUGMSG(ZONE_FUNCTION, (TEXT("DdsiTouchPanelGetDeviceCaps- ")));


    return ( rc );
}




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


    DEBUGMSG(ZONE_FUNCTION, (TEXT("DdsiTouchPanelSetMode+ ")));


    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. ")));
            SetLastError( ERROR_INVALID_PARAMETER );
            break;
        }


    DEBUGMSG(ZONE_FUNCTION, (TEXT("DdsiTouchPanelSetMode- ")));


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