From 1a948cbd930a2dd1e07ef264b6e5152446612567 Mon Sep 17 00:00:00 2001
From: Bob-the-Kuhn <bob.kuhn@att.net>
Date: Fri, 22 Dec 2017 11:03:22 -0600
Subject: [PATCH] Fix SET_OUTPUT glitch - LPC1768 & DUE

---
 Marlin/src/HAL/HAL_DUE/fastio_Due.h | 18 ++++++++++--------
 Marlin/src/HAL/HAL_LPC1768/fastio.h |  4 ++--
 2 files changed, 12 insertions(+), 10 deletions(-)

diff --git a/Marlin/src/HAL/HAL_DUE/fastio_Due.h b/Marlin/src/HAL/HAL_DUE/fastio_Due.h
index 01e253db5c..2c1513f48d 100644
--- a/Marlin/src/HAL/HAL_DUE/fastio_Due.h
+++ b/Marlin/src/HAL/HAL_DUE/fastio_Due.h
@@ -78,14 +78,17 @@
 #define _TOGGLE(IO)  _WRITE(IO, !READ(IO))
 
 /// set pin as input
-#define _SET_INPUT(IO)  pmc_enable_periph_clk(g_APinDescription[IO].ulPeripheralId); \
-                        PIO_Configure(g_APinDescription[IO].pPort, PIO_INPUT, g_APinDescription[IO].ulPin, 0)
+#define _SET_INPUT(IO)  do{ pmc_enable_periph_clk(g_APinDescription[IO].ulPeripheralId); \
+                            PIO_Configure(g_APinDescription[IO].pPort, PIO_INPUT, g_APinDescription[IO].ulPin, 0); \
+                        }while(0)
 /// set pin as output
-#define _SET_OUTPUT(IO)  PIO_Configure(g_APinDescription[IO].pPort, PIO_OUTPUT_1, \
-                         g_APinDescription[IO].ulPin, g_APinDescription[IO].ulPinConfiguration)
+#define _SET_OUTPUT(IO) do{ pmc_enable_periph_clk(g_APinDescription[IO].ulPeripheralId); \
+                            PIO_Configure(g_APinDescription[IO].pPort, _READ(IO) ? PIO_OUTPUT_1 : PIO_OUTPUT_0, \
+                                          g_APinDescription[IO].ulPin, g_APinDescription[IO].ulPinConfiguration); \
+                        }while(0)
 
 /// set pin as input with pullup mode
-#define _PULLUP(IO, v)  { pinMode(IO, (v!=LOW ? INPUT_PULLUP : INPUT)); }
+#define _PULLUP(IO, v)  { pinMode(IO, v != LOW ? INPUT_PULLUP : INPUT); }
 
 /// check if pin is an input
 #define _GET_INPUT(IO)
@@ -109,9 +112,8 @@
 #define SET_INPUT(IO)  _SET_INPUT(IO)
 /// set pin as input with pullup wrapper
 #define SET_INPUT_PULLUP(IO) do{ _SET_INPUT(IO); _PULLUP(IO, HIGH); }while(0)
-/// set pin as output wrapper
-#define SET_OUTPUT(IO)  do{ _SET_OUTPUT(IO); _WRITE(IO, LOW); }while(0)
-
+/// set pin as output wrapper -  reads the pin and sets the output to that value
+#define SET_OUTPUT(IO)  _SET_OUTPUT(IO)
 /// check if pin is an input wrapper
 #define GET_INPUT(IO)  _GET_INPUT(IO)
 /// check if pin is an output wrapper
diff --git a/Marlin/src/HAL/HAL_LPC1768/fastio.h b/Marlin/src/HAL/HAL_LPC1768/fastio.h
index cc215f5256..00a3d0ccac 100644
--- a/Marlin/src/HAL/HAL_LPC1768/fastio.h
+++ b/Marlin/src/HAL/HAL_LPC1768/fastio.h
@@ -119,8 +119,8 @@ bool useable_hardware_PWM(pin_t pin);
 #define SET_INPUT(IO)  _SET_INPUT(IO)
 /// set pin as input with pullup wrapper
 #define SET_INPUT_PULLUP(IO) do{ _SET_INPUT(IO); _PULLUP(IO, HIGH); }while(0)
-/// set pin as output wrapper
-#define SET_OUTPUT(IO)  do{ _SET_OUTPUT(IO); _WRITE(IO, LOW); }while(0)
+/// set pin as output wrapper  -  reads the pin and sets the output to that value
+#define SET_OUTPUT(IO)  do{ _WRITE(IO, _READ(IO)); _SET_OUTPUT(IO); }while(0)
 
 /// check if pin is an input wrapper
 #define GET_INPUT(IO)  _GET_INPUT(IO)