Dcodes.cpp 26 KB

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