tr-spidriver.h 2.7 KB

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