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 12KB

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