SLUAAP6 September 2023 UCD3138 , UCD3138064 , UCD3138064A , UCD3138128 , UCD3138128A , UCD3138A , UCD3138A64
Stacks are part of RAM, and RAM is entirely initialized to be zeros in the load.asm. To check the usage for each stack, one option is to read from the stack location and see if there is all-zero space from the bottom of a stack. It is necessary to check the stacks while the code is running, and this can be done with an adapter and the Memory Peek/Poke tool that is embedded in the UCD3xxx Device GUI.
Connect the adapter, launch UCD3xxx Device GUI, go to Debug > Memory Peek/Poke.
Take user stack as an example. Read the memory from address 0x6a80e to 0x6bb00 like the following. It shows about 60 bytes are used and 4754 bytes are still available. Of course, it is hard to capture the worst case this way, since the stack varies from time to time during the code running. But it should give us some clues whether an overflow could possibly happen by checking how much margin it has.
Another option is to add test code in the firmware to continuously check if the bottom (or near the bottom for some margins) of the stack is non-zero. Once a non-zero detected, it shows an overflow happens, toggle an IO for report. The benefit for this option is that it checks continuously during the code running.
Take the supervisor stack as an example, the detection code can be similar to the following, in which the stack_mon.ptr is a pointer pointing at the bottom of supervisor stack initially. It is required to start from the bottom.
if (((Uint32)stack_mon.ptr == (Uint32)SUP_STACK_TOP) || (Uint32)(*(stack_mon.ptr) != 0))
{
// reached top of stack so the stack is all zeros (so stack is empty), or else encountered the stack (as encountered a non-zero word)
stack_sup_headroom = (int32)stack_mon.ptr - (int32)SUP_STACK_BOT;
if (stack_sup_headroom < STACK_MON_HEADROOM_ALERT)
{
LoopMuxRegs.DTCIOCTRL.bit.DTC_B_GPIO_VAL = 1;
}
}
else
{
// move onto the next address in the stack
stack_mon.ptr++;
}