Uboot中添加命令

u-boot的命令格式

U_BOOT_CMD(name,maxargs,repeatable,command,”usage”,“help”)
name:命令的名字,不是一个字符串;
maxargs:最大的参数个数;
repeatable:命令是否可重复的(1重复,0不重复)(即按下Enter键的时候,自动执行上次的命令);
command:对应的函数指针
第5个参数:帮助信息(short)
第6个参数:帮助信息(long)

基本步骤

1.在uboot/common目录下,随便找一个cmd_xxx.c文件,将cmd_xxx.c文件拷贝一下,并重新命名为cmd_hello.c

cp cmd_xxx.c cmd_hello.c

2.进入到cmd_hello.c中,定义执行函数
执行函数格式:int do_hello(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
然后在该函数中添加你想要做的事,比如打印一行信息

int do_hello(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
	//argc表示有几个参数
	//argv[x]表示第x个参数的值
	printf("hello world\n");
	return 0;  
}

3.uboot中添加命令基本操作已经做完,但是还差一步,就是将该命令添加进uboot中,之前的操作虽然添加了一个cmd_hello.c文件,但是还没有将该文件放进Uboot的代码中,所以,我们需要在uboot下common文件的Makfile中添加一行:

COBJS-y += cmd_hello.o

完整的例子

#include 
#include 


static int do_ctl(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
    printf("contrl  command \n");

    if(argc < 3  ||  argc  > 4 )
        return  CMD_RET_USAGE;    // age  err


    if (strcmp(argv[1], "led") == 0) 
    {
        /* goto  contrl  LED */
            printf("goto  contrl  LED \n");
            
    
        return CMD_RET_SUCCESS;
    }
    else if (strcmp(argv[1], "beep") == 0) {
        /* goto  contrl  BEEP */
        printf("goto  contrl  BEEP \n");


        return CMD_RET_SUCCESS;
    }


    return  CMD_RET_SUCCESS;  //CMD_RET_SUCCESS

}


U_BOOT_CMD(
    ctl,    CONFIG_SYS_MAXARGS,    1,    do_ctl,
    "command  ctl",
    " device contrl command \n"
    "  contrl led   -----  ex.  ctl   led    ledx   on/off \n"
    "         ledx  -----  ex.  led1  led2   led3   led4    \n"
    "  contrl beep  -----  ex.  ctl   beep   on/off        \n"
);    


----------------------------------demo3----------------------------------


#include 
#include 


#define     GPIOEALTFN0   (*(volatile unsigned int *)0xC001E020)
#define     GPIOEOUTENB   (*(volatile unsigned int *)0xC001E004)
#define     GPIOEOUT      (*(volatile unsigned int *)0xC001E000)  

#define     GPIOCALTFN0   (*(volatile unsigned int *)0xC001C020)
#define     GPIOCALTFN1   (*(volatile unsigned int *)0xC001C024)
#define     GPIOCOUTENB   (*(volatile unsigned int *)0xC001C004)
#define     GPIOCOUT      (*(volatile unsigned int *)0xC001C000)  


void gpio_init(void)
{

    //1.将GPIOE13设置成普通的GPIO功能
    GPIOEALTFN0 &=    ~(0x3<<26);//function 0

        //2.将GPIOC17/14/8/7设置成普通的GPIO功能
    GPIOCALTFN1   =    GPIOCALTFN1 &  ~(0x3 << 2)  | (0x1 << 2);  // GPIOC17
    GPIOCALTFN0   =    GPIOCALTFN0 &  ~(0x3 << 28) | (0x1 << 28);// GPIOC14
    GPIOCALTFN0   =    GPIOCALTFN0 &  ~(0x3 << 16) | (0x1 << 16);// GPIOC8
    GPIOCALTFN0   =    GPIOCALTFN0 &  ~(0x3 << 14) | (0x1 << 14);// GPIOC7

    //3.将GPIOE13/GPIOC17/8/7设置成输出引脚
    GPIOEOUTENB  |=    (0x1<<13);
    GPIOCOUTENB  |=    (0x1 << 17) | (0x1 << 14) | (0x1 << 8)| (0x1 << 7);

    //4.设置GPIO电平
    GPIOEOUT     |=    (0x1 << 13) ;
    GPIOCOUT     |=    (0x1 << 17) |(0x1 << 8)| (0x1 << 7);
    GPIOCOUT     &=    ~(0x1 << 14);
}


static int do_ctl(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
    int state = -1;
    printf("contrl  command \n");

    if(argc < 3  ||  argc  > 4 )
        return  CMD_RET_USAGE;    // age  err


    gpio_init();

        if(argc == 4)
    {
        if (strcmp(argv[3], "on") == 0)
            state = 0;
        else if (strcmp(argv[3], "off") == 0)
            state = 1;        
    }
        else if(argc == 3)
    {
        if (strcmp(argv[2], "on") == 0)
            state = 1;
        else if (strcmp(argv[2], "off") == 0)
            state = 0;        
    }

    if(state == -1)
        return  CMD_RET_USAGE; 

    if (strcmp(argv[1], "led") == 0) {
        /* goto  contrl  LED */
            printf("goto  contrl  LED \n");

        if (strcmp(argv[2], "led1") == 0) {
        
        GPIOEOUT     =  (GPIOEOUT &  ~(0x1 << 13)) | state << 13 ;
       
        }else   if (strcmp(argv[2], "led2") == 0) {
        
        GPIOCOUT     =  (GPIOCOUT &  ~(0x1 << 17)) | state << 17 ;
       
        }else   if (strcmp(argv[2], "led3") == 0) {
        
        GPIOCOUT     =  (GPIOCOUT &  ~(0x1 << 8)) | state << 8 ;    
       
        }else   if (strcmp(argv[2], "led4") == 0) {
        
        GPIOCOUT     =  (GPIOCOUT &  ~(0x1 << 7)) | state << 7 ;    
       
        }

    }
    else if (strcmp(argv[1], "beep") == 0) {
        /* goto  contrl  BEEP */
        printf("goto  contrl  BEEP \n");
        GPIOCOUT     =  (GPIOCOUT &  ~(0x1 << 14)) | state << 14 ;
        return CMD_RET_SUCCESS;
    }


    return  CMD_RET_SUCCESS;  //CMD_RET_SUCCESS

}


U_BOOT_CMD(
    ctl,    CONFIG_SYS_MAXARGS,    1,    do_ctl,
    "command  ctl",
    " device contrl command \n"
    "  contrl led   -----  ex.  ctl   led    ledx   on/off \n"
    "         ledx  -----  ex.  led1  led2   led3   led4    \n"
    "  contrl beep  -----  ex.  ctl   beep   on/off        \n"
);    

你可能感兴趣的:(uboot学习)