iMX8MP驅(qū)動(dòng)移植全過程講解

原創(chuàng) 2022-11-07 13:22:00 imx8mp移植驅(qū)動(dòng)

 各位工程師用戶在對(duì)飛凌嵌入式OKMX8MP-C開發(fā)板進(jìn)行開發(fā)的過程中,可能會(huì)遇到需要移植驅(qū)動(dòng)的情況。為避免用戶因不了解移植驅(qū)動(dòng)的過程而影響開發(fā)進(jìn)度,今天小編會(huì)基于NXP iMX8MP寫一個(gè)hello驅(qū)動(dòng)為例,演示移植驅(qū)動(dòng)的過程,有需求的小伙伴可參考此方法自行操作。

imx8mp開發(fā)板


進(jìn)入源碼的drivers目錄下,并創(chuàng)建一個(gè)名為hello的目錄:

forlinx@ubuntu:~$ cd /home/forlinx/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers
forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers$mkdir hello

進(jìn)入hello目錄,創(chuàng)建hello.c:

forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers$ cd hello
forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers/hello$ vi hello.c

在hello.c中寫入如下內(nèi)容:

#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");

程序含義:insmod驅(qū)動(dòng)掛載時(shí)打印Hello world,rmmod驅(qū)動(dòng)卸載時(shí)打印 Goodbye world

在該文件夾下創(chuàng)建Kconfig,Makefile兩個(gè)文件。

forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers/hello$ vi Kconfig

在Kconfig文件中寫入如下內(nèi)容:

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
#endmenu

表示如果使能了CONFIG_HAVE_HELLO,在內(nèi)核裁剪配置文件中,將顯示hellodrivers菜單,默認(rèn)編譯進(jìn)內(nèi)核:

y:編譯進(jìn)內(nèi)核
m:編譯為模塊.ko文件
n:表示不編譯,未使能。


forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers/hello$ vi Kconfig

在Makefile文件中寫入如下內(nèi)容:

obj-$(CONFIG_HAVE_HELLO) += hello.o

注意:

宏定義的名字要和Kconfig中的一樣。后面添加需要編譯的文件名,因?yàn)閮?nèi)核會(huì)自動(dòng)添加前綴CONFIG,所以我們這里也要在名字前面添加CONFIG_,表示CONFIG_HAVE_HELLO使能時(shí),編譯規(guī)則指定的文件為hello.c。

給添加的這三個(gè)文件權(quán)限:

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

編輯drivers頂層的Kconfig,Makefile文件。

forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers/hello$ cd ..
forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers$ vi Kconfig

在Kconfig文件中寫入如下內(nèi)容:

source "drivers/counter/Kconfig"
source "drivers/mxc/Kconfig"
source "drivers/hello/Kconfig" //在endmenu前添加hello文件夾的配置文件解析
endmenu

如此一來,配置系統(tǒng)就會(huì)按照這個(gè)配置去解析hello文件夾下的Kconfig。

編輯Makefile:

forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers$ vi Makefile

在Makefile文件中寫入如下內(nèi)容:

obj-$(CONFIG_COUNTER)                   += counter/
obj-y                                                       += mxc/
obj-$(CONFIG_HAVE_HELLO)             += hello/           //在Makefile最后加入這一句

這句話的作用是當(dāng)CONFIG_HAVE_HELLO使能后,在哪里去找源文件。再結(jié)合hello文件下模塊Makefile就形成了層次式Makefile。注意不要少了/,這里添加自定義文件夾的名字,表示把這個(gè)文件夾編譯進(jìn)內(nèi)核。

開始編譯:

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-aarch64-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/?] (NEW) m    //將hello驅(qū)動(dòng)編譯進(jìn)內(nèi)核就配置為m
CALL scripts/checksyscalls.sh
CALL scripts/atomic/check-atomics.sh
CHK include/generated/compile.h
GZIP kernel/config_data.gz

編譯完成后,即可在OK8MP-linux-kernel/drivers/hello目錄下看到編譯生成的驅(qū)動(dòng)了:

將hello.ko使用U盤或TF卡拷貝到開發(fā)板里進(jìn)行驗(yàn)證:

root@OK8MP:~# cd /run/media/sda1/           //進(jìn)入U(xiǎn)盤的路徑下
root@OK8MP:/run/media/sda1# insmod hello.ko           //掛載hello.ko [ 138.679964] Hello world           //掛載驅(qū)動(dòng)打印信息
root@OK8MP:/run/media/sda1# rmmod hello.ko           //卸載hello.ko
[ 142.022115] Goodbye world           //卸載驅(qū)動(dòng)打印信息
root@OK8MP:/run/media/sda1#

由上述測(cè)試可看,hello.ko驅(qū)動(dòng)可正常運(yùn)行。 

以上就是小編基于NXP iMX8MP開發(fā)板為大家演示的自行書寫并添加一個(gè)驅(qū)動(dòng)的過程,若您想要移植某一個(gè)模塊,可向模塊廠家索要現(xiàn)成的驅(qū)動(dòng).c文件,之后再按照上述步驟配置Makefile和Kconfig即可。

關(guān)于iMX8MP開發(fā)板的詳細(xì)信息,您可以點(diǎn)擊下圖了解。

相關(guān)產(chǎn)品 >

  • OKMX8MP-C開發(fā)板

    內(nèi)置NPU、ISP,AI計(jì)算能力高達(dá)2.3TOPS|飛凌嵌入式i.MX8MP 系列-NXP iMX8M Plus 開發(fā)板 基于高性能低功耗工業(yè)級(jí)iMX8MP核心板設(shè)計(jì),支持多種多種高速通信接口。iMX8MP開發(fā)板內(nèi)置NPU,AI計(jì)算能力2.3TOPS,支持4K,支持雙圖像信號(hào)處理器(ISP),是一款支持LinuxQT/android操作系統(tǒng)的iMX8MP開發(fā)板。

    了解詳情
    OKMX8MP-C開發(fā)板
  • FETMX8MP-C核心板

    iMX8MP核心板基于 NXP  i.MX 8M Plus 處理器設(shè)計(jì),  采用4核Cortex-A53 和 Cortex-M7架構(gòu)。支持雙千兆網(wǎng)口,iMX8MP性能強(qiáng)勁最高運(yùn)行速率可達(dá)2.3TOPS,并且i.MX8MP功耗更低≤2W 。iMX 8M Plus系列專注于機(jī)器學(xué)習(xí)和視覺、高級(jí)多媒體以及具有高可靠性的工業(yè)自動(dòng)化。它旨在滿足智慧家庭、樓宇、城市和工業(yè)4.0應(yīng)用的需求。飛凌iMX8MP核心板提供用戶手冊(cè),iMX8MP原理圖,引腳定義等。
    了解詳情
    FETMX8MP-C核心板

推薦閱讀 換一批 換一批