tao180539 发表于 2022-4-16 18:41

mini2440等待队列方式的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 "char/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_test"

static void __iomem *base_addr;

typedef struct {
      wait_queue_head_t wait_queue_finish_ADC_conver;//定义等待队列头,生成一个等待队列头
      int channel;
      int prescale;
}ADC_DEV;

//DECLARE_MUTEX(ADC_LOCK_mutex);//静态定义Mutex(互斥量),Mutex属于sleep-waiting类型的锁。
static DEFINE_MUTEX(ADC_LOCK_mutex);

//动态定义mutex_init(&mutex);
//struct mutex ADC_LOCK_mutex;

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)
/*
#define START_ADC_AIN(ch, prescale) \
      do{ \
                ADCCON = PRESCALE_EN | PRSCVL(prescale) | ADC_INPUT((ch)) ; \
                ADCCON |= ADC_START; \
      }while(0)
*/
static voidSTART_ADC_AIN(int ch, int prescale)
          {
                ADCCON = (PRESCALE_EN) | PRSCVL(prescale) | ADC_INPUT(ch);
                ADCCON |= ADC_START;
          }

static irqreturn_t adcdone_int_handler(int irq, void *dev_id)
{
      if (OwnADC)
      {
                adc_data = ADCDAT0 & 0x3ff;

                ev_adc = 1;
                wake_up_interruptible(&adcdev.wait_queue_finish_ADC_conver);
      }

      return IRQ_HANDLED;
}

static ssize_t s3c2410_adc_read(struct file *filp, int *buffer, size_t count, loff_t *ppos)
{
      
      int value;
      int ret;
      mutex_lock(&ADC_LOCK_mutex);//为指定的mutex上锁,如果不可用则睡眠
      {
                OwnADC = 1;
                START_ADC_AIN(adcdev.channel, adcdev.prescale);
                /*
               wait_event_interruptible_timeout(wq, condition, timeout)
                * 函数作用:~睡眠~,直到condition为真,或timeout超时;
                * @wq: 要等待的等待队列
                * @condition: 等待事件发生的条件(一个C表达式 )
                * @timeout: 超时时间
                */

                wait_event_interruptible(adcdev.wait_queue_finish_ADC_conver, ev_adc);
                adc_data = ADCDAT0 & 0x3ff;
                ev_adc = 0;
                value = adc_data;
                OwnADC = 0;               
      }
      mutex_unlock(&ADC_LOCK_mutex); //为指定的mutex解锁
      ret = copy_to_user(buffer, &value, sizeof(value));
      return ret;
      
}

static ssize_t s3c2410_adc_open(struct inode *inode, struct file *filp)
{
      init_waitqueue_head(&(adcdev.wait_queue_finish_ADC_conver));//初始化等待队列

      adcdev.channel=0;
      adcdev.prescale=0xff;

      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 ADC_dev_init(void)
{
      int ret;

      base_addr=ioremap(S3C2410_PA_ADC,0x20);//0x20长度是字节长度,不是寄存器个数
      if (base_addr == NULL)
         {
                printk(KERN_ERR "Failed to remap ADC register block\n");
                return -ENOMEM;
         }

      //clk_get从一个时钟list链表中以字符id名称来查找一个时钟clk结构体并且返回,最后调用clk.enable(),来时能对应的外设时钟源。
      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);
      
      /* normal ADC */
      ADCTSC = 0;
      /*
         参数1:中断线号(多个设备共享同一个中断线号)
         参数2:中断处理程序函数指针
         参数3:标志掩码(SA_INTERRUPT, SA_SAMPLE_RANDOM, SA_SHIRQ)
         参数4:用于参数3为SA_SHIRQ(共享中断线)的时候,其他为NULL,多个设备共享同一个中断线号,当中断产生的时候到底是那一个设备            产生的中断呢,这个就取决于第四个参数dev_id
      */

      ret = request_irq(IRQ_ADC, adcdone_int_handler, IRQF_SHARED, DEVICE_NAME, &adcdev);
      if (ret) //非0代表有错误
          {
                iounmap(base_addr);
                printk ("request_irq return error");
                return ret;
          }

      ret = misc_register(&misc);

      printk(DEVICE_NAME"\ finish initialized\n");
      //mutex_init(&ADC_LOCK_mutex);//动态定义mutex类型的时候应用此函数初始化

      return ret;
}

static void __exit ADC_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_mutex);
module_init(ADC_dev_init);
module_exit(ADC_dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("FriendlyARM Inc.");
页: [1]
查看完整版本: mini2440等待队列方式的ADC驱动