linux驱动调试技巧:灌寄存器---------以mma7660为例
参考资料:http://ebetteric.com/page.aspx?id=12&show=1本文可以使用linux的cat echo命令直接设置mma7660的寄存器的值部分driver代码如下:static DEVICE_ATTR(mma7660_regs, S
参考资料:http://ebetteric.com/page.aspx?id=12&show=1
本文可以使用linux的cat echo命令直接设置mma7660的寄存器的值
部分driver代码如下:
static DEVICE_ATTR(mma7660_regs, S_IRUGO | S_IWUGO, show_7660regs, write_7660regs);
//mma7660_regs对应着一个文件名字,show_7660regs, write_7660regs,是函数名
static struct attribute *mma7660_sysfs_entries[] = {
&dev_attr_mma7660_regs.attr,
NULL
};
static struct attribute_group mma660_attr_group = {
.attrs = mma7455l_sysfs_entries,
};
在probe函数中使用如下语句来创建文件系统
err = sysfs_create_group(&client->dev.kobj, &mma7660_attr_group);
static ssize_t show_7660regs(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct mma7455l_info *mma = dev_get_drvdata(dev);
int i;
char buffer[10];
if (10 != (i = i2c_master_recv(mma->client, buffer, 10)))
{//读出mma7660十个寄存器的值
mmadbg("i2c i/o error: rc == %d (should be 10)\n", i);
return;
}
dbg("read : addr:value :\n");
for(i=0;i<=10;i++)
{if(i==5) printk("\n");printk(" %d=%x ,,", i, buffer[i] ); }
dbg("\n");
}
static ssize_t write_7660regs(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
struct mma7455l_info *mma = dev_get_drvdata(dev);
dbg("in write_mode: %s\n",buf);
int addr=0,value=0;
int i=0,flag=0;
char buffer[10];
for(i=0;i<count;i++)
printk("count=%d,\n",buf[i]);
for(i=0;buf[i]!=10;i++)
{
if(buf[i]==' ')
{flag=1;continue;}
if(flag==0)
addr=addr*10+(buf[i]-'0');
else
value=value*10+(buf[i]-'0');
}
count= reg_write(mma->client, addr, value);
return count;
}
insmod mma7660.ko后,在相应的目录中就存在一个名字是mma7660_regs的文件
/sys/class/i2c-adapter/i2c-0/0-004c # ls
bus gSelect mma7660_regs power
calibration measure modalias subsystem
driver measure_10 name uevent
使用cat 和echo操作这个文件,命令截个图如下:
如下示例使用echo "7 34" > mma7660_regs(此时会执行write_7660regs)用以设置reg 7的值为34,也是0x22
使用cat mma7660_regs(执行show_7660regs)后打出7=22
更多推荐
所有评论(0)