第二期驅動篇——4.2 觸摸屏驅動—完善程序與使用tslib

觸摸屏驅動—完善程序與使用tslib

  • 硬件平臺:韋東山嵌入式Linxu開發板(S3C2440.v3)
  • 軟件平臺:運行於VMware Workstation 12 Player下UbuntuLTS16.04_x64 系統
  • 參考資料:《嵌入式Linux應用開發手冊》、《嵌入式Linux應用開發手冊第2版》
  • 開發環境:Linux 2.6.22.6 內核、arm-linux-gcc-3.4.5-glibc-2.3.6工具鏈


一、前言

在上一篇博文【4.1 觸摸屏驅動—基礎功能實現】中,我們實現了觸摸屏驅動的基礎功能——按下與鬆開檢測,在這篇博文中,將繼續完善整個程序。

二、完善與改進

程序大致執行流程如下:
1、程序各種初始化完畢。
2、當發生觸摸屏中斷
   2.1 如果此時觸摸筆鬆開,則上報事件,進入等待按下模式
   2.2 如果此時觸摸筆按下,則進入自動測量模式,開啓ADC中斷
3、當發生ADC中斷
   3.1 如果此時檢測到觸摸筆鬆開,則丟棄轉換的值,上報事件,進入等待按下模式
   3.2 如果此時檢測到觸摸筆依然是按下,則採用多次求和的均值、軟件過濾、定時器開啓處理長按的措施進行優化,並上報事件
4、當發生定時器中斷
   4.1 如果在定時器中斷時,檢測到此時還在進行ADC轉換,則直接退出定時器中斷
   4.2 如果檢測到觸摸筆鬆開,則上報事件,進入等地啊按下模式
   4.3 如果檢測到觸摸筆依舊按下,則再次開啓ADC,進入3步驟,進行長按或滑動處理

1、 改善觸摸屏中斷函數

static irqreturn_t pen_down_up_irq(int irq, void *dev_id)
{
	if (s3c_ts_regs->adcdat0 & (1<<15)) {
		input_report_abs(s3c_ts_inputdev, ABS_PRESSURE, 0);		//上報絕對位移事件
		input_report_key(s3c_ts_inputdev, BTN_TOUCH, 0);		//上報按鍵事件
		input_sync(s3c_ts_inputdev);	//上報同步事件
		enter_wait_pen_down_mode();		//等待按下模式
	} else {
		enter_measure_xy_mode();		//開啓自動測量模式
		start_adc();	//啓動ADC
	}	
	return IRQ_HANDLED;
}

2、添加ADC中斷函數

在這裏採用了3個優化措施:

  • 查看ADC轉換完成時,觸摸筆是否已鬆開
  • 多次求和得均值
  • 軟件過濾數數據

2.1 ADC中斷處理函數

/* ADC中斷 */
static irqreturn_t adc_irq(int irq, void *dev_id)
{
	static int cnt = 0;
	int adcdat0, adcdat1;
	static int x[4], y[4];

	adcdat0 = s3c_ts_regs->adcdat0;
	adcdat1 = s3c_ts_regs->adcdat1;

	/* 優化措施2:如果在ADC轉換完成時,觸摸筆已鬆開,則丟棄值 */
	if (s3c_ts_regs->adcdat0 & (1<<15)) {
		/* 鬆開 */
		input_report_abs(s3c_ts_inputdev, ABS_PRESSURE, 0);
		input_report_key(s3c_ts_inputdev, BTN_TOUCH, 0);
		input_sync(s3c_ts_inputdev);
		enter_wait_pen_down_mode();
		cnt = 0;
	} else {
		/* 優化措施3:多次讀取求均值 */
		x[cnt] = adcdat0 & 0x3ff;
		y[cnt] = adcdat1 & 0x3ff;
		++cnt;
		if (cnt == 4) {
			/* 優化措施4: 軟件過濾 */
			if (s3c_filter_ts(x, y, cnt))	{
				input_report_abs(s3c_ts_inputdev, ABS_X, (x[0]+x[1]+x[2]+x[3])/4);
				input_report_abs(s3c_ts_inputdev, ABS_Y, (y[0]+y[1]+y[2]+y[3])/4);
				input_report_abs(s3c_ts_inputdev, ABS_PRESSURE, 1);
				input_report_key(s3c_ts_inputdev, BTN_TOUCH, 1);
				input_sync(s3c_ts_inputdev);
			}	
			cnt = 0;
			enter_wait_pen_up_mode();

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

	return IRQ_HANDLED;
}

2.2 軟件過濾數據函數

假設數組中的有4個數據:a[0]、a[1]、a[2]、a[3]
  比較a[0]+a[1]得到的平均值與a[2]做減法,若誤差太大,則拋棄整組數據
  若在誤差範圍之內,則比較a[1]+a[2]得到的平均值與a[3]做減法,重複上述比較步驟
  直到比較的右對象(上面第一次的a[2]與第二次a[3]),數組的最後一個數停止。
/* 軟件過濾函數(比較粗糙) */
static int s3c_filter_ts(int x[], int y[], int len)
{
#define ERR_LIMIT 10
	int cnt;
	int avr_x, avr_y;
	int det_x, det_y;
	
	for (cnt = 0; cnt < len-2; cnt++) {
		/* 求當前cnt與cnt+1爲索引數組的值的均值 */
		avr_x = (x[cnt] + x[cnt + 1])/2;
		avr_y = (y[cnt] + y[cnt + 1])/2;

		/* 比較誤差 */
		det_x = (x[cnt + 2] > avr_x) ? (x[cnt + 2] - avr_x) : (avr_x - x[cnt + 2]);
		det_y = (y[cnt + 2] > avr_y) ? (y[cnt + 2] - avr_y) : (avr_y - y[cnt + 2]);

		if ((det_x > ERR_LIMIT) || (det_y > ERR_LIMIT))
			return 0;
	}
	
	return 1;
}

3、添加定時器中斷函數

3.1 定義定時器相關結構體

static struct timer_list s3c_ts_timer;

3.2 在入口函數中設置定時器中斷

添加如下代碼

init_timer(&s3c_ts_timer);
	s3c_ts_timer.function = s3c_ts_timer_function;
	add_timer(&s3c_ts_timer);

3.3 編寫定時器中斷函數

static void s3c_ts_timer_function (unsigned long data)
{
	/* 如果此時正在進行ADC轉換,則不處理 */
	if (is_in_auto_mode() == 1)
			return ;

	if (s3c_ts_regs->adcdat0 & (1<<15)) {
		/* 已經鬆開 */
		input_report_abs(s3c_ts_inputdev, ABS_PRESSURE, 0);
		input_report_key(s3c_ts_inputdev, BTN_TOUCH, 0);
		input_sync(s3c_ts_inputdev);
		enter_wait_pen_down_mode();
	}
	else {
		/* 測量X/Y座標 */
		enter_measure_xy_mode();
		start_adc();
	}
}

3.4 在ADC中斷中開啓定時器

設置定時器定時數值爲10ms

mod_timer(&s3c_ts_timer, jiffies + HZ/100);		//HZ是1S,當前爲10ms

4、完整的驅動程序


#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_inputdev;
static struct timer_list s3c_ts_timer;
static volatile struct s3c_ts_regs *s3c_ts_regs;

/* 等待按鍵按下模式 */
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;
}

/* ADC自動測量模式 */
static void enter_measure_xy_mode(void)
{
	s3c_ts_regs->adctsc = (1<<3)|(1<<2);
}

/* 啓動ADC */
static void start_adc(void)
{
	s3c_ts_regs->adccon |= (1<<0);
}

/* ADC是否轉換 */
int is_in_auto_mode(void)
{
	return s3c_ts_regs->adctsc & (1 << 2);
}

/* 軟件過濾函數(比較粗糙) */
static int s3c_filter_ts(int x[], int y[], int len)
{
	int cnt;
	
#define ERR_LIMIT 10

	int avr_x, avr_y;
	int det_x, det_y;

	for (cnt = 0; cnt < len-2; cnt++) {
		/* 求當前cnt與cnt+1爲索引數組的值的均值 */
		avr_x = (x[cnt] + x[cnt + 1])/2;
		avr_y = (y[cnt] + y[cnt + 1])/2;

		/* 比較誤差 */
		det_x = (x[cnt + 2] > avr_x) ? (x[cnt + 2] - avr_x) : (avr_x - x[cnt + 2]);
		det_y = (y[cnt + 2] > avr_y) ? (y[cnt + 2] - avr_y) : (avr_y - y[cnt + 2]);

		if ((det_x > ERR_LIMIT) || (det_y > ERR_LIMIT))
			return 0;
	}
	
	return 1;
}

/* 觸摸屏中斷函數 */
static irqreturn_t pen_down_up_irq(int irq, void *dev_id)
{
	if (s3c_ts_regs->adcdat0 & (1<<15)) {
		input_report_abs(s3c_ts_inputdev, ABS_PRESSURE, 0);	//上報絕對位移事件
		input_report_key(s3c_ts_inputdev, BTN_TOUCH, 0);		//上報按鍵事件
		input_sync(s3c_ts_inputdev);	//上報同步事件
		enter_wait_pen_down_mode();		//等待按下模式
	} else {
		enter_measure_xy_mode();		//開啓自動測量模式
		start_adc();	//啓動ADC
	}
	
	return IRQ_HANDLED;
}

/* ADC中斷 */
static irqreturn_t adc_irq(int irq, void *dev_id)
{
	static int cnt = 0;
	int adcdat0, adcdat1;
	static int x[4], y[4];

	adcdat0 = s3c_ts_regs->adcdat0;
	adcdat1 = s3c_ts_regs->adcdat1;

	/* 優化措施2:如果在ADC轉換完成時,觸摸筆已鬆開,則丟棄值 */
	if (s3c_ts_regs->adcdat0 & (1<<15)) {
		/* 鬆開 */
		input_report_abs(s3c_ts_inputdev, ABS_PRESSURE, 0);
		input_report_key(s3c_ts_inputdev, BTN_TOUCH, 0);
		input_sync(s3c_ts_inputdev);
		enter_wait_pen_down_mode();
		cnt = 0;
	} else {
		/* 優化措施3:多次讀取求均值 */
		x[cnt] = adcdat0 & 0x3ff;
		y[cnt] = adcdat1 & 0x3ff;
		++cnt;
		if (cnt == 4) {
			/* 優化措施4: 軟件過濾 */
			if (s3c_filter_ts(x, y, cnt))	{
				input_report_abs(s3c_ts_inputdev, ABS_X, (x[0]+x[1]+x[2]+x[3])/4);
				input_report_abs(s3c_ts_inputdev, ABS_Y, (y[0]+y[1]+y[2]+y[3])/4);
				input_report_abs(s3c_ts_inputdev, ABS_PRESSURE, 1);
				input_report_key(s3c_ts_inputdev, BTN_TOUCH, 1);
				input_sync(s3c_ts_inputdev);
			}	
			cnt = 0;
			enter_wait_pen_up_mode();

			/* 啓動定時器處理長按/滑動的情況 */
			mod_timer(&s3c_ts_timer, jiffies + HZ/100);	//;//HZ是1S,當前爲10ms
		} else {
			enter_measure_xy_mode();
			start_adc();
		}
	}

	return IRQ_HANDLED;
}

/* 定時器中斷函數 */
static void s3c_ts_timer_function (unsigned long data)
{
	/* 如果此時正在進行ADC轉換,則不處理 */
	if (is_in_auto_mode() == 1)
			return ;

	if (s3c_ts_regs->adcdat0 & (1<<15)) {
		/* 已經鬆開 */
		input_report_abs(s3c_ts_inputdev, ABS_PRESSURE, 0);
		input_report_key(s3c_ts_inputdev, BTN_TOUCH, 0);
		input_sync(s3c_ts_inputdev);
		enter_wait_pen_down_mode();
	}
	else {
		/* 測量X/Y座標 */
		enter_measure_xy_mode();
		start_adc();
	}
}

/* 入口函數 */
static int s3c_ts_init(void)
{
	int error = 0;
	struct clk	*adc_clock;
	
	/* 1、分配input_dev  結構體 */
	s3c_ts_inputdev = input_allocate_device();
	if (!s3c_ts_inputdev)
		return -ENOMEM;

	/* 2、設置 */
	/* 2.1 能產生哪類事件 */
	set_bit(EV_KEY, s3c_ts_inputdev->evbit);			//按鍵類
	set_bit(EV_ABS, s3c_ts_inputdev->evbit);			//絕對位移事件類
	
	/* 2.2 能產生這類事件裏的哪些事件 */
	set_bit(BTN_TOUCH, s3c_ts_inputdev->keybit);

	/* 表示支持絕對值x座標,並設置它在座標系中的最小值和最大值,以及干擾值和平焊位置 */
	input_set_abs_params(s3c_ts_inputdev, ABS_X, 0, 0x3FF, 0, 0);
	input_set_abs_params(s3c_ts_inputdev, ABS_Y, 0, 0x3FF, 0, 0);
	input_set_abs_params(s3c_ts_inputdev, ABS_PRESSURE, 0, 1, 0, 0);
	
	/* 3、註冊 */
	error = input_register_device(s3c_ts_inputdev);
	if (error) {
		printk(KERN_ERR "Unable to register touch screen input device\n");
		goto fail;
	}

	/* 4、硬件相關操作 */
	/* 4.1 使能時鐘(CLKCON[15]):s3c2440爲片上系統,有很多的模塊,在上電時就會把不用的關上 */
	adc_clock = clk_get(NULL, "adc");
	if (!adc_clock) {
		printk(KERN_ERR "failed to get adc clock source\n");
		input_unregister_device(s3c_ts_inputdev);
		error = -ENOENT;
		goto fail;
	}
	clk_enable(adc_clock);

	/* 4.2 設置與ADC/TOUCH相關寄存器 */
	s3c_ts_regs = ioremap(0x58000000, sizeof(struct s3c_ts_regs));
	if (s3c_ts_regs == NULL) {
		printk(KERN_ERR "Failed to remap register 0x58000000\n");
		input_unregister_device(s3c_ts_inputdev);
		error = -ENOMEM;
		goto fail;
	}

	/* adccon
	 * 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)  | (0 << 0));

	/* 4.3 註冊中斷 */
	/* 註冊觸摸屏中斷 */
	error = request_irq(IRQ_TC, pen_down_up_irq, IRQF_SAMPLE_RANDOM, "ts_pen", NULL);
	if (error != 0) {
		printk(KERN_ERR "s3c2410_ts.c: Could not allocate ts IRQ_TC !\n");
		iounmap(s3c_ts_regs);		//取消映射
		input_unregister_device(s3c_ts_inputdev);		//註銷設備
		goto fail;
	}

	/* 註冊ADC中斷 */
	error = request_irq(IRQ_ADC, adc_irq, IRQF_SAMPLE_RANDOM, "adc", NULL);
	if (error != 0) {
		printk(KERN_ERR "s3c2410_ts.c: Could not allocate ts IRQ_ADC !\n");
		free_irq(IRQ_TC, NULL);		//釋放中斷	
		iounmap(s3c_ts_regs);		//取消映射
		input_unregister_device(s3c_ts_inputdev);		//註銷設備
		goto fail;
	}

	/* 優化措施1:設置ADCDLY爲最大值,目的:等待電壓穩定 */
	s3c_ts_regs->adcdly = 0xffff;

	/* 優化措施5:開啓定時器支持長按 */
	init_timer(&s3c_ts_timer);
	s3c_ts_timer.function = s3c_ts_timer_function;
	add_timer(&s3c_ts_timer);
	
	/* 5、先進入等待按下模式 */
	enter_wait_pen_down_mode();
	
fail:
	input_free_device(s3c_ts_inputdev);				//釋放input_dev結構體
	
	return error;
}

/* 出口函數 */
static void s3c_ts_exit(void)
{
	free_irq(IRQ_TC, NULL);							//釋放中斷
	iounmap(s3c_ts_regs);							//取消映射
	input_unregister_device(s3c_ts_inputdev);		//註銷設備
	input_free_device(s3c_ts_inputdev);				//釋放input_dev結構體
	del_timer(&s3c_ts_timer);						//釋放定時器中斷
}

/* 修飾 */
module_init(s3c_ts_init);
module_exit(s3c_ts_exit);

/* 協議 */
MODULE_LICENSE("GPL");

三、編譯與測試

1、在沒有加載驅動

在這裏插入圖片描述

2、加載驅動後:

在這裏插入圖片描述

3、使用hexdump /dev/event0進行調試

在這裏插入圖片描述

  • Code: 0000 表示X方向,後面的值就是X對應的ADC座標
  • Code: 0001 表示Y方向,後面的值就是Y對應的ADC座標
  • Code: 0018 表示壓力值,由於屏幕不是壓感屏,所以壓力值只有0和1
  • Code: 014a 表示哪個按鍵,後面的值就是按鍵的值、

4、使用tslib庫

如裸板程序一樣,在觸摸屏上的位置點擊,則在LCD中會得到相應的顯示,(此時的lcd驅動採用之前寫的lcd.ko

使用tslib庫的方法:

  1. 安裝需要的軟件
    sudo apt-get install autoconf
    sudo apt-get install automake
    sudo apt-get install libtool

  2. 解壓tslib壓縮包並編譯
    tar xzf tslib-1.4.tar.gz
    cd tslib
    ./autogen.sh

  3. 安裝
    mkdir tmp
    echo "ac_cv_func_malloc_0_nonnull=yes" >arm-linux.cache
    ./configure --host=arm-linux --cache-file=arm-linux.cache --prefix=$(pwd)/tmp
    make
    make install

  4. tmp目錄下的文件夾與文件複製到根文件系統的根文件目錄下
    cd tmp

    cp * -rfd /

  5. 在根文件系統上修改 /etc/ts.conf第1行(去掉#號和第一個空格)
    在這裏插入圖片描述

5、加載驅動

  1. 先安裝s3c_ts.ko, 後安裝lcd.ko
  2. 設置環境變量
    export TSLIB_TSDEVICE=/dev/event0 //這是指觸摸屏設備
    export TSLIB_CALIBFILE=/etc/pointercal //校驗文件放在這裏
    export TSLIB_CONFFILE=/etc/ts.conf //配置文件放在這裏
    export TSLIB_PLUGINDIR=/lib/ts //插件放在這裏
    export TSLIB_CONSOLEDEVICE=none
    export TSLIB_FBDEVICE=/dev/fb0 //顯示屏
  3. 運行tslib的程序
    在tslib編譯出來的文件中的bin目錄下有如下執行程序
    在這裏插入圖片描述
  • ts_calibrate爲校準程序,執行後開發版顯示如下:在這裏插入圖片描述
    校準後輸出一下信息:
    在這裏插入圖片描述
  • ts_test爲測試程序,執行後開發版顯示與串口輸出如下:在這裏插入圖片描述
    在這裏插入圖片描述
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章