TQ2440的AD驱动

#include <linux/module.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>


MODULE_LICENSE("GPL");


#define ADCCON 0x5800000


static struct resource adc_resource[] = {
	[0] = {
		.start = ADCCON,
		.end   = ADCCON + 8,
		.flags = IORESOURCE_MEM,//地址类型的资源
	},
	[1] = {
		.start = ADCCON,
		.end   = ADCCON+8,
		.flags = IORESOURCE_IRQ,//中断类型的资源
	},
};


struct platform_device key_device = {
	.name		  = "MY-ADC",
	.id		  = 0,
	.num_resources	  = ARRAY_SIZE(adc_resource),
	.resource	  = adc_resource,
};


static int button_init()
{
    platform_device_register(&key_device);
     
    return 0;
}




static void button_exit()
{	   
    platform_device_unregister(&key_device);
}




module_init(button_init);
module_exit(button_exit);

#include <linux/module.h>  
#include <linux/device.h>  
#include <linux/platform_device.h>  
#include <linux/interrupt.h>  
#include <linux/fs.h>  
#include <linux/wait.h>  
#include <linux/sched.h>  
#include <linux/delay.h>
#include <asm/uaccess.h>  
#include <asm/io.h>  
static int major = 237;  
  
  
static wait_queue_head_t wq;  
static int have_data = 0;  
static int adc;  
static struct resource *res1;  
static struct resource *res2;  
static void *adc_base;  
  
#define ADCCON 0x00000     
#define ADCDLY 0x00008                 
#define ADCDAT 0x0000C                
  
  
static  irqreturn_t adc_handler(int irqno, void *dev)  
{  
	  int a;
    have_data = 1;  
  
    printk("11111\n");  
     
    a=readl(adc_base + ADCCON); 
    if((a>>15)==0x1)
    wake_up_interruptible(&wq);  
    return IRQ_HANDLED;  
}  
static int adc_open (struct inode *inod, struct file *filep)  
{  
  
    return 0;  
}  
static ssize_t adc_read(struct file *filep, char __user *buf, size_t len, loff_t *pos)  
{  
		writel(0X0000,adc_base +ADCCON );
    writel(1<<0 | 1<<14 | 0X0<<3 | 0X31<<6 ,adc_base +ADCCON );  
  
    wait_event_interruptible(wq, have_data==1);  
  mdelay(100);
    /*read data*/  
    adc = readl(adc_base+ADCDAT);  
    printk("%d",adc);  
  //  adc = adc&0x3f;
    if(copy_to_user(buf,&adc,sizeof(int)))  
    {  
        return -EFAULT;  
    }  
    have_data = 0;  
    return len;  
}  
static  int adc_release(struct inode *inode, struct file *filep)  
{  
    return 0;  
}  
static struct file_operations  adc_ops =  
{  
    .open = adc_open,  
    .release = adc_release,  
    .read = adc_read,  
};  
  
  
static int hello_probe(struct platform_device *pdev)  
{  
    int ret;  
    printk("match 0k \n");  
  
    res1 = platform_get_resource(pdev,IORESOURCE_IRQ, 0);  
    res2 = platform_get_resource(pdev,IORESOURCE_MEM, 0);   
         
    ret = request_irq(res1->start,adc_handler,IRQF_DISABLED,"adc1",NULL);  
      adc_base = ioremap(res2->start,res2->end-res2->start);  
  
    register_chrdev( major, "adc", &adc_ops);  
    init_waitqueue_head(&wq);  
      
    return 0;  
}  
static int hello_remove(struct platform_device *pdev)  
{  
    free_irq(res1->start,NULL);  
    free_irq(res2->start,NULL);    
    unregister_chrdev( major, "adc");  
    return 0;  
}  
/*  
static struct of_device_id adc_id[]=  
{  
    {.compatible = "fs4412,adc" },  
};  
  */
static struct platform_driver hello_driver=  
{  
      
    .probe = hello_probe,  
    .remove = hello_remove,  
    .driver ={  
        .name = "MY-ADC",  
        .owner	= THIS_MODULE,  
    },  
};  
  
static int hello_init(void)  
{  
    printk("hello_init");  
    return platform_driver_register(&hello_driver);  
}  
static void hello_exit(void)  
{  
    platform_driver_unregister(&hello_driver);  
    printk("hello_exit \n");  
    return;  
}  
MODULE_LICENSE("GPL");  
module_init(hello_init);  
module_exit(hello_exit);

#include <sys/types.h>  
#include <sys/stat.h>  
#include <fcntl.h>  
#include <stdio.h>  


  
main()  
{  
    int fd,len;  
    int adc=0;  


    fd = open("/dev/adc",O_RDWR);
    
    if(fd<0)  
    {  
        perror("open fail \n");  
        return ;  
    }  
  
    while(1)  
    {  
        read(fd,&adc,4);  
        adc= adc<<12;
        adc= adc>>12;
     //   adc= adc>>8;


        printf("adc%0.2f V \n",(1.8*adc)/51314);
   //     printf("adc%x V \n",adc);


     sleep(2);
    }  
  
    close(fd);  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值