An open source lightweight microcontroller command line interactive component

Follow the + star public account and never miss exciting content

44107c8ed2c6d48d8b31a6e81bacb9cd.gif

Source | Gitee

Author | Nrush

Command line interaction is relatively common in host computer software, and there are many cases of implementing command interaction in microcontrollers. Here is an open source command line tool: nr_micro_shell .

Project addressgithub.com/Nrusher/nr_micro_shell

1 Introduction

When debugging and maintaining, it is often necessary to interact with the microcontroller to obtain and set certain parameters or perform certain operations. nr_micro_shell is a basic command line tool written for MCUs with fewer resources to meet this demand.

Although the powerful finsh command line interaction tool has been provided in the RT_Thread component, finsh is still slightly bulky for microcontrollers with less ROM and RAM resources. On these platforms, if you still want to retain the basic command line interaction function, nr_micro_shell is a good choice.

nr_micro_shell has the following advantages

1. It takes up less resources, is simple to use, flexible and convenient. The usage process only involves two functions, shell_init() and shell(). This tool can be easily applied whether using RTOS or bare metal without additional coding work.

2. Good interactive experience. Completely similar to the Linux shell command line, when the serial port terminal supports ANSI (such as Hypertrm terminal), it not only supports basic command line interaction, but also provides Tab key command completion, query history commands, and arrow keys to move the cursor and modify it.

3. Good scalability. nr_micro_shell provides users with standard function prototypes for custom commands. You only need to write the command function according to the command and register the command function to use the command.

 Comparison of resource usage between nr_micro_shell and finsh under the same configuration (finsh does not use msh)


original project Add nr_micro_shell increment Add finsh increment
ROM 63660 +3832 +26908
RAM 4696 +1104 +1304

Both configurations are

  • Up to 3 historical commands.

  • Supports Tab completion.

  • The maximum length of the command line is 100.

  • Up to 10 command parameters.

  • The command line thread stack is 512 bytes.

nr_micro_shell demonstration effect is as follows

3836a5ec485db70feb406860105c6057.gif

0fec425982f95ef9fee5d856869ab305.gif

1.1 Directory structure

name illustrate
docs Document directory, including demo GIF images, etc.
examples Example directory, including command function examples: nr_micro_shell_commands.c  and RT_Thread usage examples  nr_micro_shell_thread.c
inc Header file directory
src Source code directory

1.2 License

The nr_micro_shell package is licensed under the MIT license, see  LICENSE documentation for details.

1.3 Dependencies

No dependencies

2. Use of ENV tool under RT_Thread

RT_Thread To use the nr_micro_shell package package, you need to select it in the package manager of RT-Thread. The specific path is as follows:

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

After pressing the key to select the relevant settings sapce, press enterto configure the relevant parameters. Then let RT-Thread's package manager update automatically, or use  pkgs --update the command to update the package to the BSP.

If you need to run the example, please ensure that the option in the RT_Thread configuration Using console for kt_printf.is turned on, kt_printf can work normally, and Use components automatically initialization.the option is turned on. You can use nr_micro_shell by compiling and directly downloading or simulating. When the command line is blank, press Tab to display all supported commands. For test example commands, see the usage example animation under doc/pic. To customize the command process, refer to 3. below. Use the method in the nr_micro_shell package under bare metal.

3. Use under bare metal

3.1 Configuration:

All configuration work can be done in  nr_micro_shell_config.h  . See the comments in the file for details.

3.2 Usage:

  • Make sure all files have been added to the project.

  • Make sure that  the macro functions "shell_printf(), ansi_show_char()" in nr_micro_shell_config.h  can be used normally in the project.

  • Usage examples are as follows

#include "nr_micro_shell.h"

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

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

Before using hardware input directly, it is recommended to use the following code (to ensure that information can be printed normally) to verify whether nr_micro_shell can run normally.

#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 Add your own commands

STEP1:

You need to implement a command function in nr_micro_shell_commands.c *. The prototype of the command function is as follows

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

argc is the number of arguments. argv stores the starting address and content of each parameter. If the input string is

test -a 1

Then argc is 3, and the content of argv is

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

If you want to know the contents of the first or second parameter, you should use

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

STEP2 : You need to register the command before using it. There are two ways to register the command.

1. When it is not defined in the configuration file NR_SHELL_USING_EXPORT_CMD, static_cmd[]write it in the table

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

Note: Do not delete {"\0", NULL}!

2. When it is defined in the configuration file NR_SHELL_USING_EXPORT_CMDand NR_SHELL_CMD_EXPORT()supports the compiler used, you can use the following method to register the command

NR_SHELL_CMD_EXPORT(your_command_name,your_command_funtion);

4. Use nr_micro_shell simulation under linux

./examples/simulator/The nr_micro_shell simulation code is stored in the project directory. ./examples/nr_micro_shell_commands.cAdd the custom command in the file as above. After the addition is completed, you can use the make command to compile the source code. The produced executable file is ./examples/simulator/out/nr_micro_shellor ./examples/simulator/out/nr_micro_shell_db. The make commands that can be used are as follows:

# 编译可执行文件
make
# 编译可仿真执行文件
make debug
# 清除编译生成文件
make clean

5. Things to note

Use NR_SHELL_USING_EXPORT_CMD to select the command registration method according to your usage habits.

When using the registry registration command, make sure the registry exists in your project

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

When using NR_SHELL_CMD_EXPORT(), make sure that the compiler used by NR_SHELL_CMD_EXPORT() supports it, otherwise an error will be reported.

nr_micro_shell does not support control keys (control characters) such as the ESC key.

original

Come

source

At:

https://gitee.com/nrush/nr_micro_shell

Statement: The material of this article comes from the Internet, and the copyright belongs to the original author. If there is any copyright issue with the work, please contact me to delete it.

------------ END ------------

7bba679c842f1263d2cf3030eff79de8.gif

●Column "Embedded Tools "

●Column "Embedded Development"

●Column "Keil Tutorial"

●Embedded column selected tutorials

Follow the official account and reply " Add Group " to join the technical exchange group according to the rules, and reply " 1024 " to view more content.

b7c2e4720c687bef6c9911af0dd4c612.jpeg

a817a871e0bbd290300716fe70df525d.png

Click " Read the original text " to view more sharing.

Guess you like

Origin blog.csdn.net/ybhuangfugui/article/details/133365883