[原][树莓派] - 内核模块编译--续
来源:互联网 发布:vue.js 列表显示隐藏 编辑:程序博客网 时间:2024/05/03 11:26
目的
验证树莓派的内核以及内核头文件的正确性,为后面开发驱动做准备。
使用工具
1、安装raspbian的树莓派一个。
2、网线,有串口更好。
相关附件下载
1、内核源代码头文件
http://www.niksula.hut.fi/~mhiienka/Rpi/linux-headers-rpi/
操作教程
1、内核版本确认
使用下面命令查看内核版本
</pre><pre name="code" class="python"> $ uname -r3.18.11+
2、下载内核版本对应的内核头文件。
$ sudo apt-get install linux-headers-3.18.11+
可惜了,没有可以用的版本。
3、应对2中出现的问题
1)使用附件地址下载对应的内核头文件包
http://www.niksula.hut.fi/~mhiienka/Rpi/linux-headers-rpi/linux-headers-3.18.11%2B_3.18.11%2B-2_armhf.deb
得到包之后使用ftp上传到树莓派上。
执行下面命令:
$ ar -xlinux-headers-3.18.11\ _3.18.11\ -2_armhf.deb
得到3个文件:
control.tar.gz debian-binary data.tar.gz
解压data.tar.gz
$ tar vxfdata.tar.gz
得到./usr/src/linux-headers-3.18.11+目录,进入目录执行命令拷贝树莓派的config.gz
$ lsarch Documentation fs ipc kernel mm samples sound virtblock drivers include Kbuild lib Module.symvers scripts toolscrypto firmware init Kconfig Makefile net security usr$ gzip -dc /proc/config.gz > .config
执行make oldconfig
$ make oldconfigscripts/kconfig/conf--oldconfig Kconfig## configurationwritten to .config#
中途可能出现NEW的设备,直接N加回车就行了。等待2分钟就好了。
2)直接安装带有内核头文件的内核版本
$ sudo apt-get installlinux-image-3.18.0-trunk-rpi
更改启动设置
config.txt尾部添加:
kernel=vmlinuz-3.18.0-trunk-rpiinitramfsinitrd.img-3.18.0-trunk-rpi followkernel
重启
$ sudo reboot
查看内核版本:
$ uname -r3.18.0-trunk-rpi
然后安装对应内核头文件:
$ sudo apt-getinstall linux-headers-3.18.0-trunk-rpi
以上请注意选择内核的版本,区分树莓派 1与2。装上新内核之后,红色LED灯不亮了,这个不清楚是什么情况,但一定是有什么没有设置对。
4、内核模块helloworld编写
源代码编写
/*!hello.c */#include <linuxinit.h="">#include <linuxmodule.h="">MODULE_LICENSE("DualBSD/GPL"); static inthello_init(void){ printk(KERN_ALERT"Hello, world"); return 0;} static voidhello_exit(void) { printk(KERN_ALERT"Goodbye, cruel world");} module_init(hello_init);module_exit(hello_exit);
Makefile编写
obj-m := hello.oKDIR := /usr/src/linux-headers-3.18.0-trunk-rpi/ #选择自己内核头文件的位置all:make-C $(KDIR) M=$(PWD) modulesclean:rm-f *.ko *.o *.mod.o *.mod.c *.symvers modul*
5、实验结果
准备工作做好了,现在开始编译
$ make
会生成以下文件:
$ lshello.c hello.mod.c hello.o modules.orderhello.ko hello.mod.o Makefile Module.symvers
查看这个内核模块的信息:
$ modinfo hello.kofilename: /home/pi/workspace/kernel_modules/helloworld/hello.kolicense: Dual BSD/GPLdepends: vermagic: 3.18.0-trunk-rpi preempt mod_unloadmodversions ARMv6
加载内核:
$ sudo insmod hello.ko
移除
$ sudo rmmod hello
查看输出:
$ dmesg | tail -2[ 511.065934] Hello, World[ 520.894053] Goodbye, cruel world
可以看到,这个hello内核模块工作的很正常。
总结
内核模块编译的环境需要内核源代码以及相同版本的内核,最好用源代码编译好内核。
内核模块的编译速度比较快,电脑上几乎感觉不到。树莓派需要10s左右。
- [原][树莓派] - 内核模块编译--续
- 树莓派内核模块交叉编译
- 树莓派编译内核(编译模块)
- 内核编译 & 模块编译
- linux 模块编译步骤(原)
- 树莓派3B 内核模块编译
- 编译内核及内核模块
- 内核模块编译入门
- 内核模块编译说明
- 编译内核模块命令
- 编译内核模块
- linux内核模块编译
- Ubuntu内核模块编译
- 开始编译内核模块
- 编译内核模块
- 编译内核模块问题
- 内核模块及编译
- linux内核模块编译
- 网络多线程下载图片(SDWebImage)
- leetcode笔记:Combination Sum III
- merker—hellman背包加密的C语言简单实现
- OC--第一周精华
- 采用Java写冒泡法———2015.11.27
- [原][树莓派] - 内核模块编译--续
- 我新建的小网站
- 网络多线程-pthread的使用
- 绝对路径和相对路径---速记
- des工具类
- VPS架设VPN教程
- ping
- 包装类
- c读取mat文件,并调用m生成的dll中的函数,函数返回值保存为mat文件