4 Techniques to Improve printf’s Real-Time Performance

Developers need to take an active role to ensure that printf gets mapped and used without affecting the system's behavior.

The printf statement has been a long-time cornerstone that developers use to get human readable information onto a console that they can use to understand and debug an embedded system. The problem with printf, though, is that it can be incredibly slow and can impact a system's real-time performance. For example, using the standard UART baud rate of 9600 bps can result in “Hello World!” blocking the application for more than 12 milliseconds. Add in a simple variable to output and the time balloons to more than 20 milliseconds. This is a lifetime for a modern embedded processor. One might be wondering what can be done to improve printf. It turns out there are several techniques a developer can use to speed up their printf statements, such as:

  • Increasing the baud rate
  • Using a channel on the direct memory access (DMA) controller
  • Using RTT
  • Utilizing the ITM

Let’s take a quick look at these in greater detail.

Technique #1 – Increasing the Baud Rate

The first, and most obvious adjustment, is to increase the baud rate. The faster the data is transmitted over the communication medium, the less time will be required while the real-time impact is kept to a minimum. The problem with going faster and faster, though, is that at some point, a developer may reach a point where the time to process and prepare the buffer that printf uses becomes the major time blocker.

Technique #2 – Using a Channel on the Direct Memory Access (DMA) Controller

The DMA controller can be an amazing tool that developers can use to offload the CPU cycles that would have been spent on printing text to a console. While developers often use the DMA to collect sensor data and move data around an application, the DMA controller can also be used to transfer a memory buffer to a UART. That means that with a few modifications to printf, a developer can prepare a string buffer and then kick off the DMA channel to transfer the buffer to the console while the CPU executes other real-time application code.

Technique #3 – Utilizing the ITM

Developers who are using an ARM microcontroller can also utilize the hardware-based ITM to print messages to a console. The ITM has the ability to be broken up into 32 different channels—each of which could be used to print a different type of message. The ARM CMSIS standard includes the API calls that a developer needs in order to take advantage of the ITM. All that a developer needs to do is modify the printf or serial output functions to call ITM_SendChar and the character will be added to the ITM for transmissions back to the console over the trace channel.

Technique #4 – Using RTT

RTT is a proprietary serial transfer protocol from SEGGER. It can be used with any of their J-Link or J-Trace products to transfer printf statements back through the debugger in microseconds or less. RTT can be used on any microcontroller that allows background memory access. This allows the data and memory to be accessed simultaneously and transferred in the background while the CPU is executing its normal instructions.

Conclusion

The printf statement has and will continue to be an important tool available to embedded software developers for years to come. Developers can’t assume that printf is fast and won’t interfere with the real-time system performance because in many instances, it will. To ensure that printf gets mapped and used without affecting the system's behavior, developers need to take an active role.

Jacob Beningo is an embedded software consultant who currently works with clients in more than a dozen countries to dramatically transform their businesses by improving product quality, cost, and time to market. He has published more than 200 articles on embedded software development techniques, is a sought-after speaker and technical trainer, and holds three degrees, which include a Masters of Engineering from the University of Michigan. Feel free to contact him at [email protected], at his website www.beningo.com/, and sign-up for his monthly Embedded Bytes Newsletter.

Atlantic Design & Manufacturing, New York, 3D Printing, Additive Manufacturing, IoT, IIoT, cyber security, smart manufacturing, smart factoryINSPIRE. COLLABORATE. INNOVATE. Atlantic Design & Manufacturing, part of the largest advanced design and manufacturing industry event on the East Coast, is the annual must-attend trade show for discovering the latest in design engineering. Source from the region's most comprehensive collection of cutting-edge suppliers, deepen your expertise with free, conference-level education, and network with thousands of professionals who can help you advance your projects — and your career. From prototyping to full-scale production, one lap of the show floor will help you overcome your toughest manufacturing challenges and keep you up to speed on innovations transforming the industry. Everything you need to take projects to market faster and more cost effectively is here. Click here to register for your free pass today!

Comments (0)

Please log in or to post comments.
  • Oldest First
  • Newest First
Loading Comments...