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

📄 ads7842.c

📁 基于arm下的linux操作系统中的驱动程序
💻 C
字号:
/*****************************************************************************
;Institue of Automation, Chinese Academy of Sciences
;www.hyesco.com
;Description: 	ADS7842 driver on Linux 
;Date:					2007-01-20		
;Author:						
;E_mail:			
*****************************************************************************/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/fs.h>
#include <linux/poll.h>
#include <linux/slab.h>
#include <linux/ioport.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <asm/irq.h> 				
#include <asm/arch/AT91RM9200_SYS.h>

#include <asm/arch/hardware.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/string.h>
#include <linux/devfs_fs_kernel.h>

//*********************************************************
//ADC 16通道端口地址定义:	 
//*********************************************************
#define ADC_ADDR      0x20
//*********************************************************
//系统外设基地址:
//0x7000,0000: NCS6;
//*********************************************************
#define	PHYADDR   		0x70000060L
#define ADDR_Size			0x24				

//*********************************************************
//全局变量、常量定义:
//*********************************************************
static int major=0;						
static char dev_name[]="ADS7842";
static volatile unsigned char * IO_Addr = NULL;

//*********************************************************
//函数定义;
//*********************************************************
static int s_open(struct inode * s_node,struct file * s_file);
static int s_write(struct file*s_file, const char*w_buf,int len,loff_t* loff);

//*********************************************************
//文件操作结构体;
//*********************************************************
struct file_operations Tmint_fops_ADDR=
{
    write	:	(void(*)) s_write,
    open	:	(void(*)) s_open,		
 };

//*********************************************************
//延时函数;
//*********************************************************
void Delay(unsigned int x)
{
		unsigned int i,j;
		for(i=0;i<=x;i++)
			for(j=0;j<100;j++)
				;
}

//*********************************************************
//初始化模块函数;
//*********************************************************
static devfs_handle_t devfs_handle;
static int __init s_init_module(void)
{
    int ret;

		//注册模块;
		if ((ret=devfs_register_chrdev(major,dev_name,&Tmint_fops_ADDR)) < 0)
			  {
			   printk("\r\n<1>Register Fail!\r\n");        
			   return ret;
			  }

		//动态分配主设备号;
		if(major == 0)
			   major = ret;
		
		//建立设备文件;
		devfs_handle = devfs_register(NULL, dev_name ,DEVFS_FL_DEFAULT,
			major, 0, S_IFCHR | S_IRUSR | S_IWUSR,&Tmint_fops_ADDR, NULL);
		
		//申请内存区域,以检测该地址空间是否被使用;
		if (!request_mem_region(PHYADDR,ADDR_Size,dev_name))
	    printk("Error request mem address! \r\n");         
    		
		//进行内存区域的映射,把物理地址映射为逻辑地址;  
		IO_Addr = (unsigned char*)ioremap_nocache(PHYADDR,ADDR_Size);
		
		//定义EBI控制寄存器的读写参数,参考AT91RM9200用户手册P190;
		AT91_SYS -> EBI_SMC2_CSR[6] = (AT91C_SMC2_NWS & 0x0f) | AT91C_SMC2_WSEN 
																	| AT91C_SMC2_DBW_16 | AT91C_SMC2_BAT
																	| AT91C_SMC2_ACSS_3_CYCLES 
																	| AT91C_SMC2_RWSETUP | AT91C_SMC2_RWHOLD;
		
		return 0;
}

//*********************************************************
//清除模块函数;
//*********************************************************
static void __exit s_cleanup_module(void)
{
    int retv;
 		
 		//取消内存区域映射;
		iounmap((unsigned char *)IO_Addr);
	
		//释放申请的内存区域;
		release_mem_region(PHYADDR,ADDR_Size);
		
		//注销模块;
		retv=devfs_unregister_chrdev(major,dev_name);
		if(retv<0)
			{
    		printk("<1>IO_ADDR UnRegister Fail!\n");
   			return ;
			}
		printk("<1>IO_ADDR:Good Bye!\n");
}

//*********************************************************
//open函数;
//*********************************************************
static int s_open(struct inode * s_node,struct file * s_file)
{	
		MOD_INC_USE_COUNT;
		return 0;
}

//*********************************************************
//write函数;
//********************************************************* 
static int s_write(struct file*s_file, const char *w_buf,int len,loff_t* loff)
{
    unsigned char CHA;
		unsigned short AVER;
		unsigned int SUM,i;
		
		//从用户空间拷贝数据到内核空间;		
		if(copy_from_user(&CHA,w_buf,sizeof(unsigned char)))	
			return -EFAULT;
	
		//根据不同的通道号,送出相应的数据;
		writew(0x0,(volatile unsigned short *)(IO_Addr + 2*CHA));
    
    //延时以稳定输入模拟信号;
    Delay(100);
    
    //采样256次,求平均值;
    SUM = 0;
    for(i=0;i<256;i++)
    {
    	//启动ADC;
      writew(0x0,(volatile unsigned short *)(IO_Addr + ADC_ADDR));
      Delay(10);
      SUM = SUM + (readw((volatile unsigned short *)(IO_Addr + ADC_ADDR))& 0xfff);
    }
    
    AVER = SUM >> 8;
   	return AVER;      		
}

module_init(s_init_module);
module_exit(s_cleanup_module);
MODULE_LICENSE("GPL");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -