zoukankan      html  css  js  c++  java
  • 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结构体中注册的文件操作接口进行操作。

    [cpp] view plain?
    1. struct miscdevice  {  
    2.     int minor;              //次设备号,如果设置为MISC_DYNAMIC_MINOR则系统自动分配  
    3.     const char *name;       //设备名  
    4.     const struct file_operations *fops;     //操作函数  
    5.     struct list_head list;  
    6.     struct device *parent;  
    7.     struct device *this_device;  
    8. };  
    dev_init入口函数分析:

    [cpp] view plain?
    1. static int __init dev_init(void)  
    2. {  
    3.     int ret;  
    4.   
    5.     base_addr=ioremap(S3C2410_PA_ADC,0x20);  
    6.     if (base_addr == NULL)  
    7.     {  
    8.         printk(KERN_ERR "failed to remap register block ");  
    9.         return -ENOMEM;  
    10.     }  
    11.   
    12.     adc_clock = clk_get(NULL, "adc");  
    13.     if (!adc_clock)  
    14.     {  
    15.         printk(KERN_ERR "failed to get adc clock source ");  
    16.         return -ENOENT;  
    17.     }  
    18.     clk_enable(adc_clock);  
    19.       
    20.     ADCTSC = 0;  
    21.   
    22.     ret = request_irq(IRQ_ADC, adcdone_int_handler, IRQF_SHARED, DEVICE_NAME, &adcdev);  
    23.     if (ret)  
    24.     {  
    25.         iounmap(base_addr);  
    26.         return ret;  
    27.     }  
    28.   
    29.     ret = misc_register(&misc);  
    30.   
    31.     printk (DEVICE_NAME" initialized ");  
    32.     return ret;  
    33. }  
    首先是映射ADC寄存器地址将其转换为虚拟地址,然后获得ADC时钟并使能ADC时钟,接着申请ADC中断,其中断处理函数为

    adcdone_int_handler,而flags为IRQF_SHARED,即共享中断,因为触摸屏里也要申请ADC中断,最后注册一个混杂设备。

    当应用程序open ("/dev/adc",...)时,就会调用到驱动里面的open函数,那么我们来看看open函数做了什么?

    [cpp] view plain?
    1. static int tq2440_adc_open(struct inode *inode, struct file *filp)  
    2. {  
    3.     /* 初始化等待队列头 */  
    4.     init_waitqueue_head(&(adcdev.wait));  
    5.   
    6.     /* 开发板上ADC的通道2连接着一个电位器 */  
    7.     adcdev.channel=2;   //设置ADC的通道  
    8.     adcdev.prescale=0xff;  
    9.   
    10.     DPRINTK( "ADC opened ");  
    11.     return 0;  
    12. }  
    很简单,先初始化一个等待队列头,因为入口函数里既然有申请ADC中断,那么肯定要使用等待队列,接着设置ADC通道,因为TQ2440的ADC输入通道默认是2,设置预分频值为0xff。

    当应用程序read时,就会调用到驱动里面的read函数,那么我们来看看read函数做了些什么?

    [cpp] view plain?
    1. static ssize_t tq2440_adc_read(struct file *filp, char *buffer, size_t count, loff_t *ppos)  
    2. {  
    3.     char str[20];  
    4.     int value;  
    5.     size_t len;  
    6.   
    7.     /* 尝试获得ADC_LOCK信号量,如果能够立刻获得,它就获得信号量并返回0  
    8.      * 否则,返回非零,它不会导致调用者睡眠,可以在中断上下文使用 
    9.      */  
    10.     if (down_trylock(&ADC_LOCK) == 0)  
    11.     {  
    12.         /* 表示A/D转换器资源可用 */  
    13.         ADC_enable = 1;  
    14.   
    15.         /* 使能预分频,选择ADC通道,最后启动ADC转换*/  
    16.         START_ADC_AIN(adcdev.channel, adcdev.prescale);  
    17.   
    18.         /* 等待事件,当ev_adc = 0时,进程被阻塞,直到ev_adc>0 */  
    19.         wait_event_interruptible(adcdev.wait, ev_adc);  
    20.   
    21.         ev_adc = 0;  
    22.   
    23.         DPRINTK("AIN[%d] = 0x%04x, %d ", adcdev.channel, adc_data, ((ADCCON & 0x80) ? 1:0));  
    24.   
    25.         /* 将在ADC中断处理函数读取的ADC转换结果赋值给value */  
    26.         value = adc_data;  
    27.         sprintf(str,"%5d", adc_data);  
    28.         copy_to_user(buffer, (char *)&adc_data, sizeof(adc_data));  
    29.   
    30.         ADC_enable = 0;  
    31.         up(&ADC_LOCK);  
    32.     }  
    33.     else  
    34.     {  
    35.         /* 如果A/D转换器资源不可用,将value赋值为-1 */  
    36.         value = -1;  
    37.     }  
    38.   
    39.     /* 将ADC转换结果输出到str数组里,以便传给应用空间 */  
    40.     len = sprintf(str, "%d ", value);  
    41.     if (count >= len)  
    42.     {  
    43.         /* 从str数组里拷贝len字节的数据到buffer,即将ADC转换数据传给应用空间 */  
    44.         int r = copy_to_user(buffer, str, len);  
    45.         return r ? r : len;  
    46.     }  
    47.     else  
    48.     {  
    49.         return -EINVAL;  
    50.     }  
    51. }  
    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

    [cpp] view plain?
    1. /* ADC中断处理函数 */  
    2. static irqreturn_t adcdone_int_handler(int irq, void *dev_id)  
    3. {  
    4.     /* A/D转换器资源可用 */  
    5.     if (ADC_enable)  
    6.     {  
    7.         /* 读ADC转换结果数据 */  
    8.         adc_data = ADCDAT0 & 0x3ff;  
    9.   
    10.         /* 唤醒标志位,作为wait_event_interruptible的唤醒条件 */  
    11.         ev_adc = 1;  
    12.         wake_up_interruptible(&adcdev.wait);  
    13.     }  
    14.     return IRQ_HANDLED;  
    15. }  
    当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驱动参考源码:

    [cpp] view plain?
    1. /************************************* 
    2.  
    3. NAME:EmbedSky_adc.c 
    4. COPYRIGHT:www.embedsky.net 
    5.  
    6. *************************************/  
    7.   
    8. #include <linux/errno.h>  
    9. #include <linux/kernel.h>  
    10. #include <linux/module.h>  
    11. #include <linux/slab.h>  
    12. #include <linux/input.h>  
    13. #include <linux/init.h>  
    14. #include <linux/serio.h>  
    15. #include <linux/delay.h>  
    16. #include <linux/clk.h>  
    17. #include <asm/io.h>  
    18. #include <asm/irq.h>  
    19. #include <asm/uaccess.h>  
    20. #include <mach/regs-clock.h>  
    21. #include <plat/regs-timer.h>  
    22.        
    23. #include <plat/regs-adc.h>  
    24. #include <mach/regs-gpio.h>  
    25. #include <linux/cdev.h>  
    26. #include <linux/miscdevice.h>  
    27.   
    28. #include "tq2440_adc.h"  
    29.   
    30. #undef DEBUG  
    31. //#define DEBUG  
    32. #ifdef DEBUG  
    33. #define DPRINTK(x...) {printk(KERN_DEBUG "EmbedSky_adc: " x);}  
    34. #else  
    35. #define DPRINTK(x...) (void)(0)  
    36. #endif  
    37.   
    38. #define DEVICE_NAME "adc"       /* 设备节点: /dev/adc */  
    39.   
    40. static void __iomem *base_addr;  
    41.   
    42. typedef struct  
    43. {  
    44.     wait_queue_head_t wait;     /* 定义等待队列头 */  
    45.     int channel;  
    46.     int prescale;  
    47. }ADC_DEV;  
    48.   
    49. DECLARE_MUTEX(ADC_LOCK);    /* 定义并初始化信号量,并初始化为1 */  
    50. static int ADC_enable = 0;          /* A/D转换器资是否可用标志位 */  
    51.   
    52. static ADC_DEV adcdev;              /* 用于表示ADC设备 */  
    53. static volatile int ev_adc = 0;     /* 作为wait_event_interruptible的唤醒条件 */  
    54. static int adc_data;  
    55.   
    56. static struct clk   *adc_clock;  
    57.   
    58. #define ADCCON      (*(volatile unsigned long *)(base_addr + S3C2410_ADCCON))   //ADC control  
    59. #define ADCTSC      (*(volatile unsigned long *)(base_addr + S3C2410_ADCTSC))   //ADC touch screen control  
    60. #define ADCDLY      (*(volatile unsigned long *)(base_addr + S3C2410_ADCDLY))   //ADC start or Interval Delay  
    61. #define ADCDAT0     (*(volatile unsigned long *)(base_addr + S3C2410_ADCDAT0))  //ADC conversion data 0  
    62. #define ADCDAT1     (*(volatile unsigned long *)(base_addr + S3C2410_ADCDAT1))  //ADC conversion data 1  
    63. #define ADCUPDN     (*(volatile unsigned long *)(base_addr + 0x14))         //Stylus Up/Down interrupt status  
    64.   
    65. #define PRESCALE_DIS    (0 << 14)  
    66. #define PRESCALE_EN     (1 << 14)  
    67. #define PRSCVL(x)       ((x) << 6)  
    68. #define ADC_INPUT(x)    ((x) << 3)  
    69. #define ADC_START       (1 << 0)  
    70. #define ADC_ENDCVT      (1 << 15)  
    71.   
    72.   
    73. /* 使能预分频,选择ADC通道,最后启动ADC转换*/  
    74. #define START_ADC_AIN(ch, prescale)   
    75.     do{     ADCCON = PRESCALE_EN | PRSCVL(prescale) | ADC_INPUT((ch)) ;   
    76.         ADCCON |= ADC_START;   
    77.     }while(0)  
    78.   
    79.   
    80. /* ADC中断处理函数 */  
    81. static irqreturn_t adcdone_int_handler(int irq, void *dev_id)  
    82. {  
    83.     /* A/D转换器资源可用 */  
    84.     if (ADC_enable)  
    85.     {  
    86.         /* 读ADC转换结果数据 */  
    87.         adc_data = ADCDAT0 & 0x3ff;  
    88.   
    89.         /* 唤醒标志位,作为wait_event_interruptible的唤醒条件 */  
    90.         ev_adc = 1;  
    91.         wake_up_interruptible(&adcdev.wait);  
    92.     }  
    93.     return IRQ_HANDLED;  
    94. }  
    95.   
    96. static ssize_t tq2440_adc_read(struct file *filp, char *buffer, size_t count, loff_t *ppos)  
    97. {  
    98.     char str[20];  
    99.     int value;  
    100.     size_t len;  
    101.   
    102.     /* 尝试获得ADC_LOCK信号量,如果能够立刻获得,它就获得信号量并返回0  
    103.      * 否则,返回非零,它不会导致调用者睡眠,可以在中断上下文使用 
    104.      */  
    105.     if (down_trylock(&ADC_LOCK) == 0)  
    106.     {  
    107.         /* 表示A/D转换器资源可用 */  
    108.         ADC_enable = 1;  
    109.   
    110.         /* 使能预分频,选择ADC通道,最后启动ADC转换*/  
    111.         START_ADC_AIN(adcdev.channel, adcdev.prescale);  
    112.   
    113.         /* 等待事件,当ev_adc = 0时,进程被阻塞,直到ev_adc>0 */  
    114.         wait_event_interruptible(adcdev.wait, ev_adc);  
    115.   
    116.         ev_adc = 0;  
    117.   
    118.         DPRINTK("AIN[%d] = 0x%04x, %d ", adcdev.channel, adc_data, ((ADCCON & 0x80) ? 1:0));  
    119.   
    120.         /* 将在ADC中断处理函数读取的ADC转换结果赋值给value */  
    121.         value = adc_data;  
    122.         sprintf(str,"%5d", adc_data);  
    123.         copy_to_user(buffer, (char *)&adc_data, sizeof(adc_data));  
    124.   
    125.         ADC_enable = 0;  
    126.         up(&ADC_LOCK);  
    127.     }  
    128.     else  
    129.     {  
    130.         /* 如果A/D转换器资源不可用,将value赋值为-1 */  
    131.         value = -1;  
    132.     }  
    133.   
    134.     /* 将ADC转换结果输出到str数组里,以便传给应用空间 */  
    135.     len = sprintf(str, "%d ", value);  
    136.     if (count >= len)  
    137.     {  
    138.         /* 从str数组里拷贝len字节的数据到buffer,即将ADC转换数据传给应用空间 */  
    139.         int r = copy_to_user(buffer, str, len);  
    140.         return r ? r : len;  
    141.     }  
    142.     else  
    143.     {  
    144.         return -EINVAL;  
    145.     }  
    146. }  
    147.   
    148. static int tq2440_adc_open(struct inode *inode, struct file *filp)  
    149. {  
    150.     /* 初始化等待队列头 */  
    151.     init_waitqueue_head(&(adcdev.wait));  
    152.   
    153.     /* 开发板上ADC的通道2连接着一个电位器 */  
    154.     adcdev.channel=2;   //设置ADC的通道  
    155.     adcdev.prescale=0xff;  
    156.   
    157.     DPRINTK( "ADC opened ");  
    158.     return 0;  
    159. }  
    160.   
    161. static int tq2440_adc_release(struct inode *inode, struct file *filp)  
    162. {  
    163.     DPRINTK( "ADC closed ");  
    164.     return 0;  
    165. }  
    166.   
    167.   
    168. static struct file_operations dev_fops = {  
    169.     owner:  THIS_MODULE,  
    170.     open:   tq2440_adc_open,  
    171.     read:   tq2440_adc_read,      
    172.     release:    tq2440_adc_release,  
    173. };  
    174.   
    175. static struct miscdevice misc = {  
    176.     .minor = MISC_DYNAMIC_MINOR,  
    177.     .name = DEVICE_NAME,  
    178.     .fops = &dev_fops,  
    179. };  
    180.   
    181. static int __init dev_init(void)  
    182. {  
    183.     int ret;  
    184.   
    185.     base_addr=ioremap(S3C2410_PA_ADC,0x20);  
    186.     if (base_addr == NULL)  
    187.     {  
    188.         printk(KERN_ERR "failed to remap register block ");  
    189.         return -ENOMEM;  
    190.     }  
    191.   
    192.     adc_clock = clk_get(NULL, "adc");  
    193.     if (!adc_clock)  
    194.     {  
    195.         printk(KERN_ERR "failed to get adc clock source ");  
    196.         return -ENOENT;  
    197.     }  
    198.     clk_enable(adc_clock);  
    199.       
    200.     ADCTSC = 0;  
    201.   
    202.     ret = request_irq(IRQ_ADC, adcdone_int_handler, IRQF_SHARED, DEVICE_NAME, &adcdev);  
    203.     if (ret)  
    204.     {  
    205.         iounmap(base_addr);  
    206.         return ret;  
    207.     }  
    208.   
    209.     ret = misc_register(&misc);  
    210.   
    211.     printk (DEVICE_NAME" initialized ");  
    212.     return ret;  
    213. }  
    214.   
    215. static void __exit dev_exit(void)  
    216. {  
    217.     free_irq(IRQ_ADC, &adcdev);  
    218.     iounmap(base_addr);  
    219.   
    220.     if (adc_clock)  
    221.     {  
    222.         clk_disable(adc_clock);  
    223.         clk_put(adc_clock);  
    224.         adc_clock = NULL;  
    225.     }  
    226.   
    227.     misc_deregister(&misc);  
    228. }  
    229.   
    230. EXPORT_SYMBOL(ADC_LOCK);  
    231. module_init(dev_init);  
    232. module_exit(dev_exit);  
    233.   
    234. MODULE_LICENSE("GPL");  
    235. MODULE_AUTHOR("www.embedsky.net");  
    236. MODULE_DESCRIPTION("ADC Drivers for EmbedSky SKY2440/TQ2440 Board and support touch");  
    ADC应用测试参考源码:

    [cpp] view plain?
    1. /************************************* 
    2.  
    3. NAME:EmbedSky_adc.c 
    4. COPYRIGHT:www.embedsky.net 
    5.  
    6. *************************************/  
    7.   
    8. #include <stdio.h>  
    9. #include <unistd.h>  
    10. #include <stdlib.h>  
    11. #include <sys/types.h>  
    12. #include <sys/stat.h>  
    13. #include <sys/ioctl.h>  
    14. #include <fcntl.h>  
    15. #include <linux/fs.h>  
    16. #include <errno.h>  
    17. #include <string.h>  
    18.   
    19. int main(void)  
    20. {  
    21.     int fd ;  
    22.     char temp = 1;  
    23.   
    24.     fd = open("/dev/adc", 0);  
    25.     if (fd < 0)  
    26.     {  
    27.         perror("open ADC device !");  
    28.         exit(1);  
    29.     }  
    30.       
    31.     for( ; ; )  
    32.     {  
    33.         char buffer[30];  
    34.         int len ;  
    35.   
    36.         len = read(fd, buffer, sizeof buffer -1);  
    37.         if (len > 0)  
    38.         {  
    39.             buffer[len] = '';  
    40.             int value;  
    41.             sscanf(buffer, "%d", &value);  
    42.             printf("ADC Value: %d ", value);  
    43.         }  
    44.         else  
    45.         {  
    46.             perror("read ADC device !");  
    47.             exit(1);  
    48.         }  
    49.         sleep(1);  
    50.     }  
    51. adcstop:      
    52.     close(fd);  
    53. }  
    测试结果:

    [cpp] view plain?
    1. [WJ2440]# ./adc_test   
    2. ADC Value: 693  
    3. ADC Value: 695  
    4. ADC Value: 694  
    5. ADC Value: 695  
    6. ADC Value: 702  
    7. ADC Value: 740  
    8. ADC Value: 768  
    9. ADC Value: 775  
    10. ADC Value: 820  
    11. ADC Value: 844  
    12. ADC Value: 887  
    13. ADC Value: 937  
    14. ADC Value: 978  
    15. ADC Value: 1000  
    16. ADC Value: 1023  
    17. ADC Value: 1023  
    18. ADC Value: 1023  
  • 相关阅读:
    Python基础之文件、目录
    Python基础知识之基本类型、循环
    Python基础知识之函数、模块
    Python基础之小知识要点
    【Android】ADB常用指令与logcat日志(转)
    Android 中的 Service 全面总结 (转)
    Android实现双进程守护 (转)
    Android adb常见问题整理(转)
    Android代码内存优化建议-OnTrimMemory优化
    优化Android应用内存的若干方法
  • 原文地址:https://www.cnblogs.com/alan666/p/8312408.html
Copyright © 2011-2022 走看看