Skip to content

Commit

Permalink
feat: linker support for firmware in external flash (#5290)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelcoeffic authored Jul 26, 2024
1 parent 4b917b7 commit 3d8854a
Show file tree
Hide file tree
Showing 17 changed files with 285 additions and 186 deletions.
10 changes: 9 additions & 1 deletion radio/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -590,9 +590,17 @@ if(ACCESS_DENIED)
target_link_libraries(firmware AccessDenied)
endif()

if(FIRMWARE_STRIP_BOOTLOADER)
# Strip bootloader from binary
message("-- Strip bootloader from firmware binary")
set(OBJCOPY_ARGS -R .bootloader)
else()
message("-- Bootloader contained in firmware binary")
endif()

add_custom_command(
TARGET firmware POST_BUILD
COMMAND ${CMAKE_OBJCOPY} -O binary firmware.elf firmware.bin
COMMAND ${CMAKE_OBJCOPY} -O binary ${OBJCOPY_ARGS} firmware.elf firmware.bin
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)

Expand Down
6 changes: 3 additions & 3 deletions radio/src/boards/generic_stm32/linker/common_sections.ld
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
.ARM.extab :
{
*(.ARM.extab* .gnu.linkonce.armextab.*)
} > FLASH
} > REGION_TEXT AT> REGION_TEXT_STORAGE

.ARM :
{
PROVIDE(__exidx_start = .);
*(.ARM.exidx* .gnu.linkonce.armexidx.*)
PROVIDE(__exidx_end = .);
} > FLASH
} > REGION_TEXT AT> REGION_TEXT_STORAGE

.reboot_buffer(NOLOAD) :
{
Expand All @@ -26,7 +26,7 @@
*(.data .data*) /* .data sections */
. = ALIGN(4);
_edata = .; /* define a global symbol at data end */
} > REGION_DATA AT> FLASH
} > REGION_DATA AT> REGION_TEXT_STORAGE

/* used by the startup to initialize data */
_sidata = LOADADDR(.data);
Expand Down
42 changes: 29 additions & 13 deletions radio/src/boards/generic_stm32/linker/firmware.ld
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,46 @@ INCLUDE definitions.ld
/* Define output sections */
SECTIONS
{
/* The startup code goes first into FLASH */
.bootloader :
{
KEEP(*(.bootloader))
. = BOOTLOADER_SIZE;
} > REGION_BOOTLOADER

/* ISR vector to be loaded */
.isr_vector :
{
_sisr_vector = .;
KEEP(*(.isr_vector))

. = ALIGN(4);
_eisr_vector = .;
} > REGION_ISR_VECT AT> REGION_TEXT_STORAGE

_isr_load = LOADADDR(.isr_vector);

/* The program code and other data goes into FLASH */
/* Main code section */
.text :
{
FILL(0xFFFF)

CREATE_OBJECT_SYMBOLS

/* Bootloader code */
KEEP(*(.bootloader))

. = BOOTLOADER_SIZE; /* Set the start of the main program */
_stext = .; /* Provide the name for the start of this section */

. = ALIGN(4);
KEEP(*(.isr_vector)) /* Startup code */
_stext = .;
KEEP(*(.fwversiondata))

INCLUDE common_text.ld

. = ALIGN(4);
_etext = .; /* define a global symbols at end of code */
} > FLASH
_etext = .;
} > REGION_TEXT AT> REGION_TEXT_STORAGE

_text_load = LOADADDR(.text);

/* Firmware bootstrap code to load ISR vector and code sections */
.bootstrap :
{
*(.bootstrap)
} > REGION_TEXT_STORAGE

INCLUDE common_sections.ld
}
5 changes: 5 additions & 0 deletions radio/src/boards/generic_stm32/linker/stm32f20x/layout.ld
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ MEMORY
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
}

REGION_ALIAS("REGION_BOOTLOADER", FLASH);
REGION_ALIAS("REGION_ISR_VECT", FLASH);
REGION_ALIAS("REGION_TEXT", FLASH);
REGION_ALIAS("REGION_TEXT_STORAGE", FLASH);

REGION_ALIAS("REGION_DATA", RAM);
REGION_ALIAS("REGION_BSS", RAM);
REGION_ALIAS("REGION_RAM", RAM);
5 changes: 5 additions & 0 deletions radio/src/boards/generic_stm32/linker/stm32f40x/layout.ld
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ MEMORY
CCM (xrw) : ORIGIN = 0x10000000, LENGTH = 64K
}

REGION_ALIAS("REGION_BOOTLOADER", FLASH);
REGION_ALIAS("REGION_ISR_VECT", FLASH);
REGION_ALIAS("REGION_TEXT", FLASH);
REGION_ALIAS("REGION_TEXT_STORAGE", FLASH);

REGION_ALIAS("REGION_DATA", CCM);
REGION_ALIAS("REGION_BSS", CCM);
REGION_ALIAS("REGION_RAM", RAM);
5 changes: 5 additions & 0 deletions radio/src/boards/generic_stm32/linker/stm32f413/layout.ld
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ MEMORY
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 320K
}

REGION_ALIAS("REGION_BOOTLOADER", FLASH);
REGION_ALIAS("REGION_ISR_VECT", FLASH);
REGION_ALIAS("REGION_TEXT", FLASH);
REGION_ALIAS("REGION_TEXT_STORAGE", FLASH);

REGION_ALIAS("REGION_DATA", RAM);
REGION_ALIAS("REGION_BSS", RAM);
REGION_ALIAS("REGION_RAM", RAM);
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ MEMORY
SDRAM(xrw) : ORIGIN = SDRAM_START, LENGTH = SDRAM_SIZE
}

REGION_ALIAS("REGION_BOOTLOADER", FLASH);
REGION_ALIAS("REGION_ISR_VECT", FLASH);
REGION_ALIAS("REGION_TEXT", FLASH);
REGION_ALIAS("REGION_TEXT_STORAGE", FLASH);

REGION_ALIAS("REGION_DATA", CCM);
REGION_ALIAS("REGION_BSS", RAM);
REGION_ALIAS("REGION_RAM", RAM);
2 changes: 2 additions & 0 deletions radio/src/io/bootloader_flash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

#include "bootloader_flash.h"
#include "timers_driver.h"
#include "flash_driver.h"

#include "hal/watchdog_driver.h"

#if defined(LIBOPENUI)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ target_compile_definitions(bootloader PRIVATE BOOT)
target_compile_definitions(board_bl PRIVATE BOOT)

target_link_options(bootloader BEFORE PRIVATE
-L${LINKER_DIR}/${TARGET_LINKER_DIR}/bootloader
-T${LINKER_DIR}/bootloader.ld
-Wl,-Map=bootloader.map,--cref,--no-warn-mismatch,--gc-sections
-lm --specs=nano.specs
Expand Down
26 changes: 16 additions & 10 deletions radio/src/targets/common/arm/stm32/bootloader/boot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
* GNU General Public License for more details.
*/

#if !defined(SIMU)
#include "stm32_hal_ll.h"
#include "stm32_hal.h"
#include "stm32_timer.h"
#endif
// #if !defined(SIMU)
// #include "stm32_hal_ll.h"
// #include "stm32_hal.h"
// #include "stm32_timer.h"
// #endif

#include "hal/usb_driver.h"

Expand All @@ -39,15 +39,15 @@
#include "debug.h"

#include "timers_driver.h"
#include "flash_driver.h"

#include "hal/abnormal_reboot.h"
#include "hal/rotary_encoder.h"

#if defined(DEBUG_SEGGER_RTT)
#include "thirdparty/Segger_RTT/RTT/SEGGER_RTT.h"
#endif

#define APP_START_ADDRESS (uint32_t)(FIRMWARE_ADDRESS + BOOTLOADER_SIZE)

#if defined(EEPROM) || defined(SPI_FLASH)
#define MAIN_MENU_LEN 3
#else
Expand All @@ -66,6 +66,12 @@ const uint8_t bootloaderVersion[] __attribute__ ((section(".version"), used)) =
{'B', 'O', 'O', 'T', '1', '0'};
#endif

#if defined(SIMU)
#define __weak
#elif !defined(__weak)
#define __weak __attribute__((weak))
#endif

volatile tmr10ms_t g_tmr10ms;
volatile uint8_t tenms = 1;

Expand Down Expand Up @@ -444,8 +450,8 @@ int bootloaderMain()
// confirmed

if (memoryType == MEM_FLASH) {
firmwareSize = binFiles[vpos].size - BOOTLOADER_SIZE;
firmwareAddress = FIRMWARE_ADDRESS + BOOTLOADER_SIZE;
firmwareSize = FIRMWARE_LEN(binFiles[vpos].size);
firmwareAddress = APP_START_ADDRESS;
firmwareWritten = 0;
}
#if defined(EEPROM)
Expand Down Expand Up @@ -484,7 +490,7 @@ int bootloaderMain()
if (BlockCount == 0) {
state = ST_FLASH_DONE; // EOF
}
else if (memoryType == MEM_FLASH && firmwareWritten >= FLASHSIZE - BOOTLOADER_SIZE) {
else if (memoryType == MEM_FLASH && firmwareWritten >= FIRMWARE_MAX_LEN) {
state = ST_FLASH_DONE; // Backstop
}
#if defined(EEPROM)
Expand Down
2 changes: 2 additions & 0 deletions radio/src/targets/common/arm/stm32/f2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ if(NOT NATIVE_BUILD)
${STM32CUBE_DIR}/Src/stm32f2xx_hal_i2c.c
${STM32CUBE_DIR}/Src/stm32f2xx_hal_pcd.c
${STM32CUBE_DIR}/Src/stm32f2xx_hal_pcd_ex.c
${STM32CUBE_DIR}/Src/stm32f2xx_hal_flash.c
${STM32CUBE_DIR}/Src/stm32f2xx_hal_flash_ex.c
)

target_compile_options(stm32cube_ll PRIVATE -DUSE_FULL_LL_DRIVER)
Expand Down
Loading

0 comments on commit 3d8854a

Please sign in to comment.