S3C2410 LED驅(qū)動(dòng)程序
編寫驅(qū)動(dòng)程序代碼:
#include
#include
#include
#include
#include
#include
#include
#includeccess.h>
#undef DEBUG
#define DEBUG
#ifdef DEBUG
#define DPRINTK(x...) printk("s3c2410-led:" x)
#else
#define DPRINTK(x...)
#endif
#define DEVICE_NAME "s3c2410-led"
#define DbLedRAW_MINOR
static int DbLedMajor=0;
static char ledstatus=2;
static void Updateled(void)
{
if(ledstatus&0x01)
write_gpio_bit(GPIO_B0,1); //LED亮
else
write_gpio_bit(GPIO_B0,0); //LED滅
}
static ssize_t s3c2410_DbLed_write(struct file *file,const char *buffer,size_t count,loff_t *ppos)
{
copy_from_user(&ledstatus,buffer,sizeof(ledstatus));
Updateled();
DPRINTK("write: led=0x%x,count=%dn",ledstatus,count);
return sizeof(ledstatus);
}
static int s3c2410_DbLed_open(struct inode *inode,struct file *filp)
{
MOD_INC_USE_COUNT;
DPRINTK("openn");
return 0;
}
static int s3c2410_DbLed_release(struct inode *inode,struct file *filp)
{
MOD_DEC_USE_COUNT;
DPRINTK("releasen");
return 0;
}
static struct file_operations s3c2410_fops={ //注冊(cè)字符設(shè)備時(shí)調(diào)用
owner: THIS_MODULE,
open: s3c2410_DbLed_open,
write: s3c2410_DbLed_write,
release: s3c2410_DbLed_release,
};
#ifdef CONFIG_DEVFS_FS
static devfs_handle_t devfs_DbLed_dir,devfs_DbLedraw;
#endif
static int __init s3c2410_DbLed_init(void) //begin
{
int ret;
set_gpio_ctrl(GPIO_MODE_OUT|GPIO_PULLUP_DIS|GPIO_B0); //GPIO_B0設(shè)為輸出無(wú)上拉狀態(tài)
Updateled();
ret=register_chrdev(0,DEVICE_NAME,&s3c2410_fops); //注冊(cè)字符設(shè)備驅(qū)動(dòng)
if(ret<0) { //小于0,也就是失敗
printk(DEVICE_NAME "can't get major numbern");
return ret;
}
DbLedMajor=ret;
#ifdef CONFIG_DEVFS_FS //創(chuàng)建文件系統(tǒng)節(jié)點(diǎn)
devfs_DbLed_dir=devfs_mk_dir(NULL,"led",NULL);
devfs_DbLedraw=devfs_register(devfs_DbLed_dir,"0",DEVFS_FL_DEFAULT,DbLedMajor,0,S_IFCHR|S_IRUSR|S_IWUSR,&s3c2410_fops,NULL);
#endif
printk(DEVICE_NAME " initializedn");
return 0;
}
static void __exit s3c2410_DbLed_exit(void)
{
#ifdef CONFIG_DEVFS_FS
devfs_unregister(devfs_DbLedraw);
devfs_unregister(devfs_DbLed_dir);
#endif
unregister_chrdev(DbLedMajor,DEVICE_NAME);
}
module_init(s3c2410_DbLed_init);
module_exit(s3c2410_DbLed_exit);
因?yàn)長(zhǎng)ED位字符設(shè)備,習(xí)慣上將簡(jiǎn)單的字符設(shè)備的驅(qū)動(dòng)程序放在內(nèi)核跟目錄drivers/char目錄下
2.
在LED驅(qū)動(dòng)程序中,把驅(qū)動(dòng)程序添加到內(nèi)核,實(shí)現(xiàn)裁剪和編譯,需要修改drivers/char/Config.in和drivers/char/Makefile
在drivers/char/Config.in文件中添加:
Dep_tristate ‘S3C2410 LED support’ CONFIG_S3C2410_GPIO_LED
$CONFIG_ARCH_S3C2410
其含義是:只要定義了CONFIG_ARCH_S3C2410位y或m,在內(nèi)核配置時(shí)(make menuconfig),Character devices 分類下,就會(huì)出現(xiàn)S3C2410 LED support選項(xiàng),它對(duì)應(yīng)了CONFIG_S3C2410_GPIO_LED的定義。
在drivers/char/Makefile文件中添加:
obj-$ (CONFIG_S3C2410_GPIO_LED) +=LED.o
Makefile會(huì)根據(jù)obj-m和obj-y編譯并連接對(duì)應(yīng)的源碼。這里在配置內(nèi)核時(shí)選擇編譯為內(nèi)核可加載的模塊。
3.執(zhí)行編譯命令:
Make modules
可以編譯內(nèi)核中所有配置為模塊的驅(qū)動(dòng)程序。
或者使用命令:
Make modules SUBDIRS=drivers/char
只編譯內(nèi)核源碼中drivers/char目錄下的模塊。
4.將編譯好的LED.o文件復(fù)制到網(wǎng)絡(luò)文件系統(tǒng)文件夾nfsfile(或者復(fù)制到開發(fā)板),啟動(dòng)開發(fā)板,掛載nfsfile文件夾,然后進(jìn)入nfsfile文件夾,執(zhí)行insmod命令把驅(qū)動(dòng)程序加載到內(nèi)核中:
Insmod LED.o
(使用rnmod LED卸載已經(jīng)加載到內(nèi)核中的模塊)
5.測(cè)試LED驅(qū)動(dòng)程序
用echo命令的重定向(>)方法,實(shí)現(xiàn)對(duì)這個(gè)設(shè)備的open,close,write等操作。
使用echo –n 1 >/dev/led/0 (-n阻止echo命令自動(dòng)換行)
向驅(qū)動(dòng)程序?qū)懭?x31( 1的ASCII碼),LED被點(diǎn)亮
執(zhí)行echo –n 0 > /dev/led/0,led熄滅
(也可以直接編譯進(jìn)內(nèi)核)