Chinaunix首页 | 论坛 | 博客
  • 博客访问: 465251
  • 博文数量: 191
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 172
  • 用 户 组: 普通用户
  • 注册时间: 2013-10-31 14:35
个人简介

没有时间把一件事情做好,却有时间把一件事情反复做!

文章分类

全部博文(191)

文章存档

2016年(2)

2015年(74)

2014年(111)

2013年(4)

我的朋友

分类: 嵌入式

2014-03-26 15:31:05

首先来分析一下触摸屏的原理:

首先,它属于输入子系统。还记得我们上篇分析的关于linux输入子系统的架构吗???

 

这里找来了另一张图片:小弟先敬上:

好了,先来一段废话  触摸屏原理分析:(已知的朋友可绕道)

 

原理:由横向电阻比和纵向电阻比组成。由nYPON、YMON、nXPON、XMON四个控制信号控制4个MOS管(S1-S4)的通断。(实质也是同鼠标一样获取坐标)

 

S3C2440触摸屏控制器2种模式:

X/Y位置分别转换模式

X/Y位置自动转换模式

 

工作流程:

选择模式

设置触摸屏接口到等待接口状态

如果中断发生,激活转换模式

获取坐标后,返回等待中断状态

(INT_TC中断用于按下或弹起触摸屏)

(INT_ADC用于坐标转换完成)

 

下面以tq2440触摸屏为例:

/*************************************

NAME:tq2440_ts.c
COPYRIGHT:

*************************************/

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#include
#include

/* For ts.dev.id.version */
#define S3C2410TSVERSION 0x0101

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

static char *tq2440ts_name = "TQ2440 TouchScreen";

static struct input_dev *dev;
static long xp;
static long yp;
static int count;

extern struct semaphore ADC_LOCK;
static int OwnADC = 0;

static void __iomem *base_addr;
/*
static inline void tq2440_ts_connect(void)
{
s3c2410_gpio_cfgpin(S3C2410_GPG12, S3C2410_GPG12_XMON);
s3c2410_gpio_cfgpin(S3C2410_GPG13, S3C2410_GPG13_nXPON);
s3c2410_gpio_cfgpin(S3C2410_GPG14, S3C2410_GPG14_YMON);
s3c2410_gpio_cfgpin(S3C2410_GPG15, S3C2410_GPG15_nYPON);
}
*/
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));

if (updown) {
if (count != 0)
{
long tmp;

tmp = xp;
xp = yp;
yp = tmp;

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;

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
{
count = 0;

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

static struct timer_list touch_timer =
TIMER_INITIALIZER(touch_timer_fire, 0, 0);

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 = (!(data0 & S3C2410_ADCDAT0_UPDOWN)) && (!(data1 & S3C2410_ADCDAT0_UPDOWN));

if (updown)
{
touch_timer_fire(0);
}
else
{
OwnADC = 0;
up(&ADC_LOCK);
}
}

return IRQ_HANDLED;
}


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

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

if (count < (1<<2))
{
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 clk *adc_clock;

static int __init tq2440ts_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 */
// tq2440_ts_connect();

iowrite32(S3C2410_ADCCON_PRSCEN | S3C2410_ADCCON_PRSCVL(0xFF),base_addr+S3C2410_ADCCON);
iowrite32(0xffff, base_addr+S3C2410_ADCDLY);
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;
dev->evbit[0] = BIT(EV_SYN) | BIT(EV_KEY) | BIT(EV_ABS);
dev->keybit[BITS_TO_LONGS(BTN_TOUCH)] = BIT(BTN_TOUCH);
input_set_abs_params(dev, ABS_X, 0, 0x3FF, 0, 0);
input_set_abs_params(dev, ABS_Y, 0, 0x3FF, 0, 0);
input_set_abs_params(dev, ABS_PRESSURE, 0, 1, 0, 0);

dev->name = tq2440ts_name;
dev->id.bustype = BUS_RS232;
dev->id.vendor = 0xDEAD;
dev->id.product = 0xBEEF;
dev->id.version = S3C2410TSVERSION;

if (request_irq(IRQ_ADC, stylus_action, IRQF_SHARED|IRQF_SAMPLE_RANDOM, tq2440ts_name, dev))
{
printk(KERN_ERR "tq2440_ts.c: Could not allocate ts IRQ_ADC !\n");
iounmap(base_addr);
return -EIO;
}
if (request_irq(IRQ_TC, stylus_updown, IRQF_SAMPLE_RANDOM, tq2440ts_name, dev))
{
printk(KERN_ERR "tq2440_ts.c: Could not allocate ts IRQ_ADC !\n");
iounmap(base_addr);
return -EIO;
}

printk(KERN_INFO "%s successfully loaded\n", tq2440ts_name);

input_register_device(dev);

return 0;
}

static void __exit tq2440ts_exit(void)
{
disable_irq(IRQ_ADC);
disable_irq(IRQ_TC);
free_irq(IRQ_TC,dev);
free_irq(IRQ_ADC,dev);

if (adc_clock)
{
clk_disable(adc_clock);
clk_put(adc_clock);
adc_clock = NULL;
}

input_unregister_device(dev);
iounmap(base_addr);
}


module_init(tq2440ts_init);
module_exit(tq2440ts_exit);

 

 

关于代码的分析,大家可以参考:  关于字符驱动与输入子系统的知识,我们暂时结束了。。。。。。。。。。。。

阅读(847) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~