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.

programming Article's
30 articles in total
Programming is the process of writing, testing, and maintaining code to create software applications for various purposes and platforms.
Favicon
7 Developer Tools That Will Boost Your Workflow in 2025
Favicon
What ((programming) language) should I learn this year, 2025 ?
Favicon
Lessons from A Philosophy of Software Design
Favicon
🕒 What’s your most productive time of the day?
Favicon
Designing for developers means designing for LLMs too
Favicon
Unique Symbols: How to Use Symbols for Type Safety
Favicon
Filling a 10 Million Image Grid with PHP for Internet History
Favicon
When AI Fails, Good Documentation Saves the Day 🤖📚
Favicon
The Language Server Protocol - Building DBChat (Part 5)
Favicon
Основы изучения Python: Руководство для начинающих
Favicon
GraphQL Transforming API Development
Favicon
Easy Discount Calculation: Tax, Fees & Discount Percentage Explained
Favicon
Example of using Late Static Binding in PHP.
Favicon
Top 5 Python Scripts to Automate Your Daily Tasks: Boost Productivity with Automation
Favicon
How to Resolve the 'Permission Denied' Error in PHP File Handling
Favicon
7 Mistakes Developers Make When Learning a New Framework (and How to Avoid Them)
Favicon
Why Is Everyone Unhappy With JavaScript? | State of Javascript 2024 Survey
Favicon
Python в 2025: стоит ли начинать с нуля? Личный опыт и рекомендации
Favicon
Cómo gestionar tus proyectos de software con Github
Favicon
Decreasing server load by using debouncing/throttle technique in reactjs
Favicon
2429. Minimize XOR
Favicon
➡️💡Guide, Innovate, Succeed: Becoming a Software Development Leader 🚀
Favicon
Debugging Adventure Day 1: What to Do When Your Code Doesn’t Work
Favicon
🚀 New Book Release: "Navigate the Automation Seas" – A Practical Guide to Building Automation Frameworks
Favicon
Булеві типи і вирази
Favicon
Build a Secure Password Generator with Javascript
Favicon
join my project semester simulator
Favicon
Как создать свой VPN и получить доступ ко всему?
Favicon
Revolutionary AI Model Self-Adapts Like Human Brain: Transformer Shows 15% Better Performance in Complex Tasks
Favicon
Flow Networks Breakthrough: New Theory Shows Promise for Machine Learning Structure Discovery

Featured ones: