... | ... | @@ -15,12 +15,12 @@ First of all you have to enable the backup SRAM: the backup domain is not readab |
|
|
Moreover, after a reset, the backup domain is protected against possible unwanted write accesses, so you have to disable write protection before being able to write again to the backup region.
|
|
|
|
|
|
The Miosix SGM driver constructor prepares the backup domain for you: it enables the backup clock and disables the write protection. It also retrieves the last board reset reason.
|
|
|
Please remember that the backup domain content is wiped by the driver if, after the reset, the `RCC_CSR_SFTRSTF` (i.e. software reset) flag is not set in the `RCC_CSR` register.
|
|
|
Please remember that the backup domain content is wiped by the driver if, after the reset, the `RCC_CSR_SFTRSTF` (i.e. software reset) flag is not set in the `RCC->CSR` register.
|
|
|
|
|
|
> :warning: **WARNING: Notice that more than one flag could be set at the same time in the `RCC_CSR` register!**
|
|
|
> :warning: **WARNING: Notice that more than one flag could be set at the same time in the `RCC->CSR` register!**
|
|
|
> **(e.g. software reset and reset pin flags)**
|
|
|
|
|
|
After enabling it, in order to read/write from/to the backup domain, you can simply use pointers or standard C/C++ functions, such as `memcpy` (better performance), starting from the backup region base address, which is `BKPSRAM_BASE`.
|
|
|
After enabling it, in order to read/write from/to the backup domain, you can simply use pointers or standard C/C++ functions, such as `memcpy`, starting from the backup region base address, which is `BKPSRAM_BASE`. Notice that _this is not the preferable way to use the backup SRAM region_.
|
|
|
|
|
|
Otherwise, by specifing a dedicated section in the linker script (see below), you can use the `__attribute__(section(<section-name>))` in order to automatically store a global variable in the backup region. Then, in case of reboot, the variable's content is preserved.
|
|
|
The Miosix SGM driver defines the following macro:
|
... | ... | @@ -42,6 +42,7 @@ uint8_t myvar __attribute__((section(".preserve__at_0x40024055"))); |
|
|
```
|
|
|
|
|
|
## Example
|
|
|
<!---
|
|
|
These basic examples show how you can write to the backup SRAM using the Miosix SGM driver.
|
|
|
In a very similar way you can also read the data you stored.
|
|
|
|
... | ... | @@ -69,7 +70,10 @@ int main() { |
|
|
return 0;
|
|
|
}
|
|
|
```
|
|
|
#### 2. Section Attribute
|
|
|
--->
|
|
|
|
|
|
<!---#### 2. Section Attribute--->
|
|
|
|
|
|
If we declare `myvar` using the `PRESERVE` directive, its content will be preserved in the backup SRAM.
|
|
|
```cpp
|
|
|
#include <Common.h>
|
... | ... | @@ -83,18 +87,25 @@ uint8_t myvar2 __attribute__((section(".preserve"))); |
|
|
int main() {
|
|
|
|
|
|
SGM &sgm = SGM::instance();
|
|
|
ResetReason reason = SGM::instance().lastResetReason();
|
|
|
|
|
|
// the TRACE will print "myvar = 5"
|
|
|
// after the reboot
|
|
|
TRACE("myvar = %u", myvar);
|
|
|
Thread::sleep(1000);
|
|
|
|
|
|
myvar = 5;
|
|
|
if (reason == RST_SW)
|
|
|
{
|
|
|
// the TRACE will print "myvar = 5"
|
|
|
// after the reboot
|
|
|
TRACE("myvar = %u \n", myvar);
|
|
|
|
|
|
myvar = 5;
|
|
|
}
|
|
|
|
|
|
reboot();
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
```
|
|
|
In the _References_ sections below you can find two more detailed examples of how to get the reboot reset
|
|
|
|
|
|
## Linker Script
|
|
|
In the board's linker script, a backup memory region has to be defined:
|
... | ... | @@ -148,6 +159,8 @@ Refer to the [Skyward STM32F429ZI Anakin board](https://git.skywarder.eu/scs/mio |
|
|
|
|
|
* [StackOverflow - How to use backup SRAM as EEPROM in STM32F4](https://stackoverflow.com/questions/20667754/how-to-use-backup-sram-as-eeprom-in-stm32f4)
|
|
|
|
|
|
* Usage Examples : https://pastebin.com/juMtttmj and https://pastebin.com/zipPb1bT
|
|
|
|
|
|
* [Skyward's STM32F429ZI Anakin board](https://git.skywarder.eu/scs/miosix-kernel/-/tree/master/miosix/arch/cortexM4_stm32f4/stm32f429zi_skyward_anakin)
|
|
|
|
|
|
* [RM0090, Reference Manual - STM32F405/415, STM32F407/417, STM32F427/437 and STM32F429/439 advanced Arm-based 32-bit MCUs (mainly sections 2.3.1, 5.1.2, 6.3.20/7.3.20)](https://www.st.com/resource/en/reference_manual/dm00031020-stm32f405-415-stm32f407-417-stm32f427-437-and-stm32f429-439-advanced-arm-based-32-bit-mcus-stmicroelectronics.pdf) |