Dcodes.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. printf_P(PSTR("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. printf_P(PSTR("cal_status=%d\n"), cal_status?1:0);
  326. for (uint8_t i = 0; i < 6; i++)
  327. {
  328. uint16_t offs = 0;
  329. if (i > 0) offs = eeprom_read_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + (i - 1));
  330. float foffs = ((float)offs) / axis_steps_per_unit[Z_AXIS];
  331. offs = 1000 * foffs;
  332. printf_P(PSTR("temp_pinda=%dC temp_shift=%dum\n"), 35 + i * 5, offs);
  333. }
  334. }
  335. else if (strchr_pointer[1+1] == '!')
  336. {
  337. cal_status = 1;
  338. eeprom_write_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_PINDA, cal_status);
  339. eeprom_write_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + 0, 8); //40C - 20um - 8usteps
  340. eeprom_write_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + 1, 24); //45C - 60um - 24usteps
  341. eeprom_write_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + 2, 48); //50C - 120um - 48usteps
  342. eeprom_write_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + 3, 80); //55C - 200um - 80usteps
  343. eeprom_write_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + 4, 120); //60C - 300um - 120usteps
  344. }
  345. else
  346. {
  347. if (code_seen('P')) // Pinda temperature [C]
  348. temp_pinda = code_value();
  349. offset_z = temp_compensation_pinda_thermistor_offset(temp_pinda);
  350. if (code_seen('Z')) // Z Offset [mm]
  351. {
  352. offset_z = code_value();
  353. }
  354. }
  355. printf_P(PSTR("temp_pinda=%d offset_z=%d.%03d\n"), (int)temp_pinda, (int)offset_z, ((int)(1000 * offset_z) % 1000));
  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. #include "tmc2130.h"
  370. #include "Marlin.h"
  371. #include "planner.h"
  372. extern void st_synchronize();
  373. void dcode_2130()
  374. {
  375. // printf("test");
  376. printf_P(PSTR("D2130 - TMC2130\n"));
  377. uint8_t axis = 0xff;
  378. if (code_seen('X'))
  379. axis = X_AXIS;
  380. else if (code_seen('Y'))
  381. axis = Y_AXIS;
  382. if (axis != 0xff)
  383. {
  384. homeaxis(axis);
  385. tmc2130_sg_meassure_start(axis);
  386. memcpy(destination, current_position, sizeof(destination));
  387. destination[axis] = 200;
  388. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], homing_feedrate[X_AXIS]/60, active_extruder);
  389. st_synchronize();
  390. memcpy(destination, current_position, sizeof(destination));
  391. destination[axis] = 0;
  392. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], homing_feedrate[X_AXIS]/60, active_extruder);
  393. st_synchronize();
  394. uint16_t sg = tmc2130_sg_meassure_stop();
  395. tmc2130_sg_meassure = 0xff;
  396. printf_P(PSTR("Meassure avg = %d\n"), sg);
  397. }
  398. }
  399. void dcode_9125()
  400. {
  401. LOG("D9125 - PAT9125\n");
  402. if ((strchr_pointer[1+4] == '?') || (strchr_pointer[1+4] == 0))
  403. {
  404. 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);
  405. return;
  406. }
  407. if (strchr_pointer[1+4] == '!')
  408. {
  409. pat9125_update();
  410. printf("x=%d y=%d b=%d s=%d\n", pat9125_x, pat9125_y, pat9125_b, pat9125_s);
  411. return;
  412. }
  413. if (code_seen('R'))
  414. {
  415. unsigned char res = (int)code_value();
  416. LOG("pat9125_init(xres=yres=%d)=%d\n", res, pat9125_init(res, res));
  417. }
  418. if (code_seen('X'))
  419. {
  420. pat9125_x = (int)code_value();
  421. LOG("pat9125_x=%d\n", pat9125_x);
  422. }
  423. if (code_seen('Y'))
  424. {
  425. pat9125_y = (int)code_value();
  426. LOG("pat9125_y=%d\n", pat9125_y);
  427. }
  428. if (code_seen('L'))
  429. {
  430. fsensor_log = (int)code_value();
  431. LOG("fsensor_log=%d\n", fsensor_log);
  432. }
  433. }
  434. #endif //DEBUG_DCODES