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.
 
 
 
 
 

123 regels
2.9 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://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. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <fcntl.h>
  27. #include <sys/ioctl.h>
  28. #include <linux/i2c-dev.h>
  29. #include "wiringPi.h"
  30. #include "wiringPiI2C.h"
  31. /*
  32. * wiringPiI2CRead:
  33. * Simple device read
  34. *********************************************************************************
  35. */
  36. int wiringPiI2CRead (int fd)
  37. {
  38. return i2c_smbus_read_byte (fd) ;
  39. }
  40. /*
  41. * wiringPiI2CReadReg8: wiringPiI2CReadReg16:
  42. * Read an 8 or 16-bit value from a regsiter on the device
  43. *********************************************************************************
  44. */
  45. int wiringPiI2CReadReg8 (int fd, int reg)
  46. {
  47. return i2c_smbus_read_byte_data (fd, reg) ;
  48. }
  49. int wiringPiI2CReadReg16 (int fd, int reg)
  50. {
  51. return i2c_smbus_read_word_data (fd, reg) ;
  52. }
  53. /*
  54. * wiringPiI2CWrite:
  55. * Simple device write
  56. *********************************************************************************
  57. */
  58. int wiringPiI2CWrite (int fd, int data)
  59. {
  60. return i2c_smbus_write_byte (fd, data) ;
  61. }
  62. /*
  63. * wiringPiI2CWriteReg8: wiringPiI2CWriteReg16:
  64. * Write an 8 or 16-bit value to the given register
  65. *********************************************************************************
  66. */
  67. int wiringPiI2CWriteReg8 (int fd, int reg, int data)
  68. {
  69. return i2c_smbus_write_byte_data (fd, reg, data) ;
  70. }
  71. int wiringPiI2CWriteReg16 (int fd, int reg, int data)
  72. {
  73. return i2c_smbus_write_word_data (fd, reg, data) ;
  74. }
  75. /*
  76. * wiringPiI2CSetup:
  77. * Open the I2C device, and regsiter the target device
  78. *********************************************************************************
  79. */
  80. int wiringPiI2CSetup (int devId)
  81. {
  82. int rev, fd ;
  83. char *device ;
  84. if ((rev = piBoardRev ()) < 0)
  85. {
  86. fprintf (stderr, "wiringPiI2CSetup: Unable to determine Pi board revision\n") ;
  87. exit (1) ;
  88. }
  89. if (rev == 1)
  90. device = "/dev/i2c-0" ;
  91. else
  92. device = "/dev/i2c-1" ;
  93. if ((fd = open (device, O_RDWR)) < 0)
  94. return -1 ;
  95. if (ioctl (fd, I2C_SLAVE, devId) < 0)
  96. return -1 ;
  97. return fd ;
  98. }