第六章、第一个linux驱动程序:统计单词个数 读书笔记

第六章、第一个linux驱动程序:统计单词个数

一、编写Linux驱动程序的步骤

1.建立Linux驱动骨架(装载和卸载Linux驱动)

Module_init处理驱动初始化,module_exit处理驱动退出

2.注册和注销设备文件

Misc_register函数创建和misc_deregister移除设备文件

3.指定与驱动相关的信息

modinfo

4.指定回调函数

5.编写业务逻辑

6.编写makefile文件

7.编译Linux驱动程序

8.安装和卸载Linux驱动

二.Linux驱动:统计单词个数

1.准备工作

(1)建立存放Linux驱动程序的目录

Mkdir –p /root/drivers/ch06/word_count

Cd /root/drivers/ch06/word_count

建立驱动源代码文件

Echo ‘’>word_count.c

编写makefile

Echo ‘obj-m := word_count.o’> Makefile  (obj-m表示将Linux驱动作为模块编译,obj-y表示编译进Linux内核)

当Linux驱动依赖其它程序则需要这样编写makefile文件

Obj-m:=word_count.o

Word_count-y :=process.o data.o

2.编写Linux驱动程序的骨架(初始化和退出驱动)

Printk函数,该函数用于输出日志信息

测试编译Linux驱动源代码(不一定在Android设备上完成)

Make –C /usr/src/linux-headers-3.0.0-15-generic M=/root/driver/ch06/word_count

安装Linux驱动

Insmod word_ount.ko

查看word_count是否安装成功

Lsmod | grep word_count

卸载Linux驱动

Rmmod word-count

查看Linux驱动输出的日志信息

Dmessg |grep word_count | tail –n 2 或者cat /var/log/syslog | grep word_count | tail –n 2

3.开源协议、GPL协议、LGPL协议、BSD协议、Apachelicence2.0协议、MIT协议

4、注册和注销设备

Extern int misc_register(struct miscdevice * misc)

Extern int misc_deregister(struct miscdevice * misc)

还需要修改word_count.c文件中的word_count_init和word_count_exit函数

设备文件由主设备号和次设备号描述。主设备号统一设为10,是Linux系统中拥有共同特性的简单字符设备,称为misc设备。当成功注册了设备文件,misc_register函数返回非0的整数,失败返回0.

Insmod word_count.ko

Rmmod word_count

Ls –a /dev

Ls –l /dev

Cat /proc/devices(获取主设备及其主设备号)

5、指定回调函数 word_count_read 和word_count_write

最常用的交互方式就是读写设备文件。

6.实现

单词数使用int类型变量存储。Mem数组的长度为10000,而最后一个字符为“\0”,因此带统计的字符串最大长度为999

7.编译、安装、卸载Linux驱动程序

三、使用多种方式测试Linux驱动

1.使用Ubuntu Linux测试Linux驱动

2.在Android模拟器上通过原生(Native)C程序测试Linux驱动

3.使用Android NDK测试Linux驱动

4.使用Java代码直接操作设备文件来测试Linux驱动

5.使用S3C6410开发板侧试Linux驱动

6.将驱动编译经Linux内核进行测试

四、使用eclipse开发和测试Linux驱动程序

1.在eclipse中开发Linux驱动程序

建立C工程

建立C源代码文件链接

设置include路径

编译Linux驱动

2.在eclipse中测试Linux驱动

导入test-word_count.c

设置include路径

建立Target

Build工程

运行测试程序

posted @ 2016-06-03 12:14  a870542373  阅读(159)  评论(0编辑  收藏  举报