How can I get the total RAM size on my HW programmatically in Arduino? Is there already a macro I can use? I am using an ARM test board. I can see the RAM size during compilation, but wondering if there is anyway to get it directly from the code.
Have a look at this may be that will help depending what you are trying to achieve
J-M-L:
Have a look at this may be that will help depending what you are trying to achieve
Unfortunately, I am not interested in the current available memory, I am interested in the total RAM available on the device. What I want to do is to calculate the memory consumption of my code, while I am running that code on different platforms. The code you pointed me to gives me the free memory, so I need the total memory first to calculate the memory consumed.
What I want to do is to calculate the memory consumption of my code
Well If you use types such as uint16_t
instead of int
then the beginning size is the same and then you can see the consumption at run time . Known base + consumption = total use
(dynamic consumption being the available free at startup minus what’s available at a given point in time)
That's actually impossible to do at runtime. Most chips are set up with an address space larger than the physical memory. That way more expensive chips can run the same program without modification. If there was only 1024 bytes of memory then address 1025 is still a valid address, it just points back to the first byte in memory.
Learn to use the #define'd constants that the compiler inserts for each chip that it compiles for. That way your code can tell at compile time what chip it's on and then you will know how much memory it has.
-
There's usually a symbol in the include file that defines all the chip-specific information. But it doesn't have a standardized name. (Looks like IRAM_SIZE on SAM3, and HMCRAMC0_SIZE on SAMD, for example.)
-
The linker will know the size and location of RAM for the chip, so that it can place variables/etc. Frequently it will define symbols like data_start for the start of RAM, and ram_end for the end of ram because malloc() will need the info. But those names don't seem to be standardized or consistent, either
You can also modify the linker scripts to provide you with "known" names.
-
At runtime, you can "probe" ram to see whether it's present. You need to either do this before C starts up and begins to use variable space, and/or you have to be VERY careful. This is easier if you have a list of "possible" RAM sizes.
-
You can do various sneaky things to figure out the current value of the stack pointer. If you assume that the stack is placed at "the end" of RAM, and you know where RAM starts, you can make some good guesses. (alas, not always that easy, since some chips will make use of the address wrapping that MorganS mentions to lump together two otherwise non-contiguous chunks of RAM.)
MorganS:
That's actually impossible to do at runtime. Most chips are set up with an address space larger than the physical memory. That way more expensive chips can run the same program without modification. If there was only 1024 bytes of memory then address 1025 is still a valid address, it just points back to the first byte in memory.Learn to use the #define'd constants that the compiler inserts for each chip that it compiles for. That way your code can tell at compile time what chip it's on and then you will know how much memory it has.
Where can I get more information on what these compiler constants are exactly?
Where can I get more information on what these compiler constants are exactly?
Pretty much only by reading the source code for the include file(s) for your chip, looking for something that say "RAM"
Exact symbol names are non-standardized.
Locations of the files are non-standardized.
Names of the files are not standardized.
(ok, usually those will all be somewhat consistent within a particular chip family. But it's still a mess.)