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

全部博文(38)

文章存档

2014年(38)

我的朋友

分类: 嵌入式

2014-05-08 22:07:46

现在来使用ADC来转换转换x轴和y轴坐标,并打印出来
代码如下

点击(此处)折叠或打开

  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 irqreturn_t pen_down_up_irq(int irq, void *dev_id)
  47. {
  48.     if (ts_regs->adcdat0 & (1<<15))//如果寄存器adcdat0的bit-15值为1,说明触摸笔松开
  49.     {    
  50.         printk("pen up\n");
  51.         enter_wait_pen_down_mode();//Detect Stylus Down Interrupt Signal,检查触笔落下中断
  52.     }
  53.     else
  54.     {
  55.         printk("pen down\n");
  56.         //enter_wait_pen_up_mode();//Detect Stylus Up Interrupt Signal,检查触笔松开中断
  57.         enter_measure_xy_mode();/* 进入"自动测量x/y座标模式" */
  58.         start_adc();/* 启动ADC */
  59.     }
  60.     
  61.     ts_regs->adcupdn = 0;//Stylus Up/Down Interrupt history. (After check, this bit should be cleared manually)
  62.     ts_regs->adcclrint = 1;// 产生adc中断后要清除adc中断,给寄存器adcclrint赋任意值都能清除adc中断
  63.     ts_regs->adcclrintpndnup = 1;// 产生up/down中断后要清除up/down中断,给寄存器adcclrintpndnup赋任意值都能清除up/down中断
  64.     
  65.     return IRQ_HANDLED;
  66. }

  67. static irqreturn_t adc_irq(int irq, void *dev_id)
  68. {    
  69.     int x,y;
  70.     int adcdat0,adcdat1;
  71.     
  72.     adcdat0 = ts_regs->adcdat0;
  73.     adcdat1 = ts_regs->adcdat1;

  74.     x = adcdat0 & 0xfff;
  75.     y = adcdat1 & 0xfff;
  76.     
  77.     printk("x = %d, y = %d\n", x, y);
  78.     enter_wait_pen_up_mode();//Detect Stylus Up Interrupt Signal,检查触笔松开中断
  79.     
  80.     ts_regs->adcupdn = 0;//Stylus Up/Down Interrupt history. (After check, this bit should be cleared manually)
  81.     ts_regs->adcclrint = 1 ;// 产生adc中断后要清除adc中断,给寄存器adcclrint赋任意值都能清除adc中断
  82.     ts_regs->adcclrintpndnup = 1;// 产生up/down中断后要清除up/down中断,给寄存器adcclrintpndnup赋任意值都能清除up/down中断
  83.     
  84.     return IRQ_HANDLED;
  85. }

  86. static int ts_init(void)
  87. {
  88.     struct clk *clk;
  89.     
  90.     /* 1. 分配input_dev */
  91.     ts_dev = input_allocate_device();
  92.     
  93.     /* 2. 设置 */
  94.     /* 2.1 能产生哪类事件 */
  95.     set_bit(EV_KEY, ts_dev->evbit);//能产生按键类事件
  96.     set_bit(EV_ABS, ts_dev->evbit);//能产生绝对位移事件
  97.     /* 2.2 能产生这类事件里的哪些事件 */
  98.     set_bit(BTN_TOUCH, ts_dev->keybit);//能产生按键类里面的触摸屏事件
  99.     input_set_abs_params(ts_dev, ABS_X, 0, 0xFFF, 0, 0);
  100.     input_set_abs_params(ts_dev, ABS_Y, 0, 0xFFF, 0, 0);
  101.     input_set_abs_params(ts_dev, ABS_PRESSURE, 0, 1, 0, 0);
  102.     
  103.     /* 3. 注册 */
  104.     if( input_register_device(ts_dev) )
  105.         return -EFAULT;
  106.     
  107.     /* 4.硬件相关操作 */
  108.     //4.1使能时钟
  109.     clk = clk_get(NULL, "adc");
  110.     clk_enable(clk);/* PCLK_GATE[12]设为1 */
  111.     //4.2设置s3c6410的ADC/TS寄存器
  112.     ts_regs = ioremap(0x7E00B000, sizeof(struct ts_regs));
  113.     /* bit[16] : 1 = 12-bit A/D conversion
  114.      * bit[14] : 1-A/D converter prescaler enable
  115.      * bit[13:6]: A/D converter prescaler value,
  116.      * 13, ADCCLK=PCLK/(13+1)=66MHz/(13+1)=4.75MHz,最高支持5Mhz
  117.      * bit[2]: 设为0
  118.      * bit[0]: A/D conversion starts by enable. 先设为0
  119.      */
  120.     ts_regs->adccon =(1<<16) | (1<<14) | (13<<6);
  121.     
  122.     ts_regs->adcclrintpndnup = 1;//Clear Pen Down/Up Interrupt,给寄存器adcclrintpndnup赋任意值都能清除up/down中断
  123.     
  124.     if ( request_irq(IRQ_PENDN, pen_down_up_irq, IRQF_SAMPLE_RANDOM, "ts_pen", NULL) )
  125.         return -EFAULT;
  126.             
  127.     if ( request_irq(IRQ_ADC, adc_irq, IRQF_SAMPLE_RANDOM, "adc", NULL) )
  128.         return -EFAULT;    
  129.     
  130.     /* 进入"wait for interrupt mode", 等待触摸笔按下或松开的模式 */
  131.     enter_wait_pen_down_mode();
  132.     
  133.     return 0;
  134. }

  135. static void ts_exit(void)
  136. {
  137.     free_irq(IRQ_PENDN, NULL);
  138.     free_irq(IRQ_ADC, NULL);
  139.     iounmap(ts_regs);
  140.     input_unregister_device(ts_dev);
  141.     input_free_device(ts_dev);
  142. }

  143. module_init(ts_init);
  144. module_exit(ts_exit);
  145. MODULE_LICENSE("GPL");

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