Dcodes.cpp 24 KB

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