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

全部博文(38)

文章存档

2014年(38)

我的朋友

分类: 嵌入式

2014-05-08 23:35:43

上报事件,是系统能够调用此设备
把原来的打印改成上报事件就可以了
代码如下

点击(此处)折叠或打开

  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 70

  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.         input_report_abs(ts_dev, ABS_PRESSURE, 0);//1表示按下,0表示松开,其实不添加以下三行也没关系
  72.         input_report_key(ts_dev, BTN_TOUCH, 0);//1表示按下,0表示松开
  73.         input_sync(ts_dev);
  74.         enter_wait_pen_down_mode();
  75.     }
  76.     else/* 否则重新测量X/Y坐标 */
  77.     {
  78.         enter_measure_xy_mode();
  79.         start_adc();
  80.     }
  81. }

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

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

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

  220. static void ts_exit(void)
  221. {
  222.     free_irq(IRQ_PENDN, NULL);
  223.     free_irq(IRQ_ADC, NULL);
  224.     iounmap(ts_regs);
  225.     input_unregister_device(ts_dev);
  226.     input_free_device(ts_dev);
  227.     del_timer(&ts_timer);
  228. }

  229. module_init(ts_init);
  230. module_exit(ts_exit);
  231. MODULE_LICENSE("GPL");

测试方法:加载驱动后,可以用原来移植内核是用的tslib测试
阅读(938) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~