Skip to content

Instantly share code, notes, and snippets.

@hasenbanck
Created August 8, 2018 06:53
Show Gist options
  • Save hasenbanck/1e40681b7d74e942a0dd21de8d97bc5e to your computer and use it in GitHub Desktop.
Save hasenbanck/1e40681b7d74e942a0dd21de8d97bc5e to your computer and use it in GitHub Desktop.
How to use the Backup SRAM of a STM32F7 / STM32
This might also work for other STM32 chips. I use the HAL, so it also might be supported by your chip.
To use the Backup SRAM (most of the time 4 Kb), you need to add a backup power source (like a battery or a supercap)
to the VBAT pin of your STM32. I use a simple CR2032 battery.
Use following code to get access to the Backup SRAM:
__HAL_RCC_PWR_CLK_ENABLE();
HAL_PWR_EnableBkUpAccess();
__HAL_RCC_BKPSRAM_CLK_ENABLE();
After this, you can already read / write to the Backup SRAM, but to save the content in case of a power loss of the
main power, you need to active the backup power regulator:
// Enable backup regulator
LL_PWR_EnableBkUpRegulator();
// Wait until backup regulator is initialized
while (!LL_PWR_IsActiveFlag_BRR());
Now you can safely save your content in the Backup SRAM.
You can write to it like this:
*(__IO uint8_t *) (BKPSRAM_BASE + offset) = x;
You can read from it like this:
uint8_t x = (*(__IO uint8_t *) (BKPSRAM_BASE + offset));
@evgenomus
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment