文档详情

嵌入式应用程序设计设备驱动.ppt

发布:2017-06-18约4.2千字共24页下载文档
文本预览下载声明
嵌入式应用程序设计 第八章 嵌入式Linux设备驱动编程 知识点回顾 TCP/IP模型 套接字 TCP 套接字编程 UDP 套接字编程 设备驱动的基本概念 Linux内核与驱动的关系 Linux内核模块结构 内核模块命令 1.设备驱动概念 是处理和操作硬件控制器的软件。驱动程序是内核的一部分,是操作系统内核与硬件设备的直接接口,驱动程序屏蔽了硬件的细节,完成以下功能: ? 对设备初始化和释放; ? 对设备进行管理,包括实时参数设置,以及提供对设备的操作接口; ? 读取应用程序传送给设备文件的数据或者回送应用程序请求的数据; ? 检测和处理设备出现的错误。 2.Linux系统的设备分类 字符设备通常指以字节为单位顺序读写的设备, 如并口设备、虚拟控制台等。 块设备通常指以块为单位随机读写的设备,如IDE硬盘、SCSI硬盘、光驱等。 网络设备通常是指通过网络能够与其他主机进行数据通信的设备,如网卡等。 举例:linux设备识别 在linux中任何设备都是文件。所有设备的设备文件节点都在/dev目录下。 #:ls –al /dev 3.设备驱动程序的特点 内核代码 内核接口 内核机制和服务 可装载 可设置 动态性 4.设备驱动程序与整个软硬件系统的关系 1.Linux的内核模块 Linux内核采用可加载内核模块化设计 (Loadable Kernel Module,LKM) 内核模块:是一些可以让操作系统内核在需要时载入和执行的代码。是一种目标对象文件,通常由一组函数和数据结构组成。 内核的一个模块可以以两种方式被编译和加载。 直接编译进Linux内核,随同Linux启动时加载; 编译成一个可加载和删除的模块。 8.1 设备驱动编程基础—内核模块编程 C语言程序 Linux内核模块 运行 用户空间 内核空间 入口 main() module_init()指定; 出口 main() module_exit()指定; 编译 gcc –c Makefile 连接 ld insmod 运行 直接运行 insmod 调试 gdb kdbug, kdb, kgdb等 8.1 设备驱动编程基础—内核模块编程 3.内核模块相关命令 insmod module.o Load the module 注意,只有超级用户才能使用这个命令 rmmod module Unload the module lsmod List all modules loaded into the kernel 这个命令和cat /proc/modules等价 modprobe [-r] module name – Load the module specified and modules it depends 举例:模块相关命令 lsmod insmod hello.o rmmod hello modprobe ? /lib/modules/linux版本号/modules.dep? /etc/modprobe.conf modeproble –r hello 最简单的内核模块例子 生成makefile文件 Makefile文件 obj-m := hello.o all: make -C /usr/src/linux-2.4.20/ M=$(shell pwd) modules clean: make -C /usr/src/linux-2.4.20/ M=$(shell pwd) modules clean 编译装载过程 $ ls hello.c? Makefile $ make make -C /usr/src/linux-2.4.20/ M=/wu/char_8 modules $ ls hello.c?? hello.mod.c? hello.mode.o hello.o??make.log? Makefile? modules.order $ sudo insmod hello.o $ dmesg | tail -1 Hello kernel… $ lsmod | grep hello hello??????? 1216? 0 $ sudo rmmod hello $ dmesg | tail -1 bye bye…. 不需要编写Makefile的方法 gcc
显示全部
相似文档