From e8701195e66f2d27ffe17fb514eae8173795aaf7 Mon Sep 17 00:00:00 2001 From: Georgiy Bondarenko <69736697+nehilo@users.noreply.github.com> Date: Thu, 4 Mar 2021 22:54:23 +0500 Subject: Initial commit --- Marlin/src/module/stepper/L64xx.cpp | 225 +++++++ Marlin/src/module/stepper/L64xx.h | 364 +++++++++++ Marlin/src/module/stepper/TMC26X.cpp | 144 +++++ Marlin/src/module/stepper/TMC26X.h | 164 +++++ Marlin/src/module/stepper/indirection.cpp | 50 ++ Marlin/src/module/stepper/indirection.h | 1003 +++++++++++++++++++++++++++++ Marlin/src/module/stepper/trinamic.cpp | 877 +++++++++++++++++++++++++ Marlin/src/module/stepper/trinamic.h | 362 +++++++++++ 8 files changed, 3189 insertions(+) create mode 100644 Marlin/src/module/stepper/L64xx.cpp create mode 100644 Marlin/src/module/stepper/L64xx.h create mode 100644 Marlin/src/module/stepper/TMC26X.cpp create mode 100644 Marlin/src/module/stepper/TMC26X.h create mode 100644 Marlin/src/module/stepper/indirection.cpp create mode 100644 Marlin/src/module/stepper/indirection.h create mode 100644 Marlin/src/module/stepper/trinamic.cpp create mode 100644 Marlin/src/module/stepper/trinamic.h (limited to 'Marlin/src/module/stepper') diff --git a/Marlin/src/module/stepper/L64xx.cpp b/Marlin/src/module/stepper/L64xx.cpp new file mode 100644 index 0000000..3e2bf09 --- /dev/null +++ b/Marlin/src/module/stepper/L64xx.cpp @@ -0,0 +1,225 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +/** + * stepper/L64xx.cpp + * Stepper driver indirection for L64XX drivers + */ + +#include "../../inc/MarlinConfig.h" + +#if HAS_L64XX + +#include "L64xx.h" + +#if AXIS_IS_L64XX(X) + L64XX_CLASS(X) stepperX(L6470_CHAIN_SS_PIN); +#endif +#if AXIS_IS_L64XX(X2) + L64XX_CLASS(X2) stepperX2(L6470_CHAIN_SS_PIN); +#endif +#if AXIS_IS_L64XX(Y) + L64XX_CLASS(Y) stepperY(L6470_CHAIN_SS_PIN); +#endif +#if AXIS_IS_L64XX(Y2) + L64XX_CLASS(Y2) stepperY2(L6470_CHAIN_SS_PIN); +#endif +#if AXIS_IS_L64XX(Z) + L64XX_CLASS(Z) stepperZ(L6470_CHAIN_SS_PIN); +#endif +#if AXIS_IS_L64XX(Z2) + L64XX_CLASS(Z2) stepperZ2(L6470_CHAIN_SS_PIN); +#endif +#if AXIS_IS_L64XX(Z3) + L64XX_CLASS(Z3) stepperZ3(L6470_CHAIN_SS_PIN); +#endif +#if AXIS_IS_L64XX(Z4) + L64XX_CLASS(Z4) stepperZ4(L6470_CHAIN_SS_PIN); +#endif +#if AXIS_IS_L64XX(E0) + L64XX_CLASS(E0) stepperE0(L6470_CHAIN_SS_PIN); +#endif +#if AXIS_IS_L64XX(E1) + L64XX_CLASS(E1) stepperE1(L6470_CHAIN_SS_PIN); +#endif +#if AXIS_IS_L64XX(E2) + L64XX_CLASS(E2) stepperE2(L6470_CHAIN_SS_PIN); +#endif +#if AXIS_IS_L64XX(E3) + L64XX_CLASS(E3) stepperE3(L6470_CHAIN_SS_PIN); +#endif +#if AXIS_IS_L64XX(E4) + L64XX_CLASS(E4) stepperE4(L6470_CHAIN_SS_PIN); +#endif +#if AXIS_IS_L64XX(E5) + L64XX_CLASS(E5) stepperE5(L6470_CHAIN_SS_PIN); +#endif +#if AXIS_IS_L64XX(E6) + L64XX_CLASS(E6) stepperE6(L6470_CHAIN_SS_PIN); +#endif +#if AXIS_IS_L64XX(E7) + L64XX_CLASS(E7) stepperE7(L6470_CHAIN_SS_PIN); +#endif + +// Not using L64XX class init method because it +// briefly sends power to the steppers + +inline void L6470_init_chip(L64XX &st, const int ms, const int oc, const int sc, const int mv, const int slew_rate) { + st.set_handlers(L64xxManager.spi_init, L64xxManager.transfer_single, L64xxManager.transfer_chain); // specify which external SPI routines to use + switch (st.L6470_status_layout) { + case L6470_STATUS_LAYOUT: { + st.resetDev(); + st.softFree(); + st.SetParam(st.L64XX_CONFIG, CONFIG_PWM_DIV_1 | CONFIG_PWM_MUL_2 | CONFIG_OC_SD_DISABLE | CONFIG_VS_COMP_DISABLE | CONFIG_SW_HARD_STOP | CONFIG_INT_16MHZ); + st.SetParam(L6470_KVAL_RUN, 0xFF); + st.SetParam(L6470_KVAL_ACC, 0xFF); + st.SetParam(L6470_KVAL_DEC, 0xFF); + st.setMicroSteps(ms); + st.setOverCurrent(oc); + st.setStallCurrent(sc); + st.SetParam(L6470_KVAL_HOLD, mv); + st.SetParam(L6470_ABS_POS, 0); + uint32_t config_temp = st.GetParam(st.L64XX_CONFIG); + config_temp &= ~CONFIG_POW_SR; + switch (slew_rate) { + case 0: st.SetParam(st.L64XX_CONFIG, config_temp | CONFIG_SR_75V_us); break; + default: + case 1: st.SetParam(st.L64XX_CONFIG, config_temp | CONFIG_SR_110V_us); break; + case 3: + case 2: st.SetParam(st.L64XX_CONFIG, config_temp | CONFIG_SR_260V_us); break; + } + st.getStatus(); + st.getStatus(); + break; + } + + case L6474_STATUS_LAYOUT: { + st.free(); + //st.SetParam(st.L64XX_CONFIG, CONFIG_PWM_DIV_1 | CONFIG_PWM_MUL_2 | CONFIG_OC_SD_DISABLE | CONFIG_VS_COMP_DISABLE | CONFIG_SW_HARD_STOP | CONFIG_INT_16MHZ); + //st.SetParam(L6474_TVAL, 0xFF); + st.setMicroSteps(ms); + st.setOverCurrent(oc); + st.setTVALCurrent(sc); + st.SetParam(L6470_ABS_POS, 0); + uint32_t config_temp = st.GetParam(st.L64XX_CONFIG); + config_temp &= ~CONFIG_POW_SR & ~CONFIG_EN_TQREG; // clear out slew rate and set current to be controlled by TVAL register + switch (slew_rate) { + case 0: st.SetParam(st.L64XX_CONFIG, config_temp | CONFIG_SR_75V_us); break; + default: + case 1: st.SetParam(st.L64XX_CONFIG, config_temp | CONFIG_SR_110V_us); break; + case 3: + case 2: st.SetParam(st.L64XX_CONFIG, config_temp | CONFIG_SR_260V_us); break; + //case 0: st.SetParam(st.L64XX_CONFIG, 0x2E88 | CONFIG_EN_TQREG | CONFIG_SR_75V_us); break; + //default: + //case 1: st.SetParam(st.L64XX_CONFIG, 0x2E88 | CONFIG_EN_TQREG | CONFIG_SR_110V_us); break; + //case 3: + //case 2: st.SetParam(st.L64XX_CONFIG, 0x2E88 | CONFIG_EN_TQREG | CONFIG_SR_260V_us); break; + + //case 0: st.SetParam(st.L64XX_CONFIG, 0x2E88 ); break; + //default: + //case 1: st.SetParam(st.L64XX_CONFIG, 0x2E88 ); break; + //case 3: + //case 2: st.SetParam(st.L64XX_CONFIG, 0x2E88 ); break; + } + st.getStatus(); + st.getStatus(); + break; + } + + case L6480_STATUS_LAYOUT: { + st.resetDev(); + st.softFree(); + st.SetParam(st.L64XX_CONFIG, CONFIG_PWM_DIV_1 | CONFIG_PWM_MUL_2 | CONFIG_OC_SD_DISABLE | CONFIG_VS_COMP_DISABLE | CONFIG_SW_HARD_STOP | CONFIG_INT_16MHZ); + st.SetParam(L6470_KVAL_RUN, 0xFF); + st.SetParam(L6470_KVAL_ACC, 0xFF); + st.SetParam(L6470_KVAL_DEC, 0xFF); + st.setMicroSteps(ms); + st.setOverCurrent(oc); + st.setStallCurrent(sc); + st.SetParam(+-L6470_KVAL_HOLD, mv); + st.SetParam(L6470_ABS_POS, 0); + st.SetParam(st.L64XX_CONFIG,(st.GetParam(st.L64XX_CONFIG) | PWR_VCC_7_5V)); + st.getStatus(); // must clear out status bits before can set slew rate + st.getStatus(); + switch (slew_rate) { + case 0: st.SetParam(L6470_GATECFG1, CONFIG1_SR_220V_us); st.SetParam(L6470_GATECFG2, CONFIG2_SR_220V_us); break; + default: + case 1: st.SetParam(L6470_GATECFG1, CONFIG1_SR_400V_us); st.SetParam(L6470_GATECFG2, CONFIG2_SR_400V_us); break; + case 2: st.SetParam(L6470_GATECFG1, CONFIG1_SR_520V_us); st.SetParam(L6470_GATECFG2, CONFIG2_SR_520V_us); break; + case 3: st.SetParam(L6470_GATECFG1, CONFIG1_SR_980V_us); st.SetParam(L6470_GATECFG2, CONFIG2_SR_980V_us); break; + } + break; + } + } +} + +#define L6470_INIT_CHIP(Q) L6470_init_chip(stepper##Q, Q##_MICROSTEPS, Q##_OVERCURRENT, Q##_STALLCURRENT, Q##_MAX_VOLTAGE, Q##_SLEW_RATE) + +void L64XX_Marlin::init_to_defaults() { + #if AXIS_IS_L64XX(X) + L6470_INIT_CHIP(X); + #endif + #if AXIS_IS_L64XX(X2) + L6470_INIT_CHIP(X2); + #endif + #if AXIS_IS_L64XX(Y) + L6470_INIT_CHIP(Y); + #endif + #if AXIS_IS_L64XX(Y2) + L6470_INIT_CHIP(Y2); + #endif + #if AXIS_IS_L64XX(Z) + L6470_INIT_CHIP(Z); + #endif + #if AXIS_IS_L64XX(Z2) + L6470_INIT_CHIP(Z2); + #endif + #if AXIS_IS_L64XX(Z3) + L6470_INIT_CHIP(Z3); + #endif + #if AXIS_IS_L64XX(E0) + L6470_INIT_CHIP(E0); + #endif + #if AXIS_IS_L64XX(E1) + L6470_INIT_CHIP(E1); + #endif + #if AXIS_IS_L64XX(E2) + L6470_INIT_CHIP(E2); + #endif + #if AXIS_IS_L64XX(E3) + L6470_INIT_CHIP(E3); + #endif + #if AXIS_IS_L64XX(E4) + L6470_INIT_CHIP(E4); + #endif + #if AXIS_IS_L64XX(E5) + L6470_INIT_CHIP(E5); + #endif + #if AXIS_IS_L64XX(E6) + L6470_INIT_CHIP(E6); + #endif + #if AXIS_IS_L64XX(E7) + L6470_INIT_CHIP(E7); + #endif +} + +#endif // HAS_L64XX diff --git a/Marlin/src/module/stepper/L64xx.h b/Marlin/src/module/stepper/L64xx.h new file mode 100644 index 0000000..9c8b0b1 --- /dev/null +++ b/Marlin/src/module/stepper/L64xx.h @@ -0,0 +1,364 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +#pragma once + +/** + * stepper/L64xx.h + * Stepper driver indirection for L64XX drivers + */ + +#include "../../inc/MarlinConfig.h" +#include "../../libs/L64XX/L64XX_Marlin.h" + +// Convert option names to L64XX classes +#define CLASS_L6470 L6470 +#define CLASS_L6474 L6474 +#define CLASS_POWERSTEP01 powerSTEP01 + +#define __L64XX_CLASS(TYPE) CLASS_##TYPE +#define _L64XX_CLASS(TYPE) __L64XX_CLASS(TYPE) +#define L64XX_CLASS(ST) _L64XX_CLASS(ST##_DRIVER_TYPE) + +#define L6474_DIR_WRITE(A,STATE) do{ L64xxManager.dir_commands[A] = dSPIN_L6474_ENABLE; WRITE(A##_DIR_PIN, STATE); }while(0) +#define L64XX_DIR_WRITE(A,STATE) do{ L64xxManager.dir_commands[A] = (STATE) ? dSPIN_STEP_CLOCK_REV : dSPIN_STEP_CLOCK_FWD; }while(0) + +// X Stepper +#if AXIS_IS_L64XX(X) + extern L64XX_CLASS(X) stepperX; + #define X_ENABLE_INIT() NOOP + #define X_ENABLE_WRITE(STATE) (STATE ? stepperX.hardStop() : stepperX.free()) + #define X_ENABLE_READ() (stepperX.getStatus() & STATUS_HIZ) + #if AXIS_DRIVER_TYPE_X(L6474) + #define X_DIR_INIT() SET_OUTPUT(X_DIR_PIN) + #define X_DIR_WRITE(STATE) L6474_DIR_WRITE(X, STATE) + #define X_DIR_READ() READ(X_DIR_PIN) + #else + #define X_DIR_INIT() NOOP + #define X_DIR_WRITE(STATE) L64XX_DIR_WRITE(X, STATE) + #define X_DIR_READ() (stepper##X.getStatus() & STATUS_DIR); + #if AXIS_DRIVER_TYPE_X(L6470) + #define DISABLE_STEPPER_X() stepperX.free() + #endif + #endif +#endif + +// Y Stepper +#if AXIS_IS_L64XX(Y) + extern L64XX_CLASS(Y) stepperY; + #define Y_ENABLE_INIT() NOOP + #define Y_ENABLE_WRITE(STATE) (STATE ? stepperY.hardStop() : stepperY.free()) + #define Y_ENABLE_READ() (stepperY.getStatus() & STATUS_HIZ) + #if AXIS_DRIVER_TYPE_Y(L6474) + #define Y_DIR_INIT() SET_OUTPUT(Y_DIR_PIN) + #define Y_DIR_WRITE(STATE) L6474_DIR_WRITE(Y, STATE) + #define Y_DIR_READ() READ(Y_DIR_PIN) + #else + #define Y_DIR_INIT() NOOP + #define Y_DIR_WRITE(STATE) L64XX_DIR_WRITE(Y, STATE) + #define Y_DIR_READ() (stepper##Y.getStatus() & STATUS_DIR); + #if AXIS_DRIVER_TYPE_Y(L6470) + #define DISABLE_STEPPER_Y() stepperY.free() + #endif + #endif +#endif + +// Z Stepper +#if AXIS_IS_L64XX(Z) + extern L64XX_CLASS(Z) stepperZ; + #define Z_ENABLE_INIT() NOOP + #define Z_ENABLE_WRITE(STATE) (STATE ? stepperZ.hardStop() : stepperZ.free()) + #define Z_ENABLE_READ() (stepperZ.getStatus() & STATUS_HIZ) + #if AXIS_DRIVER_TYPE_Z(L6474) + #define Z_DIR_INIT() SET_OUTPUT(Z_DIR_PIN) + #define Z_DIR_WRITE(STATE) L6474_DIR_WRITE(Z, STATE) + #define Z_DIR_READ() READ(Z_DIR_PIN) + #else + #define Z_DIR_INIT() NOOP + #define Z_DIR_WRITE(STATE) L64XX_DIR_WRITE(Z, STATE) + #define Z_DIR_READ() (stepper##Z.getStatus() & STATUS_DIR); + #if AXIS_DRIVER_TYPE_Z(L6470) + #define DISABLE_STEPPER_Z() stepperZ.free() + #endif + #endif +#endif + +// X2 Stepper +#if HAS_X2_ENABLE && AXIS_IS_L64XX(X2) + extern L64XX_CLASS(X2) stepperX2; + #define X2_ENABLE_INIT() NOOP + #define X2_ENABLE_WRITE(STATE) (STATE ? stepperX2.hardStop() : stepperX2.free()) + #define X2_ENABLE_READ() (stepperX2.getStatus() & STATUS_HIZ) + #if AXIS_DRIVER_TYPE_X2(L6474) + #define X2_DIR_INIT() SET_OUTPUT(X2_DIR_PIN) + #define X2_DIR_WRITE(STATE) L6474_DIR_WRITE(X2, STATE) + #define X2_DIR_READ() READ(X2_DIR_PIN) + #else + #define X2_DIR_INIT() NOOP + #define X2_DIR_WRITE(STATE) L64XX_DIR_WRITE(X2, STATE) + #define X2_DIR_READ() (stepper##X2.getStatus() & STATUS_DIR); + #endif +#endif + +#if AXIS_DRIVER_TYPE_X2(L6470) + #define DISABLE_STEPPER_X2() stepperX2.free() +#endif + +// Y2 Stepper +#if HAS_Y2_ENABLE && AXIS_IS_L64XX(Y2) + extern L64XX_CLASS(Y2) stepperY2; + #define Y2_ENABLE_INIT() NOOP + #define Y2_ENABLE_WRITE(STATE) (STATE ? stepperY2.hardStop() : stepperY2.free()) + #define Y2_ENABLE_READ() (stepperY2.getStatus() & STATUS_HIZ) + #if AXIS_DRIVER_TYPE_Y2(L6474) + #define Y2_DIR_INIT() SET_OUTPUT(Y2_DIR_PIN) + #define Y2_DIR_WRITE(STATE) L6474_DIR_WRITE(Y2, STATE) + #define Y2_DIR_READ() READ(Y2_DIR_PIN) + #else + #define Y2_DIR_INIT() NOOP + #define Y2_DIR_WRITE(STATE) L64XX_DIR_WRITE(Y2, STATE) + #define Y2_DIR_READ() (stepper##Y2.getStatus() & STATUS_DIR); + #endif +#endif + +#if AXIS_DRIVER_TYPE_Y2(L6470) + #define DISABLE_STEPPER_Y2() stepperY2.free() +#endif + +// Z2 Stepper +#if HAS_Z2_ENABLE && AXIS_IS_L64XX(Z2) + extern L64XX_CLASS(Z2) stepperZ2; + #define Z2_ENABLE_INIT() NOOP + #define Z2_ENABLE_WRITE(STATE) (STATE ? stepperZ2.hardStop() : stepperZ2.free()) + #define Z2_ENABLE_READ() (stepperZ2.getStatus() & STATUS_HIZ) + #if AXIS_DRIVER_TYPE_Z2(L6474) + #define Z2_DIR_INIT() SET_OUTPUT(Z2_DIR_PIN) + #define Z2_DIR_WRITE(STATE) L6474_DIR_WRITE(Z2, STATE) + #define Z2_DIR_READ() READ(Z2_DIR_PIN) + #else + #define Z2_DIR_INIT() NOOP + #define Z2_DIR_WRITE(STATE) L64XX_DIR_WRITE(Z2, STATE) + #define Z2_DIR_READ() (stepper##Z2.getStatus() & STATUS_DIR); + #endif +#endif + +#if AXIS_DRIVER_TYPE_Z2(L6470) + #define DISABLE_STEPPER_Z2() stepperZ2.free() +#endif + +// Z3 Stepper +#if HAS_Z3_ENABLE && AXIS_IS_L64XX(Z3) + extern L64XX_CLASS(Z3) stepperZ3; + #define Z3_ENABLE_INIT() NOOP + #define Z3_ENABLE_WRITE(STATE) (STATE ? stepperZ3.hardStop() : stepperZ3.free()) + #define Z3_ENABLE_READ() (stepperZ3.getStatus() & STATUS_HIZ) + #if AXIS_DRIVER_TYPE_Z3(L6474) + #define Z3_DIR_INIT() SET_OUTPUT(Z3_DIR_PIN) + #define Z3_DIR_WRITE(STATE) L6474_DIR_WRITE(Z3, STATE) + #define Z3_DIR_READ() READ(Z3_DIR_PIN) + #else + #define Z3_DIR_INIT() NOOP + #define Z3_DIR_WRITE(STATE) L64XX_DIR_WRITE(Z3, STATE) + #define Z3_DIR_READ() (stepper##Z3.getStatus() & STATUS_DIR); + #endif +#endif + +#if AXIS_DRIVER_TYPE_Z3(L6470) + #define DISABLE_STEPPER_Z3() stepperZ3.free() +#endif + +// Z4 Stepper +#if HAS_Z4_ENABLE && AXIS_IS_L64XX(Z4) + extern L64XX_CLASS(Z4) stepperZ4; + #define Z4_ENABLE_INIT() NOOP + #define Z4_ENABLE_WRITE(STATE) (STATE ? stepperZ4.hardStop() : stepperZ4.free()) + #define Z4_ENABLE_READ() (stepperZ4.getStatus() & STATUS_HIZ) + #if AXIS_DRIVER_TYPE_Z4(L6474) + #define Z4_DIR_INIT() SET_OUTPUT(Z4_DIR_PIN) + #define Z4_DIR_WRITE(STATE) L6474_DIR_WRITE(Z4, STATE) + #define Z4_DIR_READ() READ(Z4_DIR_PIN) + #else + #define Z4_DIR_INIT() NOOP + #define Z4_DIR_WRITE(STATE) L64XX_DIR_WRITE(Z4, STATE) + #define Z4_DIR_READ() (stepper##Z4.getStatus() & STATUS_DIR); + #endif +#endif + +#if AXIS_DRIVER_TYPE_Z4(L6470) + #define DISABLE_STEPPER_Z4() stepperZ4.free() +#endif + +// E0 Stepper +#if AXIS_IS_L64XX(E0) + extern L64XX_CLASS(E0) stepperE0; + #define E0_ENABLE_INIT() NOOP + #define E0_ENABLE_WRITE(STATE) (STATE ? stepperE0.hardStop() : stepperE0.free()) + #define E0_ENABLE_READ() (stepperE0.getStatus() & STATUS_HIZ) + #if AXIS_DRIVER_TYPE_E0(L6474) + #define E0_DIR_INIT() SET_OUTPUT(E0_DIR_PIN) + #define E0_DIR_WRITE(STATE) L6474_DIR_WRITE(E0, STATE) + #define E0_DIR_READ() READ(E0_DIR_PIN) + #else + #define E0_DIR_INIT() NOOP + #define E0_DIR_WRITE(STATE) L64XX_DIR_WRITE(E0, STATE) + #define E0_DIR_READ() (stepper##E0.getStatus() & STATUS_DIR); + #if AXIS_DRIVER_TYPE_E0(L6470) + #define DISABLE_STEPPER_E0() do{ stepperE0.free(); }while(0) + #endif + #endif +#endif + +// E1 Stepper +#if AXIS_IS_L64XX(E1) + extern L64XX_CLASS(E1) stepperE1; + #define E1_ENABLE_INIT() NOOP + #define E1_ENABLE_WRITE(STATE) (STATE ? stepperE1.hardStop() : stepperE1.free()) + #define E1_ENABLE_READ() (stepperE1.getStatus() & STATUS_HIZ) + #if AXIS_DRIVER_TYPE_E1(L6474) + #define E1_DIR_INIT() SET_OUTPUT(E1_DIR_PIN) + #define E1_DIR_WRITE(STATE) L6474_DIR_WRITE(E1, STATE) + #define E1_DIR_READ() READ(E1_DIR_PIN) + #else + #define E1_DIR_INIT() NOOP + #define E1_DIR_WRITE(STATE) L64XX_DIR_WRITE(E1, STATE) + #define E1_DIR_READ() (stepper##E1.getStatus() & STATUS_DIR); + #if AXIS_DRIVER_TYPE_E1(L6470) + #define DISABLE_STEPPER_E1() do{ stepperE1.free(); }while(0) + #endif + #endif +#endif + +// E2 Stepper +#if AXIS_IS_L64XX(E2) + extern L64XX_CLASS(E2) stepperE2; + #define E2_ENABLE_INIT() NOOP + #define E2_ENABLE_WRITE(STATE) (STATE ? stepperE2.hardStop() : stepperE2.free()) + #define E2_ENABLE_READ() (stepperE2.getStatus() & STATUS_HIZ) + #if AXIS_DRIVER_TYPE_E2(L6474) + #define E2_DIR_INIT() SET_OUTPUT(E2_DIR_PIN) + #define E2_DIR_WRITE(STATE) L6474_DIR_WRITE(E2, STATE) + #define E2_DIR_READ() READ(E2_DIR_PIN) + #else + #define E2_DIR_INIT() NOOP + #define E2_DIR_WRITE(STATE) L64XX_DIR_WRITE(E2, STATE) + #define E2_DIR_READ() (stepper##E2.getStatus() & STATUS_DIR); + #if AXIS_DRIVER_TYPE_E2(L6470) + #define DISABLE_STEPPER_E2() do{ stepperE2.free(); }while(0) + #endif + #endif +#endif + +// E3 Stepper +#if AXIS_IS_L64XX(E3) + extern L64XX_CLASS(E3) stepperE3; + #define E3_ENABLE_INIT() NOOP + #define E3_ENABLE_WRITE(STATE) (STATE ? stepperE3.hardStop() : stepperE3.free()) + #define E3_ENABLE_READ() (stepperE3.getStatus() & STATUS_HIZ) + #if AXIS_DRIVER_TYPE_E3(L6474) + #define E3_DIR_INIT() SET_OUTPUT(E3_DIR_PIN) + #define E3_DIR_WRITE(STATE) L6474_DIR_WRITE(E3, STATE) + #define E3_DIR_READ() READ(E3_DIR_PIN) + #else + #define E3_DIR_INIT() NOOP + #define E3_DIR_WRITE(STATE) L64XX_DIR_WRITE(E3, STATE) + #define E3_DIR_READ() (stepper##E3.getStatus() & STATUS_DIR); + #endif +#endif + +// E4 Stepper +#if AXIS_IS_L64XX(E4) + extern L64XX_CLASS(E4) stepperE4; + #define E4_ENABLE_INIT() NOOP + #define E4_ENABLE_WRITE(STATE) (STATE ? stepperE4.hardStop() : stepperE4.free()) + #define E4_ENABLE_READ() (stepperE4.getStatus() & STATUS_HIZ) + #if AXIS_DRIVER_TYPE_E4(L6474) + #define E4_DIR_INIT() SET_OUTPUT(E4_DIR_PIN) + #define E4_DIR_WRITE(STATE) L6474_DIR_WRITE(E4, STATE) + #define E4_DIR_READ() READ(E4_DIR_PIN) + #else + #define E4_DIR_INIT() NOOP + #define E4_DIR_WRITE(STATE) L64XX_DIR_WRITE(E4, STATE) + #define E4_DIR_READ() (stepper##E4.getStatus() & STATUS_DIR); + #if AXIS_DRIVER_TYPE_E4(L6470) + #define DISABLE_STEPPER_E4() do{ stepperE4.free(); }while(0) + #endif + #endif +#endif + +// E5 Stepper +#if AXIS_IS_L64XX(E5) + extern L64XX_CLASS(E5) stepperE5; + #define E5_ENABLE_INIT() NOOP + #define E5_ENABLE_WRITE(STATE) (STATE ? stepperE5.hardStop() : stepperE5.free()) + #define E5_ENABLE_READ() (stepperE5.getStatus() & STATUS_HIZ) + #if AXIS_DRIVER_TYPE_E5(L6474) + #define E5_DIR_INIT() SET_OUTPUT(E5_DIR_PIN) + #define E5_DIR_WRITE(STATE) L6474_DIR_WRITE(E5, STATE) + #define E5_DIR_READ() READ(E5_DIR_PIN) + #else + #define E5_DIR_INIT() NOOP + #define E5_DIR_WRITE(STATE) L64XX_DIR_WRITE(E5, STATE) + #define E5_DIR_READ() (stepper##E5.getStatus() & STATUS_DIR); + #if AXIS_DRIVER_TYPE_E5(L6470) + #define DISABLE_STEPPER_E5() do{ stepperE5.free(); }while(0) + #endif + #endif +#endif + +// E6 Stepper +#if AXIS_IS_L64XX(E6) + extern L64XX_CLASS(E6) stepperE6; + #define E6_ENABLE_INIT() NOOP + #define E6_ENABLE_WRITE(STATE) (STATE ? stepperE6.hardStop() : stepperE6.free()) + #define E6_ENABLE_READ() (stepperE6.getStatus() & STATUS_HIZ) + #if AXIS_DRIVER_TYPE_E6(L6474) + #define E6_DIR_INIT() SET_OUTPUT(E6_DIR_PIN) + #define E6_DIR_WRITE(STATE) L6474_DIR_WRITE(E6, STATE) + #define E6_DIR_READ() READ(E6_DIR_PIN) + #else + #define E6_DIR_INIT() NOOP + #define E6_DIR_WRITE(STATE) L64XX_DIR_WRITE(E6, STATE) + #define E6_DIR_READ() (stepper##E6.getStatus() & STATUS_DIR); + #if AXIS_DRIVER_TYPE_E6(L6470) + #define DISABLE_STEPPER_E6() do{ stepperE6.free(); }while(0) + #endif + #endif +#endif + +// E7 Stepper +#if AXIS_IS_L64XX(E7) + extern L64XX_CLASS(E7) stepperE7; + #define E7_ENABLE_INIT() NOOP + #define E7_ENABLE_WRITE(STATE) (STATE ? stepperE7.hardStop() : stepperE7.free()) + #define E7_ENABLE_READ() (stepperE7.getStatus() & STATUS_HIZ) + #if AXIS_DRIVER_TYPE_E7(L6474) + #define E7_DIR_INIT() SET_OUTPUT(E7_DIR_PIN) + #define E7_DIR_WRITE(STATE) L6474_DIR_WRITE(E7, STATE) + #define E7_DIR_READ() READ(E7_DIR_PIN) + #else + #define E7_DIR_INIT() NOOP + #define E7_DIR_WRITE(STATE) L64XX_DIR_WRITE(E7, STATE) + #define E7_DIR_READ() (stepper##E7.getStatus() & STATUS_DIR); + #if AXIS_DRIVER_TYPE_E7(L6470) + #define DISABLE_STEPPER_E7() do{ stepperE7.free(); }while(0) + #endif + #endif +#endif diff --git a/Marlin/src/module/stepper/TMC26X.cpp b/Marlin/src/module/stepper/TMC26X.cpp new file mode 100644 index 0000000..926f1a4 --- /dev/null +++ b/Marlin/src/module/stepper/TMC26X.cpp @@ -0,0 +1,144 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +/** + * stepper/TMC26X.cpp + * Stepper driver indirection for TMC26X drivers + */ + +#include "../../inc/MarlinConfig.h" + +// +// TMC26X Driver objects and inits +// +#if HAS_TMC26X + +#include "TMC26X.h" + +#define _TMC26X_DEFINE(ST) TMC26XStepper stepper##ST(200, ST##_CS_PIN, ST##_STEP_PIN, ST##_DIR_PIN, ST##_MAX_CURRENT, ST##_SENSE_RESISTOR) + +#if AXIS_DRIVER_TYPE_X(TMC26X) + _TMC26X_DEFINE(X); +#endif +#if AXIS_DRIVER_TYPE_X2(TMC26X) + _TMC26X_DEFINE(X2); +#endif +#if AXIS_DRIVER_TYPE_Y(TMC26X) + _TMC26X_DEFINE(Y); +#endif +#if AXIS_DRIVER_TYPE_Y2(TMC26X) + _TMC26X_DEFINE(Y2); +#endif +#if AXIS_DRIVER_TYPE_Z(TMC26X) + _TMC26X_DEFINE(Z); +#endif +#if AXIS_DRIVER_TYPE_Z2(TMC26X) + _TMC26X_DEFINE(Z2); +#endif +#if AXIS_DRIVER_TYPE_Z3(TMC26X) + _TMC26X_DEFINE(Z3); +#endif +#if AXIS_DRIVER_TYPE_Z4(TMC26X) + _TMC26X_DEFINE(Z4); +#endif +#if AXIS_DRIVER_TYPE_E0(TMC26X) + _TMC26X_DEFINE(E0); +#endif +#if AXIS_DRIVER_TYPE_E1(TMC26X) + _TMC26X_DEFINE(E1); +#endif +#if AXIS_DRIVER_TYPE_E2(TMC26X) + _TMC26X_DEFINE(E2); +#endif +#if AXIS_DRIVER_TYPE_E3(TMC26X) + _TMC26X_DEFINE(E3); +#endif +#if AXIS_DRIVER_TYPE_E4(TMC26X) + _TMC26X_DEFINE(E4); +#endif +#if AXIS_DRIVER_TYPE_E5(TMC26X) + _TMC26X_DEFINE(E5); +#endif +#if AXIS_DRIVER_TYPE_E6(TMC26X) + _TMC26X_DEFINE(E6); +#endif +#if AXIS_DRIVER_TYPE_E7(TMC26X) + _TMC26X_DEFINE(E7); +#endif + +#define _TMC26X_INIT(A) do{ \ + stepper##A.setMicrosteps(A##_MICROSTEPS); \ + stepper##A.start(); \ +}while(0) + +void tmc26x_init_to_defaults() { + #if AXIS_DRIVER_TYPE_X(TMC26X) + _TMC26X_INIT(X); + #endif + #if AXIS_DRIVER_TYPE_X2(TMC26X) + _TMC26X_INIT(X2); + #endif + #if AXIS_DRIVER_TYPE_Y(TMC26X) + _TMC26X_INIT(Y); + #endif + #if AXIS_DRIVER_TYPE_Y2(TMC26X) + _TMC26X_INIT(Y2); + #endif + #if AXIS_DRIVER_TYPE_Z(TMC26X) + _TMC26X_INIT(Z); + #endif + #if AXIS_DRIVER_TYPE_Z2(TMC26X) + _TMC26X_INIT(Z2); + #endif + #if AXIS_DRIVER_TYPE_Z3(TMC26X) + _TMC26X_INIT(Z3); + #endif + #if AXIS_DRIVER_TYPE_Z4(TMC26X) + _TMC26X_INIT(Z4); + #endif + #if AXIS_DRIVER_TYPE_E0(TMC26X) + _TMC26X_INIT(E0); + #endif + #if AXIS_DRIVER_TYPE_E1(TMC26X) + _TMC26X_INIT(E1); + #endif + #if AXIS_DRIVER_TYPE_E2(TMC26X) + _TMC26X_INIT(E2); + #endif + #if AXIS_DRIVER_TYPE_E3(TMC26X) + _TMC26X_INIT(E3); + #endif + #if AXIS_DRIVER_TYPE_E4(TMC26X) + _TMC26X_INIT(E4); + #endif + #if AXIS_DRIVER_TYPE_E5(TMC26X) + _TMC26X_INIT(E5); + #endif + #if AXIS_DRIVER_TYPE_E6(TMC26X) + _TMC26X_INIT(E6); + #endif + #if AXIS_DRIVER_TYPE_E7(TMC26X) + _TMC26X_INIT(E7); + #endif +} + +#endif // HAS_TMC26X diff --git a/Marlin/src/module/stepper/TMC26X.h b/Marlin/src/module/stepper/TMC26X.h new file mode 100644 index 0000000..547eb65 --- /dev/null +++ b/Marlin/src/module/stepper/TMC26X.h @@ -0,0 +1,164 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +#pragma once + +/** + * stepper/TMC26X.h + * Stepper driver indirection for TMC26X drivers + */ + +#include "../../inc/MarlinConfig.h" + +// TMC26X drivers have STEP/DIR on normal pins, but ENABLE via SPI + +#include +#include + +void tmc26x_init_to_defaults(); + +// X Stepper +#if AXIS_DRIVER_TYPE_X(TMC26X) + extern TMC26XStepper stepperX; + #define X_ENABLE_INIT() NOOP + #define X_ENABLE_WRITE(STATE) stepperX.setEnabled(STATE) + #define X_ENABLE_READ() stepperX.isEnabled() +#endif + +// Y Stepper +#if AXIS_DRIVER_TYPE_Y(TMC26X) + extern TMC26XStepper stepperY; + #define Y_ENABLE_INIT() NOOP + #define Y_ENABLE_WRITE(STATE) stepperY.setEnabled(STATE) + #define Y_ENABLE_READ() stepperY.isEnabled() +#endif + +// Z Stepper +#if AXIS_DRIVER_TYPE_Z(TMC26X) + extern TMC26XStepper stepperZ; + #define Z_ENABLE_INIT() NOOP + #define Z_ENABLE_WRITE(STATE) stepperZ.setEnabled(STATE) + #define Z_ENABLE_READ() stepperZ.isEnabled() +#endif + +// X2 Stepper +#if HAS_X2_ENABLE && AXIS_DRIVER_TYPE_X2(TMC26X) + extern TMC26XStepper stepperX2; + #define X2_ENABLE_INIT() NOOP + #define X2_ENABLE_WRITE(STATE) stepperX2.setEnabled(STATE) + #define X2_ENABLE_READ() stepperX2.isEnabled() +#endif + +// Y2 Stepper +#if HAS_Y2_ENABLE && AXIS_DRIVER_TYPE_Y2(TMC26X) + extern TMC26XStepper stepperY2; + #define Y2_ENABLE_INIT() NOOP + #define Y2_ENABLE_WRITE(STATE) stepperY2.setEnabled(STATE) + #define Y2_ENABLE_READ() stepperY2.isEnabled() +#endif + +// Z2 Stepper +#if HAS_Z2_ENABLE && AXIS_DRIVER_TYPE_Z2(TMC26X) + extern TMC26XStepper stepperZ2; + #define Z2_ENABLE_INIT() NOOP + #define Z2_ENABLE_WRITE(STATE) stepperZ2.setEnabled(STATE) + #define Z2_ENABLE_READ() stepperZ2.isEnabled() +#endif + +// Z3 Stepper +#if HAS_Z3_ENABLE && AXIS_DRIVER_TYPE_Z3(TMC26X) + extern TMC26XStepper stepperZ3; + #define Z3_ENABLE_INIT() NOOP + #define Z3_ENABLE_WRITE(STATE) stepperZ3.setEnabled(STATE) + #define Z3_ENABLE_READ() stepperZ3.isEnabled() +#endif + +// Z4 Stepper +#if HAS_Z4_ENABLE && AXIS_DRIVER_TYPE_Z4(TMC26X) + extern TMC26XStepper stepperZ4; + #define Z4_ENABLE_INIT() NOOP + #define Z4_ENABLE_WRITE(STATE) stepperZ4.setEnabled(STATE) + #define Z4_ENABLE_READ() stepperZ4.isEnabled() +#endif + +// E0 Stepper +#if AXIS_DRIVER_TYPE_E0(TMC26X) + extern TMC26XStepper stepperE0; + #define E0_ENABLE_INIT() NOOP + #define E0_ENABLE_WRITE(STATE) stepperE0.setEnabled(STATE) + #define E0_ENABLE_READ() stepperE0.isEnabled() +#endif + +// E1 Stepper +#if AXIS_DRIVER_TYPE_E1(TMC26X) + extern TMC26XStepper stepperE1; + #define E1_ENABLE_INIT() NOOP + #define E1_ENABLE_WRITE(STATE) stepperE1.setEnabled(STATE) + #define E1_ENABLE_READ() stepperE1.isEnabled() +#endif + +// E2 Stepper +#if AXIS_DRIVER_TYPE_E2(TMC26X) + extern TMC26XStepper stepperE2; + #define E2_ENABLE_INIT() NOOP + #define E2_ENABLE_WRITE(STATE) stepperE2.setEnabled(STATE) + #define E2_ENABLE_READ() stepperE2.isEnabled() +#endif + +// E3 Stepper +#if AXIS_DRIVER_TYPE_E3(TMC26X) + extern TMC26XStepper stepperE3; + #define E3_ENABLE_INIT() NOOP + #define E3_ENABLE_WRITE(STATE) stepperE3.setEnabled(STATE) + #define E3_ENABLE_READ() stepperE3.isEnabled() +#endif + +// E4 Stepper +#if AXIS_DRIVER_TYPE_E4(TMC26X) + extern TMC26XStepper stepperE4; + #define E4_ENABLE_INIT() NOOP + #define E4_ENABLE_WRITE(STATE) stepperE4.setEnabled(STATE) + #define E4_ENABLE_READ() stepperE4.isEnabled() +#endif + +// E5 Stepper +#if AXIS_DRIVER_TYPE_E5(TMC26X) + extern TMC26XStepper stepperE5; + #define E5_ENABLE_INIT() NOOP + #define E5_ENABLE_WRITE(STATE) stepperE5.setEnabled(STATE) + #define E5_ENABLE_READ() stepperE5.isEnabled() +#endif + +// E6 Stepper +#if AXIS_DRIVER_TYPE_E6(TMC26X) + extern TMC26XStepper stepperE6; + #define E6_ENABLE_INIT() NOOP + #define E6_ENABLE_WRITE(STATE) stepperE6.setEnabled(STATE) + #define E6_ENABLE_READ() stepperE6.isEnabled() +#endif + +// E7 Stepper +#if AXIS_DRIVER_TYPE_E7(TMC26X) + extern TMC26XStepper stepperE7; + #define E7_ENABLE_INIT() NOOP + #define E7_ENABLE_WRITE(STATE) stepperE7.setEnabled(STATE) + #define E7_ENABLE_READ() stepperE7.isEnabled() +#endif diff --git a/Marlin/src/module/stepper/indirection.cpp b/Marlin/src/module/stepper/indirection.cpp new file mode 100644 index 0000000..6297d83 --- /dev/null +++ b/Marlin/src/module/stepper/indirection.cpp @@ -0,0 +1,50 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +/** + * stepper/indirection.cpp + * + * Stepper motor driver indirection to allow some stepper functions to + * be done via SPI/I2c instead of direct pin manipulation. + * + * Copyright (c) 2015 Dominik Wenger + */ + +#include "../../inc/MarlinConfig.h" +#include "indirection.h" + +void restore_stepper_drivers() { + TERN_(HAS_TRINAMIC_CONFIG, restore_trinamic_drivers()); +} + +void reset_stepper_drivers() { + #if HAS_DRIVER(TMC26X) + tmc26x_init_to_defaults(); + #endif + TERN_(HAS_L64XX, L64xxManager.init_to_defaults()); + TERN_(HAS_TRINAMIC_CONFIG, reset_trinamic_drivers()); +} + +#if ENABLED(SOFTWARE_DRIVER_ENABLE) + // Flags to optimize XYZ Enabled state + xyz_bool_t axis_sw_enabled; // = { false, false, false } +#endif diff --git a/Marlin/src/module/stepper/indirection.h b/Marlin/src/module/stepper/indirection.h new file mode 100644 index 0000000..4346e9d --- /dev/null +++ b/Marlin/src/module/stepper/indirection.h @@ -0,0 +1,1003 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +#pragma once + +/** + * stepper/indirection.h + * + * Stepper motor driver indirection to allow some stepper functions to + * be done via SPI/I2c instead of direct pin manipulation. + * + * Copyright (c) 2015 Dominik Wenger + */ + +#include "../../inc/MarlinConfig.h" + +#if HAS_L64XX + #include "L64xx.h" +#endif + +#if HAS_DRIVER(TMC26X) + #include "TMC26X.h" +#endif + +#if HAS_TRINAMIC_CONFIG + #include "trinamic.h" +#endif + +void restore_stepper_drivers(); // Called by PSU_ON +void reset_stepper_drivers(); // Called by settings.load / settings.reset + +// X Stepper +#ifndef X_ENABLE_INIT + #define X_ENABLE_INIT() SET_OUTPUT(X_ENABLE_PIN) + #define X_ENABLE_WRITE(STATE) WRITE(X_ENABLE_PIN,STATE) + #define X_ENABLE_READ() bool(READ(X_ENABLE_PIN)) +#endif +#ifndef X_DIR_INIT + #define X_DIR_INIT() SET_OUTPUT(X_DIR_PIN) + #define X_DIR_WRITE(STATE) WRITE(X_DIR_PIN,STATE) + #define X_DIR_READ() bool(READ(X_DIR_PIN)) +#endif +#define X_STEP_INIT() SET_OUTPUT(X_STEP_PIN) +#ifndef X_STEP_WRITE + #define X_STEP_WRITE(STATE) WRITE(X_STEP_PIN,STATE) +#endif +#define X_STEP_READ() bool(READ(X_STEP_PIN)) + +// Y Stepper +#ifndef Y_ENABLE_INIT + #define Y_ENABLE_INIT() SET_OUTPUT(Y_ENABLE_PIN) + #define Y_ENABLE_WRITE(STATE) WRITE(Y_ENABLE_PIN,STATE) + #define Y_ENABLE_READ() bool(READ(Y_ENABLE_PIN)) +#endif +#ifndef Y_DIR_INIT + #define Y_DIR_INIT() SET_OUTPUT(Y_DIR_PIN) + #define Y_DIR_WRITE(STATE) WRITE(Y_DIR_PIN,STATE) + #define Y_DIR_READ() bool(READ(Y_DIR_PIN)) +#endif +#define Y_STEP_INIT() SET_OUTPUT(Y_STEP_PIN) +#ifndef Y_STEP_WRITE + #define Y_STEP_WRITE(STATE) WRITE(Y_STEP_PIN,STATE) +#endif +#define Y_STEP_READ() bool(READ(Y_STEP_PIN)) + +// Z Stepper +#ifndef Z_ENABLE_INIT + #define Z_ENABLE_INIT() SET_OUTPUT(Z_ENABLE_PIN) + #define Z_ENABLE_WRITE(STATE) WRITE(Z_ENABLE_PIN,STATE) + #define Z_ENABLE_READ() bool(READ(Z_ENABLE_PIN)) +#endif +#ifndef Z_DIR_INIT + #define Z_DIR_INIT() SET_OUTPUT(Z_DIR_PIN) + #define Z_DIR_WRITE(STATE) WRITE(Z_DIR_PIN,STATE) + #define Z_DIR_READ() bool(READ(Z_DIR_PIN)) +#endif +#define Z_STEP_INIT() SET_OUTPUT(Z_STEP_PIN) +#ifndef Z_STEP_WRITE + #define Z_STEP_WRITE(STATE) WRITE(Z_STEP_PIN,STATE) +#endif +#define Z_STEP_READ() bool(READ(Z_STEP_PIN)) + +// X2 Stepper +#if HAS_X2_ENABLE + #ifndef X2_ENABLE_INIT + #define X2_ENABLE_INIT() SET_OUTPUT(X2_ENABLE_PIN) + #define X2_ENABLE_WRITE(STATE) WRITE(X2_ENABLE_PIN,STATE) + #define X2_ENABLE_READ() bool(READ(X2_ENABLE_PIN)) + #endif + #ifndef X2_DIR_INIT + #define X2_DIR_INIT() SET_OUTPUT(X2_DIR_PIN) + #define X2_DIR_WRITE(STATE) WRITE(X2_DIR_PIN,STATE) + #define X2_DIR_READ() bool(READ(X2_DIR_PIN)) + #endif + #define X2_STEP_INIT() SET_OUTPUT(X2_STEP_PIN) + #ifndef X2_STEP_WRITE + #define X2_STEP_WRITE(STATE) WRITE(X2_STEP_PIN,STATE) + #endif + #define X2_STEP_READ() bool(READ(X2_STEP_PIN)) +#endif + +// Y2 Stepper +#if HAS_Y2_ENABLE + #ifndef Y2_ENABLE_INIT + #define Y2_ENABLE_INIT() SET_OUTPUT(Y2_ENABLE_PIN) + #define Y2_ENABLE_WRITE(STATE) WRITE(Y2_ENABLE_PIN,STATE) + #define Y2_ENABLE_READ() bool(READ(Y2_ENABLE_PIN)) + #endif + #ifndef Y2_DIR_INIT + #define Y2_DIR_INIT() SET_OUTPUT(Y2_DIR_PIN) + #define Y2_DIR_WRITE(STATE) WRITE(Y2_DIR_PIN,STATE) + #define Y2_DIR_READ() bool(READ(Y2_DIR_PIN)) + #endif + #define Y2_STEP_INIT() SET_OUTPUT(Y2_STEP_PIN) + #ifndef Y2_STEP_WRITE + #define Y2_STEP_WRITE(STATE) WRITE(Y2_STEP_PIN,STATE) + #endif + #define Y2_STEP_READ() bool(READ(Y2_STEP_PIN)) +#else + #define Y2_DIR_WRITE(STATE) NOOP +#endif + +// Z2 Stepper +#if HAS_Z2_ENABLE + #ifndef Z2_ENABLE_INIT + #define Z2_ENABLE_INIT() SET_OUTPUT(Z2_ENABLE_PIN) + #define Z2_ENABLE_WRITE(STATE) WRITE(Z2_ENABLE_PIN,STATE) + #define Z2_ENABLE_READ() bool(READ(Z2_ENABLE_PIN)) + #endif + #ifndef Z2_DIR_INIT + #define Z2_DIR_INIT() SET_OUTPUT(Z2_DIR_PIN) + #define Z2_DIR_WRITE(STATE) WRITE(Z2_DIR_PIN,STATE) + #define Z2_DIR_READ() bool(READ(Z2_DIR_PIN)) + #endif + #define Z2_STEP_INIT() SET_OUTPUT(Z2_STEP_PIN) + #ifndef Z2_STEP_WRITE + #define Z2_STEP_WRITE(STATE) WRITE(Z2_STEP_PIN,STATE) + #endif + #define Z2_STEP_READ() bool(READ(Z2_STEP_PIN)) +#else + #define Z2_DIR_WRITE(STATE) NOOP +#endif + +// Z3 Stepper +#if HAS_Z3_ENABLE + #ifndef Z3_ENABLE_INIT + #define Z3_ENABLE_INIT() SET_OUTPUT(Z3_ENABLE_PIN) + #define Z3_ENABLE_WRITE(STATE) WRITE(Z3_ENABLE_PIN,STATE) + #define Z3_ENABLE_READ() bool(READ(Z3_ENABLE_PIN)) + #endif + #ifndef Z3_DIR_INIT + #define Z3_DIR_INIT() SET_OUTPUT(Z3_DIR_PIN) + #define Z3_DIR_WRITE(STATE) WRITE(Z3_DIR_PIN,STATE) + #define Z3_DIR_READ() bool(READ(Z3_DIR_PIN)) + #endif + #define Z3_STEP_INIT() SET_OUTPUT(Z3_STEP_PIN) + #ifndef Z3_STEP_WRITE + #define Z3_STEP_WRITE(STATE) WRITE(Z3_STEP_PIN,STATE) + #endif + #define Z3_STEP_READ() bool(READ(Z3_STEP_PIN)) +#else + #define Z3_DIR_WRITE(STATE) NOOP +#endif + +// Z4 Stepper +#if HAS_Z4_ENABLE + #ifndef Z4_ENABLE_INIT + #define Z4_ENABLE_INIT() SET_OUTPUT(Z4_ENABLE_PIN) + #define Z4_ENABLE_WRITE(STATE) WRITE(Z4_ENABLE_PIN,STATE) + #define Z4_ENABLE_READ() bool(READ(Z4_ENABLE_PIN)) + #endif + #ifndef Z4_DIR_INIT + #define Z4_DIR_INIT() SET_OUTPUT(Z4_DIR_PIN) + #define Z4_DIR_WRITE(STATE) WRITE(Z4_DIR_PIN,STATE) + #define Z4_DIR_READ() bool(READ(Z4_DIR_PIN)) + #endif + #define Z4_STEP_INIT() SET_OUTPUT(Z4_STEP_PIN) + #ifndef Z4_STEP_WRITE + #define Z4_STEP_WRITE(STATE) WRITE(Z4_STEP_PIN,STATE) + #endif + #define Z4_STEP_READ() bool(READ(Z4_STEP_PIN)) +#else + #define Z4_DIR_WRITE(STATE) NOOP +#endif + +// E0 Stepper +#ifndef E0_ENABLE_INIT + #define E0_ENABLE_INIT() SET_OUTPUT(E0_ENABLE_PIN) + #define E0_ENABLE_WRITE(STATE) WRITE(E0_ENABLE_PIN,STATE) + #define E0_ENABLE_READ() bool(READ(E0_ENABLE_PIN)) +#endif +#ifndef E0_DIR_INIT + #define E0_DIR_INIT() SET_OUTPUT(E0_DIR_PIN) + #define E0_DIR_WRITE(STATE) WRITE(E0_DIR_PIN,STATE) + #define E0_DIR_READ() bool(READ(E0_DIR_PIN)) +#endif +#define E0_STEP_INIT() SET_OUTPUT(E0_STEP_PIN) +#ifndef E0_STEP_WRITE + #define E0_STEP_WRITE(STATE) WRITE(E0_STEP_PIN,STATE) +#endif +#define E0_STEP_READ() bool(READ(E0_STEP_PIN)) + +// E1 Stepper +#ifndef E1_ENABLE_INIT + #define E1_ENABLE_INIT() SET_OUTPUT(E1_ENABLE_PIN) + #define E1_ENABLE_WRITE(STATE) WRITE(E1_ENABLE_PIN,STATE) + #define E1_ENABLE_READ() bool(READ(E1_ENABLE_PIN)) +#endif +#ifndef E1_DIR_INIT + #define E1_DIR_INIT() SET_OUTPUT(E1_DIR_PIN) + #define E1_DIR_WRITE(STATE) WRITE(E1_DIR_PIN,STATE) + #define E1_DIR_READ() bool(READ(E1_DIR_PIN)) +#endif +#define E1_STEP_INIT() SET_OUTPUT(E1_STEP_PIN) +#ifndef E1_STEP_WRITE + #define E1_STEP_WRITE(STATE) WRITE(E1_STEP_PIN,STATE) +#endif +#define E1_STEP_READ() bool(READ(E1_STEP_PIN)) + +// E2 Stepper +#ifndef E2_ENABLE_INIT + #define E2_ENABLE_INIT() SET_OUTPUT(E2_ENABLE_PIN) + #define E2_ENABLE_WRITE(STATE) WRITE(E2_ENABLE_PIN,STATE) + #define E2_ENABLE_READ() bool(READ(E2_ENABLE_PIN)) +#endif +#ifndef E2_DIR_INIT + #define E2_DIR_INIT() SET_OUTPUT(E2_DIR_PIN) + #define E2_DIR_WRITE(STATE) WRITE(E2_DIR_PIN,STATE) + #define E2_DIR_READ() bool(READ(E2_DIR_PIN)) +#endif +#define E2_STEP_INIT() SET_OUTPUT(E2_STEP_PIN) +#ifndef E2_STEP_WRITE + #define E2_STEP_WRITE(STATE) WRITE(E2_STEP_PIN,STATE) +#endif +#define E2_STEP_READ() bool(READ(E2_STEP_PIN)) + +// E3 Stepper +#ifndef E3_ENABLE_INIT + #define E3_ENABLE_INIT() SET_OUTPUT(E3_ENABLE_PIN) + #define E3_ENABLE_WRITE(STATE) WRITE(E3_ENABLE_PIN,STATE) + #define E3_ENABLE_READ() bool(READ(E3_ENABLE_PIN)) +#endif +#ifndef E3_DIR_INIT + #define E3_DIR_INIT() SET_OUTPUT(E3_DIR_PIN) + #define E3_DIR_WRITE(STATE) WRITE(E3_DIR_PIN,STATE) + #define E3_DIR_READ() bool(READ(E3_DIR_PIN)) +#endif +#define E3_STEP_INIT() SET_OUTPUT(E3_STEP_PIN) +#ifndef E3_STEP_WRITE + #define E3_STEP_WRITE(STATE) WRITE(E3_STEP_PIN,STATE) +#endif +#define E3_STEP_READ() bool(READ(E3_STEP_PIN)) + +// E4 Stepper +#ifndef E4_ENABLE_INIT + #define E4_ENABLE_INIT() SET_OUTPUT(E4_ENABLE_PIN) + #define E4_ENABLE_WRITE(STATE) WRITE(E4_ENABLE_PIN,STATE) + #define E4_ENABLE_READ() bool(READ(E4_ENABLE_PIN)) +#endif +#ifndef E4_DIR_INIT + #define E4_DIR_INIT() SET_OUTPUT(E4_DIR_PIN) + #define E4_DIR_WRITE(STATE) WRITE(E4_DIR_PIN,STATE) + #define E4_DIR_READ() bool(READ(E4_DIR_PIN)) +#endif +#define E4_STEP_INIT() SET_OUTPUT(E4_STEP_PIN) +#ifndef E4_STEP_WRITE + #define E4_STEP_WRITE(STATE) WRITE(E4_STEP_PIN,STATE) +#endif +#define E4_STEP_READ() bool(READ(E4_STEP_PIN)) + +// E5 Stepper +#ifndef E5_ENABLE_INIT + #define E5_ENABLE_INIT() SET_OUTPUT(E5_ENABLE_PIN) + #define E5_ENABLE_WRITE(STATE) WRITE(E5_ENABLE_PIN,STATE) + #define E5_ENABLE_READ() bool(READ(E5_ENABLE_PIN)) +#endif +#ifndef E5_DIR_INIT + #define E5_DIR_INIT() SET_OUTPUT(E5_DIR_PIN) + #define E5_DIR_WRITE(STATE) WRITE(E5_DIR_PIN,STATE) + #define E5_DIR_READ() bool(READ(E5_DIR_PIN)) +#endif +#define E5_STEP_INIT() SET_OUTPUT(E5_STEP_PIN) +#ifndef E5_STEP_WRITE + #define E5_STEP_WRITE(STATE) WRITE(E5_STEP_PIN,STATE) +#endif +#define E5_STEP_READ() bool(READ(E5_STEP_PIN)) + +// E6 Stepper +#ifndef E6_ENABLE_INIT + #define E6_ENABLE_INIT() SET_OUTPUT(E6_ENABLE_PIN) + #define E6_ENABLE_WRITE(STATE) WRITE(E6_ENABLE_PIN,STATE) + #define E6_ENABLE_READ() bool(READ(E6_ENABLE_PIN)) +#endif +#ifndef E6_DIR_INIT + #define E6_DIR_INIT() SET_OUTPUT(E6_DIR_PIN) + #define E6_DIR_WRITE(STATE) WRITE(E6_DIR_PIN,STATE) + #define E6_DIR_READ() bool(READ(E6_DIR_PIN)) +#endif +#define E6_STEP_INIT() SET_OUTPUT(E6_STEP_PIN) +#ifndef E6_STEP_WRITE + #define E6_STEP_WRITE(STATE) WRITE(E6_STEP_PIN,STATE) +#endif +#define E6_STEP_READ() bool(READ(E6_STEP_PIN)) + +// E7 Stepper +#ifndef E7_ENABLE_INIT + #define E7_ENABLE_INIT() SET_OUTPUT(E7_ENABLE_PIN) + #define E7_ENABLE_WRITE(STATE) WRITE(E7_ENABLE_PIN,STATE) + #define E7_ENABLE_READ() bool(READ(E7_ENABLE_PIN)) +#endif +#ifndef E7_DIR_INIT + #define E7_DIR_INIT() SET_OUTPUT(E7_DIR_PIN) + #define E7_DIR_WRITE(STATE) WRITE(E7_DIR_PIN,STATE) + #define E7_DIR_READ() bool(READ(E7_DIR_PIN)) +#endif +#define E7_STEP_INIT() SET_OUTPUT(E7_STEP_PIN) +#ifndef E7_STEP_WRITE + #define E7_STEP_WRITE(STATE) WRITE(E7_STEP_PIN,STATE) +#endif +#define E7_STEP_READ() bool(READ(E7_STEP_PIN)) + +/** + * Extruder indirection for the single E axis + */ +#if ENABLED(SWITCHING_EXTRUDER) // One stepper driver per two extruders, reversed on odd index + #if EXTRUDERS > 7 + #define E_STEP_WRITE(E,V) do{ if (E < 2) { E0_STEP_WRITE(V); } else if (E < 4) { E1_STEP_WRITE(V); } else if (E < 6) { E2_STEP_WRITE(V); } else { E3_STEP_WRITE(V); } }while(0) + #define NORM_E_DIR(E) do{ switch (E) { \ + case 0: E0_DIR_WRITE(!INVERT_E0_DIR); break; case 1: E0_DIR_WRITE( INVERT_E0_DIR); break; \ + case 2: E1_DIR_WRITE(!INVERT_E1_DIR); break; case 3: E1_DIR_WRITE( INVERT_E1_DIR); break; \ + case 4: E2_DIR_WRITE(!INVERT_E2_DIR); break; case 5: E2_DIR_WRITE( INVERT_E2_DIR); break; \ + case 6: E3_DIR_WRITE( INVERT_E3_DIR); break; case 7: E3_DIR_WRITE( INVERT_E3_DIR); break; \ + } }while(0) + #define REV_E_DIR(E) do{ switch (E) { \ + case 0: E0_DIR_WRITE( INVERT_E0_DIR); break; case 1: E0_DIR_WRITE(!INVERT_E0_DIR); break; \ + case 2: E1_DIR_WRITE( INVERT_E1_DIR); break; case 3: E1_DIR_WRITE(!INVERT_E1_DIR); break; \ + case 4: E2_DIR_WRITE( INVERT_E2_DIR); break; case 5: E2_DIR_WRITE(!INVERT_E2_DIR); break; \ + case 6: E3_DIR_WRITE(!INVERT_E3_DIR); break; case 7: E3_DIR_WRITE(!INVERT_E3_DIR); break; \ + } }while(0) + #elif EXTRUDERS > 6 + #define E_STEP_WRITE(E,V) do{ if (E < 2) { E0_STEP_WRITE(V); } else if (E < 4) { E1_STEP_WRITE(V); } else if (E < 6) { E2_STEP_WRITE(V); } else { E3_STEP_WRITE(V); } }while(0) + #define NORM_E_DIR(E) do{ switch (E) { \ + case 0: E0_DIR_WRITE(!INVERT_E0_DIR); break; case 1: E0_DIR_WRITE( INVERT_E0_DIR); break; \ + case 2: E1_DIR_WRITE(!INVERT_E1_DIR); break; case 3: E1_DIR_WRITE( INVERT_E1_DIR); break; \ + case 4: E2_DIR_WRITE(!INVERT_E2_DIR); break; case 5: E2_DIR_WRITE( INVERT_E2_DIR); break; \ + case 6: E3_DIR_WRITE( INVERT_E3_DIR); break; \ + } }while(0) + #define REV_E_DIR(E) do{ switch (E) { \ + case 0: E0_DIR_WRITE( INVERT_E0_DIR); break; case 1: E0_DIR_WRITE(!INVERT_E0_DIR); break; \ + case 2: E1_DIR_WRITE( INVERT_E1_DIR); break; case 3: E1_DIR_WRITE(!INVERT_E1_DIR); break; \ + case 4: E2_DIR_WRITE( INVERT_E2_DIR); break; case 5: E2_DIR_WRITE(!INVERT_E2_DIR); break; \ + case 6: E3_DIR_WRITE(!INVERT_E3_DIR); } }while(0) + #elif EXTRUDERS > 5 + #define E_STEP_WRITE(E,V) do{ if (E < 2) { E0_STEP_WRITE(V); } else if (E < 4) { E1_STEP_WRITE(V); } else { E2_STEP_WRITE(V); } }while(0) + #define NORM_E_DIR(E) do{ switch (E) { \ + case 0: E0_DIR_WRITE(!INVERT_E0_DIR); break; case 1: E0_DIR_WRITE( INVERT_E0_DIR); break; \ + case 2: E1_DIR_WRITE(!INVERT_E1_DIR); break; case 3: E1_DIR_WRITE( INVERT_E1_DIR); break; \ + case 4: E2_DIR_WRITE(!INVERT_E2_DIR); break; case 5: E2_DIR_WRITE( INVERT_E2_DIR); break; \ + } }while(0) + #define REV_E_DIR(E) do{ switch (E) { \ + case 0: E0_DIR_WRITE( INVERT_E0_DIR); break; case 1: E0_DIR_WRITE(!INVERT_E0_DIR); break; \ + case 2: E1_DIR_WRITE( INVERT_E1_DIR); break; case 3: E1_DIR_WRITE(!INVERT_E1_DIR); break; \ + case 4: E2_DIR_WRITE( INVERT_E2_DIR); break; case 5: E2_DIR_WRITE(!INVERT_E2_DIR); break; \ + } }while(0) + #elif EXTRUDERS > 4 + #define E_STEP_WRITE(E,V) do{ if (E < 2) { E0_STEP_WRITE(V); } else if (E < 4) { E1_STEP_WRITE(V); } else { E2_STEP_WRITE(V); } }while(0) + #define NORM_E_DIR(E) do{ switch (E) { \ + case 0: E0_DIR_WRITE(!INVERT_E0_DIR); break; case 1: E0_DIR_WRITE( INVERT_E0_DIR); break; \ + case 2: E1_DIR_WRITE(!INVERT_E1_DIR); break; case 3: E1_DIR_WRITE( INVERT_E1_DIR); break; \ + case 4: E2_DIR_WRITE(!INVERT_E2_DIR); break; \ + } }while(0) + #define REV_E_DIR(E) do{ switch (E) { \ + case 0: E0_DIR_WRITE( INVERT_E0_DIR); break; case 1: E0_DIR_WRITE(!INVERT_E0_DIR); break; \ + case 2: E1_DIR_WRITE( INVERT_E1_DIR); break; case 3: E1_DIR_WRITE(!INVERT_E1_DIR); break; \ + case 4: E2_DIR_WRITE( INVERT_E2_DIR); break; \ + } }while(0) + #elif EXTRUDERS > 3 + #define E_STEP_WRITE(E,V) do{ if (E < 2) { E0_STEP_WRITE(V); } else { E1_STEP_WRITE(V); } }while(0) + #define NORM_E_DIR(E) do{ switch (E) { \ + case 0: E0_DIR_WRITE(!INVERT_E0_DIR); break; case 1: E0_DIR_WRITE( INVERT_E0_DIR); break; \ + case 2: E1_DIR_WRITE(!INVERT_E1_DIR); break; case 3: E1_DIR_WRITE( INVERT_E1_DIR); break; \ + } }while(0) + #define REV_E_DIR(E) do{ switch (E) { \ + case 0: E0_DIR_WRITE( INVERT_E0_DIR); break; case 1: E0_DIR_WRITE(!INVERT_E0_DIR); break; \ + case 2: E1_DIR_WRITE( INVERT_E1_DIR); break; case 3: E1_DIR_WRITE(!INVERT_E1_DIR); break; \ + } }while(0) + #elif EXTRUDERS > 2 + #define E_STEP_WRITE(E,V) do{ if (E < 2) { E0_STEP_WRITE(V); } else { E1_STEP_WRITE(V); } }while(0) + #define NORM_E_DIR(E) do{ switch (E) { \ + case 0: E0_DIR_WRITE(!INVERT_E0_DIR); break; case 1: E0_DIR_WRITE( INVERT_E0_DIR); break; \ + case 2: E1_DIR_WRITE(!INVERT_E1_DIR); break; \ + } }while(0) + #define REV_E_DIR(E) do{ switch (E) { \ + case 0: E0_DIR_WRITE( INVERT_E0_DIR); break; case 1: E0_DIR_WRITE(!INVERT_E0_DIR); break; \ + case 2: E1_DIR_WRITE( INVERT_E1_DIR); break; \ + } }while(0) + #else + #define E_STEP_WRITE(E,V) E0_STEP_WRITE(V) + #define NORM_E_DIR(E) do{ E0_DIR_WRITE(E ? INVERT_E0_DIR : !INVERT_E0_DIR); }while(0) + #define REV_E_DIR(E) do{ E0_DIR_WRITE(E ? !INVERT_E0_DIR : INVERT_E0_DIR); }while(0) + #endif + +#elif HAS_PRUSA_MMU2 + + #define E_STEP_WRITE(E,V) E0_STEP_WRITE(V) + #define NORM_E_DIR(E) E0_DIR_WRITE(!INVERT_E0_DIR) + #define REV_E_DIR(E) E0_DIR_WRITE( INVERT_E0_DIR) + +#elif HAS_PRUSA_MMU1 // One multiplexed stepper driver, reversed on odd index + + #define E_STEP_WRITE(E,V) E0_STEP_WRITE(V) + #define NORM_E_DIR(E) do{ E0_DIR_WRITE(TEST(E, 0) ? !INVERT_E0_DIR: INVERT_E0_DIR); }while(0) + #define REV_E_DIR(E) do{ E0_DIR_WRITE(TEST(E, 0) ? INVERT_E0_DIR: !INVERT_E0_DIR); }while(0) + +#elif E_STEPPERS > 1 + + #if E_STEPPERS > 7 + + #define _E_STEP_WRITE(E,V) do{ switch (E) { \ + case 0: E0_STEP_WRITE(V); break; case 1: E1_STEP_WRITE(V); break; case 2: E2_STEP_WRITE(V); break; case 3: E3_STEP_WRITE(V); break; \ + case 4: E4_STEP_WRITE(V); break; case 5: E5_STEP_WRITE(V); break; case 6: E6_STEP_WRITE(V); break; case 7: E7_STEP_WRITE(V); break; \ + } }while(0) + #define _NORM_E_DIR(E) do{ switch (E) { \ + case 0: E0_DIR_WRITE(!INVERT_E0_DIR); break; case 1: E1_DIR_WRITE(!INVERT_E1_DIR); break; \ + case 2: E2_DIR_WRITE(!INVERT_E2_DIR); break; case 3: E3_DIR_WRITE(!INVERT_E3_DIR); break; \ + case 4: E4_DIR_WRITE(!INVERT_E4_DIR); break; case 5: E5_DIR_WRITE(!INVERT_E5_DIR); break; \ + case 6: E6_DIR_WRITE(!INVERT_E6_DIR); break; case 7: E7_DIR_WRITE(!INVERT_E7_DIR); break; \ + } }while(0) + #define _REV_E_DIR(E) do{ switch (E) { \ + case 0: E0_DIR_WRITE( INVERT_E0_DIR); break; case 1: E1_DIR_WRITE( INVERT_E1_DIR); break; \ + case 2: E2_DIR_WRITE( INVERT_E2_DIR); break; case 3: E3_DIR_WRITE( INVERT_E3_DIR); break; \ + case 4: E4_DIR_WRITE( INVERT_E4_DIR); break; case 5: E5_DIR_WRITE( INVERT_E5_DIR); break; \ + case 6: E6_DIR_WRITE( INVERT_E6_DIR); break; case 7: E7_DIR_WRITE( INVERT_E7_DIR); break; \ + } }while(0) + + #elif E_STEPPERS > 6 + + #define _E_STEP_WRITE(E,V) do{ switch (E) { \ + case 0: E0_STEP_WRITE(V); break; case 1: E1_STEP_WRITE(V); break; case 2: E2_STEP_WRITE(V); break; case 3: E3_STEP_WRITE(V); break; \ + case 4: E4_STEP_WRITE(V); break; case 5: E5_STEP_WRITE(V); break; case 6: E6_STEP_WRITE(V); break; \ + } }while(0) + #define _NORM_E_DIR(E) do{ switch (E) { \ + case 0: E0_DIR_WRITE(!INVERT_E0_DIR); break; case 1: E1_DIR_WRITE(!INVERT_E1_DIR); break; \ + case 2: E2_DIR_WRITE(!INVERT_E2_DIR); break; case 3: E3_DIR_WRITE(!INVERT_E3_DIR); break; \ + case 4: E4_DIR_WRITE(!INVERT_E4_DIR); break; case 5: E5_DIR_WRITE(!INVERT_E5_DIR); break; \ + case 6: E6_DIR_WRITE(!INVERT_E6_DIR); break; \ + } }while(0) + #define _REV_E_DIR(E) do{ switch (E) { \ + case 0: E0_DIR_WRITE( INVERT_E0_DIR); break; case 1: E1_DIR_WRITE( INVERT_E1_DIR); break; \ + case 2: E2_DIR_WRITE( INVERT_E2_DIR); break; case 3: E3_DIR_WRITE( INVERT_E3_DIR); break; \ + case 4: E4_DIR_WRITE( INVERT_E4_DIR); break; case 5: E5_DIR_WRITE( INVERT_E5_DIR); break; \ + case 6: E6_DIR_WRITE( INVERT_E6_DIR); break; \ + } }while(0) + + #elif E_STEPPERS > 5 + + #define _E_STEP_WRITE(E,V) do{ switch (E) { \ + case 0: E0_STEP_WRITE(V); break; case 1: E1_STEP_WRITE(V); break; case 2: E2_STEP_WRITE(V); break; case 3: E3_STEP_WRITE(V); break; \ + case 4: E4_STEP_WRITE(V); break; case 5: E5_STEP_WRITE(V); break; \ + } }while(0) + #define _NORM_E_DIR(E) do{ switch (E) { \ + case 0: E0_DIR_WRITE(!INVERT_E0_DIR); break; case 1: E1_DIR_WRITE(!INVERT_E1_DIR); break; \ + case 2: E2_DIR_WRITE(!INVERT_E2_DIR); break; case 3: E3_DIR_WRITE(!INVERT_E3_DIR); break; \ + case 4: E4_DIR_WRITE(!INVERT_E4_DIR); break; case 5: E5_DIR_WRITE(!INVERT_E5_DIR); break; \ + } }while(0) + #define _REV_E_DIR(E) do{ switch (E) { \ + case 0: E0_DIR_WRITE( INVERT_E0_DIR); break; case 1: E1_DIR_WRITE( INVERT_E1_DIR); break; \ + case 2: E2_DIR_WRITE( INVERT_E2_DIR); break; case 3: E3_DIR_WRITE( INVERT_E3_DIR); break; \ + case 4: E4_DIR_WRITE( INVERT_E4_DIR); break; case 5: E5_DIR_WRITE( INVERT_E5_DIR); break; \ + } }while(0) + + #elif E_STEPPERS > 4 + + #define _E_STEP_WRITE(E,V) do{ switch (E) { \ + case 0: E0_STEP_WRITE(V); break; case 1: E1_STEP_WRITE(V); break; case 2: E2_STEP_WRITE(V); break; case 3: E3_STEP_WRITE(V); break; \ + case 4: E4_STEP_WRITE(V); break; \ + } }while(0) + #define _NORM_E_DIR(E) do{ switch (E) { \ + case 0: E0_DIR_WRITE(!INVERT_E0_DIR); break; case 1: E1_DIR_WRITE(!INVERT_E1_DIR); break; \ + case 2: E2_DIR_WRITE(!INVERT_E2_DIR); break; case 3: E3_DIR_WRITE(!INVERT_E3_DIR); break; \ + case 4: E4_DIR_WRITE(!INVERT_E4_DIR); break; \ + } }while(0) + #define _REV_E_DIR(E) do{ switch (E) { \ + case 0: E0_DIR_WRITE( INVERT_E0_DIR); break; case 1: E1_DIR_WRITE( INVERT_E1_DIR); break; \ + case 2: E2_DIR_WRITE( INVERT_E2_DIR); break; case 3: E3_DIR_WRITE( INVERT_E3_DIR); break; \ + case 4: E4_DIR_WRITE( INVERT_E4_DIR); break; \ + } }while(0) + + #elif E_STEPPERS > 3 + + #define _E_STEP_WRITE(E,V) do{ switch (E) { \ + case 0: E0_STEP_WRITE(V); break; case 1: E1_STEP_WRITE(V); break; case 2: E2_STEP_WRITE(V); break; case 3: E3_STEP_WRITE(V); break; \ + } }while(0) + #define _NORM_E_DIR(E) do{ switch (E) { \ + case 0: E0_DIR_WRITE(!INVERT_E0_DIR); break; case 1: E1_DIR_WRITE(!INVERT_E1_DIR); break; \ + case 2: E2_DIR_WRITE(!INVERT_E2_DIR); break; case 3: E3_DIR_WRITE(!INVERT_E3_DIR); break; \ + } }while(0) + #define _REV_E_DIR(E) do{ switch (E) { \ + case 0: E0_DIR_WRITE( INVERT_E0_DIR); break; case 1: E1_DIR_WRITE( INVERT_E1_DIR); break; \ + case 2: E2_DIR_WRITE( INVERT_E2_DIR); break; case 3: E3_DIR_WRITE( INVERT_E3_DIR); break; \ + } }while(0) + + #elif E_STEPPERS > 2 + + #define _E_STEP_WRITE(E,V) do{ switch (E) { case 0: E0_STEP_WRITE(V); break; case 1: E1_STEP_WRITE(V); break; case 2: E2_STEP_WRITE(V); } }while(0) + #define _NORM_E_DIR(E) do{ switch (E) { case 0: E0_DIR_WRITE(!INVERT_E0_DIR); break; case 1: E1_DIR_WRITE(!INVERT_E1_DIR); break; case 2: E2_DIR_WRITE(!INVERT_E2_DIR); } }while(0) + #define _REV_E_DIR(E) do{ switch (E) { case 0: E0_DIR_WRITE( INVERT_E0_DIR); break; case 1: E1_DIR_WRITE( INVERT_E1_DIR); break; case 2: E2_DIR_WRITE( INVERT_E2_DIR); } }while(0) + + #else + + #define _E_STEP_WRITE(E,V) do{ if (E == 0) { E0_STEP_WRITE(V); } else { E1_STEP_WRITE(V); } }while(0) + #define _NORM_E_DIR(E) do{ if (E == 0) { E0_DIR_WRITE(!INVERT_E0_DIR); } else { E1_DIR_WRITE(!INVERT_E1_DIR); } }while(0) + #define _REV_E_DIR(E) do{ if (E == 0) { E0_DIR_WRITE( INVERT_E0_DIR); } else { E1_DIR_WRITE( INVERT_E1_DIR); } }while(0) + #endif + + #if HAS_DUPLICATION_MODE + + #if ENABLED(MULTI_NOZZLE_DUPLICATION) + #define _DUPE(N,T,V) do{ if (TEST(duplication_e_mask, N)) E##N##_##T##_WRITE(V); }while(0) + #else + #define _DUPE(N,T,V) E##N##_##T##_WRITE(V) + #endif + + #define NDIR(N) _DUPE(N,DIR,!INVERT_E##N##_DIR) + #define RDIR(N) _DUPE(N,DIR, INVERT_E##N##_DIR) + + #define E_STEP_WRITE(E,V) do{ if (extruder_duplication_enabled) { DUPE(STEP,V); } else _E_STEP_WRITE(E,V); }while(0) + + #if E_STEPPERS > 2 + #if E_STEPPERS > 7 + #define DUPE(T,V) do{ _DUPE(0,T,V); _DUPE(1,T,V); _DUPE(2,T,V); _DUPE(3,T,V); _DUPE(4,T,V); _DUPE(5,T,V); _DUPE(6,T,V); _DUPE(7,T,V); }while(0) + #define NORM_E_DIR(E) do{ if (extruder_duplication_enabled) { NDIR(0); NDIR(1); NDIR(2); NDIR(3); NDIR(4); NDIR(5); NDIR(6); NDIR(7); } else _NORM_E_DIR(E); }while(0) + #define REV_E_DIR(E) do{ if (extruder_duplication_enabled) { RDIR(0); RDIR(1); RDIR(2); RDIR(3); RDIR(4); RDIR(5); RDIR(6); RDIR(7); } else _REV_E_DIR(E); }while(0) + #elif E_STEPPERS > 6 + #define DUPE(T,V) do{ _DUPE(0,T,V); _DUPE(1,T,V); _DUPE(2,T,V); _DUPE(3,T,V); _DUPE(4,T,V); _DUPE(5,T,V); _DUPE(6,T,V); }while(0) + #define NORM_E_DIR(E) do{ if (extruder_duplication_enabled) { NDIR(0); NDIR(1); NDIR(2); NDIR(3); NDIR(4); NDIR(5); NDIR(6); } else _NORM_E_DIR(E); }while(0) + #define REV_E_DIR(E) do{ if (extruder_duplication_enabled) { RDIR(0); RDIR(1); RDIR(2); RDIR(3); RDIR(4); RDIR(5); RDIR(6); } else _REV_E_DIR(E); }while(0) + #elif E_STEPPERS > 5 + #define DUPE(T,V) do{ _DUPE(0,T,V); _DUPE(1,T,V); _DUPE(2,T,V); _DUPE(3,T,V); _DUPE(4,T,V); _DUPE(5,T,V); }while(0) + #define NORM_E_DIR(E) do{ if (extruder_duplication_enabled) { NDIR(0); NDIR(1); NDIR(2); NDIR(3); NDIR(4); NDIR(5); } else _NORM_E_DIR(E); }while(0) + #define REV_E_DIR(E) do{ if (extruder_duplication_enabled) { RDIR(0); RDIR(1); RDIR(2); RDIR(3); RDIR(4); RDIR(5); } else _REV_E_DIR(E); }while(0) + #elif E_STEPPERS > 4 + #define DUPE(T,V) do{ _DUPE(0,T,V); _DUPE(1,T,V); _DUPE(2,T,V); _DUPE(3,T,V); _DUPE(4,T,V); }while(0) + #define NORM_E_DIR(E) do{ if (extruder_duplication_enabled) { NDIR(0); NDIR(1); NDIR(2); NDIR(3); NDIR(4); } else _NORM_E_DIR(E); }while(0) + #define REV_E_DIR(E) do{ if (extruder_duplication_enabled) { RDIR(0); RDIR(1); RDIR(2); RDIR(3); RDIR(4); } else _REV_E_DIR(E); }while(0) + #elif E_STEPPERS > 3 + #define DUPE(T,V) do{ _DUPE(0,T,V); _DUPE(1,T,V); _DUPE(2,T,V); _DUPE(3,T,V); }while(0) + #define NORM_E_DIR(E) do{ if (extruder_duplication_enabled) { NDIR(0); NDIR(1); NDIR(2); NDIR(3); } else _NORM_E_DIR(E); }while(0) + #define REV_E_DIR(E) do{ if (extruder_duplication_enabled) { RDIR(0); RDIR(1); RDIR(2); RDIR(3); } else _REV_E_DIR(E); }while(0) + #else + #define DUPE(T,V) do{ _DUPE(0,T,V); _DUPE(1,T,V); _DUPE(2,T,V); }while(0) + #define NORM_E_DIR(E) do{ if (extruder_duplication_enabled) { NDIR(0); NDIR(1); NDIR(2); } else _NORM_E_DIR(E); }while(0) + #define REV_E_DIR(E) do{ if (extruder_duplication_enabled) { RDIR(0); RDIR(1); RDIR(2); } else _REV_E_DIR(E); }while(0) + #endif + #else + #define DUPE(T,V) do{ _DUPE(0,T,V); _DUPE(1,T,V); }while(0) + #define NORM_E_DIR(E) do{ if (extruder_duplication_enabled) { NDIR(0); NDIR(1); } else _NORM_E_DIR(E); }while(0) + #define REV_E_DIR(E) do{ if (extruder_duplication_enabled) { RDIR(0); RDIR(1); } else _REV_E_DIR(E); }while(0) + #endif + + #else + + #define E_STEP_WRITE(E,V) _E_STEP_WRITE(E,V) + #define NORM_E_DIR(E) _NORM_E_DIR(E) + #define REV_E_DIR(E) _REV_E_DIR(E) + + #endif + +#elif E_STEPPERS + #define E_STEP_WRITE(E,V) E0_STEP_WRITE(V) + #define NORM_E_DIR(E) E0_DIR_WRITE(!INVERT_E0_DIR) + #define REV_E_DIR(E) E0_DIR_WRITE( INVERT_E0_DIR) + +#else + #define E_STEP_WRITE(E,V) NOOP + #define NORM_E_DIR(E) NOOP + #define REV_E_DIR(E) NOOP + +#endif + +// +// Individual stepper enable / disable macros +// + +#ifndef ENABLE_STEPPER_X + #if HAS_X_ENABLE + #define ENABLE_STEPPER_X() X_ENABLE_WRITE( X_ENABLE_ON) + #else + #define ENABLE_STEPPER_X() NOOP + #endif +#endif +#ifndef DISABLE_STEPPER_X + #if HAS_X_ENABLE + #define DISABLE_STEPPER_X() X_ENABLE_WRITE(!X_ENABLE_ON) + #else + #define DISABLE_STEPPER_X() NOOP + #endif +#endif + +#ifndef ENABLE_STEPPER_X2 + #if HAS_X2_ENABLE + #define ENABLE_STEPPER_X2() X2_ENABLE_WRITE( X_ENABLE_ON) + #else + #define ENABLE_STEPPER_X2() NOOP + #endif +#endif +#ifndef DISABLE_STEPPER_X2 + #if HAS_X2_ENABLE + #define DISABLE_STEPPER_X2() X2_ENABLE_WRITE(!X_ENABLE_ON) + #else + #define DISABLE_STEPPER_X2() NOOP + #endif +#endif + +#ifndef ENABLE_STEPPER_Y + #if HAS_Y_ENABLE + #define ENABLE_STEPPER_Y() Y_ENABLE_WRITE( Y_ENABLE_ON) + #else + #define ENABLE_STEPPER_Y() NOOP + #endif +#endif +#ifndef DISABLE_STEPPER_Y + #if HAS_Y_ENABLE + #define DISABLE_STEPPER_Y() Y_ENABLE_WRITE(!Y_ENABLE_ON) + #else + #define DISABLE_STEPPER_Y() NOOP + #endif +#endif + +#ifndef ENABLE_STEPPER_Y2 + #if HAS_Y2_ENABLE + #define ENABLE_STEPPER_Y2() Y2_ENABLE_WRITE( Y_ENABLE_ON) + #else + #define ENABLE_STEPPER_Y2() NOOP + #endif +#endif +#ifndef DISABLE_STEPPER_Y2 + #if HAS_Y2_ENABLE + #define DISABLE_STEPPER_Y2() Y2_ENABLE_WRITE(!Y_ENABLE_ON) + #else + #define DISABLE_STEPPER_Y2() NOOP + #endif +#endif + +#ifndef ENABLE_STEPPER_Z + #if HAS_Z_ENABLE + #define ENABLE_STEPPER_Z() Z_ENABLE_WRITE( Z_ENABLE_ON) + #else + #define ENABLE_STEPPER_Z() NOOP + #endif +#endif +#ifndef DISABLE_STEPPER_Z + #if HAS_Z_ENABLE + #define DISABLE_STEPPER_Z() Z_ENABLE_WRITE(!Z_ENABLE_ON) + #else + #define DISABLE_STEPPER_Z() NOOP + #endif +#endif + +#ifndef ENABLE_STEPPER_Z2 + #if HAS_Z2_ENABLE + #define ENABLE_STEPPER_Z2() Z2_ENABLE_WRITE( Z_ENABLE_ON) + #else + #define ENABLE_STEPPER_Z2() NOOP + #endif +#endif +#ifndef DISABLE_STEPPER_Z2 + #if HAS_Z2_ENABLE + #define DISABLE_STEPPER_Z2() Z2_ENABLE_WRITE(!Z_ENABLE_ON) + #else + #define DISABLE_STEPPER_Z2() NOOP + #endif +#endif + +#ifndef ENABLE_STEPPER_Z3 + #if HAS_Z3_ENABLE + #define ENABLE_STEPPER_Z3() Z3_ENABLE_WRITE( Z_ENABLE_ON) + #else + #define ENABLE_STEPPER_Z3() NOOP + #endif +#endif +#ifndef DISABLE_STEPPER_Z3 + #if HAS_Z3_ENABLE + #define DISABLE_STEPPER_Z3() Z3_ENABLE_WRITE(!Z_ENABLE_ON) + #else + #define DISABLE_STEPPER_Z3() NOOP + #endif +#endif + +#ifndef ENABLE_STEPPER_Z4 + #if HAS_Z4_ENABLE + #define ENABLE_STEPPER_Z4() Z4_ENABLE_WRITE( Z_ENABLE_ON) + #else + #define ENABLE_STEPPER_Z4() NOOP + #endif +#endif +#ifndef DISABLE_STEPPER_Z4 + #if HAS_Z4_ENABLE + #define DISABLE_STEPPER_Z4() Z4_ENABLE_WRITE(!Z_ENABLE_ON) + #else + #define DISABLE_STEPPER_Z4() NOOP + #endif +#endif + +#ifndef ENABLE_STEPPER_E0 + #if HAS_E0_ENABLE + #define ENABLE_STEPPER_E0() E0_ENABLE_WRITE( E_ENABLE_ON) + #else + #define ENABLE_STEPPER_E0() NOOP + #endif +#endif +#ifndef DISABLE_STEPPER_E0 + #if HAS_E0_ENABLE + #define DISABLE_STEPPER_E0() E0_ENABLE_WRITE(!E_ENABLE_ON) + #else + #define DISABLE_STEPPER_E0() NOOP + #endif +#endif + +#ifndef ENABLE_STEPPER_E1 + #if E_STEPPERS > 1 && HAS_E1_ENABLE + #define ENABLE_STEPPER_E1() E1_ENABLE_WRITE( E_ENABLE_ON) + #else + #define ENABLE_STEPPER_E1() NOOP + #endif +#endif +#ifndef DISABLE_STEPPER_E1 + #if E_STEPPERS > 1 && HAS_E1_ENABLE + #define DISABLE_STEPPER_E1() E1_ENABLE_WRITE(!E_ENABLE_ON) + #else + #define DISABLE_STEPPER_E1() NOOP + #endif +#endif + +#ifndef ENABLE_STEPPER_E2 + #if E_STEPPERS > 2 && HAS_E2_ENABLE + #define ENABLE_STEPPER_E2() E2_ENABLE_WRITE( E_ENABLE_ON) + #else + #define ENABLE_STEPPER_E2() NOOP + #endif +#endif +#ifndef DISABLE_STEPPER_E2 + #if E_STEPPERS > 2 && HAS_E2_ENABLE + #define DISABLE_STEPPER_E2() E2_ENABLE_WRITE(!E_ENABLE_ON) + #else + #define DISABLE_STEPPER_E2() NOOP + #endif +#endif + +#ifndef ENABLE_STEPPER_E3 + #if E_STEPPERS > 3 && HAS_E3_ENABLE + #define ENABLE_STEPPER_E3() E3_ENABLE_WRITE( E_ENABLE_ON) + #else + #define ENABLE_STEPPER_E3() NOOP + #endif +#endif +#ifndef DISABLE_STEPPER_E3 + #if E_STEPPERS > 3 && HAS_E3_ENABLE + #define DISABLE_STEPPER_E3() E3_ENABLE_WRITE(!E_ENABLE_ON) + #else + #define DISABLE_STEPPER_E3() NOOP + #endif +#endif + +#ifndef ENABLE_STEPPER_E4 + #if E_STEPPERS > 4 && HAS_E4_ENABLE + #define ENABLE_STEPPER_E4() E4_ENABLE_WRITE( E_ENABLE_ON) + #else + #define ENABLE_STEPPER_E4() NOOP + #endif +#endif +#ifndef DISABLE_STEPPER_E4 + #if E_STEPPERS > 4 && HAS_E4_ENABLE + #define DISABLE_STEPPER_E4() E4_ENABLE_WRITE(!E_ENABLE_ON) + #else + #define DISABLE_STEPPER_E4() NOOP + #endif +#endif + +#ifndef ENABLE_STEPPER_E5 + #if E_STEPPERS > 5 && HAS_E5_ENABLE + #define ENABLE_STEPPER_E5() E5_ENABLE_WRITE( E_ENABLE_ON) + #else + #define ENABLE_STEPPER_E5() NOOP + #endif +#endif +#ifndef DISABLE_STEPPER_E5 + #if E_STEPPERS > 5 && HAS_E5_ENABLE + #define DISABLE_STEPPER_E5() E5_ENABLE_WRITE(!E_ENABLE_ON) + #else + #define DISABLE_STEPPER_E5() NOOP + #endif +#endif + +#ifndef ENABLE_STEPPER_E6 + #if E_STEPPERS > 6 && HAS_E6_ENABLE + #define ENABLE_STEPPER_E6() E6_ENABLE_WRITE( E_ENABLE_ON) + #else + #define ENABLE_STEPPER_E6() NOOP + #endif +#endif +#ifndef DISABLE_STEPPER_E6 + #if E_STEPPERS > 6 && HAS_E6_ENABLE + #define DISABLE_STEPPER_E6() E6_ENABLE_WRITE(!E_ENABLE_ON) + #else + #define DISABLE_STEPPER_E6() NOOP + #endif +#endif + +#ifndef ENABLE_STEPPER_E7 + #if E_STEPPERS > 7 && HAS_E7_ENABLE + #define ENABLE_STEPPER_E7() E7_ENABLE_WRITE( E_ENABLE_ON) + #else + #define ENABLE_STEPPER_E7() NOOP + #endif +#endif +#ifndef DISABLE_STEPPER_E7 + #if E_STEPPERS > 7 && HAS_E7_ENABLE + #define DISABLE_STEPPER_E7() E7_ENABLE_WRITE(!E_ENABLE_ON) + #else + #define DISABLE_STEPPER_E7() NOOP + #endif +#endif + +// +// Axis steppers enable / disable macros +// +#if ENABLED(SOFTWARE_DRIVER_ENABLE) + // Avoid expensive calls to enable / disable steppers + extern xyz_bool_t axis_sw_enabled; + #define SHOULD_ENABLE(N) !axis_sw_enabled.N + #define SHOULD_DISABLE(N) axis_sw_enabled.N + #define AFTER_CHANGE(N,TF) axis_sw_enabled.N = TF +#else + #define SHOULD_ENABLE(N) true + #define SHOULD_DISABLE(N) true + #define AFTER_CHANGE(N,TF) NOOP +#endif + +#define ENABLE_AXIS_X() if (SHOULD_ENABLE(x)) { ENABLE_STEPPER_X(); ENABLE_STEPPER_X2(); AFTER_CHANGE(x, true); } +#define DISABLE_AXIS_X() if (SHOULD_DISABLE(x)) { DISABLE_STEPPER_X(); DISABLE_STEPPER_X2(); AFTER_CHANGE(x, false); set_axis_untrusted(X_AXIS); } +#define ENABLE_AXIS_Y() if (SHOULD_ENABLE(y)) { ENABLE_STEPPER_Y(); ENABLE_STEPPER_Y2(); AFTER_CHANGE(y, true); } +#define DISABLE_AXIS_Y() if (SHOULD_DISABLE(y)) { DISABLE_STEPPER_Y(); DISABLE_STEPPER_Y2(); AFTER_CHANGE(y, false); set_axis_untrusted(Y_AXIS); } +#define ENABLE_AXIS_Z() if (SHOULD_ENABLE(z)) { ENABLE_STEPPER_Z(); ENABLE_STEPPER_Z2(); ENABLE_STEPPER_Z3(); ENABLE_STEPPER_Z4(); AFTER_CHANGE(z, true); } +#define DISABLE_AXIS_Z() if (SHOULD_DISABLE(z)) { DISABLE_STEPPER_Z(); DISABLE_STEPPER_Z2(); DISABLE_STEPPER_Z3(); DISABLE_STEPPER_Z4(); AFTER_CHANGE(z, false); set_axis_untrusted(Z_AXIS); Z_RESET(); } + +#ifdef Z_AFTER_DEACTIVATE + #define Z_RESET() do{ current_position.z = Z_AFTER_DEACTIVATE; sync_plan_position(); }while(0) +#else + #define Z_RESET() +#endif + +// +// Extruder steppers enable / disable macros +// + +#if ENABLED(MIXING_EXTRUDER) + /** + * Mixing steppers keep all their enable (and direction) states synchronized + */ + #define _CALL_ENA_E(N) ENABLE_STEPPER_E##N () ; + #define _CALL_DIS_E(N) DISABLE_STEPPER_E##N () ; + #define ENABLE_AXIS_E0() { RREPEAT(MIXING_STEPPERS, _CALL_ENA_E) } + #define DISABLE_AXIS_E0() { RREPEAT(MIXING_STEPPERS, _CALL_DIS_E) } +#endif + +#ifndef ENABLE_AXIS_E0 + #if E_STEPPERS && HAS_E0_ENABLE + #define ENABLE_AXIS_E0() ENABLE_STEPPER_E0() + #else + #define ENABLE_AXIS_E0() NOOP + #endif +#endif +#ifndef DISABLE_AXIS_E0 + #if E_STEPPERS && HAS_E0_ENABLE + #define DISABLE_AXIS_E0() DISABLE_STEPPER_E0() + #else + #define DISABLE_AXIS_E0() NOOP + #endif +#endif + +#ifndef ENABLE_AXIS_E1 + #if E_STEPPERS > 1 && HAS_E1_ENABLE + #define ENABLE_AXIS_E1() ENABLE_STEPPER_E1() + #else + #define ENABLE_AXIS_E1() NOOP + #endif +#endif +#ifndef DISABLE_AXIS_E1 + #if E_STEPPERS > 1 && HAS_E1_ENABLE + #define DISABLE_AXIS_E1() DISABLE_STEPPER_E1() + #else + #define DISABLE_AXIS_E1() NOOP + #endif +#endif + +#ifndef ENABLE_AXIS_E2 + #if E_STEPPERS > 2 && HAS_E2_ENABLE + #define ENABLE_AXIS_E2() ENABLE_STEPPER_E2() + #else + #define ENABLE_AXIS_E2() NOOP + #endif +#endif +#ifndef DISABLE_AXIS_E2 + #if E_STEPPERS > 2 && HAS_E2_ENABLE + #define DISABLE_AXIS_E2() DISABLE_STEPPER_E2() + #else + #define DISABLE_AXIS_E2() NOOP + #endif +#endif + +#ifndef ENABLE_AXIS_E3 + #if E_STEPPERS > 3 && HAS_E3_ENABLE + #define ENABLE_AXIS_E3() ENABLE_STEPPER_E3() + #else + #define ENABLE_AXIS_E3() NOOP + #endif +#endif +#ifndef DISABLE_AXIS_E3 + #if E_STEPPERS > 3 && HAS_E3_ENABLE + #define DISABLE_AXIS_E3() DISABLE_STEPPER_E3() + #else + #define DISABLE_AXIS_E3() NOOP + #endif +#endif + +#ifndef ENABLE_AXIS_E4 + #if E_STEPPERS > 4 && HAS_E4_ENABLE + #define ENABLE_AXIS_E4() ENABLE_STEPPER_E4() + #else + #define ENABLE_AXIS_E4() NOOP + #endif +#endif +#ifndef DISABLE_AXIS_E4 + #if E_STEPPERS > 4 && HAS_E4_ENABLE + #define DISABLE_AXIS_E4() DISABLE_STEPPER_E4() + #else + #define DISABLE_AXIS_E4() NOOP + #endif +#endif + +#ifndef ENABLE_AXIS_E5 + #if E_STEPPERS > 5 && HAS_E5_ENABLE + #define ENABLE_AXIS_E5() ENABLE_STEPPER_E5() + #else + #define ENABLE_AXIS_E5() NOOP + #endif +#endif +#ifndef DISABLE_AXIS_E5 + #if E_STEPPERS > 5 && HAS_E5_ENABLE + #define DISABLE_AXIS_E5() DISABLE_STEPPER_E5() + #else + #define DISABLE_AXIS_E5() NOOP + #endif +#endif + +#ifndef ENABLE_AXIS_E6 + #if E_STEPPERS > 6 && HAS_E6_ENABLE + #define ENABLE_AXIS_E6() ENABLE_STEPPER_E6() + #else + #define ENABLE_AXIS_E6() NOOP + #endif +#endif +#ifndef DISABLE_AXIS_E6 + #if E_STEPPERS > 6 && HAS_E6_ENABLE + #define DISABLE_AXIS_E6() DISABLE_STEPPER_E6() + #else + #define DISABLE_AXIS_E6() NOOP + #endif +#endif + +#ifndef ENABLE_AXIS_E7 + #if E_STEPPERS > 7 && HAS_E7_ENABLE + #define ENABLE_AXIS_E7() ENABLE_STEPPER_E7() + #else + #define ENABLE_AXIS_E7() NOOP + #endif +#endif +#ifndef DISABLE_AXIS_E7 + #if E_STEPPERS > 7 && HAS_E7_ENABLE + #define DISABLE_AXIS_E7() DISABLE_STEPPER_E7() + #else + #define DISABLE_AXIS_E7() NOOP + #endif +#endif diff --git a/Marlin/src/module/stepper/trinamic.cpp b/Marlin/src/module/stepper/trinamic.cpp new file mode 100644 index 0000000..c33581d --- /dev/null +++ b/Marlin/src/module/stepper/trinamic.cpp @@ -0,0 +1,877 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +/** + * stepper/trinamic.cpp + * Stepper driver indirection for Trinamic + */ + +#include "../../inc/MarlinConfig.h" + +#if HAS_TRINAMIC_CONFIG + +#include "trinamic.h" +#include "../stepper.h" + +#include +#include + +enum StealthIndex : uint8_t { STEALTH_AXIS_XY, STEALTH_AXIS_Z, STEALTH_AXIS_E }; +#define TMC_INIT(ST, STEALTH_INDEX) tmc_init(stepper##ST, ST##_CURRENT, ST##_MICROSTEPS, ST##_HYBRID_THRESHOLD, stealthchop_by_axis[STEALTH_INDEX], chopper_timing_##ST, ST##_INTERPOLATE) + +// IC = TMC model number +// ST = Stepper object letter +// L = Label characters +// AI = Axis Enum Index +// SWHW = SW/SH UART selection +#if ENABLED(TMC_USE_SW_SPI) + #define __TMC_SPI_DEFINE(IC, ST, L, AI) TMCMarlin stepper##ST(ST##_CS_PIN, float(ST##_RSENSE), TMC_SW_MOSI, TMC_SW_MISO, TMC_SW_SCK, ST##_CHAIN_POS) +#else + #define __TMC_SPI_DEFINE(IC, ST, L, AI) TMCMarlin stepper##ST(ST##_CS_PIN, float(ST##_RSENSE), ST##_CHAIN_POS) +#endif + +#if ENABLED(TMC_SERIAL_MULTIPLEXER) + #define TMC_UART_HW_DEFINE(IC, ST, L, AI) TMCMarlin stepper##ST(&ST##_HARDWARE_SERIAL, float(ST##_RSENSE), ST##_SLAVE_ADDRESS, SERIAL_MUL_PIN1, SERIAL_MUL_PIN2) +#else + #define TMC_UART_HW_DEFINE(IC, ST, L, AI) TMCMarlin stepper##ST(&ST##_HARDWARE_SERIAL, float(ST##_RSENSE), ST##_SLAVE_ADDRESS) +#endif +#define TMC_UART_SW_DEFINE(IC, ST, L, AI) TMCMarlin stepper##ST(ST##_SERIAL_RX_PIN, ST##_SERIAL_TX_PIN, float(ST##_RSENSE), ST##_SLAVE_ADDRESS) + +#define _TMC_SPI_DEFINE(IC, ST, AI) __TMC_SPI_DEFINE(IC, ST, TMC_##ST##_LABEL, AI) +#define TMC_SPI_DEFINE(ST, AI) _TMC_SPI_DEFINE(ST##_DRIVER_TYPE, ST, AI##_AXIS) + +#define _TMC_UART_DEFINE(SWHW, IC, ST, AI) TMC_UART_##SWHW##_DEFINE(IC, ST, TMC_##ST##_LABEL, AI) +#define TMC_UART_DEFINE(SWHW, ST, AI) _TMC_UART_DEFINE(SWHW, ST##_DRIVER_TYPE, ST, AI##_AXIS) + +#if DISTINCT_E > 1 + #define TMC_SPI_DEFINE_E(AI) TMC_SPI_DEFINE(E##AI, E##AI) + #define TMC_UART_DEFINE_E(SWHW, AI) TMC_UART_DEFINE(SWHW, E##AI, E##AI) +#else + #define TMC_SPI_DEFINE_E(AI) TMC_SPI_DEFINE(E##AI, E) + #define TMC_UART_DEFINE_E(SWHW, AI) TMC_UART_DEFINE(SWHW, E##AI, E) +#endif + +// Stepper objects of TMC2130/TMC2160/TMC2660/TMC5130/TMC5160 steppers used +#if AXIS_HAS_SPI(X) + TMC_SPI_DEFINE(X, X); +#endif +#if AXIS_HAS_SPI(X2) + TMC_SPI_DEFINE(X2, X); +#endif +#if AXIS_HAS_SPI(Y) + TMC_SPI_DEFINE(Y, Y); +#endif +#if AXIS_HAS_SPI(Y2) + TMC_SPI_DEFINE(Y2, Y); +#endif +#if AXIS_HAS_SPI(Z) + TMC_SPI_DEFINE(Z, Z); +#endif +#if AXIS_HAS_SPI(Z2) + TMC_SPI_DEFINE(Z2, Z); +#endif +#if AXIS_HAS_SPI(Z3) + TMC_SPI_DEFINE(Z3, Z); +#endif +#if AXIS_HAS_SPI(Z4) + TMC_SPI_DEFINE(Z4, Z); +#endif +#if AXIS_HAS_SPI(E0) + TMC_SPI_DEFINE_E(0); +#endif +#if AXIS_HAS_SPI(E1) + TMC_SPI_DEFINE_E(1); +#endif +#if AXIS_HAS_SPI(E2) + TMC_SPI_DEFINE_E(2); +#endif +#if AXIS_HAS_SPI(E3) + TMC_SPI_DEFINE_E(3); +#endif +#if AXIS_HAS_SPI(E4) + TMC_SPI_DEFINE_E(4); +#endif +#if AXIS_HAS_SPI(E5) + TMC_SPI_DEFINE_E(5); +#endif +#if AXIS_HAS_SPI(E6) + TMC_SPI_DEFINE_E(6); +#endif +#if AXIS_HAS_SPI(E7) + TMC_SPI_DEFINE_E(7); +#endif + +#ifndef TMC_BAUD_RATE + // Reduce baud rate for boards not already overriding TMC_BAUD_RATE for software serial. + // Testing has shown that 115200 is not 100% reliable on AVR platforms, occasionally + // failing to read status properly. 32-bit platforms typically define an even lower + // TMC_BAUD_RATE, due to differences in how SoftwareSerial libraries work on different + // platforms. + #define TMC_BAUD_RATE TERN(HAS_TMC_SW_SERIAL, 57600, 115200) +#endif + +#if HAS_DRIVER(TMC2130) + template + void tmc_init(TMCMarlin &st, const uint16_t mA, const uint16_t microsteps, const uint32_t hyb_thrs, const bool stealth, const chopper_timing_t &chop_init, const bool interpolate) { + st.begin(); + + CHOPCONF_t chopconf{0}; + chopconf.tbl = 0b01; + chopconf.toff = chop_init.toff; + chopconf.intpol = interpolate; + chopconf.hend = chop_init.hend + 3; + chopconf.hstrt = chop_init.hstrt - 1; + TERN_(SQUARE_WAVE_STEPPING, chopconf.dedge = true); + st.CHOPCONF(chopconf.sr); + + st.rms_current(mA, HOLD_MULTIPLIER); + st.microsteps(microsteps); + st.iholddelay(10); + st.TPOWERDOWN(128); // ~2s until driver lowers to hold current + + st.en_pwm_mode(stealth); + st.stored.stealthChop_enabled = stealth; + + PWMCONF_t pwmconf{0}; + pwmconf.pwm_freq = 0b01; // f_pwm = 2/683 f_clk + pwmconf.pwm_autoscale = true; + pwmconf.pwm_grad = 5; + pwmconf.pwm_ampl = 180; + st.PWMCONF(pwmconf.sr); + + TERN(HYBRID_THRESHOLD, st.set_pwm_thrs(hyb_thrs), UNUSED(hyb_thrs)); + + st.GSTAT(); // Clear GSTAT + } +#endif // TMC2130 + +#if HAS_DRIVER(TMC2160) + template + void tmc_init(TMCMarlin &st, const uint16_t mA, const uint16_t microsteps, const uint32_t hyb_thrs, const bool stealth, const chopper_timing_t &chop_init, const bool interpolate) { + st.begin(); + + CHOPCONF_t chopconf{0}; + chopconf.tbl = 0b01; + chopconf.toff = chop_init.toff; + chopconf.intpol = interpolate; + chopconf.hend = chop_init.hend + 3; + chopconf.hstrt = chop_init.hstrt - 1; + TERN_(SQUARE_WAVE_STEPPING, chopconf.dedge = true); + st.CHOPCONF(chopconf.sr); + + st.rms_current(mA, HOLD_MULTIPLIER); + st.microsteps(microsteps); + st.iholddelay(10); + st.TPOWERDOWN(128); // ~2s until driver lowers to hold current + + st.en_pwm_mode(stealth); + st.stored.stealthChop_enabled = stealth; + + TMC2160_n::PWMCONF_t pwmconf{0}; + pwmconf.pwm_lim = 12; + pwmconf.pwm_reg = 8; + pwmconf.pwm_autograd = true; + pwmconf.pwm_autoscale = true; + pwmconf.pwm_freq = 0b01; + pwmconf.pwm_grad = 14; + pwmconf.pwm_ofs = 36; + st.PWMCONF(pwmconf.sr); + + TERN(HYBRID_THRESHOLD, st.set_pwm_thrs(hyb_thrs), UNUSED(hyb_thrs)); + + st.GSTAT(); // Clear GSTAT + } +#endif // TMC2160 + +// +// TMC2208/2209 Driver objects and inits +// +#if HAS_TMC220x + #if AXIS_HAS_UART(X) + #ifdef X_HARDWARE_SERIAL + TMC_UART_DEFINE(HW, X, X); + #define X_HAS_HW_SERIAL 1 + #else + TMC_UART_DEFINE(SW, X, X); + #define X_HAS_SW_SERIAL 1 + #endif + #endif + #if AXIS_HAS_UART(X2) + #ifdef X2_HARDWARE_SERIAL + TMC_UART_DEFINE(HW, X2, X); + #define X2_HAS_HW_SERIAL 1 + #else + TMC_UART_DEFINE(SW, X2, X); + #define X2_HAS_SW_SERIAL 1 + #endif + #endif + #if AXIS_HAS_UART(Y) + #ifdef Y_HARDWARE_SERIAL + TMC_UART_DEFINE(HW, Y, Y); + #define Y_HAS_HW_SERIAL 1 + #else + TMC_UART_DEFINE(SW, Y, Y); + #define Y_HAS_SW_SERIAL 1 + #endif + #endif + #if AXIS_HAS_UART(Y2) + #ifdef Y2_HARDWARE_SERIAL + TMC_UART_DEFINE(HW, Y2, Y); + #define Y2_HAS_HW_SERIAL 1 + #else + TMC_UART_DEFINE(SW, Y2, Y); + #define Y2_HAS_SW_SERIAL 1 + #endif + #endif + #if AXIS_HAS_UART(Z) + #ifdef Z_HARDWARE_SERIAL + TMC_UART_DEFINE(HW, Z, Z); + #define Z_HAS_HW_SERIAL 1 + #else + TMC_UART_DEFINE(SW, Z, Z); + #define Z_HAS_SW_SERIAL 1 + #endif + #endif + #if AXIS_HAS_UART(Z2) + #ifdef Z2_HARDWARE_SERIAL + TMC_UART_DEFINE(HW, Z2, Z); + #define Z2_HAS_HW_SERIAL 1 + #else + TMC_UART_DEFINE(SW, Z2, Z); + #define Z2_HAS_SW_SERIAL 1 + #endif + #endif + #if AXIS_HAS_UART(Z3) + #ifdef Z3_HARDWARE_SERIAL + TMC_UART_DEFINE(HW, Z3, Z); + #define Z3_HAS_HW_SERIAL 1 + #else + TMC_UART_DEFINE(SW, Z3, Z); + #define Z3_HAS_SW_SERIAL 1 + #endif + #endif + #if AXIS_HAS_UART(Z4) + #ifdef Z4_HARDWARE_SERIAL + TMC_UART_DEFINE(HW, Z4, Z); + #define Z4_HAS_HW_SERIAL 1 + #else + TMC_UART_DEFINE(SW, Z4, Z); + #define Z4_HAS_SW_SERIAL 1 + #endif + #endif + #if AXIS_HAS_UART(E0) + #ifdef E0_HARDWARE_SERIAL + TMC_UART_DEFINE_E(HW, 0); + #define E0_HAS_HW_SERIAL 1 + #else + TMC_UART_DEFINE_E(SW, 0); + #define E0_HAS_SW_SERIAL 1 + #endif + #endif + #if AXIS_HAS_UART(E1) + #ifdef E1_HARDWARE_SERIAL + TMC_UART_DEFINE_E(HW, 1); + #define E1_HAS_HW_SERIAL 1 + #else + TMC_UART_DEFINE_E(SW, 1); + #define E1_HAS_SW_SERIAL 1 + #endif + #endif + #if AXIS_HAS_UART(E2) + #ifdef E2_HARDWARE_SERIAL + TMC_UART_DEFINE_E(HW, 2); + #define E2_HAS_HW_SERIAL 1 + #else + TMC_UART_DEFINE_E(SW, 2); + #define E2_HAS_SW_SERIAL 1 + #endif + #endif + #if AXIS_HAS_UART(E3) + #ifdef E3_HARDWARE_SERIAL + TMC_UART_DEFINE_E(HW, 3); + #define E3_HAS_HW_SERIAL 1 + #else + TMC_UART_DEFINE_E(SW, 3); + #define E3_HAS_SW_SERIAL 1 + #endif + #endif + #if AXIS_HAS_UART(E4) + #ifdef E4_HARDWARE_SERIAL + TMC_UART_DEFINE_E(HW, 4); + #define E4_HAS_HW_SERIAL 1 + #else + TMC_UART_DEFINE_E(SW, 4); + #define E4_HAS_SW_SERIAL 1 + #endif + #endif + #if AXIS_HAS_UART(E5) + #ifdef E5_HARDWARE_SERIAL + TMC_UART_DEFINE_E(HW, 5); + #define E5_HAS_HW_SERIAL 1 + #else + TMC_UART_DEFINE_E(SW, 5); + #define E5_HAS_SW_SERIAL 1 + #endif + #endif + #if AXIS_HAS_UART(E6) + #ifdef E6_HARDWARE_SERIAL + TMC_UART_DEFINE_E(HW, 6); + #define E6_HAS_HW_SERIAL 1 + #else + TMC_UART_DEFINE_E(SW, 6); + #define E6_HAS_SW_SERIAL 1 + #endif + #endif + #if AXIS_HAS_UART(E7) + #ifdef E7_HARDWARE_SERIAL + TMC_UART_DEFINE_E(HW, 7); + #define E7_HAS_HW_SERIAL 1 + #else + TMC_UART_DEFINE_E(SW, 7); + #define E7_HAS_SW_SERIAL 1 + #endif + #endif + + enum TMCAxis : uint8_t { X, Y, Z, X2, Y2, Z2, Z3, Z4, E0, E1, E2, E3, E4, E5, E6, E7, TOTAL }; + + void tmc_serial_begin() { + #if HAS_TMC_HW_SERIAL + struct { + const void *ptr[TMCAxis::TOTAL]; + bool began(const TMCAxis a, const void * const p) { + LOOP_L_N(i, a) if (p == ptr[i]) return true; + ptr[a] = p; return false; + }; + } sp_helper; + + #define HW_SERIAL_BEGIN(A) do{ if (!sp_helper.began(TMCAxis::A, &A##_HARDWARE_SERIAL)) \ + A##_HARDWARE_SERIAL.begin(TMC_BAUD_RATE); }while(0) + #endif + + #if AXIS_HAS_UART(X) + #ifdef X_HARDWARE_SERIAL + HW_SERIAL_BEGIN(X); + #else + stepperX.beginSerial(TMC_BAUD_RATE); + #endif + #endif + #if AXIS_HAS_UART(X2) + #ifdef X2_HARDWARE_SERIAL + HW_SERIAL_BEGIN(X2); + #else + stepperX2.beginSerial(TMC_BAUD_RATE); + #endif + #endif + #if AXIS_HAS_UART(Y) + #ifdef Y_HARDWARE_SERIAL + HW_SERIAL_BEGIN(Y); + #else + stepperY.beginSerial(TMC_BAUD_RATE); + #endif + #endif + #if AXIS_HAS_UART(Y2) + #ifdef Y2_HARDWARE_SERIAL + HW_SERIAL_BEGIN(Y2); + #else + stepperY2.beginSerial(TMC_BAUD_RATE); + #endif + #endif + #if AXIS_HAS_UART(Z) + #ifdef Z_HARDWARE_SERIAL + HW_SERIAL_BEGIN(Z); + #else + stepperZ.beginSerial(TMC_BAUD_RATE); + #endif + #endif + #if AXIS_HAS_UART(Z2) + #ifdef Z2_HARDWARE_SERIAL + HW_SERIAL_BEGIN(Z2); + #else + stepperZ2.beginSerial(TMC_BAUD_RATE); + #endif + #endif + #if AXIS_HAS_UART(Z3) + #ifdef Z3_HARDWARE_SERIAL + HW_SERIAL_BEGIN(Z3); + #else + stepperZ3.beginSerial(TMC_BAUD_RATE); + #endif + #endif + #if AXIS_HAS_UART(Z4) + #ifdef Z4_HARDWARE_SERIAL + HW_SERIAL_BEGIN(Z4); + #else + stepperZ4.beginSerial(TMC_BAUD_RATE); + #endif + #endif + #if AXIS_HAS_UART(E0) + #ifdef E0_HARDWARE_SERIAL + HW_SERIAL_BEGIN(E0); + #else + stepperE0.beginSerial(TMC_BAUD_RATE); + #endif + #endif + #if AXIS_HAS_UART(E1) + #ifdef E1_HARDWARE_SERIAL + HW_SERIAL_BEGIN(E1); + #else + stepperE1.beginSerial(TMC_BAUD_RATE); + #endif + #endif + #if AXIS_HAS_UART(E2) + #ifdef E2_HARDWARE_SERIAL + HW_SERIAL_BEGIN(E2); + #else + stepperE2.beginSerial(TMC_BAUD_RATE); + #endif + #endif + #if AXIS_HAS_UART(E3) + #ifdef E3_HARDWARE_SERIAL + HW_SERIAL_BEGIN(E3); + #else + stepperE3.beginSerial(TMC_BAUD_RATE); + #endif + #endif + #if AXIS_HAS_UART(E4) + #ifdef E4_HARDWARE_SERIAL + HW_SERIAL_BEGIN(E4); + #else + stepperE4.beginSerial(TMC_BAUD_RATE); + #endif + #endif + #if AXIS_HAS_UART(E5) + #ifdef E5_HARDWARE_SERIAL + HW_SERIAL_BEGIN(E5); + #else + stepperE5.beginSerial(TMC_BAUD_RATE); + #endif + #endif + #if AXIS_HAS_UART(E6) + #ifdef E6_HARDWARE_SERIAL + HW_SERIAL_BEGIN(E6); + #else + stepperE6.beginSerial(TMC_BAUD_RATE); + #endif + #endif + #if AXIS_HAS_UART(E7) + #ifdef E7_HARDWARE_SERIAL + HW_SERIAL_BEGIN(E7); + #else + stepperE7.beginSerial(TMC_BAUD_RATE); + #endif + #endif + } +#endif + +#if HAS_DRIVER(TMC2208) + template + void tmc_init(TMCMarlin &st, const uint16_t mA, const uint16_t microsteps, const uint32_t hyb_thrs, const bool stealth, const chopper_timing_t &chop_init, const bool interpolate) { + TMC2208_n::GCONF_t gconf{0}; + gconf.pdn_disable = true; // Use UART + gconf.mstep_reg_select = true; // Select microsteps with UART + gconf.i_scale_analog = false; + gconf.en_spreadcycle = !stealth; + st.GCONF(gconf.sr); + st.stored.stealthChop_enabled = stealth; + + TMC2208_n::CHOPCONF_t chopconf{0}; + chopconf.tbl = 0b01; // blank_time = 24 + chopconf.toff = chop_init.toff; + chopconf.intpol = interpolate; + chopconf.hend = chop_init.hend + 3; + chopconf.hstrt = chop_init.hstrt - 1; + TERN_(SQUARE_WAVE_STEPPING, chopconf.dedge = true); + st.CHOPCONF(chopconf.sr); + + st.rms_current(mA, HOLD_MULTIPLIER); + st.microsteps(microsteps); + st.iholddelay(10); + st.TPOWERDOWN(128); // ~2s until driver lowers to hold current + + TMC2208_n::PWMCONF_t pwmconf{0}; + pwmconf.pwm_lim = 12; + pwmconf.pwm_reg = 8; + pwmconf.pwm_autograd = true; + pwmconf.pwm_autoscale = true; + pwmconf.pwm_freq = 0b01; + pwmconf.pwm_grad = 14; + pwmconf.pwm_ofs = 36; + st.PWMCONF(pwmconf.sr); + + TERN(HYBRID_THRESHOLD, st.set_pwm_thrs(hyb_thrs), UNUSED(hyb_thrs)); + + st.GSTAT(0b111); // Clear + delay(200); + } +#endif // TMC2208 + +#if HAS_DRIVER(TMC2209) + template + void tmc_init(TMCMarlin &st, const uint16_t mA, const uint16_t microsteps, const uint32_t hyb_thrs, const bool stealth, const chopper_timing_t &chop_init, const bool interpolate) { + TMC2208_n::GCONF_t gconf{0}; + gconf.pdn_disable = true; // Use UART + gconf.mstep_reg_select = true; // Select microsteps with UART + gconf.i_scale_analog = false; + gconf.en_spreadcycle = !stealth; + st.GCONF(gconf.sr); + st.stored.stealthChop_enabled = stealth; + + TMC2208_n::CHOPCONF_t chopconf{0}; + chopconf.tbl = 0b01; // blank_time = 24 + chopconf.toff = chop_init.toff; + chopconf.intpol = interpolate; + chopconf.hend = chop_init.hend + 3; + chopconf.hstrt = chop_init.hstrt - 1; + TERN_(SQUARE_WAVE_STEPPING, chopconf.dedge = true); + st.CHOPCONF(chopconf.sr); + + st.rms_current(mA, HOLD_MULTIPLIER); + st.microsteps(microsteps); + st.iholddelay(10); + st.TPOWERDOWN(128); // ~2s until driver lowers to hold current + + TMC2208_n::PWMCONF_t pwmconf{0}; + pwmconf.pwm_lim = 12; + pwmconf.pwm_reg = 8; + pwmconf.pwm_autograd = true; + pwmconf.pwm_autoscale = true; + pwmconf.pwm_freq = 0b01; + pwmconf.pwm_grad = 14; + pwmconf.pwm_ofs = 36; + st.PWMCONF(pwmconf.sr); + + TERN(HYBRID_THRESHOLD, st.set_pwm_thrs(hyb_thrs), UNUSED(hyb_thrs)); + + st.GSTAT(0b111); // Clear + delay(200); + } +#endif // TMC2209 + +#if HAS_DRIVER(TMC2660) + template + void tmc_init(TMCMarlin &st, const uint16_t mA, const uint16_t microsteps, const uint32_t, const bool, const chopper_timing_t &chop_init, const bool interpolate) { + st.begin(); + + TMC2660_n::CHOPCONF_t chopconf{0}; + chopconf.tbl = 0b01; + chopconf.toff = chop_init.toff; + chopconf.hend = chop_init.hend + 3; + chopconf.hstrt = chop_init.hstrt - 1; + st.CHOPCONF(chopconf.sr); + + st.sdoff(0); + st.rms_current(mA); + st.microsteps(microsteps); + TERN_(SQUARE_WAVE_STEPPING, st.dedge(true)); + st.intpol(interpolate); + st.diss2g(true); // Disable short to ground protection. Too many false readings? + TERN_(TMC_DEBUG, st.rdsel(0b01)); + } +#endif // TMC2660 + +#if HAS_DRIVER(TMC5130) + template + void tmc_init(TMCMarlin &st, const uint16_t mA, const uint16_t microsteps, const uint32_t hyb_thrs, const bool stealth, const chopper_timing_t &chop_init, const bool interpolate) { + st.begin(); + + CHOPCONF_t chopconf{0}; + chopconf.tbl = 0b01; + chopconf.toff = chop_init.toff; + chopconf.intpol = interpolate; + chopconf.hend = chop_init.hend + 3; + chopconf.hstrt = chop_init.hstrt - 1; + TERN_(SQUARE_WAVE_STEPPING, chopconf.dedge = true); + st.CHOPCONF(chopconf.sr); + + st.rms_current(mA, HOLD_MULTIPLIER); + st.microsteps(microsteps); + st.iholddelay(10); + st.TPOWERDOWN(128); // ~2s until driver lowers to hold current + + st.en_pwm_mode(stealth); + st.stored.stealthChop_enabled = stealth; + + PWMCONF_t pwmconf{0}; + pwmconf.pwm_freq = 0b01; // f_pwm = 2/683 f_clk + pwmconf.pwm_autoscale = true; + pwmconf.pwm_grad = 5; + pwmconf.pwm_ampl = 180; + st.PWMCONF(pwmconf.sr); + + TERN(HYBRID_THRESHOLD, st.set_pwm_thrs(hyb_thrs), UNUSED(hyb_thrs)); + + st.GSTAT(); // Clear GSTAT + } +#endif // TMC5130 + +#if HAS_DRIVER(TMC5160) + template + void tmc_init(TMCMarlin &st, const uint16_t mA, const uint16_t microsteps, const uint32_t hyb_thrs, const bool stealth, const chopper_timing_t &chop_init, const bool interpolate) { + st.begin(); + + CHOPCONF_t chopconf{0}; + chopconf.tbl = 0b01; + chopconf.toff = chop_init.toff; + chopconf.intpol = interpolate; + chopconf.hend = chop_init.hend + 3; + chopconf.hstrt = chop_init.hstrt - 1; + TERN_(SQUARE_WAVE_STEPPING, chopconf.dedge = true); + st.CHOPCONF(chopconf.sr); + + st.rms_current(mA, HOLD_MULTIPLIER); + st.microsteps(microsteps); + st.iholddelay(10); + st.TPOWERDOWN(128); // ~2s until driver lowers to hold current + + st.en_pwm_mode(stealth); + st.stored.stealthChop_enabled = stealth; + + TMC2160_n::PWMCONF_t pwmconf{0}; + pwmconf.pwm_lim = 12; + pwmconf.pwm_reg = 8; + pwmconf.pwm_autograd = true; + pwmconf.pwm_autoscale = true; + pwmconf.pwm_freq = 0b01; + pwmconf.pwm_grad = 14; + pwmconf.pwm_ofs = 36; + st.PWMCONF(pwmconf.sr); + + #if ENABLED(HYBRID_THRESHOLD) + st.set_pwm_thrs(hyb_thrs); + #else + UNUSED(hyb_thrs); + #endif + st.GSTAT(); // Clear GSTAT + } +#endif // TMC5160 + +void restore_trinamic_drivers() { + #if AXIS_IS_TMC(X) + stepperX.push(); + #endif + #if AXIS_IS_TMC(X2) + stepperX2.push(); + #endif + #if AXIS_IS_TMC(Y) + stepperY.push(); + #endif + #if AXIS_IS_TMC(Y2) + stepperY2.push(); + #endif + #if AXIS_IS_TMC(Z) + stepperZ.push(); + #endif + #if AXIS_IS_TMC(Z2) + stepperZ2.push(); + #endif + #if AXIS_IS_TMC(Z3) + stepperZ3.push(); + #endif + #if AXIS_IS_TMC(Z4) + stepperZ4.push(); + #endif + #if AXIS_IS_TMC(E0) + stepperE0.push(); + #endif + #if AXIS_IS_TMC(E1) + stepperE1.push(); + #endif + #if AXIS_IS_TMC(E2) + stepperE2.push(); + #endif + #if AXIS_IS_TMC(E3) + stepperE3.push(); + #endif + #if AXIS_IS_TMC(E4) + stepperE4.push(); + #endif + #if AXIS_IS_TMC(E5) + stepperE5.push(); + #endif + #if AXIS_IS_TMC(E6) + stepperE6.push(); + #endif + #if AXIS_IS_TMC(E7) + stepperE7.push(); + #endif +} + +void reset_trinamic_drivers() { + static constexpr bool stealthchop_by_axis[] = { ENABLED(STEALTHCHOP_XY), ENABLED(STEALTHCHOP_Z), ENABLED(STEALTHCHOP_E) }; + + #if AXIS_IS_TMC(X) + TMC_INIT(X, STEALTH_AXIS_XY); + #endif + #if AXIS_IS_TMC(X2) + TMC_INIT(X2, STEALTH_AXIS_XY); + #endif + #if AXIS_IS_TMC(Y) + TMC_INIT(Y, STEALTH_AXIS_XY); + #endif + #if AXIS_IS_TMC(Y2) + TMC_INIT(Y2, STEALTH_AXIS_XY); + #endif + #if AXIS_IS_TMC(Z) + TMC_INIT(Z, STEALTH_AXIS_Z); + #endif + #if AXIS_IS_TMC(Z2) + TMC_INIT(Z2, STEALTH_AXIS_Z); + #endif + #if AXIS_IS_TMC(Z3) + TMC_INIT(Z3, STEALTH_AXIS_Z); + #endif + #if AXIS_IS_TMC(Z4) + TMC_INIT(Z4, STEALTH_AXIS_Z); + #endif + #if AXIS_IS_TMC(E0) + TMC_INIT(E0, STEALTH_AXIS_E); + #endif + #if AXIS_IS_TMC(E1) + TMC_INIT(E1, STEALTH_AXIS_E); + #endif + #if AXIS_IS_TMC(E2) + TMC_INIT(E2, STEALTH_AXIS_E); + #endif + #if AXIS_IS_TMC(E3) + TMC_INIT(E3, STEALTH_AXIS_E); + #endif + #if AXIS_IS_TMC(E4) + TMC_INIT(E4, STEALTH_AXIS_E); + #endif + #if AXIS_IS_TMC(E5) + TMC_INIT(E5, STEALTH_AXIS_E); + #endif + #if AXIS_IS_TMC(E6) + TMC_INIT(E6, STEALTH_AXIS_E); + #endif + #if AXIS_IS_TMC(E7) + TMC_INIT(E7, STEALTH_AXIS_E); + #endif + + #if USE_SENSORLESS + #if X_SENSORLESS + stepperX.homing_threshold(X_STALL_SENSITIVITY); + #if AXIS_HAS_STALLGUARD(X2) + stepperX2.homing_threshold(CAT(TERN(X2_SENSORLESS, X2, X), _STALL_SENSITIVITY)); + #endif + #endif + #if Y_SENSORLESS + stepperY.homing_threshold(Y_STALL_SENSITIVITY); + #if AXIS_HAS_STALLGUARD(Y2) + stepperY2.homing_threshold(CAT(TERN(Y2_SENSORLESS, Y2, Y), _STALL_SENSITIVITY)); + #endif + #endif + #if Z_SENSORLESS + stepperZ.homing_threshold(Z_STALL_SENSITIVITY); + #if AXIS_HAS_STALLGUARD(Z2) + stepperZ2.homing_threshold(CAT(TERN(Z2_SENSORLESS, Z2, Z), _STALL_SENSITIVITY)); + #endif + #if AXIS_HAS_STALLGUARD(Z3) + stepperZ3.homing_threshold(CAT(TERN(Z3_SENSORLESS, Z3, Z), _STALL_SENSITIVITY)); + #endif + #if AXIS_HAS_STALLGUARD(Z4) + stepperZ4.homing_threshold(CAT(TERN(Z4_SENSORLESS, Z4, Z), _STALL_SENSITIVITY)); + #endif + #endif + #endif + + #ifdef TMC_ADV + TMC_ADV() + #endif + + stepper.set_directions(); +} + +// TMC Slave Address Conflict Detection +// +// Conflict detection is performed in the following way. Similar methods are used for +// hardware and software serial, but the implementations are indepenent. +// +// 1. Populate a data structure with UART parameters and addresses for all possible axis. +// If an axis is not in use, populate it with recognizable placeholder data. +// 2. For each axis in use, static_assert using a constexpr function, which counts the +// number of matching/conflicting axis. If the value is not exactly 1, fail. + +#if ANY_AXIS_HAS(HW_SERIAL) + // Hardware serial names are compared as strings, since actually resolving them cannot occur in a constexpr. + // Using a fixed-length character array for the port name allows this to be constexpr compatible. + struct SanityHwSerialDetails { const char port[20]; uint32_t address; }; + #define TMC_HW_DETAIL_ARGS(A) TERN(A##_HAS_HW_SERIAL, STRINGIFY(A##_HARDWARE_SERIAL), ""), TERN0(A##_HAS_HW_SERIAL, A##_SLAVE_ADDRESS) + #define TMC_HW_DETAIL(A) {TMC_HW_DETAIL_ARGS(A)} + constexpr SanityHwSerialDetails sanity_tmc_hw_details[] = { + TMC_HW_DETAIL(X), TMC_HW_DETAIL(X2), + TMC_HW_DETAIL(Y), TMC_HW_DETAIL(Y2), + TMC_HW_DETAIL(Z), TMC_HW_DETAIL(Z2), TMC_HW_DETAIL(Z3), TMC_HW_DETAIL(Z4), + TMC_HW_DETAIL(E0), TMC_HW_DETAIL(E1), TMC_HW_DETAIL(E2), TMC_HW_DETAIL(E3), TMC_HW_DETAIL(E4), TMC_HW_DETAIL(E5), TMC_HW_DETAIL(E6), TMC_HW_DETAIL(E7) + }; + + // constexpr compatible string comparison + constexpr bool str_eq_ce(const char * a, const char * b) { + return *a == *b && (*a == '\0' || str_eq_ce(a+1,b+1)); + } + + constexpr bool sc_hw_done(size_t start, size_t end) { return start == end; } + constexpr bool sc_hw_skip(const char* port_name) { return !(*port_name); } + constexpr bool sc_hw_match(const char* port_name, uint32_t address, size_t start, size_t end) { + return !sc_hw_done(start, end) && !sc_hw_skip(port_name) && (address == sanity_tmc_hw_details[start].address && str_eq_ce(port_name, sanity_tmc_hw_details[start].port)); + } + constexpr int count_tmc_hw_serial_matches(const char* port_name, uint32_t address, size_t start, size_t end) { + return sc_hw_done(start, end) ? 0 : ((sc_hw_skip(port_name) ? 0 : (sc_hw_match(port_name, address, start, end) ? 1 : 0)) + count_tmc_hw_serial_matches(port_name, address, start + 1, end)); + } + + #define TMC_HWSERIAL_CONFLICT_MSG(A) STRINGIFY(A) "_SLAVE_ADDRESS conflicts with another driver using the same " STRINGIFY(A) "_HARDWARE_SERIAL" + #define SA_NO_TMC_HW_C(A) static_assert(1 >= count_tmc_hw_serial_matches(TMC_HW_DETAIL_ARGS(A), 0, COUNT(sanity_tmc_hw_details)), TMC_HWSERIAL_CONFLICT_MSG(A)); + SA_NO_TMC_HW_C(X);SA_NO_TMC_HW_C(X2); + SA_NO_TMC_HW_C(Y);SA_NO_TMC_HW_C(Y2); + SA_NO_TMC_HW_C(Z);SA_NO_TMC_HW_C(Z2);SA_NO_TMC_HW_C(Z3);SA_NO_TMC_HW_C(Z4); + SA_NO_TMC_HW_C(E0);SA_NO_TMC_HW_C(E1);SA_NO_TMC_HW_C(E2);SA_NO_TMC_HW_C(E3);SA_NO_TMC_HW_C(E4);SA_NO_TMC_HW_C(E5);SA_NO_TMC_HW_C(E6);SA_NO_TMC_HW_C(E7); +#endif + +#if ANY_AXIS_HAS(SW_SERIAL) + struct SanitySwSerialDetails { int32_t txpin; int32_t rxpin; uint32_t address; }; + #define TMC_SW_DETAIL_ARGS(A) TERN(A##_HAS_SW_SERIAL, A##_SERIAL_TX_PIN, -1), TERN(A##_HAS_SW_SERIAL, A##_SERIAL_RX_PIN, -1), TERN0(A##_HAS_SW_SERIAL, A##_SLAVE_ADDRESS) + #define TMC_SW_DETAIL(A) TMC_SW_DETAIL_ARGS(A) + constexpr SanitySwSerialDetails sanity_tmc_sw_details[] = { + TMC_SW_DETAIL(X), TMC_SW_DETAIL(X2), + TMC_SW_DETAIL(Y), TMC_SW_DETAIL(Y2), + TMC_SW_DETAIL(Z), TMC_SW_DETAIL(Z2), TMC_SW_DETAIL(Z3), TMC_SW_DETAIL(Z4), + TMC_SW_DETAIL(E0), TMC_SW_DETAIL(E1), TMC_SW_DETAIL(E2), TMC_SW_DETAIL(E3), TMC_SW_DETAIL(E4), TMC_SW_DETAIL(E5), TMC_SW_DETAIL(E6), TMC_SW_DETAIL(E7) + }; + + constexpr bool sc_sw_done(size_t start, size_t end) { return start == end; } + constexpr bool sc_sw_skip(int32_t txpin) { return txpin < 0; } + constexpr bool sc_sw_match(int32_t txpin, int32_t rxpin, uint32_t address, size_t start, size_t end) { + return !sc_sw_done(start, end) && !sc_sw_skip(txpin) && (txpin == sanity_tmc_sw_details[start].txpin || rxpin == sanity_tmc_sw_details[start].rxpin) && (address == sanity_tmc_sw_details[start].address); + } + constexpr int count_tmc_sw_serial_matches(int32_t txpin, int32_t rxpin, uint32_t address, size_t start, size_t end) { + return sc_sw_done(start, end) ? 0 : ((sc_sw_skip(txpin) ? 0 : (sc_sw_match(txpin, rxpin, address, start, end) ? 1 : 0)) + count_tmc_sw_serial_matches(txpin, rxpin, address, start + 1, end)); + } + + #define TMC_SWSERIAL_CONFLICT_MSG(A) STRINGIFY(A) "_SLAVE_ADDRESS conflicts with another driver using the same " STRINGIFY(A) "_SERIAL_RX_PIN or " STRINGIFY(A) "_SERIAL_TX_PIN" + #define SA_NO_TMC_SW_C(A) static_assert(1 >= count_tmc_sw_serial_matches(TMC_SW_DETAIL_ARGS(A), 0, COUNT(sanity_tmc_sw_details)), TMC_SWSERIAL_CONFLICT_MSG(A)); + SA_NO_TMC_SW_C(X);SA_NO_TMC_SW_C(X2); + SA_NO_TMC_SW_C(Y);SA_NO_TMC_SW_C(Y2); + SA_NO_TMC_SW_C(Z);SA_NO_TMC_SW_C(Z2);SA_NO_TMC_SW_C(Z3);SA_NO_TMC_SW_C(Z4); + SA_NO_TMC_SW_C(E0);SA_NO_TMC_SW_C(E1);SA_NO_TMC_SW_C(E2);SA_NO_TMC_SW_C(E3);SA_NO_TMC_SW_C(E4);SA_NO_TMC_SW_C(E5);SA_NO_TMC_SW_C(E6);SA_NO_TMC_SW_C(E7); +#endif + +#endif // HAS_TRINAMIC_CONFIG diff --git a/Marlin/src/module/stepper/trinamic.h b/Marlin/src/module/stepper/trinamic.h new file mode 100644 index 0000000..9f7445e --- /dev/null +++ b/Marlin/src/module/stepper/trinamic.h @@ -0,0 +1,362 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +#pragma once + +/** + * stepper/trinamic.h + * Stepper driver indirection for Trinamic + */ + +#include +#if TMCSTEPPER_VERSION < 0x000500 + #error "Update TMCStepper library to 0.5.0 or newer." +#endif + +#include "../../inc/MarlinConfig.h" +#include "../../feature/tmc_util.h" + +#define CLASS_TMC2130 TMC2130Stepper +#define CLASS_TMC2160 TMC2160Stepper +#define CLASS_TMC2208 TMC2208Stepper +#define CLASS_TMC2209 TMC2209Stepper +#define CLASS_TMC2660 TMC2660Stepper +#define CLASS_TMC5130 TMC5130Stepper +#define CLASS_TMC5160 TMC5160Stepper + +#define TMC_X_LABEL 'X', '0' +#define TMC_Y_LABEL 'Y', '0' +#define TMC_Z_LABEL 'Z', '0' + +#define TMC_X2_LABEL 'X', '2' +#define TMC_Y2_LABEL 'Y', '2' +#define TMC_Z2_LABEL 'Z', '2' +#define TMC_Z3_LABEL 'Z', '3' +#define TMC_Z4_LABEL 'Z', '4' + +#define TMC_E0_LABEL 'E', '0' +#define TMC_E1_LABEL 'E', '1' +#define TMC_E2_LABEL 'E', '2' +#define TMC_E3_LABEL 'E', '3' +#define TMC_E4_LABEL 'E', '4' +#define TMC_E5_LABEL 'E', '5' +#define TMC_E6_LABEL 'E', '6' +#define TMC_E7_LABEL 'E', '7' + +#define __TMC_CLASS(TYPE, L, I, A) TMCMarlin +#define _TMC_CLASS(TYPE, LandI, A) __TMC_CLASS(TYPE, LandI, A) +#define TMC_CLASS(ST, A) _TMC_CLASS(ST##_DRIVER_TYPE, TMC_##ST##_LABEL, A##_AXIS) +#if ENABLED(DISTINCT_E_FACTORS) + #define TMC_CLASS_E(N) TMC_CLASS(E##N, E##N) +#else + #define TMC_CLASS_E(N) TMC_CLASS(E##N, E) +#endif + +typedef struct { + uint8_t toff; + int8_t hend; + uint8_t hstrt; +} chopper_timing_t; + +#ifndef CHOPPER_TIMING_X + #define CHOPPER_TIMING_X CHOPPER_TIMING +#endif +#ifndef CHOPPER_TIMING_Y + #define CHOPPER_TIMING_Y CHOPPER_TIMING +#endif +#ifndef CHOPPER_TIMING_Z + #define CHOPPER_TIMING_Z CHOPPER_TIMING +#endif +#ifndef CHOPPER_TIMING_E + #define CHOPPER_TIMING_E CHOPPER_TIMING +#endif + +#if HAS_TMC220x + void tmc_serial_begin(); +#endif + +void restore_trinamic_drivers(); +void reset_trinamic_drivers(); + +#define AXIS_HAS_SQUARE_WAVE(A) (AXIS_IS_TMC(A) && ENABLED(SQUARE_WAVE_STEPPING)) + +// X Stepper +#if AXIS_IS_TMC(X) + extern TMC_CLASS(X, X) stepperX; + static constexpr chopper_timing_t chopper_timing_X = CHOPPER_TIMING_X; + #if ENABLED(SOFTWARE_DRIVER_ENABLE) + #define X_ENABLE_INIT() NOOP + #define X_ENABLE_WRITE(STATE) stepperX.toff((STATE)==X_ENABLE_ON ? chopper_timing_X.toff : 0) + #define X_ENABLE_READ() stepperX.isEnabled() + #endif + #if AXIS_HAS_SQUARE_WAVE(X) + #define X_STEP_WRITE(STATE) do{ if(STATE) TOGGLE(X_STEP_PIN); }while(0) + #endif +#endif + +// Y Stepper +#if AXIS_IS_TMC(Y) + extern TMC_CLASS(Y, Y) stepperY; + static constexpr chopper_timing_t chopper_timing_Y = CHOPPER_TIMING_Y; + #if ENABLED(SOFTWARE_DRIVER_ENABLE) + #define Y_ENABLE_INIT() NOOP + #define Y_ENABLE_WRITE(STATE) stepperY.toff((STATE)==Y_ENABLE_ON ? chopper_timing_Y.toff : 0) + #define Y_ENABLE_READ() stepperY.isEnabled() + #endif + #if AXIS_HAS_SQUARE_WAVE(Y) + #define Y_STEP_WRITE(STATE) do{ if (STATE) TOGGLE(Y_STEP_PIN); }while(0) + #endif +#endif + +// Z Stepper +#if AXIS_IS_TMC(Z) + extern TMC_CLASS(Z, Z) stepperZ; + static constexpr chopper_timing_t chopper_timing_Z = CHOPPER_TIMING_Z; + #if ENABLED(SOFTWARE_DRIVER_ENABLE) + #define Z_ENABLE_INIT() NOOP + #define Z_ENABLE_WRITE(STATE) stepperZ.toff((STATE)==Z_ENABLE_ON ? chopper_timing_Z.toff : 0) + #define Z_ENABLE_READ() stepperZ.isEnabled() + #endif + #if AXIS_HAS_SQUARE_WAVE(Z) + #define Z_STEP_WRITE(STATE) do{ if(STATE) TOGGLE(Z_STEP_PIN); }while(0) + #endif +#endif + +// X2 Stepper +#if HAS_X2_ENABLE && AXIS_IS_TMC(X2) + extern TMC_CLASS(X2, X) stepperX2; + #ifndef CHOPPER_TIMING_X2 + #define CHOPPER_TIMING_X2 CHOPPER_TIMING_X + #endif + static constexpr chopper_timing_t chopper_timing_X2 = CHOPPER_TIMING_X2; + #if ENABLED(SOFTWARE_DRIVER_ENABLE) + #define X2_ENABLE_INIT() NOOP + #define X2_ENABLE_WRITE(STATE) stepperX2.toff((STATE)==X_ENABLE_ON ? chopper_timing_X2.toff : 0) + #define X2_ENABLE_READ() stepperX2.isEnabled() + #endif + #if AXIS_HAS_SQUARE_WAVE(X2) + #define X2_STEP_WRITE(STATE) do{ if(STATE) TOGGLE(X2_STEP_PIN); }while(0) + #endif +#endif + +// Y2 Stepper +#if HAS_Y2_ENABLE && AXIS_IS_TMC(Y2) + extern TMC_CLASS(Y2, Y) stepperY2; + #ifndef CHOPPER_TIMING_Y2 + #define CHOPPER_TIMING_Y2 CHOPPER_TIMING_Y + #endif + static constexpr chopper_timing_t chopper_timing_Y2 = CHOPPER_TIMING_Y2; + #if ENABLED(SOFTWARE_DRIVER_ENABLE) + #define Y2_ENABLE_INIT() NOOP + #define Y2_ENABLE_WRITE(STATE) stepperY2.toff((STATE)==Y_ENABLE_ON ? chopper_timing_Y2.toff : 0) + #define Y2_ENABLE_READ() stepperY2.isEnabled() + #endif + #if AXIS_HAS_SQUARE_WAVE(Y2) + #define Y2_STEP_WRITE(STATE) do{ if(STATE) TOGGLE(Y2_STEP_PIN); }while(0) + #endif +#endif + +// Z2 Stepper +#if HAS_Z2_ENABLE && AXIS_IS_TMC(Z2) + extern TMC_CLASS(Z2, Z) stepperZ2; + #ifndef CHOPPER_TIMING_Z2 + #define CHOPPER_TIMING_Z2 CHOPPER_TIMING_Z + #endif + static constexpr chopper_timing_t chopper_timing_Z2 = CHOPPER_TIMING_Z2; + #if ENABLED(SOFTWARE_DRIVER_ENABLE) && AXIS_IS_TMC(Z2) + #define Z2_ENABLE_INIT() NOOP + #define Z2_ENABLE_WRITE(STATE) stepperZ2.toff((STATE)==Z_ENABLE_ON ? chopper_timing_Z2.toff : 0) + #define Z2_ENABLE_READ() stepperZ2.isEnabled() + #endif + #if AXIS_HAS_SQUARE_WAVE(Z2) + #define Z2_STEP_WRITE(STATE) do{ if(STATE) TOGGLE(Z2_STEP_PIN); }while(0) + #endif +#endif + +// Z3 Stepper +#if HAS_Z3_ENABLE && AXIS_IS_TMC(Z3) + extern TMC_CLASS(Z3, Z) stepperZ3; + #ifndef CHOPPER_TIMING_Z3 + #define CHOPPER_TIMING_Z3 CHOPPER_TIMING_Z + #endif + static constexpr chopper_timing_t chopper_timing_Z3 = CHOPPER_TIMING_Z3; + #if ENABLED(SOFTWARE_DRIVER_ENABLE) + #define Z3_ENABLE_INIT() NOOP + #define Z3_ENABLE_WRITE(STATE) stepperZ3.toff((STATE)==Z_ENABLE_ON ? chopper_timing_Z3.toff : 0) + #define Z3_ENABLE_READ() stepperZ3.isEnabled() + #endif + #if AXIS_HAS_SQUARE_WAVE(Z3) + #define Z3_STEP_WRITE(STATE) do{ if(STATE) TOGGLE(Z3_STEP_PIN); }while(0) + #endif +#endif + +// Z4 Stepper +#if HAS_Z4_ENABLE && AXIS_IS_TMC(Z4) + extern TMC_CLASS(Z4, Z) stepperZ4; + #ifndef CHOPPER_TIMING_Z4 + #define CHOPPER_TIMING_Z4 CHOPPER_TIMING_Z + #endif + static constexpr chopper_timing_t chopper_timing_Z4 = CHOPPER_TIMING_Z4; + #if ENABLED(SOFTWARE_DRIVER_ENABLE) + #define Z4_ENABLE_INIT() NOOP + #define Z4_ENABLE_WRITE(STATE) stepperZ4.toff((STATE)==Z_ENABLE_ON ? chopper_timing_Z4.toff : 0) + #define Z4_ENABLE_READ() stepperZ4.isEnabled() + #endif + #if AXIS_HAS_SQUARE_WAVE(Z4) + #define Z4_STEP_WRITE(STATE) do{ if(STATE) TOGGLE(Z4_STEP_PIN); }while(0) + #endif +#endif + +// E0 Stepper +#if AXIS_IS_TMC(E0) + extern TMC_CLASS_E(0) stepperE0; + #ifndef CHOPPER_TIMING_E0 + #define CHOPPER_TIMING_E0 CHOPPER_TIMING_E + #endif + static constexpr chopper_timing_t chopper_timing_E0 = CHOPPER_TIMING_E0; + #if ENABLED(SOFTWARE_DRIVER_ENABLE) && AXIS_IS_TMC(E0) + #define E0_ENABLE_INIT() NOOP + #define E0_ENABLE_WRITE(STATE) stepperE0.toff((STATE)==E_ENABLE_ON ? chopper_timing_E0.toff : 0) + #define E0_ENABLE_READ() stepperE0.isEnabled() + #endif + #if AXIS_HAS_SQUARE_WAVE(E0) + #define E0_STEP_WRITE(STATE) do{ if(STATE) TOGGLE(E0_STEP_PIN); }while(0) + #endif +#endif + +// E1 Stepper +#if AXIS_IS_TMC(E1) + extern TMC_CLASS_E(1) stepperE1; + #ifndef CHOPPER_TIMING_E1 + #define CHOPPER_TIMING_E1 CHOPPER_TIMING_E + #endif + static constexpr chopper_timing_t chopper_timing_E1 = CHOPPER_TIMING_E1; + #if ENABLED(SOFTWARE_DRIVER_ENABLE) && AXIS_IS_TMC(E1) + #define E1_ENABLE_INIT() NOOP + #define E1_ENABLE_WRITE(STATE) stepperE1.toff((STATE)==E_ENABLE_ON ? chopper_timing_E1.toff : 0) + #define E1_ENABLE_READ() stepperE1.isEnabled() + #endif + #if AXIS_HAS_SQUARE_WAVE(E1) + #define E1_STEP_WRITE(STATE) do{ if(STATE) TOGGLE(E1_STEP_PIN); }while(0) + #endif +#endif + +// E2 Stepper +#if AXIS_IS_TMC(E2) + extern TMC_CLASS_E(2) stepperE2; + #ifndef CHOPPER_TIMING_E2 + #define CHOPPER_TIMING_E2 CHOPPER_TIMING_E + #endif + static constexpr chopper_timing_t chopper_timing_E2 = CHOPPER_TIMING_E2; + #if ENABLED(SOFTWARE_DRIVER_ENABLE) && AXIS_IS_TMC(E2) + #define E2_ENABLE_INIT() NOOP + #define E2_ENABLE_WRITE(STATE) stepperE2.toff((STATE)==E_ENABLE_ON ? chopper_timing_E2.toff : 0) + #define E2_ENABLE_READ() stepperE2.isEnabled() + #endif + #if AXIS_HAS_SQUARE_WAVE(E2) + #define E2_STEP_WRITE(STATE) do{ if(STATE) TOGGLE(E2_STEP_PIN); }while(0) + #endif +#endif + +// E3 Stepper +#if AXIS_IS_TMC(E3) + extern TMC_CLASS_E(3) stepperE3; + #ifndef CHOPPER_TIMING_E3 + #define CHOPPER_TIMING_E3 CHOPPER_TIMING_E + #endif + static constexpr chopper_timing_t chopper_timing_E3 = CHOPPER_TIMING_E3; + #if ENABLED(SOFTWARE_DRIVER_ENABLE) && AXIS_IS_TMC(E3) + #define E3_ENABLE_INIT() NOOP + #define E3_ENABLE_WRITE(STATE) stepperE3.toff((STATE)==E_ENABLE_ON ? chopper_timing_E3.toff : 0) + #define E3_ENABLE_READ() stepperE3.isEnabled() + #endif + #if AXIS_HAS_SQUARE_WAVE(E3) + #define E3_STEP_WRITE(STATE) do{ if(STATE) TOGGLE(E3_STEP_PIN); }while(0) + #endif +#endif + +// E4 Stepper +#if AXIS_IS_TMC(E4) + extern TMC_CLASS_E(4) stepperE4; + #ifndef CHOPPER_TIMING_E4 + #define CHOPPER_TIMING_E4 CHOPPER_TIMING_E + #endif + static constexpr chopper_timing_t chopper_timing_E4 = CHOPPER_TIMING_E4; + #if ENABLED(SOFTWARE_DRIVER_ENABLE) && AXIS_IS_TMC(E4) + #define E4_ENABLE_INIT() NOOP + #define E4_ENABLE_WRITE(STATE) stepperE4.toff((STATE)==E_ENABLE_ON ? chopper_timing_E4.toff : 0) + #define E4_ENABLE_READ() stepperE4.isEnabled() + #endif + #if AXIS_HAS_SQUARE_WAVE(E4) + #define E4_STEP_WRITE(STATE) do{ if(STATE) TOGGLE(E4_STEP_PIN); }while(0) + #endif +#endif + +// E5 Stepper +#if AXIS_IS_TMC(E5) + extern TMC_CLASS_E(5) stepperE5; + #ifndef CHOPPER_TIMING_E5 + #define CHOPPER_TIMING_E5 CHOPPER_TIMING_E + #endif + static constexpr chopper_timing_t chopper_timing_E5 = CHOPPER_TIMING_E5; + #if ENABLED(SOFTWARE_DRIVER_ENABLE) && AXIS_IS_TMC(E5) + #define E5_ENABLE_INIT() NOOP + #define E5_ENABLE_WRITE(STATE) stepperE5.toff((STATE)==E_ENABLE_ON ? chopper_timing_E5.toff : 0) + #define E5_ENABLE_READ() stepperE5.isEnabled() + #endif + #if AXIS_HAS_SQUARE_WAVE(E5) + #define E5_STEP_WRITE(STATE) do{ if(STATE) TOGGLE(E5_STEP_PIN); }while(0) + #endif +#endif + +// E6 Stepper +#if AXIS_IS_TMC(E6) + extern TMC_CLASS_E(6) stepperE6; + #ifndef CHOPPER_TIMING_E6 + #define CHOPPER_TIMING_E6 CHOPPER_TIMING_E + #endif + static constexpr chopper_timing_t chopper_timing_E6 = CHOPPER_TIMING_E6; + #if ENABLED(SOFTWARE_DRIVER_ENABLE) && AXIS_IS_TMC(E6) + #define E6_ENABLE_INIT() NOOP + #define E6_ENABLE_WRITE(STATE) stepperE6.toff((STATE)==E_ENABLE_ON ? chopper_timing_E6.toff : 0) + #define E6_ENABLE_READ() stepperE6.isEnabled() + #endif + #if AXIS_HAS_SQUARE_WAVE(E6) + #define E6_STEP_WRITE(STATE) do{ if(STATE) TOGGLE(E6_STEP_PIN); }while(0) + #endif +#endif + +// E7 Stepper +#if AXIS_IS_TMC(E7) + extern TMC_CLASS_E(7) stepperE7; + #ifndef CHOPPER_TIMING_E7 + #define CHOPPER_TIMING_E7 CHOPPER_TIMING_E + #endif + static constexpr chopper_timing_t chopper_timing_E7 = CHOPPER_TIMING_E7; + #if ENABLED(SOFTWARE_DRIVER_ENABLE) && AXIS_IS_TMC(E7) + #define E7_ENABLE_INIT() NOOP + #define E7_ENABLE_WRITE(STATE) stepperE7.toff((STATE)==E_ENABLE_ON ? chopper_timing_E7.toff : 0) + #define E7_ENABLE_READ() stepperE7.isEnabled() + #endif + #if AXIS_HAS_SQUARE_WAVE(E7) + #define E7_STEP_WRITE(STATE) do{ if(STATE) TOGGLE(E7_STEP_PIN); }while(0) + #endif +#endif -- cgit v1.2.3