国产精品免费无遮挡无码永久视频-国产高潮视频在线观看-精品久久国产字幕高潮-国产精品99精品无码视亚

查看: 1601|回復: 0
打印 上一主題 下一主題

i.MX8MP開發板移植驅動全過程

[復制鏈接]
跳轉到指定樓層
樓主
發表于 2022-11-4 11:44:01 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式

各位工程師用戶在對飛凌嵌入式OKMX8MP-C開發板進行開發的過程中,可能會遇到需要移植驅動的情況。為避免用戶因不了解移植驅動的過程而影響開發進度,今天小編會以寫一個hello驅動為例,演示移植驅動的過程,有需求的小伙伴可參考此方法自行操作。



01進入源碼的drivers目錄下,并創建一個名為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



02進入hello目錄,創建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中寫入如下內容:



  • #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驅動掛載時打印Hello world,rmmod驅動卸載時打印 Goodbye world


03​​​​​​​在該文件夾下創建Kconfig,Makefile兩個文件。

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

在Kconfig文件中寫入如下內容:



  • 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,在內核裁剪配置文件中,將顯示hellodrivers菜單,默認編譯進內核:

y:編譯進內核

m:編譯為模塊.ko文件

n:表示不編譯,未使能。

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

在Makefile文件中寫入如下內容:

obj-$(CONFIG_HAVE_HELLO)        +=      hello.o

注意:

宏定義的名字要和Kconfig中的一樣。后面添加需要編譯的文件名,因為內核會自動添加前綴CONFIG,所以我們這里也要在名字前面添加CONFIG_,表示CONFIG_HAVE_HELLO使能時,編譯規則指定的文件為hello.c。


給添加的這三個文件權限:​​​​​​​



  • 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



04編輯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文件中寫入如下內容:​​​​​​​



  • source  "drivers/counter/Kconfig"



  • source  "drivers/mxc/Kconfig"



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



  • endmenu



如此一來,配置系統就會按照這個配置去解析hello文件夾下的Kconfig。


編輯Makefile:


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

在Makefile文件中寫入如下內容:



  • obj-$(CONFIG_COUNTER)           += counter/



  • obj-y                           += mxc/



  • obj-$(CONFIG_HAVE_HELLO)        +=      hello/     //在Makefile最后加入這一句



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


5開始編譯:



  • 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驅動編譯進內核就配置為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



  • […]



  • 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




編譯完成后,即可在OK8MP-linux-kernel/drivers/hello目錄下看到編譯生成的驅動了:​​​​​​​



  • 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



6



將hello.ko使用U盤或TF卡拷貝到開發板里進行驗證:​​​​​​​



  • root@OK8MP:~# cd  /run/media/sda1/              //進入U盤的路徑下



  • root@OK8MP:/run/media/sda1#  insmod  hello.ko   //掛載hello.ko



  • [  138.679964] Hello  world                     //掛載驅動打印信息



  • root@OK8MP:/run/media/sda1#  rmmod  hello.ko    //卸載hello.ko



  • [  142.022115]  Goodbye  world                  //卸載驅動打印信息



  • root@OK8MP:/run/media/sda1#



由上述測試可看,hello.ko驅動可正常運行。


以上就是小編為大家演示的自行書寫并添加一個驅動的過程,若您想要移植某一個模塊,可向模塊廠家索要現成的驅動.c文件,之后再按照上述步驟配置Makefile和Kconfig即可。


您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

關于我們  -  服務條款  -  使用指南  -  站點地圖  -  友情鏈接  -  聯系我們
電子工程網 © 版權所有   京ICP備16069177號 | 京公網安備11010502021702
快速回復 返回頂部 返回列表