Chinaunix首页 | 论坛 | 博客
  • 博客访问: 438783
  • 博文数量: 52
  • 博客积分: 3193
  • 博客等级: 中校
  • 技术积分: 860
  • 用 户 组: 普通用户
  • 注册时间: 2007-11-19 09:58
文章分类

全部博文(52)

文章存档

2012年(1)

2011年(9)

2009年(18)

2008年(24)

我的朋友

分类: LINUX

2008-09-22 13:13:12

最近想再U-boot中加一个LED点亮led灯的命令,就研究啦一下U-Boot中的命令工作原理!

 

原理:

每个命令都有一个命令结构体

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

};去定义它。Cmd为要调用的命令函数!name为该命令名字符串。

 

u-boot里面有这样的宏

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

 

#define U_BOOT_CMD(name,maxargs,rep,cmd,usage,help) \

cmd_tbl_t __u_boot_cmd_##name Struct_Section = {#name, maxargs, rep, cmd, usage, help}

 

U_BOOT_CMD(name,maxargs,rep,cmd,usage,help)就是将

cmd_tbl_s{

name,

maxargs,

rep,

cmd,

usage,

help

} 这样的一个命令结构体放入内存.u_boot_cmd这个区域,.u_boot_cmd这个域在board/smdk2410/u-boot.lds中定义!在U-boot中的shell中,根据用户输入的命令,就会在.u_boot_cmd这个内存区域中查找,当.u_boot_cmd中某一个 cmd_tbl_s命令结构体的cmd_tbl_s.name和输入的命令字符串相符时,就调用该命令

结构体的cmd_tbl_s.cmd( .)函数!

 

 

怎样添加命令函参数!

下面以添加LED点亮led灯命令为例!

 

1. include/configs/smdk2410.h中增加一项:

#define CONFIG_CMD_LED

 

2. common\下面加入cmd_leds.c

框架如下

 

 

#if defined(CONFIG_CMD_LED)

 

int do_led_test (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])

 

{

 

。。。。。。。

 

}

 

 

 

U_BOOT_CMD(

       led,    2,    1,     do_led_test,

       "led   - simple led test\n",

       " [1]|[0]| \n"

       "    - simple LED on/off/shining test\n"

);

 

#endif

 

3. common/Makefile 添加要编译的目标文件

4. 重新编译u-boot,OK啦!

阅读(1958) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~