button_drv.c驱动文件:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <asm/io.h> 
#include <asm/uaccess.h> 
#include <linux/device.h> 
#include <asm/arch/regs-gpio.h> 
#include <linux/irq.h>
#include <asm-arm/irq.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <asm/hardware.h>
#include <linux/poll.h>

#define DRIVER_NAME "button_drv"
#define DEVICE_NAME "button_dev"

int major;

static atomic_t openFlag = ATOMIC_INIT(1);     //定义原子变量openFlag并初始化为1

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

struct class *button_class;
struct class_device *button_class_device;

unsigned char ev_press;
DECLARE_WAIT_QUEUE_HEAD(button_waitq);       //注册等待队列

struct fasync_struct *button_fasync;        //定义fasync_struct结构体

unsigned char keyVal;

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},
};

irqreturn_t buttons_irq(int irq, void *dev_id)
{
  unsigned int pin_val;
  struct pin_desc *pin_desc = (struct pin_desc *)dev_id;
  pin_val = s3c2410_gpio_getpin(pin_desc->pin);
  if(pin_val)
  {
    keyVal = 0x80 | pin_desc->key_val;
  }
  else
  {
    keyVal = pin_desc->key_val;
  }
  wake_up_interruptible(&button_waitq);
  ev_press = 1;
  kill_fasync(&button_fasync, SIGIO, POLL_IN); //发送信号给进程
  return IRQ_HANDLED;
}

int button_drv_open(struct inode *inode, struct file *file)
{
  int ret;
  if(atomic_dec_and_test(&openFlag)==0)     //自减后是否为0, 不为0说明已经被别的线程调用, 要是为0则返回为真, 否则返回为假
  {
    atomic_inc(&openFlag);
    return -EBUSY;
  }
  ret = request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "S1", &pins_desc[0]);
  if(ret<0)
  {
    printk("failed 1 button_drv_open");
  }
  ret = request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[1]);
  if(ret<0)
  {
    printk("failed 2 button_drv_open");
  }
  ret = request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[2]);
  if(ret<0)
  {
    printk("failed 3 button_drv_open");
  }
  ret = request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[3]);
  if(ret<0)
  {
    printk("failed 4 button_drv_open");
  }
  return 0;
}

ssize_t button_drv_read(struct file *file, char __user *userbuf, size_t count, loff_t *off)
{
  int ret;
  ret = copy_to_user(userbuf, &keyVal, 1);
  if(ret<0)
  {
    printk("failed 1 button_drv_read \n");
    return -1;
  }
  ev_press = 0;
  return 1;
}

int button_drv_close(struct inode *inode, struct file *file)
{
  atomic_inc(&openFlag); //原子变量加1
  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;
}

unsigned int button_drv_poll(struct file *file, poll_table *wait)
{
  unsigned int mask = 0;
  poll_wait(file, &button_waitq, wait);
  if(ev_press)
  {
    mask |= POLLIN | POLLRDNORM;
  }
  return mask;
}

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;
}

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

int __init button_drv_init(void)
{
  major = register_chrdev(0, DRIVER_NAME, &button_drv_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 += timer_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);
  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驱动5(atomic)原子量的更多相关文章

  1. Linux 驱动——Button驱动7(Timer)消抖

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

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

    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. Linux GPIO键盘驱动开发记录_OMAPL138

    Linux GPIO键盘驱动开发记录_OMAPL138 Linux基本配置完毕了,这几天开始着手Linux驱动的开发,从一个最简单的键盘驱动开始,逐步的了解开发驱动的过程有哪些.看了一下Linux3. ...

  8. linux块设备驱动之实例

    1.注册:向内核注册个块设备驱动,其实就是用主设备号告诉内核这个代表块设备驱动 sbull_major  =  register_blkdev(sbull_major, "sbull&quo ...

  9. Linux 视频设备驱动V4L2最常用的控制命令

    http://blog.csdn.net/shaolyh/article/details/6583226 Linux 视频设备驱动V4L2最常用的控制命令使用说明(1.02) 命令 功能 VIDIOC ...

随机推荐

  1. c++ 查缺补漏

    c++句柄 win句柄保存对象的实时地址(对象消失,句柄消失).指针保存固定地址(对象消失,内存泄漏) 超简单句柄类 指针型句柄 管理图书类句柄 c++ 枚举 enum Suit { Diamonds ...

  2. 爬虫(二)Python网络爬虫相关基础概念、爬取get请求的页面数据

    什么是爬虫 爬虫就是通过编写程序模拟浏览器上网,然后让其去互联网上抓取数据的过程. 哪些语言可以实现爬虫    1.php:可以实现爬虫.php被号称是全世界最优美的语言(当然是其自己号称的,就是王婆 ...

  3. Spring IOC、AOP、Transaction、MVC小结

    1.IOC.AOP:把对象交给Spring进行管理,通过面向切面编程来实现一些“模板式”的操作,使得程序员解放出来,可以更多的关注业务实现.                             - ...

  4. QVector常见使用方法

    仅在此简单介绍QVector的一些常见函数,有兴趣的可以查下QT,在QT中介绍的很详细 构造函数,QVector的构造函数很多样化,常见的有 QVector() 无参的构造函数 QVector(int ...

  5. iOS 底层解析weak的实现原理(包含weak对象的初始化,引用,释放的分析)

    原文 很少有人知道weak表其实是一个hash(哈希)表,Key是所指对象的地址,Value是weak指针的地址数组.更多人的人只是知道weak是弱引用,所引用对象的计数器不会加一,并在引用对象被释放 ...

  6. EAP-MD5计算方法

    一.说明 领导要求确认设备进行802.1X认证时,是否直接将用户名密码明文传给交换机.配好端口镜像.搭好Radius环境后进行了抓包分析. 二.计算 2.1 802.1X认证过程 完整流程如下: 客户 ...

  7. !/usr/bin/env python和!/usr/bin/python的区别

    脚本语言第一行 作用:文件中代码用指定可执行程序运行 #!/usr/bin/Python  执行脚本时,调用/usr/bin下python解释器 #!/usr/bin/env python  在环境设 ...

  8. 简单的新手加法运算(基于Struts2)

    软件151  王帅 在搭好框架的前提下,首先编写action.java文件: import com.opensymphony.xwork2.ActionSupport; public class Us ...

  9. HIbernate常见异常(转载)

    SSH阶段常见的异常系列之一hibernate(15条) 异常一 异常一 异常描述: Sax解析异常:cvc-复杂的类型,发现了以元素maping开头的无效内容,应该是以 ‘{“http://www. ...

  10. 用docker搭建python项目运行环境

    Docker Hub镜像加速器 安装docker: curl -sSL http://acs-public-mirror.oss-cn-hangzhou.aliyuncs.com/docker-eng ...