目的:同一个时刻,只能有一个应用程序打开我们的驱动程序。

①原子操作:

v = ATOMIC_INIT( i )   定义原子变量v并初始化为i

atomic_read(v)        返回原子变量的值
atomic_set(v,i)        设置原子变量的值

atomic_inc_and_test(v)     自加后和测试是否为0  为0则返回true

atomic_dec_and_test(v)     自减后和测试是否为0  为0则返回true

atomic_inc(v)      自加
#atomic_dec(v)     自减

驱动代码:

#include <linux/sched.h>
#include <linux/signal.h>
#include <linux/spinlock.h>
#include <linux/errno.h>
#include <linux/random.h>
#include <linux/poll.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/wait.h>
#include <linux/mutex.h>
#include <linux/io.h>
#include <asm/irq.h>
#include <linux/irq.h>
#include <linux/fs.h>
#include <asm/arch/regs-gpio.h>
#include <linux/interrupt.h>
#include <linux/poll.h> static struct class *key_class; //创建类
static struct class_device *key_class_devs; //创建类对应的设备 struct pin_desc{
unsigned int pin;
unsigned int key_val;
};
struct pin_desc pins_desc[] = {
{S3C2410_GPF0,0X01},
{S3C2410_GPF2,0X02},
{S3C2410_GPG3,0X03},
{S3C2410_GPG11,0X04},
};
unsigned char keyvals=; static volatile int ev_press = ;
static DECLARE_WAIT_QUEUE_HEAD(button_waitq); static struct fasync_struct *key_async_queue; atomic_t canopen = ATOMIC_INIT(); static irqreturn_t keys_irq(int irq, void *dev_id)
{
struct pin_desc *pindesc = (struct pin_desc *)dev_id;
unsigned int pinval;
pinval = s3c2410_gpio_getpin(pindesc->pin);
if(pinval)
{
keyvals = pindesc->key_val|0x80;
}
else
{
keyvals = pindesc->key_val;
}
ev_press = ;
wake_up_interruptible(&button_waitq); kill_fasync (&key_async_queue, SIGIO, POLL_IN); return IRQ_HANDLED;
} int key_open(struct inode *inode, struct file *fp)
{
if(!atomic_dec_and_test(&canopen)) //if --canopen==0 return ture /*自减后和0相比 如果等于0 则返回true*/
{
atomic_inc(&canopen);
return -EBUSY;
} request_irq( IRQ_EINT0, keys_irq, IRQT_BOTHEDGE, "key2", &pins_desc[]);
request_irq( IRQ_EINT2, keys_irq, IRQT_BOTHEDGE, "key3", &pins_desc[]);
request_irq( IRQ_EINT11, keys_irq, IRQT_BOTHEDGE, "key4", &pins_desc[]);
request_irq( IRQ_EINT19, keys_irq, IRQT_BOTHEDGE, "key5", &pins_desc[]);
return ;
} ssize_t key_read(struct file *fp, char __user *buff, size_t count, loff_t *offp){ if(count != )
{
return -EINVAL;
}
wait_event_interruptible(button_waitq,ev_press); copy_to_user(buff,&keyvals,);
ev_press = ;
return ;
} ssize_t key_write(struct file *fp, const char __user *buf, size_t count, loff_t *ppos){
} int key_close(struct inode *inode, struct file *file)
{
atomic_inc(&canopen);
free_irq(IRQ_EINT0,&pins_desc[]);
free_irq(IRQ_EINT2,&pins_desc[]);
free_irq(IRQ_EINT11,&pins_desc[]);
free_irq(IRQ_EINT19,&pins_desc[]);
} static unsigned int key_poll(struct file *file, struct poll_table_struct *wait)
{
unsigned int mask = ;
poll_wait(file, &button_waitq,wait);
if(ev_press)
mask |= POLLIN|POLLRDNORM;
return mask;
} static int key_fsync (int fd, struct file *filp, int on)
{
printk("ok\n");
return fasync_helper (fd, filp, on, &key_async_queue);
} struct file_operations led_fops={
.owner = THIS_MODULE,
.open = key_open,
.write = key_write,
.read = key_read,
.release = key_close,
.poll = key_poll,
.fasync = key_fsync,
}; int major;
static int key_init(void)
{
major = register_chrdev( ,"key_drv", &led_fops );
key_class = class_create(THIS_MODULE,"key_class");
key_class_devs = class_device_create(key_class,NULL,MKDEV(major,),NULL,"my_keys"); printk("key install Module\n");
return ;
} static void key_exit(void)
{
unregister_chrdev( major, "key_drv" );
class_device_unregister(key_class_devs);
class_destroy(key_class);
printk("key Module exit\n");
} module_init(key_init);
module_exit(key_exit);
MODULE_LICENSE("GPL");

信号量:

#include <linux/sched.h>
#include <linux/signal.h>
#include <linux/spinlock.h>
#include <linux/errno.h>
#include <linux/random.h>
#include <linux/poll.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/wait.h>
#include <linux/mutex.h>
#include <linux/io.h>
#include <asm/irq.h>
#include <linux/irq.h>
#include <linux/fs.h>
#include <asm/arch/regs-gpio.h>
#include <linux/interrupt.h>
#include <linux/poll.h> static struct class *key_class; //创建类
static struct class_device *key_class_devs; //创建类对应的设备 struct pin_desc{
unsigned int pin;
unsigned int key_val;
};
struct pin_desc pins_desc[] = {
{S3C2410_GPF0,0X01},
{S3C2410_GPF2,0X02},
{S3C2410_GPG3,0X03},
{S3C2410_GPG11,0X04},
};
unsigned char keyvals=; static volatile int ev_press = ;
static DECLARE_WAIT_QUEUE_HEAD(button_waitq); static struct fasync_struct *key_async_queue; static DECLARE_MUTEX(canopen); //定义互斥锁 static irqreturn_t keys_irq(int irq, void *dev_id)
{
struct pin_desc *pindesc = (struct pin_desc *)dev_id;
unsigned int pinval;
pinval = s3c2410_gpio_getpin(pindesc->pin);
if(pinval)
{
keyvals = pindesc->key_val|0x80;
}
else
{
keyvals = pindesc->key_val;
}
ev_press = ;
wake_up_interruptible(&button_waitq); kill_fasync (&key_async_queue, SIGIO, POLL_IN); return IRQ_HANDLED;
} int key_open(struct inode *inode, struct file *fp)
{
/*获取信号量*/
down(&canopen);      //如果设备已经被打开,那么程序将会一直停在这里知道信号量被释放。 request_irq( IRQ_EINT0, keys_irq, IRQT_BOTHEDGE, "key2", &pins_desc[]);
request_irq( IRQ_EINT2, keys_irq, IRQT_BOTHEDGE, "key3", &pins_desc[]);
request_irq( IRQ_EINT11, keys_irq, IRQT_BOTHEDGE, "key4", &pins_desc[]);
request_irq( IRQ_EINT19, keys_irq, IRQT_BOTHEDGE, "key5", &pins_desc[]);
return ;
} ssize_t key_read(struct file *fp, char __user *buff, size_t count, loff_t *offp){ if(count != )
{
return -EINVAL;
}
wait_event_interruptible(button_waitq,ev_press); copy_to_user(buff,&keyvals,);
ev_press = ;
return ;
} ssize_t key_write(struct file *fp, const char __user *buf, size_t count, loff_t *ppos){
} int key_close(struct inode *inode, struct file *file)
{
free_irq(IRQ_EINT0,&pins_desc[]);
free_irq(IRQ_EINT2,&pins_desc[]);
free_irq(IRQ_EINT11,&pins_desc[]);
free_irq(IRQ_EINT19,&pins_desc[]);
up(&canopen);
} static unsigned int key_poll(struct file *file, struct poll_table_struct *wait)
{
unsigned int mask = ;
poll_wait(file, &button_waitq,wait);
if(ev_press)
mask |= POLLIN|POLLRDNORM;
return mask;
} static int key_fsync (int fd, struct file *filp, int on)
{
printk("ok\n");
return fasync_helper (fd, filp, on, &key_async_queue);
} struct file_operations led_fops={
.owner = THIS_MODULE,
.open = key_open,
.write = key_write,
.read = key_read,
.release = key_close,
.poll = key_poll,
.fasync = key_fsync,
}; int major;
static int key_init(void)
{
major = register_chrdev( ,"key_drv", &led_fops );
key_class = class_create(THIS_MODULE,"key_class");
key_class_devs = class_device_create(key_class,NULL,MKDEV(major,),NULL,"my_keys"); printk("key install Module\n");
return ;
} static void key_exit(void)
{
unregister_chrdev( major, "key_drv" );
class_device_unregister(key_class_devs);
class_destroy(key_class);
printk("key Module exit\n");
} module_init(key_init);
module_exit(key_exit);
MODULE_LICENSE("GPL");

阻塞:分为阻塞和非阻塞

  如何分辨阻塞和非阻塞呢?则需要在打开设备文件的时候传入一个参数

#include <linux/sched.h>
#include <linux/signal.h>
#include <linux/spinlock.h>
#include <linux/errno.h>
#include <linux/random.h>
#include <linux/poll.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/wait.h>
#include <linux/mutex.h>
#include <linux/io.h>
#include <asm/irq.h>
#include <linux/irq.h>
#include <linux/fs.h>
#include <asm/arch/regs-gpio.h>
#include <linux/interrupt.h>
#include <linux/poll.h> static struct class *key_class; //创建类
static struct class_device *key_class_devs; //创建类对应的设备 struct pin_desc{
unsigned int pin;
unsigned int key_val;
};
struct pin_desc pins_desc[] = {
{S3C2410_GPF0,0X01},
{S3C2410_GPF2,0X02},
{S3C2410_GPG3,0X03},
{S3C2410_GPG11,0X04},
};
unsigned char keyvals=; static volatile int ev_press = ;
static DECLARE_WAIT_QUEUE_HEAD(button_waitq); static struct fasync_struct *key_async_queue; static DECLARE_MUTEX(canopen); //定义互斥锁 static irqreturn_t keys_irq(int irq, void *dev_id)
{
struct pin_desc *pindesc = (struct pin_desc *)dev_id;
unsigned int pinval;
pinval = s3c2410_gpio_getpin(pindesc->pin);
if(pinval)
{
keyvals = pindesc->key_val|0x80;
}
else
{
keyvals = pindesc->key_val;
}
ev_press = ;
wake_up_interruptible(&button_waitq); kill_fasync (&key_async_queue, SIGIO, POLL_IN); return IRQ_HANDLED;
} int key_open(struct inode *inode, struct file *fp)
{
/*获取信号量*/
if( fp->f_flags & O_NONBLOCK )
{
if(down_trylock(&canopen))
return -EBUSY;
}
else
{
down(&canopen);
}
request_irq( IRQ_EINT0, keys_irq, IRQT_BOTHEDGE, "key2", &pins_desc[]);
request_irq( IRQ_EINT2, keys_irq, IRQT_BOTHEDGE, "key3", &pins_desc[]);
request_irq( IRQ_EINT11, keys_irq, IRQT_BOTHEDGE, "key4", &pins_desc[]);
request_irq( IRQ_EINT19, keys_irq, IRQT_BOTHEDGE, "key5", &pins_desc[]);
return ;
} ssize_t key_read(struct file *fp, char __user *buff, size_t count, loff_t *offp){ if(fp->f_flags & O_NONBLOCK )
{
if(!ev_press)
return -EAGAIN;
}
else
{
wait_event_interruptible(button_waitq,ev_press);
}
if(count != )
{
return -EINVAL;
} copy_to_user(buff,&keyvals,);
ev_press = ;
return ;
} ssize_t key_write(struct file *fp, const char __user *buf, size_t count, loff_t *ppos){
} int key_close(struct inode *inode, struct file *file)
{
free_irq(IRQ_EINT0,&pins_desc[]);
free_irq(IRQ_EINT2,&pins_desc[]);
free_irq(IRQ_EINT11,&pins_desc[]);
free_irq(IRQ_EINT19,&pins_desc[]);
up(&canopen);
} static unsigned int key_poll(struct file *file, struct poll_table_struct *wait)
{
unsigned int mask = ;
poll_wait(file, &button_waitq,wait);
if(ev_press)
mask |= POLLIN|POLLRDNORM;
return mask;
} static int key_fsync (int fd, struct file *filp, int on)
{
printk("ok\n");
return fasync_helper (fd, filp, on, &key_async_queue);
} struct file_operations led_fops={
.owner = THIS_MODULE,
.open = key_open,
.write = key_write,
.read = key_read,
.release = key_close,
.poll = key_poll,
.fasync = key_fsync,
}; int major;
static int key_init(void)
{
major = register_chrdev( ,"key_drv", &led_fops );
key_class = class_create(THIS_MODULE,"key_class");
key_class_devs = class_device_create(key_class,NULL,MKDEV(major,),NULL,"my_keys"); printk("key install Module\n");
return ;
} static void key_exit(void)
{
unregister_chrdev( major, "key_drv" );
class_device_unregister(key_class_devs);
class_destroy(key_class);
printk("key Module exit\n");
} module_init(key_init);
module_exit(key_exit);
MODULE_LICENSE("GPL");

阻塞测试文件:

#include <stdio.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h> int fd; static char key_val;
int main( int argc, char **argv )
{
int oflags;
fd = open("/dev/my_keys",O_RDWR|O_NONBLOCK);  /* O_NONBLOCK为非阻塞*/ if(fd<)
{
printf("open failed\n");
return ;
} while()
{
read(fd,&key_val,);
printf("key_val:%d\n",key_val);
sleep();
} return ;
}

嵌入式Linux驱动学习之路(十四)按键驱动-同步、互斥、阻塞的更多相关文章

  1. 嵌入式Linux驱动学习之路(十五)按键驱动-定时器防抖

    在之前的定时器驱动程序中,我们发现在连续按下按键的时候,正常情况下应该是一次按下对应一次松开.而程序有时候会显示是两次按下,一次松开.这个问题是因为在按下的时候,因为是机械按键,所以电压信号会产生一定 ...

  2. 嵌入式Linux驱动学习之路(十二)按键驱动-poll机制

    实现的功能是在读取按键信息的时候,如果没有产生按键,则程序休眠在read函数中,利用poll机制,可以在没有退出的情况下让程序自动退出. 下面的程序就是在读取按键信息的时候,如果5000ms内没有按键 ...

  3. 嵌入式Linux驱动学习之路(十八)LCD驱动

    驱动代码: /************************************************************************* > File Name: lcd ...

  4. 嵌入式Linux驱动学习之路(十)字符设备驱动-my_led

    首先贴上代码: 字符设备驱动代码: /** *file name: led.c */#include <linux/sched.h> #include <linux/signal.h ...

  5. 嵌入式Linux驱动学习之路(十九)触摸屏驱动、tslib测试

    触摸屏使用流程: 1. 按下产生中断. 2.在中断处理程序中启动AD转换XY坐标. 3.AD转换结束并产生AD中断. 4. 在AD的中断处理函数中上报信息,启动定时器. 5. 定时器时间到后进入中断, ...

  6. 嵌入式Linux驱动学习之路(十六)输入子系统

    以前写的一些输入设备的驱动都是采用字符设备处理的.问题由此而来,Linux开源社区的大神们看到了这大量输入设备如此分散不堪,有木有可以实现一种机制,可以对分散的.不同类别的输入设备进行统一的驱动,所以 ...

  7. 嵌入式linux的学习之路[转]

    我认为的一条学习嵌入式Linux的路: 1)学习 Linux系统安装. 常用命令.应用程序安装. 2) 学习 Linux 下的 C 编程.这本书必学<UNIX 环境高级编程>.<UN ...

  8. python学习笔记-(十四)I/O多路复用 阻塞、非阻塞、同步、异步

    1. 概念说明 1.1 用户空间与内核空间 现在操作系统都是采用虚拟存储器,那么对32位操作系统而言,它的寻址空间(虚拟存储空间)为4G(2的32次方).操作系统的核心是内核,独立于普通的应用程序,可 ...

  9. 学习之路十四:客户端调用WCF服务的几种方法小议

    最近项目中接触了一点WCF的知识,也就是怎么调用WCF服务,上网查了一些资料,很快就搞出来,可是不符合头的要求,主要有以下几个方面: ①WCF的地址会变动,地址虽变,但是里面的逻辑不变! ②不要引用W ...

随机推荐

  1. JAVA 8 Streams

    什么是Stream 首先要说的是,不要被它的名称骗了,这里的Stream跟JAVA I/O中的InputStream和OutputStream是两个不同的概念.Java 8中的Stream其实是函数式 ...

  2. ExtJS关于组件Component生命周期

    extjs组件生命周期大体分为3个阶段:初始化.渲染.销毁. 第一阶段:初始化 初始化工作开始于组件的诞生,所有必须的配置设定.事件注册.预渲染处理等都在此时进行. 1.应用组件的配置: 当初始化一个 ...

  3. jQuery静态方法parseJSON方法使用和源码分析

    该方法接受一个JSON字符串,返回解析后的对象. 传入一个畸形的JSON字符串会抛出一个异常.比如下面的都是畸形的JSON字符串: {test: 1} ( test 没有包围双引号) {'test': ...

  4. jquery编写插件的方法

     版权声明:作者原创,转载请注明出处! 编写插件的两种方式: 1.类级别开发插件(1%) 2.对象级别开发(99%) 类级别的静态开发就是给jquery添加静态方法,三种方式 1.添加新的全局函数 2 ...

  5. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q112-Q115)

    Question  112 You are designing a public-facing SharePoint 2010 Web site for an elementary school th ...

  6. Linux0.11内核--文件系统理论知识

    1.文件系统介绍 一个简单的文件系统大致需要这么几个要素: ● 要有地方存放Metadata: ● 要有地方记录扇区的使用情况: ● 要有地方来记录任一文件的信息,比如占用了哪些扇区等: ● 要有地方 ...

  7. 【代码笔记】iOS-圆角矩形

    代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. se ...

  8. 深入浅出React Native 2: 我的第一个应用

    这是深入浅出React Native教程的第二篇文章. 1. 环境配置 React Native环境配好之后,就可以开始创建我们的第一个App啦. 打开控制台,输入 react-native init ...

  9. Web客户端数据存储学习笔记——Cookie

    今天对登录访问的安全以及web客户端存储做了一些大致的学习,决定在这方面加深理解,记录在博客里.第一个接触到的是Cookie... WHAT? WHY? HOW? 在学习cookie的使用时发现其名称 ...

  10. ORA-00824: cannot set sga_target due to existing internal settings, see alert log for more information

    这篇文章是上篇文章”Expdp 导数错误 ORA-00832”的延续,前几天工作比较忙.累,直到今天才整理发出来.这个数据库实例的参数设置比较诡异其实是有原因的,由于这台数据库服务器系统是32位,数据 ...