Get the App
SLTechnology News&Howtos  ›  Servers  › 

Example of using gpio Interface in Linux

Shulou Source: shulou.com Published: 2022-06-02 02:11:29 09月17日 Update

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.

Tags: Interface content file level program reference learning driver usage method legality inter-value system kernel function preface and at the same time commonly used Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Xiaomi OPPO Reno vpn Microsoft