Dcodes.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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__1()
  87. {
  88. printf("D-1 - Endless loop\n");
  89. cli();
  90. while (1);
  91. }
  92. void dcode_0()
  93. {
  94. if (*(strchr_pointer + 1) == 0) return;
  95. LOG("D0 - Reset\n");
  96. if (code_seen('B')) //bootloader
  97. {
  98. cli();
  99. wdt_enable(WDTO_15MS);
  100. while(1);
  101. }
  102. else //reset
  103. {
  104. #ifndef _NO_ASM
  105. asm volatile("jmp 0x00000");
  106. #endif //_NO_ASM
  107. }
  108. }
  109. void dcode_1()
  110. {
  111. LOG("D1 - Clear EEPROM and RESET\n");
  112. cli();
  113. for (int i = 0; i < 8192; i++)
  114. eeprom_write_byte((unsigned char*)i, (unsigned char)0xff);
  115. wdt_enable(WDTO_15MS);
  116. while(1);
  117. }
  118. void dcode_2()
  119. {
  120. LOG("D2 - Read/Write RAM\n");
  121. uint16_t address = 0x0000; //default 0x0000
  122. uint16_t count = 0x2000; //default 0x2000 (entire ram)
  123. if (code_seen('A')) // Address (0x0000-0x1fff)
  124. address = (strchr_pointer[1] == 'x')?strtol(strchr_pointer + 2, 0, 16):(int)code_value();
  125. if (code_seen('C')) // Count (0x0001-0x2000)
  126. count = (int)code_value();
  127. address &= 0x1fff;
  128. if (count > 0x2000) count = 0x2000;
  129. if ((address + count) > 0x2000) count = 0x2000 - address;
  130. if (code_seen('X')) // Data
  131. {
  132. uint8_t data[16];
  133. count = parse_hex(strchr_pointer + 1, data, 16);
  134. if (count > 0)
  135. {
  136. for (int i = 0; i < count; i++)
  137. *((uint8_t*)(address + i)) = data[i];
  138. LOG("%d bytes written to RAM at address %04x", count, address);
  139. }
  140. else
  141. count = 0;
  142. }
  143. print_mem(address, count, 0);
  144. /* while (count)
  145. {
  146. print_hex_word(address);
  147. putchar(' ');
  148. uint8_t countperline = 16;
  149. while (count && countperline)
  150. {
  151. uint8_t data = *((uint8_t*)address++);
  152. putchar(' ');
  153. print_hex_byte(data);
  154. countperline--;
  155. count--;
  156. }
  157. putchar('\n');
  158. }*/
  159. }
  160. void dcode_3()
  161. {
  162. LOG("D3 - Read/Write EEPROM\n");
  163. uint16_t address = 0x0000; //default 0x0000
  164. uint16_t count = 0x2000; //default 0x2000 (entire eeprom)
  165. if (code_seen('A')) // Address (0x0000-0x1fff)
  166. address = (strchr_pointer[1] == 'x')?strtol(strchr_pointer + 2, 0, 16):(int)code_value();
  167. if (code_seen('C')) // Count (0x0001-0x2000)
  168. count = (int)code_value();
  169. address &= 0x1fff;
  170. if (count > 0x2000) count = 0x2000;
  171. if ((address + count) > 0x2000) count = 0x2000 - address;
  172. if (code_seen('X')) // Data
  173. {
  174. uint8_t data[16];
  175. count = parse_hex(strchr_pointer + 1, data, 16);
  176. if (count > 0)
  177. {
  178. for (int i = 0; i < count; i++)
  179. eeprom_write_byte((uint8_t*)(address + i), data[i]);
  180. LOG(count, DEC);
  181. LOG(" bytes written to EEPROM at address ");
  182. print_hex_word(address);
  183. putchar('\n');
  184. }
  185. else
  186. count = 0;
  187. }
  188. print_mem(address, count, 1);
  189. /* while (count)
  190. {
  191. print_hex_word(address);
  192. putchar(' ');
  193. uint8_t countperline = 16;
  194. while (count && countperline)
  195. {
  196. uint8_t data = eeprom_read_byte((uint8_t*)address++);
  197. putchar(' ');
  198. print_hex_byte(data);
  199. countperline--;
  200. count--;
  201. }
  202. putchar('\n');
  203. }*/
  204. }
  205. void dcode_4()
  206. {
  207. LOG("D4 - Read/Write PIN\n");
  208. if (code_seen('P')) // Pin (0-255)
  209. {
  210. int pin = (int)code_value();
  211. if ((pin >= 0) && (pin <= 255))
  212. {
  213. if (code_seen('F')) // Function in/out (0/1)
  214. {
  215. int fnc = (int)code_value();
  216. if (fnc == 0) pinMode(pin, INPUT);
  217. else if (fnc == 1) pinMode(pin, OUTPUT);
  218. }
  219. if (code_seen('V')) // Value (0/1)
  220. {
  221. int val = (int)code_value();
  222. if (val == 0) digitalWrite(pin, LOW);
  223. else if (val == 1) digitalWrite(pin, HIGH);
  224. }
  225. else
  226. {
  227. int val = (digitalRead(pin) != LOW)?1:0;
  228. printf("PIN%d=%d", pin, val);
  229. }
  230. }
  231. }
  232. }
  233. /*
  234. void dcode_5()
  235. {
  236. LOG("D5 - Read/Write FLASH\n");
  237. uint32_t address = 0x0000; //default 0x0000
  238. uint16_t count = 0x0400; //default 0x0400 (1kb block)
  239. if (code_seen('A')) // Address (0x00000-0x3ffff)
  240. address = (strchr_pointer[1] == 'x')?strtol(strchr_pointer + 2, 0, 16):(int)code_value();
  241. if (code_seen('C')) // Count (0x0001-0x2000)
  242. count = (int)code_value();
  243. address &= 0x3ffff;
  244. if (count > 0x2000) count = 0x2000;
  245. if ((address + count) > 0x40000) count = 0x40000 - address;
  246. bool bErase = false;
  247. bool bCopy = false;
  248. if (code_seen('E')) //Erase
  249. bErase = true;
  250. uint8_t data[16];
  251. if (code_seen('X')) // Data
  252. {
  253. count = parse_hex(strchr_pointer + 1, data, 16);
  254. if (count > 0) bCopy = true;
  255. }
  256. if (bErase || bCopy)
  257. {
  258. if (bErase)
  259. {
  260. LOG(count, DEC);
  261. LOG(" bytes of FLASH at address ");
  262. print_hex_word(address);
  263. putchar(" will be erased\n");
  264. }
  265. if (bCopy)
  266. {
  267. LOG(count, DEC);
  268. LOG(" bytes will be written to FLASH at address ");
  269. print_hex_word(address);
  270. putchar('\n');
  271. }
  272. cli();
  273. boot_app_magic = 0x55aa55aa;
  274. boot_app_flags = (bErase?(BOOT_APP_FLG_ERASE):0) | (bCopy?(BOOT_APP_FLG_COPY):0);
  275. boot_copy_size = (uint16_t)count;
  276. boot_dst_addr = (uint32_t)address;
  277. boot_src_addr = (uint32_t)(&data);
  278. wdt_enable(WDTO_15MS);
  279. while(1);
  280. }
  281. while (count)
  282. {
  283. print_hex_nibble(address >> 16);
  284. print_hex_word(address);
  285. putchar(' ');
  286. uint8_t countperline = 16;
  287. while (count && countperline)
  288. {
  289. uint8_t data = pgm_read_byte_far((uint8_t*)address++);
  290. putchar(' ');
  291. print_hex_byte(data);
  292. countperline--;
  293. count--;
  294. }
  295. putchar('\n');
  296. }
  297. }
  298. */
  299. void dcode_6()
  300. {
  301. LOG("D6 - Read/Write external FLASH\n");
  302. }
  303. void dcode_7()
  304. {
  305. LOG("D7 - Read/Write Bootloader\n");
  306. /*
  307. cli();
  308. boot_app_magic = 0x55aa55aa;
  309. boot_app_flags = BOOT_APP_FLG_ERASE | BOOT_APP_FLG_COPY | BOOT_APP_FLG_FLASH;
  310. boot_copy_size = (uint16_t)0xc00;
  311. boot_src_addr = (uint32_t)0x0003e400;
  312. boot_dst_addr = (uint32_t)0x0003f400;
  313. wdt_enable(WDTO_15MS);
  314. while(1);
  315. */
  316. }
  317. void dcode_8()
  318. {
  319. LOG("D8 - Read/Write PINDA\n");
  320. uint8_t cal_status = calibration_status_pinda();
  321. float temp_pinda = current_temperature_pinda;
  322. float offset_z = temp_compensation_pinda_thermistor_offset(temp_pinda);
  323. if ((strchr_pointer[1+1] == '?') || (strchr_pointer[1+1] == 0))
  324. {
  325. LOG("cal_status=");
  326. LOG(cal_status?"1\n":"0\n");
  327. for (uint8_t i = 0; i < 6; i++)
  328. {
  329. LOG("temp_pinda=");
  330. LOG(35 + i * 5, DEC);
  331. LOG("C, temp_shift=");
  332. uint16_t offs = 0;
  333. if (i > 0) offs = eeprom_read_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + (i - 1));
  334. LOG(((float)offs) / axis_steps_per_unit[Z_AXIS], 3);
  335. LOG("mm\n");
  336. }
  337. }
  338. else if (strchr_pointer[1+1] == '!')
  339. {
  340. cal_status = 1;
  341. eeprom_write_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_PINDA, cal_status);
  342. eeprom_write_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + 0, 50); //40C -
  343. eeprom_write_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + 1, 100); //45C -
  344. eeprom_write_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + 2, 150); //50C -
  345. eeprom_write_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + 3, 200); //55C -
  346. eeprom_write_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + 4, 250); //60C -
  347. }
  348. else
  349. {
  350. if (code_seen('P')) // Pinda temperature [C]
  351. temp_pinda = code_value();
  352. offset_z = temp_compensation_pinda_thermistor_offset(temp_pinda);
  353. if (code_seen('Z')) // Z Offset [mm]
  354. {
  355. offset_z = code_value();
  356. }
  357. }
  358. LOG("temp_pinda=");
  359. LOG(temp_pinda);
  360. LOG("offset_z=");
  361. LOG(offset_z, 3);
  362. }
  363. void dcode_10()
  364. {//Tell the printer that XYZ calibration went OK
  365. LOG("D10 - XYZ calibration = OK\n");
  366. calibration_status_store(CALIBRATION_STATUS_LIVE_ADJUST);
  367. }
  368. void dcode_12()
  369. {//Reset Filament error, Power loss and crash counter ( Do it before every print and you can get stats for the print )
  370. LOG("D12 - Reset failstat counters\n");
  371. eeprom_update_byte((uint8_t*)EEPROM_CRASH_COUNT, 0x00);
  372. eeprom_update_byte((uint8_t*)EEPROM_FERROR_COUNT, 0x00);
  373. eeprom_update_byte((uint8_t*)EEPROM_POWER_COUNT, 0x00);
  374. }
  375. #include "tmc2130.h"
  376. #include "Marlin.h"
  377. #include "planner.h"
  378. extern void st_synchronize();
  379. void dcode_2130()
  380. {
  381. // printf("test");
  382. printf_P(PSTR("D2130 - TMC2130\n"));
  383. uint8_t axis = 0xff;
  384. if (code_seen('X'))
  385. axis = X_AXIS;
  386. else if (code_seen('Y'))
  387. axis = Y_AXIS;
  388. if (axis != 0xff)
  389. {
  390. homeaxis(axis);
  391. tmc2130_sg_meassure_start(axis);
  392. memcpy(destination, current_position, sizeof(destination));
  393. destination[axis] = 200;
  394. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], homing_feedrate[X_AXIS]/60, active_extruder);
  395. st_synchronize();
  396. memcpy(destination, current_position, sizeof(destination));
  397. destination[axis] = 0;
  398. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], homing_feedrate[X_AXIS]/60, active_extruder);
  399. st_synchronize();
  400. uint16_t sg = tmc2130_sg_meassure_stop();
  401. tmc2130_sg_meassure = 0xff;
  402. printf_P(PSTR("Meassure avg = %d\n"), sg);
  403. }
  404. }
  405. void dcode_9125()
  406. {
  407. LOG("D9125 - PAT9125\n");
  408. if ((strchr_pointer[1+4] == '?') || (strchr_pointer[1+4] == 0))
  409. {
  410. 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);
  411. return;
  412. }
  413. if (strchr_pointer[1+4] == '!')
  414. {
  415. pat9125_update();
  416. printf("x=%d y=%d b=%d s=%d\n", pat9125_x, pat9125_y, pat9125_b, pat9125_s);
  417. return;
  418. }
  419. if (code_seen('R'))
  420. {
  421. unsigned char res = (int)code_value();
  422. LOG("pat9125_init(xres=yres=%d)=%d\n", res, pat9125_init(res, res));
  423. }
  424. if (code_seen('X'))
  425. {
  426. pat9125_x = (int)code_value();
  427. LOG("pat9125_x=%d\n", pat9125_x);
  428. }
  429. if (code_seen('Y'))
  430. {
  431. pat9125_y = (int)code_value();
  432. LOG("pat9125_y=%d\n", pat9125_y);
  433. }
  434. if (code_seen('L'))
  435. {
  436. fsensor_log = (int)code_value();
  437. LOG("fsensor_log=%d\n", fsensor_log);
  438. }
  439. }
  440. #endif //DEBUG_DCODES