嵌入式Linux驱动开发中的编译与调试技巧

以韦东山imx6ull Pro开发板为例

编译内核,不同的开发板对应不同的配置文件,配置文件位于内核源码arch/arm/configs/目录

cd /home/book/100ask_imx6ull-sdk/Linux-4.9.88
make mrproper
make 100ask_imx6ull_defconfig
make zImage -j4

内核目录下编译内核模块

make modules

内核目录下安装内核模块

make ARCH=arm INSTALL_MOD_PATH=/home/book/nfs_rootfs modules_install

编译完成后,在arch/arm/boot目录下生成zImage内核文件,拷贝内核到网络文件系统

cp arch/arm/boot/zImage ~/nfs_rootfs

用vi命令修改相应的设备树文件dts,编译设备树文件

make dtbs

在arch/arm/boot/dts目录下生成设备树的二进制文件100ask_imx6ull-14x14.dtb,拷贝编译好的设备树dtb文件到网络文件系统

cp arch/arm/boot/dts/100ask_imx6ull-14x14.dtb ~/nfs_rootfs

安装tree命令查看目录结构

sudo apt install tree
cd ~/nfs_rootfs/
tree

安装内核和模块到开发板

mount -t nfs -o nolock,vers=3 192.168.5.11:/home/book/nfs_rootfs /mnt
cp /mnt/zImage /boot
cp /mnt/100ask_imx6ull-14x14.dtb /boot
cp /mnt/lib/modules /lib -rfd
sync

重启开发板

cd /sys/firmware/devicetree/base/

查看设备树该节点

ls -ld *100ask*

内核解析后生成的设备节点

cd /sys/bus/platform/devices

在Linux的要编译的文件目录下配置环境变量,不同开发板不一定相同

export ARCH=arm
export CROSS_COMPILE=arm-buildroot-linux-gnueabihf-
export  PATH=$PATH:/home/book/100ask_imx6ull-sdk/ToolChain/arm-buildroot-linux-gnueabihf_sdk-buildroot/bin

测试交叉编译工具链环境变量

echo $ARCH
arm
echo $CROSS_COMPILE
arm-buildroot-linux-gnueabihf-
arm-buildroot-linux-gnueabihf-gcc -v

编写makefile文件

指定KERN_DIR = /home/book/100ask_imx6ull-sdk/Linux-4.9.88内核路径

# 参考内核源码drivers/char/ipmi/Makefile

# 要想把a.c, b.c编译成ab.ko, 可以这样指定:

# ab-y := a.o b.o

# obj-m += ab.o

# 将a.c b.c 分别编译成a.o b.o

# 若b依赖于a,则应先编译a

# 即obj-m +=a.o b.o

make编译程序

拷贝编译好的程序到网络文件系统下

cp *.ko test ~/nfs_rootfs

开发板挂载网络文件系统

mount -t nfs -o nolock,vers=3 192.168.5.11:/home/book/nfs_rootfs /mnt

打开内核打印信息

echo "7 4 1 7" > /proc/sys/kernel/printk
insmod /mnt/xxx.ko

安装完成后可以执行lsmod 命令来查看是否安装成功

驱动程序写好是在dev文件夹里

执行测试程序

chmod +x ./ledtest

为什么编译驱动程序之前要先编译内核?

  • 驱动程序要用到内核文件:

比如驱动程序中这样包含头文件:#include <asm/io.h>,其中的asm是一个链接文件,指向asm-arm或asm-mips,这需要先配置、编译内核才会生成asm这个链接文件。

  • 编译驱动时用的内核、开发板上运行到内核,要一致:

开发板上运行到内核是出厂时烧录的,你编译驱动时用的内核是你自己编译的,这两个内核不一致时会导致一些问题。所以我们编译驱动程序前,要把自己编译出来到内核放到板子上去,替代原来的内核。

  • 更换板子上的内核后,板子上的其他驱动也要更换:

板子使用新编译出来的内核时,板子上原来的其他驱动也要更换为新编译出来的。所以在编译我们自己的第1个驱动程序之前,要先编译内核、模块,并且放到板子上去。

Linux下有3大帮助方法:help、man、info。

想查看某个命令的用法时,比如查看ls命令的用法,可以执行:ls --help

help只能用于查看某个命令的用法,而man手册既可以查看命令的用法,还可以查看函数的详细介绍等等。它含有9大分类,如下:

1 Executable programs or shell commands // 命令

2 System calls (functions provided by the kernel) // 系统调用,比如 man 2 open

3 Library calls (functions within program libraries) // 函数库调用

4 Special files (usually found in /dev) // 特殊文件, 比如 man 4 tty

5 File formats and conventions eg /etc/passwd // 文件格式和约定, 比如man 5 passwd

6 Games // 游戏

7 Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7)  //杂项

8 System administration commands (usually only for root) // 系统管理命令

9 Kernel routines [Non standard] // 内核例程

比如想查看open函数的用法时,可以直接执行“man open”,发现这不是想要内容时再执行“man 2 open”。

在man命令中可以及时按“h”查看帮助信息了解快捷键。常用的快捷键是:

f 往前翻一页

b 往后翻一页

/patten 往前搜

?patten 往后搜

就内容来说,info手册比man手册编写得要更全面,但man手册使用起来更容易些。

以书来形容info手册和man手册的话,info手册相当于一章,里面含有若干节,阅读时你需要掌握如果从这一节跳到下一节;而man手册只相当于一节,阅读起来当然更容易。

可以直接执行“info”命令后,输入“H”查看它的快捷键,在info手册中,某一节被称为“node”,常用的快捷键如下:

Up         Move up one line.

Down    Move down one line.

PgUp     Scroll backward one screenful.

PgDn     Scroll forward one screenful.

Home    Go to the beginning of this node.

End        Go to the end of this node.

TAB        Skip to the next hypertext link.

RET        Follow the hypertext link under the cursor.

l             Go back to the last node seen in this window.

[             Go to the previous node in the document.

]             Go to the next node in the document.

p            Go to the previous node on this level.

n            Go to the next node on this level.

u            Go up one level.

t             Go to the top node of this document.

d            Go to the main 'directory' node.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值