MY-I.MX6 评估板 开发指导(GPIO)

来自明远智睿的wiki
Admin讨论 | 贡献2017年5月26日 (五) 10:04的版本

跳转至: 导航搜索

GPIO输出和输入(GPIO output & input)

GPIO输入输出以SD1_CLK管脚为例。
GPIO output & input take SD1_CLK leads as an example。

修改.h文件(modification “.h” file)

1)检查头文件中对SD1_CLK的配置
check configuration for SD1_CLK in file)
示例如下(example):
$ sed -n "/PAD_SD1_CLK/p" arch/arm/mach-mx6/board-mx6q_sabresd.h

2)注释头文件中对SD1_CLK的配置
configuration for SD1_CLK in comment header file
示例如下(example):
$ sed -i "s/MX6Q_PAD_SD1_CLK/\/\/ &/g" arch/arm/mach-mx6/board-mx6q_sabresd.h

3)在mx6q_sabresd_pads中添加对SD1_CLK的配置
adding configuration for SD1_CLK in mx6q_sabresd_pads
配置SD1_CLK功能为MX6Q_PAD_SD1_CLK__GPIO_1_20
configure SD1_CLK function as MX6Q_PAD_SD1_CLK__GPIO_1_20
示例如下(example):
$ sed -i "/mx6q_sabresd_pads/a\\t MX6Q_PAD_SD1_CLK__GPIO_1_20," \
arch/arm/mach-mx6/board-mx6q_sabresd.h

4)查看修改结果
check modified result

示例如下(example):
$ sed -n "/PAD_SD1_CLK/p" arch/arm/mach-mx6/board-mx6q_sabresd.h

配置内核(configure kernel)

菜单定位(mamu position)
Device Drivers ---> -
- GPIO Support ---> [*] /sys/class/gpio/... (sysfs interface)
My imx6 gpio 1.2.0.1.jpg

编译(compilation)

make uImage

重新烧写系统并启动(re-program system and boot)

设备操作(device operation)

查看gpio目录(check out gpio directory)

在/sys/class/gpio/目录下可以看到已经导出的GPIO
GPIO led out can bee seen under /sys/class/gpio/ directory
$ ls /sys/class/gpio/

  My imx6 gpio 1.5.1.1.jpg

向系统注册GPIO(register GPIO to system)

  使用echo注册GPIO_1_20到系统,即可看到gpio目录下多出一个gpio20目录。
  register GPIO_1_20 to system with echo,then an extra gpio20 directory can be found under gpio directory。
  提示:I.MX6 GPIO的值计算方法是(所在的组 - 1) * 32 + 序号,所以GPIO_1_20的管脚值为 (1 – 1) * 32 + 20 = 20。
  tips:I.MX6 GPIO value is calculation this way(where grouped - 1) * 32 + order number,so lead number of GPIO_1_20 is(1 – 1) * 32 + 20 = 20。
$ echo 20 > /sys/class/gpio/export

  My imx6 gpio 1.5.2.1.jpg

GPIO输出控制(GPIO output control)

  • 设置管脚为输出状态

set leads as output state
$ echo out > /sys/class/gpio/gpio20/direction

  • 设置管脚为输出为高电平

set lead output at high electrical level
echo 1 > /sys/class/gpio/gpio20/value

  • 设置管脚为输出为低电平

set lead output at low electrical level
echo 0 > /sys/class/gpio/gpio20/value

GPIO输入设置(set lead as input state)

  • 设置管脚为输入状态

set lead as input state
echo in > /sys/class/gpio/gpio20/direction

  • 获取输入值

gain lead value
cat /sys/class/gpio/gpio20/value

注销GPIO接口(logout GPIO interface)

  • 注销GPIO_1_20

logout GPIO_1_20
echo 20 > /sys/class/gpio/unexport

= GPIO LED =

说明:GPIO LED以PAD NANDF_CS0为例。
instruction:take GPIO LED以PAD NANDF_CS0 as example。

进入内核目录(enter kernel directory)

$ cd ~/MY-I.MX6/linux-3.0.35/
My imx6 gpio 2.1.0.1.jpg

添加代码(add code)

修改.h文件(modification.h file)

1)查找头文件中对NANDF_CS0的配置
check out configuration for NANDF_CS0 in header file.
示例如下(example):
$ sed -n "/PAD_NANDF_CS0/p" arch/arm/mach-mx6/board-mx6q_sabresd.h

2)注释头文件中对NANDF_CS0的配置
comment configuration for NANDF_CS0 in header file.
示例如下(example):
$ sed -i "s/MX6Q_PAD_NANDF_CS0/\/\/ &/g" arch/arm/mach-mx6/board-mx6q_sabresd.h

3)在mx6q_sabresd_pads中添加对NANDF_CS0的配置
add configuration for NANDF_CS0 in mx6q_sabresd_pads.
配置NANDF_CS0功能为MX6Q_PAD_NANDF_CS0__GPIO_6_11
configure NANDF_CS0 function as MX6Q_PAD_NANDF_CS0__GPIO_6_11.
示例如下(example):
$ sed -i "/mx6q_sabresd_pads/a\\tMX6Q_PAD_NANDF_CS0__GPIO_6_11," arch/arm/mach-mx6/board-mx6q_sabresd.h

4)查看修改结果
check out modified result.
示例如下(example):
$ sed -n "/PAD_NANDF_CS0/p" arch/arm/mach-mx6/board-mx6q_sabresd.h

修改.c文件(modification.c file)

1)检查.c文件中GPIO是否被重复使用
check out whether GPIO is used repeatedly in .c file
示例如下(example):
$ sed -n '/IMX_GPIO_NR(6,.11)/p' arch/arm/mach-mx6/board-mx6q_sabresd.c

2)在.c文件中增加GPIO的宏定义
add macro definition of GPIOin .c file
增加#define SABRESD_USER_LED IMX_GPIO_NR(6, 11)
add #define SABRESD_USER_LED IMX_GPIO_NR(6, 11)
示例如下(example):
$ GPIO_DEFINE="#define SABRESD_USER_LED\tIMX_GPIO_NR(6, 11)"
$ sed -i "1,/#define/{/#define/s/#define/${GPIO_DEFINE}\n&/}" arch/arm/mach-mx6/board-mx6q_sabresd.c

2)检查gpio_led类型的数据定义
check out data definition with gpio_led
示例如下(example):
$ sed -n '/struct gpio_led /,/}/p' arch/arm/mach-mx6/board-mx6q_sabresd.c

3)增加user_led的数据定义
add data definition of user_led
static struct gpio_led imx6q_gpio_leds[] = {
    GPIO_LED(SABRESD_USER_LED, "user_led", 0, 1,
        "charger"),
};

内核配置(kernel configuration)

菜单定位(manu position)
Device Drivers ---> [
] LED Support ---> <*> LED Support for GPIO connected LEDs
My imx6 gpio 2.3.0.1.jpg

编译(compilation)

make uImage

重新烧写系统并启动(re-program system and boot)

GPIO_LED操作(GPIO_LED operation)

设备文件目录(device file directory)

启动系统后即存在/sys/class/leds/user_led目录,使用ls即可看到
to be saved under /sys/class/leds/user_led directory after booting system,which can be seen with ls.
$ ls /sys/class/leds/user_led

  My imx6 gpio 2.6.1.1.jpg

操作示例(operating example)

1)置NANDF_CS0为1(set NANDF_CS0 as 1)
$ echo 1 > /sys/class/leds/user_led/brightness
2)置NANDF_CS0为0(set NANDF_CS0 as 0)
$ echo 0 > /sys/class/leds/user_led/brightness

= GPIO按键(GPIO key) =

GPIO按键以GPIO_4、EIM_BCLK、CSI0_DATA_EN为例,分别是原理图的按键SW2,SW3和SW4。
GPIO key by taking GPIO_4、EIM_BCLK、CSI0_DATA_EN as example,are separetely SW2,SW3和SW4 in schematic。

修改.h文件(modification.h file)

1)查找头文件中对PAD的配置
check out configuration for PAD in header file.
示例如下(example):
$ sed -n "/PAD_GPIO_4/p" arch/arm/mach-mx6/board-mx6q_sabresd.h
$ sed -n "/PAD_EIM_BCLK/p" arch/arm/mach-mx6/board-mx6q_sabresd.h
$ sed -n "/PAD_CSI0_DATA_EN/p" arch/arm/mach-mx6/board-mx6q_sabresd.h

2)注释头文件中对PAD的配置
comment configuration for PAD in header file.
示例如下(example):
$ sed -i "s/MX6Q_PAD_GPIO_4/\/\/ &/g" arch/arm/mach-mx6/board-mx6q_sabresd.h
$ sed -i "s/MX6Q_PAD_EIM_BCLK/\/\/ &/g" arch/arm/mach-mx6/board-mx6q_sabresd.h
$ sed -i "s/MX6Q_PAD_CSI0_DATA_EN/\/\/ &/g" arch/arm/mach-mx6/board-mx6q_sabresd.h

3)在mx6q_sabresd_pads中添加对PAD的配置
add configuration for PAD in mx6q_sabresd_pads.
配置GPIO_4功能为MX6Q_PAD_GPIO_4__GPIO_1_4
configure GPIO_4 function as MX6Q_PAD_GPIO_4__GPIO_1_4.
示例如下(example):
$ sed -i "/mx6q_sabresd_pads/a\\tMX6Q_PAD_GPIO_4__GPIO_1_4," arch/arm/mach-mx6/board-mx6q_sabresd.h
配置EIM_BCLK功能为MX6Q_PAD_EIM_BCLK__GPIO_6_31
configure EIM_BCLK function as MX6Q_PAD_EIM_BCLK__GPIO_6_31.
示例如下(example):
$ sed -i "/mx6q_sabresd_pads/a\\tMX6Q_PAD_EIM_BCLK__GPIO_6_31," arch/arm/mach-mx6/board-mx6q_sabresd.h
配置CSI0_DATA_EN的PAD功能为MX6Q_PAD_CSI0_DATA_EN__GPIO_5_20
configrue PAD function of CSI0_DATA_EN as MX6Q_PAD_CSI0_DATA_EN__GPIO_5_20.
示例如下:
$ sed -i "/mx6q_sabresd_pads/a\\tMX6Q_PAD_CSI0_DATA_EN__GPIO_5_20," arch/arm/mach-mx6/board-mx6q_sabresd.h

4)查看修改结果
check out modified result
示例如下(example):
$ sed -n "/PAD_GPIO_4/p" arch/arm/mach-mx6/board-mx6q_sabresd.h
$ sed -n "/PAD_EIM_BCLK/p" arch/arm/mach-mx6/board-mx6q_sabresd.h
$ sed -n "/PAD_CSI0_DATA_EN/p" arch/arm/mach-mx6/board-mx6q_sabresd.h

修改.c文件(modification.c file)

1)检查.c文件中GPIO是否被重复使用
check out whether GPIO is used repeatedly in .c file.
示例如下(example):
$ sed -n '/IMX_GPIO_NR(1,.4)/p' arch/arm/mach-mx6/board-mx6q_sabresd.c
$ sed -n '/IMX_GPIO_NR(6,.31)/p' arch/arm/mach-mx6/board-mx6q_sabresd.c
$ sed -n '/IMX_GPIO_NR(5,.20)/p' arch/arm/mach-mx6/board-mx6q_sabresd.c

2)在.c文件中增加GPIO的宏定义
add macro definition of GPIO in .c file.

  • MX6Q_PAD_GPIO_4__GPIO_1_4

在.c文件中添加#define SABRESD_POWER_OFF IMX_GPIO_NR(1, 4)
in .c file add #define SABRESD_POWER_OFF IMX_GPIO_NR(1, 4))
示例如下:
$ GPIO_DEFINE="#define SABRESD_POWER_OFF\tIMX_GPIO_NR(1, 4)"
$ sed -i "1,/#define/{/#define/s/#define/${GPIO_DEFINE}\n&/}" arch/arm/mach-mx6/board-mx6q_sabresd.c

  • MX6Q_PAD_CSI0_DATA_EN__GPIO_5_20

在.c文件中添加#define SABRESD_VOLUME_UP IMX_GPIO_NR(6, 31)
in .c file add #define SABRESD_VOLUME_UP IMX_GPIO_NR(6, 31))
示例如下(example):
$ GPIO_DEFINE="#define SABRESD_VOLUME_UP\tIMX_GPIO_NR(6, 31)"
$ sed -i "1,/#define/{/#define/s/#define/${GPIO_DEFINE}\n&/}" arch/arm/mach-mx6/board-mx6q_sabresd.c

  • MX6Q_PAD_CSI0_DATA_EN__GPIO_5_20

在.c文件中添加#define SABRESD_VOLUME_DN IMX_GPIO_NR(5, 20)
in .c file add #define SABRESD_VOLUME_DN IMX_GPIO_NR(5, 20))
示例如下(example):
$ GPIO_DEFINE="#define SABRESD_VOLUME_DN\tIMX_GPIO_NR(5, 20)"
$ sed -i "1,/#define/{/#define/s/#define/${GPIO_DEFINE}\n&/}" arch/arm/mach-mx6/board-mx6q_sabresd.c

3)检查gpio_keys_button类型的数据定义
chech out data definition with gpio_keys_button.
示例如下(example):
$ sed -n '/gpio_keys_button/,/}/p' arch/arm/mach-mx6/board-mx6q_sabresd.c

4)增加volume-up、volume-down、power的数据定义
add data definition for volume-up、volume-down、power.
static struct gpio_keys_button imx6q_buttons[] = {
GPIO_BUTTON(SABRESD_VOLUME_UP, KEY_VOLUMEUP, 1, "volume-up", 0, 1),
GPIO_BUTTON(SABRESD_VOLUME_DN, KEY_VOLUMEDOWN, 1, "volume-down", 0, 1),
GPIO_BUTTON(SABRESD_POWER_OFF, KEY_POWER, 1, "power", 1, 1),
};

内核配置(kernel configuration)

菜单定位(manu position):
Device Drivers ---> Input device support ---> [
] Keyboards ---> <*> GPIO Buttons
My imx6 gpio 3.2.0.1.jpg

编译(compilation)

make uImage

重新烧写系统并启动(re-program system and boot)

GPIO按键操作(GPIO key operation)

GPIO_KEYS设备(GPIO_KEYS device)

启动系统后即存在/sys/devices/platform/gpio-keys目录,使用ls即可看到
to be saved under /sys/devices/platform/gpio-keys directory after booting system,which can be seen with ls.

  My imx6 gpio 3.5.1.1.jpg

$ ls /sys/devices/platform/gpio-keys

查看设备信息(check out device information)

在proc文件系统下可以看到相关信息
relatie information can be found in proc file system.
$ cat /proc/bus/input/devices

  My imx6 gpio 3.5.2.1.jpg

设备文件(device file)

event0即对应gpio按键对应的设备文件,使用ls可以查看:
event0 is matched up with device file corresponding to gpio key,check out with ls.

  My imx6 gpio 3.5.3.1.jpg

$ ls /dev/input/event0 –la

操作示例operating example)

见Linux测试手册
refer to Linux test manual.