From c9718e1ec0570a96bd104cd4bbefed57cc613d5d Mon Sep 17 00:00:00 2001
From: Augusto Zanellato <augusto.zanellato@gmail.com>
Date: Tue, 19 Oct 2021 17:24:22 +0200
Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Eryone=20Ery32=20mini=20(STM32F103V?=
 =?UTF-8?q?ET6)=20board=20(#22956)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 Marlin/src/core/boards.h                      |   1 +
 Marlin/src/pins/pins.h                        |   2 +
 .../src/pins/stm32f1/pins_ERYONE_ERY32_MINI.h | 195 ++++++++++++++++++
 .../PlatformIO/ldscripts/eryone_ery32_mini.ld |  14 ++
 .../variants/marlin_MEEB_3DP/ld/mem-flash.inc |   4 +-
 ini/stm32f1-maple.ini                         |  19 ++
 6 files changed, 233 insertions(+), 2 deletions(-)
 create mode 100644 Marlin/src/pins/stm32f1/pins_ERYONE_ERY32_MINI.h
 create mode 100644 buildroot/share/PlatformIO/ldscripts/eryone_ery32_mini.ld

diff --git a/Marlin/src/core/boards.h b/Marlin/src/core/boards.h
index dd08f6a1b4..82fd51e2a3 100644
--- a/Marlin/src/core/boards.h
+++ b/Marlin/src/core/boards.h
@@ -355,6 +355,7 @@
 #define BOARD_ZONESTAR_ZM3E2          4055  // Zonestar ZM3E2    (STM32F103RCT6)
 #define BOARD_ZONESTAR_ZM3E4          4056  // Zonestar ZM3E4 V1 (STM32F103VCT6)
 #define BOARD_ZONESTAR_ZM3E4V2        4057  // Zonestar ZM3E4 V2 (STM32F103VCT6)
+#define BOARD_ERYONE_ERY32_MINI       4058  // Eryone Ery32 mini (STM32F103VET6)
 
 //
 // ARM Cortex-M4F
diff --git a/Marlin/src/pins/pins.h b/Marlin/src/pins/pins.h
index 14a13219dd..15794641ca 100644
--- a/Marlin/src/pins/pins.h
+++ b/Marlin/src/pins/pins.h
@@ -571,6 +571,8 @@
   #include "stm32f1/pins_ZM3E4_V1_0.h"          // STM32F1                                env:STM32F103VC_ZM3E4_USB env:STM32F103VC_ZM3E4_USB_maple
 #elif MB(ZONESTAR_ZM3E4V2)
   #include "stm32f1/pins_ZM3E4_V2_0.h"          // STM32F1                                env:STM32F103VE_ZM3E4V2_USB env:STM32F103VE_ZM3E4V2_USB_maple
+#elif MB(ERYONE_ERY32_MINI)
+  #include "stm32f1/pins_ERYONE_ERY32_MINI.h"   // STM32F103VET6                          env:ERYONE_ERY32_MINI_maple
 
 //
 // ARM Cortex-M4F
diff --git a/Marlin/src/pins/stm32f1/pins_ERYONE_ERY32_MINI.h b/Marlin/src/pins/stm32f1/pins_ERYONE_ERY32_MINI.h
new file mode 100644
index 0000000000..7ce21a20af
--- /dev/null
+++ b/Marlin/src/pins/stm32f1/pins_ERYONE_ERY32_MINI.h
@@ -0,0 +1,195 @@
+/**
+ * Marlin 3D Printer Firmware
+ * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
+ *
+ * Based on Sprinter and grbl.
+ * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+#pragma once
+
+/**
+ * Eryone Ery32 mini (STM32F103VET6) board pin assignments
+ */
+
+#include "env_validate.h"
+
+#if HOTENDS > 2 || E_STEPPERS > 2
+  #error "Eryone Ery32 mini supports up to 2 hotends / E-steppers. Comment out this line to continue."
+#endif
+
+#ifndef BOARD_INFO_NAME
+  #define BOARD_INFO_NAME "ERYONE Ery32 mini"
+#endif
+
+//#define DISABLE_DEBUG
+#define DISABLE_JTAG
+//#define ENABLE_SPI3
+#define FLASH_EEPROM_EMULATION
+#define FAN_SOFT_PWM
+
+#if ENABLED(FLASH_EEPROM_EMULATION)
+  #define EEPROM_PAGE_SIZE     (0x800U)           // 2KB
+  #define EEPROM_START_ADDRESS (0x8000000UL + (STM32_FLASH_SIZE) * 1024UL - (EEPROM_PAGE_SIZE) * 2UL)
+  #define MARLIN_EEPROM_SIZE (EEPROM_PAGE_SIZE)
+#endif
+
+//
+// Servos
+//
+#define SERVO0_PIN                          PA12
+
+//
+// Limit Switches
+// 
+#define X_STOP_PIN                          PD8
+#define Y_STOP_PIN                          PD15
+#define Z_MIN_PIN                           PA11
+//#define Z_MAX_PIN                         PB1
+
+//
+// Steppers
+//
+#define X_STEP_PIN                          PB15
+#define X_DIR_PIN                           PB14
+#define X_ENABLE_PIN                        PD10
+
+#define Y_STEP_PIN                          PD14
+#define Y_DIR_PIN                           PD13
+#define Y_ENABLE_PIN                        PC6
+
+#define Z_STEP_PIN                          PC8
+#define Z_DIR_PIN                           PC7
+#define Z_ENABLE_PIN                        PA8
+
+#define E0_STEP_PIN                         PE13
+#define E0_DIR_PIN                          PE14
+#define E0_ENABLE_PIN                       PB13
+
+//#define E1_STEP_PIN                       PD13
+//#define E1_DIR_PIN                        PD12
+//#define E1_ENABLE_PIN                     PC6
+
+//
+// Heaters 0,1 / Fans / Bed
+//
+#define HEATER_0_PIN                        PD11
+
+#if ENABLED(FET_ORDER_EFB)                        // Hotend, Fan, Bed
+  #define HEATER_BED_PIN                    PD12
+#elif ENABLED(FET_ORDER_EEF)                      // Hotend, Hotend, Fan
+  #define HEATER_1_PIN                      PD4
+#elif ENABLED(FET_ORDER_EEB)                      // Hotend, Hotend, Bed
+  #define HEATER_1_PIN                      PD4
+  #define HEATER_BED_PIN                    PD12
+#elif ENABLED(FET_ORDER_EFF)                      // Hotend, Fan, Fan
+  #define FAN1_PIN                          PD12
+#elif DISABLED(FET_ORDER_SF)                      // Not Spindle, Fan (i.e., "EFBF" or "EFBE")
+  #define HEATER_BED_PIN                    PD12
+  #if EITHER(HAS_MULTI_HOTEND, HEATERS_PARALLEL)
+    #define HEATER_1_PIN                    PB9
+  #else
+    #define FAN1_PIN                        PB9
+  #endif
+#endif
+
+#ifndef FAN_PIN
+  #if EITHER(FET_ORDER_EFB, FET_ORDER_EFF)        // Hotend, Fan, Bed or Hotend, Fan, Fan
+    #define FAN_PIN                         PB5
+  #elif EITHER(FET_ORDER_EEF, FET_ORDER_SF)       // Hotend, Hotend, Fan or Spindle, Fan
+    #define FAN_PIN                         PD12
+  #elif ENABLED(FET_ORDER_EEB)                    // Hotend, Hotend, Bed
+    #define FAN_PIN                         -1    // IO pin. Buffer needed
+  #else                                           // Non-specific are "EFB" (i.e., "EFBF" or "EFBE")
+    #define FAN_PIN                         PB5
+  #endif
+#endif
+
+//
+// Misc. Functions
+//
+//#define PS_ON_PIN                         PB9
+
+#if HAS_TMC_UART
+  /**
+   * TMC2208/TMC2209 stepper drivers
+   *
+   * Hardware serial communication ports.
+   * If undefined software serial is used according to the pins below
+   */
+  #define X_HARDWARE_SERIAL  MSerial4
+  #define Y_HARDWARE_SERIAL  MSerial4
+  #define Z_HARDWARE_SERIAL  MSerial4
+  #define E0_HARDWARE_SERIAL MSerial4
+  #ifndef X_SLAVE_ADDRESS
+    #define X_SLAVE_ADDRESS                    2
+  #endif
+  #ifndef Y_SLAVE_ADDRESS
+    #define Y_SLAVE_ADDRESS                    3
+  #endif
+  #ifndef Z_SLAVE_ADDRESS
+    #define Z_SLAVE_ADDRESS                    1
+  #endif
+  #ifndef E0_SLAVE_ADDRESS
+    #define E0_SLAVE_ADDRESS                   0
+  #endif
+#endif
+//
+// Temperature Sensors
+//
+#define TEMP_BED_PIN                        PC2   //TB
+#define TEMP_0_PIN                          PC1   //TH1
+//#define TEMP_1_PIN                        PC3   //TH2
+#define TEMP_BOARD_PIN                      PC3
+#ifndef TEMP_SENSOR_BOARD
+  #define TEMP_SENSOR_BOARD                   13
+#endif
+#define FIL_RUNOUT_PIN                      PA10  // MT_DET
+
+//
+// LCD Pins
+//
+#if HAS_WIRED_LCD
+  #define BEEPER_PIN                        PE12
+  #define BTN_ENC                           PE11
+  #define LCD_PINS_ENABLE                   PE10
+  #define LCD_PINS_RS                       PE9
+  #define BTN_EN1                           PE4
+  #define BTN_EN2                           PE3
+  #define LCD_PINS_D4                       PE8
+  #define LCD_PINS_D5                       PE7
+  #define LCD_PINS_D6                       PB2
+  #define LCD_PINS_D7                       PB1
+  
+  #if ENABLED(REPRAP_DISCOUNT_FULL_GRAPHIC_SMART_CONTROLLER)
+    #define BTN_ENC_EN               LCD_PINS_D7  // Detect the presence of the encoder
+  #endif
+
+  #define BOARD_ST7920_DELAY_1                50
+  #define BOARD_ST7920_DELAY_2                50
+  #define BOARD_ST7920_DELAY_3                50
+
+#endif // HAS_WIRED_LCD
+
+//
+// SD Card
+//
+#define ENABLE_SPI1
+#define SD_DETECT_PIN                       PA4
+#define SCK_PIN                             PA5
+#define MISO_PIN                            PA6
+#define MOSI_PIN                            PA7
+#define SS_PIN                              PC4
diff --git a/buildroot/share/PlatformIO/ldscripts/eryone_ery32_mini.ld b/buildroot/share/PlatformIO/ldscripts/eryone_ery32_mini.ld
new file mode 100644
index 0000000000..0458574256
--- /dev/null
+++ b/buildroot/share/PlatformIO/ldscripts/eryone_ery32_mini.ld
@@ -0,0 +1,14 @@
+MEMORY
+{
+  ram (rwx) : ORIGIN = 0x20000000, LENGTH =  64K -  4K
+  rom (rx)  : ORIGIN = 0x08004000, LENGTH = 512K - 16K
+}
+
+/* Provide memory region aliases for common.inc */
+REGION_ALIAS("REGION_TEXT", rom);
+REGION_ALIAS("REGION_DATA", ram);
+REGION_ALIAS("REGION_BSS", ram);
+REGION_ALIAS("REGION_RODATA", rom);
+
+/* Let common.inc handle the real work. */
+INCLUDE common.inc
diff --git a/buildroot/share/PlatformIO/variants/marlin_MEEB_3DP/ld/mem-flash.inc b/buildroot/share/PlatformIO/variants/marlin_MEEB_3DP/ld/mem-flash.inc
index ddb8876fa5..75030ec01a 100644
--- a/buildroot/share/PlatformIO/variants/marlin_MEEB_3DP/ld/mem-flash.inc
+++ b/buildroot/share/PlatformIO/variants/marlin_MEEB_3DP/ld/mem-flash.inc
@@ -1,5 +1,5 @@
 MEMORY
 {
-  ram (rwx) : ORIGIN = 0x20000C00, LENGTH = 61K
-  rom (rx)  : ORIGIN = 0x08005000, LENGTH = 492K
+  ram (rwx) : ORIGIN = 0x20000C00, LENGTH = 64K - 3K
+  rom (rx)  : ORIGIN = 0x08005000, LENGTH = 512K - 20K
 }
diff --git a/ini/stm32f1-maple.ini b/ini/stm32f1-maple.ini
index e11aa03560..f86bf98ebd 100644
--- a/ini/stm32f1-maple.ini
+++ b/ini/stm32f1-maple.ini
@@ -400,3 +400,22 @@ board                = genericSTM32F103VE
 board_build.ldscript = ZONESTAR_ZM3E_512K.ld
 build_flags          = ${ZONESTAR_ZM3E_maple.build_flags} -DTONE_TIMER=1 -DTONE_CHANNEL=2
 board_upload.maximum_size = 499712
+
+#
+# ERYONE ERY32 Mini (STM32F103VET6)
+#
+[env:ERYONE_ERY32_MINI_maple]
+platform             = ${common_stm32f1.platform}
+extends              = common_stm32f1
+board                = genericSTM32F103VE
+build_flags          = ${common_stm32f1.build_flags}
+  -ffunction-sections -fdata-sections -nostdlib -MMD
+  -DMCU_STM32F103VE -DARDUINO_GENERIC_STM32F103V -DARDUINO_ARCH_STM32F1 -O0
+  -DDEBUG_LEVEL=DEBUG_NONE -DCONFIG_MAPLE_MINI_NO_DISABLE_DEBUG=1
+  -DSS_TIMER=4
+board_build.variant  = MARLIN_F103Vx
+board_build.ldscript = eryone_ery32_mini.ld
+board_build.address  = 0x08004000
+build_unflags        = ${common_stm32f1.build_unflags}
+extra_scripts        = ${common_stm32f1.extra_scripts}
+  buildroot/share/PlatformIO/scripts/custom_board.py