spidevc.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright 2013 bitfury
  3. * Copyright 2013 Luke Dashjr
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #include "spidevc.h"
  24. #include <sys/mman.h>
  25. #include <stdbool.h>
  26. #include <stdint.h>
  27. #include <unistd.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <getopt.h>
  32. #include <fcntl.h>
  33. #include <sys/ioctl.h>
  34. #include <linux/types.h>
  35. #include <signal.h>
  36. #include <sys/types.h>
  37. #include <linux/spi/spidev.h>
  38. #include <time.h>
  39. #include <unistd.h>
  40. #include <linux/i2c.h>
  41. #include <linux/i2c-dev.h>
  42. #include <sys/stat.h>
  43. #include "logging.h"
  44. #define HAVE_LINUX_SPI
  45. #ifdef HAVE_LINUX_SPI
  46. bool sys_spi_txrx(struct spi_port *port);
  47. static volatile unsigned *gpio;
  48. #endif
  49. struct spi_port *sys_spi;
  50. void spi_init(void)
  51. {
  52. #ifdef HAVE_LINUX_SPI
  53. int fd;
  54. fd = open("/dev/mem",O_RDWR|O_SYNC);
  55. if (fd < 0)
  56. {
  57. perror("/dev/mem trouble");
  58. return;
  59. }
  60. gpio = mmap(0,4096,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0x20200000);
  61. if (gpio == MAP_FAILED)
  62. {
  63. perror("gpio mmap trouble");
  64. return;
  65. }
  66. close(fd);
  67. sys_spi = malloc(sizeof(*sys_spi));
  68. *sys_spi = (struct spi_port){
  69. .txrx = sys_spi_txrx,
  70. };
  71. #endif
  72. }
  73. #ifdef HAVE_LINUX_SPI
  74. #define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
  75. #define OUT_GPIO(g) *(gpio+((g)/10)) |= (1<<(((g)%10)*3))
  76. #define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
  77. #define GPIO_SET *(gpio+7) // sets bits which are 1 ignores bits which are 0
  78. #define GPIO_CLR *(gpio+10) // clears bits which are 1 ignores bits which are 0
  79. // Bit-banging reset, to reset more chips in chain - toggle for longer period... Each 3 reset cycles reset first chip in chain
  80. static
  81. int spi_reset(int a)
  82. {
  83. int i,j;
  84. int len = 8;
  85. INP_GPIO(10); OUT_GPIO(10);
  86. INP_GPIO(11); OUT_GPIO(11);
  87. GPIO_SET = 1 << 11; // Set SCK
  88. for (i = 0; i < 32; i++) { // On standard settings this unoptimized code produces 1 Mhz freq.
  89. GPIO_SET = 1 << 10;
  90. for (j = 0; j < len; j++) {
  91. a *= a;
  92. }
  93. GPIO_CLR = 1 << 10;
  94. for (j = 0; j < len; j++) {
  95. a *= a;
  96. }
  97. }
  98. GPIO_CLR = 1 << 10;
  99. GPIO_CLR = 1 << 11;
  100. INP_GPIO(10);
  101. SET_GPIO_ALT(10,0);
  102. INP_GPIO(11);
  103. SET_GPIO_ALT(11,0);
  104. INP_GPIO(9);
  105. SET_GPIO_ALT(9,0);
  106. return a;
  107. }
  108. #define BAILOUT(s) do{ \
  109. perror(s); \
  110. close(fd); \
  111. return false; \
  112. }while(0)
  113. bool sys_spi_txrx(struct spi_port *port)
  114. {
  115. const void *wrbuf = spi_gettxbuf(port);
  116. void *rdbuf = spi_getrxbuf(port);
  117. size_t bufsz = spi_getbufsz(port);
  118. int fd;
  119. int mode, bits, speed, rv, i, j;
  120. struct spi_ioc_transfer tr[16];
  121. memset(&tr,0,sizeof(tr));
  122. mode = 0; bits = 8; speed = 4000000;
  123. if (port->speed)
  124. speed = port->speed;
  125. spi_reset(1234);
  126. fd = open("/dev/spidev0.0", O_RDWR);
  127. if (fd < 0) {
  128. perror("Unable to open SPI device");
  129. return false;
  130. }
  131. if (ioctl(fd, SPI_IOC_WR_MODE, &mode) < 0)
  132. BAILOUT("Unable to set WR MODE");
  133. if (ioctl(fd, SPI_IOC_RD_MODE, &mode) < 0)
  134. BAILOUT("Unable to set RD MODE");
  135. if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) < 0)
  136. BAILOUT("Unable to set WR_BITS_PER_WORD");
  137. if (ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits) < 0)
  138. BAILOUT("Unable to set RD_BITS_PER_WORD");
  139. if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0)
  140. BAILOUT("Unable to set WR_MAX_SPEED_HZ");
  141. if (ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0)
  142. BAILOUT("Unable to set RD_MAX_SPEED_HZ");
  143. rv = 0;
  144. while (bufsz >= 4096) {
  145. tr[rv].tx_buf = (uintptr_t) wrbuf;
  146. tr[rv].rx_buf = (uintptr_t) rdbuf;
  147. tr[rv].len = 4096;
  148. tr[rv].delay_usecs = 1;
  149. tr[rv].speed_hz = speed;
  150. tr[rv].bits_per_word = bits;
  151. bufsz -= 4096;
  152. wrbuf += 4096; rdbuf += 4096; rv ++;
  153. }
  154. if (bufsz > 0) {
  155. tr[rv].tx_buf = (uintptr_t) wrbuf;
  156. tr[rv].rx_buf = (uintptr_t) rdbuf;
  157. tr[rv].len = (unsigned)bufsz;
  158. tr[rv].delay_usecs = 1;
  159. tr[rv].speed_hz = speed;
  160. tr[rv].bits_per_word = bits;
  161. rv ++;
  162. }
  163. i = rv;
  164. for (j = 0; j < i; j++) {
  165. rv = (int)ioctl(fd, SPI_IOC_MESSAGE(1), (intptr_t)&tr[j]);
  166. if (rv < 0)
  167. BAILOUT("WTF!");
  168. }
  169. close(fd);
  170. spi_reset(4321);
  171. return true;
  172. }
  173. #endif
  174. static
  175. void spi_emit_buf_reverse(struct spi_port *port, const void *p, size_t sz)
  176. {
  177. const unsigned char *str = p;
  178. if (port->spibufsz + sz >= SPIMAXSZ)
  179. return;
  180. for (size_t i = 0; i < sz; ++i)
  181. {
  182. // Reverse bit order in each byte!
  183. unsigned char p = str[i];
  184. p = ((p & 0xaa)>>1) | ((p & 0x55) << 1);
  185. p = ((p & 0xcc)>>2) | ((p & 0x33) << 2);
  186. p = ((p & 0xf0)>>4) | ((p & 0x0f) << 4);
  187. port->spibuf[port->spibufsz++] = p;
  188. }
  189. }
  190. static
  191. void spi_emit_buf(struct spi_port *port, void *str, size_t sz)
  192. {
  193. if (port->spibufsz + sz >= SPIMAXSZ)
  194. return;
  195. memcpy(&port->spibuf[port->spibufsz], str, sz);
  196. port->spibufsz += sz;
  197. }
  198. /* TODO: in production, emit just bit-sequences! Eliminate padding to byte! */
  199. void spi_emit_break(struct spi_port *port)
  200. {
  201. spi_emit_buf(port, "\x4", 1);
  202. }
  203. void spi_emit_fsync(struct spi_port *port)
  204. {
  205. spi_emit_buf(port, "\x6", 1);
  206. }
  207. void spi_emit_fasync(struct spi_port *port, int n)
  208. {
  209. int i;
  210. for (i = 0; i < n; i++) {
  211. spi_emit_buf(port, "\x5", 1);
  212. }
  213. }
  214. void spi_emit_nop(struct spi_port *port, int n) {
  215. int i;
  216. for (i = 0; i < n; n++) {
  217. spi_emit_buf(port, "\x0", 1);
  218. }
  219. }
  220. void spi_emit_data(struct spi_port *port, uint16_t addr, const void *buf, size_t len)
  221. {
  222. unsigned char otmp[3];
  223. if (len < 4 || len > 128) return; /* This cannot be programmed in single frame! */
  224. len /= 4; /* Strip */
  225. otmp[0] = (len - 1) | 0xE0;
  226. otmp[1] = (addr >> 8)&0xFF; otmp[2] = addr & 0xFF;
  227. spi_emit_buf(port, otmp, 3);
  228. spi_emit_buf_reverse(port, buf, len*4);
  229. }
  230. void spi_bfsb_select_bank(int bank)
  231. {
  232. static int last_bank = -2;
  233. if (bank == last_bank)
  234. return;
  235. const int banks[4]={18,23,24,25}; // GPIO connected to OE of level shifters
  236. int i;
  237. for(i=0;i<4;i++)
  238. {
  239. INP_GPIO(banks[i]);
  240. OUT_GPIO(banks[i]);
  241. if(i==bank)
  242. {
  243. GPIO_SET = 1 << banks[i]; // enable bank
  244. }
  245. else
  246. {
  247. GPIO_CLR = 1 << banks[i];// disable bank
  248. }
  249. }
  250. last_bank = bank;
  251. }