IIC驱动3.4.2

I2C硬件原理

I2C基于总线设备驱动模型.
在这里插入图片描述
1 左边注册一个设备:i2c_client
2.右边注册一个驱动:i2c_driver
3.比较它们的名字,如果相同则调用probe函数
4.probe函数里,register_chrdev…
在这里插入图片描述
一个I2C总线上可能有多个设备,所以有多个设备驱动程序(明白传输数据的含义)。它们都有共性,都要发出S,ACK等信号,驱动里面会用到一些发送信号和地址等的函数,由核心层提供。核心层提供统一的I2C操作函数,适配器是硬件相关的操作(可能包含多个适配器/总线/I2C寄存器)。

i2c_client的4种构建方法

一.定义一个i2c_board_info结构体, 里面有:名字, 设备地址。
然后注册i2c_register_board_info(busnum, …) (把它们放入__i2c_board_list链表)
list_add_tail(&devinfo->list, &__i2c_board_list);
链表何时使用:
i2c_register_adapter > i2c_scan_static_board_info > i2c_new_device
使用限制:必须在 i2c_register_adapter 之前 i2c_register_board_info
所以:不适合我们动态加载insmod

二.显式实例化设备 (推荐)
直接i2c_new_device, i2c_new_probed_device
1 i2c_new_device(i2c_adap,i2c_board_info) : 认为设备肯定存在(不管是否真存在,设备地址不同)
2 i2c_new_probed_device :对于"已经识别出来的设备"(probed_device),才会创建(“new”)
static const unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
i2c_new_probed_device
probe(adap, addr_list[i]) /* 确定设备是否真实存在 */
info->addr = addr_list[i];
i2c_new_device(adap, info);

三.从用户空间创建设备
创建设备
echo at24c08 0x50 > /sys/class/i2c-adapter/i2c-0/new_device
导致i2c_new_device被调用
删除设备
echo 0x50 > /sys/class/i2c-adapter/i2c-0/delete_devic
导致i2c_unregister_device

四.前面的3种方法都要事先确定适配器(I2C总线/I2C控制器)
如果我事先并不知道这个I2C设备在哪个适配器上,怎么办?去class表示的所有的适配器上查找。
有些I2C设备的地址是一样,怎么继续分配它是哪一款?用detect函数

static struct i2c_driver at24cxx_driver = {
	.class  = I2C_CLASS_HWMON, /* 表示去哪一类适配器上找设备 */
	.driver	= {
		.name	= "100ask",
		.owner	= THIS_MODULE,
	},
	.probe		= at24cxx_probe,
	.remove		= __devexit_p(at24cxx_remove),
	.id_table	    = at24cxx_id_table,
	.detect      = at24cxx_detect,   /* 用这个函数来检测设备确实存在 */
	.address_list	= addr_list,    /* 这些设备的地址 */
};

去"class表示的这一类"I2C适配器,用"detect函数"来确定能否找到"address_list里的设备",如果能找到就调用i2c_new_device来注册i2c_client, 这会和i2c_driver的id_table比较,如果匹配,调用probe。

i2c_add_driver
	i2c_register_driver
		a. at24cxx_driver放入i2c_bus_type的drv链表
		   并且从dev链表里取出能匹配的i2c_client并调用probe
		driver_register
		b. 对于每一个适配器,调用__process_new_driver
		   对于每一个适配器,调用它的函数确定address_list里的设备是否存在
		   如果存在,再调用detect进一步确定、设置,然后i2c_new_device
		/* Walk the adapters that are already present */
		i2c_for_each_dev(driver, __process_new_driver);
			__process_new_driver
				i2c_do_add_adapter
					/* Detect supported devices on that bus, and instantiate them */
					i2c_detect(adap, driver);
						for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
							err = i2c_detect_address(temp_client, driver);
								/* 判断这个设备是否存在:简单的发出S信号确定有ACK */
								if (!i2c_default_probe(adapter, addr))
									return 0;
								memset(&info, 0, sizeof(struct i2c_board_info));
								info.addr = addr;	
								// 设置info.type
								err = driver->detect(temp_client, &info);
								i2c_new_device

程序

drv函数
分配设置注册i2c_driver,id_table名字匹配成功调用probe函数注册字符设备,字符设备的读写函数调用核心层提供的同一的i2c操作函数
i2c_smbus_read_byte_data(client, addr)
i2c_smbus_write_byte_data(client, addr, data)

static int major;
static struct class *class;
static struct i2c_client *at24cxx_client;

/* 传入: buf[0] : addr	输出: buf[0] : data */
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);
	copy_to_user(buf, &data, 1);
	return 1;
}

/* buf[0] : addr	buf[1] : data  */
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];

	printk("addr = 0x%02x, data = 0x%02x\n", addr, data);

	if (!i2c_smbus_write_byte_data(at24cxx_client, addr, data))
		return 2;
	else
		return -EIO;	
}

static struct file_operations at24cxx_fops = {
	.owner = THIS_MODULE,
	.read  = at24cxx_read,
	.write = at24cxx_write,
};

static int __devinit at24cxx_probe(struct i2c_client *client,const struct i2c_device_id *id){
	at24cxx_client = client;
	major = register_chrdev(0, "at24cxx", &at24cxx_fops);
	class = class_create(THIS_MODULE, "at24cxx");
	device_create(class, NULL, MKDEV(major, 0), NULL, "at24cxx"); /* /dev/at24cxx */	
	return 0;
}

static int __devexit at24cxx_remove(struct i2c_client *client){
	device_destroy(class, MKDEV(major, 0));
	class_destroy(class);
	unregister_chrdev(major, "at24cxx");		
	return 0;
}
static const struct i2c_device_id at24cxx_id_table[] = {	//id_table,名字用于匹配
	{ "at24c08", 0 },	
	{}
};

/* 1. 分配/设置i2c_driver */
static struct i2c_driver at24cxx_driver = {
	.driver	= {
		.name	= "100ask",
		.owner	= THIS_MODULE,
	},
	.probe		= at24cxx_probe,
	.remove		= __devexit_p(at24cxx_remove),
	.id_table	= at24cxx_id_table,	//id_table,名字用于匹配
};

static int at24cxx_drv_init(void){	
	i2c_add_driver(&at24cxx_driver);	/* 2. 注册i2c_driver */
	return 0;
}
static void at24cxx_drv_exit(void){
	i2c_del_driver(&at24cxx_driver);
}

module_init(at24cxx_drv_init);
module_exit(at24cxx_drv_exit);
MODULE_LICENSE("GPL");

dev函数

static struct i2c_board_info at24cxx_info = {	//I2C单板信息
	I2C_BOARD_INFO("at24c08", 0x50),	//名字,设备地址 :用于匹配
};

static struct i2c_client *at24cxx_client;

static int at24cxx_dev_init(void){
	struct i2c_adapter *i2c_adap;
	i2c_adap = i2c_get_adapter(0);	//获取适配器(总线/i2c控制器)
	//在该总线下创建设备,以后用这个adapter里函数发信号
	at24cxx_client = i2c_new_device(i2c_adap, &at24cxx_info);		
	i2c_put_adapter(i2c_adap);
	return 0;
}

static void at24cxx_dev_exit(void){
	i2c_unregister_device(at24cxx_client);
}

module_init(at24cxx_dev_init);
module_exit(at24cxx_dev_exit);
MODULE_LICENSE("GPL");

测试函数

/* 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 (strcmp(argv[1], "r") == 0){
		buf[0] = strtoul(argv[2], NULL, 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值