From c7f1f0dae6eba78542e7f031e2c431d8e0eb0772 Mon Sep 17 00:00:00 2001
From: Bob-the-Kuhn <bob.kuhn@att.net>
Date: Sat, 29 Oct 2016 13:42:43 -0500
Subject: [PATCH 1/9] Add endstop monitor & make pins report pretty

---
 Marlin/Marlin_main.cpp |   20 +-
 Marlin/pinsDebug.h     | 1148 +++++++++++++++++++++++++++++++---------
 Marlin/temperature.cpp |   93 +++-
 3 files changed, 1012 insertions(+), 249 deletions(-)

diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp
index f3588fd289b..be1d2c1eb2f 100755
--- a/Marlin/Marlin_main.cpp
+++ b/Marlin/Marlin_main.cpp
@@ -148,6 +148,7 @@
  *        The '#' is necessary when calling from within sd files, as it stops buffer prereading
  * M33  - Get the longname version of a path. (Requires LONG_FILENAME_HOST_SUPPORT)
  * M42  - Change pin status via gcode: M42 P<pin> S<value>. LED pin assumed if P is omitted.
+ * M43  - Monitor pins & report changes - report active pins
  * M48  - Measure Z Probe repeatability: M48 P<points> X<pos> Y<pos> V<level> E<engage> L<legs>. (Requires Z_MIN_PROBE_REPEATABILITY_TEST)
  * M75  - Start the print job timer.
  * M76  - Pause the print job timer.
@@ -4675,8 +4676,14 @@ inline void gcode_M42() {
   /**
    * M43: Pin report and debug
    *
+   *      pin report if just M43 with no codes
    *      P<pin> Will read/watch a single pin
    *      W      Watch pins for changes until reboot
+   *      E      toggles endstop monitor
+   *               reports changes to endstops
+   *               toggles LED when endstop changes
+   *               background function (machine continues to operate as normal)
+   *
    */
   inline void gcode_M43() {
     int first_pin = 0, last_pin = DIO_COUNT - 1;
@@ -4721,9 +4728,16 @@ inline void gcode_M42() {
         safe_delay(500);
       }
     }
-    else // single pins report
-      for (int8_t pin = first_pin; pin <= last_pin; pin++)
-        report_pin_state(pin);
+    if ( !(code_seen('P') || code_seen('W') || code_seen('E')))   // single pins report
+      for (uint8_t pin = first_pin; pin <= last_pin; pin++)
+        report_pin_state_extended(pin, code_seen('I') );        // "hidden" option to ignore protected list
+    
+    if (code_seen('E')) {
+      endstop_monitor_flag ^= true;
+      SERIAL_PROTOCOLPGM("endstop monitor ");
+      SERIAL_PROTOCOL(endstop_monitor_flag ? "en" : "dis");
+      SERIAL_PROTOCOLLNPGM("abled");
+    }    
   }
 
 #endif // PINS_DEBUGGING
diff --git a/Marlin/pinsDebug.h b/Marlin/pinsDebug.h
index c06eabbe6bb..d22d79ae90a 100644
--- a/Marlin/pinsDebug.h
+++ b/Marlin/pinsDebug.h
@@ -22,7 +22,8 @@
 
 // How many DIO pins are defined?
 #if defined(DIO85_PIN)
-  #define DIO_COUNT 86
+//  #define DIO_COUNT 86
+  #define DIO_COUNT 70  // digitalRead and other Arduino IDE routines only know about pins 0 through 69
 #elif defined(DIO53_PIN)
   #define DIO_COUNT 54
 #elif defined(DIO47_PIN)
@@ -33,139 +34,162 @@
   #define DIO_COUNT 22
 #endif
 
-#define _PIN_SAY(NAME) { SERIAL_ECHOPGM(STRINGIFY(NAME)); return true; }
-#define PIN_SAY(NAME) if (pin == NAME) _PIN_SAY(_##NAME##_);
-#define ANALOG_PIN_SAY(NAME) if (pin == analogInputToDigitalPin(NAME)) _PIN_SAY(_##NAME##_);
+bool endstop_monitor_flag = false;
+
+#define  NAME_FORMAT "%-28s"   // one place to specify the format of all the sources of names
+                               // "-" left justify, "28" minimum width of name, pad with blanks
+
+#define _PIN_SAY(NAME) { sprintf(buffer, NAME_FORMAT, NAME); SERIAL_ECHO(buffer); return true; }
+#define PIN_SAY(NAME) if (pin == NAME) _PIN_SAY(#NAME);
+
+#define _ANALOG_PIN_SAY(NAME)   { sprintf(buffer, NAME_FORMAT, NAME); SERIAL_ECHO(buffer); pin_is_analog = true; return true; }
+#define ANALOG_PIN_SAY(NAME) if (pin == analogInputToDigitalPin(NAME)) _ANALOG_PIN_SAY(#NAME);
+
 #define IS_ANALOG(P) ((P) >= analogInputToDigitalPin(0) && ((P) <= analogInputToDigitalPin(15) || (P) <= analogInputToDigitalPin(5)))
 
+
+
+int digitalRead_mod(int8_t pin)  // same as digitalRead except the PWM stop section has been removed
+{
+	uint8_t bit = digitalPinToBitMask(pin);
+	uint8_t port = digitalPinToPort(pin);
+  
+	if (port == NOT_A_PIN) return LOW;
+
+	if (*portInputRegister(port) & bit) return HIGH;
+	return LOW;
+}
+
+bool get_pinMode(int8_t pin)
+{
+	uint8_t bit = digitalPinToBitMask(pin);
+	uint8_t port = digitalPinToPort(pin);
+	volatile uint8_t *reg;
+	reg = portModeRegister(port);
+  return   *reg & bit;
+}
+
 // Report pin name for a given fastio digital pin index
-static bool report_pin_name(int8_t pin) {
-
-  SERIAL_ECHO((int)pin);
-  SERIAL_CHAR(' ');
 
+static bool report_pin_name(int8_t pin,bool &pin_is_analog) {
+  
+  char buffer[30];   // for the sprintf statements
+  pin_is_analog = false;   // default to digital pin
+  
   if (IS_ANALOG(pin)) {
-    SERIAL_CHAR('('); SERIAL_CHAR('A');
-    SERIAL_ECHO(int(pin - analogInputToDigitalPin(0)));
-    SERIAL_CHAR(')'); SERIAL_CHAR(' ');
+    sprintf(buffer, "(A%2d)  ", int(pin - analogInputToDigitalPin(0)));
+    SERIAL_ECHO(buffer);   
   }
+  else SERIAL_ECHOPGM("       ");
 
   #if defined(RXD) && RXD > -1
-    if (pin == 0) { SERIAL_ECHOPGM("RXD"); return true; }
+    if (pin == 0) { sprintf(buffer, NAME_FORMAT, "RXD"); SERIAL_ECHO(buffer); return true; }
   #endif
   #if defined(TXD) && TXD > -1
-    if (pin == 1) { SERIAL_ECHOPGM("TXD"); return true; }
+    if (pin == 1) { sprintf(buffer, NAME_FORMAT, "TXD"); SERIAL_ECHO(buffer); return true; }
   #endif
 
-  #if PIN_EXISTS(SERVO0)
-    PIN_SAY(SERVO0_PIN);
+  
+  // Pin list updated from 7 OCT RCBugfix branch
+  #if defined(__FD) && __FD > -1   
+    PIN_SAY(__FD)
   #endif
-  #if PIN_EXISTS(SERVO1)
-    PIN_SAY(SERVO1_PIN);
+  #if defined(__FS) && __FS > -1
+    PIN_SAY(__FS)
   #endif
-  #if PIN_EXISTS(SERVO2)
-    PIN_SAY(SERVO2_PIN);
+  #if defined(__GD) && __GD > -1
+    PIN_SAY(__GD)
   #endif
-  #if PIN_EXISTS(SERVO3)
-    PIN_SAY(SERVO3_PIN);
+  #if defined(__GS) && __GS > -1
+    PIN_SAY(__GS)
   #endif
-
-  #if PIN_EXISTS(X_MIN)
-    PIN_SAY(X_MIN_PIN);
+ 
+  #if PIN_EXISTS(AVR_MISO)
+    PIN_SAY(AVR_MISO_PIN);
   #endif
-  #if PIN_EXISTS(X_MAX)
-    PIN_SAY(X_MAX_PIN);
+  #if PIN_EXISTS(AVR_MOSI)
+    PIN_SAY(AVR_MOSI_PIN);
   #endif
-  #if PIN_EXISTS(Y_MIN)
-    PIN_SAY(Y_MIN_PIN);
+  #if PIN_EXISTS(AVR_SCK)
+    PIN_SAY(AVR_SCK_PIN);
   #endif
-  #if PIN_EXISTS(Y_MAX)
-    PIN_SAY(Y_MAX_PIN);
+  #if PIN_EXISTS(AVR_SS)
+    PIN_SAY(AVR_SS_PIN);
   #endif
-  #if PIN_EXISTS(Z_MIN)
-    PIN_SAY(Z_MIN_PIN);
+  #if PIN_EXISTS(BEEPER)
+    PIN_SAY(BEEPER_PIN);
   #endif
-  #if PIN_EXISTS(Z_MAX)
-    PIN_SAY(Z_MAX_PIN);
+  #if defined(BTN_CENTER) && BTN_CENTER > -1
+    PIN_SAY(BTN_CENTER);
   #endif
-  #if PIN_EXISTS(Z_MIN_PROBE)
-    PIN_SAY(Z_MIN_PROBE_PIN);
+  #if defined(BTN_DOWN) && BTN_DOWN > -1
+    PIN_SAY(BTN_DOWN);
   #endif
-  #if PIN_EXISTS(X_STEP)
-    PIN_SAY(X_STEP_PIN);
+  #if defined(BTN_DWN) && BTN_DWN > -1
+    PIN_SAY(BTN_DWN);
   #endif
-  #if PIN_EXISTS(X_DIR)
-    PIN_SAY(X_DIR_PIN);
+  #if defined(BTN_EN1) && BTN_EN1 > -1
+    PIN_SAY(BTN_EN1);
   #endif
-  #if PIN_EXISTS(X_ENABLE)
-    PIN_SAY(X_ENABLE_PIN);
+  #if defined(BTN_EN2) && BTN_EN2 > -1
+    PIN_SAY(BTN_EN2);
   #endif
-  #if PIN_EXISTS(X_MS1)
-    PIN_SAY(X_MS1_PIN);
+  #if defined(BTN_ENC) && BTN_ENC > -1
+    PIN_SAY(BTN_ENC);
   #endif
-  #if PIN_EXISTS(X_MS2)
-    PIN_SAY(X_MS2_PIN);
+  #if defined(BTN_HOME) && BTN_HOME > -1
+    PIN_SAY(BTN_HOME);
   #endif
-  #if PIN_EXISTS(X2_STEP)
-    PIN_SAY(X2_STEP_PIN);
+  #if defined(BTN_LEFT) && BTN_LEFT > -1
+    PIN_SAY(BTN_LEFT);
   #endif
-  #if PIN_EXISTS(X2_DIR)
-    PIN_SAY(X2_DIR_PIN);
+  #if defined(BTN_LFT) && BTN_LFT > -1
+    PIN_SAY(BTN_LFT);
   #endif
-  #if PIN_EXISTS(X2_ENABLE)
-    PIN_SAY(X2_ENABLE_PIN);
+  #if defined(BTN_RIGHT) && BTN_RIGHT > -1
+    PIN_SAY(BTN_RIGHT);
   #endif
-  #if PIN_EXISTS(Y_STEP)
-    PIN_SAY(Y_STEP_PIN);
+  #if defined(BTN_RT) && BTN_RT > -1
+    PIN_SAY(BTN_RT);
   #endif
-  #if PIN_EXISTS(Y_DIR)
-    PIN_SAY(Y_DIR_PIN);
+  #if defined(BTN_UP) && BTN_UP > -1
+    PIN_SAY(BTN_UP);
   #endif
-  #if PIN_EXISTS(Y_ENABLE)
-    PIN_SAY(Y_ENABLE_PIN);
+  #if PIN_EXISTS(CONTROLLERFAN)
+    PIN_SAY(CONTROLLERFAN_PIN);
   #endif
-  #if PIN_EXISTS(Y_MS1)
-    PIN_SAY(Y_MS1_PIN);
+  #if PIN_EXISTS(DAC_DISABLE)
+    PIN_SAY(DAC_DISABLE_PIN);
   #endif
-  #if PIN_EXISTS(Y_MS2)
-    PIN_SAY(Y_MS2_PIN);
+  #if defined(DAC_STEPPER_GAIN) && DAC_STEPPER_GAIN > -1
+    PIN_SAY(DAC_STEPPER_GAIN);
   #endif
-  #if PIN_EXISTS(Y2_STEP)
-    PIN_SAY(Y2_STEP_PIN);
+  #if defined(DAC_STEPPER_VREF) && DAC_STEPPER_VREF > -1
+    PIN_SAY(DAC_STEPPER_VREF);
   #endif
-  #if PIN_EXISTS(Y2_DIR)
-    PIN_SAY(Y2_DIR_PIN);
+  #if PIN_EXISTS(DEBUG)
+    PIN_SAY(DEBUG_PIN);
   #endif
-  #if PIN_EXISTS(Y2_ENABLE)
-    PIN_SAY(Y2_ENABLE_PIN);
+  #if PIN_EXISTS(DIGIPOTSS)
+    PIN_SAY(DIGIPOTSS_PIN);
   #endif
-  #if PIN_EXISTS(Z_STEP)
-    PIN_SAY(Z_STEP_PIN);
+  #if defined(DIO_COUNT) && DIO_COUNT > -1
+    PIN_SAY(DIO_COUNT);
   #endif
-  #if PIN_EXISTS(Z_DIR)
-    PIN_SAY(Z_DIR_PIN);
+  #if defined(DOGLCD_A0) && DOGLCD_A0 > -1
+    PIN_SAY(DOGLCD_A0);
   #endif
-  #if PIN_EXISTS(Z_ENABLE)
-    PIN_SAY(Z_ENABLE_PIN);
+  #if defined(DOGLCD_CS) && DOGLCD_CS > -1
+    PIN_SAY(DOGLCD_CS);
   #endif
-  #if PIN_EXISTS(Z_MS1)
-    PIN_SAY(Z_MS1_PIN);
+  #if defined(DOGLCD_MOSI) && DOGLCD_MOSI > -1
+    PIN_SAY(DOGLCD_MOSI);
   #endif
-  #if PIN_EXISTS(Z_MS2)
-    PIN_SAY(Z_MS2_PIN);
+  #if defined(DOGLCD_SCK) && DOGLCD_SCK > -1
+    PIN_SAY(DOGLCD_SCK);
   #endif
-  #if PIN_EXISTS(Z2_STEP)
-    PIN_SAY(Z2_STEP_PIN);
-  #endif
-  #if PIN_EXISTS(Z2_DIR)
-    PIN_SAY(Z2_DIR_PIN);
-  #endif
-  #if PIN_EXISTS(Z2_ENABLE)
-    PIN_SAY(Z2_ENABLE_PIN);
-  #endif
-
-  #if PIN_EXISTS(E0_STEP)
-    PIN_SAY(E0_STEP_PIN);
+  #if PIN_EXISTS(E0_ATT)
+    PIN_SAY(E0_ATT_PIN);
   #endif
   #if PIN_EXISTS(E0_DIR)
     PIN_SAY(E0_DIR_PIN);
@@ -179,8 +203,8 @@ static bool report_pin_name(int8_t pin) {
   #if PIN_EXISTS(E0_MS2)
     PIN_SAY(E0_MS2_PIN);
   #endif
-  #if PIN_EXISTS(E1_STEP)
-    PIN_SAY(E1_STEP_PIN);
+  #if PIN_EXISTS(E0_STEP)
+    PIN_SAY(E0_STEP_PIN);
   #endif
   #if PIN_EXISTS(E1_DIR)
     PIN_SAY(E1_DIR_PIN);
@@ -194,8 +218,8 @@ static bool report_pin_name(int8_t pin) {
   #if PIN_EXISTS(E1_MS2)
     PIN_SAY(E1_MS2_PIN);
   #endif
-  #if PIN_EXISTS(E2_STEP)
-    PIN_SAY(E2_STEP_PIN);
+  #if PIN_EXISTS(E1_STEP)
+    PIN_SAY(E1_STEP_PIN);
   #endif
   #if PIN_EXISTS(E2_DIR)
     PIN_SAY(E2_DIR_PIN);
@@ -203,8 +227,8 @@ static bool report_pin_name(int8_t pin) {
   #if PIN_EXISTS(E2_ENABLE)
     PIN_SAY(E2_ENABLE_PIN);
   #endif
-  #if PIN_EXISTS(E3_STEP)
-    PIN_SAY(E3_STEP_PIN);
+  #if PIN_EXISTS(E2_STEP)
+    PIN_SAY(E2_STEP_PIN);
   #endif
   #if PIN_EXISTS(E3_DIR)
     PIN_SAY(E3_DIR_PIN);
@@ -212,8 +236,8 @@ static bool report_pin_name(int8_t pin) {
   #if PIN_EXISTS(E3_ENABLE)
     PIN_SAY(E3_ENABLE_PIN);
   #endif
-  #if PIN_EXISTS(E4_STEP)
-    PIN_SAY(E4_STEP_PIN);
+  #if PIN_EXISTS(E3_STEP)
+    PIN_SAY(E3_STEP_PIN);
   #endif
   #if PIN_EXISTS(E4_DIR)
     PIN_SAY(E4_DIR_PIN);
@@ -221,30 +245,89 @@ static bool report_pin_name(int8_t pin) {
   #if PIN_EXISTS(E4_ENABLE)
     PIN_SAY(E4_ENABLE_PIN);
   #endif
-
+  #if PIN_EXISTS(E4_STEP)
+    PIN_SAY(E4_STEP_PIN);
+  #endif
+  #if defined(encrot1) && encrot1 > -1
+    PIN_SAY(encrot1);
+  #endif
+  #if defined(encrot2) && encrot2 > -1
+    PIN_SAY(encrot2);
+  #endif
+  #if defined(encrot3) && encrot3 > -1
+    PIN_SAY(encrot3);
+  #endif
+  #if defined(EXT_AUX_A0_IO) && EXT_AUX_A0_IO > -1
+    PIN_SAY(EXT_AUX_A0_IO);
+  #endif
+  #if defined(EXT_AUX_A1) && EXT_AUX_A1 > -1
+    PIN_SAY(EXT_AUX_A1);
+  #endif
+  #if defined(EXT_AUX_A1_IO) && EXT_AUX_A1_IO > -1
+    PIN_SAY(EXT_AUX_A1_IO);
+  #endif
+  #if defined(EXT_AUX_A2) && EXT_AUX_A2 > -1
+    PIN_SAY(EXT_AUX_A2);
+  #endif
+  #if defined(EXT_AUX_A2_IO) && EXT_AUX_A2_IO > -1
+    PIN_SAY(EXT_AUX_A2_IO);
+  #endif
+  #if defined(EXT_AUX_A3) && EXT_AUX_A3 > -1
+    PIN_SAY(EXT_AUX_A3);
+  #endif
+  #if defined(EXT_AUX_A3_IO) && EXT_AUX_A3_IO > -1
+    PIN_SAY(EXT_AUX_A3_IO);
+  #endif
+  #if defined(EXT_AUX_A4) && EXT_AUX_A4 > -1
+    PIN_SAY(EXT_AUX_A4);
+  #endif
+  #if defined(EXT_AUX_A4_IO) && EXT_AUX_A4_IO > -1
+    PIN_SAY(EXT_AUX_A4_IO);
+  #endif
+  #if defined(EXT_AUX_PWM_D24) && EXT_AUX_PWM_D24 > -1
+    PIN_SAY(EXT_AUX_PWM_D24);
+  #endif
+  #if defined(EXT_AUX_RX1_D2) && EXT_AUX_RX1_D2 > -1
+    PIN_SAY(EXT_AUX_RX1_D2);
+  #endif
+  #if defined(EXT_AUX_SDA_D1) && EXT_AUX_SDA_D1 > -1
+    PIN_SAY(EXT_AUX_SDA_D1);
+  #endif
+  #if defined(EXT_AUX_TX1_D3) && EXT_AUX_TX1_D3 > -1
+    PIN_SAY(EXT_AUX_TX1_D3);
+  #endif
+  #if PIN_EXISTS(EXTRUDER_0_AUTO_FAN)
+    PIN_SAY(EXTRUDER_0_AUTO_FAN_PIN);
+  #endif
+  #if PIN_EXISTS(EXTRUDER_1_AUTO_FAN)
+    PIN_SAY(EXTRUDER_1_AUTO_FAN_PIN);
+  #endif
+  #if PIN_EXISTS(EXTRUDER_2_AUTO_FAN)
+    PIN_SAY(EXTRUDER_2_AUTO_FAN_PIN);
+  #endif
+  #if PIN_EXISTS(EXTRUDER_3_AUTO_FAN)
+    PIN_SAY(EXTRUDER_3_AUTO_FAN_PIN);
+  #endif
   #if PIN_EXISTS(FAN)
     PIN_SAY(FAN_PIN);
   #endif
+  #if PIN_EXISTS(FAN0)
+    PIN_SAY(FAN0_PIN);
+  #endif
   #if PIN_EXISTS(FAN1)
     PIN_SAY(FAN1_PIN);
   #endif
   #if PIN_EXISTS(FAN2)
     PIN_SAY(FAN2_PIN);
   #endif
-  #if PIN_EXISTS(CONTROLLERFAN)
-    PIN_SAY(CONTROLLERFAN_PIN);
+  #if PIN_EXISTS(FIL_RUNOUT)
+    PIN_SAY(FIL_RUNOUT_PIN);
   #endif
-  #if PIN_EXISTS(E0_AUTO_FAN)
-    PIN_SAY(E0_AUTO_FAN_PIN);
+  #if PIN_EXISTS(FILWIDTH)
+    ANALOG_PIN_SAY(FILWIDTH_PIN);
   #endif
-  #if PIN_EXISTS(E1_AUTO_FAN)
-    PIN_SAY(E1_AUTO_FAN_PIN);
-  #endif
-  #if PIN_EXISTS(E2_AUTO_FAN)
-    PIN_SAY(E2_AUTO_FAN_PIN);
-  #endif
-  #if PIN_EXISTS(E3_AUTO_FAN)
-    PIN_SAY(E3_AUTO_FAN_PIN);
+  #if defined(GEN7_VERSION) && GEN7_VERSION > -1
+    PIN_SAY(GEN7_VERSION);
   #endif
   #if PIN_EXISTS(HEATER_0)
     PIN_SAY(HEATER_0_PIN);
@@ -258,138 +341,35 @@ static bool report_pin_name(int8_t pin) {
   #if PIN_EXISTS(HEATER_3)
     PIN_SAY(HEATER_3_PIN);
   #endif
+  #if PIN_EXISTS(HEATER_4)
+    PIN_SAY(HEATER_4_PIN);
+  #endif
+  #if PIN_EXISTS(HEATER_5)
+    PIN_SAY(HEATER_5_PIN);
+  #endif
+  #if PIN_EXISTS(HEATER_6)
+    PIN_SAY(HEATER_6_PIN);
+  #endif
+  #if PIN_EXISTS(HEATER_7)
+    PIN_SAY(HEATER_7_PIN);
+  #endif
   #if PIN_EXISTS(HEATER_BED)
     PIN_SAY(HEATER_BED_PIN);
   #endif
-
-  #if PIN_EXISTS(X_ATT)
-    PIN_SAY(X_ATT_PIN);
-  #endif
-  #if PIN_EXISTS(Y_ATT)
-    PIN_SAY(Y_ATT_PIN);
-  #endif
-  #if PIN_EXISTS(Z_ATT)
-    PIN_SAY(Z_ATT_PIN);
-  #endif
-  #if PIN_EXISTS(E0_ATT)
-    PIN_SAY(E0_ATT_PIN);
-  #endif
-
-  #if PIN_EXISTS(TEMP_0)
-    ANALOG_PIN_SAY(TEMP_0_PIN);
-  #endif
-  #if PIN_EXISTS(TEMP_1)
-    ANALOG_PIN_SAY(TEMP_1_PIN);
-  #endif
-  #if PIN_EXISTS(TEMP_2)
-    ANALOG_PIN_SAY(TEMP_2_PIN);
-  #endif
-  #if PIN_EXISTS(TEMP_3)
-    ANALOG_PIN_SAY(TEMP_3_PIN);
-  #endif
-  #if PIN_EXISTS(TEMP_BED)
-    ANALOG_PIN_SAY(TEMP_BED_PIN);
-  #endif
-  #if PIN_EXISTS(FILWIDTH)
-    ANALOG_PIN_SAY(FILWIDTH_PIN);
-  #endif
-
-  #if PIN_EXISTS(BEEPER)
-    PIN_SAY(BEEPER_PIN);
-  #endif
-  #if PIN_EXISTS(SLED)
-    PIN_SAY(SLED_PIN);
-  #endif
-  #if PIN_EXISTS(FIL_RUNOUT)
-    PIN_SAY(FIL_RUNOUT_PIN);
-  #endif
-
-  #if PIN_EXISTS(LED)
-    PIN_SAY(LED_PIN);
-  #endif
-  // #if defined(DEBUG_LED) && DEBUG_LED > -1
-  //   PIN_SAY(DEBUG_LED);
-  // #endif
-  #if PIN_EXISTS(STAT_LED_RED)
-    PIN_SAY(STAT_LED_RED_PIN);
-  #endif
-  #if PIN_EXISTS(STAT_LED_BLUE)
-    PIN_SAY(STAT_LED_BLUE_PIN);
-  #endif
-
-  #if PIN_EXISTS(DIGIPOTSS)
-    PIN_SAY(DIGIPOTSS_PIN);
-  #endif
-
-  #if PIN_EXISTS(SCK)
-    PIN_SAY(SCK_PIN);
-  #endif
-  #if PIN_EXISTS(MISO)
-    PIN_SAY(MISO_PIN);
-  #endif
-  #if PIN_EXISTS(MOSI)
-    PIN_SAY(MOSI_PIN);
-  #endif
-  #if PIN_EXISTS(SS)
-    PIN_SAY(SS_PIN);
-  #endif
-
-  #if PIN_EXISTS(SD_DETECT)
-    PIN_SAY(SD_DETECT_PIN);
-  #endif
-
-  #if defined(SDPOWER) && SDPOWER > -1
-    PIN_SAY(SDPOWER);
-  #endif
-  #if defined(SDSS) && SDSS > -1
-    PIN_SAY(SDSS);
-  #endif
   #if defined(I2C_SCL) && I2C_SCL > -1
     PIN_SAY(I2C_SCL);
   #endif
   #if defined(I2C_SDA) && I2C_SDA > -1
     PIN_SAY(I2C_SDA);
   #endif
-  #if defined(SCL) && SCL > -1
-    PIN_SAY(SCL);
-  #endif
-  #if defined(SDA) && SDA > -1
-    PIN_SAY(SDA);
-  #endif
-
-  #if PIN_EXISTS(PS_ON)
-    PIN_SAY(PS_ON_PIN);
-  #endif
   #if PIN_EXISTS(KILL)
     PIN_SAY(KILL_PIN);
   #endif
-  #if PIN_EXISTS(SUICIDE)
-    PIN_SAY(SUICIDE_PIN);
+  #if PIN_EXISTS(LCD_BACKLIGHT)
+    PIN_SAY(LCD_BACKLIGHT_PIN);
   #endif
-  #if PIN_EXISTS(DEBUG)
-    PIN_SAY(DEBUG_PIN);
-  #endif
-  #if PIN_EXISTS(PHOTOGRAPH)
-    PIN_SAY(PHOTOGRAPH_PIN);
-  #endif
-
-  #if PIN_EXISTS(BEEPER)
-    PIN_SAY(BEEPER_PIN);
-  #endif
-  #if defined(BTN_EN1) && BTN_EN1 > -1
-    PIN_SAY(BTN_EN1);
-  #endif
-  #if defined(BTN_EN2) && BTN_EN2 > -1
-    PIN_SAY(BTN_EN2);
-  #endif
-  #if defined(BTN_ENC) && BTN_ENC > -1
-    PIN_SAY(BTN_ENC);
-  #endif
-  #if defined(LCD_PINS_RS) && LCD_PINS_RS > -1
-    PIN_SAY(LCD_PINS_RS);
-  #endif
-  #if defined(LCD_PINS_ENABLE) && LCD_PINS_ENABLE > -1
-    PIN_SAY(LCD_PINS_ENABLE);
+  #if defined(LCD_CONTRAST) && LCD_CONTRAST > -1
+    PIN_SAY(LCD_CONTRAST);
   #endif
   #if defined(LCD_PINS_D4) && LCD_PINS_D4 > -1
     PIN_SAY(LCD_PINS_D4);
@@ -403,33 +383,676 @@ static bool report_pin_name(int8_t pin) {
   #if defined(LCD_PINS_D7) && LCD_PINS_D7 > -1
     PIN_SAY(LCD_PINS_D7);
   #endif
-
+  #if defined(LCD_PINS_ENABLE) && LCD_PINS_ENABLE > -1
+    PIN_SAY(LCD_PINS_ENABLE);
+  #endif
+  #if defined(LCD_PINS_RS) && LCD_PINS_RS > -1
+    PIN_SAY(LCD_PINS_RS);
+  #endif
+  #if defined(LCD_SDSS) && LCD_SDSS > -1
+    PIN_SAY(LCD_SDSS);
+  #endif
+  #if PIN_EXISTS(LED)
+    PIN_SAY(LED_PIN);
+  #endif
+  #if PIN_EXISTS(MAIN_VOLTAGE_MEASURE)
+    PIN_SAY(MAIN_VOLTAGE_MEASURE_PIN);
+  #endif
+  #if defined(MAX6675_SS) && MAX6675_SS > -1
+    PIN_SAY(MAX6675_SS);
+  #endif
+  #if PIN_EXISTS(MISO)
+    PIN_SAY(MISO_PIN);
+  #endif
+  #if PIN_EXISTS(MOSFET_D)
+    PIN_SAY(MOSFET_D_PIN);
+  #endif
+  #if PIN_EXISTS(MOSI)
+    PIN_SAY(MOSI_PIN);
+  #endif
+  #if PIN_EXISTS(MOTOR_CURRENT_PWM_E)
+    PIN_SAY(MOTOR_CURRENT_PWM_E_PIN);
+  #endif
+  #if PIN_EXISTS(MOTOR_CURRENT_PWM_XY)
+    PIN_SAY(MOTOR_CURRENT_PWM_XY_PIN);
+  #endif
+  #if PIN_EXISTS(MOTOR_CURRENT_PWM_Z)
+    PIN_SAY(MOTOR_CURRENT_PWM_Z_PIN);
+  #endif
+  #if defined(NUM_TLCS) && NUM_TLCS > -1
+    PIN_SAY(NUM_TLCS);
+  #endif
+  #if PIN_EXISTS(PHOTOGRAPH)
+    PIN_SAY(PHOTOGRAPH_PIN);
+  #endif
+  #if PIN_EXISTS(PS_ON)
+    PIN_SAY(PS_ON_PIN);
+  #endif
+  #if PIN_EXISTS(RAMPS_D10)
+    PIN_SAY(RAMPS_D10_PIN);
+  #endif
   #if PIN_EXISTS(RAMPS_D8)
     PIN_SAY(RAMPS_D8_PIN);
   #endif
   #if PIN_EXISTS(RAMPS_D9)
     PIN_SAY(RAMPS_D9_PIN);
   #endif
-  #if PIN_EXISTS(RAMPS_D10)
-    PIN_SAY(RAMPS_D10_PIN);
-  #endif
-  #if PIN_EXISTS(MOSFET_D)
-    PIN_SAY(MOSFET_D_PIN);
-  #endif
-
-  #if PIN_EXISTS(TX_ENABLE)
-    PIN_SAY(TX_ENABLE_PIN);
-  #endif
   #if PIN_EXISTS(RX_ENABLE)
     PIN_SAY(RX_ENABLE_PIN);
   #endif
+  #if PIN_EXISTS(SAFETY_TRIGGERED)
+    PIN_SAY(SAFETY_TRIGGERED_PIN);
+  #endif
+  #if PIN_EXISTS(SCK)
+    PIN_SAY(SCK_PIN);
+  #endif
+  #if defined(SCL) && SCL > -1
+    PIN_SAY(SCL);
+  #endif
+  #if PIN_EXISTS(SD_DETECT)
+    PIN_SAY(SD_DETECT_PIN);
+  #endif
+  #if defined(SDA) && SDA > -1
+    PIN_SAY(SDA);
+  #endif
+  #if defined(SDPOWER) && SDPOWER > -1
+    PIN_SAY(SDPOWER);
+  #endif
+  #if defined(SDSS) && SDSS > -1
+    PIN_SAY(SDSS);
+  #endif
+  #if PIN_EXISTS(SERVO0)
+    PIN_SAY(SERVO0_PIN);
+  #endif
+  #if PIN_EXISTS(SERVO1)
+    PIN_SAY(SERVO1_PIN);
+  #endif
+  #if PIN_EXISTS(SERVO2)
+    PIN_SAY(SERVO2_PIN);
+  #endif
+  #if PIN_EXISTS(SERVO3)
+    PIN_SAY(SERVO3_PIN);
+  #endif
+  #if defined(SHIFT_CLK) && SHIFT_CLK > -1
+    PIN_SAY(SHIFT_CLK);
+  #endif
+  #if defined(SHIFT_EN) && SHIFT_EN > -1
+    PIN_SAY(SHIFT_EN);
+  #endif
+  #if defined(SHIFT_LD) && SHIFT_LD > -1
+    PIN_SAY(SHIFT_LD);
+  #endif
+  #if defined(SHIFT_OUT) && SHIFT_OUT > -1
+    PIN_SAY(SHIFT_OUT);
+  #endif
+  #if PIN_EXISTS(SLED)
+    PIN_SAY(SLED_PIN);
+  #endif
+  #if PIN_EXISTS(SLEEP_WAKE)
+    PIN_SAY(SLEEP_WAKE_PIN);
+  #endif
+  #if PIN_EXISTS(SOL1)
+    PIN_SAY(SOL1_PIN);
+  #endif
+  #if PIN_EXISTS(SOL2)
+    PIN_SAY(SOL2_PIN);
+  #endif
+  #if PIN_EXISTS(SPINDLE_ENABLE)
+    PIN_SAY(SPINDLE_ENABLE_PIN);
+  #endif
+  #if PIN_EXISTS(SPINDLE_SPEED)
+    PIN_SAY(SPINDLE_SPEED_PIN);
+  #endif
+  #if PIN_EXISTS(SS)
+    PIN_SAY(SS_PIN);
+  #endif
+  #if PIN_EXISTS(STAT_LED_BLUE)
+    PIN_SAY(STAT_LED_BLUE_PIN);
+  #endif
+  #if PIN_EXISTS(STAT_LED_RED)
+    PIN_SAY(STAT_LED_RED_PIN);
+  #endif
+  #if PIN_EXISTS(STEPPER_RESET)
+    PIN_SAY(STEPPER_RESET_PIN);
+  #endif
+  #if PIN_EXISTS(SUICIDE)
+    PIN_SAY(SUICIDE_PIN);
+  #endif
+  #if defined(TC1) && TC1 > -1
+    ANALOG_PIN_SAY(TC1);
+  #endif
+  #if defined(TC2) && TC2 > -1
+    ANALOG_PIN_SAY(TC2);
+  #endif
+  #if PIN_EXISTS(TEMP_0)
+    ANALOG_PIN_SAY(TEMP_0_PIN);
+  #endif
+  #if PIN_EXISTS(TEMP_1)
+    ANALOG_PIN_SAY(TEMP_1_PIN);
+  #endif
+  #if PIN_EXISTS(TEMP_2)
+    ANALOG_PIN_SAY(TEMP_2_PIN);
+  #endif
+  #if PIN_EXISTS(TEMP_3)
+    ANALOG_PIN_SAY(TEMP_3_PIN);
+  #endif
+  #if PIN_EXISTS(TEMP_4)
+    ANALOG_PIN_SAY(TEMP_4_PIN);
+  #endif
+  #if PIN_EXISTS(TEMP_BED)
+    ANALOG_PIN_SAY(TEMP_BED_PIN);
+  #endif
+  #if PIN_EXISTS(TEMP_X)
+    ANALOG_PIN_SAY(TEMP_X_PIN);
+  #endif
+  #if defined(TLC_BLANK_BIT) && TLC_BLANK_BIT > -1
+    PIN_SAY(TLC_BLANK_BIT);
+  #endif
+  #if PIN_EXISTS(TLC_BLANK)
+    PIN_SAY(TLC_BLANK_PIN);
+  #endif
+  #if defined(TLC_CLOCK_BIT) && TLC_CLOCK_BIT > -1
+    PIN_SAY(TLC_CLOCK_BIT);
+  #endif
+  #if PIN_EXISTS(TLC_CLOCK)
+    PIN_SAY(TLC_CLOCK_PIN);
+  #endif
+  #if defined(TLC_DATA_BIT) && TLC_DATA_BIT > -1
+    PIN_SAY(TLC_DATA_BIT);
+  #endif
+  #if PIN_EXISTS(TLC_DATA)
+    PIN_SAY(TLC_DATA_PIN);
+  #endif
+  #if PIN_EXISTS(TLC_XLAT)
+    PIN_SAY(TLC_XLAT_PIN);
+  #endif
+  #if PIN_EXISTS(TX_ENABLE)
+    PIN_SAY(TX_ENABLE_PIN);
+  #endif
+  #if defined(UNUSED_PWM) && UNUSED_PWM > -1
+    PIN_SAY(UNUSED_PWM);
+  #endif
+  #if PIN_EXISTS(X_ATT)
+    PIN_SAY(X_ATT_PIN);
+  #endif
+  #if PIN_EXISTS(X_DIR)
+    PIN_SAY(X_DIR_PIN);
+  #endif
+  #if PIN_EXISTS(X_ENABLE)
+    PIN_SAY(X_ENABLE_PIN);
+  #endif
+  #if PIN_EXISTS(X_MAX)
+    PIN_SAY(X_MAX_PIN);
+  #endif
+  #if PIN_EXISTS(X_MIN)
+    PIN_SAY(X_MIN_PIN);
+  #endif
+  #if PIN_EXISTS(X_MS1)
+    PIN_SAY(X_MS1_PIN);
+  #endif
+  #if PIN_EXISTS(X_MS2)
+    PIN_SAY(X_MS2_PIN);
+  #endif
+  #if PIN_EXISTS(X_STEP)
+    PIN_SAY(X_STEP_PIN);
+  #endif
+  #if PIN_EXISTS(X_STOP)
+    PIN_SAY(X_STOP_PIN);
+  #endif
+  #if PIN_EXISTS(X2_DIR)
+    PIN_SAY(X2_DIR_PIN);
+  #endif
+  #if PIN_EXISTS(X2_ENABLE)
+    PIN_SAY(X2_ENABLE_PIN);
+  #endif
+  #if PIN_EXISTS(X2_STEP)
+    PIN_SAY(X2_STEP_PIN);
+  #endif
+  #if PIN_EXISTS(Y_ATT)
+    PIN_SAY(Y_ATT_PIN);
+  #endif
+  #if PIN_EXISTS(Y_DIR)
+    PIN_SAY(Y_DIR_PIN);
+  #endif
+  #if PIN_EXISTS(Y_ENABLE)
+    PIN_SAY(Y_ENABLE_PIN);
+  #endif
+  #if PIN_EXISTS(Y_MAX)
+    PIN_SAY(Y_MAX_PIN);
+  #endif
+  #if PIN_EXISTS(Y_MIN)
+    PIN_SAY(Y_MIN_PIN);
+  #endif
+  #if PIN_EXISTS(Y_MS1)
+    PIN_SAY(Y_MS1_PIN);
+  #endif
+  #if PIN_EXISTS(Y_MS2)
+    PIN_SAY(Y_MS2_PIN);
+  #endif
+  #if PIN_EXISTS(Y_STEP)
+    PIN_SAY(Y_STEP_PIN);
+  #endif
+  #if PIN_EXISTS(Y_STOP)
+    PIN_SAY(Y_STOP_PIN);
+  #endif
+  #if PIN_EXISTS(Y2_DIR)
+    PIN_SAY(Y2_DIR_PIN);
+  #endif
+  #if PIN_EXISTS(Y2_ENABLE)
+    PIN_SAY(Y2_ENABLE_PIN);
+  #endif
+  #if PIN_EXISTS(Y2_STEP)
+    PIN_SAY(Y2_STEP_PIN);
+  #endif
+  #if PIN_EXISTS(Z_ATT)
+    PIN_SAY(Z_ATT_PIN);
+  #endif
+  #if PIN_EXISTS(Z_DIR)
+    PIN_SAY(Z_DIR_PIN);
+  #endif
+  #if PIN_EXISTS(Z_ENABLE)
+    PIN_SAY(Z_ENABLE_PIN);
+  #endif
+  #if PIN_EXISTS(Z_MAX)
+    PIN_SAY(Z_MAX_PIN);
+  #endif
+  #if PIN_EXISTS(Z_MIN)
+    PIN_SAY(Z_MIN_PIN);
+  #endif
+  #if PIN_EXISTS(Z_MIN_PROBE)
+    PIN_SAY(Z_MIN_PROBE_PIN);
+  #endif
+  #if PIN_EXISTS(Z_MS1)
+    PIN_SAY(Z_MS1_PIN);
+  #endif
+  #if PIN_EXISTS(Z_MS2)
+    PIN_SAY(Z_MS2_PIN);
+  #endif
+  #if PIN_EXISTS(Z_STEP)
+    PIN_SAY(Z_STEP_PIN);
+  #endif
+  #if PIN_EXISTS(Z_STOP)
+    PIN_SAY(Z_STOP_PIN);
+  #endif
+  #if PIN_EXISTS(Z2_DIR)
+    PIN_SAY(Z2_DIR_PIN);
+  #endif
+  #if PIN_EXISTS(Z2_ENABLE)
+    PIN_SAY(Z2_ENABLE_PIN);
+  #endif
+  #if PIN_EXISTS(Z2_STEP)
+    PIN_SAY(Z2_STEP_PIN);
+  #endif
 
-  SERIAL_ECHOPGM("<unused>");
+  sprintf(buffer, NAME_FORMAT, "<unused> ");
+  SERIAL_ECHO(buffer);
+  
   return false;
-}
+}  //  report_pin_name
+
+//  True - currently a PWM pin
+static bool PWM_status(uint8_t pin) {
+  char buffer[20];   // for the sprintf statements
+  
+  switch(digitalPinToTimer(pin)) {
+
+    #if defined(TCCR0A) && defined(COM0A1)
+      case TIMER0A:
+        if (TCCR0A & (_BV(COM0A1) | _BV(COM0A0))){
+          sprintf(buffer, "PWM:  %4d", OCR0A); 
+          SERIAL_ECHO(buffer);
+          return true;
+        }
+        else return false;
+        break;
+      case TIMER0B:
+        if (TCCR0A & (_BV(COM0B1) | _BV(COM0B0))){
+          sprintf(buffer, "PWM:  %4d",OCR0B); 
+          SERIAL_ECHO(buffer);
+          return true;
+        }
+        else return false;
+        break;
+    #endif
+
+    #if defined(TCCR1A) && defined(COM1A1)
+      case TIMER1A:
+        if (TCCR1A & (_BV(COM1A1) | _BV(COM1A0))){
+          sprintf(buffer, "PWM:  %4d",OCR1A); 
+          SERIAL_ECHO(buffer);
+          return true;
+        }
+        else return false;
+        break;
+      case TIMER1B:
+        if (TCCR1A & (_BV(COM1B1) | _BV(COM1B0))){
+          sprintf(buffer, "PWM:  %4d",OCR1B); 
+          SERIAL_ECHO(buffer);
+          return true;
+        }
+        else return false;
+        break; 
+      case TIMER1C:
+        if (TCCR1A & (_BV(COM1C1) | _BV(COM1C0))){
+          sprintf(buffer, "PWM:  %4d",OCR1C); 
+          SERIAL_ECHO(buffer);
+          return true;
+        }
+        else return false;
+        break;
+    #endif
+
+    #if defined(TCCR2A) && defined(COM2A1)
+      case TIMER2A:
+        if (TCCR2A & (_BV(COM2A1) | _BV(COM2A0))){
+          sprintf(buffer, "PWM:  %4d",OCR2A); 
+          SERIAL_ECHO(buffer);
+          return true;
+        }
+        else return false;
+        break;
+      case TIMER2B:
+        if (TCCR2A & (_BV(COM2B1) | _BV(COM2B0))){
+          sprintf(buffer, "PWM:  %4d",OCR2B); 
+          SERIAL_ECHO(buffer);
+          return true;
+        }
+        else return false;
+        break;
+    #endif
+
+    #if defined(TCCR3A) && defined(COM3A1)
+      case TIMER3A:
+        if (TCCR3A & (_BV(COM3A1) | _BV(COM3A0))){
+          sprintf(buffer, "PWM:  %4d",OCR3A); 
+          SERIAL_ECHO(buffer);
+          return true;
+        }
+        else return false;
+        break;
+      case TIMER3B:
+        if (TCCR3A & (_BV(COM3B1) | _BV(COM3B0))){
+          sprintf(buffer, "PWM:  %4d",OCR3B); 
+          SERIAL_ECHO(buffer);
+          return true;
+        }
+        else return false;
+        break;
+      case TIMER3C:
+        if (TCCR3A & (_BV(COM3C1) | _BV(COM3C0))){
+          sprintf(buffer, "PWM:  %4d",OCR3C); 
+          SERIAL_ECHO(buffer);
+          return true;
+        }
+        else return false;
+        break;
+    #endif
+
+    #if defined(TCCR4A)
+      case TIMER4A:
+        if (TCCR4A & (_BV(COM4A1) | _BV(COM4A0))){
+          sprintf(buffer, "PWM:  %4d",OCR4A); 
+          SERIAL_ECHO(buffer);
+          return true;
+        }
+        else return false;
+        break;
+      case TIMER4B:
+        if (TCCR4A & (_BV(COM4B1) | _BV(COM4B0))){
+          sprintf(buffer, "PWM:  %4d",OCR4B); 
+          SERIAL_ECHO(buffer);
+          return true;
+        }
+        else return false;
+        break;
+      case TIMER4C:
+        if (TCCR4A & (_BV(COM4C1) | _BV(COM4C0))){
+          sprintf(buffer, "PWM:  %4d",OCR4C); 
+          SERIAL_ECHO(buffer);
+          return true;
+        }
+        else return false;
+        break;
+    #endif
+          
+    #if defined(TCCR5A) && defined(COM5A1)
+      case TIMER5A:
+        if (TCCR5A & (_BV(COM5A1) | _BV(COM5A0))){
+          sprintf(buffer, "PWM:  %4d",OCR5A); 
+          SERIAL_ECHO(buffer);
+          return true;
+        }
+        else return false;
+        break;
+      case TIMER5B:
+        if (TCCR5A & (_BV(COM5B1) | _BV(COM5B0))){
+          sprintf(buffer, "PWM:  %4d",OCR5B); 
+          SERIAL_ECHO(buffer);
+          return true;
+        }
+        else return false;
+        break;
+      case TIMER5C:
+        if (TCCR5A & (_BV(COM5C1) | _BV(COM5C0))){
+          sprintf(buffer, "PWM:  %4d",OCR5C); 
+          SERIAL_ECHO(buffer);
+          return true;
+        }
+        else return false;
+        break;
+    #endif
+
+    case NOT_ON_TIMER:
+      return false;
+      break;
+     
+    default:
+      return false;
+
+  }
+  
+  SERIAL_PROTOCOLPGM("  ");   
+}  //PWM_status
+
+static void PWM_details(uint8_t pin)
+{
+
+  uint8_t  WGM;
+
+  switch(digitalPinToTimer(pin)) {
+ 
+	#if defined(TCCR0A) && defined(COM0A1)
+    case TIMER0A:
+      SERIAL_PROTOCOLPGM("    TIMER0A");
+      WGM = ((TCCR0B & _BV(WGM02)) >> 1 ) | (TCCR0A & (_BV(WGM00) | _BV(WGM01) ));
+      SERIAL_PROTOCOLPAIR("    WGM: ", WGM);
+      SERIAL_PROTOCOLPAIR("    TIMSK0: ", TIMSK0);
+      if (WGM == 0 || WGM == 2 || WGM == 4 || WGM == 6) SERIAL_PROTOCOLPGM("   Can't be used as a PWM because of counter mode");
+      else if (TIMSK0 & _BV(OCIE0A))  SERIAL_PROTOCOLPGM("   Can't be used as a PWM because being used to generate an interrupt");
+      else if (TIMSK0 & _BV(TOIE0) )  SERIAL_PROTOCOLPGM("   Probably can't be used as a PWM because counter/timer being used to generate an interrupt");
+      else SERIAL_PROTOCOLPGM("   can be used as PWM   ");
+      break;         
+    case TIMER0B:
+      SERIAL_PROTOCOLPGM("    TIMER0B");
+      WGM = ((TCCR0B & _BV(WGM02)) >> 1 ) | (TCCR0A & (_BV(WGM00) | _BV(WGM01) ));
+      SERIAL_PROTOCOLPAIR("    WGM: ", WGM);
+      SERIAL_PROTOCOLPAIR("    TIMSK0: ", TIMSK0);
+      if (WGM == 0 || WGM == 2 || WGM == 4 || WGM == 6) SERIAL_PROTOCOLPGM("   Can't be used as a PWM because of counter mode");
+      else if (TIMSK0 & _BV(OCIE0B))  SERIAL_PROTOCOLPGM("   Can't be used as a PWM because being used to generate an interrupt");
+      else if (TIMSK0 & _BV(TOIE0) )  SERIAL_PROTOCOLPGM("   Probably can't be used as a PWM because counter/timer being used to generate an interrupt");
+      else SERIAL_PROTOCOLPGM("   can be used as PWM   ");
+      break;
+  #endif
+
+  #if defined(TCCR1A) && defined(COM1A1)
+    case TIMER1A:
+      SERIAL_PROTOCOLPGM("    TIMER1A");
+      WGM =   ((TCCR1B & (_BV(WGM12) | _BV(WGM13) )) >> 1 ) | (TCCR1A & (_BV(WGM10) | _BV(WGM11) ));   
+      SERIAL_PROTOCOLPAIR("    WGM: ", WGM);
+      SERIAL_PROTOCOLPAIR("    TIMSK1: ", TIMSK1);
+      if (WGM == 0 || WGM == 4 || WGM == 12 || WGM == 13)   SERIAL_PROTOCOLPGM("   Can't be used as a PWM because of counter mode");
+      else if (TIMSK1 &  _BV(OCIE1A))  SERIAL_PROTOCOLPGM("   Can't be used as a PWM because being used to generate an interrupt");
+      else if (TIMSK1 & (_BV(TOIE1) | _BV(ICIE1)) )  SERIAL_PROTOCOLPGM("   Probably can't be used as a PWM because counter/timer being used to generate an interrupt");
+      else SERIAL_PROTOCOLPGM("   can be used as PWM   ");
+      break;
+    case TIMER1B:
+      SERIAL_PROTOCOLPGM("    TIMER1B");
+      WGM =   ((TCCR1B & (_BV(WGM12) | _BV(WGM13) )) >> 1 ) | (TCCR1A & (_BV(WGM10) | _BV(WGM11) ));   
+      SERIAL_PROTOCOLPAIR("    WGM: ", WGM);
+      SERIAL_PROTOCOLPAIR("    TIMSK1: ", TIMSK1);
+      if (WGM == 0 || WGM == 4 || WGM == 12 || WGM == 13)   SERIAL_PROTOCOLPGM("   Can't be used as a PWM because of counter mode");
+      else if (TIMSK1 &  _BV(OCIE1B))  SERIAL_PROTOCOLPGM("   Can't be used as a PWM because being used to generate an interrupt");
+      else if (TIMSK1 & (_BV(TOIE1) | _BV(ICIE1)) )  SERIAL_PROTOCOLPGM("   Probably can't be used as a PWM because counter/timer being used to generate an interrupt");
+      else SERIAL_PROTOCOLPGM("   can be used as PWM   ");          
+      break;
+    case TIMER1C:
+      SERIAL_PROTOCOLPGM("    TIMER1C");
+      WGM =   ((TCCR1B & (_BV(WGM12) | _BV(WGM13) )) >> 1 ) | (TCCR1A & (_BV(WGM10) | _BV(WGM11) ));   
+      SERIAL_PROTOCOLPAIR("    WGM: ", WGM);
+      SERIAL_PROTOCOLPAIR("    TIMSK1: ", TIMSK1);
+      if (WGM == 0 || WGM == 4 || WGM == 12 || WGM == 13)   SERIAL_PROTOCOLPGM("   Can't be used as a PWM because of counter mode");
+      else if (TIMSK1 &  _BV(OCIE1C))  SERIAL_PROTOCOLPGM("   Can't be used as a PWM because being used to generate an interrupt");
+      else if (TIMSK1 & (_BV(TOIE1) | _BV(ICIE1)) )  SERIAL_PROTOCOLPGM("   Probably can't be used as a PWM because counter/timer being used to generate an interrupt");
+      else SERIAL_PROTOCOLPGM("   can be used as PWM   ");
+      break;
+  #endif
+
+  #if defined(TCCR2A) && defined(COM2A1)
+    case TIMER2A:
+      SERIAL_PROTOCOLPGM("    TIMER2A");
+      WGM =   ((TCCR2B & _BV(WGM22) ) >> 1 ) | (TCCR2A & (_BV(WGM20) | _BV(WGM21) ));
+      SERIAL_PROTOCOLPAIR("    WGM: ", WGM);
+      SERIAL_PROTOCOLPAIR("    TIMSK2: ", TIMSK2);
+      if (WGM == 0 || WGM == 2 || WGM == 4 || WGM == 6) SERIAL_PROTOCOLPGM("   Can't be used as a PWM because of counter mode");
+      else if (TIMSK2 & (_BV(TOIE2) | _BV(OCIE2A)))  SERIAL_PROTOCOLPGM("   Can't be used as a PWM because being used to generate an interrupt");
+      else if (TIMSK2 & _BV(TOIE2) )  SERIAL_PROTOCOLPGM("   Probably can't be used as a PWM because counter/timer being used to generate an interrupt");
+      else SERIAL_PROTOCOLPGM("   can be used as PWM   ");
+      break;
+    case TIMER2B:
+      SERIAL_PROTOCOLPGM("    TIMER2B");
+      WGM =   ((TCCR2B & _BV(WGM22) ) >> 1 ) | (TCCR2A & (_BV(WGM20) | _BV(WGM21) ));
+      SERIAL_PROTOCOLPAIR("    WGM: ", WGM);
+      SERIAL_PROTOCOLPAIR("    TIMSK2: ", TIMSK2);
+      if (WGM == 0 || WGM == 2 || WGM == 4 || WGM == 6) SERIAL_PROTOCOLPGM("   Can't be used as a PWM because of counter mode");
+      else if (TIMSK2 & _BV(OCIE2B))  SERIAL_PROTOCOLPGM("   Can't be used as a PWM because being used to generate an interrupt");
+      else if (TIMSK2 & _BV(TOIE2) )  SERIAL_PROTOCOLPGM("   Probably can't be used as a PWM because counter/timer being used to generate an interrupt");
+      else SERIAL_PROTOCOLPGM("   can be used as PWM   ");
+      break; 
+  #endif
+
+  #if defined(TCCR3A) && defined(COM3A1)
+    case TIMER3A:
+      SERIAL_PROTOCOLPGM("    TIMER3A");
+      WGM =   ((TCCR3B & _BV(WGM32) ) >> 1 ) | (TCCR3A & (_BV(WGM30) | _BV(WGM31) ));
+      SERIAL_PROTOCOLPAIR("    WGM: ", WGM);
+      SERIAL_PROTOCOLPAIR("    TIMSK3: ", TIMSK3);
+      if (WGM == 0 || WGM == 4 || WGM == 12 || WGM == 13)   SERIAL_PROTOCOLPGM("   Can't be used as a PWM because of counter mode");
+      else if (TIMSK3 &  _BV(OCIE3A))  SERIAL_PROTOCOLPGM("   Can't be used as a PWM because being used to generate an interrupt");
+      else if (TIMSK3 & (_BV(TOIE3) | _BV(ICIE3)) )  SERIAL_PROTOCOLPGM("   Probably can't be used as a PWM because counter/timer being used to generate an interrupt");
+      else SERIAL_PROTOCOLPGM("   can be used as PWM   ");
+      break;      
+    case TIMER3B:
+      SERIAL_PROTOCOLPGM("    TIMER3B");
+      WGM =   ((TCCR3B & _BV(WGM32) ) >> 1 ) | (TCCR3A & (_BV(WGM30) | _BV(WGM31) ));
+      SERIAL_PROTOCOLPAIR("    WGM: ", WGM);
+      SERIAL_PROTOCOLPAIR("    TIMSK3: ", TIMSK3);
+      if (WGM == 0 || WGM == 4 || WGM == 12 || WGM == 13)   SERIAL_PROTOCOLPGM("   Can't be used as a PWM because of counter mode");
+      else if (TIMSK3 &  _BV(OCIE3B))  SERIAL_PROTOCOLPGM("   Can't be used as a PWM because being used to generate an interrupt");
+      else if (TIMSK3 & (_BV(TOIE3) | _BV(ICIE3)) )  SERIAL_PROTOCOLPGM("   Probably can't be used as a PWM because counter/timer being used to generate an interrupt");
+      else SERIAL_PROTOCOLPGM("   can be used as PWM   ");
+      break;
+    case TIMER3C:
+      SERIAL_PROTOCOLPGM("    TIMER3C");
+      WGM =   ((TCCR3B & _BV(WGM32) ) >> 1 ) | (TCCR3A & (_BV(WGM30) | _BV(WGM31) ));
+      SERIAL_PROTOCOLPAIR("    WGM: ", WGM);
+      SERIAL_PROTOCOLPAIR("    TIMSK3: ", TIMSK3);
+      if (WGM == 0 || WGM == 4 || WGM == 12 || WGM == 13)   SERIAL_PROTOCOLPGM("   Can't be used as a PWM because of counter mode");
+      else if (TIMSK3 &  _BV(OCIE3C))  SERIAL_PROTOCOLPGM("   Can't be used as a PWM because being used to generate an interrupt");
+      else if (TIMSK3 & (_BV(TOIE3) | _BV(ICIE3)) )  SERIAL_PROTOCOLPGM("   Probably can't be used as a PWM because counter/timer being used to generate an interrupt");
+      else SERIAL_PROTOCOLPGM("   can be used as PWM   ");
+      break;
+  #endif
+
+  #if defined(TCCR4A)
+    case TIMER4A:
+      SERIAL_PROTOCOLPGM("    TIMER4A");
+      WGM =   ((TCCR4B & (_BV(WGM42) | _BV(WGM43) )) >> 1 ) | (TCCR4A & (_BV(WGM40) | _BV(WGM41) ));
+      SERIAL_PROTOCOLPAIR("    WGM: ", WGM);
+      SERIAL_PROTOCOLPAIR("    TIMSK4: ", TIMSK4);
+      if (WGM == 0 || WGM == 4 || WGM == 12 || WGM == 13)   SERIAL_PROTOCOLPGM("   Can't be used as a PWM because of counter mode");
+      else if (TIMSK4 &  _BV(OCIE4A))  SERIAL_PROTOCOLPGM("   Can't be used as a PWM because being used to generate an interrupt");
+      else if (TIMSK4 & (_BV(TOIE4) | _BV(ICIE4)) )  SERIAL_PROTOCOLPGM("   Probably can't be used as a PWM because counter/timer being used to generate an interrupt");
+      else SERIAL_PROTOCOLPGM("   can be used as PWM   ");
+      break;      
+    case TIMER4B:
+      SERIAL_PROTOCOLPGM("    TIMER4B");
+      WGM =   ((TCCR4B & (_BV(WGM42) | _BV(WGM43) )) >> 1 ) | (TCCR4A & (_BV(WGM40) | _BV(WGM41) ));
+      SERIAL_PROTOCOLPAIR("    WGM: ", WGM);
+      SERIAL_PROTOCOLPAIR("    TIMSK4: ", TIMSK4);
+      if (WGM == 0 || WGM == 4 || WGM == 12 || WGM == 13)   SERIAL_PROTOCOLPGM("   Can't be used as a PWM because of counter mode");
+      else if (TIMSK4 &  _BV(OCIE4B))  SERIAL_PROTOCOLPGM("   Can't be used as a PWM because being used to generate an interrupt");
+      else if (TIMSK4 & (_BV(TOIE4) | _BV(ICIE4)) )  SERIAL_PROTOCOLPGM("   Probably can't be used as a PWM because counter/timer being used to generate an interrupt");
+      else SERIAL_PROTOCOLPGM("   can be used as PWM   ");
+      break;      
+    case TIMER4C:
+      SERIAL_PROTOCOLPGM("    TIMER4C");
+      WGM =   ((TCCR4B & (_BV(WGM42) | _BV(WGM43) )) >> 1 ) | (TCCR4A & (_BV(WGM40) | _BV(WGM41) ));
+      SERIAL_PROTOCOLPAIR("    WGM: ", WGM);
+      SERIAL_PROTOCOLPAIR("    TIMSK4: ", TIMSK4);
+      if (WGM == 0 || WGM == 4 || WGM == 12 || WGM == 13)   SERIAL_PROTOCOLPGM("   Can't be used as a PWM because of counter mode");
+      else if (TIMSK4 &  _BV(OCIE4C))  SERIAL_PROTOCOLPGM("   Can't be used as a PWM because being used to generate an interrupt");
+      else if (TIMSK4 & (_BV(TOIE4) | _BV(ICIE4)) )  SERIAL_PROTOCOLPGM("   Probably can't be used as a PWM because counter/timer being used to generate an interrupt");
+      else SERIAL_PROTOCOLPGM("   can be used as PWM   ");
+      break;      
+
+  #endif
+          
+  #if defined(TCCR5A) && defined(COM5A1)
+    case TIMER5A:
+      SERIAL_PROTOCOLPGM("    TIMER5A");
+      WGM =   ((TCCR5B & (_BV(WGM52) | _BV(WGM53) )) >> 1 ) | (TCCR5A & (_BV(WGM50) | _BV(WGM51) ));
+      SERIAL_PROTOCOLPAIR("    WGM: ", WGM);
+      SERIAL_PROTOCOLPAIR("    TIMSK5: ", TIMSK5);
+      if (WGM == 0 || WGM == 4 || WGM == 12 || WGM == 13)   SERIAL_PROTOCOLPGM("   Can't be used as a PWM because of counter mode");
+      else if (TIMSK5 &  _BV(OCIE5A))  SERIAL_PROTOCOLPGM("   Can't be used as a PWM because being used to generate an interrupt");
+      else if (TIMSK5 & (_BV(TOIE5) | _BV(ICIE5)) )  SERIAL_PROTOCOLPGM("   Probably can't be used as a PWM because counter/timer being used to generate an interrupt");
+      else SERIAL_PROTOCOLPGM("   can be used as PWM   ");
+      break;      
+    case TIMER5B:
+      SERIAL_PROTOCOLPGM("    TIMER5B");
+      WGM =   ((TCCR5B & (_BV(WGM52) | _BV(WGM53) )) >> 1 ) | (TCCR5A & (_BV(WGM50) | _BV(WGM51) ));
+      SERIAL_PROTOCOLPAIR("    WGM: ", WGM);
+      SERIAL_PROTOCOLPAIR("    TIMSK5: ", TIMSK5);
+      if (WGM == 0 || WGM == 4 || WGM == 12 || WGM == 13)   SERIAL_PROTOCOLPGM("   Can't be used as a PWM because of counter mode");
+      else if (TIMSK5 &  _BV(OCIE5B))  SERIAL_PROTOCOLPGM("   Can't be used as a PWM because being used to generate an interrupt");
+      else if (TIMSK5 & (_BV(TOIE5) | _BV(ICIE5)) )  SERIAL_PROTOCOLPGM("   Probably can't be used as a PWM because counter/timer being used to generate an interrupt");
+      else SERIAL_PROTOCOLPGM("   can be used as PWM   ");
+      break;      
+    case TIMER5C:
+      SERIAL_PROTOCOLPGM("    TIMER5C");
+      WGM =   ((TCCR5B & (_BV(WGM52) | _BV(WGM53) )) >> 1 ) | (TCCR5A & (_BV(WGM50) | _BV(WGM51) ));
+      SERIAL_PROTOCOLPAIR("    WGM: ", WGM);
+      SERIAL_PROTOCOLPAIR("    TIMSK5: ", TIMSK5);
+      if (WGM == 0 || WGM == 4 || WGM == 12 || WGM == 13)   SERIAL_PROTOCOLPGM("   Can't be used as a PWM because of counter mode");
+      else if (TIMSK5 &  _BV(OCIE5C))  SERIAL_PROTOCOLPGM("   Can't be used as a PWM because being used to generate an interrupt");
+      else if (TIMSK5 & (_BV(TOIE5) | _BV(ICIE5)) )  SERIAL_PROTOCOLPGM("   Probably can't be used as a PWM because counter/timer being used to generate an interrupt");
+      else SERIAL_PROTOCOLPGM("   can be used as PWM   ");
+      break;      
+  #endif
+
+	case NOT_ON_TIMER:
+    break;
+	}
+  SERIAL_PROTOCOLPGM("  ");   
+}  // PWM_details
+
+
 
 inline void report_pin_state(int8_t pin) {
-  if (report_pin_name(pin)) {
+  SERIAL_ECHO((int)pin);
+  SERIAL_CHAR(' ');
+  bool dummy;
+  if (report_pin_name(pin, dummy)) {
     if (pin_is_protected(pin))
       SERIAL_ECHOPGM(" (protected)");
     else {
@@ -445,3 +1068,40 @@ inline void report_pin_state(int8_t pin) {
   }
   SERIAL_EOL;
 }
+
+// pretty report with PWM info
+inline void report_pin_state_extended(int8_t pin, bool ignore) {
+
+  char buffer[30];   // for the sprintf statements
+    
+ // report pin number 
+  sprintf(buffer, "PIN:% 3d ", pin);
+  SERIAL_ECHO(buffer);   
+
+ // report pin name 
+  bool analog_pin;
+  report_pin_name(pin, analog_pin); 
+  
+// report pin state
+  if (pin_is_protected(pin) && ignore == false) 
+    SERIAL_ECHOPGM("protected ");
+  else {
+    if (analog_pin) {
+        sprintf(buffer, "Analog in =% 5d", analogRead(pin - analogInputToDigitalPin(0)));
+        SERIAL_ECHO(buffer); 
+       }
+    else {
+      if (!get_pinMode(pin)) {
+        pinMode(pin, INPUT_PULLUP);  // make sure input isn't floating
+        SERIAL_PROTOCOLPAIR("Input  = ", digitalRead_mod(pin));
+      }  
+      else if  (PWM_status(pin)) ;
+      else SERIAL_PROTOCOLPAIR("Output = ", digitalRead_mod(pin));  
+    }
+  }
+
+// report PWM capabilities
+  PWM_details(pin);
+  SERIAL_EOL;
+}
+
diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp
index b1f97eb61dc..a3c3c9c8491 100644
--- a/Marlin/temperature.cpp
+++ b/Marlin/temperature.cpp
@@ -462,12 +462,12 @@ int Temperature::getHeaterPower(int heater) {
       AUTO_3_IS_0 ? 0 : AUTO_3_IS_1 ? 1 : AUTO_3_IS_2 ? 2 : 3
     };
     uint8_t fanState = 0;
- 
+
     HOTEND_LOOP() {
       if (current_temperature[e] > EXTRUDER_AUTO_FAN_TEMPERATURE)
         SBI(fanState, fanBit[e]);
     }
- 
+
     uint8_t fanDone = 0;
     for (uint8_t f = 0; f < COUNT(fanPin); f++) {
       int8_t pin = fanPin[f];
@@ -1393,6 +1393,87 @@ void Temperature::set_current_temp_raw() {
   temp_meas_ready = true;
 }
 
+#if ENABLED(PINS_DEBUGGING)
+  /**
+   * monitors endstops & Z probe for changes
+   *
+   * If a change is detected then the LED is toggled and
+   * a message is sent out the serial port
+   *
+   * Yes, we could miss a rapid back & forth change but
+   * that won't matter because this is all manual.
+   *
+   */
+  void endstop_monitor() {
+    static uint16_t old_endstop_bits_local = 0;
+    static uint8_t local_LED_status = 0;
+    if (endstop_monitor_flag) {
+      uint16_t current_endstop_bits_local = 0;
+      #if HAS_X_MIN
+        if (READ(X_MIN_PIN)) current_endstop_bits_local |= _BV(X_MIN);
+        if ((current_endstop_bits_local ^ old_endstop_bits_local) & _BV(X_MIN)) {
+          SERIAL_PROTOCOLPAIR("X_MIN: ", (current_endstop_bits_local & _BV(X_MIN)) ? 1 : 0);
+        }
+      #endif
+      #if HAS_X_MAX
+        if (READ(X_MAX_PIN)) current_endstop_bits_local |= _BV(X_MAX);
+        if ((current_endstop_bits_local ^ old_endstop_bits_local) & _BV(X_MAX)) {
+          SERIAL_PROTOCOLPAIR("   X_MAX: ", (current_endstop_bits_local & _BV(X_MAX)) ? 1 : 0);
+        }
+      #endif
+      #if HAS_Y_MIN
+        if (READ(Y_MIN_PIN)) current_endstop_bits_local |= _BV(Y_MIN);
+        if ((current_endstop_bits_local ^ old_endstop_bits_local) & _BV(Y_MIN)) {
+          SERIAL_PROTOCOLPAIR("   Y_MIN: ", (current_endstop_bits_local & _BV(Y_MIN)) ? 1 : 0);
+        }
+      #endif
+      #if HAS_Y_MAX
+        if (READ(Y_MAX_PIN)) current_endstop_bits_local |= _BV(Y_MAX);
+        if ((current_endstop_bits_local ^ old_endstop_bits_local) & _BV(Y_MAX)) {
+          SERIAL_PROTOCOLPAIR("   Y_MAX: ", (current_endstop_bits_local & _BV(Y_MAX)) ? 1 : 0);
+        }
+      #endif
+      #if HAS_Z_MIN
+        if (READ(Z_MIN_PIN)) current_endstop_bits_local |= _BV(Z_MIN);
+        if ((current_endstop_bits_local ^ old_endstop_bits_local) & _BV(Z_MIN)) {
+          SERIAL_PROTOCOLPAIR("   Z_MIN: ", (current_endstop_bits_local & _BV(Z_MIN)) ? 1 : 0);
+        }
+      #endif
+      #if HAS_Z_MAX
+        if (READ(Z_MAX_PIN)) current_endstop_bits_local |= _BV(Z_MAX);
+        if ((current_endstop_bits_local ^ old_endstop_bits_local) & _BV(Z_MAX)) {
+          SERIAL_PROTOCOLPAIR("   Z_MAX: ", (current_endstop_bits_local & _BV(Z_MAX)) ? 1 : 0);
+        }
+      #endif
+      #if HAS_Z_MIN_PROBE_PIN
+        if (READ(Z_MIN_PROBE_PIN)) current_endstop_bits_local |= _BV(Z_MIN_PROBE);
+        if ((current_endstop_bits_local ^ old_endstop_bits_local) & _BV(Z_MIN_PROBE)) {
+          SERIAL_PROTOCOLPAIR("   PROBE: ", (current_endstop_bits_local & _BV(Z_MIN_PROBE)) ? 1 : 0);
+        }
+      #endif
+      #if HAS_Z2_MIN
+        if (READ(Z2_MIN_PIN)) current_endstop_bits_local |= _BV(Z2_MIN);
+        if ((current_endstop_bits_local ^ old_endstop_bits_local) & _BV(Z2_MIN)) {
+          SERIAL_PROTOCOLPAIR("   Z2_MIN: ", (current_endstop_bits_local & _BV(Z2_MIN)) ? 1 : 0);
+        }
+      #endif
+      #if HAS_Z2_MAX
+        if (READ(Z2_MAX_PIN)) current_endstop_bits_local |= _BV(Z2_MAX);
+        if ((current_endstop_bits_local ^ old_endstop_bits_local) & _BV(Z2_MAX)) {
+          SERIAL_PROTOCOLPAIR("   Z2_MAX: ", (current_endstop_bits_local & _BV(Z2_MAX)) ? 1 : 0);
+        }
+      #endif
+
+      if (current_endstop_bits_local != old_endstop_bits_local) {
+        analogWrite(LED_PIN, local_LED_status );                // toggle LED
+        SERIAL_PROTOCOLPGM("\n\n");                             // make it easy to see the message
+        old_endstop_bits_local = current_endstop_bits_local ;   // get ready for next change
+        local_LED_status  = local_LED_status ? 0 : 255;
+      }
+    }
+  }
+#endif // PINS_DEBUGGING
+
 /**
  * Timer 0 is shared with millies so don't change the prescaler.
  *
@@ -1848,4 +1929,12 @@ void Temperature::isr() {
       }
     }
   #endif //BABYSTEPPING
+  #if ENABLED(PINS_DEBUGGING)
+    extern bool endstop_monitor_flag;
+    // run the endstop monitor at 15Hz
+    static uint8_t endstop_monitor_count = 16;  // offset this check from the others
+    endstop_monitor_count += _BV(1);  //  15 Hz
+    endstop_monitor_count &= 0x7F;
+    if (endstop_monitor_count == 0) endstop_monitor();  // report changes in endstop status
+  #endif
 }

From 81397d7a9a00166bb1f04bd210123ebdda67332d Mon Sep 17 00:00:00 2001
From: Bob-the-Kuhn <bob.kuhn@att.net>
Date: Sat, 29 Oct 2016 16:18:28 -0500
Subject: [PATCH 2/9] EXTRUDER_x_AUTO_FAN name change to Ex_AUTO_FAN

I stumbled across a name change that'll be used a lot so I'm updateing
the name list.
---
 Marlin/pinsDebug.h | 25 +++++++++++++------------
 1 file changed, 13 insertions(+), 12 deletions(-)

diff --git a/Marlin/pinsDebug.h b/Marlin/pinsDebug.h
index d22d79ae90a..ec455cfca3f 100644
--- a/Marlin/pinsDebug.h
+++ b/Marlin/pinsDebug.h
@@ -191,6 +191,18 @@ static bool report_pin_name(int8_t pin,bool &pin_is_analog) {
   #if PIN_EXISTS(E0_ATT)
     PIN_SAY(E0_ATT_PIN);
   #endif
+  #if PIN_EXISTS(E0_AUTO_FAN)
+    PIN_SAY(E0_AUTO_FAN_PIN);
+  #endif
+  #if PIN_EXISTS(E1_AUTO_FAN)
+    PIN_SAY(E1_AUTO_FAN_PIN);
+  #endif
+  #if PIN_EXISTS(E2_AUTO_FAN)
+    PIN_SAY(E2_AUTO_FAN_PIN);
+  #endif
+  #if PIN_EXISTS(E3_AUTO_FAN)
+    PIN_SAY(E3_AUTO_FAN_PIN);
+  #endif
   #if PIN_EXISTS(E0_DIR)
     PIN_SAY(E0_DIR_PIN);
   #endif
@@ -296,18 +308,7 @@ static bool report_pin_name(int8_t pin,bool &pin_is_analog) {
   #if defined(EXT_AUX_TX1_D3) && EXT_AUX_TX1_D3 > -1
     PIN_SAY(EXT_AUX_TX1_D3);
   #endif
-  #if PIN_EXISTS(EXTRUDER_0_AUTO_FAN)
-    PIN_SAY(EXTRUDER_0_AUTO_FAN_PIN);
-  #endif
-  #if PIN_EXISTS(EXTRUDER_1_AUTO_FAN)
-    PIN_SAY(EXTRUDER_1_AUTO_FAN_PIN);
-  #endif
-  #if PIN_EXISTS(EXTRUDER_2_AUTO_FAN)
-    PIN_SAY(EXTRUDER_2_AUTO_FAN_PIN);
-  #endif
-  #if PIN_EXISTS(EXTRUDER_3_AUTO_FAN)
-    PIN_SAY(EXTRUDER_3_AUTO_FAN_PIN);
-  #endif
+
   #if PIN_EXISTS(FAN)
     PIN_SAY(FAN_PIN);
   #endif

From af26d227413d574df18e7fead29a4c6c86b970c0 Mon Sep 17 00:00:00 2001
From: Scott Lahteine <sourcetree@thinkyhead.com>
Date: Sun, 30 Oct 2016 19:12:51 -0500
Subject: [PATCH 3/9] Clean up some spacing and semantics

---
 Marlin/pinsDebug.h | 224 +++++++++++++++++++++------------------------
 1 file changed, 106 insertions(+), 118 deletions(-)

diff --git a/Marlin/pinsDebug.h b/Marlin/pinsDebug.h
index ec455cfca3f..e0bf9406614 100644
--- a/Marlin/pinsDebug.h
+++ b/Marlin/pinsDebug.h
@@ -21,7 +21,7 @@
  */
 
 // How many DIO pins are defined?
-#if defined(DIO85_PIN)
+#ifdef DIO85_PIN
 //  #define DIO_COUNT 86
   #define DIO_COUNT 70  // digitalRead and other Arduino IDE routines only know about pins 0 through 69
 #elif defined(DIO53_PIN)
@@ -42,68 +42,52 @@ bool endstop_monitor_flag = false;
 #define _PIN_SAY(NAME) { sprintf(buffer, NAME_FORMAT, NAME); SERIAL_ECHO(buffer); return true; }
 #define PIN_SAY(NAME) if (pin == NAME) _PIN_SAY(#NAME);
 
-#define _ANALOG_PIN_SAY(NAME)   { sprintf(buffer, NAME_FORMAT, NAME); SERIAL_ECHO(buffer); pin_is_analog = true; return true; }
+#define _ANALOG_PIN_SAY(NAME) { sprintf(buffer, NAME_FORMAT, NAME); SERIAL_ECHO(buffer); pin_is_analog = true; return true; }
 #define ANALOG_PIN_SAY(NAME) if (pin == analogInputToDigitalPin(NAME)) _ANALOG_PIN_SAY(#NAME);
 
 #define IS_ANALOG(P) ((P) >= analogInputToDigitalPin(0) && ((P) <= analogInputToDigitalPin(15) || (P) <= analogInputToDigitalPin(5)))
 
-
-
-int digitalRead_mod(int8_t pin)  // same as digitalRead except the PWM stop section has been removed
-{
-	uint8_t bit = digitalPinToBitMask(pin);
+int digitalRead_mod(int8_t pin) { // same as digitalRead except the PWM stop section has been removed
 	uint8_t port = digitalPinToPort(pin);
-  
-	if (port == NOT_A_PIN) return LOW;
-
-	if (*portInputRegister(port) & bit) return HIGH;
-	return LOW;
+	return (port != NOT_A_PIN) && (*portInputRegister(port) & digitalPinToBitMask(pin)) ? HIGH : LOW;
 }
 
-bool get_pinMode(int8_t pin)
-{
-	uint8_t bit = digitalPinToBitMask(pin);
-	uint8_t port = digitalPinToPort(pin);
-	volatile uint8_t *reg;
-	reg = portModeRegister(port);
-  return   *reg & bit;
-}
+/**
+ * Report pin name for a given fastio digital pin index
+ */
+static bool report_pin_name(int8_t pin, bool &pin_is_analog) {
 
-// Report pin name for a given fastio digital pin index
-
-static bool report_pin_name(int8_t pin,bool &pin_is_analog) {
-  
   char buffer[30];   // for the sprintf statements
   pin_is_analog = false;   // default to digital pin
-  
+
   if (IS_ANALOG(pin)) {
     sprintf(buffer, "(A%2d)  ", int(pin - analogInputToDigitalPin(0)));
-    SERIAL_ECHO(buffer);   
+    SERIAL_ECHO(buffer);
   }
   else SERIAL_ECHOPGM("       ");
 
-  #if defined(RXD) && RXD > -1
+  #if defined(RXD) && RXD >= 0
     if (pin == 0) { sprintf(buffer, NAME_FORMAT, "RXD"); SERIAL_ECHO(buffer); return true; }
   #endif
-  #if defined(TXD) && TXD > -1
+
+  #if defined(TXD) && TXD >= 0
     if (pin == 1) { sprintf(buffer, NAME_FORMAT, "TXD"); SERIAL_ECHO(buffer); return true; }
   #endif
 
-  
   // Pin list updated from 7 OCT RCBugfix branch
-  #if defined(__FD) && __FD > -1   
+  #if defined(__FD) && __FD >= 0
     PIN_SAY(__FD)
   #endif
-  #if defined(__FS) && __FS > -1
+  #if defined(__FS) && __FS >= 0
     PIN_SAY(__FS)
   #endif
-  #if defined(__GD) && __GD > -1
+  #if defined(__GD) && __GD >= 0
     PIN_SAY(__GD)
   #endif
-  #if defined(__GS) && __GS > -1
+  #if defined(__GS) && __GS >= 0
     PIN_SAY(__GS)
   #endif
- 
+
   #if PIN_EXISTS(AVR_MISO)
     PIN_SAY(AVR_MISO_PIN);
   #endif
@@ -119,40 +103,40 @@ static bool report_pin_name(int8_t pin,bool &pin_is_analog) {
   #if PIN_EXISTS(BEEPER)
     PIN_SAY(BEEPER_PIN);
   #endif
-  #if defined(BTN_CENTER) && BTN_CENTER > -1
+  #if defined(BTN_CENTER) && BTN_CENTER >= 0
     PIN_SAY(BTN_CENTER);
   #endif
-  #if defined(BTN_DOWN) && BTN_DOWN > -1
+  #if defined(BTN_DOWN) && BTN_DOWN >= 0
     PIN_SAY(BTN_DOWN);
   #endif
-  #if defined(BTN_DWN) && BTN_DWN > -1
+  #if defined(BTN_DWN) && BTN_DWN >= 0
     PIN_SAY(BTN_DWN);
   #endif
-  #if defined(BTN_EN1) && BTN_EN1 > -1
+  #if defined(BTN_EN1) && BTN_EN1 >= 0
     PIN_SAY(BTN_EN1);
   #endif
-  #if defined(BTN_EN2) && BTN_EN2 > -1
+  #if defined(BTN_EN2) && BTN_EN2 >= 0
     PIN_SAY(BTN_EN2);
   #endif
-  #if defined(BTN_ENC) && BTN_ENC > -1
+  #if defined(BTN_ENC) && BTN_ENC >= 0
     PIN_SAY(BTN_ENC);
   #endif
-  #if defined(BTN_HOME) && BTN_HOME > -1
+  #if defined(BTN_HOME) && BTN_HOME >= 0
     PIN_SAY(BTN_HOME);
   #endif
-  #if defined(BTN_LEFT) && BTN_LEFT > -1
+  #if defined(BTN_LEFT) && BTN_LEFT >= 0
     PIN_SAY(BTN_LEFT);
   #endif
-  #if defined(BTN_LFT) && BTN_LFT > -1
+  #if defined(BTN_LFT) && BTN_LFT >= 0
     PIN_SAY(BTN_LFT);
   #endif
-  #if defined(BTN_RIGHT) && BTN_RIGHT > -1
+  #if defined(BTN_RIGHT) && BTN_RIGHT >= 0
     PIN_SAY(BTN_RIGHT);
   #endif
-  #if defined(BTN_RT) && BTN_RT > -1
+  #if defined(BTN_RT) && BTN_RT >= 0
     PIN_SAY(BTN_RT);
   #endif
-  #if defined(BTN_UP) && BTN_UP > -1
+  #if defined(BTN_UP) && BTN_UP >= 0
     PIN_SAY(BTN_UP);
   #endif
   #if PIN_EXISTS(CONTROLLERFAN)
@@ -161,10 +145,10 @@ static bool report_pin_name(int8_t pin,bool &pin_is_analog) {
   #if PIN_EXISTS(DAC_DISABLE)
     PIN_SAY(DAC_DISABLE_PIN);
   #endif
-  #if defined(DAC_STEPPER_GAIN) && DAC_STEPPER_GAIN > -1
+  #if defined(DAC_STEPPER_GAIN) && DAC_STEPPER_GAIN >= 0
     PIN_SAY(DAC_STEPPER_GAIN);
   #endif
-  #if defined(DAC_STEPPER_VREF) && DAC_STEPPER_VREF > -1
+  #if defined(DAC_STEPPER_VREF) && DAC_STEPPER_VREF >= 0
     PIN_SAY(DAC_STEPPER_VREF);
   #endif
   #if PIN_EXISTS(DEBUG)
@@ -173,19 +157,19 @@ static bool report_pin_name(int8_t pin,bool &pin_is_analog) {
   #if PIN_EXISTS(DIGIPOTSS)
     PIN_SAY(DIGIPOTSS_PIN);
   #endif
-  #if defined(DIO_COUNT) && DIO_COUNT > -1
+  #if defined(DIO_COUNT) && DIO_COUNT >= 0
     PIN_SAY(DIO_COUNT);
   #endif
-  #if defined(DOGLCD_A0) && DOGLCD_A0 > -1
+  #if defined(DOGLCD_A0) && DOGLCD_A0 >= 0
     PIN_SAY(DOGLCD_A0);
   #endif
-  #if defined(DOGLCD_CS) && DOGLCD_CS > -1
+  #if defined(DOGLCD_CS) && DOGLCD_CS >= 0
     PIN_SAY(DOGLCD_CS);
   #endif
-  #if defined(DOGLCD_MOSI) && DOGLCD_MOSI > -1
+  #if defined(DOGLCD_MOSI) && DOGLCD_MOSI >= 0
     PIN_SAY(DOGLCD_MOSI);
   #endif
-  #if defined(DOGLCD_SCK) && DOGLCD_SCK > -1
+  #if defined(DOGLCD_SCK) && DOGLCD_SCK >= 0
     PIN_SAY(DOGLCD_SCK);
   #endif
   #if PIN_EXISTS(E0_ATT)
@@ -260,52 +244,52 @@ static bool report_pin_name(int8_t pin,bool &pin_is_analog) {
   #if PIN_EXISTS(E4_STEP)
     PIN_SAY(E4_STEP_PIN);
   #endif
-  #if defined(encrot1) && encrot1 > -1
+  #if defined(encrot1) && encrot1 >= 0
     PIN_SAY(encrot1);
   #endif
-  #if defined(encrot2) && encrot2 > -1
+  #if defined(encrot2) && encrot2 >= 0
     PIN_SAY(encrot2);
   #endif
-  #if defined(encrot3) && encrot3 > -1
+  #if defined(encrot3) && encrot3 >= 0
     PIN_SAY(encrot3);
   #endif
-  #if defined(EXT_AUX_A0_IO) && EXT_AUX_A0_IO > -1
+  #if defined(EXT_AUX_A0_IO) && EXT_AUX_A0_IO >= 0
     PIN_SAY(EXT_AUX_A0_IO);
   #endif
-  #if defined(EXT_AUX_A1) && EXT_AUX_A1 > -1
+  #if defined(EXT_AUX_A1) && EXT_AUX_A1 >= 0
     PIN_SAY(EXT_AUX_A1);
   #endif
-  #if defined(EXT_AUX_A1_IO) && EXT_AUX_A1_IO > -1
+  #if defined(EXT_AUX_A1_IO) && EXT_AUX_A1_IO >= 0
     PIN_SAY(EXT_AUX_A1_IO);
   #endif
-  #if defined(EXT_AUX_A2) && EXT_AUX_A2 > -1
+  #if defined(EXT_AUX_A2) && EXT_AUX_A2 >= 0
     PIN_SAY(EXT_AUX_A2);
   #endif
-  #if defined(EXT_AUX_A2_IO) && EXT_AUX_A2_IO > -1
+  #if defined(EXT_AUX_A2_IO) && EXT_AUX_A2_IO >= 0
     PIN_SAY(EXT_AUX_A2_IO);
   #endif
-  #if defined(EXT_AUX_A3) && EXT_AUX_A3 > -1
+  #if defined(EXT_AUX_A3) && EXT_AUX_A3 >= 0
     PIN_SAY(EXT_AUX_A3);
   #endif
-  #if defined(EXT_AUX_A3_IO) && EXT_AUX_A3_IO > -1
+  #if defined(EXT_AUX_A3_IO) && EXT_AUX_A3_IO >= 0
     PIN_SAY(EXT_AUX_A3_IO);
   #endif
-  #if defined(EXT_AUX_A4) && EXT_AUX_A4 > -1
+  #if defined(EXT_AUX_A4) && EXT_AUX_A4 >= 0
     PIN_SAY(EXT_AUX_A4);
   #endif
-  #if defined(EXT_AUX_A4_IO) && EXT_AUX_A4_IO > -1
+  #if defined(EXT_AUX_A4_IO) && EXT_AUX_A4_IO >= 0
     PIN_SAY(EXT_AUX_A4_IO);
   #endif
-  #if defined(EXT_AUX_PWM_D24) && EXT_AUX_PWM_D24 > -1
+  #if defined(EXT_AUX_PWM_D24) && EXT_AUX_PWM_D24 >= 0
     PIN_SAY(EXT_AUX_PWM_D24);
   #endif
-  #if defined(EXT_AUX_RX1_D2) && EXT_AUX_RX1_D2 > -1
+  #if defined(EXT_AUX_RX1_D2) && EXT_AUX_RX1_D2 >= 0
     PIN_SAY(EXT_AUX_RX1_D2);
   #endif
-  #if defined(EXT_AUX_SDA_D1) && EXT_AUX_SDA_D1 > -1
+  #if defined(EXT_AUX_SDA_D1) && EXT_AUX_SDA_D1 >= 0
     PIN_SAY(EXT_AUX_SDA_D1);
   #endif
-  #if defined(EXT_AUX_TX1_D3) && EXT_AUX_TX1_D3 > -1
+  #if defined(EXT_AUX_TX1_D3) && EXT_AUX_TX1_D3 >= 0
     PIN_SAY(EXT_AUX_TX1_D3);
   #endif
 
@@ -327,7 +311,7 @@ static bool report_pin_name(int8_t pin,bool &pin_is_analog) {
   #if PIN_EXISTS(FILWIDTH)
     ANALOG_PIN_SAY(FILWIDTH_PIN);
   #endif
-  #if defined(GEN7_VERSION) && GEN7_VERSION > -1
+  #if defined(GEN7_VERSION) && GEN7_VERSION >= 0
     PIN_SAY(GEN7_VERSION);
   #endif
   #if PIN_EXISTS(HEATER_0)
@@ -357,10 +341,10 @@ static bool report_pin_name(int8_t pin,bool &pin_is_analog) {
   #if PIN_EXISTS(HEATER_BED)
     PIN_SAY(HEATER_BED_PIN);
   #endif
-  #if defined(I2C_SCL) && I2C_SCL > -1
+  #if defined(I2C_SCL) && I2C_SCL >= 0
     PIN_SAY(I2C_SCL);
   #endif
-  #if defined(I2C_SDA) && I2C_SDA > -1
+  #if defined(I2C_SDA) && I2C_SDA >= 0
     PIN_SAY(I2C_SDA);
   #endif
   #if PIN_EXISTS(KILL)
@@ -369,28 +353,28 @@ static bool report_pin_name(int8_t pin,bool &pin_is_analog) {
   #if PIN_EXISTS(LCD_BACKLIGHT)
     PIN_SAY(LCD_BACKLIGHT_PIN);
   #endif
-  #if defined(LCD_CONTRAST) && LCD_CONTRAST > -1
+  #if defined(LCD_CONTRAST) && LCD_CONTRAST >= 0
     PIN_SAY(LCD_CONTRAST);
   #endif
-  #if defined(LCD_PINS_D4) && LCD_PINS_D4 > -1
+  #if defined(LCD_PINS_D4) && LCD_PINS_D4 >= 0
     PIN_SAY(LCD_PINS_D4);
   #endif
-  #if defined(LCD_PINS_D5) && LCD_PINS_D5 > -1
+  #if defined(LCD_PINS_D5) && LCD_PINS_D5 >= 0
     PIN_SAY(LCD_PINS_D5);
   #endif
-  #if defined(LCD_PINS_D6) && LCD_PINS_D6 > -1
+  #if defined(LCD_PINS_D6) && LCD_PINS_D6 >= 0
     PIN_SAY(LCD_PINS_D6);
   #endif
-  #if defined(LCD_PINS_D7) && LCD_PINS_D7 > -1
+  #if defined(LCD_PINS_D7) && LCD_PINS_D7 >= 0
     PIN_SAY(LCD_PINS_D7);
   #endif
-  #if defined(LCD_PINS_ENABLE) && LCD_PINS_ENABLE > -1
+  #if defined(LCD_PINS_ENABLE) && LCD_PINS_ENABLE >= 0
     PIN_SAY(LCD_PINS_ENABLE);
   #endif
-  #if defined(LCD_PINS_RS) && LCD_PINS_RS > -1
+  #if defined(LCD_PINS_RS) && LCD_PINS_RS >= 0
     PIN_SAY(LCD_PINS_RS);
   #endif
-  #if defined(LCD_SDSS) && LCD_SDSS > -1
+  #if defined(LCD_SDSS) && LCD_SDSS >= 0
     PIN_SAY(LCD_SDSS);
   #endif
   #if PIN_EXISTS(LED)
@@ -399,7 +383,7 @@ static bool report_pin_name(int8_t pin,bool &pin_is_analog) {
   #if PIN_EXISTS(MAIN_VOLTAGE_MEASURE)
     PIN_SAY(MAIN_VOLTAGE_MEASURE_PIN);
   #endif
-  #if defined(MAX6675_SS) && MAX6675_SS > -1
+  #if defined(MAX6675_SS) && MAX6675_SS >= 0
     PIN_SAY(MAX6675_SS);
   #endif
   #if PIN_EXISTS(MISO)
@@ -420,7 +404,7 @@ static bool report_pin_name(int8_t pin,bool &pin_is_analog) {
   #if PIN_EXISTS(MOTOR_CURRENT_PWM_Z)
     PIN_SAY(MOTOR_CURRENT_PWM_Z_PIN);
   #endif
-  #if defined(NUM_TLCS) && NUM_TLCS > -1
+  #if defined(NUM_TLCS) && NUM_TLCS >= 0
     PIN_SAY(NUM_TLCS);
   #endif
   #if PIN_EXISTS(PHOTOGRAPH)
@@ -447,19 +431,19 @@ static bool report_pin_name(int8_t pin,bool &pin_is_analog) {
   #if PIN_EXISTS(SCK)
     PIN_SAY(SCK_PIN);
   #endif
-  #if defined(SCL) && SCL > -1
+  #if defined(SCL) && SCL >= 0
     PIN_SAY(SCL);
   #endif
   #if PIN_EXISTS(SD_DETECT)
     PIN_SAY(SD_DETECT_PIN);
   #endif
-  #if defined(SDA) && SDA > -1
+  #if defined(SDA) && SDA >= 0
     PIN_SAY(SDA);
   #endif
-  #if defined(SDPOWER) && SDPOWER > -1
+  #if defined(SDPOWER) && SDPOWER >= 0
     PIN_SAY(SDPOWER);
   #endif
-  #if defined(SDSS) && SDSS > -1
+  #if defined(SDSS) && SDSS >= 0
     PIN_SAY(SDSS);
   #endif
   #if PIN_EXISTS(SERVO0)
@@ -474,16 +458,16 @@ static bool report_pin_name(int8_t pin,bool &pin_is_analog) {
   #if PIN_EXISTS(SERVO3)
     PIN_SAY(SERVO3_PIN);
   #endif
-  #if defined(SHIFT_CLK) && SHIFT_CLK > -1
+  #if defined(SHIFT_CLK) && SHIFT_CLK >= 0
     PIN_SAY(SHIFT_CLK);
   #endif
-  #if defined(SHIFT_EN) && SHIFT_EN > -1
+  #if defined(SHIFT_EN) && SHIFT_EN >= 0
     PIN_SAY(SHIFT_EN);
   #endif
-  #if defined(SHIFT_LD) && SHIFT_LD > -1
+  #if defined(SHIFT_LD) && SHIFT_LD >= 0
     PIN_SAY(SHIFT_LD);
   #endif
-  #if defined(SHIFT_OUT) && SHIFT_OUT > -1
+  #if defined(SHIFT_OUT) && SHIFT_OUT >= 0
     PIN_SAY(SHIFT_OUT);
   #endif
   #if PIN_EXISTS(SLED)
@@ -519,10 +503,10 @@ static bool report_pin_name(int8_t pin,bool &pin_is_analog) {
   #if PIN_EXISTS(SUICIDE)
     PIN_SAY(SUICIDE_PIN);
   #endif
-  #if defined(TC1) && TC1 > -1
+  #if defined(TC1) && TC1 >= 0
     ANALOG_PIN_SAY(TC1);
   #endif
-  #if defined(TC2) && TC2 > -1
+  #if defined(TC2) && TC2 >= 0
     ANALOG_PIN_SAY(TC2);
   #endif
   #if PIN_EXISTS(TEMP_0)
@@ -546,19 +530,19 @@ static bool report_pin_name(int8_t pin,bool &pin_is_analog) {
   #if PIN_EXISTS(TEMP_X)
     ANALOG_PIN_SAY(TEMP_X_PIN);
   #endif
-  #if defined(TLC_BLANK_BIT) && TLC_BLANK_BIT > -1
+  #if defined(TLC_BLANK_BIT) && TLC_BLANK_BIT >= 0
     PIN_SAY(TLC_BLANK_BIT);
   #endif
   #if PIN_EXISTS(TLC_BLANK)
     PIN_SAY(TLC_BLANK_PIN);
   #endif
-  #if defined(TLC_CLOCK_BIT) && TLC_CLOCK_BIT > -1
+  #if defined(TLC_CLOCK_BIT) && TLC_CLOCK_BIT >= 0
     PIN_SAY(TLC_CLOCK_BIT);
   #endif
   #if PIN_EXISTS(TLC_CLOCK)
     PIN_SAY(TLC_CLOCK_PIN);
   #endif
-  #if defined(TLC_DATA_BIT) && TLC_DATA_BIT > -1
+  #if defined(TLC_DATA_BIT) && TLC_DATA_BIT >= 0
     PIN_SAY(TLC_DATA_BIT);
   #endif
   #if PIN_EXISTS(TLC_DATA)
@@ -570,7 +554,7 @@ static bool report_pin_name(int8_t pin,bool &pin_is_analog) {
   #if PIN_EXISTS(TX_ENABLE)
     PIN_SAY(TX_ENABLE_PIN);
   #endif
-  #if defined(UNUSED_PWM) && UNUSED_PWM > -1
+  #if defined(UNUSED_PWM) && UNUSED_PWM >= 0
     PIN_SAY(UNUSED_PWM);
   #endif
   #if PIN_EXISTS(X_ATT)
@@ -687,14 +671,14 @@ static bool report_pin_name(int8_t pin,bool &pin_is_analog) {
 
   sprintf(buffer, NAME_FORMAT, "<unused> ");
   SERIAL_ECHO(buffer);
-  
+
   return false;
-}  //  report_pin_name
+} // report_pin_name
 
 //  True - currently a PWM pin
 static bool PWM_status(uint8_t pin) {
   char buffer[20];   // for the sprintf statements
-  
+
   switch(digitalPinToTimer(pin)) {
 
     #if defined(TCCR0A) && defined(COM0A1)
@@ -789,7 +773,7 @@ static bool PWM_status(uint8_t pin) {
         break;
     #endif
 
-    #if defined(TCCR4A)
+    #ifdef TCCR4A
       case TIMER4A:
         if (TCCR4A & (_BV(COM4A1) | _BV(COM4A0))){
           sprintf(buffer, "PWM:  %4d",OCR4A); 
@@ -815,7 +799,7 @@ static bool PWM_status(uint8_t pin) {
         else return false;
         break;
     #endif
-          
+
     #if defined(TCCR5A) && defined(COM5A1)
       case TIMER5A:
         if (TCCR5A & (_BV(COM5A1) | _BV(COM5A0))){
@@ -1044,7 +1028,7 @@ static void PWM_details(uint8_t pin)
 	case NOT_ON_TIMER:
     break;
 	}
-  SERIAL_PROTOCOLPGM("  ");   
+  SERIAL_PROTOCOLPGM("  ");
 }  // PWM_details
 
 
@@ -1070,38 +1054,42 @@ inline void report_pin_state(int8_t pin) {
   SERIAL_EOL;
 }
 
+bool get_pinMode(int8_t pin) { return *portModeRegister(digitalPinToPort(pin)) & digitalPinToBitMask(pin); }
+
 // pretty report with PWM info
 inline void report_pin_state_extended(int8_t pin, bool ignore) {
 
   char buffer[30];   // for the sprintf statements
-    
- // report pin number 
-  sprintf(buffer, "PIN:% 3d ", pin);
-  SERIAL_ECHO(buffer);   
 
- // report pin name 
+  // report pin number
+  sprintf(buffer, "PIN:% 3d ", pin);
+  SERIAL_ECHO(buffer);
+
+  // report pin name
   bool analog_pin;
-  report_pin_name(pin, analog_pin); 
-  
-// report pin state
-  if (pin_is_protected(pin) && ignore == false) 
+  report_pin_name(pin, analog_pin);
+
+  // report pin state
+  if (pin_is_protected(pin) && !ignore)
     SERIAL_ECHOPGM("protected ");
   else {
     if (analog_pin) {
-        sprintf(buffer, "Analog in =% 5d", analogRead(pin - analogInputToDigitalPin(0)));
-        SERIAL_ECHO(buffer); 
-       }
+      sprintf(buffer, "Analog in =% 5d", analogRead(pin - analogInputToDigitalPin(0)));
+      SERIAL_ECHO(buffer);
+    }
     else {
       if (!get_pinMode(pin)) {
         pinMode(pin, INPUT_PULLUP);  // make sure input isn't floating
         SERIAL_PROTOCOLPAIR("Input  = ", digitalRead_mod(pin));
-      }  
-      else if  (PWM_status(pin)) ;
-      else SERIAL_PROTOCOLPAIR("Output = ", digitalRead_mod(pin));  
+      }
+      else if (PWM_status(pin)) {
+        // do nothing
+      }
+      else SERIAL_PROTOCOLPAIR("Output = ", digitalRead_mod(pin));
     }
   }
 
-// report PWM capabilities
+  // report PWM capabilities
   PWM_details(pin);
   SERIAL_EOL;
 }

From dbe414ef0352ebd3abfe5156580a67195f3d7a6e Mon Sep 17 00:00:00 2001
From: Scott Lahteine <sourcetree@thinkyhead.com>
Date: Sun, 30 Oct 2016 19:16:33 -0500
Subject: [PATCH 4/9] Squash redundant code in PWM_status

---
 Marlin/pinsDebug.h | 164 ++++++++-------------------------------------
 1 file changed, 29 insertions(+), 135 deletions(-)

diff --git a/Marlin/pinsDebug.h b/Marlin/pinsDebug.h
index e0bf9406614..ecd5cd69c29 100644
--- a/Marlin/pinsDebug.h
+++ b/Marlin/pinsDebug.h
@@ -675,168 +675,62 @@ static bool report_pin_name(int8_t pin, bool &pin_is_analog) {
   return false;
 } // report_pin_name
 
-//  True - currently a PWM pin
+#define PWM_PRINT(V) do{ sprintf(buffer, "PWM:  %4d", V); SERIAL_ECHO(buffer); }while(0)
+#define PWM_CASE(N) \
+  case TIMER##N: \
+    if (TCCR##N & (_BV(COM## N ##1) | _BV(COM## N ##0))) { \
+      PWM_PRINT(OCR##N); \
+      return true; \
+    } else return false
+
+/**
+ * Print a pin's PWM status.
+ * Return true if it's currently a PWM pin.
+ */
 static bool PWM_status(uint8_t pin) {
   char buffer[20];   // for the sprintf statements
 
   switch(digitalPinToTimer(pin)) {
 
     #if defined(TCCR0A) && defined(COM0A1)
-      case TIMER0A:
-        if (TCCR0A & (_BV(COM0A1) | _BV(COM0A0))){
-          sprintf(buffer, "PWM:  %4d", OCR0A); 
-          SERIAL_ECHO(buffer);
-          return true;
-        }
-        else return false;
-        break;
-      case TIMER0B:
-        if (TCCR0A & (_BV(COM0B1) | _BV(COM0B0))){
-          sprintf(buffer, "PWM:  %4d",OCR0B); 
-          SERIAL_ECHO(buffer);
-          return true;
-        }
-        else return false;
-        break;
+      PWM_CASE(0A);
+      PWM_CASE(0B);
     #endif
 
     #if defined(TCCR1A) && defined(COM1A1)
-      case TIMER1A:
-        if (TCCR1A & (_BV(COM1A1) | _BV(COM1A0))){
-          sprintf(buffer, "PWM:  %4d",OCR1A); 
-          SERIAL_ECHO(buffer);
-          return true;
-        }
-        else return false;
-        break;
-      case TIMER1B:
-        if (TCCR1A & (_BV(COM1B1) | _BV(COM1B0))){
-          sprintf(buffer, "PWM:  %4d",OCR1B); 
-          SERIAL_ECHO(buffer);
-          return true;
-        }
-        else return false;
-        break; 
-      case TIMER1C:
-        if (TCCR1A & (_BV(COM1C1) | _BV(COM1C0))){
-          sprintf(buffer, "PWM:  %4d",OCR1C); 
-          SERIAL_ECHO(buffer);
-          return true;
-        }
-        else return false;
-        break;
+      PWM_CASE(1A);
+      PWM_CASE(1B);
+      PWM_CASE(1C);
     #endif
 
     #if defined(TCCR2A) && defined(COM2A1)
-      case TIMER2A:
-        if (TCCR2A & (_BV(COM2A1) | _BV(COM2A0))){
-          sprintf(buffer, "PWM:  %4d",OCR2A); 
-          SERIAL_ECHO(buffer);
-          return true;
-        }
-        else return false;
-        break;
-      case TIMER2B:
-        if (TCCR2A & (_BV(COM2B1) | _BV(COM2B0))){
-          sprintf(buffer, "PWM:  %4d",OCR2B); 
-          SERIAL_ECHO(buffer);
-          return true;
-        }
-        else return false;
-        break;
+      PWM_CASE(2A);
+      PWM_CASE(2B);
     #endif
 
     #if defined(TCCR3A) && defined(COM3A1)
-      case TIMER3A:
-        if (TCCR3A & (_BV(COM3A1) | _BV(COM3A0))){
-          sprintf(buffer, "PWM:  %4d",OCR3A); 
-          SERIAL_ECHO(buffer);
-          return true;
-        }
-        else return false;
-        break;
-      case TIMER3B:
-        if (TCCR3A & (_BV(COM3B1) | _BV(COM3B0))){
-          sprintf(buffer, "PWM:  %4d",OCR3B); 
-          SERIAL_ECHO(buffer);
-          return true;
-        }
-        else return false;
-        break;
-      case TIMER3C:
-        if (TCCR3A & (_BV(COM3C1) | _BV(COM3C0))){
-          sprintf(buffer, "PWM:  %4d",OCR3C); 
-          SERIAL_ECHO(buffer);
-          return true;
-        }
-        else return false;
-        break;
+      PWM_CASE(3A);
+      PWM_CASE(3B);
+      PWM_CASE(3C);
     #endif
 
     #ifdef TCCR4A
-      case TIMER4A:
-        if (TCCR4A & (_BV(COM4A1) | _BV(COM4A0))){
-          sprintf(buffer, "PWM:  %4d",OCR4A); 
-          SERIAL_ECHO(buffer);
-          return true;
-        }
-        else return false;
-        break;
-      case TIMER4B:
-        if (TCCR4A & (_BV(COM4B1) | _BV(COM4B0))){
-          sprintf(buffer, "PWM:  %4d",OCR4B); 
-          SERIAL_ECHO(buffer);
-          return true;
-        }
-        else return false;
-        break;
-      case TIMER4C:
-        if (TCCR4A & (_BV(COM4C1) | _BV(COM4C0))){
-          sprintf(buffer, "PWM:  %4d",OCR4C); 
-          SERIAL_ECHO(buffer);
-          return true;
-        }
-        else return false;
-        break;
+      PWM_CASE(4A);
+      PWM_CASE(4B);
+      PWM_CASE(4C);
     #endif
 
     #if defined(TCCR5A) && defined(COM5A1)
-      case TIMER5A:
-        if (TCCR5A & (_BV(COM5A1) | _BV(COM5A0))){
-          sprintf(buffer, "PWM:  %4d",OCR5A); 
-          SERIAL_ECHO(buffer);
-          return true;
-        }
-        else return false;
-        break;
-      case TIMER5B:
-        if (TCCR5A & (_BV(COM5B1) | _BV(COM5B0))){
-          sprintf(buffer, "PWM:  %4d",OCR5B); 
-          SERIAL_ECHO(buffer);
-          return true;
-        }
-        else return false;
-        break;
-      case TIMER5C:
-        if (TCCR5A & (_BV(COM5C1) | _BV(COM5C0))){
-          sprintf(buffer, "PWM:  %4d",OCR5C); 
-          SERIAL_ECHO(buffer);
-          return true;
-        }
-        else return false;
-        break;
+      PWM_CASE(5A);
+      PWM_CASE(5B);
+      PWM_CASE(5C);
     #endif
 
     case NOT_ON_TIMER:
-      return false;
-      break;
-     
     default:
       return false;
-
   }
-  
-  SERIAL_PROTOCOLPGM("  ");   
+  SERIAL_PROTOCOLPGM("  ");
 }  //PWM_status
 
 static void PWM_details(uint8_t pin)

From b4444e91aea67cc4022eb315991473ee559cdad1 Mon Sep 17 00:00:00 2001
From: Scott Lahteine <sourcetree@thinkyhead.com>
Date: Sun, 30 Oct 2016 19:36:58 -0500
Subject: [PATCH 5/9] Reduce code and build size of PWM_details

---
 Marlin/pinsDebug.h | 340 +++++++++++++++++++++------------------------
 1 file changed, 161 insertions(+), 179 deletions(-)

diff --git a/Marlin/pinsDebug.h b/Marlin/pinsDebug.h
index ecd5cd69c29..ad946c59ba3 100644
--- a/Marlin/pinsDebug.h
+++ b/Marlin/pinsDebug.h
@@ -733,194 +733,176 @@ static bool PWM_status(uint8_t pin) {
   SERIAL_PROTOCOLPGM("  ");
 }  //PWM_status
 
-static void PWM_details(uint8_t pin)
-{
+#define WGM_MAKE3(N) ((TEST(TCCR##N##B, WGM##N##2) >> 1) | (TCCR##N##A & (_BV(WGM##N##0) | _BV(WGM##N##1))))
+#define WGM_MAKE4(N) (WGM_MAKE3(N) | (TEST(TCCR##N##B, WGM##N##3) >> 1))
+#define TIMER_PREFIX(T,L,N) do{ \
+    WGM = WGM_MAKE##N(T); \
+    SERIAL_PROTOCOLPGM("    TIMER"); \
+    SERIAL_PROTOCOLPGM(STRINGIFY(T) STRINGIFY(L)); \
+    SERIAL_PROTOCOLPAIR("    WGM: ", WGM); \
+    SERIAL_PROTOCOLPAIR("    TIMSK" STRINGIFY(T) ": ", TIMSK##T); \
+  }while(0)
 
-  uint8_t  WGM;
+#define WGM_TEST1 (WGM == 0 || WGM == 2 || WGM == 4 || WGM == 6)
+#define WGM_TEST2 (WGM == 0 || WGM == 4 || WGM == 12 || WGM == 13)
+
+static void err_is_counter() {
+  SERIAL_PROTOCOLPGM("   Can't ");
+  SERIAL_PROTOCOLPGM("be used as a PWM because ");
+  SERIAL_PROTOCOLPGM("of counter mode");
+}
+static void err_is_interrupt() {
+  SERIAL_PROTOCOLPGM("   Can't ");
+  SERIAL_PROTOCOLPGM("be used as a PWM because ");
+  SERIAL_PROTOCOLPGM("it's ");
+  SERIAL_PROTOCOLPGM("being used as an interrupt");
+}
+static void err_prob_interrupt() {
+  SERIAL_PROTOCOLPGM("   Probably can't ");
+  SERIAL_PROTOCOLPGM("be used as a PWM because ");
+  SERIAL_PROTOCOLPGM("counter/timer is ");
+  SERIAL_PROTOCOLPGM("being used as an interrupt");
+}
+static void can_be_used() { SERIAL_PROTOCOLPGM("   can be used as PWM   "); }
+
+static void PWM_details(uint8_t pin) {
+
+  uint8_t WGM;
 
   switch(digitalPinToTimer(pin)) {
- 
-	#if defined(TCCR0A) && defined(COM0A1)
-    case TIMER0A:
-      SERIAL_PROTOCOLPGM("    TIMER0A");
-      WGM = ((TCCR0B & _BV(WGM02)) >> 1 ) | (TCCR0A & (_BV(WGM00) | _BV(WGM01) ));
-      SERIAL_PROTOCOLPAIR("    WGM: ", WGM);
-      SERIAL_PROTOCOLPAIR("    TIMSK0: ", TIMSK0);
-      if (WGM == 0 || WGM == 2 || WGM == 4 || WGM == 6) SERIAL_PROTOCOLPGM("   Can't be used as a PWM because of counter mode");
-      else if (TIMSK0 & _BV(OCIE0A))  SERIAL_PROTOCOLPGM("   Can't be used as a PWM because being used to generate an interrupt");
-      else if (TIMSK0 & _BV(TOIE0) )  SERIAL_PROTOCOLPGM("   Probably can't be used as a PWM because counter/timer being used to generate an interrupt");
-      else SERIAL_PROTOCOLPGM("   can be used as PWM   ");
-      break;         
-    case TIMER0B:
-      SERIAL_PROTOCOLPGM("    TIMER0B");
-      WGM = ((TCCR0B & _BV(WGM02)) >> 1 ) | (TCCR0A & (_BV(WGM00) | _BV(WGM01) ));
-      SERIAL_PROTOCOLPAIR("    WGM: ", WGM);
-      SERIAL_PROTOCOLPAIR("    TIMSK0: ", TIMSK0);
-      if (WGM == 0 || WGM == 2 || WGM == 4 || WGM == 6) SERIAL_PROTOCOLPGM("   Can't be used as a PWM because of counter mode");
-      else if (TIMSK0 & _BV(OCIE0B))  SERIAL_PROTOCOLPGM("   Can't be used as a PWM because being used to generate an interrupt");
-      else if (TIMSK0 & _BV(TOIE0) )  SERIAL_PROTOCOLPGM("   Probably can't be used as a PWM because counter/timer being used to generate an interrupt");
-      else SERIAL_PROTOCOLPGM("   can be used as PWM   ");
-      break;
-  #endif
 
-  #if defined(TCCR1A) && defined(COM1A1)
-    case TIMER1A:
-      SERIAL_PROTOCOLPGM("    TIMER1A");
-      WGM =   ((TCCR1B & (_BV(WGM12) | _BV(WGM13) )) >> 1 ) | (TCCR1A & (_BV(WGM10) | _BV(WGM11) ));   
-      SERIAL_PROTOCOLPAIR("    WGM: ", WGM);
-      SERIAL_PROTOCOLPAIR("    TIMSK1: ", TIMSK1);
-      if (WGM == 0 || WGM == 4 || WGM == 12 || WGM == 13)   SERIAL_PROTOCOLPGM("   Can't be used as a PWM because of counter mode");
-      else if (TIMSK1 &  _BV(OCIE1A))  SERIAL_PROTOCOLPGM("   Can't be used as a PWM because being used to generate an interrupt");
-      else if (TIMSK1 & (_BV(TOIE1) | _BV(ICIE1)) )  SERIAL_PROTOCOLPGM("   Probably can't be used as a PWM because counter/timer being used to generate an interrupt");
-      else SERIAL_PROTOCOLPGM("   can be used as PWM   ");
-      break;
-    case TIMER1B:
-      SERIAL_PROTOCOLPGM("    TIMER1B");
-      WGM =   ((TCCR1B & (_BV(WGM12) | _BV(WGM13) )) >> 1 ) | (TCCR1A & (_BV(WGM10) | _BV(WGM11) ));   
-      SERIAL_PROTOCOLPAIR("    WGM: ", WGM);
-      SERIAL_PROTOCOLPAIR("    TIMSK1: ", TIMSK1);
-      if (WGM == 0 || WGM == 4 || WGM == 12 || WGM == 13)   SERIAL_PROTOCOLPGM("   Can't be used as a PWM because of counter mode");
-      else if (TIMSK1 &  _BV(OCIE1B))  SERIAL_PROTOCOLPGM("   Can't be used as a PWM because being used to generate an interrupt");
-      else if (TIMSK1 & (_BV(TOIE1) | _BV(ICIE1)) )  SERIAL_PROTOCOLPGM("   Probably can't be used as a PWM because counter/timer being used to generate an interrupt");
-      else SERIAL_PROTOCOLPGM("   can be used as PWM   ");          
-      break;
-    case TIMER1C:
-      SERIAL_PROTOCOLPGM("    TIMER1C");
-      WGM =   ((TCCR1B & (_BV(WGM12) | _BV(WGM13) )) >> 1 ) | (TCCR1A & (_BV(WGM10) | _BV(WGM11) ));   
-      SERIAL_PROTOCOLPAIR("    WGM: ", WGM);
-      SERIAL_PROTOCOLPAIR("    TIMSK1: ", TIMSK1);
-      if (WGM == 0 || WGM == 4 || WGM == 12 || WGM == 13)   SERIAL_PROTOCOLPGM("   Can't be used as a PWM because of counter mode");
-      else if (TIMSK1 &  _BV(OCIE1C))  SERIAL_PROTOCOLPGM("   Can't be used as a PWM because being used to generate an interrupt");
-      else if (TIMSK1 & (_BV(TOIE1) | _BV(ICIE1)) )  SERIAL_PROTOCOLPGM("   Probably can't be used as a PWM because counter/timer being used to generate an interrupt");
-      else SERIAL_PROTOCOLPGM("   can be used as PWM   ");
-      break;
-  #endif
+  	#if defined(TCCR0A) && defined(COM0A1)
+      case TIMER0A:
+        TIMER_PREFIX(0,A,3);
+        if (WGM_TEST1) err_is_counter();
+        else if (TEST(TIMSK0, OCIE0A)) err_is_interrupt();
+        else if (TEST(TIMSK0, TOIE0)) err_prob_interrupt();
+        else can_be_used();
+        break;
+      case TIMER0B:
+        TIMER_PREFIX(0,B,3);
+        if (WGM_TEST1) err_is_counter();
+        else if (TEST(TIMSK0, OCIE0B)) err_is_interrupt();
+        else if (TEST(TIMSK0, TOIE0)) err_prob_interrupt();
+        else can_be_used();
+        break;
+    #endif
 
-  #if defined(TCCR2A) && defined(COM2A1)
-    case TIMER2A:
-      SERIAL_PROTOCOLPGM("    TIMER2A");
-      WGM =   ((TCCR2B & _BV(WGM22) ) >> 1 ) | (TCCR2A & (_BV(WGM20) | _BV(WGM21) ));
-      SERIAL_PROTOCOLPAIR("    WGM: ", WGM);
-      SERIAL_PROTOCOLPAIR("    TIMSK2: ", TIMSK2);
-      if (WGM == 0 || WGM == 2 || WGM == 4 || WGM == 6) SERIAL_PROTOCOLPGM("   Can't be used as a PWM because of counter mode");
-      else if (TIMSK2 & (_BV(TOIE2) | _BV(OCIE2A)))  SERIAL_PROTOCOLPGM("   Can't be used as a PWM because being used to generate an interrupt");
-      else if (TIMSK2 & _BV(TOIE2) )  SERIAL_PROTOCOLPGM("   Probably can't be used as a PWM because counter/timer being used to generate an interrupt");
-      else SERIAL_PROTOCOLPGM("   can be used as PWM   ");
-      break;
-    case TIMER2B:
-      SERIAL_PROTOCOLPGM("    TIMER2B");
-      WGM =   ((TCCR2B & _BV(WGM22) ) >> 1 ) | (TCCR2A & (_BV(WGM20) | _BV(WGM21) ));
-      SERIAL_PROTOCOLPAIR("    WGM: ", WGM);
-      SERIAL_PROTOCOLPAIR("    TIMSK2: ", TIMSK2);
-      if (WGM == 0 || WGM == 2 || WGM == 4 || WGM == 6) SERIAL_PROTOCOLPGM("   Can't be used as a PWM because of counter mode");
-      else if (TIMSK2 & _BV(OCIE2B))  SERIAL_PROTOCOLPGM("   Can't be used as a PWM because being used to generate an interrupt");
-      else if (TIMSK2 & _BV(TOIE2) )  SERIAL_PROTOCOLPGM("   Probably can't be used as a PWM because counter/timer being used to generate an interrupt");
-      else SERIAL_PROTOCOLPGM("   can be used as PWM   ");
-      break; 
-  #endif
+    #if defined(TCCR1A) && defined(COM1A1)
+      case TIMER1A:
+        TIMER_PREFIX(1,A,4);
+        if (WGM_TEST2) err_is_counter();
+        else if (TEST(TIMSK1, OCIE1A)) err_is_interrupt();
+        else if (TIMSK1 & (_BV(TOIE1) | _BV(ICIE1))) err_prob_interrupt();
+        else can_be_used();
+        break;
+      case TIMER1B:
+        TIMER_PREFIX(1,B,4);
+        if (WGM_TEST2) err_is_counter();
+        else if (TEST(TIMSK1, OCIE1B)) err_is_interrupt();
+        else if (TIMSK1 & (_BV(TOIE1) | _BV(ICIE1))) err_prob_interrupt();
+        else can_be_used();
+        break;
+      case TIMER1C:
+        TIMER_PREFIX(1,C,4);
+        if (WGM_TEST2) err_is_counter();
+        else if (TEST(TIMSK1, OCIE1C)) err_is_interrupt();
+        else if (TIMSK1 & (_BV(TOIE1) | _BV(ICIE1))) err_prob_interrupt();
+        else can_be_used();
+        break;
+    #endif
 
-  #if defined(TCCR3A) && defined(COM3A1)
-    case TIMER3A:
-      SERIAL_PROTOCOLPGM("    TIMER3A");
-      WGM =   ((TCCR3B & _BV(WGM32) ) >> 1 ) | (TCCR3A & (_BV(WGM30) | _BV(WGM31) ));
-      SERIAL_PROTOCOLPAIR("    WGM: ", WGM);
-      SERIAL_PROTOCOLPAIR("    TIMSK3: ", TIMSK3);
-      if (WGM == 0 || WGM == 4 || WGM == 12 || WGM == 13)   SERIAL_PROTOCOLPGM("   Can't be used as a PWM because of counter mode");
-      else if (TIMSK3 &  _BV(OCIE3A))  SERIAL_PROTOCOLPGM("   Can't be used as a PWM because being used to generate an interrupt");
-      else if (TIMSK3 & (_BV(TOIE3) | _BV(ICIE3)) )  SERIAL_PROTOCOLPGM("   Probably can't be used as a PWM because counter/timer being used to generate an interrupt");
-      else SERIAL_PROTOCOLPGM("   can be used as PWM   ");
-      break;      
-    case TIMER3B:
-      SERIAL_PROTOCOLPGM("    TIMER3B");
-      WGM =   ((TCCR3B & _BV(WGM32) ) >> 1 ) | (TCCR3A & (_BV(WGM30) | _BV(WGM31) ));
-      SERIAL_PROTOCOLPAIR("    WGM: ", WGM);
-      SERIAL_PROTOCOLPAIR("    TIMSK3: ", TIMSK3);
-      if (WGM == 0 || WGM == 4 || WGM == 12 || WGM == 13)   SERIAL_PROTOCOLPGM("   Can't be used as a PWM because of counter mode");
-      else if (TIMSK3 &  _BV(OCIE3B))  SERIAL_PROTOCOLPGM("   Can't be used as a PWM because being used to generate an interrupt");
-      else if (TIMSK3 & (_BV(TOIE3) | _BV(ICIE3)) )  SERIAL_PROTOCOLPGM("   Probably can't be used as a PWM because counter/timer being used to generate an interrupt");
-      else SERIAL_PROTOCOLPGM("   can be used as PWM   ");
-      break;
-    case TIMER3C:
-      SERIAL_PROTOCOLPGM("    TIMER3C");
-      WGM =   ((TCCR3B & _BV(WGM32) ) >> 1 ) | (TCCR3A & (_BV(WGM30) | _BV(WGM31) ));
-      SERIAL_PROTOCOLPAIR("    WGM: ", WGM);
-      SERIAL_PROTOCOLPAIR("    TIMSK3: ", TIMSK3);
-      if (WGM == 0 || WGM == 4 || WGM == 12 || WGM == 13)   SERIAL_PROTOCOLPGM("   Can't be used as a PWM because of counter mode");
-      else if (TIMSK3 &  _BV(OCIE3C))  SERIAL_PROTOCOLPGM("   Can't be used as a PWM because being used to generate an interrupt");
-      else if (TIMSK3 & (_BV(TOIE3) | _BV(ICIE3)) )  SERIAL_PROTOCOLPGM("   Probably can't be used as a PWM because counter/timer being used to generate an interrupt");
-      else SERIAL_PROTOCOLPGM("   can be used as PWM   ");
-      break;
-  #endif
+    #if defined(TCCR2A) && defined(COM2A1)
+      case TIMER2A:
+        TIMER_PREFIX(2,A,3);
+        if (WGM_TEST1) err_is_counter();
+        else if (TIMSK2 & (_BV(TOIE2) | _BV(OCIE2A))) err_is_interrupt();
+        else if (TEST(TIMSK2, TOIE2)) err_prob_interrupt();
+        else can_be_used();
+        break;
+      case TIMER2B:
+        TIMER_PREFIX(2,B,3);
+        if (WGM_TEST1) err_is_counter();
+        else if (TEST(TIMSK2, OCIE2B)) err_is_interrupt();
+        else if (TEST(TIMSK2, TOIE2)) err_prob_interrupt();
+        else can_be_used();
+        break;
+    #endif
 
-  #if defined(TCCR4A)
-    case TIMER4A:
-      SERIAL_PROTOCOLPGM("    TIMER4A");
-      WGM =   ((TCCR4B & (_BV(WGM42) | _BV(WGM43) )) >> 1 ) | (TCCR4A & (_BV(WGM40) | _BV(WGM41) ));
-      SERIAL_PROTOCOLPAIR("    WGM: ", WGM);
-      SERIAL_PROTOCOLPAIR("    TIMSK4: ", TIMSK4);
-      if (WGM == 0 || WGM == 4 || WGM == 12 || WGM == 13)   SERIAL_PROTOCOLPGM("   Can't be used as a PWM because of counter mode");
-      else if (TIMSK4 &  _BV(OCIE4A))  SERIAL_PROTOCOLPGM("   Can't be used as a PWM because being used to generate an interrupt");
-      else if (TIMSK4 & (_BV(TOIE4) | _BV(ICIE4)) )  SERIAL_PROTOCOLPGM("   Probably can't be used as a PWM because counter/timer being used to generate an interrupt");
-      else SERIAL_PROTOCOLPGM("   can be used as PWM   ");
-      break;      
-    case TIMER4B:
-      SERIAL_PROTOCOLPGM("    TIMER4B");
-      WGM =   ((TCCR4B & (_BV(WGM42) | _BV(WGM43) )) >> 1 ) | (TCCR4A & (_BV(WGM40) | _BV(WGM41) ));
-      SERIAL_PROTOCOLPAIR("    WGM: ", WGM);
-      SERIAL_PROTOCOLPAIR("    TIMSK4: ", TIMSK4);
-      if (WGM == 0 || WGM == 4 || WGM == 12 || WGM == 13)   SERIAL_PROTOCOLPGM("   Can't be used as a PWM because of counter mode");
-      else if (TIMSK4 &  _BV(OCIE4B))  SERIAL_PROTOCOLPGM("   Can't be used as a PWM because being used to generate an interrupt");
-      else if (TIMSK4 & (_BV(TOIE4) | _BV(ICIE4)) )  SERIAL_PROTOCOLPGM("   Probably can't be used as a PWM because counter/timer being used to generate an interrupt");
-      else SERIAL_PROTOCOLPGM("   can be used as PWM   ");
-      break;      
-    case TIMER4C:
-      SERIAL_PROTOCOLPGM("    TIMER4C");
-      WGM =   ((TCCR4B & (_BV(WGM42) | _BV(WGM43) )) >> 1 ) | (TCCR4A & (_BV(WGM40) | _BV(WGM41) ));
-      SERIAL_PROTOCOLPAIR("    WGM: ", WGM);
-      SERIAL_PROTOCOLPAIR("    TIMSK4: ", TIMSK4);
-      if (WGM == 0 || WGM == 4 || WGM == 12 || WGM == 13)   SERIAL_PROTOCOLPGM("   Can't be used as a PWM because of counter mode");
-      else if (TIMSK4 &  _BV(OCIE4C))  SERIAL_PROTOCOLPGM("   Can't be used as a PWM because being used to generate an interrupt");
-      else if (TIMSK4 & (_BV(TOIE4) | _BV(ICIE4)) )  SERIAL_PROTOCOLPGM("   Probably can't be used as a PWM because counter/timer being used to generate an interrupt");
-      else SERIAL_PROTOCOLPGM("   can be used as PWM   ");
-      break;      
+    #if defined(TCCR3A) && defined(COM3A1)
+      case TIMER3A:
+        TIMER_PREFIX(3,A,3);
+        if (WGM_TEST2) err_is_counter();
+        else if (TEST(TIMSK3, OCIE3A)) err_is_interrupt();
+        else if (TIMSK3 & (_BV(TOIE3) | _BV(ICIE3))) err_prob_interrupt();
+        else can_be_used();
+        break;
+      case TIMER3B:
+        TIMER_PREFIX(3,B,3);
+        if (WGM_TEST2) err_is_counter();
+        else if (TEST(TIMSK3, OCIE3B)) err_is_interrupt();
+        else if (TIMSK3 & (_BV(TOIE3) | _BV(ICIE3))) err_prob_interrupt();
+        else can_be_used();
+        break;
+      case TIMER3C:
+        TIMER_PREFIX(3,C,3);
+        if (WGM_TEST2) err_is_counter();
+        else if (TEST(TIMSK3, OCIE3C)) err_is_interrupt();
+        else if (TIMSK3 & (_BV(TOIE3) | _BV(ICIE3))) err_prob_interrupt();
+        else can_be_used();
+        break;
+    #endif
 
-  #endif
-          
-  #if defined(TCCR5A) && defined(COM5A1)
-    case TIMER5A:
-      SERIAL_PROTOCOLPGM("    TIMER5A");
-      WGM =   ((TCCR5B & (_BV(WGM52) | _BV(WGM53) )) >> 1 ) | (TCCR5A & (_BV(WGM50) | _BV(WGM51) ));
-      SERIAL_PROTOCOLPAIR("    WGM: ", WGM);
-      SERIAL_PROTOCOLPAIR("    TIMSK5: ", TIMSK5);
-      if (WGM == 0 || WGM == 4 || WGM == 12 || WGM == 13)   SERIAL_PROTOCOLPGM("   Can't be used as a PWM because of counter mode");
-      else if (TIMSK5 &  _BV(OCIE5A))  SERIAL_PROTOCOLPGM("   Can't be used as a PWM because being used to generate an interrupt");
-      else if (TIMSK5 & (_BV(TOIE5) | _BV(ICIE5)) )  SERIAL_PROTOCOLPGM("   Probably can't be used as a PWM because counter/timer being used to generate an interrupt");
-      else SERIAL_PROTOCOLPGM("   can be used as PWM   ");
-      break;      
-    case TIMER5B:
-      SERIAL_PROTOCOLPGM("    TIMER5B");
-      WGM =   ((TCCR5B & (_BV(WGM52) | _BV(WGM53) )) >> 1 ) | (TCCR5A & (_BV(WGM50) | _BV(WGM51) ));
-      SERIAL_PROTOCOLPAIR("    WGM: ", WGM);
-      SERIAL_PROTOCOLPAIR("    TIMSK5: ", TIMSK5);
-      if (WGM == 0 || WGM == 4 || WGM == 12 || WGM == 13)   SERIAL_PROTOCOLPGM("   Can't be used as a PWM because of counter mode");
-      else if (TIMSK5 &  _BV(OCIE5B))  SERIAL_PROTOCOLPGM("   Can't be used as a PWM because being used to generate an interrupt");
-      else if (TIMSK5 & (_BV(TOIE5) | _BV(ICIE5)) )  SERIAL_PROTOCOLPGM("   Probably can't be used as a PWM because counter/timer being used to generate an interrupt");
-      else SERIAL_PROTOCOLPGM("   can be used as PWM   ");
-      break;      
-    case TIMER5C:
-      SERIAL_PROTOCOLPGM("    TIMER5C");
-      WGM =   ((TCCR5B & (_BV(WGM52) | _BV(WGM53) )) >> 1 ) | (TCCR5A & (_BV(WGM50) | _BV(WGM51) ));
-      SERIAL_PROTOCOLPAIR("    WGM: ", WGM);
-      SERIAL_PROTOCOLPAIR("    TIMSK5: ", TIMSK5);
-      if (WGM == 0 || WGM == 4 || WGM == 12 || WGM == 13)   SERIAL_PROTOCOLPGM("   Can't be used as a PWM because of counter mode");
-      else if (TIMSK5 &  _BV(OCIE5C))  SERIAL_PROTOCOLPGM("   Can't be used as a PWM because being used to generate an interrupt");
-      else if (TIMSK5 & (_BV(TOIE5) | _BV(ICIE5)) )  SERIAL_PROTOCOLPGM("   Probably can't be used as a PWM because counter/timer being used to generate an interrupt");
-      else SERIAL_PROTOCOLPGM("   can be used as PWM   ");
-      break;      
-  #endif
+    #ifdef TCCR4A
+      case TIMER4A:
+        TIMER_PREFIX(4,A,4);
+        if (WGM_TEST2) err_is_counter();
+        else if (TEST(TIMSK4, OCIE4A)) err_is_interrupt();
+        else if (TIMSK4 & (_BV(TOIE4) | _BV(ICIE4))) err_prob_interrupt();
+        else can_be_used();
+        break;
+      case TIMER4B:
+        TIMER_PREFIX(4,B,4);
+        if (WGM_TEST2) err_is_counter();
+        else if (TEST(TIMSK4, OCIE4B)) err_is_interrupt();
+        else if (TIMSK4 & (_BV(TOIE4) | _BV(ICIE4))) err_prob_interrupt();
+        else can_be_used();
+        break;
+      case TIMER4C:
+        TIMER_PREFIX(4,C,4);
+        if (WGM_TEST2) err_is_counter();
+        else if (TEST(TIMSK4, OCIE4C)) err_is_interrupt();
+        else if (TIMSK4 & (_BV(TOIE4) | _BV(ICIE4))) err_prob_interrupt();
+        else can_be_used();
+        break;
+    #endif
+
+    #if defined(TCCR5A) && defined(COM5A1)
+      case TIMER5A:
+        TIMER_PREFIX(5,A,4);
+        if (WGM_TEST2) err_is_counter();
+        else if (TEST(TIMSK5, OCIE5A)) err_is_interrupt();
+        else if (TIMSK5 & (_BV(TOIE5) | _BV(ICIE5))) err_prob_interrupt();
+        else can_be_used();
+        break;
+      case TIMER5B:
+        TIMER_PREFIX(5,B,4);
+        if (WGM_TEST2) err_is_counter();
+        else if (TEST(TIMSK5, OCIE5B)) err_is_interrupt();
+        else if (TIMSK5 & (_BV(TOIE5) | _BV(ICIE5))) err_prob_interrupt();
+        else can_be_used();
+        break;
+      case TIMER5C:
+        TIMER_PREFIX(5,C,4);
+        if (WGM_TEST2) err_is_counter();
+        else if (TEST(TIMSK5, OCIE5C)) err_is_interrupt();
+        else if (TIMSK5 & (_BV(TOIE5) | _BV(ICIE5))) err_prob_interrupt();
+        else can_be_used();
+        break;
+    #endif
+
+  	case NOT_ON_TIMER: break;
 
-	case NOT_ON_TIMER:
-    break;
 	}
   SERIAL_PROTOCOLPGM("  ");
 }  // PWM_details

From 24f6612551c12cd857721f6a96be9dacfa73c55c Mon Sep 17 00:00:00 2001
From: Scott Lahteine <sourcetree@thinkyhead.com>
Date: Mon, 31 Oct 2016 07:28:51 -0500
Subject: [PATCH 6/9] Reduce and optimize endstop_monitor code

---
 Marlin/temperature.cpp | 99 ++++++++++++++++++++++--------------------
 1 file changed, 51 insertions(+), 48 deletions(-)

diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp
index a3c3c9c8491..61b6eb4b331 100644
--- a/Marlin/temperature.cpp
+++ b/Marlin/temperature.cpp
@@ -1407,69 +1407,69 @@ void Temperature::set_current_temp_raw() {
   void endstop_monitor() {
     static uint16_t old_endstop_bits_local = 0;
     static uint8_t local_LED_status = 0;
-    if (endstop_monitor_flag) {
-      uint16_t current_endstop_bits_local = 0;
+    uint16_t current_endstop_bits_local = 0;
+    #if HAS_X_MIN
+      if (READ(X_MIN_PIN)) SBI(current_endstop_bits_local, X_MIN);
+    #endif
+    #if HAS_X_MAX
+      if (READ(X_MAX_PIN)) SBI(current_endstop_bits_local, X_MAX);
+    #endif
+    #if HAS_Y_MIN
+      if (READ(Y_MIN_PIN)) SBI(current_endstop_bits_local, Y_MIN);
+    #endif
+    #if HAS_Y_MAX
+      if (READ(Y_MAX_PIN)) SBI(current_endstop_bits_local, Y_MAX);
+    #endif
+    #if HAS_Z_MIN
+      if (READ(Z_MIN_PIN)) SBI(current_endstop_bits_local, Z_MIN);
+    #endif
+    #if HAS_Z_MAX
+      if (READ(Z_MAX_PIN)) SBI(current_endstop_bits_local, Z_MAX);
+    #endif
+    #if HAS_Z_MIN_PROBE_PIN
+      if (READ(Z_MIN_PROBE_PIN)) SBI(current_endstop_bits_local, Z_MIN_PROBE);
+    #endif
+    #if HAS_Z2_MIN
+      if (READ(Z2_MIN_PIN)) SBI(current_endstop_bits_local, Z2_MIN);
+    #endif
+    #if HAS_Z2_MAX
+      if (READ(Z2_MAX_PIN)) SBI(current_endstop_bits_local, Z2_MAX);
+    #endif
+
+    uint16_t endstop_change = current_endstop_bits_local ^ old_endstop_bits_local;
+
+    if (endstop_change) {
       #if HAS_X_MIN
-        if (READ(X_MIN_PIN)) current_endstop_bits_local |= _BV(X_MIN);
-        if ((current_endstop_bits_local ^ old_endstop_bits_local) & _BV(X_MIN)) {
-          SERIAL_PROTOCOLPAIR("X_MIN: ", (current_endstop_bits_local & _BV(X_MIN)) ? 1 : 0);
-        }
+        if (TEST(endstop_change, X_MIN)) SERIAL_PROTOCOLPAIR("X_MIN:", !!TEST(current_endstop_bits_local, X_MIN));
       #endif
       #if HAS_X_MAX
-        if (READ(X_MAX_PIN)) current_endstop_bits_local |= _BV(X_MAX);
-        if ((current_endstop_bits_local ^ old_endstop_bits_local) & _BV(X_MAX)) {
-          SERIAL_PROTOCOLPAIR("   X_MAX: ", (current_endstop_bits_local & _BV(X_MAX)) ? 1 : 0);
-        }
+        if (TEST(endstop_change, X_MAX)) SERIAL_PROTOCOLPAIR("  X_MAX:", !!TEST(current_endstop_bits_local, X_MAX));
       #endif
       #if HAS_Y_MIN
-        if (READ(Y_MIN_PIN)) current_endstop_bits_local |= _BV(Y_MIN);
-        if ((current_endstop_bits_local ^ old_endstop_bits_local) & _BV(Y_MIN)) {
-          SERIAL_PROTOCOLPAIR("   Y_MIN: ", (current_endstop_bits_local & _BV(Y_MIN)) ? 1 : 0);
-        }
+        if (TEST(endstop_change, Y_MIN)) SERIAL_PROTOCOLPAIR("  Y_MIN:", !!TEST(current_endstop_bits_local, Y_MIN));
       #endif
       #if HAS_Y_MAX
-        if (READ(Y_MAX_PIN)) current_endstop_bits_local |= _BV(Y_MAX);
-        if ((current_endstop_bits_local ^ old_endstop_bits_local) & _BV(Y_MAX)) {
-          SERIAL_PROTOCOLPAIR("   Y_MAX: ", (current_endstop_bits_local & _BV(Y_MAX)) ? 1 : 0);
-        }
+        if (TEST(endstop_change, Y_MAX)) SERIAL_PROTOCOLPAIR("  Y_MAX:", !!TEST(current_endstop_bits_local, Y_MAX));
       #endif
       #if HAS_Z_MIN
-        if (READ(Z_MIN_PIN)) current_endstop_bits_local |= _BV(Z_MIN);
-        if ((current_endstop_bits_local ^ old_endstop_bits_local) & _BV(Z_MIN)) {
-          SERIAL_PROTOCOLPAIR("   Z_MIN: ", (current_endstop_bits_local & _BV(Z_MIN)) ? 1 : 0);
-        }
+        if (TEST(endstop_change, Z_MIN)) SERIAL_PROTOCOLPAIR("  Z_MIN:", !!TEST(current_endstop_bits_local, Z_MIN));
       #endif
       #if HAS_Z_MAX
-        if (READ(Z_MAX_PIN)) current_endstop_bits_local |= _BV(Z_MAX);
-        if ((current_endstop_bits_local ^ old_endstop_bits_local) & _BV(Z_MAX)) {
-          SERIAL_PROTOCOLPAIR("   Z_MAX: ", (current_endstop_bits_local & _BV(Z_MAX)) ? 1 : 0);
-        }
+        if (TEST(endstop_change, Z_MAX)) SERIAL_PROTOCOLPAIR("  Z_MAX:", !!TEST(current_endstop_bits_local, Z_MAX));
       #endif
       #if HAS_Z_MIN_PROBE_PIN
-        if (READ(Z_MIN_PROBE_PIN)) current_endstop_bits_local |= _BV(Z_MIN_PROBE);
-        if ((current_endstop_bits_local ^ old_endstop_bits_local) & _BV(Z_MIN_PROBE)) {
-          SERIAL_PROTOCOLPAIR("   PROBE: ", (current_endstop_bits_local & _BV(Z_MIN_PROBE)) ? 1 : 0);
-        }
+        if (TEST(endstop_change, Z_MIN_PROBE)) SERIAL_PROTOCOLPAIR("  PROBE:", !!TEST(current_endstop_bits_local, Z_MIN_PROBE));
       #endif
       #if HAS_Z2_MIN
-        if (READ(Z2_MIN_PIN)) current_endstop_bits_local |= _BV(Z2_MIN);
-        if ((current_endstop_bits_local ^ old_endstop_bits_local) & _BV(Z2_MIN)) {
-          SERIAL_PROTOCOLPAIR("   Z2_MIN: ", (current_endstop_bits_local & _BV(Z2_MIN)) ? 1 : 0);
-        }
+        if (TEST(endstop_change, Z2_MIN)) SERIAL_PROTOCOLPAIR("  Z2_MIN:", !!TEST(current_endstop_bits_local, Z2_MIN));
       #endif
       #if HAS_Z2_MAX
-        if (READ(Z2_MAX_PIN)) current_endstop_bits_local |= _BV(Z2_MAX);
-        if ((current_endstop_bits_local ^ old_endstop_bits_local) & _BV(Z2_MAX)) {
-          SERIAL_PROTOCOLPAIR("   Z2_MAX: ", (current_endstop_bits_local & _BV(Z2_MAX)) ? 1 : 0);
-        }
+        if (TEST(endstop_change, Z2_MAX)) SERIAL_PROTOCOLPAIR("  Z2_MAX:", !!TEST(current_endstop_bits_local, Z2_MAX));
       #endif
-
-      if (current_endstop_bits_local != old_endstop_bits_local) {
-        analogWrite(LED_PIN, local_LED_status );                // toggle LED
-        SERIAL_PROTOCOLPGM("\n\n");                             // make it easy to see the message
-        old_endstop_bits_local = current_endstop_bits_local ;   // get ready for next change
-        local_LED_status  = local_LED_status ? 0 : 255;
-      }
+      SERIAL_PROTOCOLPGM("\n\n");
+      analogWrite(LED_PIN, local_LED_status);
+      local_LED_status ^= 255;
+      old_endstop_bits_local = current_endstop_bits_local;
     }
   }
 #endif // PINS_DEBUGGING
@@ -1929,12 +1929,15 @@ void Temperature::isr() {
       }
     }
   #endif //BABYSTEPPING
+
   #if ENABLED(PINS_DEBUGGING)
     extern bool endstop_monitor_flag;
     // run the endstop monitor at 15Hz
     static uint8_t endstop_monitor_count = 16;  // offset this check from the others
-    endstop_monitor_count += _BV(1);  //  15 Hz
-    endstop_monitor_count &= 0x7F;
-    if (endstop_monitor_count == 0) endstop_monitor();  // report changes in endstop status
+    if (endstop_monitor_flag) {
+      endstop_monitor_count += _BV(1);  //  15 Hz
+      endstop_monitor_count &= 0x7F;
+      if (!endstop_monitor_count) endstop_monitor();  // report changes in endstop status
+    }
   #endif
 }

From 04a1fac029d0469faa34ae0d6acdcdfc9a65553d Mon Sep 17 00:00:00 2001
From: Scott Lahteine <sourcetree@thinkyhead.com>
Date: Sun, 30 Oct 2016 20:10:17 -0500
Subject: [PATCH 7/9] Some cleanup to M43

---
 Marlin/Marlin_main.cpp | 48 ++++++++++++++++++++++++++----------------
 1 file changed, 30 insertions(+), 18 deletions(-)

diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp
index be1d2c1eb2f..581036d5e8b 100755
--- a/Marlin/Marlin_main.cpp
+++ b/Marlin/Marlin_main.cpp
@@ -4676,26 +4676,43 @@ inline void gcode_M42() {
   /**
    * M43: Pin report and debug
    *
-   *      pin report if just M43 with no codes
-   *      P<pin> Will read/watch a single pin
-   *      W      Watch pins for changes until reboot
-   *      E      toggles endstop monitor
-   *               reports changes to endstops
-   *               toggles LED when endstop changes
-   *               background function (machine continues to operate as normal)
+   *      E<bool> Enable / disable background endstop monitoring
+   *               - Machine continues to operate
+   *               - Reports changes to endstops
+   *               - Toggles LED when an endstop changes
+   *
+   *   or
+   *
+   *      P<pin>  Pin to read or watch. If omitted, read/watch all pins.
+   *      W<bool> Watch pins -reporting changes- until reset, click, or M108.
+   *      I<bool> Flag to ignore Marlin's pin protection.
    *
    */
   inline void gcode_M43() {
+
+    // Enable or disable endstop monitoring
+    if (code_seen('E')) {
+      endstop_monitor_flag = code_value_bool();
+      SERIAL_PROTOCOLPGM("endstop monitor ");
+      SERIAL_PROTOCOL(endstop_monitor_flag ? "en" : "dis");
+      SERIAL_PROTOCOLLNPGM("abled");
+      return;
+    }
+
+    // Get the range of pins to test or watch
     int first_pin = 0, last_pin = DIO_COUNT - 1;
     if (code_seen('P')) {
       first_pin = last_pin = code_value_byte();
       if (first_pin > DIO_COUNT - 1) return;
     }
 
+    bool ignore_protection = code_seen('I') ? code_value_bool() : false;
+
+    // Watch until click, M108, or reset
     if (code_seen('W') && code_value_bool()) { // watch digital pins
       byte pin_state[last_pin - first_pin + 1];
       for (int8_t pin = first_pin; pin <= last_pin; pin++) {
-        if (pin_is_protected(pin)) continue;
+        if (pin_is_protected(pin) && !ignore_protection) continue;
         pinMode(pin, INPUT_PULLUP);
         // if (IS_ANALOG(pin))
         //   pin_state[pin - first_pin] = analogRead(pin - analogInputToDigitalPin(0)); // int16_t pin_state[...]
@@ -4727,17 +4744,12 @@ inline void gcode_M42() {
 
         safe_delay(500);
       }
+      return;
     }
-    if ( !(code_seen('P') || code_seen('W') || code_seen('E')))   // single pins report
-      for (uint8_t pin = first_pin; pin <= last_pin; pin++)
-        report_pin_state_extended(pin, code_seen('I') );        // "hidden" option to ignore protected list
-    
-    if (code_seen('E')) {
-      endstop_monitor_flag ^= true;
-      SERIAL_PROTOCOLPGM("endstop monitor ");
-      SERIAL_PROTOCOL(endstop_monitor_flag ? "en" : "dis");
-      SERIAL_PROTOCOLLNPGM("abled");
-    }    
+
+    // Report current state of selected pin(s)
+    for (uint8_t pin = first_pin; pin <= last_pin; pin++)
+      report_pin_state_extended(pin, ignore_protection);
   }
 
 #endif // PINS_DEBUGGING

From ff3a8ca0d1431c8bc6d9bc1e41e34822aae725e4 Mon Sep 17 00:00:00 2001
From: Scott Lahteine <sourcetree@thinkyhead.com>
Date: Mon, 31 Oct 2016 07:36:01 -0500
Subject: [PATCH 8/9] Use NUM_DIGITAL_PINS instead of custom DIO_COUNT

---
 Marlin/Marlin_main.cpp |  4 ++--
 Marlin/pinsDebug.h     | 17 -----------------
 2 files changed, 2 insertions(+), 19 deletions(-)

diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp
index 581036d5e8b..918bae1a0d9 100755
--- a/Marlin/Marlin_main.cpp
+++ b/Marlin/Marlin_main.cpp
@@ -4700,10 +4700,10 @@ inline void gcode_M42() {
     }
 
     // Get the range of pins to test or watch
-    int first_pin = 0, last_pin = DIO_COUNT - 1;
+    int first_pin = 0, last_pin = NUM_DIGITAL_PINS - 1;
     if (code_seen('P')) {
       first_pin = last_pin = code_value_byte();
-      if (first_pin > DIO_COUNT - 1) return;
+      if (first_pin > NUM_DIGITAL_PINS - 1) return;
     }
 
     bool ignore_protection = code_seen('I') ? code_value_bool() : false;
diff --git a/Marlin/pinsDebug.h b/Marlin/pinsDebug.h
index ad946c59ba3..9ccd9b25402 100644
--- a/Marlin/pinsDebug.h
+++ b/Marlin/pinsDebug.h
@@ -20,20 +20,6 @@
  *
  */
 
-// How many DIO pins are defined?
-#ifdef DIO85_PIN
-//  #define DIO_COUNT 86
-  #define DIO_COUNT 70  // digitalRead and other Arduino IDE routines only know about pins 0 through 69
-#elif defined(DIO53_PIN)
-  #define DIO_COUNT 54
-#elif defined(DIO47_PIN)
-  #define DIO_COUNT 48
-#elif defined(DIO31_PIN)
-  #define DIO_COUNT 32
-#elif defined(DIO21_PIN)
-  #define DIO_COUNT 22
-#endif
-
 bool endstop_monitor_flag = false;
 
 #define  NAME_FORMAT "%-28s"   // one place to specify the format of all the sources of names
@@ -157,9 +143,6 @@ static bool report_pin_name(int8_t pin, bool &pin_is_analog) {
   #if PIN_EXISTS(DIGIPOTSS)
     PIN_SAY(DIGIPOTSS_PIN);
   #endif
-  #if defined(DIO_COUNT) && DIO_COUNT >= 0
-    PIN_SAY(DIO_COUNT);
-  #endif
   #if defined(DOGLCD_A0) && DOGLCD_A0 >= 0
     PIN_SAY(DOGLCD_A0);
   #endif

From 85e307dcd8fb889e08c56130a3513d4abd466336 Mon Sep 17 00:00:00 2001
From: Scott Lahteine <sourcetree@thinkyhead.com>
Date: Mon, 31 Oct 2016 07:51:31 -0500
Subject: [PATCH 9/9] No FAN0_PIN

---
 Marlin/pinsDebug.h | 3 ---
 Marlin/pins_A4JP.h | 4 ++--
 2 files changed, 2 insertions(+), 5 deletions(-)

diff --git a/Marlin/pinsDebug.h b/Marlin/pinsDebug.h
index 9ccd9b25402..f116312e301 100644
--- a/Marlin/pinsDebug.h
+++ b/Marlin/pinsDebug.h
@@ -279,9 +279,6 @@ static bool report_pin_name(int8_t pin, bool &pin_is_analog) {
   #if PIN_EXISTS(FAN)
     PIN_SAY(FAN_PIN);
   #endif
-  #if PIN_EXISTS(FAN0)
-    PIN_SAY(FAN0_PIN);
-  #endif
   #if PIN_EXISTS(FAN1)
     PIN_SAY(FAN1_PIN);
   #endif
diff --git a/Marlin/pins_A4JP.h b/Marlin/pins_A4JP.h
index 7e9acd43128..bc0fc84d11e 100644
--- a/Marlin/pins_A4JP.h
+++ b/Marlin/pins_A4JP.h
@@ -115,8 +115,8 @@
 #define HEATER_BED_PIN       3
 
 #define FAN_PIN              8
-#define FAN0_PIN             6
-#define FAN1_PIN             2
+#define FAN1_PIN             6
+#define FAN2_PIN             2
 
 //
 // Misc. Functions