Dcodes.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. #include "Dcodes.h"
  2. //#include "Marlin.h"
  3. #include "language.h"
  4. #include "cmdqueue.h"
  5. #include <stdio.h>
  6. #include <avr/pgmspace.h>
  7. #define SHOW_TEMP_ADC_VALUES
  8. #include "temperature.h"
  9. #define DBG(args...) printf_P(args)
  10. inline void print_hex_nibble(uint8_t val)
  11. {
  12. putchar((val > 9)?(val - 10 + 'a'):(val + '0'));
  13. }
  14. void print_hex_byte(uint8_t val)
  15. {
  16. print_hex_nibble(val >> 4);
  17. print_hex_nibble(val & 15);
  18. }
  19. void print_hex_word(uint16_t val)
  20. {
  21. print_hex_byte(val >> 8);
  22. print_hex_byte(val & 255);
  23. }
  24. void print_eeprom(uint16_t address, uint16_t count, uint8_t countperline = 16)
  25. {
  26. while (count)
  27. {
  28. print_hex_word(address);
  29. putchar(' ');
  30. uint8_t count_line = countperline;
  31. while (count && count_line)
  32. {
  33. putchar(' ');
  34. print_hex_byte(eeprom_read_byte((uint8_t*)address++));
  35. count_line--;
  36. count--;
  37. }
  38. putchar('\n');
  39. }
  40. }
  41. int parse_hex(char* hex, uint8_t* data, int count)
  42. {
  43. int parsed = 0;
  44. while (*hex)
  45. {
  46. if (count && (parsed >= count)) break;
  47. char c = *(hex++);
  48. if (c == ' ') continue;
  49. if (c == '\n') break;
  50. uint8_t val = 0x00;
  51. if ((c >= '0') && (c <= '9')) val |= ((c - '0') << 4);
  52. else if ((c >= 'a') && (c <= 'f')) val |= ((c - 'a' + 10) << 4);
  53. else return -parsed;
  54. c = *(hex++);
  55. if ((c >= '0') && (c <= '9')) val |= (c - '0');
  56. else if ((c >= 'a') && (c <= 'f')) val |= (c - 'a' + 10);
  57. else return -parsed;
  58. data[parsed] = val;
  59. parsed++;
  60. }
  61. return parsed;
  62. }
  63. void print_mem(uint32_t address, uint16_t count, uint8_t type, uint8_t countperline = 16)
  64. {
  65. while (count)
  66. {
  67. if (type == 2)
  68. print_hex_nibble(address >> 16);
  69. print_hex_word(address);
  70. putchar(' ');
  71. uint8_t count_line = countperline;
  72. while (count && count_line)
  73. {
  74. uint8_t data = 0;
  75. switch (type)
  76. {
  77. case 0: data = *((uint8_t*)address++); break;
  78. case 1: data = eeprom_read_byte((uint8_t*)address++); break;
  79. case 2: data = pgm_read_byte_far((uint8_t*)address++); break;
  80. }
  81. putchar(' ');
  82. print_hex_byte(data);
  83. count_line--;
  84. count--;
  85. }
  86. putchar('\n');
  87. }
  88. }
  89. #ifdef DEBUG_DCODE3
  90. #define EEPROM_SIZE 0x1000
  91. /*!
  92. *
  93. ### D3 - Read/Write EEPROM <a href="https://reprap.org/wiki/G-code#D3:_Read.2FWrite_EEPROM">D3: Read/Write EEPROM</a>
  94. This command can be used without any additional parameters. It will read the entire eeprom.
  95. D3 [ A | C | X ]
  96. - `A` - Address (0x0000-0x0fff)
  97. - `C` - Count (0x0001-0x1000)
  98. - `X` - Data
  99. *
  100. */
  101. void dcode_3()
  102. {
  103. DBG(_N("D3 - Read/Write EEPROM\n"));
  104. uint16_t address = 0x0000; //default 0x0000
  105. uint16_t count = EEPROM_SIZE; //default 0x1000 (entire eeprom)
  106. if (code_seen('A')) // Address (0x0000-0x0fff)
  107. address = (strchr_pointer[1] == 'x')?strtol(strchr_pointer + 2, 0, 16):(int)code_value();
  108. if (code_seen('C')) // Count (0x0001-0x1000)
  109. count = (int)code_value();
  110. address &= 0x1fff;
  111. if (count > EEPROM_SIZE) count = EEPROM_SIZE;
  112. if ((address + count) > EEPROM_SIZE) count = EEPROM_SIZE - address;
  113. if (code_seen('X')) // Data
  114. {
  115. uint8_t data[16];
  116. count = parse_hex(strchr_pointer + 1, data, 16);
  117. if (count > 0)
  118. {
  119. for (uint16_t i = 0; i < count; i++)
  120. eeprom_write_byte((uint8_t*)(address + i), data[i]);
  121. printf_P(_N("%d bytes written to EEPROM at address 0x%04x"), count, address);
  122. putchar('\n');
  123. }
  124. else
  125. count = 0;
  126. }
  127. print_mem(address, count, 1);
  128. /* while (count)
  129. {
  130. print_hex_word(address);
  131. putchar(' ');
  132. uint8_t countperline = 16;
  133. while (count && countperline)
  134. {
  135. uint8_t data = eeprom_read_byte((uint8_t*)address++);
  136. putchar(' ');
  137. print_hex_byte(data);
  138. countperline--;
  139. count--;
  140. }
  141. putchar('\n');
  142. }*/
  143. }
  144. #endif //DEBUG_DCODE3
  145. #include "ConfigurationStore.h"
  146. #include "cmdqueue.h"
  147. #include "pat9125.h"
  148. #include "adc.h"
  149. #include "temperature.h"
  150. #include <avr/wdt.h>
  151. #include "bootapp.h"
  152. #if 0
  153. #define FLASHSIZE 0x40000
  154. #define RAMSIZE 0x2000
  155. #define boot_src_addr (*((uint32_t*)(RAMSIZE - 16)))
  156. #define boot_dst_addr (*((uint32_t*)(RAMSIZE - 12)))
  157. #define boot_copy_size (*((uint16_t*)(RAMSIZE - 8)))
  158. #define boot_reserved (*((uint8_t*)(RAMSIZE - 6)))
  159. #define boot_app_flags (*((uint8_t*)(RAMSIZE - 5)))
  160. #define boot_app_magic (*((uint32_t*)(RAMSIZE - 4)))
  161. #define BOOT_APP_FLG_ERASE 0x01
  162. #define BOOT_APP_FLG_COPY 0x02
  163. #define BOOT_APP_FLG_FLASH 0x04
  164. extern uint8_t fsensor_log;
  165. extern float current_temperature_pinda;
  166. extern float axis_steps_per_unit[NUM_AXIS];
  167. #define LOG(args...) printf(args)
  168. #endif //0
  169. #define LOG(args...)
  170. /*!
  171. *
  172. ### D-1 - Endless Loop <a href="https://reprap.org/wiki/G-code#G28:_Move_to_Origin_.28Home.29">D-1: Endless Loop</a>
  173. D-1
  174. *
  175. */
  176. void dcode__1()
  177. {
  178. printf_P(PSTR("D-1 - Endless loop\n"));
  179. // cli();
  180. while (1);
  181. }
  182. #ifdef DEBUG_DCODES
  183. /*!
  184. *
  185. ### D0 - Reset <a href="https://reprap.org/wiki/G-code#D0:_Reset">D0: Reset</a>
  186. D0 [ B ]
  187. - `B` - Bootloader
  188. *
  189. */
  190. void dcode_0()
  191. {
  192. if (*(strchr_pointer + 1) == 0) return;
  193. LOG("D0 - Reset\n");
  194. if (code_seen('B')) //bootloader
  195. {
  196. cli();
  197. wdt_enable(WDTO_15MS);
  198. while(1);
  199. }
  200. else //reset
  201. {
  202. #ifndef _NO_ASM
  203. asm volatile("jmp 0x00000");
  204. #endif //_NO_ASM
  205. }
  206. }
  207. /*!
  208. *
  209. ### D1 - Clear EEPROM and RESET <a href="https://reprap.org/wiki/G-code#D1:_Clear_EEPROM_and_RESET">D1: Clear EEPROM and RESET</a>
  210. D1
  211. *
  212. */
  213. void dcode_1()
  214. {
  215. LOG("D1 - Clear EEPROM and RESET\n");
  216. cli();
  217. for (int i = 0; i < 8192; i++)
  218. eeprom_write_byte((unsigned char*)i, (unsigned char)0xff);
  219. wdt_enable(WDTO_15MS);
  220. while(1);
  221. }
  222. /*!
  223. *
  224. ### D2 - Read/Write RAM <a href="https://reprap.org/wiki/G-code#D2:_Read.2FWrite_RAM">D2: Read/Write RAM</a>
  225. This command can be used without any additional parameters. It will read the entire RAM.
  226. D2 [ A | C | X ]
  227. - `A` - Address (0x0000-0x1fff)
  228. - `C` - Count (0x0001-0x2000)
  229. - `X` - Data
  230. *
  231. */
  232. void dcode_2()
  233. {
  234. LOG("D2 - Read/Write RAM\n");
  235. uint16_t address = 0x0000; //default 0x0000
  236. uint16_t count = 0x2000; //default 0x2000 (entire ram)
  237. if (code_seen('A')) // Address (0x0000-0x1fff)
  238. address = (strchr_pointer[1] == 'x')?strtol(strchr_pointer + 2, 0, 16):(int)code_value();
  239. if (code_seen('C')) // Count (0x0001-0x2000)
  240. count = (int)code_value();
  241. address &= 0x1fff;
  242. if (count > 0x2000) count = 0x2000;
  243. if ((address + count) > 0x2000) count = 0x2000 - address;
  244. if (code_seen('X')) // Data
  245. {
  246. uint8_t data[16];
  247. count = parse_hex(strchr_pointer + 1, data, 16);
  248. if (count > 0)
  249. {
  250. for (uint16_t i = 0; i < count; i++)
  251. *((uint8_t*)(address + i)) = data[i];
  252. LOG("%d bytes written to RAM at address %04x", count, address);
  253. }
  254. else
  255. count = 0;
  256. }
  257. print_mem(address, count, 0);
  258. /* while (count)
  259. {
  260. print_hex_word(address);
  261. putchar(' ');
  262. uint8_t countperline = 16;
  263. while (count && countperline)
  264. {
  265. uint8_t data = *((uint8_t*)address++);
  266. putchar(' ');
  267. print_hex_byte(data);
  268. countperline--;
  269. count--;
  270. }
  271. putchar('\n');
  272. }*/
  273. }
  274. /*!
  275. *
  276. ### D4 - Read/Write PIN <a href="https://reprap.org/wiki/G-code#D4:_Read.2FWrite_PIN">D4: Read/Write PIN</a>
  277. To read the digital value of a pin you need only to define the pin number.
  278. D4 [ P | F | V ]
  279. - `P` - Pin (0-255)
  280. - `F` - Function in/out (0/1)
  281. - `V` - Value (0/1)
  282. *
  283. */
  284. void dcode_4()
  285. {
  286. LOG("D4 - Read/Write PIN\n");
  287. if (code_seen('P')) // Pin (0-255)
  288. {
  289. int pin = (int)code_value();
  290. if ((pin >= 0) && (pin <= 255))
  291. {
  292. if (code_seen('F')) // Function in/out (0/1)
  293. {
  294. int fnc = (int)code_value();
  295. if (fnc == 0) pinMode(pin, INPUT);
  296. else if (fnc == 1) pinMode(pin, OUTPUT);
  297. }
  298. if (code_seen('V')) // Value (0/1)
  299. {
  300. int val = (int)code_value();
  301. if (val == 0) digitalWrite(pin, LOW);
  302. else if (val == 1) digitalWrite(pin, HIGH);
  303. }
  304. else
  305. {
  306. int val = (digitalRead(pin) != LOW)?1:0;
  307. printf("PIN%d=%d", pin, val);
  308. }
  309. }
  310. }
  311. }
  312. #endif //DEBUG_DCODES
  313. #ifdef DEBUG_DCODE5
  314. /*!
  315. *
  316. ### D5 - Read/Write FLASH <a href="https://reprap.org/wiki/G-code#D5:_Read.2FWrite_FLASH">D5: Read/Write Flash</a>
  317. This command can be used without any additional parameters. It will read the 1kb FLASH.
  318. D5 [ A | C | X | E ]
  319. - `A` - Address (0x00000-0x3ffff)
  320. - `C` - Count (0x0001-0x2000)
  321. - `X` - Data
  322. - `E` - Erase
  323. *
  324. */
  325. void dcode_5()
  326. {
  327. printf_P(PSTR("D5 - Read/Write FLASH\n"));
  328. uint32_t address = 0x0000; //default 0x0000
  329. uint16_t count = 0x0400; //default 0x0400 (1kb block)
  330. if (code_seen('A')) // Address (0x00000-0x3ffff)
  331. address = (strchr_pointer[1] == 'x')?strtol(strchr_pointer + 2, 0, 16):(int)code_value();
  332. if (code_seen('C')) // Count (0x0001-0x2000)
  333. count = (int)code_value();
  334. address &= 0x3ffff;
  335. if (count > 0x2000) count = 0x2000;
  336. if ((address + count) > 0x40000) count = 0x40000 - address;
  337. bool bErase = false;
  338. bool bCopy = false;
  339. if (code_seen('E')) //Erase
  340. bErase = true;
  341. uint8_t data[16];
  342. if (code_seen('X')) // Data
  343. {
  344. count = parse_hex(strchr_pointer + 1, data, 16);
  345. if (count > 0) bCopy = true;
  346. }
  347. if (bErase || bCopy)
  348. {
  349. if (bErase)
  350. {
  351. printf_P(PSTR("%d bytes of FLASH at address %05x will be erased\n"), count, address);
  352. }
  353. if (bCopy)
  354. {
  355. printf_P(PSTR("%d bytes will be written to FLASH at address %05x\n"), count, address);
  356. }
  357. cli();
  358. boot_app_magic = 0x55aa55aa;
  359. boot_app_flags = (bErase?(BOOT_APP_FLG_ERASE):0) | (bCopy?(BOOT_APP_FLG_COPY):0);
  360. boot_copy_size = (uint16_t)count;
  361. boot_dst_addr = (uint32_t)address;
  362. boot_src_addr = (uint32_t)(&data);
  363. bootapp_print_vars();
  364. wdt_enable(WDTO_15MS);
  365. while(1);
  366. }
  367. while (count)
  368. {
  369. print_hex_nibble(address >> 16);
  370. print_hex_word(address);
  371. putchar(' ');
  372. uint8_t countperline = 16;
  373. while (count && countperline)
  374. {
  375. uint8_t data = pgm_read_byte_far((uint8_t*)address++);
  376. putchar(' ');
  377. print_hex_byte(data);
  378. countperline--;
  379. count--;
  380. }
  381. putchar('\n');
  382. }
  383. }
  384. #endif //DEBUG_DCODE5
  385. #ifdef DEBUG_DCODES
  386. /*!
  387. *
  388. ### D6 - Read/Write external FLASH <a href="https://reprap.org/wiki/G-code#D6:_Read.2FWrite_external_FLASH">D6: Read/Write external Flash</a>
  389. Reserved
  390. *
  391. */
  392. void dcode_6()
  393. {
  394. LOG("D6 - Read/Write external FLASH\n");
  395. }
  396. /*!
  397. *
  398. ### D7 - Read/Write Bootloader <a href="https://reprap.org/wiki/G-code#D7:_Read.2FWrite_Bootloader">D7: Read/Write Bootloader</a>
  399. Reserved
  400. *
  401. */
  402. void dcode_7()
  403. {
  404. LOG("D7 - Read/Write Bootloader\n");
  405. /*
  406. cli();
  407. boot_app_magic = 0x55aa55aa;
  408. boot_app_flags = BOOT_APP_FLG_ERASE | BOOT_APP_FLG_COPY | BOOT_APP_FLG_FLASH;
  409. boot_copy_size = (uint16_t)0xc00;
  410. boot_src_addr = (uint32_t)0x0003e400;
  411. boot_dst_addr = (uint32_t)0x0003f400;
  412. wdt_enable(WDTO_15MS);
  413. while(1);
  414. */
  415. }
  416. /*!
  417. *
  418. ### D8 - Read/Write PINDA <a href="https://reprap.org/wiki/G-code#D8:_Read.2FWrite_PINDA">D8: Read/Write PINDA</a>
  419. D8 [ ? | ! | P | Z ]
  420. - `?` - Read PINDA temperature shift values
  421. - `!` - Reset PINDA temperature shift values to default
  422. - `P` - Pinda temperature [C]
  423. - `Z` - Z Offset [mm]
  424. *
  425. */
  426. void dcode_8()
  427. {
  428. printf_P(PSTR("D8 - Read/Write PINDA\n"));
  429. uint8_t cal_status = calibration_status_pinda();
  430. float temp_pinda = current_temperature_pinda;
  431. float offset_z = temp_compensation_pinda_thermistor_offset(temp_pinda);
  432. if ((strchr_pointer[1+1] == '?') || (strchr_pointer[1+1] == 0))
  433. {
  434. printf_P(PSTR("cal_status=%d\n"), cal_status?1:0);
  435. for (uint8_t i = 0; i < 6; i++)
  436. {
  437. uint16_t offs = 0;
  438. if (i > 0) offs = eeprom_read_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + (i - 1));
  439. float foffs = ((float)offs) / cs.axis_steps_per_unit[Z_AXIS];
  440. offs = 1000 * foffs;
  441. printf_P(PSTR("temp_pinda=%dC temp_shift=%dum\n"), 35 + i * 5, offs);
  442. }
  443. }
  444. else if (strchr_pointer[1+1] == '!')
  445. {
  446. cal_status = 1;
  447. eeprom_write_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_PINDA, cal_status);
  448. eeprom_write_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + 0, 8); //40C - 20um - 8usteps
  449. eeprom_write_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + 1, 24); //45C - 60um - 24usteps
  450. eeprom_write_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + 2, 48); //50C - 120um - 48usteps
  451. eeprom_write_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + 3, 80); //55C - 200um - 80usteps
  452. eeprom_write_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + 4, 120); //60C - 300um - 120usteps
  453. }
  454. else
  455. {
  456. if (code_seen('P')) // Pinda temperature [C]
  457. temp_pinda = code_value();
  458. offset_z = temp_compensation_pinda_thermistor_offset(temp_pinda);
  459. if (code_seen('Z')) // Z Offset [mm]
  460. {
  461. offset_z = code_value();
  462. }
  463. }
  464. printf_P(PSTR("temp_pinda=%d offset_z=%d.%03d\n"), (int)temp_pinda, (int)offset_z, ((int)(1000 * offset_z) % 1000));
  465. }
  466. /*!
  467. *
  468. ### D9 - Read ADC <a href="https://reprap.org/wiki/G-code#D9:_Read.2FWrite_ADC">D9: Read ADC</a>
  469. D9 [ I | V ]
  470. - `I` - ADC channel index
  471. - `0` - Heater 0 temperature
  472. - `1` - Heater 1 temperature
  473. - `2` - Bed temperature
  474. - `3` - PINDA temperature
  475. - `4` - PWR voltage
  476. - `5` - Ambient temperature
  477. - `6` - BED voltage
  478. - `V` Value to be written as simulated
  479. *
  480. */
  481. const char* dcode_9_ADC_name(uint8_t i)
  482. {
  483. switch (i)
  484. {
  485. case 0: return PSTR("TEMP_HEATER0");
  486. case 1: return PSTR("TEMP_HEATER1");
  487. case 2: return PSTR("TEMP_BED");
  488. case 3: return PSTR("TEMP_PINDA");
  489. case 4: return PSTR("VOLT_PWR");
  490. case 5: return PSTR("TEMP_AMBIENT");
  491. case 6: return PSTR("VOLT_BED");
  492. }
  493. return 0;
  494. }
  495. #ifdef AMBIENT_THERMISTOR
  496. extern int current_temperature_raw_ambient;
  497. #endif //AMBIENT_THERMISTOR
  498. #ifdef VOLT_PWR_PIN
  499. extern int current_voltage_raw_pwr;
  500. #endif //VOLT_PWR_PIN
  501. #ifdef VOLT_BED_PIN
  502. extern int current_voltage_raw_bed;
  503. #endif //VOLT_BED_PIN
  504. uint16_t dcode_9_ADC_val(uint8_t i)
  505. {
  506. switch (i)
  507. {
  508. case 0: return current_temperature_raw[0];
  509. case 1: return 0;
  510. case 2: return current_temperature_bed_raw;
  511. case 3: return current_temperature_raw_pinda;
  512. #ifdef VOLT_PWR_PIN
  513. case 4: return current_voltage_raw_pwr;
  514. #endif //VOLT_PWR_PIN
  515. #ifdef AMBIENT_THERMISTOR
  516. case 5: return current_temperature_raw_ambient;
  517. #endif //AMBIENT_THERMISTOR
  518. #ifdef VOLT_BED_PIN
  519. case 6: return current_voltage_raw_bed;
  520. #endif //VOLT_BED_PIN
  521. }
  522. return 0;
  523. }
  524. void dcode_9()
  525. {
  526. printf_P(PSTR("D9 - Read/Write ADC\n"));
  527. if ((strchr_pointer[1+1] == '?') || (strchr_pointer[1+1] == 0))
  528. {
  529. for (uint8_t i = 0; i < ADC_CHAN_CNT; i++)
  530. printf_P(PSTR("\tADC%d=%4d\t(%S)\n"), i, dcode_9_ADC_val(i) >> 4, dcode_9_ADC_name(i));
  531. }
  532. else
  533. {
  534. uint8_t index = 0xff;
  535. if (code_seen('I')) // index (index of used channel, not avr channel index)
  536. index = code_value();
  537. if (index < ADC_CHAN_CNT)
  538. {
  539. if (code_seen('V')) // value to be written as simulated
  540. {
  541. adc_sim_mask |= (1 << index);
  542. adc_values[index] = (((int)code_value()) << 4);
  543. printf_P(PSTR("ADC%d=%4d\n"), index, adc_values[index] >> 4);
  544. }
  545. }
  546. }
  547. }
  548. /*!
  549. *
  550. ### D10 - Set XYZ calibration = OK <a href="https://reprap.org/wiki/G-code#D10:_Set_XYZ_calibration_.3D_OK">D10: Set XYZ calibration = OK</a>
  551. *
  552. */
  553. void dcode_10()
  554. {//Tell the printer that XYZ calibration went OK
  555. LOG("D10 - XYZ calibration = OK\n");
  556. calibration_status_store(CALIBRATION_STATUS_LIVE_ADJUST);
  557. }
  558. /*!
  559. *
  560. ### D12 - Time <a href="https://reprap.org/wiki/G-code#D12:_Time">D12: Time</a>
  561. *
  562. */
  563. void dcode_12()
  564. {//Time
  565. LOG("D12 - Time\n");
  566. }
  567. #ifdef TMC2130
  568. #include "planner.h"
  569. #include "tmc2130.h"
  570. extern void st_synchronize();
  571. /**
  572. * @brief D2130 Trinamic stepper controller
  573. * D2130<axis><command>[subcommand][value]
  574. * * Axis
  575. * * * 'X'
  576. * * * 'Y'
  577. * * * 'Z'
  578. * * * 'E'
  579. * * command
  580. * * * '0' current off
  581. * * * '1' current on
  582. * * * '+' single step
  583. * * * * value sereval steps
  584. * * * '-' dtto oposite direction
  585. * * * '?' read register
  586. * * * * "mres"
  587. * * * * "step"
  588. * * * * "mscnt"
  589. * * * * "mscuract"
  590. * * * * "wave"
  591. * * * '!' set register
  592. * * * * "mres"
  593. * * * * "step"
  594. * * * * "wave"
  595. * * * * *0, 180..250 meaning: off, 0.9..1.25, recommended value is 1.1
  596. * * * '@' home calibrate axis
  597. *
  598. * Example:
  599. * D2130E?wave //print extruder microstep linearity compensation curve
  600. * D2130E!wave0 //disable extruder linearity compensation curve, (sine curve is used)
  601. * D2130E!wave220 // (sin(x))^1.1 extruder microstep compensation curve used
  602. */
  603. void dcode_2130()
  604. {
  605. printf_P(PSTR("D2130 - TMC2130\n"));
  606. uint8_t axis = 0xff;
  607. switch (strchr_pointer[1+4])
  608. {
  609. case 'X': axis = X_AXIS; break;
  610. case 'Y': axis = Y_AXIS; break;
  611. case 'Z': axis = Z_AXIS; break;
  612. case 'E': axis = E_AXIS; break;
  613. }
  614. if (axis != 0xff)
  615. {
  616. char ch_axis = strchr_pointer[1+4];
  617. if (strchr_pointer[1+5] == '0') { tmc2130_set_pwr(axis, 0); }
  618. else if (strchr_pointer[1+5] == '1') { tmc2130_set_pwr(axis, 1); }
  619. else if (strchr_pointer[1+5] == '+')
  620. {
  621. if (strchr_pointer[1+6] == 0)
  622. {
  623. tmc2130_set_dir(axis, 0);
  624. tmc2130_do_step(axis);
  625. }
  626. else
  627. {
  628. uint8_t steps = atoi(strchr_pointer + 1 + 6);
  629. tmc2130_do_steps(axis, steps, 0, 1000);
  630. }
  631. }
  632. else if (strchr_pointer[1+5] == '-')
  633. {
  634. if (strchr_pointer[1+6] == 0)
  635. {
  636. tmc2130_set_dir(axis, 1);
  637. tmc2130_do_step(axis);
  638. }
  639. else
  640. {
  641. uint8_t steps = atoi(strchr_pointer + 1 + 6);
  642. tmc2130_do_steps(axis, steps, 1, 1000);
  643. }
  644. }
  645. else if (strchr_pointer[1+5] == '?')
  646. {
  647. if (strcmp(strchr_pointer + 7, "mres") == 0) printf_P(PSTR("%c mres=%d\n"), ch_axis, tmc2130_mres[axis]);
  648. else if (strcmp(strchr_pointer + 7, "step") == 0) printf_P(PSTR("%c step=%d\n"), ch_axis, tmc2130_rd_MSCNT(axis) >> tmc2130_mres[axis]);
  649. else if (strcmp(strchr_pointer + 7, "mscnt") == 0) printf_P(PSTR("%c MSCNT=%d\n"), ch_axis, tmc2130_rd_MSCNT(axis));
  650. else if (strcmp(strchr_pointer + 7, "mscuract") == 0)
  651. {
  652. uint32_t val = tmc2130_rd_MSCURACT(axis);
  653. int curA = (val & 0xff);
  654. int curB = ((val >> 16) & 0xff);
  655. if ((val << 7) & 0x8000) curA -= 256;
  656. if ((val >> 9) & 0x8000) curB -= 256;
  657. printf_P(PSTR("%c MSCURACT=0x%08lx A=%d B=%d\n"), ch_axis, val, curA, curB);
  658. }
  659. else if (strcmp(strchr_pointer + 7, "wave") == 0)
  660. {
  661. tmc2130_get_wave(axis, 0, stdout);
  662. }
  663. }
  664. else if (strchr_pointer[1+5] == '!')
  665. {
  666. if (strncmp(strchr_pointer + 7, "step", 4) == 0)
  667. {
  668. uint8_t step = atoi(strchr_pointer + 11);
  669. uint16_t res = tmc2130_get_res(axis);
  670. tmc2130_goto_step(axis, step & (4*res - 1), 2, 1000, res);
  671. }
  672. else if (strncmp(strchr_pointer + 7, "mres", 4) == 0)
  673. {
  674. uint8_t mres = strchr_pointer[11] - '0';
  675. if (mres <= 8)
  676. {
  677. st_synchronize();
  678. uint16_t res = tmc2130_get_res(axis);
  679. uint16_t res_new = tmc2130_mres2usteps(mres);
  680. tmc2130_set_res(axis, res_new);
  681. if (res_new > res)
  682. cs.axis_steps_per_unit[axis] *= (res_new / res);
  683. else
  684. cs.axis_steps_per_unit[axis] /= (res / res_new);
  685. }
  686. }
  687. else if (strncmp(strchr_pointer + 7, "wave", 4) == 0)
  688. {
  689. uint8_t fac1000 = atoi(strchr_pointer + 11) & 0xffff;
  690. if (fac1000 < TMC2130_WAVE_FAC1000_MIN) fac1000 = 0;
  691. if (fac1000 > TMC2130_WAVE_FAC1000_MAX) fac1000 = TMC2130_WAVE_FAC1000_MAX;
  692. tmc2130_set_wave(axis, 247, fac1000);
  693. tmc2130_wave_fac[axis] = fac1000;
  694. }
  695. }
  696. else if (strchr_pointer[1+5] == '@')
  697. {
  698. tmc2130_home_calibrate(axis);
  699. }
  700. }
  701. }
  702. #endif //TMC2130
  703. #ifdef PAT9125
  704. /*!
  705. *
  706. ### D9125 - PAT9125 filament sensor <a href="https://reprap.org/wiki/G-code#D9:_Read.2FWrite_ADC">D9125: PAT9125 filament sensor</a>
  707. D9125 [ ? | ! | R | X | Y | L ]
  708. - `?` - Print values
  709. - `!` - Print values
  710. - `R` - Resolution. Not active in code
  711. - `X` - X values
  712. - `Y` - Y values
  713. - `L` - Activate filament sensor log
  714. *
  715. */
  716. void dcode_9125()
  717. {
  718. LOG("D9125 - PAT9125\n");
  719. if ((strchr_pointer[1+4] == '?') || (strchr_pointer[1+4] == 0))
  720. {
  721. // 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);
  722. printf("x=%d y=%d b=%d s=%d\n", pat9125_x, pat9125_y, pat9125_b, pat9125_s);
  723. return;
  724. }
  725. if (strchr_pointer[1+4] == '!')
  726. {
  727. pat9125_update();
  728. printf("x=%d y=%d b=%d s=%d\n", pat9125_x, pat9125_y, pat9125_b, pat9125_s);
  729. return;
  730. }
  731. /*
  732. if (code_seen('R'))
  733. {
  734. unsigned char res = (int)code_value();
  735. LOG("pat9125_init(xres=yres=%d)=%d\n", res, pat9125_init(res, res));
  736. }
  737. */
  738. if (code_seen('X'))
  739. {
  740. pat9125_x = (int)code_value();
  741. LOG("pat9125_x=%d\n", pat9125_x);
  742. }
  743. if (code_seen('Y'))
  744. {
  745. pat9125_y = (int)code_value();
  746. LOG("pat9125_y=%d\n", pat9125_y);
  747. }
  748. if (code_seen('L'))
  749. {
  750. fsensor_log = (int)code_value();
  751. LOG("fsensor_log=%d\n", fsensor_log);
  752. }
  753. }
  754. #endif //PAT9125
  755. #endif //DEBUG_DCODES