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

當(dāng)前位置:首頁 > 芯聞號(hào) > 充電吧
[導(dǎo)讀] 當(dāng)我們用阻塞的方式來驅(qū)動(dòng)按鍵時(shí): 按下按鍵由于機(jī)械按鍵的波動(dòng)性,會(huì)導(dǎo)致中斷處理函數(shù)發(fā)生多次。 我們可以采用定時(shí)器來解決此bug: Makefile: KER

當(dāng)我們用阻塞的方式來驅(qū)動(dòng)按鍵時(shí):

按下按鍵由于機(jī)械按鍵的波動(dòng)性,會(huì)導(dǎo)致中斷處理函數(shù)發(fā)生多次。


我們可以采用定時(shí)器來解決此bug:

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 += seventh_drv.o

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

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

static struct fasync_struct *button_async;
static struct class *sixthdrv_class;
static struct class_device *sixthdrv_class_dev;

static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

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

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},
};

//atomic_t canopen = ATOMIC_INIT(1);//定義原子變量canopen并初始化為1
static DECLARE_MUTEX(button_lock);     //定義互斥鎖

static struct pin_desc *irq_pd;

static irqreturn_t buttons_irq(int irq, void *dev_id)
{
    /* 設(shè)置定時(shí)器,發(fā)生中斷10ms后再去讀電平值 */
    irq_pd = (struct pin_desc *)dev_id;
    mod_timer(&buttons_timer,jiffies+HZ/100);
    return IRQ_RETVAL(IRQ_HANDLED);
}


static int sixth_drv_open(struct inode *inode,struct file *file)
{
#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
    {
        /* 獲 取 信 號(hào) 量 */
        down(&button_lock);
    }
    /* 配置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 sixth_drv_read (struct file *file, char __user *buf, size_t count, loff_t *ppos)
{
    //看用戶需要讀取的空間,和這里的是否相同
    if(count != 1)
        return -EINVAL;
    if(file->f_flags & O_NONBLOCK)
    {
        if(!ev_press)
            return -EAGAIN;
    }
    else
    {
    /* 如果無按鍵動(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 sixth_drv_close (struct inode *inode, struct file *file)
{
    //atomic_inc(&canopen); 
    /* 釋放信號(hào)量 */
    up(&button_lock);
    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 sixth_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 int sixth_drv_fasync (int fd, struct file *filp, int on)
{
    printk("driver: sixth_drv_fasyncn");
    return fasync_helper (fd, filp, on, &button_async);
}

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

int major; 

static int 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);
        *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)程 */
    kill_fasync (&button_async, SIGIO, POLL_IN);

}

static int sixth_drv_init(void)
{
        init_timer(&buttons_timer);
        buttons_timer.function = buttons_timer_function;
        major = register_chrdev(0,"sixth_drv",&sixth_drv_fops);
        sixthdrv_class = class_create(THIS_MODULE,"sixthdrv");
        sixthdrv_class_dev = class_device_create(sixthdrv_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 sixth_drv_exit(void)
{
        unregister_chrdev(major,"sixth_drv");
        class_device_unregister(sixthdrv_class_dev);
        class_destroy(sixthdrv_class);
        iounmap(gpfcon);
        iounmap(gpgcon);
        return 0;
}

module_init(sixth_drv_init);
module_exit(sixth_drv_exit);
MODULE_LICENSE("GPL");

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

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

    /* 
     * sixthdrvtest
     */
    int fd;

    void czg_signal_handler(int signum)
    {
        unsigned char key_val = 0;
        read(fd,&key_val,1);
        printf("key_val: 0x%xn",key_val);
    }

    int main(int argc, char **argv)
    {
        int ret;
        int oflags;
        unsigned char key_val = 0;
        //signal(SIGIO,czg_signal_handler);
        fd = open("/dev/buttons",O_RDWR);
        if(fd < 0)
        {
            printf("can't open!n");
            return -1;
        }
        //fcntl(fd,F_SETOWN,getpid()); // 告訴內(nèi)核,發(fā)給誰
        //oflags = fcntl(fd,F_GETFL);
        //fcntl(fd,F_SETFL,oflags | FASYNC); // 改變fasync標(biāo)記,
        //最終會(huì)調(diào)用到驅(qū)動(dòng)的faync > fasync_helper:初始化/釋放fasync_struct
        //然后當(dāng)按鍵按下時(shí)候,在irqreturn_t buttons_irq中斷處理中調(diào)用kill_fasync
        while(1)
        {
            ret = read(fd,&key_val,1);
            printf("key_val: 0x%x,ret = %dn",key_val,ret);
            //sleep(4);
        }
        return 0;
    }

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

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

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

加利福尼亞州圣克拉拉縣2024年8月30日 /美通社/ -- 數(shù)字化轉(zhuǎn)型技術(shù)解決方案公司Trianz今天宣布,該公司與Amazon Web Services (AWS)簽訂了...

關(guān)鍵字: AWS AN BSP 數(shù)字化

倫敦2024年8月29日 /美通社/ -- 英國汽車技術(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)易近期正在縮減他們對日本游戲市場的投資。

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

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

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

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

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

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

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

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

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

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

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