常用的處理并發(fā)和競爭的機(jī)制有四種,原子操作、自旋鎖、信號量和互斥體。下邊就通過編寫驅(qū)動來實(shí)現(xiàn),展示一下相關(guān)效果。當(dāng)前臺的應(yīng)用一直運(yùn)行,控制臺是不能輸入指令,測試并發(fā)與競爭最好是在后臺運(yùn)行,而解決并非與競爭最直接的手段就是只允許一個應(yīng)用去調(diào)用相關(guān)資源,這里為了好展示效果就通過任務(wù)運(yùn)行來體現(xiàn)。下面的實(shí)驗(yàn)使用了新字符驅(qū)動GPIO源碼,只需要復(fù)制一份即可使用。
|原子操作
驅(qū)動源碼
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
/*添加頭文件*/
#include
#include
#include
#include
#defineCHRDEVBASE_CNT1/*設(shè)備號個數(shù)*/
#defineCHRDEVBASE_NAME"chrdevbase"/*名字*/
#defineLEDOFF0/*關(guān)燈*/
#defineLEDON1/*開燈*/
/*chrdevbase設(shè)備結(jié)構(gòu)體*/
structnewchr_dev{
dev_tdevid;/*設(shè)備號*/
structcdevcdev;/*cdev*/
structclass*class;/*類*/
structdevice*device;/*設(shè)備*/
intmajor;/*主設(shè)備號*/
intminor;/*次設(shè)備號*/
structdevice_node*nd;/*設(shè)備節(jié)點(diǎn)*/
intled_gpio;/*led所使用的GPIO編號*/
atomic_tlock;/*原子變量*/
};
structnewchr_devchrdevbase;/*自定義字符設(shè)備*/
/*
*@description:LED硬件初始化
*@param:無
*@return:無
*/
staticintled_hal_init(void)
{
intret=0;
/*設(shè)置LED所使用的GPIO*/
/* 1、獲取設(shè)備節(jié)點(diǎn):gpioled */
chrdevbase.nd=of_find_node_by_path("/gpioled");
if(chrdevbase.nd==NULL){
printk("chrdevbasenodecantnotfound!
");
return-EINVAL;
}else{
printk("chrdevbasenodehasbeenfound!
");
}
/*2、獲取設(shè)備樹中的gpio屬性,得到LED所使用的LED編號*/
chrdevbase.led_gpio=of_get_named_gpio(chrdevbase.nd,"led-gpio",0);
if(chrdevbase.led_gpio0){
printk("can'tgetled-gpio");
return-EINVAL;
}
printk("led-gpionum=%d
",chrdevbase.led_gpio);
/*3、設(shè)置GPIO1_IO03為輸出,并且輸出高電平,默認(rèn)關(guān)閉LED燈*/
ret=gpio_direction_output(chrdevbase.led_gpio,1);
if(ret0){
printk("can'tsetgpio!
");
}
return0;
}
/*
*@description:打開設(shè)備
*@param-inode:傳遞給驅(qū)動的inode
*@param-filp:設(shè)備文件,file結(jié)構(gòu)體有個叫做private_data的成員變量
*一般在open的時候?qū)rivate_data指向設(shè)備結(jié)構(gòu)體。
*@return:0成功;其他失敗
*/
staticintchrdevbase_open(structinode*inode,structfile*filp)
{
/*通過判斷原子變量的值來檢查LED有沒有被別的應(yīng)用使用*/
if(!atomic_dec_and_test(&chrdevbase.lock))
{
atomic_inc(&chrdevbase.lock);/*小于0的話就加1,使其原子變量等于0*/
return-EBUSY;/*LED被使用,返回忙*/
}
printk("[BSP]chrdevbaseopen!
");
filp->private_data=&chrdevbase;/*設(shè)置私有數(shù)據(jù)*/
return0;
}
/*
*@description:從設(shè)備讀取數(shù)據(jù)
*@param-filp:要打開的設(shè)備文件(文件描述符)
*@param-buf:返回給用戶空間的數(shù)據(jù)緩沖區(qū)
*@param-cnt:要讀取的數(shù)據(jù)長度
*@param-offt:相對于文件首地址的偏移
*@return:讀取的字節(jié)數(shù),如果為負(fù)值,表示讀取失敗
*/
staticssize_tchrdevbase_read(structfile*filp,char__user*buf,size_tcnt,loff_t*offt)
{
printk("chrdevbaseread!
");
return0;
}
/*
*@description:向設(shè)備寫數(shù)據(jù)
*@param-filp:設(shè)備文件,表示打開的文件描述符
*@param-buf:要寫給設(shè)備寫入的數(shù)據(jù)
*@param-cnt:要寫入的數(shù)據(jù)長度
*@param-offt:相對于文件首地址的偏移
*@return:寫入的字節(jié)數(shù),如果為負(fù)值,表示寫入失敗
*/
staticssize_tchrdevbase_write(structfile*filp,constchar__user*buf,size_tcnt,loff_t*offt)
{
intretvalue=0;
charwritebuf[1];
structnewchr_dev*dev=filp->private_data;
/*接收用戶空間傳遞給內(nèi)核的數(shù)據(jù)并且打印出來*/
retvalue=copy_from_user(writebuf,buf,cnt);
printk("[BSP]kernelrecevdatadata:%d!
",writebuf[0]);
if(writebuf[0]==LEDON){
gpio_set_value(dev->led_gpio,0);/*打開LED燈*/
}elseif(writebuf[0]==LEDOFF){
gpio_set_value(dev->led_gpio,1);/*關(guān)閉LED燈*/
}
//printk("chrdevbasewrite!
");
return0;
}
/*
*@description:關(guān)閉/釋放設(shè)備
*@param-filp:要關(guān)閉的設(shè)備文件(文件描述符)
*@return:0成功;其他失敗
*/
staticintchrdevbase_release(structinode*inode,structfile*filp)
{
structnewchr_dev*dev=filp->private_data;
/*關(guān)閉驅(qū)動文件的時候釋放原子變量*/
atomic_inc(&dev->lock);
printk("[BSP]release!
");
return0;
}
/*
*設(shè)備操作函數(shù)結(jié)構(gòu)體
*/
staticstructfile_operationschrdevbase_fops={
.owner=THIS_MODULE,
.open=chrdevbase_open,
.read=chrdevbase_read,
.write=chrdevbase_write,
.release=chrdevbase_release,
};
/*
*@description:驅(qū)動入口函數(shù)
*@param:無
*@return:0成功;其他失敗
*/
staticint__initchrdevbase_init(void)
{
/*初始化原子變量*/
atomic_set(&chrdevbase.lock,1);/*原子變量初始值為1*/
/*初始化硬件*/
led_hal_init();
/*注冊字符設(shè)備驅(qū)動*/
/*1、創(chuàng)建設(shè)備號*/
if(chrdevbase.major){/*定義了設(shè)備號*/
chrdevbase.devid=MKDEV(chrdevbase.major,0);
register_chrdev_region(chrdevbase.devid,CHRDEVBASE_CNT,CHRDEVBASE_NAME);
}else{/*沒有定義設(shè)備號*/
alloc_chrdev_region(&chrdevbase.devid,0,CHRDEVBASE_CNT,CHRDEVBASE_NAME);/*申請?jiān)O(shè)備號*/
chrdevbase.major=MAJOR(chrdevbase.devid);/*獲取主設(shè)備號*/
chrdevbase.minor=MINOR(chrdevbase.devid);/*獲取次設(shè)備號*/
}
printk("newcheledmajor=%d,minor=%d
",chrdevbase.major,chrdevbase.minor);
/*2、初始化cdev*/
chrdevbase.cdev.owner=THIS_MODULE;
cdev_init(&chrdevbase.cdev,&chrdevbase_fops);
/*3、添加一個cdev*/
cdev_add(&chrdevbase.cdev,chrdevbase.devid,CHRDEVBASE_CNT);
/*4、創(chuàng)建類*/
chrdevbase.class=class_create(THIS_MODULE,CHRDEVBASE_NAME);
if(IS_ERR(chrdevbase.class)){
returnPTR_ERR(chrdevbase.class);
}
/*5、創(chuàng)建設(shè)備*/
chrdevbase.device=device_create(chrdevbase.class,NULL,chrdevbase.devid,NULL,CHRDEVBASE_NAME);
if(IS_ERR(chrdevbase.device)){
returnPTR_ERR(chrdevbase.device);
}
return0;
}
/*
*@description:驅(qū)動出口函數(shù)
*@param:無
*@return:無
*/
staticvoid__exitchrdevbase_exit(void)
{
/*注銷字符設(shè)備*/
cdev_del(&chrdevbase.cdev);/*刪除cdev*/
unregister_chrdev_region(chrdevbase.devid,CHRDEVBASE_CNT);/*注銷設(shè)備號*/
device_destroy(chrdevbase.class,chrdevbase.devid);/*銷毀設(shè)備*/
class_destroy(chrdevbase.class);/*銷毀類*/
printk("[BSP]chrdevbaseexit!
");
}
/*
*將上面兩個函數(shù)指定為驅(qū)動的入口和出口函數(shù)
*/
module_init(chrdevbase_init);
module_exit(chrdevbase_exit);
/*
*LICENSE和作者信息
*/
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zuozhongkai");
應(yīng)用源碼
#include"stdio.h"
#include"unistd.h"
#include"sys/types.h"
#include"sys/stat.h"
#include"fcntl.h"
#include"stdlib.h"
#include"string.h"
/*
*@description:main主程序
*@param-argc:argv數(shù)組元素個數(shù)
*@param-argv:具體參數(shù)
*@return:0成功;其他失敗
*/
intmain(intargc,char*argv[])
{
intfd,retvalue;
char*filename;
charwritebuf[100];
unsignedchardatabuf[1];
unsignedcharcnt=0;
if(argc!=3){
printf("[APP]ErrorUsage!
");
return-1;
}
filename=argv[1];
/*打開驅(qū)動文件*/
fd=open(filename,O_RDWR);
if(fd0){
printf("[APP]Can'topenfile%s
",filename);
return-1;
}
/*把第三個參數(shù)賦值給databuf*/
databuf[0]=atoi(argv[2]);
/*向設(shè)備驅(qū)動寫數(shù)據(jù)*/
memcpy(writebuf,databuf,sizeof(databuf));
retvalue=write(fd,writebuf,sizeof(databuf));
if(retvalue0){
printf("[APP]writefile%sfailed!
",filename);
}
/*模擬占用25SLED*/
while(1)
{
sleep(5);
cnt++;
printf("Apprunningtimes:%d
",cnt);
if(cnt>=5)break;
}
printf("Apprunningfinished!");
/*關(guān)閉設(shè)備*/
retvalue=close(fd);
if(retvalue0){
printf("[APP]Can'tclosefile%s
",filename);
return-1;
}
return0;
}
實(shí)驗(yàn)現(xiàn)象
加了原子操作后,應(yīng)用程序運(yùn)行時,再次觸發(fā)是不能運(yùn)行的,這就解決了在復(fù)雜環(huán)境下的并發(fā)和競爭的問題。注意不加“&”表示直接運(yùn)行,控制臺不能輸入指令,加了“&”表示后臺運(yùn)行,可以繼續(xù)輸入指令。
套路分析
1、先在結(jié)構(gòu)體定義一個變量
/*chrdevbase設(shè)備結(jié)構(gòu)體*/
structnewchr_dev{
......
atomic_tlock;/*原子變量*/
};
2、在驅(qū)動入口初始化原子變量
/*
*@description:驅(qū)動入口函數(shù)
*@param:無
*@return:0成功;其他失敗
*/
staticint__initchrdevbase_init(void)
{
/*初始化原子變量*/
atomic_set(&chrdevbase.lock,1);/*原子變量初始值為1*/
......
return0;
}
3、在打開設(shè)備時判斷原子變量
/*
*@description:打開設(shè)備
*@param-inode:傳遞給驅(qū)動的inode
*@param-filp:設(shè)備文件,file結(jié)構(gòu)體有個叫做private_data的成員變量
*一般在open的時候?qū)rivate_data指向設(shè)備結(jié)構(gòu)體。
*@return:0成功;其他失敗
*/
staticintchrdevbase_open(structinode*inode,structfile*filp)
{
/*通過判斷原子變量的值來檢查LED有沒有被別的應(yīng)用使用*/
if(!atomic_dec_and_test(&chrdevbase.lock))
{
atomic_inc(&chrdevbase.lock);/*小于0的話就加1,使其原子變量等于0*/
return-EBUSY;/*LED被使用,返回忙*/
}
......
return0;
}
4、在釋放設(shè)備時釋放原子變量
/*
*@description:關(guān)閉/釋放設(shè)備
*@param-filp:要關(guān)閉的設(shè)備文件(文件描述符)
*@return:0成功;其他失敗
*/
staticintchrdevbase_release(structinode*inode,structfile*filp)
{
structnewchr_dev*dev=filp->private_data;
/*關(guān)閉驅(qū)動文件的時候釋放原子變量*/
atomic_inc(&dev->lock);
......
return0;
}
| 自旋鎖
驅(qū)動源碼
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
/*添加頭文件*/
#include
#include
#include
#include
#defineCHRDEVBASE_CNT1/*設(shè)備號個數(shù)*/
#defineCHRDEVBASE_NAME"chrdevbase"/*名字*/
#defineLEDOFF0/*關(guān)燈*/
#defineLEDON1/*開燈*/
/*chrdevbase設(shè)備結(jié)構(gòu)體*/
structnewchr_dev{
dev_tdevid;/*設(shè)備號*/
structcdevcdev;/*cdev*/
structclass*class;/*類*/
structdevice*device;/*設(shè)備*/
intmajor;/*主設(shè)備號*/
intminor;/*次設(shè)備號*/
structdevice_node*nd;/*設(shè)備節(jié)點(diǎn)*/
intled_gpio;/*led所使用的GPIO編號*/
intdev_stats;/*設(shè)備狀態(tài),0,設(shè)備未使用;>0,設(shè)備已經(jīng)被使用*/
spinlock_tlock;/*自旋鎖*/
};
structnewchr_devchrdevbase;/*自定義字符設(shè)備*/
/*
*@description:LED硬件初始化
*@param:無
*@return:無
*/
staticintled_hal_init(void)
{
intret=0;
/*設(shè)置LED所使用的GPIO*/
/* 1、獲取設(shè)備節(jié)點(diǎn):gpioled */
chrdevbase.nd=of_find_node_by_path("/gpioled");
if(chrdevbase.nd==NULL){
printk("chrdevbasenodecantnotfound!
");
return-EINVAL;
}else{
printk("chrdevbasenodehasbeenfound!
");
}
/*2、獲取設(shè)備樹中的gpio屬性,得到LED所使用的LED編號*/
chrdevbase.led_gpio=of_get_named_gpio(chrdevbase.nd,"led-gpio",0);
if(chrdevbase.led_gpio0){
printk("can'tgetled-gpio");
return-EINVAL;
}
printk("led-gpionum=%d
",chrdevbase.led_gpio);
/*3、設(shè)置GPIO1_IO03為輸出,并且輸出高電平,默認(rèn)關(guān)閉LED燈*/
ret=gpio_direction_output(chrdevbase.led_gpio,1);
if(ret0){
printk("can'tsetgpio!
");
}
return0;
}
/*
*@description:打開設(shè)備
*@param-inode:傳遞給驅(qū)動的inode
*@param-filp:設(shè)備文件,file結(jié)構(gòu)體有個叫做private_data的成員變量
*一般在open的時候?qū)rivate_data指向設(shè)備結(jié)構(gòu)體。
*@return:0成功;其他失敗
*/
staticintchrdevbase_open(structinode*inode,structfile*filp)
{
unsignedlongflags;
filp->private_data=&chrdevbase;/*設(shè)置私有數(shù)據(jù)*/
spin_lock_irqsave(&chrdevbase.lock,flags);/*上鎖*/
if(chrdevbase.dev_stats){/*如果設(shè)備被使用了*/
spin_unlock_irqrestore(&chrdevbase.lock,flags);/*解鎖*/
return-EBUSY;
}
chrdevbase.dev_stats++;/*如果設(shè)備沒有打開,那么就標(biāo)記已經(jīng)打開了*/
spin_unlock_irqrestore(&chrdevbase.lock,flags);/*解鎖*/
printk("[BSP]chrdevbaseopen!
");
return0;
}
/*
*@description:從設(shè)備讀取數(shù)據(jù)
*@param-filp:要打開的設(shè)備文件(文件描述符)
*@param-buf:返回給用戶空間的數(shù)據(jù)緩沖區(qū)
*@param-cnt:要讀取的數(shù)據(jù)長度
*@param-offt:相對于文件首地址的偏移
*@return:讀取的字節(jié)數(shù),如果為負(fù)值,表示讀取失敗
*/
staticssize_tchrdevbase_read(structfile*filp,char__user*buf,size_tcnt,loff_t*offt)
{
printk("chrdevbaseread!
");
return0;
}
/*
*@description:向設(shè)備寫數(shù)據(jù)
*@param-filp:設(shè)備文件,表示打開的文件描述符
*@param-buf:要寫給設(shè)備寫入的數(shù)據(jù)
*@param-cnt:要寫入的數(shù)據(jù)長度
*@param-offt:相對于文件首地址的偏移
*@return:寫入的字節(jié)數(shù),如果為負(fù)值,表示寫入失敗
*/
staticssize_tchrdevbase_write(structfile*filp,constchar__user*buf,size_tcnt,loff_t*offt)
{
intretvalue=0;
charwritebuf[1];
structnewchr_dev*dev=filp->private_data;
/*接收用戶空間傳遞給內(nèi)核的數(shù)據(jù)并且打印出來*/
retvalue=copy_from_user(writebuf,buf,cnt);
printk("[BSP]kernelrecevdatadata:%d!
",writebuf[0]);
if(writebuf[0]==LEDON){
gpio_set_value(dev->led_gpio,0);/*打開LED燈*/
}elseif(writebuf[0]==LEDOFF){
gpio_set_value(dev->led_gpio,1);/*關(guān)閉LED燈*/
}
//printk("chrdevbasewrite!
");
return0;
}
/*
*@description:關(guān)閉/釋放設(shè)備
*@param-filp:要關(guān)閉的設(shè)備文件(文件描述符)
*@return:0成功;其他失敗
*/
staticintchrdevbase_release(structinode*inode,structfile*filp)
{
unsignedlongflags;
structnewchr_dev*dev=filp->private_data;
/*關(guān)閉驅(qū)動文件的時候?qū)ev_stats減1*/
spin_lock_irqsave(&dev->lock,flags);/*上鎖*/
if(dev->dev_stats){
dev->dev_stats--;
}
spin_unlock_irqrestore(&dev->lock,flags);/*解鎖*/
printk("[BSP]release!
");
return0;
}
/*
*設(shè)備操作函數(shù)結(jié)構(gòu)體
*/
staticstructfile_operationschrdevbase_fops={
.owner=THIS_MODULE,
.open=chrdevbase_open,
.read=chrdevbase_read,
.write=chrdevbase_write,
.release=chrdevbase_release,
};
/*
*@description:驅(qū)動入口函數(shù)
*@param:無
*@return:0成功;其他失敗
*/
staticint__initchrdevbase_init(void)
{
/*初始化自旋鎖*/
spin_lock_init(&chrdevbase.lock);
/*初始化硬件*/
led_hal_init();
/*注冊字符設(shè)備驅(qū)動*/
/*1、創(chuàng)建設(shè)備號*/
if(chrdevbase.major){/*定義了設(shè)備號*/
chrdevbase.devid=MKDEV(chrdevbase.major,0);
register_chrdev_region(chrdevbase.devid,CHRDEVBASE_CNT,CHRDEVBASE_NAME);
}else{/*沒有定義設(shè)備號*/
alloc_chrdev_region(&chrdevbase.devid,0,CHRDEVBASE_CNT,CHRDEVBASE_NAME);/*申請?jiān)O(shè)備號*/
chrdevbase.major=MAJOR(chrdevbase.devid);/*獲取主設(shè)備號*/
chrdevbase.minor=MINOR(chrdevbase.devid);/*獲取次設(shè)備號*/
}
printk("newcheledmajor=%d,minor=%d
",chrdevbase.major,chrdevbase.minor);
/*2、初始化cdev*/
chrdevbase.cdev.owner=THIS_MODULE;
cdev_init(&chrdevbase.cdev,&chrdevbase_fops);
/*3、添加一個cdev*/
cdev_add(&chrdevbase.cdev,chrdevbase.devid,CHRDEVBASE_CNT);
/*4、創(chuàng)建類*/
chrdevbase.class=class_create(THIS_MODULE,CHRDEVBASE_NAME);
if(IS_ERR(chrdevbase.class)){
returnPTR_ERR(chrdevbase.class);
}
/*5、創(chuàng)建設(shè)備*/
chrdevbase.device=device_create(chrdevbase.class,NULL,chrdevbase.devid,NULL,CHRDEVBASE_NAME);
if(IS_ERR(chrdevbase.device)){
returnPTR_ERR(chrdevbase.device);
}
return0;
}
/*
*@description:驅(qū)動出口函數(shù)
*@param:無
*@return:無
*/
staticvoid__exitchrdevbase_exit(void)
{
/*注銷字符設(shè)備*/
cdev_del(&chrdevbase.cdev);/*刪除cdev*/
unregister_chrdev_region(chrdevbase.devid,CHRDEVBASE_CNT);/*注銷設(shè)備號*/
device_destroy(chrdevbase.class,chrdevbase.devid);/*銷毀設(shè)備*/
class_destroy(chrdevbase.class);/*銷毀類*/
printk("[BSP]chrdevbaseexit!
");
}
/*
*將上面兩個函數(shù)指定為驅(qū)動的入口和出口函數(shù)
*/
module_init(chrdevbase_init);
module_exit(chrdevbase_exit);
/*
*LICENSE和作者信息
*/
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zuozhongkai");
應(yīng)用源碼
和原子操作應(yīng)用源碼一樣,不需要修改!
實(shí)驗(yàn)現(xiàn)象
套路分析
自旋鎖和RTOS中的臨界保護(hù)有點(diǎn)類似,套路分析如下:
1、在結(jié)構(gòu)體加入自旋鎖變量和一個狀態(tài)變量
/*chrdevbase設(shè)備結(jié)構(gòu)體*/
structnewchr_dev{
......
intdev_stats;/*設(shè)備狀態(tài),0,設(shè)備未使用;>0,設(shè)備已經(jīng)被使用*/
spinlock_tlock;/*自旋鎖*/
};
2、在驅(qū)動入口初始化自旋鎖
/*
*@description:驅(qū)動入口函數(shù)
*@param:無
*@return:0成功;其他失敗
*/
staticint__initchrdevbase_init(void)
{
/*初始化自旋鎖*/
spin_lock_init(&chrdevbase.lock);
......
return0;
}
3、在打開設(shè)備時判斷設(shè)備是否被使用
/*
*@description:打開設(shè)備
*@param-inode:傳遞給驅(qū)動的inode
*@param-filp:設(shè)備文件,file結(jié)構(gòu)體有個叫做private_data的成員變量
*一般在open的時候?qū)rivate_data指向設(shè)備結(jié)構(gòu)體。
*@return:0成功;其他失敗
*/
staticintchrdevbase_open(structinode*inode,structfile*filp)
{
unsignedlongflags;
filp->private_data=&chrdevbase;/*設(shè)置私有數(shù)據(jù)*/
spin_lock_irqsave(&chrdevbase.lock,flags);/*上鎖*/
if(chrdevbase.dev_stats){/*如果設(shè)備被使用了*/
spin_unlock_irqrestore(&chrdevbase.lock,flags);/*解鎖*/
return-EBUSY;
}
chrdevbase.dev_stats++;/*如果設(shè)備沒有打開,那么就標(biāo)記已經(jīng)打開了*/
spin_unlock_irqrestore(&chrdevbase.lock,flags);/*解鎖*/
......
return0;
}
4、在釋放設(shè)備時對狀態(tài)變量自減
/*
*@description:關(guān)閉/釋放設(shè)備
*@param-filp:要關(guān)閉的設(shè)備文件(文件描述符)
*@return:0成功;其他失敗
*/
staticintchrdevbase_release(structinode*inode,structfile*filp)
{
unsignedlongflags;
structnewchr_dev*dev=filp->private_data;
/*關(guān)閉驅(qū)動文件的時候?qū)ev_stats減1*/
spin_lock_irqsave(&dev->lock,flags);/*上鎖*/
if(dev->dev_stats){
dev->dev_stats--;
}
spin_unlock_irqrestore(&dev->lock,flags);/*解鎖*/
printk("[BSP]release!
");
return0;
}
| 信號量
驅(qū)動源碼
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
/*添加頭文件*/
#include
#include
#include
#include
#defineCHRDEVBASE_CNT1/*設(shè)備號個數(shù)*/
#defineCHRDEVBASE_NAME"chrdevbase"/*名字*/
#defineLEDOFF0/*關(guān)燈*/
#defineLEDON1/*開燈*/
/*chrdevbase設(shè)備結(jié)構(gòu)體*/
structnewchr_dev{
dev_tdevid;/*設(shè)備號*/
structcdevcdev;/*cdev*/
structclass*class;/*類*/
structdevice*device;/*設(shè)備*/
intmajor;/*主設(shè)備號*/
intminor;/*次設(shè)備號*/
structdevice_node*nd;/*設(shè)備節(jié)點(diǎn)*/
intled_gpio;/*led所使用的GPIO編號*/
structsemaphoresem;/*信號量*/
};
structnewchr_devchrdevbase;/*自定義字符設(shè)備*/
/*
*@description:LED硬件初始化
*@param:無
*@return:無
*/
staticintled_hal_init(void)
{
intret=0;
/*設(shè)置LED所使用的GPIO*/
/* 1、獲取設(shè)備節(jié)點(diǎn):gpioled */
chrdevbase.nd=of_find_node_by_path("/gpioled");
if(chrdevbase.nd==NULL){
printk("chrdevbasenodecantnotfound!
");
return-EINVAL;
}else{
printk("chrdevbasenodehasbeenfound!
");
}
/*2、獲取設(shè)備樹中的gpio屬性,得到LED所使用的LED編號*/
chrdevbase.led_gpio=of_get_named_gpio(chrdevbase.nd,"led-gpio",0);
if(chrdevbase.led_gpio0){
printk("can'tgetled-gpio");
return-EINVAL;
}
printk("led-gpionum=%d
",chrdevbase.led_gpio);
/*3、設(shè)置GPIO1_IO03為輸出,并且輸出高電平,默認(rèn)關(guān)閉LED燈*/
ret=gpio_direction_output(chrdevbase.led_gpio,1);
if(ret0){
printk("can'tsetgpio!
");
}
return0;
}
/*
*@description:打開設(shè)備
*@param-inode:傳遞給驅(qū)動的inode
*@param-filp:設(shè)備文件,file結(jié)構(gòu)體有個叫做private_data的成員變量
*一般在open的時候?qū)rivate_data指向設(shè)備結(jié)構(gòu)體。
*@return:0成功;其他失敗
*/
staticintchrdevbase_open(structinode*inode,structfile*filp)
{
printk("[BSP]chrdevbaseopen!
");
filp->private_data=&chrdevbase;/*設(shè)置私有數(shù)據(jù)*/
/*獲取信號量,進(jìn)入休眠狀態(tài)的進(jìn)程可以被信號打斷*/
if(down_interruptible(&chrdevbase.sem)){
return-ERESTARTSYS;
}
#if0
down(&chrdevbase.sem);/*不能被信號打斷*/
#endif
return0;
}
/*
*@description:從設(shè)備讀取數(shù)據(jù)
*@param-filp:要打開的設(shè)備文件(文件描述符)
*@param-buf:返回給用戶空間的數(shù)據(jù)緩沖區(qū)
*@param-cnt:要讀取的數(shù)據(jù)長度
*@param-offt:相對于文件首地址的偏移
*@return:讀取的字節(jié)數(shù),如果為負(fù)值,表示讀取失敗
*/
staticssize_tchrdevbase_read(structfile*filp,char__user*buf,size_tcnt,loff_t*offt)
{
printk("chrdevbaseread!
");
return0;
}
/*
*@description:向設(shè)備寫數(shù)據(jù)
*@param-filp:設(shè)備文件,表示打開的文件描述符
*@param-buf:要寫給設(shè)備寫入的數(shù)據(jù)
*@param-cnt:要寫入的數(shù)據(jù)長度
*@param-offt:相對于文件首地址的偏移
*@return:寫入的字節(jié)數(shù),如果為負(fù)值,表示寫入失敗
*/
staticssize_tchrdevbase_write(structfile*filp,constchar__user*buf,size_tcnt,loff_t*offt)
{
intretvalue=0;
charwritebuf[1];
structnewchr_dev*dev=filp->private_data;
/*接收用戶空間傳遞給內(nèi)核的數(shù)據(jù)并且打印出來*/
retvalue=copy_from_user(writebuf,buf,cnt);
printk("[BSP]kernelrecevdatadata:%d!
",writebuf[0]);
if(writebuf[0]==LEDON){
gpio_set_value(dev->led_gpio,0);/*打開LED燈*/
}elseif(writebuf[0]==LEDOFF){
gpio_set_value(dev->led_gpio,1);/*關(guān)閉LED燈*/
}
//printk("chrdevbasewrite!
");
return0;
}
/*
*@description:關(guān)閉/釋放設(shè)備
*@param-filp:要關(guān)閉的設(shè)備文件(文件描述符)
*@return:0成功;其他失敗
*/
staticintchrdevbase_release(structinode*inode,structfile*filp)
{
structnewchr_dev*dev=filp->private_data;
up(&dev->sem);/*釋放信號量,信號量值加1*/
printk("[BSP]release!
");
return0;
}
/*
*設(shè)備操作函數(shù)結(jié)構(gòu)體
*/
staticstructfile_operationschrdevbase_fops={
.owner=THIS_MODULE,
.open=chrdevbase_open,
.read=chrdevbase_read,
.write=chrdevbase_write,
.release=chrdevbase_release,
};
/*
*@description:驅(qū)動入口函數(shù)
*@param:無
*@return:0成功;其他失敗
*/
staticint__initchrdevbase_init(void)
{
/*初始化信號量*/
sema_init(&chrdevbase.sem,1);
/*初始化硬件*/
led_hal_init();
/*注冊字符設(shè)備驅(qū)動*/
/*1、創(chuàng)建設(shè)備號*/
if(chrdevbase.major){/*定義了設(shè)備號*/
chrdevbase.devid=MKDEV(chrdevbase.major,0);
register_chrdev_region(chrdevbase.devid,CHRDEVBASE_CNT,CHRDEVBASE_NAME);
}else{/*沒有定義設(shè)備號*/
alloc_chrdev_region(&chrdevbase.devid,0,CHRDEVBASE_CNT,CHRDEVBASE_NAME);/*申請?jiān)O(shè)備號*/
chrdevbase.major=MAJOR(chrdevbase.devid);/*獲取主設(shè)備號*/
chrdevbase.minor=MINOR(chrdevbase.devid);/*獲取次設(shè)備號*/
}
printk("newcheledmajor=%d,minor=%d
",chrdevbase.major,chrdevbase.minor);
/*2、初始化cdev*/
chrdevbase.cdev.owner=THIS_MODULE;
cdev_init(&chrdevbase.cdev,&chrdevbase_fops);
/*3、添加一個cdev*/
cdev_add(&chrdevbase.cdev,chrdevbase.devid,CHRDEVBASE_CNT);
/*4、創(chuàng)建類*/
chrdevbase.class=class_create(THIS_MODULE,CHRDEVBASE_NAME);
if(IS_ERR(chrdevbase.class)){
returnPTR_ERR(chrdevbase.class);
}
/*5、創(chuàng)建設(shè)備*/
chrdevbase.device=device_create(chrdevbase.class,NULL,chrdevbase.devid,NULL,CHRDEVBASE_NAME);
if(IS_ERR(chrdevbase.device)){
returnPTR_ERR(chrdevbase.device);
}
return0;
}
/*
*@description:驅(qū)動出口函數(shù)
*@param:無
*@return:無
*/
staticvoid__exitchrdevbase_exit(void)
{
/*注銷字符設(shè)備*/
cdev_del(&chrdevbase.cdev);/*刪除cdev*/
unregister_chrdev_region(chrdevbase.devid,CHRDEVBASE_CNT);/*注銷設(shè)備號*/
device_destroy(chrdevbase.class,chrdevbase.devid);/*銷毀設(shè)備*/
class_destroy(chrdevbase.class);/*銷毀類*/
printk("[BSP]chrdevbaseexit!
");
}
/*
*將上面兩個函數(shù)指定為驅(qū)動的入口和出口函數(shù)
*/
module_init(chrdevbase_init);
module_exit(chrdevbase_exit);
/*
*LICENSE和作者信息
*/
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zuozhongkai");
應(yīng)用源碼
和原子操作應(yīng)用源碼一樣,不需要修改!
實(shí)驗(yàn)現(xiàn)象
使用信號量不會出現(xiàn)設(shè)備打不開的問題,它會在任務(wù)結(jié)束后再次執(zhí)行!
套路分析
1、在結(jié)構(gòu)體加入信號量
/*chrdevbase設(shè)備結(jié)構(gòu)體*/
structnewchr_dev{
......
structsemaphoresem;/*信號量*/
};
2、在驅(qū)動入口初始信號量
/*
*@description:驅(qū)動入口函數(shù)
*@param:無
*@return:0成功;其他失敗
*/
staticint__initchrdevbase_init(void)
{
/*初始化信號量*/
sema_init(&chrdevbase.sem,1);
......
return0;
3、在打開設(shè)備時獲取信號量
/*
*@description:打開設(shè)備
*@param-inode:傳遞給驅(qū)動的inode
*@param-filp:設(shè)備文件,file結(jié)構(gòu)體有個叫做private_data的成員變量
*一般在open的時候?qū)rivate_data指向設(shè)備結(jié)構(gòu)體。
*@return:0成功;其他失敗
*/
staticintchrdevbase_open(structinode*inode,structfile*filp)
{
printk("[BSP]chrdevbaseopen!
");
filp->private_data=&chrdevbase;/*設(shè)置私有數(shù)據(jù)*/
/*獲取信號量,進(jìn)入休眠狀態(tài)的進(jìn)程可以被信號打斷*/
if(down_interruptible(&chrdevbase.sem)){
return-ERESTARTSYS;
}
#if0
down(&chrdevbase.sem);/*不能被信號打斷*/
#endif
return0;
}
4、在釋放設(shè)備時釋放信號量
/*
*@description:關(guān)閉/釋放設(shè)備
*@param-filp:要關(guān)閉的設(shè)備文件(文件描述符)
*@return:0成功;其他失敗
*/
staticintchrdevbase_release(structinode*inode,structfile*filp)
{
structnewchr_dev*dev=filp->private_data;
up(&dev->sem);/*釋放信號量,信號量值加1*/
printk("[BSP]release!
");
return0;
}
| 互斥體
驅(qū)動源碼
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
/*添加頭文件*/
#include
#include
#include
#include
#defineCHRDEVBASE_CNT1/*設(shè)備號個數(shù)*/
#defineCHRDEVBASE_NAME"chrdevbase"/*名字*/
#defineLEDOFF0/*關(guān)燈*/
#defineLEDON1/*開燈*/
/*chrdevbase設(shè)備結(jié)構(gòu)體*/
structnewchr_dev{
dev_tdevid;/*設(shè)備號*/
structcdevcdev;/*cdev*/
structclass*class;/*類*/
structdevice*device;/*設(shè)備*/
intmajor;/*主設(shè)備號*/
intminor;/*次設(shè)備號*/
structdevice_node*nd;/*設(shè)備節(jié)點(diǎn)*/
intled_gpio;/*led所使用的GPIO編號*/
structmutexlock;/*互斥體*/
};
structnewchr_devchrdevbase;/*自定義字符設(shè)備*/
/*
*@description:LED硬件初始化
*@param:無
*@return:無
*/
staticintled_hal_init(void)
{
intret=0;
/*設(shè)置LED所使用的GPIO*/
/*1、獲取設(shè)備節(jié)點(diǎn):gpioled*/
chrdevbase.nd=of_find_node_by_path("/gpioled");
if(chrdevbase.nd==NULL){
printk("chrdevbasenodecantnotfound!
");
return-EINVAL;
}else{
printk("chrdevbasenodehasbeenfound!
");
}
/*2、獲取設(shè)備樹中的gpio屬性,得到LED所使用的LED編號*/
chrdevbase.led_gpio=of_get_named_gpio(chrdevbase.nd,"led-gpio",0);
if(chrdevbase.led_gpio0){
printk("can'tgetled-gpio");
return-EINVAL;
}
printk("led-gpionum=%d
",chrdevbase.led_gpio);
/*3、設(shè)置GPIO1_IO03為輸出,并且輸出高電平,默認(rèn)關(guān)閉LED燈*/
ret=gpio_direction_output(chrdevbase.led_gpio,1);
if(ret0){
printk("can'tsetgpio!
");
}
return0;
}
/*
*@description:打開設(shè)備
*@param-inode:傳遞給驅(qū)動的inode
*@param-filp:設(shè)備文件,file結(jié)構(gòu)體有個叫做private_data的成員變量
*一般在open的時候?qū)rivate_data指向設(shè)備結(jié)構(gòu)體。
*@return:0成功;其他失敗
*/
staticintchrdevbase_open(structinode*inode,structfile*filp)
{
printk("[BSP]chrdevbaseopen!
");
filp->private_data=&chrdevbase;/*設(shè)置私有數(shù)據(jù)*/
/*獲取互斥體,可以被信號打斷*/
if(mutex_lock_interruptible(&chrdevbase.lock)){
return-ERESTARTSYS;
}
#if0
mutex_lock(&chrdevbase.lock);/*不能被信號打斷*/
#endif
return0;
}
/*
*@description:從設(shè)備讀取數(shù)據(jù)
*@param-filp:要打開的設(shè)備文件(文件描述符)
*@param-buf:返回給用戶空間的數(shù)據(jù)緩沖區(qū)
*@param-cnt:要讀取的數(shù)據(jù)長度
*@param-offt:相對于文件首地址的偏移
*@return:讀取的字節(jié)數(shù),如果為負(fù)值,表示讀取失敗
*/
staticssize_tchrdevbase_read(structfile*filp,char__user*buf,size_tcnt,loff_t*offt)
{
printk("chrdevbaseread!
");
return0;
}
/*
*@description:向設(shè)備寫數(shù)據(jù)
*@param-filp:設(shè)備文件,表示打開的文件描述符
*@param-buf:要寫給設(shè)備寫入的數(shù)據(jù)
*@param-cnt:要寫入的數(shù)據(jù)長度
*@param-offt:相對于文件首地址的偏移
*@return:寫入的字節(jié)數(shù),如果為負(fù)值,表示寫入失敗
*/
staticssize_tchrdevbase_write(structfile*filp,constchar__user*buf,size_tcnt,loff_t*offt)
{
intretvalue=0;
charwritebuf[1];
structnewchr_dev*dev=filp->private_data;
/*接收用戶空間傳遞給內(nèi)核的數(shù)據(jù)并且打印出來*/
retvalue=copy_from_user(writebuf,buf,cnt);
printk("[BSP]kernelrecevdatadata:%d!
",writebuf[0]);
if(writebuf[0]==LEDON){
gpio_set_value(dev->led_gpio,0);/*打開LED燈*/
}elseif(writebuf[0]==LEDOFF){
gpio_set_value(dev->led_gpio,1);/*關(guān)閉LED燈*/
}
//printk("chrdevbasewrite!
");
return0;
}
/*
*@description:關(guān)閉/釋放設(shè)備
*@param-filp:要關(guān)閉的設(shè)備文件(文件描述符)
*@return:0成功;其他失敗
*/
staticintchrdevbase_release(structinode*inode,structfile*filp)
{
structnewchr_dev*dev=filp->private_data;
/*釋放互斥鎖*/
mutex_unlock(&dev->lock);
printk("[BSP]release!
");
return0;
}
/*
*設(shè)備操作函數(shù)結(jié)構(gòu)體
*/
staticstructfile_operationschrdevbase_fops={
.owner=THIS_MODULE,
.open=chrdevbase_open,
.read=chrdevbase_read,
.write=chrdevbase_write,
.release=chrdevbase_release,
};
/*
*@description:驅(qū)動入口函數(shù)
*@param:無
*@return:0成功;其他失敗
*/
staticint__initchrdevbase_init(void)
{
/*初始化互斥體*/
mutex_init(&chrdevbase.lock);
/*初始化硬件*/
led_hal_init();
/*注冊字符設(shè)備驅(qū)動*/
/*1、創(chuàng)建設(shè)備號*/
if(chrdevbase.major){/*定義了設(shè)備號*/
chrdevbase.devid=MKDEV(chrdevbase.major,0);
register_chrdev_region(chrdevbase.devid,CHRDEVBASE_CNT,CHRDEVBASE_NAME);
}else{/*沒有定義設(shè)備號*/
alloc_chrdev_region(&chrdevbase.devid,0,CHRDEVBASE_CNT,CHRDEVBASE_NAME);/*申請?jiān)O(shè)備號*/
chrdevbase.major=MAJOR(chrdevbase.devid);/*獲取主設(shè)備號*/
chrdevbase.minor=MINOR(chrdevbase.devid);/*獲取次設(shè)備號*/
}
printk("newcheledmajor=%d,minor=%d
",chrdevbase.major,chrdevbase.minor);
/*2、初始化cdev*/
chrdevbase.cdev.owner=THIS_MODULE;
cdev_init(&chrdevbase.cdev,&chrdevbase_fops);
/*3、添加一個cdev*/
cdev_add(&chrdevbase.cdev,chrdevbase.devid,CHRDEVBASE_CNT);
/*4、創(chuàng)建類*/
chrdevbase.class=class_create(THIS_MODULE,CHRDEVBASE_NAME);
if(IS_ERR(chrdevbase.class)){
returnPTR_ERR(chrdevbase.class);
}
/*5、創(chuàng)建設(shè)備*/
chrdevbase.device=device_create(chrdevbase.class,NULL,chrdevbase.devid,NULL,CHRDEVBASE_NAME);
if(IS_ERR(chrdevbase.device)){
returnPTR_ERR(chrdevbase.device);
}
return0;
}
/*
*@description:驅(qū)動出口函數(shù)
*@param:無
*@return:無
*/
staticvoid__exitchrdevbase_exit(void)
{
/*注銷字符設(shè)備*/
cdev_del(&chrdevbase.cdev);/*刪除cdev*/
unregister_chrdev_region(chrdevbase.devid,CHRDEVBASE_CNT);/*注銷設(shè)備號*/
device_destroy(chrdevbase.class,chrdevbase.devid);/*銷毀設(shè)備*/
class_destroy(chrdevbase.class);/*銷毀類*/
printk("[BSP]chrdevbaseexit!
");
}
/*
*將上面兩個函數(shù)指定為驅(qū)動的入口和出口函數(shù)
*/
module_init(chrdevbase_init);
module_exit(chrdevbase_exit);
/*
*LICENSE和作者信息
*/
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zuozhongkai");
應(yīng)用源碼
和原子操作應(yīng)用源碼一樣,不需要修改!
實(shí)驗(yàn)現(xiàn)象
互斥體和信號量的效果類似,也會在任務(wù)完成后再運(yùn)行。
套路分析
1、在結(jié)構(gòu)體加入互斥體
/*chrdevbase設(shè)備結(jié)構(gòu)體*/
structnewchr_dev{
....
structmutexlock;/*互斥體*/
};
2、在驅(qū)動入口初始化互斥體
/*
*@description:驅(qū)動入口函數(shù)
*@param:無
*@return:0成功;其他失敗
*/
staticint__initchrdevbase_init(void)
{
/*初始化互斥體*/
mutex_init(&chrdevbase.lock);
......
return0;
}
3、在打開設(shè)備時獲取互斥體
/*
*@description:打開設(shè)備
*@param-inode:傳遞給驅(qū)動的inode
*@param-filp:設(shè)備文件,file結(jié)構(gòu)體有個叫做private_data的成員變量
*一般在open的時候?qū)rivate_data指向設(shè)備結(jié)構(gòu)體。
*@return:0成功;其他失敗
*/
staticintchrdevbase_open(structinode*inode,structfile*filp)
{
printk("[BSP]chrdevbaseopen!
");
filp->private_data=&chrdevbase;/*設(shè)置私有數(shù)據(jù)*/
/*獲取互斥體,可以被信號打斷*/
if(mutex_lock_interruptible(&chrdevbase.lock)){
return-ERESTARTSYS;
}
#if0
mutex_lock(&chrdevbase.lock);/*不能被信號打斷*/
#endif
return0;
}
4、在釋放設(shè)備時釋放互斥體
/*
*@description:關(guān)閉/釋放設(shè)備
*@param-filp:要關(guān)閉的設(shè)備文件(文件描述符)
*@return:0成功;其他失敗
*/
staticintchrdevbase_release(structinode*inode,structfile*filp)
{
structnewchr_dev*dev=filp->private_data;
/*釋放互斥鎖*/
mutex_unlock(&dev->lock);
printk("[BSP]release!
");
return0;
}
-
驅(qū)動
+關(guān)注
關(guān)注
12文章
1851瀏覽量
85647 -
控制臺
+關(guān)注
關(guān)注
0文章
85瀏覽量
10411 -
源碼
+關(guān)注
關(guān)注
8文章
652瀏覽量
29458
原文標(biāo)題:i.MX6ULL|并發(fā)與競爭實(shí)驗(yàn)
文章出處:【微信號:玩轉(zhuǎn)單片機(jī),微信公眾號:玩轉(zhuǎn)單片機(jī)】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
評論