Dcodes.cpp 27 KB

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