Linux驱动之触摸屏

简介:

我的开发板是FL2440,其他的开发板在以下代码中基本上是一样的,还需要注意的是,不同的内核用的头文件可能不一样。还要注意下LCD显示屏和触摸屏是两个概念,触摸屏用的是两章很薄的电阻纸片重合在一起贴在LCD显示屏上的。触摸屏是对ADC转换的一种应用,初学者要多看寄存器手册,上面讲得很详细,开始可以参考别人的代码,但是一定要理解过来,转换成自己的思想,废话不多说了,请看下面的代码。

内核版本2.6.28.7       虚拟机ubuntu :9.1

#include <linux/errno.h>  
#include <linux/kernel.h>  
#include <linux/module.h>  
#include <linux/slab.h>  
#include <linux/input.h>  
#include <linux/init.h>  
#include <linux/serio.h>  
#include <linux/delay.h>  
#include <linux/platform_device.h>  
#include <linux/clk.h>  
#include <asm/io.h>  
#include <asm/irq.h>  
  
#include <mach/regs-gpio.h>  
#include <mach/s3c2410_ts.h>  
  
#include <plat/regs-adc.h>  

struct s3c_ts_regs {   /* 是adc所用到的寄存器,放到结构体中方便我们后面用指针操作 ,只是定义了这种类型没有分配内存,不能添加static,后面代码中必须把这个结构体的首地址映射到adc寄存器的首地址,我们就可以直接操作adc寄存器了,记住adc寄存器必须是连续的,我们映射是一一对应的,我们才入口函数中映射*/
unsigned long adccon;
unsigned long adctsc;
unsigned long adcdly;
unsigned long adcdat0;
unsigned long adcdat1;
unsigned long adcupdn;
};

static struct input_dev *s3c_ts_dev;                 /* 定义input_dev类型的结构体指针,input输入事件是重中之重 */
static volatile struct s3c_ts_regs *s3c_ts_regs;
static struct timer_list ts_timer;                 /* 对定时器的操作,方便定时 */


static void enter_wait_pen_down_mode(void)   /* 等待触摸笔按下模式 */
{
s3c_ts_regs->adctsc = 0xd3;
}


static void enter_wait_pen_up_mode(void)  /* 等待触摸笔按下模式 */
{
s3c_ts_regs->adctsc = 0x1d3;
}


static void enter_measure_xy_mode(void)   /* 等进入测量模式 */
{
s3c_ts_regs->adctsc = (1<<3)|(1<<2);//禁止上拉,设置自动进入测量模式
}


static void start_adc(void)     /* 开始启动转换ADC */
{
s3c_ts_regs->adccon |= (1<<0);
}


static int s3c_filter_ts(int x[], int y[])   /* 一个软件上处理数据,减小误差 */
{
#define ERR_LIMIT 10

int avr_x, avr_y;
int det_x, det_y;

avr_x = (x[0] + x[1])/2;
avr_y = (y[0] + y[1])/2;

det_x = (x[2] > avr_x) ? (x[2] - avr_x) : (avr_x - x[2]);
det_y = (y[2] > avr_y) ? (y[2] - avr_y) : (avr_y - y[2]);

if ((det_x > ERR_LIMIT) || (det_y > ERR_LIMIT))
return 0;

avr_x = (x[1] + x[2])/2;
avr_y = (y[1] + y[2])/2;

det_x = (x[3] > avr_x) ? (x[3] - avr_x) : (avr_x - x[3]);
det_y = (y[3] > avr_y) ? (y[3] - avr_y) : (avr_y - y[3]);

if ((det_x > ERR_LIMIT) || (det_y > ERR_LIMIT))
return 0;

return 1;
}

static void s3c_ts_timer_function(unsigned long data)    /* 定时器的子函数 */
{
if (s3c_ts_regs->adcdat0 & (1<<15))
{
/* 已经松开 */
input_report_abs(s3c_ts_dev, ABS_PRESSURE, 0);   /* 上报事件 */
input_report_key(s3c_ts_dev, BTN_TOUCH, 0);
input_sync(s3c_ts_dev);
enter_wait_pen_down_mode();
}
else
{
/* 测量X/Y坐标 */
enter_measure_xy_mode();   /* 进入测量模式 */
start_adc(); /* 开始转换后ADC */
}
}

static irqreturn_t pen_down_up_irq(int irq, void *dev_id)   /* 触摸笔按下将产生一个中断 */
{
if (s3c_ts_regs->adcdat0 & (1<<15))
{
//printk("pen up\n");
input_report_abs(s3c_ts_dev, ABS_PRESSURE, 0);
input_report_key(s3c_ts_dev, BTN_TOUCH, 0);
input_sync(s3c_ts_dev);
enter_wait_pen_down_mode();
}
else
{
//printk("pen down\n");
//enter_wait_pen_up_mode();
enter_measure_xy_mode();
start_adc();
}
return IRQ_HANDLED;
}


static irqreturn_t adc_irq(int irq, void *dev_id)    /* adc转换完成将产生一个中断 */
{
static int cnt = 0;
static int x[4], y[4];
int adcdat0, adcdat1;

/* 优化措施2: 如果ADC完成时, 发现触摸笔已经松开, 则丢弃此次结果 */
adcdat0 = s3c_ts_regs->adcdat0;
adcdat1 = s3c_ts_regs->adcdat1;

if (s3c_ts_regs->adcdat0 & (1<<15))
{
/* 已经松开 */
cnt = 0;
input_report_abs(s3c_ts_dev, ABS_PRESSURE, 0);
input_report_key(s3c_ts_dev, BTN_TOUCH, 0);
input_sync(s3c_ts_dev);
enter_wait_pen_down_mode();
}
else
{
// printk("adc_irq cnt = %d, x = %d, y = %d\n", ++cnt, adcdat0 & 0x3ff, adcdat1 & 0x3ff);
/* 优化措施3: 多次测量求平均值 */
x[cnt] = adcdat0 & 0x3ff;
y[cnt] = adcdat1 & 0x3ff;
++cnt;
if (cnt == 4)
{
/* 优化措施4: 软件过滤 */
if (s3c_filter_ts(x, y))
{
//printk("x = %d, y = %d\n", (x[0]+x[1]+x[2]+x[3])/4, (y[0]+y[1]+y[2]+y[3])/4);
input_report_abs(s3c_ts_dev, ABS_X, (x[0]+x[1]+x[2]+x[3])/4);
input_report_abs(s3c_ts_dev, ABS_Y, (y[0]+y[1]+y[2]+y[3])/4);
input_report_abs(s3c_ts_dev, ABS_PRESSURE, 1);
input_report_key(s3c_ts_dev, BTN_TOUCH, 1);
input_sync(s3c_ts_dev);
}
cnt = 0;
enter_wait_pen_up_mode();

/* 启动定时器处理长按/滑动的情况 */
mod_timer(&ts_timer, jiffies + HZ/100);
}
else
{
enter_measure_xy_mode();
start_adc();
}
}
return IRQ_HANDLED;
}


static int s3c_ts_init(void)
{
struct clk* clk;

/* 1. 分配一个input_dev结构体 */
s3c_ts_dev = input_allocate_device();

/* 2. 设置 */
/* 2.1 能产生哪类事件 */
set_bit(EV_KEY, s3c_ts_dev->evbit);
set_bit(EV_ABS, s3c_ts_dev->evbit);

/* 2.2 能产生这类事件里的哪些事件 */
set_bit(BTN_TOUCH, s3c_ts_dev->keybit);

input_set_abs_params(s3c_ts_dev, ABS_X, 0, 0x3FF, 0, 0);
input_set_abs_params(s3c_ts_dev, ABS_Y, 0, 0x3FF, 0, 0);
input_set_abs_params(s3c_ts_dev, ABS_PRESSURE, 0, 1, 0, 0);

/* 3. 注册 */
input_register_device(s3c_ts_dev);

/* 4. 硬件相关的操作 */
/* 4.1 使能时钟(CLKCON[15]) */
clk = clk_get(NULL, "adc");
clk_enable(clk);

/* 4.2 设置S3C2440的ADC/TS寄存器 */
s3c_ts_regs = ioremap(0x58000000, sizeof(struct s3c_ts_regs));  /* 这里就把我们定义的结构体类型和adc寄存器地址相关连起来了,这里映射函数有兴趣的朋友可以去分析分析,看看怎么把物理地址转换成虚拟地址的 */

/* bit[14]  : 1-A/D converter prescaler enable
* bit[13:6]: A/D converter prescaler value,
*            49, ADCCLK=PCLK/(49+1)=50MHz/(49+1)=1MHz
* bit[0]: A/D conversion starts by enable. 先设为0
*/
s3c_ts_regs->adccon = (1<<14)|(49<<6);

request_irq(IRQ_TC, pen_down_up_irq, IRQF_SAMPLE_RANDOM, "ts_pen", NULL);
request_irq(IRQ_ADC, adc_irq, IRQF_SAMPLE_RANDOM, "adc", NULL);

/* 优化措施1: 
* 设置ADCDLY为最大值, 这使得电压稳定后再发出IRQ_TC中断
*/
s3c_ts_regs->adcdly = 0xffff;

/* 优化措施5: 使用定时器处理长按,滑动的情况

*/
init_timer(&ts_timer);
ts_timer.function = s3c_ts_timer_function;
add_timer(&ts_timer);
enter_wait_pen_down_mode();
return 0;
}

static void s3c_ts_exit(void)
{
free_irq(IRQ_TC, NULL);
free_irq(IRQ_ADC, NULL);
iounmap(s3c_ts_regs);
input_unregister_device(s3c_ts_dev);
input_free_device(s3c_ts_dev);
del_timer(&ts_timer);
}

module_init(s3c_ts_init);
module_exit(s3c_ts_exit);
MODULE_LICENSE("GPL");


测试如下:

1. make menuconfig 去掉原来的触摸屏驱动程序
-> Device Drivers
  -> Input device support
    -> Generic input layer
      -> Touchscreens
      <>   S3C2410/S3C2440 touchscreens

make uImage
使用新内核启动

2. insmod s3c_ts.ko
按下/松开触摸笔
1. ls /dev/event* 
2. insmod s3c_ts.ko
3. ls /dev/event* 
4. hexdump /dev/event0
            秒       微秒   type code    value
0000000 29a4 0000 8625 0008 0003 0000 0172 0000
0000010 29a4 0000 8631 0008 0003 0001 027c 0000
0000020 29a4 0000 8634 0008 0003 0018 0001 0000
0000030 29a4 0000 8638 0008 0001 014a 0001 0000
0000040 29a4 0000 863c 0008 0000 0000 0000 0000
0000050 29a4 0000 c85e 0008 0003 0000 0171 0000
0000060 29a4 0000 c874 0008 0003 0001 027d 0000
0000070 29a4 0000 c87b 0008 0000 0000 0000 0000
0000080 29a4 0000 ed37 0008 0003 0018 0000 0000
0000090 29a4 0000 ed48 0008 0001 014a 0000 0000
00000a0 29a4 0000 ed4a 0008 0000 0000 0000 0000


或者使用tslib库来测试校验触摸屏函数,前提是你lcd驱动程序能够正常工作,具体实现方法请看我tslib移植这篇文章。

目录
相关文章
|
2月前
|
Linux API 调度
Linux系统驱动跟裸机驱动的区别
Linux系统驱动跟裸机驱动的区别
31 0
|
2月前
|
Linux C语言 SoC
嵌入式linux总线设备驱动模型分析
嵌入式linux总线设备驱动模型分析
32 1
|
2月前
|
存储 缓存 Linux
【Shell 命令集合 磁盘维护 】Linux 设置和查看硬盘驱动器参数 hdparm命令使用教程
【Shell 命令集合 磁盘维护 】Linux 设置和查看硬盘驱动器参数 hdparm命令使用教程
38 0
|
2月前
|
分布式计算 关系型数据库 MySQL
Sqoop【部署 01】CentOS Linux release 7.5 安装配置 sqoop-1.4.7 解决警告并验证(附Sqoop1+Sqoop2最新版安装包+MySQL驱动包资源)
【2月更文挑战第8天】Sqoop CentOS Linux release 7.5 安装配置 sqoop-1.4.7 解决警告并验证(附Sqoop1+Sqoop2最新版安装包+MySQL驱动包资源)
103 1
|
15天前
|
Linux Go
Linux命令Top 100驱动人生! 面试必备
探索Linux命令不再迷茫!本文分10部分详解20个基础命令,带你由浅入深掌握文件、目录管理和文本处理。 [1]: <https://cloud.tencent.com/developer/article/2396114> [2]: <https://pan.quark.cn/s/865a0bbd5720> [3]: <https://yv4kfv1n3j.feishu.cn/docx/MRyxdaqz8ow5RjxyL1ucrvOYnnH>
68 0
|
28天前
|
Linux
Linux驱动运行灯 Heartbeat
Linux驱动运行灯 Heartbeat
12 0
|
2月前
|
Linux
Linux内核中USB设备驱动实现
Linux内核中USB设备驱动实现
28 0
|
3月前
|
Ubuntu Linux 芯片
Linux 驱动开发基础知识——设备树的语法驱动开发基础知识(九)
Linux 驱动开发基础知识——设备树的语法驱动开发基础知识(九)
74 1
Linux 驱动开发基础知识——设备树的语法驱动开发基础知识(九)
|
3月前
|
Linux
Linux 驱动开发基础知识——总线设备驱动模型(八)
Linux 驱动开发基础知识——总线设备驱动模型(八)
40 0
Linux 驱动开发基础知识——总线设备驱动模型(八)