• uboot添加自定义命令


    版本

    v2024.01-rc2

    添加命令

    在 uboot 中添加一个命令控制 led 的输出,led 通过 HC595 控制。

    总共需要修改3个文件,可参考 cmd/gpio.c 文件

    主要是下面四个函数:

    gpio_request(gpio, "cmd_gpio"); 
    gpio_direction_output(gpio, 0);
    gpio_set_value(gpio, 1);
    gpio_free(gpio);
    
    • 1
    • 2
    • 3
    • 4

    gpio 值可根据不通芯片计算得出,如这里使用的 rk3588 的 gpio1_d6
    计算方式 32 * 1 + 3 * 8 + 6 = 62

    • U_BOOT_CMD 包含6个参数,ctrl c+v 以下含义:
      1:添加的命令的名字
      2:添加的命令最多有几个参数(注意,假如你设置的参数个数是3,而实际的参数个数是4,
      那么执行命令会输出帮助信息的)
      3:是否重复(1重复,0不重复)(即按下Enter键的时候,自动执行上次的命令)
      4:执行函数,即运行了命令具体做啥会在这个函数中体现出来
      5:帮助信息(short)
      6:帮助信息(long
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    修改添加

    • cmd 目录下,新建文件 leds.c
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    static int do_leds(struct cmd_tbl *cmdtp, int flag, int argc,
    		   char *const argv[])
    {
    	//GPIO1_D6  GPIO1_B0  GPIO1_D7
    	unsigned int gpio_rclk=62, gpio_srclk=40, gpio_sdi=63;
    	int ret;
    	
    	if (argc < 2)
    		return CMD_RET_USAGE;
    	
    	int value = hextoul(argv[1], NULL);
    
    //#if defined(CONFIG_DM_GPIO)
    	/*
    	 * TODO(sjg@chromium.org): For now we must fit into the existing GPIO
    	 * framework, so we look up the name here and convert it to a GPIO number.
    	 * Once all GPIO drivers are converted to driver model, we can change the
    	 * code here to use the GPIO uclass interface instead of the numbered
    	 * GPIO compatibility layer.
    	 */
    //	ret = gpio_lookup_name(str_gpio, NULL, NULL, &gpio);
    //	if (ret) {
    //		printf("GPIO: '%s' not found\n", str_gpio);
    //		return cmd_process_error(cmdtp, ret);
    //	}
    //#else
    //	/* turn the gpio name into a gpio number */
    //	gpio = name_to_gpio(str_gpio);
    //	if (gpio < 0)
    //		goto show_usage;
    //#endif
    	/* grab the pin before we tweak it */
    	ret = gpio_request(gpio_rclk, "cmd_gpio");
    	if (ret && ret != -EBUSY) {
    		printf("gpio: requesting pin %u failed\n", gpio_rclk);
    		return -1;
    	}
    	gpio_direction_output(gpio_rclk, 0);
    	
    	ret = gpio_request(gpio_srclk, "cmd_gpio");
    	if (ret && ret != -EBUSY) {
    		printf("gpio: requesting pin %u failed\n", gpio_srclk);
    		return -1;
    	}	
    	gpio_direction_output(gpio_srclk, 0);
    	
    	ret = gpio_request(gpio_sdi, "cmd_gpio");
    	if (ret && ret != -EBUSY) {
    		printf("gpio: requesting pin %u failed\n", gpio_sdi);
    		return -1;
    	}	
    	gpio_direction_output(gpio_sdi, 0);
    
    	for(int i=0; i<16; i++){
    		(value >> i) & 0x1 ? gpio_set_value(gpio_sdi, 1) : gpio_set_value(gpio_sdi, 0);
    		gpio_set_value(gpio_srclk, 1);
    		gpio_set_value(gpio_srclk, 0);
    	}
    	gpio_set_value(gpio_rclk, 1);
    	gpio_set_value(gpio_rclk, 0);
    	
    	if (ret != -EBUSY){
    		gpio_free(gpio_rclk);
    		gpio_free(gpio_srclk);
    		gpio_free(gpio_sdi);
    	}
    	return 0;
    }
    
    U_BOOT_CMD(leds, 2, 0, do_leds,
    	   "leds",
    	   "leds n : n is hex");
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 在同目录下的 Makefile 添加
    obj-$(CONFIG_CMD_LEDS) += leds.o
    
    • 1
    • 在同目录 Kconfig 文件下添加,在紧跟 cmd_gpio 添加即可
    config CMD_LEDS
    	bool "hc595 leds"
    	help
    	  leds support.
    
    • 1
    • 2
    • 3
    • 4

    然后通过 menuconfig 添加即可

    如果使用 obj-y += leds.o 则无需修改 Kconfig

    执行

    在 uboot 中,使用 leds 222 命令,即可执行,数值根据需要修改

    参考

    https://zhuanlan.zhihu.com/p/584094262

  • 相关阅读:
    Windows Linux 子系统迎来多项改进,并增加IPv6支持
    搭建虚拟通道
    Java基础知识点整理
    燕东微通过注册:预计年营收超20亿 亦庄国投与京东方是股东
    注意力机制 - 注意力汇聚:Nadaraya-Watson核回归
    抖音面试:说说延迟任务的调度算法?
    hive函数总结
    msvcr100.dll不存在
    MySQL:数据类型和运算符
    轮播图的两种方法
  • 原文地址:https://blog.csdn.net/feitingfj/article/details/134386639