访问eeprom设备的方法三(理解iic总线接口应用以及创建sysfs文件系统的bin文件访问接口(新的访问设备的文件接口))

该博客详细介绍了如何通过sysfs在Linux驱动中访问EEPROM设备,特别是使用I2C总线接口。驱动程序没有注册字符或杂项设备,而是注册了一个二进制属性文件。读写操作通过指定的at24_bin_write和at24_bin_read函数完成,这两个函数内部利用了mutex_lock进行同步,并通过at24_eeprom_write和at24_eeprom_read进行实际的I2C/SMBus通信。文章还指出,驱动支持AT24CXX系列,并且能够处理多个I2C地址。在读写过程中,msg数据包的构建因读写操作而异,主要是因为读写协议的不同需求。
摘要由CSDN通过智能技术生成

驱动并没有注册任何字符设备或者杂项设备,只是向sys文件系统注册了一个二进制属性文件。因此要访问设备,必须通过该文件的读写函数来。

读写函数在probe函数中指定为at24_bin_write和at24_bin_read

1:写函数

static ssize_t at24_bin_write(struct file *filp, struct kobject *kobj,
struct bin_attribute *attr,
char *buf, loff_t off, size_t count)
{
struct at24_data *at24;

/* 通过kobj获取device,再获取driver_data */
at24 = dev_get_drvdata(container_of(kobj, struct device, kobj));
return at24_write(at24, buf, off, count);
}


static ssize_t at24_write(struct at24_data *at24, const char *buf, loff_t off,
 size_t count)
{
ssize_t retval = 0;
if (unlikely(!count))
return count;
/*
* Write data to chip, protecting against concurrent updates
* from this host, but not from other I2C masters.
*/

 /* 访问设备前,加锁*/ 
mutex_lock(&at24->lock);
while (count) {
ssize_t status;
status = at24_eeprom_write(at24, buf, off, count);//写入数据
if (status <= 0) {
if (retval == 0)
retval = status;
break;
}
buf += status;//调整偏移量
off += status;
count -= status;
retval += status;
}
mutex_unlock(&at24->lock);
return retval;
}


static ssize_t at24_eeprom_write(struct at24_data *at24, const char *buf,
unsigned offset, size_t count)
{
struct i2c_client *client;
struct i2c_msg msg;
ssize_t status;
unsigned long timeout, write_time;
unsigned next_page;

/* Get corresponding I2C address and adjust offset */
client = at24_translate_offset(at24, &offset);//用于计算地址信息对于多i2c设备的地址

/* write_max is at most a page */
if (count > at24->write_max)//表明最大一次只能写一个page
count = at24->write_max;

/* Never roll over backwards, to the start of this page */

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值