Dcodes.cpp 23 KB

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