天天看點

基于JZ2440編寫觸摸屏驅動

簡介

JZ2440開發闆配套的是一款4.3英寸電阻觸控屏,電阻觸控屏原理很簡單,就是尖銳物體在某一個點按下,上面觸控薄膜導電,由于這個點舉例起始點暫且規定00點,距離不同,電阻薄膜導電電阻不同,電壓壓降也就不同,原理就是通過判斷XY兩個方向軸與零點之間的電壓差确定觸控位置。

核心觸控驅動

核心自帶觸摸屏驅動,在s3c2440_ts.c這個檔案,實作這個檔案的主體架構就是,利用平台裝置模型,在裝置注冊時又利用了輸入子系統将觸控裝置添加進去。我們自己寫大緻也是這個思路。在輸入子系統中軟體層也就是handler層核心已經為我們寫好,我們隻需要書寫裝置層以及硬體相關代碼即可。基于linux2.6

觸摸屏使用過程

  • 按下觸摸屏,進入中斷
  • 在中斷處理程式中,啟動ADC轉換X、Y坐标
  • ADC轉換結束,産生ADC中斷
  • 在ADC中斷處理函數裡面,上報事件(input_even),啟動定時器
  • 定時器時間到,再次啟動ADC,這樣操作可以處理連續滑動、長按等情況
  • 松開

輸入子系統書寫套路

  • 配置設定inrut_dev
  • 設定
  • 注冊
  • 硬體相關

源碼

#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/input.h>
#include <linux/init.h>
#include <linux/serio.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/clk.h>
#include <asm/io.h>
#include <asm/irq.h>

#include <asm/plat-s3c24xx/ts.h>

#include <asm/arch/regs-adc.h>
#include <asm/arch/regs-gpio.h>

struct s3c_ts_regs {
    unsigned long adccon;
    unsigned long adctsc;
    unsigned long adcdly;
    unsigned long adcdat0;
    unsigned long adcdat1;
    unsigned long adcupdn;
};

static struct input_dev *s3c_ts_dev;
static volatile struct s3c_ts_regs *s3c_ts_regs;

static struct timer_list ts_timer;

static void enter_wait_pen_down_mode(void)
{
    s3c_ts_regs->adctsc = ;
}

static void enter_wait_pen_up_mode(void)
{
    s3c_ts_regs->adctsc = ;
}

static void enter_measure_xy_mode(void)
{
    s3c_ts_regs->adctsc = (<<)|(<<);
}

static void start_adc(void)
{
    s3c_ts_regs->adccon |= (<<);
}

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[] + x[])/;
    avr_y = (y[] + y[])/;

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

    if ((det_x > ERR_LIMIT) || (det_y > ERR_LIMIT))
        return ;

    avr_x = (x[] + x[])/;
    avr_y = (y[] + y[])/;

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

    if ((det_x > ERR_LIMIT) || (det_y > ERR_LIMIT))
        return ;

    return ;
}

static void s3c_ts_timer_function(unsigned long data)
{
    if (s3c_ts_regs->adcdat0 & (<<))
    {
        /* 已經松開 */
        input_report_abs(s3c_ts_dev, ABS_PRESSURE, );
        input_report_key(s3c_ts_dev, BTN_TOUCH, );
        input_sync(s3c_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 & (<<))
    {
        //printk("pen up\n");
        input_report_abs(s3c_ts_dev, ABS_PRESSURE, );
        input_report_key(s3c_ts_dev, BTN_TOUCH, );
        input_sync(s3c_ts_dev);
        enter_wait_pen_down_mode();
    }
    else
    {
        //printk("pen down\n");
        //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 = ;
    static int x[], y[];
    int adcdat0, adcdat1;


    /* 優化措施2: 如果ADC完成時, 發現觸摸筆已經松開, 則丢棄此次結果 */
    adcdat0 = s3c_ts_regs->adcdat0;
    adcdat1 = s3c_ts_regs->adcdat1;

    if (s3c_ts_regs->adcdat0 & (<<))
    {
        /* 已經松開 */
        cnt = ;
        input_report_abs(s3c_ts_dev, ABS_PRESSURE, );
        input_report_key(s3c_ts_dev, BTN_TOUCH, );
        input_sync(s3c_ts_dev);
        enter_wait_pen_down_mode();
    }
    else
    {
        // printk("adc_irq cnt = %d, x = %d, y = %d\n", ++cnt, adcdat0 & 0x3ff, adcdat1 & 0x3ff);
        /* 優化措施3: 多次測量求平均值 */
        x[cnt] = adcdat0 & ;
        y[cnt] = adcdat1 & ;
        ++cnt;
        if (cnt == )
        {
            /* 優化措施4: 軟體過濾 */
            if (s3c_filter_ts(x, y))
            {           
                //printk("x = %d, y = %d\n", (x[0]+x[1]+x[2]+x[3])/4, (y[0]+y[1]+y[2]+y[3])/4);
                input_report_abs(s3c_ts_dev, ABS_X, (x[]+x[]+x[]+x[])/);
                input_report_abs(s3c_ts_dev, ABS_Y, (y[]+y[]+y[]+y[])/);
                input_report_abs(s3c_ts_dev, ABS_PRESSURE, );
                input_report_key(s3c_ts_dev, BTN_TOUCH, );
                input_sync(s3c_ts_dev);
            }
            cnt = ;
            enter_wait_pen_up_mode();

            /* 啟動定時器處理長按/滑動的情況 */
            mod_timer(&ts_timer, jiffies + HZ/);
        }
        else
        {
            enter_measure_xy_mode();
            start_adc();
        }       
    }

    return IRQ_HANDLED;
}

static int s3c_ts_init(void)
{
    struct clk* clk;

    /* 1. 配置設定一個input_dev結構體 */
    s3c_ts_dev = input_allocate_device();

    /* 2. 設定 */
    /* 2.1 能産生哪類事件 */
    set_bit(EV_KEY, s3c_ts_dev->evbit);
    set_bit(EV_ABS, s3c_ts_dev->evbit);

    /* 2.2 能産生這類事件裡的哪些事件 */
    set_bit(BTN_TOUCH, s3c_ts_dev->keybit);

    input_set_abs_params(s3c_ts_dev, ABS_X, , , , );
    input_set_abs_params(s3c_ts_dev, ABS_Y, , , , );
    input_set_abs_params(s3c_ts_dev, ABS_PRESSURE, , , , );


    /* 3. 注冊 */
    input_register_device(s3c_ts_dev);

    /* 4. 硬體相關的操作 */
    /* 4.1 使能時鐘(CLKCON[15]) */
    clk = clk_get(NULL, "adc");
    clk_enable(clk);

    /* 4.2 設定S3C2440的ADC/TS寄存器 */
    s3c_ts_regs = ioremap(, 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 = (<<)|(<<);

    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 = ;

    /* 優化措施5: 使用定時器處理長按,滑動的情況
     * 
     */
    init_timer(&ts_timer);
    ts_timer.function = s3c_ts_timer_function;
    add_timer(&ts_timer);

    enter_wait_pen_down_mode();

    return ;
}

static void s3c_ts_exit(void)
{
    free_irq(IRQ_TC, NULL);
    free_irq(IRQ_ADC, NULL);
    iounmap(s3c_ts_regs);
    input_unregister_device(s3c_ts_dev);
    input_free_device(s3c_ts_dev);
    del_timer(&ts_timer);
}

module_init(s3c_ts_init);
module_exit(s3c_ts_exit);


MODULE_LICENSE("GPL");


           

繼續閱讀