From 086fa0f0a738e7a3b97ea15b2b3decf41e197882 Mon Sep 17 00:00:00 2001
From: Scott Lahteine <thinkyhead@users.noreply.github.com>
Date: Mon, 30 Aug 2021 17:02:12 -0500
Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20Fix=20HAL/STM32=20F103Zx=20build?=
 =?UTF-8?q?s=20(#22610)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 Marlin/src/gcode/motion/G2_G3.cpp             |  2 +-
 Marlin/src/pins/stm32f1/pins_TRIGORILLA_PRO.h |  3 +
 .../MARLIN_BIGTREE_E3_RRF/hal_conf_extra.h    |  2 +-
 .../MARLIN_BIGTREE_GTR_V1/hal_conf_extra.h    |  2 +-
 .../hal_conf_extra.h                          |  2 +-
 .../hal_conf_extra.h                          |  2 +-
 .../MARLIN_F103VE_LONGER/hal_conf_custom.h    | 41 +++--------
 .../variants/MARLIN_F103Zx/PinNamesVar.h      |  3 +-
 .../variants/MARLIN_F103Zx/hal_conf_custom.h  | 71 ++++++++++---------
 ini/stm32f1.ini                               | 12 +++-
 10 files changed, 66 insertions(+), 74 deletions(-)

diff --git a/Marlin/src/gcode/motion/G2_G3.cpp b/Marlin/src/gcode/motion/G2_G3.cpp
index f9f9c2b3da..33167d9dff 100644
--- a/Marlin/src/gcode/motion/G2_G3.cpp
+++ b/Marlin/src/gcode/motion/G2_G3.cpp
@@ -217,7 +217,7 @@ void plan_arc(
    * tool precision in some cases. Therefore, arc path correction is implemented.
    *
    * Small angle approximation may be used to reduce computation overhead further. This approximation
-   * holds for everything, but very small circles and large MM_PER_ARC_SEGMENT values. In other words,
+   * holds for everything, but very small circles and large MAX_ARC_SEGMENT_MM values. In other words,
    * theta_per_segment would need to be greater than 0.1 rad and N_ARC_CORRECTION would need to be large
    * to cause an appreciable drift error. N_ARC_CORRECTION~=25 is more than small enough to correct for
    * numerical drift error. N_ARC_CORRECTION may be on the order a hundred(s) before error becomes an
diff --git a/Marlin/src/pins/stm32f1/pins_TRIGORILLA_PRO.h b/Marlin/src/pins/stm32f1/pins_TRIGORILLA_PRO.h
index 5b59a157f1..bf38955127 100644
--- a/Marlin/src/pins/stm32f1/pins_TRIGORILLA_PRO.h
+++ b/Marlin/src/pins/stm32f1/pins_TRIGORILLA_PRO.h
@@ -147,6 +147,9 @@
   #define FSMC_CS_PIN                       PD7   // NE4
   #define FSMC_RS_PIN                       PD11  // A0
 
+  #define TFT_CS_PIN                 FSMC_CS_PIN
+  #define TFT_RS_PIN                 FSMC_RS_PIN
+
   #define LCD_USE_DMA_FSMC                        // Use DMA transfers to send data to the TFT
   #define FSMC_DMA_DEV                      DMA2
   #define FSMC_DMA_CHANNEL               DMA_CH5
diff --git a/buildroot/share/PlatformIO/variants/MARLIN_BIGTREE_E3_RRF/hal_conf_extra.h b/buildroot/share/PlatformIO/variants/MARLIN_BIGTREE_E3_RRF/hal_conf_extra.h
index 6c4a991f33..2c1557cbbb 100644
--- a/buildroot/share/PlatformIO/variants/MARLIN_BIGTREE_E3_RRF/hal_conf_extra.h
+++ b/buildroot/share/PlatformIO/variants/MARLIN_BIGTREE_E3_RRF/hal_conf_extra.h
@@ -8,7 +8,7 @@
 #define HAL_I2C_MODULE_ENABLED
 #define HAL_PWR_MODULE_ENABLED
 #define HAL_RCC_MODULE_ENABLED
-//#define HAL_RTC_MODULE_ENABLED Real Time Clock...do we use it?
+//#define HAL_RTC_MODULE_ENABLED // Real Time Clock...do we use it?
 #define HAL_SPI_MODULE_ENABLED
 #define HAL_TIM_MODULE_ENABLED
 #define HAL_USART_MODULE_ENABLED
diff --git a/buildroot/share/PlatformIO/variants/MARLIN_BIGTREE_GTR_V1/hal_conf_extra.h b/buildroot/share/PlatformIO/variants/MARLIN_BIGTREE_GTR_V1/hal_conf_extra.h
index e6d558b3e1..40b340c205 100644
--- a/buildroot/share/PlatformIO/variants/MARLIN_BIGTREE_GTR_V1/hal_conf_extra.h
+++ b/buildroot/share/PlatformIO/variants/MARLIN_BIGTREE_GTR_V1/hal_conf_extra.h
@@ -8,7 +8,7 @@
 #define HAL_I2C_MODULE_ENABLED
 #define HAL_PWR_MODULE_ENABLED
 #define HAL_RCC_MODULE_ENABLED
-//#define HAL_RTC_MODULE_ENABLED Real Time Clock...do we use it?
+//#define HAL_RTC_MODULE_ENABLED // Real Time Clock...do we use it?
 #define HAL_SPI_MODULE_ENABLED
 #define HAL_TIM_MODULE_ENABLED
 #define HAL_USART_MODULE_ENABLED
diff --git a/buildroot/share/PlatformIO/variants/MARLIN_BIGTREE_OCTOPUS_V1/hal_conf_extra.h b/buildroot/share/PlatformIO/variants/MARLIN_BIGTREE_OCTOPUS_V1/hal_conf_extra.h
index da974b1ba7..42c76a4bbf 100644
--- a/buildroot/share/PlatformIO/variants/MARLIN_BIGTREE_OCTOPUS_V1/hal_conf_extra.h
+++ b/buildroot/share/PlatformIO/variants/MARLIN_BIGTREE_OCTOPUS_V1/hal_conf_extra.h
@@ -8,7 +8,7 @@
 #define HAL_I2C_MODULE_ENABLED
 #define HAL_PWR_MODULE_ENABLED
 #define HAL_RCC_MODULE_ENABLED
-//#define HAL_RTC_MODULE_ENABLED Real Time Clock...do we use it?
+//#define HAL_RTC_MODULE_ENABLED // Real Time Clock...do we use it?
 #define HAL_SPI_MODULE_ENABLED
 #define HAL_TIM_MODULE_ENABLED
 #define HAL_USART_MODULE_ENABLED
diff --git a/buildroot/share/PlatformIO/variants/MARLIN_BIGTREE_SKR_PRO_11/hal_conf_extra.h b/buildroot/share/PlatformIO/variants/MARLIN_BIGTREE_SKR_PRO_11/hal_conf_extra.h
index e6d558b3e1..40b340c205 100644
--- a/buildroot/share/PlatformIO/variants/MARLIN_BIGTREE_SKR_PRO_11/hal_conf_extra.h
+++ b/buildroot/share/PlatformIO/variants/MARLIN_BIGTREE_SKR_PRO_11/hal_conf_extra.h
@@ -8,7 +8,7 @@
 #define HAL_I2C_MODULE_ENABLED
 #define HAL_PWR_MODULE_ENABLED
 #define HAL_RCC_MODULE_ENABLED
-//#define HAL_RTC_MODULE_ENABLED Real Time Clock...do we use it?
+//#define HAL_RTC_MODULE_ENABLED // Real Time Clock...do we use it?
 #define HAL_SPI_MODULE_ENABLED
 #define HAL_TIM_MODULE_ENABLED
 #define HAL_USART_MODULE_ENABLED
diff --git a/buildroot/share/PlatformIO/variants/MARLIN_F103VE_LONGER/hal_conf_custom.h b/buildroot/share/PlatformIO/variants/MARLIN_F103VE_LONGER/hal_conf_custom.h
index e2247addb9..fd20d8fe6f 100644
--- a/buildroot/share/PlatformIO/variants/MARLIN_F103VE_LONGER/hal_conf_custom.h
+++ b/buildroot/share/PlatformIO/variants/MARLIN_F103VE_LONGER/hal_conf_custom.h
@@ -20,25 +20,18 @@ extern "C" {
   */
 #include "stm32yyxx_hal_conf.h"
 
-#ifdef HAL_PWR_MODULE_ENABLED
-  #undef HAL_PWR_MODULE_ENABLED // only way to disable it
-#endif
+#undef HAL_PWR_MODULE_ENABLED
+#define HAL_PWR_MODULE_ONLY   // disable low power & PA0 wakeup pin (its T°c pin)
 
-#if defined(HAL_PWR_MODULE_ENABLED) && !defined(HAL_PWR_MODULE_ONLY)
-  #define HAL_PWR_MODULE_ONLY // disable low power & PA0 wakeup pin (its T°c pin)
-#endif
-
-#ifndef HAL_IWDG_MODULE_ENABLED
-  #define HAL_IWDG_MODULE_ENABLED // USE_WATCHDOG
-#endif
+#define HAL_IWDG_MODULE_ENABLED // USE_WATCHDOG
 
 #ifdef HAL_PCD_MODULE_ENABLED
-  #warning No direct STM32 USB pins on Longer3D board
+  #warning "No direct STM32 USB pins on Longer3D board"
   #undef HAL_PCD_MODULE_ENABLED // USB Device
 #endif
 
 #ifdef HAL_HCD_MODULE_ENABLED
-  #warning No direct STM32 USB pins on Longer3D board
+  #warning "No direct STM32 USB pins on Longer3D board"
   #undef HAL_HCD_MODULE_ENABLED // USB Host
 #endif
 
@@ -46,25 +39,12 @@ extern "C" {
   //#define HAL_USART_MODULE_ENABLED // Useless.... UART_MODULE do it
 #endif
 
-#ifdef HAL_CAN_LEGACY_MODULE_ENABLED
-  #undef HAL_CAN_LEGACY_MODULE_ENABLED
-#endif
+#undef HAL_CAN_LEGACY_MODULE_ENABLED
+#undef HAL_CAN_MODULE_ENABLED
+#undef HAL_DAC_MODULE_ENABLED
+#undef HAL_RTC_MODULE_ENABLED
 
-#ifdef HAL_CAN_MODULE_ENABLED
-  #undef HAL_CAN_MODULE_ENABLED
-#endif
-
-#ifdef HAL_DAC_MODULE_ENABLED
-  #undef HAL_DAC_MODULE_ENABLED
-#endif
-
-#ifdef HAL_RTC_MODULE_ENABLED
-  #undef HAL_RTC_MODULE_ENABLED
-#endif
-
-#ifndef HAL_EXTI_MODULE_ENABLED
-  #define HAL_EXTI_MODULE_ENABLED // for ENDSTOP_INTERRUPTS_FEATURE
-#endif
+#define HAL_EXTI_MODULE_ENABLED // for ENDSTOP_INTERRUPTS_FEATURE
 
 /**
   * @brief List of modules in the framework (first ones enabled by default)
@@ -345,4 +325,3 @@ extern "C" {
 #ifdef __cplusplus
 }
 #endif
-
diff --git a/buildroot/share/PlatformIO/variants/MARLIN_F103Zx/PinNamesVar.h b/buildroot/share/PlatformIO/variants/MARLIN_F103Zx/PinNamesVar.h
index d9076b4dfb..b0259ab335 100644
--- a/buildroot/share/PlatformIO/variants/MARLIN_F103Zx/PinNamesVar.h
+++ b/buildroot/share/PlatformIO/variants/MARLIN_F103Zx/PinNamesVar.h
@@ -1,5 +1,6 @@
 /* SYS_WKUP */
-#ifdef PWR_WAKEUP_PIN1
+#if defined(PWR_WAKEUP_PIN1) && defined(HAL_PWR_MODULE_ENABLED) && !defined(HAL_PWR_MODULE_ONLY)
+  #error "PA0 is used by thermal sensor. Disable low power wake with -DHAL_PWR_MODULE_ONLY."
   SYS_WKUP1 = PA_0,
 #endif
 #ifdef PWR_WAKEUP_PIN2
diff --git a/buildroot/share/PlatformIO/variants/MARLIN_F103Zx/hal_conf_custom.h b/buildroot/share/PlatformIO/variants/MARLIN_F103Zx/hal_conf_custom.h
index 4e55fe1240..7d19d532a5 100644
--- a/buildroot/share/PlatformIO/variants/MARLIN_F103Zx/hal_conf_custom.h
+++ b/buildroot/share/PlatformIO/variants/MARLIN_F103Zx/hal_conf_custom.h
@@ -37,40 +37,43 @@ extern "C" {
 /**
   * @brief This is the list of modules to be used in the HAL driver
   */
-//#define HAL_MODULE_ENABLED
-//#define HAL_ADC_MODULE_ENABLED
-//#define HAL_CAN_MODULE_ENABLED
-//#define HAL_CAN_LEGACY_MODULE_ENABLED
-//#define HAL_CEC_MODULE_ENABLED
-//#define HAL_CORTEX_MODULE_ENABLED
-//#define HAL_CRC_MODULE_ENABLED
-//#define HAL_DAC_MODULE_ENABLED
-//#define HAL_DMA_MODULE_ENABLED
-//#define HAL_ETH_MODULE_ENABLED
-//#define HAL_EXTI_MODULE_ENABLED
-//#define HAL_FLASH_MODULE_ENABLED
-//#define HAL_GPIO_MODULE_ENABLED
-//#define HAL_HCD_MODULE_ENABLED
-//#define HAL_I2C_MODULE_ENABLED
-//#define HAL_I2S_MODULE_ENABLED
-//#define HAL_IRDA_MODULE_ENABLED
-//#define HAL_IWDG_MODULE_ENABLED
-//#define HAL_NAND_MODULE_ENABLED
-//#define HAL_NOR_MODULE_ENABLED
-//#define HAL_PCCARD_MODULE_ENABLED
-//#define HAL_PCD_MODULE_ENABLED
-//#define HAL_PWR_MODULE_ENABLED
-//#define HAL_RCC_MODULE_ENABLED
-//#define HAL_RTC_MODULE_ENABLED
-//#define HAL_SD_MODULE_ENABLED
-//#define HAL_SMARTCARD_MODULE_ENABLED
-//#define HAL_SPI_MODULE_ENABLED
-//#define HAL_SRAM_MODULE_ENABLED
-//#define HAL_TIM_MODULE_ENABLED
-//#define HAL_UART_MODULE_ENABLED
-//#define HAL_USART_MODULE_ENABLED
-//#define HAL_WWDG_MODULE_ENABLED
-//#define HAL_MMC_MODULE_ENABLED
+#define HAL_MODULE_ENABLED
+#define HAL_ADC_MODULE_ENABLED
+#define HAL_CRC_MODULE_ENABLED
+#define HAL_DMA_MODULE_ENABLED
+#define HAL_EXTI_MODULE_ENABLED
+#define HAL_FLASH_MODULE_ENABLED
+#define HAL_GPIO_MODULE_ENABLED
+#define HAL_I2C_MODULE_ENABLED
+#define HAL_IWDG_MODULE_ENABLED
+#define HAL_RCC_MODULE_ENABLED
+#define HAL_SPI_MODULE_ENABLED
+#define HAL_SRAM_MODULE_ENABLED
+#define HAL_TIM_MODULE_ENABLED
+#define HAL_USART_MODULE_ENABLED
+#define HAL_CORTEX_MODULE_ENABLED
+//#define HAL_UART_MODULE_ENABLED // by default
+//#define HAL_PCD_MODULE_ENABLED  // Since STM32 v3.10700.191028 this is automatically added if any type of USB is enabled (as in Arduino IDE)
+#define HAL_SD_MODULE_ENABLED
+
+#undef HAL_CAN_MODULE_ENABLED
+#undef HAL_CAN_LEGACY_MODULE_ENABLED
+#undef HAL_CEC_MODULE_ENABLED
+#undef HAL_DAC_MODULE_ENABLED
+#undef HAL_ETH_MODULE_ENABLED
+#undef HAL_HCD_MODULE_ENABLED         // USB Host
+#undef HAL_I2S_MODULE_ENABLED
+#undef HAL_IRDA_MODULE_ENABLED
+#undef HAL_NAND_MODULE_ENABLED
+#undef HAL_NOR_MODULE_ENABLED
+#undef HAL_PCCARD_MODULE_ENABLED
+#undef HAL_PWR_MODULE_ENABLED
+#undef HAL_RTC_MODULE_ENABLED
+#undef HAL_SMARTCARD_MODULE_ENABLED
+#undef HAL_WWDG_MODULE_ENABLED
+#undef HAL_MMC_MODULE_ENABLED
+
+#define HAL_PWR_MODULE_ONLY
 
 /* ########################## Oscillator Values adaptation ####################*/
 /**
diff --git a/ini/stm32f1.ini b/ini/stm32f1.ini
index f1cb078fd8..a27d275fc1 100644
--- a/ini/stm32f1.ini
+++ b/ini/stm32f1.ini
@@ -348,9 +348,15 @@ debug_tool                  = stlink
 # TRIGORILLA PRO (STM32F103ZET6)
 #
 [env:trigorilla_pro]
-platform      = ${common_stm32.platform}
-extends       = env:mks_robin
-extra_scripts = ${common_stm32.extra_scripts}
+platform             = ${stm32_variant.platform}
+extends              = stm32_variant
+board                = genericSTM32F103ZE
+board_build.variant  = MARLIN_F103Zx
+board_build.offset   = 0x7000
+build_flags          = ${stm32_variant.build_flags}
+                       -DENABLE_HWSERIAL3 -DTIMER_SERIAL=TIM5
+build_unflags        = ${stm32_variant.build_unflags}
+                       -DUSBCON -DUSBD_USE_CDC
 
 #
 # Chitu boards like Tronxy X5s (STM32F103ZET6)