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

270 行
7.2 KiB

  1. /*
  2. * wiringPiI2C.c:
  3. * Simplified I2C access routines
  4. * Copyright (c) 2013-2024 Gordon Henderson and contributors
  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 <unistd.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. int wiringPiI2CReadBlockData (int fd, int reg, uint8_t *values, uint8_t size)
  132. {
  133. union i2c_smbus_data data;
  134. if (size>I2C_SMBUS_BLOCK_MAX) {
  135. size = I2C_SMBUS_BLOCK_MAX;
  136. }
  137. data.block[0] = size;
  138. int result = i2c_smbus_access (fd, I2C_SMBUS_READ, reg, I2C_SMBUS_I2C_BLOCK_DATA, &data);
  139. if (result<0) {
  140. return result;
  141. }
  142. memcpy(values, &data.block[1], size);
  143. return data.block[0];
  144. }
  145. int wiringPiI2CRawRead (int fd, uint8_t *values, uint8_t size)
  146. {
  147. return(read(fd, values, size));
  148. }
  149. /*
  150. * wiringPiI2CWrite:
  151. * Simple device write
  152. *********************************************************************************
  153. */
  154. int wiringPiI2CWrite (int fd, int data)
  155. {
  156. return i2c_smbus_access (fd, I2C_SMBUS_WRITE, data, I2C_SMBUS_BYTE, NULL) ;
  157. }
  158. /*
  159. * wiringPiI2CWriteReg8: wiringPiI2CWriteReg16:
  160. * Write an 8 or 16-bit value to the given register
  161. *********************************************************************************
  162. */
  163. int wiringPiI2CWriteReg8 (int fd, int reg, int value)
  164. {
  165. union i2c_smbus_data data ;
  166. data.byte = value ;
  167. return i2c_smbus_access (fd, I2C_SMBUS_WRITE, reg, I2C_SMBUS_BYTE_DATA, &data) ;
  168. }
  169. int wiringPiI2CWriteReg16 (int fd, int reg, int value)
  170. {
  171. union i2c_smbus_data data ;
  172. data.word = value ;
  173. return i2c_smbus_access (fd, I2C_SMBUS_WRITE, reg, I2C_SMBUS_WORD_DATA, &data) ;
  174. }
  175. int wiringPiI2CWriteBlockData (int fd, int reg, const uint8_t *values, uint8_t size)
  176. {
  177. union i2c_smbus_data data;
  178. if (size>I2C_SMBUS_BLOCK_MAX) {
  179. size = I2C_SMBUS_BLOCK_MAX;
  180. }
  181. data.block[0] = size;
  182. memcpy(&data.block[1], values, size);
  183. return i2c_smbus_access (fd, I2C_SMBUS_WRITE, reg, I2C_SMBUS_BLOCK_DATA, &data) ;
  184. }
  185. int wiringPiI2CRawWrite (int fd, const uint8_t *values, uint8_t size)
  186. {
  187. return(write(fd, values, size));
  188. }
  189. /*
  190. * wiringPiI2CSetupInterface:
  191. * Undocumented access to set the interface explicitly - might be used
  192. * for the Pi's 2nd I2C interface...
  193. *********************************************************************************
  194. */
  195. int wiringPiI2CSetupInterface (const char *device, int devId)
  196. {
  197. int fd ;
  198. if ((fd = open (device, O_RDWR)) < 0)
  199. return wiringPiFailure (WPI_ALMOST, "Unable to open I2C device: %s\n", strerror (errno)) ;
  200. if (ioctl (fd, I2C_SLAVE, devId) < 0)
  201. return wiringPiFailure (WPI_ALMOST, "Unable to select I2C device: %s\n", strerror (errno)) ;
  202. return fd ;
  203. }
  204. /*
  205. * wiringPiI2CSetup:
  206. * Open the I2C device, and regsiter the target device
  207. *********************************************************************************
  208. */
  209. int wiringPiI2CSetup (const int devId)
  210. {
  211. int rev ;
  212. const char *device ;
  213. rev = piGpioLayout () ;
  214. if (rev == 1)
  215. device = "/dev/i2c-0" ;
  216. else
  217. device = "/dev/i2c-1" ;
  218. return wiringPiI2CSetupInterface (device, devId) ;
  219. }