Chinaunix首页 | 论坛 | 博客
  • 博客访问: 97045
  • 博文数量: 38
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 384
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-06 16:52
文章分类

全部博文(38)

文章存档

2014年(38)

我的朋友

分类: 嵌入式

2014-05-08 23:06:09

用定时器来处理长按和滑动
代码如下

点击(此处)折叠或打开

  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 <mach/hardware.h>

  14. #include <plat/regs-adc.h>
  15. #include <mach/ts.h>
  16. #include <mach/irqs.h>

  17. struct ts_regs{
  18.     unsigned long adccon;
  19.     unsigned long adctsc;
  20.     unsigned long adcdly;
  21.     unsigned long adcdat0;
  22.     unsigned long adcdat1;
  23.     unsigned long adcupdn;
  24.     unsigned long adcclrint;
  25.     unsigned long reserverd;
  26.     unsigned long adcclrintpndnup;
  27. };

  28. static volatile struct ts_regs *ts_regs;

  29. static struct input_dev *ts_dev;

  30. static struct timer_list ts_timer;

  31. static void enter_wait_pen_down_mode(void)//等待中断模式
  32. {
  33.     ts_regs->adctsc = 0xd3;//adctsc寄存器的bit-8为0时,Detect Stylus Down Interrupt Signal
  34. }

  35. static void enter_wait_pen_up_mode(void)//等待中断模式
  36. {
  37.     ts_regs->adctsc = 0x1d3;//adctsc寄存器的bit-8为1时,Detect Stylus Up Interrupt Signal
  38. }

  39. static void enter_measure_xy_mode(void)//自动测量x/y座标模式
  40. {
  41.     ts_regs->adctsc = (1<<3)|(1<<2);
  42. }

  43. static void start_adc(void)//启动ADC
  44. {
  45.     ts_regs->adccon |= (1<<0);
  46. }

  47. static int s3c_filter_ts(int x[], int y[])/* 优化措施4: 软件过滤函数 */
  48. {
  49.     #define ERR_LIMIT 10

  50.     int avr_x, avr_y;
  51.     int det_x, det_y;

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

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

  56.     if ((det_x > ERR_LIMIT) || (det_y > ERR_LIMIT))
  57.         return 0;

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

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

  62.     if ((det_x > ERR_LIMIT) || (det_y > ERR_LIMIT))
  63.         return 0;
  64.     
  65.     return 1;
  66. }

  67. static void ts_timer_function(unsigned long data)//定时器处理函数
  68. {
  69.     if (ts_regs->adcdat0 & (1<<15))/* 如果已经松开,等待按下 */
  70.     {
  71.         enter_wait_pen_down_mode();
  72.     }
  73.     else/* 否则重新测量X/Y坐标 */
  74.     {
  75.         enter_measure_xy_mode();
  76.         start_adc();
  77.     }
  78. }

  79. static irqreturn_t pen_down_up_irq(int irq, void *dev_id)
  80. {
  81.     if (ts_regs->adcdat0 & (1<<15))//如果寄存器adcdat0的bit-15值为1,说明触摸笔松开
  82.     {    
  83.         printk("pen up\n");
  84.         enter_wait_pen_down_mode();//Detect Stylus Down Interrupt Signal,检查触笔落下中断
  85.     }
  86.     else
  87.     {
  88.         printk("pen down\n");
  89.         //enter_wait_pen_up_mode();//Detect Stylus Up Interrupt Signal,检查触笔松开中断
  90.         enter_measure_xy_mode();/* 进入"自动测量x/y座标模式" */
  91.         start_adc();/* 启动ADC */
  92.     }
  93.     
  94.     ts_regs->adcupdn = 0;//Stylus Up/Down Interrupt history. (After check, this bit should be cleared manually)
  95.     ts_regs->adcclrint = 1;// 产生adc中断后要清除adc中断,给寄存器adcclrint赋任意值都能清除adc中断
  96.     ts_regs->adcclrintpndnup = 1;// 产生up/down中断后要清除up/down中断,给寄存器adcclrintpndnup赋任意值都能清除up/down中断
  97.     
  98.     return IRQ_HANDLED;
  99. }

  100. static irqreturn_t adc_irq(int irq, void *dev_id)
  101. {    
  102.     static int cnt = 0;
  103.     static int x[4], y[4];
  104.     int adcdat0,adcdat1;
  105.     
  106.     adcdat0 = ts_regs->adcdat0;
  107.     adcdat1 = ts_regs->adcdat1;
  108.     
  109.     /* 优化措施2: 如果ADC完成时, 发现触摸笔已经松开, 则丢弃此次结果 */
  110.     if (ts_regs->adcdat0 & (1<<15))//如果触摸笔松开
  111.     {
  112.         enter_wait_pen_down_mode();//丢弃结果,等待触摸笔按下
  113.     }
  114.     else
  115.     {
  116.         /* 优化措施3: 多次测量求平均值 */
  117.         x[cnt] = adcdat0 & 0xfff;
  118.         y[cnt] = adcdat1 & 0xfff;
  119.         ++cnt;
  120.         if (cnt == 4)//测量到四次结果后,打印出平均值
  121.         {
  122.             if (s3c_filter_ts(x, y))/* 优化措施4: 软件过滤 */    
  123.                 printk("x = %d, y = %d\n", (x[0]+x[1]+x[2]+x[3])/4, (y[0]+y[1]+y[2]+y[3])/4);
  124.             cnt = 0;
  125.             enter_wait_pen_up_mode();//Detect Stylus Up Interrupt Signal,检查触笔松开中断
  126.             
  127.             /* 启动定时器处理长按/滑动的情况 */
  128.             mod_timer(&ts_timer, jiffies + HZ/100);//HZ/100表示10ms
  129.         }
  130.         else//没有测量到四次结果,再次启动ADC继续测量
  131.         {
  132.             enter_measure_xy_mode();/* 进入"自动测量x/y座标模式" */
  133.             start_adc();/* 启动ADC */
  134.         }
  135.         
  136.     }
  137.     
  138.     
  139.     ts_regs->adcupdn = 0;//Stylus Up/Down Interrupt history. (After check, this bit should be cleared manually)
  140.     ts_regs->adcclrint = 1 ;// 产生adc中断后要清除adc中断,给寄存器adcclrint赋任意值都能清除adc中断
  141.     ts_regs->adcclrintpndnup = 1;// 产生up/down中断后要清除up/down中断,给寄存器adcclrintpndnup赋任意值都能清除up/down中断
  142.     
  143.     return IRQ_HANDLED;
  144. }

  145. static int ts_init(void)
  146. {
  147.     struct clk *clk;
  148.     
  149.     /* 1. 分配input_dev */
  150.     ts_dev = input_allocate_device();
  151.     
  152.     /* 2. 设置 */
  153.     /* 2.1 能产生哪类事件 */
  154.     set_bit(EV_KEY, ts_dev->evbit);//能产生按键类事件
  155.     set_bit(EV_ABS, ts_dev->evbit);//能产生绝对位移事件
  156.     /* 2.2 能产生这类事件里的哪些事件 */
  157.     set_bit(BTN_TOUCH, ts_dev->keybit);//能产生按键类里面的触摸屏事件
  158.     input_set_abs_params(ts_dev, ABS_X, 0, 0xFFF, 0, 0);
  159.     input_set_abs_params(ts_dev, ABS_Y, 0, 0xFFF, 0, 0);
  160.     input_set_abs_params(ts_dev, ABS_PRESSURE, 0, 1, 0, 0);
  161.     
  162.     /* 3. 注册 */
  163.     if( input_register_device(ts_dev) )
  164.         return -EFAULT;
  165.     
  166.     /* 4.硬件相关操作 */
  167.     //4.1使能时钟
  168.     clk = clk_get(NULL, "adc");
  169.     clk_enable(clk);/* PCLK_GATE[12]设为1 */
  170.     //4.2设置s3c6410的ADC/TS寄存器
  171.     ts_regs = ioremap(0x7E00B000, sizeof(struct ts_regs));
  172.     /* bit[16] : 1 = 12-bit A/D conversion
  173.      * bit[14] : 1-A/D converter prescaler enable
  174.      * bit[13:6]: A/D converter prescaler value,
  175.      * 13, ADCCLK=PCLK/(13+1)=66MHz/(13+1)=4.75MHz,最高支持5Mhz
  176.      * bit[2]: 设为0
  177.      * bit[0]: A/D conversion starts by enable. 先设为0
  178.      */
  179.     ts_regs->adccon =(1<<16) | (1<<14) | (13<<6);
  180.     
  181.     ts_regs->adcclrintpndnup = 1;//Clear Pen Down/Up Interrupt,给寄存器adcclrintpndnup赋任意值都能清除up/down中断
  182.     
  183.     if ( request_irq(IRQ_PENDN, pen_down_up_irq, IRQF_SAMPLE_RANDOM, "ts_pen", NULL) )
  184.         return -EFAULT;
  185.             
  186.     if ( request_irq(IRQ_ADC, adc_irq, IRQF_SAMPLE_RANDOM, "adc", NULL) )
  187.         return -EFAULT;    
  188.             
  189.     /* 优化措施1:
  190.      * 设置ADCDLY为最大值, 这使得电压稳定后再发出IRQ_PENDN中断
  191.      */
  192.     ts_regs->adcdly = 0xffff;
  193.     
  194.     /* 使用定时器处理长按,滑动的情况 */
  195.     init_timer(&ts_timer);//初始化定时器
  196.     ts_timer.function = ts_timer_function;//指定定时器处理函数
  197.     add_timer(&ts_timer);
  198.     
  199.     /* 进入"wait for interrupt mode", 等待触摸笔按下或松开的模式 */
  200.     enter_wait_pen_down_mode();
  201.     
  202.     return 0;
  203. }

  204. static void ts_exit(void)
  205. {
  206.     free_irq(IRQ_PENDN, NULL);
  207.     free_irq(IRQ_ADC, NULL);
  208.     iounmap(ts_regs);
  209.     input_unregister_device(ts_dev);
  210.     input_free_device(ts_dev);
  211.     del_timer(&ts_timer);
  212. }

  213. module_init(ts_init);
  214. module_exit(ts_exit);
  215. MODULE_LICENSE("GPL");


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