選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 

271 行
7.6 KiB

  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://github.com/WiringPi/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. int result ;
  92. args.read_write = rw ;
  93. args.command = command ;
  94. args.size = size ;
  95. args.data = data ;
  96. result = ioctl (fd, I2C_SMBUS, &args) ;
  97. if (result < 0) {
  98. return wiringPiFailure (WPI_FATAL, "i2c_smbus_access() -> I2C SMBUS access failed: [%s]\n", strerror (errno)) ;
  99. }
  100. else {
  101. return ioctl (fd, I2C_SMBUS, &args) ;
  102. }
  103. }
  104. /*
  105. * wiringPiI2CRead:
  106. * Simple device read
  107. *********************************************************************************
  108. */
  109. int wiringPiI2CRead (int fd)
  110. {
  111. union i2c_smbus_data data ;
  112. if (i2c_smbus_access (fd, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data))
  113. return -1 ;
  114. else
  115. return data.byte & 0xFF ;
  116. }
  117. /*
  118. * wiringPiI2CReadReg8: wiringPiI2CReadReg16:
  119. * Read an 8 or 16-bit value from a regsiter on the device
  120. *********************************************************************************
  121. */
  122. int wiringPiI2CReadReg8 (int fd, int reg)
  123. {
  124. union i2c_smbus_data data;
  125. if (i2c_smbus_access (fd, I2C_SMBUS_READ, reg, I2C_SMBUS_BYTE_DATA, &data))
  126. return -1 ;
  127. else
  128. return data.byte & 0xFF ;
  129. }
  130. int wiringPiI2CReadReg16 (int fd, int reg)
  131. {
  132. union i2c_smbus_data data;
  133. if (i2c_smbus_access (fd, I2C_SMBUS_READ, reg, I2C_SMBUS_WORD_DATA, &data))
  134. return -1 ;
  135. else
  136. return data.word & 0xFFFF ;
  137. }
  138. /*
  139. * wiringPiI2CWrite:
  140. * Simple device write
  141. *********************************************************************************
  142. */
  143. int wiringPiI2CWrite (int fd, int data)
  144. {
  145. return i2c_smbus_access (fd, I2C_SMBUS_WRITE, data, I2C_SMBUS_BYTE, NULL) ;
  146. }
  147. /*
  148. * wiringPiI2CWriteReg8: wiringPiI2CWriteReg16:
  149. * Write an 8 or 16-bit value to the given register
  150. *********************************************************************************
  151. */
  152. int wiringPiI2CWriteReg8 (int fd, int reg, int value)
  153. {
  154. union i2c_smbus_data data ;
  155. data.byte = value ;
  156. return i2c_smbus_access (fd, I2C_SMBUS_WRITE, reg, I2C_SMBUS_BYTE_DATA, &data) ;
  157. }
  158. int wiringPiI2CWriteReg16 (int fd, int reg, int value)
  159. {
  160. union i2c_smbus_data data ;
  161. data.word = value ;
  162. return i2c_smbus_access (fd, I2C_SMBUS_WRITE, reg, I2C_SMBUS_WORD_DATA, &data) ;
  163. }
  164. /*
  165. * wiringPiI2CSetupInterface:
  166. * Undocumented access to set the interface explicitly - might be used
  167. * for the Pi's 2nd I2C interface...
  168. *********************************************************************************
  169. */
  170. int wiringPiI2CSetupInterface (const char *device, int devId)
  171. {
  172. int fd ;
  173. if ((fd = open (device, O_RDWR)) < 0)
  174. return wiringPiFailure (WPI_ALMOST, "wiringPiI2CSetupInterface() -> Unable to open I2C device [%s]: %s\n", device, strerror (errno)) ;
  175. if (ioctl (fd, I2C_SLAVE, devId) < 0)
  176. return wiringPiFailure (WPI_ALMOST, "wiringPiI2CSetupInterface() -> Unable to select I2C devId [%d]: [%s]\n", devId, strerror (errno)) ;
  177. return fd ;
  178. }
  179. /*
  180. * wiringPiI2CSetup:
  181. * Open the I2C device, and regsiter the target device.
  182. * Based on the Raspberry Pi revision number /dev/i2c-0 or /dev/i2c-1 will be used
  183. *********************************************************************************
  184. */
  185. int wiringPiI2CSetup (const int devId)
  186. {
  187. return wiringPiI2CSetupDevice (devId, -1);
  188. }
  189. /*
  190. * wiringPiI2CSetupDevice:
  191. * Open the I2C device, and regsiter the target device.
  192. * This function allows one to use another I2C bus than the default i2c-0 or i2c-1
  193. * bus. More I2C busses can be available when using an I2C mux like the PCA9548.
  194. * Backwards compatibility is guaranteed: existing code is using wiringPiI2CSetup()
  195. * and that function will call wiringPiI2CSetupDevice() with dev == -1 resulting
  196. * in executing code as it used to be (if rev == 1 use /dev/i2c-0 else use
  197. * /dev/i2c-1).
  198. *********************************************************************************
  199. */
  200. int wiringPiI2CSetupDevice (const int devId, const int dev)
  201. {
  202. int rev ;
  203. char device[12]; // could be "/dev/i2c-xx" + the NULL terminator '\0'
  204. rev = piGpioLayout () ;
  205. if (rev == 1) {
  206. sprintf(device, "/dev/i2c-0");
  207. }
  208. else {
  209. if (dev == -1) {
  210. sprintf(device, "/dev/i2c-1");
  211. // printf("Original device = %s\n", device);
  212. }
  213. else {
  214. snprintf(device, 12, "/dev/i2c-%d", dev);
  215. // printf("wiringPiI2CSetupDevice() -> Device = [%s]\n", device);
  216. }
  217. }
  218. return wiringPiI2CSetupInterface (device, devId) ;
  219. }