Dcodes.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. #include "Dcodes.h"
  2. #include "Marlin.h"
  3. #include "cmdqueue.h"
  4. #include "pat9125.h"
  5. #include <avr/wdt.h>
  6. #define RAMSIZE 0x2000
  7. #define boot_src_addr (*((uint32_t*)(RAMSIZE - 16)))
  8. #define boot_dst_addr (*((uint32_t*)(RAMSIZE - 12)))
  9. #define boot_copy_size (*((uint16_t*)(RAMSIZE - 8)))
  10. #define boot_reserved (*((uint8_t*)(RAMSIZE - 6)))
  11. #define boot_app_flags (*((uint8_t*)(RAMSIZE - 5)))
  12. #define boot_app_magic (*((uint32_t*)(RAMSIZE - 4)))
  13. #define BOOT_APP_FLG_ERASE 0x01
  14. #define BOOT_APP_FLG_COPY 0x02
  15. #define BOOT_APP_FLG_FLASH 0x04
  16. inline void serial_print_hex_nibble(uint8_t val)
  17. {
  18. MYSERIAL.write((val > 9)?(val - 10 + 'a'):(val + '0'));
  19. }
  20. void serial_print_hex_byte(uint8_t val)
  21. {
  22. serial_print_hex_nibble(val >> 4);
  23. serial_print_hex_nibble(val & 15);
  24. }
  25. void serial_print_hex_word(uint16_t val)
  26. {
  27. serial_print_hex_byte(val >> 8);
  28. serial_print_hex_byte(val & 255);
  29. }
  30. int parse_hex(char* hex, uint8_t* data, int count)
  31. {
  32. int parsed = 0;
  33. while (*hex)
  34. {
  35. if (count && (parsed >= count)) break;
  36. char c = *(hex++);
  37. if (c == ' ') continue;
  38. if (c == '\n') break;
  39. uint8_t val = 0x00;
  40. if ((c >= '0') && (c <= '9')) val |= ((c - '0') << 4);
  41. else if ((c >= 'a') && (c <= 'f')) val |= ((c - 'a' + 10) << 4);
  42. else return -parsed;
  43. c = *(hex++);
  44. if ((c >= '0') && (c <= '9')) val |= (c - '0');
  45. else if ((c >= 'a') && (c <= 'f')) val |= (c - 'a' + 10);
  46. else return -parsed;
  47. data[parsed] = val;
  48. parsed++;
  49. }
  50. return parsed;
  51. }
  52. void dcode_0()
  53. {
  54. if (*(strchr_pointer + 1) == 0) return;
  55. MYSERIAL.println("D0 - Reset");
  56. if (code_seen('B')) //bootloader
  57. {
  58. cli();
  59. wdt_enable(WDTO_15MS);
  60. while(1);
  61. }
  62. else //reset
  63. {
  64. #ifndef _NO_ASM
  65. asm volatile("jmp 0x00000");
  66. #endif //_NO_ASM
  67. }
  68. }
  69. void dcode_1()
  70. {
  71. MYSERIAL.println("D1 - Clear EEPROM");
  72. cli();
  73. for (int i = 0; i < 4096; i++)
  74. eeprom_write_byte((unsigned char*)i, (unsigned char)0);
  75. sei();
  76. }
  77. void dcode_2()
  78. {
  79. MYSERIAL.println("D2 - Read/Write RAM");
  80. uint16_t address = 0x0000; //default 0x0000
  81. uint16_t count = 0x2000; //default 0x2000 (entire ram)
  82. if (code_seen('A')) // Address (0x0000-0x1fff)
  83. address = (strchr_pointer[1] == 'x')?strtol(strchr_pointer + 2, 0, 16):(int)code_value();
  84. if (code_seen('C')) // Count (0x0001-0x2000)
  85. count = (int)code_value();
  86. address &= 0x1fff;
  87. if (count > 0x2000) count = 0x2000;
  88. if ((address + count) > 0x2000) count = 0x2000 - address;
  89. if (code_seen('X')) // Data
  90. {
  91. uint8_t data[16];
  92. count = parse_hex(strchr_pointer + 1, data, 16);
  93. if (count > 0)
  94. {
  95. for (int i = 0; i < count; i++)
  96. *((uint8_t*)(address + i)) = data[i];
  97. MYSERIAL.print(count, DEC);
  98. MYSERIAL.println(" bytes written to RAM at address ");
  99. serial_print_hex_word(address);
  100. MYSERIAL.write('\n');
  101. }
  102. else
  103. count = 0;
  104. }
  105. while (count)
  106. {
  107. serial_print_hex_word(address);
  108. MYSERIAL.write(' ');
  109. uint8_t countperline = 16;
  110. while (count && countperline)
  111. {
  112. uint8_t data = *((uint8_t*)address++);
  113. MYSERIAL.write(' ');
  114. serial_print_hex_byte(data);
  115. countperline--;
  116. count--;
  117. }
  118. MYSERIAL.write('\n');
  119. }
  120. }
  121. void dcode_3()
  122. {
  123. MYSERIAL.println("D3 - Read/Write EEPROM");
  124. uint16_t address = 0x0000; //default 0x0000
  125. uint16_t count = 0x2000; //default 0x2000 (entire eeprom)
  126. if (code_seen('A')) // Address (0x0000-0x1fff)
  127. address = (strchr_pointer[1] == 'x')?strtol(strchr_pointer + 2, 0, 16):(int)code_value();
  128. if (code_seen('C')) // Count (0x0001-0x2000)
  129. count = (int)code_value();
  130. address &= 0x1fff;
  131. if (count > 0x2000) count = 0x2000;
  132. if ((address + count) > 0x2000) count = 0x2000 - address;
  133. if (code_seen('X')) // Data
  134. {
  135. uint8_t data[16];
  136. count = parse_hex(strchr_pointer + 1, data, 16);
  137. if (count > 0)
  138. {
  139. for (int i = 0; i < count; i++)
  140. eeprom_write_byte((uint8_t*)(address + i), data[i]);
  141. MYSERIAL.print(count, DEC);
  142. MYSERIAL.println(" bytes written to EEPROM at address ");
  143. serial_print_hex_word(address);
  144. MYSERIAL.write('\n');
  145. }
  146. else
  147. count = 0;
  148. }
  149. while (count)
  150. {
  151. serial_print_hex_word(address);
  152. MYSERIAL.write(' ');
  153. uint8_t countperline = 16;
  154. while (count && countperline)
  155. {
  156. uint8_t data = eeprom_read_byte((uint8_t*)address++);
  157. MYSERIAL.write(' ');
  158. serial_print_hex_byte(data);
  159. countperline--;
  160. count--;
  161. }
  162. MYSERIAL.write('\n');
  163. }
  164. }
  165. void dcode_4()
  166. {
  167. MYSERIAL.println("D4 - Read/Write PIN");
  168. if (code_seen('P')) // Pin (0-255)
  169. {
  170. int pin = (int)code_value();
  171. if ((pin >= 0) && (pin <= 255))
  172. {
  173. if (code_seen('F')) // Function in/out (0/1)
  174. {
  175. int fnc = (int)code_value();
  176. if (fnc == 0) pinMode(pin, INPUT);
  177. else if (fnc == 1) pinMode(pin, OUTPUT);
  178. }
  179. if (code_seen('V')) // Value (0/1)
  180. {
  181. int val = (int)code_value();
  182. if (val == 0) digitalWrite(pin, LOW);
  183. else if (val == 1) digitalWrite(pin, HIGH);
  184. }
  185. else
  186. {
  187. int val = (digitalRead(pin) != LOW)?1:0;
  188. MYSERIAL.print("PIN");
  189. MYSERIAL.print(pin);
  190. MYSERIAL.print("=");
  191. MYSERIAL.println(val);
  192. }
  193. }
  194. }
  195. }
  196. void dcode_5()
  197. {
  198. MYSERIAL.println("D5 - Read/Write FLASH");
  199. uint32_t address = 0x0000; //default 0x0000
  200. uint16_t count = 0x0400; //default 0x0400 (1kb block)
  201. if (code_seen('A')) // Address (0x00000-0x3ffff)
  202. address = (strchr_pointer[1] == 'x')?strtol(strchr_pointer + 2, 0, 16):(int)code_value();
  203. if (code_seen('C')) // Count (0x0001-0x2000)
  204. count = (int)code_value();
  205. address &= 0x3ffff;
  206. if (count > 0x2000) count = 0x2000;
  207. if ((address + count) > 0x40000) count = 0x40000 - address;
  208. bool bErase = false;
  209. bool bCopy = false;
  210. if (code_seen('E')) //Erase
  211. bErase = true;
  212. uint8_t data[16];
  213. if (code_seen('X')) // Data
  214. {
  215. count = parse_hex(strchr_pointer + 1, data, 16);
  216. if (count > 0) bCopy = true;
  217. }
  218. if (bErase || bCopy)
  219. {
  220. if (bErase)
  221. {
  222. MYSERIAL.print(count, DEC);
  223. MYSERIAL.println(" bytes of FLASH at address ");
  224. serial_print_hex_word(address);
  225. MYSERIAL.write(" will be erased\n");
  226. }
  227. if (bCopy)
  228. {
  229. MYSERIAL.print(count, DEC);
  230. MYSERIAL.println(" bytes will be written to FLASH at address ");
  231. serial_print_hex_word(address);
  232. MYSERIAL.write('\n');
  233. }
  234. cli();
  235. boot_app_magic = 0x55aa55aa;
  236. boot_app_flags = (bErase?(BOOT_APP_FLG_ERASE):0) | (bCopy?(BOOT_APP_FLG_COPY):0);
  237. boot_copy_size = (uint16_t)count;
  238. boot_dst_addr = (uint32_t)address;
  239. boot_src_addr = (uint32_t)(&data);
  240. wdt_enable(WDTO_15MS);
  241. while(1);
  242. }
  243. while (count)
  244. {
  245. serial_print_hex_nibble(address >> 16);
  246. serial_print_hex_word(address);
  247. MYSERIAL.write(' ');
  248. uint8_t countperline = 16;
  249. while (count && countperline)
  250. {
  251. uint8_t data = pgm_read_byte_far((uint8_t*)address++);
  252. MYSERIAL.write(' ');
  253. serial_print_hex_byte(data);
  254. countperline--;
  255. count--;
  256. }
  257. MYSERIAL.write('\n');
  258. }
  259. }
  260. void dcode_6()
  261. {
  262. cli();
  263. boot_app_magic = 0x55aa55aa;
  264. boot_app_flags = BOOT_APP_FLG_ERASE | BOOT_APP_FLG_COPY | BOOT_APP_FLG_FLASH;
  265. boot_copy_size = (uint16_t)0xc00;
  266. boot_src_addr = (uint32_t)0x0003e400;
  267. boot_dst_addr = (uint32_t)0x0003f400;
  268. wdt_enable(WDTO_15MS);
  269. while(1);
  270. /* MYSERIAL.println("D6 - Test");
  271. MYSERIAL.print("REGx90=0x");
  272. MYSERIAL.println(REGx90, HEX);
  273. REGx90 = 100;
  274. MYSERIAL.print("REGx90=0x");
  275. MYSERIAL.println(REGx90, HEX);*/
  276. }
  277. void dcode_7()
  278. {
  279. }
  280. void dcode_2130()
  281. {
  282. // printf("test");
  283. }
  284. void dcode_9125()
  285. {
  286. MYSERIAL.println("D9125 - PAT9125");
  287. if ((strchr_pointer[1+4] == '?') || (strchr_pointer[1+4] == 0))
  288. {
  289. MYSERIAL.print("res_x=");
  290. MYSERIAL.print(pat9125_xres, DEC);
  291. MYSERIAL.print(" res_y=");
  292. MYSERIAL.print(pat9125_yres, DEC);
  293. MYSERIAL.print(" x=");
  294. MYSERIAL.print(pat9125_x, DEC);
  295. MYSERIAL.print(" y=");
  296. MYSERIAL.print(pat9125_y, DEC);
  297. MYSERIAL.print(" b=");
  298. MYSERIAL.print(pat9125_b, DEC);
  299. MYSERIAL.print(" s=");
  300. MYSERIAL.println(pat9125_s, DEC);
  301. return;
  302. }
  303. if (strchr_pointer[1+4] == '!')
  304. {
  305. pat9125_update();
  306. MYSERIAL.print("x=");
  307. MYSERIAL.print(pat9125_x, DEC);
  308. MYSERIAL.print(" y=");
  309. MYSERIAL.print(pat9125_y, DEC);
  310. MYSERIAL.print(" b=");
  311. MYSERIAL.print(pat9125_b, DEC);
  312. MYSERIAL.print(" s=");
  313. MYSERIAL.println(pat9125_s, DEC);
  314. return;
  315. }
  316. if (code_seen('R'))
  317. {
  318. unsigned char res = (int)code_value();
  319. MYSERIAL.print("pat9125_init(xres=yres=");
  320. MYSERIAL.print(res, DEC);
  321. MYSERIAL.print(")=");
  322. MYSERIAL.println(pat9125_init(res, res), DEC);
  323. }
  324. if (code_seen('X'))
  325. {
  326. pat9125_x = (int)code_value();
  327. MYSERIAL.print("pat9125_x=");
  328. MYSERIAL.print(pat9125_x, DEC);
  329. }
  330. if (code_seen('Y'))
  331. {
  332. pat9125_y = (int)code_value();
  333. MYSERIAL.print("pat9125_y=");
  334. MYSERIAL.print(pat9125_y, DEC);
  335. }
  336. }