命令的结构体表示(include/command.h)
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
};
使用U_BOOT_CMD()这个宏来声明一个命令。
示例:
U_BOOT_CMD(
flinfo, 2, 1, do_flinfo,
"flinfo - print FLASH memory information\n",
"\n - print information for all FLASH memory banks\n"
"flinfo N\n - print information for FLASH memory bank # N\n"
);
#define Struct_Section __attribute__ ((unused,section (".u_boot_cmd")))
#ifdef CFG_LONGHELP
#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}
#else /* no long help info */
#define U_BOOT_CMD(name,maxargs,rep,cmd,usage,help) \
cmd_tbl_t __u_boot_cmd_##name Struct_Section = {#name, maxargs, rep, cmd, usage}
#endif /* CFG_LONGHELP */
比如: cmd_console.c文件
………………………………………………….
void do_test_addcmd()
{
printf("only test adding command\n");
}
/***************************************************/
U_BOOT_CMD(
coninfo, 3, 1, do_coninfo,
"coninfo - print console devices and information\n",
""
);
U_BOOT_CMD(
test_addcmd, 1, 1, do_test_addcmd,
"test_addcmd - only test adding command \n",
""
);
实例:
在U-Boot中添加一个打开或关闭蜂鸣器的命令:当输入buzzer open时打开蜂鸣器使它响,输入buzzer close时关闭蜂鸣器。
cmd_buzzer.c(添加在common目录下,并在Makefile添加“COBJS-y +=cmd_buzze.o”语句):
#include <common.h>
#include <command.h>
#define GPD0CON ( *((volatile unsigned long *)0xE02000A0) )
#define GPD0DAT ( *((volatile unsigned long *)0xE02000A4) )
void do_buzzer_addcmd(cmd_tbl_t *cmdtp, int flag, int argc,char *argv[])
{
GPD0CON &= 0xfffffff0;
GPD0CON |= 0x00000001; //设置为输出
GPD0DAT &= 0xfe; //关闭蜂鸣器
if(0 == strcmp(argv[1],"open"))
{
GPD0DAT |= 0x01;
printf("Open the buzzer\n");
}
else if(0 == strcmp(argv[1],"close"))
{
GPD0DAT &= 0xfe;
printf("Close the buzzer\n");
}
else if(0 == strcmp(argv[1],"--help"))
printf ("Help:\n%s\n", cmdtp->help);
else
printf ("Usage:\n%s\n", cmdtp->usage);
}
U_BOOT_CMD(
buzzer, 2, 1, do_buzzer_addcmd,
"\tbuzzer --Close or open the buzzer \n",//命令说明
"\tbuzzer open --Open the buzzer\n" //帮助信息
"\tbuzzer close --Close the buzzer\n"
);
在顶层make,把生成的u-boot.bin下载到开发板上,在u-boot下运行命令。
附录(下载、烧写U-Boot步骤,s5pv210为例):
1. 使用USB线通过DNW下载sq210_usb.bin到0xd0020010(初始化板子,使板子有一个下载环境)
2. 使用USB线通过DNW下载u-boot.bin到0x23e00000(下载到内存(RAM))
3. 烧录uboot(烧写到flash)
nand erase 0x0 0x60000
tftp 0x20008000 u-boot.bin
nand write 0x20008000 0x0 0x60000