Linux中的gpio口使用方法__Linux

來源:互聯網
上載者:User

Linux中的IO使用方法
應該是新版本核心才有的方法。
請參考:./Documentation/gpio.txt檔案

提供的API:
驅動需要包含 #include <linux/gpio.h>

判斷一個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);

To access such GPIOs, a different set of accessors is defined:

/* 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);

在驅動中使用延時函數mdelay,需要包含<linux/delay.h>檔案。


說明:本文轉自http://www.linuxidc.com/Linux/2012-07/64604.htm

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.