u-boot添加新命令的方法

转载至:https://www.cnblogs.com/wulei0630/p/6658704.html

具体实现步骤

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

    $vi 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"
    );
    
  2. 在./common/Makefile中添加:

    $vi common/Makefile

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

原理分析

  1. 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}
    
  2. Struct_Section 定义为:

    #define Struct_Section  __attribute__ ((unused,section (".u_boot_cmd")))
    
  3. 我们定义的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 = .;
    
  4. led命令执行过程:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值