串口接收到数据,并传递给run_command()函数,run_command()函数调用common/command.c中实现的find_cmd()函数在u_boot_list段内查找命令,并返回cmd_tbl_t结构。然后run_command()函数使用返回的cmd_tbl_t结构中的函数指针调用do_hello(),从而完成命令的执行

cmd_tbl_t *find_cmd (const char *cmd)
{
    cmd_tbl_t *cmdtp;
    cmd_tbl_t *cmdtp_temp = &__u_boot_cmd_start;    /*Init value */
    const char *p;
    int len;
    int n_found = 0;

    /*
     * Some commands allow length modifiers (like "cp.b");
     * compare command name only until first dot.
     */
    len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);

    for (cmdtp = &__u_boot_cmd_start;
         cmdtp != &__u_boot_cmd_end;
         cmdtp++) {
        if (strncmp (cmd, cmdtp->name, len) == 0) {
            if (len == strlen (cmdtp->name))
                return cmdtp;    /* full match */

            cmdtp_temp = cmdtp;    /* abbreviated command ? */
            n_found++;
        }
    }
    if (n_found == 1) {            /* exactly one match */
        return cmdtp_temp;
    }

    return NULL;    /* not found or ambiguous command */
}
在u-boot.1.1.6/board/100ask24x0/u-boot.lds中
__u_boot_cmd_start = .;
    .u_boot_cmd : { *(.u_boot_cmd) }
    __u_boot_cmd_end = .;

在u-boot.1.1.6/include/command.h中

#define U_BOOT_CMD(name,maxargs,rep,cmd,usage,help) \
cmd_tbl_t __u_boot_cmd_##name Struct_Section = {#name, maxargs, rep, cmd, usage}

#define Struct_Section  __attribute__ ((unused,section (".u_boot_cmd")))
//即可解释为:实例化一个cmd_tbl_t类型的结构体,并将它保存在.u_boot_cmd段里
在u-boot.1.1.6/include/command.h中,cmd_tbl_t的定义如下
struct cmd_tbl_s {
    char        *name;        /* Command Name            */
    int        maxargs;    /* maximum number of arguments    */
    int        repeatable;    /* autorepeat allowed?        */
                    /* Implementation function    */
    int        (*cmd)(struct cmd_tbl_s *, int, int, char *[]);
    char        *usage;        /* Usage message    (short)    */
#ifdef    CFG_LONGHELP
    char        *help;        /* Help  message    (long)    */
#endif
#ifdef CONFIG_AUTO_COMPLETE
    /* do auto completion on the arguments */
    int        (*complete)(int argc, char *argv[], char last_char, int maxv, char *cmdv[]);
#endif
};
typedef struct cmd_tbl_s cmd_tbl_t;

在u-boot.1.1.6/common中,新增cmd_hello.c文件

#include <common.h>
#include <command.h>

int do_hello ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] )
{
    printf("this is hello!\n");
    return 0;
}

U_BOOT_CMD(
    hello, 1, 0, do_hello,
    "hello\t- this cmd is hello\n",
    "this cmd is hello,too\n"
);

在u-boot.1.1.6/common/Makefile的COBJS后面添加cmd_hello.o后;在根目录make,烧写;即可出现新增的hello命令

 

 

 

posted on 2019-09-21 15:35  lzd626  阅读(234)  评论(0编辑  收藏  举报