Dcodes.cpp 24 KB

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