Logo

dev-resources.site

for different kinds of informations.

NXP i.MX8MP Platform Porting Driver Tutorial

Published at
1/15/2025
Categories
programming
tutorial
beginners
devops
Author
ronnie_r_152dc2151d9449c6
Author
25 person written this
ronnie_r_152dc2151d9449c6
open
NXP i.MX8MP Platform Porting Driver Tutorial

This tutorial will take the example of writing and porting a driver called Hello Driver and explain the complete process from code writing to driver testing in detail. This process teaches you how to create, configure, compile, and load custom drivers on the i.MX8MP platform.

Step 1: Create the drive directory and files
1.1 Go to the source code directory

Before we start writing the driver, we first need to enter the directory where the Linux kernel source code is located. Open a terminal and enter the following command to navigate to the driver's folder in your Linux kernel source directory.

cd /home/forlinx/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers
1.2 Create a new Hello directory

After entering the drivers folder, we create a new directory called hello to store the files related to the hello driver.

mkdir hello
cd hello
1.3 Write the Hello driver source code:

create the hello.c file using a text editor (e.g. vi) and enter the following code:

include

include

static int hello_init(void) {
printk(KERN_ALERT "Hello world\n");
return 0;
}
static void hello_exit(void) {
printk(KERN_ALERT "Goodbye world\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("Dual BSD/GPL");
Meaning of the demo program: Print Hello World when the insmod driver is mounted, and Goodbye World when the rmmod driver is unmounted.

Step 2: Configure Kconfig and Makefile
2.1 Create Kconfig:

Create a new Kconfig file in the hello directory to define the configuration options of the driver.

forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers/hello$ vi Kconfig
Write the following in the Kconfig file:

config HAVE_HELLO
tristate "hello driver"
help
This hello driver is just to show how to develop driver process.
This driver can also be built as a module. If so, the module will be called.
default y
Indicates that if CONFIG_HAVE_HELLO is enabled in the kernel trimming configuration file, the hello drivers menu will be displayed and compiled into the kernel by default:

y:Compile into the kernel

m:Compile as a module.ko file

n:Indicates no compilation, not enabled.

2.2 Create Makefile:

Create a new Makefile in the hello directory and specify the compilation rules.

forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers/hello$ vi Makefile
Write the following in the Makefile file:

obj-$(CONFIG_HAVE_HELLO) += hello.o
Note: The name of the macro definition should be the same as that in Kconfig. Add the name of the file that needs to be compiled. Because the kernel automatically adds the prefix CONFIG, we also need to add CONFIG _ in front of the name here. Indicates that the file specified by the compilation rule is a hello. c when the CONFIG _ HAVE _ HELLO is enabled.

2.3 Modify file permissions:

Ensure that the hello. C, Kconfig, and Makefile files have executable permissions.

forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers/hello$ chmod 777 hello.c
forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers/hello$ chmod 777 Kconfig
forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers/hello$ chmod 777 Makefile
Step 3: Integrate into the kernel build system
3.1 Edit the top-level kconfig:

In the kconfig file in the drivers directory, add a configuration reference to the hello directory.

Write the following in the Kconfig file:

source "drivers/counter/Kconfig"
source "drivers/mxc/Kconfig"
source "drivers/hello/Kconfig" //Add the configuration file parsing of the hello folder before the endmenu
endmenu
In this way, the configuration system will parse the Kconfig under the hello folder according to this configuration.

3.2 Edit the top-level Makefile:

In the Makefile in the drivers directory, add the conditions for compiling the hello driver.

Write the following in the Makefile file:

obj-$(CONFIG_COUNTER) += counter/
obj-y += mxc/
obj-$(CONFIG_HAVE_HELLO) += hello/ //Add this sentence at the end of Kconfig
The effect of this statement is where to find the source file when CONFIG_HAVE_HELLO is enabled. Combining this with the hello file under the module Makefile creates a hierarchical Makefile. Note that/is not missing, and the name of the custom folder is added here, which means that the folder is compiled into the kernel.

Step 4: Compile the driver
Set the compilation environment:

Load the compilation environment variables to ensure that the correct cross-compilation toolchain is used.

forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers$ cd ../..
forlinx@ubuntu:~/work/OK8MP-linux-sdk$ . /opt/fsl-imx-xwayland/5.4-zeus/environment-setup-aa\
rch64-poky-linux
forlinx@ubuntu:~/work/OK8MP-linux-sdk$ . environment-setup-aarch64-poky-linux
forlinx@ubuntu:~/work/OK8MP-linux-sdk$ cd OK8MP-linux-kernel
forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel$ make modules
scripts/kconfig/conf --syncconfig Kconfig
drivers/hello/Kconfig:7:warning: ignoring unsupported character '�'
drivers/hello/Kconfig:7:warning: ignoring unsupported character '�'
drivers/hello/Kconfig:7:warning: ignoring unsupported character '�'
drivers/hello/Kconfig:7:warning: ignoring unsupported character '�'
*

  • Restart config... * *
  • Device Drivers * Trust the bootloader to initialize Linux's CRNG (RANDOM_TRUST_BOOTLOADER) [N/y/?] n Platform support for Chrome hardware (transitional) (MFD_CROS_EC) [Y/n/m/?] y Trusted Execution Environment support (TEE) [Y/n/m/?] y hello driver (HAVE_HELLO) Y/n/m/? m //Compile the hello driver into the kernel and configure it to m. CALL scripts/checksyscalls.sh CALL scripts/atomic/check-atomics.sh CHK include/generated/compile.h GZIP kernel/config_data.gz CC kernel/configs.o AR kernel/built-in.a CC [M] drivers/hello/hello.o GEN .version CHK include/generated/compile.h UPD include/generated/compile.h CC init/version.o AR init/built-in.a LD vmlinux.o MODPOST vmlinux.o MODINFO modules.builtin.modinfo LD .tmp_vmlinux.kallsyms1 KSYM .tmp_vmlinux.kallsyms1.o LD .tmp_vmlinux.kallsyms2 KSYM .tmp_vmlinux.kallsyms2.o LD vmlinux SORTEX vmlinux SYSMAP System.map Building modules, stage 2. MODPOST 536 modules CC [M] drivers/hello/hello.mod.o LD [M] drivers/hello/hello.ko After compiling, you can see the compiled driver in the OK8MP-linux-kernel/drivers/hello directory:

forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/$ ls drivers/hello
hello.c hello.ko hello.mod hello.mod.c hello.mod.o hello.o Kconfig Makefile modules.order
Step 5: Test Drive
Copy the driver to the development board:

Use U disk or TF card to copy the hello. Ko file to the development board.

On the development board, use insmod to load the driver, rmmod to unload the driver, and observe the print information.

insmod hello.ko

Should see "Hello world" printed

rmmod hello.ko

Should see the print information of the Goodbye world ".

So far, the writing, compiling and testing of the first Hello driver on the i.MX8MP platform have been completed. Hopefully, this simple example has opened the door to driver development, and it can serve as a foundation for exploring more complex and interesting driver development projects in the future.

tutorial Article's
30 articles in total
Tutorials offer step-by-step instructions to help learners grasp concepts and complete tasks in various domains.
Favicon
Creating a live HTML, CSS and JS displayer
Favicon
Build Your First AI Application Using LlamaIndex!
Favicon
Creating Arrays with Reference Variables
Favicon
How To Build Beautiful Terminal UIs (TUIs) in JavaScript 2: forms!
Favicon
Chronicles of Supermarket website
Favicon
Easy development environments with Nix and Nix flakes!
Favicon
ruby -run
Favicon
Основы изучения Python: Руководство для начинающих
Favicon
How to Use JavaScript to Reduce HTML Code: A Simple Example
Favicon
SQL Performance Tuning: Best Practices for Faster Queries
Favicon
Php Base64 encode/decode – best practices and use cases
Favicon
10 Must-Bookmark Open Source Projects for Developers
Favicon
Easy 301 Redirects For SEO
Favicon
ruby -run, again
Favicon
🚀 New Book Release: "Navigate the Automation Seas" – A Practical Guide to Building Automation Frameworks
Favicon
Top Kubernetes CI/CD Tools in 2025
Favicon
340+ Websites every developer should know
Favicon
Survival Manual: How to Create and Manage a Project in Git
Favicon
Strong Female Role Models in the Sector: Oya Narin
Favicon
Test Scenarios vs. Test Cases: Understanding the Differences
Favicon
Angular validation common functions
Favicon
KDE vs GNOME vs Others: Choosing the Best Linux Desktop Environment in 2025
Favicon
NXP i.MX8MP Platform Porting Driver Tutorial
Favicon
Response Handling Best Practices
Favicon
Why AWS Matters: A Beginner's View
Favicon
How the Internet Works: OSI Model, DNS, TCP vs. UDP, and Essential Concepts for Beginners
Favicon
Resolving Auto-Scroll issues for overflow container in a Nuxt app
Favicon
#131 — Use Association Table to Handle Interval Association
Favicon
Deferred loading with @defer: Optimize Your App's Performance
Favicon
Psychotherapy Technology Advancements

Featured ones: