petalinux -t modules --name ktest --enable [Usage] petalinux-create -t modules --name mymodule --enable -t type: project, apps, modules --name, -n specify the name for a component or project --enableenable the apps or modules for the project
This command will creat the file and directory project-spec/meta-user/recipes-modules/ktest as following
1 2 3 4
| files | - ktest.c | - Makefile | ktest.bb
1. Modify the base module
We first modify the ktest.c file in the directory files:
#include<linux/init.h> #include<linux/module.h> #include<linux/uaccess.h> #include<linux/fs.h> #include<linux/proc_fs.h> // Module metadata MODULE_DESCRIPTION("Hello world driver"); MODULE_LICENSE("GPL"); // Custom init and exit methods staticint __init custom_init(void) { /*When a function is declared as static in C, it means that the function has internal linkage, which restricts its visibility to the translation unit (source file) where it is defined. In other words, the function is only accessible within the same source file.*/ printk(KERN_INFO "Hello world driver loaded.\n"); return0; } staticvoid __exit custom_exit(void) { printk(KERN_INFO "Goodbye my friend...\n"); } // Register the init and exit function // module_init() will either be called during do_initcalls() (if builtin) // or at module insertion time (if a module). There can only be one per module. module_init(custom_init); // module_exit() will wrap the driver clean-up code with cleanup_module() when // used with rmmod when the driver is a module. If the driver is statically // compiled into the kernel, module_exit() has no effect. There can only be one // per module. module_exit(custom_exit);
Then, we update the ktest.bb with appending the following configuration:
1
KERNEL_MODULE_AUTOLOAD += "ktest"
This configuration will automatically load the kernel while booting.