API/宏 說明
CMD_OPTIONS_STATEMENT(name)
MSH_OPT_ID_GET(name)
MSH_OPT_DUMP(name)
CMD_OPTIONS_NODE_START(name)
CMD_OPTIONS_NODE(id, cmd, desc)
CMD_OPTIONS_NODE_END
以上宏中 name 參數(shù)最好與命令函數(shù)字符串一致, 子選項固定為命令后的第一個字符串(argv[1])。
CMD_OPTIONS_NODE:添加一個子選項, 第一個參數(shù)是子命令的id,這個宏必須在CMD_OPTIONS_NODE_START 和 CMD_OPTIONS_NODE_END之間。
MSH_OPT_ID_GET:用于檢索子選項對應(yīng)的id,也是CMD_OPTIONS_NODE中填寫的id,可以與switch配合使用。
CMD_OPTIONS_STATEMENT:這個宏必須定義在命令函數(shù)實現(xiàn)之前,因為使用這個宏會創(chuàng)建一個結(jié)構(gòu)體參數(shù)。
MSH_OPT_DUMP:用于dump出子命令及命令描述。
MSH_CMD_EXPORT 和 MSH_CMD_EXPORT_ALIAS宏與之前兼容,如需使用二級補全,只需再添加一項參數(shù),內(nèi)容不限。
宏對比:
MSH_CMD_EXPORT(command, desc) or MSH_CMD_EXPORT(command, desc, opt)
MSH_CMD_EXPORT_ALIAS(command, alias, desc) or MSH_CMD_EXPORT_ALIAS(command, alias, desc, opt)
使用二級補全實現(xiàn)一個demo,用于讀寫內(nèi)存
#include
#include
#include
static rt_uint64_t data[100];
int main(void)
{
printf("hello rt-threadn");
printf("data addr: %pn", data);
return 0;
}
CMD_OPTIONS_STATEMENT(memroy_tools)
static int memroy_tools(int argc, char *argv[])
{
rt_uint64_t address, value;
if (argc >= 3)
{
address = strtoul(argv[2], RT_NULL, 0);
switch (MSH_OPT_ID_GET(memroy_tools))
{
case 1:
value = HWREG64(address);
rt_kprintf("addr:%p = %pn", address, value);
break;
case 2:
if (argc >= 4)
{
value = strtoul(argv[3], RT_NULL, 0);
HWREG64(address) = value;
rt_kprintf("addr:%p = %pn", address, value);
}
break;
default:
goto _usage;
break;
}
}
else
{
goto _usage;
}
return 0;
_usage:
rt_kprintf("Usage: mt [options] address [value]n");
rt_kprintf("[options]:n");
MSH_OPT_DUMP(memroy_tools);
return 0;
}
CMD_OPTIONS_NODE_START(memroy_tools)
CMD_OPTIONS_NODE(1, readq, read address)
CMD_OPTIONS_NODE(2, writeq, write address value)
CMD_OPTIONS_NODE_END
MSH_CMD_EXPORT_ALIAS(memroy_tools, mt, memroy tools, options);
測試日志:
| /
RT - Thread Operating System
/ | 5.0.1 build Oct 8 2023 00:09:31
2006 - 2022 Copyright by RT-Thread team
file system initialization fail!
hello rt-threadmsh />
data addr: 0x40101008
msh />
msh />mt
Usage: mt [options] address [value]
[options]:
readq - read address
writeq - write address value
msh />
msh />
msh />mt rea
readq
msh />mt readq 0x40101008
addr:0x0000000040101008 = 0x0000000000000000
msh />
msh />mt w
writeq
msh />mt writeq 0x40101008 0x1234
addr:0x0000000040101008 = 0x0000000000001234
msh />
msh />mt re
readq
msh />mt readq 0x40101008
addr:0x0000000040101008 = 0x0000000000001234
-
CMD命令
+關(guān)注
關(guān)注
0文章
28瀏覽量
8300 -
RT-Thread
+關(guān)注
關(guān)注
31文章
1273瀏覽量
39924
發(fā)布評論請先 登錄
相關(guān)推薦
評論