之前我們從0開始新建了一個簡單的Zephyr應(yīng)用-從0開始打造屬于自己的HelloWorld,本期就帶著大家熟悉一下Zephyr的shell和log系統(tǒng)。
首先給大家介紹Zephyr log系統(tǒng)的使用,在使用log前需要:
1. 修改proj.conf打開log功能:
CONFIG_LOG=y
2. 編輯main.c使用log功能:
LOG_MODULE_REGISTER(main);
int main(void)
{
int32_t count = 0;
printk("Hello Zephyr!
");
while (1)
{
LOG_INF("Hello from main : %d", count++);
k_sleep(K_MSEC(1000));
}
return 0;
}
這里通過LOG_MODULE_REGISTER注冊了一個叫做main的log系統(tǒng),并通過LOG_INF來進(jìn)行打印,執(zhí)行程序編譯和下載后可以得到結(jié)果:
接下來是shell的使用,同樣的首先要打開shell功能:
1. 編輯proj.conf文件:
CONFIG_LOG=y
CONFIG_SHELL=y
2. 重新構(gòu)建下載程序查看效果:
CONFIG_LOG=y
CONFIG_LOG_CMDS=y
CONFIG_SHELL=y
CONFIG_SHELL_PROMPT_UART="shell>"
CONFIG_SHELL_VT100_COLORS=y
CONFIG_KERNEL_SHELL=y
CONFIG_THREAD_MONITOR=y
#add a reboot command
CONFIG_REBOOT=y
4. 添加一個自定義shell命令, 拷貝如下內(nèi)容到main.c:
static int monkey_handler(const struct shell *shell,
size_t argc,
char **argv)
{
ARG_UNUSED(argc);
ARG_UNUSED(argv);
shell_fprintf(shell,SHELL_VT100_COLOR_YELLOW,"
");
shell_fprintf(shell,SHELL_VT100_COLOR_YELLOW," ██████████████████████████
");
shell_fprintf(shell,SHELL_VT100_COLOR_YELLOW," ██??????????????????????????██
");
shell_fprintf(shell,SHELL_VT100_COLOR_YELLOW," ██????????????????????????????██
");
shell_fprintf(shell,SHELL_VT100_COLOR_YELLOW," ██??????????????????????????????██
");
shell_fprintf(shell,SHELL_VT100_COLOR_YELLOW," ██████????????????????????????????????██
");
shell_fprintf(shell,SHELL_VT100_COLOR_YELLOW," ██??????????????????????????????????????██████
");
shell_fprintf(shell,SHELL_VT100_COLOR_YELLOW," ██??????????????????????????????????????██????██
");
shell_fprintf(shell,SHELL_VT100_COLOR_YELLOW," ██??????????????????██????????██????????██????██
");
shell_fprintf(shell,SHELL_VT100_COLOR_YELLOW," ████??????????????██????????██????????██████
");
shell_fprintf(shell,SHELL_VT100_COLOR_YELLOW," ██????????????????????????????????██
");
shell_fprintf(shell,SHELL_VT100_COLOR_YELLOW," ████ ██????????????????????????????██
");
shell_fprintf(shell,SHELL_VT100_COLOR_YELLOW," ██ ██ ██????????????????????????██
");
shell_fprintf(shell,SHELL_VT100_COLOR_YELLOW," ██ ██ ██????????????????????????????██
");
shell_fprintf(shell,SHELL_VT100_COLOR_YELLOW," ██ ██????????????????????????????██
");
shell_fprintf(shell,SHELL_VT100_COLOR_YELLOW," ████ ██????????????????????????????????██
");
shell_fprintf(shell,SHELL_VT100_COLOR_YELLOW," ██████????????????????????????????????██
");
shell_fprintf(shell,SHELL_VT100_COLOR_YELLOW," ██????██????????????????????██????██
");
shell_fprintf(shell,SHELL_VT100_COLOR_YELLOW," ██████????????????????????██████
");
shell_fprintf(shell,SHELL_VT100_COLOR_YELLOW," ██??????████████??????██
");
shell_fprintf(shell,SHELL_VT100_COLOR_YELLOW," ██????██ ██????██
");
shell_fprintf(shell,SHELL_VT100_COLOR_YELLOW," ██████ ██████
");
shell_fprintf(shell,SHELL_VT100_COLOR_YELLOW,"
");
shell_fprintf(shell,SHELL_VT100_COLOR_CYAN," I'm Super
");
return 0;
}
SHELL_CMD_REGISTER(monkey, NULL, "I'm super.", monkey_handler);
這里我們通過SHELL_CMD_REGISTER聲明了一個叫做monkey的指令,其處理函數(shù)是monkey_handler, 會打印一個黃色猴子:
這樣,我們就介紹完了shell和log系統(tǒng)的添加。在上一期中,我們只添加了一個main.c,在本期我們?yōu)榇蠹已a(bǔ)一個如果添加額外的一個.c文件,需要干兩件事兒:
1. 編輯CMakeLists.txt 添加c文件:
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(hello_world)
target_sources(app PRIVATE src/main.c)
target_sources(app PRIVATE src/t2.c)
2. 創(chuàng)建t2.c:
// Thread configuration
void t2_thread_task(void *, void *, void *);
K_THREAD_DEFINE(t2_thread, CONFIG__T2_TH_STACK_SIZE,
t2_thread_task, NULL, NULL, NULL,
CONFIG__T2_TH_PRIORITY, 0, 0);
LOG_MODULE_REGISTER(t2);
void t2_thread_task(void *, void *, void *)
{
while (1)
{
LOG_INF("Hello from T2!");
k_sleep(K_MSEC(500));
}
}
3. 使用west build + west flash編譯下載代碼:
這樣,即便我們后面有更復(fù)雜的工程需要添加更多的.c文件,我們也能游刃有余,處理得當(dāng)!!
恩智浦半導(dǎo)體NXP Semiconductors N.V.(納斯達(dá)克股票代碼:NXPI)是汽車、工業(yè)物聯(lián)網(wǎng)、移動設(shè)備和通信基礎(chǔ)設(shè)施市場值得信賴的合作伙伴,致力于提供創(chuàng)新解決方案。
-
Log
+關(guān)注
關(guān)注
0文章
15瀏覽量
11364 -
Shell
+關(guān)注
關(guān)注
1文章
367瀏覽量
23514 -
Zephyr
+關(guān)注
關(guān)注
0文章
29瀏覽量
6028
原文標(biāo)題:Zephyr的shell和log功能介紹
文章出處:【微信號:NXP_SMART_HARDWARE,微信公眾號:恩智浦MCU加油站】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
MCXN947與Zephyr的跨界合作 嵌入式技術(shù)的新飛躍

Zephyr與FreeRTOS實時性測試比較
MIMXRT595-EVK是否可以在SRAM中運(yùn)行hello_world zephyr示例?
LOG101/LOG104偏流調(diào)零電路

淺析Zephyr應(yīng)用的代碼結(jié)構(gòu)中的自定義驅(qū)動
嵌入式系統(tǒng)log模塊設(shè)計

使用Keysight免費(fèi)評估Log4j/Log4Shell零日漏洞
Zephyr中斷服務(wù)類型及實際應(yīng)用
泰凌微電子Zephyr編譯環(huán)境搭建
瑞薩正式加入Zephyr項目
深入解析Zephyr RTOS的技術(shù)細(xì)節(jié)

評論