From ca4423ed2a8188bcd671ef59c25765e44e67fbcf Mon Sep 17 00:00:00 2001
From: Scott Lahteine <github@thinkyhead.com>
Date: Thu, 5 Mar 2020 17:56:17 -0600
Subject: [PATCH] Clean up pins debugging

---
 Marlin/src/gcode/config/M43.cpp |  4 +-
 Marlin/src/pins/pins.h          | 40 ++++++++++++++++++++
 Marlin/src/pins/pinsDebug.h     | 67 ++++++++++++++-------------------
 3 files changed, 71 insertions(+), 40 deletions(-)

diff --git a/Marlin/src/gcode/config/M43.cpp b/Marlin/src/gcode/config/M43.cpp
index 0dd0013e05..39da373944 100644
--- a/Marlin/src/gcode/config/M43.cpp
+++ b/Marlin/src/gcode/config/M43.cpp
@@ -61,12 +61,12 @@ inline void toggle_pins() {
     pin_t pin = GET_PIN_MAP_PIN_M43(i);
     if (!VALID_PIN(pin)) continue;
     if (M43_NEVER_TOUCH(i) || (!ignore_protection && pin_is_protected(pin))) {
-      report_pin_state_extended(pin, ignore_protection, true, "Untouched ");
+      report_pin_state_extended(pin, ignore_protection, true, PSTR("Untouched "));
       SERIAL_EOL();
     }
     else {
       watchdog_refresh();
-      report_pin_state_extended(pin, ignore_protection, true, "Pulsing   ");
+      report_pin_state_extended(pin, ignore_protection, true, PSTR("Pulsing   "));
       #if AVR_AT90USB1286_FAMILY // Teensy IDEs don't know about these pins so must use FASTIO
         if (pin == TEENSY_E2) {
           SET_OUTPUT(TEENSY_E2);
diff --git a/Marlin/src/pins/pins.h b/Marlin/src/pins/pins.h
index 4f1483d55f..a3b14e22aa 100644
--- a/Marlin/src/pins/pins.h
+++ b/Marlin/src/pins/pins.h
@@ -862,6 +862,43 @@
   #define E7_ENABLE_PIN -1
 #endif
 
+//
+// Destroy unused CS pins
+//
+#if !AXIS_HAS_SPI(X)
+  #undef X_CS_PIN
+#endif
+#if !AXIS_HAS_SPI(Y)
+  #undef Y_CS_PIN
+#endif
+#if !AXIS_HAS_SPI(Z)
+  #undef Z_CS_PIN
+#endif
+#if !AXIS_HAS_SPI(E0)
+  #undef E0_CS_PIN
+#endif
+#if !AXIS_HAS_SPI(E1)
+  #undef E1_CS_PIN
+#endif
+#if !AXIS_HAS_SPI(E2)
+  #undef E2_CS_PIN
+#endif
+#if !AXIS_HAS_SPI(E3)
+  #undef E3_CS_PIN
+#endif
+#if !AXIS_HAS_SPI(E4)
+  #undef E4_CS_PIN
+#endif
+#if !AXIS_HAS_SPI(E5)
+  #undef E5_CS_PIN
+#endif
+#if !AXIS_HAS_SPI(E6)
+  #undef E6_CS_PIN
+#endif
+#if !AXIS_HAS_SPI(E7)
+  #undef E7_CS_PIN
+#endif
+
 #ifndef X_CS_PIN
   #define X_CS_PIN -1
 #endif
@@ -1170,6 +1207,9 @@
 
 #if HAS_FILAMENT_SENSOR
   #define FIL_RUNOUT1_PIN FIL_RUNOUT_PIN
+#else
+  #undef FIL_RUNOUT_PIN
+  #undef FIL_RUNOUT1_PIN
 #endif
 
 #ifndef LCD_PINS_D4
diff --git a/Marlin/src/pins/pinsDebug.h b/Marlin/src/pins/pinsDebug.h
index f247c7cacf..7fb9e808be 100644
--- a/Marlin/src/pins/pinsDebug.h
+++ b/Marlin/src/pins/pinsDebug.h
@@ -207,17 +207,32 @@ static void print_input_or_output(const bool isout) {
 }
 
 // pretty report with PWM info
-inline void report_pin_state_extended(pin_t pin, bool ignore, bool extended = false, const char *start_string = "") {
+inline void report_pin_state_extended(const pin_t pin, const bool ignore, const bool extended=false, PGM_P const start_string=nullptr) {
   char buffer[MAX_NAME_LENGTH + 1];   // for the sprintf statements
   bool found = false, multi_name_pin = false;
 
+  auto alt_pin_echo = [](const pin_t &pin) {
+    #if AVR_AT90USB1286_FAMILY
+      // Use FastIO for pins Teensy doesn't expose
+      if (pin == 46) {
+        print_input_or_output(IS_OUTPUT(46));
+        SERIAL_CHAR('0' + READ(46));
+        return false;
+      }
+      else if (pin == 47) {
+        print_input_or_output(IS_OUTPUT(47));
+        SERIAL_CHAR('0' + READ(47));
+        return false;
+      }
+    #endif
+    return true;
+  };
+
   for (uint8_t x = 0; x < COUNT(pin_array); x++)  {    // scan entire array and report all instances of this pin
     if (GET_ARRAY_PIN(x) == pin) {
-      if (found) multi_name_pin = true;
-      found = true;
-      if (!multi_name_pin) {    // report digital and analog pin number only on the first time through
-        sprintf_P(buffer, PSTR("%sPIN: "), start_string);     // digital pin number
-        SERIAL_ECHO(buffer);
+      if (!found) {    // report digital and analog pin number only on the first time through
+        if (start_string) serialprintPGM(start_string);
+        serialprintPGM(PSTR("PIN: "));
         PRINT_PIN(pin);
         PRINT_PORT(pin);
         if (int8_t(DIGITAL_PIN_TO_ANALOG_PIN(pin)) >= 0) {
@@ -228,27 +243,14 @@ inline void report_pin_state_extended(pin_t pin, bool ignore, bool extended = fa
       }
       else {
         SERIAL_CHAR('.');
-        SERIAL_ECHO_SP(MULTI_NAME_PAD + strlen(start_string));  // add padding if not the first instance found
+        SERIAL_ECHO_SP(MULTI_NAME_PAD + (start_string ? strlen_P(start_string) : 0));  // add padding if not the first instance found
       }
       PRINT_ARRAY_NAME(x);
       if (extended) {
         if (pin_is_protected(pin) && !ignore)
           SERIAL_ECHOPGM("protected ");
         else {
-          #if AVR_AT90USB1286_FAMILY //Teensy IDEs don't know about these pins so must use FASTIO
-            if (pin == 46 || pin == 47) {
-              if (pin == 46) {
-                print_input_or_output(IS_OUTPUT(46));
-                SERIAL_CHAR('0' + READ(46));
-              }
-              else if (pin == 47) {
-                print_input_or_output(IS_OUTPUT(47));
-                SERIAL_CHAR('0' + READ(47));
-              }
-            }
-            else
-          #endif
-          {
+          if (alt_pin_echo(pin)) {
             if (!GET_ARRAY_IS_DIGITAL(x)) {
               sprintf_P(buffer, PSTR("Analog in = %5ld"), (long)analogRead(DIGITAL_PIN_TO_ANALOG_PIN(pin)));
               SERIAL_ECHO(buffer);
@@ -274,12 +276,14 @@ inline void report_pin_state_extended(pin_t pin, bool ignore, bool extended = fa
         }
       }
       SERIAL_EOL();
+      multi_name_pin = found;
+      found = true;
     }  // end of IF
   } // end of for loop
 
   if (!found) {
-    sprintf_P(buffer, PSTR("%sPIN: "), start_string);
-    SERIAL_ECHO(buffer);
+    if (start_string) serialprintPGM(start_string);
+    serialprintPGM(PSTR("PIN: "));
     PRINT_PIN(pin);
     PRINT_PORT(pin);
     if (int8_t(DIGITAL_PIN_TO_ANALOG_PIN(pin)) >= 0) {
@@ -290,21 +294,8 @@ inline void report_pin_state_extended(pin_t pin, bool ignore, bool extended = fa
       SERIAL_ECHO_SP(8);   // add padding if not an analog pin
     SERIAL_ECHOPGM("<unused/unknown>");
     if (extended) {
-      #if AVR_AT90USB1286_FAMILY  //Teensy IDEs don't know about these pins so must use FASTIO
-        if (pin == 46 || pin == 47) {
-          SERIAL_ECHO_SP(12);
-          if (pin == 46) {
-            print_input_or_output(IS_OUTPUT(46));
-            SERIAL_CHAR('0' + READ(46));
-          }
-          else {
-            print_input_or_output(IS_OUTPUT(47));
-            SERIAL_CHAR('0' + READ(47));
-          }
-        }
-        else
-      #endif
-      {
+
+      if (alt_pin_echo(pin)) {
         if (pwm_status(pin)) {
           // do nothing
         }
-- 
GitLab