2

I am programming a micro in C++. I need a static variable to survive across a reset.

The object is used to hold more detailed information about the reason for the reset. I can't be guaranteed that I can log it to flash at time of reset, so I need to log it after the reset.

I can use pointer constants and manually allocate a block of ram that is not given to the linker in the segments file.... and do in place initialisation.

But I was wondering is there a way of using attributes or something similar in gcc that will allow me to avoid static initialisation of a static variable. This makes it slightly more portable between the different micros I am using as I don't have to fiddle with the memory map manually.

I need the standard object constructors to work when the object is assigned explicitly.

7
  • 5
    You could make a new section that is stored in RAM but isn't part of .data or .bss, which would not be touched by the start-up code. What micro is it? Some of them have persistent scratchpad registers that survive a reset. Commented May 15, 2023 at 11:23
  • You might not even need a separate section if you can read the variable you want before copying the .data section into RAM. I did that on a recent STM32 project, it woks fine. Commented May 15, 2023 at 11:27
  • 2
    Does this answer your question? C, skip initialisation static array Commented May 15, 2023 at 11:30
  • I have actually a few: STM, GD, ESP .... which is why I was trying to avoid playing with the memory map manually. Also the block is about 80 bytes as it includes stack trace of the function calls before the failure, so it doesn't fit into the scratch pad registers. Commented May 15, 2023 at 11:35
  • 1
    There seem to be some additional issues raised by C++ code and GCC that make the "no init" only part of the solution. The code I am using is C++ and there seems to be problem with the generated code if the initialisation of the class variables are done inline with the declaration and no init attribute raised on the class, calling the constructor explicitly to initialise the class fails with garbage. If the initialisation is moved to the constructor initialisation list they are constructed correctly. Commented May 15, 2023 at 14:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.