spidevc.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright 2013 www.bitfury.org
  3. */
  4. #include "spidevc.h"
  5. #include <sys/mman.h>
  6. #include <stdint.h>
  7. #include <unistd.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <getopt.h>
  12. #include <fcntl.h>
  13. #include <sys/ioctl.h>
  14. #include <linux/types.h>
  15. #include <signal.h>
  16. #include <sys/types.h>
  17. #include <linux/spi/spidev.h>
  18. #include <time.h>
  19. #include <unistd.h>
  20. #include <linux/i2c.h>
  21. #include <linux/i2c-dev.h>
  22. #include <sys/stat.h>
  23. static volatile unsigned *gpio;
  24. void spi_init(void)
  25. {
  26. int fd;
  27. fd = open("/dev/mem",O_RDWR|O_SYNC);
  28. if (fd < 0) { perror("/dev/mem trouble"); exit(1); }
  29. gpio = mmap(0,4096,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0x20200000);
  30. if (gpio == MAP_FAILED) { perror("gpio mmap trouble"); exit(1); }
  31. close(fd);
  32. }
  33. #define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
  34. #define OUT_GPIO(g) *(gpio+((g)/10)) |= (1<<(((g)%10)*3))
  35. #define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
  36. #define GPIO_SET *(gpio+7) // sets bits which are 1 ignores bits which are 0
  37. #define GPIO_CLR *(gpio+10) // clears bits which are 1 ignores bits which are 0
  38. // Bit-banging reset, to reset more chips in chain - toggle for longer period... Each 3 reset cycles reset first chip in chain
  39. void spi_reset(void)
  40. {
  41. int i;
  42. INP_GPIO(10); OUT_GPIO(10);
  43. INP_GPIO(11); OUT_GPIO(11);
  44. GPIO_SET = 1 << 11; // Set SCK
  45. for (i = 0; i < 16; i++) { // On standard settings this unoptimized code produces 1 Mhz freq.
  46. GPIO_SET = 1 << 10;
  47. GPIO_SET = 1 << 10;
  48. GPIO_SET = 1 << 10;
  49. GPIO_SET = 1 << 10;
  50. GPIO_SET = 1 << 10;
  51. GPIO_SET = 1 << 10;
  52. GPIO_SET = 1 << 10;
  53. GPIO_SET = 1 << 10;
  54. GPIO_SET = 1 << 10;
  55. GPIO_SET = 1 << 10;
  56. GPIO_SET = 1 << 10;
  57. GPIO_SET = 1 << 10;
  58. GPIO_CLR = 1 << 10;
  59. GPIO_CLR = 1 << 10;
  60. GPIO_CLR = 1 << 10;
  61. GPIO_CLR = 1 << 10;
  62. GPIO_CLR = 1 << 10;
  63. GPIO_CLR = 1 << 10;
  64. GPIO_CLR = 1 << 10;
  65. GPIO_CLR = 1 << 10;
  66. GPIO_CLR = 1 << 10;
  67. GPIO_CLR = 1 << 10;
  68. GPIO_CLR = 1 << 10;
  69. GPIO_CLR = 1 << 10;
  70. }
  71. GPIO_CLR = 1 << 10;
  72. GPIO_CLR = 1 << 11;
  73. INP_GPIO(10);
  74. SET_GPIO_ALT(10,0);
  75. INP_GPIO(11);
  76. SET_GPIO_ALT(11,0);
  77. INP_GPIO(9);
  78. SET_GPIO_ALT(9,0);
  79. }
  80. int spi_txrx(const char *wrbuf, char *rdbuf, int bufsz)
  81. {
  82. int fd;
  83. int mode, bits, speed, rv, i, j;
  84. struct timespec tv;
  85. struct spi_ioc_transfer tr[16];
  86. memset(&tr,0,sizeof(tr));
  87. mode = 0; bits = 8; speed = 200000;
  88. spi_reset();
  89. fd = open("/dev/spidev0.0", O_RDWR);
  90. if (fd < 0) { perror("Unable to open SPI device"); exit(1); }
  91. if (ioctl(fd, SPI_IOC_WR_MODE, &mode) < 0) { perror("Unable to set WR MODE"); close(fd); return -1; }
  92. if (ioctl(fd, SPI_IOC_RD_MODE, &mode) < 0) { perror("Unable to set RD MODE"); close(fd); return -1; }
  93. if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) < 0) { perror("Unable to set WR_BITS_PER_WORD"); close(fd); return -1; }
  94. if (ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits) < 0) { perror("Unable to set RD_BITS_PER_WORD"); close(fd); return -1; }
  95. if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0) { perror("Unable to set WR_MAX_SPEED_HZ"); close(fd); return -1; }
  96. if (ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0) { perror("Unable to set RD_MAX_SPEED_HZ"); close(fd); return -1; }
  97. rv = 0;
  98. while (bufsz >= 4096) {
  99. tr[rv].tx_buf = (uintptr_t) wrbuf;
  100. tr[rv].rx_buf = (uintptr_t) rdbuf;
  101. tr[rv].len = 4096;
  102. tr[rv].delay_usecs = 1;
  103. tr[rv].speed_hz = speed;
  104. tr[rv].bits_per_word = bits;
  105. bufsz -= 4096;
  106. wrbuf += 4096; rdbuf += 4096; rv ++;
  107. }
  108. if (bufsz > 0) {
  109. tr[rv].tx_buf = (uintptr_t) wrbuf;
  110. tr[rv].rx_buf = (uintptr_t) rdbuf;
  111. tr[rv].len = (unsigned)bufsz;
  112. tr[rv].delay_usecs = 1;
  113. tr[rv].speed_hz = speed;
  114. tr[rv].bits_per_word = bits;
  115. rv ++;
  116. }
  117. i = rv;
  118. for (j = 0; j < i; j++) {
  119. rv = (int)ioctl(fd, SPI_IOC_MESSAGE(1), (intptr_t)&tr[j]);
  120. if (rv < 0) { perror("WTF!"); close(fd); return -1; }
  121. }
  122. close(fd);
  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_data(unsigned addr, const char *buf, unsigned len)
  161. {
  162. unsigned char otmp[3];
  163. if (len < 4 || len > 128) return; /* This cannot be programmed in single frame! */
  164. len /= 4; /* Strip */
  165. otmp[0] = (len - 1) | 0xE0;
  166. otmp[1] = (addr >> 8)&0xFF; otmp[2] = addr & 0xFF;
  167. spi_emit_buf(otmp, 3);
  168. spi_emit_buf_reverse(buf, len*4);
  169. }