TQ2440 TouchScreen 电阻屏
TQ2440 TouchScreen 电阻屏
==============================
零. 相关链接
输入子系统事件类型:http://www.cnblogs.com/TS-qrt/articles/linux_it.html
一、原理部分:

=== 测X坐标
1.XP接3.3V, XM接地 2.YP, YM不接 3.测试YP电压
=== 测Y坐标
1.YP接3.3V,YM接地 2.XP, XM不接 3.测试XP电压
================================
触摸屏使用过程
1. 按下,产生中断 <------------------------------------------| ↓ | 2. 在中断处理程序里,| 启动 |ADC转换x,y坐标 | 3. ADC结束,产生ADC中断 | 4. 在ADC中断处理函数里,上报(input_event),启动定时器 | 5. 定时器时间到 (处理长按,滑动) ---->-------------------------------|
驱动自带触屏驱动,如果带有触屏驱动的话
-> Device Driver -> Input device support -> Generic input layer -> [*]Touchscreens [ ] ..... [ ] .....
PS: 如果有选择触摸屏驱动,需要去掉,重新编译新的内核(如上图示)
=================================================
二、代码部分
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 <plat/regs-adc.h> #include <mach/regs-gpio.h> struct s3c_ts_regs { 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; static volatile struct s3c_ts_regs* s3c_ts_regs; /* timer */ 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) { s3c_ts_regs->adccon |= (1<<0); } 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(); } } 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 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 irqreturn_t adc_irq(int irq, void *dev_id) { 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)) { //up 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, s3c_ts_regs->adcdat0 & 0x3ff, s3c_ts_regs->adcdat1 & 0x3ff); /* 优化措施3: 多次测量求平均值 */ x[cnt] = s3c_ts_regs->adcdat0 & 0x3ff; y[cnt] = s3c_ts_regs->adcdat1 & 0x3ff; ++ cnt; if(cnt==4) { /* 优化措施4: 软件过滤 */ if(s3c_filter_ts(x,y)) { // printk("x=%d, y=%d\n", (x[1]+x[2]+x[3]+x[4])/4, (y[1]+y[2]+y[3]+y[4])/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)); /* 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: * 设置sADCDLY为最大值, 这使得电压稳定后再发出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(); // enter_wait_pen_up_mode(); return 0; } static void s3c_ts_exit(void) { del_timer(&ts_timer); free_irq(IRQ_TC, NULL); iounmap(s3c_ts_regs); input_unregister_device(s3c_ts_dev); input_free_device(s3c_ts_dev); } module_init(s3c_ts_init); module_exit(s3c_ts_exit); MODULE_LICENSE("GPL");
2.Makifile
obj-m+=s3c_ts.o CC=arm-linux-gcc KERNELDIR=/opt/EmbedSky/linux-2.6.30.4 PWD:=$(shell pwd) default: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules clean: $(MAKE) -C $(KERNELDIR) M=$(PWD) clean
此时测试,只能检测按下松开(基础)
insmod s3c_ts.ko
三、安装 tslib-1.4.tar.gz
sudo apt-get install autoconf sudo apt-get install automake sudo apt-get install libtool 编译: tar xzf tslib-1.4.tar.gz cd tslib ./autogen.sh mkdir tmp echo "ac_cv_func_malloc_0_nonnull=yes" >arm-linux.cache ./configure --host=arm-linux --cache-file=arm-linux.cache --prefix=$(pwd)/tmp make make install 安装: cd tmp @1>cp * -rf /nfsroot(网路文件系统)
@2>slef: cp * -rfd /opt/share ##我的网络文件系统文件夹
cp * -rfd / ##开发板执行,复制到开发板文件系统根目录
ps: @2是自己的操作步骤,代替@1 使用:安装lcd.ko, 参考1>warn 先安装s3c_ts.ko, lcd.ko 1. 修改 /etc/ts.conf第1行(去掉#号和第一个空格): # module_raw input 改为: module_raw input 2.复制一下内容,粘贴到开发板终端(执行) export TSLIB_TSDEVICE=/dev/event0 export TSLIB_CALIBFILE=/etc/pointercal export TSLIB_CONFFILE=/etc/ts.conf export TSLIB_PLUGINDIR=/lib/ts export TSLIB_CONSOLEDEVICE=none export TSLIB_FBDEVICE=/dev/fb0 ps:安装s3c_ts.ko驱动的时候,确认生成的设备是/dev/event0,如下 ls /dev/event* ts_calibrate ts_test
1>lcd驱动安装
可以用内核只带驱动,如果只带驱动没有效果,可在实现下面链接lcd驱动的基础上操作
http://www.cnblogs.com/TS-qrt/articles/embed_lcd.html
insmod cfbcopyarea.ko
insmod cfbfillrect.ko
insmod cfbimgblt.ko
insmod lcd.ko
2>安装问题
./autogen.sh: 4: autoreconf: not found
缺少工具
sudo apt-get install autoconf automake libtool
---------------------------------------------------------------------
上面执行,如果ko就已经结束啦
ts_calibrate ##开发板
ts_test ##开发板
--------------------------------------------------------------------
------------------------THE END !! ----------------------------------


浙公网安备 33010602011771号