日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當前位置:首頁 > > 充電吧
[導讀]我們想來達到一個目的:同一時刻,只能有一個應用程序打開/dev/buttons一般的,我們想達到目的會想到下面這種方法:static?int?canopen?=?1; static?int?sixth

我們想來達到一個目的:同一時刻,只能有一個應用程序打開/dev/buttons

一般的,我們想達到目的會想到下面這種方法:

static?int?canopen?=?1;
static?int?sixth_drv_open(struct?inode?*inode,struct?file?*file)
{
????if(--canopen?!=?0)
????{
????????canopen++;
????????return?-EBUSY;
????}
}
static?int?sixth_drv_close?(struct?inode?*inode,?struct?file?*file)
{
????canopen++;
}

假設程序A來調(diào)用,那么進入open函數(shù),canopen = 0,if條件不成立;如果A沒有釋放,程序B來調(diào)用時候,canopen = -1,if條件成立,return -EBUSY;

原則上是可以實現(xiàn),但是我們進行–canopen操作實際上轉(zhuǎn)化程匯編使用三條指令(讀->改->寫)執(zhí)行的,由于Linux是多任務編程的。當我們程序A進行–canopen的讀取后,程序B剛好進行那么就有可能發(fā)生程序AB都能調(diào)用驅(qū)動!看下圖分析:

下面來介紹三種解決上面bug的方法 一、原子操作:

原子操作指的是在執(zhí)行過程中不會被別的代碼路徑所中斷的操作。
?常用原子操作函數(shù)舉例:

atomic_t?v?=?ATOMIC_INIT(0);?????//定義原子變量v并初始化為0
atomic_read(atomic_t?*v);????????//返回原子變量的值
void?atomic_inc(atomic_t?*v);????//原子變量增加1
void?atomic_dec(atomic_t?*v);????//原子變量減少1
int?atomic_dec_and_test(atomic_t?*v);?//自減操作后測試其是否為0,為0則返回true,否則返回false。

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

驅(qū)動函數(shù):sixth_drv.c

#include#include#include#include#include#include#include#include#include#include#include#includestatic?struct?fasync_struct?*button_async;
static?struct?class?*sixthdrv_class;
static?struct?class_device?*sixthdrv_class_dev;

static?DECLARE_WAIT_QUEUE_HEAD(button_waitq);

/*?中斷事件標志,?中斷服務程序?qū)⑺?,s3c24xx_sixth_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;
};

/*?鍵值:?按下時,0x01、0x02、0x03?*/
/*?鍵值:?松開時,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?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<key_val;
????????*gpfdat?&=?~((1<<4)?|?(1<<5)?|?(1<<6));
????}
????ev_press?=?1;????????????????/*?表示中斷發(fā)生了?*/
????wake_up_interruptible(&button_waitq);???/*?喚醒休眠的進程?*/
????kill_fasync?(&button_async,?SIGIO,?POLL_IN);
????return?IRQ_RETVAL(IRQ_HANDLED);
}


static?int?sixth_drv_open(struct?inode?*inode,struct?file?*file)
{
????if(!atomic_dec_and_test(&canopen))
????{
????????atomic_inc(&canopen);?
????????return?-EBUSY;

????}
????/*?配置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;
????/*?如果無按鍵動作發(fā)生,則進行休眠狀態(tài)?*/
????/*?如果ev_press等于0,休眠?*/
????wait_event_interruptible(button_waitq,ev_press);

????/*?如果有按鍵動作發(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);?
????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,?/*?這是一個宏,推向編譯模塊時自動創(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?sixth_drv_init(void)
{
????????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ū)動測試函數(shù):sixthdrvtest.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;
????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標記,
????//最終會調(diào)用到驅(qū)動的faync?>?fasync_helper:初始化/釋放fasync_struct
????//然后當按鍵按下時候,在irqreturn_t?buttons_irq中斷處理中調(diào)用kill_fasync
????while(1)
????{
????????sleep(1000);
????}
????return?0;
}

測試:

make
arm-linux-gcc?-o?sixthdrvtest?sixthdrvtest.c?
cp?sixthdrvtest?sixth_drv.ko?/work/nfs_root/czg
insmod?sixth_drv.ko
./sixthdrvtest?&


二、信號量:

信號量(semaphore)是用于保護臨界區(qū)的一種常用方法,只有得到信號量的進程才能執(zhí)行臨界區(qū)代碼。當獲取不到信號量時,進程進入休眠等待狀態(tài)。

//定義信號量
struct?semaphore?sem;
//初始化信號量
void?sema_init?(struct?semaphore?*sem,?int?val);
void?init_MUTEX(struct?semaphore?*sem);//初始化為0
static?DECLARE_MUTEX(button_lock);?????//定義互斥鎖
//獲得信號量
void?down(struct?semaphore?*?sem);
int?down_interruptible(struct?semaphore?*?sem);?
int?down_trylock(struct?semaphore?*?sem);
//釋放信號量
void?up(struct?semaphore?*?sem);

代碼只需要在上面的驅(qū)動程序sixth_drv.c稍做修改



static?DECLARE_MUTEX(button_lock);?????//定義互斥鎖
/*?獲?取?信?號?量?*/
down(&button_lock);
/*?釋放信號量?*/
up(&button_lock);



三、阻塞: 阻塞操作:

是指在執(zhí)行設備操作時若不能獲得資源則掛起進程,直到滿足可操作的條件后再進行操作。被掛起的進程進入休眠狀態(tài),被從調(diào)度器的運行隊列移走,直到等待的條件被滿足。

非阻塞操作:

進程在不能進行設備操作時并不掛起,它或者放棄,或者不停地查詢,直至可以進行操作為止。

代碼在上面的驅(qū)動程序sixth_drv.c稍做修改


static?int?sixth_drv_open(struct?inode?*inode,struct?file?*file)
{
????if?(file->f_flags?&??O_NONBLOCK)
????{
????????if?(down_trylock(&button_lock))
????????????return?-EBUSY;
????}
????else
????{
????????/*?獲?取?信?號?量?*/
????????down(&button_lock);
????}
}
/*************************************************/
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
????{
????/*?如果無按鍵動作發(fā)生,則進行休眠狀態(tài)?*/
????/*?如果ev_press等于0,休眠?*/
????wait_event_interruptible(button_waitq,ev_press);
????}
}

阻塞:

驅(qū)動測試代碼:sixthdrvtest.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標記,
????//最終會調(diào)用到驅(qū)動的faync?>?fasync_helper:初始化/釋放fasync_struct
????//然后當按鍵按下時候,在irqreturn_t?buttons_irq中斷處理中調(diào)用kill_fasync
????while(1)
????{
????????read(fd,&key_val,1);
????????printf("key_val:?0x%xn",key_val);
????????//sleep(1000);
????}
????return?0;
}

非阻塞:

驅(qū)動測試代碼:sixthdrvtest.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?|?O_NONBLOCK);
????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標記,
????//最終會調(diào)用到驅(qū)動的faync?>?fasync_helper:初始化/釋放fasync_struct
????//然后當按鍵按下時候,在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)機構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內(nèi)容真實性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時聯(lián)系本站刪除。
換一批
延伸閱讀

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

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

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

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

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

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

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

關(guān)鍵字: LED 設計 驅(qū)動電源

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

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

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

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

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

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

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

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

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

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

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

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