Dcodes.cpp 22 KB

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