DolphinV4 API  1.6.1.0
Stack Analysis

The 8051 core has no HW possibility to detect stack overflow situations. That's why the DolphinV4 API offers a function for analysing the stack. The stack is situated in the internal data memory (for more info see Memory architecture). There are only 256 bytes of internal data available.

To know where does the stack of your application begins you can check the M51 file and see where the ?STACK is stored. It is always allocated after the last variable in the internal data. From this address the STACK will grow upward until the 256 limit. Each time a function is called or there is an interrupt, the stack will grow 2 bytes. Because the DolphinV4 API several 3 interrupt levels it can be possible that two interrupts are pending same time, so reserve 6 bytes of stack use for that. Each API function prototype shows how many nested function it contains so the application can approximate what is the current stack usage. Of course the application can't see how big stack is required for the interrupts and also can't predict when is an interrupt executed.

To be sure that there is no stack overflow the amount of stack usage can be calculated using the misc_getStackReserve function. This function requires that by the startup in startup.a51 the internal data is initialised to 0x00. When calling this function it calculates how many memory addresses in the internal data are still 0x00. From this information the stack usage can be evaluated. If there is situation in the application where only ca. 1-3 bytes of internal data are still 0x00, than the application has to free some more internal data, otherwise there is a danger of a stack overflow.

For more information read the misc_getStackReserve function description.

Note:
Make sure to take a look at the app_stack_diag example.