怎么在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"
);
  1.  编译完成升级后,进入到uboot会有gpio_test命令
  2. gpio_test requ
    1.   初始化gpio
  3. gpio_test out
    1.   设置gpio direction
  4. gpio_test on
    1.   设置 ir_cur (的两根PIN) 状态
posted @ 2022-10-19 15:12  X-Long  阅读(4347)  评论(0)    收藏  举报