0. Driver Development for Xilinx MPSoC FPGA

0. Create a kernel module using petalinux

Use the command below to create a kernel module

1
2
3
4
5
6
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
--enable enable 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#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
static int __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");
return 0;
}
static void __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.

2. Compile the kernel image and boot

3. Test the kernel

After booting the kernel,

1
2
3
4
$ dmesg | grep "Hello"
[ 19.061541] Hello world driver loaded.
$ rmmod ktest && dmesg | grep "Goodbye"
[ 97.127431] Goodbye my friend...