我正在使用zynq-microzed板卡,我想通过kernel space.访问GPIO
有没有人能告诉我怎么才能做到这一点?
发布于 2015-07-16 20:12:37
*注:这来自Zynq-7000。我相信它们大体上是一样的。
假设你使用的是一个devicetree,这是一个示例条目(在devicetree中):
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>>将在这个答案中告诉你所有的事情,甚至更多。
发布于 2017-01-17 23:53:26
检查以下链接: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);致以问候。
https://stackoverflow.com/questions/24178484
复制相似问题