wiring.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. #ifndef CS
  2. #if defined(ESP8266)
  3. #define CS D8
  4. #else
  5. #define CS 8
  6. #endif
  7. #endif
  8. #if defined(ESP8266)
  9. #define YIELD() yield()
  10. #else
  11. #define YIELD()
  12. #endif
  13. class GDTransport {
  14. private:
  15. byte model;
  16. public:
  17. void ios() {
  18. pinMode(CS, OUTPUT);
  19. digitalWrite(CS, HIGH);
  20. pinMode(SD_PIN, OUTPUT);
  21. digitalWrite(SD_PIN, HIGH);
  22. }
  23. void begin0() {
  24. ios();
  25. SPI.begin();
  26. #if defined(TEENSYDUINO) || defined(ARDUINO_ARCH_STM32L4)
  27. SPI.beginTransaction(SPISettings(3000000, MSBFIRST, SPI_MODE0));
  28. #else
  29. #if !defined(__DUE__) && !defined(ESP8266)
  30. SPI.setClockDivider(SPI_CLOCK_DIV2);
  31. SPSR = (1 << SPI2X);
  32. #endif
  33. #endif
  34. hostcmd(0x42); // SLEEP
  35. hostcmd(0x61); // CLKSEL default
  36. hostcmd(0x00); // ACTIVE
  37. #if (BOARD != BOARD_GAMEDUINO23)
  38. hostcmd(0x44); // CLKEXT
  39. #else
  40. hostcmd(0x48); // CLKINT
  41. #endif
  42. hostcmd(0x49); // PD_ROMS all up
  43. hostcmd(0x68); // RST_PULSE
  44. }
  45. void begin1() {
  46. #if 0
  47. delay(120);
  48. #else
  49. while ((__rd16(0xc0000UL) & 0xff) != 0x08)
  50. ;
  51. #endif
  52. // Test point: saturate SPI
  53. while (0) {
  54. digitalWrite(CS, LOW);
  55. SPI.transfer(0x55);
  56. digitalWrite(CS, HIGH);
  57. }
  58. #if 0
  59. // Test point: attempt to wake up FT8xx every 2 seconds
  60. while (0) {
  61. hostcmd(0x00);
  62. delay(120);
  63. hostcmd(0x68);
  64. delay(120);
  65. digitalWrite(CS, LOW);
  66. Serial.println(SPI.transfer(0x10), HEX);
  67. Serial.println(SPI.transfer(0x24), HEX);
  68. Serial.println(SPI.transfer(0x00), HEX);
  69. Serial.println(SPI.transfer(0xff), HEX);
  70. Serial.println(SPI.transfer(0x00), HEX);
  71. Serial.println(SPI.transfer(0x00), HEX);
  72. Serial.println();
  73. digitalWrite(CS, HIGH);
  74. delay(2000);
  75. }
  76. #endif
  77. // So that FT800,801 FT81x
  78. // model 0 1
  79. ft8xx_model = __rd16(0x0c0000) >> 12;
  80. wp = 0;
  81. freespace = 4096 - 4;
  82. stream();
  83. }
  84. void external_crystal() {
  85. __end();
  86. hostcmd(0x44);
  87. }
  88. void cmd32(uint32_t x) {
  89. if (freespace < 4) {
  90. getfree(4);
  91. }
  92. wp += 4;
  93. freespace -= 4;
  94. #if defined(ESP8266)
  95. // SPI.writeBytes((uint8_t*)&x, 4);
  96. SPI.write32(x, 0);
  97. #else
  98. union {
  99. uint32_t c;
  100. uint8_t b[4];
  101. };
  102. c = x;
  103. SPI.transfer(b[0]);
  104. SPI.transfer(b[1]);
  105. SPI.transfer(b[2]);
  106. SPI.transfer(b[3]);
  107. #endif
  108. }
  109. void cmdbyte(byte x) {
  110. if (freespace == 0) {
  111. getfree(1);
  112. }
  113. wp++;
  114. freespace--;
  115. SPI.transfer(x);
  116. }
  117. void cmd_n(byte *s, uint16_t n) {
  118. if (freespace < n) {
  119. getfree(n);
  120. }
  121. wp += n;
  122. freespace -= n;
  123. while (n > 8) {
  124. n -= 8;
  125. SPI.transfer(*s++);
  126. SPI.transfer(*s++);
  127. SPI.transfer(*s++);
  128. SPI.transfer(*s++);
  129. SPI.transfer(*s++);
  130. SPI.transfer(*s++);
  131. SPI.transfer(*s++);
  132. SPI.transfer(*s++);
  133. }
  134. while (n--)
  135. SPI.transfer(*s++);
  136. }
  137. void flush() {
  138. YIELD();
  139. getfree(0);
  140. }
  141. uint16_t rp() {
  142. uint16_t r = __rd16(REG_CMD_READ);
  143. if (r == 0xfff) {
  144. GD.alert("COPROCESSOR EXCEPTION");
  145. }
  146. return r;
  147. }
  148. void finish() {
  149. wp &= 0xffc;
  150. __end();
  151. __wr16(REG_CMD_WRITE, wp);
  152. while (rp() != wp)
  153. YIELD();
  154. stream();
  155. }
  156. byte rd(uint32_t addr)
  157. {
  158. __end(); // stop streaming
  159. __start(addr);
  160. SPI.transfer(0); // dummy
  161. byte r = SPI.transfer(0);
  162. stream();
  163. return r;
  164. }
  165. void wr(uint32_t addr, byte v)
  166. {
  167. __end(); // stop streaming
  168. __wstart(addr);
  169. SPI.transfer(v);
  170. stream();
  171. }
  172. uint16_t rd16(uint32_t addr)
  173. {
  174. uint16_t r = 0;
  175. __end(); // stop streaming
  176. __start(addr);
  177. SPI.transfer(0);
  178. r = SPI.transfer(0);
  179. r |= (SPI.transfer(0) << 8);
  180. stream();
  181. return r;
  182. }
  183. void wr16(uint32_t addr, uint32_t v)
  184. {
  185. __end(); // stop streaming
  186. __wstart(addr);
  187. SPI.transfer(v);
  188. SPI.transfer(v >> 8);
  189. stream();
  190. }
  191. uint32_t rd32(uint32_t addr)
  192. {
  193. __end(); // stop streaming
  194. __start(addr);
  195. SPI.transfer(0);
  196. union {
  197. uint32_t c;
  198. uint8_t b[4];
  199. };
  200. b[0] = SPI.transfer(0);
  201. b[1] = SPI.transfer(0);
  202. b[2] = SPI.transfer(0);
  203. b[3] = SPI.transfer(0);
  204. stream();
  205. return c;
  206. }
  207. void rd_n(byte *dst, uint32_t addr, uint16_t n)
  208. {
  209. __end(); // stop streaming
  210. __start(addr);
  211. SPI.transfer(0);
  212. while (n--)
  213. *dst++ = SPI.transfer(0);
  214. stream();
  215. }
  216. #if defined(ARDUINO) && !defined(__DUE__) && !defined(ESP8266) && !defined(ARDUINO_ARCH_STM32L4)
  217. void wr_n(uint32_t addr, byte *src, uint16_t n)
  218. {
  219. __end(); // stop streaming
  220. __wstart(addr);
  221. while (n--) {
  222. SPDR = *src++;
  223. asm volatile("nop");
  224. asm volatile("nop");
  225. asm volatile("nop");
  226. asm volatile("nop");
  227. asm volatile("nop");
  228. asm volatile("nop");
  229. asm volatile("nop");
  230. asm volatile("nop");
  231. asm volatile("nop");
  232. asm volatile("nop");
  233. }
  234. while (!(SPSR & _BV(SPIF))) ;
  235. stream();
  236. }
  237. #else
  238. void wr_n(uint32_t addr, byte *src, uint16_t n)
  239. {
  240. __end(); // stop streaming
  241. __wstart(addr);
  242. #if defined(ESP8266)
  243. SPI.writeBytes(src, n);
  244. #else
  245. while (n--)
  246. SPI.transfer(*src++);
  247. #endif
  248. stream();
  249. }
  250. #endif
  251. void wr32(uint32_t addr, unsigned long v)
  252. {
  253. __end(); // stop streaming
  254. __wstart(addr);
  255. SPI.transfer(v);
  256. SPI.transfer(v >> 8);
  257. SPI.transfer(v >> 16);
  258. SPI.transfer(v >> 24);
  259. stream();
  260. }
  261. uint32_t getwp(void) {
  262. return RAM_CMD + (wp & 0xffc);
  263. }
  264. void bulk(uint32_t addr) {
  265. __end(); // stop streaming
  266. __start(addr);
  267. }
  268. void resume(void) {
  269. stream();
  270. }
  271. static void __start(uint32_t addr) // start an SPI transaction to addr
  272. {
  273. digitalWrite(CS, LOW);
  274. SPI.transfer(addr >> 16);
  275. SPI.transfer(highByte(addr));
  276. SPI.transfer(lowByte(addr));
  277. }
  278. static void __wstart(uint32_t addr) // start an SPI write transaction to addr
  279. {
  280. digitalWrite(CS, LOW);
  281. SPI.transfer(0x80 | (addr >> 16));
  282. SPI.transfer(highByte(addr));
  283. SPI.transfer(lowByte(addr));
  284. }
  285. static void __end() // end the SPI transaction
  286. {
  287. digitalWrite(CS, HIGH);
  288. }
  289. void stop() // end the SPI transaction
  290. {
  291. wp &= 0xffc;
  292. __end();
  293. __wr16(REG_CMD_WRITE, wp);
  294. // while (__rd16(REG_CMD_READ) != wp) ;
  295. }
  296. void stream(void) {
  297. __end();
  298. __wstart(RAM_CMD + (wp & 0xfff));
  299. }
  300. static unsigned int __rd16(uint32_t addr)
  301. {
  302. unsigned int r;
  303. __start(addr);
  304. SPI.transfer(0); // dummy
  305. r = SPI.transfer(0);
  306. r |= (SPI.transfer(0) << 8);
  307. __end();
  308. return r;
  309. }
  310. static void __wr16(uint32_t addr, unsigned int v)
  311. {
  312. __wstart(addr);
  313. SPI.transfer(lowByte(v));
  314. SPI.transfer(highByte(v));
  315. __end();
  316. }
  317. static void hostcmd(byte a)
  318. {
  319. digitalWrite(CS, LOW);
  320. SPI.transfer(a);
  321. SPI.transfer(0x00);
  322. SPI.transfer(0x00);
  323. digitalWrite(CS, HIGH);
  324. }
  325. void getfree(uint16_t n)
  326. {
  327. wp &= 0xfff;
  328. __end();
  329. __wr16(REG_CMD_WRITE, wp & 0xffc);
  330. do {
  331. uint16_t fullness = (wp - rp()) & 4095;
  332. freespace = (4096 - 4) - fullness;
  333. } while (freespace < n);
  334. stream();
  335. }
  336. byte streaming;
  337. uint16_t wp;
  338. uint16_t freespace;
  339. };