Read button status using poll way, also with timer, atomic_t/semaphore, NONBLOCK func inside_readbuttonstatus(buttonstruct *button)-程序员宅基地

技术标签: DRIVER  

/**
  * read the button status using request_irq, poll way, also with timer, atomic_t/semaphore, NONBLOCKfunctions inside;
  */

------------------------------------driver part-----------------------------------------
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/init.h>  
#include <asm/uaccess.h>
#include <asm/io.h>  
#include <asm/arch/map.h>

#include <asm/arch/regs-gpio.h>
#include <asm/irq.h> 
#include <linux/interrupt.h>

#include <linux/poll.h>

static DECLARE_WAIT_QUEUE_HEAD(czpoll_wq);

volatile unsigned long *gpfcon = NULL;//key
volatile unsigned long *gpfdat = NULL;//key
static int czwq_pressed = 0;//up
static struct timer_list cz_timer;
static unsigned char cztouser_val;


struct czkey_desc {
 unsigned int pin;
 unsigned char val;
};

static struct czkey_desc czbuttons[4]={
   {S3C2410_GPF1, 0x01},
   {S3C2410_GPF4, 0x02},
   {S3C2410_GPF2, 0x03},
   {S3C2410_GPF0, 0x04},
};

static struct czkey_desc *key_desc = NULL;

#if 0
/*  tips about atomic_t usage:
  *  atomic_t v = ATOMIC_INIT(0);
  *  atomic_read(atomic_t *v);
  *  void atomic_inc(atomic_t *v);
  *  void atomic_dec(atomic_t *v);
  *  int atomic_dec_and_test(atomic_t *v);
  */
static atomic_t canopen = ATOMIC_INIT(1);
#endif

/*  tips about semaphore definition in way 1:
  *  first(define): struct semaphore sem;
  *  second(init with 0):void sema_init(struct semaphore *sem, int val); or void init_MUTEX(struct semaphore *sem);
  */
/*  tips about semaphore definition in way 2:
  *  static DECLARE_MUTEX(czpoll_lock);
  */
/*  get semaphore:
  *  void down(struct semaphore * sem);
  *  int down_interruptible(struct semaphore * sem);
  *  int down_trylock(struct semaphore * sem);
  */
/*  release semaphore:
  *  void up(struct semaphore * sem);
  */
static DECLARE_MUTEX(czpoll_lock);

static irqreturn_t czpoll_irq(int irq, void *dev_id)
{
 printk("driver:czpoll_irq in\n");
 czwq_pressed = 1;//down
 
 key_desc = (struct czkey_desc *)dev_id;

 mod_timer(&cz_timer, jiffies +HZ/50);

 return IRQ_HANDLED;
}

static int czpoll_open(struct inode * inode, struct file * file)

 printk("driver:czpoll_open in\n");
#if 0

 //atomic_dec_and_test is true when the val that after handled is 0;
 if(!atomic_dec_and_test(&canopen))
  {
   atomic_inc(&canopen);
   return -EBUSY;
     }
 printk("driver:czpoll_open canopen is %d\n",atomic_read(&canopen));
#endif

// NONBLOCK and BLOCK function:
 if (file->f_flags & O_NONBLOCK)
  {
   if(down_trylock(&czpoll_lock))
    return -EBUSY;
  }
 else
  {
   down(&czpoll_lock);
  }

 request_irq(IRQ_EINT1, czpoll_irq,
  IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING, "czpoll_K1",&czbuttons[0]);
 request_irq(IRQ_EINT4, czpoll_irq,
  IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING, "czpoll_K2",&czbuttons[1]);
 request_irq(IRQ_EINT2, czpoll_irq,
  IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING, "czpoll_K3",&czbuttons[2]);
 request_irq(IRQ_EINT0, czpoll_irq,
  IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING, "czpoll_K4",&czbuttons[3]);
 
  return 0;
}

static int czpoll_read(struct file * file, char __user * userbuf, size_t count, loff_t * off)
{
 printk("driver:czpoll_read in\n");
 int ret;
 
// NONBLOCK and BLOCK function:
 if(file->f_flags & O_NONBLOCK)
  {
   if(!czwq_pressed)
    return -EAGAIN;
  }
 else
  {
   wait_event_interruptible(czpoll_wq,czwq_pressed);
  }
 
 czwq_pressed = 0;//reset
 
 ret = copy_to_user(userbuf,&cztouser_val,1);
 if( ret==0 )
  printk("driver:czpoll_read copy_to_user successed\n");
 else
  printk("driver:czpoll_read copy_to_user failed\n");
 
 
 printk("driver:czpoll_read out\n");
 return 0;
}

static int czpoll_close(struct inode *inode, struct file *file)
{
 printk("driver:czpoll_close in\n");
 
#if 0
 atomic_inc(&canopen);
#endif

 free_irq(IRQ_EINT1, &czbuttons[0]);
 free_irq(IRQ_EINT4, &czbuttons[1]);
 free_irq(IRQ_EINT2, &czbuttons[2]);
 free_irq(IRQ_EINT0, &czbuttons[3]);
 
 up(&czpoll_lock);
 
 return 0;
}

static unsigned int czpoll_poll(struct file *file, poll_table *wait)

    unsigned int mask = 0;
 poll_wait(file, &czpoll_wq, wait);
 
 if(czwq_pressed)
 mask |= POLLIN | POLLRDNORM;
 
 return mask;
}

static struct file_operations czpoll_fops = {
 .owner = THIS_MODULE,
 .open = czpoll_open,
 .release = czpoll_close,
 .read = czpoll_read,
 .poll = czpoll_poll,
};

static struct class *pcz_cls;
static struct class_device *pczcls_dev;
int major;

//tips:if there is no static as follows,there will have oops!
static void czpoll_timer_function(unsigned long data)
{
    printk("driver:czpoll_timer_function in\n");
 
 if(!key_desc)
  return;
 
 if(s3c2410_gpio_getpin(key_desc->pin))
  {
     //key_desc->val = key_desc->val | 0x80;
     cztouser_val = key_desc->val | 0x80;
     printk("driver:czpoll_irq pin:%d,val:0x%x\n",key_desc->pin, cztouser_val );
  }
 else
  {
     //key_desc->val = key_desc->val & 0x0f;
     cztouser_val = key_desc->val;
     printk("driver:czpoll_irq pin:%d,val:0x%x\n",key_desc->pin, cztouser_val );
  }
 
 wake_up_interruptible(&czpoll_wq);

}

static int czpoll_init(void)
{
 printk("driver:czpoll_init in\n");
 
 init_timer(&cz_timer);
 cz_timer.function = czpoll_timer_function;
 //cz_timer->expires = 0;//default is 0
 add_timer(&cz_timer);

 major = register_chrdev(0,"cz_chr",&czpoll_fops);
 
 pcz_cls = class_create(THIS_MODULE,"czpoll_cls");
 pczcls_dev = class_device_create(pcz_cls,NULL,MKDEV(major,0),NULL,"czcls_dev");

 gpfcon = (volatile unsigned long *)ioremap(0x56000050,16);//key
 gpfdat = gpfcon +1;//key
   
 return 0;
}

static void czpoll_exit(void)
{
 printk("driver:czpoll_exit in\n");
 
 //class_device_destroy(pczcls_dev,MKDEV(major,0));
 class_device_unregister(pczcls_dev);
 printk("driver:czpoll_exit pczcls_dev\n");
 class_destroy(pcz_cls);
 printk("driver:czpoll_exit pcz_cls\n");
 
 unregister_chrdev(major,"cz_chr");
 printk("driver:czpoll_exit cz_chr\n");
 
 //tips:if iounmap is in front of czpoll_exit,there will have oops!
 iounmap(gpfcon);//key
 printk("driver:czpoll_exit gpfcon\n");
}

module_init(czpoll_init);
module_exit(czpoll_exit);
MODULE_AUTHOR("chaozang(cz),copy frm weidongshan,thanks!");
MODULE_LICENSE("GPL");

-------------------------------------------app part--------------------------------------------

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

int main(int argc,char ** argv)
{
      int fd;
      unsigned char val = 0;
   int ret;

   struct pollfd fds[1];
                                                    
      //fd = open("/dev/czcls_dev", O_RDWR);//BLOCK
   fd = open("/dev/czcls_dev", O_RDWR | O_NONBLOCK);//NONBLOCK

   fds[0].fd     = fd;
   fds[0].events = POLLIN;
    
      //write(fd,&val,4);
     
   if( fd < 0)
     printf("app: fd < 1,can`t open\n");
   else
     printf("app: fd opened\n");
 
   while(1){

    ret = poll(fds,1,15000);         
     if( ret == 0)
            printf("app:time out"\n);
     else
      {  

            read(fd,&val,1);
             printf("app: buttons val is: 0x%x\n",val);
             //sleep(15);
      }
        }

    return 0;
}

--------------------------------------------------------------

Contact:[email protected]

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/zangchaodotcnatgmail/article/details/8478451

智能推荐

java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotationUtils.isInJavaLangAnnotat_caused by: java.lang.nosuchmethoderror: org.spring-程序员宅基地

文章浏览阅读1.4w次。最近在做spring框架练习时,遇到启动程序报错,摘取部分片段如下:Caused by: java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotationUtils.isInJavaLangAnnotationPackage(Ljava/lang/annotation/Annotation;)Z at o..._caused by: java.lang.nosuchmethoderror: org.springframework.core.annotation.

大数据开发需要学习哪些编程语言_大数据开发语言-程序员宅基地

文章浏览阅读6.5k次。大数据开发需要学什么编程语言?随着大数据的持续升温,越来越多的人投身于大数据的浪潮之中,不少完全没基础的小伙伴,难免会有这样的疑问,从事大数据需要学习什么编程语言呢?其实这个问题没有固定的答案,像Python、R、Java和Scala都是很好的选择,大家可以根据自身的实际情况进行选择1、Python一般的数据科学家都会选择Python作为大数据语言的首选。一直以来,Python流行于学术界,在自然语言处理(NLP)等领域尤其如此。所以,当有一个需要NLP处理的项目时,就会面临数量多得让人眼花缭乱的选_大数据开发语言

Python读取文件内容-程序员宅基地

文章浏览阅读3.5w次,点赞23次,收藏125次。Python 读取文件内容_python读取文件内容

港科夜闻|香港科大举办网上招生说明会,详细解读招生政策。-程序员宅基地

文章浏览阅读189次。关注并星标每周阅读港科夜闻建立新视野 开启新思维/近日要闻一览/▼1、香港科大举办网上招生说明会,详细解读招生政策。招生说明会邀请了香港科大本科招生办主任和学生大使担任讲者,为有兴趣入读...

【Linux】bash: /home/stefan/jdk1.7.0_55/bin/java: /lib/ld-linux.so.2: bad ELF interpreter_-bash: /home/rootplace/jdk8/bin/java: /lib/ld-linu-程序员宅基地

文章浏览阅读215次。今天在linux系统中配置jdk的时候,配置完成之后提示bash: /home/stefan/jdk1.7.0_55/bin/java: /lib/ld-linux.so.2: bad ELF interpreter: No such file or directory后来感觉可能只因为之前卸载yum导致的问题,因为我在另外一个虚拟机中也是这么配置的于是搜了一下问题,执行如下命令即可yu..._-bash: /home/rootplace/jdk8/bin/java: /lib/ld-linux.so.2: bad elf interpreter: 没有那个文件或目录

高级SQL优化(一) -程序员宅基地

文章浏览阅读319次。目录:Oracle数据完整性和锁机制 索引及优化之表分析 表分析、约束及表间关系 Oracle体系结构1Oracle体系结构2 海量数据库及分区1 海量数据库及分区2 海量数据库及分区3 海量数据库及分区4 高级SQL优化(一) PPT和源码下载: http://sishuok.com/forum/posts/list/6..._高级sql优化

随便推点

Editor_Selection,MenuItem学习02_editor.selection-程序员宅基地

文章浏览阅读305次。//也就选中的物体public class Editor_Selection{/* 1.选中的物体(可操作)2.使用快捷键操作3.选中右击添加组体,要先判断是否有此组件4.MenuItem最二个参数的用法 **/ [MenuItem("EditTools/selection1")] static void SelectionObj() { i_editor.selection

更新版:软件测试菜鸟入门_软件测试在哪更新-程序员宅基地

文章浏览阅读2.5k次,点赞16次,收藏92次。改版前链接:https://weltest.blog.csdn.net/article/details/79847217前言​ 随着技术的发展,各种应用程序、各种App应运而生!在早期,这些应用程序只是通过开发人员、产品以及部分用户使用之后,给出相应的修改意见,感觉都OK后就进行上线,在网上或一些app下载平台上就可以直接使用,没有进行过规范的软件测试!这些软件或多或少会存在一些bug,这些bug有可能是功能上、兼容性、性能等各方面的问题!为了改善软件质量不高的问题,软件测试这门行业才开始受到重视!软件_软件测试在哪更新

SpringBoot保存数据库中文乱码以及输出json乱码_springboot保存中文到数据库乱码-程序员宅基地

文章浏览阅读811次。首先第一个,保存数据库乱码,凡是保存到中文字符的地方都出现乱码,一堆???看不懂的文字,这时候需要检查三个地方,第一检查链接数据库驱动配置是否配置了utf-8url: jdbc:mysql://localhost:3306/sell?characterEncoding=utf-8&useSSL=false第二检查数据库属性,当时创建数据库时是否是utf-8的编码第三若..._springboot保存中文到数据库乱码

Objective-C 类别使用_objective-c 类别shiyong-程序员宅基地

文章浏览阅读387次。Objective-C 类别使用_objective-c 类别shiyong

js 写入图片Exif信息piexifjs-程序员宅基地

文章浏览阅读3.2k次。SamplesInsert Exif into jpegfunction handleFileSelect(evt) { var file = evt.target.files[0]; var zeroth = {}; var exif = {}; var gps = {}; zeroth[piexif.ImageIFD.Ma_piexifjs

设计模式——命令模式实现撤销_软件设计模式命令模式撤销实验的实现-程序员宅基地

文章浏览阅读1k次。总结一下工作中用到的这个设计模式,,看了下大话设计模式里好像也有这个,,以后看到了在完善吧,,现在这个是项目要求实现一个撤销功能,就跟我说用命令模式写就行~听简单的~QAQ然后看了很多关于命令模式的博客,感觉我写的应该差不多吧,,应该没有理解错,直接放代码public class 命令模式 : MonoBehaviour{ CommandManager comdMag = new CommandManager(); public int TestValue = 0; // Start is_软件设计模式命令模式撤销实验的实现

推荐文章

热门文章

相关标签