0

I need to check the RAM of a MCU at startup using a checkerboard-like algorithm. I do not want to lose whatever data is already in RAM, and also i do not know how not to affect the variables im using to perform this algorithm. I was thinking something like:

for (position=0; position< 4096; position++)
 {
     *Temporal = 0x5555;
     if(*Temporal != 0x5555) Error = TRUE;
     *Temporal  = 0xAAAA;
     if(*Temporal != 0xAAAA) Error= TRUE;

    Temporal +=1;

 }

should i modify the linker to know where Temporal and Error are being placed?

RobertoNovelo
  • 3,347
  • 3
  • 21
  • 32
  • 2
    Are you validating that you can read and write to all locations in memory? Are you validating that certain data is *already* in memory? If you're starting up, why is it bad to overwrite the memory? This code should probably occur before RAM gets filled with kernel/user data. – user7116 Jan 03 '13 at 16:01
  • 2
    What is the point in doing a checksum for the whole RAM where the data may change any time? Against what you will check for integrity. Sorry if I am overlooking. – NeonGlow Jan 03 '13 at 16:08

3 Answers3

0

Use the modifier "register" when declaring your pointers (as in "register int *"). Registers are a special segment of memory inside the processor core that (usually) does not count as part of RAM, so any changes to them don't count as RAM read/writes. Some exceptions exist; for instance, in AVR microcontroles, the first 32 bytes of RAM are "faked" into registers.

Your question probably got a downvote for the lack of clarity, and the question about retaining whatever was in RAM being easily solved by most beginner C programmers (just copy the contents of the pointer into a temp variable before testing it, and copying back after the end of the test). Also, you're not performing a Checksum: you're just making a Memory Test. These are very different, separate things.

MVittiS
  • 434
  • 3
  • 13
  • not wanting to lose RAM contents is due to having a bootloader executed before my application is. While preforming a RAM check, modifying at some point whatever variable I am using in my code would affect the test wouldn't it? – RobertoNovelo Jan 03 '13 at 17:00
  • It usually would, but if the test variables are all kept inside the processor registers then it won't (you can't have a pointer to a register, for instance, because they are *separate* from RAM). Only by compiling your source code to assembly and checking it by hand you will make sure the registers are being used instead of RAM. – MVittiS Jan 03 '13 at 18:14
  • 1
    @MVittiS : Your answer is not a proper solution to Roberts problem. There is no guarantee that if you use register keyword, that variable will be stored in a register. See this post http://stackoverflow.com/questions/578202/register-keyword-in-c – NeonGlow Jan 04 '13 at 04:13
  • 1
    What did I just said in the previous comment? Only by checking the assembly output of the compiler he will make sure that the compiler turned them into registers. – MVittiS Jan 04 '13 at 11:17
0

You need to ensure the memory that you are testing is in a different location than the memory containing the program you are running. You may be able to do this by running directly out of flash, or whatever other permanent storage you have connected. If not, you need to do something with the link map to ensure proper memory segmentation.

Inside the test function, ussing register as MVittiS suggests is a good idea. Another alternate is to use a global variable mapped to a different segment than the one under test.

You may want to read this article about memory testing to understand the limitations of the test you propose, how memory can fail, and what you should be testing for.

AShelly
  • 34,686
  • 15
  • 91
  • 152
0

I don't know (not enough details) but your memory test may be a cache test and might not test any RAM at all.

Your memory test (if it does test memory) is also very badly designed. For example, you could chop (or short out) all of the address lines and all data lines except for the least significant 2 data lines, and even though there'd be many extremely serious faults the test will still pass.

My advice would be to read something like this web page about memory testing, just to get some ideas and background information: http://www.esacademy.com/en/library/technical-articles-and-documents/miscellaneous/software-based-memory-testing.html

In general, for non-destructive tests you'd copy whatever you want to keep somewhere else, then do the test, then copy the data back. It's very important that "somewhere else" is tested first (you don't want to copy everything into faulty RAM and then copy it back).

I would also recommend using assembly (instead of C) to ensure no unwanted memory accesses are made; including your stack.

If your code is in RAM that needs to be tested then you will probably need 2 copies of your RAM testing code. You'd use the second copy of your code when you're testing the RAM that contains the first copy of your code. If your code is in ROM then it's much easier (but you still need to worry about your stack).

Brendan
  • 35,656
  • 2
  • 39
  • 66
  • the problem is i would still have to check the stack. Could i still control memory access to avoid using asm? – RobertoNovelo Jan 03 '13 at 17:22
  • If no functions are called, no IRQs are possible and you can guarantee that no local variables are on the stack; then you're not using the stack while testing and can copy the stack somewhere, test the RAM, then copy the stack's data back. Of course the "register" keyword is a deprecated joke (most compilers ignore it) and doing all of this in a single function without too many local variables is nearly impossible. – Brendan Jan 03 '13 at 18:59
  • An alternative would be to switch to a different stack when you need to test the RAM that the original stack uses; but you can't do that in C either (and if you use minimal pieces of inline assembly for stack switching only, then you have to worry about where the compiler stored arguments and local variables on the original stack). In both cases (not using a stack and using a different stack), attempting to do it in C is "less than sane". – Brendan Jan 03 '13 at 19:02
  • @Brendan, in my experience many compilers for microcontrollers _do_ make a point of honoring the `register` keyword. They are designed to let you get as low level as possible. The OP should check the docs for his particular compiler. – AShelly Jan 03 '13 at 20:06
  • @Ashelly: Try using 123 local variables all marked as `register` and see if the compiler magically reconfigures the CPU's silicon to suit. Some compilers might *try* to honor the `register` keyword if they can, but this is NOT a guarantee. – Brendan Jan 04 '13 at 01:21