From c914761327077cb0e715171bd0fd5db741eafeb9 Mon Sep 17 00:00:00 2001
From: Michael Hill <mhill@hillsoftware.com>
Date: Sat, 18 Mar 2023 01:56:07 -0400
Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Sovol=20v1.3.1=20(Sovol=20SV06)=20(?=
 =?UTF-8?q?#25267)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 Marlin/src/core/boards.h                      |   1 +
 Marlin/src/inc/Warnings.cpp                   |   7 +
 Marlin/src/pins/pins.h                        |   2 +
 Marlin/src/pins/stm32f1/pins_CREALITY_V4.h    |   1 +
 Marlin/src/pins/stm32f1/pins_SOVOL_V131.h     | 236 ++++++++++++++++++
 buildroot/share/PlatformIO/ldscripts/sovol.ld |  14 ++
 ini/stm32f1-maple.ini                         |  14 ++
 7 files changed, 275 insertions(+)
 create mode 100644 Marlin/src/pins/stm32f1/pins_SOVOL_V131.h
 create mode 100644 buildroot/share/PlatformIO/ldscripts/sovol.ld

diff --git a/Marlin/src/core/boards.h b/Marlin/src/core/boards.h
index f861e8864f4..8a1cec714ee 100644
--- a/Marlin/src/core/boards.h
+++ b/Marlin/src/core/boards.h
@@ -381,6 +381,7 @@
 #define BOARD_ZONESTAR_ZM3E4V2        5064  // Zonestar ZM3E4 V2 (STM32F103VC)
 #define BOARD_ERYONE_ERY32_MINI       5065  // Eryone Ery32 mini (STM32F103VE)
 #define BOARD_PANDA_PI_V29            5066  // Panda Pi V2.9 - Standalone (STM32F103RC)
+#define BOARD_SOVOL_V131              5067  // Sovol V1.3.1 (GD32F103RET6)
 
 //
 // ARM Cortex-M4F
diff --git a/Marlin/src/inc/Warnings.cpp b/Marlin/src/inc/Warnings.cpp
index 184d1a08da6..55b9a18e77e 100644
--- a/Marlin/src/inc/Warnings.cpp
+++ b/Marlin/src/inc/Warnings.cpp
@@ -777,6 +777,13 @@
   #warning "Define MK3_FAN_PINS to swap hotend and part cooling fan pins. (Define NO_MK3_FAN_PINS_WARNING to suppress this warning.)"
 #endif
 
+/**
+ * GD32 is not exactly like STM32
+ */
+#if MB(SOVOL_V131)
+  #warning "GD32 based controllers may not be fully compatible with Maple Generic STM32F103RE. Please report any issues."
+#endif
+
 /**
  * BD Sensor should always include BABYSTEPPING
  */
diff --git a/Marlin/src/pins/pins.h b/Marlin/src/pins/pins.h
index 721e037b8c0..6269eecb0b0 100644
--- a/Marlin/src/pins/pins.h
+++ b/Marlin/src/pins/pins.h
@@ -627,6 +627,8 @@
   #include "stm32f1/pins_ERYONE_ERY32_MINI.h"   // STM32F103VET6                          env:ERYONE_ERY32_MINI_maple
 #elif MB(PANDA_PI_V29)
   #include "stm32f1/pins_PANDA_PI_V29.h"        // STM32F103RCT6                          env:PANDA_PI_V29
+#elif MB(SOVOL_V131)
+  #include "stm32f1/pins_SOVOL_V131.h"          // GD32F1                                 env:GD32F103RET6_sovol_maple
 
 //
 // ARM Cortex-M4F
diff --git a/Marlin/src/pins/stm32f1/pins_CREALITY_V4.h b/Marlin/src/pins/stm32f1/pins_CREALITY_V4.h
index f633ee0983c..7c4df3520ce 100644
--- a/Marlin/src/pins/stm32f1/pins_CREALITY_V4.h
+++ b/Marlin/src/pins/stm32f1/pins_CREALITY_V4.h
@@ -53,6 +53,7 @@
 #if NO_EEPROM_SELECTED
   #define IIC_BL24CXX_EEPROM                      // EEPROM on I2C-0
   //#define SDCARD_EEPROM_EMULATION
+  #undef NO_EEPROM_SELECTED
 #endif
 
 #if ENABLED(IIC_BL24CXX_EEPROM)
diff --git a/Marlin/src/pins/stm32f1/pins_SOVOL_V131.h b/Marlin/src/pins/stm32f1/pins_SOVOL_V131.h
new file mode 100644
index 00000000000..67b2944f456
--- /dev/null
+++ b/Marlin/src/pins/stm32f1/pins_SOVOL_V131.h
@@ -0,0 +1,236 @@
+/**
+ * Marlin 3D Printer Firmware
+ * Copyright (c) 2023 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
+
+/**
+ * Sovol 1.3.1 (GD32F103RET6) board pin assignments
+ */
+
+#include "env_validate.h"
+
+#if HOTENDS > 1 || E_STEPPERS > 1
+  #error "SOVOL V131 only supports 1 hotend / E-stepper."
+#endif
+
+#ifndef BOARD_INFO_NAME
+  #define BOARD_INFO_NAME      "Sovol V131"
+#endif
+#ifndef DEFAULT_MACHINE_NAME
+  #define DEFAULT_MACHINE_NAME "Sovol SV06"
+#endif
+
+//#define BOARD_NO_NATIVE_USB
+
+//
+// Release PB4 (Y_ENABLE_PIN) from JTAG NRST role
+//
+#ifndef DISABLE_DEBUG
+  #define DISABLE_DEBUG
+#endif
+
+//
+// EEPROM
+//
+#if NO_EEPROM_SELECTED
+  #define IIC_BL24CXX_EEPROM                      // EEPROM on I2C-0
+  //#define SDCARD_EEPROM_EMULATION
+  #undef NO_EEPROM_SELECTED
+#endif
+
+#if ENABLED(IIC_BL24CXX_EEPROM)
+  #define IIC_EEPROM_SDA                    PA11
+  #define IIC_EEPROM_SCL                    PA12
+  #define MARLIN_EEPROM_SIZE               0x800  // 2K (24C16)
+#elif ENABLED(SDCARD_EEPROM_EMULATION)
+  #define MARLIN_EEPROM_SIZE               0x800  // 2K
+#endif
+
+//
+// Servos
+//
+#ifndef SERVO0_PIN
+  #ifndef HAS_PIN_27_BOARD
+    #define SERVO0_PIN                      PB0   // BLTouch OUT
+  #else
+    #define SERVO0_PIN                      PC6
+  #endif
+#endif
+
+//
+// Limit Switches
+//
+#ifndef X_STOP_PIN
+  #define X_STOP_PIN                        PA5
+#endif
+#ifndef Y_STOP_PIN
+  #define Y_STOP_PIN                        PA6
+#endif
+#ifndef Z_STOP_PIN
+  #define Z_STOP_PIN                        PA7
+#endif
+
+#ifndef Z_MIN_PROBE_PIN
+  #define Z_MIN_PROBE_PIN                   PB1   // BLTouch IN
+#endif
+
+//
+// Filament Runout Sensor
+//
+#ifndef FIL_RUNOUT_PIN
+  #define FIL_RUNOUT_PIN                    PA4   // "Pulled-high"
+#endif
+
+//
+// Steppers
+//
+#ifndef X_STEP_PIN
+  #define X_STEP_PIN                        PC2
+#endif
+#ifndef X_DIR_PIN
+  #define X_DIR_PIN                         PB9
+#endif
+#define X_ENABLE_PIN                        PC3   // Shared
+
+#ifndef Y_STEP_PIN
+  #define Y_STEP_PIN                        PB8
+#endif
+#ifndef Y_DIR_PIN
+  #define Y_DIR_PIN                         PB7
+#endif
+#define Y_ENABLE_PIN                X_ENABLE_PIN
+
+#ifndef Z_STEP_PIN
+  #define Z_STEP_PIN                        PB6
+#endif
+#ifndef Z_DIR_PIN
+  #define Z_DIR_PIN                         PB5
+#endif
+#define Z_ENABLE_PIN                X_ENABLE_PIN
+
+#ifndef E0_STEP_PIN
+  #define E0_STEP_PIN                       PB4
+#endif
+#ifndef E0_DIR_PIN
+  #define E0_DIR_PIN                        PB3
+#endif
+#define E0_ENABLE_PIN               X_ENABLE_PIN
+
+#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_SERIAL_TX_PIN                   PC1
+  #define X_SERIAL_RX_PIN                   PC1
+
+  #define Y_SERIAL_TX_PIN                   PC0
+  #define Y_SERIAL_RX_PIN                   PC0
+
+  #define Z_SERIAL_TX_PIN                   PA15
+  #define Z_SERIAL_RX_PIN                   PA15
+
+  #define E0_SERIAL_TX_PIN                  PC14
+  #define E0_SERIAL_RX_PIN                  PC14
+
+  // Reduce baud rate to improve software serial reliability
+  #define TMC_BAUD_RATE                    19200
+
+#endif // HAS_TMC_UART
+
+//
+// Temperature Sensors
+//
+#define TEMP_0_PIN                          PC5   // TH1
+#define TEMP_BED_PIN                        PC4   // TB1
+
+//
+// Heaters / Fans
+//
+#ifndef HEATER_0_PIN
+  #define HEATER_0_PIN                      PA1   // HEATER1
+#endif
+#ifndef HEATER_BED_PIN
+  #define HEATER_BED_PIN                    PA2   // HOT BED
+#endif
+#ifndef FAN_PIN
+  #define FAN_PIN                           PA0   // FAN
+#endif
+#define FAN_SOFT_PWM_REQUIRED
+
+//
+// SD Card
+//
+#define SD_DETECT_PIN                       PC7
+#define SDCARD_CONNECTION ONBOARD
+#define ONBOARD_SPI_DEVICE                     1
+#define ONBOARD_SD_CS_PIN                   PA4   // SDSS
+#define SDIO_SUPPORT
+#define NO_SD_HOST_DRIVE                          // This board's SD is only seen by the printer
+
+#if ANY(RET6_12864_LCD, HAS_DWIN_E3V2, IS_DWIN_MARLINUI)
+  /**
+   *    RET6 12864 LCD
+   *        ------
+   *  PC6  | 1  2 | PB2
+   *  PB10 | 3  4 | PB11
+   *  PB14   5  6 | PB13
+   *  PB12 | 7  8 | PB15
+   *   GND | 9 10 | 5V
+   *        ------
+   */
+  #define EXP3_01_PIN                       PC6
+  #define EXP3_02_PIN                       PB2
+  #define EXP3_03_PIN                       PB10
+  #define EXP3_04_PIN                       PB11
+  #define EXP3_05_PIN                       PB14
+  #define EXP3_06_PIN                       PB13
+  #define EXP3_07_PIN                       PB12
+  #define EXP3_08_PIN                       PB15
+#endif
+
+#if ENABLED(CR10_STOCKDISPLAY)
+
+  #if ENABLED(RET6_12864_LCD)
+
+    #define LCD_PINS_RS              EXP3_07_PIN
+    #define LCD_PINS_ENABLE          EXP3_08_PIN
+    #define LCD_PINS_D4              EXP3_06_PIN
+
+    #define BTN_ENC                  EXP3_02_PIN
+    #define BTN_EN1                  EXP3_03_PIN
+    #define BTN_EN2                  EXP3_05_PIN
+
+    #ifndef HAS_PIN_27_BOARD
+      #define BEEPER_PIN             EXP3_01_PIN
+    #endif
+
+  #else
+
+    #error "Only the RET6_12864_LCD variant of CR10_STOCKDISPLAY is supported."
+
+  #endif
+
+#endif
diff --git a/buildroot/share/PlatformIO/ldscripts/sovol.ld b/buildroot/share/PlatformIO/ldscripts/sovol.ld
new file mode 100644
index 00000000000..785345543c0
--- /dev/null
+++ b/buildroot/share/PlatformIO/ldscripts/sovol.ld
@@ -0,0 +1,14 @@
+MEMORY
+{
+  ram (rwx) : ORIGIN = 0x20000000, LENGTH = 64K - 40
+  rom (rx)  : ORIGIN = 0x08007000, LENGTH = 512K - 28K
+}
+
+/* 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/ini/stm32f1-maple.ini b/ini/stm32f1-maple.ini
index be7966be9d8..e223bdb4ebb 100644
--- a/ini/stm32f1-maple.ini
+++ b/ini/stm32f1-maple.ini
@@ -392,3 +392,17 @@ board_build.variant  = MARLIN_F103Vx
 board_build.ldscript = eryone_ery32_mini.ld
 board_build.address  = 0x08004000
 build_unflags        = ${STM32F1_maple.build_unflags}
+
+#
+# Sovol (GD32F103RET6)
+#
+[env:GD32F103RET6_sovol_maple]
+extends              = env:STM32F103RE_maple
+build_flags          = ${STM32F1_maple.build_flags} -DTEMP_TIMER_CHAN=4
+board_build.address  = 0x08007000
+board_build.ldscript = sovol.ld
+extra_scripts        = ${STM32F1_maple.extra_scripts}
+  pre:buildroot/share/PlatformIO/scripts/random-bin.py
+  buildroot/share/PlatformIO/scripts/custom_board.py
+debug_tool           = jlink
+upload_protocol      = jlink