遠程視頻監控之驅動篇(按鍵)

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   os   ar   sp   

       轉載請註明出處:http://blog.csdn.net/ruoyunliufeng/article/details/38515211


       這裡我僅僅貼出了代碼和應用,沒有進行其它的解說。由於之前我寫過的按鍵驅動的恩恩怨怨,解說的很清楚,這個驅動就是依據之前寫的改寫而成。串連:http://blog.csdn.net/ruoyunliufeng/article/details/23946487

一.代碼

#include <linux/module.h>#include <linux/sched.h>#include <linux/kernel.h>#include <linux/fs.h>#include <linux/init.h>#include <linux/delay.h>#include <linux/irq.h>#include <asm/uaccess.h>#include <asm/irq.h>#include <asm/io.h>#include <linux/device.h>#include <mach/gpio.h>#include <linux/interrupt.h>#include <linux/poll.h>static struct class *wvm_buttons_class;static  int major;volatile unsigned long *gpfcon;volatile unsigned long *gpfdat;volatile unsigned long *gpgcon;volatile unsigned long *gpgdat;static struct timer_list buttons_timer;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 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] = {{S3C2410_GPF(0), 0x01},{S3C2410_GPF(3), 0x02},{S3C2410_GPG(5), 0x03},{S3C2410_GPG(6), 0x04},};static struct pin_desc *irq_pd;struct semaphore button_lock;/*  * 確定按索引值  */static irqreturn_t buttons_irq(int irq, void *dev_id){/* 20ms後啟動定時器 */irq_pd = (struct pin_desc *)dev_id;mod_timer(&buttons_timer, jiffies+HZ/50);return IRQ_RETVAL(IRQ_HANDLED);}static int wvm_buttons_drv_open(struct inode *inode, struct file *file){        static int IRQ_EINT8_r,IRQ_EINT11_r,IRQ_EINT13_r,IRQ_EINT14_r;if (file->f_flags & O_NONBLOCK){       if (down_trylock(&button_lock))return -EBUSY;}else{/* 擷取訊號量 */down(&button_lock);}/* 配置GPF0,2為輸入引腳 *//* 配置GPG3,11為輸入引腳 */IRQ_EINT8_r=request_irq(IRQ_EINT8,  buttons_irq, (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING), "S2", &pins_desc[0]);if (IRQ_EINT8_r)return -EBUSY;IRQ_EINT11_r=request_irq(IRQ_EINT11,  buttons_irq, (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING), "S3", &pins_desc[1]);if (IRQ_EINT11_r)return -EBUSY;IRQ_EINT13_r=request_irq(IRQ_EINT13, buttons_irq, (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING), "S4", &pins_desc[2]);if (IRQ_EINT13_r)return -EBUSY;IRQ_EINT14_r=request_irq(IRQ_EINT14, buttons_irq, (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING), "S5", &pins_desc[3]);if (IRQ_EINT14_r)return -EBUSY;return 0;}ssize_t wvm_buttons_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos) //應用中沒用到這樣的方式{int val;if (size != 1)return -EINVAL;if (file->f_flags & O_NONBLOCK){if (!ev_press)return -EAGAIN;}else{/* 假設沒有按鍵動作, 休眠 */wait_event_interruptible(button_waitq, ev_press);}/* 假設有按鍵動作, 返回索引值 */val=copy_to_user(buf, &key_val, 1);if(val)return -EAGAIN;ev_press = 0;return 1;}static int wvm_buttons_drv_fasync (int fd, struct file *filp, int on){printk("driver: wvm_buttons_fasync\n");return fasync_helper (fd, filp, on, &button_async);}int wvm_buttons_drv_close(struct inode *inode, struct file *file){//atomic_inc(&canopen);free_irq(IRQ_EINT8,  &pins_desc[0]);free_irq(IRQ_EINT11, &pins_desc[1]);free_irq(IRQ_EINT13, &pins_desc[2]);free_irq(IRQ_EINT14, &pins_desc[3]);up(&button_lock);return 0;}static struct file_operations wvm_buttons_drv_fops = {        .owner   =  THIS_MODULE,    /* 這是一個宏,推向編譯模組時自己主動建立的__this_module變數 */        .open    =  wvm_buttons_drv_open,     .read =  wvm_buttons_drv_read,   .release =  wvm_buttons_drv_close,.fasync =  wvm_buttons_drv_fasync,};static void buttons_timer_function(unsigned long data){struct pin_desc * pindesc = irq_pd;unsigned int pinval;if (!pindesc)return;pinval = s3c2410_gpio_getpin(pindesc->pin);if (pinval){/* 鬆開 */key_val = 0x80 | pindesc->key_val;}else{/* 按下 */key_val = pindesc->key_val;}        ev_press = 1;                  /* 表示中斷髮生了 */        wake_up_interruptible(&button_waitq);   /* 喚醒休眠的進程 */kill_fasync (&button_async, SIGIO, POLL_IN);}static int wvm_buttons_drv_init(void) //入口{init_timer(&buttons_timer);buttons_timer.function = buttons_timer_function;add_timer(&buttons_timer); major = register_chrdev(0, "wvm_buttons", &wvm_buttons_drv_fops);if(major < 0)        {          printk(  " wvm_buttons register falid!/n");          return major;        }wvm_buttons_class = class_create(THIS_MODULE, "wvm_buttons");if(IS_ERR(wvm_buttons_class))        {          printk( " wvm_buttons register class falid!/n");          return -1;        }/* 為了讓mdev依據這些資訊來建立裝置節點 */device_create(wvm_buttons_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/buttons */gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);gpfdat = gpfcon + 1;gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);gpgdat = gpgcon + 1;sema_init(&button_lock, 1);return 0;}static void wvm_buttons_drv_exit(void)  //出口{unregister_chrdev(major, "wvm_buttons");device_destroy(wvm_buttons_class, MKDEV(major, 0));class_destroy(wvm_buttons_class);iounmap(gpfcon);iounmap(gpgcon);}module_init(wvm_buttons_drv_init);module_exit(wvm_buttons_drv_exit);MODULE_LICENSE("GPL");


二.應用測試

#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>/* wvm_buttonstest   */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/buttons", O_RDWR);if (fd < 0){printf("can't open!\n");}fcntl(fd, F_SETOWN, getpid());Oflags = fcntl(fd, F_GETFL); fcntl(fd, F_SETFL, Oflags | FASYNC);while (1){sleep(1000);}return 0;}


遠程視頻監控之驅動篇(按鍵)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.