You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
3.7 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. A user-space program to get data from an I2C device.
  3. Gustavo Zamboni
  4. */
  5. #include <errno.h>
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <linux/i2c-dev.h>
  11. #include <sys/ioctl.h>
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include <fcntl.h>
  15. char buf[10];
  16. extern int com_serial;
  17. extern int failcount;
  18. //////////
  19. // Init I2Cdevice
  20. //////////
  21. int i2c_init(char filename[40], int addr)
  22. {
  23. int file;
  24. if ((file = open(filename,O_RDWR)) < 0)
  25. {
  26. printf("Failed to open the bus.");
  27. /* ERROR HANDLING; you can check errno to see what went wrong */
  28. com_serial=0;
  29. exit(1);
  30. }
  31. if (ioctl(file,I2C_SLAVE,addr) < 0)
  32. {
  33. printf("Failed to acquire bus access and/or talk to slave.\n");
  34. /* ERROR HANDLING; you can check errno to see what went wrong */
  35. com_serial=0;
  36. exit(1);
  37. }
  38. return file;
  39. }
  40. //////////
  41. // Set pointer address
  42. //////////
  43. void i2c_set_pointer(int address,int value,int file)
  44. {
  45. /*
  46. //printf("end: 0x%x%x\n", address,value);
  47. if (i2c_smbus_write_byte_data(file, address, value)<0)
  48. {
  49. fprintf(stderr, "Warning - write failed\n");
  50. }
  51. */
  52. char buf[10];
  53. buf[0]=address;
  54. buf[1]=value;
  55. if (write(file, buf, 2) != 2)
  56. {
  57. fprintf(stderr, "Error setting pointer\n");
  58. com_serial=0;
  59. failcount++;
  60. }
  61. else
  62. {
  63. //printf("w_0x%0*x\n", 2, buf[0]);
  64. //printf("w_0x%0*x\n", 2, buf[1]);
  65. com_serial=1;
  66. failcount=0;
  67. }
  68. }
  69. //////////
  70. // Read n bytes
  71. //////////
  72. char * i2c_read(int add1, int add2, int nbytes,int file)
  73. {
  74. int n;
  75. i2c_set_pointer(add1,add2,file);
  76. if (read(file, buf, nbytes) != nbytes)
  77. {
  78. fprintf(stderr, "Error reading %i bytes\n",nbytes);
  79. com_serial=0;
  80. failcount++;
  81. }
  82. else
  83. {
  84. for (n=0;n<nbytes;n++)
  85. {
  86. //printf("r_0x%0*x\n", 2, buf[n]);
  87. }
  88. com_serial=1;
  89. failcount=0;
  90. return buf;
  91. }
  92. }
  93. //////////
  94. // Write n bytes
  95. //////////
  96. void i2c_write(int add1,int add2,int nbytes,char value[10],int file)
  97. {
  98. int n;
  99. unsigned char buf[10];
  100. buf[0] = add1;
  101. buf[1] = add2;
  102. if (nbytes>=1) buf[2] = value[0];
  103. if (nbytes>=2) buf[3] = value[1];
  104. if (nbytes>=3) buf[4] = value[2];
  105. if (nbytes>=4) buf[5] = value[3];
  106. if (write(file, buf, nbytes) != nbytes)
  107. {
  108. fprintf(stderr, "Error writing %i bytes\n",nbytes);
  109. com_serial=0;
  110. failcount++;
  111. }
  112. else
  113. {
  114. for (n=0;n<(nbytes+2);n++)
  115. {
  116. //printf("w_0x%0*x\n", 2, buf[n]);
  117. }
  118. com_serial=1;
  119. failcount=0;
  120. }
  121. }
  122. //Usage example:
  123. char *buffer;
  124. char buf[10];
  125. file=i2c_init("/dev/i2c-1",0x38); //dev,slavei2caddr
  126. buf[0] = 0x41;
  127. buf[1] = 0xFF;
  128. i2c_write(0xE6,0x0E,2,buf,file); //this will write value 0x41FF to the address 0xE60E
  129. buffer=(char *)i2c_read(0xE6,0x0E,4,file); //reading a 4 bytes value at the address 0xE60E
  130. close(file);