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

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