I2C驱动程序框架probe道路

时间:2023-03-09 05:19:48
I2C驱动程序框架probe道路

基于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匹配的keyword
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;
}

2014--12--17

征途開始

兴许补充