spidev.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. class GDTransport {
  2. int fd;
  3. uint8_t writebuf[4096];
  4. size_t nwrite;
  5. public:
  6. void begin() {
  7. const char *DEV = "/dev/spidev0.0";
  8. fd = open(DEV, O_RDWR);
  9. if (fd <= 0) {
  10. perror(DEV);
  11. exit(1);
  12. }
  13. uint32_t speed = 500000;
  14. ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
  15. scu(0, 0, 0);
  16. #if PROTO == 0
  17. scu(0x44, 0, 0); // from external crystal
  18. #endif
  19. #if PROTO == 1
  20. scu(0x48, 0, 0); // from internal oscillator
  21. #endif
  22. scu(0x68, 0, 0);
  23. delay(50);
  24. wp = 0;
  25. freespace = 4096 - 4;
  26. stream();
  27. }
  28. static void default_spi_ioc(struct spi_ioc_transfer *ps) {
  29. memset(ps, 0, sizeof(*ps));
  30. ps->speed_hz = 20000000;
  31. ps->bits_per_word = 8;
  32. }
  33. // Raw read from GD2 memory
  34. void __read(void *dst, uint32_t addr, uint32_t n) {
  35. struct spi_ioc_transfer mesg[2];
  36. default_spi_ioc(&mesg[0]);
  37. default_spi_ioc(&mesg[1]);
  38. uint8_t tx[] = {(addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr >> 0) & 0xff, 0xff};
  39. mesg[0].tx_buf = (__u64)tx;
  40. mesg[0].len = 4;
  41. mesg[0].cs_change = 0;
  42. mesg[1].rx_buf = (__u64)dst;
  43. mesg[1].len = n;
  44. mesg[1].cs_change = 1;
  45. ioctl(fd, SPI_IOC_MESSAGE(2), mesg);
  46. // printf("__read(%x %d) %x\n", addr, n, *(byte*)dst);
  47. }
  48. // Raw write to GD2 memory
  49. void __write(uint32_t addr, void *src, uint32_t n) {
  50. // printf("__write(%x %d)\n", addr, n);
  51. fflush(stdout);
  52. struct spi_ioc_transfer mesg[2];
  53. default_spi_ioc(&mesg[0]);
  54. default_spi_ioc(&mesg[1]);
  55. uint8_t tx[] = {0x80 | ((addr >> 16) & 0xff), (addr >> 8) & 0xff, (addr >> 0) & 0xff};
  56. mesg[0].tx_buf = (__u64)tx;
  57. mesg[0].len = 3;
  58. mesg[0].cs_change = 0;
  59. mesg[1].tx_buf = (__u64)src;
  60. mesg[1].len = n;
  61. mesg[1].cs_change = 1;
  62. ioctl(fd, SPI_IOC_MESSAGE(2), mesg);
  63. }
  64. void spisend(uint8_t b) {
  65. writebuf[nwrite++] = b;
  66. }
  67. void cmd32(uint32_t x) {
  68. if (freespace < 4) {
  69. getfree(4);
  70. }
  71. wp += 4;
  72. freespace -= 4;
  73. union {
  74. uint32_t c;
  75. uint8_t b[4];
  76. };
  77. c = x;
  78. spisend(b[0]);
  79. spisend(b[1]);
  80. spisend(b[2]);
  81. spisend(b[3]);
  82. }
  83. void cmdbyte(byte x) {
  84. if (freespace == 0) {
  85. getfree(1);
  86. }
  87. wp++;
  88. freespace--;
  89. spisend(x);
  90. }
  91. void cmd_n(byte *s, uint16_t n) {
  92. if (freespace < n) {
  93. getfree(n);
  94. }
  95. wp += n;
  96. freespace -= n;
  97. while (n) {
  98. n --;
  99. spisend(*s++);
  100. }
  101. }
  102. void flush() {
  103. getfree(0);
  104. }
  105. uint16_t rp() {
  106. uint16_t r = __rd16(REG_CMD_READ);
  107. if (r == 0xfff) {
  108. REPORT(/*EXCEPTION*/r);
  109. for (;;) ;
  110. }
  111. return r;
  112. }
  113. void finish() {
  114. wp &= 0xffc;
  115. stopstream();
  116. __wr16(REG_CMD_WRITE, wp);
  117. while (rp() != wp)
  118. ;
  119. stream();
  120. }
  121. byte rd(uint32_t addr)
  122. {
  123. stopstream(); // stop streaming
  124. byte r;
  125. __read(&r, addr, 1);
  126. stream();
  127. return r;
  128. }
  129. void wr(uint32_t addr, byte v)
  130. {
  131. stopstream(); // stop streaming
  132. __write(addr, &v, 1);
  133. stream();
  134. }
  135. uint16_t rd16(uint32_t addr)
  136. {
  137. stopstream(); // stop streaming
  138. uint16_t r = 0;
  139. __read(&r, addr, 2);
  140. stream();
  141. return r;
  142. }
  143. void wr16(uint32_t addr, uint32_t v)
  144. {
  145. stopstream(); // stop streaming
  146. __write(addr, &v, 2);
  147. stream();
  148. }
  149. uint32_t rd32(uint32_t addr)
  150. {
  151. stopstream(); // stop streaming
  152. uint32_t r = 0;
  153. __read(&r, addr, 4);
  154. stream();
  155. return r;
  156. }
  157. void rd_n(byte *dst, uint32_t addr, uint32_t n)
  158. {
  159. stopstream(); // stop streaming
  160. __read(dst, addr, n);
  161. stream();
  162. }
  163. void wr_n(uint32_t addr, byte *src, uint32_t n)
  164. {
  165. stopstream(); // stop streaming
  166. __write(addr, src, n);
  167. stream();
  168. }
  169. void wr32(uint32_t addr, unsigned long v)
  170. {
  171. stopstream(); // stop streaming
  172. __write(addr, &v, 4);
  173. stream();
  174. }
  175. uint32_t getwp(void) {
  176. return RAM_CMD + (wp & 0xffc);
  177. }
  178. void bulk(uint32_t addr) {
  179. /*
  180. __end(); // stop streaming
  181. __start(addr);
  182. */
  183. }
  184. void resume(void) {
  185. stream();
  186. }
  187. void stopstream() // flush any streamed output
  188. {
  189. if (nwrite != 0) {
  190. __write(wptr0, writebuf, nwrite);
  191. nwrite = 0;
  192. }
  193. }
  194. void stop() // end the SPI transaction
  195. {
  196. wp &= 0xffc;
  197. stopstream();
  198. __wr16(REG_CMD_WRITE, wp);
  199. // while (__rd16(REG_CMD_READ) != wp) ;
  200. }
  201. void __end() // end the SPI transaction
  202. {
  203. stopstream();
  204. }
  205. void stream(void) {
  206. nwrite = 0;
  207. wptr0 = RAM_CMD + wp;
  208. }
  209. uint16_t __rd16(uint32_t addr)
  210. {
  211. uint16_t r;
  212. __read(&r, addr, sizeof(r));
  213. return r;
  214. }
  215. void __wr16(uint32_t addr, uint16_t v)
  216. {
  217. __write(addr, &v, sizeof(v));
  218. }
  219. void scu(byte a, byte b, byte c)
  220. {
  221. uint8_t scu0[3] = { a, 0x00, 0x00 };
  222. write(fd, scu0, 3);
  223. delay(4);
  224. }
  225. void getfree(uint16_t n)
  226. {
  227. wp &= 0xfff;
  228. stopstream();
  229. __wr16(REG_CMD_WRITE, wp & 0xffc);
  230. do {
  231. uint16_t fullness = (wp - rp()) & 4095;
  232. freespace = (4096 - 4) - fullness;
  233. } while (freespace < n);
  234. stream();
  235. }
  236. byte streaming;
  237. uint16_t wp;
  238. uint16_t freespace;
  239. uint32_t wptr0;
  240. };