at24分析 at24_bin_read

/sys下创建eeprom节点

1
2
3
4
5
6
7
8
9
static int (struct i2c_client *client, const struct i2c_device_id *id)
{
sysfs_bin_attr_init(&at24->bin);
at24->bin.attr.name = "eeprom";
at24->bin.attr.mode = chip.flags & AT24_FLAG_IRUGO ? S_IRUGO : S_IRUSR;
at24->bin.read = at24_bin_read;

err = sysfs_create_bin_file(&client->dev.kobj, &at24->bin);
}

at24_bin_read

从off寄存器开始读取count个数据到buf中
主要是填充struct i2c_msg,然后调用i2c_transfer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
static ssize_t at24_bin_read(struct file *filp, struct kobject *kobj,
struct bin_attribute *attr,
char *buf, loff_t off, size_t count)
{
return at24_read(at24, buf, off, count);
while (count) {
status = at24_eeprom_read(at24, buf, off, count);

// 先写寄存器地址再读
msgbuf[i++] = offset;
msg[0].addr = client->addr;
msg[0].buf = msgbuf; // buf中存储的是寄存器地址
msg[0].len = i;

msg[1].addr = client->addr;
msg[1].flags = I2C_M_RD;
msg[1].buf = buf;
msg[1].len = count;

status = i2c_transfer(client->adapter, msg, 2);
}
}