Dcodes.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. #include "Dcodes.h"
  2. #include "Marlin.h"
  3. #
  4. #include "ConfigurationStore.h"
  5. #include "cmdqueue.h"
  6. #include "pat9125.h"
  7. #include <avr/wdt.h>
  8. #define RAMSIZE 0x2000
  9. #define boot_src_addr (*((uint32_t*)(RAMSIZE - 16)))
  10. #define boot_dst_addr (*((uint32_t*)(RAMSIZE - 12)))
  11. #define boot_copy_size (*((uint16_t*)(RAMSIZE - 8)))
  12. #define boot_reserved (*((uint8_t*)(RAMSIZE - 6)))
  13. #define boot_app_flags (*((uint8_t*)(RAMSIZE - 5)))
  14. #define boot_app_magic (*((uint32_t*)(RAMSIZE - 4)))
  15. #define BOOT_APP_FLG_ERASE 0x01
  16. #define BOOT_APP_FLG_COPY 0x02
  17. #define BOOT_APP_FLG_FLASH 0x04
  18. extern uint8_t fsensor_log;
  19. extern float current_temperature_pinda;
  20. extern float axis_steps_per_unit[NUM_AXIS];
  21. inline void serial_print_hex_nibble(uint8_t val)
  22. {
  23. MYSERIAL.write((val > 9)?(val - 10 + 'a'):(val + '0'));
  24. }
  25. void serial_print_hex_byte(uint8_t val)
  26. {
  27. serial_print_hex_nibble(val >> 4);
  28. serial_print_hex_nibble(val & 15);
  29. }
  30. void serial_print_hex_word(uint16_t val)
  31. {
  32. serial_print_hex_byte(val >> 8);
  33. serial_print_hex_byte(val & 255);
  34. }
  35. int parse_hex(char* hex, uint8_t* data, int count)
  36. {
  37. int parsed = 0;
  38. while (*hex)
  39. {
  40. if (count && (parsed >= count)) break;
  41. char c = *(hex++);
  42. if (c == ' ') continue;
  43. if (c == '\n') break;
  44. uint8_t val = 0x00;
  45. if ((c >= '0') && (c <= '9')) val |= ((c - '0') << 4);
  46. else if ((c >= 'a') && (c <= 'f')) val |= ((c - 'a' + 10) << 4);
  47. else return -parsed;
  48. c = *(hex++);
  49. if ((c >= '0') && (c <= '9')) val |= (c - '0');
  50. else if ((c >= 'a') && (c <= 'f')) val |= (c - 'a' + 10);
  51. else return -parsed;
  52. data[parsed] = val;
  53. parsed++;
  54. }
  55. return parsed;
  56. }
  57. void dcode_0()
  58. {
  59. if (*(strchr_pointer + 1) == 0) return;
  60. MYSERIAL.println("D0 - Reset");
  61. if (code_seen('B')) //bootloader
  62. {
  63. cli();
  64. wdt_enable(WDTO_15MS);
  65. while(1);
  66. }
  67. else //reset
  68. {
  69. #ifndef _NO_ASM
  70. asm volatile("jmp 0x00000");
  71. #endif //_NO_ASM
  72. }
  73. }
  74. void dcode_1()
  75. {
  76. MYSERIAL.println("D1 - Clear EEPROM and RESET");
  77. cli();
  78. for (int i = 0; i < 8192; i++)
  79. eeprom_write_byte((unsigned char*)i, (unsigned char)0xff);
  80. wdt_enable(WDTO_15MS);
  81. while(1);
  82. }
  83. void dcode_2()
  84. {
  85. MYSERIAL.println("D2 - Read/Write RAM");
  86. uint16_t address = 0x0000; //default 0x0000
  87. uint16_t count = 0x2000; //default 0x2000 (entire ram)
  88. if (code_seen('A')) // Address (0x0000-0x1fff)
  89. address = (strchr_pointer[1] == 'x')?strtol(strchr_pointer + 2, 0, 16):(int)code_value();
  90. if (code_seen('C')) // Count (0x0001-0x2000)
  91. count = (int)code_value();
  92. address &= 0x1fff;
  93. if (count > 0x2000) count = 0x2000;
  94. if ((address + count) > 0x2000) count = 0x2000 - address;
  95. if (code_seen('X')) // Data
  96. {
  97. uint8_t data[16];
  98. count = parse_hex(strchr_pointer + 1, data, 16);
  99. if (count > 0)
  100. {
  101. for (int i = 0; i < count; i++)
  102. *((uint8_t*)(address + i)) = data[i];
  103. MYSERIAL.print(count, DEC);
  104. MYSERIAL.println(" bytes written to RAM at address ");
  105. serial_print_hex_word(address);
  106. MYSERIAL.write('\n');
  107. }
  108. else
  109. count = 0;
  110. }
  111. while (count)
  112. {
  113. serial_print_hex_word(address);
  114. MYSERIAL.write(' ');
  115. uint8_t countperline = 16;
  116. while (count && countperline)
  117. {
  118. uint8_t data = *((uint8_t*)address++);
  119. MYSERIAL.write(' ');
  120. serial_print_hex_byte(data);
  121. countperline--;
  122. count--;
  123. }
  124. MYSERIAL.write('\n');
  125. }
  126. }
  127. void dcode_3()
  128. {
  129. MYSERIAL.println("D3 - Read/Write EEPROM");
  130. uint16_t address = 0x0000; //default 0x0000
  131. uint16_t count = 0x2000; //default 0x2000 (entire eeprom)
  132. if (code_seen('A')) // Address (0x0000-0x1fff)
  133. address = (strchr_pointer[1] == 'x')?strtol(strchr_pointer + 2, 0, 16):(int)code_value();
  134. if (code_seen('C')) // Count (0x0001-0x2000)
  135. count = (int)code_value();
  136. address &= 0x1fff;
  137. if (count > 0x2000) count = 0x2000;
  138. if ((address + count) > 0x2000) count = 0x2000 - address;
  139. if (code_seen('X')) // Data
  140. {
  141. uint8_t data[16];
  142. count = parse_hex(strchr_pointer + 1, data, 16);
  143. if (count > 0)
  144. {
  145. for (int i = 0; i < count; i++)
  146. eeprom_write_byte((uint8_t*)(address + i), data[i]);
  147. MYSERIAL.print(count, DEC);
  148. MYSERIAL.println(" bytes written to EEPROM at address ");
  149. serial_print_hex_word(address);
  150. MYSERIAL.write('\n');
  151. }
  152. else
  153. count = 0;
  154. }
  155. while (count)
  156. {
  157. serial_print_hex_word(address);
  158. MYSERIAL.write(' ');
  159. uint8_t countperline = 16;
  160. while (count && countperline)
  161. {
  162. uint8_t data = eeprom_read_byte((uint8_t*)address++);
  163. MYSERIAL.write(' ');
  164. serial_print_hex_byte(data);
  165. countperline--;
  166. count--;
  167. }
  168. MYSERIAL.write('\n');
  169. }
  170. }
  171. void dcode_4()
  172. {
  173. MYSERIAL.println("D4 - Read/Write PIN");
  174. if (code_seen('P')) // Pin (0-255)
  175. {
  176. int pin = (int)code_value();
  177. if ((pin >= 0) && (pin <= 255))
  178. {
  179. if (code_seen('F')) // Function in/out (0/1)
  180. {
  181. int fnc = (int)code_value();
  182. if (fnc == 0) pinMode(pin, INPUT);
  183. else if (fnc == 1) pinMode(pin, OUTPUT);
  184. }
  185. if (code_seen('V')) // Value (0/1)
  186. {
  187. int val = (int)code_value();
  188. if (val == 0) digitalWrite(pin, LOW);
  189. else if (val == 1) digitalWrite(pin, HIGH);
  190. }
  191. else
  192. {
  193. int val = (digitalRead(pin) != LOW)?1:0;
  194. MYSERIAL.print("PIN");
  195. MYSERIAL.print(pin);
  196. MYSERIAL.print("=");
  197. MYSERIAL.println(val);
  198. }
  199. }
  200. }
  201. }
  202. void dcode_5()
  203. {
  204. MYSERIAL.println("D5 - Read/Write FLASH");
  205. uint32_t address = 0x0000; //default 0x0000
  206. uint16_t count = 0x0400; //default 0x0400 (1kb block)
  207. if (code_seen('A')) // Address (0x00000-0x3ffff)
  208. address = (strchr_pointer[1] == 'x')?strtol(strchr_pointer + 2, 0, 16):(int)code_value();
  209. if (code_seen('C')) // Count (0x0001-0x2000)
  210. count = (int)code_value();
  211. address &= 0x3ffff;
  212. if (count > 0x2000) count = 0x2000;
  213. if ((address + count) > 0x40000) count = 0x40000 - address;
  214. bool bErase = false;
  215. bool bCopy = false;
  216. if (code_seen('E')) //Erase
  217. bErase = true;
  218. uint8_t data[16];
  219. if (code_seen('X')) // Data
  220. {
  221. count = parse_hex(strchr_pointer + 1, data, 16);
  222. if (count > 0) bCopy = true;
  223. }
  224. if (bErase || bCopy)
  225. {
  226. if (bErase)
  227. {
  228. MYSERIAL.print(count, DEC);
  229. MYSERIAL.println(" bytes of FLASH at address ");
  230. serial_print_hex_word(address);
  231. MYSERIAL.write(" will be erased\n");
  232. }
  233. if (bCopy)
  234. {
  235. MYSERIAL.print(count, DEC);
  236. MYSERIAL.println(" bytes will be written to FLASH at address ");
  237. serial_print_hex_word(address);
  238. MYSERIAL.write('\n');
  239. }
  240. cli();
  241. boot_app_magic = 0x55aa55aa;
  242. boot_app_flags = (bErase?(BOOT_APP_FLG_ERASE):0) | (bCopy?(BOOT_APP_FLG_COPY):0);
  243. boot_copy_size = (uint16_t)count;
  244. boot_dst_addr = (uint32_t)address;
  245. boot_src_addr = (uint32_t)(&data);
  246. wdt_enable(WDTO_15MS);
  247. while(1);
  248. }
  249. while (count)
  250. {
  251. serial_print_hex_nibble(address >> 16);
  252. serial_print_hex_word(address);
  253. MYSERIAL.write(' ');
  254. uint8_t countperline = 16;
  255. while (count && countperline)
  256. {
  257. uint8_t data = pgm_read_byte_far((uint8_t*)address++);
  258. MYSERIAL.write(' ');
  259. serial_print_hex_byte(data);
  260. countperline--;
  261. count--;
  262. }
  263. MYSERIAL.write('\n');
  264. }
  265. }
  266. void dcode_6()
  267. {
  268. MYSERIAL.println("D6 - Read/Write external FLASH");
  269. }
  270. void dcode_7()
  271. {
  272. MYSERIAL.println("D7 - Read/Write Bootloader");
  273. /*
  274. cli();
  275. boot_app_magic = 0x55aa55aa;
  276. boot_app_flags = BOOT_APP_FLG_ERASE | BOOT_APP_FLG_COPY | BOOT_APP_FLG_FLASH;
  277. boot_copy_size = (uint16_t)0xc00;
  278. boot_src_addr = (uint32_t)0x0003e400;
  279. boot_dst_addr = (uint32_t)0x0003f400;
  280. wdt_enable(WDTO_15MS);
  281. while(1);
  282. */
  283. }
  284. void dcode_8()
  285. {
  286. MYSERIAL.println("D8 - Read/Write PINDA");
  287. uint8_t cal_status = calibration_status_pinda();
  288. float temp_pinda = current_temperature_pinda;
  289. float offset_z = temp_compensation_pinda_thermistor_offset(temp_pinda);
  290. if ((strchr_pointer[1+1] == '?') || (strchr_pointer[1+1] == 0))
  291. {
  292. MYSERIAL.print("cal_status=");
  293. MYSERIAL.println(cal_status?"1":"0");
  294. for (uint8_t i = 0; i < 6; i++)
  295. {
  296. MYSERIAL.print("temp_pinda=");
  297. MYSERIAL.print(35 + i * 5, DEC);
  298. MYSERIAL.print("C, temp_shift=");
  299. uint16_t offs = 0;
  300. if (i > 0) offs = eeprom_read_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + (i - 1));
  301. MYSERIAL.print(((float)offs) / axis_steps_per_unit[Z_AXIS], 3);
  302. MYSERIAL.println("mm");
  303. }
  304. }
  305. else if (strchr_pointer[1+1] == '!')
  306. {
  307. cal_status = 1;
  308. eeprom_write_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_PINDA, cal_status);
  309. eeprom_write_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + 0, 50); //40C -
  310. eeprom_write_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + 1, 100); //45C -
  311. eeprom_write_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + 2, 150); //50C -
  312. eeprom_write_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + 3, 200); //55C -
  313. eeprom_write_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + 4, 250); //60C -
  314. }
  315. else
  316. {
  317. if (code_seen('P')) // Pinda temperature [C]
  318. temp_pinda = code_value();
  319. offset_z = temp_compensation_pinda_thermistor_offset(temp_pinda);
  320. if (code_seen('Z')) // Z Offset [mm]
  321. {
  322. offset_z = code_value();
  323. }
  324. }
  325. MYSERIAL.print("temp_pinda=");
  326. MYSERIAL.println(temp_pinda);
  327. MYSERIAL.print("offset_z=");
  328. MYSERIAL.println(offset_z, 3);
  329. }
  330. void dcode_10()
  331. {//Tell the printer that XYZ calibration went OK
  332. MYSERIAL.println("D10 - XYZ calibration = OK");
  333. calibration_status_store(CALIBRATION_STATUS_LIVE_ADJUST);
  334. }
  335. void dcode_12()
  336. {//Reset Filament error, Power loss and crash counter ( Do it before every print and you can get stats for the print )
  337. MYSERIAL.println("D12 - Reset failstat counters");
  338. eeprom_update_byte((uint8_t*)EEPROM_CRASH_COUNT, 0x00);
  339. eeprom_update_byte((uint8_t*)EEPROM_FERROR_COUNT, 0x00);
  340. eeprom_update_byte((uint8_t*)EEPROM_POWER_COUNT, 0x00);
  341. }
  342. void dcode_2130()
  343. {
  344. // printf("test");
  345. }
  346. void dcode_9125()
  347. {
  348. MYSERIAL.println("D9125 - PAT9125");
  349. if ((strchr_pointer[1+4] == '?') || (strchr_pointer[1+4] == 0))
  350. {
  351. MYSERIAL.print("res_x=");
  352. MYSERIAL.print(pat9125_xres, DEC);
  353. MYSERIAL.print(" res_y=");
  354. MYSERIAL.print(pat9125_yres, DEC);
  355. MYSERIAL.print(" x=");
  356. MYSERIAL.print(pat9125_x, DEC);
  357. MYSERIAL.print(" y=");
  358. MYSERIAL.print(pat9125_y, DEC);
  359. MYSERIAL.print(" b=");
  360. MYSERIAL.print(pat9125_b, DEC);
  361. MYSERIAL.print(" s=");
  362. MYSERIAL.println(pat9125_s, DEC);
  363. return;
  364. }
  365. if (strchr_pointer[1+4] == '!')
  366. {
  367. pat9125_update();
  368. MYSERIAL.print("x=");
  369. MYSERIAL.print(pat9125_x, DEC);
  370. MYSERIAL.print(" y=");
  371. MYSERIAL.print(pat9125_y, DEC);
  372. MYSERIAL.print(" b=");
  373. MYSERIAL.print(pat9125_b, DEC);
  374. MYSERIAL.print(" s=");
  375. MYSERIAL.println(pat9125_s, DEC);
  376. return;
  377. }
  378. if (code_seen('R'))
  379. {
  380. unsigned char res = (int)code_value();
  381. MYSERIAL.print("pat9125_init(xres=yres=");
  382. MYSERIAL.print(res, DEC);
  383. MYSERIAL.print(")=");
  384. MYSERIAL.println(pat9125_init(res, res), DEC);
  385. }
  386. if (code_seen('X'))
  387. {
  388. pat9125_x = (int)code_value();
  389. MYSERIAL.print("pat9125_x=");
  390. MYSERIAL.print(pat9125_x, DEC);
  391. }
  392. if (code_seen('Y'))
  393. {
  394. pat9125_y = (int)code_value();
  395. MYSERIAL.print("pat9125_y=");
  396. MYSERIAL.print(pat9125_y, DEC);
  397. }
  398. if (code_seen('L'))
  399. {
  400. fsensor_log = (int)code_value();
  401. }
  402. }