Dcodes.cpp 10 KB

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