码迷,mamicode.com
首页 > 其他好文 > 详细

UBoot添加命令的方法

时间:2017-04-02 12:24:35      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:char   har   linux   函数   数据   find   开发板   fine   告诉   

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(),从而完成命令的执行。

UBoot添加命令的方法

标签:char   har   linux   函数   数据   find   开发板   fine   告诉   

原文地址:http://www.cnblogs.com/wulei0630/p/6658704.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!