www.久久久久|狼友网站av天堂|精品国产无码a片|一级av色欲av|91在线播放视频|亚洲无码主播在线|国产精品草久在线|明星AV网站在线|污污内射久久一区|婷婷综合视频网站

當(dāng)前位置:首頁 > 芯聞號(hào) > 充電吧
[導(dǎo)讀] 先來看三種按鍵驅(qū)動(dòng)方式: 1、查詢方法:一直在查詢,不斷去查詢是否有事件發(fā)生,整個(gè)過程都是占用CPU資源,非常消耗CPU資源。 2、中斷方式:當(dāng)有事件發(fā)生時(shí),就去跳轉(zhuǎn)到相應(yīng)事件去處

先來看三種按鍵驅(qū)動(dòng)方式:

1、查詢方法:一直在查詢,不斷去查詢是否有事件發(fā)生,整個(gè)過程都是占用CPU資源,非常消耗CPU資源。 2、中斷方式:當(dāng)有事件發(fā)生時(shí),就去跳轉(zhuǎn)到相應(yīng)事件去處理,CPU占用時(shí)間少。 3、poll方式: 中斷方式雖然占用CPU資源少,但是在應(yīng)用程序上需要不斷在死循環(huán)里面執(zhí)行讀取函數(shù),應(yīng)用程序不能去做其它事情。poll機(jī)制解決了這個(gè)問題,當(dāng)有事件發(fā)生時(shí),才去執(zhí)行讀read函數(shù),按鍵事件沒有按下時(shí)<如果規(guī)定了時(shí)間,超過時(shí)間后返回?zé)o按鍵信息>,去執(zhí)行其它的處理函數(shù)。 poll機(jī)制分析 一、poll內(nèi)核框架: 所有的系統(tǒng)調(diào)用,都可以基于它的名字前加上“sys_”前綴,這就是它在內(nèi)核中對(duì)應(yīng)的函數(shù)。比如系統(tǒng)調(diào)用open、read、write、poll,與之對(duì)應(yīng)的內(nèi)核函數(shù)為:sys_open、sys_read、sys_write、sys_poll。

對(duì)于系統(tǒng)調(diào)用poll或select,它們對(duì)應(yīng)的內(nèi)核函數(shù)都是sys_poll。分析sys_poll,即可理解poll機(jī)制。

① sys_poll函數(shù)位于fs/Select.c文件中,代碼如下:
asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,
            long timeout_msecs)
{
    s64 timeout_jiffies;

    if (timeout_msecs > 0) {
#if HZ > 1000
        /* We can only overflow if HZ > 1000 */
        if (timeout_msecs / 1000 > (s64)0x7fffffffffffffffULL / (s64)HZ)
            timeout_jiffies = -1;
        else
#endif
            timeout_jiffies = msecs_to_jiffies(timeout_msecs);
    } else {
        /* Infinite (< 0) or no (0) timeout */
        timeout_jiffies = timeout_msecs;
    }

    return do_sys_poll(ufds, nfds, &timeout_jiffies);
}
它對(duì)超時(shí)參數(shù)稍作處理后,直接調(diào)用do_sys_poll。 ② do_sys_poll函數(shù)也位于位于fs/Select.c文件中,我們忽略其他代碼:
int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, s64 *timeout)
{
……
poll_initwait(&table);
……
    fdcount = do_poll(nfds, head, &table, timeout);
……
}
②-Ⅰ poll_initwait函數(shù)非常簡(jiǎn)單,它初始化一個(gè)poll_wqueues變量table:

poll_initwait > init_poll_funcptr(&pwq->pt, __pollwait); > pt->qproc = qproc;
table->pt->qproc = __pollwait,__pollwait將在驅(qū)動(dòng)的poll函數(shù)里用到。

②-Ⅱ do_poll函數(shù)位于fs/Select.c文件中,代碼如下:
static int do_poll(unsigned int nfds,  struct poll_list *list,
           struct poll_wqueues *wait, s64 *timeout)
{
01 ……
02  for (;;) {
03 ……
04              if (do_pollfd(pfd, pt)) {
05                  count++;
06                  pt = NULL;
07              }
08 ……
09      if (count || !*timeout || signal_pending(current))
10          break;
11      count = wait->error;
12      if (count)
13          break;
14
15      if (*timeout < 0) {
16          /* Wait indefinitely */
17          __timeout = MAX_SCHEDULE_TIMEOUT;
18      } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT-1)) {
19          /*
20           * Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in
21           * a loop
22           */
23          __timeout = MAX_SCHEDULE_TIMEOUT - 1;
24          *timeout -= __timeout;
25      } else {
26          __timeout = *timeout;
27          *timeout = 0;
28      }
29 
30      __timeout = schedule_timeout(__timeout);
31      if (*timeout >= 0)
32          *timeout += __timeout;
33  }
34  __set_current_state(TASK_RUNNING);
35  return count;
36 }
分析其中的代碼,可以發(fā)現(xiàn),它的作用如下: ① 從02行可以知道,這是個(gè)循環(huán),它退出的條件為: a. 09行的3個(gè)條件之一(count非0,超時(shí)、有信號(hào)等待處理) count非0表示04行的do_pollfd至少有一個(gè)成功。 b. 11、12行:發(fā)生錯(cuò)誤 ②重點(diǎn)在do_pollfd函數(shù),后面再分析 ③第30行,讓本進(jìn)程休眠一段時(shí)間,注意:應(yīng)用程序執(zhí)行poll調(diào)用后,如果①②的條件不滿足,進(jìn)程就會(huì)進(jìn)入休眠。那么,誰喚醒呢?除了休眠到指定時(shí)間被系統(tǒng)喚醒外,還可以被驅(qū)動(dòng)程序喚醒──記住這點(diǎn),這就是為什么驅(qū)動(dòng)的poll里要調(diào)用poll_wait的原因,后面分析。 ②-Ⅱ->do_pollfd函數(shù)位于fs/Select.c文件中,代碼如下:
static inline unsigned int do_pollfd(struct pollfd *pollfd, poll_table *pwait)
{
……
            if (file->f_op && file->f_op->poll)
                mask = file->f_op->poll(file, pwait);
……
}
可見,它就是調(diào)用我們的驅(qū)動(dòng)程序里注冊(cè)的poll函數(shù)。 二、驅(qū)動(dòng)程序: 驅(qū)動(dòng)程序里與poll相關(guān)的地方有兩處:一是構(gòu)造file_operation結(jié)構(gòu)時(shí),要定義自己的poll函數(shù)。二是通過poll_wait來調(diào)用上面說到的__pollwait函數(shù),pollwait的代碼如下:
static inline void poll_wait(struct file * filp, wait_queue_head_t * wait_address, poll_table *p)
{
    if (p && wait_address)
        p->qproc(filp, wait_address, p);
}
p->qproc就是__pollwait函數(shù),從它的代碼可知,它只是把當(dāng)前進(jìn)程掛入我們驅(qū)動(dòng)程序里定義的一個(gè)隊(duì)列里而已。它的代碼如下:
static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
                poll_table *p)
{
    struct poll_table_entry *entry = poll_get_entry(p);
    if (!entry)
        return;
    get_file(filp);
    entry->filp = filp;
    entry->wait_address = wait_address;
    init_waitqueue_entry(&entry->wait, current);
    add_wait_queue(wait_address, &entry->wait);
}
執(zhí)行到驅(qū)動(dòng)程序的poll_wait函數(shù)時(shí),進(jìn)程并沒有休眠,我們的驅(qū)動(dòng)程序里實(shí)現(xiàn)的poll函數(shù)是不會(huì)引起休眠的。讓進(jìn)程進(jìn)入休眠,是前面分析的do_sys_poll函數(shù)的30行“__timeout = schedule_timeout(__timeout)”。 poll_wait只是把本進(jìn)程掛入某個(gè)隊(duì)列,應(yīng)用程序調(diào)用poll > sys_poll > do_sys_poll > poll_initwait,do_poll > do_pollfd > 我們自己寫的poll函數(shù)后,再調(diào)用schedule_timeout進(jìn)入休眠。如果我們的驅(qū)動(dòng)程序發(fā)現(xiàn)情況就緒,可以把這個(gè)隊(duì)列上掛著的進(jìn)程喚醒??梢?,poll_wait的作用,只是為了讓驅(qū)動(dòng)程序能找到要喚醒的進(jìn)程。即使不用poll_wait,我們的程序也有機(jī)會(huì)被喚醒:chedule_timeout(__timeout),只是要休眠_(dá)_time_out這段時(shí)間。 總結(jié)一下poll機(jī)制:

1. poll > sys_poll > do_sys_poll > poll_initwait,poll_initwait函數(shù)注冊(cè)一下回調(diào)函數(shù)__pollwait,它就是我們的驅(qū)動(dòng)程序執(zhí)行poll_wait時(shí),真正被調(diào)用的函數(shù)。 2. 接下來執(zhí)行file->f_op->poll,即我們驅(qū)動(dòng)程序里自己實(shí)現(xiàn)的poll函數(shù),它會(huì)調(diào)用poll_wait把自己掛入某個(gè)隊(duì)列,這個(gè)隊(duì)列也是我們的驅(qū)動(dòng)自己定義的;它還判斷一下設(shè)備是否就緒。 3. 如果設(shè)備未就緒,do_sys_poll里會(huì)讓進(jìn)程休眠一定時(shí)間 4. 進(jìn)程被喚醒的條件有兩個(gè):一是上面說的“一定時(shí)間”到了,二是被驅(qū)動(dòng)程序喚醒。驅(qū)動(dòng)程序發(fā)現(xiàn)條件就緒時(shí),就把“某個(gè)隊(duì)列”上掛著的進(jìn)程喚醒,這個(gè)隊(duì)列,就是前面通過poll_wait把本進(jìn)程掛過去的隊(duì)列。 5. 如果驅(qū)動(dòng)程序沒有去喚醒進(jìn)程,那么chedule_timeout(__timeou)超時(shí)后,會(huì)重復(fù)2、3動(dòng)作,直到應(yīng)用程序的poll調(diào)用傳入的時(shí)間到達(dá)。 下面為用poll機(jī)制操控按鍵例子:

驅(qū)動(dòng)程序:fourth_drv.c

#include   
#include   
#include   
#include   
#include   
#include 
#include   
#include   
#include   
#include   
#include 
#include 

static struct class *fourthdrv_class;
static struct class_device *fourthdrv_class_dev;

static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

/* 中斷事件標(biāo)志, 中斷服務(wù)程序?qū)⑺?,s3c24xx_fourth_read將它清0 */
static volatile int ev_press = 0;

volatile unsigned long *gpfcon;
volatile unsigned long *gpfdat;
volatile unsigned long *gpgcon;
volatile unsigned long *gpgdat;

struct pin_desc{
    unsigned int pin;
    unsigned int key_val;
};

/* 鍵值: 按下時(shí),0x01、0x02、0x03 */
/* 鍵值: 松開時(shí),0x81、0x82、0x83 */
static unsigned char key_val;

struct pin_desc pin_desc[3] = {
    {S3C2410_GPF0,0X01},
    {S3C2410_GPF2,0X02},
    {S3C2410_GPG3,0X03},
};


static irqreturn_t buttons_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)
    {
        /* 松開 */
        key_val = 0x80 | (pindesc->key_val);
        *gpfdat |= ((1<<4) | (1<<5) | (1<<6));
    }
    else
    {
        /* 按下 */
        key_val = pindesc->key_val;
        *gpfdat &= ~((1<<4) | (1<<5) | (1<<6));
    }
    ev_press = 1;                /* 表示中斷發(fā)生了 */
    wake_up_interruptible(&button_waitq);   /* 喚醒休眠的進(jìn)程 */
    return IRQ_RETVAL(IRQ_HANDLED);
}


static int fourth_drv_open(struct inode *inode,struct file *file)
{
    /* 配置GPF0,2、GPG3為中斷引腳 */
    request_irq(IRQ_EINT0, buttons_irq,IRQT_BOTHEDGE,"s2",&pin_desc[0]);
    request_irq(IRQ_EINT2, buttons_irq,IRQT_BOTHEDGE,"s3",&pin_desc[1]);
    request_irq(IRQ_EINT11,buttons_irq,IRQT_BOTHEDGE,"s4",&pin_desc[2]);
    /* 配置GPF4、5、6為輸入引腳 */
    *gpfcon &= ~((0x3<<4*2) | (0x3<<5*2) | (0x3<<6*2));
    *gpfcon |=  ((1<<4*2) | (1<<5*2) | (1<<6*2));
    return 0;
}

static ssize_t fourth_drv_read (struct file *file, char __user *buf, size_t count, loff_t *ppos)
{
    //看用戶需要讀取的空間,和這里的是否相同
    if(count != 1)
        return -EINVAL;
    /* 如果無按鍵動(dòng)作發(fā)生,則進(jìn)行休眠狀態(tài) */
    /* 如果ev_press等于0,休眠 */
    wait_event_interruptible(button_waitq,ev_press);

    /* 如果有按鍵動(dòng)作發(fā)生,則返回按鍵的值 */
    copy_to_user(buf,&key_val,1);
    ev_press = 0;

    return 1;
}

static int fourth_drv_close (struct inode *inode, struct file *file)
{
    free_irq(IRQ_EINT0,  &pin_desc[0]);
    free_irq(IRQ_EINT2,  &pin_desc[1]);
    free_irq(IRQ_EINT11, &pin_desc[2]);
    return 0;

}

static unsigned int fourth_drv_poll(struct file *file, struct poll_table_struct *wait)
{
    unsigned int mask = 0;

    poll_wait(file, &button_waitq, wait);

    if (ev_press)
        mask |= POLLIN | POLLRDNORM;   
    return mask;
}

static struct file_operations fourth_drv_fops = {
    .owner = THIS_MODULE,   /* 這是一個(gè)宏,推向編譯模塊時(shí)自動(dòng)創(chuàng)建的__this_module變量 */
    .open  = fourth_drv_open,
    .read = fourth_drv_read,
    .release = fourth_drv_close,
    .poll = fourth_drv_poll
};

int major; 

static int fourth_drv_init(void)
{
        major = register_chrdev(0,"fourth_drv",&fourth_drv_fops);
        fourthdrv_class = class_create(THIS_MODULE,"fourthdrv");
        fourthdrv_class_dev = class_device_create(fourthdrv_class,NULL,MKDEV(major,0),NULL,"buttons");
        gpfcon = (volatile unsigned long *)ioremap(0x56000050,16);
        gpfdat = gpfcon + 1;
        gpgcon = (volatile unsigned long *)ioremap(0x56000060,16);
        gpgdat = gpgcon + 1;
        return 0;
}

static int fourth_drv_exit(void)
{
        unregister_chrdev(major,"fourth_drv");
        class_device_unregister(fourthdrv_class_dev);
        class_destroy(fourthdrv_class);
        iounmap(gpfcon);
        iounmap(gpgcon);
        return 0;
}

module_init(fourth_drv_init);
module_exit(fourth_drv_exit);
MODULE_LICENSE("GPL");

驅(qū)動(dòng)測(cè)試程序:fourthdrvtest.c

#include   
#include   
#include   
#include   
#include 

/* 
 * fourthdrvtest
 */

int main(int argc, char **argv)
{
    int fd;
    unsigned char key_val;
    struct pollfd fds[1];
    int ret;
    fd = open("/dev/buttons",O_RDWR);
    if(fd < 0)
    {
        printf("can't open!n");
    }
    fds[0].fd = fd;
    fds[0].events = POLLIN;
    while(1)
    {
        ret = poll(fds,1,5000);
        if(ret == 0)
        {
            printf("Time out!n");

        }
        else
        {
        read(fd,&key_val,1);
        printf("key_val = 0x%xn",key_val);
        }
    }

    return 0;
}

Makefile

KERN_DIR = /work/system/linux-2.6.22.6

all:
    make -C $(KERN_DIR) M=`pwd` modules 

clean:
    make -C $(KERN_DIR) M=`pwd` modules clean
    rm -rf modules.order

obj-m += fourth_drv.o

測(cè)試命令:

make
arm-linux-gcc -o fourthdrvtest fourthdrvtest.c
cp fourthdrvtest fourth_drv.ko /work/nfs_root/czg
insmod fourth_drv.ko
./fourthdrvtest &
top

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車的華為或?qū)⒋呱龈蟮莫?dú)角獸公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

倫敦2024年8月29日 /美通社/ -- 英國(guó)汽車技術(shù)公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車工程師從創(chuàng)意到認(rèn)證的所有需求的工具,可用于創(chuàng)建軟件定義汽車。 SODA V工具的開發(fā)耗時(shí)1.5...

關(guān)鍵字: 汽車 人工智能 智能驅(qū)動(dòng) BSP

北京2024年8月28日 /美通社/ -- 越來越多用戶希望企業(yè)業(yè)務(wù)能7×24不間斷運(yùn)行,同時(shí)企業(yè)卻面臨越來越多業(yè)務(wù)中斷的風(fēng)險(xiǎn),如企業(yè)系統(tǒng)復(fù)雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務(wù)連續(xù)性,提升韌性,成...

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報(bào)道,騰訊和網(wǎng)易近期正在縮減他們對(duì)日本游戲市場(chǎng)的投資。

關(guān)鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國(guó)國(guó)際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)開幕式在貴陽舉行,華為董事、質(zhì)量流程IT總裁陶景文發(fā)表了演講。

關(guān)鍵字: 華為 12nm EDA 半導(dǎo)體

8月28日消息,在2024中國(guó)國(guó)際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)上,華為常務(wù)董事、華為云CEO張平安發(fā)表演講稱,數(shù)字世界的話語權(quán)最終是由生態(tài)的繁榮決定的。

關(guān)鍵字: 華為 12nm 手機(jī) 衛(wèi)星通信

要點(diǎn): 有效應(yīng)對(duì)環(huán)境變化,經(jīng)營(yíng)業(yè)績(jī)穩(wěn)中有升 落實(shí)提質(zhì)增效舉措,毛利潤(rùn)率延續(xù)升勢(shì) 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務(wù)引領(lǐng)增長(zhǎng) 以科技創(chuàng)新為引領(lǐng),提升企業(yè)核心競(jìng)爭(zhēng)力 堅(jiān)持高質(zhì)量發(fā)展策略,塑強(qiáng)核心競(jìng)爭(zhēng)優(yōu)勢(shì)...

關(guān)鍵字: 通信 BSP 電信運(yùn)營(yíng)商 數(shù)字經(jīng)濟(jì)

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺(tái)與中國(guó)電影電視技術(shù)學(xué)會(huì)聯(lián)合牽頭組建的NVI技術(shù)創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會(huì)上宣布正式成立。 活動(dòng)現(xiàn)場(chǎng) NVI技術(shù)創(chuàng)新聯(lián)...

關(guān)鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長(zhǎng)三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會(huì)上,軟通動(dòng)力信息技術(shù)(集團(tuán))股份有限公司(以下簡(jiǎn)稱"軟通動(dòng)力")與長(zhǎng)三角投資(上海)有限...

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉
關(guān)閉