Feedback
Feedback

If you are having issues with the exercises, please create a ticket on DevZone: devzone.nordicsemi.com
Drag & Drop Files, Choose Files to Upload 您最多可以上传 2 个文件。
  • 简体中文
    Eng
    简体中文
  • 练习 2 – 使用日志模块

    使用日志模块

    在本练习中,我们将简单地重做本课的练习 1,但这次我们将使用功能丰富的日志模块(logger module)。我们将启用该日志模块,并利用它打印不同严重等级的日志,以及对变量进行十六进制转储(hexdump)。

    日志级别输出与说明

    时间戳功能 (LOG_BACKEND_FORMAT_TIMESTAMP) 以及错误和警告日志的着色功能 (LOG_BACKEND_SHOW_COLOR) 默认已启用。同样,日志模块通过内部调用内核函数 k_cycle_get_32() 来获取时间戳。该例程返回自启动以来的当前时间(运行时间),由系统硬件时钟测量。如果系统中存在外部实时时钟,您可以将其更改为返回实际的时间和日期。我们将使用上一个练习的代码作为起点。

    在开始之前,请确保您的开发套件已通电并连接,并且您的终端仿真器已按照练习 1 中步骤 2 至 4 的要求正确配置。

    练习步骤

    1. 在本课程的 GitHub 仓库中,打开此练习的基础代码,位于 l4/l4_e2。

    2. 启用日志模块。通过在应用程序配置文件 prj.conf 中添加以下配置行来完成。

    在prj.conf里的 STEP 2 下面输入以下行:

    CONFIG_LOG=y
    Kconfig

    请务必保存 prj.conf 文件 (Ctrl+S)。添加 CONFIG_LOG=y 将把日志模块源代码包含到构建中。

    注意

    在 Visual Studio Code 中,您可以通过“文件 -> 自动保存”来启用自动保存。这将为您节省手动保存的时间。

    3. 打开 l4_e2/src 目录下的 main.c。

    4. 包含日志模块的头文件。

    在 main.c 中搜索 STEP 4,并在其下方写入以下行:

    #include <zephyr/logging/log.h>
    C

    5. 向日志模块注册您的代码。

    为了使用日志模块,您必须先向其注册。这可以通过使用宏来完成: LOG_MODULE_REGISTER() 该宏接受两个参数:

    • 必选的模块名称。模块名称将包含在每个日志条目中,这有助于区分由不同模块生成的条目和/或进行过滤。模块名称不得作为字符串传递。换句话说,不要使用引号 “”。
    • 可选的模块最大日志级别。最大日志级别将决定哪些消息将被发送到控制台。例如,如果您将模块 X 的日志级别设置为 LOG_LEVEL_DBG,这意味着生成的所有消息(调试、信息、警告和错误)都将发送到控制台。另一方面,如果您将模块 Y 的最大日志级别设置为 LOG_LEVEL_WRN,这意味着只有严重级别为警告和错误的消息才会发送到控制台。如果未提供最低日志级别,则文件中使用默认全局日志级别 (CONFIG_LOG_DEFAULT_LEVEL)。默认全局日志级别设置为 LOG_LEVEL_INF。

    在本练习中,我们将模块命名为 Less4_Exer2,并使用最低日志级别,即 LOG_LEVEL_DBG(将模块 Less4_Exer2 生成的所有消息发送到控制台)。

    在 main.c 中搜索 STEP 5,并在其下方编写以下代码:

    LOG_MODULE_REGISTER(Less4_Exer2,LOG_LEVEL_DBG);
    C

    6. 打印日志信息。

    一切就绪!让我们编写一些日志。我们将尝试打印所有级别(调试、信息、警告和错误)的消息(日志),并对一些变量进行十六进制转储。

    在 main.c 中搜索 STEP 6,并将以下代码片段复制到其下方:

    int exercise_num = 2;
    uint8_t data[] = {0x00, 0x01, 0x02, 0x03,
                      0x04, 0x05, 0x06, 0x07,
                      'H', 'e', 'l', 'l','o'};
    // Printf-like messages
    LOG_INF("nRF Connect SDK Fundamentals");
    LOG_INF("Exercise %d",exercise_num);    
    LOG_DBG("A log message in debug level");
    LOG_WRN("A log message in warning level!");
    LOG_ERR("A log message in Error level!");
    // Hexdump some data
    LOG_HEXDUMP_INF(data, sizeof(data),"Sample Data!"); 
    
    C

    7. 更改回调函数 button_pressed() 以使用异步日志 API,而不是阻塞式的 printk()。

    我们将用功能更强大的日志宏调用替换上一个练习中旧的 printk()。

    复制新的 button_pressed() 函数并用新代码替换当前函数。旧回调函数与新回调函数的主要区别在于,新函数使用日志宏而不是 printk() 阻塞调用。

    void button_pressed(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
    {
    	int i;
    	long int factorial = 1;
    
        LOG_INF("Calculating the factorials of numbers 1 to %d:",MAX_NUMBER_FACT);
    	for (i = 1; i <= MAX_NUMBER_FACT; i++) {
    		factorial = factorial * i;
            LOG_INF("The factorial of %2d = %ld",i,factorial);
    	}
    	/*Important note!
    	Code in ISR runs at a high priority, therefore, 
    	it should be written with timing in mind.
    	Too lengthy or too complex tasks should not be performed by an ISR,
    	they should be deferred	to a thread.
    	*/
    }
    
    C

    使用复制的代码替换 button_pressed() 函数,如下所示:

    8. 按照前面课程中的步骤构建练习。

    9. 按照前面课程中的步骤将其烧录到开发板。

    使用串口终端,您应该看到以下输出:

    每当您按下按钮 1(nRF54 系列 DK 上的按钮 0)时,您应该观察到控制台上打印了从 1 到 10 的数字阶乘,如下所示:

    [00:00:06.900,482] <inf> Less4_Exer2: Calculating the factorials of numbers 1 to 10: 
    [00:00:06.900,512] <inf> Less4_Exer2: The factorial of  1 = 1 
    [00:00:06.900,512] <inf> Less4_Exer2: The factorial of  2 = 2 
    [00:00:06.900,512] <inf> Less4_Exer2: The factorial of  3 = 6 
    [00:00:06.900,543] <inf> Less4_Exer2: The factorial of  4 = 24 
    [00:00:06.900,543] <inf> Less4_Exer2: The factorial of  5 = 120 
    [00:00:06.900,543] <inf> Less4_Exer2: The factorial of  6 = 720 
    [00:00:06.900,543] <inf> Less4_Exer2: The factorial of  7 = 5040 
    [00:00:06.900,543] <inf> Less4_Exer2: The factorial of  8 = 40320 
    [00:00:06.900,573] <inf> Less4_Exer2: The factorial of  9 = 362880 
    [00:00:06.900,573] <inf> Less4_Exer2: The factorial of 10 = 3628800 
    Terminal

    此练习的解决方案可以在 GitHub 仓库的 l4/l4_e2_sol 中找到。

    Switch language?

    Progress is tracked separately for each language. Switching will continue from your progress in that language or start fresh if you haven't begun.

    Your current progress is saved, and you can switch back anytime.

    Register an account
    Already have an account? Log in
    (All fields are required unless specified optional)

    • 8 or more characters
    • Upper and lower case letters
    • At least one number or special character

    Forgot your password?
    Enter the email associated with your account, and we will send you a link to reset your password.