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.

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