Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

wiringPiI2C.c 6.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 "wiringPi.h"
  54. #include "wiringPiI2C.h"
  55. // I2C definitions
  56. #define I2C_SLAVE 0x0703
  57. #define I2C_SMBUS 0x0720 /* SMBus-level access */
  58. #define I2C_SMBUS_READ 1
  59. #define I2C_SMBUS_WRITE 0
  60. // SMBus transaction types
  61. #define I2C_SMBUS_QUICK 0
  62. #define I2C_SMBUS_BYTE 1
  63. #define I2C_SMBUS_BYTE_DATA 2
  64. #define I2C_SMBUS_WORD_DATA 3
  65. #define I2C_SMBUS_PROC_CALL 4
  66. #define I2C_SMBUS_BLOCK_DATA 5
  67. #define I2C_SMBUS_I2C_BLOCK_BROKEN 6
  68. #define I2C_SMBUS_BLOCK_PROC_CALL 7 /* SMBus 2.0 */
  69. #define I2C_SMBUS_I2C_BLOCK_DATA 8
  70. // SMBus messages
  71. #define I2C_SMBUS_BLOCK_MAX 32 /* As specified in SMBus standard */
  72. #define I2C_SMBUS_I2C_BLOCK_MAX 32 /* Not specified but we use same structure */
  73. // Structures used in the ioctl() calls
  74. union i2c_smbus_data
  75. {
  76. uint8_t byte ;
  77. uint16_t word ;
  78. uint8_t block [I2C_SMBUS_BLOCK_MAX + 2] ; // block [0] is used for length + one more for PEC
  79. } ;
  80. struct i2c_smbus_ioctl_data
  81. {
  82. char read_write ;
  83. uint8_t command ;
  84. int size ;
  85. union i2c_smbus_data *data ;
  86. } ;
  87. static inline int i2c_smbus_access (int fd, char rw, uint8_t command, int size, union i2c_smbus_data *data)
  88. {
  89. struct i2c_smbus_ioctl_data args ;
  90. args.read_write = rw ;
  91. args.command = command ;
  92. args.size = size ;
  93. args.data = data ;
  94. return ioctl (fd, I2C_SMBUS, &args) ;
  95. }
  96. /*
  97. * wiringPiI2CRead:
  98. * Simple device read
  99. *********************************************************************************
  100. */
  101. int wiringPiI2CRead (int fd)
  102. {
  103. union i2c_smbus_data data ;
  104. if (i2c_smbus_access (fd, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data))
  105. return -1 ;
  106. else
  107. return data.byte & 0xFF ;
  108. }
  109. /*
  110. * wiringPiI2CReadReg8: wiringPiI2CReadReg16:
  111. * Read an 8 or 16-bit value from a regsiter on the device
  112. *********************************************************************************
  113. */
  114. int wiringPiI2CReadReg8 (int fd, int reg)
  115. {
  116. union i2c_smbus_data data;
  117. if (i2c_smbus_access (fd, I2C_SMBUS_READ, reg, I2C_SMBUS_BYTE_DATA, &data))
  118. return -1 ;
  119. else
  120. return data.byte & 0xFF ;
  121. }
  122. int wiringPiI2CReadReg16 (int fd, int reg)
  123. {
  124. union i2c_smbus_data data;
  125. if (i2c_smbus_access (fd, I2C_SMBUS_READ, reg, I2C_SMBUS_WORD_DATA, &data))
  126. return -1 ;
  127. else
  128. return data.word & 0xFFFF ;
  129. }
  130. /*
  131. * wiringPiI2CWrite:
  132. * Simple device write
  133. *********************************************************************************
  134. */
  135. int wiringPiI2CWrite (int fd, int data)
  136. {
  137. return i2c_smbus_access (fd, I2C_SMBUS_WRITE, data, I2C_SMBUS_BYTE, NULL) ;
  138. }
  139. /*
  140. * wiringPiI2CWriteReg8: wiringPiI2CWriteReg16:
  141. * Write an 8 or 16-bit value to the given register
  142. *********************************************************************************
  143. */
  144. int wiringPiI2CWriteReg8 (int fd, int reg, int value)
  145. {
  146. union i2c_smbus_data data ;
  147. data.byte = value ;
  148. return i2c_smbus_access (fd, I2C_SMBUS_WRITE, reg, I2C_SMBUS_BYTE_DATA, &data) ;
  149. }
  150. int wiringPiI2CWriteReg16 (int fd, int reg, int value)
  151. {
  152. union i2c_smbus_data data ;
  153. data.word = value ;
  154. return i2c_smbus_access (fd, I2C_SMBUS_WRITE, reg, I2C_SMBUS_WORD_DATA, &data) ;
  155. }
  156. /*
  157. * wiringPiI2CSetupInterface:
  158. * Undocumented access to set the interface explicitly - might be used
  159. * for the Pi's 2nd I2C interface...
  160. *********************************************************************************
  161. */
  162. int wiringPiI2CSetupInterface (const char *device, int devId)
  163. {
  164. int fd ;
  165. if ((fd = open (device, O_RDWR)) < 0)
  166. return wiringPiFailure (WPI_ALMOST, "Unable to open I2C device: %s\n", strerror (errno)) ;
  167. if (ioctl (fd, I2C_SLAVE, devId) < 0)
  168. return wiringPiFailure (WPI_ALMOST, "Unable to select I2C device: %s\n", strerror (errno)) ;
  169. return fd ;
  170. }
  171. /*
  172. * wiringPiI2CSetup:
  173. * Open the I2C device, and regsiter the target device
  174. *********************************************************************************
  175. */
  176. int wiringPiI2CSetup (const int devId)
  177. {
  178. int rev ;
  179. const char *device ;
  180. rev = piBoardRev () ;
  181. if (rev == 1)
  182. device = "/dev/i2c-0" ;
  183. else
  184. device = "/dev/i2c-1" ;
  185. return wiringPiI2CSetupInterface (device, devId) ;
  186. }