This time, I would like to introduce tips and settings about printf debug as a handy method for debugging.
Printf debug is the debug using the printf() function, and whether variables are expected values, code execution, conditional branching, etc. are working properly, or not.
And, It helps to visualize the log of the code execution etc. as a dynamic state of the code to output to a standard output (display etc) for debug.
- 1 Effective case for printf debug
- 2 Notes on printf debugging
- 3 What is a semihosting?
- 4 Setting semihosting in MCUXpresso IDE
- 5 Debug
- 6 Summary
Effective case for printf debug
In these days, we use a debugger in IDE as a debugging method.
It is a common way to break and stop the code execution and to try to find a place where it seems to be a problem around and check whether the variables or the code is correct.
However, such a way of debugging like stopping the execution may be difficult to analyze a bug for applications that can not be stopped like a motor or that seems a error-free in the code, even if you use a debugger.
In such cases, printf Debugging is useful when you want to dynamically visualize the application at that time and want to narrow down where the bugs might come from.
In such a sense, there are many people who prefer printf debugging.
Notes on printf debugging
You have to keep in mind when doing printf debugging.
Overhead of executing printf function
However, if you simply put the printf function, due to the overhead of executing printf() function, a behavior of the application would change in some cases.
Small devices with no display
Many of the small devices lack digital display, for example, there is no digital display on TV remote control, there are only buttons. In developing such a device, it is difficult to output the dynamic state of the program on the screen.
In such a case, semihosting output is effective.
What is a semihosting?
Source of quote：ARMARM Compiler User Guide
In other words, it is a mechanism that can output the printf output to the debugger screen of the development tool, not the serial terminal screen connected by UART etc.
In normal way of using printf debug, in the development of products using MCU, printf output redirects to serial output such as UART on the board, and outputs to the external device or display.
However, if the development is not equipped with serial output yet in the early stages, or if there is no display on a small device by nature, it can not be output with the serial interface.
So semihosting will output to the debugger screen (console) by executing the usual printf() function.
There are some notes that you have to be careful about when using LPC MCU.
Setting semihosting in MCUXpresso IDE
When using the LPC MCU, the LPCOpen SDK is already installed in the MCUXpresso IDE.
Normally, when you build our project using LPCOpen, printf() output is configured to output directly to the UART by default.
If you want printf() to output to the debugger console screen by semihosting, you have to set two settings.
- No redirect to UART
- Change to C compiler with semixosting
1-1 No redirect to UART
Disable printf output setting to UART. To disable it, it is necessary to do one of the followings.
- Comment-out the include file (retarget.h) in the board library board.c
- Define the DEBUG_SEMIHOSTING in board library project
In the case of comment-out an include file in board.c
In the case of define DEBUG_SEMIHOSTINGDEBUG_SEMIHOSTING
1-2 Configure a compiler with Semihosting feature for your application project
And, set “Quick Setting” – “Set Library/header typeW” – “Redline (semihost)” on Quick Start Panel (bottom left panel).
Finally, you have to rebuild your project and start debug. Then, you will see printf output on debugger console.
Below picture executes printf(“Hello World\r\n”).
Printf Debugging is a handy debugging technique for dynamic analysis of applications. Also in the development of devices without screen such as small devices, printf debugging is possible by semihosting as well.
Since printf debugging takes the execution overhead of the printf function at execution time and affects the behavior of the actual application, it may be an unintended operation, so it is necessary to fully understand and use it.
Actually, using debugging circuit called ITM of ARM Cortex-M, it is also possible to perform printf debugging without any overhead of executing printf(). I would like to write articles about ITM printf debugging as well in the future.
Thank you so much for your reading to the end.
If you think it was good! It would be greatly appreciated if you could share it.