Chinaunix首页 | 论坛 | 博客
  • 博客访问: 178858
  • 博文数量: 27
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 618
  • 用 户 组: 普通用户
  • 注册时间: 2013-11-15 09:12
文章分类
文章存档

2014年(17)

2013年(10)

我的朋友

分类: C/C++

2014-03-13 17:36:02

写程序熟悉 练练手,利用PMIC提供debug接口,dump出来PMIC的寄存器的值

点击(此处)折叠或打开

  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #include<string.h>

  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <unistd.h>

  8. #define SUCCESSFULL 1
  9. #define READ_ERROR -1
  10. #define ARGS_ERROR -2
  11. #define PAGE_SIZE 4

  12. #define PMIC_START_ADDR 0x00
  13. #define PMIC_END_ADDR 0xFF

  14. #define PMIC_DEBUG_ADDR "/sys/kernel/pmic_debug/pmic_debug/addr"
  15. #define PMIC_DEBUG_OPS "/sys/kernel/pmic_debug/pmic_debug/ops"
  16. #define PMIC_DEBUG_DATA "/sys/kernel/pmic_debug/pmic_debug/data"

  17. typedef struct {
  18.         char *addr_path;
  19.         char *ops_path;
  20.         char *data_path;
  21.         char *addr;
  22.         char *data;
  23.         char *ops;
  24. } cmd_class;
  25. static int _read(cmd_class cmd)
  26. {
  27.         int ret = 0;
  28.         int addr_fd = -1;
  29.         int data_fd = -1;
  30.         int ops_fd = -1;
  31.         
  32.         addr_fd = open(cmd.addr_path, O_WRONLY);
  33.         if (addr_fd < 0)
  34.                 return -1;
  35.         ret = write(addr_fd, cmd.addr, sizeof(cmd.addr));
  36.         if (ret < 0) {
  37.                 close(addr_fd);
  38.                 return -1;
  39.         }
  40.         close(addr_fd);
  41.         
  42.         ops_fd = open(cmd.ops_path, O_WRONLY);
  43.         if (addr_fd < 0)
  44.                 return -1;
  45.         ret = write(ops_fd, cmd.ops, sizeof(cmd.addr));
  46.         if (ret < 0) {
  47.                 close(ops_fd);
  48.                 return -1;
  49.         }
  50.         close(ops_fd);
  51.         
  52.         data_fd = open(cmd.data_path, O_RDONLY);
  53.         if (data_fd < 0)
  54.                 return -1;

  55.         do {
  56.                 char *buff = malloc(4);
  57.                 ret = read(data_fd, buff, sizeof(PAGE_SIZE));
  58.                 strcpy(cmd.data, buff);
  59.                 cmd.data = cmd.data + ret;
  60.         } while (ret != 0);

  61.         close(data_fd);
  62.         
  63.         return ret;
  64. }

  65. static int _dump()
  66. {
  67.         int i = PMIC_START_ADDR, ret = SUCCESSFULL;
  68.         
  69.         printf("dump PMIC register:\n");
  70.         for (; i <= PMIC_END_ADDR; ++i) {
  71.                 cmd_class cmd;
  72.                 
  73.                 cmd.addr_path = PMIC_DEBUG_ADDR;
  74.                 cmd.addr = malloc(6);
  75.                 sprintf(cmd.addr, "0x%02x", i);
  76.                 
  77.                 cmd.ops_path = PMIC_DEBUG_OPS;
  78.                 cmd.ops = malloc(5);
  79.                 cmd.ops = "read";
  80.                 
  81.                 cmd.data_path = PMIC_DEBUG_DATA;
  82.                 cmd.data = malloc(13);
  83.         
  84.                 if (_read(cmd) == READ_ERROR) {
  85.                         printf("_read():%d ERROR: - dump addr = %s faile\n", READ_ERROR, cmd.addr);
  86.                         ret = READ_ERROR;
  87.                         return ret;
  88.                 }
  89.                 printf("\taddr = %s %s\n", cmd.addr, cmd.data);
  90.         }
  91.         return ret;
  92. }
  93. int main(char** argv, int argc)
  94. {
  95.         int ret = READ_ERROR;
  96.         
  97.         ret = _dump();
  98.                 
  99.         return ret;
  100. }
这里边碰见一个问题,在read data的时候开始总是只能读到4个字节,不知道为什么,后来看了看driver,发现driver这样写的
按照4字节往接口发送,结果read函数只能读到4个字节(然后有一个结束符?),最后只能通过while实现了。吐口水cat函数是不是也是while循环呢,估计没错

点击(此处)折叠或打开

  1. case PMIC_DBG_MASK:
  2.                 for (i = 0; i < valid; i++) {
  3.                         buf_size = PAGE_SIZE - ret;
  4.                         ret += snprintf(buf + ret, buf_size,
  5.                                         pmic_msg_format[type],
  6.                                         i, array[i]);
  7.                 }
  8.                 break;


阅读(2095) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~