To use shell commands, four files, in addition to the clk module, must be included in the build:
Micirum/Software/uC-Clk/Cmd/clk_cmd.c
Micirum/Software/uC-Clk/Cmd/clk_cmd.h
Micrium/Software/uC-Shell/Source/shell.c
Micrium/Software/uC-Shell/Source/shell.h
The file clk_cmd.h
and shell.h
must also be #included in any application or header files initialize µC/Shell or handle shell commands. The following directories must be on the project include path:
Micrium/Software/uC-Clk/Cmd
Micrium/Software/uC-Shell
µC/Shell with the µC/Clk shell commands is initialized in . The file system initialization (FS_Init()
) function should have previously been called.
Initializing µC/Shell
CPU_BOOLEAN App_ShellInit (void) { CPU_BOOLEAN ok; CLK_CMD_ERR clk_cmd_err; ok = Shell_Init(); if (ok == DEF_FAIL) { return (DEF_FAIL); } ClkCmd_Init(&clk_cmd_err); if (clk_cmd_err != CLK_CMD_ERR_NONE) { return (DEF_FAIL); } return (DEF_OK); }
Listing - Initializing µC/Shell
It’s assumed that the application will create a task to receive input from a terminal; this task should be written as shown in Listing - Executing shell commands & handling shell output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | void App_ShellTask ( void *p_arg) { CPU_CHAR cmd_line[MAX_CMD_LEN]; SHELL_ERR err; SHELL_CMD_PARAM cmd_param; CPU_CHAR cwd_path[FS_CFG_FULL_ NAME_LEN + 1u]; (1) Str_Copy (&cwd_path[0], ( CPU_CHAR *) "\\" ); cmd_param.pcur_working_dir = ( void *)cwd_path[0]; cmd_param.pout_opt = ( void *)0; while ( DEF_TRUE ) { App_ShellIn(cmd_line, MAX_CMD_LEN); (2) (3) Shell_Exec(cmd_line, App_ShellOut, &cmd_param, &err); switch (err) { case SHELL_ERR_CMD_NOT_FOUND: case SHELL_ERR_CMD_SEARCH: case SHELL_ERR_ARG_TBL_FULL: App_ShellOut( "Command not found\r\n" , 19, cmd_param.pout_opt); break ; default : break ; } } } /* ************************************************************************************** * App_ShellIn() ************************************************************************************** */ CPU_INT16S App_ShellIn ( CPU_CHAR *pbuf, CPU_INT16U buf_len) { /* $$$$ Store line from terminal/command line into ‘pbuf’; return length of line. */ } /* ************************************************************************************** * App_ShellOut() ************************************************************************************** */ CPU_INT16S App_ShellOut ( CPU_CHAR *pbuf, CPU_INT16U buf_len, void *popt) { /* $$$$ Output ‘pbuf’ data on terminal/command line; return nbr bytes tx’d. */ } |
Listing - Executing shell commands & handling shell output
(1)
The SHELL_CMD_PARAM
structure that will be passed to Shell_Exec()
must be initialized. The pcur_working_dir
member must be assigned a pointer to a string of at least FS_SHELL_CFG_MAX_PATH_LEN
characters. This string must have been initialized to the default working directory path; if the root directory, “\”.
(2)
The next command, ending with a newline, should be read from the command line.
(3)
The received command should be executed with Shell_Exec()
. If the command is a valid command, the appropriate command function will be called. For example, the command “fs_ls
” will result in FSShell_ls()
in fs_shell.c
being called. FSShell_ls()
will then print the entries in the working directory to the command line with the output function App_ShellOut()
, passed as the second argument of Shell_Exec()
.