Chinaunix首页 | 论坛 | 博客
  • 博客访问: 410209
  • 博文数量: 115
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 393
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-26 12:10
个人简介

踏实做事,认真做人

文章分类

全部博文(115)

文章存档

2017年(1)

2016年(2)

2015年(14)

2014年(63)

2013年(35)

分类: LINUX

2015-06-29 18:39:17

触摸屏归纳为输入子系统,这里主要是针对电阻屏,其使用过程如下
当用触摸笔按下时,产生中断。
在中断处理函数处理函数中启动ADC转换x,y坐标。
ADC结束,产生ADC中断
在ADC中断处理函数里上报(input_event)启动定时器
再次启动定时器(可以处理滑动、长按)
松开按键

其驱动程序的写法和之前写输入子系统的写法基本上一致。
写出入口函数,出口函数并加以修饰,加入相关头文件,然后开始完善各函数,在入口函数中分配input_dev结构体,设置(能产生哪类事件,能产生这类事件中的哪些事件),注册设备,硬件相关的操作等。出口函数中主要对之前注册、分配的一些资源进行释放。
还应根据2440数据手册ADC转换和触摸屏那一章,对相关寄存器根据实际需要进行设置。

点击(此处)折叠或打开

  1. #include <linux/errno.h>
  2. #include <linux/kernel.h>
  3. #include <linux/module.h>
  4. #include <linux/slab.h>
  5. #include <linux/input.h>
  6. #include <linux/init.h>
  7. #include <linux/serio.h>
  8. #include <linux/delay.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/clk.h>
  11. #include <asm/io.h>
  12. #include <asm/irq.h>

  13. #include <asm/plat-s3c24xx/ts.h>

  14. #include <asm/arch/regs-adc.h>
  15. #include <asm/arch/regs-gpio.h>

  16. struct s3c_ts_regs {                     /* 相关的寄存器 */
  17.     unsigned long adccon;
  18.     unsigned long adctsc;
  19.     unsigned long adcdly;
  20.     unsigned long adcdat0;
  21.     unsigned long adcdat1;
  22.     unsigned long adcupdn;
  23. };

  24. static struct input_dev *s3c_ts_dev;
  25. static volatile struct s3c_ts_regs *s3c_ts_regs;

  26. static struct timer_list ts_timer;

  27. void enter_wait_pen_down_mode(void) /* 进入等待触摸笔按下模式 */
  28. {
  29.     s3c_ts_regs->adctsc = 0xd3; /* 进入等待中断模式 bit[8]为0 2440手册P442 */
  30. }

  31. void enter_wait_pen_up_mode(void) /* 进入等待触摸笔松开模式 */
  32. {
  33.     s3c_ts_regs->adctsc = 0x1d3; /* 进入等待中断模式 bit[8]为1 2440手册P442 */
  34. }

  35. static void enter_measure_xy_mode(void) /* 进入xy测量模式 */
  36. {
  37.     s3c_ts_regs->adctsc = (1<<3) | (1<<2);
  38. }

  39. static void start_adc(void)
  40. {
  41.     s3c_ts_regs->adccon |= (1<<0); /* 启动ADC */
  42. }

  43. static int s3c_filter_ts(int x[], int y[]) /* 软件过滤 */
  44. {
  45. #define ERR_LIMIT 10      /* 经验值,容差值 */   

  46.     int avr_x, avr_y;
  47.     int det_x, det_y;
  48.     
  49.     avr_x = (x[0] + x[1])/2;
  50.     avr_y = (y[0] + y[1])/2;

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

  53.     if ((det_x > ERR_LIMIT) || (det_y > ERR_LIMIT))
  54.         return 0;
  55.     
  56.     avr_x = (x[1] + x[2])/2;
  57.     avr_y = (y[1] + y[2])/2;

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

  60.     if ((det_x > ERR_LIMIT) || (det_y > ERR_LIMIT))
  61.         return 1;
  62. }

  63. static void s3c_ts_timer_functions(unsigned long data)
  64. {
  65.     if (s3c_ts->adcdat0 & (1<<15)) /* 假设时间到 */
  66.     {
  67.         /* 如果触摸已经松开 */
  68.         input_report_abs(s3c_ts_dev, ABS_PRESSURE, 0); /* 上报事件,压力值为0 */
  69.         input_report_key(s3c_ts_dev, BTN_TOUCH, 0);
  70.         input_sync(s3c_ts_dev); /* 上报完后要同步 */
  71.         enter_wait_pen_down_mode(); /* 进入触摸等待模式 */
  72.     }
  73.     else
  74.     {
  75.         /* 否则测量x,y坐标 */
  76.         enter_measure_xy_mode();
  77.         start_adc();
  78.     }
  79.         
  80. }

  81. static irqreturn_t pen_down_up_irq(int irq, void *dev id)
  82. {
  83.     if (s3c_ts->adcdat0 & (1<<15)) /* 2440手册P447 ADCDAT0寄存器 */
  84.     {
  85.         printk("pen up\n");
  86.         enter_wait_pen_down_mode();
  87.     }
  88.     else
  89.     {
  90.         //printk("pen down\n");
  91.         //enter_wait_pen_up_mode();
  92.         enter_measure_xy_mode();
  93.         start_adc();
  94.     }
  95.     return IRQ_HANDLED;
  96. }

  97. static irqreturn_t adc_irq(int irq, void *dev id)
  98. {
  99.     static int cnt = 0;
  100.     static int x[4], y[4];
  101.     int adcdat0, adcdat1;
  102.     /* 优化措施2
  103.      * 如果ACD完成时,发现触摸笔已松开,则丢弃此次结果
  104.      */
  105.     adcdat0 = s3c_ts_regs->adcdat0;
  106.     adcdat1 = s3c_ts_regs->adcdat1;
  107.     if (s3c_ts->adcdat0 & (1<<15)) /* bit[15]判断是否松开 */
  108.     {
  109.         /* 如果已经松开则丢弃结果 */
  110.         cnt = 0;
  111.         input_report_abs(s3c_ts_dev, ABS_PRESSURE, 0); /* 上报事件,压力值为0 */
  112.         input_report_key(s3c_ts_dev, BTN_TOUCH, 0);
  113.         input_sync(s3c_ts_dev);
  114.         enter_wait_pen_up_mode();
  115.     }
  116.     else
  117.     {
  118.         /* 如果还是按下,则打印结果并进入等待松开模式 */
  119.         //printk("adc_irq cnt = %d,x = %d, y = %d\n", ++cnt, adcdat0 & 0x3ff, adcdat1 & 0x3ff);
  120.         
  121.         /* 优化措施3:
  122.          * 多次测量取平均值
  123.          */
  124.         x[cnt] = adcdat0 & 0x3ff; /* 将测量结果存入静态变量中 */
  125.         y[cnt] = adcdat1 & 0x3ff;
  126.         ++cnt;
  127.         if (cnt == 4)
  128.         {
  129.             /* 优化措施4
  130.              * 软件过滤
  131.              */
  132.              if (s3c_filter_ts(x, y))
  133.             {
  134.                 //printk("x = %d, y = %d\n", (x[0]+x[1]+x[2]+x[3])/4, (y[0]+y[1]+y[2]+y[3])/4);
  135.                 input_report_abs(s3c_ts_dev, ABS_X, (x[0]+x[1]+x[2]+x[3])/4);
  136.                 input_report_abs(s3c_ts_dev, ABS_Y, (y[0]+y[1]+y[2]+y[3])/4);
  137.                 input_report_abs(s3c_ts_dev, ABS_PRESSURE, 1);
  138.                 input_report_key(s3c_ts_dev, BTN_TOUCH, 1);
  139.                 input_sync(s3c_ts_dev);
  140.             }
  141.             cnt = 0; /* cnt计数清0 */
  142.             enter_wait_pen_up_mode(); /* 测量完后要进入等待松开模式,这样才能连续操作 */

  143.             /* 启动定时器处理长按/滑动的情况 */
  144.             mod_timer(&ts_timer, jiffies + HZ/100); /* 1HZ/100 = 10ms */
  145.         }
  146.         else /* 否则再测量一次 */
  147.         {
  148.             enter_measure_xy_mode();
  149.             start_adc();
  150.         }
  151.     }
  152.         return IRQ_HANDLED;
  153. }

  154. static int s3c_ts_init(void)
  155. {
  156.     struct clk* clk;
  157.     /* 1.分配一个input_dev结构体 */
  158.     s3c_ts_dev = input_allocate_device();
  159.     /* 2.设置 */
  160.     /* 2.1 能产生哪类事件 */
  161.     set_bit(EV_KEY, s3c_ts_dev->evbit); /* 能够产生按键事件 */
  162.     set_bit(EV_ABS, s3c_ts_dev->evbit); /* 能够产生绝对位移事件 */
  163.     
  164.     /* 2.2 能产生这类事件里的哪些事件 */
  165.     set_bit(BTN_TOUCH, s3c_ts_dev->evbit); /* 能够产生按键类里面的触摸屏事件 */

  166.     input_set_abs_params(s3c_ts_dev, ABS_X, 0, 0x3FF, 0, 0); /* X方向 0xFF是因为触摸屏ADC是10位 */
  167.     input_set_abs_params(s3c_ts_dev, ABS_Y, 0, 0x3FF, 0, 0); /* Y方向 */
  168.     input_set_abs_params(s3c_ts_dev, ABS_PRESSURE, 0, 1, 0, 0);/* 压力方向 */
  169.     
  170.     /* 3.注册 */
  171.     
  172.     input_register_device(s3c_ts_dev);
  173.     
  174.     /* 4.硬件相关的操作 */
  175.     /* 4.1 使能时钟CLKCON[15] (总开关,一般对不用的设备,时钟一般是关闭的) */
  176.     clk = clk_get(NULL, "adc");
  177.     clk_enable(clk);
  178.     
  179.     /* 4.2 设置S3c2440的ADC/TS寄存器 */
  180.     s3c_ts_regs = ioremap(0x58000000, sizeof(struct s3c_ts_regs));
  181.     /* ADCCON
  182.      * bit[14] : 1 预分频使能
  183.      * bit[13:6] : 预分频系数
  184.      * 49 ,ADCCLK = PCLK/(49+1) = 50MHz/(49+1)=1MHz
  185.      * bit[5:3] : 多路选择
  186.      * bit[2] : 省电模式选择
  187.      * bit[1] : AD启动方式,通过读来启动
  188.      * bit[0] : 启动AD转换,启动后会自动清零
  189.      */
  190.     s3c_ts_regs->adccon = (1<<14) | (49<<6);
  191.     request_irq(IRQ_TC, pen_down_up_irq, IRQF_SAMPLE_RANDOM, "ts_pen", NULL);
  192.     request_irq(IRQ_ADC, adc_irq, IRQF_SAMPLE_RANDOM, "adc", NULL);
  193.     
  194.     /* 优化措施1
  195.      * 设置ADCDLY为最大值,使得电压稳定后再发出中断IRQ_TC
  196.      */
  197.     s3c_ts_regs->adcdly = 0xffff;

  198.     /* 优化措施5
  199.      * 使用定时器,用来解决连按或滑动
  200.      */
  201.     init_timer(&ts_timer);
  202.     ts_timer.function = s3c_ts_timer_function;
  203.     add_timer(&ts_timer);
  204.     
  205.     enter_wait_pen_down_mode();
  206.     return 0;
  207. }

  208. static void s3c_ts_exit(void)
  209. {
  210.     free_irq(IRQ_TC, NULL);
  211.     free_irq(IRQ_ADC, NULL);
  212.     iounmap(s3c_ts_regs);
  213.     input_unregister_device(s3c_ts_dev);
  214.     input_free_device(s3c_ts_dev);
  215.     del_timer(&ts_timer);
  216. }


  217. module_init(s3c_ts_init);
  218. module_exit(s3c_ts_exit);

  219. MODULE_DESCRIPTION("s3c_ts driver for the s3c2440");
  220. MODULE_LICENSE("GPL");

测试方法主要是检测上报事件是否正常,要想更好的测试,需要移植ts_lib这方面的资料网上都可以找到。
以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
cp * -rf /nfsroot    //  /nfsroot可根据实际情况来定


使用:

先安装s3c_ts.ko, lcd.ko     // lcd.ko是之前编译好的LCD驱动,如果后面编译s3c_ts时改过配置,直接装载之前编译好的lcd.ko可能会出现段错误,重新编译一下lcd驱动就可以了。

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

ts_calibrate

ts_test

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