触摸屏的主要分为四种,分别是电阻式,电容感应式,红外线式以及表面声波式。
我们现在我们主要谈论是电阻式触摸屏。
看下图可以大概了解电阻式触摸屏。
电阻式触摸屏的屏体部分是一块与显示器表面相匹配的多层复合薄膜,由一层玻璃或有机玻璃作为基层,表面涂有一层透明
的导电层,上面再盖有一层外表面的硬化处理,光滑的防刮的塑料层,它的内表面也涂有一层透明的导电层,在两层导电层之
间有许多细小的透明隔离点把它们隔开绝缘。当手指触摸屏时,平常相互绝缘的两层导电层就在触摸点位置有了一个接触,
因其中一面导电层接通Y轴方向5V均匀电压场,使得侦测层电压由零变为非零,这种接通状态被控制器侦测到后,进行A/D
转换,并将得到的电压值与5V相比即可得到触摸点的Y轴坐标,同理得出X轴的坐标。
S3C2440A触摸屏控制器
linux-2.6.28下的触摸屏源码位置是:\linux-2.6.28.7\drivers\input\touchscreen\s3c2410_ts.c
从加载函数开始分析:
static int __init s3c2410ts_init(void)
{
struct input_dev *input_dev;
adc_clock = clk_get(NULL, "adc");
if (!adc_clock) {
printk(KERN_ERR "failed to get adc clock source\n");
return -ENOENT;
}
clk_enable(adc_clock);
base_addr=ioremap(S3C2410_PA_ADC,0x20);
if (base_addr == NULL) {
printk(KERN_ERR "Failed to remap register block\n");
return -ENOMEM;
}
/* Configure GPIOs */
s3c2410_ts_connect();
//预分频使能,分频值0xFF,写ADCCON
iowrite32(S3C2410_ADCCON_PRSCEN | S3C2410_ADCCON_PRSCVL(0xFF),\
base_addr+S3C2410_ADCCON);
//启动延时,延时值0xffff
iowrite32(0xffff, base_addr+S3C2410_ADCDLY);
//检测笔尖落下,YM使能,YP,XP禁止,等待中断模式
iowrite32(WAIT4INT(0), base_addr+S3C2410_ADCTSC);
/* Initialise input stuff */
input_dev = input_allocate_device();
if (!input_dev) {
printk(KERN_ERR "Unable to allocate the input device !!\n");
return -ENOMEM;
}
dev = input_dev;
//设定input输入子系统同步,键类型
dev->evbit[0] = BIT(EV_SYN) | BIT(EV_KEY) | BIT(EV_ABS);
dev->keybit[BITS_TO_LONGS(BTN_TOUCH)] = BIT(BTN_TOUCH);
//设置绝对坐标X轴,值0至0x3FF
input_set_abs_params(dev, ABS_X, 0, 0x3FF, 0, 0);
//同理Y轴
input_set_abs_params(dev, ABS_Y, 0, 0x3FF, 0, 0);
//同理是否按下ABS_PRESSURE值,0或1
input_set_abs_params(dev, ABS_PRESSURE, 0, 1, 0, 0);
dev->name = s3c2410ts_name;
dev->id.bustype = BUS_RS232;
dev->id.vendor = 0xDEAD;
dev->id.product = 0xBEEF;
dev->id.version = S3C2410TSVERSION;
/* Get irqs */
//ADC中断处理函数stylus_action
if (request_irq(IRQ_ADC, stylus_action, IRQF_SHARED|IRQF_SAMPLE_RANDOM,
"s3c2410_action", dev)) {
printk(KERN_ERR "s3c2410_ts.c: Could not allocate ts IRQ_ADC !\n");
iounmap(base_addr);
return -EIO;
}
//TC中断处理函数stylus_updown
if (request_irq(IRQ_TC, stylus_updown, IRQF_SAMPLE_RANDOM,
"s3c2410_action", dev)) {
printk(KERN_ERR "s3c2410_ts.c: Could not allocate ts IRQ_TC !\n");
iounmap(base_addr);
return -EIO;
}
printk(KERN_INFO "%s successfully loaded\n", s3c2410ts_name);
/* All went ok, so register to the input system */
input_register_device(dev);
return 0;
}
ADC中断处理函数:
static irqreturn_t stylus_action(int irq, void *dev_id)
{
unsigned long data0;
unsigned long data1;
if (OwnADC) {
data0 = ioread32(base_addr+S3C2410_ADCDAT0);
data1 = ioread32(base_addr+S3C2410_ADCDAT1);
//取出X,Y值
xp += data0 & S3C2410_ADCDAT0_XPDATA_MASK;
yp += data1 & S3C2410_ADCDAT1_YPDATA_MASK;
count++;
if (count < (1<<2)) {
/* 再一次启动AD转换*/
iowrite32(S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST, base_addr+S3C2410_ADCTSC);
iowrite32(ioread32(base_addr+S3C2410_ADCCON) | S3C2410_ADCCON_ENABLE_START, base_addr+S3C2410_ADCCON);
} else {
//修改内核计时器
mod_timer(&touch_timer, jiffies+1);
iowrite32(WAIT4INT(1), base_addr+S3C2410_ADCTSC);
}
}
return IRQ_HANDLED;
}
内核计时
static struct timer_list touch_timer =
TIMER_INITIALIZER(touch_timer_fire, 0, 0);
touch_timer_fire函数:
static void touch_timer_fire(unsigned long data)
{
unsigned long data0;
unsigned long data1;
int updown;
data0 = ioread32(base_addr+S3C2410_ADCDAT0);
data1 = ioread32(base_addr+S3C2410_ADCDAT1);
updown = (!(data0 & S3C2410_ADCDAT0_UPDOWN)) && (!(data1 & S3C2410_ADCDAT0_UPDOWN));
//data1,data0都为0落下进入
if (updown) {
if (count != 0) {
long tmp;
tmp = xp;
xp = yp;
yp = tmp;
/*取4次平均值*/
xp >>= 2;
yp >>= 2;
/*上报坐标*/
input_report_abs(dev, ABS_X, xp);
input_report_abs(dev, ABS_Y, yp);
input_report_key(dev, BTN_TOUCH, 1);
input_report_abs(dev, ABS_PRESSURE, 1);
input_sync(dev);
}
xp = 0;
yp = 0;
count = 0;
/* 启动AD 转换*/
iowrite32(S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST, base_addr+S3C2410_ADCTSC);
//X方向测量
iowrite32(ioread32(base_addr+S3C2410_ADCCON) | S3C2410_ADCCON_ENABLE_START, base_addr+S3C2410_ADCCON);
} else {
count = 0;
//置位BTN_TOUCH,ABS_PRESSURE
input_report_key(dev, BTN_TOUCH, 0);
input_report_abs(dev, ABS_PRESSURE, 0);
input_sync(dev);
iowrite32(WAIT4INT(0), base_addr+S3C2410_ADCTSC);
if (OwnADC) {
OwnADC = 0;
up(&ADC_LOCK);
}
}
}
TC中断:
static irqreturn_t stylus_updown(int irq, void *dev_id)
{
unsigned long data0;
unsigned long data1;
int updown;
if (down_trylock(&ADC_LOCK) == 0) {
OwnADC = 1;
data0 = ioread32(base_addr+S3C2410_ADCDAT0);
data1 = ioread32(base_addr+S3C2410_ADCDAT1);
/*updown ==1 落下*/
updown = (!(data0 & S3C2410_ADCDAT0_UPDOWN)) && (!(data1 & S3C2410_ADCDAT0_UPDOWN));
if (updown) {//笔尖落下,data0,data1都为落下
touch_timer_fire(0);
} else {//笔尖弹起
OwnADC = 0;
up(&ADC_LOCK);
}
}
return IRQ_HANDLED;
}