From 7a9c183c20f01eec204b7bb89180aebe9723e576 Mon Sep 17 00:00:00 2001
From: Dave Hylands <dhylands@gmail.com>
Date: Mon, 3 Oct 2016 22:28:48 -0700
Subject: [PATCH] stmhal: Fix ESPRUINO_PICO by adding ld scripts with correct
 flash size.

---
 stmhal/Makefile                               | 11 ++++---
 stmhal/boards/ESPRUINO_PICO/mpconfigboard.mk  |  2 +-
 .../boards/ESPRUINO_PICO/stm32f4xx_hal_conf.h |  6 ++--
 stmhal/boards/NUCLEO_F401RE/mpconfigboard.mk  |  2 +-
 stmhal/boards/common.ld                       | 10 +++++-
 stmhal/boards/stm32f401xd.ld                  | 31 +++++++++++++++++++
 .../boards/{stm32f401.ld => stm32f401xe.ld}   |  7 ++---
 7 files changed, 55 insertions(+), 14 deletions(-)
 create mode 100644 stmhal/boards/stm32f401xd.ld
 rename stmhal/boards/{stm32f401.ld => stm32f401xe.ld} (79%)

diff --git a/stmhal/Makefile b/stmhal/Makefile
index 477cb2631..7be0f28fe 100644
--- a/stmhal/Makefile
+++ b/stmhal/Makefile
@@ -261,12 +261,15 @@ OBJ += $(addprefix $(BUILD)/, $(SRC_USBDEV:.c=.o))
 OBJ += $(addprefix $(BUILD)/, $(SRC_MOD:.c=.o))
 OBJ += $(BUILD)/pins_$(BOARD).o
 
-# We put ff.o and stm32f4xx_hal_sd.o into the first 16K section with the ISRs.
+# We put several files into the first 16K section with the ISRs.
 # If we compile these using -O0 then it won't fit. So if you really want these
-# to be compiled with -O0, then edit stm32f405.ld (in the .isr_vector section)
-# and comment out the following 2 lines.
+# to be compiled with -O0, then edit boards/common.ld (in the .isr_vector section)
+# and comment out the following lines.
 $(BUILD)/$(FATFS_DIR)/ff.o: COPT += -Os
-$(BUILD)/$(HAL_DIR)/src/stm32$(MCU_SERIES)xx_hal_sd.o: COPT += -Os
+$(filter $(PY_BUILD)/../extmod/vfs_fat_%.o, $(PY_O)): COPT += -Os
+$(PY_BUILD)/formatfloat.o: COPT += -Os
+$(PY_BUILD)/parsenum.o: COPT += -Os
+$(PY_BUILD)/mpprint.o: COPT += -Os
 
 all: $(BUILD)/firmware.dfu $(BUILD)/firmware.hex
 
diff --git a/stmhal/boards/ESPRUINO_PICO/mpconfigboard.mk b/stmhal/boards/ESPRUINO_PICO/mpconfigboard.mk
index 428b271ab..4c44022c3 100644
--- a/stmhal/boards/ESPRUINO_PICO/mpconfigboard.mk
+++ b/stmhal/boards/ESPRUINO_PICO/mpconfigboard.mk
@@ -1,4 +1,4 @@
 MCU_SERIES = f4
 CMSIS_MCU = STM32F401xE
 AF_FILE = boards/stm32f401_af.csv
-LD_FILE = boards/stm32f401.ld
+LD_FILE = boards/stm32f401xd.ld
diff --git a/stmhal/boards/ESPRUINO_PICO/stm32f4xx_hal_conf.h b/stmhal/boards/ESPRUINO_PICO/stm32f4xx_hal_conf.h
index fcbb6637a..c68c00d19 100644
--- a/stmhal/boards/ESPRUINO_PICO/stm32f4xx_hal_conf.h
+++ b/stmhal/boards/ESPRUINO_PICO/stm32f4xx_hal_conf.h
@@ -54,10 +54,10 @@
   */
 #define HAL_MODULE_ENABLED
 #define HAL_ADC_MODULE_ENABLED
-#define HAL_CAN_MODULE_ENABLED
+/* #define HAL_CAN_MODULE_ENABLED */
 /* #define HAL_CRC_MODULE_ENABLED */
 /* #define HAL_CRYP_MODULE_ENABLED */
-#define HAL_DAC_MODULE_ENABLED
+/* #define HAL_DAC_MODULE_ENABLED */
 /* #define HAL_DCMI_MODULE_ENABLED */
 #define HAL_DMA_MODULE_ENABLED
 /* #define HAL_DMA2D_MODULE_ENABLED */
@@ -79,7 +79,7 @@
 #define HAL_RNG_MODULE_ENABLED
 #define HAL_RTC_MODULE_ENABLED
 /* #define HAL_SAI_MODULE_ENABLED */
-#define HAL_SD_MODULE_ENABLED
+/* #define HAL_SD_MODULE_ENABLED */
 #define HAL_SPI_MODULE_ENABLED
 #define HAL_TIM_MODULE_ENABLED
 #define HAL_UART_MODULE_ENABLED
diff --git a/stmhal/boards/NUCLEO_F401RE/mpconfigboard.mk b/stmhal/boards/NUCLEO_F401RE/mpconfigboard.mk
index 428b271ab..eb391bed7 100644
--- a/stmhal/boards/NUCLEO_F401RE/mpconfigboard.mk
+++ b/stmhal/boards/NUCLEO_F401RE/mpconfigboard.mk
@@ -1,4 +1,4 @@
 MCU_SERIES = f4
 CMSIS_MCU = STM32F401xE
 AF_FILE = boards/stm32f401_af.csv
-LD_FILE = boards/stm32f401.ld
+LD_FILE = boards/stm32f401xe.ld
diff --git a/stmhal/boards/common.ld b/stmhal/boards/common.ld
index 9716c596e..fcae1b4c6 100644
--- a/stmhal/boards/common.ld
+++ b/stmhal/boards/common.ld
@@ -14,8 +14,16 @@ SECTIONS
            out. */
 
         . = ALIGN(4);
+
+        /* NOTE: If you update the list of files contained in .isr_vector,
+                 then be sure to also update smhal/Makefile where it forcibly
+           builds each of these files with -Os */
+
         */ff.o(.text*)
-        */stm32f4xx_hal_sd.o(.text*)
+        */vfs_fat_*.o(.text*)
+        */py/formatfloat.o(.text*)
+        */py/parsenum.o(.text*)
+        */py/mpprint.o(.text*)
 
         . = ALIGN(4);
     } >FLASH_ISR
diff --git a/stmhal/boards/stm32f401xd.ld b/stmhal/boards/stm32f401xd.ld
new file mode 100644
index 000000000..53aa83d53
--- /dev/null
+++ b/stmhal/boards/stm32f401xd.ld
@@ -0,0 +1,31 @@
+/*
+    GNU linker script for STM32F401xD
+*/
+
+/* Specify the memory areas */
+MEMORY
+{
+    FLASH (rx)      : ORIGIN = 0x08000000, LENGTH = 0x060000 /* entire flash, 384 KiB */
+    FLASH_ISR (rx)  : ORIGIN = 0x08000000, LENGTH = 0x004000 /* sector 0, 16 KiB */
+    FLASH_FS (rx)   : ORIGIN = 0x08004000, LENGTH = 0x01C000 /* sectors 1,2,3 are 16K, 4 is 64K */
+    FLASH_TEXT (rx) : ORIGIN = 0x08020000, LENGTH = 0x040000 /* sectors 5,6 2*128KiB = 256 KiB */
+    RAM (xrw)       : ORIGIN = 0x20000000, LENGTH = 0x018000 /* 96 KiB */
+}
+
+/* produce a link error if there is not this amount of RAM for these sections */
+_minimum_stack_size = 2K;
+_minimum_heap_size = 16K;
+ 
+/* Define tho top end of the stack.  The stack is full descending so begins just
+   above last byte of RAM.  Note that EABI requires the stack to be 8-byte
+   aligned for a call. */
+_estack = ORIGIN(RAM) + LENGTH(RAM);
+
+/* define common sections and symbols */
+INCLUDE common.ld
+
+/* RAM extents for the garbage collector */
+_ram_start = ORIGIN(RAM);
+_ram_end = ORIGIN(RAM) + LENGTH(RAM);
+_heap_start = _ebss; /* heap starts just after statically allocated memory */
+_heap_end = 0x20014000; /* tunable */
diff --git a/stmhal/boards/stm32f401.ld b/stmhal/boards/stm32f401xe.ld
similarity index 79%
rename from stmhal/boards/stm32f401.ld
rename to stmhal/boards/stm32f401xe.ld
index 2d81841ff..a91eee079 100644
--- a/stmhal/boards/stm32f401.ld
+++ b/stmhal/boards/stm32f401xe.ld
@@ -1,15 +1,14 @@
 /*
-    GNU linker script for STM32F401
+    GNU linker script for STM32F401xE
 */
 
 /* Specify the memory areas */
-/* TODO verify these regions */
 MEMORY
 {
     FLASH (rx)      : ORIGIN = 0x08000000, LENGTH = 0x080000 /* entire flash, 512 KiB */
     FLASH_ISR (rx)  : ORIGIN = 0x08000000, LENGTH = 0x004000 /* sector 0, 16 KiB */
-                                                             /* sectors 1,2,3 are 16K, 4 is 64K (for filesystem) */
-    FLASH_TEXT (rx) : ORIGIN = 0x08020000, LENGTH = 0x080000 /* sectors 5,6,7 3*128KiB = 384 KiB */
+    FLASH_FS (rx)   : ORIGIN = 0x08004000, LENGTH = 0x01C000 /* sectors 1,2,3 are 16K, 4 is 64K */
+    FLASH_TEXT (rx) : ORIGIN = 0x08020000, LENGTH = 0x060000 /* sectors 5,6,7 3*128KiB = 384 KiB */
     RAM (xrw)       : ORIGIN = 0x20000000, LENGTH = 0x018000 /* 96 KiB */
 }
 
-- 
GitLab