用定时器来处理长按和滑动
代码如下
-
#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 <mach/hardware.h>
-
-
#include <plat/regs-adc.h>
-
#include <mach/ts.h>
-
#include <mach/irqs.h>
-
-
struct ts_regs{
-
unsigned long adccon;
-
unsigned long adctsc;
-
unsigned long adcdly;
-
unsigned long adcdat0;
-
unsigned long adcdat1;
-
unsigned long adcupdn;
-
unsigned long adcclrint;
-
unsigned long reserverd;
-
unsigned long adcclrintpndnup;
-
};
-
-
static volatile struct ts_regs *ts_regs;
-
-
static struct input_dev *ts_dev;
-
-
static struct timer_list ts_timer;
-
-
static void enter_wait_pen_down_mode(void)//等待中断模式
-
{
-
ts_regs->adctsc = 0xd3;//adctsc寄存器的bit-8为0时,Detect Stylus Down Interrupt Signal
-
}
-
-
static void enter_wait_pen_up_mode(void)//等待中断模式
-
{
-
ts_regs->adctsc = 0x1d3;//adctsc寄存器的bit-8为1时,Detect Stylus Up Interrupt Signal
-
}
-
-
static void enter_measure_xy_mode(void)//自动测量x/y座标模式
-
{
-
ts_regs->adctsc = (1<<3)|(1<<2);
-
}
-
-
static void start_adc(void)//启动ADC
-
{
-
ts_regs->adccon |= (1<<0);
-
}
-
-
static int s3c_filter_ts(int x[], int y[])/* 优化措施4: 软件过滤函数 */
-
{
-
#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 ts_timer_function(unsigned long data)//定时器处理函数
-
{
-
if (ts_regs->adcdat0 & (1<<15))/* 如果已经松开,等待按下 */
-
{
-
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 (ts_regs->adcdat0 & (1<<15))//如果寄存器adcdat0的bit-15值为1,说明触摸笔松开
-
{
-
printk("pen up\n");
-
enter_wait_pen_down_mode();//Detect Stylus Down Interrupt Signal,检查触笔落下中断
-
}
-
else
-
{
-
printk("pen down\n");
-
//enter_wait_pen_up_mode();//Detect Stylus Up Interrupt Signal,检查触笔松开中断
-
enter_measure_xy_mode();/* 进入"自动测量x/y座标模式" */
-
start_adc();/* 启动ADC */
-
}
-
-
ts_regs->adcupdn = 0;//Stylus Up/Down Interrupt history. (After check, this bit should be cleared manually)
-
ts_regs->adcclrint = 1;// 产生adc中断后要清除adc中断,给寄存器adcclrint赋任意值都能清除adc中断
-
ts_regs->adcclrintpndnup = 1;// 产生up/down中断后要清除up/down中断,给寄存器adcclrintpndnup赋任意值都能清除up/down中断
-
-
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;
-
-
adcdat0 = ts_regs->adcdat0;
-
adcdat1 = ts_regs->adcdat1;
-
-
/* 优化措施2: 如果ADC完成时, 发现触摸笔已经松开, 则丢弃此次结果 */
-
if (ts_regs->adcdat0 & (1<<15))//如果触摸笔松开
-
{
-
enter_wait_pen_down_mode();//丢弃结果,等待触摸笔按下
-
}
-
else
-
{
-
/* 优化措施3: 多次测量求平均值 */
-
x[cnt] = adcdat0 & 0xfff;
-
y[cnt] = adcdat1 & 0xfff;
-
++cnt;
-
if (cnt == 4)//测量到四次结果后,打印出平均值
-
{
-
if (s3c_filter_ts(x, y))/* 优化措施4: 软件过滤 */
-
printk("x = %d, y = %d\n", (x[0]+x[1]+x[2]+x[3])/4, (y[0]+y[1]+y[2]+y[3])/4);
-
cnt = 0;
-
enter_wait_pen_up_mode();//Detect Stylus Up Interrupt Signal,检查触笔松开中断
-
-
/* 启动定时器处理长按/滑动的情况 */
-
mod_timer(&ts_timer, jiffies + HZ/100);//HZ/100表示10ms
-
}
-
else//没有测量到四次结果,再次启动ADC继续测量
-
{
-
enter_measure_xy_mode();/* 进入"自动测量x/y座标模式" */
-
start_adc();/* 启动ADC */
-
}
-
-
}
-
-
-
ts_regs->adcupdn = 0;//Stylus Up/Down Interrupt history. (After check, this bit should be cleared manually)
-
ts_regs->adcclrint = 1 ;// 产生adc中断后要清除adc中断,给寄存器adcclrint赋任意值都能清除adc中断
-
ts_regs->adcclrintpndnup = 1;// 产生up/down中断后要清除up/down中断,给寄存器adcclrintpndnup赋任意值都能清除up/down中断
-
-
return IRQ_HANDLED;
-
}
-
-
static int ts_init(void)
-
{
-
struct clk *clk;
-
-
/* 1. 分配input_dev */
-
ts_dev = input_allocate_device();
-
-
/* 2. 设置 */
-
/* 2.1 能产生哪类事件 */
-
set_bit(EV_KEY, ts_dev->evbit);//能产生按键类事件
-
set_bit(EV_ABS, ts_dev->evbit);//能产生绝对位移事件
-
/* 2.2 能产生这类事件里的哪些事件 */
-
set_bit(BTN_TOUCH, ts_dev->keybit);//能产生按键类里面的触摸屏事件
-
input_set_abs_params(ts_dev, ABS_X, 0, 0xFFF, 0, 0);
-
input_set_abs_params(ts_dev, ABS_Y, 0, 0xFFF, 0, 0);
-
input_set_abs_params(ts_dev, ABS_PRESSURE, 0, 1, 0, 0);
-
-
/* 3. 注册 */
-
if( input_register_device(ts_dev) )
-
return -EFAULT;
-
-
/* 4.硬件相关操作 */
-
//4.1使能时钟
-
clk = clk_get(NULL, "adc");
-
clk_enable(clk);/* PCLK_GATE[12]设为1 */
-
//4.2设置s3c6410的ADC/TS寄存器
-
ts_regs = ioremap(0x7E00B000, sizeof(struct ts_regs));
-
/* bit[16] : 1 = 12-bit A/D conversion
-
* bit[14] : 1-A/D converter prescaler enable
-
* bit[13:6]: A/D converter prescaler value,
-
* 13, ADCCLK=PCLK/(13+1)=66MHz/(13+1)=4.75MHz,最高支持5Mhz
-
* bit[2]: 设为0
-
* bit[0]: A/D conversion starts by enable. 先设为0
-
*/
-
ts_regs->adccon =(1<<16) | (1<<14) | (13<<6);
-
-
ts_regs->adcclrintpndnup = 1;//Clear Pen Down/Up Interrupt,给寄存器adcclrintpndnup赋任意值都能清除up/down中断
-
-
if ( request_irq(IRQ_PENDN, pen_down_up_irq, IRQF_SAMPLE_RANDOM, "ts_pen", NULL) )
-
return -EFAULT;
-
-
if ( request_irq(IRQ_ADC, adc_irq, IRQF_SAMPLE_RANDOM, "adc", NULL) )
-
return -EFAULT;
-
-
/* 优化措施1:
-
* 设置ADCDLY为最大值, 这使得电压稳定后再发出IRQ_PENDN中断
-
*/
-
ts_regs->adcdly = 0xffff;
-
-
/* 使用定时器处理长按,滑动的情况 */
-
init_timer(&ts_timer);//初始化定时器
-
ts_timer.function = ts_timer_function;//指定定时器处理函数
-
add_timer(&ts_timer);
-
-
/* 进入"wait for interrupt mode", 等待触摸笔按下或松开的模式 */
-
enter_wait_pen_down_mode();
-
-
return 0;
-
}
-
-
static void ts_exit(void)
-
{
-
free_irq(IRQ_PENDN, NULL);
-
free_irq(IRQ_ADC, NULL);
-
iounmap(ts_regs);
-
input_unregister_device(ts_dev);
-
input_free_device(ts_dev);
-
del_timer(&ts_timer);
-
}
-
-
module_init(ts_init);
-
module_exit(ts_exit);
-
MODULE_LICENSE("GPL");
阅读(919) | 评论(0) | 转发(0) |