linux自制驱动入门--Apple的学习笔记
2018-05-07 本文已影响1人
applecai
1. 自动创建设备文件
1.1 我参考其它博客,建立了hello的ko驱动模块。你们可以从我的网址获取。
https://github.com/AppleCai/bb-black-linux/tree/master/mysrc/hello
1.2 编译Ko方法:
注意Makefile中的kernel dir是否正确。并且与你的开发板kernel版本一致。
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- modules
1.3 编译app文件方法
arm-linux-gnueabihf-gcc test.c
1.4 加载hello模块
insmod hello.ko
加载成功后,通过/dev可以找到设备文件,通过dmesg也可以看到信息。
1.5 运行test文件,调用驱动
./test
1.6 结果显示
1:this is read_buf:
read_buf =
/dev/hello opened, fd=3
Read returns 127
2:this is read_buf:
read_buf = foobar not equal to barfoo
write buffer: beijing 15071 bu yao shui jiao
/dev/hello closed :)
2. 手动创建设备文件
2.1 我参考其它博客,建立了hello的ko驱动模块。你们可以从我的网址获取。
https://github.com/AppleCai/bb-black-linux/tree/master/mysrc/testled
2.2 编译Ko方法:
注意Makefile中的kernel dir是否正确。并且与你的开发板kernel版本一致。
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- modules
2.3 编译app文件方法
arm-linux-gnueabihf-gcc testled.c
2.4 加载leds模块
insmod leds.ko
加载成功后通过dmesg也可以看到信息。
通过/dev找不到设备文件。因为char_dev_create和char_reg_setup_cdev函数没有添加。所以需要手动添加node。
mknod /dev/leds c 241 0
2.5 运行test文件,调用驱动
./testled
结果板子上右上角从右到左第二个(user1)的灯,每隔1s亮,隔1s灭。
3. 过程中遇到的问题
1) zImage没有编译前,无法编译手动加载的module,报错generate/autoxxx.h不存在。
主要是编译zImage的时候会自动生成这些头文件,所以至少要编译过zImage才能编译自制模块。
2) leds驱动文件中ioctl这里居然报错。其实是警告也当错误处理。
查看了为什么有警告,include/linux/fs.h中函数定义的原型是long xxxx(xxxx),结果函数定义的是int xxxx(xxxx)。修改后即可编译通过产生ko文件。