现在来使用ADC来转换转换x轴和y轴坐标,并打印出来
代码如下
-
#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 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 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)
-
{
-
int x,y;
-
int adcdat0,adcdat1;
-
-
adcdat0 = ts_regs->adcdat0;
-
adcdat1 = ts_regs->adcdat1;
-
-
x = adcdat0 & 0xfff;
-
y = adcdat1 & 0xfff;
-
-
printk("x = %d, y = %d\n", x, y);
-
enter_wait_pen_up_mode();//Detect Stylus Up Interrupt Signal,检查触笔松开中断
-
-
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;
-
-
/* 进入"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);
-
}
-
-
module_init(ts_init);
-
module_exit(ts_exit);
-
MODULE_LICENSE("GPL");
阅读(954) | 评论(0) | 转发(0) |