版本
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);
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)
修改添加
- 在
cmd目录下,新建文件 leds.c
#include <common.h>
#include <command.h>
#include <errno.h>
#include <dm.h>
#include <log.h>
#include <malloc.h>
#include <asm/gpio.h>
#include <linux/err.h>
#include <dm/device_compat.h>
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");
- 在同目录下的
Makefile添加
obj-$(CONFIG_CMD_LEDS) += leds.o
- 在同目录
Kconfig文件下添加,在紧跟 cmd_gpio 添加即可
config CMD_LEDS
bool "hc595 leds"
help
leds support.
然后通过 menuconfig 添加即可
如果使用 obj-y += leds.o 则无需修改 Kconfig
执行
在 uboot 中,使用 leds 222 命令,即可执行,数值根据需要修改

923

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



