Robust Firmware Stack Sizing

By Brian Peavey (Expert Firmware Engineer)


Simplexity prides itself on simplifying product design. The simplest designs are often the most robust. This includes the design of embedded firmware.

A major issue in developing firmware for an embedded system is determining the stack size for each of the various threads within the system. Stack memory is used to store all the local variables within a function, plus some function call parameters. If insufficiently sized, a function will overrun its stack and can affect the memory of other threads, likely causing erroneous behavior and potentially causing a system crash.

Basic methods for gauging stack sizes

One way to size a thread’s stack is to take a wild guess at the stack sizes, run the system, and see if it is functional and stable. If you like to gamble, this method might be for you. If you want to be a little more thorough, however, a common practice is to mark the stack memory with a predefined pattern, run the system for a while, and dump each thread’s stack areas and examine how much of the stack space was used. This is certainly better than the first option, but it does not ensure you’ll have enough stack space for the worst-case scenario.

So how do you determine the worst-case scenario for stack usage? Running the system and examining the stack space that was used only gives you a snapshot of stack usage under the limited set of input conditions you exposed your system to. There are likely far too many combinations of inputs — not to mention the timing variations between those inputs — to exhaustively test even a relatively simple embedded system.

Robust methods for gauging stack size

Luckily, some tools and processes can be combined to determine a theoretical worst-case stack usage for each thread in your system. We frequently use the GNU ARM build tools for our projects. The tools support a wide range of ARM processors, the dominant processor choice these days for embedded systems. The compiler can be configured to generate the stack usage for each function in the system. Another tool in the family can be used to dump the assembly language instructions for each function. By examining this output, we can construct a call tree which represents all possible function call chains through the code. By combining this with the stack usage information, a worst-case stack usage can be calculated for each thread in the firmware.

If your code is extremely simple, this technique should work fine as described. However, most embedded systems rely on at least one C library, if not two or three different ones. If your code calls into these libraries, then you will also need to know the stack usage of the library function. This involves getting the source code for each library and recompiling it to get the stack usage information.

Simplexity’s method

But are you sure you got all those function calls in your system? Function calls are frequently made indirectly through pointers. For instance, if you open a file handle, you are given a pointer which you then use to perform various operations on the file. These operations do not translate as direct function calls in the code. Rather, they are calls through a function pointer. Resolving all these within a system can be a time-consuming and error-prone process.

Simplexity has developed a method to analyze an embedded system which parses all the function calls, both direct and indirect, including library calls. Using this information, we can determine the worst-case stack size for each thread. This eliminates the guesswork when determining the correct stack size, as well as eliminating the resulting system crash that occurs when the stack is sized too small. It also eliminates wasting valuable memory resources on stacks that are sized too conservatively. Developing a system with the proper stack sizes is one of the many pieces required for designing the most cost-efficient and highest-quality embedded system.

For more on engineering and design, follow Simplexity’s product development blog.


Subscribe For More

To receive more articles like the one above in your inbox, sign-up to our newsletter below.

Blog Posts

Tech We Can’t Wait To See At CES 2019


Ossia Cota Forever Battery: Collaborative Design


Better Firmware Faster


Demystifying What it Takes to be a CEO


Considerations for Code Refactoring


American Association for Clinical Chemistry Annual Meeting


Simplexity's Answer to Growing Pains


Consumer Electronics Show 2018 | Part Three | So, What is a Robot?


Consumer Electronics Show 2018 | Part Two | CES 2018 and IoT


Consumer Electronics Show 2018 | Part One


Why on earth are heavy weights being suspended from this printer?


10 Best Places to Buy Parts for Product Development


Robust Firmware Stack Sizing


Senaptec Strobe: A Study in Simplification


Treatment, Prevention, and Medical Engineering Solutions


Options in Product Development Models: Internal, CDM, or Design Specialist


Designing Thermal Control Systems


Simplexity’s 7 Steps to Simplification©


Battle of the Buttons: UCSD vs. PSU


Considerations For Medical and Biotech Designs


Risk Mitigation in Product Design: Part 2


Risk Mitigation in Product Design: Part 1


San Diego's Biotech Consortium


Appropriate Models for 3D Motion Analysis: Part 3


University of Oregon’s Product Design Program Is One of a Kind


Appropriate Models for 3D Motion Analysis: Part 2


From Engineer to Leader: How Do You Get There?


Appropriate Models for 3D Motion Analysis: Part 1


Why Engineering Still Matters in Product Development


How Mechatronics Improve Drone Technology


Why You Need a Gyro to Measure Position


Why I, As The CEO, Get The Same Bonus As All My Other Employees


Mechatronics Aids In Embedded System Design


Top 10 Tips for Designing Injection Molded Plastic Parts


British school kids and car hackers: the widespread appeal of open source


When should you consider designing custom gears?


Conference Report: Open Source Hardware Summit


What is a Motion Control System?


The Top 10 Questions to ask a Product Development Firm


The Potential of the Apple AirPods To Disrupt A Whole Industry


How to Use Open Source Hardware in Product Development


What is the mech in mechatronics?


3 Tips for IoT Product Success


When Should I Start Designing For High-Volume Manufacturing?


Designing a 3D Printer for the Home


It Turns Out That EMC Is Not Black Magic


Selecting the correct motor type and size


When brainstorming fails, throw an imaginary cat


Five Tips for Mechatronic System Integration


Three Tips for Designing High Volume Mechatronic Products


What Is Mechatronics?


If I could only do 3 things to simplify a design, what should they be?