文章目录
1、gpio1节点
-
GPIO1控制器的寄存器基地址
-
类似的还有gpio2~5
imx6ull.dtsi
gpio1: gpio@209c000 {
// 用于和gpio子系统平台驱动做匹配,对应的驱动文件为 drivers/gpio/gpio-mxc.c
compatible = "fsl,imx6ul-gpio", "fsl,imx35-gpio";
//对照数据手册,gpio1寄存器组的起始地址即为0x209c000,第二个值表示范围
reg = <0x209c000 0x4000>;
//描述中断相关的内容
interrupts = <GIC_SPI 66 IRQ_TYPE_LEVEL_HIGH>,
<GIC_SPI 67 IRQ_TYPE_LEVEL_HIGH>;
//描述初始化外设时钟
clocks = <&clks IMX6UL_CLK_GPIO1>;
//表明gpio1节点是一个gpio控制器
gpio-controller;
//表示要用2个cell描述一个 GPIO引脚
#gpio-cells = <2>;
//表示 gpio1 节点是个中断控制器
interrupt-controller;
//表示要用2个cell描述一个中断
#interrupt-cells = <2>;
//gpio编号转换成pin编号,如gpio子系统的0~9对应pinctrl的23~32号pin
gpio-ranges = <&iomuxc 0 23 10>, <&iomuxc 10 17 6>,
<&iomuxc 16 33 16>;
};

imx6ull-seeed-npi.dts
新增rgb_led节点,使用gpio子系统
rgb_led{
#address-cells = <1>;
#size-cells = <1>;
pinctrl-names = "default";
compatible = "fire,rgb_led";
pinctrl-0 = <&pinctrl_rgb_led>;
// 此属性有两个属性值
rgb_led_red = <&gpio1 4 GPIO_ACTIVE_LOW &gpio1 10 GPIO_ACTIVE_LOW>;
rgb_led_green = <&gpio4 20 GPIO_ACTIVE_LOW>;
rgb_led_blue = <&gpio4 19 GPIO_ACTIVE_LOW>;
status = "okay";
};
- rgb_led_red:自定义属性,&gpio1 4表示GPIO1_IO04,GPIO_ACTIVE_LOW表示低电平有效
2、常用函数
of_find_node_by_path()函数
函数原型:
// path:设备树节点的绝对路径
// 获取指定路径节点的device_node结构体
inline struct device_node *of_find_node_by_path(const char *path)
参数:
- path:设备树节点的绝对路径
返回值:
-
成功:目标节点
-
失败:NULL
of_get_named_gpio()函数
函数原型:
static inline int of_get_named_gpio(struct device_node *np,const char *propname, int index)
参数:
- np:指定的设备树节点
- propname:GPIO属性名,比如上图所示的rgb_led_red 或者 rgb_led_green 或者 rgb_led_blue节点
- index:引脚索引值,若一个属性有多个属性值,index表示选第几个属性值,取 0 表示选第一个属性值
返回值:
- 成功:GPIO编号
- 失败:负数
gpio_request()函数
把指定的gpio引脚添加到pin_ctrl子系统管理,避免不同的外设使用同一个引脚。
函数原型:
static inline int gpio_request(unsigned gpio, const char *label)
参数:
- gpio:要申请的GPIO编号,就是在gpio子系统里面的编号
- label:给 gpio 设置个名字
返回值:
- 成功:0
- 失败:负数
gpio_free()函数
使用完释放到在pin

本文介绍了Linux系统中GPIO驱动的开发,包括设备树配置、GPIO控制器节点解析、常用GPIO函数以及示例程序。通过设备树配置GPIO引脚,利用of_get_named_gpio获取GPIO编号,并通过gpio_request、gpio_direction_output等函数进行GPIO的操作。示例程序展示了如何控制RGB LED的亮灭。
最低0.47元/天 解锁文章
5207





