Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
12748 Discussions

Get stack top/bottom pointers at run time?

Altera_Forum
Honored Contributor II
1,855 Views

Hi folks- 

 

I want to write a stack test function to detemine stack usage during run time testing. For that I need to be able to get stack top and bottom pointers at run time. 

 

Can anyone please tell me how to get the C stack top and bottom pointers at run time? 

 

Thanks, John Speth
0 Kudos
5 Replies
Altera_Forum
Honored Contributor II
893 Views

There's already a feature that does this... See attached image from the BSP Editor. There is a similar option in the Nios II IDE's Syslib configuration stuff. The actual compile-time changes are mentioned in the attached image, as well. 

 

Cheers, 

 

-slacker
0 Kudos
Altera_Forum
Honored Contributor II
893 Views

 

--- Quote Start ---  

There's already a feature that does this... See attached image from the BSP Editor. There is a similar option in the Nios II IDE's Syslib configuration stuff. The actual compile-time changes are mentioned in the attached image, as well. 

--- Quote End ---  

 

 

That's a good one that I'll certainly put into use. However I'm looking for a way to know the stack usage at any time before jumping to the exception handler. My stack test function will allow me to gauge stack usage during field stress testing. Exception trapping after stack overflow won't allow me to continue. 

 

John Speth.
0 Kudos
Altera_Forum
Honored Contributor II
893 Views

You could get the current value of 'sp' using an asm() statement. 

However it is probably enough to take the address of a local variable (or argument) as this will be an on-stack address in the current stack frame.
0 Kudos
Altera_Forum
Honored Contributor II
893 Views

Ok, here's the solution from me, the OP. 

 

First some background. The NIOS stack is shared with the heap at all remaining RAM starting at the top of block RAM allocated for NIOS. At least that's true for onchip block memory. If I'm wrong, somebody please correct me. 

 

As a donation to karma, my solution is attached, free for anyone to use. Note that you can't use it if the heap is ever used. Call the zero function as early as possible after main starts. If you use the heap, you'll need to change the get function to somehow find the highest address of the heap that is in use. 

 

JJS
0 Kudos
Altera_Forum
Honored Contributor II
893 Views

Whoops! Sent the wrong file. Here's the right one. 

 

JJS
0 Kudos
Reply