Skip to content

Commit

Permalink
BTT SKR V2.0 / Stepper Driver Anti-Reverse Protection (#21503)
Browse files Browse the repository at this point in the history
  • Loading branch information
thisiskeithb authored and thinkyhead committed Apr 29, 2021
1 parent 8a8aeba commit 4da3f45
Show file tree
Hide file tree
Showing 12 changed files with 769 additions and 18 deletions.
9 changes: 9 additions & 0 deletions Marlin/Configuration_adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -3387,6 +3387,15 @@
#define POWER_MONITOR_VOLTAGE_OFFSET 0 // Offset (in volts) applied to the calculated voltage
#endif

/**
* Stepper Driver Anti-SNAFU Protection
*
* If the SAFE_POWER_PIN is defined for your board, Marlin will check
* that stepper drivers are properly plugged in before applying power.
* Disable protection if your stepper drivers don't support the feature.
*/
//#define DISABLE_DRIVER_SAFE_POWER_PROTECT

/**
* CNC Coordinate Systems
*
Expand Down
17 changes: 17 additions & 0 deletions Marlin/src/MarlinCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@
#include "lcd/extui/lib/dgus/DGUSScreenHandler.h"
#endif

#if HAS_DRIVER_SAFE_POWER_PROTECT
#include "feature/stepper_driver_safety.h"
#endif

PGMSTR(M112_KILL_STR, "M112 Shutdown");

MarlinState marlin_state = MF_INITIALIZING;
Expand Down Expand Up @@ -1223,6 +1227,15 @@ void setup() {
SETUP_RUN(ui.reset_status()); // Load welcome message early. (Retained if no errors exist.)
#endif

#if PIN_EXISTS(SAFE_POWER)
#if HAS_DRIVER_SAFE_POWER_PROTECT
SETUP_RUN(stepper_driver_backward_check());
#else
SETUP_LOG("SAFE_POWER");
OUT_WRITE(SAFE_POWER_PIN, HIGH);
#endif
#endif

#if ENABLED(PROBE_TARE)
SETUP_RUN(probe.tare_init());
#endif
Expand Down Expand Up @@ -1467,6 +1480,10 @@ void setup() {
SETUP_RUN(test_tmc_connection(true, true, true, true));
#endif

#if HAS_DRIVER_SAFE_POWER_PROTECT
SETUP_RUN(stepper_driver_backward_report());
#endif

#if HAS_PRUSA_MMU2
SETUP_RUN(mmu2.init());
#endif
Expand Down
37 changes: 19 additions & 18 deletions Marlin/src/core/boards.h
Original file line number Diff line number Diff line change
Expand Up @@ -363,25 +363,26 @@
#define BOARD_BLACK_STM32F407VE 4204 // BLACK_STM32F407VE
#define BOARD_BLACK_STM32F407ZE 4205 // BLACK_STM32F407ZE
#define BOARD_STEVAL_3DP001V1 4206 // STEVAL-3DP001V1 3D PRINTER BOARD
#define BOARD_BTT_SKR_PRO_V1_1 4207 // BigTreeTech SKR Pro v1.1 (STM32F407ZG)
#define BOARD_BTT_SKR_PRO_V1_2 4208 // BigTreeTech SKR Pro v1.2 (STM32F407ZG)
#define BOARD_BTT_BTT002_V1_0 4209 // BigTreeTech BTT002 v1.0 (STM32F407VG)
#define BOARD_BTT_SKR_PRO_V1_1 4207 // BigTreeTech SKR Pro v1.1 (STM32F407ZGT6)
#define BOARD_BTT_SKR_PRO_V1_2 4208 // BigTreeTech SKR Pro v1.2 (STM32F407ZGT6)
#define BOARD_BTT_BTT002_V1_0 4209 // BigTreeTech BTT002 v1.0 (STM32F407VGT6)
#define BOARD_BTT_E3_RRF 4210 // BigTreeTech E3 RRF (STM32F407VGT6)
#define BOARD_BTT_GTR_V1_0 4211 // BigTreeTech GTR v1.0 (STM32F407IGT)
#define BOARD_LERDGE_K 4212 // Lerdge K (STM32F407ZG)
#define BOARD_LERDGE_S 4213 // Lerdge S (STM32F407VE)
#define BOARD_LERDGE_X 4214 // Lerdge X (STM32F407VE)
#define BOARD_VAKE403D 4215 // VAkE 403D (STM32F446VET6)
#define BOARD_FYSETC_S6 4216 // FYSETC S6 (STM32F446VET6)
#define BOARD_FYSETC_S6_V2_0 4217 // FYSETC S6 v2.0 (STM32F446VET6)
#define BOARD_FYSETC_SPIDER 4218 // FYSETC Spider (STM32F446VET6)
#define BOARD_FLYF407ZG 4219 // FLYF407ZG (STM32F407ZG)
#define BOARD_MKS_ROBIN2 4220 // MKS_ROBIN2 (STM32F407ZE)
#define BOARD_MKS_ROBIN_PRO_V2 4221 // MKS Robin Pro V2 (STM32F407VE)
#define BOARD_MKS_ROBIN_NANO_V3 4222 // MKS Robin Nano V3 (STM32F407VG)
#define BOARD_ANET_ET4 4223 // ANET ET4 V1.x (STM32F407VGT6)
#define BOARD_ANET_ET4P 4224 // ANET ET4P V1.x (STM32F407VGT6)
#define BOARD_FYSETC_CHEETAH_V20 4225 // FYSETC Cheetah V2.0
#define BOARD_BTT_SKR_V2_0 4211 // BigTreeTech SKR v2.0 (STM32F407VGT6)
#define BOARD_BTT_GTR_V1_0 4212 // BigTreeTech GTR v1.0 (STM32F407IGT)
#define BOARD_LERDGE_K 4213 // Lerdge K (STM32F407ZG)
#define BOARD_LERDGE_S 4214 // Lerdge S (STM32F407VE)
#define BOARD_LERDGE_X 4215 // Lerdge X (STM32F407VE)
#define BOARD_VAKE403D 4216 // VAkE 403D (STM32F446VET6)
#define BOARD_FYSETC_S6 4217 // FYSETC S6 (STM32F446VET6)
#define BOARD_FYSETC_S6_V2_0 4218 // FYSETC S6 v2.0 (STM32F446VET6)
#define BOARD_FYSETC_SPIDER 4219 // FYSETC Spider (STM32F446VET6)
#define BOARD_FLYF407ZG 4220 // FLYF407ZG (STM32F407ZG)
#define BOARD_MKS_ROBIN2 4221 // MKS_ROBIN2 (STM32F407ZE)
#define BOARD_MKS_ROBIN_PRO_V2 4222 // MKS Robin Pro V2 (STM32F407VE)
#define BOARD_MKS_ROBIN_NANO_V3 4223 // MKS Robin Nano V3 (STM32F407VG)
#define BOARD_ANET_ET4 4224 // ANET ET4 V1.x (STM32F407VGT6)
#define BOARD_ANET_ET4P 4225 // ANET ET4P V1.x (STM32F407VGT6)
#define BOARD_FYSETC_CHEETAH_V20 4226 // FYSETC Cheetah V2.0

//
// ARM Cortex M7
Expand Down
171 changes: 171 additions & 0 deletions Marlin/src/feature/stepper_driver_safety.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2021 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 <https://www.gnu.org/licenses/>.
*
*/
#include "../inc/MarlinConfig.h"
#include "../lcd/marlinui.h"

#if HAS_DRIVER_SAFE_POWER_PROTECT

#include "stepper_driver_safety.h"

static uint32_t axis_plug_backward = 0;

void stepper_driver_backward_error(PGM_P str) {
SERIAL_ERROR_START();
SERIAL_ECHOPGM_P(str);
SERIAL_ECHOLNPGM(" driver is backward!");
ui.status_printf_P(2, PSTR(S_FMT S_FMT), str, GET_TEXT(MSG_DRIVER_BACKWARD));
}

void stepper_driver_backward_check() {

OUT_WRITE(SAFE_POWER_PIN, LOW);

#define TEST_BACKWARD(AXIS, BIT) do { \
SET_INPUT(AXIS##_ENABLE_PIN); \
OUT_WRITE(AXIS##_STEP_PIN, false); \
delay(20); \
if (READ(AXIS##_ENABLE_PIN) == false) { \
SBI(axis_plug_backward, BIT); \
stepper_driver_backward_error(PSTR(STRINGIFY(AXIS))); \
} \
}while(0)

#if HAS_X_ENABLE
TEST_BACKWARD(X, 0);
#endif
#if HAS_X2_ENABLE
TEST_BACKWARD(X2, 1);
#endif

#if HAS_Y_ENABLE
TEST_BACKWARD(Y, 2);
#endif
#if HAS_Y2_ENABLE
TEST_BACKWARD(Y2, 3);
#endif

#if HAS_Z_ENABLE
TEST_BACKWARD(Z, 4);
#endif
#if HAS_Z2_ENABLE
TEST_BACKWARD(Z2, 5);
#endif
#if HAS_Z3_ENABLE
TEST_BACKWARD(Z3, 6);
#endif
#if HAS_Z4_ENABLE
TEST_BACKWARD(Z4, 7);
#endif

#if HAS_E0_ENABLE
TEST_BACKWARD(E0, 8);
#endif
#if HAS_E1_ENABLE
TEST_BACKWARD(E1, 9);
#endif
#if HAS_E2_ENABLE
TEST_BACKWARD(E2, 10);
#endif
#if HAS_E3_ENABLE
TEST_BACKWARD(E3, 11);
#endif
#if HAS_E4_ENABLE
TEST_BACKWARD(E4, 12);
#endif
#if HAS_E5_ENABLE
TEST_BACKWARD(E5, 13);
#endif
#if HAS_E6_ENABLE
TEST_BACKWARD(E6, 14);
#endif
#if HAS_E7_ENABLE
TEST_BACKWARD(E7, 15);
#endif

if (!axis_plug_backward)
WRITE(SAFE_POWER_PIN, HIGH);
}

void stepper_driver_backward_report() {
if (!axis_plug_backward) return;

auto _report_if_backward = [](PGM_P axis, uint8_t bit) {
if (TEST(axis_plug_backward, bit))
stepper_driver_backward_error(axis);
};

#define REPORT_BACKWARD(axis, bit) _report_if_backward(PSTR(STRINGIFY(axis)), bit)

#if HAS_X_ENABLE
REPORT_BACKWARD(X, 0);
#endif
#if HAS_X2_ENABLE
REPORT_BACKWARD(X2, 1);
#endif

#if HAS_Y_ENABLE
REPORT_BACKWARD(Y, 2);
#endif
#if HAS_Y2_ENABLE
REPORT_BACKWARD(Y2, 3);
#endif

#if HAS_Z_ENABLE
REPORT_BACKWARD(Z, 4);
#endif
#if HAS_Z2_ENABLE
REPORT_BACKWARD(Z2, 5);
#endif
#if HAS_Z3_ENABLE
REPORT_BACKWARD(Z3, 6);
#endif
#if HAS_Z4_ENABLE
REPORT_BACKWARD(Z4, 7);
#endif

#if HAS_E0_ENABLE
REPORT_BACKWARD(E0, 8);
#endif
#if HAS_E1_ENABLE
REPORT_BACKWARD(E1, 9);
#endif
#if HAS_E2_ENABLE
REPORT_BACKWARD(E2, 10);
#endif
#if HAS_E3_ENABLE
REPORT_BACKWARD(E3, 11);
#endif
#if HAS_E4_ENABLE
REPORT_BACKWARD(E4, 12);
#endif
#if HAS_E5_ENABLE
REPORT_BACKWARD(E5, 13);
#endif
#if HAS_E6_ENABLE
REPORT_BACKWARD(E6, 14);
#endif
#if HAS_E7_ENABLE
REPORT_BACKWARD(E7, 15);
#endif
}

#endif // HAS_DRIVER_SAFE_POWER_PROTECT
28 changes: 28 additions & 0 deletions Marlin/src/feature/stepper_driver_safety.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2021 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 <https://www.gnu.org/licenses/>.
*
*/
#pragma once


#include "../inc/MarlinConfigPre.h"

void stepper_driver_backward_check();
void stepper_driver_backward_report();
4 changes: 4 additions & 0 deletions Marlin/src/inc/Conditionals_post.h
Original file line number Diff line number Diff line change
Expand Up @@ -3042,3 +3042,7 @@
#if BUTTONS_EXIST(EN1, EN2, ENC)
#define HAS_ROTARY_ENCODER 1
#endif

#if PIN_EXISTS(SAFE_POWER) && DISABLED(DISABLE_DRIVER_SAFE_POWER_PROTECT)
#define HAS_DRIVER_SAFE_POWER_PROTECT 1
#endif
2 changes: 2 additions & 0 deletions Marlin/src/lcd/language/language_en.h
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,8 @@ namespace Language_en {
PROGMEM Language_Str MSG_BOTTOM_RIGHT = _UxGT("Bottom Right");
PROGMEM Language_Str MSG_CALIBRATION_COMPLETED = _UxGT("Calibration Completed");
PROGMEM Language_Str MSG_CALIBRATION_FAILED = _UxGT("Calibration Failed");

PROGMEM Language_Str MSG_DRIVER_BACKWARD = _UxGT(" driver backward");
}

#if FAN_COUNT == 1
Expand Down
2 changes: 2 additions & 0 deletions Marlin/src/pins/pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,8 @@
#include "stm32f4/pins_BTT_BTT002_V1_0.h" // STM32F4 env:BIGTREE_BTT002
#elif MB(BTT_E3_RRF)
#include "stm32f4/pins_BTT_E3_RRF.h" // STM32F4 env:BIGTREE_E3_RRF
#elif MB(BTT_SKR_V2_0)
#include "stm32f4/pins_BTT_SKR_V2_0.h" // STM32F4 env:BIGTREE_SKR_2
#elif MB(LERDGE_K)
#include "stm32f4/pins_LERDGE_K.h" // STM32F4 env:LERDGEK env:LERDGEK_usb_flash_drive
#elif MB(LERDGE_S)
Expand Down
Loading

0 comments on commit 4da3f45

Please sign in to comment.