1,包含inlucde/command.h头文件。
2,使用run_command、run_commandf或run_command_list函数。
int run_command(const char *cmd, int flag);
/**
* run_commandf() - Run a command created by a format string
*
* The command cannot be larger than 127 characters
*
* @fmt: printf() format string
* @...: Arguments to use (flag is always 0)
*/
int run_commandf(const char *fmt, ...);
/**
* Run a list of commands separated by ; or even \0
*
* Note that if 'len' is not -1, then the command does not need to be nul
* terminated, Memory will be allocated for the command in that case.
*
* @param cmd List of commands to run, each separated bu semicolon
* @param len Length of commands excluding terminator if known (-1 if not)
* @param flag Execution flags (CMD_FLAG_...)
* Return: 0 on success, or != 0 on error.
*/
int run_command_list(const char *cmd, int len, int flag);
3, 一个是直接运行command,一个是使用printf格式来构造一个command并运行,最后一个是多个command一起运行,通过分号分隔。
4,Flag参数。
/*
* Command Flags:
*/
#define CMD_FLAG_REPEAT 0x0001 /* repeat last command */
#define CMD_FLAG_BOOTD 0x0002 /* command is from bootd */
#define CMD_FLAG_ENV 0x0004 /* command is from the environment */
大概意思,第一个是命令重复,第二个是和bootd命令相关,第三个和是environment相关的命令。
(bootd - boot default image, 就是boot命令,会从默认image启动,一般就是启动linux kernel image)
5,在代码里调用命令。
比如我想通过代码来控制某个GPIO:
run_command("gpio set GPIO2_11", 0);