4313|3

203

帖子

0

TA的资源

一粒金砂(中级)

楼主
 

飞凌嵌入式i.MX8MP开发板移植驱动,其实可以很简单 [复制链接]

各位工程师用户在对飞凌嵌入式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 <linux/init.h>
  • #include <linux/module.h>
  • 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卡拷贝到开发板里进行验证:​​​​​​​

  • rootK8MP:~# 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即可。

此帖出自ARM技术论坛

最新回复

感谢分享,学习了,很好的内容,加油!!!!   详情 回复 发表于 2024-8-27 19:17
点赞 关注(1)
 

回复
举报

7209

帖子

11

TA的资源

版主

沙发
 
这个编译,是要跟固件一起编译吗,还是单独的编译。如果全编译,需要时间久吗?
此帖出自ARM技术论坛
 
 
 

回复

8

帖子

0

TA的资源

一粒金砂(初级)

板凳
 

感谢分享,学习了!!

此帖出自ARM技术论坛
 
 
 

回复

417

帖子

0

TA的资源

纯净的硅(中级)

4
 

感谢分享,学习了,很好的内容,加油!!!!

此帖出自ARM技术论坛
 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条
【干货上新】电源解决方案和技术第二趴 | DigiKey 应用探索站
当月好物、电源技术资源、特色活动、DigiKey在线实用工具,干货多多~

查看 »

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

 
机器人开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网 7

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表