Methods for Outputting Debug and Log Information in Embedded Development

Follow+Star PublicAccount, don’t miss exciting content

Methods for Outputting Debug and Log Information in Embedded Development

Author | strongerHuang

WeChat Official Account | Embedded Column

In MCU-based embedded software development, there may be situations where there is no extra storage space, thus failing to effectively save debug and log information locally.
In this case, outputting debug (Debug) and log (Log) information through some means becomes meaningful.
Below, we will discuss several points regarding outputting debug and log information in embedded development.

1Standard Library printf Direct Output

In MCU embedded development, outputting debug and log information through UART serial printf is the most common method.
In large systems like Linux and Windows, using the standard C library is not a problem, but on resource-limited platforms like MCU, a micro library is usually used.
1. Using Micro Library Configuration Method
In the IDE used, such as Keil and IAR, configuration in the project options is required to use the micro library properly.
Keil UsingMicro Library:
Project -> Options for Target -> Target, check “Use MicroLIB”
Methods for Outputting Debug and Log Information in Embedded Development
IAR UsingMicro Library:
Project -> Options for Node -> General Options -> Library Configuration, select “Full”

Methods for Outputting Debug and Log Information in Embedded Development

Additionally, there are four options for using the library in IAR:

None: None
Normal: Select the runtime library with standard configuration
Full: Select the runtime library with full configuration
Custom: Select a custom runtime library
For more information about the micro library and IDE configuration, refer to my articles:

What is the difference between Micro Library and Standard C Library?

Keil Series Tutorial 05_Project Target Option Configuration (1)

IAR Series Tutorial 06_Project Node Option Configuration (1)

2. Redefining Functions
To output information, you must have a path, whether it’s UART or CAN. Therefore, function redefinition is necessary.
Taking UART serial as an example, the most common method is:
#include <stdio.h>
int fputc(int ch, FILE *f){  DEBUG_SendByte((uint8_t)ch);
  return ch;}
int fgetc(FILE *f){  while(USART_GetFlagStatus(DEBUG_COM, USART_FLAG_RXNE) == RESET);
  return (int)USART_ReceiveData(DEBUG_COM);}
C
Of course, the specific implementation in the serial port is related to your underlying hardware.
With the above configuration, you can directly use the printf function.

2Custom printf Output

In some cases, if you need to specify the output of debug or log information, you will need to customize the output format.
Example 1:
For instance: I want to set a “DEBUG switch, during the development and testing phase, I need to turn on the switch, but after the product is mass-produced, I do not need to enable debug information output.
#define DEBUG(Type, ...)   if(DEBUG_EN(Type)) 
                           { 
                             printf(__VA_ARGS__); 
                           }
C
Note:__VA_ARGS__ is a variadic macro that copies the content on the left side of the macro “…” to the position of __VA_ARGS__ on the right side.
Example 2:
Additionally: output log information with a “timestamp”:
#define DEBUG(Type, ...)   if(DEBUG_EN(Type)) 
                           { 
                             printf("%s:", GetTimeStr());
                             printf(__VA_ARGS__); 
                           }
C
More Customization:
In actual projects, there may be many requirements, such as: logging with “sensor ID” for different types of sensors.
Therefore, the methods for outputting debug or log information may vary depending on the project’s complexity and requirements, and whether to implement them needs comprehensive evaluation.

3SWO Output

Similar to UART serial output, SWO (Serial Wire Output) is also one of the methods, but it requires the MCU to support SWO functionality (many MCUs currently support it).
During online debugging, it can output to the IDE interface, for example:
Methods for Outputting Debug and Log Information in Embedded Development
It can also output offline to some tool interfaces, for example:Methods for Outputting Debug and Log Information in Embedded Development
This section will not be discussed here, please follow the public account 【Embedded Column, and reply with the keyword 【printf series tutorial】 for more details:

Methods for Outputting Debug and Log Information in Embedded Development

4CLI Debug Output Information

The above methods are purely for outputting debug/log information, a relatively advanced approach is to obtain debug information through CLI (Command Line Interface) commands.

Most people are familiar with command lines, those doing embedded development know that a Linux terminal is a command line, but the command line we’re referring to here is much simpler compared to a Linux terminal.However, the principle is similar, both involve inputting commands to view data or perform an action.

1. Advantages over printf

Through CLI, you can view specified information and perform certain operations at any time.

2.Disadvantages compared to printf

It requires integrating CLI components and the corresponding code (for viewing, executing actions, etc.), which consumes more resources. (Of course, lightweight CLI can be used, which consumes relatively little resources)

For example, one I used previously estimated to occupy less than 1K of Flash space:

Methods for Outputting Debug and Log Information in Embedded Development

In fact, many RTOS have integrated CLI components, and using them is not as complicated as imagined; it is similar to porting RTOS, or even simpler.

Due to time and space limitations, I will stop here; there is still much to share about CLI, and if time permits, I will try to share more related content.

———— END ————

Reply ‘ Embedded Software Design and Development ‘ in the background to read more related articles.

Welcome to follow my public account, reply “Join Group” to join the technical exchange group according to the rules, reply “1024” to see more content.
Welcome to follow my video account:

Methods for Outputting Debug and Log Information in Embedded Development

Click “Read Original” for more shares, and feel free to share, bookmark, like, and view.

Leave a Comment