linux驅動之LED驅動_2,linuxled_2

來源:互聯網
上載者:User

linux驅動之LED驅動_2,linuxled_2

在上一個文章中,講述了如何給led編寫驅動程式,但是實現的功能比較單一,接下來我們要實現的功能是在上一個的基礎上能夠單獨控制每一個led燈。

上一個文章的連結: linux驅動之LED驅動_1 

上一個文章的驅動源碼在:點擊開啟連結       測試源碼在:點擊開啟連結

目的:

實現板子上每個led能夠單獨的控制。

方法:0、通過次裝置號來單獨訪問每個led1、更改入口函數: 為每個led分配一個次裝置號。

for(minor=0;minor<4;minor++){firstdrv_class_dev[minor] = device_create(firstdrv_class,NULL,MKDEV(major,minor),NULL,"wq_led%d",minor);if(unlikely(IS_ERR(firstdrv_class_dev[minor])))return PTR_ERR(firstdrv_class_dev[minor]);}

2、同理,更改出口地址:

for(minor=0;minor<4;minor++){device_unregister(firstdrv_class_dev[minor]);}

3、這樣節點就掛上去了  會在/dev 裡面 出現四個裝置 分別是: wq_led0,wq_led1,wq_led2,wq_led34. 更改open 函數,增加單獨設定每個led引腳的功能: 根據次裝置號進行操作

int minor = MINOR(inode ->i_rdev);//獲得次裝置號switch(minor){case 0://GPB5 配置為輸出{*gpbcon &=~(0x3<<(5*2));*gpbcon |=(0x1<<(5*2));break;}case 1://GPB6 配置為輸出{*gpbcon &=~(0x3<<(6*2));*gpbcon |=(0x1<<(6*2));break;}case 2://GPB7 配置為輸出{*gpbcon &=~(0x3<<(7*2));*gpbcon |=(0x1<<(7*2));break;}case 3://GPB8 配置為輸出{*gpbcon &=~(0x3<<(8*2));*gpbcon |=(0x1<<(8*2));break;}}

5、更改 write 函數,根據使用者空間傳過來過來的資料和次裝置號對led進行操作

int minor = MINOR(file->f_dentry->d_inode->i_rdev);//獲得次裝置號copy_from_user(&val,buf,count);//從使用者空間向核心空間拷貝資料switch(minor){case 0://GPB5{if(val == 1)//亮{*gpbdat &=~(1<<5);}else//滅{*gpbdat|=(1<<5);}break;}case 1://GPB6{if(val == 1)//亮{*gpbdat &=~(1<<6);}else//滅{*gpbdat|=(1<<6);}break;}case 2://GPB7{if(val == 1)//亮{*gpbdat &=~(1<<7);}else//滅{*gpbdat|=(1<<7);}break;}case 3://GPB8{if(val == 1)//亮{*gpbdat &=~(1<<8);}else//滅{*gpbdat|=(1<<8);}break;}}
詳細代碼:

驅動程式:點擊開啟連結

測試程式:點擊開啟連結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.