📄 myadc.c
字号:
/****************Copyright (c)***************************** File name: myadc.c** Created by: junzuyu** Created date: 2008-10-9** Version: 1.0*********************************************************/#include <linux/config.h>#include <linux/module.h>#include <linux/version.h>#include <linux/kernel.h>#include <linux/init.h>#include <linux/fs.h>#include <asm/hardware.h>#include <asm/delay.h>#include <asm/uaccess.h>#include <linux/ioport.h>#include <linux/cdev.h>#include <asm-arm/arch-s3c2410/regs-adc.h>#include <asm/io.h>static int adc_open(struct inode *inode,struct file *filp);static int adc_release(struct inode *inode,struct file *filp);static ssize_t adc_read(struct file *filp,char *buffer,size_t count,loff_t *ppos);static int __init s3c2410_adc_init(void);static void __exit s3c2410_adc_eixt(void);module_init(s3c2410_adc_init);module_exit(s3c2410_adc_eixt);MODULE_LICENSE("Proprietary");MODULE_DESCRIPTION("Shanghai YuanHong Development Co.,LTD.");MODULE_SUPPORTED_DEVICE("utu-linux2.6.x s3c2440 spi_ad");MODULE_AUTHOR("junzuyu");#define DEVICE_NAME "myadc"#define ADCMINOR 0static int ADCMajor=0;//#define ADCMINOR 0unsigned long rADCCON_save;static void __iomem *base_addr;volatile unsigned long prescaler;struct adc_dev_t{ struct cdev cdev; unsigned long value;} adc_dev;#define REQCNT 100 #define ADC_FREQ 2500000 // adc freq#define LOOP 10000static struct file_operations myadc_fops= { .owner = THIS_MODULE, .open = adc_open, .read = adc_read, .release = adc_release,};static int adc_open(struct inode *inode,struct file *filp) { rADCCON_save=ioread32(base_addr+S3C2410_ADCCON);//保存原来的设置,退出时还原 printk("ADCCON=%lx\n",rADCCON_save); prescaler=ADC_FREQ; //预分频值 printk("ADC conv. freq. = %luHz\n",prescaler); prescaler = 50000000/ADC_FREQ -1; //PCLK:50.7MHz printk("PCLK/ADC_FREQ - 1 = %lu\n",prescaler); //prescaler=19 printk("myadc open ok!\n"); return 0;}static int adc_release(struct inode *inode,struct file *filp){ iowrite32(rADCCON_save,base_addr+S3C2410_ADCCON); printk("myadc release ok!\n"); return 0;}static ssize_t adc_read(struct file *filp,char *buffer,size_t count,loff_t *ppos){ unsigned long addata; unsigned long temp; static int ch = 1; temp=(1<<14)|(prescaler<<6)|(ch<<3); printk("temp=%lx\n",temp); iowrite32(temp,base_addr+S3C2410_ADCCON); printk("Set CH if same %lx\n",(1<<14)|(prescaler<<6)|(ch<<3)); printk("before is ok!\n"); iowrite32((ioread32(base_addr+S3C2410_ADCCON)|0x1), base_addr+S3C2410_ADCCON); //start ADC printk("start ADC %x\n",(ioread32(base_addr+S3C2410_ADCCON))); while(ioread32(base_addr+S3C2410_ADCCON) & 0x1); //check if Enable_start is low while((ioread32(base_addr+S3C2410_ADCCON) & 0x8000)); //check if EC(End of Conversion) flag is high //printk //printk("I will gave you a adc data %x\n",(readl(base_addr+S3C2410_ADCDAT0) & 0x3ff)); //iowrite32(rADCCON_save,base_addr+S3C2410_ADCCON);//还原 addata=(ioread32(base_addr+S3C2410_ADCDAT0) & 0x3ff); copy_to_user(buffer,(unsigned long *)&addata,sizeof(unsigned long)); return 0;}static int __init s3c2410_adc_init(void){ int ret; dev_t devno=MKDEV(ADCMajor,0); cdev_init(&adc_dev.cdev,&myadc_fops); adc_dev.cdev.owner=THIS_MODULE; if(ADCMajor){ register_chrdev_region(devno,1,DEVICE_NAME); }else{ alloc_chrdev_region(&devno,0,1,DEVICE_NAME); ADCMajor=MAJOR(devno); } ret=cdev_add(&adc_dev.cdev,devno,1); printk("ADCMajor=%d\n",ADCMajor); #ifdef CONFIG_DEVFS_FS devfs_mk_cdev(MKDEV(ADCMajor,ADCNMINOR),S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP,DEVICE_NAME);#endif printk(DEVICE_NAME" initialized\n"); if(request_mem_region(S3C2410_PA_ADC,0x20,DEVICE_NAME)==NULL){ printk("request_mem_region error\n"); return(0); } base_addr=ioremap(S3C2410_PA_ADC,0x20);//映射ADC的控制寄存器 if (base_addr == NULL) { printk(KERN_ERR "Failed to remap register block\n"); return -ENOMEM; } printk("base_addr=%lx\n",base_addr); return 0;} static void __exit s3c2410_adc_eixt(void){ dev_t devno; devno = MKDEV(ADCMajor,0); iounmap(base_addr); release_mem_region(S3C2410_PA_ADC,0x20); #ifdef CONFIG_DEVFS_FS devfs_remove(DEVICE_NAME); #endif unregister_chrdev_region(devno,1); cdev_del(&adc_dev.cdev);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -