Linux下基于I2C协议的RTC驱动开发
2010-05-13孟令公,朱宏,杨忠孝,杨裕辉
孟令公,朱 宏,杨忠孝,杨裕辉
摘 要:在嵌入式中,Linux渐渐成为一种流行操作系统,Linux驱动开发也成为嵌入式开发中的必备环节。介绍Linux环境下基于I2C协议的RTC驱动程序开发与实现。首先研究了Linux环境下字符设备驱动程序框架,然后介绍I2C协议,在此基础上开发基于I2C协议的RTC字符设备驱动程序。对于驱动程序,这里详细介绍其整体架构和各模块实现细节。最终成功实现了基于I2C协议的RTC驱动程序,并移植到Linux操作系统中。
关键词:嵌入式系统;Linux;驱动程序;I2C
中图分类号:TP316.81文献标识码:B
文章编号:1004-373X(2009)20-032-04
Development of RTC Driver Based on I2C under Linux
MENG Linggong,ZHU Hong,YANG Zhongxiao,YANG Yuhui
(University of Electronic Science and Technology of China,Chengdu,610054,China)
Abstract:In embedded field,Linux OS has been more and more popular,the development of Linux driver is becoming a constituent job.Reaserch and development of RTC driver based on I2C under Linux OS are introduced.First,research on the character device driver framework is given.Then,I2C protocol is introduced,RTC driver based on I2C under Linux OS is developed,the scheme and realization are proposed.Finally,RTC driver based on I2C is realized and transplanted into Linux.
Keywords:embedded system;Linux;driver;I2C
0 引 言
近年来嵌入式系统的研究与开发渐渐成为热点,在嵌入式系统中,Linux操作系统以其开源、稳定、可移植等种种优点,渐渐成为一种流行的操作系统。Linux下各种驱动程序的开发经常是软件开发中必不可缺的环节,Linux对其驱动程序提供了很好的支持框架。I2C总线是一种由Philips公司开发的两线式串行总线,用于连接微控制器及其外围设备。I2C总线产生于在20世纪80年代,最主要的优点是其简单性和有效性。这里介绍Linux字符设备驱动以及I2C总线协议,并在此基础上开发基于I2C总线的RTC驱动程序。
1 Linux字符设备驱动框架
在Linux内核中每个字符驱动程序都是基于以下框架进行设计的[1,2]:
struct file_operations {
ssize_t (*read) (struct file *,char __user *,size_t,loff_t *);
ssize_t (*write) (struct file *,const char __user *,size_t,loff_t *);
int (*ioctl) (struct inode *,struct file *,unsigned int,unsigned long);
int (*open) (struct inode *,struct file *);
int (*release) (struct inode *,struct file *);
… };
其中每个驱动程序必须实现的函数主要有:
(1) open()
在Linux或应用程序使用设备前都会调用驱动程序的这个方法。在驱动程序中open()方法一般用于对驱动程序支持的设备进行初始化。
(2) release()
当Linux或应用程序不再使用设备时会调用驱动程序的这个方法。……
