嵌入式系统设计李秀娟第剖析.pptx
文本预览下载声明
第8章 设备驱动程序开发 ;本章内容;目的和要求;8.1 设备驱动概述;8.1.1 驱动程序和应用程序的区别;应用程序与驱动程序的关系图 ; Linux的设备管理 ;字符设备 ;;块设备 ;;网络设备驱动 ;网络驱动的体系结构 ;8.2 设备驱动程序的开发过程;Linux为所有的设备文件都提供了统一的操作函数接口,具体操作方法是使用数据结构struct file_operations。 ; 在嵌入式系统的开发中,我们一般仅仅实现其中几个接口函数:read、write、ioctl、open、release,就可以完成应用系统需要的功能。
open接口
Open 接口提供给驱动程序初始化设备的能力,从而为以后的设备操作做好准备。
release接口
与 open函数相反 ;
read 和write 接口
read 函数完成将数据从内核拷贝到应用程序空间,write函数则相反,将数据从应用程序空间拷贝到内核。 ;ioctl 接口
ioctl 接口主要用于对设备进行读写之外的其他控制。
;1、LED驱动需要的头文件
# include linux/config.h //配置头文件
# include linux/kernel.h //内核头文件
# include linux/init.h //用户定义模块初始函数需引用的头文件
# include linux/module.h //模块加载的头文件
# include linux/delay.h //延时头文件
# include linux/major.h
# include asm/hardware.h //用户的硬件配置文件
# include linux/io.h;2、LED驱动需要的宏定义
# define GPIO_LED_MAJOR 220 //定义主设备号
//声明4个LED灯的I/O端口; GPFDAT 是端口F的数据寄存器
# define LED1_ON()(GPFDAT = ~0x10) //GPF4输出0
# define LED2_ON()(GPFDAT = ~0x20) //GPF5输出0
# define LED3_ON()(GPFDAT = ~0x40) //GPF6输出0
# define LED4_ON()(GPFDAT = ~0x80) //GPF7输出0
# define LED1_OFF()(GPFDAT | = 0x10) //GPF4输出1
# define LED2_OFF()(GPFDAT | = 0x20) //GPF5输出1
# define LED3_OFF()(GPFDAT | = 0x40) //GPF6输出1
# define LED4_OFF()(GPFDAT | = 0x80) //GPF7输出1
//定义LED灯的状态
# define LED_ON 0 //低电平点亮LED
# define LED_OFF 1 //高电平熄灭LED;3、file_operations 结构体的设计
struct file_operations GPIO_LED_ctl_ops={
open: GPIO_LED_open,
read: GPIO_LED_read,
write: GPIO_LED_write,
ioctl: GPIO_LED_ioctl,
release: GPIO_LED_release,
} ;;4、LED驱动程序的读写函数实现
在本例中,LED的读写操作不做任何操作,可以省略。本例仅给出了读写操作函数的框架。
//-----------------READ-------------------
ssize_t GPIO_LED_read(struct file * file ,char * buf , size_t count, loff_t * f_ops)
{
return count;
}
//------------------WRITE------------------
ssize_t GPIO_LED_write(struct file * file ,const char * buf , size_t count, loff_t * f_ops)
{
return count;
};ssize_t GPIO_LED_ioctl(struct inode
显示全部