Chinaunix首页 | 论坛 | 博客
  • 博客访问: 65275
  • 博文数量: 50
  • 博客积分: 2360
  • 博客等级: 大尉
  • 技术积分: 620
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-02 15:59
文章分类

全部博文(50)

文章存档

2011年(1)

2009年(49)

我的朋友

分类: LINUX

2009-08-19 19:43:55

//===============adcDriver.c==================================================
#ifndef __KERNEL__
    #define __KERNEL__
#endif
#ifndef MODULE
    #define MODULE
#endif
#include 
#include 
#include     /* printk() */
#include         /* __init __exit */
#include     /* size_t */
#include             /* file_operation */
#include     /* Error number */
#include     /* udelay */
#include     /* copy_to_user, copy_from_user */
#include 
#include 
#define DRIVER_NAME    "adc"
#ifdef DEBUG
#define PRINTK(fmt, arg...)        printk(KERN_NOTICE fmt, ##arg)
#else
#define PRINTK(fmt, arg...)
#endif
/*
    KERN_EMERG        用于紧急事件,一般是系统崩溃前的提示信息
    KERN_ALERT        用于需要立即采取动作的场合
    KERN_CRIT        临界状态,通常设计验证的硬件或软件操作失败
    KERN_ERR        用于报告错误状态.设备驱动程序通常会用它报告来自硬件的问题
    KERN_WARNING    就可能出现的问题提出警告.这些问题通常不会对系统造成严重破坏
    KERN_NOTICE        有必要提示的正常情况.许多安全相关的情况用这个级别汇报
    KERN_INFO        提示性信息.有很多驱动程序在启动时用这个级别打印相关信息
    KERN_DEBUG        用于调试的信息
*/
int count=0;
static int adc_Marjor = 0;        /* Driver Major Number */
/* Driver Operation Functions */
static int adc_open(struct inode *inode, struct file *filp)
{
    MOD_INC_USE_COUNT;
    ADCCON |= 0x2;
    ADCDLY = 100;                //ADC转换延时
    printk("adc open called!\n");
    return 0;
}
static int adc_release(struct inode *inode, struct file *filp)
{
    MOD_DEC_USE_COUNT;
    ADCCON &= 0x0;
    PRINTK("adcDriver release called!\n");
    return 0;
}
static ssize_t adc_read(struct file *filp, char *buf, size_t count, loff_t *f_pos)
{
    int i,j;
    int val,aa;
     val = 0;
     aa = ADCDAT0 & 0x03ff;              //启动ADC转换,读出的第一个数是无效的
     for(i=0;i<16;i++)
    {
       while(!ADCCON&0x8000);         //判断ADC转换是否结束
       
       val += (ADCDAT0 &0x03ff);    //取出ADC转换值
       for(j=0;j<500;j++);       
    }
    val = val/16; 
    copy_to_user(buf, &val, sizeof(val));
    PRINTK("read:ADCData=0x%x,count=%d\n", val, count);
    return sizeof(val);
}
static int adc_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
    if(arg>0 && arg <7)
    {
        switch(cmd)
        {
            case 0:
                ADCCON |= (arg << 3);
                break;
            default:
                break;    
        }
        return 0;
    }
    else
        printk("error:adc channel is 0~7\n");
        return  0;
}
/* Driver Operation structure */
static struct file_operations adc_fops = {
    owner:    THIS_MODULE,
    read:    adc_read,
    ioctl:    adc_ioctl,
    open:    adc_open,
    release:adc_release,
};
/* Module Init & Exit function */
#ifdef CONFIG_DEVFS_FS
devfs_handle_t devfs_adcDriver_dir;
devfs_handle_t devfs_adcDriver_raw;
#endif
static int __init myModule_init(void)
{
            /* Module init code */
    PRINTK("myModule_init\n");
            /* Driver register */
    adc_Marjor = register_chrdev(0, DRIVER_NAME, &adc_fops);
    if(adc_Marjor < 0)
    {
        PRINTK("register char device fail!\n");
        return adc_Marjor;
    }
    PRINTK("register adc OK! Major = %d\n", adc_Marjor);
#ifdef CONFIG_DEVFS_FS
    devfs_adcDriver_dir = devfs_mk_dir(NULL, "adc", NULL);
    devfs_adcDriver_raw = devfs_register(devfs_adcDriver_dir, "0", DEVFS_FL_DEFAULT, adc_Marjor, 0, S_IFCHR | S_IRUSR | S_IWUSR, &adc_fops, NULL);
    PRINTK("add dev file to devfs OK!\n");
#endif
    ADCCON = (0 << 0)|    // ADC转换设置 未设置
             (0 << 1)|    // 读AD数据触发AD转换 未使用
             (0 << 2)|    // StandBy模式选择 为普通操作模式
             (0 << 3)|    // ADC通道选择
             (49 << 6)|     // CLKDIV = Fpclk /49+1/5 ,即转换时钟为1MHz  Fpclk = 10M   ADC转换频率400K
             (1 << 14);        // 使能软件预设值
    return 0;
}
static void __exit myModule_exit(void)
{
    /* Module exit code */
    PRINTK("myModule_exit\n");
    /* Driver unregister */
    
    if(adc_Marjor > 0)
    {
#ifdef CONFIG_DEVFS_FS
        devfs_unregister(devfs_adcDriver_raw);
        devfs_unregister(devfs_adcDriver_dir);
#endif
        unregister_chrdev(adc_Marjor, DRIVER_NAME);
    }
    return;
}
MODULE_AUTHOR("sure");
MODULE_LICENSE("Dual BSD/GPL");
module_init(myModule_init);
module_exit(myModule_exit);
//===========Makefile=========================================================
OUTPUT = adcDriver.o
OUTPUT_DIR = ./
#s3c2410
KERNEL = /home/sure/kernel_2.4.18
CROSSPREFIX = /usr/local/arm/2.95.3/bin/arm-linux-
#pc 2.4
#KERNEL = /usr/src/linux-2.4
#CROSSPREFIX =
CFLAGS = -Wall -I$(KERNEL)/include -c -DDEBUG
#CFLAGS += -DEXPORT_SYMTAB -DMODVERSIONS -include $(KERNEL)/include/linux/modversions.h 
DEST = $(foreach fn, $(OUTPUT), $(OUTPUT_DIR)/$(fn))
all: $(OUTPUT_DIR)/install.sh
$(OUTPUT_DIR)/install.sh: $(OUTPUT_DIR) $(DEST)
    @rm $@ -f
    @echo -e " $(foreach fn, $(OUTPUT), "insmod $(fn)\\n")" >> $@
    @echo "Finished!"
$(OUTPUT_DIR):
    @mkdir $@
    @chmod 777 -R $@
$(OUTPUT_DIR)/%.o: %.c
    @echo -n "Compling $^..."
    @$(CROSSPREFIX)gcc $(CFLAGS) $^ -o $@
    @echo "OK"
clean:
    @find . \( -name '*.[oas]' -o -name install.sh \) -type f -print | xargs rm -f
    @echo "Cleaned!"
//======================test.c============================================
#include 
#include 
#include 
#include 
#include 
#define ADC_SET_CHANNEL 0
main()
{
        int fd;
        int ret;
        int data;
        fd = open("/dev/adc/0",O_RDWR);
        if(fd<0)
                printf("open /dev/adc/0 error");
        ret = ioctl(fd,ADC_SET_CHANNEL,1);
        while(1)
        {
                ret = read(fd,&data,4);
 
                printf("adc is %d\n",data);
                sleep(1);
        }
}
 
阅读(397) | 评论(0) | 转发(0) |
0

上一篇:ipmsg实例

下一篇:key44driver

给主人留下些什么吧!~~