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

全部博文(38)

文章存档

2014年(38)

我的朋友

分类: 嵌入式

2014-05-08 22:52:43

加载TS驱动2里面的驱动后,发现同一点的x轴和y轴坐标变化太大,需要优化
主要有以下几种优化方法:
            1.设置ADCDLY寄存器的bit0-15为最大值, 是延时最长,这使得电压稳定后再发出IRQ_PENDN中断
            2.在ADC中断服务函数中,如果ADC完成时, 发现触摸笔已经松开, 则丢弃此次结果 
            3.多次测量求平均值
            4.软件过滤

代码如下

点击(此处)折叠或打开

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  183. static void ts_exit(void)
  184. {
  185.     free_irq(IRQ_PENDN, NULL);
  186.     free_irq(IRQ_ADC, NULL);
  187.     iounmap(ts_regs);
  188.     input_unregister_device(ts_dev);
  189.     input_free_device(ts_dev);
  190. }

  191. module_init(ts_init);
  192. module_exit(ts_exit);
  193. MODULE_LICENSE("GPL");

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