Tiny210驱动开发之Hello world驱动模块编写

最近手边有一个Tiny210开发板,重温一下Linux下的驱动开发,搭建环境过程中遇到了一些坑,分享给大家一起学习!

构建交叉编译环境

从自带光盘中获取交叉编译工具链拷贝至PC机上,同样解压即可

$ tar -zxvf arm-linux-gcc-4.5.1-v6-vfp-20120301.tgz 

此时解压产生的目录中找到arm-linux-gcc可执行文件的路径,例如我的路径为

$ which arm-linux-gcc
$ /opt/FriendlyARM/toolschain/4.5.1/bin/arm-linux-gcc

设置环境变量

$ vi ~/.bashrc
$ #末尾增加如下内容,设置如下环境变量,保存退出
$ export PATH=/opt/FriendlyARM/toolschain/4.5.1/bin/:$PATH
$ #执行source命令,使得配置生效
$ source ~/.bashrc
$ #验证方式执行如下命令
$ arg-linux-gcc -v

命令执行成功则表示交叉环境已经遍历成功。
可以编写C语言的helloworld.c, 如下方式编译

$ vi ~/.bashrc
$ arm-linux-gcc helloworld.c -o hello

将hello可执行文件通过mount等方式传送到开发板系统中,执行可执行程序hello,执行成功,则表示交叉环境已经建立成功。说到这,开发板上运行的SSH服务真心爽,没有串口线看着桌面都舒服。

获取Linux内核源码

从tiny210自带光盘中拷贝出Linux内核源码,注意一定要与开发板上运行的内核版本一致。查看内核版本命令:

$ ubname -a

获取到的Linux内核源码应该是一个.tar.gz格式的压缩包,解压缩该压缩包:

$ tar -zxvf linux-3.0.8-20150528.tgz

编译Linux内核

在编写驱动程序之前必须要编译一下Linux内核,如果直接编写hello模块,make的时候会如下错误:

make -C /home/kernel/iTop4412_Kernel_3.0 M=/mnt/code/01qudong/mini_linux_module modules
make[1]: Entering directory '/home/kernel/iTop4412_Kernel_3.0'

  ERROR: Kernel configuration is invalid.
         include/generated/autoconf.h or include/config/auto.conf are missing.
         Run 'make oldconfig && make prepare' on kernel src to fix it.


  WARNING: Symbol version dump /home/kernel/iTop4412_Kernel_3.0/Module.symvers
           is missing; modules will have no dependencies and modversions.

  CC [M]  /mnt/code/01qudong/mini_linux_module/mini_linux_module.o
cc1: fatal error: include/generated/autoconf.h: No such file or directory
compilation terminated.
scripts/Makefile.build:311: recipe for target '/mnt/code/01qudong/mini_linux_module/mini_linux_module.o' failed
make[2]: *** [/mnt/code/01qudong/mini_linux_module/mini_linux_module.o] Error 1
Makefile:1368: recipe for target '_module_/mnt/code/01qudong/mini_linux_module' failed
make[1]: *** [_module_/mnt/code/01qudong/mini_linux_module] Error 2
make[1]: Leaving directory '/home/kernel/iTop4412_Kernel_3.0'
Makefile:18: recipe for target 'all' failed
make: *** [all] Error 2

此时有些童鞋就按照错误中提示的解决方法来解决这个错误,就会出现手动配置内核的界面或命令行,在Tiny210用户手册中已经说明了解决方法,光盘中的Linux内核源码包含了内核配置文件,而不需要自己重新配置,进入内核源码根目录,执行如下命令:

$ cp  mini210_linux_defconfig	.config #注意config前面的.

然后直接make编译内核即可。

编写helloworld驱动模块

创建一个自定义文件夹,名字随意,比如hello_module
在该文件夹下创建my_hello.c文件,内如如下:

#include <linux/kernel.h>
#include <linux/module.h>


static int __init mini6410_hello_module_init(void)
{
    printk("Hello, Mini6410 module is installed !\n");
    return 0;
}

static void __exit mini6410_hello_module_cleanup(void)
{
    printk("Good-bye, Mini6410 module was removed!\n");
}

module_init(mini6410_hello_module_init);
module_exit(mini6410_hello_module_cleanup);
MODULE_LICENSE("GPL");

编写helloworld驱动模块的Makefile文件

接下来在hello_module文件夹下编写Makefile编译驱动程序,内容如下:

obj-m := my_hello.o
KDIR=/your/linux/source/code/path/linux-3.0.8

CROSS_COMPILE=arm-linux-
CC=$(CROSS_COMPILE)gcc
LD=$(CROSS_COMPILE)ld

all:
	$(MAKE) -C $(KDIR) M=$(PWD) modules


clean:
	rm -rf *.o *.ko *.mod.* *.symvers *.order

注意其中的目标文件是my_hello.o,刚好与源码文件my_hello.c对应。
KDIR的值应为你解压缩出来的内核源码并且已经经过编译过的路径。
之后在当前目录下执行make即可生成my_hello.ko文件。

可能遇到的问题

  1. 找不到module.h头文件:
ERROR linux/module.h: No such file or directory
  1. .ko模块格式不正确
    当生成ko模块文件后,在目标板上加载ko模块时,产生如下错误:
insmod: can't insert 'hello.ko': invalid module format

出现这些问题的原因都是内核没有编译或者没有编译成功,按照我之前提到的方法重新编译内核,即可解决。
PS: 如果本文解决了你的问题,点赞分享才是最大的支持!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王大阳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值