From 63dd0f249d3a82f8f253a1bd0f7c2edb7d4447e4 Mon Sep 17 00:00:00 2001
From: George Fu <nailao_5918@163.com>
Date: Fri, 14 Feb 2020 21:18:57 +0800
Subject: [PATCH] Fysetc S6 pins / LCD updates (#16830)

---
 Marlin/Configuration_adv.h                         |  4 +++-
 .../extensible_ui/lib/ftdi_eve_touch_ui/compat.h   |  4 ++++
 .../lib/ftdi_eve_touch_ui/pin_mappings.h           |  9 +++++++++
 Marlin/src/pins/stm32/pins_FYSETC_S6.h             | 14 ++++++++++----
 4 files changed, 26 insertions(+), 5 deletions(-)

diff --git a/Marlin/Configuration_adv.h b/Marlin/Configuration_adv.h
index 2e0a5b5c50..cb2bdc08b4 100644
--- a/Marlin/Configuration_adv.h
+++ b/Marlin/Configuration_adv.h
@@ -1333,9 +1333,11 @@
   //#define AO_EXP1_PINMAP    // AlephObjects CLCD UI EXP1 mapping
   //#define AO_EXP2_PINMAP    // AlephObjects CLCD UI EXP2 mapping
   //#define CR10_TFT_PINMAP   // Rudolph Riedel's CR10 pin mapping
+  //#define S6_TFT_PINMAP     // FYSETC S6 pin mapping
+
   //#define OTHER_PIN_LAYOUT  // Define pins manually below
   #if ENABLED(OTHER_PIN_LAYOUT)
-    // The pins for CS and MOD_RESET (PD) must be chosen.
+    // Pins for CS and MOD_RESET (PD) must be chosen
     #define CLCD_MOD_RESET  9
     #define CLCD_SPI_CS    10
 
diff --git a/Marlin/src/lcd/extensible_ui/lib/ftdi_eve_touch_ui/compat.h b/Marlin/src/lcd/extensible_ui/lib/ftdi_eve_touch_ui/compat.h
index 90fd615a5d..c3e8052a0e 100644
--- a/Marlin/src/lcd/extensible_ui/lib/ftdi_eve_touch_ui/compat.h
+++ b/Marlin/src/lcd/extensible_ui/lib/ftdi_eve_touch_ui/compat.h
@@ -37,6 +37,10 @@
 #ifdef __MARLIN_FIRMWARE__
   // __MARLIN_FIRMWARE__ exists when compiled within Marlin.
   #include "pin_mappings.h"
+  #undef max
+  #define max(a,b) ((a)>(b)?(a):(b))
+  #undef min
+  #define min(a,b) ((a)<(b)?(a):(b))
 #else
   namespace UI {
     static inline uint32_t safe_millis() {return millis();};
diff --git a/Marlin/src/lcd/extensible_ui/lib/ftdi_eve_touch_ui/pin_mappings.h b/Marlin/src/lcd/extensible_ui/lib/ftdi_eve_touch_ui/pin_mappings.h
index d474644b8c..548c6c7439 100644
--- a/Marlin/src/lcd/extensible_ui/lib/ftdi_eve_touch_ui/pin_mappings.h
+++ b/Marlin/src/lcd/extensible_ui/lib/ftdi_eve_touch_ui/pin_mappings.h
@@ -27,6 +27,15 @@
  * without adding new pin definitions to the board.
  */
 
+#ifdef S6_TFT_PINMAP
+  #ifndef __MARLIN_FIRMWARE__
+    #error "This pin mapping requires Marlin."
+  #endif
+
+  #define CLCD_SPI_CS         PC7
+  #define CLCD_MOD_RESET      PC6
+#endif
+
 #ifdef CR10_TFT_PINMAP
   #ifndef __MARLIN_FIRMWARE__
     #error "This pin mapping requires Marlin."
diff --git a/Marlin/src/pins/stm32/pins_FYSETC_S6.h b/Marlin/src/pins/stm32/pins_FYSETC_S6.h
index 4b85e6097c..16f176a376 100644
--- a/Marlin/src/pins/stm32/pins_FYSETC_S6.h
+++ b/Marlin/src/pins/stm32/pins_FYSETC_S6.h
@@ -48,6 +48,11 @@
   #define E2END 0xFFF // 4KB
 #endif
 
+//
+// Servos
+//
+#define SERVO0_PIN         PA3
+
 //
 // Limit Switches
 //
@@ -59,10 +64,11 @@
 #define Z_MAX_PIN          PA3
 
 //
-// Servos
-// share with Z_MAX_PIN
+// Filament Sensor
 //
-#define SERVO0_PIN         PA3
+#ifndef FIL_RUNOUT_PIN
+  #define FIL_RUNOUT_PIN   PA1
+#endif
 
 //
 // Steppers
@@ -160,6 +166,7 @@
 //#define KILL_PIN           PC5
 
 #define SDSS               PA4
+#define SD_DETECT_PIN  	   PB10
 
 //
 // LCD / Controller
@@ -189,7 +196,6 @@
 
     #define BTN_EN1        PC6
     #define BTN_EN2        PC7
-    #define SD_DETECT_PIN  PB10
 
     #define LCD_SDSS       PA4