Dcodes.cpp 22 KB

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