Library 45- Interface backup SRAM on STM32F4

Almost all (except STM32F401/STM32F411) STM32F4xx MCUs have internal backup SRAM, which stores data, if power is at least on Vbat, or main voltage is active.

These MCUs have internal 4-kBytes of SRAM. It is on location where if you read “too much” you can make a HardFault error and this is something you don’t want. So be careful when you are doing with this ram.

Backup SRAM can be also used as EEPROM, if your Vbat battery is always active, so you can store data which will stay there also if you reset your device. You just have to make sure that you don’t shut down your power supply.

Difference between backup SRAM and normal SRAM is that backup SRAM stays the same if main voltage is off (but Vbat is still active), but normal SRAM will loose data stored inside.

Important: If you will try working with backup SRAM on STM32F401 or STM32F411 devices, you will get “HardFault” error when you will try to read from backup SRAM register locations.

These devices don’t have backup SRAM inside so do not try to do that or your device can stuck.

Also, not available on any STM32F4 Nucleo board.



  • Interface 4-kBytes of backup SRAM
    • Does not work on STM32F401/411
  • Read/Write 8-bit value
  • Read/Write 16-bit value
  • Read/Write 32-bit value
  • Read/Write float value
  • Version 1.1 – December 22, 2014
    • Support for read/write float numbers


    • STM32F4xx
    • STM32F4xx RCC
    • STM32F4xx PWR
  • TM
    • defines.h



In this section, I will show you, how to “fill” entire backup SRAM correctly, so you will not get like “HardFault” error or any other.

  • Fill entire backup SRAM with 8-bit values
  • Fill entire backup SRAM with 16-bit values
  • Fill entire backup SRAM with 32-bit values


Example below works really easy. When you upload your code, first release usb power and put back again, that you reset everyting first.

After power on, you will se one LED on:

  • RED led on: device was first time configured and value was written to backup sram
    • If you see this, then press reset button
  • GREEN led on: device read data back from backup SRAM successfully

Project is available on Github, download library below.

TM STM32F4 Backup SRAM Library


Owner of this site. Also electronic enthusiasts, web developer, 3D printer fan, handball player and more. Big fan of STM32F4 devices. In anticipation of the new Discovery board for STM32F7 lines.

You may also like...

Read before commenting!

Before you make a new comment, make sure you agree with things listed below:

  • - Read post to make sure if it is already posted what you are asking for,
  • - Make sure you have the latest version of libraries used in your project,
  • - Make a clean and grammatically correct written message,
  • - Report as many details as possible, including what have you done so far,
  • - Do NOT post any code here. Use Pastebin,
  • - Do NOT post any error codes here. Use Pastebin,
  • - Specify STM32Fxxx family and used Discovery/EVAL/Nucleo or custom made board,
  • - Make sure your clock is set correct for PLL,
  • - If you are using my HAL drivers, please check this post how to start.
Comment will be deleted on breaking these rules without notification!
  • xonhas

    Great work Tilen! I’ve been following your posts for some weeks now! Just wanted to make a silly question:
    How do you write to normal RAM?

    • Hi,

      thanks for that.
      Em, normal RAM are just variables and run-time things. There is no special routine for that. If you need normal ram, then make a large array for variable.

      For example:
      If you need 100 8-bit values, then use:

      uint8_t ram_space[100];

      Or probably, you could make a write like this lib, just location for ram will be other. I didn’t test it like this.

  • Aung

    Dear Tilen,

    I have successfully tried the project.
    The question is how can i access that data? and plot off-line may be in excel?

    I am sorry if i asked an inappropriate question.

    Thank you.


    • Hi,

      data which are stored in SDRAM, you have to somehow transfer to your computer for work with them. You can use USART, USB CDC/HID, ….
      You can access to data using my functions above.

  • Pingback: All STM32F4 libraries - STM32F4 Discovery()

  • Christian Julius

    Just an idea, I realized yesterday: A better solution is to define a section in linker script for the bkram.

    _sidata3 = LOADADDR(.bkram);

    .bkram :
    . = ALIGN(4);
    _sdata3 = .;
    . = ALIGN(4);
    _edata3 = .;

    If initialized vars are used, the startup code has to be modified too. In Source Code the _attribute .section Pragma makes it comfortable to place variables anywhere in the RAM, even in Backup Ram. Of course BKRAM must be activated in Startupcode with

    // Enable CCM & Backup SRAM
    ldr r0, =0x40023830
    mov r1, 0x00140000
    str r1, [r0]

    // Initialize Backup Ram with Flash Data

    ldr r1, =_sidata3
    ldr r2, =_sdata3
    ldr r3, =_edata3

    cmp r2, r3
    ittt lt
    ldrlt r0, [r1], #4
    strlt r0, [r2], #4
    blt .flash_to_bkram_loop

    Usage in Sourcecode
    #define BKRAM __attribute__((section (“.bkram”)))

    int myVar BKRAM = 42;
    int array[50] BKRAM = {0,1,2,3…..}