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.

M906.cpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. #include "../../../inc/MarlinConfig.h"
  23. #if HAS_L64XX
  24. #include "../../gcode.h"
  25. #include "../../../libs/L64XX/L64XX_Marlin.h"
  26. #include "../../../module/stepper/indirection.h"
  27. #include "../../../module/planner.h"
  28. #define DEBUG_OUT ENABLED(L6470_CHITCHAT)
  29. #include "../../../core/debug_out.h"
  30. /**
  31. * MACRO to fetch information on the items associated with current limiting
  32. * and maximum voltage output.
  33. *
  34. * L6470 can be setup to shutdown if either current threshold is exceeded.
  35. *
  36. * L6470 output current can not be set directly. It is set indirectly by
  37. * setting the maximum effective output voltage.
  38. *
  39. * Effective output voltage is set by PWM duty cycle.
  40. *
  41. * Maximum effective output voltage is affected by MANY variables. The main ones are:
  42. * KVAL_HOLD
  43. * KVAL_RUN
  44. * KVAL_ACC
  45. * KVAL_DEC
  46. * Vs compensation (if enabled)
  47. */
  48. void L64XX_report_current(L64XX &motor, const L64XX_axis_t axis) {
  49. if (L64xxManager.spi_abort) return; // don't do anything if set_directions() has occurred
  50. const L64XX_Marlin::L64XX_shadow_t &sh = L64xxManager.shadow;
  51. const uint16_t status = L64xxManager.get_status(axis); //also populates shadow structure
  52. const uint8_t OverCurrent_Threshold = uint8_t(motor.GetParam(L6470_OCD_TH));
  53. auto say_axis_status = [](const L64XX_axis_t axis, const uint16_t status) {
  54. L64xxManager.say_axis(axis);
  55. #if ENABLED(L6470_CHITCHAT)
  56. char tmp[10];
  57. sprintf_P(tmp, PSTR("%4x "), status);
  58. DEBUG_ECHOPGM(" status: ", tmp);
  59. print_bin(status);
  60. #else
  61. UNUSED(status);
  62. #endif
  63. SERIAL_EOL();
  64. };
  65. char temp_buf[10];
  66. switch (sh.STATUS_AXIS_LAYOUT) {
  67. case L6470_STATUS_LAYOUT: // L6470
  68. case L6480_STATUS_LAYOUT: { // L6480 & powerstep01
  69. const uint16_t Stall_Threshold = (uint8_t)motor.GetParam(L6470_STALL_TH),
  70. motor_status = (status & (STATUS_MOT_STATUS)) >> 5,
  71. L6470_ADC_out = motor.GetParam(L6470_ADC_OUT),
  72. L6470_ADC_out_limited = constrain(L6470_ADC_out, 8, 24);
  73. const float comp_coef = 1600.0f / L6470_ADC_out_limited;
  74. const uint16_t MicroSteps = _BV(motor.GetParam(L6470_STEP_MODE) & 0x07);
  75. say_axis_status(axis, sh.STATUS_AXIS_RAW);
  76. SERIAL_ECHOPGM("...OverCurrent Threshold: ");
  77. sprintf_P(temp_buf, PSTR("%2d ("), OverCurrent_Threshold);
  78. SERIAL_ECHO(temp_buf);
  79. SERIAL_ECHO((OverCurrent_Threshold + 1) * motor.OCD_CURRENT_CONSTANT_INV);
  80. SERIAL_ECHOPGM(" mA)");
  81. SERIAL_ECHOPGM(" Stall Threshold: ");
  82. sprintf_P(temp_buf, PSTR("%2d ("), Stall_Threshold);
  83. SERIAL_ECHO(temp_buf);
  84. SERIAL_ECHO((Stall_Threshold + 1) * motor.STALL_CURRENT_CONSTANT_INV);
  85. SERIAL_ECHOPGM(" mA)");
  86. SERIAL_ECHOPGM(" Motor Status: ");
  87. switch (motor_status) {
  88. case 0: SERIAL_ECHOPGM("stopped"); break;
  89. case 1: SERIAL_ECHOPGM("accelerating"); break;
  90. case 2: SERIAL_ECHOPGM("decelerating"); break;
  91. case 3: SERIAL_ECHOPGM("at constant speed"); break;
  92. }
  93. SERIAL_EOL();
  94. SERIAL_ECHOPGM("...MicroSteps: ", MicroSteps,
  95. " ADC_OUT: ", L6470_ADC_out);
  96. SERIAL_ECHOPGM(" Vs_compensation: ");
  97. SERIAL_ECHOF((motor.GetParam(sh.L6470_AXIS_CONFIG) & CONFIG_EN_VSCOMP) ? F("ENABLED ") : F("DISABLED"));
  98. SERIAL_ECHOLNPGM(" Compensation coefficient: ~", comp_coef * 0.01f);
  99. SERIAL_ECHOPGM("...KVAL_HOLD: ", motor.GetParam(L6470_KVAL_HOLD),
  100. " KVAL_RUN : ", motor.GetParam(L6470_KVAL_RUN),
  101. " KVAL_ACC: ", motor.GetParam(L6470_KVAL_ACC),
  102. " KVAL_DEC: ", motor.GetParam(L6470_KVAL_DEC),
  103. " V motor max = ");
  104. switch (motor_status) {
  105. case 0: SERIAL_ECHO(motor.GetParam(L6470_KVAL_HOLD) * 100 / 256); SERIAL_ECHOPGM("% (KVAL_HOLD)"); break;
  106. case 1: SERIAL_ECHO(motor.GetParam(L6470_KVAL_RUN) * 100 / 256); SERIAL_ECHOPGM("% (KVAL_RUN)"); break;
  107. case 2: SERIAL_ECHO(motor.GetParam(L6470_KVAL_ACC) * 100 / 256); SERIAL_ECHOPGM("% (KVAL_ACC)"); break;
  108. case 3: SERIAL_ECHO(motor.GetParam(L6470_KVAL_DEC) * 100 / 256); SERIAL_ECHOPGM("% (KVAL_HOLD)"); break;
  109. }
  110. SERIAL_EOL();
  111. #if ENABLED(L6470_CHITCHAT)
  112. DEBUG_ECHOPGM("...SLEW RATE: ");
  113. switch (sh.STATUS_AXIS_LAYOUT) {
  114. case L6470_STATUS_LAYOUT: {
  115. switch ((motor.GetParam(sh.L6470_AXIS_CONFIG) & CONFIG_POW_SR) >> CONFIG_POW_SR_BIT) {
  116. case 0: { DEBUG_ECHOLNPGM("320V/uS") ; break; }
  117. case 1: { DEBUG_ECHOLNPGM("75V/uS") ; break; }
  118. case 2: { DEBUG_ECHOLNPGM("110V/uS") ; break; }
  119. case 3: { DEBUG_ECHOLNPGM("260V/uS") ; break; }
  120. }
  121. break;
  122. }
  123. case L6480_STATUS_LAYOUT: {
  124. switch (motor.GetParam(L6470_GATECFG1) & CONFIG1_SR ) {
  125. case CONFIG1_SR_220V_us: { DEBUG_ECHOLNPGM("220V/uS") ; break; }
  126. case CONFIG1_SR_400V_us: { DEBUG_ECHOLNPGM("400V/uS") ; break; }
  127. case CONFIG1_SR_520V_us: { DEBUG_ECHOLNPGM("520V/uS") ; break; }
  128. case CONFIG1_SR_980V_us: { DEBUG_ECHOLNPGM("980V/uS") ; break; }
  129. default: { DEBUG_ECHOLNPGM("unknown") ; break; }
  130. }
  131. }
  132. }
  133. #endif
  134. SERIAL_EOL();
  135. break;
  136. }
  137. case L6474_STATUS_LAYOUT: { // L6474
  138. const uint16_t L6470_ADC_out = motor.GetParam(L6470_ADC_OUT) & 0x1F,
  139. L6474_TVAL_val = motor.GetParam(L6474_TVAL) & 0x7F;
  140. say_axis_status(axis, sh.STATUS_AXIS_RAW);
  141. SERIAL_ECHOPGM("...OverCurrent Threshold: ");
  142. sprintf_P(temp_buf, PSTR("%2d ("), OverCurrent_Threshold);
  143. SERIAL_ECHO(temp_buf);
  144. SERIAL_ECHO((OverCurrent_Threshold + 1) * motor.OCD_CURRENT_CONSTANT_INV);
  145. SERIAL_ECHOPGM(" mA)");
  146. SERIAL_ECHOPGM(" TVAL: ");
  147. sprintf_P(temp_buf, PSTR("%2d ("), L6474_TVAL_val);
  148. SERIAL_ECHO(temp_buf);
  149. SERIAL_ECHO((L6474_TVAL_val + 1) * motor.STALL_CURRENT_CONSTANT_INV);
  150. SERIAL_ECHOLNPGM(" mA) Motor Status: NA");
  151. const uint16_t MicroSteps = _BV(motor.GetParam(L6470_STEP_MODE) & 0x07); //NOMORE(MicroSteps, 16);
  152. SERIAL_ECHOPGM("...MicroSteps: ", MicroSteps,
  153. " ADC_OUT: ", L6470_ADC_out);
  154. SERIAL_ECHOLNPGM(" Vs_compensation: NA\n");
  155. SERIAL_ECHOLNPGM("...KVAL_HOLD: NA"
  156. " KVAL_RUN : NA"
  157. " KVAL_ACC: NA"
  158. " KVAL_DEC: NA"
  159. " V motor max = NA");
  160. #if ENABLED(L6470_CHITCHAT)
  161. DEBUG_ECHOPGM("...SLEW RATE: ");
  162. switch ((motor.GetParam(sh.L6470_AXIS_CONFIG) & CONFIG_POW_SR) >> CONFIG_POW_SR_BIT) {
  163. case 0: DEBUG_ECHOLNPGM("320V/uS") ; break;
  164. case 1: DEBUG_ECHOLNPGM("75V/uS") ; break;
  165. case 2: DEBUG_ECHOLNPGM("110V/uS") ; break;
  166. case 3: DEBUG_ECHOLNPGM("260V/uS") ; break;
  167. default: DEBUG_ECHOLNPGM("slew rate: ", (motor.GetParam(sh.L6470_AXIS_CONFIG) & CONFIG_POW_SR) >> CONFIG_POW_SR_BIT); break;
  168. }
  169. #endif
  170. SERIAL_EOL();
  171. SERIAL_EOL();
  172. break;
  173. }
  174. }
  175. }
  176. /**
  177. * M906: report or set KVAL_HOLD which sets the maximum effective voltage provided by the
  178. * PWMs to the steppers
  179. *
  180. * On L6474 this sets the TVAL register (same address).
  181. *
  182. * I - select which driver(s) to change on multi-driver axis
  183. * 0 - (default) all drivers on the axis or E0
  184. * 1 - monitor only X, Y, Z or E1
  185. * 2 - monitor only X2, Y2, Z2 or E2
  186. * 3 - monitor only Z3 or E3
  187. * 4 - monitor only Z4 or E4
  188. * 5 - monitor only E5
  189. * Xxxx, Yxxx, Zxxx, Exxx - axis to change (optional)
  190. * L6474 - current in mA (4A max)
  191. * All others - 0-255
  192. *
  193. * Sets KVAL_HOLD which affects the current being driven through the stepper.
  194. *
  195. * L6470 is used in the STEP-CLOCK mode. KVAL_HOLD is the only KVAL_xxx
  196. * that affects the effective voltage seen by the stepper.
  197. */
  198. void GcodeSuite::M906() {
  199. L64xxManager.pause_monitor(true); // Keep monitor_driver() from stealing status
  200. #define L6470_SET_KVAL_HOLD(Q) (AXIS_IS_L64XX(Q) ? stepper##Q.setTVALCurrent(value) : stepper##Q.SetParam(L6470_KVAL_HOLD, uint8_t(value)))
  201. DEBUG_ECHOLNPGM("M906");
  202. uint8_t report_current = true;
  203. #if HAS_L64XX
  204. const uint8_t index = parser.byteval('I');
  205. #endif
  206. LOOP_LOGICAL_AXES(i) if (uint16_t value = parser.intval(axis_codes[i])) {
  207. report_current = false;
  208. if (planner.has_blocks_queued() || planner.cleaning_buffer_counter) {
  209. SERIAL_ECHOLNPGM("Test aborted. Can't set KVAL_HOLD while steppers are moving.");
  210. return;
  211. }
  212. switch (i) {
  213. case X_AXIS:
  214. #if AXIS_IS_L64XX(X)
  215. if (index == 0) L6470_SET_KVAL_HOLD(X);
  216. #endif
  217. #if AXIS_IS_L64XX(X2)
  218. if (index == 1) L6470_SET_KVAL_HOLD(X2);
  219. #endif
  220. break;
  221. #if HAS_Y_AXIS
  222. case Y_AXIS:
  223. #if AXIS_IS_L64XX(Y)
  224. if (index == 0) L6470_SET_KVAL_HOLD(Y);
  225. #endif
  226. #if AXIS_IS_L64XX(Y2)
  227. if (index == 1) L6470_SET_KVAL_HOLD(Y2);
  228. #endif
  229. break;
  230. #endif
  231. #if HAS_Z_AXIS
  232. case Z_AXIS:
  233. #if AXIS_IS_L64XX(Z)
  234. if (index == 0) L6470_SET_KVAL_HOLD(Z);
  235. #endif
  236. #if AXIS_IS_L64XX(Z2)
  237. if (index == 1) L6470_SET_KVAL_HOLD(Z2);
  238. #endif
  239. #if AXIS_IS_L64XX(Z3)
  240. if (index == 2) L6470_SET_KVAL_HOLD(Z3);
  241. #endif
  242. #if AXIS_DRIVER_TYPE_Z4(L6470)
  243. if (index == 3) L6470_SET_KVAL_HOLD(Z4);
  244. #endif
  245. break;
  246. #endif
  247. #if E_STEPPERS
  248. case E_AXIS: {
  249. const int8_t target_e_stepper = get_target_e_stepper_from_command();
  250. if (target_e_stepper < 0) return;
  251. switch (target_e_stepper) {
  252. #if AXIS_IS_L64XX(E0)
  253. case 0: L6470_SET_KVAL_HOLD(E0); break;
  254. #endif
  255. #if AXIS_IS_L64XX(E1)
  256. case 1: L6470_SET_KVAL_HOLD(E1); break;
  257. #endif
  258. #if AXIS_IS_L64XX(E2)
  259. case 2: L6470_SET_KVAL_HOLD(E2); break;
  260. #endif
  261. #if AXIS_IS_L64XX(E3)
  262. case 3: L6470_SET_KVAL_HOLD(E3); break;
  263. #endif
  264. #if AXIS_IS_L64XX(E4)
  265. case 4: L6470_SET_KVAL_HOLD(E4); break;
  266. #endif
  267. #if AXIS_IS_L64XX(E5)
  268. case 5: L6470_SET_KVAL_HOLD(E5); break;
  269. #endif
  270. #if AXIS_IS_L64XX(E6)
  271. case 6: L6470_SET_KVAL_HOLD(E6); break;
  272. #endif
  273. #if AXIS_IS_L64XX(E7)
  274. case 7: L6470_SET_KVAL_HOLD(E7); break;
  275. #endif
  276. }
  277. } break;
  278. #endif
  279. }
  280. }
  281. if (report_current) {
  282. #define L64XX_REPORT_CURRENT(Q) L64XX_report_current(stepper##Q, Q)
  283. L64xxManager.spi_active = true; // Tell set_directions() a series of SPI transfers is underway
  284. #if AXIS_IS_L64XX(X)
  285. L64XX_REPORT_CURRENT(X);
  286. #endif
  287. #if AXIS_IS_L64XX(X2)
  288. L64XX_REPORT_CURRENT(X2);
  289. #endif
  290. #if AXIS_IS_L64XX(Y)
  291. L64XX_REPORT_CURRENT(Y);
  292. #endif
  293. #if AXIS_IS_L64XX(Y2)
  294. L64XX_REPORT_CURRENT(Y2);
  295. #endif
  296. #if AXIS_IS_L64XX(Z)
  297. L64XX_REPORT_CURRENT(Z);
  298. #endif
  299. #if AXIS_IS_L64XX(Z2)
  300. L64XX_REPORT_CURRENT(Z2);
  301. #endif
  302. #if AXIS_IS_L64XX(Z3)
  303. L64XX_REPORT_CURRENT(Z3);
  304. #endif
  305. #if AXIS_IS_L64XX(Z4)
  306. L64XX_REPORT_CURRENT(Z4);
  307. #endif
  308. #if AXIS_IS_L64XX(E0)
  309. L64XX_REPORT_CURRENT(E0);
  310. #endif
  311. #if AXIS_IS_L64XX(E1)
  312. L64XX_REPORT_CURRENT(E1);
  313. #endif
  314. #if AXIS_IS_L64XX(E2)
  315. L64XX_REPORT_CURRENT(E2);
  316. #endif
  317. #if AXIS_IS_L64XX(E3)
  318. L64XX_REPORT_CURRENT(E3);
  319. #endif
  320. #if AXIS_IS_L64XX(E4)
  321. L64XX_REPORT_CURRENT(E4);
  322. #endif
  323. #if AXIS_IS_L64XX(E5)
  324. L64XX_REPORT_CURRENT(E5);
  325. #endif
  326. #if AXIS_IS_L64XX(E6)
  327. L64XX_REPORT_CURRENT(E6);
  328. #endif
  329. #if AXIS_IS_L64XX(E7)
  330. L64XX_REPORT_CURRENT(E7);
  331. #endif
  332. L64xxManager.spi_active = false; // done with all SPI transfers - clear handshake flags
  333. L64xxManager.spi_abort = false;
  334. L64xxManager.pause_monitor(false);
  335. }
  336. }
  337. #endif // HAS_L64XX