基于Linux的I2C驱动,采用probe方式。按照如下这个框架可以写出任何支持I2C总线的器件的Linux驱动。
I2C器件连接至cpu的特定的i2c的接口,也就是挂载在cpu的i2c适配器上,i2c器件要和cpu进行信息交换必须要通过cpu操作适配器来交互。cpu上有1个或多个适配器,每个适配器上可以挂载256个设备地址不一样的i2c器件,通过i2c驱动就可以让cpu和适配器上的多个不一样的i2c器件通信而不会产生冲突。
驱动包括两个文件,dev.c和drive.c,其中dev.c是构建I2C设备,即创建I2C_Client结构体,而driver.c是在probe中获取dev.c中构建的i2c_client,然后构建fileoperation;具体步骤结合代码如下。
1、在dev的入口函数中构建I2C-CLient
static unsigned short addr_list[] = {
0x60, 0x50, I2C_CLIENT_END //这个数组包含了设备地址,以I2C_CLIENT_END为数组结尾</span>
};
static struct i2c_client *at24cxx_client; //创建I2C_Client结构体
static int at24cxx_dev_init(void)
{
/*构建I2C_Client*/
struct i2c_adapter *adapter;
struct i2c_board_info info;
adapter=i2c_get_adapter(0); //获取适配器,因为有些cpu有多个I2C适配器,参数0为适配器编号
memset(&info, 0, sizeof(struct i2c_board_info));
strlcpy(info.type, "at24cxx", I2C_NAME_SIZE); //这个是I2C_NAME,是和i2c_driver匹配的关键字
at24cxx_client=i2c_new_probed_device(adapter, &info, addr_list);//如果数组中地址的设备存在则成功返回i2c_client结构体//这个函数最终会调用device_create创建设备 i2c_put_adapter(adapter); if(at24cxx_client) return 0; else return -ENODEV; }
2、在driver.c的入口函数中注册I2C_driver结构体
static const struct i2c_device_id = {
{ "at24cxx", 0 },
{ }
};
static struct i2c_driver at24cxx_driver = {
.driver = {
.name = "100ask",
.owner = THIS_MODULE,
},
.probe = at24cxx_probe,
.remove = __devexit_p(at24cxx_remove),
.id_table = at24cxx_ids,//id_table中name是和dev创建i2c_client的name匹配,若匹配则会调用probe设备方法
};
static int at24cxx_drv_init(void)
{
/*×¢²ái2c_driver*/
return i2c_add_driver(&at24cxx_driver);
}3、在probe设备方法中构建file_operation注册字符设备
static struct file_operations at24cxx_fops =
{
.owner = THIS_MODULE,
.read = at24cxx_read,
.write = at24cxx_write,
};
static int at24cxx_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
printk("%s %s %d.\n",__FILE__,__FUNCTION__,__LINE__);
at24cxx_client = client;//将dev中构建的i2c_client结构体传递过来,因为这个结构体在read,write等设备方法中传递信息时需要用到
major = register_chrdev(0,"at24cxx",&at24cxx_fops);//注册字符设备
class = class_create(THIS_MODULE,"at24cxx");
device_create(class,NULL,MKDEV(major,0),NULL,"at24cxx");
return 0;
}4、构建write和read等设备方法中传递I2C消息
static ssize_t at24cxx_read(struct file *file, char __user *buf, size_t count, loff_t *off)
{
unsigned char addr,data;
copy_from_user(&addr,buf,1);
data = i2c_smbus_read_byte_data(at24cxx_client,addr);//这里用i2c_smbus_read_byte_data函数来和实际的I2C设备进行信息交互</span>
//这里具体用哪个函数来传递消息需要结合具体的器件的时序,不同的时序有不同的传递函数,查看Linux源码中的说明文档有关于传递函数的说明。
copy_to_user(buf,&data,1);
return 0;
}
static ssize_t at24cxx_write(struct file *file, const char __user *buf, size_t count , loff_t *off)
{
unsigned char ker_buf[2];
unsigned char addr,data;
copy_from_user(ker_buf,buf,2);
addr = ker_buf[0];
data = ker_buf[1];
if(!i2c_smbus_write_byte_data(at24cxx_client,addr,data))
return 2;
else
return -EIO;
}
如果i2c总线中挂载了实际的i2c设备,而且设备地址在以上的addr_list中,则无论是先加载dev.ko还是先加载driver.ko都会成功的执行probe函数,然后创建字符设备,在应用程序中open设备,并调用read和write则会相应的调用driver中的read和write设备方法。
测试应用程序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
/* i2c_test r addr
* i2c_test w addr val
*/
void print_usage(char *file)
{
printf("%s r addr\n", file);
printf("%s w addr val\n", file);
}
int main(int argc, char **argv)
{
int fd;
unsigned char buf[2];
if ((argc != 3) && (argc != 4))
{
print_usage(argv[0]);
return -1;
}
fd = open("/dev/at24cxx", O_RDWR);
if (fd < 0)
{
printf("can't open /dev/at24cxx\n");
return -1;
}
if (strcmp(argv[1], "r") == 0)
{
buf[0] = strtoul(argv[2], NULL, 0);
printf("before data: %c, %d, 0x%2x\n", buf[0], buf[0], buf[0]);
read(fd, buf, 1);
printf("data: %c, %d, 0x%2x\n", buf[0], buf[0], buf[0]);
}
else if ((strcmp(argv[1], "w") == 0) && (argc == 4))
{
buf[0] = strtoul(argv[2], NULL, 0);
buf[1] = strtoul(argv[3], NULL, 0);
if (write(fd, buf, 2) != 2)
printf("write err, addr = 0x%02x, data = 0x%02x\n", buf[0], buf[1]);
}
else
{
print_usage(argv[0]);
return -1;
}
return 0;
}
征途开始
后续补充
原文地址:http://blog.csdn.net/xiaojianjian2013/article/details/41987999