歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 在QT210 LDD的Android源碼樹中添加I2C讀寫工具(i2c-util)

在QT210 LDD的Android源碼樹中添加I2C讀寫工具(i2c-util)

日期:2017/3/1 10:36:08   编辑:Linux編程

通過/dev/i2c-n節點,用戶可以在userspace直接訪問板上的i2c外設寄存器,主要是透過I2C_RDWR這個IO控制命令將i2c_msg數組傳遞給kernel去執行。下面的代碼可以完成這個功能:


[cpp]
  1. #include <stdio.h>
  2. #include <linux/types.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <stdlib.h>
  6. #include <sys/types.h>
  7. #include <sys/ioctl.h>
  8. #include <errno.h>
  9. #include <assert.h>
  10. #include <string.h>
  11. #include <linux/i2c.h>
  12. /* This is the structure as used in the I2C_RDWR ioctl call */
  13. struct i2c_rdwr_ioctl_data {
  14. struct i2c_msg __user *msgs; /* pointers to i2c_msgs */
  15. __u32 nmsgs; /* number of i2c_msgs */
  16. };
  17. int i2c_read_reg(char *dev, unsigned char *buf, unsigned slave_address, unsigned reg_address, int len)
  18. {
  19. struct i2c_rdwr_ioctl_data work_queue;
  20. unsigned char w_val = reg_address;
  21. int ret;
  22. int fd = open(dev, O_RDWR);
  23. if (!fd) {
  24. printf("Error on opening the device file\n");
  25. return 0;
  26. }
  27. work_queue.nmsgs = 2;
  28. work_queue.msgs = (struct i2c_msg*)malloc(work_queue.nmsgs *sizeof(struct
  29. i2c_msg));
  30. if (!work_queue.msgs) {
  31. printf("Memory alloc error\n");
  32. close(fd);
  33. return 0;
  34. }
  35. ioctl(fd, I2C_TIMEOUT, 2);
  36. ioctl(fd, I2C_RETRIES, 1);
  37. (work_queue.msgs[0]).len = 1;
  38. (work_queue.msgs[0]).addr = slave_address;
  39. (work_queue.msgs[0]).buf = &w_val;
  40. (work_queue.msgs[1]).len = len;
  41. (work_queue.msgs[1]).flags = I2C_M_RD;
  42. (work_queue.msgs[1]).addr = slave_address;
  43. (work_queue.msgs[1]).buf = buf;
  44. ret = ioctl(fd, I2C_RDWR, (unsigned long) &work_queue);
  45. if (ret < 0) {
  46. printf("Error during I2C_RDWR ioctl with error code: %d\n", ret);
  47. close(fd);
  48. free(work_queue.msgs);
  49. return 0;
  50. } else {
  51. printf("read salve:%02x reg:%02x\n", slave_address, reg_address);
  52. close(fd);
  53. free(work_queue.msgs);
  54. return len;
  55. }
  56. }
  57. int i2c_write_reg(char *dev, unsigned char *buf, unsigned slave_address, unsigned reg_address, int len)
  58. {
  59. struct i2c_rdwr_ioctl_data work_queue;
  60. unsigned char w_val = reg_address;
  61. unsigned char w_buf[len+1];
  62. int ret;
  63. w_buf[0] = reg_address;
  64. int fd = open(dev, O_RDWR);
  65. if (!fd) {
  66. printf("Error on opening the device file\n");
  67. return 0;
  68. }
  69. work_queue.nmsgs = 1;
  70. work_queue.msgs = (struct i2c_msg*)malloc(work_queue.nmsgs *sizeof(struct
  71. i2c_msg));
  72. if (!work_queue.msgs) {
  73. printf("Memory alloc error\n");
  74. close(fd);
  75. return 0;
  76. }
  77. ioctl(fd, I2C_TIMEOUT, 2);
  78. ioctl(fd, I2C_RETRIES, 1);
  79. (work_queue.msgs[0]).len = 1 + len;
  80. (work_queue.msgs[0]).addr = slave_address;
  81. (work_queue.msgs[0]).buf = w_buf;
  82. memcpy(w_buf + 1, buf, len);
  83. ret = ioctl(fd, I2C_RDWR, (unsigned long) &work_queue);
  84. if (ret < 0) {
  85. printf("Error during I2C_RDWR ioctl with error code: %d\n", ret);
  86. close(fd);
  87. free(work_queue.msgs);
  88. return 0;
  89. } else {
  90. printf("write salve:%02x reg:%02x\n", slave_address, reg_address);
  91. close(fd);
  92. free(work_queue.msgs);
  93. return len;
  94. }
  95. }
  96. int main(int argc, char **argv)
  97. {
  98. unsigned int fd;
  99. unsigned int slave_address, reg_address;
  100. unsigned r_w;
  101. unsigned w_val;
  102. unsigned char rw_val;
  103. if (argc < 5) {
  104. printf("Usage:\n%s /dev/i2c-x start_addr reg_addr rw[0|1] [write_val]\n", argv[0]);
  105. return 0;
  106. }
  107. fd = open(argv[1], O_RDWR);
  108. if (!fd) {
  109. printf("Error on opening the device file %s\n", argv[1]);
  110. return 0;
  111. }
  112. sscanf(argv[2], "%x", &slave_address);
  113. sscanf(argv[3], "%x", ®_address);
  114. sscanf(argv[4], "%d", &r_w);
  115. if (r_w == 0) {
  116. i2c_read_reg(argv[1], &rw_val, slave_address, reg_address, 1);
  117. printf("Read %s-%x reg %x, read value:%x\n", argv[1], slave_address, reg_address, rw_val);
  118. } else {
  119. if (argc < 6) {
  120. printf("Usage:\n%s /dev/i2c-x start_addr reg_addr r|w[0|1] [write_val]\n", argv[0]);
  121. return 0;
  122. }
  123. sscanf(argv[5], "%d", &w_val);
  124. if ((w_val & ~0xff) != 0)
  125. printf("Error on written value %s\n", argv[5]);
  126. rw_val = (unsigned char)w_val;
  127. i2c_write_reg(argv[1], &rw_val, slave_address, reg_address, 1);
  128. }
  129. return 0;
  130. }

在Android/external/新建i2c-util目錄,上述源代碼存入android/external/i2c-util/i2c-util.c,編寫對應的Android.mk:

[plain]
  1. LOCAL_PATH := $(call my-dir)
  2. include $(CLEAR_VARS)
  3. LOCAL_MODULE_TAGS := optional
  4. LOCAL_MODULE := i2c-util
  5. LOCAL_SRC_FILES += \
  6. i2c-util.c \
  7. include $(BUILD_EXECUTABLE)

編譯Android後,上述工具會位於/system/bin目錄。在QT210 LDD上使用它:

[plain]

  1. / # i2c-rw /dev/i2c-2 0x38 0x1 0
  2. read salve:38 reg:01 value:12
  3. / #
  4. / # i2c-rw /dev/i2c-2 0x38 0x2 0
  5. read salve:38 reg:02 value:81
Copyright © Linux教程網 All Rights Reserved