I am working on an IOT project where I am using Atmel SAMD21J18A chip to send data to a remote server. I want to include a functionality where I can send the used flash memory and left over flash memory size at run time. Is there a register that holds this data? I looked in the datasheet but couldnt find an answer.
Asked
Active
Viewed 247 times
-1
-
Not something a register would hold, generally the flash consumption is defined by the toolchain, how big the binary is, so you can use your linker script to get the overall size of the binary, round that up to the flash erase block size for the part, and should be able to detect the part or compile that in too to know how much flash that part has, subtract and that is what is free. – old_timer May 01 '17 at 17:53
-
or take the other approach of starting at the top and inspecting the ram until you hit data, round back up to the flash erase boundary. – old_timer May 01 '17 at 17:54
-
There is no such thing as used and free flash memory, it is all used all the time, the data may be the erase value but it is a value it is two states per bit (one or zero) not three (one, zero, free). – old_timer May 01 '17 at 17:55
-
@old_timer thanks a lot. How about RAM usage? is there a way to determine that? – Karthik Swaminathan May 01 '17 at 19:29
-
other than stack yes, assuming you dont use malloc which I really hope you dont. if you stay somewhat traditional (only use your statically allocated variables and dont just wander out in to ram) then you can tell at compile time how much ram you are using with .data and .bss. But the stack is dynamic and you would need to do an analysis which you should be doing anyway to make sure. if nothing else you do a valgrind type approach, but that adds code that uses resources and is not 100% as it doesnt cover all possible paths. – old_timer May 01 '17 at 20:11
-
basically fill ram with some pattern on boot, run for a while, examine memory above .bss .data and see where the pattern stops this is the deepest the stack reached, doesnt mean it can never go deeper. – old_timer May 01 '17 at 20:11
-
short answer, no the stack is dynamic. – old_timer May 01 '17 at 20:12
-
If you use the flash as a filesystem or other runtime storage then its size is dynamic as well, but usually it is for .text and fixed at compile time... – old_timer May 02 '17 at 02:57
1 Answers
1
It should be possible to modify your linker script to create a symbol with a value equal to the end of the linker allocated flash memory (if it does not have one already). You can then declare that symbol as an extern
in your code and use its value. For example:
extern uint32_t END_OF_LINK_ROM ; // Linker generated symbol
How you generate the linker symbol itself will be toolchain specific. If your linker is not locating code at the start of the ROM, you may need a symbol for the start as well. You need to consult your linker documentation and consider your memory map.

Clifford
- 88,407
- 13
- 85
- 165
-
How would you calculate the left over RAM? I have a client server (IOT) code running on my Atmel Chip. Periodically the server requests for the left over RAM and I need to write a logic to report it. I am not using any heap(no dynamic allocation). my data+bss amounts to 13160 bytes(this the compiler gave me) and RAM is 32000 bytes. – Karthik Swaminathan May 16 '17 at 15:22
-
I want to find the amount of stack being used at run time so that I will add that to 13160 and report it to the server. For ex If i call the freeMem from a function A then the amount of stack used till that function + 13160 is the answer. If called from function B the answer is the amount of stack used till that function + 13160. I want to see the left over memory value changing when i receive it on the server side. Is there a way to find approximately the stack used? Like start of stack - stack pointer? – Karthik Swaminathan May 16 '17 at 15:22
-
You'll get a much better answer if you post a new question; SO is not a discussion forum. The stack is statically allocated by the linker just like any other data structure. The instantaneous stack level can be determined by the value of the stack pointer (remembering that stacks often grow downward from high to low memory addresses). If you want to _estimate_ the peak stack usage (far more useful perhaps), then you need to fill the stack with a watermark value and scan from the stack top down to find the high-tide mark. – Clifford May 17 '17 at 07:27
-
Yes. I realized that as soon as i posted this comment. I have posted a new question too. Anyways thank you so much. – Karthik Swaminathan May 17 '17 at 14:04