写程序熟悉 练练手,利用PMIC提供debug接口,dump出来PMIC的寄存器的值
-
#include<stdlib.h>
-
#include<stdio.h>
-
#include<string.h>
-
-
#include <sys/types.h>
-
#include <sys/stat.h>
-
#include <fcntl.h>
-
#include <unistd.h>
-
-
#define SUCCESSFULL 1
-
#define READ_ERROR -1
-
#define ARGS_ERROR -2
-
#define PAGE_SIZE 4
-
-
#define PMIC_START_ADDR 0x00
-
#define PMIC_END_ADDR 0xFF
-
-
#define PMIC_DEBUG_ADDR "/sys/kernel/pmic_debug/pmic_debug/addr"
-
#define PMIC_DEBUG_OPS "/sys/kernel/pmic_debug/pmic_debug/ops"
-
#define PMIC_DEBUG_DATA "/sys/kernel/pmic_debug/pmic_debug/data"
-
-
typedef struct {
-
char *addr_path;
-
char *ops_path;
-
char *data_path;
-
char *addr;
-
char *data;
-
char *ops;
-
} cmd_class;
-
static int _read(cmd_class cmd)
-
{
-
int ret = 0;
-
int addr_fd = -1;
-
int data_fd = -1;
-
int ops_fd = -1;
-
-
addr_fd = open(cmd.addr_path, O_WRONLY);
-
if (addr_fd < 0)
-
return -1;
-
ret = write(addr_fd, cmd.addr, sizeof(cmd.addr));
-
if (ret < 0) {
-
close(addr_fd);
-
return -1;
-
}
-
close(addr_fd);
-
-
ops_fd = open(cmd.ops_path, O_WRONLY);
-
if (addr_fd < 0)
-
return -1;
-
ret = write(ops_fd, cmd.ops, sizeof(cmd.addr));
-
if (ret < 0) {
-
close(ops_fd);
-
return -1;
-
}
-
close(ops_fd);
-
-
data_fd = open(cmd.data_path, O_RDONLY);
-
if (data_fd < 0)
-
return -1;
-
-
do {
-
char *buff = malloc(4);
-
ret = read(data_fd, buff, sizeof(PAGE_SIZE));
-
strcpy(cmd.data, buff);
-
cmd.data = cmd.data + ret;
-
} while (ret != 0);
-
-
close(data_fd);
-
-
return ret;
-
}
-
-
static int _dump()
-
{
-
int i = PMIC_START_ADDR, ret = SUCCESSFULL;
-
-
printf("dump PMIC register:\n");
-
for (; i <= PMIC_END_ADDR; ++i) {
-
cmd_class cmd;
-
-
cmd.addr_path = PMIC_DEBUG_ADDR;
-
cmd.addr = malloc(6);
-
sprintf(cmd.addr, "0x%02x", i);
-
-
cmd.ops_path = PMIC_DEBUG_OPS;
-
cmd.ops = malloc(5);
-
cmd.ops = "read";
-
-
cmd.data_path = PMIC_DEBUG_DATA;
-
cmd.data = malloc(13);
-
-
if (_read(cmd) == READ_ERROR) {
-
printf("_read():%d ERROR: - dump addr = %s faile\n", READ_ERROR, cmd.addr);
-
ret = READ_ERROR;
-
return ret;
-
}
-
printf("\taddr = %s %s\n", cmd.addr, cmd.data);
-
}
-
return ret;
-
}
-
int main(char** argv, int argc)
-
{
-
int ret = READ_ERROR;
-
-
ret = _dump();
-
-
return ret;
-
}
这里边碰见一个问题,在read data的时候开始总是只能读到4个字节,不知道为什么,后来看了看driver,发现driver这样写的
按照4字节往接口发送,结果read函数只能读到4个字节(然后有一个结束符?),最后只能通过while实现了。吐口水cat函数是不是也是while循环呢,估计没错
-
case PMIC_DBG_MASK:
-
for (i = 0; i < valid; i++) {
-
buf_size = PAGE_SIZE - ret;
-
ret += snprintf(buf + ret, buf_size,
-
pmic_msg_format[type],
-
i, array[i]);
-
}
-
break;
阅读(2095) | 评论(0) | 转发(0) |