Note: If you want to follow this practice, you need to have a functioning arm development Board and a corresponding version of the kernel (if you want to compile and run on a Linux host, please refer to the end of the 1)
1. Create the following file in the drivers directory of the corresponding version kernel:
Module file Tree
Where the file code is as follows:
/* hello.c */
#include <linux/init.h>#include <linux/module.h>Static intHello_init (void) {PRINTK (kern_info"[init] Can you feel me?\n");return 0;}Static voidHello_exit (void) {PRINTK (kern_info"[Exit] yes.\n");} Module_init (Hello_init); Module_exit (Hello_exit); Module_author ("Alan Wang <[email protected]>"); Module_license ("GPL"); Module_description ("A simple Hello World Module"); Module_alias ("A Simple Module");
/* Kconfig */
# Drivers/alan_test/kconfigMenu"Alan_test Driver"Comment"alan_test Driver comment"Config alan_testBOOL "Alan_test Support"Config HELLO tristate"Hello module test"Depends on Alan_testendmenu
/* Makefile */
# drivers/alan_test/Makefile# makefile for the ALAN_TESTobj-$(CONFIG_HELLO) += hello.o
2. Modify the Kconfig Makefie of the previous level of the directory:
Drivers Kconfig end Endmenu Add a row before
source "drivers/alan_test/Kconfig"endmenu
Add a line to the end of Makefile under drivers
obj-$(CONFIG_ALAN_TEST) += alan_test/
3. Make Menuconfig Add the newly-written module to the kernel:
In turn: Device drivers-->
Alan_test driver-->
Alan_test Driver Comment * * *
[*] Alan_test Support
< M > Hello module test
Exit Save Menuconfig
4. Compile the kernel:
Compiling with make
The generated zimage (arch/arm/boot/zimage) is burned to the corresponding location of the Development Board.
The generated Hello.ko(Drivers/alan_test/hello.ko) is copied to a directory on the Development Board Linux system.
4. Insert the kernel module:
Insmod
5. Use Modinfo to view module information:
Modinfo
Attachment 1: Compile the module to run on the PC
Put hello.c and Makefile in the same directory.
/* Makefile */
Kernel_ver=$ (shell uname-r)# kernel ModulesObj-m + = hello.o# Specify flags for the module compilationExtra_cflags=-g-o0build:kernel_modulesKernel_modules:Make-c/lib/modules/$ (kernel_ver)/build m=$ (CURDIR) modulesClean :Make-c/lib/modules/$ (kernel_ver)/build m=$ (CURDIR) Clean
Compile Run steps such as:
Hello module compiled and run on PC
Linux simplest kernel module Hello World Example