How to use Backup SRAM as EEPROM in STM32F4

Jolle picture Jolle · Dec 18, 2013 · Viewed 35.2k times · Source

There are two ways of emulating EEPROM on the STM32F4:

  1. On-chip 4 Kbytes backup SRAM
  2. On-chip Flash, with specific software algorithm

The second option is described here: AN3969.

But google, unfortunately, hasn't been able to provide information on how to use the first option - using the 4Kb of backup SRAM as EEPROM?..

Can anyone help on the topic?

Answer

ytasan picture ytasan · Apr 9, 2014

must do these:

  1. Enable the PWR clock

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
    
  2. Enable access to the backup domain

    PWR_BackupAccessCmd(ENABLE);
    
  3. Enable backup SRAM Clock

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_BKPSRAM, ENABLE);
    
  4. Enable the Backup SRAM low power Regulator to retain it's content in VBAT mode

    PWR_BackupRegulatorCmd(ENABLE);
    

and you can write/read datas to sram (these codes from BKP_Domain codes in STM32F4xx_DSP_StdPeriph_Lib) (in my mcu stm32f417 BKPSRAM_BASE=0x40024000)

   // Write to Backup SRAM with 32-Bit Data 
   for (i = 0x0; i < 0x100; i += 4) {
       *(__IO uint32_t *) (BKPSRAM_BASE + i) = i;
   }

   // Check the written Data 
   for (i = 0x0; i < 0x100; i += 4) {
          if ((*(__IO uint32_t *) (BKPSRAM_BASE + i)) != i){
              errorindex++;
          }
   }

then if you want:

    // Wait until the Backup SRAM low power Regulator is ready
    while(PWR_GetFlagStatus(PWR_FLAG_BRR) == RESET)
    {}

you can find these functions in STM32F4xx_DSP_StdPeriph_Lib.