spidevc.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * spidevc.c - SPI library for raspberry pi/bitfury chip/board
  3. *
  4. * Copyright (c) 2013 bitfury
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, see http://www.gnu.org/licenses/.
  17. */
  18. #include "spidevc.h"
  19. #include <sys/mman.h>
  20. #include <stdint.h>
  21. #include <unistd.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <getopt.h>
  26. #include <fcntl.h>
  27. #include <sys/ioctl.h>
  28. #include <linux/types.h>
  29. #include <signal.h>
  30. #include <sys/types.h>
  31. #include <linux/spi/spidev.h>
  32. #include <time.h>
  33. #include <unistd.h>
  34. #include <linux/i2c.h>
  35. #include <linux/i2c-dev.h>
  36. #include <sys/stat.h>
  37. static volatile unsigned *gpio;
  38. void spi_init(void)
  39. {
  40. int fd;
  41. fd = open("/dev/mem",O_RDWR|O_SYNC);
  42. if (fd < 0) { perror("/dev/mem trouble"); exit(1); }
  43. gpio = mmap(0,4096,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0x20200000);
  44. if (gpio == MAP_FAILED) { perror("gpio mmap trouble"); exit(1); }
  45. close(fd);
  46. }
  47. #define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
  48. #define OUT_GPIO(g) *(gpio+((g)/10)) |= (1<<(((g)%10)*3))
  49. #define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
  50. #define GPIO_SET *(gpio+7) // sets bits which are 1 ignores bits which are 0
  51. #define GPIO_CLR *(gpio+10) // clears bits which are 1 ignores bits which are 0
  52. // Bit-banging reset, to reset more chips in chain - toggle for longer period... Each 3 reset cycles reset first chip in chain
  53. void spi_reset(void)
  54. {
  55. int i,j;
  56. int a = 1234, len = 2;
  57. INP_GPIO(10); OUT_GPIO(10);
  58. INP_GPIO(11); OUT_GPIO(11);
  59. GPIO_SET = 1 << 11; // Set SCK
  60. for (i = 0; i < 16; i++) { // On standard settings this unoptimized code produces 1 Mhz freq.
  61. GPIO_SET = 1 << 10;
  62. for (j = 0; j < len; j++) {
  63. a *= a;
  64. }
  65. GPIO_CLR = 1 << 10;
  66. for (j = 0; j < len; j++) {
  67. a *= a;
  68. }
  69. }
  70. GPIO_CLR = 1 << 10;
  71. GPIO_CLR = 1 << 11;
  72. INP_GPIO(10);
  73. SET_GPIO_ALT(10,0);
  74. INP_GPIO(11);
  75. SET_GPIO_ALT(11,0);
  76. INP_GPIO(9);
  77. SET_GPIO_ALT(9,0);
  78. }
  79. int spi_txrx(const char *wrbuf, char *rdbuf, int bufsz)
  80. {
  81. int fd;
  82. int mode, bits, speed, rv, i, j;
  83. struct timespec tv;
  84. struct spi_ioc_transfer tr[16];
  85. memset(&tr,0,sizeof(tr));
  86. mode = 0; bits = 8; speed = 2000000;
  87. spi_reset();
  88. fd = open("/dev/spidev0.0", O_RDWR);
  89. if (fd < 0) { perror("Unable to open SPI device"); exit(1); }
  90. if (ioctl(fd, SPI_IOC_WR_MODE, &mode) < 0) { perror("Unable to set WR MODE"); close(fd); return -1; }
  91. if (ioctl(fd, SPI_IOC_RD_MODE, &mode) < 0) { perror("Unable to set RD MODE"); close(fd); return -1; }
  92. if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) < 0) { perror("Unable to set WR_BITS_PER_WORD"); close(fd); return -1; }
  93. if (ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits) < 0) { perror("Unable to set RD_BITS_PER_WORD"); close(fd); return -1; }
  94. if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0) { perror("Unable to set WR_MAX_SPEED_HZ"); close(fd); return -1; }
  95. if (ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0) { perror("Unable to set RD_MAX_SPEED_HZ"); close(fd); return -1; }
  96. rv = 0;
  97. while (bufsz >= 4096) {
  98. tr[rv].tx_buf = (uintptr_t) wrbuf;
  99. tr[rv].rx_buf = (uintptr_t) rdbuf;
  100. tr[rv].len = 4096;
  101. tr[rv].delay_usecs = 1;
  102. tr[rv].speed_hz = speed;
  103. tr[rv].bits_per_word = bits;
  104. bufsz -= 4096;
  105. wrbuf += 4096; rdbuf += 4096; rv ++;
  106. }
  107. if (bufsz > 0) {
  108. tr[rv].tx_buf = (uintptr_t) wrbuf;
  109. tr[rv].rx_buf = (uintptr_t) rdbuf;
  110. tr[rv].len = (unsigned)bufsz;
  111. tr[rv].delay_usecs = 1;
  112. tr[rv].speed_hz = speed;
  113. tr[rv].bits_per_word = bits;
  114. rv ++;
  115. }
  116. i = rv;
  117. for (j = 0; j < i; j++) {
  118. rv = (int)ioctl(fd, SPI_IOC_MESSAGE(1), (intptr_t)&tr[j]);
  119. if (rv < 0) { perror("WTF!"); close(fd); return -1; }
  120. }
  121. close(fd);
  122. spi_reset();
  123. return 0;
  124. }
  125. #define SPIMAXSZ 256*1024
  126. static unsigned char spibuf[SPIMAXSZ], spibuf_rx[SPIMAXSZ];
  127. static unsigned spibufsz;
  128. void spi_clear_buf(void) { spibufsz = 0; }
  129. unsigned char *spi_getrxbuf(void) { return spibuf_rx; }
  130. unsigned char *spi_gettxbuf(void) { return spibuf; }
  131. unsigned spi_getbufsz(void) { return spibufsz; }
  132. void spi_emit_buf_reverse(const char *str, unsigned sz)
  133. {
  134. unsigned i;
  135. if (spibufsz + sz >= SPIMAXSZ) return;
  136. for (i = 0; i < sz; i++) { // Reverse bit order in each byte!
  137. unsigned char p = str[i];
  138. p = ((p & 0xaa)>>1) | ((p & 0x55) << 1);
  139. p = ((p & 0xcc)>>2) | ((p & 0x33) << 2);
  140. p = ((p & 0xf0)>>4) | ((p & 0x0f) << 4);
  141. spibuf[spibufsz+i] = p;
  142. }
  143. spibufsz += sz;
  144. }
  145. void spi_emit_buf(const char *str, unsigned sz)
  146. {
  147. unsigned i;
  148. if (spibufsz + sz >= SPIMAXSZ) return;
  149. memcpy(&spibuf[spibufsz], str, sz); spibufsz += sz;
  150. }
  151. /* TODO: in production, emit just bit-sequences! Eliminate padding to byte! */
  152. void spi_emit_break(void) { spi_emit_buf("\x4", 1); }
  153. void spi_emit_fsync(void) { spi_emit_buf("\x6", 1); }
  154. void spi_emit_fasync(int n) {
  155. int i;
  156. for (i = 0; i < n; i++) {
  157. spi_emit_buf("\x5", 1);
  158. }
  159. }
  160. void spi_emit_nop(int n) {
  161. int i;
  162. for (i = 0; i < n; n++) {
  163. spi_emit_buf("\x0", 1);
  164. }
  165. }
  166. void spi_emit_data(unsigned addr, const char *buf, unsigned len)
  167. {
  168. unsigned char otmp[3];
  169. if (len < 4 || len > 128) return; /* This cannot be programmed in single frame! */
  170. len /= 4; /* Strip */
  171. otmp[0] = (len - 1) | 0xE0;
  172. otmp[1] = (addr >> 8)&0xFF; otmp[2] = addr & 0xFF;
  173. spi_emit_buf(otmp, 3);
  174. spi_emit_buf_reverse(buf, len*4);
  175. }