15AH, San Francisco

California, United States.

Send Your Mail At:

tianyingkejishe@sina.cn

Working Hours

Mon-Sat: 9.30am To 7.00pm

分类标题

Autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et dolore feugait.

分类归档 UBOOT

【uboot】通过uboot指令实现GPIO控制

一:在uboot下的defconfig 打开如下配置

CONFIG_DM=y
CONFIG_DM_GPIO=y
CONFIG_DWAPB_GPIO=y
CONFIG_CMD_GPIO=y

二:重新编译u-boot后会生成cmd:gpio

(板子上电时连续按回车键)进入到板端uboot cmdline下执行” gpio status -a ” 查看板端对应的gpio numbe

三:利用 uboot gpio 命令操作GPIO 做测试

  • gpio c 0 ; 将第0根PIN清零(拉低)
  • gpio s 0 ; 将第0根PIN设为output同时拉高

四:gpio 操作demo

直接以下demo code添加到uboot/cmd路径下,再在uboot/cmd/Makefile中添加编译选项,编译完成后可以直接操作gpio

#include <command.h>
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <asm/gpio.h>

int do_gpio_test(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
{
    if (argc < 2) {
        printf("usage: gpio_test [requ/out/on/off]\n");
        return 0;
    }

    if (strcmp("requ", argv[1]) == 0) {
        gpio_request(126, "ir_a");
        gpio_request(127, "ir_b");
        mdelay(10);
    } else if (strcmp("out", argv[1]) == 0) {
        gpio_direction_output(126, 1);
        gpio_direction_output(127, 1);
        mdelay(10);
    } else if (strcmp("on", argv[1]) == 0) {
        gpio_set_value(126, 0);
        gpio_set_value(127, 1);
        mdelay(100);
        gpio_set_value(126, 1);
        gpio_set_value(127, 1);
    } else if (strcmp("off", argv[1]) == 0) {
        gpio_set_value(126, 1);
        gpio_set_value(127, 0);
        mdelay(100);
        gpio_set_value(126, 1);
        gpio_set_value(127, 1);
    }

    return 0;
}

U_BOOT_CMD(
    gpio_test, 4, 1, do_gpio_test,
    "u-boot gpio cmd test",
    "gpio - just for test\n"
);

五、测试

编译完成升级后,进入到uboot会有gpio_test命令
gpio_test requ
初始化gpio
gpio_test out
设置gpio direction
gpio_test on
设置 ir_cur (的两根PIN) 状态