Chinaunix首页 | 论坛 | 博客
  • 博客访问: 487297
  • 博文数量: 63
  • 博客积分: 1187
  • 博客等级: 少尉
  • 技术积分: 706
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-05 16:53
个人简介

Must Be

文章分类

全部博文(63)

文章存档

2019年(1)

2017年(4)

2016年(6)

2015年(2)

2014年(1)

2013年(3)

2012年(10)

2011年(36)

我的朋友

分类: 嵌入式

2012-03-30 13:14:46

经典字符程序,对一些自己不太懂的地方稍微分析了一下

驱动程序


点击(此处)折叠或打开

  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/fs.h>
  4. #include <linux/init.h>
  5. #include <linux/delay.h>
  6. #include <asm/irq.h>
  7. #include <linux/interrupt.h>
  8. #include <asm/uaccess.h>
  9. #include <asm/arch/regs-gpio.h>
  10. #include <asm/hardware.h>

  11. #define DEVICE_NAME "buttons" /* 加载模式后,执行”cat /proc/devices”命令看到的设备名称 */
  12. #define BUTTON_MAJOR 232 /* 主设备号 */

  13. struct button_irq_desc {
  14.     int irq;
  15.     unsigned long flags;
  16.     char *name;
  17. };

  18. /* 用来指定按键所用的外部中断引脚及中断触发方式, 名字 */
  19. static struct button_irq_desc button_irqs [] = {
  20.     {IRQ_EINT19, IRQF_TRIGGER_FALLING, "KEY1"}, /* K1 */
  21.     {IRQ_EINT11, IRQF_TRIGGER_FALLING, "KEY2"}, /* K2 */
  22.     {IRQ_EINT2, IRQF_TRIGGER_FALLING, "KEY3"}, /* K3 */
  23.     {IRQ_EINT0, IRQF_TRIGGER_FALLING, "KEY4"}, /* K4 */
  24. };

  25. /* 按键被按下的次数(准确地说,是发生中断的次数) */
  26. static volatile int press_cnt [] = {0, 0, 0, 0};

  27. /* 等待队列:
  28. * 当没有按键被按下时,如果有进程调用s3c24xx_buttons_read函数,
  29. * 它将休眠
  30. */
  31. static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

  32. /* 中断事件标志, 中断服务程序将它置1,s3c24xx_buttons_read将它清0 */
  33. static volatile int ev_press = 0;


  34. static irqreturn_t buttons_interrupt(int irq, void *dev_id)
  35. {
  36.     volatile int *press_cnt = (volatile int *)dev_id;
  37.     
  38.     *press_cnt = *press_cnt + 1; /* 按键计数加1 */
  39.     ev_press = 1; /* 表示中断发生了 */
  40.     wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */
  41.     
  42.     return IRQ_RETVAL(IRQ_HANDLED);
  43. }
  44. //根据《LINUX设备驱动程序的说法,volatile int *press_cnt = (volatile int *)dev_id;我们会为dev_id传递一个纸箱自己设备的数据结构指针,这样,一个管理若干同样设备的驱动程序在中断处理例程中不需要做额外的代码,就可以找出那个设备产生了中断时间。
  45. 详细可以参考http://linux.chinaunix.net/techdoc/net/2008/12/16/1053047.shtml

  46. /* 应用程序对设备文件/dev/buttons执行open(...)时,
  47. * 就会调用s3c24xx_buttons_open函数
  48. *
  49. */
  50. static int s3c24xx_buttons_open(struct inode *inode, struct file *file)
  51. {
  52.     int i;
  53.     int err;
  54.     
  55.     for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) {
  56.         // 注册中断处理函数
  57.         err = request_irq(button_irqs[i].irq, buttons_interrupt, button_irqs[i].flags,
  58.                           button_irqs[i].name, (void *)&press_cnt[i]);
  59.         if (err)
  60.             break;
  61.     }
  62.     if (err) {
  63.         // 释放已经注册的中断
  64.         i--;
  65.         for (; i >= 0; i--)
  66.             free_irq(button_irqs[i].irq, (void *)&press_cnt[i]);
  67.         return -EBUSY;
  68.     }
  69.     
  70.     return 0;
  71. }
  72. /*request_irq是最核心的函数,函数原型int request_irq(unsigned int irq,irqreturn_t(*handler)(int, void *,sturct pt_reg *),const char *dev_name,void *dev_id);
  73. Unsigned int irq
  74. 这是申请的中断号。
  75. irqreturn_t(*handler)(int, void *,sturct pt_reg *)
  76. 这是要安装中断处理函数指针。
  77. Void *dev_id
  78. 这个指针用于共享的中断信号线。它是唯一的标识符,在中断信号线空闲时可以使用它,驱动程序也可以使用它指向驱动程序自己的私数据区。
  79. Flags
  80. 标志,详细可以参考《LINUX设备驱动程序》

  81. 在这条程序里面irqreturn_t(*handler)(int, void *,sturct pt_reg *)
  82. /* 应用程序对设备文件/dev/buttons执行close(...)时,
  83. * 就会调用s3c24xx_buttons_close函数
  84. */
  85. static int s3c24xx_buttons_close(struct inode *inode, struct file *file)
  86. {
  87.     int i;
  88.     
  89.     for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) {
  90.         // 释放已经注册的中断
  91.         free_irq(button_irqs[i].irq, (void *)&press_cnt[i]);
  92.     }
  93.     return 0;
  94. }


  95. /* 应用程序对设备文件/dev/buttons执行read(...)时,
  96. * 就会调用s3c24xx_buttons_read函数
  97. */
  98. static int s3c24xx_buttons_read(struct file *filp, char __user *buff,
  99.                                          size_t count, loff_t *offp)
  100. {
  101.     unsigned long err;
  102.     
  103.     /* 如果ev_press等于0,休眠 */
  104.     wait_event_interruptible(button_waitq, ev_press);

  105.     /* 执行到这里时,ev_press等于1,将它清0 */
  106.     ev_press = 0;

  107.     /* 将按键状态复制给用户,并清0 */
  108.     err = copy_to_user(buff, (const void *)press_cnt, min(sizeof(press_cnt), count));
  109.     memset((void *)press_cnt, 0, sizeof(press_cnt));

  110.     return err ? -EFAULT : 0;
  111. }

  112. //read函数的核心当然是copy_to_user,将驱动的内容传递到用户空间,为内核、驱动跟系统搭建了一个交流平台buf,通过这个位数不多的功用空间进行交流

  113. /* 这个结构是字符设备驱动程序的核心
  114. * 当应用程序操作设备文件时所调用的open、read、write等函数,
  115. * 最终会调用这个结构中的对应函数
  116. */
  117. static struct file_operations s3c24xx_buttons_fops = {
  118.     .owner = THIS_MODULE, /* 这是一个宏,指向编译模块时自动创建的__this_module变量 */
  119.     .open = s3c24xx_buttons_open,
  120.     .release = s3c24xx_buttons_close,
  121.     .read = s3c24xx_buttons_read,
  122. };

  123. /*
  124. * 执行“insmod s3c24xx_buttons.ko”命令时就会调用这个函数
  125. */
  126. static int __init s3c24xx_buttons_init(void)
  127. {
  128.     int ret;

  129.     /* 注册字符设备驱动程序
  130.      * 参数为主设备号、设备名字、file_operations结构;
  131.      * 这样,主设备号就和具体的file_operations结构联系起来了,
  132.      * 操作主设备为BUTTON_MAJOR的设备文件时,就会调用s3c24xx_buttons_fops中的相关成员函数
  133.      * BUTTON_MAJOR可以设为0,表示由内核自动分配主设备号
  134.      * 建立设备文件
  135.      */
  136.     ret = register_chrdev(BUTTON_MAJOR, DEVICE_NAME, &s3c24xx_buttons_fops);
  137.     if (ret < 0) {
  138.       printk(DEVICE_NAME " can't register major number\n");
  139.       return ret;
  140.     }
  141.     devfs_mk_cdev(MKDEV(BUTTON_MAJOR, 0), S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP, DEVICE_NAME);
  142.     printk(DEVICE_NAME " initialized\n");
  143.     return 0;
  144. }

  145. /*
  146. * 执行”rmmod s3c24xx_buttons.ko”命令时就会调用这个函数
  147. */
  148. static void __exit s3c24xx_buttons_exit(void)
  149. {
  150.     /* 卸载驱动程序 */
  151.     devfs_remove(DEVICE_NAME);
  152.     unregister_chrdev(BUTTON_MAJOR, DEVICE_NAME);
  153. }

  154. /* 这两行指定驱动程序的初始化函数和卸载函数 */
  155. module_init(s3c24xx_buttons_init);
  156. module_exit(s3c24xx_buttons_exit);



应用程序
#include
#include
#include
#include

int main(int argc, char **argv)
{
    int i;
    int ret;
    int fd;
    int press_cnt[4];
   
    fd = open("/dev/buttons", 0); // 打开设备
    if (fd < 0) {
        printf("Can't open /dev/buttons\n");
        return -1;
    }

    // 这是个无限循环,进程有可能在read函数中休眠,当有按键被按下时,它才返回
    while (1) {
        // 读出按键被按下的次数
        ret = read(fd, press_cnt, sizeof(press_cnt));
        if (ret < 0) {
            printf("read err!\n");
            continue;
        }

        for (i = 0; i < sizeof(press_cnt)/sizeof(press_cnt[0]); i++) {
            // 如果被按下的次数不为0,打印出来
            if (press_cnt[i])
                printf("K%d has been pressed %d times!\n", i+1, press_cnt[i]);
        }
    }
   
    close(fd);
    return 0;

阅读(2057) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~