From bc68664c3b198599c4ea4095313f79e78c01396a Mon Sep 17 00:00:00 2001
From: Tanguy Pruvot <tpruvot@users.noreply.github.com>
Date: Mon, 9 Aug 2021 04:37:27 +0200
Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=91=EF=B8=8F=20Init=20FastIO=20before?=
 =?UTF-8?q?=20anything=20else=20(#22508)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 Marlin/src/HAL/STM32/HAL.cpp       | 2 --
 Marlin/src/HAL/STM32/fastio.cpp    | 2 +-
 Marlin/src/HAL/STM32/fastio.h      | 1 +
 Marlin/src/HAL/STM32F1/pinsDebug.h | 6 +++---
 Marlin/src/MarlinCore.cpp          | 4 ++++
 5 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/Marlin/src/HAL/STM32/HAL.cpp b/Marlin/src/HAL/STM32/HAL.cpp
index d8035a979d..395a22171b 100644
--- a/Marlin/src/HAL/STM32/HAL.cpp
+++ b/Marlin/src/HAL/STM32/HAL.cpp
@@ -61,8 +61,6 @@ TERN_(POSTMORTEM_DEBUGGING, extern void install_min_serial());
 
 // HAL initialization task
 void HAL_init() {
-  FastIO_init();
-
   // Ensure F_CPU is a constant expression.
   // If the compiler breaks here, it means that delay code that should compute at compile time will not work.
   // So better safe than sorry here.
diff --git a/Marlin/src/HAL/STM32/fastio.cpp b/Marlin/src/HAL/STM32/fastio.cpp
index 5056e99d35..7840be845f 100644
--- a/Marlin/src/HAL/STM32/fastio.cpp
+++ b/Marlin/src/HAL/STM32/fastio.cpp
@@ -24,7 +24,7 @@
 
 #include "../../inc/MarlinConfig.h"
 
-GPIO_TypeDef* FastIOPortMap[LastPort + 1];
+GPIO_TypeDef* FastIOPortMap[LastPort + 1] = { 0 };
 
 void FastIO_init() {
   LOOP_L_N(i, NUM_DIGITAL_PINS)
diff --git a/Marlin/src/HAL/STM32/fastio.h b/Marlin/src/HAL/STM32/fastio.h
index 17751c44dd..4a48954471 100644
--- a/Marlin/src/HAL/STM32/fastio.h
+++ b/Marlin/src/HAL/STM32/fastio.h
@@ -38,6 +38,7 @@ extern GPIO_TypeDef * FastIOPortMap[];
 // ------------------------
 
 void FastIO_init(); // Must be called before using fast io macros
+#define FASTIO_INIT() FastIO_init()
 
 // ------------------------
 // Defines
diff --git a/Marlin/src/HAL/STM32F1/pinsDebug.h b/Marlin/src/HAL/STM32F1/pinsDebug.h
index 8e7a3d8135..b018a0fc8c 100644
--- a/Marlin/src/HAL/STM32F1/pinsDebug.h
+++ b/Marlin/src/HAL/STM32F1/pinsDebug.h
@@ -19,15 +19,15 @@
 #pragma once
 
 /**
- * Support routines for STM32GENERIC (Maple)
+ * Support routines for MAPLE_STM32F1
  */
 
 /**
  * Translation of routines & variables used by pinsDebug.h
  */
 
-#ifndef BOARD_NR_GPIO_PINS  // Only in STM32GENERIC (Maple)
-   #error "Expected BOARD_NR_GPIO_PINS not found"
+#ifndef BOARD_NR_GPIO_PINS // Only in MAPLE_STM32F1
+  #error "Expected BOARD_NR_GPIO_PINS not found"
 #endif
 
 #include "fastio.h"
diff --git a/Marlin/src/MarlinCore.cpp b/Marlin/src/MarlinCore.cpp
index d77993631a..46543e9dc9 100644
--- a/Marlin/src/MarlinCore.cpp
+++ b/Marlin/src/MarlinCore.cpp
@@ -1126,6 +1126,10 @@ inline void tmc_standby_setup() {
  *  - Set Marlin to RUNNING State
  */
 void setup() {
+  #ifdef FASTIO_INIT
+    FASTIO_INIT();
+  #endif
+
   #ifdef BOARD_PREINIT
     BOARD_PREINIT(); // Low-level init (before serial init)
   #endif