cancel
Showing results for 
Search instead for 
Did you mean: 

How to save app settings to flash

Louie88
Senior

I need to save a couple of bytes (a settings struct) to upper (last) page of the Bank1 flash. The settings are changed very rarely so lifetime of the flash is not an issue. The settings struct has a CSC member so when the settings are read from the flash during startup the settings would be applied only if the CSC of the settings struct is right.

I know that I have to unlock, then erase a full page (128kB) before saving the app settings. How can I do this using HAL_Flash_xxx calls? I also must lock out the last page of Bank1 from code space (Linker file?). Can anybody send an example? I am using STM32H7474I-DISCO discovery board. 

Thanks,

Louis

PS: Adding a small, external SPI/I2C NVRAM/Flash chip won't work. I have to solve saving/restoring settings within the discovery board.

 

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

Here is an example which erases a flash page and then writes to it.

STM32CubeH7/Projects/STM32H735G-DK/Examples/FLASH/FLASH_EraseProgram/Src/main.c at a64bfcbb83adf21adce13cab48ee0dce47837ac1 · STMicroelectronics/STM32CubeH7

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

3 REPLIES 3
TDK
Guru

Here is an example which erases a flash page and then writes to it.

STM32CubeH7/Projects/STM32H735G-DK/Examples/FLASH/FLASH_EraseProgram/Src/main.c at a64bfcbb83adf21adce13cab48ee0dce47837ac1 · STMicroelectronics/STM32CubeH7

If you feel a post has answered your question, please click "Accept as Solution".
Louie88
Senior

Hi TDK,

Thanks for the example!!!

I modified it to erase the last sector of the STM32H747I MCU's flash (Bank_2.Sector_7) and program 8x32-bit words.

#define FLASH_WORD_COUNT			8
#define FLASH1_BASE					0x08000000UL
#define FLASH2_BASE					0x08100000UL
// 128KB
#define FLASH_SECTOR_SIZE			0x00020000UL
// Sector 7
#define FLASH_SETTINGS_SECTOR		(7)																
// 0x081E0000 start address = BANK_2.7
#define FLASH_SETTINGS_START_ADDR   (FLASH2_BASE + (FLASH_SECTOR_SIZE * FLASH_SETTINGS_SECTOR))		

uint32_t FlashError = -1;	
uint32_t SettingsAddress = FLASH_SETTINGS_START_ADDR;
uint32_t FlashWord[FLASH_WORD_COUNT] = {
					  0x10100101,
					  0x20200202,
					  0x30300303,
					  0x40400404,
					  0x50500505,
					  0x60600606,
					  0x70700707,
					  0x80800808
					  };

// Program 
HAL_FLASH_Unlock();
SCB_DisableICache();
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, SettingsAddress, ((uint32_t)FlashWord)) == HAL_OK)
{
  FlashError = 0;
}
HAL_FLASH_Lock();
SCB_EnableICache();

20250314-FlashProgrammed.png

The example works great!.

Do I understand right that HAL_FLASH_Program() method programs 256 bits (8x32-bit or 4x64-bit words) at a time? In other words, I can't program a single 32-bit location of the flash.

Thanks,

Louis

 

Glad you're up and running.

> Do I understand right that HAL_FLASH_Program() method programs 256 bits (8x32-bit or 4x64-bit words) at a time? In other words, I can't program a single 32-bit location of the flash.

Correct. Due to ECC, the entire flash page must be programmed and cannot be reprogrammed without erasing the sector the page is in.

If you feel a post has answered your question, please click "Accept as Solution".