驱动程序
- #include <linux/kernel.h>
- #include <linux/module.h>
- #include <linux/slab.h>
- #include <linux/input.h>
- #include <linux/init.h>
- #include <linux/errno.h>
- #include <linux/serio.h>
- #include <linux/delay.h>
- #include <linux/clk.h>
- #include <linux/sched.h>
- #include <linux/cdev.h>
- #include <linux/miscdevice.h>
- #include <asm/io.h>
- #include <asm/irq.h>
- #include <asm/uaccess.h>
- #include <plat/regs-adc.h>
- #include <mach/map.h>
- static void __iomem * base_addr;
- static struct clk *adc_clock;
- /*#define ADCCON_BASE (0x7E00B000)
- #define ADCCON (*(volatile unsigned *)(ADCCON_BASE+0x00)) // ADC control
- #define ADCTSC (*(volatile unsigned *)(ADCCON_BASE+0x04)) // ADC touch screen control
- #define ADCDLY (*(volatile unsigned *)(ADCCON_BASE+0x08)) // ADC start or Interval Delay
- #define ADCDAT0 (*(volatile unsigned *)(ADCCON_BASE+0x0C)) // ADC conversion data 0
- #define ADCDAT1 (*(volatile unsigned *)(ADCCON_BASE+0x10)) // ADC conversion data 1
- #define ADCUPDN (*(volatile unsigned *)(ADCCON_BASE+0x14)) // Stylus Up/Down interrupt status
- #define PRESCALE_DIS (0 << 14)
- #define PRESCALE_EN (1 << 14)
- #define PRSCVL(x) ((x) << 6)
- #define ADC_INPUT(x) ((x) << 3)
- #define ADC_START (1 << 0)
- #define ADC_ENDCVT (1 << 15) */
- /*ADC寄存器地址*/
- #define __ADCREG(name) (*(volatile unsigned long *)(base_addr + name))
- #define ADCCON __ADCREG(S3C_ADCCON) // ADC control
- #define ADCTSC __ADCREG(S3C_ADCTSC) // ADC touch screen control
- #define ADCDLY __ADCREG(S3C_ADCDLY) // ADC start or Interval Delay
- #define ADCDAT0 __ADCREG(S3C_ADCDAT0) // ADC conversion data 0
- #define ADCDAT1 __ADCREG(S3C_ADCDAT1) // ADC conversion data 1
- #define ADCUPDN __ADCREG(S3C_ADCUPDN) // Stylus Up/Down interrupt status
- /*根据s3c6410的文档,设置相应的ADC控制器位*/
- #define PRESCALE_DIS (0 << 14)
- #define PRESCALE_EN (1 << 14)
- #define PRSCVL(x) ((x) << 6)
- #define ADC_INPUT(x) ((x) << 3)
- #define ADC_START (1 << 0)
- #define ADC_ENDCVT (1 << 15)
- #define DEVICE_NAME "adc_dev"
- static int adc_init()
- {
- printk("<0>adc_init sucessed!\n");
- unsigned int preScaler = 0XFF;
- printk("<0>111111111!\n");
- ADCCON = (1<<14)|(preScaler<<6)|(0<<3)|(0<<2);
- ADCCON |= ADC_START;
- printk("<0>2222222222!\n");
- return 0;
- }
- static int adc_open(struct inode *inode ,struct file *filp)
- {
- printk("adc_open!\n");
- adc_init();
- return 0;
- }
- static int adc_release(struct inode *inode,struct file *filp)
- {
- return 0;
- }
- static ssize_t adc_read(struct file *filp,char __user *buff,size_t size,loff_t *ppos)
- {
- printk("<0>adc_read!\n");
- int ret=0;
- ADCCON |= ADC_START;
- while(ADCCON & 0x01);//check if Enable_start is low
- while(!(ADCCON &0x8000));/*检查转换是否结束*/
- ret = ADCDAT0 & 0x3ff; //重点,与参考的不同,我觉得这样才是正确的
- copy_to_user(buff,(char *)&ret,sizeof(ret));
- return sizeof(ret);
- }
- static struct file_operations dev_fops =
- {
- .owner = THIS_MODULE,
- .open = adc_open,
- .release = adc_release,
- .read = adc_read,
- };
- static struct miscdevice misc =
- {
- .minor = MISC_DYNAMIC_MINOR,
- .name = DEVICE_NAME,
- .fops = &dev_fops,
- };
- static int __init dev_init()
- {
- int ret;
- base_addr =ioremap(0x7E00B000,0X20);//地址映射
- printk("<0>ioremap sucessed!\n");
- if(base_addr == NULL)
- {
- printk(KERN_ERR"failed to remap\n");
- return -ENOMEM;
- }
- adc_clock = clk_get(NULL,"adc");//激活adc时钟模块
- printk("<0>clk_get sucessed!\n");
- if(!adc_clock)
- {
- printk(KERN_ERR"failed to get adc clock\n");
- return -ENOENT;
- }
- clk_enable(adc_clock);
- ret = misc_register(&misc);//混杂设备注册
- printk("dev_init return ret:%d\n",ret);
- return ret;
- }
- static void __exit dev_exit()
- {
- iounmap(base_addr);//取消映射
- if(adc_clock)//disable adc clock取消adc时钟
- {
- clk_disable(adc_clock);
- clk_put(adc_clock);
- adc_clock =NULL;
- }
- misc_deregister(&misc);//注销混杂设备
- }
- module_init(dev_init);
- module_exit(dev_exit);
- MODULE_LICENSE("GPL");
- MODULE_AUTHOR("MUGE0913");
应用程序:
- #include <stdio.h>
- #include <fcntl.h>
- #include <unistd.h>
- int main()
- {
- int fp,adc_data,i;
- int ret;
- fp = open("/dev/adc_dev",O_RDWR);
- if(fp<0)
- printf("open failed!\n");
- printf("open sucessed!\n");
- for(i=0;i<100;i++)
- {
- ret = read(fp,&adc_data,sizeof(adc_data));
- printf("read sucessed!\n");
- if(ret<0)
- {
- printf("read ADC failed!\n");
- return -1;
- }
- else
- printf("Read ADC value is: %d\n",adc_data);
- sleep(1);
- }
- close(fp);
- return 0;
- }