touch.c 5.2 KB
Newer Older
1 2
#include <rthw.h>
#include <rtthread.h>
B
bernard.xiong 已提交
3
#include <s3c24x0.h>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254

/* ADCCON Register Bits */
#define S3C2410_ADCCON_ECFLG		(1<<15)
#define S3C2410_ADCCON_PRSCEN		(1<<14)
#define S3C2410_ADCCON_PRSCVL(x)	(((x)&0xFF)<<6)
#define S3C2410_ADCCON_PRSCVLMASK	(0xFF<<6)
#define S3C2410_ADCCON_SELMUX(x)	(((x)&0x7)<<3)
#define S3C2410_ADCCON_MUXMASK		(0x7<<3)
#define S3C2410_ADCCON_STDBM		(1<<2)
#define S3C2410_ADCCON_READ_START	(1<<1)
#define S3C2410_ADCCON_ENABLE_START	(1<<0)
#define S3C2410_ADCCON_STARTMASK	(0x3<<0)


/* ADCTSC Register Bits */
#define S3C2410_ADCTSC_UD_SEN		(1<<8) /* ghcstop add for s3c2440a */
#define S3C2410_ADCTSC_YM_SEN		(1<<7)
#define S3C2410_ADCTSC_YP_SEN		(1<<6)
#define S3C2410_ADCTSC_XM_SEN		(1<<5)
#define S3C2410_ADCTSC_XP_SEN		(1<<4)
#define S3C2410_ADCTSC_PULL_UP_DISABLE	(1<<3)
#define S3C2410_ADCTSC_AUTO_PST		(1<<2)
#define S3C2410_ADCTSC_XY_PST(x)	(((x)&0x3)<<0)

/* ADCDAT0 Bits */
#define S3C2410_ADCDAT0_UPDOWN		(1<<15)
#define S3C2410_ADCDAT0_AUTO_PST	(1<<14)
#define S3C2410_ADCDAT0_XY_PST		(0x3<<12)
#define S3C2410_ADCDAT0_XPDATA_MASK	(0x03FF)

/* ADCDAT1 Bits */
#define S3C2410_ADCDAT1_UPDOWN		(1<<15)
#define S3C2410_ADCDAT1_AUTO_PST	(1<<14)
#define S3C2410_ADCDAT1_XY_PST		(0x3<<12)
#define S3C2410_ADCDAT1_YPDATA_MASK	(0x03FF)

#define WAIT4INT(x)  (((x)<<8) | \
		     S3C2410_ADCTSC_YM_SEN | S3C2410_ADCTSC_YP_SEN | S3C2410_ADCTSC_XP_SEN | \
		     S3C2410_ADCTSC_XY_PST(3))

#define AUTOPST	     (S3C2410_ADCTSC_YM_SEN | S3C2410_ADCTSC_YP_SEN | S3C2410_ADCTSC_XP_SEN | \
		     S3C2410_ADCTSC_AUTO_PST | S3C2410_ADCTSC_XY_PST(0))

struct s3c2410ts
{
	long xp;
	long yp;
	int count;
	int shift;

	int delay;
	int presc;

	char phys[32];
};
static struct s3c2410ts ts;

#define X_MIN		74
#define X_MAX		934
#define Y_MIN		65
#define Y_MAX		933

#ifdef RT_USING_RTGUI
#include <rtgui/event.h>
void report_touch_input(int updown)
{
	long tmp;
	struct rtgui_event_mouse emouse;

	tmp = ts.xp;
	ts.xp = ts.yp;
	ts.yp = tmp;

	ts.xp >>= ts.shift;
	ts.yp >>= ts.shift;

	ts.xp = 240 * (ts.xp-X_MIN)/(X_MAX-X_MIN);
	ts.yp = 320 - (320*(ts.yp-Y_MIN)/(Y_MAX-Y_MIN));

	emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
	emouse.parent.sender = RT_NULL;

	emouse.x = ts.xp;
	emouse.y = ts.yp;

	/* set emouse button */
	if (updown)
	{
		emouse.button = RTGUI_MOUSE_BUTTON_DOWN;
	}
	else
	{
		emouse.button = RTGUI_MOUSE_BUTTON_UP;
	}

	rt_kprintf("touch %s: ts.x: %d, ts.y: %d, count:%d\n", updown? "down" : "up",
		ts.xp, ts.yp, ts.count);

	emouse.button |= RTGUI_MOUSE_BUTTON_LEFT;

	rtgui_server_post_event(&emouse, sizeof(struct rtgui_event_mouse));
}
#endif

static int first_down_report;
static void touch_timer_fire(void* parameter)
{
  	rt_uint32_t data0;
  	rt_uint32_t data1;
	int updown;

  	data0 = ADCDAT0;
  	data1 = ADCDAT1;

 	updown = (!(data0 & S3C2410_ADCDAT0_UPDOWN)) && (!(data1 & S3C2410_ADCDAT0_UPDOWN));

 	if (updown)
	{
 		if (ts.count != 0)
		{
			// if (first_down_report)
			{
#ifdef RT_USING_RTGUI
				report_touch_input(updown);
				first_down_report = 0;
#endif
			}
 		}

 		ts.xp = 0;
 		ts.yp = 0;
 		ts.count = 0;

 		ADCTSC = S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST;
 		ADCCON |= S3C2410_ADCCON_ENABLE_START;
 	}
	else
	{
		// if (ts.xp >= 0 && ts.yp >= 0)
		{
#ifdef RT_USING_RTGUI
			report_touch_input(updown);
			first_down_report = 1;
#endif
		}

 		ts.count = 0;

		rt_kprintf("touch time fire: up\n");

 		ADCTSC = WAIT4INT(0);
 	}
}

void s3c2410_adc_stylus_action()
{
	rt_uint32_t data0;
	rt_uint32_t data1;

	data0 = ADCDAT0;
	data1 = ADCDAT1;

	ts.xp += data0 & S3C2410_ADCDAT0_XPDATA_MASK;
	ts.yp += data1 & S3C2410_ADCDAT1_YPDATA_MASK;
	ts.count++;

	if (ts.count < (1<<ts.shift))
	{
		ADCTSC = S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST;
		ADCCON |= S3C2410_ADCCON_ENABLE_START;
	}
	else
	{
		touch_timer_fire(0);
		ADCTSC = WAIT4INT(1);
	}

	SUBSRCPND |= BIT_SUB_ADC;
}

void s3c2410_intc_stylus_updown()
{
	rt_uint32_t data0;
	rt_uint32_t data1;
	int updown;

	data0 = ADCDAT0;
	data1 = ADCDAT1;

	updown = (!(data0 & S3C2410_ADCDAT0_UPDOWN)) && (!(data1 & S3C2410_ADCDAT0_UPDOWN));

	rt_kprintf("stylus: %s\n", updown? "down" : "up");

	if (updown) touch_timer_fire(RT_NULL);
	else
	{
		if (ts.xp >= 0 && ts.yp >= 0)
		{
		#ifdef RT_USING_RTGUI
			report_touch_input(updown);
			first_down_report = 1;
		#endif
		}

		ADCTSC = WAIT4INT(0);
	}

	SUBSRCPND |= BIT_SUB_TC;
}

void rt_touch_handler(int irqno)
{
	if (SUBSRCPND & (1 << 10))
	{
		/* INT_SUB_ADC */
		s3c2410_adc_stylus_action();
	}

	if (SUBSRCPND & (1 << 9))
	{
		/* INT_SUB_TC */
		s3c2410_intc_stylus_updown();
	}

	/* clear interrupt */
	INTPND |= (1 << INTADC);
}

void rt_hw_touch_init()
{
	/* init touch screen structure */
	rt_memset(&ts, 0, sizeof(struct s3c2410ts));

	ts.delay = 5000;
	ts.presc = 49;
	ts.shift = 2;

	ADCCON = S3C2410_ADCCON_PRSCEN | S3C2410_ADCCON_PRSCVL(ts.presc);
	ADCDLY = ts.delay;

	ADCTSC = WAIT4INT(0);

	rt_hw_interrupt_install(INTADC, rt_touch_handler, RT_NULL);
	rt_hw_interrupt_umask(INTADC);

	/* install interrupt handler */
	INTSUBMSK &= ~BIT_SUB_ADC;
	INTSUBMSK &= ~BIT_SUB_TC;

	first_down_report = 1;
}