电子说
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 参数最好与命令函数字符串一致, 子选项固定为命令后的第一个字符串(argv[1])。
CMD_OPTIONS_NODE:添加一个子选项, 第一个参数是子命令的id,这个宏必须在CMD_OPTIONS_NODE_START 和 CMD_OPTIONS_NODE_END之间。
MSH_OPT_ID_GET:用于检索子选项对应的id,也是CMD_OPTIONS_NODE中填写的id,可以与switch配合使用。
CMD_OPTIONS_STATEMENT:这个宏必须定义在命令函数实现之前,因为使用这个宏会创建一个结构体参数。
MSH_OPT_DUMP:用于dump出子命令及命令描述。
MSH_CMD_EXPORT 和 MSH_CMD_EXPORT_ALIAS宏与之前兼容,如需使用二级补全,只需再添加一项参数,内容不限。
宏对比:
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)
使用二级补全实现一个demo,用于读写内存
#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
全部0条评论
快来发表一下你的评论吧 !