From 780f1f35ca47f2ba6b92e2fdd078ef0b81d550d9 Mon Sep 17 00:00:00 2001 From: Paul Fertser Date: Mon, 15 Jun 2020 14:48:51 +0300 Subject: [PATCH] Make flash and bootloader sizes configurable This patch makes allocating special pages automatic and flexible, allowing flash size and application start offset specification with linker command line arguments. It should allow easier porting to different targets and experimentation without adding code complexity. Many original STM32F103x8 chips have fully functional 128 kiB flash and so this additional space might come useful for experimentation, additional optional features etc. Tested on v2.51A board, including writing and verifying 128 kiB of random data. Make variables are added to control that, so to build for the full undocumented flash size and dapboot configured to start the app from 8 kiB offset one can run: make flash_size=128k bootldr_size=0x2000 --- workspace/TS100/Core/BSP/Miniware/flash.c | 15 ++++----- workspace/TS100/Core/BSP/Miniware/logo.cpp | 8 ++--- .../Core/BSP/Miniware/system_stm32f1xx.c | 11 +++---- workspace/TS100/Core/Src/Settings.cpp | 4 +-- workspace/TS100/LinkerScript.ld | 31 ++++++++++++------- workspace/TS100/Makefile | 6 +++- 6 files changed, 40 insertions(+), 35 deletions(-) diff --git a/workspace/TS100/Core/BSP/Miniware/flash.c b/workspace/TS100/Core/BSP/Miniware/flash.c index 26432da8..05f2b3d0 100644 --- a/workspace/TS100/Core/BSP/Miniware/flash.c +++ b/workspace/TS100/Core/BSP/Miniware/flash.c @@ -9,15 +9,15 @@ #include "BSP.h" #include "string.h" #include "stm32f1xx_hal.h" -/*Flash start OR'ed with the maximum amount of flash - 1024 bytes*/ -/*We use the last 1024 byte page*/ -#define FLASH_ADDR (0x8000000 |0xFC00) + +static uint16_t settings_page[512] __attribute__ ((section (".settings_page"))); + uint8_t flash_save_buffer(const uint8_t *buffer, const uint16_t length) { FLASH_EraseInitTypeDef pEraseInit; pEraseInit.TypeErase = FLASH_TYPEERASE_PAGES; pEraseInit.Banks = FLASH_BANK_1; pEraseInit.NbPages = 1; - pEraseInit.PageAddress = FLASH_ADDR; + pEraseInit.PageAddress = (uint32_t)settings_page; uint32_t failingAddress = 0; resetWatchdog(); __HAL_FLASH_CLEAR_FLAG( @@ -33,7 +33,7 @@ uint8_t flash_save_buffer(const uint8_t *buffer, const uint16_t length) { HAL_FLASH_Unlock(); for (uint8_t i = 0; i < (length / 2); i++) { resetWatchdog(); - HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, FLASH_ADDR + (i * 2), + HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, (uint32_t)&settings_page[i], data[i]); } HAL_FLASH_Lock(); @@ -42,8 +42,5 @@ uint8_t flash_save_buffer(const uint8_t *buffer, const uint16_t length) { void flash_read_buffer(uint8_t *buffer, const uint16_t length) { - uint16_t *data = (uint16_t*) buffer; - for (uint8_t i = 0; i < (length / 2); i++) { - data[i] = *((uint16_t*) (FLASH_ADDR + (i * 2))); - } + memcpy(buffer, settings_page, length); } diff --git a/workspace/TS100/Core/BSP/Miniware/logo.cpp b/workspace/TS100/Core/BSP/Miniware/logo.cpp index f71df93d..b0ad3cb4 100644 --- a/workspace/TS100/Core/BSP/Miniware/logo.cpp +++ b/workspace/TS100/Core/BSP/Miniware/logo.cpp @@ -7,8 +7,8 @@ #include "BSP.h" #include "OLED.hpp" -// Second last page of flash set aside for logo image. -#define FLASH_LOGOADDR (0x8000000 | 0xF800) + +static uint8_t logo_page[1024] __attribute__ ((section (".logo_page"))); // Logo header signature. #define LOGO_HEADER_VALUE 0xF00DAA55 @@ -16,11 +16,11 @@ uint8_t showBootLogoIfavailable() { // Do not show logo data if signature is not found. if (LOGO_HEADER_VALUE - != *(reinterpret_cast(FLASH_LOGOADDR))) { + != *(reinterpret_cast(logo_page))) { return 0; } - OLED::drawAreaSwapped(0, 0, 96, 16, (uint8_t*) (FLASH_LOGOADDR + 4)); + OLED::drawAreaSwapped(0, 0, 96, 16, (uint8_t*) (logo_page + 4)); OLED::refresh(); return 1; } diff --git a/workspace/TS100/Core/BSP/Miniware/system_stm32f1xx.c b/workspace/TS100/Core/BSP/Miniware/system_stm32f1xx.c index fabf4234..5c5c0297 100644 --- a/workspace/TS100/Core/BSP/Miniware/system_stm32f1xx.c +++ b/workspace/TS100/Core/BSP/Miniware/system_stm32f1xx.c @@ -11,16 +11,13 @@ #if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG) /* #define DATA_IN_ExtSRAM */ #endif /* STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG */ -//#define LOCAL_BUILD -#ifndef LOCAL_BUILD + +#ifndef VECT_TAB_OFFSET #define VECT_TAB_OFFSET 0x00004000U /*!< Vector Table base offset field. This value must be a multiple of 0x200. */ -#else -#define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field. - This value must be a multiple of 0x200. */ -#warning LOCAL_BUILD SETUP - #endif //We offset this by 0x4000 to because of the bootloader +#endif + /******************************************************************************* * Clock Definitions *******************************************************************************/ diff --git a/workspace/TS100/Core/Src/Settings.cpp b/workspace/TS100/Core/Src/Settings.cpp index 716d7c9c..3fe8a2ad 100644 --- a/workspace/TS100/Core/Src/Settings.cpp +++ b/workspace/TS100/Core/Src/Settings.cpp @@ -12,9 +12,7 @@ #include "Setup.h" #include "../../configuration.h" #include "BSP.h" -#define FLASH_ADDR \ - (0x8000000 | \ - 0xFC00) /*Flash start OR'ed with the maximum amount of flash - 1024 bytes*/ + #include "string.h" volatile systemSettingsType systemSettings; diff --git a/workspace/TS100/LinkerScript.ld b/workspace/TS100/LinkerScript.ld index 75be14f8..a3a5a942 100644 --- a/workspace/TS100/LinkerScript.ld +++ b/workspace/TS100/LinkerScript.ld @@ -9,17 +9,15 @@ _estack = 0x20005000; /* end of RAM */ _Min_Heap_Size = 0x300; /* required amount of heap */ _Min_Stack_Size = 1024; /* required amount of stack */ +__APP_BASE_ADDRESS__ = 0x08000000 + __BOOTLDR_SIZE__; +__ROM_REGION_LENGTH__ = __FLASH_SIZE__ - __BOOTLDR_SIZE__; +__FLASH_END_ADDR__ = __APP_BASE_ADDRESS__ + __ROM_REGION_LENGTH__; + /* Memories definition */ MEMORY { RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 20K - /* LOCAL_BUILD*/ -/*ROM (rx) : ORIGIN = 0x08000000, LENGTH = 48K*/ - /* production*/ - ROM (rx) : ORIGIN = 0x08004000, LENGTH = 46K - - - + ROM (rx) : ORIGIN = __APP_BASE_ADDRESS__, LENGTH = __ROM_REGION_LENGTH__ } /* ROM is normally 48K after the bootloader, however we allocate the last page for settings, and the second last one for display boot logo*/ @@ -118,11 +116,22 @@ SECTIONS _edata = .; /* define a global symbol at data end */ } >RAM AT> ROM - - /* Uninitialized data section into RAM memory */ - . = ALIGN(4); + .logo_page (NOLOAD) : + { + . = ABSOLUTE(__FLASH_END_ADDR__ - 2048); + KEEP (*(.logo_page*)) + } > ROM + + .settings_page (NOLOAD) : + { + . = ABSOLUTE(__FLASH_END_ADDR__ - 1024); + KEEP (*(.settings_page*)) + } > ROM + .bss : { + /* Uninitialized data section into RAM memory */ + . = ALIGN(4); /* This is used by the startup in order to initialize the .bss secion */ _sbss = .; /* define a global symbol at bss start */ __bss_start__ = _sbss; @@ -157,4 +166,4 @@ SECTIONS } .ARM.attributes 0 : { *(.ARM.attributes) } -} \ No newline at end of file +} diff --git a/workspace/TS100/Makefile b/workspace/TS100/Makefile index ba6ba490..222aea12 100644 --- a/workspace/TS100/Makefile +++ b/workspace/TS100/Makefile @@ -49,9 +49,11 @@ OUTPUT_DIR=Objects # code optimisation ------------------------------------------------------------ OPTIM=-Os -flto -ffat-lto-objects -finline-small-functions -findirect-inlining -fdiagnostics-color -ffunction-sections -fdata-sections +flash_size=64k +bootldr_size=0x4000 # global defines --------------------------------------------------------------- -GLOBAL_DEFINES += -D STM32F103T8Ux -D STM32F1 -D STM32 -D USE_HAL_DRIVER -D STM32F103xB -D USE_RTOS_SYSTICK -D LANG_$(lang) -D LANG -D MODEL_$(model) +GLOBAL_DEFINES += -D STM32F103T8Ux -D STM32F1 -D STM32 -D USE_HAL_DRIVER -D STM32F103xB -D USE_RTOS_SYSTICK -D LANG_$(lang) -D LANG -D MODEL_$(model) -DVECT_TAB_OFFSET=$(bootldr_size)U # Enable debug code generation DEBUG=-g3 @@ -93,6 +95,8 @@ LINKER_FLAGS=-Wl,--gc-sections \ -mthumb \ -mfloat-abi=soft \ -lm -Os -flto -Wl,--undefined=vTaskSwitchContext \ + -Wl,--defsym=__FLASH_SIZE__=$(flash_size) \ + -Wl,--defsym=__BOOTLDR_SIZE__=$(bootldr_size) \ --specs=nano.specs # compiler flags ---------------------------------------------------------------