Skip to content

Commit

Permalink
optim: vector table moved to RAM on STM32 MCUs (#507)
Browse files Browse the repository at this point in the history
* optim: vectors moved to RAM on STM32 MCUs

* fix: Invalid lib_dep removed
  • Loading branch information
cruwaller committed Dec 22, 2021
1 parent 93f6f91 commit 4540c15
Show file tree
Hide file tree
Showing 18 changed files with 108 additions and 35 deletions.
8 changes: 8 additions & 0 deletions src/include/targets.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
#define WORD_ALIGNED_ATTR __attribute__((aligned(4)))

#ifdef PLATFORM_STM32
/* ICACHE_RAM_ATTR1 is always linked into RAM */
#define ICACHE_RAM_ATTR1 __section(".ram_code")
/* ICACHE_RAM_ATTR2 is linked into RAM only if enough space */
#if RAM_CODE_LIMITED
#define ICACHE_RAM_ATTR2
#else
#define ICACHE_RAM_ATTR2 __section(".ram_code")
#endif
#define ICACHE_RAM_ATTR //nothing//
#else
#undef ICACHE_RAM_ATTR //fix to allow both esp32 and esp8266 to use ICACHE_RAM_ATTR for mapping to IRAM
Expand Down
42 changes: 42 additions & 0 deletions src/src/hal/hal_stm32.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

#if defined(PLATFORM_STM32)

#include <stm32_def.h>
#include <string.h>
#include <stdint.h>


void __attribute__((used)) copy_functions_to_ram(void)
{
/* Copy RAM_CODE section into RAM.
* NOTE: vectors are the first and then other code.
* Check variants/ldscript_gen.ld file.
*/
extern uint8_t ram_code_start;
extern uint8_t ram_code_end;
extern uint8_t ram_code;
memcpy(&ram_code_start, &ram_code, (size_t) (&ram_code_end - &ram_code_start));
}

void __attribute__((constructor(102))) __attribute__((used)) init_vectors(void)
{
copy_functions_to_ram();

/** Reset vector location
*
* g_pfnVectors variable comes from the system startup files and
* must always point to beginning of the vectors table where
* stack pointer is the first word and second is the reset vector.
* Vector table is automatically aligned by the linker command file.
*/
extern uint32_t g_pfnVectors;
SCB->VTOR = (__IO uint32_t) &g_pfnVectors;
}

extern "C"
void __attribute__((used)) initVariant(void)
{
/* This can be used to run some init before \ref setup() */
}

#endif // PLATFORM_STM32
4 changes: 4 additions & 0 deletions src/targets/common.ini
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ build_flags =
-D PLATFORM_ESP32=1
-D CONFIG_TCPIP_LWIP=1
-D BEARSSL_SSL_BASIC
-I ${PROJECTSRC_DIR}/hal
src_filter = ${common_env_data.src_filter} -<ESP8266*.*> -<STM32*.*>
lib_deps =
makuna/NeoPixelBus @ ^2.6.7
Expand All @@ -57,6 +58,7 @@ build_flags =
-D CONFIG_TCPIP_LWIP=1
-D BEARSSL_SSL_BASIC
-O2
-I ${PROJECTSRC_DIR}/hal
board_build.f_cpu = 160000000L
board_build.ldscript = eagle.flash.1m.ld
src_filter = ${common_env_data.src_filter} -<ESP32*.*> -<STM32*.*> -<WS281B*.*>
Expand All @@ -80,7 +82,9 @@ board = bluepill_f103c8
build_unflags = -Os
build_flags =
-D PLATFORM_STM32=1
-Wl,-Map,firmware.map
-O2
-I ${PROJECTSRC_DIR}/hal
src_filter = ${common_env_data.src_filter} -<ESP32*.*> -<ESP8266*.*> -<WS281B*.*>
lib_deps =
paolop74/extEEPROM @ ^3.4.1
1 change: 1 addition & 0 deletions src/targets/frsky.ini
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ src_filter = ${env_common_stm32.src_filter} -<tx_*.cpp>
[env:Frsky_RX_R9SLIMPLUS_OTA_via_STLINK]
extends = env:Frsky_RX_R9SLIMPLUS_via_BetaflightPassthrough
board = robotdyn_blackpill_f303cc
board_build.ldscript = variants/stm32f303xx.ld
upload_flags =
BOOTLOADER=bootloader/r9slim_plus_ota_bootloader.bin
VECT_OFFSET=0x8000
Expand Down
2 changes: 1 addition & 1 deletion src/variants/FM30/ldscript.ld
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* Specify the memory areas */
MEMORY
{
RAM_CODE (rx) : ORIGIN = 0x20000000, LENGTH = 0K
RAM_CODE (rx) : ORIGIN = 0x20000000, LENGTH = 1K
RAM_DATA (xrw) : ORIGIN = 0x20000000 + LENGTH(RAM_CODE), LENGTH = LD_MAX_DATA_SIZE - LENGTH(RAM_CODE)
CCMRAM (rw) : ORIGIN = 0x10000000, LENGTH = 0K
FLASH (rx) : ORIGIN = 0x8000000 + FLASH_APP_OFFSET, LENGTH = LD_MAX_SIZE - FLASH_APP_OFFSET
Expand Down
2 changes: 1 addition & 1 deletion src/variants/FM30_mini/ldscript.ld
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* Memories definition */
MEMORY
{
RAM_CODE (rx) : ORIGIN = 0x20000000, LENGTH = 0K
RAM_CODE (rx) : ORIGIN = 0x20000000, LENGTH = 1K
RAM_DATA (xrw) : ORIGIN = 0x20000000 + LENGTH(RAM_CODE), LENGTH = LD_MAX_DATA_SIZE - LENGTH(RAM_CODE)
CCMRAM (rw) : ORIGIN = 0x10000000, LENGTH = 0K
FLASH (rx) : ORIGIN = 0x8000000 + FLASH_APP_OFFSET, LENGTH = LD_MAX_SIZE - FLASH_APP_OFFSET
Expand Down
2 changes: 1 addition & 1 deletion src/variants/GHOST_ATTO/ldscript.ld
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* Specify the memory areas */
MEMORY
{
RAM_CODE (rx) : ORIGIN = 0x20000000, LENGTH = 0K
RAM_CODE (rx) : ORIGIN = 0x20000000, LENGTH = 1K
RAM_DATA (xrw) : ORIGIN = 0x20000000 + LENGTH(RAM_CODE), LENGTH = 16K - LENGTH(RAM_CODE)
CCMRAM (rw) : ORIGIN = 0x10000000, LENGTH = 4K
FLASH (rx) : ORIGIN = 0x08000000 + FLASH_APP_OFFSET, LENGTH = 64K - FLASH_APP_OFFSET
Expand Down
2 changes: 1 addition & 1 deletion src/variants/GHOST_TX/ldscript.ld
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* Specify the memory areas */
MEMORY
{
RAM_CODE (rx) : ORIGIN = 0x20000000, LENGTH = 0K
RAM_CODE (rx) : ORIGIN = 0x20000000, LENGTH = 1K
RAM_DATA (xrw) : ORIGIN = 0x20000000 + LENGTH(RAM_CODE), LENGTH = 40K - LENGTH(RAM_CODE)
CCMRAM (rw) : ORIGIN = 0x10000000, LENGTH = 8K
FLASH (rx) : ORIGIN = 0x8000000 + 16K, LENGTH = 256K - 16K
Expand Down
2 changes: 1 addition & 1 deletion src/variants/L432K/ldscript.ld
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* Specify the memory areas */
MEMORY
{
RAM_CODE (rx) : ORIGIN = 0x20000000, LENGTH = 0K
RAM_CODE (rx) : ORIGIN = 0x20000000, LENGTH = 1K
RAM_DATA (xrw) : ORIGIN = 0x20000000 + LENGTH(RAM_CODE), LENGTH = 64K - LENGTH(RAM_CODE)
CCMRAM (rw) : ORIGIN = 0x10000000, LENGTH = 0K
FLASH (rx) : ORIGIN = 0x8000000 + FLASH_APP_OFFSET, LENGTH = 128K - FLASH_APP_OFFSET
Expand Down
7 changes: 0 additions & 7 deletions src/variants/L432K/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,6 @@ WEAK void SystemClock_Config(void)
#endif // USE_HSI
}

void initVariant(void)
{
/* Reset vector location which is set wrongly by SystemInit */
extern uint32_t g_pfnVectors;
SCB->VTOR = (uint32_t) &g_pfnVectors;
}

#ifdef __cplusplus
}
#endif
2 changes: 1 addition & 1 deletion src/variants/NamimnoRC_Alpha.ld
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ BOOTLOADER_SIZE = 16K;
/* Specify the memory areas */
MEMORY
{
RAM_CODE (rx) : ORIGIN = 0x20000000, LENGTH = 0K
RAM_CODE (rx) : ORIGIN = 0x20000000, LENGTH = 1K
RAM_DATA (xrw) : ORIGIN = 0x20000000 + LENGTH(RAM_CODE), LENGTH = 20K - LENGTH(RAM_CODE)
CCMRAM (rw) : ORIGIN = 0x10000000, LENGTH = 0K
FLASH (rx) : ORIGIN = 0x8000000 + BOOTLOADER_SIZE, LENGTH = 64K - BOOTLOADER_SIZE
Expand Down
2 changes: 1 addition & 1 deletion src/variants/R9MM/R9MM_ldscript.ld
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* Specify the memory areas */
MEMORY
{
RAM_CODE (rx) : ORIGIN = 0x20000000, LENGTH = 0K
RAM_CODE (rx) : ORIGIN = 0x20000000, LENGTH = 1K
RAM_DATA (xrw) : ORIGIN = 0x20000000 + LENGTH(RAM_CODE), LENGTH = 20K - LENGTH(RAM_CODE)
CCMRAM (rw) : ORIGIN = 0x10000000, LENGTH = 0K
FLASH (rx) : ORIGIN = 0x08008000, LENGTH = 96K
Expand Down
4 changes: 2 additions & 2 deletions src/variants/R9M_Lite_Pro_ldscript.ld
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
/* Specify the memory areas */
MEMORY
{
RAM_CODE (rx) : ORIGIN = 0x20000000, LENGTH = 0K
RAM_DATA (xrw) : ORIGIN = 0x20000000, LENGTH = 40K
RAM_CODE (rx) : ORIGIN = 0x20000000, LENGTH = 1K
RAM_DATA (xrw) : ORIGIN = 0x20000000 + LENGTH(RAM_CODE), LENGTH = 40K - LENGTH(RAM_CODE)
CCMRAM (rw) : ORIGIN = 0x10000000, LENGTH = 0K
FLASH (rx) : ORIGIN = 0x8008000, LENGTH = 128K - 0x8000
}
Expand Down
2 changes: 1 addition & 1 deletion src/variants/R9M_ldscript.ld
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* Specify the memory areas */
MEMORY
{
RAM_CODE (rx) : ORIGIN = 0x20000000, LENGTH = 0K
RAM_CODE (rx) : ORIGIN = 0x20000000, LENGTH = 1K
RAM_DATA (xrw) : ORIGIN = 0x20000000 + LENGTH(RAM_CODE), LENGTH = 20K - LENGTH(RAM_CODE)
CCMRAM (rw) : ORIGIN = 0x10000000, LENGTH = 0K
FLASH (rx) : ORIGIN = 0x8004000, LENGTH = 64K - 0x4000
Expand Down
4 changes: 2 additions & 2 deletions src/variants/R9M_ldscript_old_bl.ld
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
/* Specify the memory areas */
MEMORY
{
RAM_CODE (rx) : ORIGIN = 0x20000000, LENGTH = 0K
RAM_DATA (xrw) : ORIGIN = 0x20000000, LENGTH = 20K
RAM_CODE (rx) : ORIGIN = 0x20000000, LENGTH = 1K
RAM_DATA (xrw) : ORIGIN = 0x20000000 + LENGTH(RAM_CODE), LENGTH = 20K - LENGTH(RAM_CODE)
CCMRAM (rw) : ORIGIN = 0x10000000, LENGTH = 0K
FLASH (rx) : ORIGIN = 0x8002000, LENGTH = 128K - 0x2000
}
Expand Down
37 changes: 22 additions & 15 deletions src/variants/ldscript_gen.ld
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,31 @@ _Min_BL_Size = 0x80; /* required amount of bootloader data */
/* Define output sections */
SECTIONS
{
/* The startup code goes first into FLASH */
.isr_vector :
/* Critical program code goes into RAM_CODE */
ram_code = LOADADDR(.ram_code);
.ram_code :
{
/* Vectors must be 512 bytes (128 words) aligned */
. = ALIGN(512);
ram_code_start = .;
/* NOTE: Keep vectors first! */
KEEP(*(.isr_vector))
*(.ram_code)
*(.ram_code*)
. = ALIGN(4);
KEEP(*(.isr_vector)) /* Startup code */
ram_code_end = .;
} >RAM_CODE AT> FLASH

/* Link vector code here to avoid bootloader update with the
* latest (14.x or newer) platfroms */
.text_vectors :
{
. = ALIGN(4);
*(*Reset_Handler)
*(*NMI_Handler)
*(*HardFault_Handler)
*(*MemManage_Handler)
*(*Default_Handler)
} >FLASH

/* The program code and other data goes into FLASH */
Expand Down Expand Up @@ -76,18 +95,6 @@ SECTIONS
PROVIDE_HIDDEN (__fini_array_end = .);
} >FLASH

/* Critical program code goes into RAM_CODE */
ram_code = LOADADDR(.ram_code);
.ram_code :
{
. = ALIGN(4);
ram_code_start = .;
*(.ram_code)
*(.ram_code*)
. = ALIGN(4);
ram_code_end = .;
} >RAM_CODE AT> FLASH

/* used by the startup to initialize data */
_sidata = LOADADDR(.data);

Expand Down
2 changes: 1 addition & 1 deletion src/variants/r9mx/ldscript.ld
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/* Memories definition */
MEMORY
{
RAM_CODE (rx) : ORIGIN = 0x20000000, LENGTH = 0K
RAM_CODE (rx) : ORIGIN = 0x20000000, LENGTH = 1K
RAM_DATA (xrw) : ORIGIN = 0x20000000 + LENGTH(RAM_CODE), LENGTH = 64K - LENGTH(RAM_CODE)
CCMRAM (rw) : ORIGIN = 0x10000000, LENGTH = 0K
FLASH (rx) : ORIGIN = 0x8000000 + FLASH_OFFSET, LENGTH = 128K - FLASH_OFFSET
Expand Down
18 changes: 18 additions & 0 deletions src/variants/stm32f303xx.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
*****************************************************************************
**

** Abstract : Linker script for STM32F303CC Device with
** 256KByte FLASH, 40KByte RAM, 8KByte CCMRAM
**

/* Specify the memory areas */
MEMORY
{
RAM_CODE (rx) : ORIGIN = 0x20000000, LENGTH = 1K
RAM_DATA (xrw) : ORIGIN = 0x20000000 + LENGTH(RAM_CODE), LENGTH = 40K - LENGTH(RAM_CODE)
CCMRAM (rw) : ORIGIN = 0x10000000, LENGTH = 8K
FLASH (rx) : ORIGIN = 0x8008000, LENGTH = 256K - 0x8000
}

INCLUDE "variants/ldscript_gen.ld"

0 comments on commit 4540c15

Please sign in to comment.