tr-spidriver.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include "spidriver.h"
  2. class GDTransport {
  3. SPIDriver sd;
  4. uint16_t space, wp;
  5. uint8_t buf[64];
  6. size_t nbuf;
  7. public:
  8. void begin0(void) {
  9. spi_connect(&sd, "/dev/ttyUSB0");
  10. hostcmd(0x42); // SLEEP
  11. hostcmd(0x61); // CLKSEL default
  12. hostcmd(0x00); // ACTIVE
  13. hostcmd(0x48); // CLKINT
  14. hostcmd(0x49); // PD_ROMS all up
  15. hostcmd(0x68); // RST_PULSE
  16. ft8xx_model = 1;
  17. }
  18. void begin1(void) {
  19. while ((__rd16(0xc0000UL) & 0xff) != 0x08)
  20. ;
  21. wp = 0;
  22. stream();
  23. }
  24. void ios(void) {}
  25. void external_crystal() {
  26. __end();
  27. hostcmd(0x44);
  28. }
  29. void cmdbyte(char x) {
  30. if (nbuf == 64)
  31. flush();
  32. buf[nbuf++] = x;
  33. }
  34. void cmd32(uint32_t x) {
  35. if (nbuf == 64)
  36. flush();
  37. *(uint32_t*)&buf[nbuf] = x;
  38. nbuf += 4;
  39. }
  40. void cmd_n(byte *s, size_t n) {
  41. flush();
  42. getspace(n);
  43. spi_write(&sd, (char*)s, n);
  44. wp += n;
  45. }
  46. void hostcmd(uint8_t a)
  47. {
  48. char buf[3] = {(char)a, 0, 0};
  49. spi_sel(&sd);
  50. spi_write(&sd, buf, 3);
  51. spi_unsel(&sd);
  52. }
  53. uint16_t rd16(uint32_t a) {
  54. __end();
  55. uint16_t r = __rd16(a);
  56. stream();
  57. return r;
  58. }
  59. void wr16(uint32_t a, uint16_t v) {
  60. __end();
  61. __wr16(a, v);
  62. stream();
  63. }
  64. uint8_t rd(uint32_t a) {
  65. return rd16(a);
  66. }
  67. #define ADDR3(a) (char)((a >> 16) ), (char)(a >> 8), (char)(a)
  68. #define WADDR3(a) (char)((a >> 16) | 0x80), (char)(a >> 8), (char)(a)
  69. void wr(uint32_t a, byte v)
  70. {
  71. __end();
  72. char buf[4] = {WADDR3(a), (char)v};
  73. spi_sel(&sd);
  74. spi_write(&sd, buf, sizeof(buf));
  75. spi_unsel(&sd);
  76. stream();
  77. }
  78. void __end() {
  79. spi_unsel(&sd);
  80. }
  81. void stream(void) {
  82. space = __rd16(REG_CMDB_SPACE);
  83. assert((space & 3) == 0);
  84. __wstart(REG_CMDB_WRITE);
  85. }
  86. void resume(void) {
  87. stream();
  88. }
  89. void getspace(uint16_t n) {
  90. while (space < n) {
  91. __end();
  92. stream();
  93. }
  94. space -= n;
  95. }
  96. uint32_t getwp(void) {
  97. flush();
  98. return RAM_CMD + (wp & 0xffc);
  99. }
  100. uint32_t rd32(uint32_t a) {
  101. uint32_t r;
  102. rd_n((byte*)&r, a, 4);
  103. return r;
  104. }
  105. void rd_n(byte *dst, uint32_t a, uint16_t n) {
  106. __end();
  107. char buf[4] = {ADDR3(a)};
  108. spi_sel(&sd);
  109. spi_write(&sd, buf, sizeof(buf));
  110. spi_read(&sd, (char*)dst, n);
  111. spi_unsel(&sd);
  112. stream();
  113. }
  114. void wr32(uint32_t a, uint32_t v) {
  115. }
  116. void flush() {
  117. if (nbuf) {
  118. getspace(nbuf);
  119. spi_write(&sd, (char*)buf, nbuf);
  120. wp += nbuf;
  121. nbuf = 0;
  122. }
  123. }
  124. void finish() {
  125. flush();
  126. while (space < 4092) {
  127. __end();
  128. stream();
  129. }
  130. }
  131. void bulk(uint32_t addr) {}
  132. void __wstart(uint32_t a) {
  133. char buf[3] = {WADDR3(a)};
  134. spi_sel(&sd);
  135. spi_write(&sd, buf, sizeof(buf));
  136. }
  137. unsigned int __rd16(uint32_t a) {
  138. char buf[6] = {ADDR3(a), (char)-1, (char)-1, (char)-1};
  139. spi_sel(&sd);
  140. spi_writeread(&sd, buf, sizeof(buf));
  141. spi_unsel(&sd);
  142. return *(uint16_t*)&buf[4];
  143. }
  144. void __wr16(uint32_t a, unsigned int v) {
  145. char buf[5] = {WADDR3(a), (char)v, (char)(v >> 8)};
  146. spi_sel(&sd);
  147. spi_write(&sd, buf, sizeof(buf));
  148. spi_unsel(&sd);
  149. }
  150. void stop() {} // end the SPI transaction
  151. void wr_n(uint32_t addr, byte *src, uint16_t n) {
  152. assert(0);
  153. }
  154. };