Dcodes.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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 "adc.h"
  8. #include "temperature.h"
  9. #include <avr/wdt.h>
  10. #define FLASHSIZE 0x40000
  11. #define RAMSIZE 0x2000
  12. #define boot_src_addr (*((uint32_t*)(RAMSIZE - 16)))
  13. #define boot_dst_addr (*((uint32_t*)(RAMSIZE - 12)))
  14. #define boot_copy_size (*((uint16_t*)(RAMSIZE - 8)))
  15. #define boot_reserved (*((uint8_t*)(RAMSIZE - 6)))
  16. #define boot_app_flags (*((uint8_t*)(RAMSIZE - 5)))
  17. #define boot_app_magic (*((uint32_t*)(RAMSIZE - 4)))
  18. #define BOOT_APP_FLG_ERASE 0x01
  19. #define BOOT_APP_FLG_COPY 0x02
  20. #define BOOT_APP_FLG_FLASH 0x04
  21. extern uint8_t fsensor_log;
  22. extern float current_temperature_pinda;
  23. extern float axis_steps_per_unit[NUM_AXIS];
  24. inline void print_hex_nibble(uint8_t val)
  25. {
  26. putchar((val > 9)?(val - 10 + 'a'):(val + '0'));
  27. }
  28. void print_hex_byte(uint8_t val)
  29. {
  30. print_hex_nibble(val >> 4);
  31. print_hex_nibble(val & 15);
  32. }
  33. void print_hex_word(uint16_t val)
  34. {
  35. print_hex_byte(val >> 8);
  36. print_hex_byte(val & 255);
  37. }
  38. void print_mem(uint32_t address, uint16_t count, uint8_t type, uint8_t countperline = 16)
  39. {
  40. while (count)
  41. {
  42. if (type == 2)
  43. print_hex_nibble(address >> 16);
  44. print_hex_word(address);
  45. putchar(' ');
  46. uint8_t count_line = countperline;
  47. while (count && count_line)
  48. {
  49. uint8_t data = 0;
  50. switch (type)
  51. {
  52. case 0: data = *((uint8_t*)address++); break;
  53. case 1: data = eeprom_read_byte((uint8_t*)address++); break;
  54. case 2: data = pgm_read_byte_far((uint8_t*)address++); break;
  55. }
  56. putchar(' ');
  57. print_hex_byte(data);
  58. count_line--;
  59. count--;
  60. }
  61. putchar('\n');
  62. }
  63. }
  64. //#define LOG(args...) printf(args)
  65. #define LOG(args...)
  66. int parse_hex(char* hex, uint8_t* data, int count)
  67. {
  68. int parsed = 0;
  69. while (*hex)
  70. {
  71. if (count && (parsed >= count)) break;
  72. char c = *(hex++);
  73. if (c == ' ') continue;
  74. if (c == '\n') break;
  75. uint8_t val = 0x00;
  76. if ((c >= '0') && (c <= '9')) val |= ((c - '0') << 4);
  77. else if ((c >= 'a') && (c <= 'f')) val |= ((c - 'a' + 10) << 4);
  78. else return -parsed;
  79. c = *(hex++);
  80. if ((c >= '0') && (c <= '9')) val |= (c - '0');
  81. else if ((c >= 'a') && (c <= 'f')) val |= (c - 'a' + 10);
  82. else return -parsed;
  83. data[parsed] = val;
  84. parsed++;
  85. }
  86. return parsed;
  87. }
  88. void dcode__1()
  89. {
  90. printf("D-1 - Endless loop\n");
  91. cli();
  92. while (1);
  93. }
  94. void dcode_0()
  95. {
  96. if (*(strchr_pointer + 1) == 0) return;
  97. LOG("D0 - Reset\n");
  98. if (code_seen('B')) //bootloader
  99. {
  100. cli();
  101. wdt_enable(WDTO_15MS);
  102. while(1);
  103. }
  104. else //reset
  105. {
  106. #ifndef _NO_ASM
  107. asm volatile("jmp 0x00000");
  108. #endif //_NO_ASM
  109. }
  110. }
  111. void dcode_1()
  112. {
  113. LOG("D1 - Clear EEPROM and RESET\n");
  114. cli();
  115. for (int i = 0; i < 8192; i++)
  116. eeprom_write_byte((unsigned char*)i, (unsigned char)0xff);
  117. wdt_enable(WDTO_15MS);
  118. while(1);
  119. }
  120. void dcode_2()
  121. {
  122. LOG("D2 - Read/Write RAM\n");
  123. uint16_t address = 0x0000; //default 0x0000
  124. uint16_t count = 0x2000; //default 0x2000 (entire ram)
  125. if (code_seen('A')) // Address (0x0000-0x1fff)
  126. address = (strchr_pointer[1] == 'x')?strtol(strchr_pointer + 2, 0, 16):(int)code_value();
  127. if (code_seen('C')) // Count (0x0001-0x2000)
  128. count = (int)code_value();
  129. address &= 0x1fff;
  130. if (count > 0x2000) count = 0x2000;
  131. if ((address + count) > 0x2000) count = 0x2000 - address;
  132. if (code_seen('X')) // Data
  133. {
  134. uint8_t data[16];
  135. count = parse_hex(strchr_pointer + 1, data, 16);
  136. if (count > 0)
  137. {
  138. for (int i = 0; i < count; i++)
  139. *((uint8_t*)(address + i)) = data[i];
  140. LOG("%d bytes written to RAM at address %04x", count, address);
  141. }
  142. else
  143. count = 0;
  144. }
  145. print_mem(address, count, 0);
  146. /* while (count)
  147. {
  148. print_hex_word(address);
  149. putchar(' ');
  150. uint8_t countperline = 16;
  151. while (count && countperline)
  152. {
  153. uint8_t data = *((uint8_t*)address++);
  154. putchar(' ');
  155. print_hex_byte(data);
  156. countperline--;
  157. count--;
  158. }
  159. putchar('\n');
  160. }*/
  161. }
  162. void dcode_3()
  163. {
  164. LOG("D3 - Read/Write EEPROM\n");
  165. uint16_t address = 0x0000; //default 0x0000
  166. uint16_t count = 0x2000; //default 0x2000 (entire eeprom)
  167. if (code_seen('A')) // Address (0x0000-0x1fff)
  168. address = (strchr_pointer[1] == 'x')?strtol(strchr_pointer + 2, 0, 16):(int)code_value();
  169. if (code_seen('C')) // Count (0x0001-0x2000)
  170. count = (int)code_value();
  171. address &= 0x1fff;
  172. if (count > 0x2000) count = 0x2000;
  173. if ((address + count) > 0x2000) count = 0x2000 - address;
  174. if (code_seen('X')) // Data
  175. {
  176. uint8_t data[16];
  177. count = parse_hex(strchr_pointer + 1, data, 16);
  178. if (count > 0)
  179. {
  180. for (int i = 0; i < count; i++)
  181. eeprom_write_byte((uint8_t*)(address + i), data[i]);
  182. LOG(count, DEC);
  183. LOG(" bytes written to EEPROM at address ");
  184. print_hex_word(address);
  185. putchar('\n');
  186. }
  187. else
  188. count = 0;
  189. }
  190. print_mem(address, count, 1);
  191. /* while (count)
  192. {
  193. print_hex_word(address);
  194. putchar(' ');
  195. uint8_t countperline = 16;
  196. while (count && countperline)
  197. {
  198. uint8_t data = eeprom_read_byte((uint8_t*)address++);
  199. putchar(' ');
  200. print_hex_byte(data);
  201. countperline--;
  202. count--;
  203. }
  204. putchar('\n');
  205. }*/
  206. }
  207. void dcode_4()
  208. {
  209. LOG("D4 - Read/Write PIN\n");
  210. if (code_seen('P')) // Pin (0-255)
  211. {
  212. int pin = (int)code_value();
  213. if ((pin >= 0) && (pin <= 255))
  214. {
  215. if (code_seen('F')) // Function in/out (0/1)
  216. {
  217. int fnc = (int)code_value();
  218. if (fnc == 0) pinMode(pin, INPUT);
  219. else if (fnc == 1) pinMode(pin, OUTPUT);
  220. }
  221. if (code_seen('V')) // Value (0/1)
  222. {
  223. int val = (int)code_value();
  224. if (val == 0) digitalWrite(pin, LOW);
  225. else if (val == 1) digitalWrite(pin, HIGH);
  226. }
  227. else
  228. {
  229. int val = (digitalRead(pin) != LOW)?1:0;
  230. printf("PIN%d=%d", pin, val);
  231. }
  232. }
  233. }
  234. }
  235. /*
  236. void dcode_5()
  237. {
  238. LOG("D5 - Read/Write FLASH\n");
  239. uint32_t address = 0x0000; //default 0x0000
  240. uint16_t count = 0x0400; //default 0x0400 (1kb block)
  241. if (code_seen('A')) // Address (0x00000-0x3ffff)
  242. address = (strchr_pointer[1] == 'x')?strtol(strchr_pointer + 2, 0, 16):(int)code_value();
  243. if (code_seen('C')) // Count (0x0001-0x2000)
  244. count = (int)code_value();
  245. address &= 0x3ffff;
  246. if (count > 0x2000) count = 0x2000;
  247. if ((address + count) > 0x40000) count = 0x40000 - address;
  248. bool bErase = false;
  249. bool bCopy = false;
  250. if (code_seen('E')) //Erase
  251. bErase = true;
  252. uint8_t data[16];
  253. if (code_seen('X')) // Data
  254. {
  255. count = parse_hex(strchr_pointer + 1, data, 16);
  256. if (count > 0) bCopy = true;
  257. }
  258. if (bErase || bCopy)
  259. {
  260. if (bErase)
  261. {
  262. LOG(count, DEC);
  263. LOG(" bytes of FLASH at address ");
  264. print_hex_word(address);
  265. putchar(" will be erased\n");
  266. }
  267. if (bCopy)
  268. {
  269. LOG(count, DEC);
  270. LOG(" bytes will be written to FLASH at address ");
  271. print_hex_word(address);
  272. putchar('\n');
  273. }
  274. cli();
  275. boot_app_magic = 0x55aa55aa;
  276. boot_app_flags = (bErase?(BOOT_APP_FLG_ERASE):0) | (bCopy?(BOOT_APP_FLG_COPY):0);
  277. boot_copy_size = (uint16_t)count;
  278. boot_dst_addr = (uint32_t)address;
  279. boot_src_addr = (uint32_t)(&data);
  280. wdt_enable(WDTO_15MS);
  281. while(1);
  282. }
  283. while (count)
  284. {
  285. print_hex_nibble(address >> 16);
  286. print_hex_word(address);
  287. putchar(' ');
  288. uint8_t countperline = 16;
  289. while (count && countperline)
  290. {
  291. uint8_t data = pgm_read_byte_far((uint8_t*)address++);
  292. putchar(' ');
  293. print_hex_byte(data);
  294. countperline--;
  295. count--;
  296. }
  297. putchar('\n');
  298. }
  299. }
  300. */
  301. void dcode_6()
  302. {
  303. LOG("D6 - Read/Write external FLASH\n");
  304. }
  305. void dcode_7()
  306. {
  307. LOG("D7 - Read/Write Bootloader\n");
  308. /*
  309. cli();
  310. boot_app_magic = 0x55aa55aa;
  311. boot_app_flags = BOOT_APP_FLG_ERASE | BOOT_APP_FLG_COPY | BOOT_APP_FLG_FLASH;
  312. boot_copy_size = (uint16_t)0xc00;
  313. boot_src_addr = (uint32_t)0x0003e400;
  314. boot_dst_addr = (uint32_t)0x0003f400;
  315. wdt_enable(WDTO_15MS);
  316. while(1);
  317. */
  318. }
  319. void dcode_8()
  320. {
  321. printf_P(PSTR("D8 - Read/Write PINDA\n"));
  322. uint8_t cal_status = calibration_status_pinda();
  323. float temp_pinda = current_temperature_pinda;
  324. float offset_z = temp_compensation_pinda_thermistor_offset(temp_pinda);
  325. if ((strchr_pointer[1+1] == '?') || (strchr_pointer[1+1] == 0))
  326. {
  327. printf_P(PSTR("cal_status=%d\n"), cal_status?1:0);
  328. for (uint8_t i = 0; i < 6; i++)
  329. {
  330. uint16_t offs = 0;
  331. if (i > 0) offs = eeprom_read_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + (i - 1));
  332. float foffs = ((float)offs) / axis_steps_per_unit[Z_AXIS];
  333. offs = 1000 * foffs;
  334. printf_P(PSTR("temp_pinda=%dC temp_shift=%dum\n"), 35 + i * 5, offs);
  335. }
  336. }
  337. else if (strchr_pointer[1+1] == '!')
  338. {
  339. cal_status = 1;
  340. eeprom_write_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_PINDA, cal_status);
  341. eeprom_write_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + 0, 8); //40C - 20um - 8usteps
  342. eeprom_write_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + 1, 24); //45C - 60um - 24usteps
  343. eeprom_write_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + 2, 48); //50C - 120um - 48usteps
  344. eeprom_write_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + 3, 80); //55C - 200um - 80usteps
  345. eeprom_write_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + 4, 120); //60C - 300um - 120usteps
  346. }
  347. else
  348. {
  349. if (code_seen('P')) // Pinda temperature [C]
  350. temp_pinda = code_value();
  351. offset_z = temp_compensation_pinda_thermistor_offset(temp_pinda);
  352. if (code_seen('Z')) // Z Offset [mm]
  353. {
  354. offset_z = code_value();
  355. }
  356. }
  357. printf_P(PSTR("temp_pinda=%d offset_z=%d.%03d\n"), (int)temp_pinda, (int)offset_z, ((int)(1000 * offset_z) % 1000));
  358. }
  359. const char* dcode_9_ADC_name(uint8_t i)
  360. {
  361. switch (i)
  362. {
  363. case 0: return PSTR("TEMP_HEATER0");
  364. case 1: return PSTR("TEMP_HEATER1");
  365. case 2: return PSTR("TEMP_BED");
  366. case 3: return PSTR("TEMP_PINDA");
  367. case 4: return PSTR("VOLT_PWR");
  368. case 5: return PSTR("TEMP_AMBIENT");
  369. case 6: return PSTR("VOLT_BED");
  370. }
  371. return 0;
  372. }
  373. extern int current_temperature_raw[EXTRUDERS];
  374. extern int current_temperature_bed_raw;
  375. extern int current_temperature_raw_pinda;
  376. extern int current_temperature_raw_ambient;
  377. extern int current_voltage_raw_pwr;
  378. extern int current_voltage_raw_bed;
  379. uint16_t dcode_9_ADC_val(uint8_t i)
  380. {
  381. switch (i)
  382. {
  383. case 0: return current_temperature_raw[0];
  384. case 1: return 0;
  385. case 2: return current_temperature_bed_raw;
  386. case 3: return current_temperature_raw_pinda;
  387. case 4: return current_voltage_raw_pwr;
  388. case 5: return current_temperature_raw_ambient;
  389. case 6: return current_voltage_raw_bed;
  390. }
  391. return 0;
  392. }
  393. void dcode_9()
  394. {
  395. printf_P(PSTR("D9 - Read/Write ADC\n"));
  396. if ((strchr_pointer[1+1] == '?') || (strchr_pointer[1+1] == 0))
  397. {
  398. for (uint8_t i = 0; i < ADC_CHAN_CNT; i++)
  399. printf_P(PSTR("\tADC%d=%4d\t(%S)\n"), i, dcode_9_ADC_val(i) >> 4, dcode_9_ADC_name(i));
  400. }
  401. else
  402. {
  403. uint8_t index = 0xff;
  404. if (code_seen('I')) // index (index of used channel, not avr channel index)
  405. index = code_value();
  406. if (index < ADC_CHAN_CNT)
  407. {
  408. if (code_seen('V')) // value to be written as simulated
  409. {
  410. adc_sim_mask |= (1 << index);
  411. adc_values[index] = (((int)code_value()) << 4);
  412. printf_P(PSTR("ADC%d=%4d\n"), index, adc_values[index] >> 4);
  413. }
  414. }
  415. }
  416. }
  417. void dcode_10()
  418. {//Tell the printer that XYZ calibration went OK
  419. LOG("D10 - XYZ calibration = OK\n");
  420. calibration_status_store(CALIBRATION_STATUS_LIVE_ADJUST);
  421. }
  422. void dcode_12()
  423. {//Reset Filament error, Power loss and crash counter ( Do it before every print and you can get stats for the print )
  424. LOG("D12 - Reset failstat counters\n");
  425. eeprom_update_byte((uint8_t*)EEPROM_CRASH_COUNT_X, 0x00);
  426. eeprom_update_byte((uint8_t*)EEPROM_FERROR_COUNT, 0x00);
  427. eeprom_update_byte((uint8_t*)EEPROM_POWER_COUNT, 0x00);
  428. }
  429. #include "tmc2130.h"
  430. #include "Marlin.h"
  431. #include "planner.h"
  432. extern void st_synchronize();
  433. void dcode_2130()
  434. {
  435. // printf("test");
  436. printf_P(PSTR("D2130 - TMC2130\n"));
  437. uint8_t axis = 0xff;
  438. if (code_seen('X'))
  439. axis = X_AXIS;
  440. else if (code_seen('Y'))
  441. axis = Y_AXIS;
  442. if (axis != 0xff)
  443. {
  444. homeaxis(axis);
  445. tmc2130_sg_meassure_start(axis);
  446. memcpy(destination, current_position, sizeof(destination));
  447. destination[axis] = 200;
  448. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], homing_feedrate[X_AXIS]/60, active_extruder);
  449. st_synchronize();
  450. memcpy(destination, current_position, sizeof(destination));
  451. destination[axis] = 0;
  452. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], homing_feedrate[X_AXIS]/60, active_extruder);
  453. st_synchronize();
  454. uint16_t sg = tmc2130_sg_meassure_stop();
  455. tmc2130_sg_meassure = 0xff;
  456. printf_P(PSTR("Meassure avg = %d\n"), sg);
  457. }
  458. }
  459. void dcode_9125()
  460. {
  461. LOG("D9125 - PAT9125\n");
  462. if ((strchr_pointer[1+4] == '?') || (strchr_pointer[1+4] == 0))
  463. {
  464. // 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);
  465. printf("x=%d y=%d b=%d s=%d\n", pat9125_x, pat9125_y, pat9125_b, pat9125_s);
  466. return;
  467. }
  468. if (strchr_pointer[1+4] == '!')
  469. {
  470. pat9125_update();
  471. printf("x=%d y=%d b=%d s=%d\n", pat9125_x, pat9125_y, pat9125_b, pat9125_s);
  472. return;
  473. }
  474. /*
  475. if (code_seen('R'))
  476. {
  477. unsigned char res = (int)code_value();
  478. LOG("pat9125_init(xres=yres=%d)=%d\n", res, pat9125_init(res, res));
  479. }
  480. */
  481. if (code_seen('X'))
  482. {
  483. pat9125_x = (int)code_value();
  484. LOG("pat9125_x=%d\n", pat9125_x);
  485. }
  486. if (code_seen('Y'))
  487. {
  488. pat9125_y = (int)code_value();
  489. LOG("pat9125_y=%d\n", pat9125_y);
  490. }
  491. if (code_seen('L'))
  492. {
  493. fsensor_log = (int)code_value();
  494. LOG("fsensor_log=%d\n", fsensor_log);
  495. }
  496. }
  497. #endif //DEBUG_DCODES