Detailed introduction to ESP32S3’s ESP_LOGx() console output

Preface

(1) When coming into contact with a new chip, the first step is to either turn on the light or perform serial port output. Now I will introduce how to quickly use ESP32S3 for console log output.
(2) Log output for ESP32 is relatively simple, unlike other chips that require configuring the baud rate, setting data bit check bits, etc. It's much simpler, but it also makes many young people like me who often do microcontroller development feel insecure. Although it is simple, it always feels awkward to use.
(3) Many operations of ESP32 feel related to Linux, especially the menuconfig configuration to be discussed in this article, which feels similar to Linux kernel tailoring. This will cause many brothers who have not been exposed to this aspect to look confused. In order to pursue simplicity and ease of understanding, this article is set up through the ESP32-IDF plug-in of vscode. I will also give a brief introduction to other environments.
(4)One thing to note here is if you are using U0Tx, U0Rx, U1Tx, U1Rx. This kind of log printing through the serial port will be slightly different from the log printing through the USB data cable.

Log printing

6 levels of logs

(1) The log output of ESP32 is hierarchical. It will adjust the output data through the compiler according to preset conditions.
(2) Unlike other chips, if you want to classify the serial port output data, you need to process it through macro definition. As shown below

#include <stdio.h>

#define NO_output  0
#define Error      1
#define Warning    2
#define Info       3
#define Debug      4
#define Verbose    5
 
#define Set_LOG_Level Info  //设置日志输出等级

void main()
{
    
    
#if Set_LOG_Level >= Verbose
	printf("所有数据都打印\r\n");
#endif
#if Set_LOG_Level >= Debug
	printf("打印用于调试阶段的数据\r\n");
#endif
#if Set_LOG_Level >= Info
	printf("只打印一些普通信息\r\n");
#endif
#if Set_LOG_Level >= Warning
	printf("打印警告信息\r\n");
#endif
#if Set_LOG_Level >= Error
	printf("打印报错信息\r\n");
#endif
#if Set_LOG_Level >= NO_output 
	printf("不进行信息打印\r\n");
#endif
}

Insert image description here

(3) Based on the above code and test results, we can clearly understand the working principle of ESP32's log printing. There is nothing mysterious about the grading of this kind of log printing. In fact, it is the preprocessing stage of the compiler used, which is processed according to the compilation conditions set by you. If you don’t believe it, you can use gcc’s -E command to preprocess only the c file. The final result you see is as follows:
Note : I only intercepted the main() function part, because in the preprocessing stage, the stdio.h header The file information will be copied into the current C file. If you don’t understand, you can read: Deeply understand the #include and header files of C programs, so that the C project only has .h files (dog heads)

Insert image description here

How to set the log output level

(1) Now that we already know the concept of log level, how do I set the output log level? For the above code, we set the final log output level through the #define Set_LOG_Level Info macro.
(2) This method of use varies depending on the operating environment. I will introduce the simplest method first.

Method 1 — vscode uniquely configures the log output level

Insert image description here

Method 2 — menuconfig configuration

(1) Enter idf.py menuconfig in the command line to enter the configuration interface. If you cannot open menuconfig, you can check that ESP32S3 cannot open idf.py menuconfig - the file specified for download in the windows environment.
<2>Find the following directory and configure it.

Insert image description here

Method 3 — Modify the sdkconfig file directly

(1) Find the sdkconfig file in the project path

Insert image description here

(2) Search for Bootloader config and find the following paragraphs. Copy and paste my configuration below as needed, and finally save it. Because there are too many, I will only give three examples.

/*--- 日志输出等级为INFO ---*/
# CONFIG_BOOTLOADER_LOG_LEVEL_NONE is not set
# CONFIG_BOOTLOADER_LOG_LEVEL_ERROR is not set
# CONFIG_BOOTLOADER_LOG_LEVEL_WARN is not set
CONFIG_BOOTLOADER_LOG_LEVEL_INFO=y
# CONFIG_BOOTLOADER_LOG_LEVEL_DEBUG is not set
# CONFIG_BOOTLOADER_LOG_LEVEL_VERBOSE is not set
CONFIG_BOOTLOADER_LOG_LEVEL=3
/*--- 日志输出等级为DEBUG ---*/
# CONFIG_BOOTLOADER_LOG_LEVEL_NONE is not set
# CONFIG_BOOTLOADER_LOG_LEVEL_ERROR is not set
# CONFIG_BOOTLOADER_LOG_LEVEL_WARN is not set
# CONFIG_BOOTLOADER_LOG_LEVEL_INFO is not set
CONFIG_BOOTLOADER_LOG_LEVEL_DEBUG=y
# CONFIG_BOOTLOADER_LOG_LEVEL_VERBOSE is not set
CONFIG_BOOTLOADER_LOG_LEVEL=4
/*--- 日志输出等级为VERBOSE ---*/
# CONFIG_BOOTLOADER_LOG_LEVEL_NONE is not set
# CONFIG_BOOTLOADER_LOG_LEVEL_ERROR is not set
# CONFIG_BOOTLOADER_LOG_LEVEL_WARN is not set
# CONFIG_BOOTLOADER_LOG_LEVEL_INFO is not set
# CONFIG_BOOTLOADER_LOG_LEVEL_DEBUG is not set
CONFIG_BOOTLOADER_LOG_LEVEL_VERBOSE=y
CONFIG_BOOTLOADER_LOG_LEVEL=5

Introduction to log output function

(1) The following are several functions we can use (to be precise, they are actually macro definitions. If you are interested, you can take a look at his implementation principle).
(2) If the log output level is set to INFO, then the print information of the ESP_LOGI(), ESP_LOGW(), and ESP_LOGE() functions can be output.

ESP_LOGE() // 错误
ESP_LOGW() // 警告
ESP_LOGI() // 信息
ESP_LOGD() // 调试
ESP_LOGV() // 详细

(3) When using macros such as ESP_LOGx(), the first one is always a string. It is generally used to indicate which file was executed. By checking the log information later, you can quickly locate the problematic code based on the first string.
(4) The second parameter is also a string. The % in this string can determine how many variable parameters can be had later.
Off topic: If you want to understand the principle of variable parameters, you can read How to Write a Variable Parameter Function? How to make all serial ports of all microcontrollers implement the printf function?

static const char *TAG = "main";
ESP_LOGI(TAG, "tast1: 1");
ESP_LOGI(TAG, "Compile time: %s %s", __DATE__, __TIME__);

(5) How to view log information in vscode:

Insert image description here

(6) To view log information on other platforms, enter the following command line to start monitoring

idf.py monitor

Regarding the problem of incomplete log printing

vscode’s unique solution

(1) My USB port could not be recognized before, so I initially used the CH340 to perform the burning program on the ESP32S3 through the serial port. Later, when I studied the console output of ESP_LOGx(), I found that my ESP_LOGI() has been unable to output data, which made me want to vomit.
(2) Later, I contacted Espressif’s official staff and found out what the problem was.
(3) Enter the menuconfig configuration interface and search for Channel for console output to set the ESP_LOGx() console output settings.
(4) The following paragraph is an introduction to this configuration option. To be honest, I will not introduce it randomly because I don’t understand it. The configuration in the picture is based on the instructions of Espressif’s official staff, so there will be no problem.
<1>By default, UART0 is used on predefined gpio.
<2>If you select "Custom", you can select UART0 or UART1, and you can select any pin.
<3>If "None" is selected, there will be no console output on any UART except the initial output of the ROM bootloader. This ROM output can be inhibited via GPIO strapping or EFUSE, see the chip datasheet for details.
<4>On chips with USB OTG peripherals, the "USB CDC" option redirects the output to the CDC port. This option uses the CDC driver from the chip ROM, this option is not compatible with the TinyUSB stack.
<5>On chips with a USB serial/JTAG debug controller, select this option to redirect output to the CDC/ACM (serial port emulation) component of the device.

Insert image description here

menuconfig processing method

(1) Enter idf.py menuconfig in the command line to enter the configuration interface.
(2) Top-level directory—>Component config—>ESP System Settings—>Channel for console output, select according to your conditions

Insert image description here

(3) If you find it troublesome to find the path, you can enter "/" to enter the search interface, and then enter Channel for console output to find this option. (Note that in the command line, the paste command is ctrl+shift+v)

Insert image description here

Guess you like

Origin blog.csdn.net/qq_63922192/article/details/132852003