I have a function that dumps the contents of the arduino's ram, and it works, but I still dont understand why it says my stack pointer is at 0x08C9 If the ram of the arduino is 2kb, or 2048 bytes, shouldent the max addressable size be 0x0800?
RAM: SP->0x08C9 HP->0x0371 FREE:1368
And the code:
void printAddress(uint16_t val)
{
Serial.print(F("0x"));
if(val < 16)
{
Serial.print(F("0"));
}
if(val < 256)
{
Serial.print(F("0"));
}
if(val < 4096)
{
Serial.print(F("0"));
}
Serial.print(val,HEX);
}
void setup(){
Serial.begin(115200);
//0x0800 2048
uint8_t *heapptr, *stackptr;
stackptr = (uint8_t *)malloc(4); // use stackptr temporarily
heapptr = stackptr; // save value of heap pointer
free(stackptr); // free up the memory again (sets stackptr to 0)
stackptr = (uint8_t *)(SP); // save value of stack pointer
Serial.print(F("RAM: SP->"));
printAddress(stackptr);
Serial.print(F(" HP->"));
printAddress(heapptr);//(int)&__heap_start);
Serial.print(F(" FREE:"));
Serial.print(stackptr - heapptr);
Serial.println();
}
As a note, Comments are not mine.
-
1Your code comments contain serious conceptual mistakes - free() should not alter the stack pointer at all, and its effect on what it is freeing is undefined (or perhaps better said, access to what was free()'d is subsequently undefined)Chris Stratton– Chris Stratton2017年02月03日 20:35:29 +00:00Commented Feb 3, 2017 at 20:35
-
1Okay, let me rephrase that in light of the edit; the uncredited author of the code you posted has made serious conceptual errors. You should probably find a different source of inspiration.Chris Stratton– Chris Stratton2017年02月07日 19:04:25 +00:00Commented Feb 7, 2017 at 19:04
2 Answers 2
Sure... if the RAM started at address 0, which it doesn't.
According to the datasheet the RAM starts at address 0x0100, below which are all the SFRs:
-
Is the internal Rom mapped below that?Steven Venham– Steven Venham2017年02月03日 23:07:34 +00:00Commented Feb 3, 2017 at 23:07
-
No. The flash (not ROM) is on a separate bus with its own address range.Majenko– Majenko2017年02月03日 23:35:44 +00:00Commented Feb 3, 2017 at 23:35
-
Sad, cause if it was mapped below the sram that would make it it easy to handle Progmem variables...Steven Venham– Steven Venham2017年02月05日 01:28:07 +00:00Commented Feb 5, 2017 at 1:28
-
That's why there are special helper function ctions to assist you.Majenko– Majenko2017年02月05日 10:14:23 +00:00Commented Feb 5, 2017 at 10:14
-
True, but it makes it difficult. PROGMEM Variables are treated as char* so creating functions to determine if I need to load it from ROM (Flash, ROM potato, potahto) or not is a crap-shoot. The F() macro Is all well and good to solve that but cant be used with Static variable so that wouldn't work. Had to Use the FLASH_STRING library to do what I wanted. Would have made alot more sense to keep with the memory hierarchy then just throwing it off somewhere else.Steven Venham– Steven Venham2017年02月07日 16:53:47 +00:00Commented Feb 7, 2017 at 16:53
free up the memory again (sets stackptr to 0)
Freeing memory alters the heap, not the stack. In any case the heap is not like the stack, it is a piece of memory from which memory is allocated, not necessarily contiguously. If you were to allocate A B C from the heap, and then delete B, the heap pointer would be untouched, because it would now have a gap: A ... C
shouldn't the max addressable size be 0x0800?
As Majenko pointed out, RAM starts past the "internal registers" of the processor, so the maximum RAM would be 0x8FF in this particular case.