MENU

 

08-Nov-2017

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


Ossia Cota Forever Battery: Collaborative Design

13-Dec-2018

Better Firmware Faster

19-Nov-2018

Demystifying What it Takes to be a CEO

18-Oct-2018

Considerations for Code Refactoring

24-Sep-2018

American Association for Clinical Chemistry Annual Meeting

30-Aug-2018

Simplexity's Answer to Growing Pains

09-Aug-2018

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

29-Jan-2018

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

22-Jan-2018

Consumer Electronics Show 2018 | Part One

15-Jan-2018

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

22-Dec-2017

10 Best Places to Buy Parts for Product Development

29-Nov-2017

Robust Firmware Stack Sizing

08-Nov-2017

Senaptec Strobe: A Study in Simplification

19-Oct-2017

Treatment, Prevention, and Medical Engineering Solutions

04-Oct-2017

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

20-Sep-2017

Designing Thermal Control Systems

30-Aug-2017

Simplexity’s 7 Steps to Simplification©

15-Aug-2017

Battle of the Buttons: UCSD vs. PSU

02-Aug-2017

Considerations For Medical and Biotech Designs

13-Jul-2017

Risk Mitigation in Product Design: Part 2

11-Jul-2017

Risk Mitigation in Product Design: Part 1

28-Jun-2017

San Diego's Biotech Consortium

09-Jun-2017

Appropriate Models for 3D Motion Analysis: Part 3

25-Apr-2017

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

19-Apr-2017

Appropriate Models for 3D Motion Analysis: Part 2

14-Apr-2017

From Engineer to Leader: How Do You Get There?

12-Apr-2017

Appropriate Models for 3D Motion Analysis: Part 1

06-Apr-2017

Why Engineering Still Matters in Product Development

29-Mar-2017

How Mechatronics Improve Drone Technology

16-Feb-2017

Why You Need a Gyro to Measure Position

20-Jan-2017

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

13-Dec-2016

Mechatronics Aids In Embedded System Design

07-Dec-2016

Top 10 Tips for Designing Injection Molded Plastic Parts

22-Nov-2016

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

14-Nov-2016

When should you consider designing custom gears?

07-Nov-2016

Conference Report: Open Source Hardware Summit

31-Oct-2016

What is a Motion Control System?

20-Oct-2016

The Top 10 Questions to ask a Product Development Firm

05-Oct-2016

The Potential of the Apple AirPods To Disrupt A Whole Industry

12-Sep-2016

How to Use Open Source Hardware in Product Development

01-Sep-2016

What is the mech in mechatronics?

16-Aug-2016

3 Tips for IoT Product Success

02-Aug-2016

When Should I Start Designing For High-Volume Manufacturing?

19-Jul-2016

Designing a 3D Printer for the Home

29-Jun-2016

It Turns Out That EMC Is Not Black Magic

22-Jun-2016

Selecting the correct motor type and size

06-Jun-2016

When brainstorming fails, throw an imaginary cat

18-May-2016

Five Tips for Mechatronic System Integration

09-May-2016

Three Tips for Designing High Volume Mechatronic Products

28-Apr-2016

What Is Mechatronics?

18-Apr-2016

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

06-Apr-2016