keep some RAM values on reset cortex m3

6.6k Views Asked by At

Is there any way to keep some ram values or global variables after clicking on reset button on the board or on Kiel

I am using STM32L152ZE

3

There are 3 best solutions below

0
On

Disclaimer: I'm answering this from experience doing this kind of thing on OTHER chips & toolchains. I don't know Kiel or that particular part.

Depends what the reset button does.

If it triggers an external reset interrupt directly on the MCU, you might retain ALL RAM values on reset, and only have your globals zeroed / reinitialized with C run-time initialization. To verify this, check datasheet sections on reset, RAM, and possibly standby/low-power modes for details. Most modern MCUs give a whole top-level section to each of those topics.

If it its a full power supply reset (as in all voltages supplying the chip are completely removed), then all bets are off. If that's the case, I think the only option is the on-chip EEPROM.

You'll have to look at the schematic and/or inspect reset status registers to get the definitive answer.

So lets assume it's reset that does allow you to retain RAM - If that's the case, then typical steps go...

  1. Add a special section (or sections?) to your linker directive file that is NOT initialized by C run-time init.

  2. Use #pragma statements surrounding / associated with variable definition to control where it goes (i.e. call out that special section).

  3. At startup, inspect reset reason code registers to know whether or not the RAM contains valid data.

You'll need to make similar provisions to prevent your bootloader from touching the RAM too, assuming they are two separately compiled programs.

As an alternative to creating a special section, Kiel might provide a version of the C run time startup code that does not implicitly "zero" any globals, and instead only initializing globals with an initial value. This is commonly available as a means to improve startup time. You'll have to dig into the compiler manual (or just write your own C runt time init code) to do this though....

0
On

The STM32L152ZE includes a 16 KB EEPROM and 128 Byte of backup registers.

The EEPROM is just that and does not suffer from the problem that the STM32 has writing its own flash memory where it stalls the bus and therefore code execution while writing and erasing blocks.

The backup registers are on a separate power domain than the rest of the device (along with teh RTC) and so long as power is retained to this domain, data can be retained. There is a protection mechanism requiring a specific sequence to read and write these registers.

If it is just surviving a reset that you are interested in then I believe that so long as power is maintained through teh reset (i.e. not a reset by power-cycle) then the SRAM is maintained. However by default, Keil projects are configured to initialise all IRAM segments to zero on start-up; you can reserve a section in the project settings as a "no-init" section, and use the variable __attribute__ extension to explicitly instantiate variables in this section or at specific addresses.

0
On

If you are using Keil IDE and not using your own linker configuration file then it is pretty simple. In the "Options of target - Target " there is a section Read/Write Memory Area, here you define a new area in which all the uninitialized variables are to be placed . Like for example start- 0x20000000 and size 0x20 and check the box "NoInit" if your RAM starts at 0x20000000 and you want to retain first 32 bytes after reset. Then in the "Options of target - Linker", make sure "Use memory layout from target dialog" option is selected so that linker SCT file is auto generated.

Now the last step is to place the variables into the 0x20000000 to 0x20000020 region.You declare all such variables which are to be uninitialized in a single file of your project .Now right click the file name from project window and select the option "Options for file "xxx.c" " and in the memory assignment select the 0x20000000 to 0x20000020 region for ZI data. ( Hope your uninitialized variable are not assigned with any value in code during declaration)