如何从内核空间访问GPIO? (zynq微型板)

时间:2014-06-12 07:12:18

标签: linux-kernel linux-device-driver zynq

我正在使用zynq-microzed电路板,我希望GPIO访问kernel space.

任何人都可以告诉我如何尝试这样做?

2 个答案:

答案 0 :(得分:1)

*注意:这是来自Zynq-7000。我相信它大致相同。

假设您正在使用设备,这是一个示例条目(在设备中):

gpio-device {
    compatible = "gpio-control";
    gpios = <&gpio0 54 0>; //(Add 32 to get the actual pin number. This is GPIO 86)
};

你需要在驱动程序中说明你与devicetree条目兼容(查看其他驱动程序以查看该行的位置):

.compatible = "gpio-control"

在您的驱动程序中,添加#include <linux/gpio.h>并阅读设备目录中的图钉:

struct device_node *np = pdev->dev.of_node;
int pin;

pin = of_get_gpio(np, 0);
if (pin < 0) {
    pr_err("failed to get GPIO from device tree\n");
    return -1;
}

请求使用GPIO:

int ret = gpio_request(pin, "Some name"); //Name it whatever you want

并确定方向:

int ret = gpio_direction_output(pin, 0); //The second parameter is the initial value. 0 is low, 1 is high.

之后,设置值如下:

gpio_set_value(pin, 1);

输入:

ret = gpio_direction_input(pin);
value = gpio_get_value(pin);

完成后将其释放GPIO(包括出错!):

gpio_free(pin);

在一天结束时,一个好的方法是在内核周围找grep找到你想要的驱动程序。实际上grep -r gpio <<kernel_source>>会告诉你这个答案中的所有内容以及更多内容。

答案 1 :(得分:0)

检查以下链接:enter link description here

汇总:

有一个用于处理GPIO的包含文件:

#include <linux/gpio.h>

GPIO必须在使用前分配:

int gpio_request(unsigned int gpio, const char *label);

GPIO可以通过以下方式返回系统:

void gpio_free(unsigned int gpio);

将GPIO配置为输入/输出:

int gpio_direction_input(unsigned int gpio);
int gpio_direction_output(unsigned int gpio, int value);

操作:

int gpio_get_value(unsigned int gpio);
void gpio_set_value(unsigned int gpio, int value);

问候。