您的当前位置:首页通用接口(1)——GPIO控制

通用接口(1)——GPIO控制

2023-07-19 来源:乌哈旅游

sys文件系统

涉及文件:

kernel/include/dt-bindings/pinctrl/rockchip.h			//引脚pinctrl相关宏定义

选择GPIO4_C6引脚,对该IO引脚进行控制。该引脚在OK3399上做edp屏的pwm背光,不过只在选择主或副屏选择edp显示的情况下才做pwm,默认情况下是做GPIO。
原理图接口如下:

[root@rk3399:/]# echo 150 > /sys/class/gpio/export 		//导出GPIO4_C6引脚
[root@rk3399:/]# echo out > /sys/class/gpio/gpio150/direction	//设置IO输出方向
[root@rk3399:/]# echo 0 > /sys/class/gpio/gpio150/value 	//设置IO输出为低电平
[root@rk3399:/]# echo 1 > /sys/class/gpio/gpio150/value	//设置IO输出为高电平

GPIO4_C6引脚gpio号为150的计算方法:

A-D对应1-4:A-1,B-2,C-3,D-4。
GPIOn_xy =n*32+(x-1)*8+y
GPIO4_C6=4*32+(3-1)*8+6=150

设备节点

设备树添加对应的设备节点

        gpio-inputs {
                compatible = "gpio-input";
                pinctrl-names = "default";
                pinctrl-0 = <&dis_gpio>;
                status = "okay";

                di0 {
                        label = "di0";
                        gpios = <&gpio2 2 GPIO_ACTIVE_HIGH>;
                        debounce_interval = <20>;
                };

                di1 {
                        label = "di1";
                        gpios = <&gpio2 3 GPIO_ACTIVE_LOW>;
                        debounce_interval = <20>;
                };
        };

添加对应的驱动

kernel/drivers/input/gpio_input.c

static const struct of_device_id gpio_input_of_match[] = {
        { .compatible = "gpio-input", },
        { },
};
MODULE_DEVICE_TABLE(of, gpio_input_of_match);

编写测试app

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

#define BUFFER_SIZE 1024

int main(int argc,char *argv[])
{
       int fd, ret;
       char* dev  = argv[1];
       char buffer[BUFFER_SIZE];

       fd = open(dev, O_RDWR);
       if(fd < 0){
               perror("open");
               return 1;
       }

       ret = read(fd, buffer, BUFFER_SIZE);
       if(fd < 0){
               perror("read");
               return 1;
       }

       printf("value = %d\n", buffer[0]);
       return 0;
}

uboot 接口函数

dm_gpio_is_valid
dm_gpio_set_value
gpio_request_by_name

gpio_request
gpio_direction_input
gpio_get_value
引脚
GPIO0_B1/PMUIO2_VOLSEL_d		V30_GPIO0_B1

#define LED_CONTROL_GPIO 9 /*GPIO0B1*/
	gpio_request(LED_CONTROL_GPIO, "led_control");
	gpio_direction_output(LED_CONTROL_GPIO, 0);
	gpio_set_value(LED_CONTROL_GPIO, 0);

char VENA_PWR_EN[4] = "io";
gpio_request(57,  VENA_PWR_EN);
gpio_direction_output(57, 0);
gpio_free(57);
struct udevice *dev
ret = gpio_request_by_name(dev, "enable-gpios", 0, &priv->enable_gpio, GPIOD_IS_OUT);
if (dm_gpio_is_valid(&priv->reset_gpio))
	dm_gpio_set_value(&priv->reset_gpio, 1);

电源域

LCD_EN		AJ3_GPIO4_D5
writel(0xb000b, 0xff77e640);		//设置gpio1830、audio、bt656电源域工作电压为1.8V
0xb000b : 1011 0000 0000 0000 1011

将引脚配置到与硬件相符的电源域。

Possible supplies for rk3399:
- bt656-supply:  The supply connected to APIO2_VDD.
- audio-supply:  The supply connected to APIO5_VDD.
- sdmmc-supply:  The supply connected to SDMMC0_VDD.
- gpio1830       The supply connected to APIO4_VDD.

Possible supplies for rk3399 pmu-domains:
- pmu1830-supply:The supply connected to PMUIO2_VDD.

因篇幅问题不能全部显示,请点此查看更多更全内容