背景:我们平常在调试驱动的时候,经常需要查看寄存器的配置值,或者是打印变量和设置变量,通过echo和cat可以非常方便的进行控制和查看
实现方法:
#define XXX_DEBUG
#ifdef XXX_DEBUG
static ssize_t show_xxx_debug(struct device *dev,
struct device_attribute *attr, char *buf)
{
ssize_t len = 0;
u8 i = 0;
u8 reg_val = 0;
for(i=0; i<=XXX_REG_NUM; i++) {
xxx_read_byte(i, ®_val, 1);
len += snprintf(buf+len, PAGE_SIZE-len, "reg:0x%02x=0x%02x\n", i, reg_val);
}
return len;
}
static ssize_t store_xxx_debug(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
unsigned int databuf[2] = {0};
if(2 == sscanf(buf, "%x %x", &databuf[0], &databuf[1])) {
xxx_reg_config_interface(databuf[0], databuf[1]);
}
return size;
}
static DEVICE_ATTR(debug, 0664, show_xxx_debug,
store_xxx_debug);
#endif
#ifdef XXX_DEBUG
device_create_file(&(client->dev), &dev_attr_xxx_debug);
#endif
将 device_create_file 放在驱动的probe函数里面即可,同时如果不是i2c的设备,相应的调整即可。