上传一个AD的驱动程序,虽然简单,但可能给你启发的!
//下载地址http://www.hzlitai.com.cn/bbs/dispbbs.asp?boardid=11&Id=4687&page=2
作者:tymanium
它是一个采集ad的0号通道的驱动程序,将驱动程序和测试程序编译好之后,在2410开发板上加一个可调电阻产生分压,并将分压接到ain0上,这时运行sample &就可以采集ain0上的电压了.文件里有说明用法,如下:
/* ad.c
* copyright (c) by tymanium from china
* this is a device module file used for sbc2410
* it can drive ad convertor, yes it's very easy
*/
/*usage:
1. arm-linux-gcc -d__kernel__ -i/friendly-arm/kernel/include -
dkbuild_badename=test -dmodule -c -o ad.o ad.c
2. arm-linux-gcc -o sample sample.c
3. copy ad.o and sample to /lib/ and /bin/ respectively
4. power on sbc2410 ,and then active your bash
5. run #insmod /lib/ad.o and #mknod /dev/ad c 200 0
6. here run #sample &, now you can sample the ain0
*/
#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/miscdevice.h>
#include <linux/sched.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <linux/spinlock.h>
#include <linux/irq.h>
#include <linux/delay.h>
#include <asm/hardware.h>
#define device_name "ad_test" //
#define ioport_major 200
/*this function is called whenever a process open the device file*/
static int ad_open(struct inode *inode, struct file *filp)
{
adccon=0x4c40; //
adctsc =0x58; //
adcdly=0x01;
return 0;
}
/*this function is called when a process call function ioctl( )*/
static int ad_ctl_ioctl(struct inode *inode, struct file *filp, unsigned int command, unsigned long arg)
{
switch(command){
case 0:
printk("the adccon register is %x \t", adccon);
return adcdat0; //get your sample code
case 1:
adccon |= 1; //start your ad
return 0;
}
return -einval;
}
/*this function is called when a process close our device file*/
static int ad_release(struct inode *inode, struct file *filp)
{
}
static struct file_operations ad_ctl_fops={
ioctl : ad_ctl_ioctl,
open : ad_open,
release : ad_release,
};
/*this function is called when you use insmod */
static int __init ad_init(void)
{
int ret;
unsigned long val=mpllcon;;
ret=register_chrdev(ioport_major, device_name, &ad_ctl_fops);
if (ret < 0) {
printk(device_name " can't register major number\n");
return ret;
}
printk("the val is %lx \n",val);
printk("the mpllcon register value is %lx \n",mpllcon);
printk("the clkdivn register value is %lx \n",clkdivn);
printk("the clkcon register value is %lx \n",clkcon);
return ret;
}
/*this function is called when you use rmmod*/
static void __exit ad_cleanup(void)
{
unregister_chrdev(ioport_major, device_name);
}
module_init(ad_init);
module_exit(ad_cleanup);
module_license("gpl");
下面是sample.c 文件
#include<stdio.h>
#include<stdlib.h>
#include<sys/ioctl.h>
#include<unistd.h>
#define device_adtest "/dev/ad"
int main( )
{
int fd,ret;
int val=-1;
float voltage;
if((fd=open(device_adtest,0))<0){
perror("can not open device");
exit(1);
}
while(1){
ioctl(fd,1,0);
sleep(1);
ret=ioctl(fd,0,0);
ret &= 0x3ff;
voltage=(float)ret;
voltage=(voltage-1023)*(-1)*3.3/1023;
printf("the sampling voltage is %f v\t \n",voltage);
}
close(fd);
return 0;
}