spidevc.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 "config.h"
  24. #ifdef HAVE_LINUX_SPI_SPIDEV_H
  25. #define HAVE_LINUX_SPI
  26. #endif
  27. #include "spidevc.h"
  28. #include <stdbool.h>
  29. #include <stdint.h>
  30. #include <unistd.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <getopt.h>
  35. #ifdef HAVE_LINUX_SPI
  36. #include <sys/mman.h>
  37. #include <fcntl.h>
  38. #include <sys/ioctl.h>
  39. #include <linux/types.h>
  40. #include <signal.h>
  41. #include <sys/types.h>
  42. #include <linux/spi/spidev.h>
  43. #include <time.h>
  44. #include <unistd.h>
  45. #include <linux/i2c.h>
  46. #include <linux/i2c-dev.h>
  47. #include <sys/stat.h>
  48. #endif
  49. #include "logging.h"
  50. #ifdef HAVE_LINUX_SPI
  51. bool sys_spi_txrx(struct spi_port *port);
  52. static volatile unsigned *gpio;
  53. #endif
  54. struct spi_port *sys_spi;
  55. void spi_init(void)
  56. {
  57. #ifdef HAVE_LINUX_SPI
  58. int fd;
  59. fd = open("/dev/mem",O_RDWR|O_SYNC);
  60. if (fd < 0)
  61. {
  62. perror("/dev/mem trouble");
  63. return;
  64. }
  65. gpio = mmap(0,4096,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0x20200000);
  66. if (gpio == MAP_FAILED)
  67. {
  68. perror("gpio mmap trouble");
  69. return;
  70. }
  71. close(fd);
  72. sys_spi = malloc(sizeof(*sys_spi));
  73. *sys_spi = (struct spi_port){
  74. .txrx = sys_spi_txrx,
  75. };
  76. #endif
  77. }
  78. #ifdef HAVE_LINUX_SPI
  79. #define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
  80. #define OUT_GPIO(g) *(gpio+((g)/10)) |= (1<<(((g)%10)*3))
  81. #define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
  82. #define GPIO_SET *(gpio+7) // sets bits which are 1 ignores bits which are 0
  83. #define GPIO_CLR *(gpio+10) // clears bits which are 1 ignores bits which are 0
  84. // Bit-banging reset, to reset more chips in chain - toggle for longer period... Each 3 reset cycles reset first chip in chain
  85. static
  86. int spi_reset(int a)
  87. {
  88. int i,j;
  89. int len = 8;
  90. INP_GPIO(10); OUT_GPIO(10);
  91. INP_GPIO(11); OUT_GPIO(11);
  92. GPIO_SET = 1 << 11; // Set SCK
  93. for (i = 0; i < 32; i++) { // On standard settings this unoptimized code produces 1 Mhz freq.
  94. GPIO_SET = 1 << 10;
  95. for (j = 0; j < len; j++) {
  96. a *= a;
  97. }
  98. GPIO_CLR = 1 << 10;
  99. for (j = 0; j < len; j++) {
  100. a *= a;
  101. }
  102. }
  103. GPIO_CLR = 1 << 10;
  104. GPIO_CLR = 1 << 11;
  105. INP_GPIO(10);
  106. SET_GPIO_ALT(10,0);
  107. INP_GPIO(11);
  108. SET_GPIO_ALT(11,0);
  109. INP_GPIO(9);
  110. SET_GPIO_ALT(9,0);
  111. return a;
  112. }
  113. #define BAILOUT(s) do{ \
  114. perror(s); \
  115. close(fd); \
  116. return false; \
  117. }while(0)
  118. bool sys_spi_txrx(struct spi_port *port)
  119. {
  120. const void *wrbuf = spi_gettxbuf(port);
  121. void *rdbuf = spi_getrxbuf(port);
  122. size_t bufsz = spi_getbufsz(port);
  123. int fd;
  124. int mode, bits, speed, rv, i, j;
  125. struct spi_ioc_transfer tr[16];
  126. memset(&tr,0,sizeof(tr));
  127. mode = 0; bits = 8; speed = 4000000;
  128. if (port->speed)
  129. speed = port->speed;
  130. spi_reset(1234);
  131. fd = open("/dev/spidev0.0", O_RDWR);
  132. if (fd < 0) {
  133. perror("Unable to open SPI device");
  134. return false;
  135. }
  136. if (ioctl(fd, SPI_IOC_WR_MODE, &mode) < 0)
  137. BAILOUT("Unable to set WR MODE");
  138. if (ioctl(fd, SPI_IOC_RD_MODE, &mode) < 0)
  139. BAILOUT("Unable to set RD MODE");
  140. if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) < 0)
  141. BAILOUT("Unable to set WR_BITS_PER_WORD");
  142. if (ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits) < 0)
  143. BAILOUT("Unable to set RD_BITS_PER_WORD");
  144. if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0)
  145. BAILOUT("Unable to set WR_MAX_SPEED_HZ");
  146. if (ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0)
  147. BAILOUT("Unable to set RD_MAX_SPEED_HZ");
  148. rv = 0;
  149. while (bufsz >= 4096) {
  150. tr[rv].tx_buf = (uintptr_t) wrbuf;
  151. tr[rv].rx_buf = (uintptr_t) rdbuf;
  152. tr[rv].len = 4096;
  153. tr[rv].delay_usecs = 1;
  154. tr[rv].speed_hz = speed;
  155. tr[rv].bits_per_word = bits;
  156. bufsz -= 4096;
  157. wrbuf += 4096; rdbuf += 4096; rv ++;
  158. }
  159. if (bufsz > 0) {
  160. tr[rv].tx_buf = (uintptr_t) wrbuf;
  161. tr[rv].rx_buf = (uintptr_t) rdbuf;
  162. tr[rv].len = (unsigned)bufsz;
  163. tr[rv].delay_usecs = 1;
  164. tr[rv].speed_hz = speed;
  165. tr[rv].bits_per_word = bits;
  166. rv ++;
  167. }
  168. i = rv;
  169. for (j = 0; j < i; j++) {
  170. rv = (int)ioctl(fd, SPI_IOC_MESSAGE(1), (intptr_t)&tr[j]);
  171. if (rv < 0)
  172. BAILOUT("WTF!");
  173. }
  174. close(fd);
  175. spi_reset(4321);
  176. return true;
  177. }
  178. #endif
  179. static
  180. void *spi_emit_buf_reverse(struct spi_port *port, const void *p, size_t sz)
  181. {
  182. const unsigned char *str = p;
  183. void * const rv = &port->spibuf_rx[port->spibufsz];
  184. if (port->spibufsz + sz >= SPIMAXSZ)
  185. return NULL;
  186. for (size_t i = 0; i < sz; ++i)
  187. {
  188. // Reverse bit order in each byte!
  189. unsigned char p = str[i];
  190. p = ((p & 0xaa)>>1) | ((p & 0x55) << 1);
  191. p = ((p & 0xcc)>>2) | ((p & 0x33) << 2);
  192. p = ((p & 0xf0)>>4) | ((p & 0x0f) << 4);
  193. port->spibuf[port->spibufsz++] = p;
  194. }
  195. return rv;
  196. }
  197. static
  198. void spi_emit_buf(struct spi_port *port, void *str, size_t sz)
  199. {
  200. if (port->spibufsz + sz >= SPIMAXSZ)
  201. return;
  202. memcpy(&port->spibuf[port->spibufsz], str, sz);
  203. port->spibufsz += sz;
  204. }
  205. /* TODO: in production, emit just bit-sequences! Eliminate padding to byte! */
  206. void spi_emit_break(struct spi_port *port)
  207. {
  208. spi_emit_buf(port, "\x4", 1);
  209. }
  210. void spi_emit_fsync(struct spi_port *port)
  211. {
  212. spi_emit_buf(port, "\x6", 1);
  213. }
  214. void spi_emit_fasync(struct spi_port *port, int n)
  215. {
  216. int i;
  217. for (i = 0; i < n; i++) {
  218. spi_emit_buf(port, "\x5", 1);
  219. }
  220. }
  221. void spi_emit_nop(struct spi_port *port, int n) {
  222. int i;
  223. for (i = 0; i < n; n++) {
  224. spi_emit_buf(port, "\x0", 1);
  225. }
  226. }
  227. void *spi_emit_data(struct spi_port *port, uint16_t addr, const void *buf, size_t len)
  228. {
  229. unsigned char otmp[3];
  230. if (len < 4 || len > 128)
  231. return NULL; /* This cannot be programmed in single frame! */
  232. len /= 4; /* Strip */
  233. otmp[0] = (len - 1) | 0xE0;
  234. otmp[1] = (addr >> 8)&0xFF; otmp[2] = addr & 0xFF;
  235. spi_emit_buf(port, otmp, 3);
  236. return spi_emit_buf_reverse(port, buf, len*4);
  237. }
  238. #ifdef HAVE_LINUX_SPI
  239. void spi_bfsb_select_bank(int bank)
  240. {
  241. static int last_bank = -2;
  242. if (bank == last_bank)
  243. return;
  244. const int banks[4]={18,23,24,25}; // GPIO connected to OE of level shifters
  245. int i;
  246. for(i=0;i<4;i++)
  247. {
  248. INP_GPIO(banks[i]);
  249. OUT_GPIO(banks[i]);
  250. if(i==bank)
  251. {
  252. GPIO_SET = 1 << banks[i]; // enable bank
  253. }
  254. else
  255. {
  256. GPIO_CLR = 1 << banks[i];// disable bank
  257. }
  258. }
  259. last_bank = bank;
  260. }
  261. #endif