歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Linux上第一個驅動程序:hello

寫過一篇文章是在ARM11上完成的LED的驅動和應用程序,現在換了平台,在am335x上。相比之前是在內核裡面修改Kconfig和Makefile文件,然後編譯模塊或者.ko文件,這次直接編寫Makefile,然後指定內核目錄和編譯器即可編譯獲得.ko文件,以後的驅動開發都可以參考此Makefile部分,故編寫此文檔,以備他日參考之用。

參考代碼:

#include  linux/kernel.h

#include  linux/module.h

static int __init ARM_Hello_Init(void)

{

    printk("Hello, Hello module is installed !\n");

    return 0;

}

static void __exit ARM_Hello_Exit(void)

{

    printk("Good-bye, Hello module was removed!\n");

}

module_init(ARM_Hello_Init);

module_exit(ARM_Hello_Exit);

MODULE_LICENSE("GPL");

 

參考Makefile

#ifneq ($(KERNELRELEASE),)

 

obj-m:=hello-dev.o

 

#else

 

#generate the path

CURRENT_PATH:=$(shell pwd)

export ARCH = arm

export  CROSS_COMPILE = arm-none-linux-gnueabi-

CC :=$(CROSS_COMPILE)gcc

LD :=$(CROSS_COMPILE)ld

 

#the absolute path

#LINUX_KERNEL_PATH:=/lib/modules/$(shell uname -r)/build

LINUX_KERNEL_PATH=/opt/ti-sdk-am335x-evm-08/board-support/linux-3.14.26-g2489c02

 

.PHONY: clean distclean

 

#complie object

default:

        make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules

       

clean:

        rm -rf *.o *.mod.c *.mod.o *.order *.symvers

 

distclean:

        rm -rf *.o *.mod.c *.mod.o *.order *.symvers *.ko

       

#endif

編譯結果生成:

hello.ko

查看結果信息:

$ modinfo hello.ko

filename:      /mnt/share/example/hello-dev/hello-dev.ko

license:        GPL

srcversion:     831720228EB3107175F1056

depends:       

vermagic:      3.14.26-g07d13c6-dirty mod_unload modversions ARMv7 p2v8

放到ARM平台加載、卸載:

$Insmod hello.ko

Hello, DeepFish Hello module is installed !

$rmmod hello.ko

Good-bye,DeepFish Hello module was removed!

大功告成!

還可參考另一份日志:

Linux上第一個驅動+應用程序:LED燈閃爍  http://www.linuxidc.com/Linux/2016-03/129598.htm

Copyright © Linux教程網 All Rights Reserved