您好,欢迎访问一九零五行业门户网

linux adc是什么设备

linux adc是混杂设备驱动;在linux2.6.30.4中,系统已经自带有了adc通用驱动文件“arch/arm/plat-s3c24xx/adc.c”,它是以平台驱动设备模型的架构来编写的,里面是一些比较通用稳定的代码。
本教程操作环境:linux2.6.30.4系统、dell g3电脑。
linux adc是什么设备?
linux 混杂设备驱动之adc驱动
linux2.6.30.4中,系统已经自带有了adc通用驱动文件---arch/arm/plat-s3c24xx/adc.c,它是以平台驱动设备模型的架构来编写的,里面是一些比较通用稳定的代码,但是linux2.6.30.4版本的adc通用驱动文件并不完善,居然没有读函数。后来去看了linux3.8版本的adc通用文件----arch/arm/plat-samsung/adc.c才是比较完善的。
但是本节并不是分析这个文件,而是以另外一种架构来编写adc驱动,因为adc驱动实在是比较简单,就没有使用平台驱动设备模型为架构来编写了,这次我们使用的是混杂(misc)设备驱动。
问:什么是misc设备驱动?
答:miscdevice共享一个主设备号misc_major(10),但次设备号不同。所有的miscdevice设备形成一条链表,对设备访问时内核根据设备号来查找对应的miscdevice设备,然后调用其file_operations结构体中注册的文件操作接口进行操作。
struct miscdevice  { int minor; //次设备号,如果设置为misc_dynamic_minor则系统自动分配 const char *name; //设备名 const struct file_operations *fops; //操作函数 struct list_head list; struct device *parent; struct device *this_device;};
dev_init入口函数分析:
static int __init dev_init(void){ int ret; base_addr=ioremap(s3c2410_pa_adc,0x20); if (base_addr == null) { printk(kern_err failed to remap register block\n); return -enomem; } adc_clock = clk_get(null, adc); if (!adc_clock) { printk(kern_err failed to get adc clock source\n); return -enoent; } clk_enable(adc_clock); adctsc = 0; ret = request_irq(irq_adc, adcdone_int_handler, irqf_shared, device_name, &adcdev); if (ret) { iounmap(base_addr); return ret; } ret = misc_register(&misc); printk (device_name initialized\n); return ret;}
首先是映射adc寄存器地址将其转换为虚拟地址,然后获得adc时钟并使能adc时钟,接着申请adc中断,其中断处理函数为adcdone_int_handler,而flags为irqf_shared,即共享中断,因为触摸屏里也要申请adc中断,最后注册一个混杂设备。
当应用程序open (/dev/adc,...)时,就会调用到驱动里面的open函数,那么我们来看看open函数做了什么?
static int tq2440_adc_open(struct inode *inode, struct file *filp){ /* 初始化等待队列头 */ init_waitqueue_head(&(adcdev.wait)); /* 开发板上adc的通道2连接着一个电位器 */ adcdev.channel=2; //设置adc的通道 adcdev.prescale=0xff; dprintk( adc opened\n); return 0;}
很简单,先初始化一个等待队列头,因为入口函数里既然有申请adc中断,那么肯定要使用等待队列,接着设置adc通道,因为tq2440的adc输入通道默认是2,设置预分频值为0xff。当应用程序read时,就会调用到驱动里面的read函数,那么我们来看看read函数做了些什么?
static ssize_t tq2440_adc_read(struct file *filp, char *buffer, size_t count, loff_t *ppos){ char str[20]; int value; size_t len; /* 尝试获得adc_lock信号量,如果能够立刻获得,它就获得信号量并返回0   * 否则,返回非零,它不会导致调用者睡眠,可以在中断上下文使用  */ if (down_trylock(&adc_lock) == 0) { /* 表示a/d转换器资源可用 */ adc_enable = 1; /* 使能预分频,选择adc通道,最后启动adc转换*/ start_adc_ain(adcdev.channel, adcdev.prescale); /* 等待事件,当ev_adc = 0时,进程被阻塞,直到ev_adc>0 */ wait_event_interruptible(adcdev.wait, ev_adc); ev_adc = 0; dprintk(ain[%d] = 0x%04x, %d\n, adcdev.channel, adc_data, ((adccon & 0x80) ? 1:0)); /* 将在adc中断处理函数读取的adc转换结果赋值给value */ value = adc_data; sprintf(str,%5d, adc_data); copy_to_user(buffer, (char *)&adc_data, sizeof(adc_data)); adc_enable = 0; up(&adc_lock); } else { /* 如果a/d转换器资源不可用,将value赋值为-1 */ value = -1; } /* 将adc转换结果输出到str数组里,以便传给应用空间 */ len = sprintf(str, %d\n, value); if (count >= len) { /* 从str数组里拷贝len字节的数据到buffer,即将adc转换数据传给应用空间 */ int r = copy_to_user(buffer, str, len); return r ? r : len; } else { return -einval; }}
tq2440_adc_read函数首先尝试获得adc_lock信号量,因为触摸屏驱动也有使用adc资源,两者互有竞争关系,获得adc资源后,使能预分频,选择adc通道,最后启动adc转换,接着就调用wait_event_interruptible 函数进行等待,直到ev_adc>0进程才会继续往下跑,往下跑就会将adc_data数据读出来,调用copy_to_user函数将adc数据传给应用空间,最后释放adc_lock信号量。问:什么时候ev_adc>0?默认ev_adc = 0
答:在adcdone_int_handler中断处理函数里,等数据读出后,ev_adc被设置为1。
adc中断处理函数adcdone_int_handler
/* adc中断处理函数 */static irqreturn_t adcdone_int_handler(int irq, void *dev_id){ /* a/d转换器资源可用 */ if (adc_enable) { /* 读adc转换结果数据 */ adc_data = adcdat0 & 0x3ff; /* 唤醒标志位,作为wait_event_interruptible的唤醒条件 */ ev_adc = 1; wake_up_interruptible(&adcdev.wait); } return irq_handled;}
当ad转换完成后就会触发adc中断,就会进入adcdone_int_handler,这个函数就会讲ad转换数据读到adc_data,接着将唤醒标志位ev_adc置1,最后调用wake_up_interruptible函数唤醒adcdev.wait等待队列。
总结一下adc的工作流程:一、open函数里,设置模拟输入通道,设置预分频值
二、read函数里,启动ad转换,进程休眠
三、adc_irq函数里,ad转换结束后触发adc中断,在adc中断处理函数将数据读出,唤醒进程
四、read函数里,进程被唤醒后,将adc转换数据传给应用程序
adc驱动参考源码:
/*************************************name:embedsky_adc.ccopyright:www.embedsky.net*************************************/#include <linux/errno.h>#include <linux/kernel.h>#include <linux/module.h>#include <linux/slab.h>#include <linux/input.h>#include <linux/init.h>#include <linux/serio.h>#include <linux/delay.h>#include <linux/clk.h>#include <asm/io.h>#include <asm/irq.h>#include <asm/uaccess.h>#include <mach/regs-clock.h>#include <plat/regs-timer.h>  #include <plat/regs-adc.h>#include <mach/regs-gpio.h>#include <linux/cdev.h>#include <linux/miscdevice.h>#include tq2440_adc.h#undef debug//#define debug#ifdef debug#define dprintk(x...) {printk(kern_debug embedsky_adc:  x);}#else#define dprintk(x...) (void)(0)#endif#define device_name adc /* 设备节点: /dev/adc */static void __iomem *base_addr;typedef struct{ wait_queue_head_t wait; /* 定义等待队列头 */ int channel; int prescale;}adc_dev;declare_mutex(adc_lock); /* 定义并初始化信号量,并初始化为1 */static int adc_enable = 0; /* a/d转换器资是否可用标志位 */static adc_dev adcdev; /* 用于表示adc设备 */static volatile int ev_adc = 0; /* 作为wait_event_interruptible的唤醒条件 */static int adc_data;static struct clk *adc_clock;#define adccon (*(volatile unsigned long *)(base_addr + s3c2410_adccon)) //adc control#define adctsc (*(volatile unsigned long *)(base_addr + s3c2410_adctsc)) //adc touch screen control#define adcdly (*(volatile unsigned long *)(base_addr + s3c2410_adcdly)) //adc start or interval delay#define adcdat0 (*(volatile unsigned long *)(base_addr + s3c2410_adcdat0)) //adc conversion data 0#define adcdat1 (*(volatile unsigned long *)(base_addr + s3c2410_adcdat1)) //adc conversion data 1#define adcupdn (*(volatile unsigned long *)(base_addr + 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通道,最后启动adc转换*/#define start_adc_ain(ch, prescale) \ do{ adccon = prescale_en | prscvl(prescale) | adc_input((ch)) ; \ adccon |= adc_start; \ }while(0)/* adc中断处理函数 */static irqreturn_t adcdone_int_handler(int irq, void *dev_id){ /* a/d转换器资源可用 */ if (adc_enable) { /* 读adc转换结果数据 */ adc_data = adcdat0 & 0x3ff; /* 唤醒标志位,作为wait_event_interruptible的唤醒条件 */ ev_adc = 1; wake_up_interruptible(&adcdev.wait); } return irq_handled;}static ssize_t tq2440_adc_read(struct file *filp, char *buffer, size_t count, loff_t *ppos){ char str[20]; int value; size_t len; /* 尝试获得adc_lock信号量,如果能够立刻获得,它就获得信号量并返回0 * 否则,返回非零,它不会导致调用者睡眠,可以在中断上下文使用 */ if (down_trylock(&adc_lock) == 0) { /* 表示a/d转换器资源可用 */ adc_enable = 1; /* 使能预分频,选择adc通道,最后启动adc转换*/ start_adc_ain(adcdev.channel, adcdev.prescale); /* 等待事件,当ev_adc = 0时,进程被阻塞,直到ev_adc>0 */ wait_event_interruptible(adcdev.wait, ev_adc); ev_adc = 0; dprintk(ain[%d] = 0x%04x, %d\n, adcdev.channel, adc_data, ((adccon & 0x80) ? 1:0)); /* 将在adc中断处理函数读取的adc转换结果赋值给value */ value = adc_data; sprintf(str,%5d, adc_data); copy_to_user(buffer, (char *)&adc_data, sizeof(adc_data)); adc_enable = 0; up(&adc_lock); } else { /* 如果a/d转换器资源不可用,将value赋值为-1 */ value = -1; } /* 将adc转换结果输出到str数组里,以便传给应用空间 */ len = sprintf(str, %d\n, value); if (count >= len) { /* 从str数组里拷贝len字节的数据到buffer,即将adc转换数据传给应用空间 */ int r = copy_to_user(buffer, str, len); return r ? r : len; } else { return -einval; }}static int tq2440_adc_open(struct inode *inode, struct file *filp){ /* 初始化等待队列头 */ init_waitqueue_head(&(adcdev.wait)); /* 开发板上adc的通道2连接着一个电位器 */ adcdev.channel=2; //设置adc的通道 adcdev.prescale=0xff; dprintk( adc opened\n); return 0;}static int tq2440_adc_release(struct inode *inode, struct file *filp){ dprintk( adc closed\n); return 0;}static struct file_operations dev_fops = { owner: this_module, open: tq2440_adc_open, read: tq2440_adc_read, release: tq2440_adc_release,};static struct miscdevice misc = { .minor = misc_dynamic_minor, .name = device_name, .fops = &dev_fops,};static int __init dev_init(void){ int ret; base_addr=ioremap(s3c2410_pa_adc,0x20); if (base_addr == null) { printk(kern_err failed to remap register block\n); return -enomem; } adc_clock = clk_get(null, adc); if (!adc_clock) { printk(kern_err failed to get adc clock source\n); return -enoent; } clk_enable(adc_clock); adctsc = 0; ret = request_irq(irq_adc, adcdone_int_handler, irqf_shared, device_name, &adcdev); if (ret) { iounmap(base_addr); return ret; } ret = misc_register(&misc); printk (device_name initialized\n); return ret;}static void __exit dev_exit(void){ free_irq(irq_adc, &adcdev); iounmap(base_addr); if (adc_clock) { clk_disable(adc_clock); clk_put(adc_clock); adc_clock = null; } misc_deregister(&misc);}export_symbol(adc_lock);module_init(dev_init);module_exit(dev_exit);module_license(gpl);module_author(www.embedsky.net);module_description(adc drivers for embedsky sky2440/tq2440 board and support touch);
adc应用测试参考源码:
/*************************************name:embedsky_adc.ccopyright:www.embedsky.net*************************************/#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/ioctl.h>#include <fcntl.h>#include <linux/fs.h>#include <errno.h>#include <string.h>int main(void){ int fd ; char temp = 1; fd = open(/dev/adc, 0); if (fd < 0) { perror("open adc device !"); exit(1); } for( ; ; ) { char buffer[30]; int len ; len = read(fd, buffer, sizeof buffer -1); if (len > 0) { buffer[len] = '\0'; int value; sscanf(buffer, %d, &value); printf(adc value: %d\n, value); } else { perror(read adc device !); exit(1); } sleep(1); }adcstop: close(fd);}
测试结果:
[wj2440]# ./adc_test adc value: 693adc value: 695adc value: 694adc value: 695adc value: 702adc value: 740adc value: 768adc value: 775adc value: 820adc value: 844adc value: 887adc value: 937adc value: 978adc value: 1000adc value: 1023adc value: 1023adc value: 1023
相关推荐:《linux视频教程》
以上就是linux adc是什么设备的详细内容。
其它类似信息

推荐信息