头文件:
//+++++add by Jay for proc sysfs debug #define DEBUG_ENABLE 1 #ifdef DEBUG_ENABLE #include <linux/string.h> #include <linux/vmalloc.h> #include <asm/uaccess.h> #include <linux/proc_fs.h> #include <asm/io.h> #define MAX_CHAR_LENGTH PAGE_SIZE struct proc_dir_entry *procfile; static char proc_cmd[20], proc_reg[10], proc_data[10]; static char *globalmem; static struct i2c_client *proc_client; #endif
static int __init mma8451_init(void) { /* register driver */ int res; res = i2c_add_driver(&mma8451_driver); if (res < 0) { printk(KERN_INFO "add mma8451 i2c driver failed\n"); return -ENODEV; } //++++++setup our proc debug sysfs #ifdef DEBUG_ENABLE globalmem = (char *)vmalloc(MAX_CHAR_LENGTH); if(!globalmem) printk(KERN_INFO "allocate memory error!\n"); memset(globalmem, 0, MAX_CHAR_LENGTH); procfile = create_proc_entry("mma8451",0401,NULL); if(procfile == NULL) { res = -ENOMEM; vfree(globalmem); printk(KERN_INFO "create procfile error!\n"); } else { procfile->read_proc = mma_proc_read; procfile->write_proc = mma_proc_write; } #endif return res; }
#ifdef DEBUG_ENABLE ssize_t mma_proc_write( struct file *filp, const char __user *buff, unsigned long len, void *data ) { int reg_index = 0; int reg_data = 0; int reg, result; printk(KERN_INFO "--MMA_PROC---%s----\n",__func__); if(copy_from_user(globalmem, buff, len)) return -EFAULT; globalmem[len-1] = '\0'; sscanf(globalmem, "%s%s%s", proc_cmd, proc_reg, proc_data); if(strcmp(proc_cmd, "write") == 0) { reg_index = simple_strtol(proc_reg, NULL, 16); reg_data = simple_strtol(proc_data, NULL, 16); reg = i2c_smbus_read_byte_data(proc_client, MMA8451_CTRL_REG1); reg &= ~0x01; result = i2c_smbus_write_byte_data(proc_client, MMA8451_CTRL_REG1, reg); if (result < 0) goto out; printk(KERN_INFO "index: %x, data:%x \n",reg_index,reg_data); result = i2c_smbus_write_byte_data(proc_client, reg_index, reg_data); if (result < 0) goto out; reg |= 0x01; result = i2c_smbus_write_byte_data(proc_client, MMA8451_CTRL_REG1, reg); if (result < 0) goto out; } return len; out: dev_err(&proc_client->dev, "error when init mma8451:(%d)", result); return len; }
int mma_proc_read( char *page, char **start, off_t off, int count, int *eof, void *data ) { int len = 0; int reg_index, reg_data; printk(KERN_INFO "--MMA_PROC---%s----\n",__func__); if(off > 0) { *eof = 1; return 0; } // if(strcmp(proc_cmd, "dump") == 0) { // reg_index = simple_strtol(proc_reg, NULL, 16); // printk("reg1: %x\n",i2c_smbus_read_byte_data(client, MMA8451_CTRL_REG1)); // printk("reg2: %x\n",i2c_smbus_read_byte_data(client, MMA8451_CTRL_REG2)); // printk("reg3: %x\n",i2c_smbus_read_byte_data(client, MMA8451_CTRL_REG3)); // printk("reg4: %x\n",i2c_smbus_read_byte_data(client, MMA8451_CTRL_REG4)); // printk("reg5: %x\n",i2c_smbus_read_byte_data(client, MMA8451_CTRL_REG5)); len = sprintf(page, "==========MMA REG==========\n"); reg_data = i2c_smbus_read_byte_data(proc_client, MMA8451_CTRL_REG1); len += sprintf(page + len, "REG1: 0x%x\n", reg_data); reg_data = i2c_smbus_read_byte_data(proc_client, MMA8451_CTRL_REG2); len += sprintf(page + len, "REG2: 0x%x\n", reg_data); reg_data = i2c_smbus_read_byte_data(proc_client, MMA8451_CTRL_REG3); len += sprintf(page + len, "REG3: 0x%x\n", reg_data); reg_data = i2c_smbus_read_byte_data(proc_client, MMA8451_CTRL_REG4); len += sprintf(page + len, "REG4: 0x%x\n", reg_data); reg_data = i2c_smbus_read_byte_data(proc_client, MMA8451_CTRL_REG5); len += sprintf(page + len, "REG5: 0x%x\n", reg_data); reg_data = i2c_smbus_read_byte_data(proc_client, 0x0c); len += sprintf(page + len, "INT RESOURCE: 0x%x\n", reg_data); } return len; } #endif
在mma8451,加速度传感器芯片上测试通过,这是一个通用的方法,在调试芯片寄存器的时候比较有用。