⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 touch.c

📁 基于s3c44b0的rtc,watchdog,adc,ads的驱动程序
💻 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 + -