button_drv.c驱动文件:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <asm/io.h>                //含有iomap函数iounmap函数
#include <asm/uaccess.h>              //含有copy_from_user函数
#include <linux/device.h>         //含有类相关的处理函数
#include <asm/arch/regs-gpio.h>     //含有S3C2410_GPF0等相关的
#include <linux/irq.h>          //含有IRQ_HANDLED\IRQ_TYPE_EDGE_RISING
#include <asm-arm/irq.h>          //含有IRQT_BOTHEDGE触发类型
#include <linux/interrupt.h>       //含有request_irq、free_irq函数
#include <linux/delay.h>
#include <asm/hardware.h>
#include <linux/poll.h>

#define DRVIER_NAME "button_drv"
#define DEVICE_NAME "button_dev"

int major;

int keyVal;

struct class *button_class;
struct class_device *button_class_device;

volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;
volatile unsigned long *gpgcon = NULL;
volatile unsigned long *gpgdat = NULL;

int ev_press = 0;
DECLARE_WAIT_QUEUE_HEAD(button_waitq);   //注册等待队列
DECLARE_MUTEX(button_lock);          //定义互斥锁
struct fasync_struct *button_fasync;   //定义fasync_struct结构体
struct timer_list timer_button;       //定义一个定时器

struct pin_desc {
  unsigned int pin;
  unsigned int key_val;
};

struct pin_desc pins_desc[4] = {
  {S3C2410_GPF0, 0x01},
  {S3C2410_GPF2, 0x02},
  {S3C2410_GPG3, 0x03},
  {S3C2410_GPG11, 0x04},
};

struct pin_desc *pinss_desc = NULL;

irqreturn_t button_irq(int irq, void *dev_id)
{
  pinss_desc = (struct pin_desc *)dev_id;
  mod_timer(&timer_button, jiffies+HZ/100);    //10ms之后调用定时器处理函数
  return IRQ_HANDLED;
}

int button_drv_open(struct inode *inode, struct file *file)
{
  int ret;
  if(file->f_flags&O_NONBLOCK)           //非阻塞方式
  {
    if(down_trylock(&button_lock))         //获取信号量, 若失败则立即返回
    {
      return -EBUSY;
    }
  }
  else                          //阻塞模式
  {
    down(&button_lock);                        //获取信号量, 若失败则该进程立刻进入睡眠状态
  }
  ret = request_irq(IRQ_EINT0, button_irq, IRQT_BOTHEDGE, "S1", &pins_desc[0]);
  if(ret)
  {
    printk("failed 1 button_drv_open \n");
  }
  ret = request_irq(IRQ_EINT2, button_irq, IRQT_BOTHEDGE, "S2", &pins_desc[1]);
  if(ret)
  {
    printk("failed 2 button_drv_open \n");
  }
  ret = request_irq(IRQ_EINT11, button_irq, IRQT_BOTHEDGE, "S3", &pins_desc[2]);
  if(ret)
  {
    printk("failed 3 button_drv_open \n");
  }
  ret = request_irq(IRQ_EINT19, button_irq, IRQT_BOTHEDGE, "S4", &pins_desc[3]);
  if(ret)
  {
    printk("failed 4 button_drv_open \n");
  }
  return 0;
}

int button_drv_close(struct inode *inode, struct file *file)
{
  up(&button_lock); //释放信号量
  free_irq(IRQ_EINT0, &pins_desc[0]);
  free_irq(IRQ_EINT2, &pins_desc[1]);
  free_irq(IRQ_EINT11, &pins_desc[2]);
  free_irq(IRQ_EINT19, &pins_desc[3]);
  return 0;
}

int button_drv_read(struct file *file, char __user *userbuf, size_t count, loff_t *off)
{
  int ret;
  if(file->f_flags&O_NONBLOCK)     //若文件是以非阻塞方式读取
  {
    if(!ev_press)                //判断案件是否按下, 没有则直接返回
    {
      keyVal = 0;
      ret = copy_to_user(userbuf, &keyVal, 1);
      if(ret)
      {
        printk("failed 1 button_drv_read \n");
        return -1;
      }
      return -EBUSY;
    }
  }
  else //若文件是以阻塞方式读取
  {
    wait_event_interruptible(button_waitq, ev_press); //若ev_press为0, 则将进程放入button_waitq等到队列中
  }
  ret = copy_to_user(userbuf, &keyVal, 1);
  ev_press = 0;                                        //按键已处理, 可以继续休眠
  if(ret)
  {
    printk("failed 2 button_drv_read \n");
    return -1;
  }
  return 1;
}

int button_drv_fasync(int fd, struct file *file, int on)
{
  int ret;
  ret = fasync_helper(fd, file, on, &button_fasync);
  if(ret<0)
  {
    printk("failed 1 button_drv_fasync \n");
    return ret;
  }
  return 0;
}

void timer_button_timeout(unsigned long data)
{
  unsigned int pin_val;
  if(pinss_desc==NULL)
  {
    return;
  }
  else
  {
    pin_val = s3c2410_gpio_getpin(pinss_desc->pin);
    if(pin_val)
    {
      keyVal = 0x80 | pinss_desc->key_val;
    }
    else
    {
      keyVal = pinss_desc->key_val;
    }
    wake_up_interruptible(&button_waitq);        //唤醒休眠进程
    ev_press = 1;
    kill_fasync(&button_fasync, SIGIO, POLL_IN); //发送信号给进程
  }
}

struct file_operations button_fops = {
  .owner = THIS_MODULE,
  .open = button_drv_open,
  .release = button_drv_close,
  .read = button_drv_read,
  .fasync = button_drv_fasync,
};

int __init button_drv_init(void)
{
  init_timer(&timer_button);                    //初始化一个定时器用于处理按键抖动
  timer_button.expires = 0;                     //定时器的定时时间
  timer_button.function = timer_button_timeout; //定是时间到后的处理函数
  add_timer(&timer_button);                     //将定义的定时器放入定时器链表

  major = register_chrdev(0, DRVIER_NAME, &button_fops); //注册驱动
  if(major<0)
  {
    printk("failed 1 button_drv_init \n");
  }
  button_class = class_create(THIS_MODULE, DEVICE_NAME); //创建设备类
  if(button_class<0)
  {
    printk("failed 2 button_drv_init \n");
  }
  button_class_device = class_device_create(button_class, NULL, MKDEV(major, 0), NULL, DEVICE_NAME); //创建设备文件(设备节点)
  if(button_class_device<0)
  {
    printk("failed 3 button_drv_init \n");
  }
  gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16); //虚拟地址重映射
  gpfdat = gpfcon + 1;
  gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
  gpgdat = gpgcon + 1;
  return 0;
}

void __exit button_drv_exit(void)
{
  unregister_chrdev(major, DEVICE_NAME);
  class_device_unregister(button_class_device);
  class_destroy(button_class);
  iounmap(gpfcon);
  iounmap(gpgcon);
}

module_init(button_drv_init);
module_exit(button_drv_exit);

MODULE_LICENSE("GPL");

Makefile文件:

obj-m += button_drv.o

KERN_DIR = /work/system/linux-2.6.22.6

all:
make -C $(KERN_DIR) M=`pwd` modules
clean:
rm -rf *.o *.ko *.order *.symvers *.mod.c

button_app.c文件:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <signal.h>

static int fd;

void button_signal(int signum)
{
  unsigned char keyVal;
  printf("signal = %d \n", signum);
  read(fd, &keyVal, 1);
  printf("keyVal = 0x%x \n", keyVal);
}

int main(int argc, char **argv)
{
  int oflags;
  char *filename;

  filename = argv[1];
  fd = open(filename, O_RDWR|O_NONBLOCK);   //非阻塞
  if(fd<0)
  {
    printf("can not open \n");
  }
  signal(SIGIO, button_signal);             //注册一个信号, 启动信号驱动机制
  fcntl(fd, F_SETOWN, getpid());            //将本应用程序的进程号告诉给内核,最终使得驱动程序可以成功发送信号给应用程序
  oflags = fcntl(fd, F_GETFL);              //取得当前的状态
  fcntl(fd, F_SETFL, oflags|FASYNC);        //改变fasync标记, 最终会调用到驱动的fasync->fasync_helper
  while(1)
  {
    sleep(1000);
  }
  return 0;
}

编译生成button_drv.ko和button_app文件,运行./button_app /dev/button_dev

Linux 驱动——Button驱动7(Timer)消抖的更多相关文章

  1. Linux 驱动——Button驱动6(mutex、NBLOCK、O_NONBLOCK)互斥信号量、阻塞、非阻塞

    button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/f ...

  2. Linux 驱动——Button驱动5(atomic)原子量

    button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/f ...

  3. Linux 驱动——Button驱动4(fasync)异步通知

    button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/f ...

  4. Linux 驱动——Button驱动3(poll机制)

    button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/f ...

  5. Linux 驱动——Button驱动2

    button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/f ...

  6. Linux 驱动——Button驱动1

    button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/i ...

  7. 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验二:按键模块① - 消抖

    实验二:按键模块① - 消抖 按键消抖实验可谓是经典中的经典,按键消抖实验虽曾在<建模篇>出现过,而且还惹来一堆麻烦.事实上,笔者这是在刁难各位同学,好让对方的惯性思维短路一下,但是惨遭口 ...

  8. linux enc28j60网卡驱动移植(硬件spi和模拟spi)

    本来想移植DM9000网卡的驱动,无奈硬件出了点问题,通过杜邦线链接开发板和DM9000网卡模块,系统上电,还没加载网卡驱动就直接崩溃了,找不到原因...刚好手上有一个enc28j60的网卡模块,于是 ...

  9. (笔记)linux设备驱动--LED驱动

    linux设备驱动--LED驱动 最近正在学习设备驱动开发,因此打算写一个系列博客,即是对自己学习的一个总结,也是对自己的一个督促,有不对,不足,需要改正的地方还望大家指出,而且希望结识志同道合的朋友 ...

随机推荐

  1. C# 数组,对象实例化并赋值

    [数组] 数组实例化,并赋值时,可以省略new[]. ]; --未赋值 ,}; --赋值 [对象] 对象实例化,并赋值时,可以省略(). class test { public string name ...

  2. Django_URL

    视图函数介绍 视图一般都写在app的views中,并且视图的第一个参数永远都是request(HttpRequest)对象.这个对象存储了请求过来的所有信息,包括携带的参数以及一些头部信息等.再视图中 ...

  3. Centos7安装Nginx+PHP+MySQL

    之前曾经在服务器上从头到尾搭过一次环境,但那时新手一枚,很多地方搞不定,是前辈帮忙解决的.这次独自一人在服务器上撘环境,感慨上次没有做好相关笔记,所以事后整理一下,下次再搭环境时可以轻车熟路. 一.准 ...

  4. Tensorflow选择性初始化图中的变量

    import tensorflow as tf def initialize_uninitialized(sess): global_vars = tf.global_variables() is_n ...

  5. select top 1 和select top 1 with ties * from SC 的区别

    select top1 : * FROM SC ORDER BY score DESC 运行结果如下: sc表数据查询select top 1 S# C# Score 001 03 120 WITH ...

  6. eregi

    eregi (PHP 4, PHP 5) eregi — 不区分大小写的正则表达式匹配 说明 int eregi ( string $pattern , string $string [, array ...

  7. 用git提交源代码

    码云账号 markliuning      作业已经上传 题目要求:定义一个包含有10个整数的数组a并初始化,定义一个指针变量p,p指向数组a,定义函数fun,在fun内部访问数组,并打印出数组中各元 ...

  8. 五、Vi和Vim编辑器

    1. Vim编辑器: 在Linux下一般使用vi编辑器来编辑文件.vi既可以查看文件也可以编辑文件.三种模式: 命令行.插入.底行模式 切换到命令行模式:按Esc键: 切换到插入模式:按 i .o.a ...

  9. getString与optString的区别

    JSONObject.getString("key"):当对象中没有key属性的时候,会抛出No value for "key"的异常: public Stri ...

  10. elasticsearch5.0以上版本及head插件的安装

    本文转载至:https://www.cnblogs.com/hts-technology/p/8477258.html(针对5.0以上版本) 对于es5.0以下的版本可以参考:https://www. ...