////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) Microsoft Corporation.  All rights reserved.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#ifndef _SH7262RSKTOUCH_DRIVER_H_
#define _SH7262RSKTOUCH_DRIVER_H_ 1


#include <tinyhal.h>
#include <machine.h>

#define 	I2C_KHZ				0xE
#define 	I2C_CHANNEL			1
#define 	TS_SLAVE_ADDR		0x51

#define 	I2C_BUS_BUSY_TIMEOUT        300000
#define 	I2C_STOP_TIMEOUT            300000
#define 	I2C_TDRE_TIMEOUT            300000
#define 	I2C_TEND_TIMEOUT            1000000

//ICSR
#define		I2C_ICSR_TEND		(1 << 6)
#define		I2C_ICSR_TDRE		(1 << 7)
#define		I2C_ICSR_STOP		(1 << 3)

// ICCR1
#define 	I2C_ICCR1_ICE		(1 << 7)
#define 	I2C_ICCR1_RCVD		(1 << 6)
#define 	I2C_ICCR1_MST		(1 << 5)
#define 	I2C_ICCR1_TRS 		(1 << 4)

// IICR2
#define 	I2C_ICCR2_BBSY		(1 << 7)
#define 	I2C_ICCR2_SCP		(1 << 6)

struct SH7262TOUCH_Driver
{
    static BOOL Enable(GPIO_INTERRUPT_SERVICE_ROUTINE touchIsrProc);
    static BOOL Disable();
    static void GetPoint(TOUCH_PANEL_SAMPLE_FLAGS* pTipState, int* pSource, int* pUnCalX, int* pUnCalY );
    static HRESULT GetDeviceCaps(unsigned int iIndex, void* lpOutput);
	GPIO_INTERRUPT_SERVICE_ROUTINE m_touchIsrProc;
//	static BYTE ReadEvent();
//private:
//	static void I2CInitialize();
//	static BOOL I2CWriteByte(BYTE subaddr, BYTE data);
//	static BOOL I2CReadByte(BYTE subaddr, BYTE *data);
//	static void I2CFailure();
	static void IRQEnable();
	static void IRQDisable();
	static BOOL Initialize();	
//	static void Volt2Pixel(int *x, int *y);
    static BOOL CalibrationPointGet(TOUCH_PANEL_CALIBRATION_POINT *pTCP);

	static const X_VoltageMax = 975;
	static const X_VoltageMin = 190;
	static const Y_VoltageMax = 950;
	static const Y_VoltageMin = 190;
	static const X_Screen = 640;
	static const Y_Screen = 480;
	
	//SH7262 Touch Panel
	static void CMT0_Initialize(void);
	static void TOUCH_ADXstart(void);
	static unsigned short int TOUCH_ADXin(void);
	static void TOUCH_ADYstart(void);
	static unsigned short int TOUCH_ADYin(void);
	static void TOUCH_AllHiZ(void);
	static void TOUCH_X1X2(int i);
	static void TOUCH_Y1Y2(int i);
	static void TOUCH_PenDownInitialize(void);
	static int TOUCH_PenDownCheck(void);
	//static void TOUCH_Interrupt(void);
	static int TOUCH_GetPenDown(void);
	static unsigned int TOUCH_GetXY(int mode);	
	static int TOUCH_ADCConv(void);
	static void TMU2_Initialize(void);
	static void TMU2_ResetCNT();
};

void SH7262_TOUCH_ISR(void);
extern struct SH7262TOUCH_Driver g_SH7262TOUCH_Driver;
#endif
