Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example of using gpio Interface in Linux

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Preface

Gpio is the simplest and most commonly used resource driver in the Linux kernel (like interrupt and dma,timer). Applications can use gpio,gpio to identify integers between 0~MAX_INT through the corresponding interface, not negative numbers. Gpio is closely related to the hardware architecture, but linux has a framework to deal with gpio and can use a unified interface to operate gpio. Before we talk about gpio core (gpiolib.c), let's take a look at how gpio is used.

Use gpio

To use the gpio interface, you need to include # include. To use the delay function mdelay in the driver, you need to include the # include file. The Documentation/gpio.txt file is described in detail.

Determine whether an IO is legal:

Int gpio_is_valid (int number)

Set the direction of the GPIO, if it is the output and set the level at the same time:

/ * set as input or output, returning 0 or negative errno * / int gpio_direction_input (unsigned gpio); int gpio_direction_output (unsigned gpio, int value)

Get the level of the input pin:

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

Get a GPIO and declare the tag:

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

Map GPIO to IRQ interrupts:

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

Set the IRQ interrupt type for GPIO:

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);}}

Reference article

How to use gpio Port in Linux

Summary

The above is the whole content of this article, I hope that the content of this article has a certain reference and learning value for your study or work, if you have any questions, you can leave a message and exchange, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report