您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

148 行
4.3 KiB

  1. /*
  2. * wiringPiSPI.c:
  3. * Simplified SPI access routines
  4. * Copyright (c) 2012-2015 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 <stdint.h>
  25. #include <fcntl.h>
  26. #include <errno.h>
  27. #include <string.h>
  28. #include <sys/ioctl.h>
  29. #include <asm/ioctl.h>
  30. #include <linux/spi/spidev.h>
  31. #include "wiringPi.h"
  32. #include "wiringPiSPI.h"
  33. // The SPI bus parameters
  34. // Variables as they need to be passed as pointers later on
  35. static const char *spiDev0 = "/dev/spidev0.0" ;
  36. static const char *spiDev1 = "/dev/spidev0.1" ;
  37. static const char *spiDevType3 = "/dev/spidev3.0";
  38. static const uint8_t spiBPW = 8 ;
  39. static const uint16_t spiDelay = 0 ;
  40. static uint32_t spiSpeeds [2] ;
  41. static int spiFds [2] ;
  42. /*
  43. * wiringPiSPIGetFd:
  44. * Return the file-descriptor for the given channel
  45. *********************************************************************************
  46. */
  47. int wiringPiSPIGetFd (int channel)
  48. {
  49. return spiFds [channel & 1] ;
  50. }
  51. /*
  52. * wiringPiSPIDataRW:
  53. * Write and Read a block of data over the SPI bus.
  54. * Note the data ia being read into the transmit buffer, so will
  55. * overwrite it!
  56. * This is also a full-duplex operation.
  57. *********************************************************************************
  58. */
  59. int wiringPiSPIDataRW (int channel, unsigned char *data, int len)
  60. {
  61. struct spi_ioc_transfer spi ;
  62. channel &= 1 ;
  63. // Mentioned in spidev.h but not used in the original kernel documentation
  64. // test program )-:
  65. memset (&spi, 0, sizeof (spi)) ;
  66. spi.tx_buf = (unsigned long)data ;
  67. spi.rx_buf = (unsigned long)data ;
  68. spi.len = len ;
  69. spi.delay_usecs = spiDelay ;
  70. spi.speed_hz = spiSpeeds [channel] ;
  71. spi.bits_per_word = spiBPW ;
  72. return ioctl (spiFds [channel], SPI_IOC_MESSAGE(1), &spi) ;
  73. }
  74. /*
  75. * wiringPiSPISetupMode:
  76. * Open the SPI device, and set it up, with the mode, etc.
  77. *********************************************************************************
  78. */
  79. int wiringPiSPISetupMode (int channel, int speed, int mode)
  80. {
  81. int fd ;
  82. int model, rev, mem, maker, overVolted ;
  83. const char *device ;
  84. piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
  85. mode &= 3 ; // Mode is 0, 1, 2 or 3
  86. channel &= 1 ; // Channel is 0 or 1
  87. if (model == MODEL_KHADAS_EDGE) {
  88. device = spiDevType3;
  89. if ((fd = open (device, O_RDWR)) < 0)
  90. return wiringPiFailure (WPI_ALMOST, "Unable to open SPI device: %s\n", strerror (errno)) ;
  91. }
  92. else{
  93. if ((fd = open (channel == 0 ? spiDev0 : spiDev1, O_RDWR)) < 0)
  94. return wiringPiFailure (WPI_ALMOST, "Unable to open SPI device: %s\n", strerror (errno)) ;
  95. }
  96. spiSpeeds [channel] = speed ;
  97. spiFds [channel] = fd ;
  98. // Set SPI parameters.
  99. if (ioctl (fd, SPI_IOC_WR_MODE, &mode) < 0)
  100. return wiringPiFailure (WPI_ALMOST, "SPI Mode Change failure: %s\n", strerror (errno)) ;
  101. if (ioctl (fd, SPI_IOC_WR_BITS_PER_WORD, &spiBPW) < 0)
  102. return wiringPiFailure (WPI_ALMOST, "SPI BPW Change failure: %s\n", strerror (errno)) ;
  103. if (ioctl (fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0)
  104. return wiringPiFailure (WPI_ALMOST, "SPI Speed Change failure: %s\n", strerror (errno)) ;
  105. return fd ;
  106. }
  107. /*
  108. * wiringPiSPISetup:
  109. * Open the SPI device, and set it up, etc. in the default MODE 0
  110. *********************************************************************************
  111. */
  112. int wiringPiSPISetup (int channel, int speed)
  113. {
  114. return wiringPiSPISetupMode (channel, speed, 0) ;
  115. }