在u-boot中如果我们要实现自己命令的具体功能,在comman文件夹中建立对应的.c文件。首先为了能让系统找到该指令,所以要在命令表中注册一下。
#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}
“##”与“#”都是预编译操作符,“##”有字符串连接的功能,“#”表示后面紧接着的是一个字符串。
#define Struct_Section __attribute__ ((unused,section (".u_boot_cmd")))
其中,unused表示该函数或变量可能不使用,这个属性可以避免编译器产生警告信息。
凡通过U_BOOT_CMD定义的cmd_tbl_t变量会全部被放在.u_boot_cmd段当中。这也是在你写的.c文件的末尾必须要写的,为了完成注册这个动作。
比如说:U_BOOT_CMD(nandboot,0,0,do_nandboot,"boot from nand","--help") 通过宏展开就是:
_u_boot_cmd_nandboot __attribute__((unused, section(".u_boot_cmd"))) = {"nandboot", 0, 0, do_nandboot, "boot from nand","--help"}
具体的用法例子:
#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}
“##”与“#”都是预编译操作符,“##”有字符串连接的功能,“#”表示后面紧接着的是一个字符串。
#define Struct_Section __attribute__ ((unused,section (".u_boot_cmd")))
其中,unused表示该函数或变量可能不使用,这个属性可以避免编译器产生警告信息。
凡通过U_BOOT_CMD定义的cmd_tbl_t变量会全部被放在.u_boot_cmd段当中。这也是在你写的.c文件的末尾必须要写的,为了完成注册这个动作。
比如说:U_BOOT_CMD(nandboot,0,0,do_nandboot,"boot from nand","--help") 通过宏展开就是:
_u_boot_cmd_nandboot __attribute__((unused, section(".u_boot_cmd"))) = {"nandboot", 0, 0, do_nandboot, "boot from nand","--help"}
关于cmd_tbl_s结构体定义如下:
struct cmd_tbl_s {
char *name; /* 命令名*/
int maxargs; /* 最大参数个数*/
int repeatable; /* 是否自动重复*/
int (*cmd)(struct cmd_tbl_s *, int, int, char *[]); /* 响应函数*/
char *usage; /* 简短的帮助信息*/
#ifdef CONFIG_SYS_LONGHELP
char *help; /* 较详细的帮助信息*/
#endif
#ifdef CONFIG_AUTO_COMPLETE
/* 自动补全参数*/
int (*complete)(int argc, char *argv[], char last_char, int maxv, char *cmdv[]);
#endif
};具体的用法例子:
比如我们要加入一条mytest命令:比如点亮我们板子上的LED灯
int do_mytest(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
//init the registor of GPIO
switch(argc)
{
case 2:
switch(on_off(argv[1]))
{
case 0 :
{*(volatile unsigned long *)0x30006110 = 0x0;
*(volatile unsigned long *)0x30006114 = 0x0e00;
*(volatile unsigned long *)0x30066118 = 0xffffffff;
printf("is off?");
}
break;
case 1 : {*(volatile unsigned long *)0x30006110 = 0x0;
*(volatile unsigned long *)0x30006114 = 0x0e00;
*(volatile unsigned long *)0x30066118 = 0x0;
printf("is on?");
}
break;
default : break;
}
break;
default:
printf("%s - %s\n", cmdtp -> name, cmdtp -> usage);
}
// enable the LED
return 0;
}
U_BOOT_CMD(
mytest, 2, 1, do_mytest,
"light up 3 of the led ~~",
"you can light on/off the led on the board by : mytest on/off"
);
要使得该命令可以在我们的u-boot里面去使用还需要改一些配置文件
1)在uboot/common/Makefile文件中加入需要编译的命令文件,使得该cmd_xxxxx文件可以得到编译,例如加入COBJS-$(CONFIG_CMD_MYTEST) += cmd_mytest.o ,可以让cmd_mytest.c文件可以编译生成.o文件
2)在u-boot\include\config_cmd_all.h
u-boot\include\config_cmd_default.h两个文件中加入预定义宏,使得uboot在编译命令时能加入该命令。例如#define CONFIG_CMD_MYTEST
3)在u-boot\include\configs\SEP0611.h文件中同样加入宏定义
4)最后执行编译即可
本文介绍如何在U-Boot中实现自定义命令,包括创建命令文件、注册命令及必要的配置步骤。通过实例展示了点亮LED灯的具体实现过程。

201

被折叠的 条评论
为什么被折叠?



