新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > OK6410A學(xué)習(xí)筆記三:嵌入式Linux驅(qū)動之LED驅(qū)動

OK6410A學(xué)習(xí)筆記三:嵌入式Linux驅(qū)動之LED驅(qū)動

作者: 時間:2016-11-21 來源:網(wǎng)絡(luò) 收藏
開發(fā)環(huán)境:
Windows7 + vmware workstation 6.5 + Ubuntu9.10
Linux Source Code: FORLINX_linux-3.0.1.tar.gz
ARM CROSS GCC: arm-linux-gcc v4.3.2

源碼:
//s3c6410_led.c – driver file
#include
#include
#include
#include
#include

#define DEV_MAJOR 176
#define DEV_NAME "s3c6410_leds"


#define GPMCON 0x7F008820
#define GPMDAT 0x7F008824
#define GPMPUD 0x7F008828


volatile unsigned long *gpmcon = NULL;
volatile unsigned long *gpmdat = NULL;
volatile unsigned long *gpmpud = NULL;


static int s3c6410_led_open(struct inode *inode, struct file *filp)
{
//int ret;

printk(KERN_ALERT "This is open function of s3c6410 led driver.n");

*gpmpud &= 0xffffffaa; //set GPM0~3 as pull up enabled
*gpmcon &= 0xffff1111; //set GPM0~3 as output
*gpmdat &= 0xfffffff0; //set GPM0~3 to low level, which turn on leds

return 0;

}

static ssize_t s3c6410_led_write(struct file *filp,const char __user *buf,size_t count,loff_t *ppos)
{
int val;

//copy_from_user(&val,buf,count);

printk(KERN_ALERT "This is write function of s3c6410 led driver.n");

return count;
}


struct file_operations s3c6410_led_flops = {
.owner = THIS_MODULE,
.open = s3c6410_led_open,
.write = s3c6410_led_write,
};

static int __init s3c6410_led_init(void)
{
int ret;

//register led device driver into kernel
ret = register_chrdev(DEV_MAJOR,DEV_NAME,&s3c6410_led_flops);

//retriver the vritual address by ioremap
gpmcon = (volatile unsigned long *)ioremap(GPMCON,4); //32-bit reg
gpmdat = gpmcon + 1;
gpmpud = gpmcon + 2;

return 0;
}

static void __exit s3c6410_led_exit(void)
{
//unregister led device dirver from kernel
unregister_chrdev(DEV_MAJOR,DEV_NAME);

//iounmap
iounmap(gpmcon);
}

module_init(s3c6410_led_init);
module_exit(s3c6410_led_exit);

MODULE_DESCRIPTION("This is led driver sample for OK6410A board.");
MODULE_VERSION("1.0");
MODULE_AUTHOR("");
MODULE_LICENSE("Dual BSD/GPL");

//s3c6410_led_test.c – test file
#include
#include

int main(int argc,char* argv[])
{
int fd;

fd = open("/dev/s3c6410_led",0);

if(!fd){
printf("open s3c6410 led failed.n");
}else{
printf("open s3c6410 led succeed.n");
}
return 0;
}

解析:
1. 查看OK6410A開發(fā)板原理圖,4個LED燈接在GPM0~3四個GPIO上。
2. 查看Samsung S3C6410X User’s Manual.pdf ,查看GPm端口操作說明,包括GPMCON,GPMDAT,GPMPUD三個寄存器的基地址以及配置信息,從而確定點亮LED燈和熄滅LED燈的操作。
3. 編寫源代碼,包括驅(qū)動源碼和測試源碼。這里比較重要的地方是,Linux驅(qū)動是工作在保護模式下,無法直接操作寄存器,因此必須將步驟二中找到的寄存器基地址通過IO地址重映射,從而得到虛擬地址進行操作,在驅(qū)動程序入口函數(shù)中使用ioremap()函數(shù)進行地址重映射,在退出函數(shù)中使用iounmap()結(jié)束地址重映射。另外,如果想在加載驅(qū)動的同時,讓系統(tǒng)自動新建設(shè)備節(jié)點,則要將設(shè)備信息提供給系統(tǒng)內(nèi)核,Linux系統(tǒng)支持的mdev機制會根據(jù)驅(qū)動程序提供的信息新建設(shè)備節(jié)點。在上述源碼中并沒有實現(xiàn)這一功能。
4. 編譯源碼
驅(qū)動程序Makefile如下:
obj-m:=s3c6410_led.o
KERNELDIR?=/usr/src/linux-3.0.1

default:
$(MAKE) -C $(KERNELDIR) M=$(shell pwd) modules

clean:
rm -rf *.o *~ *.ko *.mod.c *.order *.symvers

在終端下執(zhí)行如下命令:
#make //編譯驅(qū)動程序,得到s3c6410_led.ko文件
#arm-linux-gcc s3c6410_led_test.c –o s3c6410_led_test //編譯測試源碼

5. 拷貝s3c6410_led.ko和s3c6410_led_test到SD卡中,給開發(fā)板上電




評論


技術(shù)專區(qū)

關(guān)閉