wiringPiI2C.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * wiringPiI2C.c:
  3. * Simplified I2C access routines
  4. * Copyright (c) 2013 Gordon Henderson
  5. ***********************************************************************
  6. * This file is part of wiringPi:
  7. * https://projects.drogon.net/raspberry-pi/wiringpi/
  8. *
  9. * wiringPi is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * wiringPi is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with wiringPi.
  21. * If not, see <http://www.gnu.org/licenses/>.
  22. ***********************************************************************
  23. */
  24. /*
  25. * Notes:
  26. * The Linux I2C code is actually the same (almost) as the SMBus code.
  27. * SMBus is System Management Bus - and in essentially I2C with some
  28. * additional functionality added, and stricter controls on the electrical
  29. * specifications, etc. however I2C does work well with it and the
  30. * protocols work over both.
  31. *
  32. * I'm directly including the SMBus functions here as some Linux distros
  33. * lack the correct header files, and also some header files are GPLv2
  34. * rather than the LGPL that wiringPi is released under - presumably because
  35. * originally no-one expected I2C/SMBus to be used outside the kernel -
  36. * however enter the Raspberry Pi with people now taking directly to I2C
  37. * devices without going via the kernel...
  38. *
  39. * This may ultimately reduce the flexibility of this code, but it won't be
  40. * hard to maintain it and keep it current, should things change.
  41. *
  42. * Information here gained from: kernel/Documentation/i2c/dev-interface
  43. * as well as other online resources.
  44. *********************************************************************************
  45. */
  46. #include <stdio.h>
  47. #include <stdlib.h>
  48. #include <stdint.h>
  49. #include <errno.h>
  50. #include <string.h>
  51. #include <fcntl.h>
  52. #include <sys/ioctl.h>
  53. #include <asm/ioctl.h>
  54. #include "wiringPi.h"
  55. #include "wiringPiI2C.h"
  56. // I2C definitions
  57. #define I2C_SLAVE 0x0703
  58. #define I2C_SMBUS 0x0720 /* SMBus-level access */
  59. #define I2C_SMBUS_READ 1
  60. #define I2C_SMBUS_WRITE 0
  61. // SMBus transaction types
  62. #define I2C_SMBUS_QUICK 0
  63. #define I2C_SMBUS_BYTE 1
  64. #define I2C_SMBUS_BYTE_DATA 2
  65. #define I2C_SMBUS_WORD_DATA 3
  66. #define I2C_SMBUS_PROC_CALL 4
  67. #define I2C_SMBUS_BLOCK_DATA 5
  68. #define I2C_SMBUS_I2C_BLOCK_BROKEN 6
  69. #define I2C_SMBUS_BLOCK_PROC_CALL 7 /* SMBus 2.0 */
  70. #define I2C_SMBUS_I2C_BLOCK_DATA 8
  71. // SMBus messages
  72. #define I2C_SMBUS_BLOCK_MAX 32 /* As specified in SMBus standard */
  73. #define I2C_SMBUS_I2C_BLOCK_MAX 32 /* Not specified but we use same structure */
  74. // Structures used in the ioctl() calls
  75. union i2c_smbus_data
  76. {
  77. uint8_t byte ;
  78. uint16_t word ;
  79. uint8_t block [I2C_SMBUS_BLOCK_MAX + 2] ; // block [0] is used for length + one more for PEC
  80. } ;
  81. struct i2c_smbus_ioctl_data
  82. {
  83. char read_write ;
  84. uint8_t command ;
  85. int size ;
  86. union i2c_smbus_data *data ;
  87. } ;
  88. static inline int i2c_smbus_access (int fd, char rw, uint8_t command, int size, union i2c_smbus_data *data)
  89. {
  90. struct i2c_smbus_ioctl_data args ;
  91. args.read_write = rw ;
  92. args.command = command ;
  93. args.size = size ;
  94. args.data = data ;
  95. return ioctl (fd, I2C_SMBUS, &args) ;
  96. }
  97. /*
  98. * wiringPiI2CRead:
  99. * Simple device read
  100. *********************************************************************************
  101. */
  102. int wiringPiI2CRead (int fd)
  103. {
  104. union i2c_smbus_data data ;
  105. if (i2c_smbus_access (fd, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data))
  106. return -1 ;
  107. else
  108. return data.byte & 0xFF ;
  109. }
  110. /*
  111. * wiringPiI2CReadReg8: wiringPiI2CReadReg16:
  112. * Read an 8 or 16-bit value from a regsiter on the device
  113. *********************************************************************************
  114. */
  115. int wiringPiI2CReadReg8 (int fd, int reg)
  116. {
  117. union i2c_smbus_data data;
  118. if (i2c_smbus_access (fd, I2C_SMBUS_READ, reg, I2C_SMBUS_BYTE_DATA, &data))
  119. return -1 ;
  120. else
  121. return data.byte & 0xFF ;
  122. }
  123. int wiringPiI2CReadReg16 (int fd, int reg)
  124. {
  125. union i2c_smbus_data data;
  126. if (i2c_smbus_access (fd, I2C_SMBUS_READ, reg, I2C_SMBUS_WORD_DATA, &data))
  127. return -1 ;
  128. else
  129. return data.word & 0xFFFF ;
  130. }
  131. /*
  132. * wiringPiI2CWrite:
  133. * Simple device write
  134. *********************************************************************************
  135. */
  136. int wiringPiI2CWrite (int fd, int data)
  137. {
  138. return i2c_smbus_access (fd, I2C_SMBUS_WRITE, data, I2C_SMBUS_BYTE, NULL) ;
  139. }
  140. /*
  141. * wiringPiI2CWriteReg8: wiringPiI2CWriteReg16:
  142. * Write an 8 or 16-bit value to the given register
  143. *********************************************************************************
  144. */
  145. int wiringPiI2CWriteReg8 (int fd, int reg, int value)
  146. {
  147. union i2c_smbus_data data ;
  148. data.byte = value ;
  149. return i2c_smbus_access (fd, I2C_SMBUS_WRITE, reg, I2C_SMBUS_BYTE_DATA, &data) ;
  150. }
  151. int wiringPiI2CWriteReg16 (int fd, int reg, int value)
  152. {
  153. union i2c_smbus_data data ;
  154. data.word = value ;
  155. return i2c_smbus_access (fd, I2C_SMBUS_WRITE, reg, I2C_SMBUS_WORD_DATA, &data) ;
  156. }
  157. /*
  158. * wiringPiI2CSetupInterface:
  159. * Undocumented access to set the interface explicitly - might be used
  160. * for the Pi's 2nd I2C interface...
  161. *********************************************************************************
  162. */
  163. int wiringPiI2CSetupInterface (const char *device, int devId)
  164. {
  165. int fd ;
  166. if ((fd = open (device, O_RDWR)) < 0)
  167. return wiringPiFailure (WPI_ALMOST, "Unable to open I2C device: %s\n", strerror (errno)) ;
  168. if (ioctl (fd, I2C_SLAVE, devId) < 0)
  169. return wiringPiFailure (WPI_ALMOST, "Unable to select I2C device: %s\n", strerror (errno)) ;
  170. return fd ;
  171. }
  172. /*
  173. * wiringPiI2CSetup:
  174. * Open the I2C device, and regsiter the target device
  175. *********************************************************************************
  176. */
  177. int wiringPiI2CSetup (const int devId)
  178. {
  179. int rev ;
  180. const char *device ;
  181. rev = piGpioLayout () ;
  182. if (rev == 1)
  183. device = "/dev/i2c-0" ;
  184. else
  185. device = "/dev/i2c-1" ;
  186. return wiringPiI2CSetupInterface (device, devId) ;
  187. }