1 Star 0 Fork 193

kelvin / nr_micro_shell

forked from Nrush / nr_micro_shell 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

nr_micro_shell

English Version

v1.0.1

1、介绍

在进行调试和维护时,常常需要与单片机进行交互,获取、设置某些参数或执行某些操作,nr_micro_shell正是为满足这一需求,针对资源较少的MCU编写的基本命令行工具。虽然RT_Thread组件中已经提供了强大的finsh命令行交互工具,但对于ROM、RAM资源较少的单片机,finsh还是略显的庞大,在这些平台上,若仍想保留基本的命令行交互功能,nr_micro_shell是一个不错的选择。

nr_micro_shell具有以下优点

1.占用资源少,使用简单,灵活方便。使用过程只涉及两个shell_init()和shell()两个函数,无论是使用RTOS还是裸机都可以方便的应用该工具,不需要额外的编码工作。

2.交互体验好。完全类似于linux shell命令行,当串口终端支持ANSI(如Hypertrm终端)时,其不仅支持基本的命令行交互,还提供Tab键命令补全,查询历史命令,方向键移动光标修改功能。

3.扩展性好。nr_micro_shell为用户提供自定义命令的标准函数原型,只需要按照命令编写命令函数,并注册命令函数,即可使用命令。

nr_micro_shell和相同配置下的finsh (finsh不使用msh)占用资源对比

原始工程 添加nr_micro_shell增加量 添加finsh增加量
ROM 63660 +3832 +26908
RAM 4696 +1104 +1304

两者配置都为

  • 最多3条历史命令。
  • 支持Tab补全 。
  • 命令行最大长度为100。
  • 最多10个命令参数。
  • 命令行线程堆栈为512字节。

nr_micro_shell演示效果如下

RT演示 裸机演示

1.1 目录结构

名称 说明
docs 文档目录,包含演示的GIF图片等
examples 例子目录,包括命令函数示例: nr_micro_shell_commands.c 和RT_Thread下使用示例 nr_micro_shell_thread.c
inc 头文件目录
src 源代码目录

1.2 许可证

nr_micro_shell package 遵循 MIT 许可,详见 LICENSE 文件。

1.3 依赖

无依赖

2、RT_Thread 下 ENV 工具使用nr_micro_shell package

RT_Thread 使用 nr_micro_shell package package 需要在 RT-Thread 的包管理器中选择它,具体路径如下:

RT-Thread online packages
    miscellaneous packages --->
        [*] nr_micro_shell:Lightweight command line interaction tool. --->

相关的设置在按下sapce键选中后,按enter可进行相关参数配置。然后让 RT-Thread 的包管理器自动更新,或者使用 pkgs --update 命令更新包到 BSP 中。

若您需要运行示例,请保证RT_Thread配置中的Using console for kt_printf.选项是被打开的,kt_printf可以正常工作,且Use components automatically initialization.选项打开。编译直接下载或仿真便可以使用nr_micro_shell。命令行空白时按Tab,可显示所有支持的命令,测试示例命令可见doc/pic下的使用示例动图。自定义命令过程,参照下文3. 裸机下使用nr_micro_shell package中的方法。

3、裸机下使用nr_micro_shell package

3.1 配置:

所有配置工作都可以在 nr_micro_shell_config.h 中完成。有关详细信息,请参见文件中的注释。

3.2 用法:

  • 确保所有文件都已添加到项目中。

  • 确保 nr_micro_shell_config.h 中的宏函数"shell_printf(),ansi_show_char()"可以在项目中正常使用。

  • 使用示例如下

#include "nr_micro_shell.h"

int main(void)
{
    /* 初始化 */
    shell_init();

    while(1)
    {
        if(USART GET A CHAR 'c')
        {
            /* nr_micro_shell接收字符 */
            shell(c);
        }
    }
}

建议直接使用硬件输入前,建议使用如下代码(确保可以正常打印信息),验证nr_micro_shell是否可以正常运行

#include "nr_micro_shell.h"

int main(void)
{
    unsigned int i = 0;
    //匹配好结束符配置 NR_SHELL_END_OF_LINE 0
    char test_line[] = "test 1 2 3\n"
    /* 初始化 */
    shell_init();
    
    /* 初步测试代码 */
    for(i = 0; i < sizeof(test_line)-1; i++)
    {
        shell(test_line[i]);
    }

    /* 正式工作代码 */
    while(1)
    {
        if(USART GET A CHAR 'c')
        {
            /* nr_micro_shell接收字符 */
            shell(c);
        }
    }
}

3.3 添加自己的命令

STEP1:

您需要在nr_micro_shell_commands.c*中实现一个命令函数。命令函数的原型如下

void your_command_funtion(char argc, char *argv)
{
    .....
}

argc是参数的数目。argv存储每个参数的起始地址和内容。如果输入字符串是

test -a 1

argc为3,argv的内容为

-------------------------------------------------------------
0x03|0x08|0x0b|'t'|'e'|'s'|'t'|'\0'|'-'|'a'|'\0'|'1'|'\0'|
-------------------------------------------------------------

如果想知道第一个或第二个参数的内容,应该使用

/* "-a" */
printf(argv[argv[1]])
/* "1" */
printf(argv[argv[2]])

STEP2: 在使用命令前需要注册命令,共有两种方法注册命令

1.当配置文件中NR_SHELL_USING_EXPORT_CMD未被定义,在**static_cmd[]**表中写入

const static_cmd_st static_cmd[] =
{
   .....
   {"your_command_name",your_command_funtion},
   .....
   {"\0",NULL}
};

注意:不要删除{"\0",NULL}!

2.当配置文件中NR_SHELL_USING_EXPORT_CMD被定义,且NR_SHELL_CMD_EXPORT()支持使用的编译器时,可以使用以下方式注册命令

NR_SHELL_CMD_EXPORT(your_command_name,your_command_funtion);

4、注意事项

根据你的使用习惯使用NR_SHELL_USING_EXPORT_CMD选择命令注册方式。

使用注册表注册命令时,确保您的工程中存在注册表

const static_cmd_st static_cmd[] =
{
   .....
   {"\0",NULL}
};

使用NR_SHELL_CMD_EXPORT()时确保,NR_SHELL_CMD_EXPORT()支持使用的编译器,否则会报错。

nr_micro_shell 不支持ESC键等控制键(控制符)。

5、联系方式 & 感谢

# Released under MIT License Copyright (c) 2019 Ji Youzhou. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

shell for MCU. 单片机命令行交互。 展开 收起
C
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C
1
https://gitee.com/fmaxwl/nr_micro_shell.git
git@gitee.com:fmaxwl/nr_micro_shell.git
fmaxwl
nr_micro_shell
nr_micro_shell
master

搜索帮助