Linux gpio口使用方法
使用gpio
接口需要包含#include <linux/gpio.h>
,在驱动中使用延时函数mdelay
,需要包含#include <linux/delay.h>
,Documentation/gpio.txt
文件有作详细说明。
判断一个IO
是否合法:
int gpio_is_valid(int number);
设置gpio
的方向,如果是输出同时设置电平:
/* set as input or output, returning 0 or negative errno */ int gpio_direction_input(unsigned gpio); int gpio_direction_output(unsigned gpio, int value);
获取输入引脚的电平:
/* GPIO INPUT: return zero or nonzero */ int gpio_get_value(unsigned gpio); /* GPIO OUTPUT */ void gpio_set_value(unsigned gpio, int value); int gpio_cansleep(unsigned gpio); /* GPIO INPUT: return zero or nonzero, might sleep */ int gpio_get_value_cansleep(unsigned gpio); /* GPIO OUTPUT, might sleep */ void gpio_set_value_cansleep(unsigned gpio, int value);
获取一个gpio
并声明标签:
/* request GPIO, returning 0 or negative errno. * non-null labels may be useful for diagnostics. */ int gpio_request(unsigned gpio, const char *label); /* release previously-claimed GPIO */ void gpio_free(unsigned gpio);
将gpio
映射为IRQ
中断:
/* map GPIO numbers to IRQ numbers */ int gpio_to_irq(unsigned gpio); /* map IRQ numbers to GPIO numbers (avoid using this) */ int irq_to_gpio(unsigned irq);
设置gpio
的IRQ
中断类型:
if (!sw->both_edges) { if (gpio_get_value(sw->gpio)) { set_irq_type(gpio_to_irq(sw->gpio), IRQ_TYPE_EDGE_FALLING); } else { set_irq_type(gpio_to_irq(sw->gpio), IRQ_TYPE_EDGE_RISING); } }
参考文章
相关推荐
ITlover00 2011-03-29
nuligannima 2012-01-08
supjia 2020-04-19
hazing 2020-02-19
随心而作 2020-01-07
tianyayi 2020-01-05
绿豆饼 2019-12-09
linuxisperfect 2010-08-15
不断攀登的梦想 2011-08-18
yangxiang 2011-06-04
downtown 2011-06-04
ice00 2011-05-31
咏月东南 2019-11-04
mattraynor 2019-10-29
mattraynor 2019-10-23
ShadowBurn 2011-11-04
wangz 2013-05-13
tianyayi 2016-04-09
wangdamingll 2014-12-03