zoukankan      html  css  js  c++  java
  • 触摸屏驱动程序

    学习目的:

    • 使用输入子系统框架,编写触摸屏驱动程序

    触摸屏被按下时,横坐标和纵坐标方向都会产生相应的电压信号,经过ADC采样可以获取电压值。将获取的电压值和屏幕物理尺寸进行转换后就能获取被按下点的坐标位置,可以看出触摸屏也属于是输入子系统的范畴。在前面已经分析过了输入子系统的框架,并使用输入子系统实现了按键驱动程序,现在开始学习使用输入子系统编写触摸屏的驱动程序

    使用输入子系统编写驱动程序主要分为以下4个步骤:

    1)分配input_dev结构体,设置input_dev可以产生的事件类型和产生这类事件类型中的那些事件

    2)注册input_dev结构体

    3)完成硬件相关的配置

    4)有数据可以读取时,上报事件

    下面我们开始按照上面的步骤编写触摸屏的驱动程序

    1、驱动入口函数

    int ts_drv_init(void)
    {
        struct clk* clk;
    
        /* 分配input_dev结构体 */
        ts_dev = input_allocate_device();
    
        /* 设置input_dev可以产生那类事件 */
        set_bit(EV_KEY, ts_dev->evbit);
        set_bit(EV_ABS, ts_dev->evbit);
    
        /* 设置可以产生这类事件里的那些事件 */
        set_bit(BTN_TOUCH, ts_dev->keybit);
    
        input_set_abs_params(ts_dev, ABS_X, 0, 0x3FF, 0, 0);
        input_set_abs_params(ts_dev, ABS_Y, 0, 0x3FF, 0, 0);
        input_set_abs_params(ts_dev, ABS_PRESSURE, 0, 1, 0, 0);
    
        /* register a input_dev struct */
        input_register_device(ts_dev);
    
        /* 4. 硬件相关的操作 */
        /* 4.1 使能时钟(CLKCON[15]) */
        clk = clk_get(NULL, "adc");
        clk_enable(clk);
        
        /* 4.2 设置S3C2440的ADC/TS寄存器 */
        s3c_ts_regs = ioremap(0x58000000, sizeof(struct s3c_ts_regs));
    
        /* bit[14]  : 1-A/D converter prescaler enable
         * bit[13:6]: A/D converter prescaler value,
         *            49, ADCCLK=PCLK/(49+1)=50MHz/(49+1)=1MHz
         * bit[0]: A/D conversion starts by enable. 先设为0
         */
        s3c_ts_regs->adccon = (1<<14)|(49<<6);
    
        request_irq(IRQ_TC, pen_down_up_irq, IRQF_SAMPLE_RANDOM, "ts_pen", NULL);
        request_irq(IRQ_ADC, adc_irq, IRQF_SAMPLE_RANDOM, "adc", NULL);
    
        /* 优化措施1: 
         * 设置ADCDLY为最大值, 这使得电压稳定后再发出IRQ_TC中断
         */
        s3c_ts_regs->adcdly = 0xffff;
    
        /* 优化措施5: 使用定时器处理长按,滑动的情况
         * 
         */
        init_timer(&ts_timer);
        ts_timer.function = s3c_ts_timer_function;
        add_timer(&ts_timer);
    
        enter_wait_pen_down_mode();
    
        return 0;
    }

    驱动入口函数中完成前面所概况的编写输入子系统步骤的前3部分内容,具体工作如下:

    1)首先分配设置并注册了input_dev结构体 。注册的输入子系统设备,可以支持产生按键类事件中的触摸屏事件和绝对位移类事件中的X、Y方向坐标值和压力值事件。也就是说我们的触摸屏驱动可以上报触摸点的x、y方向电压值信息和压力值(压力信息虽然上报,但是无效的

    2)配置了硬件寄存器,注册了中断服务程序,初始化定时器,并让ADC触摸屏控制进入等待按下模式

    s3c2440的ADC触摸屏控制支持4线的电阻触摸屏,它的ADC可以工作在下面的4种模式

    ①  等待中断模式

    ② 分离的x/y轴坐标转换模式

    ③ 自动的x/y轴坐标转换模式

    ④ 普通的转换模式

    在这里,我们的触摸屏驱动使用了①③中的两种模式。正常情况下ADC工作在等待中断模式,当触摸屏被按下,触摸屏控制器发出INT_TC中断信号并进入IRQ_TC中断,在IRQ_TC中断服务程序中启动x/y方向电压值的测量,如果测量完成产生INT_ADC中断,此时可以在IRQ_ADC中断服务程序中读取寄存器中的测量结果

    2、坐标信息上报

    坐标信息的上报主要包含两部分,分别为触摸屏的按下、松开状态以及获取的X、Y方向被按下点的电压值。事件的上报主要在INT_TC和INT_ADC中断的服务程序以及定时器的超时服务函数中完成的

    INT_TC中断服务程序pen_down_up_irq

    static irqreturn_t pen_down_up_irq(int irq, void *dev_id)
    {
        if (s3c_ts_regs->adcdat0 & (1<<15))
        {
            //printk("pen up
    ");
            input_report_abs(ts_dev, ABS_PRESSURE, 0);
            input_report_key(ts_dev, BTN_TOUCH, 0);
            input_sync(ts_dev);
            enter_wait_pen_down_mode();
        }
        else
        {
            //printk("pen down
    ");
            //enter_wait_pen_up_mode();
            enter_measure_xy_mode();
            start_adc();
        }
        return IRQ_HANDLED;
    }

    我们可以设置2440的ADC触摸屏控制寄存器ADCTSC的位[8]为0或1时,表示等待Pen Down中断和Pen Up中断。INT_TC中断服务程序中,当检测到触摸屏被按下时使能x/y方向测量。当检测到触摸屏由按下到松开的过程时,上报压力值大小信息和释放状态

    INT_ADC中断服务程序adc_irq

    static irqreturn_t adc_irq(int irq, void *dev_id)
    {
        static int cnt = 0;
        static int x[4], y[4];
        int adcdat0, adcdat1;
        
        
        /* 优化措施2: 如果ADC完成时, 发现触摸笔已经松开, 则丢弃此次结果 */
        adcdat0 = s3c_ts_regs->adcdat0;
        adcdat1 = s3c_ts_regs->adcdat1;
    
        if (s3c_ts_regs->adcdat0 & (1<<15))------------------>①
        {
            /* 已经松开 */
            cnt = 0;
            input_report_abs(ts_dev, ABS_PRESSURE, 0);
            input_report_key(ts_dev, BTN_TOUCH, 0);
            input_sync(ts_dev);
            enter_wait_pen_down_mode();
        }
        else ------------------------------------------------>②
        {
            // printk("adc_irq cnt = %d, x = %d, y = %d
    ", ++cnt, adcdat0 & 0x3ff, adcdat1 & 0x3ff);
            /* 优化措施3: 多次测量求平均值 */
            x[cnt] = adcdat0 & 0x3ff;
            y[cnt] = adcdat1 & 0x3ff;
            ++cnt;
            if (cnt == 4)
            {
                /* 优化措施4: 软件过滤 */
                if (s3c_filter_ts(x, y))
                {            
                    //printk("x = %d, y = %d
    ", (x[0]+x[1]+x[2]+x[3])/4, (y[0]+y[1]+y[2]+y[3])/4);
                    input_report_abs(ts_dev, ABS_X, (x[0]+x[1]+x[2]+x[3])/4);
                    input_report_abs(ts_dev, ABS_Y, (y[0]+y[1]+y[2]+y[3])/4);
                    input_report_abs(ts_dev, ABS_PRESSURE, 1);
                    input_report_key(ts_dev, BTN_TOUCH, 1);
                    input_sync(ts_dev);
                }
                cnt = 0;
                enter_wait_pen_up_mode();
    
                /* 启动定时器处理长按/滑动的情况 */
                mod_timer(&ts_timer, jiffies + HZ/100); ---------------->③
            }
            else
            {
                enter_measure_xy_mode();
                start_adc();
            }        
        }
        
        return IRQ_HANDLED;
    }

    adc_irq主要是对测量完成后,获取的触摸按下点电压数据的处理,对获取的电压值进行了过滤处理并进行多次测量取平均值

    ① 判断x/y方向测量完成时,此时触摸是否松开,如果松开舍弃当前数据,上报释放状态,进入等待被按下模式

    ② 读取测量结果,存放在数组中,取连续测量4次的平均值。当获取平均值时上报触摸屏被按下点的电压信息、压力值以及是否被按下状态

    ③ 设置定时时间,用于处理长按/滑动的情况

    定时器超时服务函数s3c_ts_timer_function

    static void s3c_ts_timer_function(unsigned long data)
    {
        if (s3c_ts_regs->adcdat0 & (1<<15))
        {
            /* 已经松开 */
            input_report_abs(ts_dev, ABS_PRESSURE, 0);
            input_report_key(ts_dev, BTN_TOUCH, 0);
            input_sync(ts_dev);
            enter_wait_pen_down_mode();
        }
        else
        {
            /* 测量X/Y坐标 */
            enter_measure_xy_mode();
            start_adc();
        }
    }

    定时器超时服务函数是用来处理长按/滑动的情况

    INT_ADC中断服务程序中在获取有效值后,会将ADC触摸控制设置为等待触摸释放模式,然后设置定时器定时时间。当定时器时间到,此时检测到触摸尚未松开就继续进行X/Y坐标电压值测量,如果松开直接上报当前触摸状态,并设置ADC触屏控制进入等待按下模式。这种方式实现了长按和滑行情况的处理

    3、驱动卸载函数

    void ts_drv_exit(void)
    {
        free_irq(IRQ_TC, NULL);
        free_irq(IRQ_ADC, NULL);
        iounmap(s3c_ts_regs);
    
        input_unregister_device(ts_dev);
        input_free_device(ts_dev);
        del_timer(&ts_timer);
    }

    驱动的卸载函数中释放注册的中断、取消ADC寄存器物理地址到虚拟地址的映射、卸载注册的input_dev、释放了动态分配的input_dev结构体内存,删除添加的定时器

    完整驱动程序

    #include <linux/module.h>
    #include <linux/kernel.h>
    #include <linux/fs.h>
    #include <linux/init.h>
    #include <linux/delay.h>
    #include <linux/irq.h>
    #include <asm/uaccess.h>
    #include <asm/irq.h>
    #include <asm/io.h>
    #include <plat/gpio-fns.h>
    #include <mach/gpio-nrs.h>
    #include <linux/interrupt.h>
    #include <linux/wait.h>
    #include <linux/sched.h>
    #include <linux/device.h>
    #include <linux/gpio.h>
    #include <linux/poll.h>
    #include <linux/input.h>
    
    #include <linux/clk.h>
    
    struct s3c_ts_regs {
        unsigned long adccon;
        unsigned long adctsc;
        unsigned long adcdly;
        unsigned long adcdat0;
        unsigned long adcdat1;
        unsigned long adcupdn;
    };
    
    static volatile struct s3c_ts_regs *s3c_ts_regs;
    
    static struct input_dev *ts_dev = NULL; 
    static struct timer_list ts_timer;
    
    static void enter_wait_pen_down_mode(void)
    {
        s3c_ts_regs->adctsc = 0xd3;
    }
    
    static void enter_wait_pen_up_mode(void)
    {
        s3c_ts_regs->adctsc = 0x1d3;
    }
    
    static void enter_measure_xy_mode(void)
    {
        s3c_ts_regs->adctsc = (1<<3)|(1<<2);
    }
    
    static void start_adc(void)
    {
        s3c_ts_regs->adccon |= (1<<0);
    }
    
    static int s3c_filter_ts(int x[], int y[])
    {
    #define ERR_LIMIT 10
    
        int avr_x, avr_y;
        int det_x, det_y;
    
        avr_x = (x[0] + x[1])/2;
        avr_y = (y[0] + y[1])/2;
    
        det_x = (x[2] > avr_x) ? (x[2] - avr_x) : (avr_x - x[2]);
        det_y = (y[2] > avr_y) ? (y[2] - avr_y) : (avr_y - y[2]);
    
        if ((det_x > ERR_LIMIT) || (det_y > ERR_LIMIT))
            return 0;
    
        avr_x = (x[1] + x[2])/2;
        avr_y = (y[1] + y[2])/2;
    
        det_x = (x[3] > avr_x) ? (x[3] - avr_x) : (avr_x - x[3]);
        det_y = (y[3] > avr_y) ? (y[3] - avr_y) : (avr_y - y[3]);
    
        if ((det_x > ERR_LIMIT) || (det_y > ERR_LIMIT))
            return 0;
        
        return 1;
    }
    
    static void s3c_ts_timer_function(unsigned long data)
    {
        if (s3c_ts_regs->adcdat0 & (1<<15))
        {
            /* 已经松开 */
            input_report_abs(ts_dev, ABS_PRESSURE, 0);
            input_report_key(ts_dev, BTN_TOUCH, 0);
            input_sync(ts_dev);
            enter_wait_pen_down_mode();
        }
        else
        {
            /* 测量X/Y坐标 */
            enter_measure_xy_mode();
            start_adc();
        }
    }
    
    
    static irqreturn_t pen_down_up_irq(int irq, void *dev_id)
    {
        if (s3c_ts_regs->adcdat0 & (1<<15))
        {
            //printk("pen up
    ");
            input_report_abs(ts_dev, ABS_PRESSURE, 0);
            input_report_key(ts_dev, BTN_TOUCH, 0);
            input_sync(ts_dev);
            enter_wait_pen_down_mode();
        }
        else
        {
            //printk("pen down
    ");
            //enter_wait_pen_up_mode();
            enter_measure_xy_mode();
            start_adc();
        }
        return IRQ_HANDLED;
    }
    
    static irqreturn_t adc_irq(int irq, void *dev_id)
    {
        static int cnt = 0;
        static int x[4], y[4];
        int adcdat0, adcdat1;
        
        
        /* 优化措施2: 如果ADC完成时, 发现触摸笔已经松开, 则丢弃此次结果 */
        adcdat0 = s3c_ts_regs->adcdat0;
        adcdat1 = s3c_ts_regs->adcdat1;
    
        if (s3c_ts_regs->adcdat0 & (1<<15))
        {
            /* 已经松开 */
            cnt = 0;
            input_report_abs(ts_dev, ABS_PRESSURE, 0);
            input_report_key(ts_dev, BTN_TOUCH, 0);
            input_sync(ts_dev);
            enter_wait_pen_down_mode();
        }
        else
        {
            // printk("adc_irq cnt = %d, x = %d, y = %d
    ", ++cnt, adcdat0 & 0x3ff, adcdat1 & 0x3ff);
            /* 优化措施3: 多次测量求平均值 */
            x[cnt] = adcdat0 & 0x3ff;
            y[cnt] = adcdat1 & 0x3ff;
            ++cnt;
            if (cnt == 4)
            {
                /* 优化措施4: 软件过滤 */
                if (s3c_filter_ts(x, y))
                {            
                    //printk("x = %d, y = %d
    ", (x[0]+x[1]+x[2]+x[3])/4, (y[0]+y[1]+y[2]+y[3])/4);
                    input_report_abs(ts_dev, ABS_X, (x[0]+x[1]+x[2]+x[3])/4);
                    input_report_abs(ts_dev, ABS_Y, (y[0]+y[1]+y[2]+y[3])/4);
                    input_report_abs(ts_dev, ABS_PRESSURE, 1);
                    input_report_key(ts_dev, BTN_TOUCH, 1);
                    input_sync(ts_dev);
                }
                cnt = 0;
                enter_wait_pen_up_mode();
    
                /* 启动定时器处理长按/滑动的情况 */
                mod_timer(&ts_timer, jiffies + HZ/100);
            }
            else
            {
                enter_measure_xy_mode();
                start_adc();
            }        
        }
        
        return IRQ_HANDLED;
    }
    
    
    
    int ts_drv_init(void)
    {
        struct clk* clk;
    
        /* 分配input_dev结构体 */
        ts_dev = input_allocate_device();
    
        /* 设置input_dev可以产生那类事件 */
        set_bit(EV_KEY, ts_dev->evbit);
        set_bit(EV_ABS, ts_dev->evbit);
    
        /* 设置可以产生这类事件里的那些事件 */
        set_bit(BTN_TOUCH, ts_dev->keybit);
    
        input_set_abs_params(ts_dev, ABS_X, 0, 0x3FF, 0, 0);
        input_set_abs_params(ts_dev, ABS_Y, 0, 0x3FF, 0, 0);
        input_set_abs_params(ts_dev, ABS_PRESSURE, 0, 1, 0, 0);
    
        /* register a input_dev struct */
        input_register_device(ts_dev);
    
        /* 4. 硬件相关的操作 */
        /* 4.1 使能时钟(CLKCON[15]) */
        clk = clk_get(NULL, "adc");
        clk_enable(clk);
        
        /* 4.2 设置S3C2440的ADC/TS寄存器 */
        s3c_ts_regs = ioremap(0x58000000, sizeof(struct s3c_ts_regs));
    
        /* bit[14]  : 1-A/D converter prescaler enable
         * bit[13:6]: A/D converter prescaler value,
         *            49, ADCCLK=PCLK/(49+1)=50MHz/(49+1)=1MHz
         * bit[0]: A/D conversion starts by enable. 先设为0
         */
        s3c_ts_regs->adccon = (1<<14)|(49<<6);
    
        request_irq(IRQ_TC, pen_down_up_irq, IRQF_SAMPLE_RANDOM, "ts_pen", NULL);
        request_irq(IRQ_ADC, adc_irq, IRQF_SAMPLE_RANDOM, "adc", NULL);
    
        /* 优化措施1: 
         * 设置ADCDLY为最大值, 这使得电压稳定后再发出IRQ_TC中断
         */
        s3c_ts_regs->adcdly = 0xffff;
    
        /* 优化措施5: 使用定时器处理长按,滑动的情况
         * 
         */
        init_timer(&ts_timer);
        ts_timer.function = s3c_ts_timer_function;
        add_timer(&ts_timer);
    
        enter_wait_pen_down_mode();
    
        return 0;
    }
    
    void ts_drv_exit(void)
    {
        free_irq(IRQ_TC, NULL);
        free_irq(IRQ_ADC, NULL);
        iounmap(s3c_ts_regs);
    
        input_unregister_device(ts_dev);
        input_free_device(ts_dev);
        del_timer(&ts_timer);
    }
    
    module_init(ts_drv_init);
    module_exit(ts_drv_exit);
    
    MODULE_LICENSE("GPL");
    ts_drv.c
  • 相关阅读:
    Q15格式表示负小数
    音频算法处理笔试面试题
    有符号和无符号之间的转化
    PE5 Smallest multiple
    PE3 Largest prime factor(最大素数因子)
    PE2 Even Fibonacci numbers(最大菲波那列偶数)
    PE 4 Largest palindrome product(最大回文)
    PE1 Multiples of 3 and 5
    Codevs高精度入门(减法、加法和乘法)解题报告
    计算机网络学习笔记(二) 计算机网络结构
  • 原文地址:https://www.cnblogs.com/053179hu/p/13849927.html
Copyright © 2011-2022 走看看