My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

M916-918.cpp 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. //
  23. // NOTE: All tests assume each axis uses matching driver chips.
  24. //
  25. #include "../../../inc/MarlinConfig.h"
  26. #if HAS_L64XX
  27. #include "../../gcode.h"
  28. #include "../../../module/stepper/indirection.h"
  29. #include "../../../module/planner.h"
  30. #include "../../../libs/L64XX/L64XX_Marlin.h"
  31. #define DEBUG_OUT ENABLED(L6470_CHITCHAT)
  32. #include "../../../core/debug_out.h"
  33. /**
  34. * M916: increase KVAL_HOLD until get thermal warning
  35. * NOTE - on L6474 it is TVAL that is used
  36. *
  37. * J - select which driver(s) to monitor on multi-driver axis
  38. * 0 - (default) monitor all drivers on the axis or E0
  39. * 1 - monitor only X, Y, Z, E1
  40. * 2 - monitor only X2, Y2, Z2, E2
  41. * 3 - monitor only Z3, E3
  42. * 4 - monitor only Z4, E4
  43. *
  44. * Xxxx, Yxxx, Zxxx, Exxx - axis to be monitored with displacement
  45. * xxx (1-255) is distance moved on either side of current position
  46. *
  47. * F - feedrate
  48. * optional - will use default max feedrate from configuration.h if not specified
  49. *
  50. * T - current (mA) setting for TVAL (0 - 4A in 31.25mA increments, rounds down) - L6474 only
  51. * optional - will report current value from driver if not specified
  52. *
  53. * K - value for KVAL_HOLD (0 - 255) (ignored for L6474)
  54. * optional - will report current value from driver if not specified
  55. *
  56. * D - time (in seconds) to run each setting of KVAL_HOLD/TVAL
  57. * optional - defaults to zero (runs each setting once)
  58. */
  59. /**
  60. * This routine is also useful for determining the approximate KVAL_HOLD
  61. * where the stepper stops losing steps. The sound will get noticeably quieter
  62. * as it stops losing steps.
  63. */
  64. void GcodeSuite::M916() {
  65. DEBUG_ECHOLNPGM("M916");
  66. L64xxManager.pause_monitor(true); // Keep monitor_driver() from stealing status
  67. // Variables used by L64xxManager.get_user_input function - some may not be used
  68. char axis_mon[3][3] = { {" "}, {" "}, {" "} }; // list of Axes to be monitored
  69. L64XX_axis_t axis_index[3];
  70. uint16_t axis_status[3];
  71. uint8_t driver_count = 1;
  72. float position_max;
  73. float position_min;
  74. float final_feedrate;
  75. uint8_t kval_hold;
  76. uint8_t OCD_TH_val = 0;
  77. uint8_t STALL_TH_val = 0;
  78. uint16_t over_current_threshold;
  79. constexpr uint8_t over_current_flag = false; // M916 doesn't play with the overcurrent thresholds
  80. #define DRIVER_TYPE_L6474(Q) AXIS_DRIVER_TYPE_##Q(L6474)
  81. uint8_t j; // general purpose counter
  82. if (L64xxManager.get_user_input(driver_count, axis_index, axis_mon, position_max, position_min, final_feedrate, kval_hold, over_current_flag, OCD_TH_val, STALL_TH_val, over_current_threshold))
  83. return; // quit if invalid user input
  84. DEBUG_ECHOLNPAIR("feedrate = ", final_feedrate);
  85. planner.synchronize(); // wait for all current movement commands to complete
  86. const L64XX_Marlin::L64XX_shadow_t &sh = L64xxManager.shadow;
  87. for (j = 0; j < driver_count; j++)
  88. L64xxManager.get_status(axis_index[j]); // clear out any pre-existing error flags
  89. char temp_axis_string[] = " ";
  90. temp_axis_string[0] = axis_mon[0][0]; // need to have a string for use within sprintf format section
  91. char gcode_string[80];
  92. uint16_t status_composite = 0;
  93. uint16_t M91x_counter = kval_hold;
  94. uint16_t M91x_counter_max;
  95. if (sh.STATUS_AXIS_LAYOUT == L6474_STATUS_LAYOUT) {
  96. M91x_counter_max = 128; // TVAL is 7 bits
  97. LIMIT(M91x_counter, 0U, 127U);
  98. }
  99. else
  100. M91x_counter_max = 256; // KVAL_HOLD is 8 bits
  101. uint8_t M91x_delay_s = parser.byteval('D'); // get delay in seconds
  102. millis_t M91x_delay_ms = M91x_delay_s * 60 * 1000;
  103. millis_t M91x_delay_end;
  104. DEBUG_ECHOLNPGM(".\n.");
  105. do {
  106. if (sh.STATUS_AXIS_LAYOUT == L6474_STATUS_LAYOUT)
  107. DEBUG_ECHOLNPAIR("TVAL current (mA) = ", (M91x_counter + 1) * sh.AXIS_STALL_CURRENT_CONSTANT_INV); // report TVAL current for this run
  108. else
  109. DEBUG_ECHOLNPAIR("kval_hold = ", M91x_counter); // report KVAL_HOLD for this run
  110. for (j = 0; j < driver_count; j++)
  111. L64xxManager.set_param(axis_index[j], L6470_KVAL_HOLD, M91x_counter); //set KVAL_HOLD or TVAL (same register address)
  112. M91x_delay_end = millis() + M91x_delay_ms;
  113. do {
  114. // turn the motor(s) both directions
  115. sprintf_P(gcode_string, PSTR("G0 %s%03d F%03d"), temp_axis_string, uint16_t(position_min), uint16_t(final_feedrate));
  116. gcode.process_subcommands_now_P(gcode_string);
  117. sprintf_P(gcode_string, PSTR("G0 %s%03d F%03d"), temp_axis_string, uint16_t(position_max), uint16_t(final_feedrate));
  118. gcode.process_subcommands_now_P(gcode_string);
  119. // get the status after the motors have stopped
  120. planner.synchronize();
  121. status_composite = 0; // clear out the old bits
  122. for (j = 0; j < driver_count; j++) {
  123. axis_status[j] = (~L64xxManager.get_status(axis_index[j])) & sh.L6470_ERROR_MASK; // bits of interest are all active low
  124. status_composite |= axis_status[j] ;
  125. }
  126. if (status_composite) break;
  127. } while (millis() < M91x_delay_end);
  128. if (status_composite) break;
  129. M91x_counter++;
  130. } while (!(status_composite & (sh.STATUS_AXIS_TH_WRN | sh.STATUS_AXIS_TH_SD)) && (M91x_counter < M91x_counter_max));
  131. DEBUG_ECHOLNPGM(".");
  132. #if ENABLED(L6470_CHITCHAT)
  133. if (status_composite) {
  134. L64xxManager.error_status_decode(status_composite, axis_index[0],
  135. sh.STATUS_AXIS_TH_SD, sh.STATUS_AXIS_TH_WRN,
  136. sh.STATUS_AXIS_STEP_LOSS_A, sh.STATUS_AXIS_STEP_LOSS_B,
  137. sh.STATUS_AXIS_OCD, sh.STATUS_AXIS_LAYOUT);
  138. DEBUG_ECHOLNPGM(".");
  139. }
  140. #endif
  141. if ((status_composite & (sh.STATUS_AXIS_TH_WRN | sh.STATUS_AXIS_TH_SD)))
  142. DEBUG_ECHOLNPGM(".\n.\nTest completed normally - Thermal warning/shutdown has occurred");
  143. else if (status_composite)
  144. DEBUG_ECHOLNPGM(".\n.\nTest completed abnormally - non-thermal error has occured");
  145. else
  146. DEBUG_ECHOLNPGM(".\n.\nTest completed normally - Unable to get to thermal warning/shutdown");
  147. L64xxManager.pause_monitor(false);
  148. }
  149. /**
  150. * M917: Find minimum current thresholds
  151. *
  152. * Decrease OCD current until overcurrent error
  153. * Increase OCD until overcurrent error goes away
  154. * Decrease stall threshold until stall (not done on L6474)
  155. * Increase stall until stall error goes away (not done on L6474)
  156. *
  157. * J - select which driver(s) to monitor on multi-driver axis
  158. * 0 - (default) monitor all drivers on the axis or E0
  159. * 1 - monitor only X, Y, Z, E1
  160. * 2 - monitor only X2, Y2, Z2, E2
  161. * Xxxx, Yxxx, Zxxx, Exxx - axis to be monitored with displacement
  162. * xxx (1-255) is distance moved on either side of current position
  163. *
  164. * F - feedrate
  165. * optional - will use default max feedrate from Configuration.h if not specified
  166. *
  167. * I - starting over-current threshold
  168. * optional - will report current value from driver if not specified
  169. * if there are multiple drivers on the axis then all will be set the same
  170. *
  171. * T - current (mA) setting for TVAL (0 - 4A in 31.25mA increments, rounds down) - L6474 only
  172. * optional - will report current value from driver if not specified
  173. *
  174. * K - value for KVAL_HOLD (0 - 255) (ignored for L6474)
  175. * optional - will report current value from driver if not specified
  176. */
  177. void GcodeSuite::M917() {
  178. DEBUG_ECHOLNPGM("M917");
  179. L64xxManager.pause_monitor(true); // Keep monitor_driver() from stealing status
  180. char axis_mon[3][3] = { {" "}, {" "}, {" "} }; // list of Axes to be monitored
  181. L64XX_axis_t axis_index[3];
  182. uint16_t axis_status[3];
  183. uint8_t driver_count = 1;
  184. float position_max;
  185. float position_min;
  186. float final_feedrate;
  187. uint8_t kval_hold;
  188. uint8_t OCD_TH_val = 0;
  189. uint8_t STALL_TH_val = 0;
  190. uint16_t over_current_threshold;
  191. constexpr uint8_t over_current_flag = true;
  192. uint8_t j; // general purpose counter
  193. if (L64xxManager.get_user_input(driver_count, axis_index, axis_mon, position_max, position_min, final_feedrate, kval_hold, over_current_flag, OCD_TH_val, STALL_TH_val, over_current_threshold))
  194. return; // quit if invalid user input
  195. DEBUG_ECHOLNPAIR("feedrate = ", final_feedrate);
  196. planner.synchronize(); // wait for all current movement commands to complete
  197. const L64XX_Marlin::L64XX_shadow_t &sh = L64xxManager.shadow;
  198. for (j = 0; j < driver_count; j++)
  199. L64xxManager.get_status(axis_index[j]); // clear error flags
  200. char temp_axis_string[] = " ";
  201. temp_axis_string[0] = axis_mon[0][0]; // need a sprintf format string
  202. char gcode_string[80];
  203. uint16_t status_composite = 0;
  204. uint8_t test_phase = 0; // 0 - decreasing OCD - exit when OCD warning occurs (ignore STALL)
  205. // 1 - increasing OCD - exit when OCD warning stops (ignore STALL)
  206. // 2 - OCD finalized - decreasing STALL - exit when STALL warning happens
  207. // 3 - OCD finalized - increasing STALL - exit when STALL warning stop
  208. // 4 - all testing completed
  209. DEBUG_ECHOPAIR(".\n.\n.\nover_current threshold : ", (OCD_TH_val + 1) * 375); // first status display
  210. DEBUG_ECHOPAIR(" (OCD_TH: : ", OCD_TH_val);
  211. if (sh.STATUS_AXIS_LAYOUT != L6474_STATUS_LAYOUT) {
  212. DEBUG_ECHOPAIR(") Stall threshold: ", (STALL_TH_val + 1) * 31.25);
  213. DEBUG_ECHOPAIR(" (STALL_TH: ", STALL_TH_val);
  214. }
  215. DEBUG_ECHOLNPGM(")");
  216. do {
  217. if (sh.STATUS_AXIS_LAYOUT != L6474_STATUS_LAYOUT) DEBUG_ECHOPAIR("STALL threshold : ", (STALL_TH_val + 1) * 31.25);
  218. DEBUG_ECHOLNPAIR(" OCD threshold : ", (OCD_TH_val + 1) * 375);
  219. sprintf_P(gcode_string, PSTR("G0 %s%03d F%03d"), temp_axis_string, uint16_t(position_min), uint16_t(final_feedrate));
  220. gcode.process_subcommands_now_P(gcode_string);
  221. sprintf_P(gcode_string, PSTR("G0 %s%03d F%03d"), temp_axis_string, uint16_t(position_max), uint16_t(final_feedrate));
  222. gcode.process_subcommands_now_P(gcode_string);
  223. planner.synchronize();
  224. status_composite = 0; // clear out the old bits
  225. for (j = 0; j < driver_count; j++) {
  226. axis_status[j] = (~L64xxManager.get_status(axis_index[j])) & sh.L6470_ERROR_MASK; // bits of interest are all active low
  227. status_composite |= axis_status[j];
  228. }
  229. if (status_composite && (status_composite & sh.STATUS_AXIS_UVLO)) {
  230. DEBUG_ECHOLNPGM("Test aborted (Undervoltage lockout active)");
  231. #if ENABLED(L6470_CHITCHAT)
  232. for (j = 0; j < driver_count; j++) {
  233. if (j) DEBUG_ECHOPGM("...");
  234. L64xxManager.error_status_decode(axis_status[j], axis_index[j],
  235. sh.STATUS_AXIS_TH_SD, sh.STATUS_AXIS_TH_WRN,
  236. sh.STATUS_AXIS_STEP_LOSS_A, sh.STATUS_AXIS_STEP_LOSS_B,
  237. sh.STATUS_AXIS_OCD, sh.STATUS_AXIS_LAYOUT);
  238. }
  239. #endif
  240. return;
  241. }
  242. if (status_composite & (sh.STATUS_AXIS_TH_WRN | sh.STATUS_AXIS_TH_SD)) {
  243. DEBUG_ECHOLNPGM("thermal problem - waiting for chip(s) to cool down ");
  244. uint16_t status_composite_temp = 0;
  245. uint8_t k = 0;
  246. do {
  247. k++;
  248. if (!(k % 4)) {
  249. kval_hold *= 0.95;
  250. DEBUG_EOL();
  251. DEBUG_ECHOLNPAIR("Lowering KVAL_HOLD by about 5% to ", kval_hold);
  252. for (j = 0; j < driver_count; j++)
  253. L64xxManager.set_param(axis_index[j], L6470_KVAL_HOLD, kval_hold);
  254. }
  255. DEBUG_ECHOLNPGM(".");
  256. gcode.reset_stepper_timeout(); // reset_stepper_timeout to keep steppers powered
  257. watchdog_refresh();; // beat the dog
  258. safe_delay(5000);
  259. status_composite_temp = 0;
  260. for (j = 0; j < driver_count; j++) {
  261. axis_status[j] = (~L64xxManager.get_status(axis_index[j])) & sh.L6470_ERROR_MASK; // bits of interest are all active low
  262. status_composite_temp |= axis_status[j];
  263. }
  264. }
  265. while (status_composite_temp & (sh.STATUS_AXIS_TH_WRN | sh.STATUS_AXIS_TH_SD));
  266. DEBUG_EOL();
  267. }
  268. if (status_composite & (sh.STATUS_AXIS_STEP_LOSS_A | sh.STATUS_AXIS_STEP_LOSS_B | sh.STATUS_AXIS_OCD)) {
  269. switch (test_phase) {
  270. case 0: {
  271. if (status_composite & sh.STATUS_AXIS_OCD) {
  272. // phase 0 with OCD warning - time to go to next phase
  273. if (OCD_TH_val >= sh.AXIS_OCD_TH_MAX) {
  274. OCD_TH_val = sh.AXIS_OCD_TH_MAX; // limit to max
  275. test_phase = 2; // at highest value so skip phase 1
  276. //DEBUG_ECHOLNPGM("LOGIC E0A OCD at highest - skip to 2");
  277. DEBUG_ECHOLNPGM("OCD at highest - OCD finalized");
  278. }
  279. else {
  280. OCD_TH_val++; // normal exit to next phase
  281. test_phase = 1; // setup for first pass of phase 1
  282. //DEBUG_ECHOLNPGM("LOGIC E0B - inc OCD & go to 1");
  283. DEBUG_ECHOLNPGM("inc OCD");
  284. }
  285. }
  286. else { // phase 0 without OCD warning - keep on decrementing if can
  287. if (OCD_TH_val) {
  288. OCD_TH_val--; // try lower value
  289. //DEBUG_ECHOLNPGM("LOGIC E0C - dec OCD");
  290. DEBUG_ECHOLNPGM("dec OCD");
  291. }
  292. else {
  293. test_phase = 2; // at lowest value without warning so skip phase 1
  294. //DEBUG_ECHOLNPGM("LOGIC E0D - OCD at latest - go to 2");
  295. DEBUG_ECHOLNPGM("OCD finalized");
  296. }
  297. }
  298. } break;
  299. case 1: {
  300. if (status_composite & sh.STATUS_AXIS_OCD) {
  301. // phase 1 with OCD warning - increment if can
  302. if (OCD_TH_val >= sh.AXIS_OCD_TH_MAX) {
  303. OCD_TH_val = sh.AXIS_OCD_TH_MAX; // limit to max
  304. test_phase = 2; // at highest value so go to next phase
  305. //DEBUG_ECHOLNPGM("LOGIC E1A - OCD at max - go to 2");
  306. DEBUG_ECHOLNPGM("OCD finalized");
  307. }
  308. else {
  309. OCD_TH_val++; // try a higher value
  310. //DEBUG_ECHOLNPGM("LOGIC E1B - inc OCD");
  311. DEBUG_ECHOLNPGM("inc OCD");
  312. }
  313. }
  314. else { // phase 1 without OCD warning - normal exit to phase 2
  315. test_phase = 2;
  316. //DEBUG_ECHOLNPGM("LOGIC E1C - no OCD warning - go to 1");
  317. DEBUG_ECHOLNPGM("OCD finalized");
  318. }
  319. } break;
  320. case 2: {
  321. if (sh.STATUS_AXIS_LAYOUT == L6474_STATUS_LAYOUT) { // skip all STALL_TH steps if L6474
  322. test_phase = 4;
  323. break;
  324. }
  325. if (status_composite & (sh.STATUS_AXIS_STEP_LOSS_A | sh.STATUS_AXIS_STEP_LOSS_B)) {
  326. // phase 2 with stall warning - time to go to next phase
  327. if (STALL_TH_val >= 127) {
  328. STALL_TH_val = 127; // limit to max
  329. //DEBUG_ECHOLNPGM("LOGIC E2A - STALL warning, STALL at max, quit");
  330. DEBUG_ECHOLNPGM("finished - STALL at maximum value but still have stall warning");
  331. test_phase = 4;
  332. }
  333. else {
  334. test_phase = 3; // normal exit to next phase (found failing value of STALL)
  335. STALL_TH_val++; // setup for first pass of phase 3
  336. //DEBUG_ECHOLNPGM("LOGIC E2B - INC - STALL warning, inc Stall, go to 3");
  337. DEBUG_ECHOLNPGM("inc Stall");
  338. }
  339. }
  340. else { // phase 2 without stall warning - decrement if can
  341. if (STALL_TH_val) {
  342. STALL_TH_val--; // try a lower value
  343. //DEBUG_ECHOLNPGM("LOGIC E2C - no STALL, dec STALL");
  344. DEBUG_ECHOLNPGM("dec STALL");
  345. }
  346. else {
  347. DEBUG_ECHOLNPGM("finished - STALL at lowest value but still do NOT have stall warning");
  348. test_phase = 4;
  349. //DEBUG_ECHOLNPGM("LOGIC E2D - no STALL, at lowest so quit");
  350. }
  351. }
  352. } break;
  353. case 3: {
  354. if (sh.STATUS_AXIS_LAYOUT == L6474_STATUS_LAYOUT) { // skip all STALL_TH steps if L6474
  355. test_phase = 4;
  356. break;
  357. }
  358. if (status_composite & (sh.STATUS_AXIS_STEP_LOSS_A | sh.STATUS_AXIS_STEP_LOSS_B)) {
  359. // phase 3 with stall warning - increment if can
  360. if (STALL_TH_val >= 127) {
  361. STALL_TH_val = 127; // limit to max
  362. DEBUG_ECHOLNPGM("finished - STALL at maximum value but still have stall warning");
  363. test_phase = 4;
  364. //DEBUG_ECHOLNPGM("LOGIC E3A - STALL, at max so quit");
  365. }
  366. else {
  367. STALL_TH_val++; // still looking for passing value
  368. //DEBUG_ECHOLNPGM("LOGIC E3B - STALL, inc stall");
  369. DEBUG_ECHOLNPGM("inc stall");
  370. }
  371. }
  372. else { //phase 3 without stall warning but have OCD warning
  373. DEBUG_ECHOLNPGM("Hardware problem - OCD warning without STALL warning");
  374. test_phase = 4;
  375. //DEBUG_ECHOLNPGM("LOGIC E3C - not STALLED, hardware problem (quit)");
  376. }
  377. } break;
  378. }
  379. }
  380. else {
  381. switch (test_phase) {
  382. case 0: { // phase 0 without OCD warning - keep on decrementing if can
  383. if (OCD_TH_val) {
  384. OCD_TH_val--; // try lower value
  385. //DEBUG_ECHOLNPGM("LOGIC N0A - DEC OCD");
  386. DEBUG_ECHOLNPGM("DEC OCD");
  387. }
  388. else {
  389. test_phase = 2; // at lowest value without warning so skip phase 1
  390. //DEBUG_ECHOLNPGM("LOGIC N0B - OCD at lowest (go to phase 2)");
  391. DEBUG_ECHOLNPGM("OCD finalized");
  392. }
  393. } break;
  394. case 1: //DEBUG_ECHOLNPGM("LOGIC N1 (go directly to 2)"); // phase 1 without OCD warning - drop directly to phase 2
  395. DEBUG_ECHOLNPGM("OCD finalized");
  396. case 2: { // phase 2 without stall warning - keep on decrementing if can
  397. if (sh.STATUS_AXIS_LAYOUT == L6474_STATUS_LAYOUT) { // skip all STALL_TH steps if L6474
  398. test_phase = 4;
  399. break;
  400. }
  401. if (STALL_TH_val) {
  402. STALL_TH_val--; // try a lower value (stay in phase 2)
  403. //DEBUG_ECHOLNPGM("LOGIC N2B - dec STALL");
  404. DEBUG_ECHOLNPGM("dec STALL");
  405. }
  406. else {
  407. DEBUG_ECHOLNPGM("finished - STALL at lowest value but still no stall warning");
  408. test_phase = 4;
  409. //DEBUG_ECHOLNPGM("LOGIC N2C - STALL at lowest (quit)");
  410. }
  411. } break;
  412. case 3: {
  413. if (sh.STATUS_AXIS_LAYOUT == L6474_STATUS_LAYOUT) { // skip all STALL_TH steps if L6474
  414. test_phase = 4;
  415. break;
  416. }
  417. test_phase = 4;
  418. //DEBUG_ECHOLNPGM("LOGIC N3 - finished!");
  419. DEBUG_ECHOLNPGM("finished!");
  420. } break; // phase 3 without any warnings - desired exit
  421. } //
  422. } // end of status checks
  423. if (test_phase != 4) {
  424. for (j = 0; j < driver_count; j++) { // update threshold(s)
  425. L64xxManager.set_param(axis_index[j], L6470_OCD_TH, OCD_TH_val);
  426. if (sh.STATUS_AXIS_LAYOUT != L6474_STATUS_LAYOUT) L64xxManager.set_param(axis_index[j], L6470_STALL_TH, STALL_TH_val);
  427. if (L64xxManager.get_param(axis_index[j], L6470_OCD_TH) != OCD_TH_val) DEBUG_ECHOLNPGM("OCD mismatch");
  428. if ((L64xxManager.get_param(axis_index[j], L6470_STALL_TH) != STALL_TH_val) && (sh.STATUS_AXIS_LAYOUT != L6474_STATUS_LAYOUT)) DEBUG_ECHOLNPGM("STALL mismatch");
  429. }
  430. }
  431. } while (test_phase != 4);
  432. DEBUG_ECHOLNPGM(".");
  433. if (status_composite) {
  434. #if ENABLED(L6470_CHITCHAT)
  435. for (j = 0; j < driver_count; j++) {
  436. if (j) DEBUG_ECHOPGM("...");
  437. L64xxManager.error_status_decode(axis_status[j], axis_index[j],
  438. sh.STATUS_AXIS_TH_SD, sh.STATUS_AXIS_TH_WRN,
  439. sh.STATUS_AXIS_STEP_LOSS_A, sh.STATUS_AXIS_STEP_LOSS_B,
  440. sh.STATUS_AXIS_OCD, sh.STATUS_AXIS_LAYOUT);
  441. }
  442. DEBUG_ECHOLNPGM(".");
  443. #endif
  444. DEBUG_ECHOLNPGM("Completed with errors");
  445. }
  446. else
  447. DEBUG_ECHOLNPGM("Completed with no errors");
  448. DEBUG_ECHOLNPGM(".");
  449. L64xxManager.pause_monitor(false);
  450. }
  451. /**
  452. * M918: increase speed until error or max feedrate achieved (as shown in configuration.h))
  453. *
  454. * J - select which driver(s) to monitor on multi-driver axis
  455. * 0 - (default) monitor all drivers on the axis or E0
  456. * 1 - monitor only X, Y, Z, E1
  457. * 2 - monitor only X2, Y2, Z2, E2
  458. * Xxxx, Yxxx, Zxxx, Exxx - axis to be monitored with displacement
  459. * xxx (1-255) is distance moved on either side of current position
  460. *
  461. * I - over current threshold
  462. * optional - will report current value from driver if not specified
  463. *
  464. * T - current (mA) setting for TVAL (0 - 4A in 31.25mA increments, rounds down) - L6474 only
  465. * optional - will report current value from driver if not specified
  466. *
  467. * K - value for KVAL_HOLD (0 - 255) (ignored for L6474)
  468. * optional - will report current value from driver if not specified
  469. *
  470. * M - value for microsteps (1 - 128) (optional)
  471. * optional - will report current value from driver if not specified
  472. */
  473. void GcodeSuite::M918() {
  474. DEBUG_ECHOLNPGM("M918");
  475. L64xxManager.pause_monitor(true); // Keep monitor_driver() from stealing status
  476. char axis_mon[3][3] = { {" "}, {" "}, {" "} }; // list of Axes to be monitored
  477. L64XX_axis_t axis_index[3];
  478. uint16_t axis_status[3];
  479. uint8_t driver_count = 1;
  480. float position_max, position_min;
  481. float final_feedrate;
  482. uint8_t kval_hold;
  483. uint8_t OCD_TH_val = 0;
  484. uint8_t STALL_TH_val = 0;
  485. uint16_t over_current_threshold;
  486. constexpr uint8_t over_current_flag = true;
  487. const L64XX_Marlin::L64XX_shadow_t &sh = L64xxManager.shadow;
  488. uint8_t j; // general purpose counter
  489. if (L64xxManager.get_user_input(driver_count, axis_index, axis_mon, position_max, position_min, final_feedrate, kval_hold, over_current_flag, OCD_TH_val, STALL_TH_val, over_current_threshold))
  490. return; // quit if invalid user input
  491. L64xxManager.get_status(axis_index[0]); // populate shadow array
  492. uint8_t m_steps = parser.byteval('M');
  493. if (m_steps != 0) {
  494. LIMIT(m_steps, 1, sh.STATUS_AXIS_LAYOUT == L6474_STATUS_LAYOUT ? 16 : 128); // L6474
  495. uint8_t stepVal;
  496. for (stepVal = 0; stepVal < 8; stepVal++) { // convert to L64xx register value
  497. if (m_steps == 1) break;
  498. m_steps >>= 1;
  499. }
  500. if (sh.STATUS_AXIS_LAYOUT == L6474_STATUS_LAYOUT)
  501. stepVal |= 0x98; // NO SYNC
  502. else
  503. stepVal |= (!SYNC_EN) | SYNC_SEL_1 | stepVal;
  504. for (j = 0; j < driver_count; j++) {
  505. L64xxManager.set_param(axis_index[j], dSPIN_HARD_HIZ, 0); // can't write STEP register if stepper being powered
  506. // results in an extra NOOP being sent (data 00)
  507. L64xxManager.set_param(axis_index[j], L6470_STEP_MODE, stepVal); // set microsteps
  508. }
  509. }
  510. m_steps = L64xxManager.get_param(axis_index[0], L6470_STEP_MODE) & 0x07; // get microsteps
  511. DEBUG_ECHOLNPAIR("Microsteps = ", _BV(m_steps));
  512. DEBUG_ECHOLNPAIR("target (maximum) feedrate = ", final_feedrate);
  513. const float feedrate_inc = final_feedrate / 10, // Start at 1/10 of max & go up by 1/10 per step
  514. fr_limit = final_feedrate * 0.99f; // Rounding-safe comparison value
  515. float current_feedrate = 0;
  516. planner.synchronize(); // Wait for moves to complete
  517. for (j = 0; j < driver_count; j++)
  518. L64xxManager.get_status(axis_index[j]); // Clear error flags
  519. char temp_axis_string[2] = " ";
  520. temp_axis_string[0] = axis_mon[0][0]; // Need a sprintf format string
  521. //temp_axis_string[1] = '\n';
  522. char gcode_string[80];
  523. uint16_t status_composite = 0;
  524. DEBUG_ECHOLNPGM(".\n.\n."); // Make feedrate outputs easier to read
  525. do {
  526. current_feedrate += feedrate_inc;
  527. DEBUG_ECHOLNPAIR("...feedrate = ", current_feedrate);
  528. sprintf_P(gcode_string, PSTR("G0 %s%03d F%03d"), temp_axis_string, uint16_t(position_min), uint16_t(current_feedrate));
  529. gcode.process_subcommands_now_P(gcode_string);
  530. sprintf_P(gcode_string, PSTR("G0 %s%03d F%03d"), temp_axis_string, uint16_t(position_max), uint16_t(current_feedrate));
  531. gcode.process_subcommands_now_P(gcode_string);
  532. planner.synchronize();
  533. for (j = 0; j < driver_count; j++) {
  534. axis_status[j] = (~L64xxManager.get_status(axis_index[j])) & 0x0800; // Bits of interest are all active LOW
  535. status_composite |= axis_status[j];
  536. }
  537. if (status_composite) break; // Break on any error
  538. } while (current_feedrate < fr_limit);
  539. DEBUG_ECHOPGM("Completed with ");
  540. if (status_composite) {
  541. DEBUG_ECHOLNPGM("errors");
  542. #if ENABLED(L6470_CHITCHAT)
  543. for (j = 0; j < driver_count; j++) {
  544. if (j) DEBUG_ECHOPGM("...");
  545. L64xxManager.error_status_decode(axis_status[j], axis_index[j],
  546. sh.STATUS_AXIS_TH_SD, sh.STATUS_AXIS_TH_WRN,
  547. sh.STATUS_AXIS_STEP_LOSS_A, sh.STATUS_AXIS_STEP_LOSS_B,
  548. sh.STATUS_AXIS_OCD, sh.STATUS_AXIS_LAYOUT);
  549. }
  550. #endif
  551. }
  552. else
  553. DEBUG_ECHOLNPGM("no errors");
  554. L64xxManager.pause_monitor(false);
  555. }
  556. #endif // HAS_L64XX