Initializing RAM

Go To Last Post
2 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,

 

I just asked myself if it is somehow possible to initialize the whole RAM with zeros before the execution pointer is set to main().

 

Background:

I am running a bootloader before the application, which jumps to the start address of the application and possible leaves dirty RAM behind...

and guess what... just today I had to hunt a bug down, which was ... *right* ... an uninitialized callback-function pointer.

 

I know that one should primary have clean code, but nevertheless I'd be interested in if and how it is possible to clean up RAM at boot-time.

 

Greetings,

jabba

Last Edited: Mon. Jul 13, 2015 - 12:17 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That suggests your code was using a variable without initialisating it. Surely the C compiler warned you about this? if not try -Wall to ramp up the warnings. You should never use an unitialized variable in C.

 

If you write:

int n = 12345;
int m;

int main(void) {
    static int p;
    int q;
}

then n is guaranteed to start holding 12345, m and q are guaranteed to be 0 (they are known as ".bss" variables). It is only q in this that inherits any old rubbish that happens to be in RAM because q is a stack frame automatic and C gives no guarantee about the initial value. If there is any question that you may use it without assigning a value then simply always write:

int n = 12345;
int m;

int main(void) {
    static int p;
    int q = 0;
}

to guarantee it holds a known value.

 

It is simply papering over the cracks to come up with some scheme to wipe RAM. You should be writing your C defensively in the first place to ensure that such a thing cannot affect you.

 

If you really want to ensure it then just before the bootloader jumps into the app simply have it:

uint8_t * p;
for (p = START_RAM; p < END_RAM; p++) {
    *p++ =0;
}

Alternatively you could add that as a .init3 section (I assume AVr32 is like AVR8 in this sense?) within the app code. But it's better to build this into the bootloader so it leaves things clean rather than having every app require the code to clear memory.