关闭

tq2440 adc 驱动

181人阅读 评论(0) 收藏 举报
分类:
/*************************************************************
基于tq2440开发板,内核2.6.30

功能
用到的模拟量输入口是ain2.
成功实现了ad 转换,能连续采集
2012年7月11日16:33:29
**************************************************************/

#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/pci.h>
#include <linux/poll.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/irq.h>
#include <linux/interrupt.h>


#include <mach/regs-gpio.h>
#include <mach/hardware.h>

#include <asm/irq.h>
#include <asm/uaccess.h>
#include <asm/atomic.h>
#include <asm/unistd.h>

#define DEVICE_NAME "driver_adc"


volatile unsigned long *adccon = NULL; //adc控制寄存器
volatile unsigned long *adctsc = NULL; //adc 触摸屏控制寄存器
volatile unsigned long *adcdly = NULL; //adc 起始延迟寄存器
volatile unsigned long *adcdat0 = NULL; //adc 转换数据寄存器
volatile unsigned long *adcdat1 = NULL; //adc 转换数据寄存器
volatile unsigned long *intmsk = NULL;
//*gpbdat &= ~((1<<5) );
//ain2

// ------------------- OPEN ------------------------
ssize_t drive_open (struct inode * inode ,struct file * file)
{
printk("-----------------drive open ok----------------\n");
return 0;
}


// ------------------- RELEASE/CLOSE ---------------
ssize_t drive_release (struct inode  * inode ,struct file * file)
{
printk("-----------------drive close ok----------------\n");
return 0;
}


// ------------------- READ ------------------------
ssize_t drive_read (struct file * file ,char * buf, size_t count, loff_t * f_ops)
{
int val;
printk("-----------------drive read ok----------------\n");

*adccon |= (1<<14)|(0xff<<6)|(2<<3);//设置分频倍数0xff,设置输入通道为2
//*intmsk |= (1<<31)| (1<<5)|(1<<8);
*adccon |= 0X1; //开始转换

while( *adccon & 0x1)
;

while( !(*adccon & 0x8000) ) //等待转换结束
;
val=(*adcdat0&0x3ff);
//擦,这里竟然写错了,写成adccon了,纠结了半天
//*adccon =0x3fc4;这样写不行,貌似是ioremap  位数不够的缘故

*adccon &=~1; //关adc
printk("----------------drive -----val=%d \n",val);
copy_to_user( buf,&val,sizeof(val) );
printk("---------------drive-close-ok-------------\n",val);
return val;
}


// ------------------- WRITE -----------------------
ssize_t drive_write (struct file * file ,const char * buf, size_t count, loff_t * f_ops)
{
printk("-----------------drive write ok----------------\n");
return 0;
}

// ------------------- IOCTL -----------------------
ssize_t drive_ioctl (struct inode * inode ,struct file * file, unsigned int cmd, unsigned long arg)
{
printk("-----------------drive ioctl ok----------------\n");
return 0;
}


// -------------------------------------------------

static struct file_operations drive_ops ={

.owner  = THIS_MODULE,
.open   = drive_open,
.read    = drive_read,
.write   = drive_write,
.ioctl    = drive_ioctl,
.release = drive_release,
};


static struct miscdevice misc = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &drive_ops,
};


static int __init init_drive(void)  
{
int ret;
ret = misc_register(&misc);

adccon = (volatile unsigned long *)ioremap(0x58000000, 16);
adctsc =  (volatile unsigned long *)ioremap(0x58000004, 8);
adcdly =  (volatile unsigned long *)ioremap(0x58000008, 16);
adcdat0= (volatile unsigned long *)ioremap(0x5800000c, 16);
adcdat1= (volatile unsigned long *)ioremap(0x58000010, 16);
intmsk  = (volatile unsigned long *)ioremap(0x4a000008, 32);
printk("-----------------drive button init ok----------------\n");
return 0;
}

static void __exit exit_drive(void)
{
misc_deregister(&misc);
printk("-----------------drive button exit ok----------------\n");
}



module_init(init_drive);
module_exit(exit_drive);
MODULE_LICENSE("GPL");




//--------------------------------------------测试应用程序--------------------------------------------------

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <fcntl.h>      // open() close()
#include <unistd.h>     // read() write()

#define DEVICE_NAME "/dev/driver_adc"

//------------------------------------- main ---------------------------------------------
int main(int argc, char **argv)
{
int fd,ret;

int val;
       fd = open(DEVICE_NAME, O_RDWR);
 
if (fd == -1)
{
printf("can't open device mknod %s c zhu ci \n",DEVICE_NAME);
return 0;
}

while(1)
{
read(fd,&val,sizeof(val));
printf("------------------app--------------------\n");
printf("val=%d\n",val);
sleep(3);
}
        // close 
ret = close(fd);
if (ret == -1)
{
printf("app close error!!!!!!!!!!\n");
return 0;
}
        return 0;
}// end main

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场