How can I visualise the memory (SRAM) usage of an AVR program?

前端 未结 8 1061
不知归路
不知归路 2020-12-14 08:40

I have encountered a problem in a C program running on an AVR microcontroller (ATMega328P). I believe it is due to a stack/heap collision but I\'d like to be able to confirm

8条回答
  •  [愿得一人]
    2020-12-14 08:55

    Assuming you're using just one stack (so not an RTOS or anything) and that the stack is at the end of the memory, growing down, while the heap is starting after the BSS/DATA region, growing up. I've seen implementations of malloc that actually check the stackpointer and fail on a collision. You could try to do that.

    If you're not able to adapt the malloc code, you could choose to put your stack at the start of the memory (using the linker file). In general it's always a good idea to know/define the maximum size of the stack. If you put it at the start, you'll get an error on reading beyond the beginning of the RAM. The Heap will be at the end and can probably not grow beyond the end if it's a decent implemantation (will return NULL instead). Good thing is you know have 2 separate error cases for 2 separate issues.

    To find out the maximum stack size, you could fill your memory with a pattern, run the application and see how far it went, see also reply from Craig.

提交回复
热议问题