How do I modify Linker Script for reserving one Flash Sector for user data? [STM32H7]
How do you modify the linker script to reserve a sector for user data... and such that this user sector won't get erased when re-programming/bootloading?
The dual-core STM32H7 has TWO 8-sector Banks (0x8000000-0x80FFFFF & 0x8100000-0x81FFFFF).. with linker scripts for *EACH* cpu (STM32H745XIHX_RAM.ld, STM32H745XIHX_FLASH.ld). I assume the CPUs can run from either bank/sector, including execute from the same.. so for now, I've tried modifying just the M7's STM32H745XIHX_FLASH.ld :
MEMORY
{
FLASH (xr) : ORIGIN = 0x08000000, LENGTH = 1920K
USER_FLASH (xrw) : ORIGIN = 0x081E0000, LENGTH = 128K ********
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 512K
RAM_D2 (xrw) : ORIGIN = 0x30000000, LENGTH =
RAM_D3 (xrw) : ORIGIN = 0x38000000, LENGTH =
ITCMRAM (xrw) : ORIGIN = 0x00000000, LENGTH = 64K
}
// Section must be 32-bytes aligned (size of a Flash Word)
.NVM: ALIGN(0x10)
{
. = ALIGN(4);
_NVM= .;
. += NVM_SIZE ;
} >USER_FLASH
main.c:
// 32-byte structure
typedef struct __packed {
uint8_t val1;
uint32_t val2[2]; // 8 bytes
uint16_t val3;
uint16_t val4;
uint32_t val5[4]; // 16 bytes
uint8_t val6[2]; // 2 bytes
uint8_t val7;
} stNVM;
// Declare 32-byte variables inside USER_FLASH , should this be const??
__attribute__((__section__(".USER_FLASH"))) uint8_t NVM_user_data[16];
extern volatile stNVM _NVM;
void test ( void )
{
// flash 32 bytes at a time.
HAL_FLASH_Unlock();
// Erase / Write 32-bytes
FLASH_PageErase(0x081E0000);
CLEAR_BIT (FLASH->CR, (FLASH_CR_PER));
HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, 0x081E0000, NVM_user_data);
CLEAR_BIT (FLASH->CR, (FLASH_CR_PG));
// Erase / Write Next 32-bytes
FLASH_PageErase(0x081E0000+32);
CLEAR_BIT (FLASH->CR, (FLASH_CR_PER));
HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, 0x081E0000+32, _NVM);
CLEAR_BIT (FLASH->CR, (FLASH_CR_PG));
HAL_FLASH_Lock();
}However, not sure if its cache related, or linker script / variable declaration, but my program does not write to flash, and if I use CubeMxProgammer to manually put data in the USER_FLASH space, it will get wiped out when I re-pgrogram the application via the Bootloader.
