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

當(dāng)前位置:首頁 > > 充電吧
[導(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)測(cè)試程序: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)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除。
換一批
延伸閱讀

LED驅(qū)動(dòng)電源的輸入包括高壓工頻交流(即市電)、低壓直流、高壓直流、低壓高頻交流(如電子變壓器的輸出)等。

關(guān)鍵字: 驅(qū)動(dòng)電源

在工業(yè)自動(dòng)化蓬勃發(fā)展的當(dāng)下,工業(yè)電機(jī)作為核心動(dòng)力設(shè)備,其驅(qū)動(dòng)電源的性能直接關(guān)系到整個(gè)系統(tǒng)的穩(wěn)定性和可靠性。其中,反電動(dòng)勢(shì)抑制與過流保護(hù)是驅(qū)動(dòng)電源設(shè)計(jì)中至關(guān)重要的兩個(gè)環(huán)節(jié),集成化方案的設(shè)計(jì)成為提升電機(jī)驅(qū)動(dòng)性能的關(guān)鍵。

關(guān)鍵字: 工業(yè)電機(jī) 驅(qū)動(dòng)電源

LED 驅(qū)動(dòng)電源作為 LED 照明系統(tǒng)的 “心臟”,其穩(wěn)定性直接決定了整個(gè)照明設(shè)備的使用壽命。然而,在實(shí)際應(yīng)用中,LED 驅(qū)動(dòng)電源易損壞的問題卻十分常見,不僅增加了維護(hù)成本,還影響了用戶體驗(yàn)。要解決這一問題,需從設(shè)計(jì)、生...

關(guān)鍵字: 驅(qū)動(dòng)電源 照明系統(tǒng) 散熱

根據(jù)LED驅(qū)動(dòng)電源的公式,電感內(nèi)電流波動(dòng)大小和電感值成反比,輸出紋波和輸出電容值成反比。所以加大電感值和輸出電容值可以減小紋波。

關(guān)鍵字: LED 設(shè)計(jì) 驅(qū)動(dòng)電源

電動(dòng)汽車(EV)作為新能源汽車的重要代表,正逐漸成為全球汽車產(chǎn)業(yè)的重要發(fā)展方向。電動(dòng)汽車的核心技術(shù)之一是電機(jī)驅(qū)動(dòng)控制系統(tǒng),而絕緣柵雙極型晶體管(IGBT)作為電機(jī)驅(qū)動(dòng)系統(tǒng)中的關(guān)鍵元件,其性能直接影響到電動(dòng)汽車的動(dòng)力性能和...

關(guān)鍵字: 電動(dòng)汽車 新能源 驅(qū)動(dòng)電源

在現(xiàn)代城市建設(shè)中,街道及停車場(chǎng)照明作為基礎(chǔ)設(shè)施的重要組成部分,其質(zhì)量和效率直接關(guān)系到城市的公共安全、居民生活質(zhì)量和能源利用效率。隨著科技的進(jìn)步,高亮度白光發(fā)光二極管(LED)因其獨(dú)特的優(yōu)勢(shì)逐漸取代傳統(tǒng)光源,成為大功率區(qū)域...

關(guān)鍵字: 發(fā)光二極管 驅(qū)動(dòng)電源 LED

LED通用照明設(shè)計(jì)工程師會(huì)遇到許多挑戰(zhàn),如功率密度、功率因數(shù)校正(PFC)、空間受限和可靠性等。

關(guān)鍵字: LED 驅(qū)動(dòng)電源 功率因數(shù)校正

在LED照明技術(shù)日益普及的今天,LED驅(qū)動(dòng)電源的電磁干擾(EMI)問題成為了一個(gè)不可忽視的挑戰(zhàn)。電磁干擾不僅會(huì)影響LED燈具的正常工作,還可能對(duì)周圍電子設(shè)備造成不利影響,甚至引發(fā)系統(tǒng)故障。因此,采取有效的硬件措施來解決L...

關(guān)鍵字: LED照明技術(shù) 電磁干擾 驅(qū)動(dòng)電源

開關(guān)電源具有效率高的特性,而且開關(guān)電源的變壓器體積比串聯(lián)穩(wěn)壓型電源的要小得多,電源電路比較整潔,整機(jī)重量也有所下降,所以,現(xiàn)在的LED驅(qū)動(dòng)電源

關(guān)鍵字: LED 驅(qū)動(dòng)電源 開關(guān)電源

LED驅(qū)動(dòng)電源是把電源供應(yīng)轉(zhuǎn)換為特定的電壓電流以驅(qū)動(dòng)LED發(fā)光的電壓轉(zhuǎn)換器,通常情況下:LED驅(qū)動(dòng)電源的輸入包括高壓工頻交流(即市電)、低壓直流、高壓直流、低壓高頻交流(如電子變壓器的輸出)等。

關(guān)鍵字: LED 隧道燈 驅(qū)動(dòng)電源
關(guān)閉