UBoot添加命令的方法

1. 具体实现步骤

① 在./common文件夹下新建cmd_led.c,并在此文件中添加如下内容

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

int do_led (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
    printf("Led test start\n");
    return 0;
}

U_BOOT_CMD(
    led,    2,    0,    do_led,
    "led test command",
    "led test command"
);

 

② 在./common/Makefile中添加:

COBJS-y += cmd_led.o

 

③ 在Linux环境下,重新编译u-boot,得到u-boot.bin,并下载到自己的开发板中测试,输入help就会发现led命令

 

2.原理分析

① U_BOOT_CMD定义在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}

 

② Struct_Section 定义为:

#define Struct_Section  __attribute__ ((unused,section (".u_boot_cmd")))

 

③ 我们定义的led命令结构展开为:

cmd_tbl_t __u_boot_cmd_led __attribute__ ((unused, section (".u_boot_cmd")))= {"led", 2, 0, do_led, "Led Usage", "Led Help"};

很明显,这是在定义一个cmd_tbl_t类型的变量__u_boot_cmd_led,并给他赋初值。
__attribute__((unused, section(".u_boot_cmd")))该部分是对变量的特殊声明(GNU特有),告诉编译器,该变量存放在链接脚本所指定的u_boot_cmd段。

    __u_boot_cmd_start = .;
    .u_boot_cmd : { *(.u_boot_cmd) }
    __u_boot_cmd_end = .;

 

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

posted @ 2017-04-02 12:06  99度的水  阅读(3922)  评论(0编辑  收藏  举报