gd3load.ino 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include <EEPROM.h>
  2. #include <SPI.h>
  3. #include <GD2.h>
  4. #define SD_PIN 9
  5. #include "transports/wiring.h"
  6. #include "gd3load_assets.h"
  7. GDTransport GDTR;
  8. uint8_t gpio, gpio_dir;
  9. void set_SDA(byte n)
  10. {
  11. GDTR.__wr16(REG_GPIO_DIR, gpio_dir | (0x03 - n)); // Drive SCL, SDA low
  12. }
  13. void set_SCL(byte n)
  14. {
  15. GDTR.__wr16(REG_GPIO, gpio | (n << 1));
  16. }
  17. int get_SDA(void)
  18. {
  19. return GDTR.__rd16(REG_GPIO) & 1;
  20. }
  21. void i2c_start(void)
  22. {
  23. set_SDA(1);
  24. set_SCL(1);
  25. set_SDA(0);
  26. set_SCL(0);
  27. }
  28. void i2c_stop(void)
  29. {
  30. set_SDA(0);
  31. set_SCL(1);
  32. set_SDA(1);
  33. set_SCL(1);
  34. }
  35. int i2c_rx1()
  36. {
  37. set_SDA(1);
  38. set_SCL(1);
  39. byte r = get_SDA();
  40. set_SCL(0);
  41. return r;
  42. }
  43. void i2c_tx1(byte b)
  44. {
  45. set_SDA(b);
  46. set_SCL(1);
  47. set_SCL(0);
  48. }
  49. int i2c_tx(byte x)
  50. {
  51. for (int i = 7; i >= 0; i--)
  52. i2c_tx1(1 & (x >> i));
  53. return i2c_rx1();
  54. }
  55. int i2c_rx(int nak)
  56. {
  57. byte r = 0;
  58. for (byte i = 0; i < 8; i++)
  59. r = (r << 1) | i2c_rx1();
  60. i2c_tx1(nak);
  61. return r;
  62. }
  63. void i2c_begin(void)
  64. {
  65. gpio = GDTR.__rd16(REG_GPIO) & ~3;
  66. gpio_dir = GDTR.__rd16(REG_GPIO_DIR) & ~3;
  67. // 2-wire software reset
  68. i2c_start();
  69. i2c_rx(1);
  70. i2c_start();
  71. i2c_stop();
  72. }
  73. #define ADDR 0xa0
  74. void ram_write(const uint8_t *v)
  75. {
  76. for (byte i = 0; i < 128; i += 8) {
  77. i2c_start();
  78. i2c_tx(ADDR);
  79. i2c_tx(i);
  80. for (byte j = 0; j < 8; j++)
  81. i2c_tx(*v++);
  82. i2c_stop();
  83. delay(6);
  84. }
  85. }
  86. byte ram_read(byte a)
  87. {
  88. i2c_start();
  89. i2c_tx(ADDR);
  90. i2c_tx(a);
  91. i2c_start();
  92. i2c_tx(ADDR | 1);
  93. byte r = i2c_rx(1);
  94. i2c_stop();
  95. return r;
  96. }
  97. void ramdump(void)
  98. {
  99. for (int i = 0; i < 128; i++) {
  100. byte v = ram_read(i);
  101. Serial.print(i, HEX);
  102. Serial.print(" ");
  103. Serial.println(v, HEX);
  104. }
  105. }
  106. void ram_get(byte *v)
  107. {
  108. i2c_start();
  109. i2c_tx(ADDR);
  110. i2c_tx(0);
  111. i2c_start();
  112. i2c_tx(ADDR | 1);
  113. for (int i = 0; i < 128; i++) {
  114. *v++ = i2c_rx(i == 127);
  115. // Serial.println(v[-1], DEC);
  116. }
  117. i2c_stop();
  118. }
  119. static void load_flash(uint8_t *config)
  120. {
  121. byte b[128];
  122. i2c_begin();
  123. ram_write(config);
  124. ram_get(b);
  125. int diff = memcmp(config, b, 128);
  126. if (diff != 0) {
  127. Serial.println("Flash fault");
  128. GD.Clear();
  129. GD.cmd_text(GD.w / 2, GD.h / 2, 30, OPT_CENTERX, "Flash fault");
  130. GD.swap();
  131. for (;;);
  132. }
  133. Serial.println("Flash verified OK");
  134. GD.begin(0);
  135. }
  136. void setup()
  137. {
  138. Serial.begin(1000000);
  139. Serial.println(__LINE__);
  140. GDTR.begin0();
  141. GDTR.begin1();
  142. GDTR.__end();
  143. Serial.println(__LINE__);
  144. i2c_begin();
  145. ram_write(GD3_43__init);
  146. ramdump();
  147. }
  148. void loop()
  149. {
  150. }