并发-信号量

阻塞与非阻塞

1. 原子操作
原子操作指的是在执行过程中不会被别的代码路径所中断的操作。
常用原子操作函数举例:
atomic_t v = ATOMIC_INIT(0);     //定义原子变量v并初始化为0
atomic_read(atomic_t *v);        //返回原子变量的值
void atomic_inc(atomic_t *v);    //原子变量增加1
void atomic_dec(atomic_t *v);    //原子变量减少1
int atomic_dec_and_test(atomic_t *v); //自减操作后测试其是否为0,为0则返回true,否则返回false。

2. 信号量
信号量(semaphore)是用于保护临界区的一种常用方法,只有得到信号量的进程才能执行临界区代码。
当获取不到信号量时,进程进入休眠等待状态。

定义信号量
struct semaphore sem;
初始化信号量
void sema_init (struct semaphore *sem, int val);
void init_MUTEX(struct semaphore *sem);//初始化为0

static DECLARE_MUTEX(button_lock);     //定义互斥锁

获得信号量
void down(struct semaphore * sem);
int down_interruptible(struct semaphore * sem); 
int down_trylock(struct semaphore * sem);
释放信号量
void up(struct semaphore * sem);

3. 阻塞
阻塞操作    
是指在执行设备操作时若不能获得资源则挂起进程,直到满足可操作的条件后再进行操作。
被挂起的进程进入休眠状态,被从调度器的运行队列移走,直到等待的条件被满足。

非阻塞操作  
进程在不能进行设备操作时并不挂起,它或者放弃,或者不停地查询,直至可以进行操作为止。

fd = open("...", O_RDWR | O_NONBLOCK);

驱动

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/fb.h>
#include <linux/backlight.h>
#include <linux/err.h>
#include <linux/pwm.h>
#include <linux/slab.h>
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <linux/timer.h>  /*timer*/
#include <asm/uaccess.h>  /*jiffies*/
#include <linux/delay.h>
#include <linux/interrupt.h> //request_irq
#include <mach/irqs.h> //中断号,已包含plat/irqs.h
#include <linux/fs.h>
#include <linux/device.h> //class_create device_create
#include <mach/regs-gpio.h>
#include <linux/io.h> //ioremap ioread32 iowrite32
#include <linux/sched.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/poll.h>
#include <mach/gpio.h>
#include <linux/gpio.h>
#include <mach/gpio.h>
#include <plat/gpio-cfg.h>static struct class *sixthdrv_class;
static struct class_device  *sixthdrv_class_dev;static DECLARE_WAIT_QUEUE_HEAD(button_waitq);/* 中断事件标志, 中断服务程序将它置1,sixth_drv_read将它清0 */
static volatile int ev_press = 0;static struct fasync_struct *button_async;struct led_reg {    u32 gpm4con;u8 gpm4dat;};
static struct led_reg *led_reg;struct key_reg { u32 gpm4con;u8 gpm4dat;};
static struct key_reg *key_reg;struct beep_reg {    u32 gpm4con;u8 gpm4dat;};
static struct key_reg *beep_reg;
struct pin_desc{unsigned int pin;unsigned int key_val;
};/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
/* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
static unsigned char key_val;
struct pin_desc pins_desc[4] = {{EXYNOS4_GPX3(2), 0x01},{EXYNOS4_GPX3(3), 0x02},{EXYNOS4_GPX3(4), 0x03},{EXYNOS4_GPX3(5), 0x04},
};//static atomic_t canopen = ATOMIC_INIT(1);     //定义原子变量并初始化为1//static DECLARE_MUTEX(button_lock);     //定义互斥锁
struct semaphore button_lock;/** 确定按键值*/
static irqreturn_t buttons_irq(int irq, void *dev_id)
{printk("buttons_irq\n");struct pin_desc * pindesc = (struct pin_desc *)dev_id;unsigned int pinval;//pinval = s3c2410_gpio_getpin(pindesc->pin);//获取按键的键值,因为按键是从该寄存器的第二位开始的,所以需要左移2位,接着与上0xf---1111  //这样,如果用户按下按键,就会返回一个键值保存在key_val这个变量里   pinval = ((key_reg->gpm4dat) >> 2) & 0xf ;  key_val=pinval;printk("buttons_irq :pinval = %d \n",pinval);
#if 0if (pinval){/* 松开 */key_val = 0x80 | pindesc->key_val;printk("buttons_irq :key_val = %d\n ",key_val);}else{/* 按下 */key_val = pindesc->key_val;printk("buttons_irq :key_val = %d \n",key_val);}
#endifev_press = 1;                  /* 表示中断发生了 */wake_up_interruptible(&button_waitq);   /* 唤醒休眠的进程 */kill_fasync (&button_async, SIGIO, POLL_IN);return IRQ_RETVAL(IRQ_HANDLED);
}static int sixth_drv_open(struct inode *inode, struct file *file)
{int ret;printk("sixth_drv_open\n");
#if 0   if (!atomic_dec_and_test(&canopen)){atomic_inc(&canopen);return -EBUSY;}
#endif      if (file->f_flags & O_NONBLOCK){if (down_trylock(&button_lock))return -EBUSY;}else{/* 获取信号量 */down(&button_lock);}//配置4个按键为输入状态,因为按键是从GPXCON[2]开始的,所以要左移8位到对应的位置,将8位以后的16位清0//这样的话就将按键配置的寄存器设置为输入状态了,因为输入是0x0 key_reg->gpm4con  &= ~((0xf<<(2*4)) | (0xf<<(3*4)) | (0xf<<(4*4)) | (0xf<<(5*4)));//先对LED的端口进行清0操作led_reg->gpm4con  &= ~((0xf<<(3*4)) | (0xf<<(2*4)) | (0xf<<(1*4)) | (0xf<<(0*4)));//将4个IO口16位都设置为Output输出状态led_reg->gpm4con |= ((0x1<<(3*4)) | (0x1<<(2*4)) | (0x1<<(1*4)) | (0x1<<(0*4)));//清寄存器 beep_reg->gpm4con &= ~(0xf);//设置io为输出 beep_reg->gpm4con |= (0x1);ret = request_irq(IRQ_EINT(26), buttons_irq, IRQF_TRIGGER_FALLING , "k1", &pins_desc[0]);  ret =request_irq(IRQ_EINT(27), buttons_irq, IRQF_TRIGGER_FALLING , "k2", &pins_desc[1]); ret =request_irq(IRQ_EINT(28), buttons_irq, IRQF_TRIGGER_FALLING , "k3", &pins_desc[2]);ret =request_irq(IRQ_EINT(29), buttons_irq, IRQF_TRIGGER_FALLING , "k4", &pins_desc[3]);  return 0;
}ssize_t sixth_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{printk("sixth_drv_read\n");if (size != 1)return -EINVAL;if (file->f_flags & O_NONBLOCK){if (!ev_press)return -EAGAIN;}else{/* 如果没有按键动作, 休眠 */wait_event_interruptible(button_waitq, ev_press);}/* 如果有按键动作, 返回键值 */copy_to_user(buf, &key_val, 1);ev_press = 0;return 1;
}int sixth_drv_write(struct file *filp , const char __user *buf , size_t count , loff_t *f_pos)
{int val;printk("fifth_fasync_drv_write\n");//注意,这里是在内核中进行操作,我们需要使用copy_from_user这个函数将用户态的内容拷贝到内核态copy_from_user(&val, buf, count);switch(val){case 7:printk(KERN_EMERG"led1_on\n");led_reg->gpm4dat &= ~(1<<0) ;printk(KERN_EMERG"beep_on\n");beep_reg->gpm4dat |= 0x1 ;break ;  case 11:printk(KERN_EMERG"led2_on\n");led_reg->gpm4dat &= ~(1<<1) ;printk(KERN_EMERG"beep_off\n");beep_reg->gpm4dat &=~0x1 ; //蜂鸣器不响break ;   case 13:printk(KERN_EMERG"led3_on\n");led_reg->gpm4dat &= ~(1<<2) ;printk(KERN_EMERG"beep_on\n");beep_reg->gpm4dat |= 0x1 ;break ;    case 14:printk(KERN_EMERG"led4_on\n");led_reg->gpm4dat &= ~(1<<3) ;printk(KERN_EMERG"beep_off\n");beep_reg->gpm4dat &=~0x1 ; //蜂鸣器不响break ;       }return 0;
}
int sixth_drv_close(struct inode *inode, struct file *file)
{//atomic_inc(&canopen);led_reg->gpm4dat |= ((1<<0) | (1<<1) |(1<<2)| (1<<3)) ; beep_reg->gpm4dat &=~0x1 ; //蜂鸣器不响free_irq(IRQ_EINT(26), &pins_desc[0]);free_irq(IRQ_EINT(27), &pins_desc[1]);free_irq(IRQ_EINT(28), &pins_desc[2]);free_irq(IRQ_EINT(29), &pins_desc[3]);up(&button_lock);return 0;
}static unsigned sixth_drv_poll(struct file *file, poll_table *wait)
{printk("sixth_drv_poll\n");unsigned int mask = 0;poll_wait(file, &button_waitq, wait); // 不会立即休眠if (ev_press)mask |= POLLIN | POLLRDNORM;return mask;
}static int sixth_drv_fasync (int fd, struct file *filp, int on)
{printk("driver: sixth_drv_fasync\n");return fasync_helper (fd, filp, on, &button_async);
}static struct file_operations sencod_drv_fops = {.owner   =  THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */.open    =  sixth_drv_open,     .read     = sixth_drv_read,    .release =  sixth_drv_close,.poll    =  sixth_drv_poll,.fasync  =  sixth_drv_fasync,.write = sixth_drv_write,
};int major;
static int sixth_drv_init(void)
{printk("sixth_drv_init\n");major = register_chrdev(0, "sixth_drv", &sencod_drv_fops);sixthdrv_class = class_create(THIS_MODULE, "sixth_drv");sixthdrv_class_dev = device_create(sixthdrv_class, NULL, MKDEV(major, 0), NULL, "chenhaipan"); /* /dev/buttons */led_reg = ioremap(0x110002e0, sizeof(struct led_reg));beep_reg = ioremap(0x114000A0, sizeof(struct beep_reg));key_reg = ioremap(0x11000C60, sizeof(struct key_reg));sema_init(&button_lock, 1);return 0;
}static void sixth_drv_exit(void)
{printk("sixth_drv_exit\n");unregister_chrdev(major, "sixth_drv");device_destroy(sixthdrv_class, MKDEV(major, 0));class_destroy(sixthdrv_class);iounmap(led_reg);iounmap(beep_reg);iounmap(key_reg);
}module_init(sixth_drv_init);module_exit(sixth_drv_exit);MODULE_LICENSE("GPL");
MODULE_AUTHOR("xiangtan da xue chenhaipan");
MODULE_VERSION("2017.5.4");

应用

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>/* sixthdrvtest */
int fd;void my_signal_fun(int signum)
{unsigned char key_val;read(fd, &key_val, 1);printf("key_val: 0x%x\n", key_val);
}int main(int argc, char **argv)
{unsigned char key_val;int ret;int Oflags;//signal(SIGIO, my_signal_fun);fd = open("/dev/chenhaipan", O_RDWR | O_NONBLOCK);if (fd < 0){printf("can't open!\n");return -1;}//fcntl(fd, F_SETOWN, getpid());//Oflags = fcntl(fd, F_GETFL); //fcntl(fd, F_SETFL, Oflags | FASYNC);while (1){ret = read(fd, &key_val, 1);printf("key_val: 0x%x, ret = %d\n", key_val, ret);write(fd, &key_val,1);sleep(5);}return 0;
}

非阻塞方式,没有按键值按下,程序立马返回;
read 返回值 为 -1;

阻塞方式 open

如果不按键,就一直停留,等待,并不运行

总结:阻塞操作:
          是指在执行设备操作时,若不能获得资源则挂起进程,直到满足可操作的条件后进行操作,
          被挂起的进程进入睡眠状态,被从调度器的运行队列移走,直到等待的条件被满足.
非阻塞操作:
          进程不能进行设备操作时并不挂起,他或者放弃,或者不停的查询,直到可以进行操作为止.

4412驱动-sixth_drv 同步互斥按键驱动相关推荐

  1. 字符设备驱动笔记——中断方式按键驱动之linux异常处理结构(四)

    1.中断方式获取按键值单片机: 1)按键按下 2)cup发生中断,跳转到异常向量入口执行 3)b 函数a.保存被中断的现场b.执行中断处理函数c.恢复 linux: 1)trap_init()函数构造 ...

  2. 字符设备驱动笔记——中断方式按键驱动之linux中断处理结构(五)

    一.单片机下的中断处理1)分辨是哪一个中断2)调用处理函数3)清中断 二.linux下的中断处理1)/arch/arm/kernel/irq.c asmlinkage void __exception ...

  3. linux 按键驱动代码分析

    原文地址:http://blog.csdn.NET/woshidahuaidan2011/article/details/51695147 二.按键驱动 1.对按键驱动添加设备信息 linux-3.1 ...

  4. linux 按键驱动中断 rockchip_嵌入式linux快速入门(转载)

    从上图可以知道: ① 组成:嵌入式Linux系统 = bootloader + linux内核 + 根文件系统(里面含有APP). ② bootloader:它的目的是启动内核,去哪读内核?读到哪里? ...

  5. linux内核按键驱动,嵌入式Linux按键驱动框架

    前言 本文将通过轮询.中断.poll机制.异步通知和同步互斥阻塞等方式编写按键驱动程序.本节的驱动框架是在<嵌入式Linux驱动框架的搭建>的基础上进行改进的,所以本文只讲解修改的部分. ...

  6. 按键驱动的恩恩怨怨之概述

    转载请注明出处:http://blog.csdn.net/ruoyunliufeng/article/details/23946487 研究按键驱动已经有几天了,尽管是0基础的驱动,可是当中包括的知识 ...

  7. android系统移植之按键驱动篇

    平台:MX53_QSB开发板 MX53_QSB开发板上一起有四个按键,分别为RESET,POWER,USER1,USER2.其中RESET为纯硬件复位按键,无须软件控制.POWER,USER1,USE ...

  8. 输入子系统--按键驱动

    输入子系统 一.输入子系统 二.设备申请与成员赋值 1.申请设备空间 2.设备结构体赋值 3.设置设备事件 4.gpio与gpio中断 三.注册设备 四.注销设备 五.按键驱动实现 一.输入子系统 l ...

  9. Linux keypad 设备树,matrix_keypad 矩阵按键驱动分析

    matrix_keypad 矩阵按键驱动分析 //主要函数调用过程 matrix_keypad_probe matrix_keypad_parse_dt //根据设备树构造 pdata pdata-& ...

最新文章

  1. Clion 远程开发 配置
  2. 分数化小数(模拟除法操作)
  3. 随记一个C的毫秒级群PING
  4. java掩码校验_Java 检查Ip掩码
  5. VC按最小化、关闭按钮、Esc都隐藏主窗口
  6. 笨办法学 Python · 续 练习 40:SQL 读取
  7. 图书室用计算机,电子阅览室使用
  8. 修改监视器驱动为大显示器来提高屏幕分辨率
  9. 什么软件可以测试电脑显示器坏点,怎么检测液晶显示器亮点、暗点、坏点
  10. 「又报错了TAT」 编译器报错是什么原因error: lvalue required as left operand of assignment|
  11. 大学python挂科补考_大学挂科后补考不过怎么样一种体验?
  12. 不同空间任务要求下认知地图的神经表征
  13. java中怎么输入中文_MultiMC下载-MultiMC中文实用版 v1.0
  14. brctl 使用说明
  15. rsync使用ssh指定端口
  16. 【MidJourney】初体验——账号注册与基本操作
  17. R语言之系统聚类(层次)分析之图谱形式完整版
  18. 微信小程序实现充值,自定义金额充值
  19. qq 4 android,Android QQ HD v5.5.4 正式版发布
  20. Terraformer-wkt-parser wkt和geoJson转换

热门文章

  1. zimbra mysql stopping_zimbra数据备份
  2. el-tree 动态树结构
  3. robocopy 遷移共享文件夾
  4. 电脑开机为什么老是要两次?
  5. 虚拟机RedHatEenterpriseLinux5安装及Oracle10.2.0安装手记收藏
  6. 中国的亚健康人群的肠道问题不容忽视 身体知道酸奶能帮忙
  7. 2022年最新最全的Java零基础入门,零基础入门springboot,MySQL的学习
  8. halcon 手眼标定原理推导
  9. Fabric学习(一) — 区块链及相关技术介绍
  10. opencv:把三通道图转换成灰度图、二值图