當前位置:
首頁 >
字符驱动之按键(四:poll机制)
發(fā)布時間:2023/12/19
28
豆豆
生活随笔
收集整理的這篇文章主要介紹了
字符驱动之按键(四:poll机制)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1 采用之前的中斷按鍵法,程序會一直在read函數(shù)中死循環(huán)。
2 使用了poll之后,在一段時間內(nèi)如果有按鍵按下就會返回,如果沒有按鍵按下等時間到再返回。
3
4 應用程序的open,read,write,poll分別對應了驅(qū)動程序的open,read,write和poll。
5 首先,增加fileoperation結(jié)構(gòu)體。
6
7 static struct file_operations third_drv_fops = {
8 .owner = THIS_MODULE,
9 .open = third_drv_open,
10 .read = third_drv_read,
11 .write = third_drv_write,
12 .poll = third_drv_poll,
13 };
14
15 copy的poll函數(shù)如下:
16 staitc unsigned mounts_poll(struct file *file,poll_table *wait)
17 {
18 struct proc_mounts *p = file->private_data;
19 struct mnt_namespace *ns = p->m.private;
20 unsigned res = 0;
21
22 poll_wait(file,&ns->poll,wait);
23
24 spin_lock(&vfsmount_lock);
25 if(p->event != ns->event)
26 {
27 p->event = ns->event;
28 res = POLLERR;
29 }
30 spin_unlock(&vfsmount_lock);
31 return res;
32 }
33
34 其中的核心部分為:
35 poll_wait(file,&ns->poll,wait);
36
37
38 static unsigned forth_drv_poll(struct file *file,poll_table *wait)
39 {
40 unsigned mask = 0;
41 poll_wait(file,&button_waitq,wait); //不會立刻進入休眠
42
43 if(ev_press)
44 mask |= POLLIN | POLLRDNORM;
45 }
46
47 系統(tǒng)調(diào)用poll時,它們對應的內(nèi)核函數(shù)是sys_poll。
48 sys_poll核心是調(diào)用了do_sys_poll函數(shù)。
49
50 app: 調(diào)用poll函數(shù)
51 kernel: 調(diào)用sys_poll函數(shù)
52 do_sys_poll(ufds,nfds,&timeout_jiffies);//第三個參數(shù)是超時參數(shù)。
53 poll_initwait(&table);
54 do_poll(nfds,head,&table,timeout);
55
56
57 修改后的測試程序:
58 int main(int argc,char** argv)
59 {
60 int fd;
61 unsigned char key_val;
62
63 struct pollfd fds[1]; //需要檢測的文件個數(shù)為一
64
65 fd = open("/dev/buttons",O_RDWR);
66 if(fd < 0)
67 printf("can't open buttons\n");
68 //poll相關設置
69 fds[0].fd = fd; //打開的文件為fd
70 fds.[0].enents = POLLIN; //pollin表示有數(shù)據(jù)待讀取
71
72 while(1)
73 {
74 ret = poll(fds,1,5000); //參數(shù)分別為fds(結(jié)構(gòu)體),1為文件個數(shù),5000為事件。
75 //返回值為0表示超時
76 if(ret == 0)
77 {
78 printf("timeout!\n");
79 }
80 read(fd,&key_val,1);
81 printf("key_val = 0x%x\n",key_val);
82 }
83 }
?
轉(zhuǎn)載于:https://www.cnblogs.com/stm32f4/p/8351192.html
總結(jié)
以上是生活随笔為你收集整理的字符驱动之按键(四:poll机制)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: xp系统web服务器搭建教程,Windo
- 下一篇: 服务器芯片采购,服务器采购具体要求.pd