Abstract: Shell, as an online debugging tool for Huawei Liteos, can input and output through serial port tools, and supports common basic debugging functions. At the same time, users can add customized commands, and the new commands need to be recompiled and burned before they can be executed.

This article is shared from the HUAWEI cloud community "LIteOS----shell application (1) system shell" , author: o0龙龙0o.

Foreword:

Many times when debugging an application, we cannot set too many breakpoints or observe the real flow of the program, which statements are executed or not, and manually open a self-written application, etc., this time we need Shell command line operation, he can access the functions and services of the operating system in the form of command lines, receive user input commands, and print the output of the operating system.

1. The shell of Huawei Liteos

Shell, as an online debugging tool for Huawei Liteos, can input and output through the serial port tool, and supports common basic debugging functions. At the same time, users can add customized commands, and the new commands need to be recompiled and burned before they can be executed.

2. How to open the shell function

Shell can be configured through make menuconfig, the menu path is: Debug ---> Enable a Debug Version ---> Enable Shell.
image.png

3. Shell commands built in LiteOS

After burning the new system image, restart the system. If the shell function has been turned on, you can use the shell function that comes with the system from the serial port.

  • help, date, uname, task, free, memcheck, memused, hwi, queue, sem, mutex, dlock, swtmr, systeminfo, stack, cpup, watch, etc.

Through these commands, I can complete some basic operations on the system, such as query task execution, memory allocation and help functions for various commands.

For example: help command, enter help on the command line

The system who outputs all current shell commands:

Huawei LiteOS # help
*******************shell commands:*************************

cpup          date          dlock         dmesg         free          help          hwi
log           memcheck      mutex         queue         sem           stack         swtmr
systeminfo    task          uname         watch

The task command is used to query system task information

When task [ID] is entered, the task information of ID can be queried

For example, when you enter task 0x01, the system will print out:

Huawei LiteOS # task 0x01
TaskName = SerialEntryTask
TaskId = 0x01
*******backtrace begin*******
traceback 0 -- lr = 0x1d804    fp = 0xa86bc
traceback 1 -- lr = 0x1da40    fp = 0xa86e4
traceback 2 -- lr = 0x20154    fp = 0xa86fc
traceback 3 -- lr = 0x258e4    fp = 0xa8714
traceback 4 -- lr = 0x242f4    fp = 0xa872c
traceback 5 -- lr = 0x123e4    fp = 0xa8754
traceback 6 -- lr = 0x2a9d8    fp = 0xb0b0b0b

You can try to discover for yourself what the functions of these system functions are, and in fact find better applications.

4. Define the command function

First of all, there must be a function function that also processes commands, and the function function prototype needs to be declared in the header file.

For example, the definition here

int osShellCmdLs(int argc, const char **argv);

Generally, this function needs to include two parameters, argc and argv string pointer. argc: The number of parameters of the Shell command. Whether the command keyword is included in the number is related to the command type when registering the command. argv: an array of pointers, each element points to a string, which is the parameter passed into the command processing function when the shell command is executed. Whether the command keyword is included in the parameter is related to the command type when registering the command.

5. Register the command name

LiteOS shell has two registration methods: static registration command and dynamic registration command when the system is running. The two methods are mainly different in memory setting and release. Static registration requires the relevant support to be enabled in the compiler and settings to take effect. It is generally recommended to use dynamic shell registration, which is simple and easy to operate.

6. Use cases

1. Define a shell function name as pf_best, the function is to print "We r best", first define his function

#include "shell.h"
#include "shcmd.h"

int cmd_pfbest(void)
{
    printf("We r best!\n");
    return 0;
}

Because there is no need for input and output parameters, the function here is of void type.

2. Registration order

Follow the requirements of dynamic registration.

#include "shell.h"
void cmd_init(void)
{
osCmdReg(CMD_TYPE_EX, "pf_best", XARGS, (CMD_CBK_FUNC)cmd_pfbest);
}

After compiling and downloading, you can use this command to interact.

After talking about the shell, don’t you think it’s easy? Let’s try it all

For more learning content, please follow IoT community

Add the WeChat ID (hwc-iot) of Huawei Cloud IoT Assistant, and reply to "Read" for more information

Click to follow and learn about Huawei Cloud's fresh technology for the first time~


华为云开发者联盟
1.4k 声望1.8k 粉丝

生于云,长于云,让开发者成为决定性力量