1.硬件原理图
原理图显示两块9434的INT分别接到D2000的GPIO0_A3和GPIO0_A5.
2.配置
2.1 设备树
gpio = <&pio 1 1 1 1 1 0>;| | | | | | | || | | | | | | ------表示有效电平| | | | | | | | | | | | | ---------上下拉, 0关闭功能, 1上拉, 2下拉, 3保留| | | | | || | | | | -------------驱动力,电流等级(0 - 3),级别越高,输出电流越大| | | | || | | | -----------------gpio功能类型,0输入, 1输出, 6和外部中断,7关闭功能(具体查 | | | | 手册)| | | ---------------------pin bank 内偏移(即组内第几个io口).| | || | -------------------------哪组gpio PA(0) PB(1)| || -----------------------------指向哪个gpio控制器, pio / r_pio(PL组)| ------------------------------------属性名字(随便命名)
设备树配置:
example{compatible = "gpio,example";gpios = <&pio PE 8 1 1 1 1>; };
驱动GPIO操作
gpio = of_get_named_gpio_flags(nd, "gpios", 0, (enum of_gpio_flags *)&config);if (!gpio_is_valid(gpio))printk("gpio isn't valid\n");if (gpio_request(gpio, pdev->name) < 0)printk("gpio request failed %d\n", gpio);gpio_direction_output(gpio, 1);
2.2 直接操作GPIO
不用设备树配置,驱动代码中对固定GPIO操作
#define GPIO8_A6 254
ret = gpio_request(GPIO8_A6 , "gpio8_a6");
if (!ret) {
printk("request for gpio8_a6 failed:%d\n", ret);
return 0;
}
gpio_direction_output(GPIO8_A6 ,1);//设置GPIO8_A6为输出功能且输出高电平
2.3 寄存器操作
寄存器配置GPIO功能
驱动GPIO寄存器操作
void __iomem *base_addr;unsigned int reg_value = 0;base_addr = ioremap(GPIO0_REGBASE, 0x32);//GPIO_SWPORTA_DDRreg_value = readl(base_addr + GPIO_SWPORTA_DDR);debug_driver("GPIO_SWPORTA_DDR = 0x%x\n",reg_value);reg_value = reg_value & 0xD7;writel(reg_value, base_addr + GPIO_SWPORTA_DDR);reg_value = readl(base_addr + GPIO_SWPORTA_DDR);debug_driver("GPIO_SWPORTA_DDR = 0x%x\n",reg_value);//GPIO_INTENreg_value = readl(base_addr + GPIO_INTEN);reg_value = reg_value | 0x28;writel(reg_value, base_addr + GPIO_INTEN);reg_value = readl(base_addr + GPIO_INTEN);debug_driver("GPIO_INTEN = 0x%x\n",reg_value);//GPIO_INTMASKreg_value = readl(base_addr + GPIO_INTMASK);reg_value = reg_value & 0xD7;writel(reg_value, base_addr + GPIO_INTMASK);reg_value = readl(base_addr + GPIO_INTMASK);debug_driver("GPIO_INTMASK = 0x%x\n",reg_value);//GPIO_INTTYPE_LEVELreg_value = readl(base_addr + GPIO_INTTYPE_LEVEL);reg_value = reg_value & 0xD7;writel(reg_value, base_addr + GPIO_INTTYPE_LEVEL);reg_value = readl(base_addr + GPIO_INTTYPE_LEVEL);debug_driver("GPIO_INTTYPE_LEVEL = 0x%x\n",reg_value);//GPIO_INT_POLARITYreg_value = readl(base_addr + GPIO_INT_POLARITY);reg_value = reg_value & 0xD7;writel(reg_value, base_addr + GPIO_INT_POLARITY);reg_value = readl(base_addr + GPIO_INT_POLARITY);debug_driver("GPIO_INT_POLARITY = 0x%x\n",reg_value);iounmap(base_addr);
2.4 用户空间操作
用户空间操作/sys/class/gpio目录
/sys/class/gpio/目录下各个文件说明:
/sys/class/gpio/export文件用于通知系统需要导出控制的GPIO引脚编号;
/sys/class/gpio/unexport 用于通知系统取消导出;
/sys/class/gpio/gpioX/direction文件,可以写入in(设置输入方向)或out(设置输出方向);
/sys/class/gpio/gpioX/value文件是可以读写GPIO状态;
/sys/class/gpio/gpiochipX目录保存系统中GPIO寄存器的信息,包括每个寄存器控制引脚的起始编号,寄存器名称,引脚总数;其中X表示具体的引脚编号。