From 0cc03f912cf0a7f0923bdcf8ee6981e03da15fde Mon Sep 17 00:00:00 2001
From: X-Ryl669 <boite.pour.spam@gmail.com>
Date: Thu, 25 Feb 2021 08:26:56 +0100
Subject: [PATCH] Let libmaple accept RX/TX_BUFFER_SIZE (#21177)

---
 .../PlatformIO/scripts/fix_framework_weakness.py     | 12 ++++++++++++
 platformio.ini                                       | 12 ++++++------
 2 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/buildroot/share/PlatformIO/scripts/fix_framework_weakness.py b/buildroot/share/PlatformIO/scripts/fix_framework_weakness.py
index 94076cfbab5..4944c0003b9 100644
--- a/buildroot/share/PlatformIO/scripts/fix_framework_weakness.py
+++ b/buildroot/share/PlatformIO/scripts/fix_framework_weakness.py
@@ -27,3 +27,15 @@ if env.MarlinFeatureIsEnabled("POSTMORTEM_DEBUGGING"):
         print("Done patching exception handler")
 
     print("Libmaple modified and ready for post mortem debugging")
+
+rxBuf = env["MARLIN_FEATURES"]["RX_BUFFER_SIZE"] if "RX_BUFFER_SIZE" in env["MARLIN_FEATURES"] else "0"
+txBuf = env["MARLIN_FEATURES"]["TX_BUFFER_SIZE"] if "TX_BUFFER_SIZE" in env["MARLIN_FEATURES"] else "0"
+if int(rxBuf) < 64:
+	rxBuf = "64"
+
+if int(txBuf) < 64:
+	txBuf = "64"
+
+build_flags = env.get('BUILD_FLAGS')
+build_flags.append("-DUSART_RX_BUF_SIZE=" + rxBuf + " -DUSART_TX_BUF_SIZE=" + txBuf)
+env.Replace(BUILD_FLAGS=build_flags)
diff --git a/platformio.ini b/platformio.ini
index 5b2e5cd3bd9..fab20018d94 100644
--- a/platformio.ini
+++ b/platformio.ini
@@ -753,7 +753,7 @@ lib_deps          = ${common.lib_deps}
   SoftwareSerialM
 platform_packages = tool-stm32duino
 extra_scripts     = ${common.extra_scripts}
-  buildroot/share/PlatformIO/scripts/fix_framework_weakness.py
+  pre:buildroot/share/PlatformIO/scripts/fix_framework_weakness.py
 
 #
 # STM32F103RC
@@ -795,7 +795,7 @@ upload_protocol   = dfu
 [env:STM32F103RC_fysetc]
 platform          = ${common_stm32f1.platform}
 extends           = env:STM32F103RC
-extra_scripts     = ${common.extra_scripts}
+extra_scripts     = ${common_stm32f1.extra_scripts}
   buildroot/share/PlatformIO/scripts/STM32F103RC_fysetc.py
 build_flags       = ${common_stm32f1.build_flags} -DDEBUG_LEVEL=0
 lib_ldf_mode      = chain
@@ -819,7 +819,7 @@ upload_protocol   = serial
 [env:STM32F103RC_btt]
 platform          = ${common_stm32f1.platform}
 extends           = env:STM32F103RC
-extra_scripts     = ${common.extra_scripts}
+extra_scripts     = ${common_stm32f1.extra_scripts}
   buildroot/share/PlatformIO/scripts/STM32F103RC_SKR_MINI.py
 build_flags       = ${common_stm32f1.build_flags}
   -DDEBUG_LEVEL=0 -DSS_TIMER=4
@@ -861,7 +861,7 @@ monitor_speed     = 115200
 [env:STM32F103RE_btt]
 platform          = ${common_stm32f1.platform}
 extends           = env:STM32F103RE
-extra_scripts     = ${common.extra_scripts}
+extra_scripts     = ${common_stm32f1.extra_scripts}
   buildroot/share/PlatformIO/scripts/STM32F103RE_SKR_E3_DIP.py
 build_flags       = ${common_stm32f1.build_flags} -DDEBUG_LEVEL=0 -DSS_TIMER=4
 debug_tool        = stlink
@@ -951,7 +951,7 @@ build_flags   = ${common_stm32f1.build_flags}
 platform        = ${common_stm32f1.platform}
 extends         = common_stm32f1
 board           = genericSTM32F103VE
-extra_scripts   = ${common.extra_scripts}
+extra_scripts   = ${common_stm32f1.extra_scripts}
   buildroot/share/PlatformIO/scripts/mks_robin_nano35.py
 build_flags     = ${common_stm32f1.build_flags}
   -DMCU_STM32F103VE -DSS_TIMER=4
@@ -1019,7 +1019,7 @@ platform      = ${common_stm32f1.platform}
 extends       = common_stm32f1
 board         = genericSTM32F103RC
 platform_packages = tool-stm32duino
-extra_scripts = ${common.extra_scripts}
+extra_scripts = ${common_stm32f1.extra_scripts}
   buildroot/share/PlatformIO/scripts/mks_robin_e3.py
 build_flags   = ${common_stm32f1.build_flags}
   -DDEBUG_LEVEL=0 -DSS_TIMER=4