spidevc.c 6.9 KB

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