From 437978d349d48a575b923eb4a6e020e5b42dadd5 Mon Sep 17 00:00:00 2001
From: Markus Towara <towara@stce.rwth-aachen.de>
Date: Tue, 15 Oct 2019 18:34:45 +0200
Subject: [PATCH] Bring SPI_SENSORLESS code up to date (#15560)

---
 Marlin/src/feature/tmc_util.h  | 28 +++++-----------------------
 Marlin/src/module/endstops.cpp | 12 ++++++------
 2 files changed, 11 insertions(+), 29 deletions(-)

diff --git a/Marlin/src/feature/tmc_util.h b/Marlin/src/feature/tmc_util.h
index 8d0435b4a6..e0c9170ae5 100644
--- a/Marlin/src/feature/tmc_util.h
+++ b/Marlin/src/feature/tmc_util.h
@@ -393,34 +393,16 @@ void test_tmc_connection(const bool test_x, const bool test_y, const bool test_z
 
     template<class TMC, char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
     bool TMCMarlin<TMC, AXIS_LETTER, DRIVER_ID, AXIS_ID>::test_stall_status() {
-      uint16_t sg_result = 0;
-
       this->switchCSpin(LOW);
 
-      if (this->TMC_SW_SPI != nullptr) {
-        this->TMC_SW_SPI->transfer(TMC2130_n::DRV_STATUS_t::address);
-        this->TMC_SW_SPI->transfer16(0);
-        // We only care about the last 10 bits
-        sg_result = this->TMC_SW_SPI->transfer(0);
-        sg_result <<= 8;
-        sg_result |= this->TMC_SW_SPI->transfer(0);
-      }
-      else {
-        SPI.beginTransaction(SPISettings(16000000/8, MSBFIRST, SPI_MODE3));
-        // Read DRV_STATUS
-        SPI.transfer(TMC2130_n::DRV_STATUS_t::address);
-        SPI.transfer16(0);
-        // We only care about the last 10 bits
-        sg_result = SPI.transfer(0);
-        sg_result <<= 8;
-        sg_result |= SPI.transfer(0);
-        SPI.endTransaction();
-      }
+      // read stallGuard flag from TMC library, will handle HW and SW SPI
+      TMC2130_n::DRV_STATUS_t drv_status{0};
+      drv_status.sr = this->DRV_STATUS();
+
       this->switchCSpin(HIGH);
 
-      return (sg_result & 0x3FF) == 0;
+      return drv_status.stallGuard;
     }
-
   #endif // SPI_ENDSTOPS
 
 #endif // USE_SENSORLESS
diff --git a/Marlin/src/module/endstops.cpp b/Marlin/src/module/endstops.cpp
index 0cb7c603d4..e5d07cbc65 100644
--- a/Marlin/src/module/endstops.cpp
+++ b/Marlin/src/module/endstops.cpp
@@ -537,7 +537,7 @@ void Endstops::update() {
   /**
    * Check and update endstops
    */
-  #if HAS_X_MIN
+  #if HAS_X_MIN && !X_SPI_SENSORLESS
     #if ENABLED(X_DUAL_ENDSTOPS)
       UPDATE_ENDSTOP_BIT(X, MIN);
       #if HAS_X2_MIN
@@ -550,7 +550,7 @@ void Endstops::update() {
     #endif
   #endif
 
-  #if HAS_X_MAX
+  #if HAS_X_MAX && !X_SPI_SENSORLESS
     #if ENABLED(X_DUAL_ENDSTOPS)
       UPDATE_ENDSTOP_BIT(X, MAX);
       #if HAS_X2_MAX
@@ -563,7 +563,7 @@ void Endstops::update() {
     #endif
   #endif
 
-  #if HAS_Y_MIN
+  #if HAS_Y_MIN && !Y_SPI_SENSORLESS
     #if ENABLED(Y_DUAL_ENDSTOPS)
       UPDATE_ENDSTOP_BIT(Y, MIN);
       #if HAS_Y2_MIN
@@ -576,7 +576,7 @@ void Endstops::update() {
     #endif
   #endif
 
-  #if HAS_Y_MAX
+  #if HAS_Y_MAX && !Y_SPI_SENSORLESS
     #if ENABLED(Y_DUAL_ENDSTOPS)
       UPDATE_ENDSTOP_BIT(Y, MAX);
       #if HAS_Y2_MAX
@@ -589,7 +589,7 @@ void Endstops::update() {
     #endif
   #endif
 
-  #if HAS_Z_MIN
+  #if HAS_Z_MIN && !Z_SPI_SENSORLESS
     #if Z_MULTI_ENDSTOPS
       UPDATE_ENDSTOP_BIT(Z, MIN);
       #if HAS_Z2_MIN
@@ -616,7 +616,7 @@ void Endstops::update() {
     UPDATE_ENDSTOP_BIT(Z, MIN_PROBE);
   #endif
 
-  #if HAS_Z_MAX
+  #if HAS_Z_MAX && !Z_SPI_SENSORLESS
     // Check both Z dual endstops
     #if Z_MULTI_ENDSTOPS
       UPDATE_ENDSTOP_BIT(Z, MAX);