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.
 
 
 
 
 

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