mini2440驱动学习第四课————ADC

      很快就进入了第四课ADC驱动的学习了,经过前三课的学习,相信大家和我一样,对驱动程序有点感觉了吧,基于此,ADC的驱动我比较快地掌握了其大体的框架,本课主要掌握互斥信号量和内存映射等两个问题,对于内部更多的细节,还有请诸位看官多多指教。

 

代码贴出如下:

#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 <linux/wait.h>
#include <linux/sched.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 "s3c24xx-adc.h"

 

#undef DEBUG
//#define DEBUG
#ifdef DEBUG
#define DPRINTK(x...) {printk(__FUNCTION__"(%d): ",__LINE__);printk(##x);}
#else
#define DPRINTK(x...) (void)(0)
#endif

#define DEVICE_NAME "adc"

 

static void __iomem *base_addr;

 

typedef struct

{          
      wait_queue_head_t wait;         //每一个AD转换设备应该都有各自的等待队列
      int channel;
      int prescale;
}ADC_DEV;

 

DECLARE_MUTEX(ADC_LOCK);         //信号量,互斥模式,并初始化为1,可供一个设备占用

                                                  //DECLARE_MUTEX_LOCKED(ADC_LOCK); 则初始化为0


static int OwnADC = 0;

static ADC_DEV adcdev;
static volatile int ev_adc = 0;
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)             //宏定义ADCCON寄存器位

 

#define START_ADC_AIN(ch, prescale) /
 do{ /                                                           //开始AD转换
      ADCCON = PRESCALE_EN | PRSCVL(prescale) | ADC_INPUT((ch)) ; /
      ADCCON |= ADC_START; /
 }while(0)


static irqreturn_t adcdone_int_handler(int irq, void *dev_id)
{
     if (OwnADC)      

     {          //能够执行AD转换
           adc_data = ADCDAT0 & 0x3ff;                 //读取AD转换结果,存入变量adc_data

           ev_adc = 1;
           wake_up_interruptible(&adcdev.wait);      //唤醒等待队列
      }

      return IRQ_HANDLED;
}

 

static ssize_t s3c2410_adc_read(struct file *filp, char *buffer, size_t count, loff_t *ppos)
{
      char str[20];
      int value;
      size_t len;


      if (down_trylock(&ADC_LOCK) == 0)

      {     //信号量减1,若信号量非负,则成功返回0,否则立即返回一个非0值,永远不会休眠。
             OwnADC = 1;          //通知中断处理函数获得ADC使用权     
             START_ADC_AIN(adcdev.channel, adcdev.prescale);  //开始AD转换
             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);

             value = adc_data;

             OwnADC = 0;
             up(&ADC_LOCK);       //释放信号量(信号量加1)
      }

      else

      {
             value = -1;
      }

 

      len = sprintf(str, "%d/n", value);     //把AD转换值(int)存入str(char),并返回长度
      if (count >= len)

      {
             int r = copy_to_user(buffer, str, len);    //传送数据到用户空间(应用层)
             return r ? r : len;
      }

      else

      {
             return -EINVAL;
      }
}

 

static int s3c2410_adc_open(struct inode *inode, struct file *filp)
{
      init_waitqueue_head(&(adcdev.wait));   //注册等待队列

      adcdev.channel=0;                              //默认通道0
      adcdev.prescale=0xff;                          //默认预分频值255

      DPRINTK( "adc opened/n");                  
      return 0;
}

 

static int s3c2410_adc_release(struct inode *inode, struct file *filp)
{
      DPRINTK( "adc closed/n");
      return 0;
}


static struct file_operations dev_fops =

{              
      owner: THIS_MODULE,
      open: s3c2410_adc_open,
      read: s3c2410_adc_read, 
      release: s3c2410_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");     //获取AD时钟
      if (!adc_clock)

      {
            printk(KERN_ERR "failed to get adc clock source/n");
            return -ENOENT;
      }
      clk_enable(adc_clock);                     //使能时钟
 
 /* normal ADC */
      ADCTSC = 0;                                  //禁止触摸屏,使能AD转换

      ret = request_irq(IRQ_ADC, adcdone_int_handler, IRQF_SHARED, DEVICE_NAME, &adcdev);                                            //申请AD中断,AD转换结束触发中断
      if (ret)

      {
            iounmap(base_addr);                //AD中断申请失败,释放内存
            return ret;
      }

 

      ret = misc_register(&misc);

      printk (DEVICE_NAME"/tinitialized/n");
      return ret;
}

 

static void __exit dev_exit(void)
{
      free_irq(IRQ_ADC, &adcdev);            //释放中断
      iounmap(base_addr);                       //释放内存

      if (adc_clock)

      {
            clk_disable(adc_clock);               //禁止ADC时钟
            clk_put(adc_clock);
            adc_clock = NULL;
      }

      misc_deregister(&misc);                  
}

 

EXPORT_SYMBOL(ADC_LOCK);                 //提供外部函数调用互斥量ADC_LOCK
module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("FriendlyARM Inc.");

 

注意问题:

1.为什么需要使用互斥量

   因为在S3C2440上,AD转换和触摸屏控制器使用同一个ADC硬件资源,所以需要通过互斥信号量来避免竞态的发生。

 

2.物理地址映射问题

   base_addr=ioremap(S3C2410_PA_ADC,0x20);    

   其原型是:void *ioremap(unsigned long offset, unsigned long size);

   此函数所要完成的功能是把物理地址映射到虚拟的内核空间去。参数offset可以理解为基地址,而size则是要映射的长度。程序中完成的功能就是,把物理地址S3C2410_PA_ADC映射到内核空间base_addr上,长度为20字节。其中S3C2410_PA_ADC在.../mach-s3c2410/include/mach 中定义,是寄存器的ADCCON的地址(0x58000000)。

    iounmap(base_addr);

    释放内存空间,与映射成对使用。

 

 

你可能感兴趣的:(c,struct,Module,File,null,buffer)