?? touch.c
字號:
#include <linux/config.h>#include <linux/utsname.h>#include <linux/kernel.h>#include <linux/major.h>#include <linux/string.h>#include <linux/fcntl.h>#include <linux/timer.h>#include <linux/sched.h>#include <linux/tty.h>#include <linux/module.h>#include <linux/init.h>#include <linux/miscdevice.h>#include <linux/delay.h>#include <linux/spinlock.h>#include <asm/mach/irq.h>#include <asm/hardware.h>#include <asm/io.h>#include <asm/irq.h>#include <asm/system.h>#include <linux/poll.h>#include <asm/arch/io.h>#include "44b.h"#define TOUCH_MSR_Y 0x9c //讀Y軸坐標命令#define TOUCH_MSR_X 0xdc //讀X軸坐標命令#define DEVICE_NAME "touch"#define MCLK 64000000static int delayLoopCount=400;void Delay(int);int ReadTouch(unsigned char);void Test_Touch(void);typedef struct { int Pressed; int x; int y;}TOUCHSTATE;static TOUCHSTATE _State;int ReadTouch(unsigned char command){ unsigned char temp,i,ack; ack=0; rPDATC &=0xbeff; temp=0x80; for(i=0;i<8;i++) { if(command&temp) rPDATC |=0x0200; else rPDATC &=0xfdff; rPDATC |=0x4000; Delay(3); rPDATC &=0xbfff; Delay(3); temp=temp>>1; } temp=(unsigned char)((rPDATC&0x0400)>>8); while(temp==0) temp=(unsigned char)((rPDATC&0x0400)>>8); rPDATC &=0xf7ff; rPDATC |=0x4000; Delay(3); rPDATC &=0xbfff; Delay(3); for(i=0;i<7;i++) { rPDATC |=0x4000;//rPDATG |=0x08; temp=(unsigned char)((rPDATC&0x0800)>>8); if(temp) ack++; ack=ack<<1; Delay(3); rPDATC &=0xbfff; Delay(3); } rPDATC |=0x4000; temp=(unsigned char)((rPDATC&0x0800)>>8); if(temp) ack++; Delay(3); rPDATC &=0xbfff; rPDATC |=0x0100; return ack;}void Test_Touch(void){ _State.x=0; _State.y=0; rPCONC =0x1f05ff55; rPUPC =0x00ff; rPCONG =0x00ff; rPUPG =0x80; if(((rPDATG&0x80)==0)&&(_State.Pressed==0)) //有點擊 { _State.Pressed=1; //設置點擊狀態存在 _State.x=ReadTouch(TOUCH_MSR_X); //讀取橫坐標 _State.y=ReadTouch(TOUCH_MSR_Y); //讀取縱坐標 // printk("Touch at the point ( %4d, %4d )\n",_State.x,_State.y); } else if(((rPDATG&0x80)!=0)&&(_State.Pressed==1)) //無點擊 _State.Pressed=0; //取消點擊狀態 Delay(100); }static int s3c44b0_ts_open(struct inode *inode, struct file *filp){ printk("Open successful\n"); return 0;}static int s3c44b0_ts_release(struct inode *inode, struct file *filp){ printk("Close successful\n"); return 0;}static int s3c44b0_ts_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg){ switch(cmd) { case 0: if(!(rUTRSTAT0 & 0x1)) { Test_Touch(); //有點擊則調用函數讀取坐標 return 0; } else
return 0; default: printk("ERROR\n"); } return 0;}static int s3c44b0_ts_read(struct file *fp, char *buf, size_t count)
{ put_user(_State.Pressed, buf); //將坐標存入緩沖區,以便上層read函數調用 put_user(_State.x, buf+4); put_user(_State.y, buf+8); _State.x=0; _State.y=0; return count;
}static struct file_operations s3c44b0_fops = { open: s3c44b0_ts_open, ioctl: s3c44b0_ts_ioctl, release: s3c44b0_ts_release, read: s3c44b0_ts_read,};int s3c44b0_ts_init(void){ int ret; ret = register_chrdev(253, DEVICE_NAME, &s3c44b0_fops); if (ret < 0) { printk(DEVICE_NAME " can't get major number\n"); return ret; } else printk("OK\n"); return 0;}void Delay(int time) //延時子程序
{
int i,adjust=0;
if(time==0)
{
time=200;
adjust=1;
delayLoopCount=400;
rWTCON=((MCLK/1000000-1)<<8)|(2<<3);
rWTDAT=0xffff;
rWTCNT=0xffff;
rWTCON=((MCLK/1000000-1)<<8)|(2<<3)|(1<<5);
}
for(;time>0;time--)
for(i=0;i<delayLoopCount;i++);
if(adjust==1)
{
rWTCON=((MCLK/1000000-1)<<8)|(2<<3);
i=0xffff-rWTCNT;
delayLoopCount=8000000/(i*64);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -