Erasing/filling RAM at startup

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Erasing/filling RAM at startup

2,349 Views
scottm
Senior Contributor II

I've got a (hopefully) simple question for once:  What's the best way to zero out RAM at startup?  I'm chasing what seems to be a stack-related problem and I need to know what's actually been written to.

I seem to remember this being easy with the HCS08's linker parameter file.  I know you can fill non-volatile memory with the gcc linker, but that doesn't work with RAM.

Obviously this could be done in the startup code, but I'm not really proficient in Cortex assembly.  Is there a gdb command to do this?  Or can it be done through some other scripting mechanism?

Thanks,

Scott

Labels (1)
0 Kudos
Reply
2 Replies

2,171 Views
converse
Senior Contributor V

This is quite simple to do with a simple piece of C code in the startup file.

You are going to want to do this in the ResetISR code, before anything else. You will want code something like this:

extern unsigned int __base_RAM;

extern unsigned int __top_RAM;

//*****************************************************************************

// Reset entry point for your code.

// Sets up a simple runtime environment and initializes the C/C++

// library.

//*****************************************************************************

__attribute__ ((section(".after_vectors")))

void

ResetISR(void) {

unsigned int *baseRAM = &__base_RAM ;

while (baseRAM < &__top_RAM){

*baseRAM++ = 0 ;

}

...

I haven't tested this, but I'm sure you get the idea. Do not make any C library calls as it will not have been set up. Also, be careful with the stack. You can do something similar for each RAM block.

0 Kudos
Reply

2,171 Views
scottm
Senior Contributor II

The problem with doing it that way is that baseRAM itself gets overwritten with 0.  I worked around that by using the 'register' keyword in my pointer variable declaration.

Also, my startup code is not the default - I've got a minimal startup.s in assembly that I think I had to switch to when I started using a custom build of Newlib Nano.  So I have to put the call to my C function in the assembly code, and hope that it always uses the link register to return and doesn't need the stack.

I feel like there must be a way to do this in scripting.  From the memory view's import option I can load a blank memory image in a second or two, and I've used scripts to set individual GPIO registers before, so I'm pretty sure it must be possible.

0 Kudos
Reply