Abstract: How can RTOS system, through the serial shell control LED switch.
In everyday embedded development, we often use serial port commands to put a device into a specific state or perform a specific operation. For example, system self-check, simulation operation, or enter manual mode for device activation. There are powerful shell tools under Linux, which can allow users to interact with the system on chip. However, in the traditional single-chip system, users often need to implement a set of similar interactive tools. Alios-things has a command-line interface (CLI) that supports user – defined commands in addition to basic system interaction commands. This article describes how to customize cli commands and execute them.
We demonstrate how a CLI command with parameters can be registered and invoked by registering a CLI command to control the LED on and off from the command line, based on AliOS Things Learning Notes running the Blink Routine on Developerkit.
First, let’s make sure the original CLI features of Developerkit are available. Connect development board to PC through USB cable.
For Windows users, check the serial port number created by the development board on the device manager. For MAC and Linux users, run the following command on the terminal to check whether the USB serial port is properly connected.
ls /dev/tty.*
Copy the code
If the following device list is displayed, the connection is correct. The numbers after the usbmodem may vary from computer to computer.
/dev/tty.usbmodem14103
Copy the code
In this case, run the serial port debugging tool on the PC and set the serial port. The baud rate is 115200bps by default. It is recommended that the serial port debugging assistant be equipped with terminal function to provide better CLI experience. Next, I’ll use the serial port monitor capabilities of the aos-cube tool built into VScode as an example. Open VScode and make sure you are following the alios-studio and aos-cube plug-ins. Click the plug button in the Alios-Studio toolbar to start the serial port Monitor
If the tool does not open the serial port correctly, you can also try starting the serial port Monitor from the command line in the console. The command format is as follows:
aos monitor /dev/tty.usbmodem14103 115200 # MAC Linux
aos monitor com5 115200 # under Windows commands
Copy the code
After the serial port Monitor is successfully accessed, the help information is displayed on the terminal. Type enter, and a “#” prompt will appear, at which point you can enter the command.
- Miniterm on/dev/tty usbmodem14103, 8, 115200 N, 1 -- -- -- -- -- - Quit: Ctrl +] | Menu: Ctrl + T | Help: Ctrl+T followed by Ctrl+H ---#
#
Copy the code
Enter help and press Enter to view the currently supported CLI commands
# help====Build-in Commands==== ====Support 4 cmds once, seperate by ; = = = =help : print this
p : print memory
m : modify memory
echo : echo for command
exit : close CLI
devname : print device name
sysver : system version
reboot : reboot system
time : system time
ota : system ota
====User Commands====
loglevel : set log level
tasklist : list all thread info
dumpsys : dump system info
udp : [ip] [port] [string data] send udp data
wifi_debug: wifi debug mode
mac : get/set mac
kv : kv [set key value | get key | del key | list]
version : show version
#
Copy the code
Enter TaskList to view the running status of existing tasks
# tasklist------------------------------------------------------------------------ cpu usage period = 25 CPU usage : 1.89 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the Name State Prio StackSize MinFreesize Runtime %CPU Candidate ------------------------------------------------------------------------ Dyn_mem_proc_task PEND 6 256 216 22 0.00 N IDLE_task RDY 61 200 177 745102697 98.11 N default-workqueue PEND 20 768 738 23 0.00 N timer_Task PEND 5 300 254 25 0.00 N aos-init PEND 32 1536 1389 1419569 1.38n cli RDY 60 512 271 491760 0.47y ------------------------------------------------------------------------Copy the code
Now that we’ve verified cli functionality on the Developerkit development board, let’s register and use our own CLI commands.
We still open the blink.c file and add the code implementation there. First, the registration of CLI commands relies on a cli_command structure described as follows:
struct cli_command { const char *name; // The command body, const char *help; // Help text for the command, void (*function)(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv); // The function actually called when the command is executed};Copy the code
In this example, we name the CLI command “led_switch” and the help text string is “[on] turn on led2; [off] trun off led2”
Next, let’s implement the cli command functions. Add the following functions directly to the blink.c file.
static void led_switch(char *pwbuf, int blen, int argc, char **argv)
{
if(argc == 1) {LOG(argc == 1)"Parameter error");
return;
}
if(strcmp(argv[1],"on"Hal_gpio_output_low (& LED); hal_GPIo_output_low (& LED); // GPIO output low, lit LED2}else{ hal_gpio_output_high(&led); // GPIO output high, off LED2}}Copy the code
Cli functions have fixed forms and parameters. You only need to change the function name by referring to the preceding functions. Ensure that the parameters are consistent with the preceding functions.
The parameter name
|
Parameters to describe
|
char *pwbuf
|
A pointer to a string printed through the console when the function returns after execution. You don’t have to pass it in.
|
int blen
|
The above string length
|
int argc
|
The length of the argument passed in when the command is called, or 1 if no argument is passed in
|
char **argv
|
Pass in parameter cache, string. The first valid parameter starts at 1.
|
Next fill in the registry structure to pass command information to the CLI service. According to the design mentioned above, register the structure information as follows, copy the structure to the blink.c file.
struct cli_command led_switch_command[] = {
{
.name = "led_switch"// The command name.help ="[on] turn on led2; [off] trun off led2"Function = led_switch // the function pointer to which the command is executed}};Copy the code
Registers cli commands with the system. Cli command registration requires only one function as follows:
aos_cli_register_commands(&led_switch_command[0],1);
Copy the code
The function takes two arguments. Parameter 1 is the structure pointer to the command body that was just created. Parameter 2 is the number of commands to be registered this time. That is to say, multiple commands can be registered at one time. You only need to fill the information of several commands in the command information structure with an array.
Add the above function call to the blink.c file application_start function as follows:
aos_cli_register_commands(&led_switch_command[0],1); // Register the cli command function aos_loop_run();Copy the code
At this point, the coding part of the implementation and registration of a CLI command is complete. Compile and download to the development board to run. On the CLI console, enter help. The led_switch command is registered successfully.
# help====Build-in Commands==== ====Support 4 cmds once, seperate by ; = = = =help : print this
p : print memory
m : modify memory
echo : echo for command
exit : close CLI
devname : print device name
sysver : system version
reboot : reboot system
time : system time
ota : system ota
====User Commands====
loglevel : set log level
tasklist : list all thread info
dumpsys : dump system info
udp : [ip] [port] [string data] send udp data
wifi_debug: wifi debug mode
mac : get/set mac
kv : kv [setkey value | get key | del key | list] version : show version led_switch: [on] turn on led2; [off] trun off led2Copy the code
To test whether the command is executed, enter the following command at the command prompt and press Enter:
#led_switch on
Copy the code
Led2 light
To test the validity of parameters, enter the following command without parameters. The command returns an error alarm by design.
## led_switch[1291080]<V> Parameter errorCopy the code
Give it a try and add your own CLI functionality!