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

📄 addrv.c

📁 at91rm9200 AD/DA转换驱动 很好的驱动开发范例
💻 C
字号:
/* AD convert driver for H9200M and H9200T board    Author: Wei An   E-Mail: an.wei@mail.ia.ac.cn/weian@hyesco.com   Date:   2005/6/1*/#ifdef MODULE#include <linux/module.h>#include <linux/version.h>#else#endif#define __KERNEL__#include <linux/module.h>#include <linux/kernel.h>#include <linux/init.h>#include <linux/sched.h>#include <linux/fs.h>#include <linux/mm.h>#include <linux/poll.h>#include <linux/slab.h>#include <linux/ioport.h>#include <asm/uaccess.h>#include <asm/io.h>#include <linux/fcntl.h>#include <asm/arch/AT91RM9200_SYS.h>#define AD_PHYSICAL_REG_SIZE ((unsigned int)    0x00000004)#define AD_PHYSICAL_CH_ADDR ((unsigned int)     0x80000010)#define AD_PHYSICAL_CH_SIZE ((unsigned int)     0x00000002)#define AT91C_ADC_NCS7      ((unsigned int) 	0x80000020)#define AT91C_ADC_RD        ((unsigned int) 	0x80000030)#define AT91C_SEL_CH0         ((unsigned char )0x80000010)#define AT91C_SEL_CH1         ((unsigned char )0x80000012)#define AT91C_SEL_CH2         ((unsigned char )0x80000014)#define AT91C_SEL_CH3         ((unsigned char )0x80000016)#define AT91C_SEL_CH4         ((unsigned char )0x80000018)#define AT91C_SEL_CH5         ((unsigned char )0x8000001a)#define AT91C_SEL_CH6         ((unsigned char )0x8000001c)#define AT91C_SEL_CH7         ((unsigned char )0x8000001e)//global variables defvolatile unsigned int * ADC_NCS7;volatile unsigned int * ADC_RD;volatile unsigned char * SEL_CH0;volatile unsigned char * SEL_CH1;volatile unsigned char * SEL_CH2;volatile unsigned char * SEL_CH3;volatile unsigned char * SEL_CH4;volatile unsigned char * SEL_CH5;volatile unsigned char * SEL_CH6;volatile unsigned char * SEL_CH7;//local variables defstatic int AD_read(struct file *, char *,int,loff_t *);static int AD_write(struct file *,const char *,int,loff_t *);static struct file_operations ad_fops={      write     :(void(*))AD_write,      read      :(void(*))AD_read,};char ad_name[]="addrv";static unsigned int major =0;static void Delay (int);/* Configure pio */void AT91_CfgPIO_AD(void){     AT91PS_SYS sys = (AT91PS_SYS) AT91C_VA_BASE_SYS;     // Enable PIOC     sys->PIOC_PDR=(unsigned int)(1<<13);     // Select Peripheral A              sys->PIOC_ASR=(unsigned int)(1<<13);}/* AD convert */short  Convert(){           unsigned short t;      int temp;             writel(0x0,ADC_NCS7);       Delay(400);      writel(0x0,ADC_NCS7);       t=readl(ADC_RD)&0x0fff0000>>16;      return t;           }/* Read convert result and transfer it to user space */static int AD_read(struct file *AD_file, char *buf,int len,loff_t *loff){       short iopdata=0;       iopdata=Convert();       if(copy_to_user(buf,(char*)&iopdata,len))          return -EFAULT;       return len;}/* Select channel for anolog voltage input */static int AD_write(struct file *AD_file, const char *buf,int len,loff_t *loff){       int retv;       unsigned char iopdata;       /* obtain channel selection data from user space */       if(copy_from_user((char*)&iopdata,buf,len))         return -EFAULT;       switch(iopdata)       {                case '0':                     writeb(0x0,SEL_CH0);                  break;        case '1':                          writeb(0x0,SEL_CH1);                     break;          case '2':                     writeb(0x0,SEL_CH2);                                break;        case '3':                     writeb(0x0,SEL_CH3);                           break;          case '4':                     writeb(0x0,SEL_CH4);                           break;          case '5':                     writeb(0x0,SEL_CH5);                           break;        case '6':                     writeb(0x0,SEL_CH6);                           break;        case '7':                     writeb(0x0,SEL_CH7);                           break;         default:             break;          }        Delay(4000);return len;     }/* * Main initialization routine */static int __init AD_at91_init(void){       int retv;       AT91PS_SYS sys = (AT91PS_SYS) AT91C_VA_BASE_SYS;              sys->EBI_SMC2_CSR[7] =(AT91C_SMC2_NWS & 0xff) | AT91C_SMC2_WSEN \       | (AT91C_SMC2_TDF & 0x300) | AT91C_SMC2_BAT | AT91C_SMC2_DBW_16;       AT91_CfgPIO_AD();       /* map hardware I/O address to virtual memory address */       ADC_NCS7=(unsigned int*)ioremap(AT91C_ADC_NCS7,AD_PHYSICAL_REG_SIZE);       ADC_RD=(unsigned int*)ioremap(AT91C_ADC_RD,AD_PHYSICAL_REG_SIZE);       SEL_CH0=(unsigned char*)ioremap(AT91C_SEL_CH0,AD_PHYSICAL_CH_SIZE);       SEL_CH1=(unsigned char*)ioremap(AT91C_SEL_CH1,AD_PHYSICAL_CH_SIZE);       SEL_CH2=(unsigned char*)ioremap(AT91C_SEL_CH2,AD_PHYSICAL_CH_SIZE);       SEL_CH3=(unsigned char*)ioremap(AT91C_SEL_CH3,AD_PHYSICAL_CH_SIZE);       SEL_CH4=(unsigned char*)ioremap(AT91C_SEL_CH4,AD_PHYSICAL_CH_SIZE);       SEL_CH5=(unsigned char*)ioremap(AT91C_SEL_CH5,AD_PHYSICAL_CH_SIZE);       SEL_CH6=(unsigned char*)ioremap(AT91C_SEL_CH6,AD_PHYSICAL_CH_SIZE);       SEL_CH7=(unsigned char*)ioremap(AT91C_SEL_CH7,AD_PHYSICAL_CH_SIZE);       /* register module */       retv=register_chrdev(major,ad_name,&ad_fops);       if(retv<0)       {         printk("<1>Register Fail!\n");         return retv;       }      if(major==0)        major=retv;      return 0;}/* unregister module */static void __exit AD_at91_cleanup(void){     int retv;      retv=unregister_chrdev(major,ad_name);      if(retv<0)      {        printk("<1>UnRegister Fail!\n");        return;      }      printk("<1>ADDRV:GOOD-bye!\n");       }void Delay(int i){	int j,k;	for(j=0;j<i;j++)	{	k=0;	}    }module_init(AD_at91_init);module_exit(AD_at91_cleanup);

⌨️ 快捷键说明

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